@@ -1,34 +1,12 @@
11 import process from "node:process";
2-import type {
3- Api,
4- AssistantMessage,
5- JsonObject,
6- Message,
7- Model,
8- Models,
9- ThinkingLevel,
10- Tool,
11- UserMessage,
12-} from "@earendil-works/pi-ai";
13-import { runAgentTurn, type AgentEvent, type Phase } from "../agent.ts";
14-import type { Session } from "../session.ts";
15-import type { ToolName } from "../config.ts";
2+import type { AssistantMessage, JsonObject, Message, UserMessage } from "@earendil-works/pi-ai";
3+import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
164 import { Terminal, expandTabs, wrapLine, type Key } from "./term.ts";
175 import { Editor } from "./editor.ts";
186 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
197 import { cyan, dim, green, red } from "./styles.ts";
208 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
219
22-export interface TuiOptions {
23- models: Models;
24- model: Model<Api>;
25- systemPrompt: string;
26- tools: Tool[];
27- toolNames: ToolName[];
28- thinkingEffort: ThinkingLevel;
29- session: Session;
30-}
31-
3210 /** Status-row spinner frames; the only animation in the TUI. */
3311 const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
3412 const SPINNER_MS = 120;
@@ -158,7 +136,7 @@ class LiveRegion {
158136 }
159137
160138 class Tui {
161- private readonly opts: TuiOptions;
139+ private readonly opts: AgentOptions;
162140 private readonly term: Terminal;
163141 private readonly editor = new Editor();
164142 private readonly live = new LiveRegion();
@@ -192,7 +170,7 @@ class Tui {
192170 private frame = 0;
193171 private spinner: NodeJS.Timeout | undefined;
194172
195- constructor(opts: TuiOptions) {
173+ constructor(opts: AgentOptions) {
196174 this.opts = opts;
197175 this.term = new Terminal({
198176 onKey: (key) => this.handleKey(key),
@@ -258,13 +236,10 @@ class Tui {
258236 private submit(): void {
259237 const text = this.editor.text();
260238 if (this.active) {
261- if (this.paused && this.steeringResolve) {
239+ if (this.paused && this.steeringResolve !== null) {
262240 this.editor.clear();
263241 if (text.length > 0) this.commitUser(text);
264- const resolve = this.steeringResolve;
265- this.steeringResolve = null;
266- this.paused = false;
267- resolve(text);
242+ this.resolveSteering(text);
268243 }
269244 return;
270245 }
@@ -312,11 +287,7 @@ class Tui {
312287 requestSteering: () =>
313288 new Promise<string>((resolve) => {
314289 this.paused = true;
315- this.steeringResolve = (text) => {
316- this.paused = false;
317- this.steeringResolve = null;
318- resolve(text);
319- };
290+ this.steeringResolve = resolve;
320291 this.render();
321292 }),
322293 },
@@ -329,11 +300,7 @@ class Tui {
329300 this.active = false;
330301 this.abort = null;
331302 this.pauseRequested = false;
332- if (this.steeringResolve) {
333- const resolve = this.steeringResolve;
334- this.steeringResolve = null;
335- resolve("");
336- }
303+ this.resolveSteering("");
337304 this.paused = false;
338305 if (this.spinner) clearInterval(this.spinner);
339306 this.spinner = undefined;
@@ -342,16 +309,20 @@ class Tui {
342309 }
343310
344311 private cancel(): void {
345- if (this.steeringResolve) {
346- const resolve = this.steeringResolve;
347- this.steeringResolve = null;
348- this.paused = false;
349- resolve("");
350- }
312+ this.resolveSteering("");
351313 this.abort?.abort();
352314 this.render();
353315 }
354316
317+ /** Answers a pending steering prompt; "" leaves the turn ending. */
318+ private resolveSteering(text: string): void {
319+ const resolve = this.steeringResolve;
320+ if (resolve === null) return;
321+ this.steeringResolve = null;
322+ this.paused = false;
323+ resolve(text);
324+ }
325+
355326 private handleAgentEvent(event: AgentEvent): void {
356327 if (this.closed) return;
357328 switch (event.type) {
@@ -387,36 +358,28 @@ class Tui {
387358 this.commitToolResult(event.name, event.text, event.isError);
388359 break;
389360 case "error":
390- this.finishTurn();
391- this.commitLines(this.reply.flush());
392- this.flushCalls();
393- this.separator = true;
394- this.push(`! ${event.message}`);
361+ this.endTurn(`! ${event.message}`);
395362 break;
396363 case "cancelled":
397- this.finishTurn();
398- this.commitLines(this.reply.flush());
399- this.flushCalls();
400- this.separator = true;
401- this.push("! cancelled");
364+ this.endTurn("! cancelled");
402365 break;
403366 case "complete":
404- this.finishTurn();
405- this.commitLines(this.reply.flush());
406- this.flushCalls();
407- this.separator = true;
408- this.push(dim(`[complete · ${this.elapsed()}s]`));
367+ this.endTurn(dim(`[complete · ${this.elapsed()}s]`));
409368 break;
410369 }
411370 this.render();
412371 }
413372
414- /** Terminal states clear the status row and any in-flight preview. */
415- private finishTurn(): void {
373+ /** A terminal state: clears the status row and any in-flight preview, then commits. */
374+ private endTurn(line: string): void {
416375 this.phase = "idle";
417376 this.detail = undefined;
418377 this.paused = false;
419378 this.activity.reset();
379+ this.commitLines(this.reply.flush());
380+ this.flushCalls();
381+ this.separator = true;
382+ this.push(line);
420383 }
421384
422385 private elapsed(): number {
@@ -578,7 +541,7 @@ class Tui {
578541 }
579542 }
580543
581-export async function runTui(opts: TuiOptions): Promise<void> {
544+export async function runTui(opts: AgentOptions): Promise<void> {
582545 const tui = new Tui(opts);
583546 tui.start();
584547 return tui.done;