@@ -3,6 +3,7 @@ import type { AssistantMessage, JsonObject, Message, UserMessage } from "@earend
33 import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
44 import { Terminal, expandTabs, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
6+import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
67 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
78 import { cyan, dim, green, red } from "./styles.ts";
89 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
@@ -143,6 +144,15 @@ class Tui {
143144 private readonly messages: Message[] = [];
144145 readonly done: Promise<void>;
145146
147+ /** The only capability a command gets: styled lines into scrollback. */
148+ private readonly commandContext: CommandContext = {
149+ write: (lines) => {
150+ this.separator = true;
151+ this.commitLines(lines.map((text) => ({ text })));
152+ this.separator = true;
153+ },
154+ };
155+
146156 private resolveExit: () => void = () => {};
147157 private phase: Phase = "idle";
148158 private detail: string | undefined;
@@ -187,11 +197,6 @@ class Tui {
187197 process.on("SIGTERM", this.onSignal);
188198 this.separator = true;
189199 this.push(`mini-coder · ${this.opts.model.provider}/${this.opts.model.id}`);
190- this.push(
191- dim(
192- "Enter submit · Shift+Enter (or Ctrl+J) newline · Esc pause · Ctrl+C cancel · Ctrl+D exit",
193- ),
194- );
195200 this.separator = true;
196201 this.render();
197202 }
@@ -228,6 +233,14 @@ class Tui {
228233 }
229234 return;
230235 }
236+ if (key.type === "tab") {
237+ const completed = completeCommand(this.editor.text());
238+ if (completed !== null) {
239+ this.editor.setText(completed);
240+ this.render();
241+ }
242+ return;
243+ }
231244 const result = this.editor.handle(key);
232245 if (result === "submit") this.submit();
233246 else if (result === "changed") this.render();
@@ -244,6 +257,13 @@ class Tui {
244257 return;
245258 }
246259 if (text.trim() === "") return;
260+ const invocation = findCommand(text);
261+ if (invocation !== null) {
262+ this.editor.clear();
263+ invocation.command.run(this.commandContext, invocation.args);
264+ this.render();
265+ return;
266+ }
247267 this.editor.clear();
248268 const message: UserMessage = { role: "user", content: text, timestamp: Date.now() };
249269 this.messages.push(message);