ai 3.0.13 → 3.0.14
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/ai-model-specification/dist/index.d.mts +704 -0
- package/ai-model-specification/dist/index.d.ts +704 -0
- package/ai-model-specification/dist/index.js +806 -0
- package/ai-model-specification/dist/index.js.map +1 -0
- package/ai-model-specification/dist/index.mjs +742 -0
- package/ai-model-specification/dist/index.mjs.map +1 -0
- package/dist/index.d.mts +683 -2
- package/dist/index.d.ts +683 -2
- package/dist/index.js +1723 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1700 -15
- package/dist/index.mjs.map +1 -1
- package/mistral/dist/index.d.mts +367 -0
- package/mistral/dist/index.d.ts +367 -0
- package/mistral/dist/index.js +936 -0
- package/mistral/dist/index.js.map +1 -0
- package/mistral/dist/index.mjs +900 -0
- package/mistral/dist/index.mjs.map +1 -0
- package/openai/dist/index.d.mts +430 -0
- package/openai/dist/index.d.ts +430 -0
- package/openai/dist/index.js +1355 -0
- package/openai/dist/index.js.map +1 -0
- package/openai/dist/index.mjs +1319 -0
- package/openai/dist/index.mjs.map +1 -0
- package/package.json +30 -4
- package/prompts/dist/index.d.mts +13 -1
- package/prompts/dist/index.d.ts +13 -1
- package/prompts/dist/index.js +13 -0
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs +12 -0
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.js +35 -34
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +35 -34
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/index.d.ts +45 -8
- package/rsc/dist/rsc-server.d.mts +45 -8
- package/rsc/dist/rsc-server.mjs +67 -13
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.d.mts +5 -8
- package/rsc/dist/rsc-shared.mjs +23 -2
- package/rsc/dist/rsc-shared.mjs.map +1 -1
- package/solid/dist/index.js +29 -27
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +29 -27
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.js +31 -29
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +31 -29
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.js +29 -27
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +29 -27
- package/vue/dist/index.mjs.map +1 -1
@@ -0,0 +1,742 @@
|
|
1
|
+
// ai-model-specification/errors/api-call-error.ts
|
2
|
+
var APICallError = class extends Error {
|
3
|
+
constructor({
|
4
|
+
message,
|
5
|
+
url,
|
6
|
+
requestBodyValues,
|
7
|
+
statusCode,
|
8
|
+
responseBody,
|
9
|
+
cause,
|
10
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
11
|
+
statusCode === 409 || // conflict
|
12
|
+
statusCode === 429 || // too many requests
|
13
|
+
statusCode >= 500),
|
14
|
+
// server error
|
15
|
+
data
|
16
|
+
}) {
|
17
|
+
super(message);
|
18
|
+
this.name = "AI_APICallError";
|
19
|
+
this.url = url;
|
20
|
+
this.requestBodyValues = requestBodyValues;
|
21
|
+
this.statusCode = statusCode;
|
22
|
+
this.responseBody = responseBody;
|
23
|
+
this.cause = cause;
|
24
|
+
this.isRetryable = isRetryable;
|
25
|
+
this.data = data;
|
26
|
+
}
|
27
|
+
static isAPICallError(error) {
|
28
|
+
return error instanceof Error && error.name === "AI_APICallError" && typeof error.url === "string" && typeof error.requestBodyValues === "object" && (error.statusCode == null || typeof error.statusCode === "number") && (error.responseBody == null || typeof error.responseBody === "string") && (error.cause == null || typeof error.cause === "object") && typeof error.isRetryable === "boolean" && (error.data == null || typeof error.data === "object");
|
29
|
+
}
|
30
|
+
toJSON() {
|
31
|
+
return {
|
32
|
+
name: this.name,
|
33
|
+
message: this.message,
|
34
|
+
url: this.url,
|
35
|
+
requestBodyValues: this.requestBodyValues,
|
36
|
+
statusCode: this.statusCode,
|
37
|
+
responseBody: this.responseBody,
|
38
|
+
cause: this.cause,
|
39
|
+
isRetryable: this.isRetryable,
|
40
|
+
data: this.data
|
41
|
+
};
|
42
|
+
}
|
43
|
+
};
|
44
|
+
|
45
|
+
// ai-model-specification/errors/invalid-argument-error.ts
|
46
|
+
var InvalidArgumentError = class extends Error {
|
47
|
+
constructor({
|
48
|
+
parameter,
|
49
|
+
value,
|
50
|
+
message
|
51
|
+
}) {
|
52
|
+
super(`Invalid argument for parameter ${parameter}: ${message}`);
|
53
|
+
this.name = "AI_InvalidArgumentError";
|
54
|
+
this.parameter = parameter;
|
55
|
+
this.value = value;
|
56
|
+
}
|
57
|
+
static isInvalidArgumentError(error) {
|
58
|
+
return error instanceof Error && error.name === "AI_InvalidArgumentError" && typeof error.parameter === "string" && typeof error.value === "string";
|
59
|
+
}
|
60
|
+
toJSON() {
|
61
|
+
return {
|
62
|
+
name: this.name,
|
63
|
+
message: this.message,
|
64
|
+
stack: this.stack,
|
65
|
+
parameter: this.parameter,
|
66
|
+
value: this.value
|
67
|
+
};
|
68
|
+
}
|
69
|
+
};
|
70
|
+
|
71
|
+
// ai-model-specification/errors/invalid-data-content-error.ts
|
72
|
+
var InvalidDataContentError = class extends Error {
|
73
|
+
constructor({
|
74
|
+
content,
|
75
|
+
message = `Invalid data content. Expected a string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
|
76
|
+
}) {
|
77
|
+
super(message);
|
78
|
+
this.name = "AI_InvalidDataContentError";
|
79
|
+
this.content = content;
|
80
|
+
}
|
81
|
+
static isInvalidDataContentError(error) {
|
82
|
+
return error instanceof Error && error.name === "AI_InvalidDataContentError" && error.content != null;
|
83
|
+
}
|
84
|
+
toJSON() {
|
85
|
+
return {
|
86
|
+
name: this.name,
|
87
|
+
message: this.message,
|
88
|
+
stack: this.stack,
|
89
|
+
content: this.content
|
90
|
+
};
|
91
|
+
}
|
92
|
+
};
|
93
|
+
|
94
|
+
// ai-model-specification/errors/invalid-prompt-error.ts
|
95
|
+
var InvalidPromptError = class extends Error {
|
96
|
+
constructor({ prompt: prompt2, message }) {
|
97
|
+
super(`Invalid prompt: ${message}`);
|
98
|
+
this.name = "AI_InvalidPromptError";
|
99
|
+
this.prompt = prompt2;
|
100
|
+
}
|
101
|
+
static isInvalidPromptError(error) {
|
102
|
+
return error instanceof Error && error.name === "AI_InvalidPromptError" && prompt != null;
|
103
|
+
}
|
104
|
+
toJSON() {
|
105
|
+
return {
|
106
|
+
name: this.name,
|
107
|
+
message: this.message,
|
108
|
+
stack: this.stack,
|
109
|
+
prompt: this.prompt
|
110
|
+
};
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
// ai-model-specification/errors/invalid-response-data-error.ts
|
115
|
+
var InvalidResponseDataError = class extends Error {
|
116
|
+
constructor({
|
117
|
+
data,
|
118
|
+
message = `Invalid response data: ${JSON.stringify(data)}.`
|
119
|
+
}) {
|
120
|
+
super(message);
|
121
|
+
this.name = "AI_InvalidResponseDataError";
|
122
|
+
this.data = data;
|
123
|
+
}
|
124
|
+
static isInvalidResponseDataError(error) {
|
125
|
+
return error instanceof Error && error.name === "AI_InvalidResponseDataError" && error.data != null;
|
126
|
+
}
|
127
|
+
toJSON() {
|
128
|
+
return {
|
129
|
+
name: this.name,
|
130
|
+
message: this.message,
|
131
|
+
stack: this.stack,
|
132
|
+
data: this.data
|
133
|
+
};
|
134
|
+
}
|
135
|
+
};
|
136
|
+
|
137
|
+
// ai-model-specification/util/generate-id.ts
|
138
|
+
import { customAlphabet } from "nanoid/non-secure";
|
139
|
+
var generateId = customAlphabet(
|
140
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
141
|
+
7
|
142
|
+
);
|
143
|
+
|
144
|
+
// ai-model-specification/util/get-error-message.ts
|
145
|
+
function getErrorMessage(error) {
|
146
|
+
if (error == null) {
|
147
|
+
return "unknown error";
|
148
|
+
}
|
149
|
+
if (typeof error === "string") {
|
150
|
+
return error;
|
151
|
+
}
|
152
|
+
if (error instanceof Error) {
|
153
|
+
return error.message;
|
154
|
+
}
|
155
|
+
return JSON.stringify(error);
|
156
|
+
}
|
157
|
+
|
158
|
+
// ai-model-specification/errors/load-api-key-error.ts
|
159
|
+
var LoadAPIKeyError = class extends Error {
|
160
|
+
constructor({ message }) {
|
161
|
+
super(message);
|
162
|
+
this.name = "AI_LoadAPIKeyError";
|
163
|
+
}
|
164
|
+
static isLoadAPIKeyError(error) {
|
165
|
+
return error instanceof Error && error.name === "AI_LoadAPIKeyError";
|
166
|
+
}
|
167
|
+
toJSON() {
|
168
|
+
return {
|
169
|
+
name: this.name,
|
170
|
+
message: this.message
|
171
|
+
};
|
172
|
+
}
|
173
|
+
};
|
174
|
+
|
175
|
+
// ai-model-specification/util/load-api-key.ts
|
176
|
+
function loadApiKey({
|
177
|
+
apiKey,
|
178
|
+
environmentVariableName,
|
179
|
+
apiKeyParameterName = "apiKey",
|
180
|
+
description
|
181
|
+
}) {
|
182
|
+
if (typeof apiKey === "string") {
|
183
|
+
return apiKey;
|
184
|
+
}
|
185
|
+
if (apiKey != null) {
|
186
|
+
throw new LoadAPIKeyError({
|
187
|
+
message: `${description} API key must be a string.`
|
188
|
+
});
|
189
|
+
}
|
190
|
+
if (typeof process === "undefined") {
|
191
|
+
throw new LoadAPIKeyError({
|
192
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
193
|
+
});
|
194
|
+
}
|
195
|
+
apiKey = process.env[environmentVariableName];
|
196
|
+
if (apiKey == null) {
|
197
|
+
throw new LoadAPIKeyError({
|
198
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
199
|
+
});
|
200
|
+
}
|
201
|
+
if (typeof apiKey !== "string") {
|
202
|
+
throw new LoadAPIKeyError({
|
203
|
+
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
204
|
+
});
|
205
|
+
}
|
206
|
+
return apiKey;
|
207
|
+
}
|
208
|
+
|
209
|
+
// ai-model-specification/util/parse-json.ts
|
210
|
+
import SecureJSON from "secure-json-parse";
|
211
|
+
|
212
|
+
// ai-model-specification/errors/json-parse-error.ts
|
213
|
+
var JSONParseError = class extends Error {
|
214
|
+
constructor({ text, cause }) {
|
215
|
+
super(
|
216
|
+
`JSON parsing failed: Text: ${text}.
|
217
|
+
Error message: ${getErrorMessage(cause)}`
|
218
|
+
);
|
219
|
+
this.name = "AI_JSONParseError";
|
220
|
+
this.cause = cause;
|
221
|
+
this.text = text;
|
222
|
+
}
|
223
|
+
static isJSONParseError(error) {
|
224
|
+
return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
|
225
|
+
}
|
226
|
+
toJSON() {
|
227
|
+
return {
|
228
|
+
name: this.name,
|
229
|
+
message: this.message,
|
230
|
+
cause: this.cause,
|
231
|
+
stack: this.stack,
|
232
|
+
valueText: this.text
|
233
|
+
};
|
234
|
+
}
|
235
|
+
};
|
236
|
+
|
237
|
+
// ai-model-specification/errors/type-validation-error.ts
|
238
|
+
var TypeValidationError = class extends Error {
|
239
|
+
constructor({ value, cause }) {
|
240
|
+
super(
|
241
|
+
`Type validation failed: Value: ${JSON.stringify(value)}.
|
242
|
+
Error message: ${getErrorMessage(cause)}`
|
243
|
+
);
|
244
|
+
this.name = "AI_TypeValidationError";
|
245
|
+
this.cause = cause;
|
246
|
+
this.value = value;
|
247
|
+
}
|
248
|
+
static isTypeValidationError(error) {
|
249
|
+
return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
|
250
|
+
}
|
251
|
+
toJSON() {
|
252
|
+
return {
|
253
|
+
name: this.name,
|
254
|
+
message: this.message,
|
255
|
+
cause: this.cause,
|
256
|
+
stack: this.stack,
|
257
|
+
value: this.value
|
258
|
+
};
|
259
|
+
}
|
260
|
+
};
|
261
|
+
|
262
|
+
// ai-model-specification/util/validate-types.ts
|
263
|
+
function validateTypes({
|
264
|
+
value,
|
265
|
+
schema
|
266
|
+
}) {
|
267
|
+
try {
|
268
|
+
return schema.parse(value);
|
269
|
+
} catch (error) {
|
270
|
+
throw new TypeValidationError({ value, cause: error });
|
271
|
+
}
|
272
|
+
}
|
273
|
+
function safeValidateTypes({
|
274
|
+
value,
|
275
|
+
schema
|
276
|
+
}) {
|
277
|
+
try {
|
278
|
+
const validationResult = schema.safeParse(value);
|
279
|
+
if (validationResult.success) {
|
280
|
+
return {
|
281
|
+
success: true,
|
282
|
+
value: validationResult.data
|
283
|
+
};
|
284
|
+
}
|
285
|
+
return {
|
286
|
+
success: false,
|
287
|
+
error: new TypeValidationError({
|
288
|
+
value,
|
289
|
+
cause: validationResult.error
|
290
|
+
})
|
291
|
+
};
|
292
|
+
} catch (error) {
|
293
|
+
return {
|
294
|
+
success: false,
|
295
|
+
error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
|
296
|
+
};
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
// ai-model-specification/util/parse-json.ts
|
301
|
+
function parseJSON({
|
302
|
+
text,
|
303
|
+
schema
|
304
|
+
}) {
|
305
|
+
try {
|
306
|
+
const value = SecureJSON.parse(text);
|
307
|
+
if (schema == null) {
|
308
|
+
return value;
|
309
|
+
}
|
310
|
+
return validateTypes({ value, schema });
|
311
|
+
} catch (error) {
|
312
|
+
if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
|
313
|
+
throw error;
|
314
|
+
}
|
315
|
+
throw new JSONParseError({ text, cause: error });
|
316
|
+
}
|
317
|
+
}
|
318
|
+
function safeParseJSON({
|
319
|
+
text,
|
320
|
+
schema
|
321
|
+
}) {
|
322
|
+
try {
|
323
|
+
const value = SecureJSON.parse(text);
|
324
|
+
if (schema == null) {
|
325
|
+
return {
|
326
|
+
success: true,
|
327
|
+
value
|
328
|
+
};
|
329
|
+
}
|
330
|
+
return safeValidateTypes({ value, schema });
|
331
|
+
} catch (error) {
|
332
|
+
return {
|
333
|
+
success: false,
|
334
|
+
error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
|
335
|
+
};
|
336
|
+
}
|
337
|
+
}
|
338
|
+
function isParseableJson(input) {
|
339
|
+
try {
|
340
|
+
SecureJSON.parse(input);
|
341
|
+
return true;
|
342
|
+
} catch (e) {
|
343
|
+
return false;
|
344
|
+
}
|
345
|
+
}
|
346
|
+
|
347
|
+
// ai-model-specification/util/post-to-api.ts
|
348
|
+
var postJsonToApi = async ({
|
349
|
+
url,
|
350
|
+
headers,
|
351
|
+
body,
|
352
|
+
failedResponseHandler,
|
353
|
+
successfulResponseHandler,
|
354
|
+
abortSignal
|
355
|
+
}) => postToApi({
|
356
|
+
url,
|
357
|
+
headers: {
|
358
|
+
...headers,
|
359
|
+
"Content-Type": "application/json"
|
360
|
+
},
|
361
|
+
body: {
|
362
|
+
content: JSON.stringify(body),
|
363
|
+
values: body
|
364
|
+
},
|
365
|
+
failedResponseHandler,
|
366
|
+
successfulResponseHandler,
|
367
|
+
abortSignal
|
368
|
+
});
|
369
|
+
var postToApi = async ({
|
370
|
+
url,
|
371
|
+
headers = {},
|
372
|
+
body,
|
373
|
+
successfulResponseHandler,
|
374
|
+
failedResponseHandler,
|
375
|
+
abortSignal
|
376
|
+
}) => {
|
377
|
+
try {
|
378
|
+
const definedHeaders = Object.fromEntries(
|
379
|
+
Object.entries(headers).filter(([_key, value]) => value != null)
|
380
|
+
);
|
381
|
+
const response = await fetch(url, {
|
382
|
+
method: "POST",
|
383
|
+
headers: definedHeaders,
|
384
|
+
body: body.content,
|
385
|
+
signal: abortSignal
|
386
|
+
});
|
387
|
+
if (!response.ok) {
|
388
|
+
try {
|
389
|
+
throw await failedResponseHandler({
|
390
|
+
response,
|
391
|
+
url,
|
392
|
+
requestBodyValues: body.values
|
393
|
+
});
|
394
|
+
} catch (error) {
|
395
|
+
if (error instanceof Error) {
|
396
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
397
|
+
throw error;
|
398
|
+
}
|
399
|
+
}
|
400
|
+
throw new APICallError({
|
401
|
+
message: "Failed to process error response",
|
402
|
+
cause: error,
|
403
|
+
statusCode: response.status,
|
404
|
+
url,
|
405
|
+
requestBodyValues: body.values
|
406
|
+
});
|
407
|
+
}
|
408
|
+
}
|
409
|
+
try {
|
410
|
+
return await successfulResponseHandler({
|
411
|
+
response,
|
412
|
+
url,
|
413
|
+
requestBodyValues: body.values
|
414
|
+
});
|
415
|
+
} catch (error) {
|
416
|
+
if (error instanceof Error) {
|
417
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
418
|
+
throw error;
|
419
|
+
}
|
420
|
+
}
|
421
|
+
throw new APICallError({
|
422
|
+
message: "Failed to process successful response",
|
423
|
+
cause: error,
|
424
|
+
statusCode: response.status,
|
425
|
+
url,
|
426
|
+
requestBodyValues: body.values
|
427
|
+
});
|
428
|
+
}
|
429
|
+
} catch (error) {
|
430
|
+
if (error instanceof Error) {
|
431
|
+
if (error.name === "AbortError") {
|
432
|
+
throw error;
|
433
|
+
}
|
434
|
+
}
|
435
|
+
if (error instanceof TypeError && error.message === "fetch failed") {
|
436
|
+
const cause = error.cause;
|
437
|
+
if (cause != null) {
|
438
|
+
throw new APICallError({
|
439
|
+
message: `Cannot connect to API: ${cause.message}`,
|
440
|
+
cause,
|
441
|
+
url,
|
442
|
+
requestBodyValues: body.values,
|
443
|
+
isRetryable: true
|
444
|
+
// retry when network error
|
445
|
+
});
|
446
|
+
}
|
447
|
+
}
|
448
|
+
throw error;
|
449
|
+
}
|
450
|
+
};
|
451
|
+
|
452
|
+
// ai-model-specification/util/response-handler.ts
|
453
|
+
import {
|
454
|
+
EventSourceParserStream
|
455
|
+
} from "eventsource-parser/stream";
|
456
|
+
|
457
|
+
// ai-model-specification/errors/no-response-body-error.ts
|
458
|
+
var NoResponseBodyError = class extends Error {
|
459
|
+
constructor({ message = "No response body" } = {}) {
|
460
|
+
super(message);
|
461
|
+
this.name = "AI_NoResponseBodyError";
|
462
|
+
}
|
463
|
+
static isNoResponseBodyError(error) {
|
464
|
+
return error instanceof Error && error.name === "AI_NoResponseBodyError";
|
465
|
+
}
|
466
|
+
toJSON() {
|
467
|
+
return {
|
468
|
+
name: this.name,
|
469
|
+
message: this.message,
|
470
|
+
stack: this.stack
|
471
|
+
};
|
472
|
+
}
|
473
|
+
};
|
474
|
+
|
475
|
+
// ai-model-specification/util/response-handler.ts
|
476
|
+
var createJsonErrorResponseHandler = ({
|
477
|
+
errorSchema,
|
478
|
+
errorToMessage,
|
479
|
+
isRetryable
|
480
|
+
}) => async ({ response, url, requestBodyValues }) => {
|
481
|
+
const responseBody = await response.text();
|
482
|
+
if (responseBody.trim() === "") {
|
483
|
+
return new APICallError({
|
484
|
+
message: response.statusText,
|
485
|
+
url,
|
486
|
+
requestBodyValues,
|
487
|
+
statusCode: response.status,
|
488
|
+
responseBody,
|
489
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
490
|
+
});
|
491
|
+
}
|
492
|
+
try {
|
493
|
+
const parsedError = parseJSON({
|
494
|
+
text: responseBody,
|
495
|
+
schema: errorSchema
|
496
|
+
});
|
497
|
+
return new APICallError({
|
498
|
+
message: errorToMessage(parsedError),
|
499
|
+
url,
|
500
|
+
requestBodyValues,
|
501
|
+
statusCode: response.status,
|
502
|
+
responseBody,
|
503
|
+
data: parsedError,
|
504
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
505
|
+
});
|
506
|
+
} catch (parseError) {
|
507
|
+
return new APICallError({
|
508
|
+
message: response.statusText,
|
509
|
+
url,
|
510
|
+
requestBodyValues,
|
511
|
+
statusCode: response.status,
|
512
|
+
responseBody,
|
513
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
514
|
+
});
|
515
|
+
}
|
516
|
+
};
|
517
|
+
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
518
|
+
if (response.body == null) {
|
519
|
+
throw new NoResponseBodyError();
|
520
|
+
}
|
521
|
+
return response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
522
|
+
new TransformStream({
|
523
|
+
transform({ data }, controller) {
|
524
|
+
if (data === "[DONE]") {
|
525
|
+
return;
|
526
|
+
}
|
527
|
+
controller.enqueue(
|
528
|
+
safeParseJSON({
|
529
|
+
text: data,
|
530
|
+
schema: chunkSchema
|
531
|
+
})
|
532
|
+
);
|
533
|
+
}
|
534
|
+
})
|
535
|
+
);
|
536
|
+
};
|
537
|
+
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
538
|
+
const responseBody = await response.text();
|
539
|
+
const parsedResult = safeParseJSON({
|
540
|
+
text: responseBody,
|
541
|
+
schema: responseSchema
|
542
|
+
});
|
543
|
+
if (!parsedResult.success) {
|
544
|
+
throw new APICallError({
|
545
|
+
message: "Invalid JSON response",
|
546
|
+
cause: parsedResult.error,
|
547
|
+
statusCode: response.status,
|
548
|
+
responseBody,
|
549
|
+
url,
|
550
|
+
requestBodyValues
|
551
|
+
});
|
552
|
+
}
|
553
|
+
return parsedResult.value;
|
554
|
+
};
|
555
|
+
|
556
|
+
// ai-model-specification/util/scale.ts
|
557
|
+
function scale({
|
558
|
+
inputMin = 0,
|
559
|
+
inputMax = 1,
|
560
|
+
outputMin,
|
561
|
+
outputMax,
|
562
|
+
value
|
563
|
+
}) {
|
564
|
+
if (value === void 0) {
|
565
|
+
return void 0;
|
566
|
+
}
|
567
|
+
const inputRange = inputMax - inputMin;
|
568
|
+
const outputRange = outputMax - outputMin;
|
569
|
+
return (value - inputMin) * outputRange / inputRange + outputMin;
|
570
|
+
}
|
571
|
+
|
572
|
+
// ai-model-specification/util/uint8-utils.ts
|
573
|
+
function convertBase64ToUint8Array(base64String) {
|
574
|
+
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
575
|
+
const latin1string = globalThis.atob(base64Url);
|
576
|
+
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
|
577
|
+
}
|
578
|
+
function convertUint8ArrayToBase64(array) {
|
579
|
+
let latin1string = "";
|
580
|
+
for (let i = 0; i < array.length; i++) {
|
581
|
+
latin1string += String.fromCodePoint(array[i]);
|
582
|
+
}
|
583
|
+
return globalThis.btoa(latin1string);
|
584
|
+
}
|
585
|
+
|
586
|
+
// ai-model-specification/errors/invalid-tool-arguments-error.ts
|
587
|
+
var InvalidToolArgumentsError = class extends Error {
|
588
|
+
constructor({
|
589
|
+
toolArgs,
|
590
|
+
toolName,
|
591
|
+
cause,
|
592
|
+
message = `Invalid arguments for tool ${toolName}: ${getErrorMessage(
|
593
|
+
cause
|
594
|
+
)}`
|
595
|
+
}) {
|
596
|
+
super(message);
|
597
|
+
this.name = "AI_InvalidToolArgumentsError";
|
598
|
+
this.toolArgs = toolArgs;
|
599
|
+
this.toolName = toolName;
|
600
|
+
this.cause = cause;
|
601
|
+
}
|
602
|
+
static isInvalidToolArgumentsError(error) {
|
603
|
+
return error instanceof Error && error.name === "AI_InvalidToolArgumentsError" && typeof error.toolName === "string" && typeof error.toolArgs === "string";
|
604
|
+
}
|
605
|
+
toJSON() {
|
606
|
+
return {
|
607
|
+
name: this.name,
|
608
|
+
message: this.message,
|
609
|
+
cause: this.cause,
|
610
|
+
stack: this.stack,
|
611
|
+
toolName: this.toolName,
|
612
|
+
toolArgs: this.toolArgs
|
613
|
+
};
|
614
|
+
}
|
615
|
+
};
|
616
|
+
|
617
|
+
// ai-model-specification/errors/no-object-generated-error.ts
|
618
|
+
var NoTextGeneratedError = class extends Error {
|
619
|
+
constructor() {
|
620
|
+
super(`No text generated.`);
|
621
|
+
this.name = "AI_NoTextGeneratedError";
|
622
|
+
}
|
623
|
+
static isNoTextGeneratedError(error) {
|
624
|
+
return error instanceof Error && error.name === "AI_NoTextGeneratedError";
|
625
|
+
}
|
626
|
+
toJSON() {
|
627
|
+
return {
|
628
|
+
name: this.name,
|
629
|
+
cause: this.cause,
|
630
|
+
message: this.message,
|
631
|
+
stack: this.stack
|
632
|
+
};
|
633
|
+
}
|
634
|
+
};
|
635
|
+
|
636
|
+
// ai-model-specification/errors/no-such-tool-error.ts
|
637
|
+
var NoSuchToolError = class extends Error {
|
638
|
+
constructor({ message, toolName }) {
|
639
|
+
super(message);
|
640
|
+
this.name = "AI_NoSuchToolError";
|
641
|
+
this.toolName = toolName;
|
642
|
+
}
|
643
|
+
static isNoSuchToolError(error) {
|
644
|
+
return error instanceof Error && error.name === "AI_NoSuchToolError" && typeof error.toolName === "string";
|
645
|
+
}
|
646
|
+
toJSON() {
|
647
|
+
return {
|
648
|
+
name: this.name,
|
649
|
+
message: this.message,
|
650
|
+
stack: this.stack,
|
651
|
+
toolName: this.toolName
|
652
|
+
};
|
653
|
+
}
|
654
|
+
};
|
655
|
+
|
656
|
+
// ai-model-specification/errors/retry-error.ts
|
657
|
+
var RetryError = class extends Error {
|
658
|
+
constructor({
|
659
|
+
message,
|
660
|
+
reason,
|
661
|
+
errors
|
662
|
+
}) {
|
663
|
+
super(message);
|
664
|
+
this.name = "AI_RetryError";
|
665
|
+
this.reason = reason;
|
666
|
+
this.errors = errors;
|
667
|
+
this.lastError = errors[errors.length - 1];
|
668
|
+
}
|
669
|
+
static isRetryError(error) {
|
670
|
+
return error instanceof Error && error.name === "AI_RetryError" && typeof error.reason === "string" && Array.isArray(error.errors);
|
671
|
+
}
|
672
|
+
toJSON() {
|
673
|
+
return {
|
674
|
+
name: this.name,
|
675
|
+
message: this.message,
|
676
|
+
reason: this.reason,
|
677
|
+
lastError: this.lastError,
|
678
|
+
errors: this.errors
|
679
|
+
};
|
680
|
+
}
|
681
|
+
};
|
682
|
+
|
683
|
+
// ai-model-specification/errors/unsupported-functionality-error.ts
|
684
|
+
var UnsupportedFunctionalityError = class extends Error {
|
685
|
+
constructor({
|
686
|
+
provider,
|
687
|
+
functionality
|
688
|
+
}) {
|
689
|
+
super(
|
690
|
+
`Functionality not supported by the provider. Provider: ${provider}.
|
691
|
+
Functionality: ${functionality}`
|
692
|
+
);
|
693
|
+
this.name = "AI_UnsupportedFunctionalityError";
|
694
|
+
this.provider = provider;
|
695
|
+
this.functionality = functionality;
|
696
|
+
}
|
697
|
+
static isUnsupportedFunctionalityError(error) {
|
698
|
+
return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
|
699
|
+
}
|
700
|
+
toJSON() {
|
701
|
+
return {
|
702
|
+
name: this.name,
|
703
|
+
message: this.message,
|
704
|
+
stack: this.stack,
|
705
|
+
provider: this.provider,
|
706
|
+
functionality: this.functionality
|
707
|
+
};
|
708
|
+
}
|
709
|
+
};
|
710
|
+
export {
|
711
|
+
APICallError,
|
712
|
+
InvalidArgumentError,
|
713
|
+
InvalidDataContentError,
|
714
|
+
InvalidPromptError,
|
715
|
+
InvalidResponseDataError,
|
716
|
+
InvalidToolArgumentsError,
|
717
|
+
JSONParseError,
|
718
|
+
LoadAPIKeyError,
|
719
|
+
NoResponseBodyError,
|
720
|
+
NoSuchToolError,
|
721
|
+
NoTextGeneratedError,
|
722
|
+
RetryError,
|
723
|
+
TypeValidationError,
|
724
|
+
UnsupportedFunctionalityError,
|
725
|
+
convertBase64ToUint8Array,
|
726
|
+
convertUint8ArrayToBase64,
|
727
|
+
createEventSourceResponseHandler,
|
728
|
+
createJsonErrorResponseHandler,
|
729
|
+
createJsonResponseHandler,
|
730
|
+
generateId,
|
731
|
+
getErrorMessage,
|
732
|
+
isParseableJson,
|
733
|
+
loadApiKey,
|
734
|
+
parseJSON,
|
735
|
+
postJsonToApi,
|
736
|
+
postToApi,
|
737
|
+
safeParseJSON,
|
738
|
+
safeValidateTypes,
|
739
|
+
scale,
|
740
|
+
validateTypes
|
741
|
+
};
|
742
|
+
//# sourceMappingURL=index.mjs.map
|