ai 5.0.0-canary.3 → 5.0.0-canary.5
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 +30 -0
- package/dist/index.d.mts +1038 -178
- package/dist/index.d.ts +1038 -178
- package/dist/index.js +1839 -229
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1749 -178
- package/dist/index.mjs.map +1 -1
- package/{rsc/dist/rsc-server.d.mts → dist/internal/index.d.mts} +400 -366
- package/dist/internal/index.d.ts +782 -0
- package/dist/internal/index.js +1483 -0
- package/dist/internal/index.js.map +1 -0
- package/{rsc/dist/rsc-server.mjs → dist/internal/index.mjs} +1075 -1771
- package/dist/internal/index.mjs.map +1 -0
- package/mcp-stdio/dist/index.d.mts +6 -6
- package/mcp-stdio/dist/index.d.ts +6 -6
- package/mcp-stdio/dist/index.js +1 -1
- package/mcp-stdio/dist/index.js.map +1 -1
- package/mcp-stdio/dist/index.mjs +1 -1
- package/mcp-stdio/dist/index.mjs.map +1 -1
- package/mcp-stdio/get-environment.test.ts +13 -0
- package/mcp-stdio/get-environment.ts +1 -1
- package/package.json +14 -27
- package/rsc/dist/index.d.ts +0 -813
- package/rsc/dist/index.mjs +0 -18
- package/rsc/dist/rsc-client.d.mts +0 -1
- package/rsc/dist/rsc-client.mjs +0 -18
- package/rsc/dist/rsc-client.mjs.map +0 -1
- package/rsc/dist/rsc-server.mjs.map +0 -1
- package/rsc/dist/rsc-shared.d.mts +0 -101
- package/rsc/dist/rsc-shared.mjs +0 -308
- package/rsc/dist/rsc-shared.mjs.map +0 -1
@@ -1,351 +1,7 @@
|
|
1
|
-
//
|
2
|
-
import
|
3
|
-
import {
|
4
|
-
|
5
|
-
// util/create-resolvable-promise.ts
|
6
|
-
function createResolvablePromise() {
|
7
|
-
let resolve;
|
8
|
-
let reject;
|
9
|
-
const promise = new Promise((res, rej) => {
|
10
|
-
resolve = res;
|
11
|
-
reject = rej;
|
12
|
-
});
|
13
|
-
return {
|
14
|
-
promise,
|
15
|
-
resolve,
|
16
|
-
reject
|
17
|
-
};
|
18
|
-
}
|
19
|
-
|
20
|
-
// util/is-function.ts
|
21
|
-
var isFunction = (value) => typeof value === "function";
|
22
|
-
|
23
|
-
// rsc/ai-state.tsx
|
24
|
-
var asyncAIStateStorage = new AsyncLocalStorage();
|
25
|
-
function getAIStateStoreOrThrow(message) {
|
26
|
-
const store = asyncAIStateStorage.getStore();
|
27
|
-
if (!store) {
|
28
|
-
throw new Error(message);
|
29
|
-
}
|
30
|
-
return store;
|
31
|
-
}
|
32
|
-
function withAIState({ state, options }, fn) {
|
33
|
-
return asyncAIStateStorage.run(
|
34
|
-
{
|
35
|
-
currentState: JSON.parse(JSON.stringify(state)),
|
36
|
-
// deep clone object
|
37
|
-
originalState: state,
|
38
|
-
sealed: false,
|
39
|
-
options
|
40
|
-
},
|
41
|
-
fn
|
42
|
-
);
|
43
|
-
}
|
44
|
-
function getAIStateDeltaPromise() {
|
45
|
-
const store = getAIStateStoreOrThrow("Internal error occurred.");
|
46
|
-
return store.mutationDeltaPromise;
|
47
|
-
}
|
48
|
-
function sealMutableAIState() {
|
49
|
-
const store = getAIStateStoreOrThrow("Internal error occurred.");
|
50
|
-
store.sealed = true;
|
51
|
-
}
|
52
|
-
function getAIState(...args) {
|
53
|
-
const store = getAIStateStoreOrThrow(
|
54
|
-
"`getAIState` must be called within an AI Action."
|
55
|
-
);
|
56
|
-
if (args.length > 0) {
|
57
|
-
const key = args[0];
|
58
|
-
if (typeof store.currentState !== "object") {
|
59
|
-
throw new Error(
|
60
|
-
`You can't get the "${String(
|
61
|
-
key
|
62
|
-
)}" field from the AI state because it's not an object.`
|
63
|
-
);
|
64
|
-
}
|
65
|
-
return store.currentState[key];
|
66
|
-
}
|
67
|
-
return store.currentState;
|
68
|
-
}
|
69
|
-
function getMutableAIState(...args) {
|
70
|
-
const store = getAIStateStoreOrThrow(
|
71
|
-
"`getMutableAIState` must be called within an AI Action."
|
72
|
-
);
|
73
|
-
if (store.sealed) {
|
74
|
-
throw new Error(
|
75
|
-
"`getMutableAIState` must be called before returning from an AI Action. Please move it to the top level of the Action's function body."
|
76
|
-
);
|
77
|
-
}
|
78
|
-
if (!store.mutationDeltaPromise) {
|
79
|
-
const { promise, resolve } = createResolvablePromise();
|
80
|
-
store.mutationDeltaPromise = promise;
|
81
|
-
store.mutationDeltaResolve = resolve;
|
82
|
-
}
|
83
|
-
function doUpdate(newState, done) {
|
84
|
-
var _a9, _b;
|
85
|
-
if (args.length > 0) {
|
86
|
-
if (typeof store.currentState !== "object") {
|
87
|
-
const key = args[0];
|
88
|
-
throw new Error(
|
89
|
-
`You can't modify the "${String(
|
90
|
-
key
|
91
|
-
)}" field of the AI state because it's not an object.`
|
92
|
-
);
|
93
|
-
}
|
94
|
-
}
|
95
|
-
if (isFunction(newState)) {
|
96
|
-
if (args.length > 0) {
|
97
|
-
store.currentState[args[0]] = newState(store.currentState[args[0]]);
|
98
|
-
} else {
|
99
|
-
store.currentState = newState(store.currentState);
|
100
|
-
}
|
101
|
-
} else {
|
102
|
-
if (args.length > 0) {
|
103
|
-
store.currentState[args[0]] = newState;
|
104
|
-
} else {
|
105
|
-
store.currentState = newState;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
(_b = (_a9 = store.options).onSetAIState) == null ? void 0 : _b.call(_a9, {
|
109
|
-
key: args.length > 0 ? args[0] : void 0,
|
110
|
-
state: store.currentState,
|
111
|
-
done
|
112
|
-
});
|
113
|
-
}
|
114
|
-
const mutableState = {
|
115
|
-
get: () => {
|
116
|
-
if (args.length > 0) {
|
117
|
-
const key = args[0];
|
118
|
-
if (typeof store.currentState !== "object") {
|
119
|
-
throw new Error(
|
120
|
-
`You can't get the "${String(
|
121
|
-
key
|
122
|
-
)}" field from the AI state because it's not an object.`
|
123
|
-
);
|
124
|
-
}
|
125
|
-
return store.currentState[key];
|
126
|
-
}
|
127
|
-
return store.currentState;
|
128
|
-
},
|
129
|
-
update: function update(newAIState) {
|
130
|
-
doUpdate(newAIState, false);
|
131
|
-
},
|
132
|
-
done: function done(...doneArgs) {
|
133
|
-
if (doneArgs.length > 0) {
|
134
|
-
doUpdate(doneArgs[0], true);
|
135
|
-
}
|
136
|
-
const delta = jsondiffpatch.diff(store.originalState, store.currentState);
|
137
|
-
store.mutationDeltaResolve(delta);
|
138
|
-
}
|
139
|
-
};
|
140
|
-
return mutableState;
|
141
|
-
}
|
142
|
-
|
143
|
-
// rsc/provider.tsx
|
144
|
-
import * as React from "react";
|
145
|
-
import { InternalAIProvider } from "./rsc-shared.mjs";
|
146
|
-
import { jsx } from "react/jsx-runtime";
|
147
|
-
async function innerAction({
|
148
|
-
action,
|
149
|
-
options
|
150
|
-
}, state, ...args) {
|
151
|
-
"use server";
|
152
|
-
return await withAIState(
|
153
|
-
{
|
154
|
-
state,
|
155
|
-
options
|
156
|
-
},
|
157
|
-
async () => {
|
158
|
-
const result = await action(...args);
|
159
|
-
sealMutableAIState();
|
160
|
-
return [getAIStateDeltaPromise(), result];
|
161
|
-
}
|
162
|
-
);
|
163
|
-
}
|
164
|
-
function wrapAction(action, options) {
|
165
|
-
return innerAction.bind(null, { action, options });
|
166
|
-
}
|
167
|
-
function createAI({
|
168
|
-
actions,
|
169
|
-
initialAIState,
|
170
|
-
initialUIState,
|
171
|
-
onSetAIState,
|
172
|
-
onGetUIState
|
173
|
-
}) {
|
174
|
-
const wrappedActions = {};
|
175
|
-
for (const name9 in actions) {
|
176
|
-
wrappedActions[name9] = wrapAction(actions[name9], {
|
177
|
-
onSetAIState
|
178
|
-
});
|
179
|
-
}
|
180
|
-
const wrappedSyncUIState = onGetUIState ? wrapAction(onGetUIState, {}) : void 0;
|
181
|
-
const AI = async (props) => {
|
182
|
-
var _a9, _b;
|
183
|
-
if ("useState" in React) {
|
184
|
-
throw new Error(
|
185
|
-
"This component can only be used inside Server Components."
|
186
|
-
);
|
187
|
-
}
|
188
|
-
let uiState = (_a9 = props.initialUIState) != null ? _a9 : initialUIState;
|
189
|
-
let aiState = (_b = props.initialAIState) != null ? _b : initialAIState;
|
190
|
-
let aiStateDelta = void 0;
|
191
|
-
if (wrappedSyncUIState) {
|
192
|
-
const [newAIStateDelta, newUIState] = await wrappedSyncUIState(aiState);
|
193
|
-
if (newUIState !== void 0) {
|
194
|
-
aiStateDelta = newAIStateDelta;
|
195
|
-
uiState = newUIState;
|
196
|
-
}
|
197
|
-
}
|
198
|
-
return /* @__PURE__ */ jsx(
|
199
|
-
InternalAIProvider,
|
200
|
-
{
|
201
|
-
wrappedActions,
|
202
|
-
wrappedSyncUIState,
|
203
|
-
initialUIState: uiState,
|
204
|
-
initialAIState: aiState,
|
205
|
-
initialAIStatePatch: aiStateDelta,
|
206
|
-
children: props.children
|
207
|
-
}
|
208
|
-
);
|
209
|
-
};
|
210
|
-
return AI;
|
211
|
-
}
|
212
|
-
|
213
|
-
// rsc/stream-ui/stream-ui.tsx
|
214
|
-
import { safeParseJSON } from "@ai-sdk/provider-utils";
|
215
|
-
|
216
|
-
// util/download-error.ts
|
217
|
-
import { AISDKError } from "@ai-sdk/provider";
|
218
|
-
var name = "AI_DownloadError";
|
219
|
-
var marker = `vercel.ai.error.${name}`;
|
220
|
-
var symbol = Symbol.for(marker);
|
221
|
-
var _a;
|
222
|
-
var DownloadError = class extends AISDKError {
|
223
|
-
constructor({
|
224
|
-
url,
|
225
|
-
statusCode,
|
226
|
-
statusText,
|
227
|
-
cause,
|
228
|
-
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
229
|
-
}) {
|
230
|
-
super({ name, message, cause });
|
231
|
-
this[_a] = true;
|
232
|
-
this.url = url;
|
233
|
-
this.statusCode = statusCode;
|
234
|
-
this.statusText = statusText;
|
235
|
-
}
|
236
|
-
static isInstance(error) {
|
237
|
-
return AISDKError.hasMarker(error, marker);
|
238
|
-
}
|
239
|
-
};
|
240
|
-
_a = symbol;
|
241
|
-
|
242
|
-
// util/download.ts
|
243
|
-
async function download({ url }) {
|
244
|
-
var _a9;
|
245
|
-
const urlText = url.toString();
|
246
|
-
try {
|
247
|
-
const response = await fetch(urlText);
|
248
|
-
if (!response.ok) {
|
249
|
-
throw new DownloadError({
|
250
|
-
url: urlText,
|
251
|
-
statusCode: response.status,
|
252
|
-
statusText: response.statusText
|
253
|
-
});
|
254
|
-
}
|
255
|
-
return {
|
256
|
-
data: new Uint8Array(await response.arrayBuffer()),
|
257
|
-
mimeType: (_a9 = response.headers.get("content-type")) != null ? _a9 : void 0
|
258
|
-
};
|
259
|
-
} catch (error) {
|
260
|
-
if (DownloadError.isInstance(error)) {
|
261
|
-
throw error;
|
262
|
-
}
|
263
|
-
throw new DownloadError({ url: urlText, cause: error });
|
264
|
-
}
|
265
|
-
}
|
266
|
-
|
267
|
-
// core/util/detect-image-mimetype.ts
|
268
|
-
var mimeTypeSignatures = [
|
269
|
-
{
|
270
|
-
mimeType: "image/gif",
|
271
|
-
bytesPrefix: [71, 73, 70],
|
272
|
-
base64Prefix: "R0lG"
|
273
|
-
},
|
274
|
-
{
|
275
|
-
mimeType: "image/png",
|
276
|
-
bytesPrefix: [137, 80, 78, 71],
|
277
|
-
base64Prefix: "iVBORw"
|
278
|
-
},
|
279
|
-
{
|
280
|
-
mimeType: "image/jpeg",
|
281
|
-
bytesPrefix: [255, 216],
|
282
|
-
base64Prefix: "/9j/"
|
283
|
-
},
|
284
|
-
{
|
285
|
-
mimeType: "image/webp",
|
286
|
-
bytesPrefix: [82, 73, 70, 70],
|
287
|
-
base64Prefix: "UklGRg"
|
288
|
-
},
|
289
|
-
{
|
290
|
-
mimeType: "image/bmp",
|
291
|
-
bytesPrefix: [66, 77],
|
292
|
-
base64Prefix: "Qk"
|
293
|
-
},
|
294
|
-
{
|
295
|
-
mimeType: "image/tiff",
|
296
|
-
bytesPrefix: [73, 73, 42, 0],
|
297
|
-
base64Prefix: "SUkqAA"
|
298
|
-
},
|
299
|
-
{
|
300
|
-
mimeType: "image/tiff",
|
301
|
-
bytesPrefix: [77, 77, 0, 42],
|
302
|
-
base64Prefix: "TU0AKg"
|
303
|
-
},
|
304
|
-
{
|
305
|
-
mimeType: "image/avif",
|
306
|
-
bytesPrefix: [
|
307
|
-
0,
|
308
|
-
0,
|
309
|
-
0,
|
310
|
-
32,
|
311
|
-
102,
|
312
|
-
116,
|
313
|
-
121,
|
314
|
-
112,
|
315
|
-
97,
|
316
|
-
118,
|
317
|
-
105,
|
318
|
-
102
|
319
|
-
],
|
320
|
-
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
321
|
-
},
|
322
|
-
{
|
323
|
-
mimeType: "image/heic",
|
324
|
-
bytesPrefix: [
|
325
|
-
0,
|
326
|
-
0,
|
327
|
-
0,
|
328
|
-
32,
|
329
|
-
102,
|
330
|
-
116,
|
331
|
-
121,
|
332
|
-
112,
|
333
|
-
104,
|
334
|
-
101,
|
335
|
-
105,
|
336
|
-
99
|
337
|
-
],
|
338
|
-
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
339
|
-
}
|
340
|
-
];
|
341
|
-
function detectImageMimeType(image) {
|
342
|
-
for (const signature of mimeTypeSignatures) {
|
343
|
-
if (typeof image === "string" ? image.startsWith(signature.base64Prefix) : image.length >= signature.bytesPrefix.length && signature.bytesPrefix.every((byte, index) => image[index] === byte)) {
|
344
|
-
return signature.mimeType;
|
345
|
-
}
|
346
|
-
}
|
347
|
-
return void 0;
|
348
|
-
}
|
1
|
+
// core/prompt/standardize-prompt.ts
|
2
|
+
import { InvalidPromptError } from "@ai-sdk/provider";
|
3
|
+
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
4
|
+
import { z as z7 } from "zod";
|
349
5
|
|
350
6
|
// core/prompt/data-content.ts
|
351
7
|
import {
|
@@ -354,26 +10,26 @@ import {
|
|
354
10
|
} from "@ai-sdk/provider-utils";
|
355
11
|
|
356
12
|
// core/prompt/invalid-data-content-error.ts
|
357
|
-
import { AISDKError
|
358
|
-
var
|
359
|
-
var
|
360
|
-
var
|
361
|
-
var
|
362
|
-
var InvalidDataContentError = class extends
|
13
|
+
import { AISDKError } from "@ai-sdk/provider";
|
14
|
+
var name = "AI_InvalidDataContentError";
|
15
|
+
var marker = `vercel.ai.error.${name}`;
|
16
|
+
var symbol = Symbol.for(marker);
|
17
|
+
var _a;
|
18
|
+
var InvalidDataContentError = class extends AISDKError {
|
363
19
|
constructor({
|
364
20
|
content,
|
365
21
|
cause,
|
366
22
|
message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
|
367
23
|
}) {
|
368
|
-
super({ name
|
369
|
-
this[
|
24
|
+
super({ name, message, cause });
|
25
|
+
this[_a] = true;
|
370
26
|
this.content = content;
|
371
27
|
}
|
372
28
|
static isInstance(error) {
|
373
|
-
return
|
29
|
+
return AISDKError.hasMarker(error, marker);
|
374
30
|
}
|
375
31
|
};
|
376
|
-
|
32
|
+
_a = symbol;
|
377
33
|
|
378
34
|
// core/prompt/data-content.ts
|
379
35
|
import { z } from "zod";
|
@@ -384,8 +40,8 @@ var dataContentSchema = z.union([
|
|
384
40
|
z.custom(
|
385
41
|
// Buffer might not be available in some environments such as CloudFlare:
|
386
42
|
(value) => {
|
387
|
-
var
|
388
|
-
return (_b = (
|
43
|
+
var _a7, _b;
|
44
|
+
return (_b = (_a7 = globalThis.Buffer) == null ? void 0 : _a7.isBuffer(value)) != null ? _b : false;
|
389
45
|
},
|
390
46
|
{ message: "Must be a Buffer" }
|
391
47
|
)
|
@@ -427,662 +83,108 @@ function convertUint8ArrayToText(uint8Array) {
|
|
427
83
|
}
|
428
84
|
}
|
429
85
|
|
430
|
-
// core/prompt/
|
431
|
-
|
432
|
-
var
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
}) {
|
441
|
-
super({ name: name3, message });
|
442
|
-
this[_a3] = true;
|
443
|
-
this.role = role;
|
444
|
-
}
|
445
|
-
static isInstance(error) {
|
446
|
-
return AISDKError3.hasMarker(error, marker3);
|
447
|
-
}
|
448
|
-
};
|
449
|
-
_a3 = symbol3;
|
450
|
-
|
451
|
-
// core/prompt/split-data-url.ts
|
452
|
-
function splitDataUrl(dataUrl) {
|
453
|
-
try {
|
454
|
-
const [header, base64Content] = dataUrl.split(",");
|
455
|
-
return {
|
456
|
-
mimeType: header.split(";")[0].split(":")[1],
|
457
|
-
base64Content
|
458
|
-
};
|
459
|
-
} catch (error) {
|
460
|
-
return {
|
461
|
-
mimeType: void 0,
|
462
|
-
base64Content: void 0
|
463
|
-
};
|
464
|
-
}
|
465
|
-
}
|
466
|
-
|
467
|
-
// core/prompt/convert-to-language-model-prompt.ts
|
468
|
-
async function convertToLanguageModelPrompt({
|
469
|
-
prompt,
|
470
|
-
modelSupportsImageUrls = true,
|
471
|
-
modelSupportsUrl = () => false,
|
472
|
-
downloadImplementation = download
|
473
|
-
}) {
|
474
|
-
const downloadedAssets = await downloadAssets(
|
475
|
-
prompt.messages,
|
476
|
-
downloadImplementation,
|
477
|
-
modelSupportsImageUrls,
|
478
|
-
modelSupportsUrl
|
479
|
-
);
|
480
|
-
return [
|
481
|
-
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
482
|
-
...prompt.messages.map(
|
483
|
-
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
484
|
-
)
|
485
|
-
];
|
486
|
-
}
|
487
|
-
function convertToLanguageModelMessage(message, downloadedAssets) {
|
488
|
-
var _a9, _b, _c, _d, _e, _f;
|
489
|
-
const role = message.role;
|
490
|
-
switch (role) {
|
491
|
-
case "system": {
|
492
|
-
return {
|
493
|
-
role: "system",
|
494
|
-
content: message.content,
|
495
|
-
providerOptions: (_a9 = message.providerOptions) != null ? _a9 : message.experimental_providerMetadata
|
496
|
-
};
|
497
|
-
}
|
498
|
-
case "user": {
|
499
|
-
if (typeof message.content === "string") {
|
500
|
-
return {
|
501
|
-
role: "user",
|
502
|
-
content: [{ type: "text", text: message.content }],
|
503
|
-
providerOptions: (_b = message.providerOptions) != null ? _b : message.experimental_providerMetadata
|
504
|
-
};
|
505
|
-
}
|
506
|
-
return {
|
507
|
-
role: "user",
|
508
|
-
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
509
|
-
providerOptions: (_c = message.providerOptions) != null ? _c : message.experimental_providerMetadata
|
510
|
-
};
|
86
|
+
// core/prompt/attachments-to-parts.ts
|
87
|
+
function attachmentsToParts(attachments) {
|
88
|
+
var _a7, _b, _c;
|
89
|
+
const parts = [];
|
90
|
+
for (const attachment of attachments) {
|
91
|
+
let url;
|
92
|
+
try {
|
93
|
+
url = new URL(attachment.url);
|
94
|
+
} catch (error) {
|
95
|
+
throw new Error(`Invalid URL: ${attachment.url}`);
|
511
96
|
}
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
content: message.content.filter(
|
523
|
-
// remove empty text parts:
|
524
|
-
(part) => part.type !== "text" || part.text !== ""
|
525
|
-
).map((part) => {
|
526
|
-
var _a10;
|
527
|
-
const providerOptions = (_a10 = part.providerOptions) != null ? _a10 : part.experimental_providerMetadata;
|
528
|
-
switch (part.type) {
|
529
|
-
case "file": {
|
530
|
-
return {
|
531
|
-
type: "file",
|
532
|
-
data: part.data instanceof URL ? part.data : convertDataContentToBase64String(part.data),
|
533
|
-
filename: part.filename,
|
534
|
-
mimeType: part.mimeType,
|
535
|
-
providerOptions
|
536
|
-
};
|
537
|
-
}
|
538
|
-
case "reasoning": {
|
539
|
-
return {
|
540
|
-
type: "reasoning",
|
541
|
-
text: part.text,
|
542
|
-
signature: part.signature,
|
543
|
-
providerOptions
|
544
|
-
};
|
545
|
-
}
|
546
|
-
case "redacted-reasoning": {
|
547
|
-
return {
|
548
|
-
type: "redacted-reasoning",
|
549
|
-
data: part.data,
|
550
|
-
providerOptions
|
551
|
-
};
|
552
|
-
}
|
553
|
-
case "text": {
|
554
|
-
return {
|
555
|
-
type: "text",
|
556
|
-
text: part.text,
|
557
|
-
providerOptions
|
558
|
-
};
|
559
|
-
}
|
560
|
-
case "tool-call": {
|
561
|
-
return {
|
562
|
-
type: "tool-call",
|
563
|
-
toolCallId: part.toolCallId,
|
564
|
-
toolName: part.toolName,
|
565
|
-
args: part.args,
|
566
|
-
providerOptions
|
567
|
-
};
|
568
|
-
}
|
97
|
+
switch (url.protocol) {
|
98
|
+
case "http:":
|
99
|
+
case "https:": {
|
100
|
+
if ((_a7 = attachment.contentType) == null ? void 0 : _a7.startsWith("image/")) {
|
101
|
+
parts.push({ type: "image", image: url });
|
102
|
+
} else {
|
103
|
+
if (!attachment.contentType) {
|
104
|
+
throw new Error(
|
105
|
+
"If the attachment is not an image, it must specify a content type"
|
106
|
+
);
|
569
107
|
}
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
content: message.content.map((part) => {
|
578
|
-
var _a10;
|
579
|
-
return {
|
580
|
-
type: "tool-result",
|
581
|
-
toolCallId: part.toolCallId,
|
582
|
-
toolName: part.toolName,
|
583
|
-
result: part.result,
|
584
|
-
content: part.experimental_content,
|
585
|
-
isError: part.isError,
|
586
|
-
providerOptions: (_a10 = part.providerOptions) != null ? _a10 : part.experimental_providerMetadata
|
587
|
-
};
|
588
|
-
}),
|
589
|
-
providerOptions: (_f = message.providerOptions) != null ? _f : message.experimental_providerMetadata
|
590
|
-
};
|
591
|
-
}
|
592
|
-
default: {
|
593
|
-
const _exhaustiveCheck = role;
|
594
|
-
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
595
|
-
}
|
596
|
-
}
|
597
|
-
}
|
598
|
-
async function downloadAssets(messages, downloadImplementation, modelSupportsImageUrls, modelSupportsUrl) {
|
599
|
-
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
600
|
-
(content) => Array.isArray(content)
|
601
|
-
).flat().filter(
|
602
|
-
(part) => part.type === "image" || part.type === "file"
|
603
|
-
).filter(
|
604
|
-
(part) => !(part.type === "image" && modelSupportsImageUrls === true)
|
605
|
-
).map((part) => part.type === "image" ? part.image : part.data).map(
|
606
|
-
(part) => (
|
607
|
-
// support string urls:
|
608
|
-
typeof part === "string" && (part.startsWith("http:") || part.startsWith("https:")) ? new URL(part) : part
|
609
|
-
)
|
610
|
-
).filter((image) => image instanceof URL).filter((url) => !modelSupportsUrl(url));
|
611
|
-
const downloadedImages = await Promise.all(
|
612
|
-
urls.map(async (url) => ({
|
613
|
-
url,
|
614
|
-
data: await downloadImplementation({ url })
|
615
|
-
}))
|
616
|
-
);
|
617
|
-
return Object.fromEntries(
|
618
|
-
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
619
|
-
);
|
620
|
-
}
|
621
|
-
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
622
|
-
var _a9, _b, _c, _d;
|
623
|
-
if (part.type === "text") {
|
624
|
-
return {
|
625
|
-
type: "text",
|
626
|
-
text: part.text,
|
627
|
-
providerOptions: (_a9 = part.providerOptions) != null ? _a9 : part.experimental_providerMetadata
|
628
|
-
};
|
629
|
-
}
|
630
|
-
let mimeType = part.mimeType;
|
631
|
-
let data;
|
632
|
-
let content;
|
633
|
-
let normalizedData;
|
634
|
-
const type = part.type;
|
635
|
-
switch (type) {
|
636
|
-
case "image":
|
637
|
-
data = part.image;
|
638
|
-
break;
|
639
|
-
case "file":
|
640
|
-
data = part.data;
|
641
|
-
break;
|
642
|
-
default:
|
643
|
-
throw new Error(`Unsupported part type: ${type}`);
|
644
|
-
}
|
645
|
-
try {
|
646
|
-
content = typeof data === "string" ? new URL(data) : data;
|
647
|
-
} catch (error) {
|
648
|
-
content = data;
|
649
|
-
}
|
650
|
-
if (content instanceof URL) {
|
651
|
-
if (content.protocol === "data:") {
|
652
|
-
const { mimeType: dataUrlMimeType, base64Content } = splitDataUrl(
|
653
|
-
content.toString()
|
654
|
-
);
|
655
|
-
if (dataUrlMimeType == null || base64Content == null) {
|
656
|
-
throw new Error(`Invalid data URL format in part ${type}`);
|
657
|
-
}
|
658
|
-
mimeType = dataUrlMimeType;
|
659
|
-
normalizedData = convertDataContentToUint8Array(base64Content);
|
660
|
-
} else {
|
661
|
-
const downloadedFile = downloadedAssets[content.toString()];
|
662
|
-
if (downloadedFile) {
|
663
|
-
normalizedData = downloadedFile.data;
|
664
|
-
mimeType != null ? mimeType : mimeType = downloadedFile.mimeType;
|
665
|
-
} else {
|
666
|
-
normalizedData = content;
|
108
|
+
parts.push({
|
109
|
+
type: "file",
|
110
|
+
data: url,
|
111
|
+
mediaType: attachment.contentType
|
112
|
+
});
|
113
|
+
}
|
114
|
+
break;
|
667
115
|
}
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
116
|
+
case "data:": {
|
117
|
+
let header;
|
118
|
+
let base64Content;
|
119
|
+
let mediaType;
|
120
|
+
try {
|
121
|
+
[header, base64Content] = attachment.url.split(",");
|
122
|
+
mediaType = header.split(";")[0].split(":")[1];
|
123
|
+
} catch (error) {
|
124
|
+
throw new Error(`Error processing data URL: ${attachment.url}`);
|
125
|
+
}
|
126
|
+
if (mediaType == null || base64Content == null) {
|
127
|
+
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
128
|
+
}
|
129
|
+
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
130
|
+
parts.push({
|
131
|
+
type: "image",
|
132
|
+
image: convertDataContentToUint8Array(base64Content)
|
133
|
+
});
|
134
|
+
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
135
|
+
parts.push({
|
136
|
+
type: "text",
|
137
|
+
text: convertUint8ArrayToText(
|
138
|
+
convertDataContentToUint8Array(base64Content)
|
139
|
+
)
|
140
|
+
});
|
141
|
+
} else {
|
142
|
+
if (!attachment.contentType) {
|
143
|
+
throw new Error(
|
144
|
+
"If the attachment is not an image or text, it must specify a content type"
|
145
|
+
);
|
146
|
+
}
|
147
|
+
parts.push({
|
148
|
+
type: "file",
|
149
|
+
data: base64Content,
|
150
|
+
mediaType: attachment.contentType
|
151
|
+
});
|
152
|
+
}
|
153
|
+
break;
|
676
154
|
}
|
677
|
-
|
678
|
-
|
679
|
-
image: normalizedData,
|
680
|
-
mimeType,
|
681
|
-
providerOptions: (_c = part.providerOptions) != null ? _c : part.experimental_providerMetadata
|
682
|
-
};
|
683
|
-
}
|
684
|
-
case "file": {
|
685
|
-
if (mimeType == null) {
|
686
|
-
throw new Error(`Mime type is missing for file part`);
|
155
|
+
default: {
|
156
|
+
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
687
157
|
}
|
688
|
-
return {
|
689
|
-
type: "file",
|
690
|
-
data: normalizedData instanceof Uint8Array ? convertDataContentToBase64String(normalizedData) : normalizedData,
|
691
|
-
filename: part.filename,
|
692
|
-
mimeType,
|
693
|
-
providerOptions: (_d = part.providerOptions) != null ? _d : part.experimental_providerMetadata
|
694
|
-
};
|
695
158
|
}
|
696
159
|
}
|
160
|
+
return parts;
|
697
161
|
}
|
698
162
|
|
699
|
-
//
|
700
|
-
import { AISDKError as
|
701
|
-
var
|
702
|
-
var
|
703
|
-
var
|
704
|
-
var
|
705
|
-
var
|
163
|
+
// core/prompt/message-conversion-error.ts
|
164
|
+
import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
|
165
|
+
var name2 = "AI_MessageConversionError";
|
166
|
+
var marker2 = `vercel.ai.error.${name2}`;
|
167
|
+
var symbol2 = Symbol.for(marker2);
|
168
|
+
var _a2;
|
169
|
+
var MessageConversionError = class extends AISDKError2 {
|
706
170
|
constructor({
|
707
|
-
|
708
|
-
value,
|
171
|
+
originalMessage,
|
709
172
|
message
|
710
173
|
}) {
|
711
|
-
super({
|
712
|
-
|
713
|
-
|
714
|
-
});
|
715
|
-
this[_a4] = true;
|
716
|
-
this.parameter = parameter;
|
717
|
-
this.value = value;
|
174
|
+
super({ name: name2, message });
|
175
|
+
this[_a2] = true;
|
176
|
+
this.originalMessage = originalMessage;
|
718
177
|
}
|
719
178
|
static isInstance(error) {
|
720
|
-
return
|
179
|
+
return AISDKError2.hasMarker(error, marker2);
|
721
180
|
}
|
722
181
|
};
|
723
|
-
|
724
|
-
|
725
|
-
// core/prompt/prepare-call-settings.ts
|
726
|
-
function prepareCallSettings({
|
727
|
-
maxTokens,
|
728
|
-
temperature,
|
729
|
-
topP,
|
730
|
-
topK,
|
731
|
-
presencePenalty,
|
732
|
-
frequencyPenalty,
|
733
|
-
stopSequences,
|
734
|
-
seed
|
735
|
-
}) {
|
736
|
-
if (maxTokens != null) {
|
737
|
-
if (!Number.isInteger(maxTokens)) {
|
738
|
-
throw new InvalidArgumentError({
|
739
|
-
parameter: "maxTokens",
|
740
|
-
value: maxTokens,
|
741
|
-
message: "maxTokens must be an integer"
|
742
|
-
});
|
743
|
-
}
|
744
|
-
if (maxTokens < 1) {
|
745
|
-
throw new InvalidArgumentError({
|
746
|
-
parameter: "maxTokens",
|
747
|
-
value: maxTokens,
|
748
|
-
message: "maxTokens must be >= 1"
|
749
|
-
});
|
750
|
-
}
|
751
|
-
}
|
752
|
-
if (temperature != null) {
|
753
|
-
if (typeof temperature !== "number") {
|
754
|
-
throw new InvalidArgumentError({
|
755
|
-
parameter: "temperature",
|
756
|
-
value: temperature,
|
757
|
-
message: "temperature must be a number"
|
758
|
-
});
|
759
|
-
}
|
760
|
-
}
|
761
|
-
if (topP != null) {
|
762
|
-
if (typeof topP !== "number") {
|
763
|
-
throw new InvalidArgumentError({
|
764
|
-
parameter: "topP",
|
765
|
-
value: topP,
|
766
|
-
message: "topP must be a number"
|
767
|
-
});
|
768
|
-
}
|
769
|
-
}
|
770
|
-
if (topK != null) {
|
771
|
-
if (typeof topK !== "number") {
|
772
|
-
throw new InvalidArgumentError({
|
773
|
-
parameter: "topK",
|
774
|
-
value: topK,
|
775
|
-
message: "topK must be a number"
|
776
|
-
});
|
777
|
-
}
|
778
|
-
}
|
779
|
-
if (presencePenalty != null) {
|
780
|
-
if (typeof presencePenalty !== "number") {
|
781
|
-
throw new InvalidArgumentError({
|
782
|
-
parameter: "presencePenalty",
|
783
|
-
value: presencePenalty,
|
784
|
-
message: "presencePenalty must be a number"
|
785
|
-
});
|
786
|
-
}
|
787
|
-
}
|
788
|
-
if (frequencyPenalty != null) {
|
789
|
-
if (typeof frequencyPenalty !== "number") {
|
790
|
-
throw new InvalidArgumentError({
|
791
|
-
parameter: "frequencyPenalty",
|
792
|
-
value: frequencyPenalty,
|
793
|
-
message: "frequencyPenalty must be a number"
|
794
|
-
});
|
795
|
-
}
|
796
|
-
}
|
797
|
-
if (seed != null) {
|
798
|
-
if (!Number.isInteger(seed)) {
|
799
|
-
throw new InvalidArgumentError({
|
800
|
-
parameter: "seed",
|
801
|
-
value: seed,
|
802
|
-
message: "seed must be an integer"
|
803
|
-
});
|
804
|
-
}
|
805
|
-
}
|
806
|
-
return {
|
807
|
-
maxTokens,
|
808
|
-
// TODO v5 remove default 0 for temperature
|
809
|
-
temperature: temperature != null ? temperature : 0,
|
810
|
-
topP,
|
811
|
-
topK,
|
812
|
-
presencePenalty,
|
813
|
-
frequencyPenalty,
|
814
|
-
stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
|
815
|
-
seed
|
816
|
-
};
|
817
|
-
}
|
818
|
-
|
819
|
-
// util/retry-with-exponential-backoff.ts
|
820
|
-
import { APICallError } from "@ai-sdk/provider";
|
821
|
-
import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
822
|
-
|
823
|
-
// util/retry-error.ts
|
824
|
-
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
825
|
-
var name5 = "AI_RetryError";
|
826
|
-
var marker5 = `vercel.ai.error.${name5}`;
|
827
|
-
var symbol5 = Symbol.for(marker5);
|
828
|
-
var _a5;
|
829
|
-
var RetryError = class extends AISDKError5 {
|
830
|
-
constructor({
|
831
|
-
message,
|
832
|
-
reason,
|
833
|
-
errors
|
834
|
-
}) {
|
835
|
-
super({ name: name5, message });
|
836
|
-
this[_a5] = true;
|
837
|
-
this.reason = reason;
|
838
|
-
this.errors = errors;
|
839
|
-
this.lastError = errors[errors.length - 1];
|
840
|
-
}
|
841
|
-
static isInstance(error) {
|
842
|
-
return AISDKError5.hasMarker(error, marker5);
|
843
|
-
}
|
844
|
-
};
|
845
|
-
_a5 = symbol5;
|
846
|
-
|
847
|
-
// util/retry-with-exponential-backoff.ts
|
848
|
-
var retryWithExponentialBackoff = ({
|
849
|
-
maxRetries = 2,
|
850
|
-
initialDelayInMs = 2e3,
|
851
|
-
backoffFactor = 2
|
852
|
-
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
853
|
-
maxRetries,
|
854
|
-
delayInMs: initialDelayInMs,
|
855
|
-
backoffFactor
|
856
|
-
});
|
857
|
-
async function _retryWithExponentialBackoff(f, {
|
858
|
-
maxRetries,
|
859
|
-
delayInMs,
|
860
|
-
backoffFactor
|
861
|
-
}, errors = []) {
|
862
|
-
try {
|
863
|
-
return await f();
|
864
|
-
} catch (error) {
|
865
|
-
if (isAbortError(error)) {
|
866
|
-
throw error;
|
867
|
-
}
|
868
|
-
if (maxRetries === 0) {
|
869
|
-
throw error;
|
870
|
-
}
|
871
|
-
const errorMessage = getErrorMessage(error);
|
872
|
-
const newErrors = [...errors, error];
|
873
|
-
const tryNumber = newErrors.length;
|
874
|
-
if (tryNumber > maxRetries) {
|
875
|
-
throw new RetryError({
|
876
|
-
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
877
|
-
reason: "maxRetriesExceeded",
|
878
|
-
errors: newErrors
|
879
|
-
});
|
880
|
-
}
|
881
|
-
if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
882
|
-
await delay(delayInMs);
|
883
|
-
return _retryWithExponentialBackoff(
|
884
|
-
f,
|
885
|
-
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
886
|
-
newErrors
|
887
|
-
);
|
888
|
-
}
|
889
|
-
if (tryNumber === 1) {
|
890
|
-
throw error;
|
891
|
-
}
|
892
|
-
throw new RetryError({
|
893
|
-
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
894
|
-
reason: "errorNotRetryable",
|
895
|
-
errors: newErrors
|
896
|
-
});
|
897
|
-
}
|
898
|
-
}
|
899
|
-
|
900
|
-
// core/prompt/prepare-retries.ts
|
901
|
-
function prepareRetries({
|
902
|
-
maxRetries
|
903
|
-
}) {
|
904
|
-
if (maxRetries != null) {
|
905
|
-
if (!Number.isInteger(maxRetries)) {
|
906
|
-
throw new InvalidArgumentError({
|
907
|
-
parameter: "maxRetries",
|
908
|
-
value: maxRetries,
|
909
|
-
message: "maxRetries must be an integer"
|
910
|
-
});
|
911
|
-
}
|
912
|
-
if (maxRetries < 0) {
|
913
|
-
throw new InvalidArgumentError({
|
914
|
-
parameter: "maxRetries",
|
915
|
-
value: maxRetries,
|
916
|
-
message: "maxRetries must be >= 0"
|
917
|
-
});
|
918
|
-
}
|
919
|
-
}
|
920
|
-
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
921
|
-
return {
|
922
|
-
maxRetries: maxRetriesResult,
|
923
|
-
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
|
924
|
-
};
|
925
|
-
}
|
926
|
-
|
927
|
-
// core/prompt/prepare-tools-and-tool-choice.ts
|
928
|
-
import { asSchema } from "@ai-sdk/ui-utils";
|
929
|
-
|
930
|
-
// core/util/is-non-empty-object.ts
|
931
|
-
function isNonEmptyObject(object) {
|
932
|
-
return object != null && Object.keys(object).length > 0;
|
933
|
-
}
|
934
|
-
|
935
|
-
// core/prompt/prepare-tools-and-tool-choice.ts
|
936
|
-
function prepareToolsAndToolChoice({
|
937
|
-
tools,
|
938
|
-
toolChoice,
|
939
|
-
activeTools
|
940
|
-
}) {
|
941
|
-
if (!isNonEmptyObject(tools)) {
|
942
|
-
return {
|
943
|
-
tools: void 0,
|
944
|
-
toolChoice: void 0
|
945
|
-
};
|
946
|
-
}
|
947
|
-
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
948
|
-
([name9]) => activeTools.includes(name9)
|
949
|
-
) : Object.entries(tools);
|
950
|
-
return {
|
951
|
-
tools: filteredTools.map(([name9, tool]) => {
|
952
|
-
const toolType = tool.type;
|
953
|
-
switch (toolType) {
|
954
|
-
case void 0:
|
955
|
-
case "function":
|
956
|
-
return {
|
957
|
-
type: "function",
|
958
|
-
name: name9,
|
959
|
-
description: tool.description,
|
960
|
-
parameters: asSchema(tool.parameters).jsonSchema
|
961
|
-
};
|
962
|
-
case "provider-defined":
|
963
|
-
return {
|
964
|
-
type: "provider-defined",
|
965
|
-
name: name9,
|
966
|
-
id: tool.id,
|
967
|
-
args: tool.args
|
968
|
-
};
|
969
|
-
default: {
|
970
|
-
const exhaustiveCheck = toolType;
|
971
|
-
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
972
|
-
}
|
973
|
-
}
|
974
|
-
}),
|
975
|
-
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
976
|
-
};
|
977
|
-
}
|
978
|
-
|
979
|
-
// core/prompt/standardize-prompt.ts
|
980
|
-
import { InvalidPromptError } from "@ai-sdk/provider";
|
981
|
-
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
982
|
-
import { z as z7 } from "zod";
|
983
|
-
|
984
|
-
// core/prompt/attachments-to-parts.ts
|
985
|
-
function attachmentsToParts(attachments) {
|
986
|
-
var _a9, _b, _c;
|
987
|
-
const parts = [];
|
988
|
-
for (const attachment of attachments) {
|
989
|
-
let url;
|
990
|
-
try {
|
991
|
-
url = new URL(attachment.url);
|
992
|
-
} catch (error) {
|
993
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
994
|
-
}
|
995
|
-
switch (url.protocol) {
|
996
|
-
case "http:":
|
997
|
-
case "https:": {
|
998
|
-
if ((_a9 = attachment.contentType) == null ? void 0 : _a9.startsWith("image/")) {
|
999
|
-
parts.push({ type: "image", image: url });
|
1000
|
-
} else {
|
1001
|
-
if (!attachment.contentType) {
|
1002
|
-
throw new Error(
|
1003
|
-
"If the attachment is not an image, it must specify a content type"
|
1004
|
-
);
|
1005
|
-
}
|
1006
|
-
parts.push({
|
1007
|
-
type: "file",
|
1008
|
-
data: url,
|
1009
|
-
mimeType: attachment.contentType
|
1010
|
-
});
|
1011
|
-
}
|
1012
|
-
break;
|
1013
|
-
}
|
1014
|
-
case "data:": {
|
1015
|
-
let header;
|
1016
|
-
let base64Content;
|
1017
|
-
let mimeType;
|
1018
|
-
try {
|
1019
|
-
[header, base64Content] = attachment.url.split(",");
|
1020
|
-
mimeType = header.split(";")[0].split(":")[1];
|
1021
|
-
} catch (error) {
|
1022
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
1023
|
-
}
|
1024
|
-
if (mimeType == null || base64Content == null) {
|
1025
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
1026
|
-
}
|
1027
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
1028
|
-
parts.push({
|
1029
|
-
type: "image",
|
1030
|
-
image: convertDataContentToUint8Array(base64Content)
|
1031
|
-
});
|
1032
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
1033
|
-
parts.push({
|
1034
|
-
type: "text",
|
1035
|
-
text: convertUint8ArrayToText(
|
1036
|
-
convertDataContentToUint8Array(base64Content)
|
1037
|
-
)
|
1038
|
-
});
|
1039
|
-
} else {
|
1040
|
-
if (!attachment.contentType) {
|
1041
|
-
throw new Error(
|
1042
|
-
"If the attachment is not an image or text, it must specify a content type"
|
1043
|
-
);
|
1044
|
-
}
|
1045
|
-
parts.push({
|
1046
|
-
type: "file",
|
1047
|
-
data: base64Content,
|
1048
|
-
mimeType: attachment.contentType
|
1049
|
-
});
|
1050
|
-
}
|
1051
|
-
break;
|
1052
|
-
}
|
1053
|
-
default: {
|
1054
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
1055
|
-
}
|
1056
|
-
}
|
1057
|
-
}
|
1058
|
-
return parts;
|
1059
|
-
}
|
1060
|
-
|
1061
|
-
// core/prompt/message-conversion-error.ts
|
1062
|
-
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
|
1063
|
-
var name6 = "AI_MessageConversionError";
|
1064
|
-
var marker6 = `vercel.ai.error.${name6}`;
|
1065
|
-
var symbol6 = Symbol.for(marker6);
|
1066
|
-
var _a6;
|
1067
|
-
var MessageConversionError = class extends AISDKError6 {
|
1068
|
-
constructor({
|
1069
|
-
originalMessage,
|
1070
|
-
message
|
1071
|
-
}) {
|
1072
|
-
super({ name: name6, message });
|
1073
|
-
this[_a6] = true;
|
1074
|
-
this.originalMessage = originalMessage;
|
1075
|
-
}
|
1076
|
-
static isInstance(error) {
|
1077
|
-
return AISDKError6.hasMarker(error, marker6);
|
1078
|
-
}
|
1079
|
-
};
|
1080
|
-
_a6 = symbol6;
|
182
|
+
_a2 = symbol2;
|
1081
183
|
|
1082
184
|
// core/prompt/convert-to-core-messages.ts
|
1083
185
|
function convertToCoreMessages(messages, options) {
|
1084
|
-
var
|
1085
|
-
const tools = (
|
186
|
+
var _a7, _b;
|
187
|
+
const tools = (_a7 = options == null ? void 0 : options.tools) != null ? _a7 : {};
|
1086
188
|
const coreMessages = [];
|
1087
189
|
for (let i = 0; i < messages.length; i++) {
|
1088
190
|
const message = messages[i];
|
@@ -1120,14 +222,23 @@ function convertToCoreMessages(messages, options) {
|
|
1120
222
|
case "assistant": {
|
1121
223
|
if (message.parts != null) {
|
1122
224
|
let processBlock2 = function() {
|
225
|
+
var _a8;
|
1123
226
|
const content2 = [];
|
1124
227
|
for (const part of block) {
|
1125
228
|
switch (part.type) {
|
1126
|
-
case "file":
|
1127
229
|
case "text": {
|
1128
230
|
content2.push(part);
|
1129
231
|
break;
|
1130
232
|
}
|
233
|
+
case "file": {
|
234
|
+
content2.push({
|
235
|
+
type: "file",
|
236
|
+
data: part.data,
|
237
|
+
mediaType: (_a8 = part.mediaType) != null ? _a8 : part.mimeType
|
238
|
+
// TODO migration, remove
|
239
|
+
});
|
240
|
+
break;
|
241
|
+
}
|
1131
242
|
case "reasoning": {
|
1132
243
|
for (const detail of part.details) {
|
1133
244
|
switch (detail.type) {
|
@@ -1239,14 +350,14 @@ function convertToCoreMessages(messages, options) {
|
|
1239
350
|
break;
|
1240
351
|
}
|
1241
352
|
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
1242
|
-
var
|
1243
|
-
return Math.max(max, (
|
353
|
+
var _a8;
|
354
|
+
return Math.max(max, (_a8 = toolInvocation.step) != null ? _a8 : 0);
|
1244
355
|
}, 0);
|
1245
356
|
for (let i2 = 0; i2 <= maxStep; i2++) {
|
1246
357
|
const stepInvocations = toolInvocations.filter(
|
1247
358
|
(toolInvocation) => {
|
1248
|
-
var
|
1249
|
-
return ((
|
359
|
+
var _a8;
|
360
|
+
return ((_a8 = toolInvocation.step) != null ? _a8 : 0) === i2;
|
1250
361
|
}
|
1251
362
|
);
|
1252
363
|
if (stepInvocations.length === 0) {
|
@@ -1295,845 +406,1038 @@ function convertToCoreMessages(messages, options) {
|
|
1295
406
|
if (content && !isLastMessage) {
|
1296
407
|
coreMessages.push({ role: "assistant", content });
|
1297
408
|
}
|
1298
|
-
break;
|
1299
|
-
}
|
1300
|
-
case "data": {
|
1301
|
-
break;
|
1302
|
-
}
|
1303
|
-
default: {
|
1304
|
-
const _exhaustiveCheck = role;
|
1305
|
-
throw new MessageConversionError({
|
1306
|
-
originalMessage: message,
|
1307
|
-
message: `Unsupported role: ${_exhaustiveCheck}`
|
1308
|
-
});
|
409
|
+
break;
|
410
|
+
}
|
411
|
+
case "data": {
|
412
|
+
break;
|
413
|
+
}
|
414
|
+
default: {
|
415
|
+
const _exhaustiveCheck = role;
|
416
|
+
throw new MessageConversionError({
|
417
|
+
originalMessage: message,
|
418
|
+
message: `Unsupported role: ${_exhaustiveCheck}`
|
419
|
+
});
|
420
|
+
}
|
421
|
+
}
|
422
|
+
}
|
423
|
+
return coreMessages;
|
424
|
+
}
|
425
|
+
|
426
|
+
// core/prompt/detect-prompt-type.ts
|
427
|
+
function detectPromptType(prompt) {
|
428
|
+
if (!Array.isArray(prompt)) {
|
429
|
+
return "other";
|
430
|
+
}
|
431
|
+
if (prompt.length === 0) {
|
432
|
+
return "messages";
|
433
|
+
}
|
434
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
435
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
436
|
+
return "ui-messages";
|
437
|
+
} else if (characteristics.every(
|
438
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
439
|
+
)) {
|
440
|
+
return "messages";
|
441
|
+
} else {
|
442
|
+
return "other";
|
443
|
+
}
|
444
|
+
}
|
445
|
+
function detectSingleMessageCharacteristics(message) {
|
446
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
447
|
+
message.role === "data" || // UI-only role
|
448
|
+
"toolInvocations" in message || // UI-specific field
|
449
|
+
"parts" in message || // UI-specific field
|
450
|
+
"experimental_attachments" in message)) {
|
451
|
+
return "has-ui-specific-parts";
|
452
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
453
|
+
"experimental_providerMetadata" in message || "providerOptions" in message)) {
|
454
|
+
return "has-core-specific-parts";
|
455
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
456
|
+
return "message";
|
457
|
+
} else {
|
458
|
+
return "other";
|
459
|
+
}
|
460
|
+
}
|
461
|
+
|
462
|
+
// core/prompt/message.ts
|
463
|
+
import { z as z6 } from "zod";
|
464
|
+
|
465
|
+
// core/types/provider-metadata.ts
|
466
|
+
import { z as z3 } from "zod";
|
467
|
+
|
468
|
+
// core/types/json-value.ts
|
469
|
+
import { z as z2 } from "zod";
|
470
|
+
var jsonValueSchema = z2.lazy(
|
471
|
+
() => z2.union([
|
472
|
+
z2.null(),
|
473
|
+
z2.string(),
|
474
|
+
z2.number(),
|
475
|
+
z2.boolean(),
|
476
|
+
z2.record(z2.string(), jsonValueSchema),
|
477
|
+
z2.array(jsonValueSchema)
|
478
|
+
])
|
479
|
+
);
|
480
|
+
|
481
|
+
// core/types/provider-metadata.ts
|
482
|
+
var providerMetadataSchema = z3.record(
|
483
|
+
z3.string(),
|
484
|
+
z3.record(z3.string(), jsonValueSchema)
|
485
|
+
);
|
486
|
+
|
487
|
+
// core/prompt/content-part.ts
|
488
|
+
import { z as z5 } from "zod";
|
489
|
+
|
490
|
+
// core/prompt/tool-result-content.ts
|
491
|
+
import { z as z4 } from "zod";
|
492
|
+
var toolResultContentSchema = z4.array(
|
493
|
+
z4.union([
|
494
|
+
z4.object({ type: z4.literal("text"), text: z4.string() }),
|
495
|
+
z4.object({
|
496
|
+
type: z4.literal("image"),
|
497
|
+
data: z4.string(),
|
498
|
+
mediaType: z4.string().optional()
|
499
|
+
})
|
500
|
+
])
|
501
|
+
);
|
502
|
+
|
503
|
+
// core/prompt/content-part.ts
|
504
|
+
var textPartSchema = z5.object({
|
505
|
+
type: z5.literal("text"),
|
506
|
+
text: z5.string(),
|
507
|
+
providerOptions: providerMetadataSchema.optional(),
|
508
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
509
|
+
});
|
510
|
+
var imagePartSchema = z5.object({
|
511
|
+
type: z5.literal("image"),
|
512
|
+
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
513
|
+
mediaType: z5.string().optional(),
|
514
|
+
mimeType: z5.string().optional(),
|
515
|
+
providerOptions: providerMetadataSchema.optional(),
|
516
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
517
|
+
});
|
518
|
+
var filePartSchema = z5.object({
|
519
|
+
type: z5.literal("file"),
|
520
|
+
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
521
|
+
filename: z5.string().optional(),
|
522
|
+
mediaType: z5.string(),
|
523
|
+
mimeType: z5.string().optional(),
|
524
|
+
providerOptions: providerMetadataSchema.optional(),
|
525
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
526
|
+
});
|
527
|
+
var reasoningPartSchema = z5.object({
|
528
|
+
type: z5.literal("reasoning"),
|
529
|
+
text: z5.string(),
|
530
|
+
providerOptions: providerMetadataSchema.optional(),
|
531
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
532
|
+
});
|
533
|
+
var redactedReasoningPartSchema = z5.object({
|
534
|
+
type: z5.literal("redacted-reasoning"),
|
535
|
+
data: z5.string(),
|
536
|
+
providerOptions: providerMetadataSchema.optional(),
|
537
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
538
|
+
});
|
539
|
+
var toolCallPartSchema = z5.object({
|
540
|
+
type: z5.literal("tool-call"),
|
541
|
+
toolCallId: z5.string(),
|
542
|
+
toolName: z5.string(),
|
543
|
+
args: z5.unknown(),
|
544
|
+
providerOptions: providerMetadataSchema.optional(),
|
545
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
546
|
+
});
|
547
|
+
var toolResultPartSchema = z5.object({
|
548
|
+
type: z5.literal("tool-result"),
|
549
|
+
toolCallId: z5.string(),
|
550
|
+
toolName: z5.string(),
|
551
|
+
result: z5.unknown(),
|
552
|
+
content: toolResultContentSchema.optional(),
|
553
|
+
isError: z5.boolean().optional(),
|
554
|
+
providerOptions: providerMetadataSchema.optional(),
|
555
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
556
|
+
});
|
557
|
+
|
558
|
+
// core/prompt/message.ts
|
559
|
+
var coreSystemMessageSchema = z6.object({
|
560
|
+
role: z6.literal("system"),
|
561
|
+
content: z6.string(),
|
562
|
+
providerOptions: providerMetadataSchema.optional(),
|
563
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
564
|
+
});
|
565
|
+
var coreUserMessageSchema = z6.object({
|
566
|
+
role: z6.literal("user"),
|
567
|
+
content: z6.union([
|
568
|
+
z6.string(),
|
569
|
+
z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
|
570
|
+
]),
|
571
|
+
providerOptions: providerMetadataSchema.optional(),
|
572
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
573
|
+
});
|
574
|
+
var coreAssistantMessageSchema = z6.object({
|
575
|
+
role: z6.literal("assistant"),
|
576
|
+
content: z6.union([
|
577
|
+
z6.string(),
|
578
|
+
z6.array(
|
579
|
+
z6.union([
|
580
|
+
textPartSchema,
|
581
|
+
filePartSchema,
|
582
|
+
reasoningPartSchema,
|
583
|
+
redactedReasoningPartSchema,
|
584
|
+
toolCallPartSchema
|
585
|
+
])
|
586
|
+
)
|
587
|
+
]),
|
588
|
+
providerOptions: providerMetadataSchema.optional(),
|
589
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
590
|
+
});
|
591
|
+
var coreToolMessageSchema = z6.object({
|
592
|
+
role: z6.literal("tool"),
|
593
|
+
content: z6.array(toolResultPartSchema),
|
594
|
+
providerOptions: providerMetadataSchema.optional(),
|
595
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
596
|
+
});
|
597
|
+
var coreMessageSchema = z6.union([
|
598
|
+
coreSystemMessageSchema,
|
599
|
+
coreUserMessageSchema,
|
600
|
+
coreAssistantMessageSchema,
|
601
|
+
coreToolMessageSchema
|
602
|
+
]);
|
603
|
+
|
604
|
+
// core/prompt/standardize-prompt.ts
|
605
|
+
function standardizePrompt({
|
606
|
+
prompt,
|
607
|
+
tools
|
608
|
+
}) {
|
609
|
+
if (prompt.prompt == null && prompt.messages == null) {
|
610
|
+
throw new InvalidPromptError({
|
611
|
+
prompt,
|
612
|
+
message: "prompt or messages must be defined"
|
613
|
+
});
|
614
|
+
}
|
615
|
+
if (prompt.prompt != null && prompt.messages != null) {
|
616
|
+
throw new InvalidPromptError({
|
617
|
+
prompt,
|
618
|
+
message: "prompt and messages cannot be defined at the same time"
|
619
|
+
});
|
620
|
+
}
|
621
|
+
if (prompt.system != null && typeof prompt.system !== "string") {
|
622
|
+
throw new InvalidPromptError({
|
623
|
+
prompt,
|
624
|
+
message: "system must be a string"
|
625
|
+
});
|
626
|
+
}
|
627
|
+
if (prompt.prompt != null) {
|
628
|
+
if (typeof prompt.prompt !== "string") {
|
629
|
+
throw new InvalidPromptError({
|
630
|
+
prompt,
|
631
|
+
message: "prompt must be a string"
|
632
|
+
});
|
633
|
+
}
|
634
|
+
return {
|
635
|
+
type: "prompt",
|
636
|
+
system: prompt.system,
|
637
|
+
messages: [
|
638
|
+
{
|
639
|
+
role: "user",
|
640
|
+
content: prompt.prompt
|
641
|
+
}
|
642
|
+
]
|
643
|
+
};
|
644
|
+
}
|
645
|
+
if (prompt.messages != null) {
|
646
|
+
const promptType = detectPromptType(prompt.messages);
|
647
|
+
if (promptType === "other") {
|
648
|
+
throw new InvalidPromptError({
|
649
|
+
prompt,
|
650
|
+
message: "messages must be an array of CoreMessage or UIMessage"
|
651
|
+
});
|
652
|
+
}
|
653
|
+
const messages = promptType === "ui-messages" ? convertToCoreMessages(prompt.messages, {
|
654
|
+
tools
|
655
|
+
}) : prompt.messages;
|
656
|
+
if (messages.length === 0) {
|
657
|
+
throw new InvalidPromptError({
|
658
|
+
prompt,
|
659
|
+
message: "messages must not be empty"
|
660
|
+
});
|
661
|
+
}
|
662
|
+
const validationResult = safeValidateTypes({
|
663
|
+
value: messages,
|
664
|
+
schema: z7.array(coreMessageSchema)
|
665
|
+
});
|
666
|
+
if (!validationResult.success) {
|
667
|
+
throw new InvalidPromptError({
|
668
|
+
prompt,
|
669
|
+
message: "messages must be an array of CoreMessage or UIMessage",
|
670
|
+
cause: validationResult.error
|
671
|
+
});
|
672
|
+
}
|
673
|
+
return {
|
674
|
+
type: "messages",
|
675
|
+
messages,
|
676
|
+
system: prompt.system
|
677
|
+
};
|
678
|
+
}
|
679
|
+
throw new Error("unreachable");
|
680
|
+
}
|
681
|
+
|
682
|
+
// core/util/index.ts
|
683
|
+
import { generateId } from "@ai-sdk/provider-utils";
|
684
|
+
|
685
|
+
// core/util/schema.ts
|
686
|
+
import { validatorSymbol } from "@ai-sdk/provider-utils";
|
687
|
+
|
688
|
+
// core/util/zod-schema.ts
|
689
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
690
|
+
function zodSchema(zodSchema2, options) {
|
691
|
+
var _a7;
|
692
|
+
const useReferences = (_a7 = options == null ? void 0 : options.useReferences) != null ? _a7 : false;
|
693
|
+
return jsonSchema(
|
694
|
+
zodToJsonSchema(zodSchema2, {
|
695
|
+
$refStrategy: useReferences ? "root" : "none",
|
696
|
+
target: "jsonSchema7"
|
697
|
+
// note: openai mode breaks various gemini conversions
|
698
|
+
}),
|
699
|
+
{
|
700
|
+
validate: (value) => {
|
701
|
+
const result = zodSchema2.safeParse(value);
|
702
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
1309
703
|
}
|
1310
704
|
}
|
705
|
+
);
|
706
|
+
}
|
707
|
+
|
708
|
+
// core/util/schema.ts
|
709
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
710
|
+
function jsonSchema(jsonSchema2, {
|
711
|
+
validate
|
712
|
+
} = {}) {
|
713
|
+
return {
|
714
|
+
[schemaSymbol]: true,
|
715
|
+
_type: void 0,
|
716
|
+
// should never be used directly
|
717
|
+
[validatorSymbol]: true,
|
718
|
+
jsonSchema: jsonSchema2,
|
719
|
+
validate
|
720
|
+
};
|
721
|
+
}
|
722
|
+
function isSchema(value) {
|
723
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
724
|
+
}
|
725
|
+
function asSchema(schema) {
|
726
|
+
return isSchema(schema) ? schema : zodSchema(schema);
|
727
|
+
}
|
728
|
+
|
729
|
+
// core/util/is-non-empty-object.ts
|
730
|
+
function isNonEmptyObject(object) {
|
731
|
+
return object != null && Object.keys(object).length > 0;
|
732
|
+
}
|
733
|
+
|
734
|
+
// core/prompt/prepare-tools-and-tool-choice.ts
|
735
|
+
function prepareToolsAndToolChoice({
|
736
|
+
tools,
|
737
|
+
toolChoice,
|
738
|
+
activeTools
|
739
|
+
}) {
|
740
|
+
if (!isNonEmptyObject(tools)) {
|
741
|
+
return {
|
742
|
+
tools: void 0,
|
743
|
+
toolChoice: void 0
|
744
|
+
};
|
1311
745
|
}
|
1312
|
-
|
746
|
+
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
747
|
+
([name7]) => activeTools.includes(name7)
|
748
|
+
) : Object.entries(tools);
|
749
|
+
return {
|
750
|
+
tools: filteredTools.map(([name7, tool]) => {
|
751
|
+
const toolType = tool.type;
|
752
|
+
switch (toolType) {
|
753
|
+
case void 0:
|
754
|
+
case "function":
|
755
|
+
return {
|
756
|
+
type: "function",
|
757
|
+
name: name7,
|
758
|
+
description: tool.description,
|
759
|
+
parameters: asSchema(tool.parameters).jsonSchema
|
760
|
+
};
|
761
|
+
case "provider-defined":
|
762
|
+
return {
|
763
|
+
type: "provider-defined",
|
764
|
+
name: name7,
|
765
|
+
id: tool.id,
|
766
|
+
args: tool.args
|
767
|
+
};
|
768
|
+
default: {
|
769
|
+
const exhaustiveCheck = toolType;
|
770
|
+
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
771
|
+
}
|
772
|
+
}
|
773
|
+
}),
|
774
|
+
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
775
|
+
};
|
1313
776
|
}
|
1314
777
|
|
1315
|
-
//
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
778
|
+
// errors/invalid-argument-error.ts
|
779
|
+
import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
|
780
|
+
var name3 = "AI_InvalidArgumentError";
|
781
|
+
var marker3 = `vercel.ai.error.${name3}`;
|
782
|
+
var symbol3 = Symbol.for(marker3);
|
783
|
+
var _a3;
|
784
|
+
var InvalidArgumentError = class extends AISDKError3 {
|
785
|
+
constructor({
|
786
|
+
parameter,
|
787
|
+
value,
|
788
|
+
message
|
789
|
+
}) {
|
790
|
+
super({
|
791
|
+
name: name3,
|
792
|
+
message: `Invalid argument for parameter ${parameter}: ${message}`
|
793
|
+
});
|
794
|
+
this[_a3] = true;
|
795
|
+
this.parameter = parameter;
|
796
|
+
this.value = value;
|
1319
797
|
}
|
1320
|
-
|
1321
|
-
return
|
798
|
+
static isInstance(error) {
|
799
|
+
return AISDKError3.hasMarker(error, marker3);
|
1322
800
|
}
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
801
|
+
};
|
802
|
+
_a3 = symbol3;
|
803
|
+
|
804
|
+
// util/retry-with-exponential-backoff.ts
|
805
|
+
import { APICallError } from "@ai-sdk/provider";
|
806
|
+
import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
807
|
+
|
808
|
+
// util/retry-error.ts
|
809
|
+
import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
|
810
|
+
var name4 = "AI_RetryError";
|
811
|
+
var marker4 = `vercel.ai.error.${name4}`;
|
812
|
+
var symbol4 = Symbol.for(marker4);
|
813
|
+
var _a4;
|
814
|
+
var RetryError = class extends AISDKError4 {
|
815
|
+
constructor({
|
816
|
+
message,
|
817
|
+
reason,
|
818
|
+
errors
|
819
|
+
}) {
|
820
|
+
super({ name: name4, message });
|
821
|
+
this[_a4] = true;
|
822
|
+
this.reason = reason;
|
823
|
+
this.errors = errors;
|
824
|
+
this.lastError = errors[errors.length - 1];
|
825
|
+
}
|
826
|
+
static isInstance(error) {
|
827
|
+
return AISDKError4.hasMarker(error, marker4);
|
828
|
+
}
|
829
|
+
};
|
830
|
+
_a4 = symbol4;
|
831
|
+
|
832
|
+
// util/retry-with-exponential-backoff.ts
|
833
|
+
var retryWithExponentialBackoff = ({
|
834
|
+
maxRetries = 2,
|
835
|
+
initialDelayInMs = 2e3,
|
836
|
+
backoffFactor = 2
|
837
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
838
|
+
maxRetries,
|
839
|
+
delayInMs: initialDelayInMs,
|
840
|
+
backoffFactor
|
841
|
+
});
|
842
|
+
async function _retryWithExponentialBackoff(f, {
|
843
|
+
maxRetries,
|
844
|
+
delayInMs,
|
845
|
+
backoffFactor
|
846
|
+
}, errors = []) {
|
847
|
+
try {
|
848
|
+
return await f();
|
849
|
+
} catch (error) {
|
850
|
+
if (isAbortError(error)) {
|
851
|
+
throw error;
|
852
|
+
}
|
853
|
+
if (maxRetries === 0) {
|
854
|
+
throw error;
|
855
|
+
}
|
856
|
+
const errorMessage = getErrorMessage(error);
|
857
|
+
const newErrors = [...errors, error];
|
858
|
+
const tryNumber = newErrors.length;
|
859
|
+
if (tryNumber > maxRetries) {
|
860
|
+
throw new RetryError({
|
861
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
862
|
+
reason: "maxRetriesExceeded",
|
863
|
+
errors: newErrors
|
864
|
+
});
|
865
|
+
}
|
866
|
+
if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
867
|
+
await delay(delayInMs);
|
868
|
+
return _retryWithExponentialBackoff(
|
869
|
+
f,
|
870
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
871
|
+
newErrors
|
872
|
+
);
|
873
|
+
}
|
874
|
+
if (tryNumber === 1) {
|
875
|
+
throw error;
|
876
|
+
}
|
877
|
+
throw new RetryError({
|
878
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
879
|
+
reason: "errorNotRetryable",
|
880
|
+
errors: newErrors
|
881
|
+
});
|
1332
882
|
}
|
1333
883
|
}
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
884
|
+
|
885
|
+
// core/prompt/prepare-retries.ts
|
886
|
+
function prepareRetries({
|
887
|
+
maxRetries
|
888
|
+
}) {
|
889
|
+
if (maxRetries != null) {
|
890
|
+
if (!Number.isInteger(maxRetries)) {
|
891
|
+
throw new InvalidArgumentError({
|
892
|
+
parameter: "maxRetries",
|
893
|
+
value: maxRetries,
|
894
|
+
message: "maxRetries must be an integer"
|
895
|
+
});
|
896
|
+
}
|
897
|
+
if (maxRetries < 0) {
|
898
|
+
throw new InvalidArgumentError({
|
899
|
+
parameter: "maxRetries",
|
900
|
+
value: maxRetries,
|
901
|
+
message: "maxRetries must be >= 0"
|
902
|
+
});
|
903
|
+
}
|
1348
904
|
}
|
905
|
+
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
906
|
+
return {
|
907
|
+
maxRetries: maxRetriesResult,
|
908
|
+
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
|
909
|
+
};
|
1349
910
|
}
|
1350
911
|
|
1351
|
-
// core/prompt/
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
z2.null(),
|
1362
|
-
z2.string(),
|
1363
|
-
z2.number(),
|
1364
|
-
z2.boolean(),
|
1365
|
-
z2.record(z2.string(), jsonValueSchema),
|
1366
|
-
z2.array(jsonValueSchema)
|
1367
|
-
])
|
1368
|
-
);
|
1369
|
-
|
1370
|
-
// core/types/provider-metadata.ts
|
1371
|
-
var providerMetadataSchema = z3.record(
|
1372
|
-
z3.string(),
|
1373
|
-
z3.record(z3.string(), jsonValueSchema)
|
1374
|
-
);
|
1375
|
-
|
1376
|
-
// core/prompt/content-part.ts
|
1377
|
-
import { z as z5 } from "zod";
|
1378
|
-
|
1379
|
-
// core/prompt/tool-result-content.ts
|
1380
|
-
import { z as z4 } from "zod";
|
1381
|
-
var toolResultContentSchema = z4.array(
|
1382
|
-
z4.union([
|
1383
|
-
z4.object({ type: z4.literal("text"), text: z4.string() }),
|
1384
|
-
z4.object({
|
1385
|
-
type: z4.literal("image"),
|
1386
|
-
data: z4.string(),
|
1387
|
-
mimeType: z4.string().optional()
|
1388
|
-
})
|
1389
|
-
])
|
1390
|
-
);
|
1391
|
-
|
1392
|
-
// core/prompt/content-part.ts
|
1393
|
-
var textPartSchema = z5.object({
|
1394
|
-
type: z5.literal("text"),
|
1395
|
-
text: z5.string(),
|
1396
|
-
providerOptions: providerMetadataSchema.optional(),
|
1397
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1398
|
-
});
|
1399
|
-
var imagePartSchema = z5.object({
|
1400
|
-
type: z5.literal("image"),
|
1401
|
-
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
1402
|
-
mimeType: z5.string().optional(),
|
1403
|
-
providerOptions: providerMetadataSchema.optional(),
|
1404
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1405
|
-
});
|
1406
|
-
var filePartSchema = z5.object({
|
1407
|
-
type: z5.literal("file"),
|
1408
|
-
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
1409
|
-
filename: z5.string().optional(),
|
1410
|
-
mimeType: z5.string(),
|
1411
|
-
providerOptions: providerMetadataSchema.optional(),
|
1412
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1413
|
-
});
|
1414
|
-
var reasoningPartSchema = z5.object({
|
1415
|
-
type: z5.literal("reasoning"),
|
1416
|
-
text: z5.string(),
|
1417
|
-
providerOptions: providerMetadataSchema.optional(),
|
1418
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1419
|
-
});
|
1420
|
-
var redactedReasoningPartSchema = z5.object({
|
1421
|
-
type: z5.literal("redacted-reasoning"),
|
1422
|
-
data: z5.string(),
|
1423
|
-
providerOptions: providerMetadataSchema.optional(),
|
1424
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1425
|
-
});
|
1426
|
-
var toolCallPartSchema = z5.object({
|
1427
|
-
type: z5.literal("tool-call"),
|
1428
|
-
toolCallId: z5.string(),
|
1429
|
-
toolName: z5.string(),
|
1430
|
-
args: z5.unknown(),
|
1431
|
-
providerOptions: providerMetadataSchema.optional(),
|
1432
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1433
|
-
});
|
1434
|
-
var toolResultPartSchema = z5.object({
|
1435
|
-
type: z5.literal("tool-result"),
|
1436
|
-
toolCallId: z5.string(),
|
1437
|
-
toolName: z5.string(),
|
1438
|
-
result: z5.unknown(),
|
1439
|
-
content: toolResultContentSchema.optional(),
|
1440
|
-
isError: z5.boolean().optional(),
|
1441
|
-
providerOptions: providerMetadataSchema.optional(),
|
1442
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1443
|
-
});
|
1444
|
-
|
1445
|
-
// core/prompt/message.ts
|
1446
|
-
var coreSystemMessageSchema = z6.object({
|
1447
|
-
role: z6.literal("system"),
|
1448
|
-
content: z6.string(),
|
1449
|
-
providerOptions: providerMetadataSchema.optional(),
|
1450
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1451
|
-
});
|
1452
|
-
var coreUserMessageSchema = z6.object({
|
1453
|
-
role: z6.literal("user"),
|
1454
|
-
content: z6.union([
|
1455
|
-
z6.string(),
|
1456
|
-
z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
|
1457
|
-
]),
|
1458
|
-
providerOptions: providerMetadataSchema.optional(),
|
1459
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1460
|
-
});
|
1461
|
-
var coreAssistantMessageSchema = z6.object({
|
1462
|
-
role: z6.literal("assistant"),
|
1463
|
-
content: z6.union([
|
1464
|
-
z6.string(),
|
1465
|
-
z6.array(
|
1466
|
-
z6.union([
|
1467
|
-
textPartSchema,
|
1468
|
-
filePartSchema,
|
1469
|
-
reasoningPartSchema,
|
1470
|
-
redactedReasoningPartSchema,
|
1471
|
-
toolCallPartSchema
|
1472
|
-
])
|
1473
|
-
)
|
1474
|
-
]),
|
1475
|
-
providerOptions: providerMetadataSchema.optional(),
|
1476
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1477
|
-
});
|
1478
|
-
var coreToolMessageSchema = z6.object({
|
1479
|
-
role: z6.literal("tool"),
|
1480
|
-
content: z6.array(toolResultPartSchema),
|
1481
|
-
providerOptions: providerMetadataSchema.optional(),
|
1482
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1483
|
-
});
|
1484
|
-
var coreMessageSchema = z6.union([
|
1485
|
-
coreSystemMessageSchema,
|
1486
|
-
coreUserMessageSchema,
|
1487
|
-
coreAssistantMessageSchema,
|
1488
|
-
coreToolMessageSchema
|
1489
|
-
]);
|
1490
|
-
|
1491
|
-
// core/prompt/standardize-prompt.ts
|
1492
|
-
function standardizePrompt({
|
1493
|
-
prompt,
|
1494
|
-
tools
|
912
|
+
// core/prompt/prepare-call-settings.ts
|
913
|
+
function prepareCallSettings({
|
914
|
+
maxTokens,
|
915
|
+
temperature,
|
916
|
+
topP,
|
917
|
+
topK,
|
918
|
+
presencePenalty,
|
919
|
+
frequencyPenalty,
|
920
|
+
stopSequences,
|
921
|
+
seed
|
1495
922
|
}) {
|
1496
|
-
if (
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
923
|
+
if (maxTokens != null) {
|
924
|
+
if (!Number.isInteger(maxTokens)) {
|
925
|
+
throw new InvalidArgumentError({
|
926
|
+
parameter: "maxTokens",
|
927
|
+
value: maxTokens,
|
928
|
+
message: "maxTokens must be an integer"
|
929
|
+
});
|
930
|
+
}
|
931
|
+
if (maxTokens < 1) {
|
932
|
+
throw new InvalidArgumentError({
|
933
|
+
parameter: "maxTokens",
|
934
|
+
value: maxTokens,
|
935
|
+
message: "maxTokens must be >= 1"
|
936
|
+
});
|
937
|
+
}
|
1507
938
|
}
|
1508
|
-
if (
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
939
|
+
if (temperature != null) {
|
940
|
+
if (typeof temperature !== "number") {
|
941
|
+
throw new InvalidArgumentError({
|
942
|
+
parameter: "temperature",
|
943
|
+
value: temperature,
|
944
|
+
message: "temperature must be a number"
|
945
|
+
});
|
946
|
+
}
|
1513
947
|
}
|
1514
|
-
if (
|
1515
|
-
if (typeof
|
1516
|
-
throw new
|
1517
|
-
|
1518
|
-
|
948
|
+
if (topP != null) {
|
949
|
+
if (typeof topP !== "number") {
|
950
|
+
throw new InvalidArgumentError({
|
951
|
+
parameter: "topP",
|
952
|
+
value: topP,
|
953
|
+
message: "topP must be a number"
|
1519
954
|
});
|
1520
955
|
}
|
1521
|
-
return {
|
1522
|
-
type: "prompt",
|
1523
|
-
system: prompt.system,
|
1524
|
-
messages: [
|
1525
|
-
{
|
1526
|
-
role: "user",
|
1527
|
-
content: prompt.prompt
|
1528
|
-
}
|
1529
|
-
]
|
1530
|
-
};
|
1531
956
|
}
|
1532
|
-
if (
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
message: "
|
957
|
+
if (topK != null) {
|
958
|
+
if (typeof topK !== "number") {
|
959
|
+
throw new InvalidArgumentError({
|
960
|
+
parameter: "topK",
|
961
|
+
value: topK,
|
962
|
+
message: "topK must be a number"
|
1538
963
|
});
|
1539
964
|
}
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
message: "
|
965
|
+
}
|
966
|
+
if (presencePenalty != null) {
|
967
|
+
if (typeof presencePenalty !== "number") {
|
968
|
+
throw new InvalidArgumentError({
|
969
|
+
parameter: "presencePenalty",
|
970
|
+
value: presencePenalty,
|
971
|
+
message: "presencePenalty must be a number"
|
1547
972
|
});
|
1548
973
|
}
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
974
|
+
}
|
975
|
+
if (frequencyPenalty != null) {
|
976
|
+
if (typeof frequencyPenalty !== "number") {
|
977
|
+
throw new InvalidArgumentError({
|
978
|
+
parameter: "frequencyPenalty",
|
979
|
+
value: frequencyPenalty,
|
980
|
+
message: "frequencyPenalty must be a number"
|
981
|
+
});
|
982
|
+
}
|
983
|
+
}
|
984
|
+
if (seed != null) {
|
985
|
+
if (!Number.isInteger(seed)) {
|
986
|
+
throw new InvalidArgumentError({
|
987
|
+
parameter: "seed",
|
988
|
+
value: seed,
|
989
|
+
message: "seed must be an integer"
|
1558
990
|
});
|
1559
991
|
}
|
1560
|
-
return {
|
1561
|
-
type: "messages",
|
1562
|
-
messages,
|
1563
|
-
system: prompt.system
|
1564
|
-
};
|
1565
992
|
}
|
1566
|
-
throw new Error("unreachable");
|
1567
|
-
}
|
1568
|
-
|
1569
|
-
// core/types/usage.ts
|
1570
|
-
function calculateLanguageModelUsage({
|
1571
|
-
promptTokens,
|
1572
|
-
completionTokens
|
1573
|
-
}) {
|
1574
993
|
return {
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
994
|
+
maxTokens,
|
995
|
+
// TODO v5 remove default 0 for temperature
|
996
|
+
temperature: temperature != null ? temperature : 0,
|
997
|
+
topP,
|
998
|
+
topK,
|
999
|
+
presencePenalty,
|
1000
|
+
frequencyPenalty,
|
1001
|
+
stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
|
1002
|
+
seed
|
1578
1003
|
};
|
1579
1004
|
}
|
1580
1005
|
|
1581
|
-
//
|
1582
|
-
import {
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
var
|
1587
|
-
var
|
1006
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
1007
|
+
import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
|
1008
|
+
|
1009
|
+
// util/download-error.ts
|
1010
|
+
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
1011
|
+
var name5 = "AI_DownloadError";
|
1012
|
+
var marker5 = `vercel.ai.error.${name5}`;
|
1013
|
+
var symbol5 = Symbol.for(marker5);
|
1014
|
+
var _a5;
|
1015
|
+
var DownloadError = class extends AISDKError5 {
|
1588
1016
|
constructor({
|
1589
|
-
|
1590
|
-
|
1017
|
+
url,
|
1018
|
+
statusCode,
|
1019
|
+
statusText,
|
1591
1020
|
cause,
|
1592
|
-
message = `
|
1593
|
-
cause
|
1594
|
-
)}`
|
1021
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
1595
1022
|
}) {
|
1596
|
-
super({ name:
|
1597
|
-
this[
|
1598
|
-
this.
|
1599
|
-
this.
|
1023
|
+
super({ name: name5, message, cause });
|
1024
|
+
this[_a5] = true;
|
1025
|
+
this.url = url;
|
1026
|
+
this.statusCode = statusCode;
|
1027
|
+
this.statusText = statusText;
|
1600
1028
|
}
|
1601
1029
|
static isInstance(error) {
|
1602
|
-
return
|
1030
|
+
return AISDKError5.hasMarker(error, marker5);
|
1603
1031
|
}
|
1604
1032
|
};
|
1605
|
-
|
1033
|
+
_a5 = symbol5;
|
1034
|
+
|
1035
|
+
// util/download.ts
|
1036
|
+
async function download({ url }) {
|
1037
|
+
var _a7;
|
1038
|
+
const urlText = url.toString();
|
1039
|
+
try {
|
1040
|
+
const response = await fetch(urlText);
|
1041
|
+
if (!response.ok) {
|
1042
|
+
throw new DownloadError({
|
1043
|
+
url: urlText,
|
1044
|
+
statusCode: response.status,
|
1045
|
+
statusText: response.statusText
|
1046
|
+
});
|
1047
|
+
}
|
1048
|
+
return {
|
1049
|
+
data: new Uint8Array(await response.arrayBuffer()),
|
1050
|
+
mediaType: (_a7 = response.headers.get("content-type")) != null ? _a7 : void 0
|
1051
|
+
};
|
1052
|
+
} catch (error) {
|
1053
|
+
if (DownloadError.isInstance(error)) {
|
1054
|
+
throw error;
|
1055
|
+
}
|
1056
|
+
throw new DownloadError({ url: urlText, cause: error });
|
1057
|
+
}
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
// core/util/detect-media-type.ts
|
1061
|
+
var imageMediaTypeSignatures = [
|
1062
|
+
{
|
1063
|
+
mediaType: "image/gif",
|
1064
|
+
bytesPrefix: [71, 73, 70],
|
1065
|
+
base64Prefix: "R0lG"
|
1066
|
+
},
|
1067
|
+
{
|
1068
|
+
mediaType: "image/png",
|
1069
|
+
bytesPrefix: [137, 80, 78, 71],
|
1070
|
+
base64Prefix: "iVBORw"
|
1071
|
+
},
|
1072
|
+
{
|
1073
|
+
mediaType: "image/jpeg",
|
1074
|
+
bytesPrefix: [255, 216],
|
1075
|
+
base64Prefix: "/9j/"
|
1076
|
+
},
|
1077
|
+
{
|
1078
|
+
mediaType: "image/webp",
|
1079
|
+
bytesPrefix: [82, 73, 70, 70],
|
1080
|
+
base64Prefix: "UklGRg"
|
1081
|
+
},
|
1082
|
+
{
|
1083
|
+
mediaType: "image/bmp",
|
1084
|
+
bytesPrefix: [66, 77],
|
1085
|
+
base64Prefix: "Qk"
|
1086
|
+
},
|
1087
|
+
{
|
1088
|
+
mediaType: "image/tiff",
|
1089
|
+
bytesPrefix: [73, 73, 42, 0],
|
1090
|
+
base64Prefix: "SUkqAA"
|
1091
|
+
},
|
1092
|
+
{
|
1093
|
+
mediaType: "image/tiff",
|
1094
|
+
bytesPrefix: [77, 77, 0, 42],
|
1095
|
+
base64Prefix: "TU0AKg"
|
1096
|
+
},
|
1097
|
+
{
|
1098
|
+
mediaType: "image/avif",
|
1099
|
+
bytesPrefix: [
|
1100
|
+
0,
|
1101
|
+
0,
|
1102
|
+
0,
|
1103
|
+
32,
|
1104
|
+
102,
|
1105
|
+
116,
|
1106
|
+
121,
|
1107
|
+
112,
|
1108
|
+
97,
|
1109
|
+
118,
|
1110
|
+
105,
|
1111
|
+
102
|
1112
|
+
],
|
1113
|
+
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
1114
|
+
},
|
1115
|
+
{
|
1116
|
+
mediaType: "image/heic",
|
1117
|
+
bytesPrefix: [
|
1118
|
+
0,
|
1119
|
+
0,
|
1120
|
+
0,
|
1121
|
+
32,
|
1122
|
+
102,
|
1123
|
+
116,
|
1124
|
+
121,
|
1125
|
+
112,
|
1126
|
+
104,
|
1127
|
+
101,
|
1128
|
+
105,
|
1129
|
+
99
|
1130
|
+
],
|
1131
|
+
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
1132
|
+
}
|
1133
|
+
];
|
1134
|
+
function detectMediaType({
|
1135
|
+
data,
|
1136
|
+
signatures
|
1137
|
+
}) {
|
1138
|
+
for (const signature of signatures) {
|
1139
|
+
if (typeof data === "string" ? data.startsWith(signature.base64Prefix) : data.length >= signature.bytesPrefix.length && signature.bytesPrefix.every((byte, index) => data[index] === byte)) {
|
1140
|
+
return signature.mediaType;
|
1141
|
+
}
|
1142
|
+
}
|
1143
|
+
return void 0;
|
1144
|
+
}
|
1606
1145
|
|
1607
|
-
//
|
1608
|
-
import { AISDKError as
|
1609
|
-
var
|
1610
|
-
var
|
1611
|
-
var
|
1612
|
-
var
|
1613
|
-
var
|
1146
|
+
// core/prompt/invalid-message-role-error.ts
|
1147
|
+
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
|
1148
|
+
var name6 = "AI_InvalidMessageRoleError";
|
1149
|
+
var marker6 = `vercel.ai.error.${name6}`;
|
1150
|
+
var symbol6 = Symbol.for(marker6);
|
1151
|
+
var _a6;
|
1152
|
+
var InvalidMessageRoleError = class extends AISDKError6 {
|
1614
1153
|
constructor({
|
1615
|
-
|
1616
|
-
|
1617
|
-
message = `Model tried to call unavailable tool '${toolName}'. ${availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`}`
|
1154
|
+
role,
|
1155
|
+
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
1618
1156
|
}) {
|
1619
|
-
super({ name:
|
1620
|
-
this[
|
1621
|
-
this.
|
1622
|
-
this.availableTools = availableTools;
|
1157
|
+
super({ name: name6, message });
|
1158
|
+
this[_a6] = true;
|
1159
|
+
this.role = role;
|
1623
1160
|
}
|
1624
1161
|
static isInstance(error) {
|
1625
|
-
return
|
1162
|
+
return AISDKError6.hasMarker(error, marker6);
|
1626
1163
|
}
|
1627
1164
|
};
|
1628
|
-
|
1629
|
-
|
1630
|
-
// util/is-async-generator.ts
|
1631
|
-
function isAsyncGenerator(value) {
|
1632
|
-
return value != null && typeof value === "object" && Symbol.asyncIterator in value;
|
1633
|
-
}
|
1634
|
-
|
1635
|
-
// util/is-generator.ts
|
1636
|
-
function isGenerator(value) {
|
1637
|
-
return value != null && typeof value === "object" && Symbol.iterator in value;
|
1638
|
-
}
|
1639
|
-
|
1640
|
-
// util/constants.ts
|
1641
|
-
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
1165
|
+
_a6 = symbol6;
|
1642
1166
|
|
1643
|
-
//
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
1657
|
-
current,
|
1658
|
-
/* @__PURE__ */ jsx2(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx2(R, { c: chunk.value, n: chunk.next }) })
|
1659
|
-
] });
|
1660
|
-
}
|
1661
|
-
return /* @__PURE__ */ jsx2(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx2(R, { c: chunk.value, n: chunk.next }) });
|
1167
|
+
// core/prompt/split-data-url.ts
|
1168
|
+
function splitDataUrl(dataUrl) {
|
1169
|
+
try {
|
1170
|
+
const [header, base64Content] = dataUrl.split(",");
|
1171
|
+
return {
|
1172
|
+
mediaType: header.split(";")[0].split(":")[1],
|
1173
|
+
base64Content
|
1174
|
+
};
|
1175
|
+
} catch (error) {
|
1176
|
+
return {
|
1177
|
+
mediaType: void 0,
|
1178
|
+
base64Content: void 0
|
1179
|
+
};
|
1662
1180
|
}
|
1663
|
-
][0];
|
1664
|
-
function createSuspendedChunk(initialValue) {
|
1665
|
-
const { promise, resolve, reject } = createResolvablePromise();
|
1666
|
-
return {
|
1667
|
-
row: /* @__PURE__ */ jsx2(Suspense, { fallback: initialValue, children: /* @__PURE__ */ jsx2(R, { c: initialValue, n: promise }) }),
|
1668
|
-
resolve,
|
1669
|
-
reject
|
1670
|
-
};
|
1671
1181
|
}
|
1672
1182
|
|
1673
|
-
//
|
1674
|
-
function
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
return streamable;
|
1704
|
-
}
|
1705
|
-
const resolvable = createResolvablePromise();
|
1706
|
-
currentValue = value;
|
1707
|
-
resolve({ value: currentValue, done: false, next: resolvable.promise });
|
1708
|
-
resolve = resolvable.resolve;
|
1709
|
-
reject = resolvable.reject;
|
1710
|
-
warnUnclosedStream();
|
1711
|
-
return streamable;
|
1712
|
-
},
|
1713
|
-
append(value) {
|
1714
|
-
assertStream(".append()");
|
1715
|
-
const resolvable = createResolvablePromise();
|
1716
|
-
currentValue = value;
|
1717
|
-
resolve({ value, done: false, append: true, next: resolvable.promise });
|
1718
|
-
resolve = resolvable.resolve;
|
1719
|
-
reject = resolvable.reject;
|
1720
|
-
warnUnclosedStream();
|
1721
|
-
return streamable;
|
1722
|
-
},
|
1723
|
-
error(error) {
|
1724
|
-
assertStream(".error()");
|
1725
|
-
if (warningTimeout) {
|
1726
|
-
clearTimeout(warningTimeout);
|
1727
|
-
}
|
1728
|
-
closed = true;
|
1729
|
-
reject(error);
|
1730
|
-
return streamable;
|
1731
|
-
},
|
1732
|
-
done(...args) {
|
1733
|
-
assertStream(".done()");
|
1734
|
-
if (warningTimeout) {
|
1735
|
-
clearTimeout(warningTimeout);
|
1736
|
-
}
|
1737
|
-
closed = true;
|
1738
|
-
if (args.length) {
|
1739
|
-
resolve({ value: args[0], done: true });
|
1740
|
-
return streamable;
|
1741
|
-
}
|
1742
|
-
resolve({ value: currentValue, done: true });
|
1743
|
-
return streamable;
|
1183
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
1184
|
+
async function convertToLanguageModelPrompt({
|
1185
|
+
prompt,
|
1186
|
+
modelSupportsImageUrls = true,
|
1187
|
+
modelSupportsUrl = () => false,
|
1188
|
+
downloadImplementation = download
|
1189
|
+
}) {
|
1190
|
+
const downloadedAssets = await downloadAssets(
|
1191
|
+
prompt.messages,
|
1192
|
+
downloadImplementation,
|
1193
|
+
modelSupportsImageUrls,
|
1194
|
+
modelSupportsUrl
|
1195
|
+
);
|
1196
|
+
return [
|
1197
|
+
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
1198
|
+
...prompt.messages.map(
|
1199
|
+
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
1200
|
+
)
|
1201
|
+
];
|
1202
|
+
}
|
1203
|
+
function convertToLanguageModelMessage(message, downloadedAssets) {
|
1204
|
+
var _a7, _b, _c, _d, _e, _f;
|
1205
|
+
const role = message.role;
|
1206
|
+
switch (role) {
|
1207
|
+
case "system": {
|
1208
|
+
return {
|
1209
|
+
role: "system",
|
1210
|
+
content: message.content,
|
1211
|
+
providerOptions: (_a7 = message.providerOptions) != null ? _a7 : message.experimental_providerMetadata
|
1212
|
+
};
|
1744
1213
|
}
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
model,
|
1753
|
-
tools,
|
1754
|
-
toolChoice,
|
1755
|
-
system,
|
1756
|
-
prompt,
|
1757
|
-
messages,
|
1758
|
-
maxRetries,
|
1759
|
-
abortSignal,
|
1760
|
-
headers,
|
1761
|
-
initial,
|
1762
|
-
text,
|
1763
|
-
experimental_providerMetadata,
|
1764
|
-
providerOptions = experimental_providerMetadata,
|
1765
|
-
onFinish,
|
1766
|
-
...settings
|
1767
|
-
}) {
|
1768
|
-
if (typeof model === "string") {
|
1769
|
-
throw new Error(
|
1770
|
-
"`model` cannot be a string in `streamUI`. Use the actual model instance instead."
|
1771
|
-
);
|
1772
|
-
}
|
1773
|
-
if ("functions" in settings) {
|
1774
|
-
throw new Error(
|
1775
|
-
"`functions` is not supported in `streamUI`, use `tools` instead."
|
1776
|
-
);
|
1777
|
-
}
|
1778
|
-
if ("provider" in settings) {
|
1779
|
-
throw new Error(
|
1780
|
-
"`provider` is no longer needed in `streamUI`. Use `model` instead."
|
1781
|
-
);
|
1782
|
-
}
|
1783
|
-
if (tools) {
|
1784
|
-
for (const [name9, tool] of Object.entries(tools)) {
|
1785
|
-
if ("render" in tool) {
|
1786
|
-
throw new Error(
|
1787
|
-
"Tool definition in `streamUI` should not have `render` property. Use `generate` instead. Found in tool: " + name9
|
1788
|
-
);
|
1214
|
+
case "user": {
|
1215
|
+
if (typeof message.content === "string") {
|
1216
|
+
return {
|
1217
|
+
role: "user",
|
1218
|
+
content: [{ type: "text", text: message.content }],
|
1219
|
+
providerOptions: (_b = message.providerOptions) != null ? _b : message.experimental_providerMetadata
|
1220
|
+
};
|
1789
1221
|
}
|
1222
|
+
return {
|
1223
|
+
role: "user",
|
1224
|
+
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
1225
|
+
providerOptions: (_c = message.providerOptions) != null ? _c : message.experimental_providerMetadata
|
1226
|
+
};
|
1790
1227
|
}
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
renderer,
|
1799
|
-
streamableUI,
|
1800
|
-
isLastCall = false
|
1801
|
-
}) {
|
1802
|
-
if (!renderer)
|
1803
|
-
return;
|
1804
|
-
const renderFinished = createResolvablePromise();
|
1805
|
-
finished = finished ? finished.then(() => renderFinished.promise) : renderFinished.promise;
|
1806
|
-
const rendererResult = renderer(...args);
|
1807
|
-
if (isAsyncGenerator(rendererResult) || isGenerator(rendererResult)) {
|
1808
|
-
while (true) {
|
1809
|
-
const { done, value } = await rendererResult.next();
|
1810
|
-
const node = await value;
|
1811
|
-
if (isLastCall && done) {
|
1812
|
-
streamableUI.done(node);
|
1813
|
-
} else {
|
1814
|
-
streamableUI.update(node);
|
1815
|
-
}
|
1816
|
-
if (done)
|
1817
|
-
break;
|
1818
|
-
}
|
1819
|
-
} else {
|
1820
|
-
const node = await rendererResult;
|
1821
|
-
if (isLastCall) {
|
1822
|
-
streamableUI.done(node);
|
1823
|
-
} else {
|
1824
|
-
streamableUI.update(node);
|
1228
|
+
case "assistant": {
|
1229
|
+
if (typeof message.content === "string") {
|
1230
|
+
return {
|
1231
|
+
role: "assistant",
|
1232
|
+
content: [{ type: "text", text: message.content }],
|
1233
|
+
providerOptions: (_d = message.providerOptions) != null ? _d : message.experimental_providerMetadata
|
1234
|
+
};
|
1825
1235
|
}
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
1842
|
-
|
1843
|
-
activeTools: void 0
|
1844
|
-
}),
|
1845
|
-
inputFormat: validatedPrompt.type,
|
1846
|
-
prompt: await convertToLanguageModelPrompt({
|
1847
|
-
prompt: validatedPrompt,
|
1848
|
-
modelSupportsImageUrls: model.supportsImageUrls,
|
1849
|
-
modelSupportsUrl: (_a9 = model.supportsUrl) == null ? void 0 : _a9.bind(model)
|
1850
|
-
// support 'this' context
|
1851
|
-
}),
|
1852
|
-
providerOptions,
|
1853
|
-
abortSignal,
|
1854
|
-
headers
|
1855
|
-
});
|
1856
|
-
}
|
1857
|
-
);
|
1858
|
-
const [stream, forkedStream] = result.stream.tee();
|
1859
|
-
(async () => {
|
1860
|
-
try {
|
1861
|
-
let content = "";
|
1862
|
-
let hasToolCall = false;
|
1863
|
-
const reader = forkedStream.getReader();
|
1864
|
-
while (true) {
|
1865
|
-
const { done, value } = await reader.read();
|
1866
|
-
if (done)
|
1867
|
-
break;
|
1868
|
-
switch (value.type) {
|
1869
|
-
case "text-delta": {
|
1870
|
-
content += value.textDelta;
|
1871
|
-
render({
|
1872
|
-
renderer: textRender,
|
1873
|
-
args: [{ content, done: false, delta: value.textDelta }],
|
1874
|
-
streamableUI: ui
|
1875
|
-
});
|
1876
|
-
break;
|
1877
|
-
}
|
1878
|
-
case "tool-call-delta": {
|
1879
|
-
hasToolCall = true;
|
1880
|
-
break;
|
1881
|
-
}
|
1882
|
-
case "tool-call": {
|
1883
|
-
const toolName = value.toolName;
|
1884
|
-
if (!tools) {
|
1885
|
-
throw new NoSuchToolError({ toolName });
|
1236
|
+
return {
|
1237
|
+
role: "assistant",
|
1238
|
+
content: message.content.filter(
|
1239
|
+
// remove empty text parts:
|
1240
|
+
(part) => part.type !== "text" || part.text !== ""
|
1241
|
+
).map((part) => {
|
1242
|
+
var _a8, _b2;
|
1243
|
+
const providerOptions = (_a8 = part.providerOptions) != null ? _a8 : part.experimental_providerMetadata;
|
1244
|
+
switch (part.type) {
|
1245
|
+
case "file": {
|
1246
|
+
return {
|
1247
|
+
type: "file",
|
1248
|
+
data: part.data instanceof URL ? part.data : convertDataContentToBase64String(part.data),
|
1249
|
+
filename: part.filename,
|
1250
|
+
mediaType: (_b2 = part.mediaType) != null ? _b2 : part.mimeType,
|
1251
|
+
providerOptions
|
1252
|
+
};
|
1886
1253
|
}
|
1887
|
-
|
1888
|
-
|
1889
|
-
|
1890
|
-
|
1891
|
-
|
1892
|
-
|
1254
|
+
case "reasoning": {
|
1255
|
+
return {
|
1256
|
+
type: "reasoning",
|
1257
|
+
text: part.text,
|
1258
|
+
signature: part.signature,
|
1259
|
+
providerOptions
|
1260
|
+
};
|
1893
1261
|
}
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1897
|
-
|
1898
|
-
|
1899
|
-
|
1900
|
-
|
1901
|
-
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1262
|
+
case "redacted-reasoning": {
|
1263
|
+
return {
|
1264
|
+
type: "redacted-reasoning",
|
1265
|
+
data: part.data,
|
1266
|
+
providerOptions
|
1267
|
+
};
|
1268
|
+
}
|
1269
|
+
case "text": {
|
1270
|
+
return {
|
1271
|
+
type: "text",
|
1272
|
+
text: part.text,
|
1273
|
+
providerOptions
|
1274
|
+
};
|
1275
|
+
}
|
1276
|
+
case "tool-call": {
|
1277
|
+
return {
|
1278
|
+
type: "tool-call",
|
1279
|
+
toolCallId: part.toolCallId,
|
1280
|
+
toolName: part.toolName,
|
1281
|
+
args: part.args,
|
1282
|
+
providerOptions
|
1283
|
+
};
|
1905
1284
|
}
|
1906
|
-
render({
|
1907
|
-
renderer: tool.generate,
|
1908
|
-
args: [
|
1909
|
-
parseResult.value,
|
1910
|
-
{
|
1911
|
-
toolName,
|
1912
|
-
toolCallId: value.toolCallId
|
1913
|
-
}
|
1914
|
-
],
|
1915
|
-
streamableUI: ui,
|
1916
|
-
isLastCall: true
|
1917
|
-
});
|
1918
|
-
break;
|
1919
|
-
}
|
1920
|
-
case "error": {
|
1921
|
-
throw value.error;
|
1922
|
-
}
|
1923
|
-
case "finish": {
|
1924
|
-
finishEvent = {
|
1925
|
-
finishReason: value.finishReason,
|
1926
|
-
usage: calculateLanguageModelUsage(value.usage),
|
1927
|
-
warnings: result.warnings,
|
1928
|
-
rawResponse: result.rawResponse
|
1929
|
-
};
|
1930
|
-
break;
|
1931
1285
|
}
|
1932
|
-
}
|
1933
|
-
|
1934
|
-
|
1935
|
-
render({
|
1936
|
-
renderer: textRender,
|
1937
|
-
args: [{ content, done: true }],
|
1938
|
-
streamableUI: ui,
|
1939
|
-
isLastCall: true
|
1940
|
-
});
|
1941
|
-
}
|
1942
|
-
await finished;
|
1943
|
-
if (finishEvent && onFinish) {
|
1944
|
-
await onFinish({
|
1945
|
-
...finishEvent,
|
1946
|
-
value: ui.value
|
1947
|
-
});
|
1948
|
-
}
|
1949
|
-
} catch (error) {
|
1950
|
-
ui.error(error);
|
1951
|
-
}
|
1952
|
-
})();
|
1953
|
-
return {
|
1954
|
-
...result,
|
1955
|
-
stream,
|
1956
|
-
value: ui.value
|
1957
|
-
};
|
1958
|
-
}
|
1959
|
-
|
1960
|
-
// rsc/streamable-value/streamable-value.ts
|
1961
|
-
var STREAMABLE_VALUE_TYPE = Symbol.for("ui.streamable.value");
|
1962
|
-
|
1963
|
-
// rsc/streamable-value/create-streamable-value.ts
|
1964
|
-
var STREAMABLE_VALUE_INTERNAL_LOCK = Symbol("streamable.value.lock");
|
1965
|
-
function createStreamableValue(initialValue) {
|
1966
|
-
const isReadableStream = initialValue instanceof ReadableStream || typeof initialValue === "object" && initialValue !== null && "getReader" in initialValue && typeof initialValue.getReader === "function" && "locked" in initialValue && typeof initialValue.locked === "boolean";
|
1967
|
-
if (!isReadableStream) {
|
1968
|
-
return createStreamableValueImpl(initialValue);
|
1969
|
-
}
|
1970
|
-
const streamableValue = createStreamableValueImpl();
|
1971
|
-
streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
|
1972
|
-
(async () => {
|
1973
|
-
try {
|
1974
|
-
const reader = initialValue.getReader();
|
1975
|
-
while (true) {
|
1976
|
-
const { value, done } = await reader.read();
|
1977
|
-
if (done) {
|
1978
|
-
break;
|
1979
|
-
}
|
1980
|
-
streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
|
1981
|
-
if (typeof value === "string") {
|
1982
|
-
streamableValue.append(value);
|
1983
|
-
} else {
|
1984
|
-
streamableValue.update(value);
|
1985
|
-
}
|
1986
|
-
streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
|
1987
|
-
}
|
1988
|
-
streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
|
1989
|
-
streamableValue.done();
|
1990
|
-
} catch (e) {
|
1991
|
-
streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
|
1992
|
-
streamableValue.error(e);
|
1286
|
+
}),
|
1287
|
+
providerOptions: (_e = message.providerOptions) != null ? _e : message.experimental_providerMetadata
|
1288
|
+
};
|
1993
1289
|
}
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
|
2006
|
-
|
2007
|
-
|
1290
|
+
case "tool": {
|
1291
|
+
return {
|
1292
|
+
role: "tool",
|
1293
|
+
content: message.content.map((part) => {
|
1294
|
+
var _a8;
|
1295
|
+
return {
|
1296
|
+
type: "tool-result",
|
1297
|
+
toolCallId: part.toolCallId,
|
1298
|
+
toolName: part.toolName,
|
1299
|
+
result: part.result,
|
1300
|
+
content: part.experimental_content,
|
1301
|
+
isError: part.isError,
|
1302
|
+
providerOptions: (_a8 = part.providerOptions) != null ? _a8 : part.experimental_providerMetadata
|
1303
|
+
};
|
1304
|
+
}),
|
1305
|
+
providerOptions: (_f = message.providerOptions) != null ? _f : message.experimental_providerMetadata
|
1306
|
+
};
|
2008
1307
|
}
|
2009
|
-
|
2010
|
-
|
2011
|
-
|
2012
|
-
);
|
1308
|
+
default: {
|
1309
|
+
const _exhaustiveCheck = role;
|
1310
|
+
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
2013
1311
|
}
|
2014
1312
|
}
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
1313
|
+
}
|
1314
|
+
async function downloadAssets(messages, downloadImplementation, modelSupportsImageUrls, modelSupportsUrl) {
|
1315
|
+
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
1316
|
+
(content) => Array.isArray(content)
|
1317
|
+
).flat().filter(
|
1318
|
+
(part) => part.type === "image" || part.type === "file"
|
1319
|
+
).filter(
|
1320
|
+
(part) => !(part.type === "image" && modelSupportsImageUrls === true)
|
1321
|
+
).map((part) => part.type === "image" ? part.image : part.data).map(
|
1322
|
+
(part) => (
|
1323
|
+
// support string urls:
|
1324
|
+
typeof part === "string" && (part.startsWith("http:") || part.startsWith("https:")) ? new URL(part) : part
|
1325
|
+
)
|
1326
|
+
).filter((image) => image instanceof URL).filter((url) => !modelSupportsUrl(url));
|
1327
|
+
const downloadedImages = await Promise.all(
|
1328
|
+
urls.map(async (url) => ({
|
1329
|
+
url,
|
1330
|
+
data: await downloadImplementation({ url })
|
1331
|
+
}))
|
1332
|
+
);
|
1333
|
+
return Object.fromEntries(
|
1334
|
+
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
1335
|
+
);
|
1336
|
+
}
|
1337
|
+
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1338
|
+
var _a7, _b, _c, _d, _e;
|
1339
|
+
if (part.type === "text") {
|
1340
|
+
return {
|
1341
|
+
type: "text",
|
1342
|
+
text: part.text,
|
1343
|
+
providerOptions: (_a7 = part.providerOptions) != null ? _a7 : part.experimental_providerMetadata
|
1344
|
+
};
|
1345
|
+
}
|
1346
|
+
let mediaType = (_b = part.mediaType) != null ? _b : part.mimeType;
|
1347
|
+
let data;
|
1348
|
+
let content;
|
1349
|
+
let normalizedData;
|
1350
|
+
const type = part.type;
|
1351
|
+
switch (type) {
|
1352
|
+
case "image":
|
1353
|
+
data = part.image;
|
1354
|
+
break;
|
1355
|
+
case "file":
|
1356
|
+
data = part.data;
|
1357
|
+
break;
|
1358
|
+
default:
|
1359
|
+
throw new Error(`Unsupported part type: ${type}`);
|
1360
|
+
}
|
1361
|
+
try {
|
1362
|
+
content = typeof data === "string" ? new URL(data) : data;
|
1363
|
+
} catch (error) {
|
1364
|
+
content = data;
|
2027
1365
|
}
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
|
1366
|
+
if (content instanceof URL) {
|
1367
|
+
if (content.protocol === "data:") {
|
1368
|
+
const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(
|
1369
|
+
content.toString()
|
1370
|
+
);
|
1371
|
+
if (dataUrlMediaType == null || base64Content == null) {
|
1372
|
+
throw new Error(`Invalid data URL format in part ${type}`);
|
1373
|
+
}
|
1374
|
+
mediaType = dataUrlMediaType;
|
1375
|
+
normalizedData = convertDataContentToUint8Array(base64Content);
|
2033
1376
|
} else {
|
2034
|
-
|
2035
|
-
|
1377
|
+
const downloadedFile = downloadedAssets[content.toString()];
|
1378
|
+
if (downloadedFile) {
|
1379
|
+
normalizedData = downloadedFile.data;
|
1380
|
+
mediaType != null ? mediaType : mediaType = downloadedFile.mediaType;
|
2036
1381
|
} else {
|
2037
|
-
|
1382
|
+
normalizedData = content;
|
2038
1383
|
}
|
2039
1384
|
}
|
2040
|
-
|
2041
|
-
|
2042
|
-
}
|
2043
|
-
if (initialChunk) {
|
2044
|
-
init.type = STREAMABLE_VALUE_TYPE;
|
2045
|
-
}
|
2046
|
-
return init;
|
1385
|
+
} else {
|
1386
|
+
normalizedData = convertDataContentToUint8Array(content);
|
2047
1387
|
}
|
2048
|
-
|
2049
|
-
|
2050
|
-
|
2051
|
-
|
2052
|
-
|
2053
|
-
|
2054
|
-
}
|
1388
|
+
switch (type) {
|
1389
|
+
case "image": {
|
1390
|
+
if (normalizedData instanceof Uint8Array) {
|
1391
|
+
mediaType = (_c = detectMediaType({
|
1392
|
+
data: normalizedData,
|
1393
|
+
signatures: imageMediaTypeSignatures
|
1394
|
+
})) != null ? _c : mediaType;
|
2055
1395
|
}
|
1396
|
+
return {
|
1397
|
+
type: "file",
|
1398
|
+
mediaType: mediaType != null ? mediaType : "image/*",
|
1399
|
+
// any image
|
1400
|
+
filename: void 0,
|
1401
|
+
data: normalizedData instanceof Uint8Array ? convertUint8ArrayToBase642(normalizedData) : normalizedData,
|
1402
|
+
providerOptions: (_d = part.providerOptions) != null ? _d : part.experimental_providerMetadata
|
1403
|
+
};
|
2056
1404
|
}
|
2057
|
-
|
2058
|
-
|
2059
|
-
|
2060
|
-
set [STREAMABLE_VALUE_INTERNAL_LOCK](state) {
|
2061
|
-
locked = state;
|
2062
|
-
},
|
2063
|
-
get value() {
|
2064
|
-
return createWrapped(true);
|
2065
|
-
},
|
2066
|
-
update(value) {
|
2067
|
-
assertStream(".update()");
|
2068
|
-
const resolvePrevious = resolvable.resolve;
|
2069
|
-
resolvable = createResolvablePromise();
|
2070
|
-
updateValueStates(value);
|
2071
|
-
currentPromise = resolvable.promise;
|
2072
|
-
resolvePrevious(createWrapped());
|
2073
|
-
warnUnclosedStream();
|
2074
|
-
return streamable;
|
2075
|
-
},
|
2076
|
-
append(value) {
|
2077
|
-
assertStream(".append()");
|
2078
|
-
if (typeof currentValue !== "string" && typeof currentValue !== "undefined") {
|
2079
|
-
throw new Error(
|
2080
|
-
`.append(): The current value is not a string. Received: ${typeof currentValue}`
|
2081
|
-
);
|
2082
|
-
}
|
2083
|
-
if (typeof value !== "string") {
|
2084
|
-
throw new Error(
|
2085
|
-
`.append(): The value is not a string. Received: ${typeof value}`
|
2086
|
-
);
|
2087
|
-
}
|
2088
|
-
const resolvePrevious = resolvable.resolve;
|
2089
|
-
resolvable = createResolvablePromise();
|
2090
|
-
if (typeof currentValue === "string") {
|
2091
|
-
currentPatchValue = [0, value];
|
2092
|
-
currentValue = currentValue + value;
|
2093
|
-
} else {
|
2094
|
-
currentPatchValue = void 0;
|
2095
|
-
currentValue = value;
|
2096
|
-
}
|
2097
|
-
currentPromise = resolvable.promise;
|
2098
|
-
resolvePrevious(createWrapped());
|
2099
|
-
warnUnclosedStream();
|
2100
|
-
return streamable;
|
2101
|
-
},
|
2102
|
-
error(error) {
|
2103
|
-
assertStream(".error()");
|
2104
|
-
if (warningTimeout) {
|
2105
|
-
clearTimeout(warningTimeout);
|
2106
|
-
}
|
2107
|
-
closed = true;
|
2108
|
-
currentError = error;
|
2109
|
-
currentPromise = void 0;
|
2110
|
-
resolvable.resolve({ error });
|
2111
|
-
return streamable;
|
2112
|
-
},
|
2113
|
-
done(...args) {
|
2114
|
-
assertStream(".done()");
|
2115
|
-
if (warningTimeout) {
|
2116
|
-
clearTimeout(warningTimeout);
|
2117
|
-
}
|
2118
|
-
closed = true;
|
2119
|
-
currentPromise = void 0;
|
2120
|
-
if (args.length) {
|
2121
|
-
updateValueStates(args[0]);
|
2122
|
-
resolvable.resolve(createWrapped());
|
2123
|
-
return streamable;
|
1405
|
+
case "file": {
|
1406
|
+
if (mediaType == null) {
|
1407
|
+
throw new Error(`Media type is missing for file part`);
|
2124
1408
|
}
|
2125
|
-
|
2126
|
-
|
1409
|
+
return {
|
1410
|
+
type: "file",
|
1411
|
+
mediaType,
|
1412
|
+
filename: part.filename,
|
1413
|
+
data: normalizedData instanceof Uint8Array ? convertDataContentToBase64String(normalizedData) : normalizedData,
|
1414
|
+
providerOptions: (_e = part.providerOptions) != null ? _e : part.experimental_providerMetadata
|
1415
|
+
};
|
2127
1416
|
}
|
1417
|
+
}
|
1418
|
+
}
|
1419
|
+
|
1420
|
+
// core/types/usage.ts
|
1421
|
+
function calculateLanguageModelUsage({
|
1422
|
+
promptTokens,
|
1423
|
+
completionTokens
|
1424
|
+
}) {
|
1425
|
+
return {
|
1426
|
+
promptTokens,
|
1427
|
+
completionTokens,
|
1428
|
+
totalTokens: promptTokens + completionTokens
|
2128
1429
|
};
|
2129
|
-
return streamable;
|
2130
1430
|
}
|
1431
|
+
|
1432
|
+
// util/constants.ts
|
1433
|
+
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
2131
1434
|
export {
|
2132
|
-
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
|
2137
|
-
|
1435
|
+
HANGING_STREAM_WARNING_TIME_MS,
|
1436
|
+
calculateLanguageModelUsage,
|
1437
|
+
convertToLanguageModelPrompt,
|
1438
|
+
prepareCallSettings,
|
1439
|
+
prepareRetries,
|
1440
|
+
prepareToolsAndToolChoice,
|
1441
|
+
standardizePrompt
|
2138
1442
|
};
|
2139
|
-
//# sourceMappingURL=
|
1443
|
+
//# sourceMappingURL=index.mjs.map
|