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