ai 3.0.16 → 3.0.18
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/anthropic/dist/index.d.mts +371 -0
- package/anthropic/dist/index.d.ts +371 -0
- package/anthropic/dist/index.js +946 -0
- package/anthropic/dist/index.js.map +1 -0
- package/anthropic/dist/index.mjs +910 -0
- package/anthropic/dist/index.mjs.map +1 -0
- package/dist/index.js +29 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +29 -2
- package/dist/index.mjs.map +1 -1
- package/google/dist/index.d.mts +365 -0
- package/google/dist/index.d.ts +365 -0
- package/google/dist/index.js +950 -0
- package/google/dist/index.js.map +1 -0
- package/google/dist/index.mjs +914 -0
- package/google/dist/index.mjs.map +1 -0
- package/mistral/dist/index.js +6 -6
- package/mistral/dist/index.js.map +1 -1
- package/mistral/dist/index.mjs +6 -6
- package/mistral/dist/index.mjs.map +1 -1
- package/openai/dist/index.js +1 -2
- package/openai/dist/index.js.map +1 -1
- package/openai/dist/index.mjs +1 -2
- package/openai/dist/index.mjs.map +1 -1
- package/package.json +18 -2
- package/spec/dist/index.js +1 -2
- package/spec/dist/index.js.map +1 -1
- package/spec/dist/index.mjs +1 -2
- package/spec/dist/index.mjs.map +1 -1
@@ -0,0 +1,950 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// google/index.ts
|
31
|
+
var google_exports = {};
|
32
|
+
__export(google_exports, {
|
33
|
+
Google: () => Google,
|
34
|
+
google: () => google
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(google_exports);
|
37
|
+
|
38
|
+
// spec/errors/api-call-error.ts
|
39
|
+
var APICallError = class extends Error {
|
40
|
+
constructor({
|
41
|
+
message,
|
42
|
+
url,
|
43
|
+
requestBodyValues,
|
44
|
+
statusCode,
|
45
|
+
responseBody,
|
46
|
+
cause,
|
47
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
48
|
+
statusCode === 409 || // conflict
|
49
|
+
statusCode === 429 || // too many requests
|
50
|
+
statusCode >= 500),
|
51
|
+
// server error
|
52
|
+
data
|
53
|
+
}) {
|
54
|
+
super(message);
|
55
|
+
this.name = "AI_APICallError";
|
56
|
+
this.url = url;
|
57
|
+
this.requestBodyValues = requestBodyValues;
|
58
|
+
this.statusCode = statusCode;
|
59
|
+
this.responseBody = responseBody;
|
60
|
+
this.cause = cause;
|
61
|
+
this.isRetryable = isRetryable;
|
62
|
+
this.data = data;
|
63
|
+
}
|
64
|
+
static isAPICallError(error) {
|
65
|
+
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");
|
66
|
+
}
|
67
|
+
toJSON() {
|
68
|
+
return {
|
69
|
+
name: this.name,
|
70
|
+
message: this.message,
|
71
|
+
url: this.url,
|
72
|
+
requestBodyValues: this.requestBodyValues,
|
73
|
+
statusCode: this.statusCode,
|
74
|
+
responseBody: this.responseBody,
|
75
|
+
cause: this.cause,
|
76
|
+
isRetryable: this.isRetryable,
|
77
|
+
data: this.data
|
78
|
+
};
|
79
|
+
}
|
80
|
+
};
|
81
|
+
|
82
|
+
// spec/util/generate-id.ts
|
83
|
+
var import_non_secure = require("nanoid/non-secure");
|
84
|
+
var generateId = (0, import_non_secure.customAlphabet)(
|
85
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
86
|
+
7
|
87
|
+
);
|
88
|
+
|
89
|
+
// spec/util/get-error-message.ts
|
90
|
+
function getErrorMessage(error) {
|
91
|
+
if (error == null) {
|
92
|
+
return "unknown error";
|
93
|
+
}
|
94
|
+
if (typeof error === "string") {
|
95
|
+
return error;
|
96
|
+
}
|
97
|
+
if (error instanceof Error) {
|
98
|
+
return error.message;
|
99
|
+
}
|
100
|
+
return JSON.stringify(error);
|
101
|
+
}
|
102
|
+
|
103
|
+
// spec/errors/load-api-key-error.ts
|
104
|
+
var LoadAPIKeyError = class extends Error {
|
105
|
+
constructor({ message }) {
|
106
|
+
super(message);
|
107
|
+
this.name = "AI_LoadAPIKeyError";
|
108
|
+
}
|
109
|
+
static isLoadAPIKeyError(error) {
|
110
|
+
return error instanceof Error && error.name === "AI_LoadAPIKeyError";
|
111
|
+
}
|
112
|
+
toJSON() {
|
113
|
+
return {
|
114
|
+
name: this.name,
|
115
|
+
message: this.message
|
116
|
+
};
|
117
|
+
}
|
118
|
+
};
|
119
|
+
|
120
|
+
// spec/util/load-api-key.ts
|
121
|
+
function loadApiKey({
|
122
|
+
apiKey,
|
123
|
+
environmentVariableName,
|
124
|
+
apiKeyParameterName = "apiKey",
|
125
|
+
description
|
126
|
+
}) {
|
127
|
+
if (typeof apiKey === "string") {
|
128
|
+
return apiKey;
|
129
|
+
}
|
130
|
+
if (apiKey != null) {
|
131
|
+
throw new LoadAPIKeyError({
|
132
|
+
message: `${description} API key must be a string.`
|
133
|
+
});
|
134
|
+
}
|
135
|
+
if (typeof process === "undefined") {
|
136
|
+
throw new LoadAPIKeyError({
|
137
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
138
|
+
});
|
139
|
+
}
|
140
|
+
apiKey = process.env[environmentVariableName];
|
141
|
+
if (apiKey == null) {
|
142
|
+
throw new LoadAPIKeyError({
|
143
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
144
|
+
});
|
145
|
+
}
|
146
|
+
if (typeof apiKey !== "string") {
|
147
|
+
throw new LoadAPIKeyError({
|
148
|
+
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
149
|
+
});
|
150
|
+
}
|
151
|
+
return apiKey;
|
152
|
+
}
|
153
|
+
|
154
|
+
// spec/util/parse-json.ts
|
155
|
+
var import_secure_json_parse = __toESM(require("secure-json-parse"));
|
156
|
+
|
157
|
+
// spec/errors/json-parse-error.ts
|
158
|
+
var JSONParseError = class extends Error {
|
159
|
+
constructor({ text, cause }) {
|
160
|
+
super(
|
161
|
+
`JSON parsing failed: Text: ${text}.
|
162
|
+
Error message: ${getErrorMessage(cause)}`
|
163
|
+
);
|
164
|
+
this.name = "AI_JSONParseError";
|
165
|
+
this.cause = cause;
|
166
|
+
this.text = text;
|
167
|
+
}
|
168
|
+
static isJSONParseError(error) {
|
169
|
+
return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
|
170
|
+
}
|
171
|
+
toJSON() {
|
172
|
+
return {
|
173
|
+
name: this.name,
|
174
|
+
message: this.message,
|
175
|
+
cause: this.cause,
|
176
|
+
stack: this.stack,
|
177
|
+
valueText: this.text
|
178
|
+
};
|
179
|
+
}
|
180
|
+
};
|
181
|
+
|
182
|
+
// spec/errors/type-validation-error.ts
|
183
|
+
var TypeValidationError = class extends Error {
|
184
|
+
constructor({ value, cause }) {
|
185
|
+
super(
|
186
|
+
`Type validation failed: Value: ${JSON.stringify(value)}.
|
187
|
+
Error message: ${getErrorMessage(cause)}`
|
188
|
+
);
|
189
|
+
this.name = "AI_TypeValidationError";
|
190
|
+
this.cause = cause;
|
191
|
+
this.value = value;
|
192
|
+
}
|
193
|
+
static isTypeValidationError(error) {
|
194
|
+
return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
|
195
|
+
}
|
196
|
+
toJSON() {
|
197
|
+
return {
|
198
|
+
name: this.name,
|
199
|
+
message: this.message,
|
200
|
+
cause: this.cause,
|
201
|
+
stack: this.stack,
|
202
|
+
value: this.value
|
203
|
+
};
|
204
|
+
}
|
205
|
+
};
|
206
|
+
|
207
|
+
// spec/util/validate-types.ts
|
208
|
+
function validateTypes({
|
209
|
+
value,
|
210
|
+
schema
|
211
|
+
}) {
|
212
|
+
try {
|
213
|
+
return schema.parse(value);
|
214
|
+
} catch (error) {
|
215
|
+
throw new TypeValidationError({ value, cause: error });
|
216
|
+
}
|
217
|
+
}
|
218
|
+
function safeValidateTypes({
|
219
|
+
value,
|
220
|
+
schema
|
221
|
+
}) {
|
222
|
+
try {
|
223
|
+
const validationResult = schema.safeParse(value);
|
224
|
+
if (validationResult.success) {
|
225
|
+
return {
|
226
|
+
success: true,
|
227
|
+
value: validationResult.data
|
228
|
+
};
|
229
|
+
}
|
230
|
+
return {
|
231
|
+
success: false,
|
232
|
+
error: new TypeValidationError({
|
233
|
+
value,
|
234
|
+
cause: validationResult.error
|
235
|
+
})
|
236
|
+
};
|
237
|
+
} catch (error) {
|
238
|
+
return {
|
239
|
+
success: false,
|
240
|
+
error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
|
241
|
+
};
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
// spec/util/parse-json.ts
|
246
|
+
function parseJSON({
|
247
|
+
text,
|
248
|
+
schema
|
249
|
+
}) {
|
250
|
+
try {
|
251
|
+
const value = import_secure_json_parse.default.parse(text);
|
252
|
+
if (schema == null) {
|
253
|
+
return value;
|
254
|
+
}
|
255
|
+
return validateTypes({ value, schema });
|
256
|
+
} catch (error) {
|
257
|
+
if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
|
258
|
+
throw error;
|
259
|
+
}
|
260
|
+
throw new JSONParseError({ text, cause: error });
|
261
|
+
}
|
262
|
+
}
|
263
|
+
function safeParseJSON({
|
264
|
+
text,
|
265
|
+
schema
|
266
|
+
}) {
|
267
|
+
try {
|
268
|
+
const value = import_secure_json_parse.default.parse(text);
|
269
|
+
if (schema == null) {
|
270
|
+
return {
|
271
|
+
success: true,
|
272
|
+
value
|
273
|
+
};
|
274
|
+
}
|
275
|
+
return safeValidateTypes({ value, schema });
|
276
|
+
} catch (error) {
|
277
|
+
return {
|
278
|
+
success: false,
|
279
|
+
error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
|
280
|
+
};
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
// spec/util/post-to-api.ts
|
285
|
+
var postJsonToApi = async ({
|
286
|
+
url,
|
287
|
+
headers,
|
288
|
+
body,
|
289
|
+
failedResponseHandler,
|
290
|
+
successfulResponseHandler,
|
291
|
+
abortSignal
|
292
|
+
}) => postToApi({
|
293
|
+
url,
|
294
|
+
headers: {
|
295
|
+
...headers,
|
296
|
+
"Content-Type": "application/json"
|
297
|
+
},
|
298
|
+
body: {
|
299
|
+
content: JSON.stringify(body),
|
300
|
+
values: body
|
301
|
+
},
|
302
|
+
failedResponseHandler,
|
303
|
+
successfulResponseHandler,
|
304
|
+
abortSignal
|
305
|
+
});
|
306
|
+
var postToApi = async ({
|
307
|
+
url,
|
308
|
+
headers = {},
|
309
|
+
body,
|
310
|
+
successfulResponseHandler,
|
311
|
+
failedResponseHandler,
|
312
|
+
abortSignal
|
313
|
+
}) => {
|
314
|
+
try {
|
315
|
+
const definedHeaders = Object.fromEntries(
|
316
|
+
Object.entries(headers).filter(([_key, value]) => value != null)
|
317
|
+
);
|
318
|
+
const response = await fetch(url, {
|
319
|
+
method: "POST",
|
320
|
+
headers: definedHeaders,
|
321
|
+
body: body.content,
|
322
|
+
signal: abortSignal
|
323
|
+
});
|
324
|
+
if (!response.ok) {
|
325
|
+
try {
|
326
|
+
throw await failedResponseHandler({
|
327
|
+
response,
|
328
|
+
url,
|
329
|
+
requestBodyValues: body.values
|
330
|
+
});
|
331
|
+
} catch (error) {
|
332
|
+
if (error instanceof Error) {
|
333
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
334
|
+
throw error;
|
335
|
+
}
|
336
|
+
}
|
337
|
+
throw new APICallError({
|
338
|
+
message: "Failed to process error response",
|
339
|
+
cause: error,
|
340
|
+
statusCode: response.status,
|
341
|
+
url,
|
342
|
+
requestBodyValues: body.values
|
343
|
+
});
|
344
|
+
}
|
345
|
+
}
|
346
|
+
try {
|
347
|
+
return await successfulResponseHandler({
|
348
|
+
response,
|
349
|
+
url,
|
350
|
+
requestBodyValues: body.values
|
351
|
+
});
|
352
|
+
} catch (error) {
|
353
|
+
if (error instanceof Error) {
|
354
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
355
|
+
throw error;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
throw new APICallError({
|
359
|
+
message: "Failed to process successful response",
|
360
|
+
cause: error,
|
361
|
+
statusCode: response.status,
|
362
|
+
url,
|
363
|
+
requestBodyValues: body.values
|
364
|
+
});
|
365
|
+
}
|
366
|
+
} catch (error) {
|
367
|
+
if (error instanceof Error) {
|
368
|
+
if (error.name === "AbortError") {
|
369
|
+
throw error;
|
370
|
+
}
|
371
|
+
}
|
372
|
+
if (error instanceof TypeError && error.message === "fetch failed") {
|
373
|
+
const cause = error.cause;
|
374
|
+
if (cause != null) {
|
375
|
+
throw new APICallError({
|
376
|
+
message: `Cannot connect to API: ${cause.message}`,
|
377
|
+
cause,
|
378
|
+
url,
|
379
|
+
requestBodyValues: body.values,
|
380
|
+
isRetryable: true
|
381
|
+
// retry when network error
|
382
|
+
});
|
383
|
+
}
|
384
|
+
}
|
385
|
+
throw error;
|
386
|
+
}
|
387
|
+
};
|
388
|
+
|
389
|
+
// spec/util/response-handler.ts
|
390
|
+
var import_stream = require("eventsource-parser/stream");
|
391
|
+
|
392
|
+
// spec/errors/no-response-body-error.ts
|
393
|
+
var NoResponseBodyError = class extends Error {
|
394
|
+
constructor({ message = "No response body" } = {}) {
|
395
|
+
super(message);
|
396
|
+
this.name = "AI_NoResponseBodyError";
|
397
|
+
}
|
398
|
+
static isNoResponseBodyError(error) {
|
399
|
+
return error instanceof Error && error.name === "AI_NoResponseBodyError";
|
400
|
+
}
|
401
|
+
toJSON() {
|
402
|
+
return {
|
403
|
+
name: this.name,
|
404
|
+
message: this.message,
|
405
|
+
stack: this.stack
|
406
|
+
};
|
407
|
+
}
|
408
|
+
};
|
409
|
+
|
410
|
+
// spec/util/response-handler.ts
|
411
|
+
var createJsonErrorResponseHandler = ({
|
412
|
+
errorSchema,
|
413
|
+
errorToMessage,
|
414
|
+
isRetryable
|
415
|
+
}) => async ({ response, url, requestBodyValues }) => {
|
416
|
+
const responseBody = await response.text();
|
417
|
+
if (responseBody.trim() === "") {
|
418
|
+
return new APICallError({
|
419
|
+
message: response.statusText,
|
420
|
+
url,
|
421
|
+
requestBodyValues,
|
422
|
+
statusCode: response.status,
|
423
|
+
responseBody,
|
424
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
425
|
+
});
|
426
|
+
}
|
427
|
+
try {
|
428
|
+
const parsedError = parseJSON({
|
429
|
+
text: responseBody,
|
430
|
+
schema: errorSchema
|
431
|
+
});
|
432
|
+
return new APICallError({
|
433
|
+
message: errorToMessage(parsedError),
|
434
|
+
url,
|
435
|
+
requestBodyValues,
|
436
|
+
statusCode: response.status,
|
437
|
+
responseBody,
|
438
|
+
data: parsedError,
|
439
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
440
|
+
});
|
441
|
+
} catch (parseError) {
|
442
|
+
return new APICallError({
|
443
|
+
message: response.statusText,
|
444
|
+
url,
|
445
|
+
requestBodyValues,
|
446
|
+
statusCode: response.status,
|
447
|
+
responseBody,
|
448
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
449
|
+
});
|
450
|
+
}
|
451
|
+
};
|
452
|
+
var createEventSourceResponseHandler = (chunkSchema2) => async ({ response }) => {
|
453
|
+
if (response.body == null) {
|
454
|
+
throw new NoResponseBodyError();
|
455
|
+
}
|
456
|
+
return response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
|
457
|
+
new TransformStream({
|
458
|
+
transform({ data }, controller) {
|
459
|
+
if (data === "[DONE]") {
|
460
|
+
return;
|
461
|
+
}
|
462
|
+
controller.enqueue(
|
463
|
+
safeParseJSON({
|
464
|
+
text: data,
|
465
|
+
schema: chunkSchema2
|
466
|
+
})
|
467
|
+
);
|
468
|
+
}
|
469
|
+
})
|
470
|
+
);
|
471
|
+
};
|
472
|
+
var createJsonResponseHandler = (responseSchema2) => async ({ response, url, requestBodyValues }) => {
|
473
|
+
const responseBody = await response.text();
|
474
|
+
const parsedResult = safeParseJSON({
|
475
|
+
text: responseBody,
|
476
|
+
schema: responseSchema2
|
477
|
+
});
|
478
|
+
if (!parsedResult.success) {
|
479
|
+
throw new APICallError({
|
480
|
+
message: "Invalid JSON response",
|
481
|
+
cause: parsedResult.error,
|
482
|
+
statusCode: response.status,
|
483
|
+
responseBody,
|
484
|
+
url,
|
485
|
+
requestBodyValues
|
486
|
+
});
|
487
|
+
}
|
488
|
+
return parsedResult.value;
|
489
|
+
};
|
490
|
+
|
491
|
+
// spec/util/uint8-utils.ts
|
492
|
+
function convertUint8ArrayToBase64(array) {
|
493
|
+
let latin1string = "";
|
494
|
+
for (let i = 0; i < array.length; i++) {
|
495
|
+
latin1string += String.fromCodePoint(array[i]);
|
496
|
+
}
|
497
|
+
return globalThis.btoa(latin1string);
|
498
|
+
}
|
499
|
+
|
500
|
+
// spec/errors/unsupported-functionality-error.ts
|
501
|
+
var UnsupportedFunctionalityError = class extends Error {
|
502
|
+
constructor({
|
503
|
+
provider,
|
504
|
+
functionality
|
505
|
+
}) {
|
506
|
+
super(
|
507
|
+
`'${functionality}' functionality not supported by the '${provider}' provider.`
|
508
|
+
);
|
509
|
+
this.name = "AI_UnsupportedFunctionalityError";
|
510
|
+
this.provider = provider;
|
511
|
+
this.functionality = functionality;
|
512
|
+
}
|
513
|
+
static isUnsupportedFunctionalityError(error) {
|
514
|
+
return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
|
515
|
+
}
|
516
|
+
toJSON() {
|
517
|
+
return {
|
518
|
+
name: this.name,
|
519
|
+
message: this.message,
|
520
|
+
stack: this.stack,
|
521
|
+
provider: this.provider,
|
522
|
+
functionality: this.functionality
|
523
|
+
};
|
524
|
+
}
|
525
|
+
};
|
526
|
+
|
527
|
+
// google/google-generative-ai-language-model.ts
|
528
|
+
var import_zod2 = require("zod");
|
529
|
+
|
530
|
+
// google/convert-to-google-generative-ai-messages.ts
|
531
|
+
function convertToGoogleGenerativeAIMessages({
|
532
|
+
prompt,
|
533
|
+
provider
|
534
|
+
}) {
|
535
|
+
const messages = [];
|
536
|
+
for (const { role, content } of prompt) {
|
537
|
+
switch (role) {
|
538
|
+
case "system": {
|
539
|
+
messages.push({ role: "user", parts: [{ text: content }] });
|
540
|
+
messages.push({ role: "model", parts: [{ text: "" }] });
|
541
|
+
break;
|
542
|
+
}
|
543
|
+
case "user": {
|
544
|
+
messages.push({
|
545
|
+
role: "user",
|
546
|
+
parts: content.map((part) => {
|
547
|
+
var _a;
|
548
|
+
switch (part.type) {
|
549
|
+
case "text": {
|
550
|
+
return { text: part.text };
|
551
|
+
}
|
552
|
+
case "image": {
|
553
|
+
if (part.image instanceof URL) {
|
554
|
+
throw new UnsupportedFunctionalityError({
|
555
|
+
provider,
|
556
|
+
functionality: "URL image parts"
|
557
|
+
});
|
558
|
+
} else {
|
559
|
+
return {
|
560
|
+
inlineData: {
|
561
|
+
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
562
|
+
data: convertUint8ArrayToBase64(part.image)
|
563
|
+
}
|
564
|
+
};
|
565
|
+
}
|
566
|
+
}
|
567
|
+
}
|
568
|
+
})
|
569
|
+
});
|
570
|
+
break;
|
571
|
+
}
|
572
|
+
case "assistant": {
|
573
|
+
messages.push({
|
574
|
+
role: "model",
|
575
|
+
parts: content.map((part) => {
|
576
|
+
switch (part.type) {
|
577
|
+
case "text": {
|
578
|
+
return part.text.length === 0 ? void 0 : { text: part.text };
|
579
|
+
}
|
580
|
+
case "tool-call": {
|
581
|
+
return {
|
582
|
+
functionCall: {
|
583
|
+
name: part.toolName,
|
584
|
+
args: part.args
|
585
|
+
}
|
586
|
+
};
|
587
|
+
}
|
588
|
+
}
|
589
|
+
}).filter(
|
590
|
+
(part) => part !== void 0
|
591
|
+
)
|
592
|
+
});
|
593
|
+
break;
|
594
|
+
}
|
595
|
+
case "tool": {
|
596
|
+
messages.push({
|
597
|
+
role: "user",
|
598
|
+
parts: content.map((part) => ({
|
599
|
+
functionResponse: {
|
600
|
+
name: part.toolName,
|
601
|
+
response: part.result
|
602
|
+
}
|
603
|
+
}))
|
604
|
+
});
|
605
|
+
break;
|
606
|
+
}
|
607
|
+
default: {
|
608
|
+
const _exhaustiveCheck = role;
|
609
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
610
|
+
}
|
611
|
+
}
|
612
|
+
}
|
613
|
+
return messages;
|
614
|
+
}
|
615
|
+
|
616
|
+
// google/google-error.ts
|
617
|
+
var import_zod = require("zod");
|
618
|
+
var googleErrorDataSchema = import_zod.z.object({
|
619
|
+
error: import_zod.z.object({
|
620
|
+
code: import_zod.z.number().nullable(),
|
621
|
+
message: import_zod.z.string(),
|
622
|
+
status: import_zod.z.string()
|
623
|
+
})
|
624
|
+
});
|
625
|
+
var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
626
|
+
errorSchema: googleErrorDataSchema,
|
627
|
+
errorToMessage: (data) => data.error.message
|
628
|
+
});
|
629
|
+
|
630
|
+
// google/map-google-generative-ai-finish-reason.ts
|
631
|
+
function mapGoogleGenerativeAIFinishReason(finishReason) {
|
632
|
+
switch (finishReason) {
|
633
|
+
case "STOP":
|
634
|
+
return "stop";
|
635
|
+
case "MAX_TOKENS":
|
636
|
+
return "length";
|
637
|
+
case "RECITATION":
|
638
|
+
case "SAFETY":
|
639
|
+
return "content-filter";
|
640
|
+
case "FINISH_REASON_UNSPECIFIED":
|
641
|
+
case "OTHER":
|
642
|
+
default:
|
643
|
+
return "other";
|
644
|
+
}
|
645
|
+
}
|
646
|
+
|
647
|
+
// google/google-generative-ai-language-model.ts
|
648
|
+
var GoogleGenerativeAILanguageModel = class {
|
649
|
+
constructor(modelId, settings, config) {
|
650
|
+
this.specificationVersion = "v1";
|
651
|
+
this.defaultObjectGenerationMode = void 0;
|
652
|
+
this.modelId = modelId;
|
653
|
+
this.settings = settings;
|
654
|
+
this.config = config;
|
655
|
+
}
|
656
|
+
get provider() {
|
657
|
+
return this.config.provider;
|
658
|
+
}
|
659
|
+
getArgs({
|
660
|
+
mode,
|
661
|
+
prompt,
|
662
|
+
maxTokens,
|
663
|
+
temperature,
|
664
|
+
topP,
|
665
|
+
frequencyPenalty,
|
666
|
+
presencePenalty,
|
667
|
+
seed
|
668
|
+
}) {
|
669
|
+
var _a;
|
670
|
+
const type = mode.type;
|
671
|
+
const warnings = [];
|
672
|
+
if (frequencyPenalty != null) {
|
673
|
+
warnings.push({
|
674
|
+
type: "unsupported-setting",
|
675
|
+
setting: "frequencyPenalty"
|
676
|
+
});
|
677
|
+
}
|
678
|
+
if (presencePenalty != null) {
|
679
|
+
warnings.push({
|
680
|
+
type: "unsupported-setting",
|
681
|
+
setting: "presencePenalty"
|
682
|
+
});
|
683
|
+
}
|
684
|
+
if (seed != null) {
|
685
|
+
warnings.push({
|
686
|
+
type: "unsupported-setting",
|
687
|
+
setting: "seed"
|
688
|
+
});
|
689
|
+
}
|
690
|
+
const baseArgs = {
|
691
|
+
generationConfig: {
|
692
|
+
// model specific settings:
|
693
|
+
topK: this.settings.topK,
|
694
|
+
// standardized settings:
|
695
|
+
maxOutputTokens: maxTokens,
|
696
|
+
temperature,
|
697
|
+
topP
|
698
|
+
},
|
699
|
+
// prompt:
|
700
|
+
contents: convertToGoogleGenerativeAIMessages({
|
701
|
+
provider: this.provider,
|
702
|
+
prompt
|
703
|
+
})
|
704
|
+
};
|
705
|
+
switch (type) {
|
706
|
+
case "regular": {
|
707
|
+
const functionDeclarations = (_a = mode.tools) == null ? void 0 : _a.map((tool) => {
|
708
|
+
var _a2;
|
709
|
+
return {
|
710
|
+
name: tool.name,
|
711
|
+
description: (_a2 = tool.description) != null ? _a2 : "",
|
712
|
+
parameters: prepareJsonSchema(tool.parameters)
|
713
|
+
};
|
714
|
+
});
|
715
|
+
return {
|
716
|
+
args: {
|
717
|
+
...baseArgs,
|
718
|
+
tools: functionDeclarations == null ? void 0 : { functionDeclarations }
|
719
|
+
},
|
720
|
+
warnings
|
721
|
+
};
|
722
|
+
}
|
723
|
+
case "object-json": {
|
724
|
+
throw new UnsupportedFunctionalityError({
|
725
|
+
functionality: "object-json mode",
|
726
|
+
provider: this.provider
|
727
|
+
});
|
728
|
+
}
|
729
|
+
case "object-tool": {
|
730
|
+
throw new UnsupportedFunctionalityError({
|
731
|
+
functionality: "object-tool mode",
|
732
|
+
provider: this.provider
|
733
|
+
});
|
734
|
+
}
|
735
|
+
case "object-grammar": {
|
736
|
+
throw new UnsupportedFunctionalityError({
|
737
|
+
functionality: "object-grammar mode",
|
738
|
+
provider: this.provider
|
739
|
+
});
|
740
|
+
}
|
741
|
+
default: {
|
742
|
+
const _exhaustiveCheck = type;
|
743
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
744
|
+
}
|
745
|
+
}
|
746
|
+
}
|
747
|
+
async doGenerate(options) {
|
748
|
+
var _a;
|
749
|
+
const { args, warnings } = this.getArgs(options);
|
750
|
+
const response = await postJsonToApi({
|
751
|
+
url: `${this.config.baseUrl}/${this.modelId}:generateContent`,
|
752
|
+
headers: this.config.headers(),
|
753
|
+
body: args,
|
754
|
+
failedResponseHandler: googleFailedResponseHandler,
|
755
|
+
successfulResponseHandler: createJsonResponseHandler(responseSchema),
|
756
|
+
abortSignal: options.abortSignal
|
757
|
+
});
|
758
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
759
|
+
const candidate = response.candidates[0];
|
760
|
+
return {
|
761
|
+
text: getTextFromParts(candidate.content.parts),
|
762
|
+
toolCalls: getToolCallsFromParts({
|
763
|
+
parts: candidate.content.parts,
|
764
|
+
generateId: this.config.generateId
|
765
|
+
}),
|
766
|
+
finishReason: mapGoogleGenerativeAIFinishReason(candidate.finishReason),
|
767
|
+
usage: {
|
768
|
+
promptTokens: NaN,
|
769
|
+
completionTokens: (_a = candidate.tokenCount) != null ? _a : NaN
|
770
|
+
},
|
771
|
+
rawCall: { rawPrompt, rawSettings },
|
772
|
+
warnings
|
773
|
+
};
|
774
|
+
}
|
775
|
+
async doStream(options) {
|
776
|
+
const { args, warnings } = this.getArgs(options);
|
777
|
+
const response = await postJsonToApi({
|
778
|
+
url: `${this.config.baseUrl}/${this.modelId}:streamGenerateContent?alt=sse`,
|
779
|
+
headers: this.config.headers(),
|
780
|
+
body: args,
|
781
|
+
failedResponseHandler: googleFailedResponseHandler,
|
782
|
+
successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),
|
783
|
+
abortSignal: options.abortSignal
|
784
|
+
});
|
785
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
786
|
+
let finishReason = "other";
|
787
|
+
let usage = {
|
788
|
+
promptTokens: Number.NaN,
|
789
|
+
completionTokens: Number.NaN
|
790
|
+
};
|
791
|
+
const generateId2 = this.config.generateId;
|
792
|
+
return {
|
793
|
+
stream: response.pipeThrough(
|
794
|
+
new TransformStream({
|
795
|
+
transform(chunk, controller) {
|
796
|
+
if (!chunk.success) {
|
797
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
798
|
+
return;
|
799
|
+
}
|
800
|
+
const value = chunk.value;
|
801
|
+
const candidate = value.candidates[0];
|
802
|
+
if ((candidate == null ? void 0 : candidate.finishReason) != null) {
|
803
|
+
finishReason = mapGoogleGenerativeAIFinishReason(
|
804
|
+
candidate.finishReason
|
805
|
+
);
|
806
|
+
}
|
807
|
+
if (candidate.tokenCount != null) {
|
808
|
+
usage = {
|
809
|
+
promptTokens: NaN,
|
810
|
+
completionTokens: candidate.tokenCount
|
811
|
+
};
|
812
|
+
}
|
813
|
+
const deltaText = getTextFromParts(candidate.content.parts);
|
814
|
+
if (deltaText != null) {
|
815
|
+
controller.enqueue({
|
816
|
+
type: "text-delta",
|
817
|
+
textDelta: deltaText
|
818
|
+
});
|
819
|
+
}
|
820
|
+
const toolCallDeltas = getToolCallsFromParts({
|
821
|
+
parts: candidate.content.parts,
|
822
|
+
generateId: generateId2
|
823
|
+
});
|
824
|
+
if (toolCallDeltas != null) {
|
825
|
+
for (const toolCall of toolCallDeltas) {
|
826
|
+
controller.enqueue({
|
827
|
+
type: "tool-call-delta",
|
828
|
+
toolCallType: "function",
|
829
|
+
toolCallId: toolCall.toolCallId,
|
830
|
+
toolName: toolCall.toolName,
|
831
|
+
argsTextDelta: toolCall.args
|
832
|
+
});
|
833
|
+
controller.enqueue({
|
834
|
+
type: "tool-call",
|
835
|
+
toolCallType: "function",
|
836
|
+
toolCallId: toolCall.toolCallId,
|
837
|
+
toolName: toolCall.toolName,
|
838
|
+
args: toolCall.args
|
839
|
+
});
|
840
|
+
}
|
841
|
+
}
|
842
|
+
},
|
843
|
+
flush(controller) {
|
844
|
+
controller.enqueue({ type: "finish", finishReason, usage });
|
845
|
+
}
|
846
|
+
})
|
847
|
+
),
|
848
|
+
rawCall: { rawPrompt, rawSettings },
|
849
|
+
warnings
|
850
|
+
};
|
851
|
+
}
|
852
|
+
};
|
853
|
+
function prepareJsonSchema(jsonSchema) {
|
854
|
+
if (typeof jsonSchema !== "object") {
|
855
|
+
return jsonSchema;
|
856
|
+
}
|
857
|
+
if (Array.isArray(jsonSchema)) {
|
858
|
+
return jsonSchema.map(prepareJsonSchema);
|
859
|
+
}
|
860
|
+
const result = {};
|
861
|
+
for (const [key, value] of Object.entries(jsonSchema)) {
|
862
|
+
if (key === "additionalProperties" || key === "$schema") {
|
863
|
+
continue;
|
864
|
+
}
|
865
|
+
result[key] = prepareJsonSchema(value);
|
866
|
+
}
|
867
|
+
return result;
|
868
|
+
}
|
869
|
+
function getToolCallsFromParts({
|
870
|
+
parts,
|
871
|
+
generateId: generateId2
|
872
|
+
}) {
|
873
|
+
const functionCallParts = parts.filter(
|
874
|
+
(part) => "functionCall" in part
|
875
|
+
);
|
876
|
+
return functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
877
|
+
toolCallType: "function",
|
878
|
+
toolCallId: generateId2(),
|
879
|
+
toolName: part.functionCall.name,
|
880
|
+
args: JSON.stringify(part.functionCall.args)
|
881
|
+
}));
|
882
|
+
}
|
883
|
+
function getTextFromParts(parts) {
|
884
|
+
const textParts = parts.filter((part) => "text" in part);
|
885
|
+
return textParts.length === 0 ? void 0 : textParts.map((part) => part.text).join("");
|
886
|
+
}
|
887
|
+
var contentSchema = import_zod2.z.object({
|
888
|
+
role: import_zod2.z.string(),
|
889
|
+
parts: import_zod2.z.array(
|
890
|
+
import_zod2.z.union([
|
891
|
+
import_zod2.z.object({
|
892
|
+
text: import_zod2.z.string()
|
893
|
+
}),
|
894
|
+
import_zod2.z.object({
|
895
|
+
functionCall: import_zod2.z.object({
|
896
|
+
name: import_zod2.z.string(),
|
897
|
+
args: import_zod2.z.unknown()
|
898
|
+
})
|
899
|
+
})
|
900
|
+
])
|
901
|
+
)
|
902
|
+
});
|
903
|
+
var candidateSchema = import_zod2.z.object({
|
904
|
+
content: contentSchema,
|
905
|
+
finishReason: import_zod2.z.string().optional(),
|
906
|
+
tokenCount: import_zod2.z.number().optional()
|
907
|
+
});
|
908
|
+
var responseSchema = import_zod2.z.object({
|
909
|
+
candidates: import_zod2.z.array(candidateSchema)
|
910
|
+
});
|
911
|
+
var chunkSchema = import_zod2.z.object({
|
912
|
+
candidates: import_zod2.z.array(candidateSchema)
|
913
|
+
});
|
914
|
+
|
915
|
+
// google/google-facade.ts
|
916
|
+
var Google = class {
|
917
|
+
constructor(options = {}) {
|
918
|
+
var _a;
|
919
|
+
this.baseUrl = options.baseUrl;
|
920
|
+
this.apiKey = options.apiKey;
|
921
|
+
this.generateId = (_a = options.generateId) != null ? _a : generateId;
|
922
|
+
}
|
923
|
+
get baseConfig() {
|
924
|
+
var _a;
|
925
|
+
return {
|
926
|
+
baseUrl: (_a = this.baseUrl) != null ? _a : "https://generativelanguage.googleapis.com/v1beta",
|
927
|
+
headers: () => ({
|
928
|
+
"x-goog-api-key": loadApiKey({
|
929
|
+
apiKey: this.apiKey,
|
930
|
+
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
931
|
+
description: "Google Generative AI"
|
932
|
+
})
|
933
|
+
})
|
934
|
+
};
|
935
|
+
}
|
936
|
+
generativeAI(modelId, settings = {}) {
|
937
|
+
return new GoogleGenerativeAILanguageModel(modelId, settings, {
|
938
|
+
provider: "google.generative-ai",
|
939
|
+
...this.baseConfig,
|
940
|
+
generateId: this.generateId
|
941
|
+
});
|
942
|
+
}
|
943
|
+
};
|
944
|
+
var google = new Google();
|
945
|
+
// Annotate the CommonJS export names for ESM import in node:
|
946
|
+
0 && (module.exports = {
|
947
|
+
Google,
|
948
|
+
google
|
949
|
+
});
|
950
|
+
//# sourceMappingURL=index.js.map
|