Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/promptSyntax/promptFileRewriter.ts
5254 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 { CancellationToken } from '../../../../../base/common/cancellation.js';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { ICodeEditorService } from '../../../../../editor/browser/services/codeEditorService.js';
9
import { EditOperation } from '../../../../../editor/common/core/editOperation.js';
10
import { Range } from '../../../../../editor/common/core/range.js';
11
import { ITextModel } from '../../../../../editor/common/model.js';
12
import { ILanguageModelToolsService, IToolAndToolSetEnablementMap } from '../../common/tools/languageModelToolsService.js';
13
import { PromptHeaderAttributes } from '../../common/promptSyntax/promptFileParser.js';
14
import { IPromptsService } from '../../common/promptSyntax/service/promptsService.js';
15
import { formatArrayValue } from '../../common/promptSyntax/utils/promptEditHelper.js';
16
17
export class PromptFileRewriter {
18
constructor(
19
@ICodeEditorService private readonly _codeEditorService: ICodeEditorService,
20
@IPromptsService private readonly _promptsService: IPromptsService,
21
@ILanguageModelToolsService private readonly _languageModelToolsService: ILanguageModelToolsService
22
) {
23
}
24
25
public async openAndRewriteTools(uri: URI, newTools: IToolAndToolSetEnablementMap | undefined, token: CancellationToken): Promise<void> {
26
const editor = await this._codeEditorService.openCodeEditor({ resource: uri }, this._codeEditorService.getFocusedCodeEditor());
27
if (!editor || !editor.hasModel()) {
28
return;
29
}
30
const model = editor.getModel();
31
32
const promptAST = this._promptsService.getParsedPromptFile(model);
33
if (!promptAST.header) {
34
return undefined;
35
}
36
37
const toolsAttr = promptAST.header.getAttribute(PromptHeaderAttributes.tools);
38
if (!toolsAttr) {
39
return undefined;
40
}
41
42
editor.setSelection(toolsAttr.range);
43
if (newTools === undefined) {
44
this.rewriteAttribute(model, '', toolsAttr.range);
45
return;
46
} else {
47
this.rewriteTools(model, newTools, toolsAttr.value.range, toolsAttr.value.type === 'string');
48
}
49
}
50
51
public rewriteTools(model: ITextModel, newTools: IToolAndToolSetEnablementMap, range: Range, isString: boolean): void {
52
const newToolNames = this._languageModelToolsService.toFullReferenceNames(newTools);
53
const newEntries = newToolNames.map(toolName => formatArrayValue(toolName)).join(', ');
54
const newValue = isString ? newEntries : `[${newEntries}]`;
55
this.rewriteAttribute(model, newValue, range);
56
}
57
58
private rewriteAttribute(model: ITextModel, newValue: string, range: Range): void {
59
model.pushStackElement();
60
model.pushEditOperations(null, [EditOperation.replaceMove(range, newValue)], () => null);
61
model.pushStackElement();
62
}
63
64
public async openAndRewriteName(uri: URI, newName: string, token: CancellationToken): Promise<void> {
65
const editor = await this._codeEditorService.openCodeEditor({ resource: uri }, this._codeEditorService.getFocusedCodeEditor());
66
if (!editor || !editor.hasModel()) {
67
return;
68
}
69
const model = editor.getModel();
70
71
const promptAST = this._promptsService.getParsedPromptFile(model);
72
if (!promptAST.header) {
73
return;
74
}
75
76
const nameAttr = promptAST.header.getAttribute(PromptHeaderAttributes.name);
77
if (!nameAttr) {
78
return;
79
}
80
if (nameAttr.value.type === 'string' && nameAttr.value.value === newName) {
81
return;
82
}
83
84
editor.setSelection(nameAttr.range);
85
this.rewriteAttribute(model, newName, nameAttr.value.range);
86
}
87
}
88
89
90