diff --git a/src/features/agents/Composer.tsx b/src/features/agents/Composer.tsx index 1a7dd8ef86..900d1b17f7 100644 --- a/src/features/agents/Composer.tsx +++ b/src/features/agents/Composer.tsx @@ -24,7 +24,9 @@ import { registerKeyClaim } from "@/platform/keymap"; import { isVirtualUri } from "@/platform/virtualDocs"; import { copySourceOf } from "@/platform/clipboardSource"; import { + EDITOR_TAB_MIME, getAgentContextSources, + maybeEditorService, notifications, OWNS_FILE_DROP, type AgentContextSource, @@ -100,6 +102,18 @@ type Item = const isSection = (item: Item) => item.kind === "section"; +/** The file of an editor tab drag payload (`{paneId, tabId}`), null for virtual and non-file tabs. */ +const tabFilePath = (payload: string): string | null => { + try { + const p = JSON.parse(payload) as { paneId?: unknown; tabId?: unknown }; + if (typeof p.paneId !== "string" || typeof p.tabId !== "string") return null; + const input = maybeEditorService()?.tabInput?.(p.paneId, p.tabId); + return input?.kind === "file" && input.path && !isVirtualUri(input.path) ? input.path : null; + } catch { + return null; + } +}; + const SECTION_TITLES: Record = { command: "Commands", skill: "Skills", @@ -941,17 +955,21 @@ export function Composer(props: ComposerProps) { }; }, [addPaths, addImages]); - // Files and folders dragged from the Project view: the same `@` references. + // Files and folders dragged from the Project view, and file tabs dragged from the editor: + // the same `@` references. useEffect(() => { const el = boxRef.current; if (!el) return; return registerDropTarget(el, { - accepts: (type) => type === FILES_DRAG, + accepts: (type, data) => type === FILES_DRAG || (type === EDITOR_TAB_MIME && !!tabFilePath(data.payload)), over: () => setFileOver(true), leave: () => setFileOver(false), drop: (data) => { setFileOver(false); - addPaths(data.payload.split("\n").filter(Boolean)); + if (data.type === EDITOR_TAB_MIME) { + const path = tabFilePath(data.payload); + if (path) addPaths([path]); + } else addPaths(data.payload.split("\n").filter(Boolean)); }, }); }, [addPaths]); diff --git a/src/features/agents/composerFolders.test.tsx b/src/features/agents/composerFolders.test.tsx index 3c36337070..1cabe06a3e 100644 --- a/src/features/agents/composerFolders.test.tsx +++ b/src/features/agents/composerFolders.test.tsx @@ -7,6 +7,7 @@ import userEvent from "@testing-library/user-event"; import { caretOf, setCaret, textOf, typeChars, typeIn } from "./composerTesting"; import chipStyles from "./ReferenceChip.module.css"; import { FILES_DRAG, dragSession } from "@/ui"; +import { EDITOR_TAB_MIME, provideEditorService, type EditorService } from "@/workbench/contributions"; import { useProject } from "@/platform/project"; const findFiles = vi.fn(); @@ -123,6 +124,32 @@ describe("Composer dropped paths", () => { expect(textOf(box)).toBe("@src/a.ts @src/dir/ "); }); + it("references the file of an editor tab dragged onto it and ignores other tabs", () => { + provideEditorService({ + tabInput: (_pane: string, tab: string) => + tab === "file" + ? { kind: "file", key: "/p/src/a.ts", path: "/p/src/a.ts" } + : { kind: "agentSession", key: "agent:one", data: { task: "one" } }, + } as unknown as EditorService); + const box = renderComposer(); + const restore = document.elementFromPoint; + document.elementFromPoint = () => box; + try { + act(() => { + const other = dragSession({ + type: EDITOR_TAB_MIME, + payload: JSON.stringify({ paneId: "p", tabId: "session" }), + }); + expect(other.drop(0, 0)).toBe(false); + const file = dragSession({ type: EDITOR_TAB_MIME, payload: JSON.stringify({ paneId: "p", tabId: "file" }) }); + expect(file.drop(0, 0)).toBe(true); + }); + } finally { + document.elementFromPoint = restore; + } + expect(textOf(box)).toBe("@src/a.ts "); + }); + it("deduplicates live references and allows a deleted reference to be dropped again", () => { const box = renderComposer(); drop(box, "/p/src/a.ts\n/p/src/a.ts\n/p/src/dir/"); diff --git a/src/features/agents/index.ts b/src/features/agents/index.ts index c6633fa1ac..ca5159ad2e 100644 --- a/src/features/agents/index.ts +++ b/src/features/agents/index.ts @@ -232,7 +232,7 @@ export function activate(): () => void { registerStatusItem({ id: "agents.status", align: "right", priority: 3, title: "Agents", component: AgentsStatus }), registerMenuGroup({ id: "agents.send", - menus: ["editor", "projectView"], + menus: ["editor", "editorTab", "projectView"], order: 80, items: (menu, { paths, dirs }) => paths.length && !paths.some(isVirtualUri) && agentsHere() @@ -240,7 +240,8 @@ export function activate(): () => void { { kind: "action", action: "agents.sendToAgent", - arg: menu === "projectView" ? paths.map((p, i) => (dirs?.[i] ? `${p}/` : p)) : undefined, + // The editor sends its selection; a tab or the Project view sends the files. + arg: menu === "editor" ? undefined : paths.map((p, i) => (dirs?.[i] ? `${p}/` : p)), }, ] : [], diff --git a/src/features/agents/unread.test.tsx b/src/features/agents/unread.test.tsx index 25ff285745..58e500d0c5 100644 --- a/src/features/agents/unread.test.tsx +++ b/src/features/agents/unread.test.tsx @@ -3,6 +3,7 @@ import { act, fireEvent, render, screen } from "@testing-library/react"; import type { AgentEntry, AgentEvent, AgentTask } from "@/bindings"; import { dropOnEditor, + menuGroupItems, useContributions, useEditorTabTitles, type EditorInput, @@ -595,3 +596,20 @@ describe("the diff of an agent change", () => { expect(backend.openInput.mock.lastCall![0].data.chain).toBeUndefined(); }); }); + +describe("Send to Agent menus", () => { + it("sends the selection from the editor and the files from an editor tab or the Project view", () => { + const dispose = activate(); + try { + const send = (menu: "editor" | "editorTab" | "projectView", dirs?: boolean[]) => + menuGroupItems(menu, { paths: ["/p/a.ts", "/p/src"].slice(0, dirs ? 2 : 1), dirs }).find( + (i) => "action" in i && i.action === "agents.sendToAgent", + ); + expect(send("editor")).toMatchObject({ arg: undefined }); + expect(send("editorTab")).toMatchObject({ arg: ["/p/a.ts"] }); + expect(send("projectView", [false, true])).toMatchObject({ arg: ["/p/a.ts", "/p/src/"] }); + } finally { + dispose(); + } + }); +});