Skip to content

Commit 3ac8333

Browse files
committed
feat(ollama): add reasoning effort selectors and gate on Enable Thinking
- Chat bar and settings page share option computation via reasoning-effort.ts; both hide when Enable Thinking is unticked - Ollama settings now renders its own thinking checkbox + effort dropdown; generic ThinkingBudget is skipped for it - Fetcher advertises low/medium/high/max for thinking-capable models - Handler sends think: "max" verbatim (previously clamped to "high") and maps "none" to think: false so the None option disables thinking
1 parent afdede5 commit 3ac8333

16 files changed

Lines changed: 1342 additions & 113 deletions

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,13 @@ describe("NativeOllamaHandler", () => {
319319

320320
it("should map reasoningEffort levels to Ollama think values", async () => {
321321
const cases: Array<
322-
[NonNullable<ApiHandlerOptions["reasoningEffort"]>, boolean | "high" | "medium" | "low"]
322+
[NonNullable<ApiHandlerOptions["reasoningEffort"]>, boolean | "high" | "medium" | "low" | "max"]
323323
> = [
324324
["low", "low"],
325325
["medium", "medium"],
326326
["high", "high"],
327327
["xhigh", "high"],
328-
["max", "high"],
329-
["none", true],
328+
["max", "max"],
330329
["minimal", true],
331330
["disable", false],
332331
]
@@ -362,6 +361,36 @@ describe("NativeOllamaHandler", () => {
362361
}
363362
})
364363

364+
it("should send think: false when reasoningEffort is 'none'", async () => {
365+
// Ollama has no native "none" thinking level; the "None" option in
366+
// the selector means thinking disabled, so it maps to think: false
367+
// rather than enabling thinking with the default budget.
368+
const options: ApiHandlerOptions = {
369+
apiModelId: "qwen3",
370+
ollamaModelId: "qwen3",
371+
ollamaBaseUrl: "http://localhost:11434",
372+
enableReasoningEffort: true,
373+
reasoningEffort: "none",
374+
}
375+
376+
handler = new NativeOllamaHandler(options)
377+
378+
mockChat.mockImplementation(async function* () {
379+
yield { message: { content: "ok" } }
380+
})
381+
382+
const stream = handler.createMessage("System", [{ role: "user" as const, content: "Hi" }])
383+
for await (const _ of stream) {
384+
// consume
385+
}
386+
387+
expect(mockChat).toHaveBeenCalledWith(
388+
expect.objectContaining({
389+
think: false,
390+
}),
391+
)
392+
})
393+
365394
it("should not send think parameter when reasoningEffort is undefined", async () => {
366395
mockChat.mockImplementation(async function* () {
367396
yield { message: { content: "ok" } }
@@ -782,6 +811,34 @@ describe("NativeOllamaHandler", () => {
782811
)
783812
})
784813

814+
it("should send think: max in completePrompt when reasoningEffort is max", async () => {
815+
// The Ollama API accepts "max" as the highest thinking level; the SDK
816+
// passes it through verbatim. Selectors surface "max" for models that
817+
// advertise it (e.g. qwen3 on Ollama Cloud), so the single-shot path
818+
// must honor it rather than clamping to "high".
819+
const options: ApiHandlerOptions = {
820+
apiModelId: "qwen3",
821+
ollamaModelId: "qwen3",
822+
ollamaBaseUrl: "http://localhost:11434",
823+
enableReasoningEffort: true,
824+
reasoningEffort: "max",
825+
}
826+
827+
handler = new NativeOllamaHandler(options)
828+
829+
mockChat.mockResolvedValue({
830+
message: { content: "Response" },
831+
})
832+
833+
await handler.completePrompt("Test prompt")
834+
835+
expect(mockChat).toHaveBeenCalledWith(
836+
expect.objectContaining({
837+
think: "max",
838+
}),
839+
)
840+
})
841+
785842
it("should not send think parameter in completePrompt when reasoningEffort is undefined", async () => {
786843
mockChat.mockResolvedValue({
787844
message: { content: "Response" },

‎src/api/providers/fetchers/__tests__/ollama.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,34 @@ describe("Ollama Fetcher", () => {
114114
expect(parsedModel!.supportsImages).toBe(true)
115115
expect(parsedModel!.contextWindow).toBeGreaterThan(0)
116116
})
117+
118+
it("should advertise native reasoning effort for models with the 'thinking' capability", () => {
119+
const modelDataWithThinking = {
120+
...ollamaModelsData["qwen3-2to16:latest"],
121+
capabilities: ["completion", "tools", "thinking"],
122+
}
123+
124+
const parsedModel = parseOllamaModel(modelDataWithThinking as Parameters<typeof parseOllamaModel>[0])
125+
126+
expect(parsedModel).not.toBeNull()
127+
// Ollama Cloud accepts low/medium/high/max and rejects "xhigh", so the
128+
// selector must surface exactly those native effort levels.
129+
expect(parsedModel!.supportsReasoningEffort).toEqual(["low", "medium", "high", "max"])
130+
expect(parsedModel!.reasoningEffort).toBe("medium")
131+
})
132+
133+
it("should not advertise reasoning effort when the 'thinking' capability is absent", () => {
134+
const modelDataWithoutThinking = {
135+
...ollamaModelsData["qwen3-2to16:latest"],
136+
capabilities: ["completion", "tools"],
137+
}
138+
139+
const parsedModel = parseOllamaModel(modelDataWithoutThinking as Parameters<typeof parseOllamaModel>[0])
140+
141+
expect(parsedModel).not.toBeNull()
142+
expect(parsedModel!.supportsReasoningEffort).toBeUndefined()
143+
expect(parsedModel!.reasoningEffort).toBeUndefined()
144+
})
117145
})
118146

119147
describe("getOllamaModels", () => {

‎src/api/providers/fetchers/ollama.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ export const parseOllamaModel = (rawModel: OllamaModelInfoResponse): ModelInfo |
5959
// Inherit the sane default (4096) from ollamaDefaultModelInfo instead.
6060
})
6161

62+
// Models that advertise the "thinking" capability expose a native
63+
// reasoning-effort control. Ollama Cloud accepts low/medium/high/max and
64+
// rejects "xhigh", so advertise exactly those values (matching the user's
65+
// API verification) so the reasoning selector shows the model's real options
66+
// instead of falling back to the generic low/medium/high defaults.
67+
if (rawModel.capabilities?.includes("thinking")) {
68+
modelInfo.supportsReasoningEffort = ["low", "medium", "high", "max"]
69+
modelInfo.reasoningEffort = "medium"
70+
}
71+
6272
return modelInfo
6373
}
6474

‎src/api/providers/native-ollama.ts‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ interface OllamaChatOptions {
1414
num_ctx?: number
1515
}
1616

17+
// The installed ollama SDK (v0.6.x) types the `think` request field as
18+
// `boolean | "high" | "medium" | "low"`, but the Ollama API also accepts
19+
// `"max"` (see https://docs.ollama.com/capabilities/thinking). The runtime
20+
// serializes the field verbatim, so `getOllamaThinkParam` returns the wider
21+
// union and the `client.chat` call sites cast down to the SDK's narrower
22+
// type. Remove those casts once the SDK types catch up.
23+
export type OllamaThinkParam = boolean | "high" | "medium" | "low" | "max" | undefined
24+
1725
// Narrow local types for non-Anthropic content blocks that may be carried in
1826
// the conversation history. The Anthropic SDK union does not include the
1927
// custom `reasoning` block (used by non-Anthropic protocols) or the
@@ -311,7 +319,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
311319

312320
/**
313321
* Maps the configured reasoning effort setting to Ollama's native `think`
314-
* request parameter (boolean | "high" | "medium" | "low").
322+
* request parameter (boolean | "high" | "medium" | "low" | "max").
315323
*
316324
* Requires an explicit Ollama opt-in (`enableReasoningEffort === true`)
317325
* before translating `reasoningEffort`. This prevents inherited
@@ -323,19 +331,16 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
323331
* the model/Modelfile in control (preserving prior behavior where models
324332
* that emit think/thought tags in content are still handled by TagMatcher).
325333
*
326-
* Note: The Ollama API itself also accepts `"max"` (see
327-
* https://docs.ollama.com/capabilities/thinking), but the installed
328-
* `ollama` SDK (v0.6.x) only types `think` as
329-
* `boolean | "high" | "medium" | "low"`. Until the SDK types catch up,
330-
* "xhigh"/"max" efforts are clamped to "high".
331-
*
332334
* - enableReasoningEffort !== true -> undefined (no think param sent)
333335
* - "disable" -> false (thinking off)
334-
* - "none" / "minimal" -> true (enable thinking with default budget)
335-
* - "low" / "medium" / "high" -> the matching effort level
336-
* - "xhigh" / "max" -> "high" (highest level the SDK currently supports)
336+
* - "none" -> false (thinking off; Ollama has no native "none" level, and
337+
* the "None" selector label means thinking disabled)
338+
* - "minimal" -> true (enable thinking with default budget)
339+
* - "low" / "medium" / "high" / "max" -> the matching effort level
340+
* - "xhigh" -> "high" (highest level the Ollama API accepts; see
341+
* https://docs.ollama.com/capabilities/thinking)
337342
*/
338-
private getOllamaThinkParam(): boolean | "high" | "medium" | "low" | undefined {
343+
private getOllamaThinkParam(): OllamaThinkParam {
339344
// Require an explicit Ollama opt-in before mapping reasoningEffort.
340345
// Without this guard, a stale reasoningEffort inherited from another
341346
// provider config could still emit a think param when the UI checkbox
@@ -351,8 +356,8 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
351356

352357
switch (effort) {
353358
case "disable":
354-
return false
355359
case "none":
360+
return false
356361
case "minimal":
357362
return true
358363
case "low":
@@ -362,7 +367,9 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
362367
case "high":
363368
case "xhigh":
364369
case "max":
365-
return "high"
370+
// "max" is accepted verbatim by the Ollama API; "xhigh" (used by
371+
// some third-party model catalogs) clamps to "high".
372+
return effort === "max" ? "max" : "high"
366373
default:
367374
return undefined
368375
}
@@ -377,9 +384,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
377384
* Returns a tuple of `[chatOptions, thinkParam]` where `thinkParam` is
378385
* `undefined` when no `think` field should be sent to Ollama.
379386
*/
380-
private buildChatRequestOptions(
381-
useR1Format: boolean,
382-
): [OllamaChatOptions, boolean | "high" | "medium" | "low" | undefined] {
387+
private buildChatRequestOptions(useR1Format: boolean): [OllamaChatOptions, OllamaThinkParam] {
383388
const chatOptions: OllamaChatOptions = {
384389
temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0),
385390
}
@@ -427,14 +432,16 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
427432
// Create the actual API request promise. The `stream: true` literal
428433
// is kept inline so TypeScript selects the streaming overload of
429434
// client.chat. The `think` parameter is spread conditionally to
430-
// avoid sending an explicit `think: undefined` to the runtime.
435+
// avoid sending an explicit `think: undefined` to the runtime. The
436+
// cast narrows our wider OllamaThinkParam (which includes "max",
437+
// accepted by the API) to the SDK's stale union type.
431438
const stream = await client.chat({
432439
model: modelId,
433440
messages: ollamaMessages,
434441
stream: true,
435442
options: chatOptions,
436443
tools: this.convertToolsToOllama(metadata?.tools),
437-
...(thinkParam !== undefined ? { think: thinkParam } : {}),
444+
...(thinkParam !== undefined ? { think: thinkParam as boolean | "high" | "medium" | "low" } : {}),
438445
})
439446

440447
let totalInputTokens = 0
@@ -566,7 +573,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
566573
messages: [{ role: "user", content: prompt }],
567574
stream: false,
568575
options: chatOptions,
569-
...(thinkParam !== undefined ? { think: thinkParam } : {}),
576+
...(thinkParam !== undefined ? { think: thinkParam as boolean | "high" | "medium" | "low" } : {}),
570577
})
571578

572579
return response.message?.content || ""

‎webview-ui/src/components/chat/ChatTextArea.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { StandardTooltip } from "@src/components/ui"
2727
import Thumbnails from "../common/Thumbnails"
2828
import { ModeSelector } from "./ModeSelector"
2929
import { ApiConfigSelector } from "./ApiConfigSelector"
30+
import { ReasoningEffortSelector } from "./ReasoningEffortSelector"
3031
import { AutoApproveDropdown } from "./AutoApproveDropdown"
3132
import { MAX_IMAGES_PER_MESSAGE } from "./ChatView"
3233
import ContextMenu from "./ContextMenu"
@@ -1319,6 +1320,7 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
13191320
lockApiConfigAcrossModes={!!lockApiConfigAcrossModes}
13201321
onToggleLockApiConfig={handleToggleLockApiConfig}
13211322
/>
1323+
<ReasoningEffortSelector triggerClassName="min-w-[28px] text-ellipsis overflow-hidden flex-shrink" />
13221324
<AutoApproveDropdown triggerClassName="min-w-[28px] text-ellipsis overflow-hidden flex-shrink" />
13231325
</div>
13241326
<div className={cn("flex flex-shrink-0 items-center gap-0.5 h-5 leading-none pr-2")}>

0 commit comments

Comments
 (0)