|
| 1 | +import { copyToClipboard } from "../../clipboard/copy.js"; |
| 2 | +import type { Command } from "../types.js"; |
| 3 | + |
| 4 | +export const help: Command = { |
| 5 | + name: "help", |
| 6 | + description: "List available slash commands and keyboard shortcuts.", |
| 7 | + handler: (_args, ctx) => { |
| 8 | + const lines: string[] = []; |
| 9 | + lines.push("Keyboard shortcuts:"); |
| 10 | + lines.push(" / slash-command autocomplete (Tab to complete, ↑↓ to choose)"); |
| 11 | + lines.push(" !cmd run a shell command directly (e.g. !git status)"); |
| 12 | + lines.push(" @path inline a file's contents into the next prompt"); |
| 13 | + lines.push(" ↑/↓ recall prior prompts (at line start)"); |
| 14 | + lines.push(" \\<Enter> insert a newline instead of submitting"); |
| 15 | + lines.push(" Ctrl-C cancel turn (busy) · twice to exit (idle)"); |
| 16 | + lines.push(""); |
| 17 | + lines.push("Slash commands:"); |
| 18 | + for (const cmd of ctx.registry.list()) { |
| 19 | + const aliasPart = cmd.aliases?.length ? ` (${cmd.aliases.map((a) => `/${a}`).join(", ")})` : ""; |
| 20 | + lines.push(` /${cmd.name}${aliasPart} — ${cmd.description}`); |
| 21 | + } |
| 22 | + ctx.emit(lines.join("\n")); |
| 23 | + return { handled: true }; |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +export const whoami: Command = { |
| 28 | + name: "whoami", |
| 29 | + aliases: ["status"], |
| 30 | + description: "Show current sign-in status.", |
| 31 | + handler: (_args, ctx) => { |
| 32 | + const source = ctx.bundle.source; |
| 33 | + ctx.emit( |
| 34 | + source === "proxy" |
| 35 | + ? "signed in via codebase.foundation (inference proxy)" |
| 36 | + : source === "explicit" |
| 37 | + ? "using model selected via CODEBASE_PROVIDER + CODEBASE_MODEL" |
| 38 | + : "using auto-detected provider from env", |
| 39 | + ); |
| 40 | + return { handled: true }; |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +export const pwd: Command = { |
| 45 | + name: "pwd", |
| 46 | + aliases: ["cwd"], |
| 47 | + description: "Print the current working directory and copy it to the clipboard.", |
| 48 | + handler: async (_args, ctx) => { |
| 49 | + const cwd = ctx.bundle.toolContext.cwd; |
| 50 | + const copied = await copyToClipboard(cwd).catch(() => false); |
| 51 | + ctx.emit(copied ? `${cwd}\n(copied to clipboard)` : cwd); |
| 52 | + return { handled: true }; |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Diagnostic for "the model isn't remembering what I told it earlier." |
| 58 | + * Compares the UI's display state (what the user sees in the transcript) |
| 59 | + * with the agent's internal _state.messages (what actually ships to the |
| 60 | + * model on the next turn). If those diverge, that's the bug. If they |
| 61 | + * match but the model still acts amnesiac, the issue is in the wire |
| 62 | + * call — pi-ai's openai-completions builder, the proxy, or the upstream. |
| 63 | + */ |
| 64 | +export const debug: Command = { |
| 65 | + name: "debug", |
| 66 | + description: "Inspect internal agent state — message count, token estimate, last few roles.", |
| 67 | + handler: (_args, ctx) => { |
| 68 | + const display = ctx.state.messages; |
| 69 | + const internal = ctx.bundle.agent.state.messages; |
| 70 | + const rolesTail = (msgs: readonly { role: string }[], n: number) => |
| 71 | + msgs |
| 72 | + .slice(-n) |
| 73 | + .map((m) => m.role) |
| 74 | + .join(" → ") || "(empty)"; |
| 75 | + const u = ctx.state.usage; |
| 76 | + const used = u.input + u.cacheRead; |
| 77 | + const compactAt = ctx.bundle.compaction.threshold(); |
| 78 | + const divergent = display.length !== internal.length; |
| 79 | + const lines = [ |
| 80 | + "Internal state inspection:", |
| 81 | + "", |
| 82 | + ` Display messages (UI): ${display.length}`, |
| 83 | + ` Agent state messages: ${internal.length}${divergent ? " ← MISMATCH!" : ""}`, |
| 84 | + "", |
| 85 | + ` Last 5 display roles: ${rolesTail(display, 5)}`, |
| 86 | + ` Last 5 agent state roles: ${rolesTail(internal, 5)}`, |
| 87 | + "", |
| 88 | + ` Estimated tokens used: ${used.toLocaleString()}`, |
| 89 | + ` Compaction triggers at: ${compactAt.toLocaleString()}`, |
| 90 | + ` Streaming in progress: ${ctx.state.streaming ? "yes" : "no"}`, |
| 91 | + "", |
| 92 | + divergent |
| 93 | + ? "Mismatch means the agent and the UI disagree about what's been said. " + |
| 94 | + "That's the source of 'the model forgot' — the next turn ships internal " + |
| 95 | + "messages, not display messages. Report this with a `codebase --debug-input` " + |
| 96 | + "transcript so we can see how it happened." |
| 97 | + : "Display and agent state match. If the model is still acting amnesiac, the " + |
| 98 | + "context is leaving the CLI correctly but something on the wire is dropping " + |
| 99 | + "it — capture with OPENAI_LOG=debug codebase to see the raw HTTP request body.", |
| 100 | + ]; |
| 101 | + ctx.emit(lines.join("\n")); |
| 102 | + return { handled: true }; |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +export const context: Command = { |
| 107 | + name: "context", |
| 108 | + description: "Visualize how full the context window is right now.", |
| 109 | + handler: (_args, ctx) => { |
| 110 | + const u = ctx.state.usage; |
| 111 | + const used = u.input + u.cacheRead; |
| 112 | + // Anthropic Claude Sonnet 4.x is 200K input by default; Opus is 200K too. |
| 113 | + // pi-ai's model.contextLength would be the source of truth; for now use |
| 114 | + // 200K as the floor and clamp. |
| 115 | + const window = (ctx.state.model as { contextLength?: number }).contextLength ?? 200_000; |
| 116 | + const ratio = Math.min(1, used / window); |
| 117 | + const compactAt = ctx.bundle.compaction.threshold(); |
| 118 | + const barWidth = 40; |
| 119 | + const filled = Math.round(ratio * barWidth); |
| 120 | + const compactMark = Math.round((compactAt / window) * barWidth); |
| 121 | + let bar = ""; |
| 122 | + for (let i = 0; i < barWidth; i++) { |
| 123 | + if (i < filled) bar += "█"; |
| 124 | + else if (i === compactMark) bar += "│"; |
| 125 | + else bar += "░"; |
| 126 | + } |
| 127 | + const pct = `${(ratio * 100).toFixed(1)}%`; |
| 128 | + const compactPct = `${((compactAt / window) * 100).toFixed(0)}%`; |
| 129 | + ctx.emit( |
| 130 | + `context window:\n ${bar}\n ${used.toLocaleString()} / ${window.toLocaleString()} tokens (${pct})\n │ = compaction triggers at ${compactAt.toLocaleString()} (${compactPct})`, |
| 131 | + ); |
| 132 | + return { handled: true }; |
| 133 | + }, |
| 134 | +}; |
0 commit comments