@assistant-ui/react-ag-ui 0.0.31 → 0.0.33
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.
- package/dist/index.d.ts +3 -4
- package/dist/index.js +2 -2
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts +85 -82
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtime/AgUiThreadRuntimeCore.js +542 -628
- package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts +39 -36
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +294 -390
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/adapter/run-aggregator.d.ts +54 -51
- package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
- package/dist/runtime/adapter/run-aggregator.js +367 -413
- package/dist/runtime/adapter/run-aggregator.js.map +1 -1
- package/dist/runtime/adapter/subscriber.d.ts +94 -91
- package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
- package/dist/runtime/adapter/subscriber.js +77 -86
- package/dist/runtime/adapter/subscriber.js.map +1 -1
- package/dist/runtime/event-parser.d.ts +9 -5
- package/dist/runtime/event-parser.d.ts.map +1 -1
- package/dist/runtime/event-parser.js +172 -177
- package/dist/runtime/event-parser.js.map +1 -1
- package/dist/runtime/logger.d.ts +7 -4
- package/dist/runtime/logger.d.ts.map +1 -1
- package/dist/runtime/logger.js +7 -3
- package/dist/runtime/logger.js.map +1 -1
- package/dist/runtime/types.d.ts +121 -117
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js +0 -2
- package/dist/useAgUiRuntime.d.ts +10 -6
- package/dist/useAgUiRuntime.d.ts.map +1 -1
- package/dist/useAgUiRuntime.js +105 -142
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +7 -7
- package/src/useAgUiRuntime.ts +3 -47
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- 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
|
-
|
|
6
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
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
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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
|