@@ -1,16 +1,13 @@
11 import * as c from "yoctocolors";
22 import { isAbortError } from "../agent/agent-helpers.ts";
3-import {
4- deleteMcpServer,
5- listMcpServers,
6- upsertMcpServer,
7-} from "../session/db/index.ts";
8-import { loadAgents } from "./agents.ts";
3+import { handleAgentCommand } from "./commands-agent.ts";
94 import {
105 handleCacheCommand,
116 handleContextCommand,
127 handleReasoningCommand,
138 } from "./commands-config.ts";
9+import { renderHelpCommand } from "./commands-help.ts";
10+import { handleMcpCommand } from "./commands-mcp.ts";
1411 import { handleModelCommand } from "./commands-model.ts";
1512 import {
1613 type CustomCommand,
@@ -21,8 +18,6 @@ import { watchForCancel } from "./input.ts";
2118 import { renderMarkdown } from "./markdown.ts";
2219 import { PREFIX, renderBanner, write, writeln } from "./output.ts";
2320
24-import { loadSkillsIndex } from "./skills.ts";
25-
2621 // ─── Types ────────────────────────────────────────────────────────────────────
2722
2823 import type { CommandContext } from "./types.ts";
@@ -51,119 +46,6 @@ async function handleUndo(ctx: CommandContext): Promise<void> {
5146 }
5247 }
5348
54-async function handleMcp(ctx: CommandContext, args: string): Promise<void> {
55- const parts = args.trim().split(/\s+/);
56- const sub = parts[0] ?? "list";
57-
58- switch (sub) {
59- case "list": {
60- const servers = listMcpServers();
61- if (servers.length === 0) {
62- writeln(c.dim(" no MCP servers configured"));
63- writeln(
64- c.dim(
65- " /mcp add <name> http <url> · /mcp add <name> stdio <cmd> [args...]",
66- ),
67- );
68- return;
69- }
70- writeln();
71- for (const s of servers) {
72- const detail = s.url
73- ? c.dim(` ${s.url}`)
74- : s.command
75- ? c.dim(` ${s.command}`)
76- : "";
77- writeln(
78- ` ${c.yellow("⚙")} ${c.bold(s.name)} ${c.dim(s.transport)}${detail}`,
79- );
80- }
81- return;
82- }
83-
84- case "add": {
85- // http: /mcp add <name> http <url>
86- // stdio: /mcp add <name> stdio <cmd> [args...]
87- const [, name, transport, ...rest] = parts;
88- if (!name || !transport || rest.length === 0) {
89- writeln(`${PREFIX.error} usage: /mcp add <name> http <url>`);
90- writeln(`${PREFIX.error} /mcp add <name> stdio <cmd> [args...]`);
91-
92- return;
93- }
94- if (transport === "http") {
95- const url = rest[0];
96- if (!url) {
97- writeln(`${PREFIX.error} usage: /mcp add <name> http <url>`);
98-
99- return;
100- }
101- upsertMcpServer({
102- name,
103- transport,
104- url,
105- command: null,
106- args: null,
107- env: null,
108- });
109- } else if (transport === "stdio") {
110- const [command, ...cmdArgs] = rest;
111- if (!command) {
112- writeln(
113- `${PREFIX.error} usage: /mcp add <name> stdio <cmd> [args...]`,
114- );
115-
116- return;
117- }
118- upsertMcpServer({
119- name,
120- transport,
121- url: null,
122- command,
123- args: cmdArgs.length ? JSON.stringify(cmdArgs) : null,
124- env: null,
125- });
126- } else {
127- writeln(
128- `${PREFIX.error} unknown transport: ${transport} (use http or stdio)`,
129- );
130-
131- return;
132- }
133- // Connect immediately so tools are available in this session
134- try {
135- await ctx.connectMcpServer(name);
136- writeln(
137- `${PREFIX.success} mcp server ${c.cyan(name)} added and connected`,
138- );
139- } catch (e) {
140- writeln(
141- `${PREFIX.success} mcp server ${c.cyan(name)} saved ${c.dim(`(connection failed: ${String(e)})`)}`,
142- );
143- }
144- return;
145- }
146-
147- case "remove":
148- case "rm": {
149- const [, name] = parts;
150- if (!name) {
151- writeln(`${PREFIX.error} usage: /mcp remove <name>`);
152-
153- return;
154- }
155- deleteMcpServer(name);
156- writeln(`${PREFIX.success} mcp server ${c.cyan(name)} removed`);
157- return;
158- }
159-
160- default:
161- writeln(`${PREFIX.error} unknown: /mcp ${sub}`);
162-
163- writeln(c.dim(" subcommands: list · add · remove"));
164- }
165-}
166-
16749 function handleNew(ctx: CommandContext): void {
16850 ctx.startNewSession();
16951 // Clear terminal and reprint banner for a fresh session feel
@@ -228,157 +110,6 @@ async function handleCustomCommand(
228110 }
229111 }
230112
231-// ─── Agent ────────────────────────────────────────────────────────────────────
232-
233-function handleAgent(ctx: CommandContext, args: string): void {
234- const raw = args.trim();
235- const agents = loadAgents(ctx.cwd);
236-
237- if (!raw) {
238- // List all agents
239- if (agents.size === 0) {
240- writeln(
241- c.dim(" no agents found (~/.agents/agents/ or .agents/agents/)"),
242- );
243- writeln(
244- c.dim(" /agent <name> to activate · /agent off to deactivate"),
245- );
246- return;
247- }
248- writeln();
249- writeln(c.dim(" agents:"));
250- for (const agent of agents.values()) {
251- const modeTag = agent.mode ? c.dim(` [${agent.mode}]`) : "";
252- const srcTag =
253- agent.source === "local" ? c.dim(" (local)") : c.dim(" (global)");
254- const active = ctx.activeAgent === agent.name ? c.cyan(" ◀ active") : "";
255- writeln(
256- ` ${c.magenta(`@${agent.name}`.padEnd(26))} ${c.dim(agent.description)}${modeTag}${srcTag}${active}`,
257- );
258- }
259- writeln();
260- writeln(
261- c.dim(" /agent <name> to activate · /agent off to deactivate"),
262- );
263- writeln();
264- return;
265- }
266-
267- if (raw.toLowerCase() === "off" || raw.toLowerCase() === "none") {
268- ctx.setActiveAgent(null);
269- writeln(`${PREFIX.info} ${c.dim("active agent cleared")}`);
270- return;
271- }
272-
273- const agent = agents.get(raw);
274- if (!agent) {
275- writeln(`${PREFIX.error} agent ${c.cyan(raw)} not found`);
276- return;
277- }
278-
279- // Pass the resolved system prompt so setActiveAgent never needs to re-lookup.
280- ctx.setActiveAgent(raw, agent.systemPrompt);
281- writeln(
282- `${PREFIX.success} active agent → ${c.cyan(raw)} ${c.dim("(instructions appended to system prompt)")}`,
283- );
284-}
285-
286-// ─── Help ─────────────────────────────────────────────────────────────────────
287-
288-function handleHelp(
289- ctx: CommandContext,
290- custom: Map<string, CustomCommand>,
291-): void {
292- writeln();
293-
294- const cmds: [string, string][] = [
295- ["/model [id]", "list or switch models (fetches live list)"],
296- ["/undo", "remove the last turn from conversation history"],
297- ["/reasoning [on|off]", "toggle display of model reasoning output"],
298- ["/context [prune|cap]", "configure context pruning and tool-result caps"],
299- [
300- "/cache [on|off|openai|gemini]",
301- "configure provider prompt-caching settings",
302- ],
303-
304- ["/agent [name]", "set or clear active primary agent"],
305- ["/mcp list", "list MCP servers"],
306- ["/mcp add <n> <t> [u]", "add an MCP server"],
307- ["/mcp remove <name>", "remove an MCP server"],
308- ["/new", "start a new session with clean context"],
309- ["/help", "this message"],
310- ["/exit", "quit"],
311- ];
312- for (const [cmd, desc] of cmds) {
313- writeln(` ${c.cyan(cmd.padEnd(26))} ${c.dim(desc)}`);
314- }
315-
316- // Show custom commands
317- if (custom.size > 0) {
318- writeln();
319- writeln(c.dim(" custom commands:"));
320- for (const cmd of custom.values()) {
321- const tag =
322- cmd.source === "local" ? c.dim(" (local)") : c.dim(" (global)");
323- writeln(
324- ` ${c.green(`/${cmd.name}`.padEnd(26))} ${c.dim(cmd.description)}${tag}`,
325- );
326- }
327- }
328-
329- // Show agents
330- const agents = loadAgents(ctx.cwd);
331- if (agents.size > 0) {
332- writeln();
333- writeln(c.dim(" agents (~/.agents/agents/ or .agents/agents/):"));
334- for (const agent of agents.values()) {
335- const modeTag = agent.mode ? c.dim(` [${agent.mode}]`) : "";
336- const tag =
337- agent.source === "local" ? c.dim(" (local)") : c.dim(" (global)");
338- writeln(
339- ` ${c.magenta(agent.name.padEnd(26))} ${c.dim(agent.description)}${modeTag}${tag}`,
340- );
341- }
342- }
343-
344- // Show skills
345- const skills = loadSkillsIndex(ctx.cwd);
346- if (skills.size > 0) {
347- writeln();
348- writeln(
349- c.dim(
350- " skills (walk-up local .agents/.claude/skills + global ~/.agents/skills ~/.claude/skills):",
351- ),
352- );
353- for (const skill of skills.values()) {
354- const tag =
355- skill.source === "local" ? c.dim(" (local)") : c.dim(" (global)");
356- writeln(
357- ` ${c.yellow(`@${skill.name}`.padEnd(26))} ${c.dim(skill.description)}${tag}`,
358- );
359- }
360- }
361-
362- writeln();
363- writeln(
364- ` ${c.green("/agent <name>".padEnd(26))} ${c.dim("activate a custom agent by name")}`,
365- );
366- writeln(
367- ` ${c.green("@skill".padEnd(26))} ${c.dim("load a skill body on demand into the prompt (Tab to complete)")}`,
368- );
369- writeln(
370- ` ${c.green("@file".padEnd(26))} ${c.dim("inject file contents into prompt (Tab to complete)")}`,
371- );
372- writeln(
373- ` ${c.green("!cmd".padEnd(26))} ${c.dim("run shell command, output added as context")}`,
374- );
375- writeln();
376- writeln(
377- ` ${c.dim("esc")} cancel response ${c.dim("·")} ${c.dim("ctrl+c")} exit ${c.dim("·")} ${c.dim("ctrl+d")} exit ${c.dim("·")} ${c.dim("ctrl+r")} history search ${c.dim("·")} ${c.dim("↑↓")} history`,
378- );
379- writeln();
380-}
381-
382113 // ─── Dispatch ─────────────────────────────────────────────────────────────────
383114
384115 export async function handleCommand(
@@ -415,11 +146,11 @@ export async function handleCommand(
415146 return { type: "handled" };
416147
417148 case "agent":
418- handleAgent(ctx, args);
149+ handleAgentCommand(ctx, args);
419150 return { type: "handled" };
420151
421152 case "mcp":
422- await handleMcp(ctx, args);
153+ await handleMcpCommand(ctx, args);
423154 return { type: "handled" };
424155
425156 case "new":
@@ -428,7 +159,7 @@ export async function handleCommand(
428159
429160 case "help":
430161 case "?":
431- handleHelp(ctx, custom);
162+ renderHelpCommand(ctx, custom);
432163 return { type: "handled" };
433164
434165 case "exit":