[v6.0] fix(langchain): register key mapping for tool calls emitted via messages mode for HITL interrupt matching#16790
Merged
Merged
Conversation
…ges mode for HITL interrupt matching (#15793) ## Background When using `@ai-sdk/langchain` with `streamMode: ["messages", "values"]` for LangGraph HITL (Human-in-the-Loop) workflows, the `__interrupt__` handler fails to match tool calls that were already emitted via the `messages` stream mode. In `processLangGraphEvent`, tool calls emitted through `messages` events are added to `emittedToolCalls` (Set) but **not** to `emittedToolCallsByKey` (Map). When the subsequent `values` event arrives with the same tool call, the values mode tool call loop skips it because `emittedToolCalls.has(toolCall.id)` is already true — but this also skips the `emittedToolCallsByKey.set()` registration. When the `__interrupt__` handler then tries `emittedToolCallsByKey.get(toolCallKey)`, it fails and falls back to generating a new ID like `hitl-delete_file-1234567890`. This causes: 1. `tool-approval-request` gets a mismatched `toolCallId` (orphaned from the original tool call card in the UI) 2. Duplicate `tool-input-start` / `tool-input-available` events are emitted for the same tool call ## Summary Added an `else if` branch in the values mode tool call loop to register the key mapping for tool calls that were already emitted via messages mode: ```typescript } else if (toolCall.id && emittedToolCalls.has(toolCall.id)) { // Register key mapping for tool calls already emitted via messages mode // so that __interrupt__ handling can match them by key const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`; emittedToolCallsByKey.set(toolCallKey, toolCall.id); } ``` This is consistent with the existing key mapping pattern at L1292 (values finalization block). **Changes:** - `packages/langchain/src/utils.ts`: 5-line else-if branch addition - `packages/langchain/src/adapter.test.ts`: 2 new test cases for the dual streamMode scenario - Updated existing HITL snapshot to reflect correct tool call ID matching ## Manual Verification Verified in a production application using `createReactAgent` + `interrupt()` + `streamMode: ["messages", "values"]`: - Before fix: `tool-approval-request` gets fallback ID → orphaned approval card in UI - After fix: `tool-approval-request` correctly references the original `toolCallId` → approval card connects to the existing tool call card The existing HITL fixture test (`LANGGRAPH_RESPONSE_1`) also demonstrates the fix — the snapshot previously contained the fallback ID `hitl-delete_file-1234567890`, now correctly shows the original `call_LOd3dMxgYxmNLWZVWXra9xWQ`. ## Related Issues * Fixes #12797 * Builds on and superseeds #12798 Co-authored-by: domuk-k [dannyworks102@gmail.com](mailto:dannyworks102@gmail.com) (cherry picked from commit 6e23bf2)
Contributor
|
|
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.
Backport of #15793 to
release-v6.0(clean cherry-pick). RegistersemittedToolCallsByKeyfor tool calls already emitted via messages mode, fixing LangGraph HITL interrupt matching (orphaned approval IDs, duplicate events).Part of the v6.0 backport tracking issue #16767.