Skip to content

Commit 34f7bb2

Browse files
committed
nes: refactor: abstract constructing messages
1 parent 737570f commit 34f7bb2

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/extension/xtab/node/xtabProvider.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { Raw } from '@vscode/prompt-tsx';
7+
import { ChatCompletionContentPartKind } from '@vscode/prompt-tsx/dist/base/output/rawTypes';
78
import { FetchStreamSource } from '../../../platform/chat/common/chatMLFetcher';
89
import { ChatFetchError, ChatFetchResponseType, ChatLocation } from '../../../platform/chat/common/commonTypes';
910
import { toTextParts } from '../../../platform/chat/common/globalStringUtils';
@@ -299,19 +300,16 @@ export class XtabProvider implements IStatelessNextEditProvider {
299300

300301
const prediction = this.getPredictedOutput(editWindowLines, promptOptions.promptingStrategy);
301302

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+
});
309307

310308
logContext.setPrompt(messages);
311309
telemetryBuilder.setPrompt(messages);
312310

313311
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);
315313
if (promptCharCount > HARD_CHAR_LIMIT) {
316314
return Result.error(new NoNextEditReason.PromptTooLarge('final'));
317315
}
@@ -1134,3 +1132,23 @@ export function findMergeConflictMarkersRange(lines: string[], editWindowRange:
11341132
}
11351133
return undefined;
11361134
}
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

Comments
 (0)