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