@ai-sdk/google 3.0.140 → 3.0.141
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 +12 -0
- package/dist/index.js +127 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +123 -31
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +119 -30
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +122 -30
- package/dist/internal/index.mjs.map +1 -1
- package/docs/15-google-generative-ai.mdx +14 -10
- package/package.json +2 -2
- package/src/convert-to-google-generative-ai-messages.ts +54 -5
- package/src/google-error.ts +46 -6
- package/src/google-generative-ai-language-model.ts +43 -16
- package/src/interactions/prepare-google-interactions-tools.ts +7 -4
package/dist/internal/index.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/google-generative-ai-language-model.ts
|
|
2
|
+
import {
|
|
3
|
+
APICallError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
2
5
|
import {
|
|
3
6
|
combineHeaders,
|
|
4
7
|
createEventSourceResponseHandler,
|
|
@@ -437,6 +440,7 @@ import {
|
|
|
437
440
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
438
441
|
} from "@ai-sdk/provider";
|
|
439
442
|
import {
|
|
443
|
+
stringifyWithBigIntAndCycles,
|
|
440
444
|
convertToBase64,
|
|
441
445
|
isUrlSupported,
|
|
442
446
|
secureJsonParse
|
|
@@ -503,6 +507,31 @@ function convertUrlToolResultPart(url, mediaType, supportedFunctionResponseUrls)
|
|
|
503
507
|
}
|
|
504
508
|
} : void 0;
|
|
505
509
|
}
|
|
510
|
+
function serializeFunctionResponseContent(value) {
|
|
511
|
+
return containsNonJsonValue(value) ? secureJsonParse(stringifyWithBigIntAndCycles(value)) : value;
|
|
512
|
+
}
|
|
513
|
+
function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
514
|
+
if (typeof value === "bigint") {
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
if (typeof value !== "object" || value === null) {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
if (typeof value.toJSON === "function") {
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
if (ancestors.has(value)) {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
ancestors.add(value);
|
|
527
|
+
try {
|
|
528
|
+
return Object.values(value).some(
|
|
529
|
+
(item) => containsNonJsonValue(item, ancestors)
|
|
530
|
+
);
|
|
531
|
+
} finally {
|
|
532
|
+
ancestors.delete(value);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
506
535
|
function appendToolResultParts(parts, toolName, outputValue, toolCallId, includeFunctionCallIds = true, supportedFunctionResponseUrls = {}) {
|
|
507
536
|
const functionResponseParts = [];
|
|
508
537
|
const responseTextParts = [];
|
|
@@ -532,12 +561,12 @@ function appendToolResultParts(parts, toolName, outputValue, toolCallId, include
|
|
|
532
561
|
if (functionResponsePart != null) {
|
|
533
562
|
functionResponseParts.push(functionResponsePart);
|
|
534
563
|
} else {
|
|
535
|
-
responseTextParts.push(
|
|
564
|
+
responseTextParts.push(stringifyWithBigIntAndCycles(contentPart));
|
|
536
565
|
}
|
|
537
566
|
break;
|
|
538
567
|
}
|
|
539
568
|
default: {
|
|
540
|
-
responseTextParts.push(
|
|
569
|
+
responseTextParts.push(stringifyWithBigIntAndCycles(contentPart));
|
|
541
570
|
break;
|
|
542
571
|
}
|
|
543
572
|
}
|
|
@@ -586,7 +615,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId, i
|
|
|
586
615
|
break;
|
|
587
616
|
}
|
|
588
617
|
default:
|
|
589
|
-
parts.push({ text:
|
|
618
|
+
parts.push({ text: stringifyWithBigIntAndCycles(contentPart) });
|
|
590
619
|
break;
|
|
591
620
|
}
|
|
592
621
|
}
|
|
@@ -728,7 +757,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
728
757
|
functionCall: {
|
|
729
758
|
...includeFunctionCallIds && part.toolCallId != null ? { id: part.toolCallId } : {},
|
|
730
759
|
name: part.toolName,
|
|
731
|
-
args: part.input
|
|
760
|
+
args: parseToolCallInput(part.input)
|
|
732
761
|
},
|
|
733
762
|
thoughtSignature: effectiveThoughtSignature
|
|
734
763
|
};
|
|
@@ -810,7 +839,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
810
839
|
name: part.toolName,
|
|
811
840
|
response: {
|
|
812
841
|
name: part.toolName,
|
|
813
|
-
content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : output.value
|
|
842
|
+
content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : serializeFunctionResponseContent(output.value)
|
|
814
843
|
}
|
|
815
844
|
}
|
|
816
845
|
});
|
|
@@ -840,6 +869,16 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
840
869
|
contents
|
|
841
870
|
};
|
|
842
871
|
}
|
|
872
|
+
function parseToolCallInput(input) {
|
|
873
|
+
if (typeof input !== "string") {
|
|
874
|
+
return input;
|
|
875
|
+
}
|
|
876
|
+
try {
|
|
877
|
+
return secureJsonParse(input);
|
|
878
|
+
} catch (e) {
|
|
879
|
+
return input;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
843
882
|
|
|
844
883
|
// src/download-tool-result-files.ts
|
|
845
884
|
import {
|
|
@@ -923,15 +962,16 @@ import {
|
|
|
923
962
|
zodSchema
|
|
924
963
|
} from "@ai-sdk/provider-utils";
|
|
925
964
|
import { z as z2 } from "zod/v4";
|
|
965
|
+
var getGoogleErrorSchema = () => z2.object({
|
|
966
|
+
code: z2.number().nullable(),
|
|
967
|
+
message: z2.string(),
|
|
968
|
+
status: z2.string(),
|
|
969
|
+
details: z2.array(z2.unknown()).nullish()
|
|
970
|
+
});
|
|
926
971
|
var googleErrorDataSchema = lazySchema(
|
|
927
972
|
() => zodSchema(
|
|
928
973
|
z2.object({
|
|
929
|
-
error:
|
|
930
|
-
code: z2.number().nullable(),
|
|
931
|
-
message: z2.string(),
|
|
932
|
-
status: z2.string(),
|
|
933
|
-
details: z2.array(z2.unknown()).nullish()
|
|
934
|
-
})
|
|
974
|
+
error: getGoogleErrorSchema()
|
|
935
975
|
})
|
|
936
976
|
)
|
|
937
977
|
);
|
|
@@ -939,6 +979,39 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
939
979
|
errorSchema: googleErrorDataSchema,
|
|
940
980
|
errorToMessage: (data) => data.error.message
|
|
941
981
|
});
|
|
982
|
+
function mapGoogleErrorStatusToHttpStatusCode(status) {
|
|
983
|
+
switch (status) {
|
|
984
|
+
case "CANCELLED":
|
|
985
|
+
return 499;
|
|
986
|
+
case "INVALID_ARGUMENT":
|
|
987
|
+
case "FAILED_PRECONDITION":
|
|
988
|
+
case "OUT_OF_RANGE":
|
|
989
|
+
return 400;
|
|
990
|
+
case "DEADLINE_EXCEEDED":
|
|
991
|
+
return 504;
|
|
992
|
+
case "NOT_FOUND":
|
|
993
|
+
return 404;
|
|
994
|
+
case "ALREADY_EXISTS":
|
|
995
|
+
case "ABORTED":
|
|
996
|
+
return 409;
|
|
997
|
+
case "PERMISSION_DENIED":
|
|
998
|
+
return 403;
|
|
999
|
+
case "UNAUTHENTICATED":
|
|
1000
|
+
return 401;
|
|
1001
|
+
case "RESOURCE_EXHAUSTED":
|
|
1002
|
+
return 429;
|
|
1003
|
+
case "UNIMPLEMENTED":
|
|
1004
|
+
return 501;
|
|
1005
|
+
case "UNKNOWN":
|
|
1006
|
+
case "INTERNAL":
|
|
1007
|
+
case "DATA_LOSS":
|
|
1008
|
+
return 500;
|
|
1009
|
+
case "UNAVAILABLE":
|
|
1010
|
+
return 503;
|
|
1011
|
+
default:
|
|
1012
|
+
return void 0;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
942
1015
|
|
|
943
1016
|
// src/google-generative-ai-options.ts
|
|
944
1017
|
import {
|
|
@@ -2167,10 +2240,11 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2167
2240
|
options.headers,
|
|
2168
2241
|
extraHeaders
|
|
2169
2242
|
);
|
|
2243
|
+
const url = `${this.config.baseURL}/${getModelPath(
|
|
2244
|
+
this.modelId
|
|
2245
|
+
)}:streamGenerateContent?alt=sse`;
|
|
2170
2246
|
const streamGenerateContent = (body) => postJsonToApi({
|
|
2171
|
-
url
|
|
2172
|
-
this.modelId
|
|
2173
|
-
)}:streamGenerateContent?alt=sse`,
|
|
2247
|
+
url,
|
|
2174
2248
|
headers,
|
|
2175
2249
|
body,
|
|
2176
2250
|
failedResponseHandler: googleFailedResponseHandler,
|
|
@@ -2260,7 +2334,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2260
2334
|
controller.enqueue({ type: "stream-start", warnings });
|
|
2261
2335
|
},
|
|
2262
2336
|
transform(chunk, controller) {
|
|
2263
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
2337
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
2264
2338
|
if (options.includeRawChunks) {
|
|
2265
2339
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2266
2340
|
}
|
|
@@ -2269,6 +2343,23 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2269
2343
|
return;
|
|
2270
2344
|
}
|
|
2271
2345
|
const value = chunk.value;
|
|
2346
|
+
if (value.error != null) {
|
|
2347
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2348
|
+
const error = value.error;
|
|
2349
|
+
controller.enqueue({
|
|
2350
|
+
type: "error",
|
|
2351
|
+
error: new APICallError({
|
|
2352
|
+
message: error.message,
|
|
2353
|
+
url,
|
|
2354
|
+
requestBodyValues: args,
|
|
2355
|
+
statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
|
|
2356
|
+
responseHeaders,
|
|
2357
|
+
responseBody: JSON.stringify({ error }),
|
|
2358
|
+
data: error
|
|
2359
|
+
})
|
|
2360
|
+
});
|
|
2361
|
+
return;
|
|
2362
|
+
}
|
|
2272
2363
|
if (!hasEmittedResponseMetadata && value.responseId != null) {
|
|
2273
2364
|
hasEmittedResponseMetadata = true;
|
|
2274
2365
|
controller.enqueue({
|
|
@@ -2290,7 +2381,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2290
2381
|
};
|
|
2291
2382
|
}
|
|
2292
2383
|
}
|
|
2293
|
-
const candidate = (
|
|
2384
|
+
const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
|
|
2294
2385
|
if (candidate != null) {
|
|
2295
2386
|
if (candidate.groundingMetadata != null) {
|
|
2296
2387
|
lastGroundingMetadata = candidate.groundingMetadata;
|
|
@@ -2325,9 +2416,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2325
2416
|
}
|
|
2326
2417
|
}
|
|
2327
2418
|
if (content != null) {
|
|
2328
|
-
const parts = (
|
|
2419
|
+
const parts = (_c = content.parts) != null ? _c : [];
|
|
2329
2420
|
for (const part of parts) {
|
|
2330
|
-
if ("executableCode" in part && ((
|
|
2421
|
+
if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
|
|
2331
2422
|
const toolCallId = generateId2();
|
|
2332
2423
|
lastCodeExecutionToolCallId = toolCallId;
|
|
2333
2424
|
controller.enqueue({
|
|
@@ -2346,7 +2437,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2346
2437
|
toolName: "code_execution",
|
|
2347
2438
|
result: {
|
|
2348
2439
|
outcome: part.codeExecutionResult.outcome,
|
|
2349
|
-
output: (
|
|
2440
|
+
output: (_e = part.codeExecutionResult.output) != null ? _e : ""
|
|
2350
2441
|
}
|
|
2351
2442
|
});
|
|
2352
2443
|
}
|
|
@@ -2440,7 +2531,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2440
2531
|
providerMetadata: fileMeta
|
|
2441
2532
|
});
|
|
2442
2533
|
} else if ("toolCall" in part && part.toolCall) {
|
|
2443
|
-
const toolCallId = (
|
|
2534
|
+
const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId2();
|
|
2444
2535
|
lastServerToolCallId = toolCallId;
|
|
2445
2536
|
const serverMeta = {
|
|
2446
2537
|
[providerOptionsName]: {
|
|
@@ -2453,13 +2544,13 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2453
2544
|
type: "tool-call",
|
|
2454
2545
|
toolCallId,
|
|
2455
2546
|
toolName: `server:${part.toolCall.toolType}`,
|
|
2456
|
-
input: JSON.stringify((
|
|
2547
|
+
input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
|
|
2457
2548
|
providerExecuted: true,
|
|
2458
2549
|
dynamic: true,
|
|
2459
2550
|
providerMetadata: serverMeta
|
|
2460
2551
|
});
|
|
2461
2552
|
} else if ("toolResponse" in part && part.toolResponse) {
|
|
2462
|
-
const responseToolCallId = (
|
|
2553
|
+
const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId2();
|
|
2463
2554
|
const serverMeta = {
|
|
2464
2555
|
[providerOptionsName]: {
|
|
2465
2556
|
...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
|
|
@@ -2471,7 +2562,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2471
2562
|
type: "tool-result",
|
|
2472
2563
|
toolCallId: responseToolCallId,
|
|
2473
2564
|
toolName: `server:${part.toolResponse.toolType}`,
|
|
2474
|
-
result: (
|
|
2565
|
+
result: (_i = part.toolResponse.response) != null ? _i : {},
|
|
2475
2566
|
providerMetadata: serverMeta
|
|
2476
2567
|
});
|
|
2477
2568
|
lastServerToolCallId = void 0;
|
|
@@ -2490,7 +2581,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2490
2581
|
const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
|
|
2491
2582
|
if (isStreamingChunk) {
|
|
2492
2583
|
if (part.functionCall.name != null) {
|
|
2493
|
-
const toolCallId = (
|
|
2584
|
+
const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
|
|
2494
2585
|
const accumulator = new GoogleJSONAccumulator();
|
|
2495
2586
|
activeStreamingToolCalls.push({
|
|
2496
2587
|
toolCallId,
|
|
@@ -2538,9 +2629,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2538
2629
|
} else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
|
|
2539
2630
|
finishActiveStreamingToolCall(controller);
|
|
2540
2631
|
} else if (isCompleteCall) {
|
|
2541
|
-
const toolCallId = (
|
|
2632
|
+
const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId2();
|
|
2542
2633
|
const toolName = part.functionCall.name;
|
|
2543
|
-
const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((
|
|
2634
|
+
const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
|
|
2544
2635
|
controller.enqueue({
|
|
2545
2636
|
type: "tool-input-start",
|
|
2546
2637
|
id: toolCallId,
|
|
@@ -2567,7 +2658,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2567
2658
|
});
|
|
2568
2659
|
hasToolCalls = true;
|
|
2569
2660
|
} else if (isNoArgsCompleteCall) {
|
|
2570
|
-
const toolCallId = (
|
|
2661
|
+
const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId2();
|
|
2571
2662
|
const toolName = part.functionCall.name;
|
|
2572
2663
|
controller.enqueue({
|
|
2573
2664
|
type: "tool-input-start",
|
|
@@ -2938,6 +3029,7 @@ var responseSchema = lazySchema3(
|
|
|
2938
3029
|
var chunkSchema = lazySchema3(
|
|
2939
3030
|
() => zodSchema3(
|
|
2940
3031
|
z4.object({
|
|
3032
|
+
error: getGoogleErrorSchema().nullish(),
|
|
2941
3033
|
responseId: z4.string().nullish(),
|
|
2942
3034
|
candidates: z4.array(
|
|
2943
3035
|
z4.object({
|
|
@@ -5140,6 +5232,9 @@ function prepareGoogleInteractionsTools({
|
|
|
5140
5232
|
}) {
|
|
5141
5233
|
var _a, _b, _c, _d;
|
|
5142
5234
|
const toolWarnings = [];
|
|
5235
|
+
if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
|
|
5236
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
5237
|
+
}
|
|
5143
5238
|
const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
5144
5239
|
if (normalized == null) {
|
|
5145
5240
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
@@ -5261,9 +5356,6 @@ function prepareGoogleInteractionsTools({
|
|
|
5261
5356
|
case "required":
|
|
5262
5357
|
mappedToolChoice = "any";
|
|
5263
5358
|
break;
|
|
5264
|
-
case "none":
|
|
5265
|
-
mappedToolChoice = "none";
|
|
5266
|
-
break;
|
|
5267
5359
|
case "tool":
|
|
5268
5360
|
mappedToolChoice = {
|
|
5269
5361
|
allowed_tools: {
|