Skip to content

Commit 304a868

Browse files
committed
Store AI result context in memory for markdown documents.
Thats because we cannot modify URI of document to review context after the result is rendered. (#4328, #4489)
1 parent 5b94b14 commit 304a868

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/commands/explainBase.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,14 @@ export abstract class ExplainCommandBase extends GlCommandBase {
135135
result: AISummarizeResult,
136136
metadata: MarkdownContentMetadata,
137137
): void {
138-
const metadataWithContext: MarkdownContentMetadata = { ...metadata, context: getAIResultContext(result) };
138+
const context = getAIResultContext(result);
139+
const metadataWithContext: MarkdownContentMetadata = { ...metadata, context: context };
139140
const headerContent = getMarkdownHeaderContent(metadataWithContext, this.container.telemetry.enabled);
140141
const content = `${headerContent}\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
141142

143+
// Store the AI result context in the feedback provider for documents that cannot store it in their URI
144+
this.container.aiFeedback.setMarkdownDocument(documentUri.toString(), context);
145+
142146
this.container.markdown.updateDocument(documentUri, content);
143147
}
144148

src/plus/ai/utils/-webview/ai.utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ export function extractAIResultContext(container: Container, uri: Uri | undefine
288288
if (!authority) return undefined;
289289

290290
try {
291+
const context: AIResultContext | undefined = container.aiFeedback.getMarkdownDocument(uri.toString());
291292
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
292-
return metadata.context;
293+
return context ?? metadata.context;
293294
} catch (ex) {
294295
Logger.error(ex, 'extractResultContext');
295296
return undefined;

src/telemetry/aiFeedbackProvider.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export class AIFeedbackProvider implements Disposable {
1212
constructor() {
1313
// Listen for document close events to clean up contexts
1414
this._disposables.push(
15-
workspace.onDidCloseTextDocument(document => this.removeChangelogDocument(document.uri)),
15+
workspace.onDidCloseTextDocument(document => {
16+
this.removeDocument(document.uri);
17+
}),
1618
);
1719
}
1820

@@ -21,16 +23,19 @@ export class AIFeedbackProvider implements Disposable {
2123
this.addChangelogUri(uri);
2224
}
2325

24-
private removeChangelogDocument(uri: Uri): void {
25-
this.deleteChangelogDocument(uri.toString());
26+
private removeDocument(uri: Uri): void {
27+
const uriString = uri.toString();
28+
this.deleteChangelogDocument(uriString);
2629
this.removeChangelogUri(uri);
30+
this.deleteMarkdownDocument(uriString);
2731
}
2832

2933
private readonly _disposables: Disposable[] = [];
3034
dispose(): void {
3135
this._disposables.forEach(d => void d.dispose());
3236
this._uriResponses.clear();
3337
this._changelogDocuments.clear();
38+
this._markdownDocuments.clear();
3439
this._changelogUris.clear();
3540
this._updateFeedbackContextDebounced = undefined;
3641
this._updateChangelogContextDebounced = undefined;
@@ -70,6 +75,18 @@ export class AIFeedbackProvider implements Disposable {
7075
this._changelogDocuments.delete(documentUri);
7176
}
7277

78+
// Storage for AI feedback context associated with any document
79+
private readonly _markdownDocuments = new Map<string, AIResultContext>();
80+
getMarkdownDocument(documentUri: string): AIResultContext | undefined {
81+
return this._markdownDocuments.get(documentUri);
82+
}
83+
setMarkdownDocument(documentUri: string, context: AIResultContext): void {
84+
this._markdownDocuments.set(documentUri, context);
85+
}
86+
private deleteMarkdownDocument(documentUri: string): void {
87+
this._markdownDocuments.delete(documentUri);
88+
}
89+
7390
// Storage for AI feedback responses by URI
7491
private readonly _uriResponses = new UriMap<AIFeedbackEvent['sentiment']>();
7592
private _updateFeedbackContextDebounced: Deferrable<() => void> | undefined;

0 commit comments

Comments
 (0)