Repository navigation
Description
Describe the bug
ChatClient.sendMessage accepts a per-call body as its second argument, documented as "Optional body parameters to merge with the client's base body for this request. Uses shallow merge with per-message body taking priority."
Every framework useChat hook hard-codes that argument to undefined, so per-send body is unreachable from the hooks. packages/ai-preact/src/use-chat.ts on main:
const sendMessage = useCallback(
async (
content: string | MultimodalContent,
sendOptions?: SendMessageOptions,
) => {
try {
await client.sendMessage(content, undefined, sendOptions)Identical in packages/ai-react/src/use-chat.ts and packages/ai-vue/src/use-chat.ts:
client.sendMessage(content, undefined, sendOptions)The hooks' public type stops at (content, sendOptions?), and UseChatReturn does not expose the underlying client, so there is no supported way to reach the parameter.
Why this looks unintended rather than a deliberate narrowing
use-mcp-app-bridge.ts — in the same package — forwards it:
chat: { sendMessage: (content, body) => chatRef.current.sendMessage(content, body) },So within @tanstack/ai-preact one hook can attach per-send body and the other cannot. ChatClient already implements the merge (mergedBody = {...bodyOption, ...forwardedPropsOption, ...pendingMessageBody}), so the hooks are pure passthroughs that drop an argument.
Why it matters
Per-send body is the natural channel for per-turn application data that the model must not see — the thing your own docs guarantee for metadata ("Adapters never forward this field onto the provider wire request"). Without it, the options from a hook are:
forwardedPropson theuseChatconfig — but that is per-render, not per-send. Computing a value inside a submit handler and callingsendMessagein the same tick races the commit, becauseuseChatre-appliesforwardedPropsfrom an effect keyed on identity.- Mutating a stable
forwardedPropsref just beforesendMessage— works, but depends onupdateOptionsstoring the reference without copying. An implementation detail. - Putting the data in the message text — which is what we did, and it cost us a production incident: a model read an instruction meant for our server, emitted a tool call for a tool that did not exist as literal text, and the turn died. The text is persisted, so it replayed on every subsequent turn of that session.
Option 3 is the one people reach for when 1 and 2 look fragile, and it is the worst one.
Expected behaviour
The hooks forward the argument, matching ChatClient and use-mcp-app-bridge:
const sendMessage = useCallback(
async (
content: string | MultimodalContent,
body?: Record<string, unknown>,
sendOptions?: SendMessageOptions,
) => {
await client.sendMessage(content, body, sendOptions)with UseChatReturn['sendMessage'] widened to match.
Ordering is the one design question: ChatClient has body second and sendOptions third, but the hooks currently expose sendOptions second, so a straight passthrough is a breaking change for anyone passing sendOptions positionally. If that is unacceptable, sendOptions.body would be non-breaking, at the cost of diverging from ChatClient.
Happy to open the PR either way — just say which shape you prefer.
Versions
@tanstack/ai-preact0.13.2 (also verified onmain)@tanstack/ai-client0.26.0@tanstack/ai0.48.0- Reproduced originally on
@tanstack/ai-preact0.10.1 /ai-client0.19.1
Affected packages
ai-preact, ai-react, ai-vue (ai-solid and ai-svelte have no use-chat.ts at that path — not checked further).
Activity