fix: empty assistant message

This commit is contained in:
Nate 2025-07-13 13:49:50 -07:00
parent 96ed948b9d
commit 5e773f5268
2 changed files with 12 additions and 6 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@ node_modules
dist dist
.env .env
/*-repomix.xml /*-repomix.xml
chat.log

View file

@ -1,3 +1,4 @@
import { ContinueClient } from "@continuedev/sdk";
import chalk from "chalk"; import chalk from "chalk";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import type { import type {
@ -8,7 +9,6 @@ import type {
import { MCPService } from "./mcp.js"; import { MCPService } from "./mcp.js";
import { executeToolCall } from "./tools.js"; import { executeToolCall } from "./tools.js";
import { BUILTIN_TOOLS } from "./tools/index.js"; import { BUILTIN_TOOLS } from "./tools/index.js";
import { ContinueClient } from "@continuedev/sdk";
dotenv.config(); dotenv.config();
@ -67,6 +67,10 @@ export async function streamChatResponse(
let stream; let stream;
try { try {
// fs.appendFileSync(
// "chat.log",
// "---\n\n" + JSON.stringify(chatHistory, null, 2) + "\n\n"
// );
stream = await client.chat.completions.create({ stream = await client.chat.completions.create({
model: assistant.getModel(), model: assistant.getModel(),
messages: chatHistory, messages: chatHistory,
@ -150,7 +154,7 @@ export async function streamChatResponse(
console.info(); // Add a newline after the response console.info(); // Add a newline after the response
// Add the tool calls to the chat history // Add the assistant's response to chat history if there's content or tool calls
if (currentToolCalls.length > 0) { if (currentToolCalls.length > 0) {
const toolCalls: ChatCompletionMessageToolCall[] = currentToolCalls.map( const toolCalls: ChatCompletionMessageToolCall[] = currentToolCalls.map(
(tc) => ({ (tc) => ({
@ -164,9 +168,12 @@ export async function streamChatResponse(
); );
chatHistory.push({ chatHistory.push({
role: "assistant", role: "assistant",
content: "", content: aiResponse,
tool_calls: toolCalls, tool_calls: toolCalls,
}); });
} else if (aiResponse.trim()) {
// Only add assistant response if there's actual content
chatHistory.push({ role: "assistant", content: aiResponse });
} }
// If we have tool calls, execute them // If we have tool calls, execute them
@ -210,9 +217,6 @@ export async function streamChatResponse(
// No more tool calls, end the conversation // No more tool calls, end the conversation
shouldContinueConversation = false; shouldContinueConversation = false;
} }
// Add the assistant's response to chat history before any tool calls
chatHistory.push({ role: "assistant", content: aiResponse });
} }
return aiResponse; return aiResponse;