-
Notifications
You must be signed in to change notification settings - Fork 39.1k
Expand file tree
/
Copy pathextHostLanguageModelTools.ts
More file actions
378 lines (323 loc) · 15.2 KB
/
extHostLanguageModelTools.ts
File metadata and controls
378 lines (323 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import { raceCancellation } from '../../../base/common/async.js';
import { CancellationToken } from '../../../base/common/cancellation.js';
import { CancellationError } from '../../../base/common/errors.js';
import { IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
import { revive } from '../../../base/common/marshalling.js';
import { generateUuid } from '../../../base/common/uuid.js';
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
import { IPreparedToolInvocation, IStreamedToolInvocation, isToolInvocationContext, IToolInvocation, IToolInvocationContext, IToolInvocationPreparationContext, IToolInvocationStreamContext, IToolResult, ToolInvocationPresentation } from '../../contrib/chat/common/tools/languageModelToolsService.js';
import { computeCombinationKey } from '../../contrib/chat/common/tools/languageModelToolsConfirmationService.js';
import { ExtensionEditToolId, InternalEditToolId } from '../../contrib/chat/common/tools/builtinTools/editFileTool.js';
import { InternalFetchWebPageToolId } from '../../contrib/chat/common/tools/builtinTools/tools.js';
import { SearchExtensionsToolId } from '../../contrib/extensions/common/searchExtensionsTool.js';
import { checkProposedApiEnabled, isProposedApiEnabled } from '../../services/extensions/common/extensions.js';
import { Dto, SerializableObjectWithBuffers } from '../../services/extensions/common/proxyIdentifier.js';
import { ExtHostLanguageModelToolsShape, IMainContext, IToolDataDto, IToolDefinitionDto, MainContext, MainThreadLanguageModelToolsShape } from './extHost.protocol.js';
import { ExtHostLanguageModels } from './extHostLanguageModels.js';
import * as typeConvert from './extHostTypeConverters.js';
import { URI } from '../../../base/common/uri.js';
class Tool {
private _data: IToolDataDto;
private _apiObject: vscode.LanguageModelToolInformation | undefined;
private _apiObjectWithChatParticipantAdditions: vscode.LanguageModelToolInformation | undefined;
constructor(data: IToolDataDto) {
this._data = data;
}
update(newData: IToolDataDto): void {
this._data = newData;
this._apiObject = undefined;
this._apiObjectWithChatParticipantAdditions = undefined;
}
get data(): IToolDataDto {
return this._data;
}
get apiObject(): vscode.LanguageModelToolInformation {
if (!this._apiObject) {
this._apiObject = Object.freeze({
name: this._data.id,
description: this._data.modelDescription,
inputSchema: this._data.inputSchema,
tags: this._data.tags ?? [],
source: undefined
});
}
return this._apiObject;
}
get apiObjectWithChatParticipantAdditions() {
if (!this._apiObjectWithChatParticipantAdditions) {
this._apiObjectWithChatParticipantAdditions = Object.freeze({
name: this._data.id,
description: this._data.modelDescription,
inputSchema: this._data.inputSchema,
tags: this._data.tags ?? [],
source: typeConvert.LanguageModelToolSource.to(this._data.source),
fullReferenceName: this._data.fullReferenceName
});
}
return this._apiObjectWithChatParticipantAdditions;
}
}
export class ExtHostLanguageModelTools implements ExtHostLanguageModelToolsShape {
/** A map of tools that were registered in this EH */
private readonly _registeredTools = new Map<string, { extension: IExtensionDescription; tool: vscode.LanguageModelTool<Object> }>();
private readonly _proxy: MainThreadLanguageModelToolsShape;
private readonly _tokenCountFuncs = new Map</* call ID */string, (text: string, token?: vscode.CancellationToken) => Thenable<number>>();
/** A map of all known tools, from other EHs or registered in vscode core */
private readonly _allTools = new Map<string, Tool>();
constructor(
mainContext: IMainContext,
private readonly _languageModels: ExtHostLanguageModels,
) {
this._proxy = mainContext.getProxy(MainContext.MainThreadLanguageModelTools);
this._proxy.$getTools().then(tools => {
for (const tool of tools) {
this._allTools.set(tool.id, new Tool(revive(tool)));
}
});
}
async $countTokensForInvocation(callId: string, input: string, token: CancellationToken): Promise<number> {
const fn = this._tokenCountFuncs.get(callId);
if (!fn) {
throw new Error(`Tool invocation call ${callId} not found`);
}
return await fn(input, token);
}
async invokeTool(extension: IExtensionDescription, toolIdOrInfo: string | vscode.LanguageModelToolInformation, options: vscode.LanguageModelToolInvocationOptions<any>, token?: CancellationToken): Promise<vscode.LanguageModelToolResult> {
const toolId = typeof toolIdOrInfo === 'string' ? toolIdOrInfo : toolIdOrInfo.name;
const callId = generateUuid();
if (options.tokenizationOptions) {
this._tokenCountFuncs.set(callId, options.tokenizationOptions.countTokens);
}
try {
if (options.toolInvocationToken && !isToolInvocationContext(options.toolInvocationToken)) {
throw new Error(`Invalid tool invocation token`);
}
if ((toolId === InternalEditToolId || toolId === ExtensionEditToolId) && !isProposedApiEnabled(extension, 'chatParticipantPrivate')) {
throw new Error(`Invalid tool: ${toolId}`);
}
// Making the round trip here because not all tools were necessarily registered in this EH
const result = await this._proxy.$invokeTool({
toolId,
callId,
parameters: options.input,
tokenBudget: options.tokenizationOptions?.tokenBudget,
context: options.toolInvocationToken as IToolInvocationContext | undefined,
chatRequestId: isProposedApiEnabled(extension, 'chatParticipantPrivate') ? options.chatRequestId : undefined,
chatInteractionId: isProposedApiEnabled(extension, 'chatParticipantPrivate') ? options.chatInteractionId : undefined,
subAgentInvocationId: isProposedApiEnabled(extension, 'chatParticipantPrivate') ? options.subAgentInvocationId : undefined,
chatStreamToolCallId: isProposedApiEnabled(extension, 'chatParticipantAdditions') ? options.chatStreamToolCallId : undefined,
preToolUseResult: isProposedApiEnabled(extension, 'chatParticipantPrivate') ? options.preToolUseResult : undefined,
}, token);
const dto: Dto<IToolResult> = result instanceof SerializableObjectWithBuffers ? result.value : result;
return typeConvert.LanguageModelToolResult.to(revive(dto));
} finally {
this._tokenCountFuncs.delete(callId);
}
}
$onDidChangeTools(tools: IToolDataDto[]): void {
const oldTools = new Set(this._allTools.keys());
for (const tool of tools) {
oldTools.delete(tool.id);
const existing = this._allTools.get(tool.id);
if (existing) {
existing.update(tool);
} else {
this._allTools.set(tool.id, new Tool(revive(tool)));
}
}
for (const id of oldTools) {
this._allTools.delete(id);
}
}
getTools(extension: IExtensionDescription): vscode.LanguageModelToolInformation[] {
const hasParticipantAdditions = isProposedApiEnabled(extension, 'chatParticipantPrivate');
return Array.from(this._allTools.values())
.map(tool => hasParticipantAdditions ? tool.apiObjectWithChatParticipantAdditions : tool.apiObject)
.filter(tool => {
switch (tool.name) {
case InternalEditToolId:
case ExtensionEditToolId:
case InternalFetchWebPageToolId:
case SearchExtensionsToolId:
return isProposedApiEnabled(extension, 'chatParticipantPrivate');
default:
return true;
}
});
}
async $invokeTool(dto: Dto<IToolInvocation>, token: CancellationToken): Promise<Dto<IToolResult> | SerializableObjectWithBuffers<Dto<IToolResult>>> {
const item = this._registeredTools.get(dto.toolId);
if (!item) {
throw new Error(`Unknown tool ${dto.toolId}`);
}
const options: vscode.LanguageModelToolInvocationOptions<Object> = {
input: dto.parameters,
toolInvocationToken: revive(dto.context) as unknown as vscode.ChatParticipantToolToken | undefined,
};
if (isProposedApiEnabled(item.extension, 'chatParticipantPrivate')) {
options.chatRequestId = dto.chatRequestId;
options.chatInteractionId = dto.chatInteractionId;
options.chatSessionResource = URI.revive(dto.context?.sessionResource);
options.subAgentInvocationId = dto.subAgentInvocationId;
}
if (isProposedApiEnabled(item.extension, 'chatParticipantAdditions') && dto.modelId) {
options.model = await this.getModel(dto.modelId, item.extension);
}
if (isProposedApiEnabled(item.extension, 'chatParticipantAdditions') && dto.chatStreamToolCallId) {
options.chatStreamToolCallId = dto.chatStreamToolCallId;
}
if (dto.tokenBudget !== undefined) {
options.tokenizationOptions = {
tokenBudget: dto.tokenBudget,
countTokens: this._tokenCountFuncs.get(dto.callId) || ((value, token = CancellationToken.None) =>
this._proxy.$countTokensForInvocation(dto.callId, value, token))
};
}
let progress: vscode.Progress<{ message?: string | vscode.MarkdownString; increment?: number }> | undefined;
if (isProposedApiEnabled(item.extension, 'toolProgress')) {
let lastProgress: number | undefined;
progress = {
report: value => {
if (value.increment !== undefined) {
lastProgress = (lastProgress ?? 0) + value.increment;
}
this._proxy.$acceptToolProgress(dto.callId, {
message: typeConvert.MarkdownString.fromStrict(value.message),
progress: lastProgress === undefined ? undefined : lastProgress / 100,
});
}
};
}
// todo: 'any' cast because TS can't handle the overloads
// eslint-disable-next-line local/code-no-any-casts
const extensionResult = await raceCancellation(Promise.resolve((item.tool.invoke as any)(options, token, progress!)), token);
if (!extensionResult) {
throw new CancellationError();
}
return typeConvert.LanguageModelToolResult.from(extensionResult, item.extension);
}
private async getModel(modelId: string, extension: IExtensionDescription): Promise<vscode.LanguageModelChat> {
let model: vscode.LanguageModelChat | undefined;
if (modelId) {
model = await this._languageModels.getLanguageModelByIdentifier(extension, modelId);
}
if (!model) {
model = await this._languageModels.getDefaultLanguageModel(extension);
if (!model) {
throw new Error('Language model unavailable');
}
}
return model;
}
async $handleToolStream(toolId: string, context: IToolInvocationStreamContext, token: CancellationToken): Promise<IStreamedToolInvocation | undefined> {
const item = this._registeredTools.get(toolId);
if (!item) {
throw new Error(`Unknown tool ${toolId}`);
}
// Only call handleToolStream if it's defined on the tool
if (!item.tool.handleToolStream) {
return undefined;
}
// Ensure the chatParticipantAdditions API is enabled
checkProposedApiEnabled(item.extension, 'chatParticipantAdditions');
const options: vscode.LanguageModelToolInvocationStreamOptions<any> = {
rawInput: context.rawInput,
chatRequestId: context.chatRequestId,
chatSessionResource: context.chatSessionResource,
chatInteractionId: context.chatInteractionId
};
const result = await item.tool.handleToolStream(options, token);
if (!result) {
return undefined;
}
return {
invocationMessage: typeConvert.MarkdownString.fromStrict(result.invocationMessage)
};
}
async $prepareToolInvocation(toolId: string, context: IToolInvocationPreparationContext, token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {
const item = this._registeredTools.get(toolId);
if (!item) {
throw new Error(`Unknown tool ${toolId}`);
}
const options: vscode.LanguageModelToolInvocationPrepareOptions<any> = {
input: context.parameters,
chatRequestId: context.chatRequestId,
chatSessionResource: context.chatSessionResource,
chatInteractionId: context.chatInteractionId,
forceConfirmationReason: context.forceConfirmationReason
};
if (context.forceConfirmationReason) {
checkProposedApiEnabled(item.extension, 'chatParticipantPrivate');
}
if (item.tool.prepareInvocation) {
const result = await item.tool.prepareInvocation(options, token);
if (!result) {
return undefined;
}
if (result.pastTenseMessage || result.presentation) {
checkProposedApiEnabled(item.extension, 'chatParticipantPrivate');
}
if (result.confirmationMessages?.approveCombination !== undefined) {
checkProposedApiEnabled(item.extension, 'toolInvocationApproveCombination');
}
const approveCombination = result.confirmationMessages?.approveCombination;
const approveCombinationLabel = approveCombination
? typeConvert.MarkdownString.fromStrict(approveCombination.message)
: undefined;
const approveCombinationKey = approveCombinationLabel
? await computeCombinationKey(toolId, context.parameters)
: undefined;
return {
confirmationMessages: result.confirmationMessages ? {
title: typeof result.confirmationMessages.title === 'string' ? result.confirmationMessages.title : typeConvert.MarkdownString.from(result.confirmationMessages.title),
message: typeof result.confirmationMessages.message === 'string' ? result.confirmationMessages.message : typeConvert.MarkdownString.from(result.confirmationMessages.message),
approveCombination: approveCombinationLabel && approveCombinationKey ? { label: approveCombinationLabel, key: approveCombinationKey, arguments: approveCombination!.arguments } : undefined,
} : undefined,
invocationMessage: typeConvert.MarkdownString.fromStrict(result.invocationMessage),
pastTenseMessage: typeConvert.MarkdownString.fromStrict(result.pastTenseMessage),
presentation: result.presentation as ToolInvocationPresentation | undefined,
};
}
return undefined;
}
registerTool(extension: IExtensionDescription, id: string, tool: vscode.LanguageModelTool<any>): IDisposable {
this._registeredTools.set(id, { extension, tool });
this._proxy.$registerTool(id, typeof tool.handleToolStream === 'function');
return toDisposable(() => {
this._registeredTools.delete(id);
this._proxy.$unregisterTool(id);
});
}
registerToolDefinition(extension: IExtensionDescription, definition: vscode.LanguageModelToolDefinition, tool: vscode.LanguageModelTool<any>): IDisposable {
checkProposedApiEnabled(extension, 'languageModelToolSupportsModel');
const id = definition.name;
// Convert the definition to a DTO
const dto: IToolDefinitionDto = {
id,
displayName: definition.displayName,
toolReferenceName: definition.toolReferenceName,
userDescription: definition.userDescription,
modelDescription: definition.description,
inputSchema: definition.inputSchema as object,
source: {
type: 'extension',
label: extension.displayName ?? extension.name,
extensionId: extension.identifier,
},
icon: typeConvert.IconPath.from(definition.icon),
models: definition.models,
toolSet: definition.toolSet,
};
this._registeredTools.set(id, { extension, tool });
this._proxy.$registerToolWithDefinition(extension.identifier, dto, typeof tool.handleToolStream === 'function');
return toDisposable(() => {
this._registeredTools.delete(id);
this._proxy.$unregisterTool(id);
});
}
}