/** * Prompt Saver Extension * * Saves the current editor text when Ctrl+C clears the input, * so accidentally discarded prompts can be retrieved with the up arrow. * * - Ctrl+C: saves the current text before clearing the editor * - Up arrow: if editor is empty and a saved prompt exists, restores it * - Subsequent up arrow presses go through normal history as usual */ import { CustomEditor, type ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { matchesKey, Key } from "@earendil-works/pi-tui"; // Module-level state: the most recently cleared prompt let savedPrompt: string | null = null; class PromptSaverEditor extends CustomEditor { handleInput(data: string): void { // Intercept Ctrl+C: save the current text before it gets cleared if (matchesKey(data, Key.ctrl("c"))) { const text = this.getText(); if (text) { savedPrompt = text; } // Let the default handler clear the editor (or copy if there's a selection) super.handleInput(data); return; } // Intercept Up arrow: restore saved prompt if editor is empty if (matchesKey(data, Key.up)) { if (!this.getText() && savedPrompt) { this.setText(savedPrompt); savedPrompt = null; return; // Don't pass to super — we handled it } // Editor has text or no saved prompt: normal up behavior (cursor up / history) } // Pass everything else to the default editor behavior super.handleInput(data); } } export default function (pi: ExtensionAPI) { pi.on("session_start", (_event, ctx) => { // Only install in TUI mode (not print/json/rpc) if (ctx.mode !== "tui") return; ctx.ui.setEditorComponent((tui, theme, kb) => new PromptSaverEditor(tui, theme, kb)); }); }