Skip to content

Commit a01d144

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 233a7c8 commit a01d144

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
@@ -133,10 +133,14 @@ export abstract class ExplainCommandBase extends GlCommandBase {
133133
result: AISummarizeResult,
134134
metadata: MarkdownContentMetadata,
135135
): void {
136-
const metadataWithContext: MarkdownContentMetadata = { ...metadata, context: getAIResultContext(result) };
136+
const context = getAIResultContext(result);
137+
const metadataWithContext: MarkdownContentMetadata = { ...metadata, context: context };
137138
const headerContent = getMarkdownHeaderContent(metadataWithContext, this.container.telemetry.enabled);
138139
const content = `${headerContent}\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
139140

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

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

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

289289
try {
290+
const context: AIResultContext | undefined = container.aiFeedback.getMarkdownDocument(uri.toString());
290291
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
291-
return metadata.context;
292+
return context ?? metadata.context;
292293
} catch (ex) {
293294
Logger.error(ex, 'extractResultContext');
294295
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.deleteChangelogFeedback(uri.toString());
26+
private removeDocument(uri: Uri): void {
27+
const uriString = uri.toString();
28+
this.deleteChangelogFeedback(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._changelogFeedbacks.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._changelogFeedbacks.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)