From 083403c65fdd09595a2eaf8eadc3b70334d58336 Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 23 Nov 2026 22:53:09 -0600 Subject: [PATCH] feat(ui): /swarm in the task composer sends a structured swarm request Co-Authored-By: Claude Opus 5.5 --- ui/src/api/issues.ts | 2 + ui/src/components/IssueChatThread.tsx | 7 ++- .../task-chat/TaskChatComposer.test.tsx | 43 ++++++++++++++++++- .../components/task-chat/TaskChatComposer.tsx | 24 +++++++++-- .../components/task-chat/composer-commands.ts | 14 +++++- ui/src/lib/composer-draft.ts | 2 + ui/src/pages/IssueDetail.tsx | 12 ++++-- 7 files changed, 92 insertions(+), 12 deletions(-) diff --git a/ui/src/api/issues.ts b/ui/src/api/issues.ts index 0a7dea84395..49a0d6d0294 100644 --- a/ui/src/api/issues.ts +++ b/ui/src/api/issues.ts @@ -734,10 +734,12 @@ export const issuesApi = { interrupt?: boolean, attachmentIds?: string[], clientRequestId?: string, + nativeCommand?: "swarm", ) => confirmedCommentResponse( api.post(`/issues/${id}/comments`, { body, + ...(nativeCommand ? { nativeCommand } : {}), ...(clientRequestId ? { clientRequestId } : {}), ...(reopen === undefined ? {} : { reopen }), ...(interrupt === undefined ? {} : { interrupt }), diff --git a/ui/src/components/IssueChatThread.tsx b/ui/src/components/IssueChatThread.tsx index 3b575b0e2e6..ad5efbeb8fe 100644 --- a/ui/src/components/IssueChatThread.tsx +++ b/ui/src/components/IssueChatThread.tsx @@ -643,6 +643,7 @@ interface IssueChatThreadProps { attachmentIds?: string[], clientRequestId?: string, runSettings?: ComposerRunSettings, + nativeCommand?: "swarm", ) => Promise; onCancelRun?: () => Promise; stopPending?: boolean; @@ -6226,12 +6227,14 @@ export function IssueChatThread({ onRefresh: onRefreshLatestComments, }); const sendComposerComment = useCallback( - async (body, reopen, reassignment, attachmentIds, clientRequestId, runSettings) => { + async (body, reopen, reassignment, attachmentIds, clientRequestId, runSettings, nativeCommand) => { pendingSubmitScrollRef.current = true; clientRequestId ??= createUuid(); const attempt = beginSubmission(clientRequestId, undefined, reopen); try { - await (runSettings + await (nativeCommand + ? onAdd(body, reopen, reassignment, attachmentIds, clientRequestId, runSettings, nativeCommand) + : runSettings ? onAdd(body, reopen, reassignment, attachmentIds, clientRequestId, runSettings) : onAdd(body, reopen, reassignment, attachmentIds, clientRequestId)); } catch (error) { diff --git a/ui/src/components/task-chat/TaskChatComposer.test.tsx b/ui/src/components/task-chat/TaskChatComposer.test.tsx index 3f7439548f0..4182010f115 100644 --- a/ui/src/components/task-chat/TaskChatComposer.test.tsx +++ b/ui/src/components/task-chat/TaskChatComposer.test.tsx @@ -10,7 +10,7 @@ import { instanceExperimentalSettingsSchema, } from "@paperclipai/shared"; import { parsePlanCommand, parseRunnerGoalCommand, TaskChatComposer } from "./TaskChatComposer"; -import { planSubmission } from "./composer-commands"; +import { parseSwarmCommand, planSubmission } from "./composer-commands"; import { ComposerAddMenu } from "./ComposerAddMenu"; import { QuestionForm } from "./QuestionForm"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; @@ -2003,6 +2003,47 @@ describe("TaskChatComposer", () => { expect(editable().textContent).toBe(`[/deploy](${SLASH_HREF}) `); }); + describe("/swarm command", () => { + it("matches only the user's leading /swarm token, through the shared rule", () => { + expect(parseSwarmCommand(" /swarm two helpers write a.txt and b.txt ")).toEqual({ request: "two helpers write a.txt and b.txt" }); + expect(parseSwarmCommand("[/sw]()")).toEqual({ request: "write a.txt" }); + expect(parseSwarmCommand("/swarm")).toEqual({ request: "" }); + expect(parseSwarmCommand("Please /swarm this")).toBeNull(); + expect(parseSwarmCommand("> /swarm from a pasted email")).toBeNull(); + expect(parseSwarmCommand("```\n/swarm\n```")).toBeNull(); + expect(parseSwarmCommand("/swarms")).toBeNull(); + expect(parseSwarmCommand("/Swarm do it")).toBeNull(); + }); + + it("sends the request as a structured swarm request, without the command text", async () => { + const onAdd = vi.fn().mockResolvedValue(undefined); + renderComposer({ onAdd, workMode: "standard", onWorkModeChange: vi.fn() }); + typeText("/swarm two helpers write a.txt and b.txt"); + pressKey("Enter", { metaKey: true }); + await flushAsync(); + expect(onAdd).toHaveBeenCalledWith("two helpers write a.txt and b.txt", undefined, undefined, undefined, expect.any(String), undefined, "swarm"); + }); + + it("asks for a request instead of sending a bare /swarm", async () => { + const onAdd = vi.fn().mockResolvedValue(undefined); + renderComposer({ onAdd, workMode: "standard", onWorkModeChange: vi.fn() }); + typeText("/swarm"); + pressKey("Enter", { metaKey: true }); + await flushAsync(); + expect(onAdd).not.toHaveBeenCalled(); + expect(container.textContent).toContain("Write what the swarm should do after /swarm."); + }); + + it("leaves a /swarm inside the message as ordinary text", async () => { + const onAdd = vi.fn().mockResolvedValue(undefined); + renderComposer({ onAdd, workMode: "standard", onWorkModeChange: vi.fn() }); + typeText("Please /swarm this"); + pressKey("Enter", { metaKey: true }); + await flushAsync(); + expect(onAdd).toHaveBeenCalledWith("Please /swarm this", undefined, undefined, undefined, expect.any(String)); + }); + }); + describe("/plan command", () => { it("matches only the user's leading /plan token", () => { expect(parsePlanCommand(" /plan Add a greeting file ")).toEqual({ matched: true, request: "Add a greeting file" }); diff --git a/ui/src/components/task-chat/TaskChatComposer.tsx b/ui/src/components/task-chat/TaskChatComposer.tsx index 014c53f23dc..65fb6c0733a 100644 --- a/ui/src/components/task-chat/TaskChatComposer.tsx +++ b/ui/src/components/task-chat/TaskChatComposer.tsx @@ -72,7 +72,7 @@ import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import type { MentionOption } from "@/components/MarkdownEditor"; import type { IssueAttachment, IssueWorkMode } from "@paperclipai/shared"; import type { RunnerGoalCapability } from "@paperclipai/shared"; -import { parseRunnerGoalCommand, planSubmission, type RunnerGoalComposerCommand } from "./composer-commands"; +import { parseRunnerGoalCommand, parseSwarmCommand, planSubmission, type RunnerGoalComposerCommand } from "./composer-commands"; import type { ActionCommandOption } from "@/context/EditorAutocompleteContext"; import { TaskChatComposerTakeoverActionsContext } from "./TaskChatComposerTakeoverContext"; @@ -115,6 +115,8 @@ interface TaskChatComposerProps { attachmentIds?: string[], clientRequestId?: string, runSettings?: ComposerRunSettings, + /** A native command the user started with a leading `/name`; only "swarm" exists. */ + nativeCommand?: "swarm", ) => Promise | void; /** New tasks share the editor and controls, but retain their draft until creation succeeds. */ creation?: { @@ -912,7 +914,17 @@ export function TaskChatComposer({ return; } const sendMode = planCommand.kind === "send" ? "planning" : pendingMode; - const trimmed = planCommand.kind === "send" ? planCommand.text : submittedBody.trim(); + // A queued edit keeps its exact text; everywhere else a leading /swarm is a request. + const swarmCommand = queuedEdit ? null : parseSwarmCommand(submittedBody); + if (swarmCommand && !swarmCommand.request) { + setActionError("Write what the swarm should do after /swarm."); + return; + } + if (swarmCommand && showAssignee && assigneeValue !== currentAssigneeValue) { + setActionError("Send /swarm without changing the assignee."); + return; + } + const trimmed = swarmCommand ? swarmCommand.request : planCommand.kind === "send" ? planCommand.text : submittedBody.trim(); const goalCommand = queuedEdit ? ({ matched: false } as const) : parseRunnerGoalCommand(submittedBody); @@ -1075,6 +1087,7 @@ export function TaskChatComposer({ ...(reassignment ? { reassignment } : {}), ...(attachmentIds.length ? { attachmentIds } : {}), ...(runSettings ? { runSettings: runSettings as unknown as Record } : {}), + ...(swarmCommand ? { nativeCommand: "swarm" } : {}), }; if (draftKey) { saveDraft(draftKey, submittedBody); @@ -1083,7 +1096,9 @@ export function TaskChatComposer({ changeBody(bodyRef.current); } sentModeChoice.current = { attemptId, generation: submittedModeGeneration }; - if (runSettings) { + if (swarmCommand) { + await onAdd(fullBody, reopen, reassignment, attachmentIds.length ? attachmentIds : undefined, attemptId, runSettings ?? undefined, "swarm"); + } else if (runSettings) { await onAdd(fullBody, reopen, reassignment, attachmentIds.length ? attachmentIds : undefined, attemptId, runSettings); } else { await onAdd(fullBody, reopen, reassignment, attachmentIds.length ? attachmentIds : undefined, attemptId); @@ -1178,7 +1193,8 @@ export function TaskChatComposer({ submission.attemptId, ] as const; try { - if (request.runSettings) await onAdd(...args, request.runSettings as unknown as ComposerRunSettings); + if (request.nativeCommand === "swarm") await onAdd(...args, request.runSettings as unknown as ComposerRunSettings | undefined, "swarm"); + else if (request.runSettings) await onAdd(...args, request.runSettings as unknown as ComposerRunSettings); else await onAdd(...args); } catch (error) { if (!isCurrentAccount()) return "pending"; diff --git a/ui/src/components/task-chat/composer-commands.ts b/ui/src/components/task-chat/composer-commands.ts index 0509c7513c4..18f2f28b33b 100644 --- a/ui/src/components/task-chat/composer-commands.ts +++ b/ui/src/components/task-chat/composer-commands.ts @@ -20,14 +20,14 @@ export type ParsedRunnerGoalCommand = | { matched: true; command: RunnerGoalComposerCommand } | { matched: true; error: string }; -function normalizeActionCommandText(value: string, command: "goal" | "plan"): string { +function normalizeActionCommandText(value: string, command: "goal" | "plan" | "swarm"): string { const trimmed = value.trim(); // MDXEditor's link extension can reinterpret a selected action command plus // its subsequently typed argument as one relative autolink. Accept only the // exact whole-document shape it generates so the action still cannot fall // through as a comment. Ordinary Markdown links remain ordinary comments. // The label is any prefix of the command typed before it was selected. - const label = command === "goal" ? "(?:go(?:al)?)?" : "(?:p(?:l(?:an?)?)?)?"; + const label = command === "goal" ? "(?:go(?:al)?)?" : command === "swarm" ? "(?:s(?:w(?:a(?:rm?)?)?)?)?" : "(?:p(?:l(?:an?)?)?)?"; const relativeAutolink = trimmed.match( new RegExp(`^\\[\\/${label}[ \\t\\u00a0]*\\]\\(<(\\/${command}(?:[ \\t\\u00a0].*)?)>\\)$`, "s"), ); @@ -55,6 +55,16 @@ export function parsePlanCommand(value: string): { matched: false } | { matched: return command ? { matched: true, request: command.argument } : { matched: false }; } +/** + * `/swarm ` asks a Google Antigravity agent for its native swarm. The + * request is sent as a structured `nativeCommand`, never as command text; the + * server decides whether a swarm can run and says why when it cannot. + */ +export function parseSwarmCommand(value: string): { request: string } | null { + const command = parseLeadingUserCommand(normalizeActionCommandText(value, "swarm"), ["swarm"]); + return command ? { request: command.argument } : null; +} + export type PlanSubmission = | { kind: "none" } /** Select Plan mode and send nothing. */ diff --git a/ui/src/lib/composer-draft.ts b/ui/src/lib/composer-draft.ts index 4003c92a289..f88eb6376fa 100644 --- a/ui/src/lib/composer-draft.ts +++ b/ui/src/lib/composer-draft.ts @@ -106,6 +106,8 @@ export interface ComposerSendRequest { attachmentIds?: string[]; /** Composer run settings, passed back unchanged. */ runSettings?: Record; + /** A structured native command ("swarm"), resent unchanged so a retry stays a swarm request. */ + nativeCommand?: "swarm"; } export interface ComposerDraftSubmission { diff --git a/ui/src/pages/IssueDetail.tsx b/ui/src/pages/IssueDetail.tsx index 66135638a4a..7a1d2bc7d79 100644 --- a/ui/src/pages/IssueDetail.tsx +++ b/ui/src/pages/IssueDetail.tsx @@ -5148,8 +5148,8 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS }); const addComment = useAccountBoundMutation({ - mutationFn: async ({ body, reopen, interrupt, attachmentIds, clientRequestId, runSettings }: { - body: string; reopen?: boolean; interrupt?: boolean; attachmentIds?: string[]; clientRequestId?: string; runSettings?: ComposerRunSettings; + mutationFn: async ({ body, reopen, interrupt, attachmentIds, clientRequestId, runSettings, nativeCommand }: { + body: string; reopen?: boolean; interrupt?: boolean; attachmentIds?: string[]; clientRequestId?: string; runSettings?: ComposerRunSettings; nativeCommand?: "swarm"; }, { assertCurrentAccount }) => { if (issue?.conversationAgentId) clearLegacyChatMessageRequests(`${issue.companyId}:${currentUserId}:${issue.conversationAgentId}`); const writableIssueId = await resolveWritableIssueId(); @@ -5171,7 +5171,7 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS assertCurrentAccount(); queryClient.setQueryData(queryKeys.issues.detail(writableIssueId), updated); } - return issuesApi.addComment(writableIssueId, body, reopen, interrupt, attachmentIds, clientRequestId ?? createUuid()); + return issuesApi.addComment(writableIssueId, body, reopen, interrupt, attachmentIds, clientRequestId ?? createUuid(), nativeCommand); }, onMutate: async ({ body, reopen, interrupt }) => { // Start cache cancellation immediately but do not put it in front of the @@ -7042,7 +7042,13 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS attachmentIds?: string[], clientRequestId?: string, runSettings?: ComposerRunSettings, + nativeCommand?: "swarm", ) => { + // A swarm request always uses the comments endpoint, which checks it. + if (nativeCommand) { + await addComment.mutateAsync({ body, reopen, attachmentIds, clientRequestId, nativeCommand }); + return; + } if (!issue?.conversationAgentId && (reassignment || runSettings)) { await addCommentAndReassign.mutateAsync({ body,