Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatVariables.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IChatVariablesService, IDynamicVariable } from '../common/chatVariables.js';
7
import { IToolAndToolSetEnablementMap } from '../common/languageModelToolsService.js';
8
import { IChatWidgetService } from './chat.js';
9
import { ChatDynamicVariableModel } from './contrib/chatDynamicVariables.js';
10
import { Range } from '../../../../editor/common/core/range.js';
11
12
export class ChatVariablesService implements IChatVariablesService {
13
declare _serviceBrand: undefined;
14
15
constructor(
16
@IChatWidgetService private readonly chatWidgetService: IChatWidgetService,
17
) { }
18
19
getDynamicVariables(sessionId: string): ReadonlyArray<IDynamicVariable> {
20
// This is slightly wrong... the parser pulls dynamic references from the input widget, but there is no guarantee that message came from the input here.
21
// Need to ...
22
// - Parser takes list of dynamic references (annoying)
23
// - Or the parser is known to implicitly act on the input widget, and we need to call it before calling the chat service (maybe incompatible with the future, but easy)
24
const widget = this.chatWidgetService.getWidgetBySessionId(sessionId);
25
if (!widget || !widget.viewModel || !widget.supportsFileReferences) {
26
return [];
27
}
28
29
const model = widget.getContrib<ChatDynamicVariableModel>(ChatDynamicVariableModel.ID);
30
if (!model) {
31
return [];
32
}
33
34
if (widget.input.attachmentModel.attachments.length > 0 && widget.viewModel.editing) {
35
const references: IDynamicVariable[] = [];
36
for (const attachment of widget.input.attachmentModel.attachments) {
37
// If the attachment has a range, it is a dynamic variable
38
if (attachment.range) {
39
const referenceObj: IDynamicVariable = {
40
id: attachment.id,
41
fullName: attachment.name,
42
modelDescription: attachment.modelDescription,
43
range: new Range(1, attachment.range.start + 1, 1, attachment.range.endExclusive + 1),
44
icon: attachment.icon,
45
isFile: attachment.kind === 'file',
46
isDirectory: attachment.kind === 'directory',
47
data: attachment.value
48
};
49
references.push(referenceObj);
50
}
51
}
52
53
return [...model.variables, ...references];
54
}
55
56
return model.variables;
57
}
58
59
getSelectedToolAndToolSets(sessionId: string): IToolAndToolSetEnablementMap {
60
const widget = this.chatWidgetService.getWidgetBySessionId(sessionId);
61
if (!widget) {
62
return new Map();
63
}
64
return widget.input.selectedToolsModel.entriesMap.get();
65
66
}
67
68
}
69
70