Skip to content

Commit

Permalink
Revert "Make the chat progress types nicer (#198175)" (#198198)
Browse files Browse the repository at this point in the history
This reverts commit 94959e8.
  • Loading branch information
alexdima committed Nov 14, 2023
1 parent f9cadb5 commit 54821ee
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 131 deletions.
37 changes: 25 additions & 12 deletions src/vs/workbench/api/browser/mainThreadChatAgents2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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 { ExtHostChatAgentsShape2, ExtHostContext, IChatProgressDto, IExtensionChatAgentMetadata, MainContext, MainThreadChatAgentsShape2 } from 'vs/workbench/api/common/extHost.protocol';
import { UriComponents } from 'vs/base/common/uri';
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 { IChatFollowup, IChatProgress, IChatService, IChatTreeData } from 'vs/workbench/contrib/chat/common/chatService';
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';

type AgentData = {
Expand All @@ -27,7 +29,7 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
private readonly _proxy: ExtHostChatAgentsShape2;

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

constructor(
extHostContext: IExtHostContext,
Expand Down Expand Up @@ -104,35 +106,46 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
this._chatAgentService.updateAgent(data.name, revive(metadataUpdate));
}

async $handleProgressChunk(requestId: string, progress: IChatProgressDto, responsePartHandle?: number): Promise<number | void> {
if (progress.kind === 'asyncContent') {
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 | IChatTreeData>();
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 && progress.kind === 'treeData') {
const withRevivedUris = revive<IChatTreeData>(progress);
if (deferredContentPromise && isCompleteInteractiveProgressTreeData(progress)) {
const withRevivedUris = revive<{ treeData: IChatResponseProgressFileTreeData }>(progress);
deferredContentPromise.complete(withRevivedUris);
this._activeResponsePartPromises.delete(responsePartId);
} else if (deferredContentPromise && progress.kind === 'content') {
} 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 (progress.kind === 'treeData') {
if (isCompleteInteractiveProgressTreeData(progress)) {
return;
}

const revivedProgress = revive(progress);
this._pendingProgress.get(requestId)?.(revivedProgress as IChatProgress);
// 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);
}
}
32 changes: 10 additions & 22 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { IRevealOptions, ITreeItem, IViewBadge } from 'vs/workbench/common/views
import { CallHierarchyItem } from 'vs/workbench/contrib/callHierarchy/common/callHierarchy';
import { IChatAgentCommand, IChatAgentMetadata, IChatAgentRequest, IChatAgentResult } from 'vs/workbench/contrib/chat/common/chatAgents';
import { IChatMessage, IChatResponseFragment, IChatResponseProviderMetadata } from 'vs/workbench/contrib/chat/common/chatProvider';
import { IChatAgentDetection, IChatAsyncContent, IChatContent, IChatContentInlineReference, IChatContentReference, IChatDynamicRequest, IChatFollowup, IChatReplyFollowup, IChatResponseErrorDetails, IChatTreeData, IChatUsedContext, IChatUserActionEvent, InteractiveSessionVoteDirection } from 'vs/workbench/contrib/chat/common/chatService';
import { IChatAgentDetection, IChatDynamicRequest, IChatFollowup, IChatReplyFollowup, IChatResponseErrorDetails, IChatUserActionEvent, InteractiveSessionVoteDirection } from 'vs/workbench/contrib/chat/common/chatService';
import { IChatRequestVariableValue, IChatVariableData } from 'vs/workbench/contrib/chat/common/chatVariables';
import { DebugConfigurationProviderTriggerKind, IAdapterDescriptor, IConfig, IDebugSessionReplMode } from 'vs/workbench/contrib/debug/common/debug';
import { IInlineChatBulkEditResponse, IInlineChatEditResponse, IInlineChatMessageResponse, IInlineChatProgressItem, IInlineChatRequest, IInlineChatSession, InlineChatResponseFeedbackKind } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
Expand Down Expand Up @@ -1174,7 +1174,7 @@ 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: string, chunk: IChatProgressDto, responsePartHandle?: number): Promise<number | void>;
$handleProgressChunk(requestId: string, chunk: IChatResponseProgressDto, responsePartHandle?: number): Promise<number | void>;
}

export interface ExtHostChatAgentsShape2 {
Expand Down Expand Up @@ -1250,26 +1250,14 @@ export type IDocumentContextDto = {
ranges: IRange[];
};

export type IChatAsyncContentDto = Dto<Omit<IChatAsyncContent, 'resolvedContent'>>;

// TODO@some type ninja who can do this without the duplication (the async content case throws me off)
export type IChatProgressDto =
| Dto<IChatContent>
| Dto<IChatTreeData>
| Dto<IChatAsyncContent>
| Dto<IChatUsedContext>
| Dto<IChatContentReference>
| Dto<IChatContentInlineReference>
| Dto<IChatAgentDetection>
| IChatAsyncContentDto;

// | { content: string | IMarkdownString }
// | { placeholder: string }
// | { treeData: IChatResponseProgressFileTreeData }
// | { documents: IDocumentContextDto[] }
// | { reference: UriComponents | ILocationDto }
// | { inlineReference: UriComponents | ILocationDto; title?: string }
// | IChatAgentDetection;
export type IChatResponseProgressDto =
| { content: string | IMarkdownString }
| { placeholder: string }
| { treeData: IChatResponseProgressFileTreeData }
| { documents: IDocumentContextDto[] }
| { reference: UriComponents | ILocationDto }
| { inlineReference: UriComponents | ILocationDto; title?: string }
| IChatAgentDetection;

export interface MainThreadChatShape extends IDisposable {
$registerChatProvider(handle: number, id: string): Promise<void>;
Expand Down
3 changes: 1 addition & 2 deletions src/vs/workbench/api/common/extHostChatAgents2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
return; /* Cancelled */
}
const [progressHandle, progressContent] = res;
const convertedContent = typeConvert.ChatResponseProgress.from(agent.extension, progressContent);
this._proxy.$handleProgressChunk(requestId, convertedContent, progressHandle ?? undefined);
this._proxy.$handleProgressChunk(requestId, progressContent, progressHandle ?? undefined);
});
} else {
this._proxy.$handleProgressChunk(requestId, convertedProgress);
Expand Down
23 changes: 10 additions & 13 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2293,36 +2293,34 @@ export namespace InteractiveEditorResponseFeedbackKind {
}

export namespace ChatResponseProgress {
export function from(extension: IExtensionDescription, progress: vscode.ChatAgentExtendedProgress): extHostProtocol.IChatProgressDto {
export function from(extension: IExtensionDescription, progress: vscode.ChatAgentExtendedProgress): extHostProtocol.IChatResponseProgressDto {
if ('placeholder' in progress && 'resolvedContent' in progress) {
return { placeholder: progress.placeholder, kind: 'asyncContent' } satisfies extHostProtocol.IChatAsyncContentDto;
return { placeholder: progress.placeholder };
} else if ('markdownContent' in progress) {
checkProposedApiEnabled(extension, 'chatAgents2Additions');
return { content: MarkdownString.from(progress.markdownContent), kind: 'content' };
return { content: MarkdownString.from(progress.markdownContent) };
} else if ('content' in progress) {
if (typeof progress.content === 'string') {
return { content: progress.content, kind: 'content' };
return progress;
}

checkProposedApiEnabled(extension, 'chatAgents2Additions');
return { content: MarkdownString.from(progress.content), kind: 'content' };
return { content: MarkdownString.from(progress.content) };
} else if ('documents' in progress) {
return {
documents: progress.documents.map(d => ({
uri: d.uri,
version: d.version,
ranges: d.ranges.map(r => Range.from(r))
})),
kind: 'usedContext'
}))
};
} else if ('reference' in progress) {
return {
reference: 'uri' in progress.reference ?
{
uri: progress.reference.uri,
range: Range.from(progress.reference.range)
} : progress.reference,
kind: 'reference'
} : progress.reference
};
} else if ('inlineReference' in progress) {
return {
Expand All @@ -2331,14 +2329,13 @@ export namespace ChatResponseProgress {
uri: progress.inlineReference.uri,
range: Range.from(progress.inlineReference.range)
} : progress.inlineReference,
name: progress.title,
kind: 'inlineReference'
title: progress.title,
};
} else if ('agentName' in progress) {
checkProposedApiEnabled(extension, 'chatAgents2Additions');
return { agentName: progress.agentName, command: progress.command, kind: 'agentDetection' };
return progress;
} else {
return { treeData: progress.treeData, kind: 'treeData' };
return progress;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/contrib/chat/browser/chat.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ class ChatSlashStaticSlashCommandsContribution extends Disposable {
const defaultAgent = chatAgentService.getDefaultAgent();
const agents = chatAgentService.getAgents();
if (defaultAgent?.metadata.helpTextPrefix) {
progress.report({ content: defaultAgent.metadata.helpTextPrefix, kind: 'content' });
progress.report({ content: '\n\n', kind: 'content' });
progress.report({ content: defaultAgent.metadata.helpTextPrefix });
progress.report({ content: '\n\n' });
}

const agentText = (await Promise.all(agents
Expand All @@ -263,10 +263,10 @@ class ChatSlashStaticSlashCommandsContribution extends Disposable {

return agentLine + '\n' + commandText;
}))).join('\n');
progress.report({ content: new MarkdownString(agentText, { isTrusted: { enabledCommands: [SubmitAction.ID] } }), kind: 'content' });
progress.report({ content: new MarkdownString(agentText, { isTrusted: { enabledCommands: [SubmitAction.ID] } }) });
if (defaultAgent?.metadata.helpTextPostfix) {
progress.report({ content: '\n\n', kind: 'content' });
progress.report({ content: defaultAgent.metadata.helpTextPostfix, kind: 'content' });
progress.report({ content: '\n\n' });
progress.report({ content: defaultAgent.metadata.helpTextPostfix });
}
}));
}
Expand Down
Loading

0 comments on commit 54821ee

Please sign in to comment.