From b83133b835206144015fddda78b2ae46fe92f077 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Mon, 13 Oct 2025 17:46:33 -0700 Subject: [PATCH] improv: add context handling --- core/control-plane/client.ts | 21 +++++++++++++ core/protocol/ideWebview.ts | 12 +++++++- .../vscode/src/extension/VsCodeMessenger.ts | 30 +++++-------------- gui/src/pages/gui/Chat.tsx | 21 ++++++++++++- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index ca2f4e1b2..76470e2ce 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -450,6 +450,8 @@ export class ControlPlaneClient { name: string, branch?: string, organizationId?: string, + contextItems?: any[], + selectedCode?: any[], ): Promise<{ id: string }> { if (!(await this.isSignedIn())) { throw new Error("Not signed in to Continue"); @@ -466,6 +468,25 @@ export class ControlPlaneClient { requestBody.organizationId = organizationId; } + // Include context items if provided + if (contextItems && contextItems.length > 0) { + requestBody.contextItems = contextItems.map((item) => ({ + content: item.content, + description: item.description, + name: item.name, + uri: item.uri, + })); + } + + // Include selected code if provided + if (selectedCode && selectedCode.length > 0) { + requestBody.selectedCode = selectedCode.map((code) => ({ + filepath: code.filepath, + range: code.range, + contents: code.contents, + })); + } + const resp = await this.requestAndHandleError("agents", { method: "POST", headers: { diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index 2376edb71..c5d52a2a0 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -6,8 +6,10 @@ import { AddToChatPayload, ApplyState, ApplyToFilePayload, + ContextItemWithId, HighlightedCodePayload, MessageContent, + RangeInFile, RangeInFileWithContents, SetCodeToEditPayload, ShowFilePayload, @@ -50,7 +52,15 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { "edit/addCurrentSelection": [undefined, void]; "edit/clearDecorations": [undefined, void]; "session/share": [{ sessionId: string }, void]; - createBackgroundAgent: [{ editorState: any; organizationId?: string }, void]; + createBackgroundAgent: [ + { + content: MessageContent; + contextItems: ContextItemWithId[]; + selectedCode: RangeInFile[]; + organizationId?: string; + }, + void, + ]; listBackgroundAgents: [ { organizationId?: string }, Array<{ diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 2cfda4608..7e14bdd71 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -265,30 +265,12 @@ export class VsCodeMessenger { this.onWebview("createBackgroundAgent", async (msg) => { const configHandler = await configHandlerPromise; - const { editorState, organizationId } = msg.data; + const { content, contextItems, selectedCode, organizationId } = msg.data; - // Import utilities for processing editor content - const { stripImages } = await import("core/util/messageContent"); + // Convert resolved content to plain text prompt + const prompt = stripImages(content); - // Extract text from editor state - // editorState is a TipTap JSONContent object - let prompt = ""; - if (editorState && editorState.content) { - // Simple extraction - concatenate text from paragraphs - for (const node of editorState.content) { - if (node.type === "paragraph" && node.content) { - for (const child of node.content) { - if (child.type === "text" && child.text) { - prompt += child.text; - } - } - prompt += "\n"; - } - } - prompt = prompt.trim(); - } - - if (!prompt) { + if (!prompt || prompt.trim().length === 0) { vscode.window.showErrorMessage( "Please enter a prompt to create a background agent", ); @@ -356,6 +338,8 @@ export class VsCodeMessenger { prompt: prompt.substring(0, 50) + "...", repoUrl, branch, + contextItemsCount: contextItems?.length || 0, + selectedCodeCount: selectedCode?.length || 0, }); const result = @@ -365,6 +349,8 @@ export class VsCodeMessenger { name, branch, organizationId, + contextItems, + selectedCode, ); vscode.window.showInformationMessage( diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 98956d1c1..71614487e 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -49,6 +49,7 @@ import { BackgroundModeView } from "../../components/BackgroundMode/BackgroundMo import { CliInstallBanner } from "../../components/CliInstallBanner"; import { FatalErrorIndicator } from "../../components/config/FatalErrorNotice"; import InlineErrorMessage from "../../components/mainInput/InlineErrorMessage"; +import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent"; import { RootState } from "../../redux/store"; import { cancelStream } from "../../redux/thunks/cancelStream"; import { EmptyChatBody } from "./EmptyChatBody"; @@ -193,8 +194,26 @@ export function Chat() { // Create agent and track loading state void (async () => { try { + // Resolve context items from editor content (same as normal chat) + const defaultContextProviders = + stateSnapshot.config.config.experimental?.defaultContext ?? []; + + const { selectedContextItems, selectedCode, content } = + await resolveEditorContent({ + editorState, + modifiers, + ideMessenger, + defaultContextProviders, + availableSlashCommands: + stateSnapshot.config.config.slashCommands, + dispatch, + getState: () => reduxStore.getState(), + }); + await ideMessenger.request("createBackgroundAgent", { - editorState, + content, + contextItems: selectedContextItems, + selectedCode, organizationId, }); setIsCreatingAgent(false);