@cloudflare/ai-chat 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +225 -459
  3. package/dist/ai-chat-v5-migration.d.ts +9 -9
  4. package/dist/ai-chat-v5-migration.js +25 -4
  5. package/dist/ai-chat-v5-migration.js.map +1 -1
  6. package/dist/experimental/forever.d.ts +29 -0
  7. package/dist/experimental/forever.js +55 -0
  8. package/dist/experimental/forever.js.map +1 -0
  9. package/dist/index.d.ts +126 -74
  10. package/dist/index.js +753 -632
  11. package/dist/index.js.map +1 -1
  12. package/dist/message-builder-Bwc_Umye.js +231 -0
  13. package/dist/message-builder-Bwc_Umye.js.map +1 -0
  14. package/dist/react.d.ts +27 -9
  15. package/dist/react.js +248 -259
  16. package/dist/react.js.map +1 -1
  17. package/dist/resumable-stream-DqXxQVjf.d.ts +103 -0
  18. package/dist/types.d.ts +16 -4
  19. package/dist/types.js +2 -0
  20. package/dist/types.js.map +1 -1
  21. package/e2e/chat.spec.ts +1703 -0
  22. package/e2e/client-tools.spec.ts +504 -0
  23. package/e2e/llm.spec.ts +637 -0
  24. package/e2e/playwright.config.ts +24 -0
  25. package/e2e/worker.ts +176 -0
  26. package/e2e/wrangler.jsonc +23 -0
  27. package/package.json +12 -5
  28. package/scripts/build.ts +2 -1
  29. package/src/ai-chat-v5-migration.ts +32 -5
  30. package/src/experimental/forever.ts +66 -0
  31. package/src/index.ts +850 -1132
  32. package/src/message-builder.ts +384 -0
  33. package/src/react-tests/use-agent-chat.test.tsx +738 -2
  34. package/src/react.tsx +396 -462
  35. package/src/resumable-stream.ts +441 -0
  36. package/src/tests/chat-context.test.ts +1 -1
  37. package/src/tests/chat-persistence.test.ts +5 -5
  38. package/src/tests/clear-history.test.ts +132 -0
  39. package/src/tests/client-tool-duplicate-message.test.ts +457 -14
  40. package/src/tests/client-tools-broadcast.test.ts +2 -2
  41. package/src/tests/client-tools-continuation.test.ts +314 -0
  42. package/src/tests/client-tools-reconnect.test.ts +143 -0
  43. package/src/tests/custom-body-continuation.test.ts +309 -0
  44. package/src/tests/custom-body.test.ts +186 -0
  45. package/src/tests/destroy.test.ts +57 -0
  46. package/src/tests/get-messages-endpoint.test.ts +79 -0
  47. package/src/tests/max-persisted-messages.test.ts +201 -0
  48. package/src/tests/merge-server-state.test.ts +106 -0
  49. package/src/tests/message-builder.test.ts +808 -0
  50. package/src/tests/migration.test.ts +282 -0
  51. package/src/tests/non-sse-response.test.ts +2 -2
  52. package/src/tests/onfinish-cleanup.test.ts +362 -0
  53. package/src/tests/plaintext-response.test.ts +87 -0
  54. package/src/tests/resumable-streaming.test.ts +220 -14
  55. package/src/tests/row-size-guard.test.ts +374 -0
  56. package/src/tests/sanitize-messages.test.ts +215 -0
  57. package/src/tests/validate-messages.test.ts +269 -0
  58. package/src/tests/worker.ts +85 -23
  59. package/src/types.ts +17 -1
  60. package/src/ws-chat-transport.ts +203 -0
@@ -0,0 +1,231 @@
1
+ //#region src/message-builder.ts
2
+ /**
3
+ * Applies a stream chunk to a mutable parts array, building up the message
4
+ * incrementally. Returns true if the chunk was handled, false if it was
5
+ * an unrecognized type (caller may handle it with additional logic).
6
+ *
7
+ * Handles all common chunk types that both server and client need:
8
+ * - text-start / text-delta / text-end
9
+ * - reasoning-start / reasoning-delta / reasoning-end
10
+ * - file
11
+ * - source-url / source-document
12
+ * - tool-input-start / tool-input-delta / tool-input-available / tool-input-error
13
+ * - tool-output-available / tool-output-error
14
+ * - step-start (aliased from start-step)
15
+ * - data-* (developer-defined typed JSON blobs)
16
+ *
17
+ * @param parts - The mutable parts array to update
18
+ * @param chunk - The parsed stream chunk data
19
+ * @returns true if handled, false if the chunk type is not recognized
20
+ */
21
+ function applyChunkToParts(parts, chunk) {
22
+ switch (chunk.type) {
23
+ case "text-start":
24
+ parts.push({
25
+ type: "text",
26
+ text: "",
27
+ state: "streaming"
28
+ });
29
+ return true;
30
+ case "text-delta": {
31
+ const lastTextPart = findLastPartByType(parts, "text");
32
+ if (lastTextPart && lastTextPart.type === "text") lastTextPart.text += chunk.delta ?? "";
33
+ else parts.push({
34
+ type: "text",
35
+ text: chunk.delta ?? "",
36
+ state: "streaming"
37
+ });
38
+ return true;
39
+ }
40
+ case "text-end": {
41
+ const lastTextPart = findLastPartByType(parts, "text");
42
+ if (lastTextPart && "state" in lastTextPart) lastTextPart.state = "done";
43
+ return true;
44
+ }
45
+ case "reasoning-start":
46
+ parts.push({
47
+ type: "reasoning",
48
+ text: "",
49
+ state: "streaming"
50
+ });
51
+ return true;
52
+ case "reasoning-delta": {
53
+ const lastReasoningPart = findLastPartByType(parts, "reasoning");
54
+ if (lastReasoningPart && lastReasoningPart.type === "reasoning") lastReasoningPart.text += chunk.delta ?? "";
55
+ else parts.push({
56
+ type: "reasoning",
57
+ text: chunk.delta ?? "",
58
+ state: "streaming"
59
+ });
60
+ return true;
61
+ }
62
+ case "reasoning-end": {
63
+ const lastReasoningPart = findLastPartByType(parts, "reasoning");
64
+ if (lastReasoningPart && "state" in lastReasoningPart) lastReasoningPart.state = "done";
65
+ return true;
66
+ }
67
+ case "file":
68
+ parts.push({
69
+ type: "file",
70
+ mediaType: chunk.mediaType,
71
+ url: chunk.url
72
+ });
73
+ return true;
74
+ case "source-url":
75
+ parts.push({
76
+ type: "source-url",
77
+ sourceId: chunk.sourceId,
78
+ url: chunk.url,
79
+ title: chunk.title,
80
+ providerMetadata: chunk.providerMetadata
81
+ });
82
+ return true;
83
+ case "source-document":
84
+ parts.push({
85
+ type: "source-document",
86
+ sourceId: chunk.sourceId,
87
+ mediaType: chunk.mediaType,
88
+ title: chunk.title,
89
+ filename: chunk.filename,
90
+ providerMetadata: chunk.providerMetadata
91
+ });
92
+ return true;
93
+ case "tool-input-start":
94
+ parts.push({
95
+ type: `tool-${chunk.toolName}`,
96
+ toolCallId: chunk.toolCallId,
97
+ toolName: chunk.toolName,
98
+ state: "input-streaming",
99
+ input: void 0
100
+ });
101
+ return true;
102
+ case "tool-input-delta": {
103
+ const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
104
+ if (toolPart) toolPart.input = chunk.input;
105
+ return true;
106
+ }
107
+ case "tool-input-available": {
108
+ const existing = findToolPartByCallId(parts, chunk.toolCallId);
109
+ if (existing) {
110
+ const p = existing;
111
+ p.state = "input-available";
112
+ p.input = chunk.input;
113
+ } else parts.push({
114
+ type: `tool-${chunk.toolName}`,
115
+ toolCallId: chunk.toolCallId,
116
+ toolName: chunk.toolName,
117
+ state: "input-available",
118
+ input: chunk.input
119
+ });
120
+ return true;
121
+ }
122
+ case "tool-input-error": {
123
+ const existing = findToolPartByCallId(parts, chunk.toolCallId);
124
+ if (existing) {
125
+ const p = existing;
126
+ p.state = "output-error";
127
+ p.errorText = chunk.errorText;
128
+ p.input = chunk.input;
129
+ } else parts.push({
130
+ type: `tool-${chunk.toolName}`,
131
+ toolCallId: chunk.toolCallId,
132
+ toolName: chunk.toolName,
133
+ state: "output-error",
134
+ input: chunk.input,
135
+ errorText: chunk.errorText
136
+ });
137
+ return true;
138
+ }
139
+ case "tool-approval-request": {
140
+ const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
141
+ if (toolPart) {
142
+ const p = toolPart;
143
+ p.state = "approval-requested";
144
+ p.approval = { id: chunk.approvalId };
145
+ }
146
+ return true;
147
+ }
148
+ case "tool-output-denied": {
149
+ const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
150
+ if (toolPart) {
151
+ const p = toolPart;
152
+ p.state = "output-denied";
153
+ }
154
+ return true;
155
+ }
156
+ case "tool-output-available": {
157
+ const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
158
+ if (toolPart) {
159
+ const p = toolPart;
160
+ p.state = "output-available";
161
+ p.output = chunk.output;
162
+ if (chunk.preliminary !== void 0) p.preliminary = chunk.preliminary;
163
+ }
164
+ return true;
165
+ }
166
+ case "tool-output-error": {
167
+ const toolPart = findToolPartByCallId(parts, chunk.toolCallId);
168
+ if (toolPart) {
169
+ const p = toolPart;
170
+ p.state = "output-error";
171
+ p.errorText = chunk.errorText;
172
+ }
173
+ return true;
174
+ }
175
+ case "step-start":
176
+ case "start-step":
177
+ parts.push({ type: "step-start" });
178
+ return true;
179
+ default:
180
+ if (chunk.type.startsWith("data-")) {
181
+ if (chunk.transient) return true;
182
+ if (chunk.id != null) {
183
+ const existing = findDataPartByTypeAndId(parts, chunk.type, chunk.id);
184
+ if (existing) {
185
+ existing.data = chunk.data;
186
+ return true;
187
+ }
188
+ }
189
+ parts.push({
190
+ type: chunk.type,
191
+ ...chunk.id != null && { id: chunk.id },
192
+ data: chunk.data
193
+ });
194
+ return true;
195
+ }
196
+ return false;
197
+ }
198
+ }
199
+ /**
200
+ * Finds the last part in the array matching the given type.
201
+ * Searches from the end for efficiency (the part we want is usually recent).
202
+ */
203
+ function findLastPartByType(parts, type) {
204
+ for (let i = parts.length - 1; i >= 0; i--) if (parts[i].type === type) return parts[i];
205
+ }
206
+ /**
207
+ * Finds a tool part by its toolCallId.
208
+ * Searches from the end since the tool part is usually recent.
209
+ */
210
+ function findToolPartByCallId(parts, toolCallId) {
211
+ if (!toolCallId) return void 0;
212
+ for (let i = parts.length - 1; i >= 0; i--) {
213
+ const p = parts[i];
214
+ if ("toolCallId" in p && p.toolCallId === toolCallId) return p;
215
+ }
216
+ }
217
+ /**
218
+ * Finds a data part by its type and id for reconciliation.
219
+ * Data parts use type+id as a composite key so when the same combination
220
+ * is seen again, the existing part's data is updated in-place.
221
+ */
222
+ function findDataPartByTypeAndId(parts, type, id) {
223
+ for (let i = parts.length - 1; i >= 0; i--) {
224
+ const p = parts[i];
225
+ if (p.type === type && "id" in p && p.id === id) return p;
226
+ }
227
+ }
228
+
229
+ //#endregion
230
+ export { applyChunkToParts as t };
231
+ //# sourceMappingURL=message-builder-Bwc_Umye.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message-builder-Bwc_Umye.js","names":[],"sources":["../src/message-builder.ts"],"sourcesContent":["/**\n * Shared message builder for reconstructing UIMessage parts from stream chunks.\n *\n * Used by both the server (_streamSSEReply) and client (onAgentMessage in react.tsx)\n * to avoid duplicating the chunk-type switch/case logic. The server handles additional\n * chunk types (tool-input-start, tool-input-delta, etc.) on top of this shared base.\n *\n * Operates on a mutable parts array for performance (avoids allocating new arrays\n * on every chunk during streaming).\n */\n\nimport type { UIMessage } from \"ai\";\n\n/** The parts array type from UIMessage */\nexport type MessageParts = UIMessage[\"parts\"];\n\n/** A single part from the UIMessage parts array */\nexport type MessagePart = MessageParts[number];\n\n/**\n * Parsed chunk data from an AI SDK stream event.\n * This is the JSON-parsed body of a CF_AGENT_USE_CHAT_RESPONSE message,\n * or the `data:` payload of an SSE line.\n */\nexport type StreamChunkData = {\n type: string;\n id?: string;\n delta?: string;\n text?: string;\n mediaType?: string;\n url?: string;\n sourceId?: string;\n title?: string;\n filename?: string;\n toolCallId?: string;\n toolName?: string;\n input?: unknown;\n inputTextDelta?: string;\n output?: unknown;\n state?: string;\n errorText?: string;\n /** When true, the output is preliminary (may be updated by a later chunk) */\n preliminary?: boolean;\n /** Approval ID for tools with needsApproval */\n approvalId?: string;\n providerMetadata?: Record<string, unknown>;\n /** Payload for data-* parts (developer-defined typed JSON) */\n data?: unknown;\n /** When true, data parts are ephemeral and not persisted to message.parts */\n transient?: boolean;\n [key: string]: unknown;\n};\n\n/**\n * Applies a stream chunk to a mutable parts array, building up the message\n * incrementally. Returns true if the chunk was handled, false if it was\n * an unrecognized type (caller may handle it with additional logic).\n *\n * Handles all common chunk types that both server and client need:\n * - text-start / text-delta / text-end\n * - reasoning-start / reasoning-delta / reasoning-end\n * - file\n * - source-url / source-document\n * - tool-input-start / tool-input-delta / tool-input-available / tool-input-error\n * - tool-output-available / tool-output-error\n * - step-start (aliased from start-step)\n * - data-* (developer-defined typed JSON blobs)\n *\n * @param parts - The mutable parts array to update\n * @param chunk - The parsed stream chunk data\n * @returns true if handled, false if the chunk type is not recognized\n */\nexport function applyChunkToParts(\n parts: MessagePart[],\n chunk: StreamChunkData\n): boolean {\n switch (chunk.type) {\n case \"text-start\": {\n parts.push({\n type: \"text\",\n text: \"\",\n state: \"streaming\"\n } as MessagePart);\n return true;\n }\n\n case \"text-delta\": {\n const lastTextPart = findLastPartByType(parts, \"text\");\n if (lastTextPart && lastTextPart.type === \"text\") {\n (lastTextPart as { text: string }).text += chunk.delta ?? \"\";\n } else {\n // No text-start received — create a new text part (stream resumption fallback)\n parts.push({\n type: \"text\",\n text: chunk.delta ?? \"\",\n state: \"streaming\"\n } as MessagePart);\n }\n return true;\n }\n\n case \"text-end\": {\n const lastTextPart = findLastPartByType(parts, \"text\");\n if (lastTextPart && \"state\" in lastTextPart) {\n (lastTextPart as { state: string }).state = \"done\";\n }\n return true;\n }\n\n case \"reasoning-start\": {\n parts.push({\n type: \"reasoning\",\n text: \"\",\n state: \"streaming\"\n } as MessagePart);\n return true;\n }\n\n case \"reasoning-delta\": {\n const lastReasoningPart = findLastPartByType(parts, \"reasoning\");\n if (lastReasoningPart && lastReasoningPart.type === \"reasoning\") {\n (lastReasoningPart as { text: string }).text += chunk.delta ?? \"\";\n } else {\n // No reasoning-start received — create a new reasoning part (stream resumption fallback)\n parts.push({\n type: \"reasoning\",\n text: chunk.delta ?? \"\",\n state: \"streaming\"\n } as MessagePart);\n }\n return true;\n }\n\n case \"reasoning-end\": {\n const lastReasoningPart = findLastPartByType(parts, \"reasoning\");\n if (lastReasoningPart && \"state\" in lastReasoningPart) {\n (lastReasoningPart as { state: string }).state = \"done\";\n }\n return true;\n }\n\n case \"file\": {\n parts.push({\n type: \"file\",\n mediaType: chunk.mediaType,\n url: chunk.url\n } as MessagePart);\n return true;\n }\n\n case \"source-url\": {\n parts.push({\n type: \"source-url\",\n sourceId: chunk.sourceId,\n url: chunk.url,\n title: chunk.title,\n providerMetadata: chunk.providerMetadata\n } as MessagePart);\n return true;\n }\n\n case \"source-document\": {\n parts.push({\n type: \"source-document\",\n sourceId: chunk.sourceId,\n mediaType: chunk.mediaType,\n title: chunk.title,\n filename: chunk.filename,\n providerMetadata: chunk.providerMetadata\n } as MessagePart);\n return true;\n }\n\n case \"tool-input-start\": {\n // Create a tool part in input-streaming state with no input yet.\n // Cross-tab clients see the tool appear immediately with \"streaming\" indicator.\n parts.push({\n type: `tool-${chunk.toolName}`,\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n state: \"input-streaming\",\n input: undefined\n } as MessagePart);\n return true;\n }\n\n case \"tool-input-delta\": {\n // Update the existing tool part with partial input as it streams in.\n const toolPart = findToolPartByCallId(parts, chunk.toolCallId);\n if (toolPart) {\n (toolPart as Record<string, unknown>).input = chunk.input;\n }\n return true;\n }\n\n case \"tool-input-available\": {\n // Finalize the tool input. If tool-input-start was received, update\n // the existing part; otherwise create a new one (for non-streaming tools).\n const existing = findToolPartByCallId(parts, chunk.toolCallId);\n if (existing) {\n const p = existing as Record<string, unknown>;\n p.state = \"input-available\";\n p.input = chunk.input;\n } else {\n parts.push({\n type: `tool-${chunk.toolName}`,\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n state: \"input-available\",\n input: chunk.input\n } as MessagePart);\n }\n return true;\n }\n\n case \"tool-input-error\": {\n // Tool input parsing failed. Update existing part or create one.\n const existing = findToolPartByCallId(parts, chunk.toolCallId);\n if (existing) {\n const p = existing as Record<string, unknown>;\n p.state = \"output-error\";\n p.errorText = chunk.errorText;\n p.input = chunk.input;\n } else {\n parts.push({\n type: `tool-${chunk.toolName}`,\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n state: \"output-error\",\n input: chunk.input,\n errorText: chunk.errorText\n } as MessagePart);\n }\n return true;\n }\n\n case \"tool-approval-request\": {\n // Tool requires user approval before executing.\n // Transition the tool part to approval-requested state with the approval ID.\n const toolPart = findToolPartByCallId(parts, chunk.toolCallId);\n if (toolPart) {\n const p = toolPart as Record<string, unknown>;\n p.state = \"approval-requested\";\n p.approval = { id: chunk.approvalId };\n }\n return true;\n }\n\n case \"tool-output-denied\": {\n // User rejected the tool approval request.\n const toolPart = findToolPartByCallId(parts, chunk.toolCallId);\n if (toolPart) {\n const p = toolPart as Record<string, unknown>;\n p.state = \"output-denied\";\n }\n return true;\n }\n\n case \"tool-output-available\": {\n // Update existing tool part with output.\n // Supports `preliminary: true` for streaming tool results —\n // the output may be updated by a subsequent chunk.\n const toolPart = findToolPartByCallId(parts, chunk.toolCallId);\n if (toolPart) {\n const p = toolPart as Record<string, unknown>;\n p.state = \"output-available\";\n p.output = chunk.output;\n if (chunk.preliminary !== undefined) {\n p.preliminary = chunk.preliminary;\n }\n }\n return true;\n }\n\n case \"tool-output-error\": {\n // Tool execution failed. Update the existing tool part.\n const toolPart = findToolPartByCallId(parts, chunk.toolCallId);\n if (toolPart) {\n const p = toolPart as Record<string, unknown>;\n p.state = \"output-error\";\n p.errorText = chunk.errorText;\n }\n return true;\n }\n\n // Both \"step-start\" (client convention) and \"start-step\" (server convention)\n case \"step-start\":\n case \"start-step\": {\n parts.push({ type: \"step-start\" } as MessagePart);\n return true;\n }\n\n default: {\n // https://ai-sdk.dev/docs/ai-sdk-ui/streaming-data\n if (chunk.type.startsWith(\"data-\")) {\n // Transient parts are ephemeral — the AI SDK client fires an onData\n // callback instead of adding them to message.parts. On the server we\n // still broadcast them (so connected clients see them in real time)\n // but skip persisting them into the stored message parts.\n if (chunk.transient) {\n return true;\n }\n\n // Reconciliation: if a part with the same type AND id already exists,\n // update its data in-place instead of appending a duplicate.\n if (chunk.id != null) {\n const existing = findDataPartByTypeAndId(parts, chunk.type, chunk.id);\n if (existing) {\n (existing as Record<string, unknown>).data = chunk.data;\n return true;\n }\n }\n\n // Append new data parts to the array directly.\n // Note: `chunk.data` should always be provided — if omitted, the\n // persisted part will have `data: undefined` which JSON.stringify\n // drops, so the part will have no `data` field on reload.\n // The cast is needed because UIMessage[\"parts\"] doesn't include\n // data-* types in its union because they're an open extension point.\n parts.push({\n type: chunk.type,\n ...(chunk.id != null && { id: chunk.id }),\n data: chunk.data\n } as MessagePart);\n return true;\n }\n\n return false;\n }\n }\n}\n\n/**\n * Finds the last part in the array matching the given type.\n * Searches from the end for efficiency (the part we want is usually recent).\n */\nfunction findLastPartByType(\n parts: MessagePart[],\n type: string\n): MessagePart | undefined {\n for (let i = parts.length - 1; i >= 0; i--) {\n if (parts[i].type === type) {\n return parts[i];\n }\n }\n return undefined;\n}\n\n/**\n * Finds a tool part by its toolCallId.\n * Searches from the end since the tool part is usually recent.\n */\nfunction findToolPartByCallId(\n parts: MessagePart[],\n toolCallId: string | undefined\n): MessagePart | undefined {\n if (!toolCallId) return undefined;\n for (let i = parts.length - 1; i >= 0; i--) {\n const p = parts[i];\n if (\"toolCallId\" in p && p.toolCallId === toolCallId) {\n return p;\n }\n }\n return undefined;\n}\n\n/**\n * Finds a data part by its type and id for reconciliation.\n * Data parts use type+id as a composite key so when the same combination\n * is seen again, the existing part's data is updated in-place.\n */\nfunction findDataPartByTypeAndId(\n parts: MessagePart[],\n type: string,\n id: string\n): MessagePart | undefined {\n for (let i = parts.length - 1; i >= 0; i--) {\n const p = parts[i];\n if (p.type === type && \"id\" in p && (p as { id: string }).id === id) {\n return p;\n }\n }\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,kBACd,OACA,OACS;AACT,SAAQ,MAAM,MAAd;EACE,KAAK;AACH,SAAM,KAAK;IACT,MAAM;IACN,MAAM;IACN,OAAO;IACR,CAAgB;AACjB,UAAO;EAGT,KAAK,cAAc;GACjB,MAAM,eAAe,mBAAmB,OAAO,OAAO;AACtD,OAAI,gBAAgB,aAAa,SAAS,OACxC,CAAC,aAAkC,QAAQ,MAAM,SAAS;OAG1D,OAAM,KAAK;IACT,MAAM;IACN,MAAM,MAAM,SAAS;IACrB,OAAO;IACR,CAAgB;AAEnB,UAAO;;EAGT,KAAK,YAAY;GACf,MAAM,eAAe,mBAAmB,OAAO,OAAO;AACtD,OAAI,gBAAgB,WAAW,a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,EAAE,MAAM,cAAc,CAAgB;AACjD,UAAO;EAGT;AAEE,OAAI,MAAM,KAAK,WAAW,QAAQ,EAAE;AAKlC,QAAI,MAAM,UACR,QAAO;AAKT,QAAI,MAAM,MAAM,MAAM;KACpB,MAAM,WAAW,wBAAwB,OAAO,MAAM,MAAM,MAAM,GAAG;AACrE,SAAI,UAAU;AACZ,MAAC,SAAqC,OAAO,MAAM;AACnD,aAAO;;;AAUX,UAAM,KAAK;KACT,MAAM,MAAM;KACZ,GAAI,MAAM,MAAM,QAAQ,EAAE,IAAI,MAAM,IAAI;KACxC,MAAM,MAAM;KACb,CAAgB;AACjB,WAAO;;AAGT,UAAO;;;;;;;AASb,SAAS,mBACP,OACA,MACyB;AACzB,MAAK,IAAI,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG,IACrC,KAAI,MAAM,GAAG,SAAS,KACpB,QAAO,MAAM;;;;;;AAUnB,SAAS,qBACP,OACA,YACyB;AACzB,KAAI,CAAC,WAAY,QAAO;AACxB,MAAK,IAAI,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;EAC1C,MAAM,IAAI,MAAM;AAChB,MAAI,gBAAgB,KAAK,EAAE,eAAe,WACxC,QAAO;;;;;;;;AAWb,SAAS,wBACP,OACA,MACA,IACyB;AACzB,MAAK,IAAI,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;EAC1C,MAAM,IAAI,MAAM;AAChB,MAAI,EAAE,SAAS,QAAQ,QAAQ,KAAM,EAAqB,OAAO,GAC/D,QAAO"}
package/dist/react.d.ts CHANGED
@@ -6,7 +6,7 @@ import { useAgent } from "agents/react";
6
6
  /**
7
7
  * JSON Schema type for tool parameters.
8
8
  * Re-exported from the AI SDK for convenience.
9
- * @deprecated Import JSONSchema7 directly from "ai" instead.
9
+ * @deprecated Import JSONSchema7 directly from "ai" instead. Will be removed in the next major version.
10
10
  */
11
11
  type JSONSchemaType = JSONSchema7;
12
12
  /**
@@ -113,7 +113,7 @@ type OnToolCallCallback = (options: {
113
113
  type UseAgentChatOptions<
114
114
  State,
115
115
  ChatMessage extends UIMessage = UIMessage
116
- > = Omit<UseChatParams<ChatMessage>, "fetch"> & {
116
+ > = Omit<UseChatParams<ChatMessage>, "fetch" | "onToolCall"> & {
117
117
  /** Agent connection from useAgent */ agent: ReturnType<
118
118
  typeof useAgent<State>
119
119
  >;
@@ -168,15 +168,15 @@ type UseAgentChatOptions<
168
168
  */
169
169
  toolsRequiringConfirmation?: string[];
170
170
  /**
171
- * When true, the server automatically continues the conversation after
172
- * receiving client-side tool results, similar to how server-executed tools
173
- * work with maxSteps in streamText. The continuation is merged into the
174
- * same assistant message.
171
+ * When true (default), the server automatically continues the conversation
172
+ * after receiving client-side tool results or approvals, similar to how
173
+ * server-executed tools work with maxSteps in streamText. The continuation
174
+ * is merged into the same assistant message.
175
175
  *
176
- * When false (default), the client calls sendMessage() after tool results
176
+ * When false, the client must call sendMessage() after tool results
177
177
  * to continue the conversation, which creates a new assistant message.
178
178
  *
179
- * @default false
179
+ * @default true
180
180
  */
181
181
  autoContinueAfterToolResult?: boolean;
182
182
  /**
@@ -196,9 +196,27 @@ type UseAgentChatOptions<
196
196
  * @default true
197
197
  */
198
198
  resume?: boolean;
199
+ /**
200
+ * Custom data to include in every chat request body.
201
+ * Accepts a static object or a function that returns one (for dynamic values).
202
+ * These fields are available in `onChatMessage` via `options.body`.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * // Static
207
+ * body: { timezone: "America/New_York", userId: "abc" }
208
+ *
209
+ * // Dynamic (called on each send)
210
+ * body: () => ({ token: getAuthToken(), timestamp: Date.now() })
211
+ * ```
212
+ */
213
+ body?:
214
+ | Record<string, unknown>
215
+ | (() => Record<string, unknown> | Promise<Record<string, unknown>>);
199
216
  /**
200
217
  * Callback to customize the request before sending messages.
201
- * Use this for advanced scenarios like adding custom headers or dynamic context.
218
+ * For most cases, use the `body` option instead.
219
+ * Use this for advanced scenarios that need access to the messages or trigger type.
202
220
  *
203
221
  * Note: Client tool schemas are automatically sent when tools have `execute` functions.
204
222
  * This callback can add additional data alongside the auto-extracted schemas.