Skip to content

Commit 10b5ac0

Browse files
committed
Fixes (hack) limit for vscode provided models
1 parent 28f6998 commit 10b5ac0

File tree

3 files changed

+120
-141
lines changed

3 files changed

+120
-141
lines changed

src/ai/geminiProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Do not make any assumptions or invent details that are not supported by the code
195195
// }
196196

197197
throw new Error(
198-
`Unable to explain commit: (${this.name}:${rsp.status}) ${json?.error?.message || rsp.statusText}`,
198+
`Unable to explain changes: (${this.name}:${rsp.status}) ${json?.error?.message || rsp.statusText}`,
199199
);
200200
}
201201

src/ai/openaiProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ Do not make any assumptions or invent details that are not supported by the code
281281
if (!rsp.ok) {
282282
if (rsp.status === 404) {
283283
throw new Error(
284-
`Unable to explain commit: Your API key doesn't seem to have access to the selected '${model.id}' model`,
284+
`Unable to explain changes: Your API key doesn't seem to have access to the selected '${model.id}' model`,
285285
);
286286
}
287287
if (rsp.status === 429) {
288288
throw new Error(
289-
`Unable to explain commit: (${this.name}:${rsp.status}) Too many requests (rate limit exceeded) or your API key is associated with an expired trial`,
289+
`Unable to explain changes: (${this.name}:${rsp.status}) Too many requests (rate limit exceeded) or your API key is associated with an expired trial`,
290290
);
291291
}
292292

@@ -303,7 +303,7 @@ Do not make any assumptions or invent details that are not supported by the code
303303
}
304304

305305
throw new Error(
306-
`Unable to explain commit: (${this.name}:${rsp.status}) ${json?.error?.message || rsp.statusText}`,
306+
`Unable to explain changes: (${this.name}:${rsp.status}) ${json?.error?.message || rsp.statusText}`,
307307
);
308308
}
309309

src/ai/vscodeProvider.ts

Lines changed: 116 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@ export class VSCodeAIProvider implements AIProvider<typeof provider.id> {
5353
cancellation = options.cancellation;
5454
}
5555

56-
// let retries = 0;
57-
const maxCodeCharacters = getMaxCharacters(model, 2600); // TODO: Use chatModel.countTokens
58-
while (true) {
59-
const code = diff.substring(0, maxCodeCharacters);
60-
61-
let customPrompt = configuration.get('experimental.generateCommitMessagePrompt');
62-
if (!customPrompt.endsWith('.')) {
63-
customPrompt += '.';
64-
}
56+
let retries = 0;
57+
let maxCodeCharacters = getMaxCharacters(model, 2600) - 1000; // TODO: Use chatModel.countTokens
58+
59+
try {
60+
while (true) {
61+
const code = diff.substring(0, maxCodeCharacters);
62+
63+
let customPrompt = configuration.get('experimental.generateCommitMessagePrompt');
64+
if (!customPrompt.endsWith('.')) {
65+
customPrompt += '.';
66+
}
6567

66-
const messages: LanguageModelChatMessage[] = [
67-
LanguageModelChatMessage.User(`You are an advanced AI programming assistant tasked with summarizing code changes into a concise and meaningful commit message. Compose a commit message that:
68+
const messages: LanguageModelChatMessage[] = [
69+
LanguageModelChatMessage.User(`You are an advanced AI programming assistant tasked with summarizing code changes into a concise and meaningful commit message. Compose a commit message that:
6870
- Strictly synthesizes meaningful information from the provided code diff
6971
- Utilizes any additional user-provided context to comprehend the rationale behind the code changes
7072
- Is clear and brief, with an informal yet professional tone, and without superfluous descriptions
@@ -73,71 +75,59 @@ export class VSCodeAIProvider implements AIProvider<typeof provider.id> {
7375
- Most importantly emphasizes the 'why' of the change, its benefits, or the problem it addresses rather than only the 'what' that changed
7476
7577
Follow the user's instructions carefully, don't repeat yourself, don't include the code in the output, or make anything up!`),
76-
LanguageModelChatMessage.User(
77-
`Here is the code diff to use to generate the commit message:\n\n${code}`,
78-
),
79-
...(options?.context
80-
? [
81-
LanguageModelChatMessage.User(
82-
`Here is additional context which should be taken into account when generating the commit message:\n\n${options.context}`,
83-
),
84-
]
85-
: []),
86-
LanguageModelChatMessage.User(customPrompt),
87-
];
88-
89-
try {
90-
const rsp = await chatModel.sendRequest(messages, {}, cancellation);
91-
// if (!rsp.ok) {
92-
// if (rsp.status === 404) {
93-
// throw new Error(
94-
// `Unable to generate commit message: Your API key doesn't seem to have access to the selected '${model}' model`,
95-
// );
96-
// }
97-
// if (rsp.status === 429) {
98-
// throw new Error(
99-
// `Unable to generate commit message: (${this.name}:${rsp.status}) Too many requests (rate limit exceeded) or your API key is associated with an expired trial`,
100-
// );
101-
// }
102-
103-
// let json;
104-
// try {
105-
// json = (await rsp.json()) as { error?: { code: string; message: string } } | undefined;
106-
// } catch {}
107-
108-
// debugger;
109-
110-
// if (retries++ < 2 && json?.error?.code === 'context_length_exceeded') {
111-
// maxCodeCharacters -= 500 * retries;
112-
// continue;
113-
// }
114-
115-
// throw new Error(
116-
// `Unable to generate commit message: (${this.name}:${rsp.status}) ${
117-
// json?.error?.message || rsp.statusText
118-
// }`,
119-
// );
120-
// }
121-
122-
if (diff.length > maxCodeCharacters) {
123-
void window.showWarningMessage(
124-
`The diff of the changes had to be truncated to ${maxCodeCharacters} characters to fit within ${getPossessiveForm(
125-
model.provider.name,
126-
)} limits.`,
78+
LanguageModelChatMessage.User(
79+
`Here is the code diff to use to generate the commit message:\n\n${code}`,
80+
),
81+
...(options?.context
82+
? [
83+
LanguageModelChatMessage.User(
84+
`Here is additional context which should be taken into account when generating the commit message:\n\n${options.context}`,
85+
),
86+
]
87+
: []),
88+
LanguageModelChatMessage.User(customPrompt),
89+
];
90+
91+
try {
92+
const rsp = await chatModel.sendRequest(messages, {}, cancellation);
93+
94+
if (diff.length > maxCodeCharacters) {
95+
void window.showWarningMessage(
96+
`The diff of the changes had to be truncated to ${maxCodeCharacters} characters to fit within ${getPossessiveForm(
97+
model.provider.name,
98+
)} limits.`,
99+
);
100+
}
101+
102+
let message = '';
103+
for await (const fragment of rsp.text) {
104+
message += fragment;
105+
}
106+
107+
return message.trim();
108+
} catch (ex) {
109+
debugger;
110+
111+
let message = ex instanceof Error ? ex.message : String(ex);
112+
113+
if (ex instanceof Error && 'cause' in ex && ex.cause instanceof Error) {
114+
message += `\n${ex.cause.message}`;
115+
116+
if (retries++ < 2 && ex.cause.message.includes('exceeds token limit')) {
117+
maxCodeCharacters -= 500 * retries;
118+
continue;
119+
}
120+
}
121+
122+
throw new Error(
123+
`Unable to generate commit message: (${getPossessiveForm(model.provider.name)}:${
124+
ex.code
125+
}) ${message}`,
127126
);
128127
}
129-
130-
let message = '';
131-
for await (const fragment of rsp.text) {
132-
message += fragment;
133-
}
134-
135-
return message.trim();
136-
} catch (ex) {
137-
debugger;
138-
} finally {
139-
cancellationSource?.dispose();
140128
}
129+
} finally {
130+
cancellationSource?.dispose();
141131
}
142132
}
143133

@@ -159,79 +149,68 @@ Follow the user's instructions carefully, don't repeat yourself, don't include t
159149
cancellation = options.cancellation;
160150
}
161151

162-
// let retries = 0;
163-
const maxCodeCharacters = getMaxCharacters(model, 3000);
164-
while (true) {
165-
const code = diff.substring(0, maxCodeCharacters);
152+
let retries = 0;
153+
let maxCodeCharacters = getMaxCharacters(model, 3000) - 1000;
154+
155+
try {
156+
while (true) {
157+
const code = diff.substring(0, maxCodeCharacters);
166158

167-
const messages: LanguageModelChatMessage[] = [
168-
LanguageModelChatMessage.User(`You are an advanced AI programming assistant tasked with summarizing code changes into an explanation that is both easy to understand and meaningful. Construct an explanation that:
159+
const messages: LanguageModelChatMessage[] = [
160+
LanguageModelChatMessage.User(`You are an advanced AI programming assistant tasked with summarizing code changes into an explanation that is both easy to understand and meaningful. Construct an explanation that:
169161
- Concisely synthesizes meaningful information from the provided code diff
170162
- Incorporates any additional context provided by the user to understand the rationale behind the code changes
171163
- Places the emphasis on the 'why' of the change, clarifying its benefits or addressing the problem that necessitated the change, beyond just detailing the 'what' has changed
172164
173165
Do not make any assumptions or invent details that are not supported by the code diff or the user-provided context.`),
174-
LanguageModelChatMessage.User(
175-
`Here is additional context provided by the author of the changes, which should provide some explanation to why these changes where made. Please strongly consider this information when generating your explanation:\n\n${message}`,
176-
),
177-
LanguageModelChatMessage.User(
178-
`Now, kindly explain the following code diff in a way that would be clear to someone reviewing or trying to understand these changes:\n\n${code}`,
179-
),
180-
LanguageModelChatMessage.User(
181-
'Remember to frame your explanation in a way that is suitable for a reviewer to quickly grasp the essence of the changes, the issues they resolve, and their implications on the codebase.',
182-
),
183-
];
184-
185-
try {
186-
const rsp = await chatModel.sendRequest(messages, {}, cancellation);
187-
// if (!rsp.ok) {
188-
// if (rsp.status === 404) {
189-
// throw new Error(
190-
// `Unable to explain commit: Your API key doesn't seem to have access to the selected '${model}' model`,
191-
// );
192-
// }
193-
// if (rsp.status === 429) {
194-
// throw new Error(
195-
// `Unable to explain commit: (${this.name}:${rsp.status}) Too many requests (rate limit exceeded) or your API key is associated with an expired trial`,
196-
// );
197-
// }
198-
199-
// let json;
200-
// try {
201-
// json = (await rsp.json()) as { error?: { code: string; message: string } } | undefined;
202-
// } catch {}
203-
204-
// debugger;
205-
206-
// if (retries++ < 2 && json?.error?.code === 'context_length_exceeded') {
207-
// maxCodeCharacters -= 500 * retries;
208-
// continue;
209-
// }
210-
211-
// throw new Error(
212-
// `Unable to explain commit: (${this.name}:${rsp.status}) ${json?.error?.message || rsp.statusText}`,
213-
// );
214-
// }
215-
216-
if (diff.length > maxCodeCharacters) {
217-
void window.showWarningMessage(
218-
`The diff of the changes had to be truncated to ${maxCodeCharacters} characters to fit within ${getPossessiveForm(
219-
model.provider.name,
220-
)} limits.`,
166+
LanguageModelChatMessage.User(
167+
`Here is additional context provided by the author of the changes, which should provide some explanation to why these changes where made. Please strongly consider this information when generating your explanation:\n\n${message}`,
168+
),
169+
LanguageModelChatMessage.User(
170+
`Now, kindly explain the following code diff in a way that would be clear to someone reviewing or trying to understand these changes:\n\n${code}`,
171+
),
172+
LanguageModelChatMessage.User(
173+
'Remember to frame your explanation in a way that is suitable for a reviewer to quickly grasp the essence of the changes, the issues they resolve, and their implications on the codebase.',
174+
),
175+
];
176+
177+
try {
178+
const rsp = await chatModel.sendRequest(messages, {}, cancellation);
179+
180+
if (diff.length > maxCodeCharacters) {
181+
void window.showWarningMessage(
182+
`The diff of the changes had to be truncated to ${maxCodeCharacters} characters to fit within ${getPossessiveForm(
183+
model.provider.name,
184+
)} limits.`,
185+
);
186+
}
187+
188+
let summary = '';
189+
for await (const fragment of rsp.text) {
190+
summary += fragment;
191+
}
192+
193+
return summary.trim();
194+
} catch (ex) {
195+
debugger;
196+
let message = ex instanceof Error ? ex.message : String(ex);
197+
198+
if (ex instanceof Error && 'cause' in ex && ex.cause instanceof Error) {
199+
message += `\n${ex.cause.message}`;
200+
201+
if (retries++ < 2 && ex.cause.message.includes('exceeds token limit')) {
202+
maxCodeCharacters -= 500 * retries;
203+
continue;
204+
}
205+
}
206+
207+
throw new Error(
208+
`Unable to explain changes: (${getPossessiveForm(model.provider.name)}:${ex.code}) ${message}`,
221209
);
222210
}
223-
224-
let summary = '';
225-
for await (const fragment of rsp.text) {
226-
summary += fragment;
227-
}
228-
229-
return summary.trim();
230-
} catch (ex) {
231-
debugger;
232-
} finally {
233-
cancellationSource?.dispose();
234211
}
212+
} finally {
213+
cancellationSource?.dispose();
235214
}
236215
}
237216
}

0 commit comments

Comments
 (0)