Skip to content

Commit 20501b7

Browse files
authored
Chat API updates (microsoft#205184)
* api - remove unused types, add jsdoc, make request handler setable (for consistency), more readonly usage microsoft#199908 * remove ChatMessage and Role * fix a ton of compile errors... * jsdoc
1 parent 40c4d29 commit 20501b7

9 files changed

+105
-146
lines changed

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
14211421
checkProposedApiEnabled(extension, 'mappedEditsProvider');
14221422
return extHostLanguageFeatures.registerMappedEditsProvider(extension, selector, provider);
14231423
},
1424-
createChatAgent(name: string, handler: vscode.ChatAgentExtendedHandler) {
1424+
createChatAgent(name: string, handler: vscode.ChatAgentExtendedRequestHandler) {
14251425
checkProposedApiEnabled(extension, 'chatAgents2');
14261426
return extHostChatAgents2.createChatAgent(extension, name, handler);
14271427
},
@@ -1460,8 +1460,6 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
14601460
Breakpoint: extHostTypes.Breakpoint,
14611461
TerminalOutputAnchor: extHostTypes.TerminalOutputAnchor,
14621462
ChatAgentResultFeedbackKind: extHostTypes.ChatAgentResultFeedbackKind,
1463-
ChatMessage: extHostTypes.ChatMessage,
1464-
ChatMessageRole: extHostTypes.ChatMessageRole,
14651463
ChatVariableLevel: extHostTypes.ChatVariableLevel,
14661464
ChatAgentCompletionItem: extHostTypes.ChatAgentCompletionItem,
14671465
CallHierarchyIncomingCall: extHostTypes.CallHierarchyIncomingCall,

src/vs/workbench/api/common/extHostChatAgents2.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Emitter } from 'vs/base/common/event';
1111
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
1212
import { DisposableMap, DisposableStore } from 'vs/base/common/lifecycle';
1313
import { StopWatch } from 'vs/base/common/stopwatch';
14+
import { assertType } from 'vs/base/common/types';
1415
import { URI } from 'vs/base/common/uri';
1516
import { localize } from 'vs/nls';
1617
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
@@ -169,7 +170,7 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
169170
this._proxy = mainContext.getProxy(MainContext.MainThreadChatAgents2);
170171
}
171172

172-
createChatAgent(extension: IExtensionDescription, name: string, handler: vscode.ChatAgentExtendedHandler): vscode.ChatAgent2 {
173+
createChatAgent(extension: IExtensionDescription, name: string, handler: vscode.ChatAgentExtendedRequestHandler): vscode.ChatAgent2 {
173174
const handle = ExtHostChatAgents2._idPool++;
174175
const agent = new ExtHostChatAgent(extension, name, this._proxy, handle, handler);
175176
this._agents.set(handle, agent);
@@ -358,7 +359,7 @@ class ExtHostChatAgent {
358359
public readonly id: string,
359360
private readonly _proxy: MainThreadChatAgentsShape2,
360361
private readonly _handle: number,
361-
private readonly _callback: vscode.ChatAgentExtendedHandler,
362+
private _requestHandler: vscode.ChatAgentExtendedRequestHandler,
362363
) { }
363364

364365
acceptFeedback(feedback: vscode.ChatAgentResult2Feedback) {
@@ -507,6 +508,13 @@ class ExtHostChatAgent {
507508
that._iconPath = v;
508509
updateMetadataSoon();
509510
},
511+
get requestHandler() {
512+
return that._requestHandler;
513+
},
514+
set requestHandler(v) {
515+
assertType(typeof v === 'function', 'Invalid request handler');
516+
that._requestHandler = v;
517+
},
510518
get commandProvider() {
511519
return that._commandProvider;
512520
},
@@ -585,6 +593,7 @@ class ExtHostChatAgent {
585593
return that._onDidReceiveFeedback.event;
586594
},
587595
set agentVariableProvider(v) {
596+
checkProposedApiEnabled(that.extension, 'chatAgents2Additions');
588597
that._agentVariableProvider = v;
589598
if (v) {
590599
if (!v.triggerCharacters.length) {
@@ -597,13 +606,16 @@ class ExtHostChatAgent {
597606
}
598607
},
599608
get agentVariableProvider() {
609+
checkProposedApiEnabled(that.extension, 'chatAgents2Additions');
600610
return that._agentVariableProvider;
601611
},
602612
set welcomeMessageProvider(v) {
613+
checkProposedApiEnabled(that.extension, 'defaultChatAgent');
603614
that._welcomeMessageProvider = v;
604615
updateMetadataSoon();
605616
},
606617
get welcomeMessageProvider() {
618+
checkProposedApiEnabled(that.extension, 'defaultChatAgent');
607619
return that._welcomeMessageProvider;
608620
},
609621
onDidPerformAction: !isProposedApiEnabled(this.extension, 'chatAgents2Additions')
@@ -621,6 +633,6 @@ class ExtHostChatAgent {
621633
}
622634

623635
invoke(request: vscode.ChatAgentRequest, context: vscode.ChatAgentContext, response: vscode.ChatAgentExtendedResponseStream, token: CancellationToken): vscode.ProviderResult<vscode.ChatAgentResult2> {
624-
return this._callback(request, context, response, token);
636+
return this._requestHandler(request, context, response, token);
625637
}
626638
}

src/vs/workbench/api/common/extHostChatProvider.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,7 @@ export class ExtHostChatProvider implements ExtHostChatProviderShape {
149149
this._proxy.$handleProgressChunk(requestId, { index: fragment.index, part: fragment.part });
150150
});
151151

152-
if (data.provider.provideLanguageModelResponse2) {
153-
return data.provider.provideLanguageModelResponse2(messages.map(typeConvert.LanguageModelMessage.to), options, ExtensionIdentifier.toKey(from), progress, token);
154-
} else {
155-
// TODO@jrieken remove
156-
return data.provider.provideLanguageModelResponse(messages.map(typeConvert.ChatMessage.to), options, ExtensionIdentifier.toKey(from), progress, token);
157-
}
158-
152+
return data.provider.provideLanguageModelResponse2(messages.map(typeConvert.LanguageModelMessage.to), options, ExtensionIdentifier.toKey(from), progress, token);
159153
}
160154

161155
//#region --- making request

src/vs/workbench/api/common/extHostTypeConverters.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,12 +2239,6 @@ export namespace ChatInlineFollowup {
22392239
}
22402240
}
22412241

2242-
export namespace ChatMessage {
2243-
export function to(message: chatProvider.IChatMessage): vscode.ChatMessage {
2244-
return new types.ChatMessage(ChatMessageRole.to(message.role), message.content);
2245-
}
2246-
}
2247-
22482242
export namespace LanguageModelMessage {
22492243

22502244
export function to(message: chatProvider.IChatMessage): vscode.LanguageModelMessage {
@@ -2268,28 +2262,6 @@ export namespace LanguageModelMessage {
22682262
}
22692263
}
22702264

2271-
2272-
export namespace ChatMessageRole {
2273-
2274-
export function to(role: chatProvider.ChatMessageRole): vscode.ChatMessageRole {
2275-
switch (role) {
2276-
case chatProvider.ChatMessageRole.System: return types.ChatMessageRole.System;
2277-
case chatProvider.ChatMessageRole.User: return types.ChatMessageRole.User;
2278-
case chatProvider.ChatMessageRole.Assistant: return types.ChatMessageRole.Assistant;
2279-
}
2280-
}
2281-
2282-
export function from(role: vscode.ChatMessageRole): chatProvider.ChatMessageRole {
2283-
switch (role) {
2284-
case types.ChatMessageRole.System: return chatProvider.ChatMessageRole.System;
2285-
case types.ChatMessageRole.Assistant: return chatProvider.ChatMessageRole.Assistant;
2286-
case types.ChatMessageRole.User:
2287-
default:
2288-
return chatProvider.ChatMessageRole.User;
2289-
}
2290-
}
2291-
}
2292-
22932265
export namespace ChatVariable {
22942266
export function objectTo(variableObject: Record<string, IChatRequestVariableValue[]>): Record<string, vscode.ChatVariableValue[]> {
22952267
const result: Record<string, vscode.ChatVariableValue[]> = {};

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4196,24 +4196,6 @@ export enum InteractiveEditorResponseFeedbackKind {
41964196
Bug = 4
41974197
}
41984198

4199-
export enum ChatMessageRole {
4200-
System = 0,
4201-
User = 1,
4202-
Assistant = 2,
4203-
}
4204-
4205-
export class ChatMessage implements vscode.ChatMessage {
4206-
4207-
role: ChatMessageRole;
4208-
content: string;
4209-
name?: string;
4210-
4211-
constructor(role: ChatMessageRole, content: string) {
4212-
this.role = role;
4213-
this.content = content;
4214-
}
4215-
}
4216-
42174199
export enum ChatAgentResultFeedbackKind {
42184200
Unhelpful = 0,
42194201
Helpful = 1,

src/vscode-dts/vscode.proposed.chatAgents2.d.ts

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@
55

66
declare module 'vscode' {
77

8-
/**
9-
* One request/response pair in chat history.
10-
*/
11-
export interface ChatAgentHistoryEntry {
12-
/**
13-
* The request that was sent to the chat agent.
14-
*/
15-
request: ChatAgentRequest;
16-
17-
/**
18-
* The content that was received from the chat agent. Only the progress parts that represent actual content (not metadata) are represented.
19-
*/
20-
response: ReadonlyArray<ChatResponseTextPart | ChatResponseMarkdownPart | ChatResponseFileTreePart | ChatResponseAnchorPart | ChatResponseCommandButtonPart>;
21-
22-
/**
23-
* The result that was received from the chat agent.
24-
*/
25-
result: ChatAgentResult2;
26-
}
27-
288
// TODO@API name: Turn?
299
export class ChatAgentRequestTurn {
3010

@@ -38,15 +18,10 @@ declare module 'vscode' {
3818
*/
3919
readonly prompt: string;
4020

41-
// TODO@API NAME agent
42-
// TODO@API TYPE {agent:string, extension:string}
43-
/** @deprecated */
44-
readonly agentId: string;
45-
4621
/**
4722
* The ID of the chat agent to which this request was directed.
4823
*/
49-
readonly agent: { extensionId: string; agentId: string };
24+
readonly agent: { readonly extensionId: string; readonly agentId: string };
5025

5126
/**
5227
* The name of the {@link ChatAgentCommand command} that was selected for this request.
@@ -74,10 +49,7 @@ declare module 'vscode' {
7449
*/
7550
readonly result: ChatAgentResult2;
7651

77-
/** @deprecated */
78-
readonly agentId: string;
79-
80-
readonly agent: { extensionId: string; agentId: string };
52+
readonly agent: { readonly extensionId: string; readonly agentId: string };
8153

8254
private constructor(response: ReadonlyArray<ChatResponseTextPart | ChatResponseMarkdownPart | ChatResponseFileTreePart | ChatResponseAnchorPart | ChatResponseCommandButtonPart>, result: ChatAgentResult2, agentId: { extensionId: string; agentId: string });
8355
}
@@ -189,14 +161,15 @@ declare module 'vscode' {
189161

190162
/**
191163
* Returns a list of commands that its agent is capable of handling. A command
192-
* can be selected by the user and will then be passed to the {@link ChatAgentHandler handler}
164+
* can be selected by the user and will then be passed to the {@link ChatAgentRequestHandler handler}
193165
* via the {@link ChatAgentRequest.command command} property.
194166
*
195167
*
196168
* @param token A cancellation token.
197169
* @returns A list of commands. The lack of a result can be signaled by returning `undefined`, `null`, or
198170
* an empty array.
199171
*/
172+
// TODO@API Q: should we provide the current history or last results for extra context?
200173
provideCommands(token: CancellationToken): ProviderResult<ChatAgentCommand[]>;
201174
}
202175

@@ -228,6 +201,7 @@ declare module 'vscode' {
228201
/**
229202
* A title to show the user, when it is different than the message.
230203
*/
204+
// TODO@API title vs tooltip?
231205
title?: string;
232206
}
233207

@@ -243,6 +217,12 @@ declare module 'vscode' {
243217
provideFollowups(result: ChatAgentResult2, token: CancellationToken): ProviderResult<ChatAgentFollowup[]>;
244218
}
245219

220+
/**
221+
* A chat request handler is a callback that will be invoked when a request is made to a chat agent.
222+
*/
223+
export type ChatAgentRequestHandler = (request: ChatAgentRequest, context: ChatAgentContext, response: ChatAgentResponseStream, token: CancellationToken) => ProviderResult<ChatAgentResult2>;
224+
225+
246226
export interface ChatAgent2 {
247227

248228
/**
@@ -274,6 +254,11 @@ declare module 'vscode' {
274254
dark: Uri;
275255
} | ThemeIcon;
276256

257+
/**
258+
* The handler for requests to this agent.
259+
*/
260+
requestHandler: ChatAgentRequestHandler;
261+
277262
/**
278263
* This provider will be called to retrieve the agent's commands.
279264
*/
@@ -284,16 +269,6 @@ declare module 'vscode' {
284269
*/
285270
followupProvider?: ChatAgentFollowupProvider;
286271

287-
288-
// TODO@API
289-
// notify(request: ChatResponsePart, reference: string): boolean;
290-
// BETTER
291-
// requestResponseStream(result: ChatAgentResult, callback: (stream: ChatAgentResponseStream) => void, why?: string): void;
292-
293-
// TODO@API
294-
// clear NEVER happens
295-
// onDidClearResult(value: TResult): void;
296-
297272
/**
298273
* When the user clicks this agent in `/help`, this text will be submitted to this command
299274
*/
@@ -315,9 +290,9 @@ declare module 'vscode' {
315290
}
316291

317292
export interface ChatAgentResolvedVariable {
318-
name: string;
319-
range: [start: number, end: number];
320-
values: ChatVariableValue[];
293+
readonly name: string;
294+
readonly range: [start: number, end: number];
295+
readonly values: ChatVariableValue[];
321296
}
322297

323298
export interface ChatAgentRequest {
@@ -330,17 +305,17 @@ declare module 'vscode' {
330305
* *Note* that the {@link ChatAgent2.name name} of the agent and the {@link ChatAgentCommand.name command}
331306
* are not part of the prompt.
332307
*/
333-
prompt: string;
308+
readonly prompt: string;
334309

335310
/**
336311
* The name of the {@link ChatAgentCommand command} that was selected for this request.
337312
*/
338-
command?: string;
313+
readonly command: string | undefined;
339314

340315
/**
341316
* The list of variables that are referenced in the prompt.
342317
*/
343-
variables: ChatAgentResolvedVariable[];
318+
readonly variables: readonly ChatAgentResolvedVariable[];
344319
}
345320

346321
export interface ChatAgentResponseStream {
@@ -429,11 +404,6 @@ declare module 'vscode' {
429404
push(part: ChatResponsePart): ChatAgentResponseStream;
430405
}
431406

432-
// TODO@API
433-
// support ChatResponseCommandPart
434-
// support ChatResponseTextEditPart
435-
// support ChatResponseCodeReferencePart
436-
437407
// TODO@API should the name suffix differentiate between rendered items (XYZPart)
438408
// and metadata like XYZItem
439409
export class ChatResponseTextPart {
@@ -482,9 +452,6 @@ declare module 'vscode' {
482452
| ChatResponseProgressPart | ChatResponseReferencePart | ChatResponseCommandButtonPart;
483453

484454

485-
// TODO@API Remove a different type of `request` so that they can
486-
// evolve at their own pace
487-
export type ChatAgentHandler = (request: ChatAgentRequest, context: ChatAgentContext, response: ChatAgentResponseStream, token: CancellationToken) => ProviderResult<ChatAgentResult2>;
488455

489456
export namespace chat {
490457

@@ -495,7 +462,7 @@ declare module 'vscode' {
495462
* @param handler The reply-handler of the agent.
496463
* @returns A new chat agent
497464
*/
498-
export function createChatAgent(name: string, handler: ChatAgentHandler): ChatAgent2;
465+
export function createChatAgent(name: string, handler: ChatAgentRequestHandler): ChatAgent2;
499466

500467
/**
501468
* Register a variable which can be used in a chat request to any agent.

src/vscode-dts/vscode.proposed.chatAgents2Additions.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ declare module 'vscode' {
158158
constructor(label: string | CompletionItemLabel, values: ChatVariableValue[]);
159159
}
160160

161-
export type ChatAgentExtendedHandler = (request: ChatAgentRequest, context: ChatAgentContext, response: ChatAgentExtendedResponseStream, token: CancellationToken) => ProviderResult<ChatAgentResult2>;
161+
export type ChatAgentExtendedRequestHandler = (request: ChatAgentRequest, context: ChatAgentContext, response: ChatAgentExtendedResponseStream, token: CancellationToken) => ProviderResult<ChatAgentResult2>;
162162

163163
export namespace chat {
164164
/**
165165
* Create a chat agent with the extended progress type
166166
*/
167-
export function createChatAgent(name: string, handler: ChatAgentExtendedHandler): ChatAgent2;
167+
export function createChatAgent(name: string, handler: ChatAgentExtendedRequestHandler): ChatAgent2;
168168
}
169169

170170
/*

0 commit comments

Comments
 (0)