Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tree data progress and other progress types in chat agents #195817

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/workbench/api/browser/mainThreadChatAgents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MainThreadChatAgents implements MainThreadChatAgentsShape {
metadata: revive(metadata),
invoke: async (request, progress, history, token) => {
const requestId = Math.random();
this._pendingProgress.set(requestId, progress);
this._pendingProgress.set(requestId, { report: progress });
try {
const message = request.command ? `/${request.command} ${request.message}` : request.message;
const result = await this._proxy.$invokeAgent(handle, requestId, message, { history }, token);
Expand Down
59 changes: 52 additions & 7 deletions src/vs/workbench/api/browser/mainThreadChatAgents2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { DeferredPromise } from 'vs/base/common/async';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { Disposable, DisposableMap } from 'vs/base/common/lifecycle';
import { revive } from 'vs/base/common/marshalling';
import { IProgress } from 'vs/platform/progress/common/progress';
import { ExtHostChatAgentsShape2, ExtHostContext, IChatResponseProgressDto, IExtensionChatAgentMetadata, MainContext, MainThreadChatAgentsShape2 } from 'vs/workbench/api/common/extHost.protocol';
import { UriComponents } from 'vs/base/common/uri';
import { generateUuid } from 'vs/base/common/uuid';
import { ExtHostChatAgentsShape2, ExtHostContext, IChatResponseProgressDto, IChatResponseProgressFileTreeData, IExtensionChatAgentMetadata, ILocationDto, MainContext, MainThreadChatAgentsShape2 } from 'vs/workbench/api/common/extHost.protocol';
import { IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents';
import { isCompleteInteractiveProgressTreeData } from 'vs/workbench/contrib/chat/common/chatModel';
import { IChatFollowup, IChatProgress, IChatService } from 'vs/workbench/contrib/chat/common/chatService';
import { IExtHostContext, extHostNamedCustomer } from 'vs/workbench/services/extensions/common/extHostCustomers';

Expand All @@ -22,9 +26,12 @@ type AgentData = {
export class MainThreadChatAgents2 extends Disposable implements MainThreadChatAgentsShape2 {

private readonly _agents = this._register(new DisposableMap<number, AgentData>());
private readonly _pendingProgress = new Map<number, IProgress<IChatProgress>>();
private readonly _pendingProgress = new Map<string, (part: IChatProgress) => void>();
private readonly _proxy: ExtHostChatAgentsShape2;

private _responsePartHandlePool = 0;
private readonly _activeResponsePartPromises = new Map<string, DeferredPromise<string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData }>>();

constructor(
extHostContext: IExtHostContext,
@IChatAgentService private readonly _chatAgentService: IChatAgentService,
Expand Down Expand Up @@ -61,7 +68,7 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
id: name,
metadata: revive(metadata),
invoke: async (request, progress, history, token) => {
const requestId = Math.random(); // Make this a guid
const requestId = generateUuid();
this._pendingProgress.set(requestId, progress);
try {
return await this._proxy.$invokeAgent(handle, request.sessionId, requestId, request, { history }, token) ?? {};
Expand Down Expand Up @@ -95,8 +102,46 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
this._chatAgentService.updateAgent(data.name, revive(metadataUpdate));
}

async $handleProgressChunk(requestId: number, chunk: IChatResponseProgressDto): Promise<void> {
// TODO copy/move $acceptResponseProgress from MainThreadChat
this._pendingProgress.get(requestId)?.report(revive(chunk) as any);
async $handleProgressChunk(requestId: string, progress: IChatResponseProgressDto, responsePartHandle?: number): Promise<number | void> {
if ('placeholder' in progress) {
const handle = ++this._responsePartHandlePool;
const responsePartId = `${requestId}_${handle}`;
const deferredContentPromise = new DeferredPromise<string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData }>();
this._activeResponsePartPromises.set(responsePartId, deferredContentPromise);
this._pendingProgress.get(requestId)?.({ ...progress, resolvedContent: deferredContentPromise.p });
return handle;
} else if (typeof responsePartHandle === 'number') {
// Complete an existing deferred promise with resolved content
const responsePartId = `${requestId}_${responsePartHandle}`;
const deferredContentPromise = this._activeResponsePartPromises.get(responsePartId);
if (deferredContentPromise && isCompleteInteractiveProgressTreeData(progress)) {
const withRevivedUris = revive<{ treeData: IChatResponseProgressFileTreeData }>(progress);
deferredContentPromise.complete(withRevivedUris);
this._activeResponsePartPromises.delete(responsePartId);
} else if (deferredContentPromise && 'content' in progress) {
deferredContentPromise.complete(progress.content);
this._activeResponsePartPromises.delete(responsePartId);
}
return responsePartHandle;
}

// No need to support standalone tree data that's not attached to a placeholder in API
if (isCompleteInteractiveProgressTreeData(progress)) {
return;
}

// TS won't let us change the type of `progress`
let revivedProgress: IChatProgress;
if ('documents' in progress) {
revivedProgress = { documents: revive(progress.documents) };
} else if ('reference' in progress) {
revivedProgress = revive<{ reference: UriComponents | ILocationDto }>(progress);
} else if ('inlineReference' in progress) {
revivedProgress = revive<{ inlineReference: UriComponents | ILocationDto; name?: string }>(progress);
} else {
revivedProgress = progress;
}

this._pendingProgress.get(requestId)?.(revivedProgress);
}
}
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,15 +1174,15 @@ export interface MainThreadChatAgentsShape2 extends IDisposable {
$registerAgent(handle: number, name: string, metadata: IExtensionChatAgentMetadata): void;
$updateAgent(handle: number, metadataUpdate: IExtensionChatAgentMetadata): void;
$unregisterAgent(handle: number): void;
$handleProgressChunk(requestId: number, chunk: IChatResponseProgressDto): Promise<void>;
$handleProgressChunk(requestId: string, chunk: IChatResponseProgressDto, responsePartHandle?: number): Promise<number | void>;
}

export interface ExtHostChatAgentsShape {
$invokeAgent(handle: number, requestId: number, prompt: string, context: { history: IChatMessage[] }, token: CancellationToken): Promise<any>;
}

export interface ExtHostChatAgentsShape2 {
$invokeAgent(handle: number, sessionId: string, requestId: number, request: IChatAgentRequest, context: { history: IChatMessage[] }, token: CancellationToken): Promise<IChatAgentResult | undefined>;
$invokeAgent(handle: number, sessionId: string, requestId: string, request: IChatAgentRequest, context: { history: IChatMessage[] }, token: CancellationToken): Promise<IChatAgentResult | undefined>;
$provideSlashCommands(handle: number, token: CancellationToken): Promise<IChatAgentCommand[]>;
$provideFollowups(handle: number, sessionId: string, token: CancellationToken): Promise<IChatFollowup[]>;
$acceptFeedback(handle: number, sessionId: string, vote: InteractiveSessionVoteDirection): void;
Expand Down
19 changes: 15 additions & 4 deletions src/vs/workbench/api/common/extHostChatAgents2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
return agent.apiAgent;
}

async $invokeAgent(handle: number, sessionId: string, requestId: number, request: IChatAgentRequest, context: { history: IChatMessage[] }, token: CancellationToken): Promise<IChatAgentResult | undefined> {
async $invokeAgent(handle: number, sessionId: string, requestId: string, request: IChatAgentRequest, context: { history: IChatMessage[] }, token: CancellationToken): Promise<IChatAgentResult | undefined> {
const agent = this._agents.get(handle);
if (!agent) {
throw new Error(`[CHAT](${handle}) CANNOT invoke agent because the agent is not registered`);
Expand Down Expand Up @@ -78,10 +78,21 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
slashCommand
},
{ history: context.history.map(typeConvert.ChatMessage.to) },
new Progress<vscode.ChatAgentProgress>(p => {
new Progress<vscode.ChatAgentProgress>(progress => {
throwIfDone();
const convertedProgress = typeConvert.ChatResponseProgress.from(p);
this._proxy.$handleProgressChunk(requestId, convertedProgress);
const convertedProgress = typeConvert.ChatResponseProgress.from(progress);
if ('placeholder' in progress && 'resolvedContent' in progress) {
const resolvedContent = Promise.all([this._proxy.$handleProgressChunk(requestId, convertedProgress), progress.resolvedContent]);
raceCancellation(resolvedContent, token).then(res => {
if (!res) {
return; /* Cancelled */
}
const [progressHandle, progressContent] = res;
this._proxy.$handleProgressChunk(requestId, progressContent, progressHandle ?? undefined);
});
} else {
this._proxy.$handleProgressChunk(requestId, convertedProgress);
}
}),
token
);
Expand Down
7 changes: 3 additions & 4 deletions src/vs/workbench/contrib/chat/common/chatAgents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Iterable } from 'vs/base/common/iterator';
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IProgress } from 'vs/platform/progress/common/progress';
import { IChatMessage } from 'vs/workbench/contrib/chat/common/chatProvider';
import { IChatFollowup, IChatProgress, IChatResponseErrorDetails, IChatResponseProgressFileTreeData } from 'vs/workbench/contrib/chat/common/chatService';
import { IChatRequestVariableValue } from 'vs/workbench/contrib/chat/common/chatVariables';
Expand All @@ -19,7 +18,7 @@ import { IChatRequestVariableValue } from 'vs/workbench/contrib/chat/common/chat
export interface IChatAgent {
id: string;
metadata: IChatAgentMetadata;
invoke(request: IChatAgentRequest, progress: IProgress<IChatProgress>, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult>;
invoke(request: IChatAgentRequest, progress: (part: IChatProgress) => void, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult>;
provideFollowups?(sessionId: string, token: CancellationToken): Promise<IChatFollowup[]>;
provideSlashCommands(token: CancellationToken): Promise<IChatAgentCommand[]>;
}
Expand Down Expand Up @@ -66,7 +65,7 @@ export interface IChatAgentService {
_serviceBrand: undefined;
readonly onDidChangeAgents: Event<void>;
registerAgent(agent: IChatAgent): IDisposable;
invokeAgent(id: string, request: IChatAgentRequest, progress: IProgress<IChatProgress>, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult>;
invokeAgent(id: string, request: IChatAgentRequest, progress: (part: IChatProgress) => void, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult>;
getFollowups(id: string, sessionId: string, token: CancellationToken): Promise<IChatFollowup[]>;
getAgents(): Array<IChatAgent>;
getAgent(id: string): IChatAgent | undefined;
Expand Down Expand Up @@ -131,7 +130,7 @@ export class ChatAgentService extends Disposable implements IChatAgentService {
return data?.agent;
}

async invokeAgent(id: string, request: IChatAgentRequest, progress: IProgress<IChatProgress>, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult> {
async invokeAgent(id: string, request: IChatAgentRequest, progress: (part: IChatProgress) => void, history: IChatMessage[], token: CancellationToken): Promise<IChatAgentResult> {
const data = this._agents.get(id);
if (!data) {
throw new Error(`No agent with id ${id}`);
Expand Down
4 changes: 1 addition & 3 deletions src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,7 @@ export class ChatService extends Disposable implements IChatService {
requestProps.message = varResult.prompt;
}

const agentResult = await this.chatAgentService.invokeAgent(agent.id, requestProps, new Progress<IChatProgress>(p => {
progressCallback(p);
}), history, token);
const agentResult = await this.chatAgentService.invokeAgent(agent.id, requestProps, progressCallback, history, token);
rawResponse = {
session: model.session!,
errorDetails: agentResult.errorDetails,
Expand Down