Skip to content

Commit 4428da7

Browse files
committed
Memory in stream events method - crucial
1 parent 7450783 commit 4428da7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/agents/mcp_agent.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ export class MCPAgent {
699699
let success = false
700700
let eventCount = 0
701701
let totalResponseLength = 0
702+
let finalResponse = ''
702703

703704
try {
704705
// Initialize if needed
@@ -763,18 +764,23 @@ export class MCPAgent {
763764

764765
yield event
765766

766-
// Handle final message for history
767+
// Capture final response from chain end event
767768
if (event.event === 'on_chain_end' && event.data?.output) {
768769
const output = event.data.output
769-
if (typeof output === 'string' && this.memoryEnabled) {
770-
this.addToHistory(new AIMessage(output))
770+
if (typeof output === 'string') {
771+
finalResponse = output
771772
}
772-
else if (output?.output && typeof output.output === 'string' && this.memoryEnabled) {
773-
this.addToHistory(new AIMessage(output.output))
773+
else if (output?.output && typeof output.output === 'string') {
774+
finalResponse = output.output
774775
}
775776
}
776777
}
777778

779+
// Add the final AI response to conversation history if memory is enabled
780+
if (this.memoryEnabled && finalResponse) {
781+
this.addToHistory(new AIMessage(finalResponse))
782+
}
783+
778784
logger.info(`🎉 StreamEvents complete - ${eventCount} events emitted`)
779785
success = true
780786
}

0 commit comments

Comments
 (0)