filter last empty assistant message

This commit is contained in:
Dallin Romney 2025-04-11 00:35:14 -07:00
parent b9c31599ad
commit bc963330c7
2 changed files with 29 additions and 8 deletions

View file

@ -1,9 +1,16 @@
import { ChatHistoryItem, ChatMessage, MessagePart, RuleWithSource } from "../";
import {
ChatHistoryItem,
ChatMessage,
RuleWithSource,
TextMessagePart,
} from "../";
import { normalizeToMessageParts } from "../util/messageContent";
import { messageIsEmpty } from "./countTokens";
import { getSystemMessageWithRules } from "./rules/getSystemMessageWithRules";
export const DEFAULT_CHAT_SYSTEM_MESSAGE_URL =
"https://github.com/continuedev/continue/blob/997dd893231971cc6b21935936aa7514cba065a3/core/llm/constructMessages.ts#L4";
"https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts#L8";
export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\
<important_rules>
Always include the language and file name in the info string when you write code blocks.
@ -53,9 +60,14 @@ export function constructMessages(
// Gather context items for user messages
let content = normalizeToMessageParts(historyItem.message);
const ctxItems = historyItem.contextItems.map((ctxItem) => {
return { type: "text", text: `${ctxItem.content}\n` } as MessagePart;
});
const ctxItems = historyItem.contextItems
.map((ctxItem) => {
return {
type: "text",
text: `${ctxItem.content}\n`,
} as TextMessagePart;
})
.filter((part) => !!part.text.trim());
content = [...ctxItems, ...content];
msgs.push({
@ -80,13 +92,22 @@ export function constructMessages(
userMessage,
currentModel: modelName,
});
if (systemMessage) {
if (systemMessage.trim()) {
msgs.unshift({
role: "system",
content: baseChatSystemMessage ?? DEFAULT_CHAT_SYSTEM_MESSAGE,
content: systemMessage,
});
}
const lastMessage = msgs.at(-1);
if (
lastMessage &&
lastMessage.role === "assistant" &&
messageIsEmpty(lastMessage)
) {
msgs.pop();
}
// Remove the "id" from all of the messages
return msgs.map((msg) => {
const { id, ...rest } = msg as any;

View file

@ -378,7 +378,7 @@ function pruneChatHistory(
return chatHistory;
}
function messageIsEmpty(message: ChatMessage): boolean {
export function messageIsEmpty(message: ChatMessage): boolean {
if (typeof message.content === "string") {
return message.content.trim() === "";
}