xonecas.io
64875
Mc

mini-coder / commit 2e8713b

feat: Add thinking effort support for models

Extended the `/model` command to allow users to optionally set a thinking effort level (low, medium, high, xhigh) alongside model selection. The choice persists across sessions and is translated into the correct provider-specific API payload at inference time.

Features:
- Normalized type `ThinkingEffort` covering Anthropic, OpenAI, and Google thinking options.
- Maintain a static `REASONING_MODELS` table in `providers.ts` to check if models support thinking.
- Extends the `/model` slash command to parse and handle thinking effort.
- Shows active thinking effort on the CLI status bar.
- Graceful degradation: if effort is set but the model pattern doesn't match the reasoning table, it silently passes through.
- Fixed a bug where subagents wouldn't inherit thinking effort.
- Fixed an issue with Google provider schema expecting lowercase string value.
- Modified Anthropic options to place effort at the root of the thinking settings object.

Sean2026-03-03 2e8713b27d9be89dc32801e81158b365de102f39 parent f06b3c8

10 files changed

src/agent/agent.ts+19−2
@@ -4,11 +4,15 @@ import type { CommandContext } from "../cli/commands.ts";
44 import { tildePath } from "../cli/output.ts";
55 import type { AgentReporter } from "./reporter.ts";
66
7-import { getContextWindow } from "../llm-api/providers.ts";
7+import { type ThinkingEffort, getContextWindow } from "../llm-api/providers.ts";
88 import type { ToolDef } from "../llm-api/types.ts";
99 import { connectMcpServer } from "../mcp/client.ts";
1010
11-import { listMcpServers, setPreferredModel } from "../session/db/index.ts";
11+import {
12+ listMcpServers,
13+ setPreferredModel,
14+ setPreferredThinkingEffort,
15+} from "../session/db/index.ts";
1216 import { createSubagentRunner } from "./subagent-runner.ts";
1317 import { buildToolSet } from "./tools.ts";
1418 import { undoLastTurn } from "./undo-snapshot.ts";
@@ -21,6 +25,7 @@ import { SessionRunner } from "./session-runner.ts";
2125 export interface AgentOptions {
2226 model: string;
2327 cwd: string;
28+ initialThinkingEffort: ThinkingEffort | null;
2429 sessionId?: string;
2530 initialPrompt?: string;
2631 reporter: AgentReporter;
@@ -29,11 +34,13 @@ export interface AgentOptions {
2934 export async function runAgent(opts: AgentOptions): Promise<void> {
3035 const cwd = opts.cwd;
3136 let currentModel = opts.model;
37+ let currentThinkingEffort = opts.initialThinkingEffort;
3238
3339 const runSubagent = createSubagentRunner(
3440 cwd,
3541 opts.reporter,
3642 () => currentModel,
43+ () => currentThinkingEffort,
3744 );
3845
3946 const agents = loadAgents(cwd);
@@ -79,6 +86,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
7986 tools,
8087 mcpTools,
8188 initialModel: currentModel,
89+ initialThinkingEffort: opts.initialThinkingEffort,
8290 sessionId: opts.sessionId,
8391 });
8492
@@ -92,6 +100,14 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
92100 setPreferredModel(m);
93101 currentModel = m; // Update local reference for runSubagent
94102 },
103+ get thinkingEffort() {
104+ return runner.currentThinkingEffort;
105+ },
106+ setThinkingEffort: (e) => {
107+ runner.currentThinkingEffort = e;
108+ setPreferredThinkingEffort(e);
109+ currentThinkingEffort = e;
110+ },
95111 get planMode() {
96112 return runner.planMode;
97113 },
@@ -140,6 +156,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
140156 contextTokens: runner.lastContextTokens,
141157 contextWindow: getContextWindow(runner.currentModel) ?? 0,
142158 ralphMode: runner.ralphMode,
159+ thinkingEffort: runner.currentThinkingEffort,
143160 });
144161 }
145162
src/agent/reporter.ts+1−0
@@ -19,6 +19,7 @@ export interface StatusBarData {
1919 contextTokens: number;
2020 contextWindow: number;
2121 ralphMode: boolean;
22+ thinkingEffort?: string | null;
2223 }
2324
2425 export interface SubagentEventData {
src/agent/session-runner.ts+7−0
@@ -2,6 +2,7 @@ import * as c from "yoctocolors";
22 import type { ImageAttachment } from "../cli/image-types.ts";
33 import { watchForInterrupt } from "../cli/input.ts";
44 import { resolveModel } from "../llm-api/providers.ts";
5+import type { ThinkingEffort } from "../llm-api/providers.ts";
56 import type { CoreMessage } from "../llm-api/turn.ts";
67 import { runTurn } from "../llm-api/turn.ts";
78 import type { ToolDef } from "../llm-api/types.ts";
@@ -32,6 +33,7 @@ export interface SessionRunnerOptions {
3233 tools: ToolDef[];
3334 mcpTools: ToolDef[];
3435 initialModel: string;
36+ initialThinkingEffort: ThinkingEffort | null;
3537 sessionId?: string | undefined;
3638 }
3739
@@ -41,6 +43,7 @@ export class SessionRunner {
4143 public tools: ToolDef[];
4244 public mcpTools: ToolDef[];
4345 public currentModel: string;
46+ public currentThinkingEffort: ThinkingEffort | null;
4447
4548 public session!: ActiveSession;
4649 public coreHistory!: CoreMessage[];
@@ -60,6 +63,7 @@ export class SessionRunner {
6063 this.tools = opts.tools;
6164 this.mcpTools = opts.mcpTools;
6265 this.currentModel = opts.initialModel;
66+ this.currentThinkingEffort = opts.initialThinkingEffort;
6367 this.initSession(opts.sessionId);
6468 }
6569
@@ -169,6 +173,9 @@ export class SessionRunner {
169173 : this.tools,
170174 systemPrompt,
171175 signal: abortController.signal,
176+ ...(this.currentThinkingEffort
177+ ? { thinkingEffort: this.currentThinkingEffort }
178+ : {}),
172179 });
173180
174181 const { inputTokens, outputTokens, contextTokens, newMessages } =
src/agent/subagent-runner.ts+5−0
@@ -7,10 +7,13 @@ import type { AgentReporter } from "./reporter.ts";
77 import { buildSystemPrompt } from "./system-prompt.ts";
88 import { buildToolSet } from "./tools.ts";
99
10+import type { ThinkingEffort } from "../llm-api/providers.ts";
11+
1012 export function createSubagentRunner(
1113 cwd: string,
1214 reporter: AgentReporter,
1315 getCurrentModel: () => string,
16+ getThinkingEffort: () => ThinkingEffort | null,
1417 ) {
1518 let nextLaneId = 1;
1619 const activeLanes = new Set<number>();
@@ -55,12 +58,14 @@ export function createSubagentRunner(
5558 let inputTokens = 0;
5659 let outputTokens = 0;
5760
61+ const effort = getThinkingEffort();
5862 const events = runTurn({
5963 model: subLlm,
6064 modelString: model,
6165 messages: subMessages,
6266 tools: subTools,
6367 systemPrompt,
68+ ...(effort ? { thinkingEffort: effort } : {}),
6469 });
6570
6671 for await (const event of events) {
src/cli/commands.ts+63−9
@@ -1,5 +1,6 @@
11 import * as c from "yoctocolors";
22 import { fetchAvailableModels } from "../llm-api/providers.ts";
3+import type { ThinkingEffort } from "../llm-api/providers.ts";
34 import type { SubagentOutput } from "../tools/subagent.ts";
45
56 import {
@@ -22,6 +23,8 @@ import { loadSkills } from "./skills.ts";
2223 export interface CommandContext {
2324 currentModel: string;
2425 setModel: (model: string) => void;
26+ thinkingEffort: ThinkingEffort | null;
27+ setThinkingEffort: (effort: ThinkingEffort | null) => void;
2528 planMode: boolean;
2629 setPlanMode: (enabled: boolean) => void;
2730 ralphMode: boolean;
@@ -44,26 +47,66 @@ export type CommandResult =
4447 // ─── Command handlers ─────────────────────────────────────────────────────────
4548
4649 async function handleModel(ctx: CommandContext, args: string): Promise<void> {
47- if (args) {
48- // If the user passed a bare model name (no provider prefix), resolve it
49- // against the live model list so we get the full "provider/model-id" form.
50- let modelId = args;
51- if (!args.includes("/")) {
50+ const parts = args.trim().split(/\s+/).filter(Boolean);
51+
52+ if (parts.length > 0) {
53+ if (parts[0] === "effort") {
54+ const effortArg = parts[1] ?? "";
55+ if (effortArg === "off") {
56+ ctx.setThinkingEffort(null);
57+ writeln(`${PREFIX.success} thinking effort disabled`);
58+ } else if (["low", "medium", "high", "xhigh"].includes(effortArg)) {
59+ ctx.setThinkingEffort(effortArg as ThinkingEffort);
60+ writeln(`${PREFIX.success} thinking effort → ${c.cyan(effortArg)}`);
61+ } else {
62+ writeln(
63+ `${PREFIX.error} usage: /model effort <low|medium|high|xhigh|off>`,
64+ );
65+ }
66+ return;
67+ }
68+
69+ // Otherwise, it's a model switch
70+ const idArg = parts[0] ?? "";
71+ let modelId = idArg;
72+ if (!idArg.includes("/")) {
5273 const models = await fetchAvailableModels();
5374 const match = models.find(
54- (m) => m.id.split("/").slice(1).join("/") === args || m.id === args,
75+ (m) => m.id.split("/").slice(1).join("/") === idArg || m.id === idArg,
5576 );
5677 if (match) {
5778 modelId = match.id;
5879 } else {
5980 writeln(
60- `${PREFIX.error} unknown model ${c.cyan(args)} ${c.dim("— run /models for the full list")}`,
81+ `${PREFIX.error} unknown model ${c.cyan(idArg)} ${c.dim("— run /models for the full list")}`,
6182 );
6283 return;
6384 }
6485 }
6586 ctx.setModel(modelId);
66- writeln(`${PREFIX.success} model → ${c.cyan(modelId)}`);
87+
88+ const effortArg = parts[1];
89+ if (effortArg) {
90+ if (effortArg === "off") {
91+ ctx.setThinkingEffort(null);
92+ writeln(
93+ `${PREFIX.success} model → ${c.cyan(modelId)} ${c.dim("(thinking disabled)")}`,
94+ );
95+ } else if (["low", "medium", "high", "xhigh"].includes(effortArg)) {
96+ ctx.setThinkingEffort(effortArg as ThinkingEffort);
97+ writeln(
98+ `${PREFIX.success} model → ${c.cyan(modelId)} ${c.dim(`(✦ ${effortArg})`)}`,
99+ );
100+ } else {
101+ writeln(`${PREFIX.success} model → ${c.cyan(modelId)}`);
102+ writeln(
103+ `${PREFIX.error} unknown effort level ${c.cyan(effortArg)} (use low, medium, high, xhigh, off)`,
104+ );
105+ }
106+ } else {
107+ const e = ctx.thinkingEffort ? c.dim(` (✦ ${ctx.thinkingEffort})`) : "";
108+ writeln(`${PREFIX.success} model → ${c.cyan(modelId)}${e}`);
109+ }
67110 return;
68111 }
69112
@@ -106,8 +149,14 @@ async function handleModel(ctx: CommandContext, args: string): Promise<void> {
106149 const ctxTag = m.context
107150 ? c.dim(` ${Math.round(m.context / 1000)}k`)
108151 : "";
152+ const effortTag =
153+ isCurrent && ctx.thinkingEffort
154+ ? c.dim(` ✦ ${ctx.thinkingEffort}`)
155+ : "";
109156 const cur = isCurrent ? c.cyan(" ◀") : "";
110- writeln(` ${c.dim("·")} ${m.displayName}${freeTag}${ctxTag}${cur}`);
157+ writeln(
158+ ` ${c.dim("·")} ${m.displayName}${freeTag}${ctxTag}${cur}${effortTag}`,
159+ );
111160 writeln(` ${c.dim(m.id)}`);
112161 }
113162 }
@@ -116,6 +165,11 @@ async function handleModel(ctx: CommandContext, args: string): Promise<void> {
116165 writeln(
117166 c.dim(" /model <id> to switch · e.g. /model zen/claude-sonnet-4-6"),
118167 );
168+ writeln(
169+ c.dim(
170+ " /model effort <low|medium|high|xhigh|off> to set thinking effort",
171+ ),
172+ );
119173 }
120174
121175 function handlePlan(ctx: CommandContext): void {
src/cli/status-bar.ts+2−0
@@ -42,11 +42,13 @@ export function renderStatusBar(opts: {
4242 contextTokens: number;
4343 contextWindow: number | null;
4444 ralphMode?: boolean;
45+ thinkingEffort?: string | null;
4546 }): void {
4647 const cols = (process.stdout as NodeJS.WriteStream).columns ?? 80;
4748
4849 // Build segments from right priority (rightmost items drop first)
4950 const left: string[] = [c.cyan(opts.model)];
51+ if (opts.thinkingEffort) left.push(c.dim(`✦ ${opts.thinkingEffort}`));
5052 if (opts.provider && opts.provider !== "zen") left.push(c.dim(opts.provider));
5153 left.push(c.dim(opts.sessionId.slice(0, 8)));
5254 if (opts.ralphMode) left.push(c.magenta("↻ ralph"));
src/index.ts+5−1
@@ -11,7 +11,10 @@ import {
1111 } from "./cli/output.ts";
1212 import { initApiLog } from "./llm-api/api-log.ts";
1313 import { autoDiscoverModel } from "./llm-api/providers.ts";
14-import { getPreferredModel } from "./session/db/index.ts";
14+import {
15+ getPreferredModel,
16+ getPreferredThinkingEffort,
17+} from "./session/db/index.ts";
1518 import { getMostRecentSession, printSessionList } from "./session/manager.ts";
1619
1720 // Register terminal cleanup handlers as early as possible so the cursor is
@@ -162,6 +165,7 @@ async function main(): Promise<void> {
162165 const agentOpts: Parameters<typeof runAgent>[0] = {
163166 model,
164167 cwd: args.cwd,
168+ initialThinkingEffort: getPreferredThinkingEffort(),
165169 reporter: new CliReporter(),
166170 };
167171 if (sessionId) agentOpts.sessionId = sessionId;
src/llm-api/providers.ts+135−0
@@ -190,6 +190,141 @@ const CONTEXT_WINDOW_TABLE: Array<[pattern: RegExp, tokens: number]> = [
190190 // Qwen3 Coder — 131k (Qwen3 standard)
191191 [/^qwen3-/, 131_000],
192192 ];
193+export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
194+
195+// Maps model-ID patterns (tested against the bare model ID, not provider/id)
196+// to whether the model supports thinking/reasoning.
197+// Matched in order; first match wins.
198+const REASONING_MODELS: Array<RegExp> = [
199+ // Anthropic — Claude 3.5+ and Claude 3.7+ support extended thinking
200+ /^claude-3-5-sonnet/,
201+ /^claude-3-7/,
202+ /^claude-sonnet-4/,
203+ /^claude-opus-4/,
204+ // OpenAI — o-series and GPT-5 reasoning models
205+ /^o1/,
206+ /^o3/,
207+ /^o4/,
208+ /^gpt-5/,
209+ // Google — Gemini 2.5+ and Gemini 3.x
210+ /^gemini-2\.5/,
211+ /^gemini-3/,
212+];
213+
214+export function supportsThinking(modelString: string): boolean {
215+ const { modelId } = parseModelString(modelString);
216+ return REASONING_MODELS.some((p) => p.test(modelId));
217+}
218+
219+// Token budgets for Anthropic extended thinking (budget_tokens mode).
220+// Adaptive-mode models (claude-sonnet-4-6+, claude-opus-4+) use effort
221+// strings instead; detected by model ID below.
222+const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
223+ low: 4_096,
224+ medium: 8_192,
225+ high: 16_384,
226+ xhigh: 32_768,
227+};
228+
229+// Clamp xhigh → high for providers that don't support it.
230+function clampEffort(
231+ effort: ThinkingEffort,
232+ max: ThinkingEffort,
233+): ThinkingEffort {
234+ const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
235+ const i = ORDER.indexOf(effort);
236+ const m = ORDER.indexOf(max);
237+ return ORDER[Math.min(i, m)] as ThinkingEffort;
238+}
239+
240+export function getThinkingProviderOptions(
241+ modelString: string,
242+ effort: ThinkingEffort,
243+): Record<string, unknown> | null {
244+ if (!supportsThinking(modelString)) return null;
245+
246+ const { provider, modelId } = parseModelString(modelString);
247+
248+ // Anthropic (direct or via Zen routing through @ai-sdk/anthropic)
249+ if (
250+ provider === "anthropic" ||
251+ (provider === "zen" && modelId.startsWith("claude-"))
252+ ) {
253+ // Claude 3.7+, Sonnet 4.x, Opus 4.x support adaptive effort strings.
254+ const isAdaptive =
255+ /^claude-3-7/.test(modelId) ||
256+ /^claude-sonnet-4/.test(modelId) ||
257+ /^claude-opus-4/.test(modelId);
258+
259+ if (isAdaptive) {
260+ // Adaptive: effort string ("low"|"medium"|"high"|"max")
261+ // xhigh maps to "max" for Opus; clamp to "high" for others.
262+ const isOpus = /^claude-opus-4/.test(modelId);
263+ const mapped = effort === "xhigh" ? (isOpus ? "max" : "high") : effort;
264+ return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
265+ }
266+
267+ // Extended thinking: budget_tokens integer
268+ const budget = ANTHROPIC_BUDGET[effort];
269+ return {
270+ anthropic: {
271+ thinking: { type: "enabled", budgetTokens: budget },
272+ betas: ["interleaved-thinking-2025-05-14"],
273+ },
274+ };
275+ }
276+
277+ // OpenAI o-series and GPT-5 (direct or via Zen routing through @ai-sdk/openai)
278+ if (
279+ provider === "openai" ||
280+ (provider === "zen" &&
281+ (modelId.startsWith("o") || modelId.startsWith("gpt-5")))
282+ ) {
283+ // xhigh only valid on gpt-5.2+, o4+; clamp for others.
284+ const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
285+ const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
286+ return { openai: { reasoningEffort: clamped } };
287+ }
288+
289+ // Google Gemini (direct or via Zen routing through @ai-sdk/google)
290+ if (
291+ provider === "google" ||
292+ (provider === "zen" && modelId.startsWith("gemini-"))
293+ ) {
294+ // Gemini 3.x: thinkingLevel enum. 2.5.x: budgetTokens.
295+ if (/^gemini-3/.test(modelId)) {
296+ // No xhigh for Gemini 3.
297+ const level = clampEffort(effort, "high");
298+ return {
299+ google: {
300+ thinkingConfig: {
301+ includeThoughts: true,
302+ thinkingLevel: level,
303+ },
304+ },
305+ };
306+ }
307+ // Gemini 2.5: budget tokens. Capped at 24575 per API limit.
308+ const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
309+ low: 4_096,
310+ medium: 8_192,
311+ high: 16_384,
312+ xhigh: 24_575,
313+ };
314+ return {
315+ google: {
316+ thinkingConfig: {
317+ includeThoughts: true,
318+ thinkingBudget: GEMINI_BUDGET[effort],
319+ },
320+ },
321+ };
322+ }
323+
324+ // Unrecognised provider with a reasoning model (e.g. future providers) —
325+ // return null and let the call proceed without thinking options.
326+ return null;
327+}
193328
194329 /**
195330 * Return the known context window size (in tokens) for a model string.
src/llm-api/turn.ts+36−10
@@ -3,6 +3,10 @@ import type { FlexibleSchema, StepResult } from "ai";
33 import { z } from "zod";
44 import { logApiEvent } from "./api-log.ts";
55 import { parseModelString } from "./providers.ts";
6+import {
7+ type ThinkingEffort,
8+ getThinkingProviderOptions,
9+} from "./providers.ts";
610 import type { ToolDef, TurnEvent } from "./types.ts";
711
812 type StreamTextOptions = Parameters<typeof streamText>[0];
@@ -89,8 +93,17 @@ export async function* runTurn(options: {
8993 tools: ToolDef[];
9094 systemPrompt?: string;
9195 signal?: AbortSignal;
96+ thinkingEffort?: ThinkingEffort;
9297 }): AsyncGenerator<TurnEvent> {
93- const { model, modelString, messages, tools, systemPrompt, signal } = options;
98+ const {
99+ model,
100+ modelString,
101+ messages,
102+ tools,
103+ systemPrompt,
104+ signal,
105+ thinkingEffort,
106+ } = options;
94107
95108 // stepCount tracks completed steps (incremented in onStepFinish).
96109 // Used to detect the second-to-last step for the warning injection.
@@ -133,6 +146,26 @@ export async function* runTurn(options: {
133146
134147 logApiEvent("turn start", { modelString, messageCount: messages.length });
135148
149+ const thinkingOpts = thinkingEffort
150+ ? getThinkingProviderOptions(modelString, thinkingEffort)
151+ : null;
152+
153+ const mergedProviderOptions = {
154+ ...(useInstructions
155+ ? { openai: { instructions: systemPrompt, store: false } }
156+ : {}),
157+ ...(thinkingOpts ?? {}),
158+ ...(useInstructions && thinkingOpts?.openai
159+ ? {
160+ openai: {
161+ instructions: systemPrompt,
162+ store: false,
163+ ...(thinkingOpts.openai as object),
164+ },
165+ }
166+ : {}),
167+ };
168+
136169 const streamOpts: StreamTextOptions = {
137170 model,
138171 messages,
@@ -160,15 +193,8 @@ export async function* runTurn(options: {
160193 },
161194
162195 ...(systemPrompt ? { system: systemPrompt } : {}),
163- ...(useInstructions
164- ? {
165- providerOptions: {
166- openai: {
167- instructions: systemPrompt,
168- store: false,
169- },
170- },
171- }
196+ ...(Object.keys(mergedProviderOptions).length > 0
197+ ? { providerOptions: mergedProviderOptions }
172198 : {}),
173199 ...(signal ? { abortSignal: signal } : {}),
174200 };
src/session/db/settings-repo.ts+18−0
@@ -24,3 +24,21 @@ export function getPreferredModel(): string | null {
2424 export function setPreferredModel(model: string): void {
2525 setSetting("preferred_model", model);
2626 }
27+
28+import type { ThinkingEffort } from "../../llm-api/providers.ts";
29+
30+export function getPreferredThinkingEffort(): ThinkingEffort | null {
31+ const v = getSetting("preferred_thinking_effort");
32+ if (v === "low" || v === "medium" || v === "high" || v === "xhigh") return v;
33+ return null;
34+}
35+
36+export function setPreferredThinkingEffort(
37+ effort: ThinkingEffort | null,
38+): void {
39+ if (effort === null) {
40+ getDb().run("DELETE FROM settings WHERE key = 'preferred_thinking_effort'");
41+ } else {
42+ setSetting("preferred_thinking_effort", effort);
43+ }
44+}