fix(client): don't stream text into an activity message with the same id#2201
Open
TheSeydiCharyyev wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #1350.
When a text message arrives under an id an activity message already holds, the
TEXT_MESSAGE_*handlers match on id alone and write into the activity message. Its structuredcontentobject is replaced with the streamed string, so the message ends up asrole: "activity"withcontent: "Hello, world!"— no longer a validActivityMessage— and the assistant's reply is never created as its own message. Reproduced on main with the repro from the issue:Message ids are unique across a conversation — every lookup in this file is a bare
findon id — so an activity message under a text message's id means the producer reused the id. The client cannot guess the intent, and it should not silently corrupt the message it already holds.The guard already exists in this file for the same situation.
ACTIVITY_DELTAchecks the role, warns, and returns when it lands on a non-activity message;resolveOrCreateAssistantMessagedoes the same forTOOL_CALL_START, warning and falling back rather than writing into whatever it found. TheTEXT_MESSAGE_*handlers are the ones missing it. This PR adds the same check to them.Note:
ACTIVITY_SNAPSHOTis left alone. Itsreplacebranch overwrites a non-activity message under the same id, which is intentional and covered by "replaces non-activity message when replace is true" and "does not alter non-activity message when replace is false". My first comment on the issue called the bug symmetric — that was wrong, and the correction is here.Changes
sdks/typescript/packages/client/src/apply/default.ts—TEXT_MESSAGE_START,TEXT_MESSAGE_CONTENTandTEXT_MESSAGE_ENDwarn and skip when the id belongs to an activity message, instead of creating/appending/announcing over it.sdks/typescript/packages/client/src/apply/__tests__/default.activity.test.ts— newTEXT_MESSAGE_* against an activity message's idblock: the activity message survives untouched, each of the three handlers warns, and text still streams normally when the id is free.docs/concepts/events.mdx— states that an activitymessageIdmust not be reused by a text message and vice versa, and what a client should do if it is.Verification
Windows 11, Node 22.17.1, pnpm 10.33.4:
contentobject is replaced by the text string and no assistant message exists{message: "Generating plan..."}, the colliding text is dropped with a warning from each handler, and an uncolliding text message still streams into its own assistant messagedefault.activity.test.ts— 30 tests pass (27 existing, unchanged, plus 3 new)@ag-ui/clientsuite is unaffectedThe pre-existing failure of
esm-interop.test.tson Windows is unrelated and is fixed separately in #2183.