|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import { Raw } from '@vscode/prompt-tsx';
|
| 7 | +import { ChatCompletionContentPartKind } from '@vscode/prompt-tsx/dist/base/output/rawTypes'; |
7 | 8 | import { FetchStreamSource } from '../../../platform/chat/common/chatMLFetcher';
|
8 | 9 | import { ChatFetchError, ChatFetchResponseType, ChatLocation } from '../../../platform/chat/common/commonTypes';
|
9 | 10 | import { toTextParts } from '../../../platform/chat/common/globalStringUtils';
|
@@ -299,19 +300,16 @@ export class XtabProvider implements IStatelessNextEditProvider {
|
299 | 300 |
|
300 | 301 | const prediction = this.getPredictedOutput(editWindowLines, promptOptions.promptingStrategy);
|
301 | 302 |
|
302 |
| - const messages = [ |
303 |
| - { |
304 |
| - role: Raw.ChatRole.System, |
305 |
| - content: toTextParts(this.pickSystemPrompt(promptOptions.promptingStrategy)) |
306 |
| - }, |
307 |
| - { role: Raw.ChatRole.User, content: toTextParts(userPrompt) } |
308 |
| - ] satisfies Raw.ChatMessage[]; |
| 303 | + const messages = constructMessages({ |
| 304 | + systemMsg: this.pickSystemPrompt(promptOptions.promptingStrategy), |
| 305 | + userMsg: userPrompt, |
| 306 | + }); |
309 | 307 |
|
310 | 308 | logContext.setPrompt(messages);
|
311 | 309 | telemetryBuilder.setPrompt(messages);
|
312 | 310 |
|
313 | 311 | const HARD_CHAR_LIMIT = 30000 * 4; // 30K tokens, assuming 4 chars per token -- we use approximation here because counting tokens exactly is time-consuming
|
314 |
| - const promptCharCount = messages.reduce((total, msg) => total + msg.content.reduce((subtotal, part) => subtotal + part.text.length, 0), 0); |
| 312 | + const promptCharCount = charCount(messages); |
315 | 313 | if (promptCharCount > HARD_CHAR_LIMIT) {
|
316 | 314 | return Result.error(new NoNextEditReason.PromptTooLarge('final'));
|
317 | 315 | }
|
@@ -1134,3 +1132,23 @@ export function findMergeConflictMarkersRange(lines: string[], editWindowRange:
|
1134 | 1132 | }
|
1135 | 1133 | return undefined;
|
1136 | 1134 | }
|
| 1135 | + |
| 1136 | +function constructMessages({ systemMsg, userMsg }: { systemMsg: string; userMsg: string }): Raw.ChatMessage[] { |
| 1137 | + return [ |
| 1138 | + { |
| 1139 | + role: Raw.ChatRole.System, |
| 1140 | + content: toTextParts(systemMsg) |
| 1141 | + }, |
| 1142 | + { |
| 1143 | + role: Raw.ChatRole.User, |
| 1144 | + content: toTextParts(userMsg) |
| 1145 | + } |
| 1146 | + ] satisfies Raw.ChatMessage[]; |
| 1147 | +} |
| 1148 | + |
| 1149 | +function charCount(messages: Raw.ChatMessage[]): number { |
| 1150 | + const promptCharCount = messages.reduce((total, msg) => |
| 1151 | + total + msg.content.reduce((subtotal, part) => |
| 1152 | + subtotal + (part.type === ChatCompletionContentPartKind.Text ? part.text.length : 0), 0), 0); |
| 1153 | + return promptCharCount; |
| 1154 | +} |
0 commit comments