Skip to content

Commit 22e3447

Browse files
authored
Add code citation message to string representation (#223160)
For accessibility
1 parent 3931440 commit 22e3447

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/vs/workbench/contrib/chat/browser/chatContentParts/chatCodeCitationContentPart.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { localize } from 'vs/nls';
1212
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1313
import { ChatTreeItem } from 'vs/workbench/contrib/chat/browser/chat';
1414
import { IChatContentPart, IChatContentPartRenderContext } from 'vs/workbench/contrib/chat/browser/chatContentParts/chatContentParts';
15+
import { getCodeCitationsMessage } from 'vs/workbench/contrib/chat/common/chatModel';
1516
import { IChatCodeCitations, IChatRendererContent } from 'vs/workbench/contrib/chat/common/chatViewModel';
1617
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
1718

@@ -31,10 +32,7 @@ export class ChatCodeCitationContentPart extends Disposable implements IChatCont
3132
) {
3233
super();
3334

34-
const licenseTypes = citations.citations.reduce((set, c) => set.add(c.license), new Set<string>());
35-
const label = licenseTypes.size === 1 ?
36-
localize('codeCitation', "Similar code found with 1 license type", licenseTypes.size) :
37-
localize('codeCitations', "Similar code found with {0} license types", licenseTypes.size);
35+
const label = getCodeCitationsMessage(citations.citations);
3836
const elements = dom.h('.chat-code-citation-message@root', [
3937
dom.h('span.chat-code-citation-label@label'),
4038
dom.h('.chat-code-citation-button-container@button'),

src/vs/workbench/contrib/chat/common/chatModel.ts

+21
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export class Response implements IResponse {
176176
*/
177177
private _markdownContent = '';
178178

179+
private _citations: IChatCodeCitation[] = [];
179180

180181
get value(): IChatProgressResponseContent[] {
181182
return this._responseParts;
@@ -260,6 +261,11 @@ export class Response implements IResponse {
260261
}
261262
}
262263

264+
public addCitation(citation: IChatCodeCitation) {
265+
this._citations.push(citation);
266+
this._updateRepr();
267+
}
268+
263269
private _updateRepr(quiet?: boolean) {
264270
this._responseRepr = this._responseParts.map(part => {
265271
if (part.kind === 'treeData') {
@@ -281,6 +287,8 @@ export class Response implements IResponse {
281287
.filter(s => s.length > 0)
282288
.join('\n\n');
283289

290+
this._responseRepr += this._citations.length ? '\n\n' + getCodeCitationsMessage(this._citations) : '';
291+
284292
this._markdownContent = this._responseParts.map(part => {
285293
if (part.kind === 'inlineReference') {
286294
return basename('uri' in part.inlineReference ? part.inlineReference.uri : part.inlineReference);
@@ -428,6 +436,7 @@ export class ChatResponseModel extends Disposable implements IChatResponseModel
428436

429437
applyCodeCitation(progress: IChatCodeCitation) {
430438
this._codeCitations.push(progress);
439+
this._response.addCitation(progress);
431440
this._onDidChange.fire();
432441
}
433442

@@ -1140,3 +1149,15 @@ export function appendMarkdownString(md1: IMarkdownString, md2: IMarkdownString
11401149
baseUri: md1.baseUri
11411150
};
11421151
}
1152+
1153+
export function getCodeCitationsMessage(citations: ReadonlyArray<IChatCodeCitation>): string {
1154+
if (citations.length === 0) {
1155+
return '';
1156+
}
1157+
1158+
const licenseTypes = citations.reduce((set, c) => set.add(c.license), new Set<string>());
1159+
const label = licenseTypes.size === 1 ?
1160+
localize('codeCitation', "Similar code found with 1 license type", licenseTypes.size) :
1161+
localize('codeCitations', "Similar code found with {0} license types", licenseTypes.size);
1162+
return label;
1163+
}

0 commit comments

Comments
 (0)