The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { chunkTerminalWrite } from "@t3tools/contracts"; |
| 2 | |
| 3 | /** Sends one chunk of terminal input; resolves false when it failed and the rest should drop. */ |
| 4 | type SendTerminalChunk = (chunk: string) => Promise<boolean | "not-dispatched">; |
| 5 | |
| 6 | /** |
| 7 | * Orders one terminal's input into writes that fit `terminal.write`. A split write goes one chunk |
| 8 | * per round trip and later input waits behind it; other writes go out immediately, like keystrokes. |
| 9 | */ |
| 10 | export function createTerminalInputWriter(onIdle = () => {}) { |
| 11 | let pending: Promise<void> | null = null; |
| 12 | const immediate = new Set<Promise<void>>(); |
| 13 | |
| 14 | let closeInterruptedPaste: (() => Promise<boolean>) | null = null; |
| 15 | const closePaste = async () => { |
| 16 | if (closeInterruptedPaste === null) return true; |
| 17 | if (!(await closeInterruptedPaste())) return false; |
| 18 | closeInterruptedPaste = null; |
| 19 | return true; |
| 20 | }; |
| 21 | |
| 22 | const sendChunks = async ( |
| 23 | data: string, |
| 24 | chunks: ReadonlyArray<string>, |
| 25 | send: SendTerminalChunk, |
| 26 | recover: () => Promise<boolean>, |
| 27 | interrupted: () => void, |
| 28 | ) => { |
| 29 | if (closeInterruptedPaste !== null && !(await closePaste())) { |
| 30 | interrupted(); |
| 31 | return; |
| 32 | } |
| 33 | const bracketed = |
| 34 | chunks.length > 1 && data.startsWith("\x1b[200~") && data.endsWith("\x1b[201~"); |
| 35 | let opened = false; |
| 36 | try { |
| 37 | for (const chunk of chunks) { |
| 38 | const result = await send(chunk); |
| 39 | if (result !== true) { |
| 40 | if (result !== "not-dispatched") opened = bracketed; |
| 41 | if (chunks.length > 1) interrupted(); |
| 42 | return; |
| 43 | } |
| 44 | opened = bracketed; |
| 45 | } |
| 46 | opened = false; |
| 47 | } finally { |
| 48 | if (opened) { |
| 49 | closeInterruptedPaste = recover; |
| 50 | await closePaste(); |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | return ( |
| 56 | data: string, |
| 57 | send: SendTerminalChunk, |
| 58 | recover: () => Promise<boolean> = async () => (await send("\x1b[201~")) === true, |
| 59 | interrupted = () => {}, |
| 60 | ): Promise<void> => { |
| 61 | const idle = () => { |
| 62 | if (pending === null && immediate.size === 0 && closeInterruptedPaste === null) onIdle(); |
| 63 | }; |
| 64 | const chunks = chunkTerminalWrite(data); |
| 65 | const run = () => sendChunks(data, chunks, send, recover, interrupted); |
| 66 | if (pending === null && closeInterruptedPaste === null && chunks.length === 1) { |
| 67 | const current = run(); |
| 68 | immediate.add(current); |
| 69 | void current.then( |
| 70 | () => { |
| 71 | immediate.delete(current); |
| 72 | idle(); |
| 73 | }, |
| 74 | () => { |
| 75 | immediate.delete(current); |
| 76 | idle(); |
| 77 | }, |
| 78 | ); |
| 79 | return current; |
| 80 | } |
| 81 | const previous = pending ?? (immediate.size > 0 ? Promise.allSettled(immediate) : null); |
| 82 | const current = previous === null ? run() : previous.then(run, run); |
| 83 | pending = current; |
| 84 | const reset = () => { |
| 85 | if (pending === current) pending = null; |
| 86 | idle(); |
| 87 | }; |
| 88 | void current.then(reset, reset); |
| 89 | return current; |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | const writers = new Map<string, ReturnType<typeof createTerminalInputWriter>>(); |
| 94 | |
| 95 | /** Retain ordering and framing obligations until acknowledged, across terminal view remounts. */ |
| 96 | export function terminalInputWriter(key: string, retain: () => () => void) { |
| 97 | const existing = writers.get(key); |
| 98 | if (existing) return existing; |
| 99 | const release = retain(); |
| 100 | const writer = createTerminalInputWriter(() => { |
| 101 | if (writers.get(key) === writer) writers.delete(key); |
| 102 | release(); |
| 103 | }); |
| 104 | writers.set(key, writer); |
| 105 | return writer; |
| 106 | } |