diff --git a/.changeset/cjk-send-on-enter.md b/.changeset/cjk-send-on-enter.md index 9241ef1f..9caed2a2 100644 --- a/.changeset/cjk-send-on-enter.md +++ b/.changeset/cjk-send-on-enter.md @@ -2,4 +2,4 @@ "aicodeman": patch --- -Add a per-device "Send on Enter" mode for the pinned CJK input field, so phone users can scroll the terminal while a typed answer waits in the field until Enter. +Add a per-device "Send on Enter" mode for the pinned CJK input field, so phone users can scroll the terminal while a typed answer waits in the field until Enter. The phone toolbar's Enter button now also sends the CJK field's text while the field is visible, in live mode too. diff --git a/docs/wiki/Input-And-Voice.md b/docs/wiki/Input-And-Voice.md index c386a1ee..e5e97bf8 100644 --- a/docs/wiki/Input-And-Voice.md +++ b/docs/wiki/Input-And-Voice.md @@ -72,7 +72,9 @@ typed text stays in it until Enter, so you can scroll the terminal back to a que your answer before sending. Shift+Enter adds a line, Esc clears the field, and autocorrect is on. Enter always goes out as its own write, a moment after the text, to the session the answer was typed for: a single character is typed, so CLI menus that take a digit still work, and anything -longer goes like the prompt composer's Send, as one paste. The toolbar's Enter button sends the +longer goes like the prompt composer's Send, as one paste. The one exception is a lone digit while +a permission or question dialog is waiting: the digit answers the dialog by itself, so no Enter +follows it, which would otherwise confirm whatever the CLI shows next. The toolbar's Enter button sends the field too. With the keyboard bar's one-shot Ctrl armed, the next key you type goes out as a control key (Ctrl, then `c` is Ctrl+C) instead of being held. With the keyboard down the field stays one line tall. A draft stays with its session: switching tabs and coming back brings it back. diff --git a/src/web/public/app.js b/src/web/public/app.js index a86aafe7..94636082 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -10047,8 +10047,8 @@ class CodemanApp { this._cleanupPreviousSession(sessionId, { keepCjkDraft: reloadingSameSession }); this.activeSessionId = sessionId; - if (typeof CjkInput !== 'undefined') CjkInput.restoreDraft?.(sessionId); this._activateFileBrowserSession?.(sessionId); + if (typeof CjkInput !== 'undefined') CjkInput.restoreDraft?.(sessionId); // A keyboard row hold belongs to the session it was taken for: release it here // if this one resizes instead (the geometry work below then fits it). this._reconcileKeyboardHold?.({ resize: false }); diff --git a/src/web/public/terminal-ui.js b/src/web/public/terminal-ui.js index 13376a12..31bb0017 100644 --- a/src/web/public/terminal-ui.js +++ b/src/web/public/terminal-ui.js @@ -4577,8 +4577,13 @@ Object.assign(CodemanApp.prototype, { * single character is typed (a selection dialog ignores a pasted `3`, then * Enter confirms the highlighted option), anything else goes through the * composer's path as one bracketed paste. The session is captured now, so the - * delayed Enter cannot follow a tab switch. Returns false when nothing was - * sent (no session, too long), so the field keeps the text. + * delayed Enter cannot follow a tab switch. A lone digit while the session + * shows a permission or question dialog goes out without the Enter: the + * dialog acts on the digit itself, and a later Enter would confirm whatever + * the CLI shows next (a second permission prompt, unseen). Same rule as the + * Approvals Inbox and the Discord bridge (server.ts awaitingChoice). Returns + * false when nothing was sent (no session, too long), so the field keeps the + * text. */ _sendCjkPrompt(text) { const sessionId = this.activeSessionId; @@ -4594,6 +4599,12 @@ Object.assign(CodemanApp.prototype, { if ([...text].length === 1) { if (this._localEchoPolicy === 'predict') this._predictiveEcho?.clearPredictions(); this._sendInputAsync(sessionId, text); + const hooks = this.pendingHooks?.get(sessionId); + const awaitingChoice = !!hooks && (hooks.has('permission_prompt') || hooks.has('elicitation_dialog')); + if (/^\d$/.test(text) && awaitingChoice) { + _crashDiag.log(`CJK prompt digit answers a dialog, no Enter`); + return true; + } setTimeout(() => this._sendInputAsync(sessionId, '\r', { useMux: true }), 120); return true; } diff --git a/test/cjk-hold-per-device-setting.test.ts b/test/cjk-hold-per-device-setting.test.ts index 9f68a232..9e6a8d84 100644 --- a/test/cjk-hold-per-device-setting.test.ts +++ b/test/cjk-hold-per-device-setting.test.ts @@ -107,8 +107,10 @@ describe('a held draft survives a reload of the same session', () => { }); it('selectSession restores the draft of the session it lands on', () => { + // After _activateFileBrowserSession: test/file-browser-search.test.ts pins + // that call as the line right after the activeSessionId assignment. expect(read('app.js')).toContain( - "this.activeSessionId = sessionId;\n if (typeof CjkInput !== 'undefined') CjkInput.restoreDraft?.(sessionId);" + "this.activeSessionId = sessionId;\n this._activateFileBrowserSession?.(sessionId);\n if (typeof CjkInput !== 'undefined') CjkInput.restoreDraft?.(sessionId);" ); }); diff --git a/test/mobile-shell-keyboard.test.ts b/test/mobile-shell-keyboard.test.ts index aff9bdd8..833f2ac0 100644 --- a/test/mobile-shell-keyboard.test.ts +++ b/test/mobile-shell-keyboard.test.ts @@ -606,6 +606,64 @@ describe('Send on Enter: a held answer goes out with Enter as its own write', () } }); + it('sends a lone digit without Enter while a permission or question dialog waits', () => { + // The dialog acts on the digit keypress itself; an Enter 120 ms later would + // confirm whatever the CLI shows next (a second permission prompt, unseen). + // Same rule as the Approvals Inbox and the Discord bridge (awaitingChoice). + vi.useFakeTimers(); + try { + for (const hook of ['permission_prompt', 'elicitation_dialog']) { + const { app, composed } = promptApp(); + const writes: Array<[string, string, unknown]> = []; + (app as unknown as { _sendInputAsync: unknown })._sendInputAsync = ( + id: string, + input: string, + opts?: unknown + ) => writes.push([id, input, opts]); + (app as unknown as { pendingHooks: Map> }).pendingHooks = new Map([ + ['session-a', new Set([hook])], + ]); + expect(app._sendCjkPrompt('2')).toBe(true); + vi.advanceTimersByTime(500); + expect(writes).toEqual([['session-a', '2', undefined]]); + composed.mockRestore(); + } + } finally { + vi.useRealTimers(); + } + }); + + it('keeps the delayed Enter for a digit when only an idle prompt is pending, or for a letter in a dialog', () => { + vi.useFakeTimers(); + try { + const cases: Array<[string, string]> = [ + ['idle_prompt', '1'], + ['permission_prompt', 'y'], + ]; + for (const [hook, text] of cases) { + const { app, composed } = promptApp(); + const writes: Array<[string, string, unknown]> = []; + (app as unknown as { _sendInputAsync: unknown })._sendInputAsync = ( + id: string, + input: string, + opts?: unknown + ) => writes.push([id, input, opts]); + (app as unknown as { pendingHooks: Map> }).pendingHooks = new Map([ + ['session-a', new Set([hook])], + ]); + expect(app._sendCjkPrompt(text)).toBe(true); + vi.advanceTimersByTime(120); + expect(writes).toEqual([ + ['session-a', text, undefined], + ['session-a', '\r', { useMux: true }], + ]); + composed.mockRestore(); + } + } finally { + vi.useRealTimers(); + } + }); + it('pastes even a short word: only one character is typed', () => { const { app, composed } = promptApp(); expect(app._sendCjkPrompt('yes')).toBe(true);