ai 5.0.0-canary.17 → 5.0.0-canary.19
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/CHANGELOG.md +36 -0
- package/README.md +4 -4
- package/dist/index.d.mts +747 -510
- package/dist/index.d.ts +747 -510
- package/dist/index.js +3345 -3688
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3349 -3690
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +441 -214
- package/dist/internal/index.d.ts +441 -214
- package/dist/internal/index.js +911 -1360
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +911 -1355
- package/dist/internal/index.mjs.map +1 -1
- package/dist/mcp-stdio/index.d.mts +2 -2
- package/dist/mcp-stdio/index.d.ts +2 -2
- package/package.json +4 -4
package/dist/internal/index.mjs
CHANGED
@@ -1,22 +1,25 @@
|
|
1
|
-
// core/prompt/
|
2
|
-
import {
|
3
|
-
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
4
|
-
import { z as z7 } from "zod";
|
1
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
2
|
+
import { isUrlSupported } from "@ai-sdk/provider-utils";
|
5
3
|
|
6
|
-
//
|
4
|
+
// util/download-error.ts
|
7
5
|
import { AISDKError } from "@ai-sdk/provider";
|
8
|
-
var name = "
|
6
|
+
var name = "AI_DownloadError";
|
9
7
|
var marker = `vercel.ai.error.${name}`;
|
10
8
|
var symbol = Symbol.for(marker);
|
11
9
|
var _a;
|
12
|
-
var
|
10
|
+
var DownloadError = class extends AISDKError {
|
13
11
|
constructor({
|
14
|
-
|
15
|
-
|
12
|
+
url,
|
13
|
+
statusCode,
|
14
|
+
statusText,
|
15
|
+
cause,
|
16
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
16
17
|
}) {
|
17
|
-
super({ name, message });
|
18
|
+
super({ name, message, cause });
|
18
19
|
this[_a] = true;
|
19
|
-
this.
|
20
|
+
this.url = url;
|
21
|
+
this.statusCode = statusCode;
|
22
|
+
this.statusText = statusText;
|
20
23
|
}
|
21
24
|
static isInstance(error) {
|
22
25
|
return AISDKError.hasMarker(error, marker);
|
@@ -24,271 +27,172 @@ var MessageConversionError = class extends AISDKError {
|
|
24
27
|
};
|
25
28
|
_a = symbol;
|
26
29
|
|
27
|
-
//
|
28
|
-
function
|
29
|
-
var _a6
|
30
|
-
const
|
31
|
-
const coreMessages = [];
|
32
|
-
for (let i = 0; i < messages.length; i++) {
|
33
|
-
const message = messages[i];
|
34
|
-
const isLastMessage = i === messages.length - 1;
|
35
|
-
const { role, content } = message;
|
36
|
-
switch (role) {
|
37
|
-
case "system": {
|
38
|
-
coreMessages.push({
|
39
|
-
role: "system",
|
40
|
-
content
|
41
|
-
});
|
42
|
-
break;
|
43
|
-
}
|
44
|
-
case "user": {
|
45
|
-
coreMessages.push({
|
46
|
-
role: "user",
|
47
|
-
content: message.parts.filter(
|
48
|
-
(part) => part.type === "text" || part.type === "file"
|
49
|
-
).map(
|
50
|
-
(part) => part.type === "file" ? {
|
51
|
-
type: "file",
|
52
|
-
mediaType: part.mediaType,
|
53
|
-
filename: part.filename,
|
54
|
-
data: part.url
|
55
|
-
} : part
|
56
|
-
)
|
57
|
-
});
|
58
|
-
break;
|
59
|
-
}
|
60
|
-
case "assistant": {
|
61
|
-
if (message.parts != null) {
|
62
|
-
let processBlock2 = function() {
|
63
|
-
const content2 = [];
|
64
|
-
for (const part of block) {
|
65
|
-
switch (part.type) {
|
66
|
-
case "text": {
|
67
|
-
content2.push(part);
|
68
|
-
break;
|
69
|
-
}
|
70
|
-
case "file": {
|
71
|
-
content2.push({
|
72
|
-
type: "file",
|
73
|
-
mediaType: part.mediaType,
|
74
|
-
data: part.url
|
75
|
-
});
|
76
|
-
break;
|
77
|
-
}
|
78
|
-
case "reasoning": {
|
79
|
-
content2.push({
|
80
|
-
type: "reasoning",
|
81
|
-
text: part.text,
|
82
|
-
providerOptions: part.providerMetadata
|
83
|
-
});
|
84
|
-
break;
|
85
|
-
}
|
86
|
-
case "tool-invocation":
|
87
|
-
content2.push({
|
88
|
-
type: "tool-call",
|
89
|
-
toolCallId: part.toolInvocation.toolCallId,
|
90
|
-
toolName: part.toolInvocation.toolName,
|
91
|
-
args: part.toolInvocation.args
|
92
|
-
});
|
93
|
-
break;
|
94
|
-
default: {
|
95
|
-
const _exhaustiveCheck = part;
|
96
|
-
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
97
|
-
}
|
98
|
-
}
|
99
|
-
}
|
100
|
-
coreMessages.push({
|
101
|
-
role: "assistant",
|
102
|
-
content: content2
|
103
|
-
});
|
104
|
-
const stepInvocations = block.filter(
|
105
|
-
(part) => part.type === "tool-invocation"
|
106
|
-
).map((part) => part.toolInvocation);
|
107
|
-
if (stepInvocations.length > 0) {
|
108
|
-
coreMessages.push({
|
109
|
-
role: "tool",
|
110
|
-
content: stepInvocations.map(
|
111
|
-
(toolInvocation) => {
|
112
|
-
if (!("result" in toolInvocation)) {
|
113
|
-
throw new MessageConversionError({
|
114
|
-
originalMessage: message,
|
115
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
116
|
-
});
|
117
|
-
}
|
118
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
119
|
-
const tool = tools[toolName];
|
120
|
-
return (tool == null ? void 0 : tool.experimental_toToolResultContent) != null ? {
|
121
|
-
type: "tool-result",
|
122
|
-
toolCallId,
|
123
|
-
toolName,
|
124
|
-
result: tool.experimental_toToolResultContent(result),
|
125
|
-
experimental_content: tool.experimental_toToolResultContent(result)
|
126
|
-
} : {
|
127
|
-
type: "tool-result",
|
128
|
-
toolCallId,
|
129
|
-
toolName,
|
130
|
-
result
|
131
|
-
};
|
132
|
-
}
|
133
|
-
)
|
134
|
-
});
|
135
|
-
}
|
136
|
-
block = [];
|
137
|
-
blockHasToolInvocations = false;
|
138
|
-
currentStep++;
|
139
|
-
};
|
140
|
-
var processBlock = processBlock2;
|
141
|
-
let currentStep = 0;
|
142
|
-
let blockHasToolInvocations = false;
|
143
|
-
let block = [];
|
144
|
-
for (const part of message.parts) {
|
145
|
-
switch (part.type) {
|
146
|
-
case "text": {
|
147
|
-
if (blockHasToolInvocations) {
|
148
|
-
processBlock2();
|
149
|
-
}
|
150
|
-
block.push(part);
|
151
|
-
break;
|
152
|
-
}
|
153
|
-
case "file":
|
154
|
-
case "reasoning": {
|
155
|
-
block.push(part);
|
156
|
-
break;
|
157
|
-
}
|
158
|
-
case "tool-invocation": {
|
159
|
-
if (((_b = part.toolInvocation.step) != null ? _b : 0) !== currentStep) {
|
160
|
-
processBlock2();
|
161
|
-
}
|
162
|
-
block.push(part);
|
163
|
-
blockHasToolInvocations = true;
|
164
|
-
break;
|
165
|
-
}
|
166
|
-
}
|
167
|
-
}
|
168
|
-
processBlock2();
|
169
|
-
break;
|
170
|
-
}
|
171
|
-
if (content && !isLastMessage) {
|
172
|
-
coreMessages.push({ role: "assistant", content });
|
173
|
-
}
|
174
|
-
break;
|
175
|
-
}
|
176
|
-
default: {
|
177
|
-
const _exhaustiveCheck = role;
|
178
|
-
throw new MessageConversionError({
|
179
|
-
originalMessage: message,
|
180
|
-
message: `Unsupported role: ${_exhaustiveCheck}`
|
181
|
-
});
|
182
|
-
}
|
183
|
-
}
|
184
|
-
}
|
185
|
-
return coreMessages;
|
186
|
-
}
|
187
|
-
|
188
|
-
// core/prompt/detect-prompt-type.ts
|
189
|
-
function detectPromptType(prompt) {
|
190
|
-
if (!Array.isArray(prompt)) {
|
191
|
-
return "other";
|
192
|
-
}
|
193
|
-
if (prompt.length === 0) {
|
194
|
-
return "messages";
|
195
|
-
}
|
196
|
-
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
197
|
-
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
198
|
-
return "ui-messages";
|
199
|
-
} else if (characteristics.every(
|
200
|
-
(c) => c === "has-core-specific-parts" || c === "message"
|
201
|
-
)) {
|
202
|
-
return "messages";
|
203
|
-
} else {
|
204
|
-
return "other";
|
205
|
-
}
|
206
|
-
}
|
207
|
-
function detectSingleMessageCharacteristics(message) {
|
208
|
-
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
209
|
-
message.role === "data" || // UI-only role
|
210
|
-
"toolInvocations" in message || // UI-specific field
|
211
|
-
"parts" in message || // UI-specific field
|
212
|
-
"experimental_attachments" in message)) {
|
213
|
-
return "has-ui-specific-parts";
|
214
|
-
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
215
|
-
"providerOptions" in message)) {
|
216
|
-
return "has-core-specific-parts";
|
217
|
-
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
218
|
-
return "message";
|
219
|
-
} else {
|
220
|
-
return "other";
|
221
|
-
}
|
222
|
-
}
|
223
|
-
|
224
|
-
// core/prompt/message.ts
|
225
|
-
import { z as z6 } from "zod";
|
226
|
-
|
227
|
-
// core/types/provider-metadata.ts
|
228
|
-
import { z as z2 } from "zod";
|
229
|
-
|
230
|
-
// core/types/json-value.ts
|
231
|
-
import { z } from "zod";
|
232
|
-
var jsonValueSchema = z.lazy(
|
233
|
-
() => z.union([
|
234
|
-
z.null(),
|
235
|
-
z.string(),
|
236
|
-
z.number(),
|
237
|
-
z.boolean(),
|
238
|
-
z.record(z.string(), jsonValueSchema),
|
239
|
-
z.array(jsonValueSchema)
|
240
|
-
])
|
241
|
-
);
|
242
|
-
|
243
|
-
// core/types/provider-metadata.ts
|
244
|
-
var providerMetadataSchema = z2.record(
|
245
|
-
z2.string(),
|
246
|
-
z2.record(z2.string(), jsonValueSchema)
|
247
|
-
);
|
248
|
-
|
249
|
-
// core/prompt/content-part.ts
|
250
|
-
import { z as z5 } from "zod";
|
251
|
-
|
252
|
-
// core/prompt/data-content.ts
|
253
|
-
import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
|
254
|
-
import {
|
255
|
-
convertBase64ToUint8Array,
|
256
|
-
convertUint8ArrayToBase64
|
257
|
-
} from "@ai-sdk/provider-utils";
|
258
|
-
import { z as z3 } from "zod";
|
259
|
-
|
260
|
-
// core/prompt/split-data-url.ts
|
261
|
-
function splitDataUrl(dataUrl) {
|
30
|
+
// util/download.ts
|
31
|
+
async function download({ url }) {
|
32
|
+
var _a6;
|
33
|
+
const urlText = url.toString();
|
262
34
|
try {
|
263
|
-
const
|
35
|
+
const response = await fetch(urlText);
|
36
|
+
if (!response.ok) {
|
37
|
+
throw new DownloadError({
|
38
|
+
url: urlText,
|
39
|
+
statusCode: response.status,
|
40
|
+
statusText: response.statusText
|
41
|
+
});
|
42
|
+
}
|
264
43
|
return {
|
265
|
-
|
266
|
-
|
44
|
+
data: new Uint8Array(await response.arrayBuffer()),
|
45
|
+
mediaType: (_a6 = response.headers.get("content-type")) != null ? _a6 : void 0
|
267
46
|
};
|
268
47
|
} catch (error) {
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
};
|
48
|
+
if (DownloadError.isInstance(error)) {
|
49
|
+
throw error;
|
50
|
+
}
|
51
|
+
throw new DownloadError({ url: urlText, cause: error });
|
273
52
|
}
|
274
53
|
}
|
275
54
|
|
276
|
-
// core/
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
55
|
+
// core/util/detect-media-type.ts
|
56
|
+
import { convertBase64ToUint8Array } from "@ai-sdk/provider-utils";
|
57
|
+
var imageMediaTypeSignatures = [
|
58
|
+
{
|
59
|
+
mediaType: "image/gif",
|
60
|
+
bytesPrefix: [71, 73, 70],
|
61
|
+
base64Prefix: "R0lG"
|
62
|
+
},
|
63
|
+
{
|
64
|
+
mediaType: "image/png",
|
65
|
+
bytesPrefix: [137, 80, 78, 71],
|
66
|
+
base64Prefix: "iVBORw"
|
67
|
+
},
|
68
|
+
{
|
69
|
+
mediaType: "image/jpeg",
|
70
|
+
bytesPrefix: [255, 216],
|
71
|
+
base64Prefix: "/9j/"
|
72
|
+
},
|
73
|
+
{
|
74
|
+
mediaType: "image/webp",
|
75
|
+
bytesPrefix: [82, 73, 70, 70],
|
76
|
+
base64Prefix: "UklGRg"
|
77
|
+
},
|
78
|
+
{
|
79
|
+
mediaType: "image/bmp",
|
80
|
+
bytesPrefix: [66, 77],
|
81
|
+
base64Prefix: "Qk"
|
82
|
+
},
|
83
|
+
{
|
84
|
+
mediaType: "image/tiff",
|
85
|
+
bytesPrefix: [73, 73, 42, 0],
|
86
|
+
base64Prefix: "SUkqAA"
|
87
|
+
},
|
88
|
+
{
|
89
|
+
mediaType: "image/tiff",
|
90
|
+
bytesPrefix: [77, 77, 0, 42],
|
91
|
+
base64Prefix: "TU0AKg"
|
92
|
+
},
|
93
|
+
{
|
94
|
+
mediaType: "image/avif",
|
95
|
+
bytesPrefix: [
|
96
|
+
0,
|
97
|
+
0,
|
98
|
+
0,
|
99
|
+
32,
|
100
|
+
102,
|
101
|
+
116,
|
102
|
+
121,
|
103
|
+
112,
|
104
|
+
97,
|
105
|
+
118,
|
106
|
+
105,
|
107
|
+
102
|
108
|
+
],
|
109
|
+
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
110
|
+
},
|
111
|
+
{
|
112
|
+
mediaType: "image/heic",
|
113
|
+
bytesPrefix: [
|
114
|
+
0,
|
115
|
+
0,
|
116
|
+
0,
|
117
|
+
32,
|
118
|
+
102,
|
119
|
+
116,
|
120
|
+
121,
|
121
|
+
112,
|
122
|
+
104,
|
123
|
+
101,
|
124
|
+
105,
|
125
|
+
99
|
126
|
+
],
|
127
|
+
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
128
|
+
}
|
129
|
+
];
|
130
|
+
var stripID3 = (data) => {
|
131
|
+
const bytes = typeof data === "string" ? convertBase64ToUint8Array(data) : data;
|
132
|
+
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
|
133
|
+
return bytes.slice(id3Size + 10);
|
134
|
+
};
|
135
|
+
function stripID3TagsIfPresent(data) {
|
136
|
+
const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
|
137
|
+
data[1] === 68 && // 'D'
|
138
|
+
data[2] === 51;
|
139
|
+
return hasId3 ? stripID3(data) : data;
|
140
|
+
}
|
141
|
+
function detectMediaType({
|
142
|
+
data,
|
143
|
+
signatures
|
144
|
+
}) {
|
145
|
+
const processedData = stripID3TagsIfPresent(data);
|
146
|
+
for (const signature of signatures) {
|
147
|
+
if (typeof processedData === "string" ? processedData.startsWith(signature.base64Prefix) : processedData.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
|
148
|
+
(byte, index) => processedData[index] === byte
|
149
|
+
)) {
|
150
|
+
return signature.mediaType;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
return void 0;
|
154
|
+
}
|
155
|
+
|
156
|
+
// core/prompt/data-content.ts
|
157
|
+
import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
|
158
|
+
import {
|
159
|
+
convertBase64ToUint8Array as convertBase64ToUint8Array2,
|
160
|
+
convertUint8ArrayToBase64
|
161
|
+
} from "@ai-sdk/provider-utils";
|
162
|
+
import { z } from "zod";
|
163
|
+
|
164
|
+
// core/prompt/split-data-url.ts
|
165
|
+
function splitDataUrl(dataUrl) {
|
166
|
+
try {
|
167
|
+
const [header, base64Content] = dataUrl.split(",");
|
168
|
+
return {
|
169
|
+
mediaType: header.split(";")[0].split(":")[1],
|
170
|
+
base64Content
|
171
|
+
};
|
172
|
+
} catch (error) {
|
173
|
+
return {
|
174
|
+
mediaType: void 0,
|
175
|
+
base64Content: void 0
|
176
|
+
};
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
// core/prompt/data-content.ts
|
181
|
+
var dataContentSchema = z.union([
|
182
|
+
z.string(),
|
183
|
+
z.instanceof(Uint8Array),
|
184
|
+
z.instanceof(ArrayBuffer),
|
185
|
+
z.custom(
|
186
|
+
// Buffer might not be available in some environments such as CloudFlare:
|
187
|
+
(value) => {
|
188
|
+
var _a6, _b;
|
189
|
+
return (_b = (_a6 = globalThis.Buffer) == null ? void 0 : _a6.isBuffer(value)) != null ? _b : false;
|
190
|
+
},
|
191
|
+
{ message: "Must be a Buffer" }
|
192
|
+
)
|
193
|
+
]);
|
194
|
+
function convertToLanguageModelV2DataContent(content) {
|
195
|
+
if (content instanceof Uint8Array) {
|
292
196
|
return { data: content, mediaType: void 0 };
|
293
197
|
}
|
294
198
|
if (content instanceof ArrayBuffer) {
|
@@ -315,625 +219,265 @@ function convertToLanguageModelV2DataContent(content) {
|
|
315
219
|
return { data: content, mediaType: void 0 };
|
316
220
|
}
|
317
221
|
|
318
|
-
// core/prompt/
|
319
|
-
import {
|
320
|
-
var
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
);
|
330
|
-
|
331
|
-
|
332
|
-
var textPartSchema = z5.object({
|
333
|
-
type: z5.literal("text"),
|
334
|
-
text: z5.string(),
|
335
|
-
providerOptions: providerMetadataSchema.optional()
|
336
|
-
});
|
337
|
-
var imagePartSchema = z5.object({
|
338
|
-
type: z5.literal("image"),
|
339
|
-
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
340
|
-
mediaType: z5.string().optional(),
|
341
|
-
providerOptions: providerMetadataSchema.optional()
|
342
|
-
});
|
343
|
-
var filePartSchema = z5.object({
|
344
|
-
type: z5.literal("file"),
|
345
|
-
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
346
|
-
filename: z5.string().optional(),
|
347
|
-
mediaType: z5.string(),
|
348
|
-
providerOptions: providerMetadataSchema.optional()
|
349
|
-
});
|
350
|
-
var reasoningPartSchema = z5.object({
|
351
|
-
type: z5.literal("reasoning"),
|
352
|
-
text: z5.string(),
|
353
|
-
providerOptions: providerMetadataSchema.optional()
|
354
|
-
});
|
355
|
-
var toolCallPartSchema = z5.object({
|
356
|
-
type: z5.literal("tool-call"),
|
357
|
-
toolCallId: z5.string(),
|
358
|
-
toolName: z5.string(),
|
359
|
-
args: z5.unknown(),
|
360
|
-
providerOptions: providerMetadataSchema.optional()
|
361
|
-
});
|
362
|
-
var toolResultPartSchema = z5.object({
|
363
|
-
type: z5.literal("tool-result"),
|
364
|
-
toolCallId: z5.string(),
|
365
|
-
toolName: z5.string(),
|
366
|
-
result: z5.unknown(),
|
367
|
-
content: toolResultContentSchema.optional(),
|
368
|
-
isError: z5.boolean().optional(),
|
369
|
-
providerOptions: providerMetadataSchema.optional()
|
370
|
-
});
|
371
|
-
|
372
|
-
// core/prompt/message.ts
|
373
|
-
var systemModelMessageSchema = z6.object(
|
374
|
-
{
|
375
|
-
role: z6.literal("system"),
|
376
|
-
content: z6.string(),
|
377
|
-
providerOptions: providerMetadataSchema.optional()
|
222
|
+
// core/prompt/invalid-message-role-error.ts
|
223
|
+
import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
|
224
|
+
var name2 = "AI_InvalidMessageRoleError";
|
225
|
+
var marker2 = `vercel.ai.error.${name2}`;
|
226
|
+
var symbol2 = Symbol.for(marker2);
|
227
|
+
var _a2;
|
228
|
+
var InvalidMessageRoleError = class extends AISDKError3 {
|
229
|
+
constructor({
|
230
|
+
role,
|
231
|
+
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
232
|
+
}) {
|
233
|
+
super({ name: name2, message });
|
234
|
+
this[_a2] = true;
|
235
|
+
this.role = role;
|
378
236
|
}
|
379
|
-
)
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
|
385
|
-
]),
|
386
|
-
providerOptions: providerMetadataSchema.optional()
|
387
|
-
});
|
388
|
-
var assistantModelMessageSchema = z6.object({
|
389
|
-
role: z6.literal("assistant"),
|
390
|
-
content: z6.union([
|
391
|
-
z6.string(),
|
392
|
-
z6.array(
|
393
|
-
z6.union([
|
394
|
-
textPartSchema,
|
395
|
-
filePartSchema,
|
396
|
-
reasoningPartSchema,
|
397
|
-
toolCallPartSchema
|
398
|
-
])
|
399
|
-
)
|
400
|
-
]),
|
401
|
-
providerOptions: providerMetadataSchema.optional()
|
402
|
-
});
|
403
|
-
var toolModelMessageSchema = z6.object({
|
404
|
-
role: z6.literal("tool"),
|
405
|
-
content: z6.array(toolResultPartSchema),
|
406
|
-
providerOptions: providerMetadataSchema.optional()
|
407
|
-
});
|
408
|
-
var modelMessageSchema = z6.union([
|
409
|
-
systemModelMessageSchema,
|
410
|
-
userModelMessageSchema,
|
411
|
-
assistantModelMessageSchema,
|
412
|
-
toolModelMessageSchema
|
413
|
-
]);
|
237
|
+
static isInstance(error) {
|
238
|
+
return AISDKError3.hasMarker(error, marker2);
|
239
|
+
}
|
240
|
+
};
|
241
|
+
_a2 = symbol2;
|
414
242
|
|
415
|
-
// core/prompt/
|
416
|
-
async function
|
243
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
244
|
+
async function convertToLanguageModelPrompt({
|
417
245
|
prompt,
|
418
|
-
|
246
|
+
supportedUrls,
|
247
|
+
downloadImplementation = download
|
419
248
|
}) {
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
prompt,
|
442
|
-
message: "prompt must be a string"
|
443
|
-
});
|
249
|
+
const downloadedAssets = await downloadAssets(
|
250
|
+
prompt.messages,
|
251
|
+
downloadImplementation,
|
252
|
+
supportedUrls
|
253
|
+
);
|
254
|
+
return [
|
255
|
+
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
256
|
+
...prompt.messages.map(
|
257
|
+
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
258
|
+
)
|
259
|
+
];
|
260
|
+
}
|
261
|
+
function convertToLanguageModelMessage(message, downloadedAssets) {
|
262
|
+
const role = message.role;
|
263
|
+
switch (role) {
|
264
|
+
case "system": {
|
265
|
+
return {
|
266
|
+
role: "system",
|
267
|
+
content: message.content,
|
268
|
+
providerOptions: message.providerOptions
|
269
|
+
};
|
444
270
|
}
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
{
|
271
|
+
case "user": {
|
272
|
+
if (typeof message.content === "string") {
|
273
|
+
return {
|
449
274
|
role: "user",
|
450
|
-
content:
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
prompt,
|
460
|
-
message: "messages must be an array of ModelMessage or UIMessage"
|
461
|
-
});
|
462
|
-
}
|
463
|
-
const messages = promptType === "ui-messages" ? convertToModelMessages(prompt.messages, {
|
464
|
-
tools
|
465
|
-
}) : prompt.messages;
|
466
|
-
if (messages.length === 0) {
|
467
|
-
throw new InvalidPromptError({
|
468
|
-
prompt,
|
469
|
-
message: "messages must not be empty"
|
470
|
-
});
|
471
|
-
}
|
472
|
-
const validationResult = await safeValidateTypes({
|
473
|
-
value: messages,
|
474
|
-
schema: z7.array(modelMessageSchema)
|
475
|
-
});
|
476
|
-
if (!validationResult.success) {
|
477
|
-
throw new InvalidPromptError({
|
478
|
-
prompt,
|
479
|
-
message: "messages must be an array of ModelMessage or UIMessage",
|
480
|
-
cause: validationResult.error
|
481
|
-
});
|
482
|
-
}
|
483
|
-
return {
|
484
|
-
messages,
|
485
|
-
system: prompt.system
|
486
|
-
};
|
487
|
-
}
|
488
|
-
throw new Error("unreachable");
|
489
|
-
}
|
490
|
-
|
491
|
-
// core/util/index.ts
|
492
|
-
import {
|
493
|
-
asSchema,
|
494
|
-
generateId,
|
495
|
-
jsonSchema
|
496
|
-
} from "@ai-sdk/provider-utils";
|
497
|
-
|
498
|
-
// core/util/data-stream-parts.ts
|
499
|
-
var textStreamPart = {
|
500
|
-
code: "0",
|
501
|
-
name: "text",
|
502
|
-
parse: (value) => {
|
503
|
-
if (typeof value !== "string") {
|
504
|
-
throw new Error('"text" parts expect a string value.');
|
275
|
+
content: [{ type: "text", text: message.content }],
|
276
|
+
providerOptions: message.providerOptions
|
277
|
+
};
|
278
|
+
}
|
279
|
+
return {
|
280
|
+
role: "user",
|
281
|
+
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
282
|
+
providerOptions: message.providerOptions
|
283
|
+
};
|
505
284
|
}
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
285
|
+
case "assistant": {
|
286
|
+
if (typeof message.content === "string") {
|
287
|
+
return {
|
288
|
+
role: "assistant",
|
289
|
+
content: [{ type: "text", text: message.content }],
|
290
|
+
providerOptions: message.providerOptions
|
291
|
+
};
|
292
|
+
}
|
293
|
+
return {
|
294
|
+
role: "assistant",
|
295
|
+
content: message.content.filter(
|
296
|
+
// remove empty text parts:
|
297
|
+
(part) => part.type !== "text" || part.text !== ""
|
298
|
+
).map((part) => {
|
299
|
+
const providerOptions = part.providerOptions;
|
300
|
+
switch (part.type) {
|
301
|
+
case "file": {
|
302
|
+
const { data, mediaType } = convertToLanguageModelV2DataContent(
|
303
|
+
part.data
|
304
|
+
);
|
305
|
+
return {
|
306
|
+
type: "file",
|
307
|
+
data,
|
308
|
+
filename: part.filename,
|
309
|
+
mediaType: mediaType != null ? mediaType : part.mediaType,
|
310
|
+
providerOptions
|
311
|
+
};
|
312
|
+
}
|
313
|
+
case "reasoning": {
|
314
|
+
return {
|
315
|
+
type: "reasoning",
|
316
|
+
text: part.text,
|
317
|
+
providerOptions
|
318
|
+
};
|
319
|
+
}
|
320
|
+
case "text": {
|
321
|
+
return {
|
322
|
+
type: "text",
|
323
|
+
text: part.text,
|
324
|
+
providerOptions
|
325
|
+
};
|
326
|
+
}
|
327
|
+
case "tool-call": {
|
328
|
+
return {
|
329
|
+
type: "tool-call",
|
330
|
+
toolCallId: part.toolCallId,
|
331
|
+
toolName: part.toolName,
|
332
|
+
args: part.args,
|
333
|
+
providerOptions
|
334
|
+
};
|
335
|
+
}
|
336
|
+
}
|
337
|
+
}),
|
338
|
+
providerOptions: message.providerOptions
|
339
|
+
};
|
515
340
|
}
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
341
|
+
case "tool": {
|
342
|
+
return {
|
343
|
+
role: "tool",
|
344
|
+
content: message.content.map((part) => ({
|
345
|
+
type: "tool-result",
|
346
|
+
toolCallId: part.toolCallId,
|
347
|
+
toolName: part.toolName,
|
348
|
+
result: part.result,
|
349
|
+
content: part.experimental_content,
|
350
|
+
isError: part.isError,
|
351
|
+
providerOptions: part.providerOptions
|
352
|
+
})),
|
353
|
+
providerOptions: message.providerOptions
|
354
|
+
};
|
525
355
|
}
|
526
|
-
|
527
|
-
|
528
|
-
};
|
529
|
-
var messageAnnotationsStreamPart = {
|
530
|
-
code: "8",
|
531
|
-
name: "message_annotations",
|
532
|
-
parse: (value) => {
|
533
|
-
if (!Array.isArray(value)) {
|
534
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
356
|
+
default: {
|
357
|
+
const _exhaustiveCheck = role;
|
358
|
+
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
535
359
|
}
|
536
|
-
return { type: "message_annotations", value };
|
537
360
|
}
|
538
|
-
}
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
361
|
+
}
|
362
|
+
async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
363
|
+
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
364
|
+
(content) => Array.isArray(content)
|
365
|
+
).flat().filter(
|
366
|
+
(part) => part.type === "image" || part.type === "file"
|
367
|
+
).map((part) => {
|
368
|
+
var _a6;
|
369
|
+
const mediaType = (_a6 = part.mediaType) != null ? _a6 : part.type === "image" ? "image/*" : void 0;
|
370
|
+
let data = part.type === "image" ? part.image : part.data;
|
371
|
+
if (typeof data === "string") {
|
372
|
+
try {
|
373
|
+
data = new URL(data);
|
374
|
+
} catch (ignored) {
|
375
|
+
}
|
547
376
|
}
|
377
|
+
return { mediaType, data };
|
378
|
+
}).filter(
|
379
|
+
(part) => part.data instanceof URL && part.mediaType != null && !isUrlSupported({
|
380
|
+
url: part.data.toString(),
|
381
|
+
mediaType: part.mediaType,
|
382
|
+
supportedUrls
|
383
|
+
})
|
384
|
+
).map((part) => part.data);
|
385
|
+
const downloadedImages = await Promise.all(
|
386
|
+
urls.map(async (url) => ({
|
387
|
+
url,
|
388
|
+
data: await downloadImplementation({ url })
|
389
|
+
}))
|
390
|
+
);
|
391
|
+
return Object.fromEntries(
|
392
|
+
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
393
|
+
);
|
394
|
+
}
|
395
|
+
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
396
|
+
var _a6, _b;
|
397
|
+
if (part.type === "text") {
|
548
398
|
return {
|
549
|
-
type: "
|
550
|
-
|
399
|
+
type: "text",
|
400
|
+
text: part.text,
|
401
|
+
providerOptions: part.providerOptions
|
551
402
|
};
|
552
403
|
}
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
type: "tool_result",
|
565
|
-
value
|
566
|
-
};
|
404
|
+
let originalData;
|
405
|
+
const type = part.type;
|
406
|
+
switch (type) {
|
407
|
+
case "image":
|
408
|
+
originalData = part.image;
|
409
|
+
break;
|
410
|
+
case "file":
|
411
|
+
originalData = part.data;
|
412
|
+
break;
|
413
|
+
default:
|
414
|
+
throw new Error(`Unsupported part type: ${type}`);
|
567
415
|
}
|
568
|
-
};
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
if (
|
574
|
-
|
575
|
-
|
576
|
-
);
|
416
|
+
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
417
|
+
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
418
|
+
let data = convertedData;
|
419
|
+
if (data instanceof URL) {
|
420
|
+
const downloadedFile = downloadedAssets[data.toString()];
|
421
|
+
if (downloadedFile) {
|
422
|
+
data = downloadedFile.data;
|
423
|
+
mediaType = (_a6 = downloadedFile.mediaType) != null ? _a6 : mediaType;
|
577
424
|
}
|
578
|
-
return {
|
579
|
-
type: "tool_call_streaming_start",
|
580
|
-
value
|
581
|
-
};
|
582
425
|
}
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
parse: (value) => {
|
588
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
|
589
|
-
throw new Error(
|
590
|
-
'"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
|
591
|
-
);
|
592
|
-
}
|
593
|
-
return {
|
594
|
-
type: "tool_call_delta",
|
595
|
-
value
|
596
|
-
};
|
597
|
-
}
|
598
|
-
};
|
599
|
-
var finishMessageStreamPart = {
|
600
|
-
code: "d",
|
601
|
-
name: "finish_message",
|
602
|
-
parse: (value) => {
|
603
|
-
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
604
|
-
throw new Error(
|
605
|
-
'"finish_message" parts expect an object with a "finishReason" property.'
|
606
|
-
);
|
607
|
-
}
|
608
|
-
const result = {
|
609
|
-
finishReason: value.finishReason
|
610
|
-
};
|
611
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
612
|
-
result.usage = {
|
613
|
-
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
614
|
-
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
615
|
-
};
|
616
|
-
}
|
617
|
-
return {
|
618
|
-
type: "finish_message",
|
619
|
-
value: result
|
620
|
-
};
|
621
|
-
}
|
622
|
-
};
|
623
|
-
var finishStepStreamPart = {
|
624
|
-
code: "e",
|
625
|
-
name: "finish_step",
|
626
|
-
parse: (value) => {
|
627
|
-
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
628
|
-
throw new Error(
|
629
|
-
'"finish_step" parts expect an object with a "finishReason" property.'
|
630
|
-
);
|
631
|
-
}
|
632
|
-
const result = {
|
633
|
-
finishReason: value.finishReason,
|
634
|
-
isContinued: false
|
635
|
-
};
|
636
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
637
|
-
result.usage = {
|
638
|
-
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
639
|
-
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
640
|
-
};
|
641
|
-
}
|
642
|
-
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
643
|
-
result.isContinued = value.isContinued;
|
644
|
-
}
|
645
|
-
return {
|
646
|
-
type: "finish_step",
|
647
|
-
value: result
|
648
|
-
};
|
649
|
-
}
|
650
|
-
};
|
651
|
-
var startStepStreamPart = {
|
652
|
-
code: "f",
|
653
|
-
name: "start_step",
|
654
|
-
parse: (value) => {
|
655
|
-
if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
|
656
|
-
throw new Error(
|
657
|
-
'"start_step" parts expect an object with an "id" property.'
|
658
|
-
);
|
659
|
-
}
|
660
|
-
return {
|
661
|
-
type: "start_step",
|
662
|
-
value: {
|
663
|
-
messageId: value.messageId
|
426
|
+
switch (type) {
|
427
|
+
case "image": {
|
428
|
+
if (data instanceof Uint8Array || typeof data === "string") {
|
429
|
+
mediaType = (_b = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _b : mediaType;
|
664
430
|
}
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
throw new Error(
|
674
|
-
'"reasoning" parts expect an object with a "text" property.'
|
675
|
-
);
|
431
|
+
return {
|
432
|
+
type: "file",
|
433
|
+
mediaType: mediaType != null ? mediaType : "image/*",
|
434
|
+
// any image
|
435
|
+
filename: void 0,
|
436
|
+
data,
|
437
|
+
providerOptions: part.providerOptions
|
438
|
+
};
|
676
439
|
}
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
text: value.text,
|
681
|
-
providerMetadata: value.providerMetadata
|
440
|
+
case "file": {
|
441
|
+
if (mediaType == null) {
|
442
|
+
throw new Error(`Media type is missing for file part`);
|
682
443
|
}
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
if (value == null || typeof value !== "object") {
|
691
|
-
throw new Error('"source" parts expect a Source object.');
|
692
|
-
}
|
693
|
-
return {
|
694
|
-
type: "source",
|
695
|
-
value
|
696
|
-
};
|
697
|
-
}
|
698
|
-
};
|
699
|
-
var fileStreamPart = {
|
700
|
-
code: "k",
|
701
|
-
name: "file",
|
702
|
-
parse: (value) => {
|
703
|
-
if (value == null || typeof value !== "object" || !("url" in value) || typeof value.url !== "string" || !("mediaType" in value) || typeof value.mediaType !== "string") {
|
704
|
-
throw new Error(
|
705
|
-
'"file" parts expect an object with a "url" and "mediaType" property.'
|
706
|
-
);
|
444
|
+
return {
|
445
|
+
type: "file",
|
446
|
+
mediaType,
|
447
|
+
filename: part.filename,
|
448
|
+
data,
|
449
|
+
providerOptions: part.providerOptions
|
450
|
+
};
|
707
451
|
}
|
708
|
-
return { type: "file", value };
|
709
|
-
}
|
710
|
-
};
|
711
|
-
var reasoningPartFinishStreamPart = {
|
712
|
-
code: "l",
|
713
|
-
name: "reasoning_part_finish",
|
714
|
-
parse: () => {
|
715
|
-
return {
|
716
|
-
type: "reasoning_part_finish",
|
717
|
-
value: {}
|
718
|
-
};
|
719
452
|
}
|
720
|
-
};
|
721
|
-
var dataStreamParts = [
|
722
|
-
textStreamPart,
|
723
|
-
dataStreamPart,
|
724
|
-
errorStreamPart,
|
725
|
-
messageAnnotationsStreamPart,
|
726
|
-
toolCallStreamPart,
|
727
|
-
toolResultStreamPart,
|
728
|
-
toolCallStreamingStartStreamPart,
|
729
|
-
toolCallDeltaStreamPart,
|
730
|
-
finishMessageStreamPart,
|
731
|
-
finishStepStreamPart,
|
732
|
-
startStepStreamPart,
|
733
|
-
reasoningStreamPart,
|
734
|
-
sourcePart,
|
735
|
-
reasoningPartFinishStreamPart,
|
736
|
-
fileStreamPart
|
737
|
-
];
|
738
|
-
var dataStreamPartsByCode = Object.fromEntries(
|
739
|
-
dataStreamParts.map((part) => [part.code, part])
|
740
|
-
);
|
741
|
-
var DataStreamStringPrefixes = Object.fromEntries(
|
742
|
-
dataStreamParts.map((part) => [part.name, part.code])
|
743
|
-
);
|
744
|
-
var validCodes = dataStreamParts.map((part) => part.code);
|
745
|
-
function formatDataStreamPart(type, value) {
|
746
|
-
const streamPart = dataStreamParts.find((part) => part.name === type);
|
747
|
-
if (!streamPart) {
|
748
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
749
|
-
}
|
750
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
751
|
-
`;
|
752
|
-
}
|
753
|
-
|
754
|
-
// core/util/is-non-empty-object.ts
|
755
|
-
function isNonEmptyObject(object) {
|
756
|
-
return object != null && Object.keys(object).length > 0;
|
757
|
-
}
|
758
|
-
|
759
|
-
// core/prompt/prepare-tools-and-tool-choice.ts
|
760
|
-
function prepareToolsAndToolChoice({
|
761
|
-
tools,
|
762
|
-
toolChoice,
|
763
|
-
activeTools
|
764
|
-
}) {
|
765
|
-
if (!isNonEmptyObject(tools)) {
|
766
|
-
return {
|
767
|
-
tools: void 0,
|
768
|
-
toolChoice: void 0
|
769
|
-
};
|
770
|
-
}
|
771
|
-
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
772
|
-
([name6]) => activeTools.includes(name6)
|
773
|
-
) : Object.entries(tools);
|
774
|
-
return {
|
775
|
-
tools: filteredTools.map(([name6, tool]) => {
|
776
|
-
const toolType = tool.type;
|
777
|
-
switch (toolType) {
|
778
|
-
case void 0:
|
779
|
-
case "function":
|
780
|
-
return {
|
781
|
-
type: "function",
|
782
|
-
name: name6,
|
783
|
-
description: tool.description,
|
784
|
-
parameters: asSchema(tool.parameters).jsonSchema
|
785
|
-
};
|
786
|
-
case "provider-defined":
|
787
|
-
return {
|
788
|
-
type: "provider-defined",
|
789
|
-
name: name6,
|
790
|
-
id: tool.id,
|
791
|
-
args: tool.args
|
792
|
-
};
|
793
|
-
default: {
|
794
|
-
const exhaustiveCheck = toolType;
|
795
|
-
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
796
|
-
}
|
797
|
-
}
|
798
|
-
}),
|
799
|
-
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
800
|
-
};
|
801
453
|
}
|
802
454
|
|
803
455
|
// errors/invalid-argument-error.ts
|
804
|
-
import { AISDKError as
|
805
|
-
var
|
806
|
-
var
|
807
|
-
var
|
808
|
-
var
|
809
|
-
var InvalidArgumentError = class extends
|
456
|
+
import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
|
457
|
+
var name3 = "AI_InvalidArgumentError";
|
458
|
+
var marker3 = `vercel.ai.error.${name3}`;
|
459
|
+
var symbol3 = Symbol.for(marker3);
|
460
|
+
var _a3;
|
461
|
+
var InvalidArgumentError = class extends AISDKError4 {
|
810
462
|
constructor({
|
811
463
|
parameter,
|
812
464
|
value,
|
813
465
|
message
|
814
466
|
}) {
|
815
467
|
super({
|
816
|
-
name:
|
468
|
+
name: name3,
|
817
469
|
message: `Invalid argument for parameter ${parameter}: ${message}`
|
818
470
|
});
|
819
|
-
this[
|
471
|
+
this[_a3] = true;
|
820
472
|
this.parameter = parameter;
|
821
473
|
this.value = value;
|
822
474
|
}
|
823
|
-
static isInstance(error) {
|
824
|
-
return AISDKError3.hasMarker(error, marker2);
|
825
|
-
}
|
826
|
-
};
|
827
|
-
_a2 = symbol2;
|
828
|
-
|
829
|
-
// util/retry-with-exponential-backoff.ts
|
830
|
-
import { APICallError } from "@ai-sdk/provider";
|
831
|
-
import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
832
|
-
|
833
|
-
// util/retry-error.ts
|
834
|
-
import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
|
835
|
-
var name3 = "AI_RetryError";
|
836
|
-
var marker3 = `vercel.ai.error.${name3}`;
|
837
|
-
var symbol3 = Symbol.for(marker3);
|
838
|
-
var _a3;
|
839
|
-
var RetryError = class extends AISDKError4 {
|
840
|
-
constructor({
|
841
|
-
message,
|
842
|
-
reason,
|
843
|
-
errors
|
844
|
-
}) {
|
845
|
-
super({ name: name3, message });
|
846
|
-
this[_a3] = true;
|
847
|
-
this.reason = reason;
|
848
|
-
this.errors = errors;
|
849
|
-
this.lastError = errors[errors.length - 1];
|
850
|
-
}
|
851
475
|
static isInstance(error) {
|
852
476
|
return AISDKError4.hasMarker(error, marker3);
|
853
477
|
}
|
854
478
|
};
|
855
479
|
_a3 = symbol3;
|
856
480
|
|
857
|
-
// util/retry-with-exponential-backoff.ts
|
858
|
-
var retryWithExponentialBackoff = ({
|
859
|
-
maxRetries = 2,
|
860
|
-
initialDelayInMs = 2e3,
|
861
|
-
backoffFactor = 2
|
862
|
-
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
863
|
-
maxRetries,
|
864
|
-
delayInMs: initialDelayInMs,
|
865
|
-
backoffFactor
|
866
|
-
});
|
867
|
-
async function _retryWithExponentialBackoff(f, {
|
868
|
-
maxRetries,
|
869
|
-
delayInMs,
|
870
|
-
backoffFactor
|
871
|
-
}, errors = []) {
|
872
|
-
try {
|
873
|
-
return await f();
|
874
|
-
} catch (error) {
|
875
|
-
if (isAbortError(error)) {
|
876
|
-
throw error;
|
877
|
-
}
|
878
|
-
if (maxRetries === 0) {
|
879
|
-
throw error;
|
880
|
-
}
|
881
|
-
const errorMessage = getErrorMessage(error);
|
882
|
-
const newErrors = [...errors, error];
|
883
|
-
const tryNumber = newErrors.length;
|
884
|
-
if (tryNumber > maxRetries) {
|
885
|
-
throw new RetryError({
|
886
|
-
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
887
|
-
reason: "maxRetriesExceeded",
|
888
|
-
errors: newErrors
|
889
|
-
});
|
890
|
-
}
|
891
|
-
if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
892
|
-
await delay(delayInMs);
|
893
|
-
return _retryWithExponentialBackoff(
|
894
|
-
f,
|
895
|
-
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
896
|
-
newErrors
|
897
|
-
);
|
898
|
-
}
|
899
|
-
if (tryNumber === 1) {
|
900
|
-
throw error;
|
901
|
-
}
|
902
|
-
throw new RetryError({
|
903
|
-
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
904
|
-
reason: "errorNotRetryable",
|
905
|
-
errors: newErrors
|
906
|
-
});
|
907
|
-
}
|
908
|
-
}
|
909
|
-
|
910
|
-
// core/prompt/prepare-retries.ts
|
911
|
-
function prepareRetries({
|
912
|
-
maxRetries
|
913
|
-
}) {
|
914
|
-
if (maxRetries != null) {
|
915
|
-
if (!Number.isInteger(maxRetries)) {
|
916
|
-
throw new InvalidArgumentError({
|
917
|
-
parameter: "maxRetries",
|
918
|
-
value: maxRetries,
|
919
|
-
message: "maxRetries must be an integer"
|
920
|
-
});
|
921
|
-
}
|
922
|
-
if (maxRetries < 0) {
|
923
|
-
throw new InvalidArgumentError({
|
924
|
-
parameter: "maxRetries",
|
925
|
-
value: maxRetries,
|
926
|
-
message: "maxRetries must be >= 0"
|
927
|
-
});
|
928
|
-
}
|
929
|
-
}
|
930
|
-
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
931
|
-
return {
|
932
|
-
maxRetries: maxRetriesResult,
|
933
|
-
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
|
934
|
-
};
|
935
|
-
}
|
936
|
-
|
937
481
|
// core/prompt/prepare-call-settings.ts
|
938
482
|
function prepareCallSettings({
|
939
483
|
maxOutputTokens,
|
@@ -1027,28 +571,27 @@ function prepareCallSettings({
|
|
1027
571
|
};
|
1028
572
|
}
|
1029
573
|
|
1030
|
-
//
|
1031
|
-
import {
|
574
|
+
// util/retry-with-exponential-backoff.ts
|
575
|
+
import { APICallError } from "@ai-sdk/provider";
|
576
|
+
import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
1032
577
|
|
1033
|
-
// util/
|
578
|
+
// util/retry-error.ts
|
1034
579
|
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
1035
|
-
var name4 = "
|
580
|
+
var name4 = "AI_RetryError";
|
1036
581
|
var marker4 = `vercel.ai.error.${name4}`;
|
1037
582
|
var symbol4 = Symbol.for(marker4);
|
1038
583
|
var _a4;
|
1039
|
-
var
|
584
|
+
var RetryError = class extends AISDKError5 {
|
1040
585
|
constructor({
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
cause,
|
1045
|
-
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
586
|
+
message,
|
587
|
+
reason,
|
588
|
+
errors
|
1046
589
|
}) {
|
1047
|
-
super({ name: name4, message
|
590
|
+
super({ name: name4, message });
|
1048
591
|
this[_a4] = true;
|
1049
|
-
this.
|
1050
|
-
this.
|
1051
|
-
this.
|
592
|
+
this.reason = reason;
|
593
|
+
this.errors = errors;
|
594
|
+
this.lastError = errors[errors.length - 1];
|
1052
595
|
}
|
1053
596
|
static isInstance(error) {
|
1054
597
|
return AISDKError5.hasMarker(error, marker4);
|
@@ -1056,146 +599,166 @@ var DownloadError = class extends AISDKError5 {
|
|
1056
599
|
};
|
1057
600
|
_a4 = symbol4;
|
1058
601
|
|
1059
|
-
// util/
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
602
|
+
// util/retry-with-exponential-backoff.ts
|
603
|
+
var retryWithExponentialBackoff = ({
|
604
|
+
maxRetries = 2,
|
605
|
+
initialDelayInMs = 2e3,
|
606
|
+
backoffFactor = 2
|
607
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
608
|
+
maxRetries,
|
609
|
+
delayInMs: initialDelayInMs,
|
610
|
+
backoffFactor
|
611
|
+
});
|
612
|
+
async function _retryWithExponentialBackoff(f, {
|
613
|
+
maxRetries,
|
614
|
+
delayInMs,
|
615
|
+
backoffFactor
|
616
|
+
}, errors = []) {
|
1063
617
|
try {
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
618
|
+
return await f();
|
619
|
+
} catch (error) {
|
620
|
+
if (isAbortError(error)) {
|
621
|
+
throw error;
|
622
|
+
}
|
623
|
+
if (maxRetries === 0) {
|
624
|
+
throw error;
|
625
|
+
}
|
626
|
+
const errorMessage = getErrorMessage(error);
|
627
|
+
const newErrors = [...errors, error];
|
628
|
+
const tryNumber = newErrors.length;
|
629
|
+
if (tryNumber > maxRetries) {
|
630
|
+
throw new RetryError({
|
631
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
632
|
+
reason: "maxRetriesExceeded",
|
633
|
+
errors: newErrors
|
1070
634
|
});
|
1071
635
|
}
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
636
|
+
if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
637
|
+
await delay(delayInMs);
|
638
|
+
return _retryWithExponentialBackoff(
|
639
|
+
f,
|
640
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
641
|
+
newErrors
|
642
|
+
);
|
643
|
+
}
|
644
|
+
if (tryNumber === 1) {
|
1078
645
|
throw error;
|
1079
646
|
}
|
1080
|
-
throw new
|
647
|
+
throw new RetryError({
|
648
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
649
|
+
reason: "errorNotRetryable",
|
650
|
+
errors: newErrors
|
651
|
+
});
|
1081
652
|
}
|
1082
653
|
}
|
1083
654
|
|
1084
|
-
// core/
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
mediaType: "image/webp",
|
1104
|
-
bytesPrefix: [82, 73, 70, 70],
|
1105
|
-
base64Prefix: "UklGRg"
|
1106
|
-
},
|
1107
|
-
{
|
1108
|
-
mediaType: "image/bmp",
|
1109
|
-
bytesPrefix: [66, 77],
|
1110
|
-
base64Prefix: "Qk"
|
1111
|
-
},
|
1112
|
-
{
|
1113
|
-
mediaType: "image/tiff",
|
1114
|
-
bytesPrefix: [73, 73, 42, 0],
|
1115
|
-
base64Prefix: "SUkqAA"
|
1116
|
-
},
|
1117
|
-
{
|
1118
|
-
mediaType: "image/tiff",
|
1119
|
-
bytesPrefix: [77, 77, 0, 42],
|
1120
|
-
base64Prefix: "TU0AKg"
|
1121
|
-
},
|
1122
|
-
{
|
1123
|
-
mediaType: "image/avif",
|
1124
|
-
bytesPrefix: [
|
1125
|
-
0,
|
1126
|
-
0,
|
1127
|
-
0,
|
1128
|
-
32,
|
1129
|
-
102,
|
1130
|
-
116,
|
1131
|
-
121,
|
1132
|
-
112,
|
1133
|
-
97,
|
1134
|
-
118,
|
1135
|
-
105,
|
1136
|
-
102
|
1137
|
-
],
|
1138
|
-
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
1139
|
-
},
|
1140
|
-
{
|
1141
|
-
mediaType: "image/heic",
|
1142
|
-
bytesPrefix: [
|
1143
|
-
0,
|
1144
|
-
0,
|
1145
|
-
0,
|
1146
|
-
32,
|
1147
|
-
102,
|
1148
|
-
116,
|
1149
|
-
121,
|
1150
|
-
112,
|
1151
|
-
104,
|
1152
|
-
101,
|
1153
|
-
105,
|
1154
|
-
99
|
1155
|
-
],
|
1156
|
-
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
655
|
+
// core/prompt/prepare-retries.ts
|
656
|
+
function prepareRetries({
|
657
|
+
maxRetries
|
658
|
+
}) {
|
659
|
+
if (maxRetries != null) {
|
660
|
+
if (!Number.isInteger(maxRetries)) {
|
661
|
+
throw new InvalidArgumentError({
|
662
|
+
parameter: "maxRetries",
|
663
|
+
value: maxRetries,
|
664
|
+
message: "maxRetries must be an integer"
|
665
|
+
});
|
666
|
+
}
|
667
|
+
if (maxRetries < 0) {
|
668
|
+
throw new InvalidArgumentError({
|
669
|
+
parameter: "maxRetries",
|
670
|
+
value: maxRetries,
|
671
|
+
message: "maxRetries must be >= 0"
|
672
|
+
});
|
673
|
+
}
|
1157
674
|
}
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
};
|
1164
|
-
function stripID3TagsIfPresent(data) {
|
1165
|
-
const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
|
1166
|
-
data[1] === 68 && // 'D'
|
1167
|
-
data[2] === 51;
|
1168
|
-
return hasId3 ? stripID3(data) : data;
|
675
|
+
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
676
|
+
return {
|
677
|
+
maxRetries: maxRetriesResult,
|
678
|
+
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
|
679
|
+
};
|
1169
680
|
}
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
681
|
+
|
682
|
+
// core/util/index.ts
|
683
|
+
import {
|
684
|
+
asSchema,
|
685
|
+
generateId,
|
686
|
+
jsonSchema
|
687
|
+
} from "@ai-sdk/provider-utils";
|
688
|
+
|
689
|
+
// core/util/is-non-empty-object.ts
|
690
|
+
function isNonEmptyObject(object) {
|
691
|
+
return object != null && Object.keys(object).length > 0;
|
692
|
+
}
|
693
|
+
|
694
|
+
// core/prompt/prepare-tools-and-tool-choice.ts
|
695
|
+
function prepareToolsAndToolChoice({
|
696
|
+
tools,
|
697
|
+
toolChoice,
|
698
|
+
activeTools
|
1173
699
|
}) {
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
return signature.mediaType;
|
1180
|
-
}
|
700
|
+
if (!isNonEmptyObject(tools)) {
|
701
|
+
return {
|
702
|
+
tools: void 0,
|
703
|
+
toolChoice: void 0
|
704
|
+
};
|
1181
705
|
}
|
1182
|
-
|
706
|
+
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
707
|
+
([name6]) => activeTools.includes(name6)
|
708
|
+
) : Object.entries(tools);
|
709
|
+
return {
|
710
|
+
tools: filteredTools.map(([name6, tool]) => {
|
711
|
+
const toolType = tool.type;
|
712
|
+
switch (toolType) {
|
713
|
+
case void 0:
|
714
|
+
case "function":
|
715
|
+
return {
|
716
|
+
type: "function",
|
717
|
+
name: name6,
|
718
|
+
description: tool.description,
|
719
|
+
parameters: asSchema(tool.parameters).jsonSchema
|
720
|
+
};
|
721
|
+
case "provider-defined":
|
722
|
+
return {
|
723
|
+
type: "provider-defined",
|
724
|
+
name: name6,
|
725
|
+
id: tool.id,
|
726
|
+
args: tool.args
|
727
|
+
};
|
728
|
+
default: {
|
729
|
+
const exhaustiveCheck = toolType;
|
730
|
+
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
731
|
+
}
|
732
|
+
}
|
733
|
+
}),
|
734
|
+
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
735
|
+
};
|
1183
736
|
}
|
1184
737
|
|
1185
|
-
// core/prompt/
|
738
|
+
// core/prompt/standardize-prompt.ts
|
739
|
+
import { InvalidPromptError } from "@ai-sdk/provider";
|
740
|
+
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
741
|
+
import { z as z7 } from "zod";
|
742
|
+
|
743
|
+
// core/ui/get-ui-text.ts
|
744
|
+
function getUIText(parts) {
|
745
|
+
return parts.map((part) => part.type === "text" ? part.text : "").join("");
|
746
|
+
}
|
747
|
+
|
748
|
+
// core/prompt/message-conversion-error.ts
|
1186
749
|
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
|
1187
|
-
var name5 = "
|
750
|
+
var name5 = "AI_MessageConversionError";
|
1188
751
|
var marker5 = `vercel.ai.error.${name5}`;
|
1189
752
|
var symbol5 = Symbol.for(marker5);
|
1190
753
|
var _a5;
|
1191
|
-
var
|
754
|
+
var MessageConversionError = class extends AISDKError6 {
|
1192
755
|
constructor({
|
1193
|
-
|
1194
|
-
message
|
756
|
+
originalMessage,
|
757
|
+
message
|
1195
758
|
}) {
|
1196
759
|
super({ name: name5, message });
|
1197
760
|
this[_a5] = true;
|
1198
|
-
this.
|
761
|
+
this.originalMessage = originalMessage;
|
1199
762
|
}
|
1200
763
|
static isInstance(error) {
|
1201
764
|
return AISDKError6.hasMarker(error, marker5);
|
@@ -1203,336 +766,400 @@ var InvalidMessageRoleError = class extends AISDKError6 {
|
|
1203
766
|
};
|
1204
767
|
_a5 = symbol5;
|
1205
768
|
|
1206
|
-
// core/prompt/convert-to-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
...prompt.messages.map(
|
1220
|
-
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
1221
|
-
)
|
1222
|
-
];
|
1223
|
-
}
|
1224
|
-
function convertToLanguageModelMessage(message, downloadedAssets) {
|
1225
|
-
const role = message.role;
|
1226
|
-
switch (role) {
|
1227
|
-
case "system": {
|
1228
|
-
return {
|
1229
|
-
role: "system",
|
1230
|
-
content: message.content,
|
1231
|
-
providerOptions: message.providerOptions
|
1232
|
-
};
|
1233
|
-
}
|
1234
|
-
case "user": {
|
1235
|
-
if (typeof message.content === "string") {
|
1236
|
-
return {
|
1237
|
-
role: "user",
|
1238
|
-
content: [{ type: "text", text: message.content }],
|
1239
|
-
providerOptions: message.providerOptions
|
1240
|
-
};
|
769
|
+
// core/prompt/convert-to-model-messages.ts
|
770
|
+
function convertToModelMessages(messages, options) {
|
771
|
+
var _a6, _b;
|
772
|
+
const tools = (_a6 = options == null ? void 0 : options.tools) != null ? _a6 : {};
|
773
|
+
const modelMessages = [];
|
774
|
+
for (const message of messages) {
|
775
|
+
switch (message.role) {
|
776
|
+
case "system": {
|
777
|
+
modelMessages.push({
|
778
|
+
role: "system",
|
779
|
+
content: getUIText(message.parts)
|
780
|
+
});
|
781
|
+
break;
|
1241
782
|
}
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
783
|
+
case "user": {
|
784
|
+
modelMessages.push({
|
785
|
+
role: "user",
|
786
|
+
content: message.parts.filter(
|
787
|
+
(part) => part.type === "text" || part.type === "file"
|
788
|
+
).map(
|
789
|
+
(part) => part.type === "file" ? {
|
790
|
+
type: "file",
|
791
|
+
mediaType: part.mediaType,
|
792
|
+
filename: part.filename,
|
793
|
+
data: part.url
|
794
|
+
} : part
|
795
|
+
)
|
796
|
+
});
|
797
|
+
break;
|
1255
798
|
}
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
799
|
+
case "assistant": {
|
800
|
+
if (message.parts != null) {
|
801
|
+
let processBlock2 = function() {
|
802
|
+
const content = [];
|
803
|
+
for (const part of block) {
|
804
|
+
switch (part.type) {
|
805
|
+
case "text": {
|
806
|
+
content.push(part);
|
807
|
+
break;
|
808
|
+
}
|
809
|
+
case "file": {
|
810
|
+
content.push({
|
811
|
+
type: "file",
|
812
|
+
mediaType: part.mediaType,
|
813
|
+
data: part.url
|
814
|
+
});
|
815
|
+
break;
|
816
|
+
}
|
817
|
+
case "reasoning": {
|
818
|
+
content.push({
|
819
|
+
type: "reasoning",
|
820
|
+
text: part.text,
|
821
|
+
providerOptions: part.providerMetadata
|
822
|
+
});
|
823
|
+
break;
|
824
|
+
}
|
825
|
+
case "tool-invocation":
|
826
|
+
content.push({
|
827
|
+
type: "tool-call",
|
828
|
+
toolCallId: part.toolInvocation.toolCallId,
|
829
|
+
toolName: part.toolInvocation.toolName,
|
830
|
+
args: part.toolInvocation.args
|
831
|
+
});
|
832
|
+
break;
|
833
|
+
default: {
|
834
|
+
const _exhaustiveCheck = part;
|
835
|
+
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
836
|
+
}
|
837
|
+
}
|
1282
838
|
}
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
839
|
+
modelMessages.push({
|
840
|
+
role: "assistant",
|
841
|
+
content
|
842
|
+
});
|
843
|
+
const stepInvocations = block.filter(
|
844
|
+
(part) => part.type === "tool-invocation"
|
845
|
+
).map((part) => part.toolInvocation);
|
846
|
+
if (stepInvocations.length > 0) {
|
847
|
+
modelMessages.push({
|
848
|
+
role: "tool",
|
849
|
+
content: stepInvocations.map(
|
850
|
+
(toolInvocation) => {
|
851
|
+
if (!("result" in toolInvocation)) {
|
852
|
+
throw new MessageConversionError({
|
853
|
+
originalMessage: message,
|
854
|
+
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
855
|
+
});
|
856
|
+
}
|
857
|
+
const { toolCallId, toolName, result } = toolInvocation;
|
858
|
+
const tool = tools[toolName];
|
859
|
+
return (tool == null ? void 0 : tool.experimental_toToolResultContent) != null ? {
|
860
|
+
type: "tool-result",
|
861
|
+
toolCallId,
|
862
|
+
toolName,
|
863
|
+
result: tool.experimental_toToolResultContent(result),
|
864
|
+
experimental_content: tool.experimental_toToolResultContent(result)
|
865
|
+
} : {
|
866
|
+
type: "tool-result",
|
867
|
+
toolCallId,
|
868
|
+
toolName,
|
869
|
+
result
|
870
|
+
};
|
871
|
+
}
|
872
|
+
)
|
873
|
+
});
|
1289
874
|
}
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
875
|
+
block = [];
|
876
|
+
blockHasToolInvocations = false;
|
877
|
+
currentStep++;
|
878
|
+
};
|
879
|
+
var processBlock = processBlock2;
|
880
|
+
let currentStep = 0;
|
881
|
+
let blockHasToolInvocations = false;
|
882
|
+
let block = [];
|
883
|
+
for (const part of message.parts) {
|
884
|
+
switch (part.type) {
|
885
|
+
case "text": {
|
886
|
+
if (blockHasToolInvocations) {
|
887
|
+
processBlock2();
|
888
|
+
}
|
889
|
+
block.push(part);
|
890
|
+
break;
|
891
|
+
}
|
892
|
+
case "file":
|
893
|
+
case "reasoning": {
|
894
|
+
block.push(part);
|
895
|
+
break;
|
896
|
+
}
|
897
|
+
case "tool-invocation": {
|
898
|
+
if (((_b = part.toolInvocation.step) != null ? _b : 0) !== currentStep) {
|
899
|
+
processBlock2();
|
900
|
+
}
|
901
|
+
block.push(part);
|
902
|
+
blockHasToolInvocations = true;
|
903
|
+
break;
|
904
|
+
}
|
1298
905
|
}
|
1299
906
|
}
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
case "tool": {
|
1305
|
-
return {
|
1306
|
-
role: "tool",
|
1307
|
-
content: message.content.map((part) => ({
|
1308
|
-
type: "tool-result",
|
1309
|
-
toolCallId: part.toolCallId,
|
1310
|
-
toolName: part.toolName,
|
1311
|
-
result: part.result,
|
1312
|
-
content: part.experimental_content,
|
1313
|
-
isError: part.isError,
|
1314
|
-
providerOptions: part.providerOptions
|
1315
|
-
})),
|
1316
|
-
providerOptions: message.providerOptions
|
1317
|
-
};
|
1318
|
-
}
|
1319
|
-
default: {
|
1320
|
-
const _exhaustiveCheck = role;
|
1321
|
-
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
1322
|
-
}
|
1323
|
-
}
|
1324
|
-
}
|
1325
|
-
async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
1326
|
-
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
1327
|
-
(content) => Array.isArray(content)
|
1328
|
-
).flat().filter(
|
1329
|
-
(part) => part.type === "image" || part.type === "file"
|
1330
|
-
).map((part) => {
|
1331
|
-
var _a6;
|
1332
|
-
const mediaType = (_a6 = part.mediaType) != null ? _a6 : part.type === "image" ? "image/*" : void 0;
|
1333
|
-
let data = part.type === "image" ? part.image : part.data;
|
1334
|
-
if (typeof data === "string") {
|
1335
|
-
try {
|
1336
|
-
data = new URL(data);
|
1337
|
-
} catch (ignored) {
|
1338
|
-
}
|
1339
|
-
}
|
1340
|
-
return { mediaType, data };
|
1341
|
-
}).filter(
|
1342
|
-
(part) => part.data instanceof URL && part.mediaType != null && !isUrlSupported({
|
1343
|
-
url: part.data.toString(),
|
1344
|
-
mediaType: part.mediaType,
|
1345
|
-
supportedUrls
|
1346
|
-
})
|
1347
|
-
).map((part) => part.data);
|
1348
|
-
const downloadedImages = await Promise.all(
|
1349
|
-
urls.map(async (url) => ({
|
1350
|
-
url,
|
1351
|
-
data: await downloadImplementation({ url })
|
1352
|
-
}))
|
1353
|
-
);
|
1354
|
-
return Object.fromEntries(
|
1355
|
-
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
1356
|
-
);
|
1357
|
-
}
|
1358
|
-
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1359
|
-
var _a6, _b;
|
1360
|
-
if (part.type === "text") {
|
1361
|
-
return {
|
1362
|
-
type: "text",
|
1363
|
-
text: part.text,
|
1364
|
-
providerOptions: part.providerOptions
|
1365
|
-
};
|
1366
|
-
}
|
1367
|
-
let originalData;
|
1368
|
-
const type = part.type;
|
1369
|
-
switch (type) {
|
1370
|
-
case "image":
|
1371
|
-
originalData = part.image;
|
1372
|
-
break;
|
1373
|
-
case "file":
|
1374
|
-
originalData = part.data;
|
1375
|
-
break;
|
1376
|
-
default:
|
1377
|
-
throw new Error(`Unsupported part type: ${type}`);
|
1378
|
-
}
|
1379
|
-
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
1380
|
-
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
1381
|
-
let data = convertedData;
|
1382
|
-
if (data instanceof URL) {
|
1383
|
-
const downloadedFile = downloadedAssets[data.toString()];
|
1384
|
-
if (downloadedFile) {
|
1385
|
-
data = downloadedFile.data;
|
1386
|
-
mediaType = (_a6 = downloadedFile.mediaType) != null ? _a6 : mediaType;
|
1387
|
-
}
|
1388
|
-
}
|
1389
|
-
switch (type) {
|
1390
|
-
case "image": {
|
1391
|
-
if (data instanceof Uint8Array || typeof data === "string") {
|
1392
|
-
mediaType = (_b = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _b : mediaType;
|
907
|
+
processBlock2();
|
908
|
+
break;
|
909
|
+
}
|
910
|
+
break;
|
1393
911
|
}
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
providerOptions: part.providerOptions
|
1401
|
-
};
|
1402
|
-
}
|
1403
|
-
case "file": {
|
1404
|
-
if (mediaType == null) {
|
1405
|
-
throw new Error(`Media type is missing for file part`);
|
912
|
+
default: {
|
913
|
+
const _exhaustiveCheck = message.role;
|
914
|
+
throw new MessageConversionError({
|
915
|
+
originalMessage: message,
|
916
|
+
message: `Unsupported role: ${_exhaustiveCheck}`
|
917
|
+
});
|
1406
918
|
}
|
1407
|
-
return {
|
1408
|
-
type: "file",
|
1409
|
-
mediaType,
|
1410
|
-
filename: part.filename,
|
1411
|
-
data,
|
1412
|
-
providerOptions: part.providerOptions
|
1413
|
-
};
|
1414
919
|
}
|
1415
920
|
}
|
921
|
+
return modelMessages;
|
922
|
+
}
|
923
|
+
|
924
|
+
// core/prompt/detect-prompt-type.ts
|
925
|
+
function detectPromptType(prompt) {
|
926
|
+
if (!Array.isArray(prompt)) {
|
927
|
+
return "other";
|
928
|
+
}
|
929
|
+
if (prompt.length === 0) {
|
930
|
+
return "model-messages";
|
931
|
+
}
|
932
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
933
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
934
|
+
return "ui-messages";
|
935
|
+
} else if (characteristics.every(
|
936
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
937
|
+
)) {
|
938
|
+
return "model-messages";
|
939
|
+
} else {
|
940
|
+
return "other";
|
941
|
+
}
|
1416
942
|
}
|
1417
|
-
|
1418
|
-
//
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
}
|
943
|
+
function detectSingleMessageCharacteristics(message) {
|
944
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
945
|
+
message.role === "data" || // UI-only role
|
946
|
+
"toolInvocations" in message || // UI-specific field
|
947
|
+
"parts" in message || // UI-specific field
|
948
|
+
"experimental_attachments" in message)) {
|
949
|
+
return "has-ui-specific-parts";
|
950
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
951
|
+
"providerOptions" in message)) {
|
952
|
+
return "has-core-specific-parts";
|
953
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
954
|
+
return "message";
|
955
|
+
} else {
|
956
|
+
return "other";
|
957
|
+
}
|
1428
958
|
}
|
1429
959
|
|
1430
|
-
// core/
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
960
|
+
// core/prompt/message.ts
|
961
|
+
import { z as z6 } from "zod";
|
962
|
+
|
963
|
+
// core/types/provider-metadata.ts
|
964
|
+
import { z as z3 } from "zod";
|
965
|
+
|
966
|
+
// core/types/json-value.ts
|
967
|
+
import { z as z2 } from "zod";
|
968
|
+
var jsonValueSchema = z2.lazy(
|
969
|
+
() => z2.union([
|
970
|
+
z2.null(),
|
971
|
+
z2.string(),
|
972
|
+
z2.number(),
|
973
|
+
z2.boolean(),
|
974
|
+
z2.record(z2.string(), jsonValueSchema),
|
975
|
+
z2.array(jsonValueSchema)
|
976
|
+
])
|
977
|
+
);
|
978
|
+
|
979
|
+
// core/types/provider-metadata.ts
|
980
|
+
var providerMetadataSchema = z3.record(
|
981
|
+
z3.string(),
|
982
|
+
z3.record(z3.string(), jsonValueSchema)
|
983
|
+
);
|
984
|
+
|
985
|
+
// core/prompt/content-part.ts
|
986
|
+
import { z as z5 } from "zod";
|
987
|
+
|
988
|
+
// core/prompt/tool-result-content.ts
|
989
|
+
import { z as z4 } from "zod";
|
990
|
+
var toolResultContentSchema = z4.array(
|
991
|
+
z4.union([
|
992
|
+
z4.object({ type: z4.literal("text"), text: z4.string() }),
|
993
|
+
z4.object({
|
994
|
+
type: z4.literal("image"),
|
995
|
+
data: z4.string(),
|
996
|
+
mediaType: z4.string().optional()
|
997
|
+
})
|
998
|
+
])
|
999
|
+
);
|
1000
|
+
|
1001
|
+
// core/prompt/content-part.ts
|
1002
|
+
var textPartSchema = z5.object({
|
1003
|
+
type: z5.literal("text"),
|
1004
|
+
text: z5.string(),
|
1005
|
+
providerOptions: providerMetadataSchema.optional()
|
1006
|
+
});
|
1007
|
+
var imagePartSchema = z5.object({
|
1008
|
+
type: z5.literal("image"),
|
1009
|
+
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
1010
|
+
mediaType: z5.string().optional(),
|
1011
|
+
providerOptions: providerMetadataSchema.optional()
|
1012
|
+
});
|
1013
|
+
var filePartSchema = z5.object({
|
1014
|
+
type: z5.literal("file"),
|
1015
|
+
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
1016
|
+
filename: z5.string().optional(),
|
1017
|
+
mediaType: z5.string(),
|
1018
|
+
providerOptions: providerMetadataSchema.optional()
|
1019
|
+
});
|
1020
|
+
var reasoningPartSchema = z5.object({
|
1021
|
+
type: z5.literal("reasoning"),
|
1022
|
+
text: z5.string(),
|
1023
|
+
providerOptions: providerMetadataSchema.optional()
|
1024
|
+
});
|
1025
|
+
var toolCallPartSchema = z5.object({
|
1026
|
+
type: z5.literal("tool-call"),
|
1027
|
+
toolCallId: z5.string(),
|
1028
|
+
toolName: z5.string(),
|
1029
|
+
args: z5.unknown(),
|
1030
|
+
providerOptions: providerMetadataSchema.optional()
|
1031
|
+
});
|
1032
|
+
var toolResultPartSchema = z5.object({
|
1033
|
+
type: z5.literal("tool-result"),
|
1034
|
+
toolCallId: z5.string(),
|
1035
|
+
toolName: z5.string(),
|
1036
|
+
result: z5.unknown(),
|
1037
|
+
content: toolResultContentSchema.optional(),
|
1038
|
+
isError: z5.boolean().optional(),
|
1039
|
+
providerOptions: providerMetadataSchema.optional()
|
1040
|
+
});
|
1041
|
+
|
1042
|
+
// core/prompt/message.ts
|
1043
|
+
var systemModelMessageSchema = z6.object(
|
1044
|
+
{
|
1045
|
+
role: z6.literal("system"),
|
1046
|
+
content: z6.string(),
|
1047
|
+
providerOptions: providerMetadataSchema.optional()
|
1048
|
+
}
|
1049
|
+
);
|
1050
|
+
var userModelMessageSchema = z6.object({
|
1051
|
+
role: z6.literal("user"),
|
1052
|
+
content: z6.union([
|
1053
|
+
z6.string(),
|
1054
|
+
z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
|
1055
|
+
]),
|
1056
|
+
providerOptions: providerMetadataSchema.optional()
|
1057
|
+
});
|
1058
|
+
var assistantModelMessageSchema = z6.object({
|
1059
|
+
role: z6.literal("assistant"),
|
1060
|
+
content: z6.union([
|
1061
|
+
z6.string(),
|
1062
|
+
z6.array(
|
1063
|
+
z6.union([
|
1064
|
+
textPartSchema,
|
1065
|
+
filePartSchema,
|
1066
|
+
reasoningPartSchema,
|
1067
|
+
toolCallPartSchema
|
1068
|
+
])
|
1069
|
+
)
|
1070
|
+
]),
|
1071
|
+
providerOptions: providerMetadataSchema.optional()
|
1072
|
+
});
|
1073
|
+
var toolModelMessageSchema = z6.object({
|
1074
|
+
role: z6.literal("tool"),
|
1075
|
+
content: z6.array(toolResultPartSchema),
|
1076
|
+
providerOptions: providerMetadataSchema.optional()
|
1077
|
+
});
|
1078
|
+
var modelMessageSchema = z6.union([
|
1079
|
+
systemModelMessageSchema,
|
1080
|
+
userModelMessageSchema,
|
1081
|
+
assistantModelMessageSchema,
|
1082
|
+
toolModelMessageSchema
|
1083
|
+
]);
|
1084
|
+
|
1085
|
+
// core/prompt/standardize-prompt.ts
|
1086
|
+
async function standardizePrompt({
|
1087
|
+
prompt,
|
1088
|
+
tools
|
1434
1089
|
}) {
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1090
|
+
if (prompt.prompt == null && prompt.messages == null) {
|
1091
|
+
throw new InvalidPromptError({
|
1092
|
+
prompt,
|
1093
|
+
message: "prompt or messages must be defined"
|
1094
|
+
});
|
1438
1095
|
}
|
1439
|
-
if (
|
1440
|
-
|
1096
|
+
if (prompt.prompt != null && prompt.messages != null) {
|
1097
|
+
throw new InvalidPromptError({
|
1098
|
+
prompt,
|
1099
|
+
message: "prompt and messages cannot be defined at the same time"
|
1100
|
+
});
|
1441
1101
|
}
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
const reader1 = stream1.getReader();
|
1448
|
-
const reader2 = stream2.getReader();
|
1449
|
-
let lastRead1 = void 0;
|
1450
|
-
let lastRead2 = void 0;
|
1451
|
-
let stream1Done = false;
|
1452
|
-
let stream2Done = false;
|
1453
|
-
async function readStream1(controller) {
|
1454
|
-
try {
|
1455
|
-
if (lastRead1 == null) {
|
1456
|
-
lastRead1 = reader1.read();
|
1457
|
-
}
|
1458
|
-
const result = await lastRead1;
|
1459
|
-
lastRead1 = void 0;
|
1460
|
-
if (!result.done) {
|
1461
|
-
controller.enqueue(result.value);
|
1462
|
-
} else {
|
1463
|
-
controller.close();
|
1464
|
-
}
|
1465
|
-
} catch (error) {
|
1466
|
-
controller.error(error);
|
1467
|
-
}
|
1102
|
+
if (prompt.system != null && typeof prompt.system !== "string") {
|
1103
|
+
throw new InvalidPromptError({
|
1104
|
+
prompt,
|
1105
|
+
message: "system must be a string"
|
1106
|
+
});
|
1468
1107
|
}
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
lastRead2 = void 0;
|
1476
|
-
if (!result.done) {
|
1477
|
-
controller.enqueue(result.value);
|
1478
|
-
} else {
|
1479
|
-
controller.close();
|
1480
|
-
}
|
1481
|
-
} catch (error) {
|
1482
|
-
controller.error(error);
|
1108
|
+
if (prompt.prompt != null) {
|
1109
|
+
if (typeof prompt.prompt !== "string") {
|
1110
|
+
throw new InvalidPromptError({
|
1111
|
+
prompt,
|
1112
|
+
message: "prompt must be a string"
|
1113
|
+
});
|
1483
1114
|
}
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
return;
|
1491
|
-
}
|
1492
|
-
if (stream2Done) {
|
1493
|
-
await readStream1(controller);
|
1494
|
-
return;
|
1495
|
-
}
|
1496
|
-
if (lastRead1 == null) {
|
1497
|
-
lastRead1 = reader1.read();
|
1498
|
-
}
|
1499
|
-
if (lastRead2 == null) {
|
1500
|
-
lastRead2 = reader2.read();
|
1501
|
-
}
|
1502
|
-
const { result, reader } = await Promise.race([
|
1503
|
-
lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
|
1504
|
-
lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
|
1505
|
-
]);
|
1506
|
-
if (!result.done) {
|
1507
|
-
controller.enqueue(result.value);
|
1508
|
-
}
|
1509
|
-
if (reader === reader1) {
|
1510
|
-
lastRead1 = void 0;
|
1511
|
-
if (result.done) {
|
1512
|
-
await readStream2(controller);
|
1513
|
-
stream1Done = true;
|
1514
|
-
}
|
1515
|
-
} else {
|
1516
|
-
lastRead2 = void 0;
|
1517
|
-
if (result.done) {
|
1518
|
-
stream2Done = true;
|
1519
|
-
await readStream1(controller);
|
1520
|
-
}
|
1115
|
+
return {
|
1116
|
+
system: prompt.system,
|
1117
|
+
messages: [
|
1118
|
+
{
|
1119
|
+
role: "user",
|
1120
|
+
content: prompt.prompt
|
1521
1121
|
}
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1122
|
+
]
|
1123
|
+
};
|
1124
|
+
}
|
1125
|
+
if (prompt.messages != null) {
|
1126
|
+
const promptType = detectPromptType(prompt.messages);
|
1127
|
+
if (promptType === "other") {
|
1128
|
+
throw new InvalidPromptError({
|
1129
|
+
prompt,
|
1130
|
+
message: "messages must be an array of ModelMessage or UIMessage"
|
1131
|
+
});
|
1529
1132
|
}
|
1530
|
-
|
1133
|
+
const messages = promptType === "ui-messages" ? convertToModelMessages(prompt.messages, {
|
1134
|
+
tools
|
1135
|
+
}) : prompt.messages;
|
1136
|
+
if (messages.length === 0) {
|
1137
|
+
throw new InvalidPromptError({
|
1138
|
+
prompt,
|
1139
|
+
message: "messages must not be empty"
|
1140
|
+
});
|
1141
|
+
}
|
1142
|
+
const validationResult = await safeValidateTypes({
|
1143
|
+
value: messages,
|
1144
|
+
schema: z7.array(modelMessageSchema)
|
1145
|
+
});
|
1146
|
+
if (!validationResult.success) {
|
1147
|
+
throw new InvalidPromptError({
|
1148
|
+
prompt,
|
1149
|
+
message: "messages must be an array of ModelMessage or UIMessage",
|
1150
|
+
cause: validationResult.error
|
1151
|
+
});
|
1152
|
+
}
|
1153
|
+
return {
|
1154
|
+
messages,
|
1155
|
+
system: prompt.system
|
1156
|
+
};
|
1157
|
+
}
|
1158
|
+
throw new Error("unreachable");
|
1531
1159
|
}
|
1532
1160
|
|
1533
1161
|
// streams/stream-callbacks.ts
|
1534
1162
|
function createCallbacksTransformer(callbacks = {}) {
|
1535
|
-
const textEncoder = new TextEncoder();
|
1536
1163
|
let aggregatedResponse = "";
|
1537
1164
|
return new TransformStream({
|
1538
1165
|
async start() {
|
@@ -1540,7 +1167,7 @@ function createCallbacksTransformer(callbacks = {}) {
|
|
1540
1167
|
await callbacks.onStart();
|
1541
1168
|
},
|
1542
1169
|
async transform(message, controller) {
|
1543
|
-
controller.enqueue(
|
1170
|
+
controller.enqueue(message);
|
1544
1171
|
aggregatedResponse += message;
|
1545
1172
|
if (callbacks.onToken)
|
1546
1173
|
await callbacks.onToken(message);
|
@@ -1549,9 +1176,6 @@ function createCallbacksTransformer(callbacks = {}) {
|
|
1549
1176
|
}
|
1550
1177
|
},
|
1551
1178
|
async flush() {
|
1552
|
-
if (callbacks.onCompletion) {
|
1553
|
-
await callbacks.onCompletion(aggregatedResponse);
|
1554
|
-
}
|
1555
1179
|
if (callbacks.onFinal) {
|
1556
1180
|
await callbacks.onFinal(aggregatedResponse);
|
1557
1181
|
}
|
@@ -1561,79 +1185,11 @@ function createCallbacksTransformer(callbacks = {}) {
|
|
1561
1185
|
|
1562
1186
|
// util/constants.ts
|
1563
1187
|
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
1564
|
-
|
1565
|
-
// streams/stream-data.ts
|
1566
|
-
var StreamData = class {
|
1567
|
-
constructor() {
|
1568
|
-
this.encoder = new TextEncoder();
|
1569
|
-
this.controller = null;
|
1570
|
-
this.isClosed = false;
|
1571
|
-
this.warningTimeout = null;
|
1572
|
-
const self = this;
|
1573
|
-
this.stream = new ReadableStream({
|
1574
|
-
start: async (controller) => {
|
1575
|
-
self.controller = controller;
|
1576
|
-
if (process.env.NODE_ENV === "development") {
|
1577
|
-
self.warningTimeout = setTimeout(() => {
|
1578
|
-
console.warn(
|
1579
|
-
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
1580
|
-
);
|
1581
|
-
}, HANGING_STREAM_WARNING_TIME_MS);
|
1582
|
-
}
|
1583
|
-
},
|
1584
|
-
pull: (controller) => {
|
1585
|
-
},
|
1586
|
-
cancel: (reason) => {
|
1587
|
-
this.isClosed = true;
|
1588
|
-
}
|
1589
|
-
});
|
1590
|
-
}
|
1591
|
-
async close() {
|
1592
|
-
if (this.isClosed) {
|
1593
|
-
throw new Error("Data Stream has already been closed.");
|
1594
|
-
}
|
1595
|
-
if (!this.controller) {
|
1596
|
-
throw new Error("Stream controller is not initialized.");
|
1597
|
-
}
|
1598
|
-
this.controller.close();
|
1599
|
-
this.isClosed = true;
|
1600
|
-
if (this.warningTimeout) {
|
1601
|
-
clearTimeout(this.warningTimeout);
|
1602
|
-
}
|
1603
|
-
}
|
1604
|
-
append(value) {
|
1605
|
-
if (this.isClosed) {
|
1606
|
-
throw new Error("Data Stream has already been closed.");
|
1607
|
-
}
|
1608
|
-
if (!this.controller) {
|
1609
|
-
throw new Error("Stream controller is not initialized.");
|
1610
|
-
}
|
1611
|
-
this.controller.enqueue(
|
1612
|
-
this.encoder.encode(formatDataStreamPart("data", [value]))
|
1613
|
-
);
|
1614
|
-
}
|
1615
|
-
appendMessageAnnotation(value) {
|
1616
|
-
if (this.isClosed) {
|
1617
|
-
throw new Error("Data Stream has already been closed.");
|
1618
|
-
}
|
1619
|
-
if (!this.controller) {
|
1620
|
-
throw new Error("Stream controller is not initialized.");
|
1621
|
-
}
|
1622
|
-
this.controller.enqueue(
|
1623
|
-
this.encoder.encode(formatDataStreamPart("message_annotations", [value]))
|
1624
|
-
);
|
1625
|
-
}
|
1626
|
-
};
|
1627
1188
|
export {
|
1628
1189
|
HANGING_STREAM_WARNING_TIME_MS,
|
1629
|
-
StreamData,
|
1630
|
-
calculateLanguageModelUsage,
|
1631
1190
|
convertToLanguageModelPrompt,
|
1632
1191
|
createCallbacksTransformer,
|
1633
|
-
formatDataStreamPart,
|
1634
|
-
mergeStreams,
|
1635
1192
|
prepareCallSettings,
|
1636
|
-
prepareResponseHeaders,
|
1637
1193
|
prepareRetries,
|
1638
1194
|
prepareToolsAndToolChoice,
|
1639
1195
|
standardizePrompt
|