Skip to content

Commit 62e6281

Browse files
committed
fix: do not store errors in chat history
1 parent 9d7df70 commit 62e6281

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

packages/agent-api/src/functions/chats-post.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { AzureCosmsosDBNoSQLChatMessageHistory } from '@langchain/azure-cosmosdb
77
import { createReactAgent } from "@langchain/langgraph/prebuilt";
88
import { AIMessage, HumanMessage } from '@langchain/core/messages';
99
import { loadMcpTools } from '@langchain/mcp-adapters';
10-
import { StreamEvent } from '@langchain/core/tracers/log_stream';
10+
import { StreamEvent } from '@langchain/core/tracers/log_stream.js';
1111
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
12-
import { Client } from '@modelcontextprotocol/sdk/client/index';
12+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
1313
import { getAzureOpenAiTokenProvider, getCredentials, getInternalUserId } from '../auth.js';
1414

1515
const agentSystemPrompt = `## Role
@@ -134,11 +134,14 @@ export async function postChats(request: HttpRequest, context: InvocationContext
134134
// Update chat history when the response is complete
135135
const onResponseComplete = async (content: string) => {
136136
try {
137-
await chatHistory.addMessages([
138-
new HumanMessage(question),
139-
new AIMessage(content),
140-
]);
141-
context.log('Chat history updated successfully');
137+
if (content) {
138+
// When no content is generated, do not update the history as it's likely an error
139+
await chatHistory.addMessages([
140+
new HumanMessage(question),
141+
new AIMessage(content),
142+
]);
143+
context.log('Chat history updated successfully');
144+
}
142145
} catch (error) {
143146
context.error('Error updating chat history:', error);
144147
}

0 commit comments

Comments
 (0)