Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/common/annotations.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
import { findLastIdx } from '../../../../base/common/arraysFind.js';
6
import { MarkdownString } from '../../../../base/common/htmlContent.js';
7
import { basename } from '../../../../base/common/resources.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import { IRange } from '../../../../editor/common/core/range.js';
10
import { IChatProgressRenderableResponseContent, IChatProgressResponseContent, appendMarkdownString, canMergeMarkdownStrings } from './chatModel.js';
11
import { IChatAgentVulnerabilityDetails, IChatMarkdownContent } from './chatService.js';
12
13
export const contentRefUrl = 'http://_vscodecontentref_'; // must be lowercase for URI
14
15
export function annotateSpecialMarkdownContent(response: Iterable<IChatProgressResponseContent>): IChatProgressRenderableResponseContent[] {
16
let refIdPool = 0;
17
18
const result: IChatProgressRenderableResponseContent[] = [];
19
for (const item of response) {
20
const previousItemIndex = findLastIdx(result, p => p.kind !== 'textEditGroup' && p.kind !== 'undoStop');
21
const previousItem = result[previousItemIndex];
22
if (item.kind === 'inlineReference') {
23
let label: string | undefined = item.name;
24
if (!label) {
25
if (URI.isUri(item.inlineReference)) {
26
label = basename(item.inlineReference);
27
} else if ('name' in item.inlineReference) {
28
label = item.inlineReference.name;
29
} else {
30
label = basename(item.inlineReference.uri);
31
}
32
}
33
34
const refId = refIdPool++;
35
const printUri = URI.parse(contentRefUrl).with({ path: String(refId) });
36
const markdownText = `[${label}](${printUri.toString()})`;
37
38
const annotationMetadata = { [refId]: item };
39
40
if (previousItem?.kind === 'markdownContent') {
41
const merged = appendMarkdownString(previousItem.content, new MarkdownString(markdownText));
42
result[previousItemIndex] = { ...previousItem, content: merged, inlineReferences: { ...annotationMetadata, ...(previousItem.inlineReferences || {}) } };
43
} else {
44
result.push({ content: new MarkdownString(markdownText), inlineReferences: annotationMetadata, kind: 'markdownContent' });
45
}
46
} else if (item.kind === 'markdownContent' && previousItem?.kind === 'markdownContent' && canMergeMarkdownStrings(previousItem.content, item.content)) {
47
const merged = appendMarkdownString(previousItem.content, item.content);
48
result[previousItemIndex] = { ...previousItem, content: merged };
49
} else if (item.kind === 'markdownVuln') {
50
const vulnText = encodeURIComponent(JSON.stringify(item.vulnerabilities));
51
const markdownText = `<vscode_annotation details='${vulnText}'>${item.content.value}</vscode_annotation>`;
52
if (previousItem?.kind === 'markdownContent') {
53
// Since this is inside a codeblock, it needs to be merged into the previous markdown content.
54
const merged = appendMarkdownString(previousItem.content, new MarkdownString(markdownText));
55
result[previousItemIndex] = { ...previousItem, content: merged };
56
} else {
57
result.push({ content: new MarkdownString(markdownText), kind: 'markdownContent' });
58
}
59
} else if (item.kind === 'codeblockUri') {
60
if (previousItem?.kind === 'markdownContent') {
61
const isEditText = item.isEdit ? ` isEdit` : '';
62
const markdownText = `<vscode_codeblock_uri${isEditText}>${item.uri.toString()}</vscode_codeblock_uri>`;
63
const merged = appendMarkdownString(previousItem.content, new MarkdownString(markdownText));
64
// delete the previous and append to ensure that we don't reorder the edit before the undo stop containing it
65
result.splice(previousItemIndex, 1);
66
result.push({ ...previousItem, content: merged });
67
}
68
} else {
69
result.push(item);
70
}
71
}
72
73
return result;
74
}
75
76
export interface IMarkdownVulnerability {
77
readonly title: string;
78
readonly description: string;
79
readonly range: IRange;
80
}
81
82
export function annotateVulnerabilitiesInText(response: ReadonlyArray<IChatProgressResponseContent>): readonly IChatMarkdownContent[] {
83
const result: IChatMarkdownContent[] = [];
84
for (const item of response) {
85
const previousItem = result[result.length - 1];
86
if (item.kind === 'markdownContent') {
87
if (previousItem?.kind === 'markdownContent') {
88
result[result.length - 1] = { content: new MarkdownString(previousItem.content.value + item.content.value, { isTrusted: previousItem.content.isTrusted }), kind: 'markdownContent' };
89
} else {
90
result.push(item);
91
}
92
} else if (item.kind === 'markdownVuln') {
93
const vulnText = encodeURIComponent(JSON.stringify(item.vulnerabilities));
94
const markdownText = `<vscode_annotation details='${vulnText}'>${item.content.value}</vscode_annotation>`;
95
if (previousItem?.kind === 'markdownContent') {
96
result[result.length - 1] = { content: new MarkdownString(previousItem.content.value + markdownText, { isTrusted: previousItem.content.isTrusted }), kind: 'markdownContent' };
97
} else {
98
result.push({ content: new MarkdownString(markdownText), kind: 'markdownContent' });
99
}
100
}
101
}
102
103
return result;
104
}
105
106
export function extractCodeblockUrisFromText(text: string): { uri: URI; isEdit?: boolean; textWithoutResult: string } | undefined {
107
const match = /<vscode_codeblock_uri( isEdit)?>(.*?)<\/vscode_codeblock_uri>/ms.exec(text);
108
if (match) {
109
const [all, isEdit, uriString] = match;
110
if (uriString) {
111
const result = URI.parse(uriString);
112
const textWithoutResult = text.substring(0, match.index) + text.substring(match.index + all.length);
113
return { uri: result, textWithoutResult, isEdit: !!isEdit };
114
}
115
}
116
return undefined;
117
}
118
119
export function extractVulnerabilitiesFromText(text: string): { newText: string; vulnerabilities: IMarkdownVulnerability[] } {
120
const vulnerabilities: IMarkdownVulnerability[] = [];
121
let newText = text;
122
let match: RegExpExecArray | null;
123
while ((match = /<vscode_annotation details='(.*?)'>(.*?)<\/vscode_annotation>/ms.exec(newText)) !== null) {
124
const [full, details, content] = match;
125
const start = match.index;
126
const textBefore = newText.substring(0, start);
127
const linesBefore = textBefore.split('\n').length - 1;
128
const linesInside = content.split('\n').length - 1;
129
130
const previousNewlineIdx = textBefore.lastIndexOf('\n');
131
const startColumn = start - (previousNewlineIdx + 1) + 1;
132
const endPreviousNewlineIdx = (textBefore + content).lastIndexOf('\n');
133
const endColumn = start + content.length - (endPreviousNewlineIdx + 1) + 1;
134
135
try {
136
const vulnDetails: IChatAgentVulnerabilityDetails[] = JSON.parse(decodeURIComponent(details));
137
vulnDetails.forEach(({ title, description }) => vulnerabilities.push({
138
title, description, range: { startLineNumber: linesBefore + 1, startColumn, endLineNumber: linesBefore + linesInside + 1, endColumn }
139
}));
140
} catch (err) {
141
// Something went wrong with encoding this text, just ignore it
142
}
143
newText = newText.substring(0, start) + content + newText.substring(start + full.length);
144
}
145
146
return { newText, vulnerabilities };
147
}
148
149