Skip to content

Commit 9d9370b

Browse files
sergeibbbeamodio
authored andcommitted
Refactors feedback context extraction for reuse
Consolidates duplicate logic for extracting feedback context into a shared function to improve maintainability and reduce code repetition. Enhances consistency and simplifies error handling for feedback commands. (#4449, #4475)
1 parent 705de8e commit 9d9370b

File tree

1 file changed

+42
-68
lines changed

1 file changed

+42
-68
lines changed

src/commands/aiFeedback.ts

Lines changed: 42 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,7 @@ export class AIFeedbackPositiveCommand extends ActiveEditorCommand {
6161
}
6262

6363
private extractFeedbackContext(editor?: TextEditor, uri?: Uri): AIFeedbackContext | undefined {
64-
uri = getCommandUri(uri, editor);
65-
if (uri?.scheme !== Schemes.GitLensMarkdown) return undefined;
66-
67-
const authority = uri.authority;
68-
if (!authority) return undefined;
69-
70-
try {
71-
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
72-
73-
// Extract feedback context from metadata
74-
if (metadata.feedbackContext) {
75-
const context = metadata.feedbackContext as unknown as AIFeedbackContext;
76-
77-
// Convert resetsOn string back to Date if it exists
78-
if (context.usage?.limits?.resetsOn && typeof context.usage.limits.resetsOn === 'string') {
79-
const parsedDate = new Date(context.usage.limits.resetsOn);
80-
// Check if the parsed date is valid
81-
if (!isNaN(parsedDate.getTime())) {
82-
context.usage.limits.resetsOn = parsedDate;
83-
} else {
84-
// If invalid date, set to undefined to avoid errors
85-
(context.usage.limits as any).resetsOn = undefined;
86-
Logger.warn('AIFeedbackPositiveCommand', 'Invalid resetsOn date string, setting to undefined');
87-
}
88-
}
89-
90-
return context;
91-
}
92-
93-
return undefined;
94-
} catch (ex) {
95-
Logger.error(ex, 'AIFeedbackPositiveCommand', 'extractFeedbackContext');
96-
return undefined;
97-
}
64+
return extractFeedbackContext(editor, uri, 'AIFeedbackPositiveCommand');
9865
}
9966
}
10067

@@ -117,40 +84,7 @@ export class AIFeedbackNegativeCommand extends ActiveEditorCommand {
11784
}
11885

11986
private extractFeedbackContext(editor?: TextEditor, uri?: Uri): AIFeedbackContext | undefined {
120-
uri = getCommandUri(uri, editor);
121-
if (uri?.scheme !== Schemes.GitLensMarkdown) return undefined;
122-
123-
const authority = uri.authority;
124-
if (!authority) return undefined;
125-
126-
try {
127-
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
128-
129-
// Extract feedback context from metadata
130-
if (metadata.feedbackContext) {
131-
const context = metadata.feedbackContext as unknown as AIFeedbackContext;
132-
133-
// Convert resetsOn string back to Date if it exists
134-
if (context.usage?.limits?.resetsOn && typeof context.usage.limits.resetsOn === 'string') {
135-
const parsedDate = new Date(context.usage.limits.resetsOn);
136-
// Check if the parsed date is valid
137-
if (!isNaN(parsedDate.getTime())) {
138-
context.usage.limits.resetsOn = parsedDate;
139-
} else {
140-
// If invalid date, set to undefined to avoid errors
141-
(context.usage.limits as any).resetsOn = undefined;
142-
Logger.warn('AIFeedbackNegativeCommand', 'Invalid resetsOn date string, setting to undefined');
143-
}
144-
}
145-
146-
return context;
147-
}
148-
149-
return undefined;
150-
} catch (ex) {
151-
Logger.error(ex, 'AIFeedbackNegativeCommand', 'extractFeedbackContext');
152-
return undefined;
153-
}
87+
return extractFeedbackContext(editor, uri, 'AIFeedbackNegativeCommand');
15488
}
15589
}
15690

@@ -196,6 +130,46 @@ async function showDetailedFeedbackForm(container: Container, context: AIFeedbac
196130
void window.showInformationMessage('Thank you for your feedback!');
197131
}
198132

133+
function extractFeedbackContext(editor?: TextEditor, uri?: Uri, commandName?: string): AIFeedbackContext | undefined {
134+
uri = getCommandUri(uri, editor);
135+
if (uri?.scheme !== Schemes.GitLensMarkdown) return undefined;
136+
137+
const authority = uri.authority;
138+
if (!authority) return undefined;
139+
140+
try {
141+
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
142+
143+
// Extract feedback context from metadata
144+
if (metadata.feedbackContext) {
145+
const context = metadata.feedbackContext as unknown as AIFeedbackContext;
146+
147+
// Convert resetsOn string back to Date if it exists
148+
if (context.usage?.limits?.resetsOn && typeof context.usage.limits.resetsOn === 'string') {
149+
const parsedDate = new Date(context.usage.limits.resetsOn);
150+
// Check if the parsed date is valid
151+
if (!isNaN(parsedDate.getTime())) {
152+
context.usage.limits.resetsOn = parsedDate;
153+
} else {
154+
// If invalid date, set to undefined to avoid errors
155+
(context.usage.limits as any).resetsOn = undefined;
156+
Logger.warn(
157+
commandName || 'AIFeedbackCommand',
158+
'Invalid resetsOn date string, setting to undefined',
159+
);
160+
}
161+
}
162+
163+
return context;
164+
}
165+
166+
return undefined;
167+
} catch (ex) {
168+
Logger.error(ex, commandName || 'AIFeedbackCommand', 'extractFeedbackContext');
169+
return undefined;
170+
}
171+
}
172+
199173
function sendFeedbackEvent(
200174
container: Container,
201175
context: AIFeedbackContext,

0 commit comments

Comments
 (0)