The Open Framework for Building Agents
[2/2] feat(eve): add TUI kill ring and yank (#3573)
Ben Pankowde29d28e415d
diff --git a/.changeset/bright-taxis-yank.md b/.changeset/bright-taxis-yank.mdnew file mode 100644index 000000000..fcb37c4a4--- /dev/null+++ b/.changeset/bright-taxis-yank.md@@ -0,0 +1,5 @@+---+"eve": patch+---++In the eve TUI, press `Ctrl+Y` to paste text you removed with `Ctrl+K`, `Ctrl+U`, or `Ctrl+W`. Press `Alt+Y` immediately afterward to cycle through earlier removals.diff --git a/packages/eve/src/cli/dev/tui/line-editor.test.ts b/packages/eve/src/cli/dev/tui/line-editor.test.tsindex 501324148..317be22ec 100644--- a/packages/eve/src/cli/dev/tui/line-editor.test.ts+++ b/packages/eve/src/cli/dev/tui/line-editor.test.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "vitest";import {+ KillRing,PromptHistory,applyLineEditorKey,backspace,@@ -159,6 +160,99 @@ describe("line editing", () => {expect(deleteWord({ text: "one\ntwo", cursor: 4 })).toEqual({ text: "one\ntwo", cursor: 4 });});+ it("stores kills and yanks them across editors", () => {+ const ring = new KillRing();+ expect(ring.apply({ text: "hello world", cursor: 5 }, { type: "ctrl-k" })).toEqual({+ text: "hello",+ cursor: 5,+ });+ expect(ring.apply(lineOf("say: "), { type: "ctrl-y" })).toEqual({+ text: "say: world",+ cursor: 11,+ });+ });++ it("cycles older kills with Alt+Y immediately after a yank", () => {+ const ring = new KillRing();+ ring.apply(lineOf("one"), { type: "ctrl-u" });+ ring.apply(lineOf("two"), { type: "ctrl-u" });+ const yanked = ring.apply(lineOf(""), { type: "ctrl-y" })!;+ expect(yanked).toEqual({ text: "two", cursor: 3 });+ const previous = ring.apply(yanked, { type: "alt-y" })!;+ expect(previous).toEqual({ text: "one", cursor: 3 });+ expect(ring.apply(previous, { type: "alt-y" })).toEqual({ text: "two", cursor: 3 });+ });++ it("preserves suffixes joined to yanked text when cycling", () => {+ const ring = new KillRing();+ ring.apply(lineOf("x"), { type: "ctrl-u" });+ ring.apply(lineOf("π¨β"), { type: "ctrl-u" });+ const yanked = ring.apply({ text: "π©", cursor: 0 }, { type: "ctrl-y" })!;+ expect(yanked).toEqual({ text: "π¨βπ©", cursor: "π¨βπ©".length });+ expect(ring.apply(yanked, { type: "alt-y" })).toEqual({ text: "xπ©", cursor: 1 });+ });++ it("preserves suffixes combined with yanked text when cycling", () => {+ const ring = new KillRing();+ ring.apply(lineOf("x"), { type: "ctrl-u" });+ ring.apply(lineOf("e"), { type: "ctrl-u" });+ const yanked = ring.apply({ text: "\u0301lan", cursor: 0 }, { type: "ctrl-y" })!;+ expect(yanked).toEqual({ text: "e\u0301lan", cursor: 2 });+ expect(ring.apply(yanked, { type: "alt-y" })).toEqual({ text: "x\u0301lan", cursor: 2 });+ });++ it("only yank-pops immediately after a yank", () => {+ const ring = new KillRing();+ ring.apply(lineOf("one"), { type: "ctrl-u" });+ ring.apply(lineOf("two"), { type: "ctrl-u" });+ const yanked = ring.apply(lineOf(""), { type: "ctrl-y" })!;+ const moved = ring.apply(yanked, { type: "left" })!;+ expect(ring.apply(moved, { type: "alt-y" })).toBe(moved);+ });++ it("interrupts yank-pop for controller-owned keys", () => {+ const ring = new KillRing();+ ring.apply(lineOf("one"), { type: "ctrl-u" });+ ring.apply(lineOf("two"), { type: "ctrl-u" });+ const yanked = ring.apply(lineOf(""), { type: "ctrl-y" })!;+ ring.interruptYank({ type: "ctrl-r" });+ expect(ring.apply(yanked, { type: "alt-y" })).toBe(yanked);+ });++ it("ignores empty kills and caps the ring", () => {+ const ring = new KillRing(1);+ expect(ring.apply(lineOf(""), { type: "ctrl-k" })).toEqual(lineOf(""));+ ring.apply(lineOf("one"), { type: "ctrl-u" });+ ring.apply(lineOf("two"), { type: "ctrl-u" });+ expect(ring.apply(lineOf(""), { type: "ctrl-y" })).toEqual({ text: "two", cursor: 3 });+ });++ it("yanks at the caret and preserves grapheme boundaries", () => {+ const ring = new KillRing();+ ring.apply(lineOf("π¨βπ©βπ§βπ¦"), { type: "ctrl-u" });++ expect(ring.apply({ text: "Aπ©B", cursor: 1 }, { type: "ctrl-y" })).toEqual({+ text: "Aπ¨βπ©βπ§βπ¦π©B",+ cursor: 1 + "π¨βπ©βπ§βπ¦".length,+ });+ });++ it("leaves yank keys untouched when no kill is available", () => {+ const ring = new KillRing();+ const line = { text: "abc", cursor: 1 };++ expect(ring.apply(line, { type: "ctrl-y" })).toBe(line);+ expect(ring.apply(line, { type: "alt-y" })).toBe(line);+ });++ it("clears killed text at credential boundaries", () => {+ const ring = new KillRing();+ ring.apply(lineOf("secret"), { type: "ctrl-u" });+ ring.clear();+ const line = lineOf("");+ expect(ring.apply(line, { type: "ctrl-y" })).toBe(line);+ });+it("routes editing keys while leaving controller keys unhandled", () => {const line = { text: "abc", cursor: 1 };@@ -192,6 +286,8 @@ describe("line editing", () => {});expect(applyLineEditorKey(line, { type: "enter" })).toBeUndefined();expect(applyLineEditorKey(line, { type: "escape" })).toBeUndefined();+ expect(applyLineEditorKey(line, { type: "ctrl-y" })).toBeUndefined();+ expect(applyLineEditorKey(line, { type: "alt-y" })).toBeUndefined();});});diff --git a/packages/eve/src/cli/dev/tui/line-editor.ts b/packages/eve/src/cli/dev/tui/line-editor.tsindex 7970f000d..bb648d105 100644--- a/packages/eve/src/cli/dev/tui/line-editor.ts+++ b/packages/eve/src/cli/dev/tui/line-editor.ts@@ -186,7 +186,7 @@ function logicalLineEnd(text: string, cursor: number): number {}/** Options for {@link applyLineEditorKey}. */-interface LineEditorOptions {+export interface LineEditorOptions {/** Single-line inputs flatten pasted newlines and ignore Shift+Enter. */readonly multiline?: boolean;}@@ -240,6 +240,92 @@ export function applyLineEditorKey(}}+interface YankState {+ readonly state: LineState;+ readonly start: number;+ readonly replacementEnd: number;+ readonly ringIndex: number;+}++/** Session-local killed text shared by the TUI's line editors. */+export class KillRing {+ readonly #entries: string[] = [];+ readonly #capacity: number;+ #yank?: YankState;++ constructor(capacity = 20) {+ this.#capacity = capacity;+ }++ clear(): void {+ this.#entries.length = 0;+ this.#yank = undefined;+ }++ interruptYank(key: TerminalKey): void {+ if (key.type !== "ctrl-y" && key.type !== "alt-y") this.#yank = undefined;+ }++ apply(state: LineState, key: TerminalKey, options?: LineEditorOptions): LineState | undefined {+ if (key.type === "ctrl-y") return this.#yankLatest(state);+ if (key.type === "alt-y") return this.#yankPrevious(state);++ this.#yank = undefined;+ const next = applyLineEditorKey(state, key, options);+ if (next !== undefined) this.#record(killedText(state, next, key));+ return next;+ }++ #record(text: string): void {+ if (text.length === 0 || this.#capacity <= 0) return;+ this.#entries.unshift(text);+ if (this.#entries.length > this.#capacity) this.#entries.length = this.#capacity;+ }++ #yankLatest(state: LineState): LineState {+ const text = this.#entries[0];+ if (text === undefined) {+ this.#yank = undefined;+ return state;+ }+ const next = insert(state, text);+ this.#yank = {+ state: next,+ start: state.cursor,+ replacementEnd: state.cursor + text.length,+ ringIndex: 0,+ };+ return next;+ }++ #yankPrevious(state: LineState): LineState {+ const yank = this.#yank;+ if (yank === undefined || this.#entries.length < 2 || state !== yank.state) return state;+ const ringIndex = (yank.ringIndex + 1) % this.#entries.length;+ const text = this.#entries[ringIndex]!;+ const replacementEnd = yank.start + text.length;+ const nextText = state.text.slice(0, yank.start) + text + state.text.slice(yank.replacementEnd);+ const next = {+ text: nextText,+ cursor: graphemeBoundaryAtOrAfter(nextText, replacementEnd),+ };+ this.#yank = { state: next, start: yank.start, replacementEnd, ringIndex };+ return next;+ }+}++function killedText(before: LineState, after: LineState, key: TerminalKey): string {+ switch (key.type) {+ case "ctrl-k":+ return before.text.slice(before.cursor, logicalLineEnd(before.text, before.cursor));+ case "ctrl-u":+ case "ctrl-w":+ return before.text.slice(after.cursor, before.cursor);+ default:+ return "";+ }+}+function isWhitespace(text: string): boolean {return /^\s+$/u.test(text);}diff --git a/packages/eve/src/cli/dev/tui/stream-format.test.ts b/packages/eve/src/cli/dev/tui/stream-format.test.tsindex 81eff40e7..10be0e86f 100644--- a/packages/eve/src/cli/dev/tui/stream-format.test.ts+++ b/packages/eve/src/cli/dev/tui/stream-format.test.ts@@ -101,9 +101,11 @@ describe("nextKey", () => {expect(nextKey("\x1bO")).toEqual({ consumed: 0, incomplete: true });});- it("decodes Alt+B/F and Alt+Backspace as complete readline chords", () => {+ it("decodes Alt+B/F/Y and Alt+Backspace as complete readline chords", () => {expect(nextKey("\x1bb")).toEqual({ key: { type: "alt-b" }, consumed: 2 });expect(nextKey("\x1bF")).toEqual({ key: { type: "alt-f" }, consumed: 2 });+ expect(nextKey("\x1by")).toEqual({ key: { type: "alt-y" }, consumed: 2 });+ expect(nextKey("\x1bY")).toEqual({ key: { type: "alt-y" }, consumed: 2 });expect(nextKey("\x1b\x7f")).toEqual({ key: { type: "alt-backspace" }, consumed: 2 });expect(nextKey("\x1b\b")).toEqual({ key: { type: "alt-backspace" }, consumed: 2 });});@@ -201,11 +203,12 @@ describe("parseKey", () => {expect(parseKey(Buffer.from("\r"))).toEqual({ type: "enter" });});- it("decodes ctrl-b/f/n/p for emacs-style navigation", () => {+ it("decodes ctrl-b/f/n/p/y for emacs-style editing", () => {expect(parseKey(Buffer.from("\u0002"))).toEqual({ type: "ctrl-b" });expect(parseKey(Buffer.from("\u0006"))).toEqual({ type: "ctrl-f" });expect(parseKey(Buffer.from("\u000e"))).toEqual({ type: "ctrl-n" });expect(parseKey(Buffer.from("\u0010"))).toEqual({ type: "ctrl-p" });+ expect(parseKey(Buffer.from("\u0019"))).toEqual({ type: "ctrl-y" });});it("decodes common modified-arrow word-movement sequences", () => {diff --git a/packages/eve/src/cli/dev/tui/stream-format.ts b/packages/eve/src/cli/dev/tui/stream-format.tsindex dde5528b1..32c7533ae 100644--- a/packages/eve/src/cli/dev/tui/stream-format.ts+++ b/packages/eve/src/cli/dev/tui/stream-format.ts@@ -42,11 +42,13 @@ export type TerminalKey =| { type: "alt-b" }| { type: "alt-f" }| { type: "alt-backspace" }+ | { type: "alt-y" }| { type: "ctrl-k" }| { type: "ctrl-n" }| { type: "ctrl-p" }| { type: "ctrl-u" }| { type: "ctrl-w" }+ | { type: "ctrl-y" }| { type: "ctrl-l" }| { type: "ctrl-r" }| { type: "ctrl-c" }@@ -195,6 +197,9 @@ export function nextKey(buffer: string): KeyToken {if (second === "f" || second === "F") {return { key: { type: "alt-f" }, consumed: 2 };}+ if (second === "y" || second === "Y") {+ return { key: { type: "alt-y" }, consumed: 2 };+ }if (second === "\x7f" || second === "\b") {return { key: { type: "alt-backspace" }, consumed: 2 };}@@ -262,6 +267,8 @@ export function parseKey(chunk: Buffer): TerminalKey {return { type: "ctrl-u" };case "\u0017":return { type: "ctrl-w" };+ case "\u0019":+ return { type: "ctrl-y" };case "\u0003":return { type: "ctrl-c" };case "\r":diff --git a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts b/packages/eve/src/cli/dev/tui/terminal-renderer.test.tsindex e61e8ba3d..d5d1b474d 100644--- a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts+++ b/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts@@ -810,6 +810,23 @@ describe("TerminalRenderer (inline scrollback)", () => {renderer.shutdown();});+ it("does not yank-pop after a controller-owned repaint key", async () => {+ const { input, renderer } = makeRenderer();++ const prompt = renderer.readPrompt();+ input.type("one");+ input.send("\u0015"); // Ctrl+U+ input.type("two");+ input.send("\u0015"); // Ctrl+U+ input.send("\u0019"); // Ctrl+Y inserts "two"+ input.send("\u0012"); // Ctrl+R interrupts yank-pop+ input.send("\x1by"); // Alt+Y must not replace it with "one"+ input.enter();++ expect(await prompt).toBe("two");+ renderer.shutdown();+ });+it("windows a line longer than the terminal around the caret", async () => {const { screen, input, renderer } = makeRenderer(20); // narrow terminaldiff --git a/packages/eve/src/cli/dev/tui/terminal-renderer.ts b/packages/eve/src/cli/dev/tui/terminal-renderer.tsindex 137772e76..ef5680b31 100644--- a/packages/eve/src/cli/dev/tui/terminal-renderer.ts+++ b/packages/eve/src/cli/dev/tui/terminal-renderer.ts@@ -108,6 +108,7 @@ import { buildAgentHeader } from "./agent-header.js";import { detectMarkdownRendering } from "./markdown.js";import {EMPTY_LINE,+ KillRing,PromptHistory,applyLineEditorKey,deleteForward,@@ -469,6 +470,7 @@ export class TerminalRenderer implements AgentTUIRenderer {#inputText = "";#inputCursor = 0;readonly #promptHistory = new PromptHistory();+ readonly #killRing = new KillRing();#inputActive = false;/*** Command suggestions for the prompt draft. Only `readPrompt` sets this β@@ -755,7 +757,7 @@ export class TerminalRenderer implements AgentTUIRenderer {this.#flowInterrupt("ctrl-c");return;}- const edited = applyLineEditorKey(editor, key, { multiline: true });+ const edited = this.#killRing.apply(editor, key, { multiline: true });if (edited !== undefined) {apply(edited);return;@@ -863,7 +865,7 @@ export class TerminalRenderer implements AgentTUIRenderer {}// Chat keeps pasted newlines and honors Shift+Enter. Setup-panel inputs// stay single-line; freeform questions opt in separately below.- const edited = applyLineEditorKey(editor, key, { multiline: true });+ const edited = this.#killRing.apply(editor, key, { multiline: true });if (edited !== undefined) {apply(edited);return;@@ -1428,7 +1430,7 @@ export class TerminalRenderer implements AgentTUIRenderer {break;default: {if (isOnFreeformRow()) {- const edited = applyLineEditorKey(editor, key);+ const edited = this.#killRing.apply(editor, key);if (edited !== undefined) {editor = edited;this.#showCaret();@@ -1454,7 +1456,7 @@ export class TerminalRenderer implements AgentTUIRenderer {return;}- const edited = applyLineEditorKey(editor, key, { multiline: true });+ const edited = this.#killRing.apply(editor, key, { multiline: true });if (edited !== undefined) {editor = edited;this.#showCaret();@@ -2327,7 +2329,7 @@ export class TerminalRenderer implements AgentTUIRenderer {}if (!onEditableRow()) return;- const edited = applyLineEditorKey(editor, key);+ const edited = this.#killRing.apply(editor, key);if (edited !== undefined) applyEditor(edited);},() => this.#stopCaretBlink(),@@ -2338,6 +2340,7 @@ export class TerminalRenderer implements AgentTUIRenderer {async #readProviderPicker(opts: ProviderPickerRequest,): Promise<ProviderPickerChoice | undefined> {+ this.#killRing.clear();const flow = this.#beginSetupQuestion(opts.message);let interaction = initialProviderPickerState(opts.options, opts.initialValue);let validation: AbortController | undefined;@@ -2457,6 +2460,7 @@ export class TerminalRenderer implements AgentTUIRenderer {}if (interaction.phase.kind !== "editing" && interaction.phase.kind !== "invalid") return;+ // Provider credentials must never enter the session kill ring.const edited = applyLineEditorKey(interaction.phase.editor, key);if (edited !== undefined) {this.#showCaret();@@ -2548,6 +2552,7 @@ export class TerminalRenderer implements AgentTUIRenderer {validate?: (value: string) => string | undefined;notices?: readonly SelectNotice[];}): Promise<string | undefined> {+ if (opts.mask === true) this.#killRing.clear();const flow = this.#beginSetupQuestion(opts.message);let editor: LineState = lineOf("");@@ -2577,7 +2582,9 @@ export class TerminalRenderer implements AgentTUIRenderer {this.#paint();};- const edited = applyLineEditorKey(editor, key);+ const edited = opts.mask+ ? applyLineEditorKey(editor, key)+ : this.#killRing.apply(editor, key);if (edited !== undefined) {apply(edited);return;@@ -3214,7 +3221,7 @@ export class TerminalRenderer implements AgentTUIRenderer {this.#keyFlushTimer = setTimeout(() => {if (this.#keyBuffer !== "\x1b") return;this.#keyBuffer = "";- this.#consumeKey?.({ type: "escape" });+ this.#dispatchKey({ type: "escape" });}, escFlushMs);this.#keyFlushTimer.unref?.();return;@@ -3229,7 +3236,7 @@ export class TerminalRenderer implements AgentTUIRenderer {const value = sanitizePastedText(stripPasteStart(stuck));this.#keyBuffer = "";if (value.length > 0) {- this.#consumeKey?.({ type: "text", value, framing: "bracketed-paste" });+ this.#dispatchKey({ type: "text", value, framing: "bracketed-paste" });}}, incompletePasteFlushMs);this.#keyFlushTimer.unref?.();@@ -3247,6 +3254,11 @@ export class TerminalRenderer implements AgentTUIRenderer {}}+ #dispatchKey(key: TerminalKey): void {+ this.#killRing.interruptYank(key);+ this.#consumeKey?.(key);+ }+#drainKeys() {while (this.#keyBuffer.length > 0) {const token = nextKey(this.#keyBuffer);@@ -3259,7 +3271,7 @@ export class TerminalRenderer implements AgentTUIRenderer {this.#handleOscReply(token.key.value);continue;}- this.#consumeKey?.(token.key);+ this.#dispatchKey(token.key);}}@@ -3343,7 +3355,7 @@ export class TerminalRenderer implements AgentTUIRenderer {break;}default: {- const edited = applyLineEditorKey(this.#streamDraft, key, { multiline: true });+ const edited = this.#killRing.apply(this.#streamDraft, key, { multiline: true });if (edited !== undefined) {this.#streamDraft = edited;this.#paint();