@assistant-ui/react-ag-ui 0.0.31 → 0.0.32

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 (37) hide show
  1. package/dist/index.d.ts +3 -4
  2. package/dist/index.js +2 -2
  3. package/dist/runtime/AgUiThreadRuntimeCore.d.ts +85 -82
  4. package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
  5. package/dist/runtime/AgUiThreadRuntimeCore.js +542 -628
  6. package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
  7. package/dist/runtime/adapter/conversions.d.ts +39 -36
  8. package/dist/runtime/adapter/conversions.d.ts.map +1 -1
  9. package/dist/runtime/adapter/conversions.js +294 -390
  10. package/dist/runtime/adapter/conversions.js.map +1 -1
  11. package/dist/runtime/adapter/run-aggregator.d.ts +54 -51
  12. package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
  13. package/dist/runtime/adapter/run-aggregator.js +367 -413
  14. package/dist/runtime/adapter/run-aggregator.js.map +1 -1
  15. package/dist/runtime/adapter/subscriber.d.ts +94 -91
  16. package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
  17. package/dist/runtime/adapter/subscriber.js +77 -86
  18. package/dist/runtime/adapter/subscriber.js.map +1 -1
  19. package/dist/runtime/event-parser.d.ts +9 -5
  20. package/dist/runtime/event-parser.d.ts.map +1 -1
  21. package/dist/runtime/event-parser.js +172 -177
  22. package/dist/runtime/event-parser.js.map +1 -1
  23. package/dist/runtime/logger.d.ts +7 -4
  24. package/dist/runtime/logger.d.ts.map +1 -1
  25. package/dist/runtime/logger.js +7 -3
  26. package/dist/runtime/logger.js.map +1 -1
  27. package/dist/runtime/types.d.ts +121 -117
  28. package/dist/runtime/types.d.ts.map +1 -1
  29. package/dist/runtime/types.js +0 -2
  30. package/dist/useAgUiRuntime.d.ts +10 -6
  31. package/dist/useAgUiRuntime.d.ts.map +1 -1
  32. package/dist/useAgUiRuntime.js +136 -142
  33. package/dist/useAgUiRuntime.js.map +1 -1
  34. package/package.json +7 -7
  35. package/dist/index.d.ts.map +0 -1
  36. package/dist/index.js.map +0 -1
  37. package/dist/runtime/types.js.map +0 -1
@@ -1,427 +1,331 @@
1
1
  "use client";
2
2
  import { toToolsJSONSchema } from "assistant-stream";
3
+ //#region src/runtime/adapter/conversions.ts
3
4
  const isObject = (value) => typeof value === "object" && value !== null;
4
5
  const getString = (record, key) => {
5
- const value = record[key];
6
- return typeof value === "string" ? value : undefined;
6
+ const value = record[key];
7
+ return typeof value === "string" ? value : void 0;
7
8
  };
8
9
  const getToolCallId = (record) => getString(record, "toolCallId") ?? getString(record, "tool_call_id");
9
10
  function parseJSONText(value) {
10
- if (!value)
11
- return value;
12
- try {
13
- return JSON.parse(value);
14
- }
15
- catch {
16
- return value;
17
- }
11
+ if (!value) return value;
12
+ try {
13
+ return JSON.parse(value);
14
+ } catch {
15
+ return value;
16
+ }
18
17
  }
19
18
  function generateId() {
20
- return (globalThis.crypto?.randomUUID?.() ??
21
- Math.random().toString(36).slice(2));
19
+ return globalThis.crypto?.randomUUID?.() ?? Math.random().toString(36).slice(2);
22
20
  }
23
21
  function normalizeToolCall(part) {
24
- const id = part.toolCallId ?? generateId();
25
- const argsText = typeof part.argsText === "string"
26
- ? part.argsText
27
- : JSON.stringify(part.args ?? {});
28
- return {
29
- id,
30
- call: {
31
- id,
32
- type: "function",
33
- function: {
34
- name: part.toolName ?? "tool",
35
- arguments: argsText,
36
- },
37
- },
38
- };
22
+ const id = part.toolCallId ?? generateId();
23
+ const argsText = typeof part.argsText === "string" ? part.argsText : JSON.stringify(part.args ?? {});
24
+ return {
25
+ id,
26
+ call: {
27
+ id,
28
+ type: "function",
29
+ function: {
30
+ name: part.toolName ?? "tool",
31
+ arguments: argsText
32
+ }
33
+ }
34
+ };
39
35
  }
40
36
  function extractText(content) {
41
- if (typeof content === "string")
42
- return content;
43
- if (!Array.isArray(content))
44
- return "";
45
- return content
46
- .filter((part) => part?.type === "text" && typeof part?.text === "string")
47
- .map((part) => part.text)
48
- .join("\n");
37
+ if (typeof content === "string") return content;
38
+ if (!Array.isArray(content)) return "";
39
+ return content.filter((part) => part?.type === "text" && typeof part?.text === "string").map((part) => part.text).join("\n");
49
40
  }
50
41
  function parseDataUrl(value) {
51
- const match = value.match(/^data:([^;,]+)(?:;[^;,]+)*;base64,(.+)$/);
52
- if (!match)
53
- return null;
54
- return { mimeType: match[1], data: match[2] };
42
+ const match = value.match(/^data:([^;,]+)(?:;[^;,]+)*;base64,(.+)$/);
43
+ if (!match) return null;
44
+ return {
45
+ mimeType: match[1],
46
+ data: match[2]
47
+ };
55
48
  }
56
49
  const httpUrlPattern = /^https?:\/\//i;
57
50
  function toInputContent(part, fallbackMimeType) {
58
- if (!isObject(part))
59
- return null;
60
- const type = getString(part, "type");
61
- if (type === "text") {
62
- const text = getString(part, "text");
63
- if (text === undefined)
64
- return null;
65
- return { type: "text", text };
66
- }
67
- if (type === "image") {
68
- const image = getString(part, "image");
69
- if (image === undefined)
70
- return null;
71
- const parsed = parseDataUrl(image);
72
- if (parsed) {
73
- return {
74
- type: "image",
75
- source: {
76
- type: "data",
77
- value: parsed.data,
78
- mimeType: parsed.mimeType,
79
- },
80
- };
81
- }
82
- return {
83
- type: "image",
84
- source: {
85
- type: "url",
86
- value: image,
87
- ...(fallbackMimeType !== undefined
88
- ? { mimeType: fallbackMimeType }
89
- : {}),
90
- },
91
- };
92
- }
93
- if (type === "file") {
94
- const data = getString(part, "data");
95
- if (data === undefined)
96
- return null;
97
- const partMimeType = getString(part, "mimeType");
98
- const filename = getString(part, "filename");
99
- const mimeType = partMimeType || fallbackMimeType || "application/octet-stream";
100
- if (httpUrlPattern.test(data)) {
101
- return {
102
- type: "binary",
103
- mimeType,
104
- url: data,
105
- ...(filename !== undefined ? { filename } : {}),
106
- };
107
- }
108
- const parsed = parseDataUrl(data);
109
- return {
110
- type: "binary",
111
- mimeType: parsed?.mimeType ?? mimeType,
112
- data: parsed?.data ?? data,
113
- ...(filename !== undefined ? { filename } : {}),
114
- };
115
- }
116
- return null;
51
+ if (!isObject(part)) return null;
52
+ const type = getString(part, "type");
53
+ if (type === "text") {
54
+ const text = getString(part, "text");
55
+ if (text === void 0) return null;
56
+ return {
57
+ type: "text",
58
+ text
59
+ };
60
+ }
61
+ if (type === "image") {
62
+ const image = getString(part, "image");
63
+ if (image === void 0) return null;
64
+ const parsed = parseDataUrl(image);
65
+ if (parsed) return {
66
+ type: "image",
67
+ source: {
68
+ type: "data",
69
+ value: parsed.data,
70
+ mimeType: parsed.mimeType
71
+ }
72
+ };
73
+ return {
74
+ type: "image",
75
+ source: {
76
+ type: "url",
77
+ value: image,
78
+ ...fallbackMimeType !== void 0 ? { mimeType: fallbackMimeType } : {}
79
+ }
80
+ };
81
+ }
82
+ if (type === "file") {
83
+ const data = getString(part, "data");
84
+ if (data === void 0) return null;
85
+ const partMimeType = getString(part, "mimeType");
86
+ const filename = getString(part, "filename");
87
+ const mimeType = partMimeType || fallbackMimeType || "application/octet-stream";
88
+ if (httpUrlPattern.test(data)) return {
89
+ type: "binary",
90
+ mimeType,
91
+ url: data,
92
+ ...filename !== void 0 ? { filename } : {}
93
+ };
94
+ const parsed = parseDataUrl(data);
95
+ return {
96
+ type: "binary",
97
+ mimeType: parsed?.mimeType ?? mimeType,
98
+ data: parsed?.data ?? data,
99
+ ...filename !== void 0 ? { filename } : {}
100
+ };
101
+ }
102
+ return null;
117
103
  }
118
104
  function buildUserContent(message) {
119
- // File parts in message.content are intentionally skipped: the canonical
120
- // binary payload for files always flows through message.attachments.
121
- const contentParts = Array.isArray(message.content)
122
- ? message.content.filter((part) => !(isObject(part) && part.type === "file"))
123
- : [];
124
- const attachments = message.attachments ?? [];
125
- const converted = [];
126
- // Promote string-form content to a leading text part so it survives when
127
- // non-text attachments are present (fromAgUiMessages emits string content).
128
- if (typeof message.content === "string" && message.content.length > 0) {
129
- converted.push({ type: "text", text: message.content });
130
- }
131
- for (const part of contentParts) {
132
- const input = toInputContent(part, undefined);
133
- if (input)
134
- converted.push(input);
135
- }
136
- for (const attachment of attachments) {
137
- if (!isObject(attachment))
138
- continue;
139
- const attachmentContent = attachment.content;
140
- if (!Array.isArray(attachmentContent))
141
- continue;
142
- const fallbackMime = getString(attachment, "contentType");
143
- for (const part of attachmentContent) {
144
- const input = toInputContent(part, fallbackMime);
145
- if (input)
146
- converted.push(input);
147
- }
148
- }
149
- const hasNonText = converted.some((part) => part.type !== "text");
150
- if (hasNonText)
151
- return converted;
152
- // All-text path: collapse to plain string. Join text parts collected from
153
- // both content and attachments so attachment-sourced text is not dropped.
154
- if (converted.length === 0)
155
- return extractText(message.content);
156
- return converted
157
- .filter((part) => part.type === "text")
158
- .map((part) => part.text)
159
- .join("\n");
105
+ const contentParts = Array.isArray(message.content) ? message.content.filter((part) => !(isObject(part) && part.type === "file")) : [];
106
+ const attachments = message.attachments ?? [];
107
+ const converted = [];
108
+ if (typeof message.content === "string" && message.content.length > 0) converted.push({
109
+ type: "text",
110
+ text: message.content
111
+ });
112
+ for (const part of contentParts) {
113
+ const input = toInputContent(part, void 0);
114
+ if (input) converted.push(input);
115
+ }
116
+ for (const attachment of attachments) {
117
+ if (!isObject(attachment)) continue;
118
+ const attachmentContent = attachment.content;
119
+ if (!Array.isArray(attachmentContent)) continue;
120
+ const fallbackMime = getString(attachment, "contentType");
121
+ for (const part of attachmentContent) {
122
+ const input = toInputContent(part, fallbackMime);
123
+ if (input) converted.push(input);
124
+ }
125
+ }
126
+ if (converted.some((part) => part.type !== "text")) return converted;
127
+ if (converted.length === 0) return extractText(message.content);
128
+ return converted.filter((part) => part.type === "text").map((part) => part.text).join("\n");
160
129
  }
161
130
  function toToolCallPart(value) {
162
- if (!isObject(value))
163
- return null;
164
- const rawFunction = isObject(value.function) ? value.function : null;
165
- const toolCallId = getString(value, "toolCallId") ?? getString(value, "id");
166
- const toolName = getString(value, "toolName") ??
167
- getString(value, "name") ??
168
- (rawFunction ? getString(rawFunction, "name") : undefined) ??
169
- "tool";
170
- const argsText = getString(value, "argsText") ??
171
- getString(value, "arguments") ??
172
- (rawFunction ? getString(rawFunction, "arguments") : undefined);
173
- const parsedArgs = typeof argsText === "string" ? parseJSONText(argsText) : undefined;
174
- const args = isObject(parsedArgs) && !Array.isArray(parsedArgs)
175
- ? parsedArgs
176
- : isObject(value.args) && !Array.isArray(value.args)
177
- ? value.args
178
- : undefined;
179
- const part = {
180
- type: "tool-call",
181
- ...(toolCallId !== undefined ? { toolCallId } : {}),
182
- toolName,
183
- argsText: argsText ?? JSON.stringify(args ?? {}),
184
- ...(args !== undefined ? { args } : {}),
185
- };
186
- if (value.type === "tool-call") {
187
- const result = value.result;
188
- const isError = value.isError;
189
- if (result !== undefined)
190
- part.result = result;
191
- if (typeof isError === "boolean")
192
- part.isError = isError;
193
- }
194
- return part;
131
+ if (!isObject(value)) return null;
132
+ const rawFunction = isObject(value.function) ? value.function : null;
133
+ const toolCallId = getString(value, "toolCallId") ?? getString(value, "id");
134
+ const toolName = getString(value, "toolName") ?? getString(value, "name") ?? (rawFunction ? getString(rawFunction, "name") : void 0) ?? "tool";
135
+ const argsText = getString(value, "argsText") ?? getString(value, "arguments") ?? (rawFunction ? getString(rawFunction, "arguments") : void 0);
136
+ const parsedArgs = typeof argsText === "string" ? parseJSONText(argsText) : void 0;
137
+ const args = isObject(parsedArgs) && !Array.isArray(parsedArgs) ? parsedArgs : isObject(value.args) && !Array.isArray(value.args) ? value.args : void 0;
138
+ const part = {
139
+ type: "tool-call",
140
+ ...toolCallId !== void 0 ? { toolCallId } : {},
141
+ toolName,
142
+ argsText: argsText ?? JSON.stringify(args ?? {}),
143
+ ...args !== void 0 ? { args } : {}
144
+ };
145
+ if (value.type === "tool-call") {
146
+ const result = value.result;
147
+ const isError = value.isError;
148
+ if (result !== void 0) part.result = result;
149
+ if (typeof isError === "boolean") part.isError = isError;
150
+ }
151
+ return part;
195
152
  }
196
153
  function extractAssistantToolCalls(message) {
197
- const parts = [];
198
- const seenToolCallIds = new Set();
199
- const pushPart = (part) => {
200
- if (!part)
201
- return;
202
- const id = part.toolCallId ?? generateId();
203
- if (seenToolCallIds.has(id))
204
- return;
205
- seenToolCallIds.add(id);
206
- parts.push({
207
- ...part,
208
- toolCallId: id,
209
- });
210
- };
211
- const content = message.content;
212
- if (Array.isArray(content)) {
213
- for (const part of content) {
214
- if (isObject(part) && part.type === "tool-call") {
215
- pushPart(toToolCallPart(part));
216
- }
217
- }
218
- }
219
- const toolCalls = Array.isArray(message.toolCalls)
220
- ? message.toolCalls
221
- : Array.isArray(message.tool_calls)
222
- ? message.tool_calls
223
- : [];
224
- for (const call of toolCalls) {
225
- pushPart(toToolCallPart(call));
226
- }
227
- return parts;
154
+ const parts = [];
155
+ const seenToolCallIds = /* @__PURE__ */ new Set();
156
+ const pushPart = (part) => {
157
+ if (!part) return;
158
+ const id = part.toolCallId ?? generateId();
159
+ if (seenToolCallIds.has(id)) return;
160
+ seenToolCallIds.add(id);
161
+ parts.push({
162
+ ...part,
163
+ toolCallId: id
164
+ });
165
+ };
166
+ const content = message.content;
167
+ if (Array.isArray(content)) {
168
+ for (const part of content) if (isObject(part) && part.type === "tool-call") pushPart(toToolCallPart(part));
169
+ }
170
+ const toolCalls = Array.isArray(message.toolCalls) ? message.toolCalls : Array.isArray(message.tool_calls) ? message.tool_calls : [];
171
+ for (const call of toolCalls) pushPart(toToolCallPart(call));
172
+ return parts;
228
173
  }
229
174
  function toAssistantSnapshotMessage(rawMessage) {
230
- const text = extractText(rawMessage.content);
231
- const toolCallParts = extractAssistantToolCalls(rawMessage);
232
- const assistantContent = [
233
- ...(text.length > 0 ? [{ type: "text", text }] : []),
234
- ...toolCallParts,
235
- ];
236
- const messageName = getString(rawMessage, "name");
237
- return {
238
- id: getString(rawMessage, "id") ?? generateId(),
239
- role: "assistant",
240
- content: assistantContent.length > 0 ? assistantContent : "",
241
- ...(messageName !== undefined ? { name: messageName } : {}),
242
- };
175
+ const text = extractText(rawMessage.content);
176
+ const toolCallParts = extractAssistantToolCalls(rawMessage);
177
+ const assistantContent = [...text.length > 0 ? [{
178
+ type: "text",
179
+ text
180
+ }] : [], ...toolCallParts];
181
+ const messageName = getString(rawMessage, "name");
182
+ return {
183
+ id: getString(rawMessage, "id") ?? generateId(),
184
+ role: "assistant",
185
+ content: assistantContent.length > 0 ? assistantContent : "",
186
+ ...messageName !== void 0 ? { name: messageName } : {}
187
+ };
243
188
  }
244
189
  function toUserOrSystemSnapshotMessage(role, rawMessage) {
245
- const messageName = getString(rawMessage, "name");
246
- return {
247
- id: getString(rawMessage, "id") ?? generateId(),
248
- role,
249
- content: extractText(rawMessage.content),
250
- ...(messageName !== undefined ? { name: messageName } : {}),
251
- };
190
+ const messageName = getString(rawMessage, "name");
191
+ return {
192
+ id: getString(rawMessage, "id") ?? generateId(),
193
+ role,
194
+ content: extractText(rawMessage.content),
195
+ ...messageName !== void 0 ? { name: messageName } : {}
196
+ };
252
197
  }
253
- export function fromAgUiMessages(messages) {
254
- const converted = [];
255
- for (const rawMessage of messages) {
256
- if (!isObject(rawMessage))
257
- continue;
258
- const role = getString(rawMessage, "role");
259
- if (!role)
260
- continue;
261
- if (role === "tool") {
262
- const toolCallId = getToolCallId(rawMessage) ?? `tool-${generateId()}`;
263
- const toolMessageId = getString(rawMessage, "id");
264
- const result = rawMessage.result !== undefined
265
- ? rawMessage.result
266
- : typeof rawMessage.content === "string"
267
- ? parseJSONText(rawMessage.content)
268
- : rawMessage.content;
269
- const isError = typeof rawMessage.error === "string" ||
270
- rawMessage.isError === true ||
271
- rawMessage.status === "error"
272
- ? true
273
- : rawMessage.isError === false
274
- ? false
275
- : undefined;
276
- let updated = false;
277
- for (let messageIndex = converted.length - 1; messageIndex >= 0 && !updated; messageIndex--) {
278
- const message = converted[messageIndex];
279
- if (!message ||
280
- message.role !== "assistant" ||
281
- !Array.isArray(message.content))
282
- continue;
283
- for (let partIndex = message.content.length - 1; partIndex >= 0; partIndex--) {
284
- const part = message.content[partIndex];
285
- if (!isObject(part) || part.type !== "tool-call")
286
- continue;
287
- if (getString(part, "toolCallId") !== toolCallId)
288
- continue;
289
- const updatedPart = {
290
- ...part,
291
- result,
292
- ...(isError !== undefined ? { isError } : {}),
293
- ...(toolMessageId !== undefined
294
- ? { unstable_toolMessageId: toolMessageId }
295
- : {}),
296
- };
297
- const updatedContent = message.content.map((contentPart, index) => index === partIndex ? updatedPart : contentPart);
298
- converted[messageIndex] = { ...message, content: updatedContent };
299
- updated = true;
300
- break;
301
- }
302
- }
303
- if (updated) {
304
- continue;
305
- }
306
- const id = toolMessageId ?? toolCallId;
307
- const toolName = getString(rawMessage, "name") ??
308
- getString(rawMessage, "toolName") ??
309
- "tool";
310
- converted.push({
311
- id: `${id}:assistant`,
312
- role: "assistant",
313
- content: [
314
- {
315
- type: "tool-call",
316
- toolCallId,
317
- toolName,
318
- args: {},
319
- argsText: "{}",
320
- result,
321
- ...(isError !== undefined ? { isError } : {}),
322
- ...(toolMessageId !== undefined
323
- ? { unstable_toolMessageId: toolMessageId }
324
- : {}),
325
- },
326
- ],
327
- });
328
- continue;
329
- }
330
- if (role === "assistant") {
331
- converted.push(toAssistantSnapshotMessage(rawMessage));
332
- continue;
333
- }
334
- if (role === "user" || role === "system") {
335
- converted.push(toUserOrSystemSnapshotMessage(role, rawMessage));
336
- }
337
- }
338
- return converted;
198
+ function fromAgUiMessages(messages) {
199
+ const converted = [];
200
+ for (const rawMessage of messages) {
201
+ if (!isObject(rawMessage)) continue;
202
+ const role = getString(rawMessage, "role");
203
+ if (!role) continue;
204
+ if (role === "tool") {
205
+ const toolCallId = getToolCallId(rawMessage) ?? `tool-${generateId()}`;
206
+ const toolMessageId = getString(rawMessage, "id");
207
+ const result = rawMessage.result !== void 0 ? rawMessage.result : typeof rawMessage.content === "string" ? parseJSONText(rawMessage.content) : rawMessage.content;
208
+ const isError = typeof rawMessage.error === "string" || rawMessage.isError === true || rawMessage.status === "error" ? true : rawMessage.isError === false ? false : void 0;
209
+ let updated = false;
210
+ for (let messageIndex = converted.length - 1; messageIndex >= 0 && !updated; messageIndex--) {
211
+ const message = converted[messageIndex];
212
+ if (!message || message.role !== "assistant" || !Array.isArray(message.content)) continue;
213
+ for (let partIndex = message.content.length - 1; partIndex >= 0; partIndex--) {
214
+ const part = message.content[partIndex];
215
+ if (!isObject(part) || part.type !== "tool-call") continue;
216
+ if (getString(part, "toolCallId") !== toolCallId) continue;
217
+ const updatedPart = {
218
+ ...part,
219
+ result,
220
+ ...isError !== void 0 ? { isError } : {},
221
+ ...toolMessageId !== void 0 ? { unstable_toolMessageId: toolMessageId } : {}
222
+ };
223
+ const updatedContent = message.content.map((contentPart, index) => index === partIndex ? updatedPart : contentPart);
224
+ converted[messageIndex] = {
225
+ ...message,
226
+ content: updatedContent
227
+ };
228
+ updated = true;
229
+ break;
230
+ }
231
+ }
232
+ if (updated) continue;
233
+ const id = toolMessageId ?? toolCallId;
234
+ const toolName = getString(rawMessage, "name") ?? getString(rawMessage, "toolName") ?? "tool";
235
+ converted.push({
236
+ id: `${id}:assistant`,
237
+ role: "assistant",
238
+ content: [{
239
+ type: "tool-call",
240
+ toolCallId,
241
+ toolName,
242
+ args: {},
243
+ argsText: "{}",
244
+ result,
245
+ ...isError !== void 0 ? { isError } : {},
246
+ ...toolMessageId !== void 0 ? { unstable_toolMessageId: toolMessageId } : {}
247
+ }]
248
+ });
249
+ continue;
250
+ }
251
+ if (role === "assistant") {
252
+ converted.push(toAssistantSnapshotMessage(rawMessage));
253
+ continue;
254
+ }
255
+ if (role === "user" || role === "system") converted.push(toUserOrSystemSnapshotMessage(role, rawMessage));
256
+ }
257
+ return converted;
339
258
  }
340
259
  function convertAssistantMessage(message, converted) {
341
- const content = extractText(message.content);
342
- const contentArray = Array.isArray(message.content) ? message.content : [];
343
- const toolCallParts = contentArray.filter((part) => part?.type === "tool-call");
344
- const toolCalls = toolCallParts.map((part) => ({
345
- ...normalizeToolCall(part),
346
- part,
347
- }));
348
- const assistantMessage = {
349
- id: message.id,
350
- role: "assistant",
351
- content,
352
- };
353
- if (message.name) {
354
- assistantMessage.name = message.name;
355
- }
356
- if (toolCalls.length > 0) {
357
- assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);
358
- }
359
- converted.push(assistantMessage);
360
- for (const { id: toolCallId, part } of toolCalls) {
361
- if (part.result === undefined)
362
- continue;
363
- const resultContent = typeof part.result === "string"
364
- ? part.result
365
- : JSON.stringify(part.result);
366
- const toolMessage = {
367
- id: part.unstable_toolMessageId ?? `${toolCallId}:tool`,
368
- role: "tool",
369
- content: resultContent,
370
- toolCallId,
371
- };
372
- if (part.isError) {
373
- toolMessage.error = resultContent;
374
- }
375
- converted.push(toolMessage);
376
- }
260
+ const content = extractText(message.content);
261
+ const toolCalls = (Array.isArray(message.content) ? message.content : []).filter((part) => part?.type === "tool-call").map((part) => ({
262
+ ...normalizeToolCall(part),
263
+ part
264
+ }));
265
+ const assistantMessage = {
266
+ id: message.id,
267
+ role: "assistant",
268
+ content
269
+ };
270
+ if (message.name) assistantMessage.name = message.name;
271
+ if (toolCalls.length > 0) assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);
272
+ converted.push(assistantMessage);
273
+ for (const { id: toolCallId, part } of toolCalls) {
274
+ if (part.result === void 0) continue;
275
+ const resultContent = typeof part.result === "string" ? part.result : JSON.stringify(part.result);
276
+ const toolMessage = {
277
+ id: part.unstable_toolMessageId ?? `${toolCallId}:tool`,
278
+ role: "tool",
279
+ content: resultContent,
280
+ toolCallId
281
+ };
282
+ if (part.isError) toolMessage.error = resultContent;
283
+ converted.push(toolMessage);
284
+ }
377
285
  }
378
286
  function convertToolMessage(message, converted) {
379
- const content = extractText(message.content);
380
- const toolCallId = message.toolCallId ?? generateId();
381
- const toolMessage = {
382
- id: message.id,
383
- role: "tool",
384
- content,
385
- toolCallId,
386
- };
387
- if (typeof message.error === "string") {
388
- toolMessage.error = message.error;
389
- }
390
- converted.push(toolMessage);
287
+ const content = extractText(message.content);
288
+ const toolCallId = message.toolCallId ?? generateId();
289
+ const toolMessage = {
290
+ id: message.id,
291
+ role: "tool",
292
+ content,
293
+ toolCallId
294
+ };
295
+ if (typeof message.error === "string") toolMessage.error = message.error;
296
+ converted.push(toolMessage);
391
297
  }
392
- export function toAgUiMessages(messages) {
393
- const converted = [];
394
- for (const message of messages) {
395
- if (message.role === "assistant") {
396
- convertAssistantMessage(message, converted);
397
- continue;
398
- }
399
- if (message.role === "tool") {
400
- convertToolMessage(message, converted);
401
- continue;
402
- }
403
- const genericMessage = {
404
- id: message.id,
405
- role: message.role,
406
- content: message.role === "user"
407
- ? buildUserContent(message)
408
- : extractText(message.content),
409
- };
410
- if (message.name) {
411
- genericMessage.name = message.name;
412
- }
413
- converted.push(genericMessage);
414
- }
415
- return converted;
298
+ function toAgUiMessages(messages) {
299
+ const converted = [];
300
+ for (const message of messages) {
301
+ if (message.role === "assistant") {
302
+ convertAssistantMessage(message, converted);
303
+ continue;
304
+ }
305
+ if (message.role === "tool") {
306
+ convertToolMessage(message, converted);
307
+ continue;
308
+ }
309
+ const genericMessage = {
310
+ id: message.id,
311
+ role: message.role,
312
+ content: message.role === "user" ? buildUserContent(message) : extractText(message.content)
313
+ };
314
+ if (message.name) genericMessage.name = message.name;
315
+ converted.push(genericMessage);
316
+ }
317
+ return converted;
416
318
  }
417
- export function toAgUiTools(tools) {
418
- if (!tools)
419
- return [];
420
- const toolsSchema = toToolsJSONSchema(tools);
421
- return Object.entries(toolsSchema).map(([name, tool]) => ({
422
- name,
423
- description: tool.description,
424
- parameters: tool.parameters,
425
- }));
319
+ function toAgUiTools(tools) {
320
+ if (!tools) return [];
321
+ const toolsSchema = toToolsJSONSchema(tools);
322
+ return Object.entries(toolsSchema).map(([name, tool]) => ({
323
+ name,
324
+ description: tool.description,
325
+ parameters: tool.parameters
326
+ }));
426
327
  }
328
+ //#endregion
329
+ export { fromAgUiMessages, toAgUiMessages, toAgUiTools };
330
+
427
331
  //# sourceMappingURL=conversions.js.map