Skip to content

fix(sdk-js): preserve AIMessage metadata in middleware - #6091

Merged
BenTaylorDev merged 4 commits into
CopilotKit:mainfrom
JYbill:fix/6087-preserve-ai-message-metadata
Jul 23, 2026
Merged

BenTaylorDev merged 4 commits into
CopilotKit:mainfrom
JYbill:fix/6087-preserve-ai-message-metadata

Conversation

@JYbill

@JYbill JYbill commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

  • Preserve non-tool AIMessage fields when frontend tool calls are intercepted in afterModel and restored in afterAgent, including additional_kwargs, response_metadata, usage_metadata, invalid_tool_calls, id, and name.
  • Use a shared rebuild helper with tool_calls as the source of truth. For LangChain v1 content blocks, remove stale tool_call and tool_call_chunk blocks before reconstructing the message.
  • Add regression coverage for the reasoning + backend/frontend tool calls -> afterModel -> afterAgent flow, including metadata preservation and v1 content block synchronization.

Related PRs and Issues

Checklist

  • I have read the Contribution Guide
  • Documentation is not required because this is an internal middleware bug fix with no public API changes
  • "Allow edits by maintainers" is checked (lets us help iterate on your PR directly — faster turnaround for everyone)

@pkg-pr-new

pkg-pr-new Bot commented Jul 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

@copilotkit/a2ui-renderer

pnpm add https://pkg.pr.new/@copilotkit/a2ui-renderer@6091

@copilotkit/agentcore-runner

pnpm add https://pkg.pr.new/@copilotkit/agentcore-runner@6091

@copilotkit/angular

pnpm add https://pkg.pr.new/@copilotkit/angular@6091

@copilotkit/channels

pnpm add https://pkg.pr.new/@copilotkit/channels@6091

@copilotkit/channels-core

pnpm add https://pkg.pr.new/@copilotkit/channels-core@6091

@copilotkit/channels-discord

pnpm add https://pkg.pr.new/@copilotkit/channels-discord@6091

@copilotkit/channels-intelligence

pnpm add https://pkg.pr.new/@copilotkit/channels-intelligence@6091

@copilotkit/channels-slack

pnpm add https://pkg.pr.new/@copilotkit/channels-slack@6091

@copilotkit/channels-teams

pnpm add https://pkg.pr.new/@copilotkit/channels-teams@6091

@copilotkit/channels-telegram

pnpm add https://pkg.pr.new/@copilotkit/channels-telegram@6091

@copilotkit/channels-ui

pnpm add https://pkg.pr.new/@copilotkit/channels-ui@6091

@copilotkit/channels-whatsapp

pnpm add https://pkg.pr.new/@copilotkit/channels-whatsapp@6091

@copilotkit/core

pnpm add https://pkg.pr.new/@copilotkit/core@6091

@copilotkit/react-core

pnpm add https://pkg.pr.new/@copilotkit/react-core@6091

@copilotkit/react-native

pnpm add https://pkg.pr.new/@copilotkit/react-native@6091

@copilotkit/react-textarea

pnpm add https://pkg.pr.new/@copilotkit/react-textarea@6091

@copilotkit/react-ui

pnpm add https://pkg.pr.new/@copilotkit/react-ui@6091

@copilotkit/runtime

pnpm add https://pkg.pr.new/@copilotkit/runtime@6091

@copilotkit/runtime-client-gql

pnpm add https://pkg.pr.new/@copilotkit/runtime-client-gql@6091

@copilotkit/sdk-js

pnpm add https://pkg.pr.new/@copilotkit/sdk-js@6091

@copilotkit/shared

pnpm add https://pkg.pr.new/@copilotkit/shared@6091

@copilotkit/sqlite-runner

pnpm add https://pkg.pr.new/@copilotkit/sqlite-runner@6091

@copilotkit/voice

pnpm add https://pkg.pr.new/@copilotkit/voice@6091

@copilotkit/vue

pnpm add https://pkg.pr.new/@copilotkit/vue@6091

@copilotkit/web-components

pnpm add https://pkg.pr.new/@copilotkit/web-components@6091

@copilotkit/web-inspector

pnpm add https://pkg.pr.new/@copilotkit/web-inspector@6091

commit: d5186bf

@BenTaylorDev BenTaylorDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Reviewed adversarially against current main and the actual installed @langchain/[email protected]:

  • The fix is mechanically correct where it counts. For v1 messages, langchain's AIMessage constructor re-derives tool_calls from any tool_call blocks left in content (ai.js:62-64) — so without the new usesV1ContentBlocks filter, the intercepted frontend tool call would leak straight back into tool_calls. The output_version === "v1" guard is the same condition langchain itself uses (ai.js:51), so there's no detection gap and non-v1 messages are correctly left untouched.
  • Complete: the only two metadata-dropping reconstruction sites (middleware.ts:490/544) are both routed through the shared helper; the two utils.ts constructions synthesize fresh interrupt messages with no source metadata to preserve, correctly out of scope.
  • Clean scope, no drift, not superseded, and a thorough test covering metadata + content-block preservation through both interception and restoration.

CI note: sdk-js build + tests pass (middleware suite 47/47). The lone red on unit (20.x) is the unrelated "Verify packed Runtime managed Channels contract" packed-install gate, not attributable to this change (the PR doesn't touch runtime).

I bless the code with a glad heart.

@BenTaylorDev
BenTaylorDev merged commit b038115 into CopilotKit:main Jul 23, 2026
34 of 35 checks passed
@JYbill
JYbill deleted the fix/6087-preserve-ai-message-metadata branch July 30, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: JS LangGraph middleware drops AIMessage metadata for frontend tool calls

2 participants