diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index e4515f759fd7a..9506ebfa619f9 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -72,7 +72,7 @@ import { collapseExpandedComposerCursor, composerRequiresModifier, composerStateAtPromptEnd, - detectComposerTrigger, + detectComposerTrigger as detectComposerTriggerForProvider, expandCollapsedComposerCursor, formatAssistantCitationForComposer, isPathSuggestionCurrent, @@ -1004,6 +1004,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) : null, }); + const detectComposerTrigger = (text: string, cursor: number) => + detectComposerTriggerForProvider( + text, + cursor, + selectedProvider === "codex" || selectedProvider === "claudeAgent", + ); + const [composerCursor, setComposerCursor] = useState(() => { const value = isLiteralPendingAnswer ? (activePendingProgress?.customAnswer ?? prompt) : prompt; return collapseExpandedComposerCursor(value, value.length, isLiteralPendingAnswer); @@ -1142,6 +1149,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) isLoading: isComposerMenuLoading, emptyState: composerMenuEmptyState, } = useComposerMenuItems({ + goalObjective: props.activeThreadShell?.goal?.objective ?? null, composerTrigger, environmentId, linkedEnvironments, @@ -1926,7 +1934,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) } return; } - const replacement = `/${item.command.name} `; + const replacement = item.replacementText ?? `/${item.command.name} `; const replacementRangeEnd = extendReplacementRangeForTrailingSpace( snapshot.value, trigger.rangeEnd, @@ -1937,6 +1945,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }); if (applied) { setComposerHighlightedItemId(null); + if ( + item.command.name.startsWith("goal ") || + (item.command.name === "goal" && /^goal[\t ]/i.test(trigger.query)) + ) { + dismissComposerTrigger(trigger); + } } return; } diff --git a/apps/web/src/components/chat/ComposerCommandMenu.tsx b/apps/web/src/components/chat/ComposerCommandMenu.tsx index 0b9dda460d8ef..7977efb39033e 100644 --- a/apps/web/src/components/chat/ComposerCommandMenu.tsx +++ b/apps/web/src/components/chat/ComposerCommandMenu.tsx @@ -56,6 +56,7 @@ export type ComposerCommandItem = type: "provider-slash-command"; provider: ProviderDriverKind; command: ServerProviderSlashCommand; + replacementText?: string; label: string; description: string; } diff --git a/apps/web/src/components/chat/composerSlashCommandSearch.test.ts b/apps/web/src/components/chat/composerSlashCommandSearch.test.ts new file mode 100644 index 0000000000000..e26a9d2aa539d --- /dev/null +++ b/apps/web/src/components/chat/composerSlashCommandSearch.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from "vite-plus/test"; +import { ProviderDriverKind } from "@t3tools/contracts"; +import type { ComposerCommandItem } from "./ComposerCommandMenu"; +import { expandGoalSubcommands, searchSlashCommandItems } from "./composerSlashCommandSearch"; + +const goal: Extract = { + id: "provider-slash-command:codex:goal", + type: "provider-slash-command", + provider: ProviderDriverKind.make("codex"), + command: { name: "goal" }, + label: "/goal", + description: "Set a goal", +}; + +describe("goal subcommand completion", () => { + it("only offers native Claude goal commands", () => { + const items = expandGoalSubcommands( + [{ ...goal, provider: ProviderDriverKind.make("claudeAgent") }], + "goal", + "Fix it", + ); + expect(items.map((item) => item.label)).toEqual(["/goal", "/goal edit", "/goal clear"]); + }); + + it("does not assume another provider implements Codex goal commands", () => { + const other = { ...goal, provider: ProviderDriverKind.make("opencode") }; + expect(expandGoalSubcommands([other], "goal", "Fix it")).toEqual([other]); + }); + + it("waits for the full goal command and omits edit when no goal is set", () => { + expect(expandGoalSubcommands([goal], "go", "Fix it")).toEqual([goal]); + expect(expandGoalSubcommands([goal], "goal", null).map((item) => item.label)).not.toContain( + "/goal edit", + ); + }); + + it.each(["goal r", "goal r", "goal\tr", "GOAL R"])("completes %j", (query) => { + const items = expandGoalSubcommands([goal], query, "Fix the tests"); + expect(searchSlashCommandItems(items, query).map((item) => item.label)).toEqual([ + "/goal resume", + ]); + }); + + it("fills the current objective when editing", () => { + const items = expandGoalSubcommands([goal], "goal", "Fix the tests"); + expect(items.find((item) => item.label === "/goal edit")).toMatchObject({ + replacementText: "/goal Fix the tests", + }); + }); +}); diff --git a/apps/web/src/components/chat/composerSlashCommandSearch.ts b/apps/web/src/components/chat/composerSlashCommandSearch.ts index ba1aacb83de31..e9430b8785f84 100644 --- a/apps/web/src/components/chat/composerSlashCommandSearch.ts +++ b/apps/web/src/components/chat/composerSlashCommandSearch.ts @@ -12,6 +12,46 @@ type SlashSearchItem = Extract< { type: "slash-command" | "provider-slash-command" | "skill" } >; +export function expandGoalSubcommands( + items: ReadonlyArray, + query: string, + objective: string | null, +): SlashSearchItem[] { + if (!/^goal(?:\s|$)/i.test(query)) return [...items]; + return items.flatMap((item): SlashSearchItem[] => { + if ( + item.type !== "provider-slash-command" || + item.command.name !== "goal" || + (item.provider !== "codex" && item.provider !== "claudeAgent") + ) + return [item]; + const commands = [ + ...(objective === null ? [] : [{ name: "edit", description: "Edit the current goal" }]), + ...(item.provider === "codex" + ? [ + { name: "pause", description: "Pause the current goal" }, + { name: "resume", description: "Resume the current goal" }, + ] + : []), + { name: "clear", description: "Remove the current goal" }, + ]; + const prefix = query.slice("goal".length).trim().toLowerCase(); + return [ + item, + ...commands + .filter(({ name }) => name.startsWith(prefix)) + .map(({ name, description }) => ({ + ...item, + id: `${item.id}:${name}`, + command: { name: `goal ${name}`, description }, + label: `/goal ${name}`, + description, + ...(name === "edit" ? { replacementText: `/goal ${objective}` } : {}), + })), + ]; + }); +} + /** Provider commands, `/side`, `/btw` and `/clear` work only at the message start. */ export function slashCommandItemsForPromptPosition( items: ReadonlyArray, @@ -79,7 +119,10 @@ export function searchSlashCommandItems( items: ReadonlyArray, query: string, ): SlashSearchItem[] { - const normalizedQuery = normalizeSearchQuery(query, { trimLeadingPattern: /^\/+/ }); + const normalizedQuery = normalizeSearchQuery(query, { trimLeadingPattern: /^\/+/ }).replace( + /^goal[\t ]+/, + "goal ", + ); if (!normalizedQuery) { return [...items]; } diff --git a/apps/web/src/components/chat/useComposerMenuItems.ts b/apps/web/src/components/chat/useComposerMenuItems.ts index 741d129029330..246ed077986b0 100644 --- a/apps/web/src/components/chat/useComposerMenuItems.ts +++ b/apps/web/src/components/chat/useComposerMenuItems.ts @@ -44,6 +44,7 @@ import { } from "../pullRequest/pullRequestList.logic"; import type { ComposerCommandItem } from "./ComposerCommandMenu"; import { + expandGoalSubcommands, searchSlashCommandItems, slashCommandItemsForPromptPosition, } from "./composerSlashCommandSearch"; @@ -71,6 +72,7 @@ export function useComposerMenuItems({ selectedProvider, selectedProviderSkills, selectedProviderSlashCommands, + goalObjective, showSkillsInSlashMenu, workspaceSkillsScanning, questionAttachmentTarget, @@ -91,6 +93,7 @@ export function useComposerMenuItems({ readonly selectedProvider: ProviderDriverKind; readonly selectedProviderSkills: ServerProvider["skills"]; readonly selectedProviderSlashCommands: ServerProvider["slashCommands"]; + readonly goalObjective: string | null; readonly showSkillsInSlashMenu: boolean; readonly workspaceSkillsScanning: boolean; readonly questionAttachmentTarget: DraftId | null; @@ -311,7 +314,10 @@ export function useComposerMenuItems({ [...builtInSlashCommandItems, ...visibleProviderSlashCommandItems, ...skillItems], composerTrigger.rangeStart === 0, ); - return searchSlashCommandItems(slashCommandItems, query); + return searchSlashCommandItems( + expandGoalSubcommands(slashCommandItems, query, goalObjective), + query, + ); } if (composerTrigger.kind === "skill") { return searchProviderSkills(selectedProviderSkills, composerTrigger.query).map((skill) => ({ diff --git a/apps/web/src/composer-logic.test.ts b/apps/web/src/composer-logic.test.ts index cb65ab6bde0b1..361ae71f48281 100644 --- a/apps/web/src/composer-logic.test.ts +++ b/apps/web/src/composer-logic.test.ts @@ -236,6 +236,30 @@ describe("composerSubmissionIntentForKey", () => { }); describe("detectComposerTrigger", () => { + it.each(["/goal ", "/goal r", "/goal resume", "/goal edit"])( + "keeps goal subcommand completion open for %j", + (text) => { + expect(detectComposerTrigger(text, text.length, true)).toEqual({ + kind: "slash-command", + query: text.slice(1), + rangeStart: 0, + rangeEnd: text.length, + }); + }, + ); + + it.each(["Mention /goal resume", "/goal fix the tests", "/goal\nresume"])( + "does not complete a goal subcommand in %j", + (text) => expect(detectComposerTrigger(text, text.length)).toBeNull(), + ); + + it.each(["/goal ", "/goal r", "/goal objective"])( + "leaves %j alone without goal support", + (text) => { + expect(detectComposerTrigger(text, text.length)).toBeNull(); + }, + ); + it.each([ ["Run /tmp/build.sh", "Run /tmp/build.sh".length], ["/etc/hosts", "/etc/hosts".length], diff --git a/apps/web/src/composer-logic.ts b/apps/web/src/composer-logic.ts index 1cfbaceb4b245..65c8f89051fdb 100644 --- a/apps/web/src/composer-logic.ts +++ b/apps/web/src/composer-logic.ts @@ -248,8 +248,21 @@ export function collapseExpandedComposerCursor( return collapsedCursor; } -export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null { +export function detectComposerTrigger( + text: string, + cursorInput: number, + goalSubcommands = false, +): ComposerTrigger | null { const cursor = clampCursor(text, cursorInput); + const prefix = text.slice(0, cursor); + if (goalSubcommands && /^\/goal[\t ]+[a-z]*$/i.test(prefix)) { + return { + kind: "slash-command", + query: prefix.slice(1), + rangeStart: 0, + rangeEnd: cursor, + }; + } const tokenStart = tokenStartForCursor(text, cursor); const token = text.slice(tokenStart, cursor); if (token.startsWith("/")) {