@@ -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 || ""
0 commit comments