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