@@ -1,13 +1,17 @@
11 import * as c from "yoctocolors";
22 import { isAbortError } from "../agent/agent-helpers.ts";
3-import type { ThinkingEffort } from "../llm-api/providers.ts";
4-import { fetchAvailableModels } from "../llm-api/providers.ts";
53 import {
64 deleteMcpServer,
75 listMcpServers,
86 upsertMcpServer,
97 } from "../session/db/index.ts";
108 import { loadAgents } from "./agents.ts";
9+import {
10+ handleCacheCommand,
11+ handleContextCommand,
12+ handleReasoningCommand,
13+} from "./commands-config.ts";
14+import { handleModelCommand } from "./commands-model.ts";
1115 import {
1216 type CustomCommand,
1317 expandTemplate,
@@ -31,274 +35,6 @@ type CommandResult =
3135
3236 // ─── Command handlers ─────────────────────────────────────────────────────────
3337
34-async function handleModel(ctx: CommandContext, args: string): Promise<void> {
35- const parts = args.trim().split(/\s+/).filter(Boolean);
36-
37- if (parts.length > 0) {
38- if (parts[0] === "effort") {
39- const effortArg = parts[1] ?? "";
40- if (effortArg === "off") {
41- ctx.setThinkingEffort(null);
42- writeln(`${PREFIX.success} thinking effort disabled`);
43- } else if (["low", "medium", "high", "xhigh"].includes(effortArg)) {
44- ctx.setThinkingEffort(effortArg as ThinkingEffort);
45- writeln(`${PREFIX.success} thinking effort → ${c.cyan(effortArg)}`);
46- } else {
47- writeln(
48- `${PREFIX.error} usage: /model effort <low|medium|high|xhigh|off>`,
49- );
50- }
51- return;
52- }
53-
54- // Otherwise, it's a model switch
55- const idArg = parts[0] ?? "";
56- let modelId = idArg;
57- if (!idArg.includes("/")) {
58- const snapshot = await fetchAvailableModels();
59- const match = snapshot.models.find(
60- (m) => m.id.split("/").slice(1).join("/") === idArg || m.id === idArg,
61- );
62- if (match) {
63- modelId = match.id;
64- } else {
65- writeln(
66- `${PREFIX.error} unknown model ${c.cyan(idArg)} ${c.dim("— run /models for the full list")}`,
67- );
68- return;
69- }
70- }
71- ctx.setModel(modelId);
72-
73- const effortArg = parts[1];
74- if (effortArg) {
75- if (effortArg === "off") {
76- ctx.setThinkingEffort(null);
77- writeln(
78- `${PREFIX.success} model → ${c.cyan(modelId)} ${c.dim("(thinking disabled)")}`,
79- );
80- } else if (["low", "medium", "high", "xhigh"].includes(effortArg)) {
81- ctx.setThinkingEffort(effortArg as ThinkingEffort);
82- writeln(
83- `${PREFIX.success} model → ${c.cyan(modelId)} ${c.dim(`(✦ ${effortArg})`)}`,
84- );
85- } else {
86- writeln(`${PREFIX.success} model → ${c.cyan(modelId)}`);
87- writeln(
88- `${PREFIX.error} unknown effort level ${c.cyan(effortArg)} (use low, medium, high, xhigh, off)`,
89- );
90- }
91- } else {
92- const e = ctx.thinkingEffort ? c.dim(` (✦ ${ctx.thinkingEffort})`) : "";
93- writeln(`${PREFIX.success} model → ${c.cyan(modelId)}${e}`);
94- }
95- return;
96- }
97-
98- ctx.startSpinner("fetching models");
99-
100- const snapshot = await fetchAvailableModels();
101- const models = snapshot.models;
102-
103- ctx.stopSpinner();
104-
105- if (models.length === 0) {
106- writeln(
107- `${PREFIX.error} No models found. Check your API keys or Ollama connection.`,
108- );
109- writeln(
110- c.dim(
111- " Set OPENCODE_API_KEY for Zen, or start Ollama for local models.",
112- ),
113- );
114- return;
115- }
116-
117- if (snapshot.stale) {
118- const lastSync = snapshot.lastSyncAt
119- ? new Date(snapshot.lastSyncAt).toLocaleString()
120- : "never";
121- const refreshTag = snapshot.refreshing ? " (refreshing in background)" : "";
122- writeln(
123- c.dim(` model metadata is stale (last sync: ${lastSync})${refreshTag}`),
124- );
125- }
126-
127- // Group by provider
128- const byProvider = new Map<string, typeof models>();
129- for (const m of models) {
130- const existing = byProvider.get(m.provider);
131- if (existing) {
132- existing.push(m);
133- } else {
134- byProvider.set(m.provider, [m]);
135- }
136- }
137-
138- writeln();
139- for (const [provider, list] of byProvider) {
140- writeln(c.bold(` ${provider}`));
141- for (const m of list) {
142- const isCurrent = ctx.currentModel === m.id;
143- const freeTag = m.free ? c.green(" free") : "";
144- const ctxTag = m.context
145- ? c.dim(` ${Math.round(m.context / 1000)}k`)
146- : "";
147- const effortTag =
148- isCurrent && ctx.thinkingEffort
149- ? c.dim(` ✦ ${ctx.thinkingEffort}`)
150- : "";
151- const cur = isCurrent ? c.cyan(" ◀") : "";
152- writeln(
153- ` ${c.dim("·")} ${m.displayName}${freeTag}${ctxTag}${cur}${effortTag}`,
154- );
155- writeln(` ${c.dim(m.id)}`);
156- }
157- }
158-
159- writeln();
160- writeln(
161- c.dim(" /model <id> to switch · e.g. /model zen/claude-sonnet-4-6"),
162- );
163- writeln(
164- c.dim(
165- " /model effort <low|medium|high|xhigh|off> to set thinking effort",
166- ),
167- );
168-}
169-
170-function handleReasoning(ctx: CommandContext, args: string): void {
171- const mode = args.trim().toLowerCase();
172- if (!mode) {
173- ctx.setShowReasoning(!ctx.showReasoning);
174- writeln(
175- `${PREFIX.success} reasoning display ${ctx.showReasoning ? c.green("on") : c.dim("off")}`,
176- );
177- return;
178- }
179- if (mode === "on") {
180- ctx.setShowReasoning(true);
181- writeln(`${PREFIX.success} reasoning display ${c.green("on")}`);
182- return;
183- }
184- if (mode === "off") {
185- ctx.setShowReasoning(false);
186- writeln(`${PREFIX.success} reasoning display ${c.dim("off")}`);
187- return;
188- }
189- writeln(`${PREFIX.error} usage: /reasoning <on|off>`);
190-}
191-
192-function handleContext(ctx: CommandContext, args: string): void {
193- const [sub, value] = args.trim().split(/\s+/, 2);
194- if (!sub) {
195- const capText =
196- ctx.toolResultPayloadCapBytes <= 0
197- ? "off"
198- : `${Math.round(ctx.toolResultPayloadCapBytes / 1024)}KB (${ctx.toolResultPayloadCapBytes} bytes)`;
199- writeln(
200- `${PREFIX.info} pruning=${c.cyan(ctx.pruningMode)} tool-result-cap=${c.cyan(capText)}`,
201- );
202- writeln(c.dim(" usage: /context prune <off|balanced|aggressive>"));
203- writeln(c.dim(" /context cap <off|bytes|kb>"));
204- return;
205- }
206-
207- if (sub === "prune") {
208- if (value === "off" || value === "balanced" || value === "aggressive") {
209- ctx.setPruningMode(value);
210- writeln(`${PREFIX.success} context pruning → ${c.cyan(value)}`);
211- return;
212- }
213- writeln(`${PREFIX.error} usage: /context prune <off|balanced|aggressive>`);
214- return;
215- }
216-
217- if (sub === "cap") {
218- if (!value) {
219- writeln(`${PREFIX.error} usage: /context cap <off|bytes|kb>`);
220- return;
221- }
222- if (value === "off") {
223- ctx.setToolResultPayloadCapBytes(0);
224- writeln(`${PREFIX.success} tool-result payload cap disabled`);
225- return;
226- }
227- const capMatch = value.match(/^(\d+)(kb)?$/i);
228- if (!capMatch) {
229- writeln(`${PREFIX.error} invalid cap: ${c.cyan(value)}`);
230- return;
231- }
232- const base = Number.parseInt(capMatch[1] ?? "", 10);
233- const capBytes =
234- (capMatch[2] ?? "").toLowerCase() === "kb" ? base * 1024 : base;
235- if (!Number.isFinite(capBytes) || capBytes < 0) {
236- writeln(`${PREFIX.error} invalid cap: ${c.cyan(value)}`);
237- return;
238- }
239- ctx.setToolResultPayloadCapBytes(capBytes);
240- writeln(
241- `${PREFIX.success} tool-result payload cap → ${c.cyan(`${capBytes} bytes`)}`,
242- );
243- return;
244- }
245-
246- writeln(`${PREFIX.error} usage: /context <prune|cap> ...`);
247-}
248-function handleCache(ctx: CommandContext, args: string): void {
249- const [sub, value] = args.trim().split(/\s+/, 2);
250-
251- if (!sub) {
252- const gCache = ctx.googleCachedContent
253- ? c.cyan(ctx.googleCachedContent)
254- : "off";
255- writeln(
256- `${PREFIX.info} prompt-caching=${ctx.promptCachingEnabled ? c.green("on") : c.dim("off")} openai-retention=${c.cyan(ctx.openaiPromptCacheRetention)} gemini-cache=${gCache}`,
257- );
258- writeln(c.dim(" usage: /cache <on|off>"));
259- writeln(c.dim(" /cache openai <in_memory|24h>"));
260- writeln(c.dim(" /cache gemini <off|cachedContents/...>"));
261- return;
262- }
263-
264- if (sub === "on" || sub === "off") {
265- ctx.setPromptCachingEnabled(sub === "on");
266- writeln(
267- `${PREFIX.success} prompt caching → ${sub === "on" ? c.green("on") : c.dim("off")}`,
268- );
269- return;
270- }
271-
272- if (sub === "openai") {
273- if (value === "in_memory" || value === "24h") {
274- ctx.setOpenAIPromptCacheRetention(value);
275- writeln(
276- `${PREFIX.success} openai prompt cache retention → ${c.cyan(value)}`,
277- );
278- return;
279- }
280- writeln(`${PREFIX.error} usage: /cache openai <in_memory|24h>`);
281- return;
282- }
283-
284- if (sub === "gemini") {
285- if (!value) {
286- writeln(`${PREFIX.error} usage: /cache gemini <off|cachedContents/...>`);
287- return;
288- }
289- if (value === "off") {
290- ctx.setGoogleCachedContent(null);
291- writeln(`${PREFIX.success} gemini cached content → ${c.dim("off")}`);
292- return;
293- }
294- ctx.setGoogleCachedContent(value);
295- writeln(`${PREFIX.success} gemini cached content → ${c.cyan(value)}`);
296- return;
297- }
298-
299- writeln(`${PREFIX.error} usage: /cache <on|off|openai|gemini> ...`);
300-}
301-
30238 async function handleUndo(ctx: CommandContext): Promise<void> {
30339 ctx.startSpinner("restoring snapshot");
30440 try {
@@ -660,7 +396,7 @@ export async function handleCommand(
660396 switch (command.toLowerCase()) {
661397 case "model":
662398 case "models":
663- await handleModel(ctx, args);
399+ await handleModelCommand(ctx, args);
664400 return { type: "handled" };
665401
666402 case "undo":
@@ -668,14 +404,14 @@ export async function handleCommand(
668404 return { type: "handled" };
669405
670406 case "reasoning":
671- handleReasoning(ctx, args);
407+ handleReasoningCommand(ctx, args);
672408 return { type: "handled" };
673409
674410 case "context":
675- handleContext(ctx, args);
411+ handleContextCommand(ctx, args);
676412 return { type: "handled" };
677413 case "cache":
678- handleCache(ctx, args);
414+ handleCacheCommand(ctx, args);
679415 return { type: "handled" };
680416
681417 case "agent":