Files
loki/scripts/test/terminal-summary.ts
CatoandClaude Opus 5.5 0dc521df93 feat(terminal): Loki tells you what to type, and typing it is one click (#1164)
* feat(terminal): Loki tells you what to type, and typing it is one click

A person looking at the terminal did not know what to do: the agent said
"Please run /login", or they wanted Opus instead of the current model, and
nothing on the page said the answer is a command. The summary offered prose
tasks, and a command sent through Inject was wrapped in project context, so
"/model opus" reached the agent as a sentence about a model.

- config/agent-commands.ts is the one list of each CLI's own commands
  (sign in, choose a model, free up memory, start fresh), in plain words.
- lib/terminal-commands.ts reads the screen: signed out, out of quota,
  memory nearly full, or a menu waiting on a choice. TerminalActions shows
  the sentence and a button that types the command or key into the session.
- The Loki panel knows the commands too. "What's going on?" and every Ask
  carry them with the screen, and a step that is a command ("→ /model opus")
  becomes a button that types it instead of an Inject draft.
- A draft that is exactly a known command is typed, never injected. "/" in
  the Prompt box lists the agent's commands above the prompt library, and
  the panel has the full list one click away.
- Clicking or tapping the read-only terminal switches to Type and puts the
  cursor where keys go, so the screen can be worked in directly.
- Copy no longer says "tap" to people on a computer: gesture-neutral wording
  where possible, usePressVerb() (and widget/press.ts) where the gesture is
  the instruction, "pressed" when narrating what someone did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* test(widget): the watch remark names the control as pressed, not tapped

The widget's narration now says "pressed", which is true on a phone and a
computer alike; this browser assertion still expected the old word.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-12 06:09:16 -07:00

125 lines
4.2 KiB
TypeScript

// The terminal's AI summary: read the screen as a person sees it, ask for a
// summary with next steps, and turn the "→" steps into one-tap injects.
// Operator ask, 2026-10-03: "make this screen an easy way to interact with the
// terminal. AI would give actionable summaries."
// Run: npx tsx scripts/test/terminal-summary.ts
import { readFileSync } from "fs";
import { join } from "path";
import { logicalLines, screenText, type ScreenBuffer } from "@/lib/terminal-screen";
import {
SUMMARY_MAX_CHARS,
screenAttachment,
splitActions,
summaryPrompt,
} from "@/lib/terminal-summary";
import { MAX_ATTACHMENT_CHARS } from "@/lib/loki/attachments";
let pass = 0;
let fail = 0;
function eq(actual: unknown, expected: unknown, label: string) {
const a = JSON.stringify(actual);
const e = JSON.stringify(expected);
if (a === e) pass++;
else {
fail++;
console.error(`✗ ${label}: expected ${e}, got ${a}`);
}
}
const buffer = (rows: string[]): ScreenBuffer => ({
length: rows.length,
getLine: (i) => (rows[i] === undefined ? undefined : { translateToString: () => rows[i]! }),
});
// --- reading the screen -------------------------------------------------------
{
const cols = 10;
const rows = ["$ npm t", "https://ex", "ample.com/", "x", "done"];
eq(
logicalLines(buffer(rows), cols, 10),
["$ npm t", "https://example.com/x", "done"],
"full-width rows join into one logical line",
);
eq(
logicalLines(buffer(rows), cols, 2),
["https://example.com/x", "done"],
"a window starting mid-line backs up to the line's start",
);
eq(logicalLines(buffer([]), cols, 10), [], "an empty screen has no lines");
}
{
eq(
screenText(["a", "", "", "", "b", "", ""], 100),
"a\n\nb",
"blank runs collapse, tail trimmed",
);
eq(screenText(["old line", "new line"], 8), "new line", "cut from the top: newest output wins");
}
// --- the request ---------------------------------------------------------------
{
const prompt = summaryPrompt("derhochhin");
eq(
prompt.includes("derhochhin") && prompt.includes('"→ "'),
true,
"names the project and the → contract",
);
eq(prompt.length < 4000, true, "fits the messages route's text limit");
eq(SUMMARY_MAX_CHARS < MAX_ATTACHMENT_CHARS, true, "the screen fits one attachment");
const att = screenAttachment("$ ls", {
label: "Working",
stepSummary: "Tests",
nextAction: "Wait.",
});
eq(
att.kind === "text" && att.content.startsWith("Run status: Working — Tests. Wait."),
true,
"run status leads the screen",
);
eq(screenAttachment("$ ls", null).content, "$ ls", "no run → just the screen");
}
// --- reading the answer back ---------------------------------------------------
{
const { body, actions } = splitActions(
[
"- Built the scaffold",
"- Waiting for PR review",
"",
"→ Merge PR #1",
" → Register the site",
"→ ",
].join("\n"),
);
eq(body, "- Built the scaffold\n- Waiting for PR review", "the body keeps the bullets");
eq(actions, ["Merge PR #1", "Register the site"], "→ lines become actions; empty ones dropped");
eq(splitActions("Nothing useful on screen.").actions, [], "no actions when none are offered");
}
// --- wiring ----------------------------------------------------------------------
{
const read = (f: string) => readFileSync(join(process.cwd(), f), "utf8");
const rail = read("src/components/terminal/TerminalLokiRail.tsx");
eq(
/from "@bitbaum\/chatkit\/react"/.test(rail) && rail.includes("<ChatThread"),
true,
"the rail's conversation is chatkit's thread",
);
eq(rail.includes("MarkdownText"), false, "no hand-rolled answer renderer left in the rail");
eq(
read("src/components/terminal/TerminalSurface.tsx").match(/readScreenRef=\{readScreenRef\}/g)
?.length,
3,
"the screen reader reaches the view, the rail and the strip of things to type",
);
eq(
read("src/components/terminal/TerminalView.tsx").includes(
"logicalLines(term.buffer.active, term.cols, 300)",
),
true,
"the link bar reads lines the same way",
);
}
console.log(`${fail ? "✗" : "✓"} terminal-summary: ${pass} passed, ${fail} failed`);
if (fail) process.exit(1);