@@ -7,9 +7,9 @@ import { AzureCosmsosDBNoSQLChatMessageHistory } from '@langchain/azure-cosmosdb
77import { createReactAgent } from "@langchain/langgraph/prebuilt" ;
88import { AIMessage , HumanMessage } from '@langchain/core/messages' ;
99import { loadMcpTools } from '@langchain/mcp-adapters' ;
10- import { StreamEvent } from '@langchain/core/tracers/log_stream' ;
10+ import { StreamEvent } from '@langchain/core/tracers/log_stream.js ' ;
1111import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js' ;
12- import { Client } from '@modelcontextprotocol/sdk/client/index' ;
12+ import { Client } from '@modelcontextprotocol/sdk/client/index.js ' ;
1313import { getAzureOpenAiTokenProvider , getCredentials , getInternalUserId } from '../auth.js' ;
1414
1515const 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