Skip to content

Commit 67fa09c

Browse files
giulio-leonegiulio-leoneAlemTuzlak
authored
fix(client): preserve reasoning messages after MESSAGES_SNAPSHOT (#1370)
Reasoning messages (role: 'reasoning') are emitted during streaming via REASONING_MESSAGE_START/CONTENT events but are not included in the final MESSAGES_SNAPSHOT (reasoning is metadata on AI chunks, not a LangChain message type). The snapshot filter in default.ts only exempts 'activity' role from the snapshot-based filter, so reasoning messages are dropped when the snapshot arrives — they disappear from agent.messages after the run. Fix: Extract a helper isClientOnlyRole() that exempts both 'activity' and 'reasoning' roles from snapshot filtering, preserving reasoning messages across the snapshot merge. Closes #1262 Co-authored-by: giulio-leone <giulio.leone@users.noreply.github.com> Co-authored-by: Alem Tuzlak <t.zlak97@gmail.com>
1 parent 74fd5ba commit 67fa09c

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

  • sdks/typescript/packages/client/src/apply

sdks/typescript/packages/client/src/apply/default.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,14 +540,18 @@ export const defaultApplyEvents = (
540540
const { messages: newMessages } = event as MessagesSnapshotEvent;
541541

542542
// Edit-based merge: update existing messages with snapshot data while
543-
// preserving activity messages (which the backend doesn't know about).
543+
// preserving activity and reasoning messages (which the backend
544+
// doesn't include in the snapshot).
544545
const snapshotMap = new Map(newMessages.map((m) => [m.id, m]));
545546

546-
// Step 1 + 2: Keep activity messages as-is, keep messages present in
547-
// the snapshot (replaced with snapshot version), drop everything else.
547+
// Step 1 + 2: Keep activity/reasoning messages as-is, keep messages
548+
// present in the snapshot (replaced with snapshot version), drop
549+
// everything else.
550+
const isClientOnlyRole = (role: string) =>
551+
role === "activity" || role === "reasoning";
548552
messages = messages
549-
.filter((m) => m.role === "activity" || snapshotMap.has(m.id))
550-
.map((m) => (m.role === "activity" ? m : snapshotMap.get(m.id)!));
553+
.filter((m) => isClientOnlyRole(m.role) || snapshotMap.has(m.id))
554+
.map((m) => (isClientOnlyRole(m.role) ? m : snapshotMap.get(m.id)!));
551555

552556
// Step 3: Append messages from the snapshot that we don't have yet.
553557
const existingIds = new Set(messages.map((m) => m.id));

0 commit comments

Comments
 (0)