@@ -7,7 +7,19 @@ import {
77 isImageFilename,
88 loadImageFile,
99 } from "./image-types.ts";
10+import {
11+ buildPromptDisplay,
12+ createPasteToken,
13+ expandInputBuffer,
14+ getVisualCursor,
15+ pruneInputPasteTokens,
16+ renderInputBuffer,
17+} from "./input-buffer.ts";
1018 import { getInputCompletion } from "./input-completion.ts";
19+import {
20+ getSubagentControlAction,
21+ getTurnControlAction,
22+} from "./input-control.ts";
1123 import { terminal } from "./terminal-io.ts";
1224
1325 // ─── ANSI escape sequences ────────────────────────────────────────────────────
@@ -35,8 +47,6 @@ const CTRL_K = "\x0B";
3547 const CTRL_L = "\x0C";
3648 const CTRL_R = "\x12";
3749 const TAB = "\x09";
38-const ESC_BYTE = ESC.charCodeAt(0);
39-const CTRL_C_BYTE = CTRL_C.charCodeAt(0);
4050
4151 // ─── Types ────────────────────────────────────────────────────────────────────
4252
@@ -49,6 +59,18 @@ export type InputResult =
4959 | { type: "command"; command: string; args: string }
5060 | { type: "shell"; command: string };
5161
62+export {
63+ buildPromptDisplay,
64+ expandInputBuffer,
65+ pasteLabel,
66+ pruneInputPasteTokens,
67+ renderInputBuffer,
68+} from "./input-buffer.ts";
69+export {
70+ getSubagentControlAction,
71+ getTurnControlAction,
72+} from "./input-control.ts";
73+
5274 // ─── Image detection ─────────────────────────────────────────────────────────
5375
5476 /**
@@ -149,33 +171,6 @@ async function readKey(reader: StreamReader): Promise<string> {
149171 // handler — NOT via the shared ReadableStream reader — so that the watcher and
150172 // readline() never race for the same bytes. The caller must call the returned
151173 // stop() in a finally block.
152-export function getTurnControlAction(
153- chunk: Uint8Array,
154-): "cancel" | "quit" | null {
155- // A bare ESC press sends exactly one byte (0x1B). Multi-byte escape
156- // sequences (arrow keys, function keys, etc.) always follow with more
157- // bytes in the same chunk, so we only cancel on a lone ESC.
158- if (chunk.length === 1 && chunk[0] === ESC_BYTE) return "cancel";
159- for (const byte of chunk) {
160- if (byte === CTRL_C_BYTE) return "quit";
161- }
162- return null;
163-}
164-
165-export function getSubagentControlAction(
166- chunk: Uint8Array,
167-): "cancel" | "quit" | null {
168- // During custom-command subagent runs there is no interactive line editing,
169- // so any ESC byte should interrupt (terminals may encode ESC as multi-byte
170- // CSI/SS3 or other escape-prefixed sequences).
171- for (const byte of chunk) {
172- if (byte === CTRL_C_BYTE) return "quit";
173- }
174- for (const byte of chunk) {
175- if (byte === ESC_BYTE) return "cancel";
176- }
177- return null;
178-}
179174
180175 type WatchForCancelOptions = {
181176 allowSubagentEsc?: boolean;
@@ -236,108 +231,6 @@ export function watchForCancel(
236231
237232 // ─── Paste handling ───────────────────────────────────────────────────────────
238233
239-const PASTE_TOKEN_START = 0xe000;
240-const PASTE_TOKEN_END = 0xf8ff;
241-
242-function createPasteToken(
243- buf: string,
244- pasteTokens: ReadonlyMap<string, string>,
245-): string {
246- for (let code = PASTE_TOKEN_START; code <= PASTE_TOKEN_END; code++) {
247- const token = String.fromCharCode(code);
248- if (!buf.includes(token) && !pasteTokens.has(token)) return token;
249- }
250- throw new Error("Too many pasted chunks in a single prompt");
251-}
252-
253-export function pasteLabel(text: string): string {
254- const lines = text.split("\n");
255- const first = lines[0] ?? "";
256- const preview = first.length > 40 ? `${first.slice(0, 40)}…` : first;
257- const extra = lines.length - 1;
258- const more = extra > 0 ? ` +${extra} more line${extra === 1 ? "" : "s"}` : "";
259- return `[pasted: "${preview}"${more}]`;
260-}
261-
262-function processInputBuffer(
263- buf: string,
264- pasteTokens: ReadonlyMap<string, string>,
265- replacer: (ch: string, pasted: string | undefined) => string,
266-): string {
267- let out = "";
268- for (let i = 0; i < buf.length; i++) {
269- const ch = buf[i] ?? "";
270- out += replacer(ch, pasteTokens.get(ch));
271- }
272- return out;
273-}
274-
275-export function renderInputBuffer(
276- buf: string,
277- pasteTokens: ReadonlyMap<string, string>,
278-): string {
279- return processInputBuffer(buf, pasteTokens, (ch, pasted) =>
280- pasted ? pasteLabel(pasted) : ch,
281- );
282-}
283-
284-export function expandInputBuffer(
285- buf: string,
286- pasteTokens: ReadonlyMap<string, string>,
287-): string {
288- return processInputBuffer(buf, pasteTokens, (ch, pasted) => pasted ?? ch);
289-}
290-
291-export function pruneInputPasteTokens(
292- pasteTokens: ReadonlyMap<string, string>,
293- ...buffers: readonly string[]
294-): Map<string, string> {
295- const referenced = buffers.join("");
296- const next = new Map<string, string>();
297- for (const [token, text] of pasteTokens) {
298- if (referenced.includes(token)) next.set(token, text);
299- }
300- return next;
301-}
302-
303-function getVisualCursor(
304- buf: string,
305- cursor: number,
306- pasteTokens: ReadonlyMap<string, string>,
307-): number {
308- let visual = 0;
309- for (let i = 0; i < Math.min(cursor, buf.length); i++) {
310- const ch = buf[i] ?? "";
311- const pasted = pasteTokens.get(ch);
312- visual += pasted ? pasteLabel(pasted).length : 1;
313- }
314- return visual;
315-}
316-
317-export function buildPromptDisplay(
318- text: string,
319- cursor: number,
320- maxLen: number,
321-): { display: string; cursor: number } {
322- const clampedCursor = Math.max(0, Math.min(cursor, text.length));
323- if (maxLen <= 0) return { display: "", cursor: 0 };
324- if (text.length <= maxLen) return { display: text, cursor: clampedCursor };
325-
326- let start = Math.max(0, clampedCursor - maxLen);
327- const end = Math.min(text.length, start + maxLen);
328- if (end - start < maxLen) start = Math.max(0, end - maxLen);
329-
330- let display = text.slice(start, end);
331- if (start > 0 && display.length > 0) display = `…${display.slice(1)}`;
332- if (end < text.length && display.length > 0)
333- display = `${display.slice(0, -1)}…`;
334-
335- return {
336- display,
337- cursor: Math.min(clampedCursor - start, display.length),
338- };
339-}
340-
341234 // ─── Main readline function ───────────────────────────────────────────────────
342235
343236 const PROMPT = c.green("▶ ");