improv: add context handling

This commit is contained in:
Tomasz Stefaniak 2025-10-13 17:46:33 -07:00
parent 5adcf1e7c6
commit b83133b835
4 changed files with 60 additions and 24 deletions

View file

@ -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: {

View file

@ -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<{

View file

@ -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(

View file

@ -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);