TypeScript
fix(web): honor the send shortcut for follow-up actions (#20929)
David Atkins4309a0565277
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 8aa649373af..5ec681b483a 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -174,6 +174,7 @@ import { useActiveThreadRef } from "../hooks/useActiveThreadRef";import {type ComposerSubmissionIntent,collapseExpandedComposerCursor,+ composerRequiresModifier,parseStandaloneComposerSlashCommand,} from "../composer-logic";import {@@ -5480,6 +5481,7 @@ export default function ChatView(props: ChatViewProps) {composerFocus: document.activeElement?.getAttribute("data-testid") === "composer-editor",draftThreadRoute: routeKind === "draft",turnRunning: phase === "running",+ composerRequiresModifier: composerRequiresModifier(settings.sendShortcut, promptRef.current),isWeb: !isElectron,isDesktop: isElectron,});diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex e3be725e1cf..2fe74cfc430 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -83,6 +83,7 @@ import {type ComposerTrigger,type ComposerSubmissionIntent,collapseExpandedComposerCursor,+ composerRequiresModifier,composerSubmissionIntentForKey,composerStateAtPromptEnd,detectComposerTrigger,@@ -7232,6 +7233,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)composerFocus: true,draftThreadRoute: routeKind === "draft",turnRunning: true,+ composerRequiresModifier: composerRequiresModifier(+ settings.sendShortcut,+ prompt,+ ),},},)}diff --git a/apps/web/src/composer-logic.test.ts b/apps/web/src/composer-logic.test.tsindex d3aee7e52d7..be114ed78d9 100644--- a/apps/web/src/composer-logic.test.ts+++ b/apps/web/src/composer-logic.test.ts@@ -102,10 +102,25 @@ describe("composerSubmissionIntentForKey", () => {metaKey: platform === "MacIntel",ctrlKey: platform !== "MacIntel",};- for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {- const running = { ...input, platform, sendShortcut, prompt: "two\nlines", isRunning: true };+ for (const [sendShortcut, prompt, requiresModifier] of [+ ["enter", "one line", false],+ ["enter", "two\nlines", false],+ ["mod-enter", "one line", true],+ ["mod-enter", "two\nlines", true],+ ["mod-enter-multiline", "one line", false],+ ["mod-enter-multiline", "two\nlines", true],+ ] as const) {+ const running = { ...input, platform, sendShortcut, prompt, isRunning: true };const intent = composerSubmissionIntentForKey({ ...running, event: modEnter });- expect(intent).toBe("alternate");+ expect(intent).toBe(requiresModifier ? "foreground" : "alternate");+ const alternateIntent = composerSubmissionIntentForKey({+ ...running,+ event: { ...modEnter, shiftKey: requiresModifier },+ });+ expect(alternateIntent).toBe("alternate");+ expect(composerSubmissionIntentForKey({ ...running, event: enter })).toBe(+ requiresModifier ? null : "foreground",+ );for (const activeTurnDefault of ["queue", "steer"] as const) {expect(resolveComposerDispatchMode({@@ -113,6 +128,15 @@ describe("composerSubmissionIntentForKey", () => {alternateModifier: intent === "alternate",activeTurnDefault,}),+ ).toBe(+ requiresModifier ? activeTurnDefault : activeTurnDefault === "queue" ? "steer" : "queue",+ );+ expect(+ resolveComposerDispatchMode({+ running: true,+ alternateModifier: alternateIntent === "alternate",+ activeTurnDefault,+ }),).toBe(activeTurnDefault === "queue" ? "steer" : "queue");}expect(@@ -137,6 +161,7 @@ describe("composerSubmissionIntentForKey", () => {...input,platform,sendShortcut,+ prompt,isDraftThread: true,event: modEnter,}),@@ -157,6 +182,28 @@ describe("composerSubmissionIntentForKey", () => {).toBeNull();});+ it("honors the send preference with an existing install's saved alternate binding", () => {+ const keybindings = mergeWithDefaultKeybindings(+ compileResolvedKeybindingsConfig([+ {+ key: "mod+enter",+ command: "composer.sendAlternate",+ when: "composerFocus && turnRunning",+ },+ ]),+ );+ const running = { ...input, keybindings, isRunning: true, sendShortcut: "mod-enter" as const };+ expect(composerSubmissionIntentForKey({ ...running, event: { ...enter, ctrlKey: true } })).toBe(+ "foreground",+ );+ expect(+ composerSubmissionIntentForKey({+ ...running,+ event: { ...enter, ctrlKey: true, shiftKey: true },+ }),+ ).toBe("alternate");+ });+it("does not start a background thread with the queued-message shortcut", () => {expect(composerSubmissionIntentForKey({@@ -186,54 +233,64 @@ describe("composerSubmissionIntentForKey", () => {).toBe("background");});- it("uses remapped keys and removes the old action bindings", () => {- const keybindings = mergeWithDefaultKeybindings(- compileResolvedKeybindingsConfig([- {- key: "alt+q",- command: "composer.sendAlternate",- when: "composerFocus && turnRunning",- },- {- key: "alt+b",- command: "composer.sendBackground",- when: "composerFocus && draftThreadRoute",- },- ]),- );- const custom = { ...input, keybindings };- expect(- composerSubmissionIntentForKey({- ...custom,- isRunning: true,- event: { ...enter, key: "q", altKey: true },- }),- ).toBe("alternate");- expect(- composerSubmissionIntentForKey({- ...custom,- isDraftThread: true,- event: { ...enter, key: "b", altKey: true },- }),- ).toBe("background");- expect(- composerSubmissionIntentForKey({- ...custom,- isRunning: true,- event: { ...enter, ctrlKey: true },- }),- ).toBe("foreground");- expect(- composerSubmissionIntentForKey({- ...custom,- isDraftThread: true,- event: { ...enter, ctrlKey: true, shiftKey: true },- }),- ).toBeNull();- expect(- composerSubmissionIntentForKey({ ...custom, event: { ...enter, key: "b", altKey: true } }),- ).toBeNull();- });+ it.each(["enter", "mod-enter", "mod-enter-multiline"] as const)(+ "uses remapped keys and removes the old action bindings with %s",+ (sendShortcut) => {+ const keybindings = mergeWithDefaultKeybindings(+ compileResolvedKeybindingsConfig([+ {+ key: "alt+q",+ command: "composer.sendAlternate",+ when: "composerFocus && turnRunning",+ },+ {+ key: "alt+b",+ command: "composer.sendBackground",+ when: "composerFocus && draftThreadRoute",+ },+ ]),+ );+ const custom = { ...input, keybindings, sendShortcut, prompt: "two\nlines" };+ expect(+ composerSubmissionIntentForKey({+ ...custom,+ isRunning: true,+ event: { ...enter, key: "q", altKey: true },+ }),+ ).toBe("alternate");+ expect(+ composerSubmissionIntentForKey({+ ...custom,+ isDraftThread: true,+ event: { ...enter, key: "b", altKey: true },+ }),+ ).toBe("background");+ expect(+ composerSubmissionIntentForKey({+ ...custom,+ isRunning: true,+ event: { ...enter, ctrlKey: true },+ }),+ ).toBe("foreground");+ expect(+ composerSubmissionIntentForKey({+ ...custom,+ isDraftThread: true,+ event: { ...enter, ctrlKey: true, shiftKey: true },+ }),+ ).toBeNull();+ expect(+ composerSubmissionIntentForKey({+ ...custom,+ isRunning: true,+ event: { ...enter, ctrlKey: true, shiftKey: true },+ }),+ ).toBeNull();+ expect(+ composerSubmissionIntentForKey({ ...custom, event: { ...enter, key: "b", altKey: true } }),+ ).toBeNull();+ },+ );it.each([{ isMobileViewport: true },diff --git a/apps/web/src/composer-logic.ts b/apps/web/src/composer-logic.tsindex c56ea4a4a21..80478a7aae5 100644--- a/apps/web/src/composer-logic.ts+++ b/apps/web/src/composer-logic.ts@@ -27,7 +27,7 @@ export function formatAssistantCitationForComposer(citation: AssistantCitation,return `${serializeAssistantCitation(withAssistantCitationComment(citation, comment))} `;}-function composerRequiresModifier(+export function composerRequiresModifier(sendShortcut: ClientSettings["sendShortcut"] | undefined,prompt: string,) {@@ -56,6 +56,7 @@ export function composerSubmissionIntentForKey(input: {composerFocus: true,draftThreadRoute: input.isDraftThread,turnRunning: input.isRunning === true,+ composerRequiresModifier: composerRequiresModifier(input.sendShortcut, input.prompt ?? ""),},});if (command === "composer.sendAlternate" && input.isRunning) return "alternate";diff --git a/apps/web/src/keybindings.test.ts b/apps/web/src/keybindings.test.tsindex 24b424c3e66..f66f306fc3e 100644--- a/apps/web/src/keybindings.test.ts+++ b/apps/web/src/keybindings.test.ts@@ -1453,6 +1453,48 @@ describe("composer and pull request shortcuts", () => {["Enter", "thread.steerQueuedMessage"],] as const;+ it.each(["MacIntel", "Win32", "Linux"])(+ "reserves the alternate send chord for the focused composer on %s",+ (platform) => {+ const modifier = {+ metaKey: platform === "MacIntel",+ ctrlKey: platform !== "MacIntel",+ };+ const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });+ for (const composerRequiresModifier of [false, true]) {+ const context = {+ composerFocus: true,+ turnRunning: true,+ composerRequiresModifier,+ };+ assert.strictEqual(+ resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {+ platform,+ context,+ }),+ composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",+ );+ assert.strictEqual(+ shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {+ platform,+ context,+ }),+ formatShortcutLabel(+ modShortcut("enter", { shiftKey: composerRequiresModifier }),+ platform,+ ),+ );+ assert.strictEqual(+ resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {+ platform,+ context: { ...context, composerFocus: false },+ }),+ "thread.steerQueuedMessage",+ );+ }+ },+ );+it.each(["MacIntel", "Win32", "Linux"])("separates queued steering and background start on %s",(platform) => {diff --git a/apps/web/src/keybindings.ts b/apps/web/src/keybindings.tsindex 30fe79060d5..3fe5486aed1 100644--- a/apps/web/src/keybindings.ts+++ b/apps/web/src/keybindings.ts@@ -2,6 +2,7 @@ import {type KeybindingCommand,type KeybindingShortcut,type KeybindingWhenNode,+ type ResolvedKeybindingRule,MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,type ResolvedKeybindingsConfig,THREAD_JUMP_KEYBINDING_COMMANDS,@@ -104,6 +105,25 @@ export function shortcutConflictKey(].join("|");}+function shortcutForContext(binding: ResolvedKeybindingRule, context: ShortcutMatchContext) {+ const shortcut = binding.shortcut;+ // Existing installs persist Mod+Enter. Resolve its alternate chord against+ // the send preference without rewriting saved or remapped shortcuts.+ if (+ context.composerRequiresModifier &&+ binding.command === "composer.sendAlternate" &&+ shortcut.key === "enter" &&+ shortcut.modKey &&+ !shortcut.metaKey &&+ !shortcut.ctrlKey &&+ !shortcut.altKey &&+ !shortcut.shiftKey+ ) {+ return { ...shortcut, shiftKey: true };+ }+ return shortcut;+}+export function effectiveShortcutsForCommand(keybindings: ResolvedKeybindingsConfig,command: KeybindingCommand,@@ -119,14 +139,15 @@ export function effectiveShortcutsForCommand(if (!binding) continue;if (!matchesWhenClause(binding.whenAst, context)) continue;- const conflictKey = shortcutConflictKey(binding.shortcut, platform);+ const shortcut = shortcutForContext(binding, context);+ const conflictKey = shortcutConflictKey(shortcut, platform);if (claimedShortcuts.has(conflictKey)) {continue;}claimedShortcuts.add(conflictKey);if (binding.command === command) {- effective.push(binding.shortcut);+ effective.push(shortcut);}}@@ -162,7 +183,7 @@ export function resolveShortcutCommand(const binding = keybindings[index];if (!binding) continue;if (!matchesWhenClause(binding.whenAst, context)) continue;- if (!matchesKeybindingShortcut(event, binding.shortcut, platform)) continue;+ if (!matchesKeybindingShortcut(event, shortcutForContext(binding, context), platform)) continue;return binding.command;}return null;diff --git a/docs/user/composer.md b/docs/user/composer.mdindex 3492e1612a1..5c5335350e5 100644--- a/docs/user/composer.md+++ b/docs/user/composer.md@@ -55,12 +55,17 @@ On web and desktop, choose **Settings → General → Follow-up behavior** to qunew messages for a later turn or steer the running turn immediately. The settingapplies to this client; already queued messages keep their place. Queued messagesare saved on the server and can be edited, reordered, or removed above the composer.-`Cmd+Enter` on macOS or `Ctrl+Enter` on Windows and Linux uses the opposite action:-it steers when your default is Queue and queues when your default is Steer.+When Enter sends, `Cmd+Enter` on macOS or `Ctrl+Enter` on Windows and Linux uses+the opposite action: it steers when your default is Queue and queues when your+default is Steer.+If your **Send shortcut** requires `Cmd+Enter` or `Ctrl+Enter`, that shortcut uses+your default follow-up behavior; add **Shift** to use the opposite action. This+applies only to multiline messages when you choose the multiline send option.Use `Cmd+Shift+Enter` on macOS or `Ctrl+Shift+Enter` on Windows and Linux to sendthe oldest queued message as a steer. This leaves the current draft intact and-requires an active turn that supports steering. Change+requires an active turn that supports steering. If that shortcut sends the opposite+action for your draft, move focus outside the composer to steer the queued message. Change`thread.steerQueuedMessage` in **Settings → Keybindings** to use another shortcut.Press `Option+Up` on macOS or `Alt+Up` on Windows and Linux with the cursor at the