@ai-sdk/google 3.0.140 → 3.0.142
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 +18 -0
- package/dist/index.js +130 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +126 -31
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +122 -30
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +125 -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/google-prepare-tools.ts +7 -0
- package/src/interactions/prepare-google-interactions-tools.ts +7 -4
package/dist/internal/index.js
CHANGED
|
@@ -29,6 +29,7 @@ __export(index_exports, {
|
|
|
29
29
|
module.exports = __toCommonJS(index_exports);
|
|
30
30
|
|
|
31
31
|
// src/google-generative-ai-language-model.ts
|
|
32
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
32
33
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
33
34
|
var import_v44 = require("zod/v4");
|
|
34
35
|
|
|
@@ -515,6 +516,31 @@ function convertUrlToolResultPart(url, mediaType, supportedFunctionResponseUrls)
|
|
|
515
516
|
}
|
|
516
517
|
} : void 0;
|
|
517
518
|
}
|
|
519
|
+
function serializeFunctionResponseContent(value) {
|
|
520
|
+
return containsNonJsonValue(value) ? (0, import_provider_utils.secureJsonParse)((0, import_provider_utils.stringifyWithBigIntAndCycles)(value)) : value;
|
|
521
|
+
}
|
|
522
|
+
function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
523
|
+
if (typeof value === "bigint") {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
if (typeof value !== "object" || value === null) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
if (typeof value.toJSON === "function") {
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
if (ancestors.has(value)) {
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
ancestors.add(value);
|
|
536
|
+
try {
|
|
537
|
+
return Object.values(value).some(
|
|
538
|
+
(item) => containsNonJsonValue(item, ancestors)
|
|
539
|
+
);
|
|
540
|
+
} finally {
|
|
541
|
+
ancestors.delete(value);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
518
544
|
function appendToolResultParts(parts, toolName, outputValue, toolCallId, includeFunctionCallIds = true, supportedFunctionResponseUrls = {}) {
|
|
519
545
|
const functionResponseParts = [];
|
|
520
546
|
const responseTextParts = [];
|
|
@@ -544,12 +570,12 @@ function appendToolResultParts(parts, toolName, outputValue, toolCallId, include
|
|
|
544
570
|
if (functionResponsePart != null) {
|
|
545
571
|
functionResponseParts.push(functionResponsePart);
|
|
546
572
|
} else {
|
|
547
|
-
responseTextParts.push(
|
|
573
|
+
responseTextParts.push((0, import_provider_utils.stringifyWithBigIntAndCycles)(contentPart));
|
|
548
574
|
}
|
|
549
575
|
break;
|
|
550
576
|
}
|
|
551
577
|
default: {
|
|
552
|
-
responseTextParts.push(
|
|
578
|
+
responseTextParts.push((0, import_provider_utils.stringifyWithBigIntAndCycles)(contentPart));
|
|
553
579
|
break;
|
|
554
580
|
}
|
|
555
581
|
}
|
|
@@ -598,7 +624,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId, i
|
|
|
598
624
|
break;
|
|
599
625
|
}
|
|
600
626
|
default:
|
|
601
|
-
parts.push({ text:
|
|
627
|
+
parts.push({ text: (0, import_provider_utils.stringifyWithBigIntAndCycles)(contentPart) });
|
|
602
628
|
break;
|
|
603
629
|
}
|
|
604
630
|
}
|
|
@@ -740,7 +766,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
740
766
|
functionCall: {
|
|
741
767
|
...includeFunctionCallIds && part.toolCallId != null ? { id: part.toolCallId } : {},
|
|
742
768
|
name: part.toolName,
|
|
743
|
-
args: part.input
|
|
769
|
+
args: parseToolCallInput(part.input)
|
|
744
770
|
},
|
|
745
771
|
thoughtSignature: effectiveThoughtSignature
|
|
746
772
|
};
|
|
@@ -822,7 +848,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
822
848
|
name: part.toolName,
|
|
823
849
|
response: {
|
|
824
850
|
name: part.toolName,
|
|
825
|
-
content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : output.value
|
|
851
|
+
content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : serializeFunctionResponseContent(output.value)
|
|
826
852
|
}
|
|
827
853
|
}
|
|
828
854
|
});
|
|
@@ -852,6 +878,16 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
852
878
|
contents
|
|
853
879
|
};
|
|
854
880
|
}
|
|
881
|
+
function parseToolCallInput(input) {
|
|
882
|
+
if (typeof input !== "string") {
|
|
883
|
+
return input;
|
|
884
|
+
}
|
|
885
|
+
try {
|
|
886
|
+
return (0, import_provider_utils.secureJsonParse)(input);
|
|
887
|
+
} catch (e) {
|
|
888
|
+
return input;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
855
891
|
|
|
856
892
|
// src/download-tool-result-files.ts
|
|
857
893
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
@@ -927,15 +963,16 @@ function getModelPath(modelId) {
|
|
|
927
963
|
// src/google-error.ts
|
|
928
964
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
929
965
|
var import_v42 = require("zod/v4");
|
|
966
|
+
var getGoogleErrorSchema = () => import_v42.z.object({
|
|
967
|
+
code: import_v42.z.number().nullable(),
|
|
968
|
+
message: import_v42.z.string(),
|
|
969
|
+
status: import_v42.z.string(),
|
|
970
|
+
details: import_v42.z.array(import_v42.z.unknown()).nullish()
|
|
971
|
+
});
|
|
930
972
|
var googleErrorDataSchema = (0, import_provider_utils3.lazySchema)(
|
|
931
973
|
() => (0, import_provider_utils3.zodSchema)(
|
|
932
974
|
import_v42.z.object({
|
|
933
|
-
error:
|
|
934
|
-
code: import_v42.z.number().nullable(),
|
|
935
|
-
message: import_v42.z.string(),
|
|
936
|
-
status: import_v42.z.string(),
|
|
937
|
-
details: import_v42.z.array(import_v42.z.unknown()).nullish()
|
|
938
|
-
})
|
|
975
|
+
error: getGoogleErrorSchema()
|
|
939
976
|
})
|
|
940
977
|
)
|
|
941
978
|
);
|
|
@@ -943,6 +980,39 @@ var googleFailedResponseHandler = (0, import_provider_utils3.createJsonErrorResp
|
|
|
943
980
|
errorSchema: googleErrorDataSchema,
|
|
944
981
|
errorToMessage: (data) => data.error.message
|
|
945
982
|
});
|
|
983
|
+
function mapGoogleErrorStatusToHttpStatusCode(status) {
|
|
984
|
+
switch (status) {
|
|
985
|
+
case "CANCELLED":
|
|
986
|
+
return 499;
|
|
987
|
+
case "INVALID_ARGUMENT":
|
|
988
|
+
case "FAILED_PRECONDITION":
|
|
989
|
+
case "OUT_OF_RANGE":
|
|
990
|
+
return 400;
|
|
991
|
+
case "DEADLINE_EXCEEDED":
|
|
992
|
+
return 504;
|
|
993
|
+
case "NOT_FOUND":
|
|
994
|
+
return 404;
|
|
995
|
+
case "ALREADY_EXISTS":
|
|
996
|
+
case "ABORTED":
|
|
997
|
+
return 409;
|
|
998
|
+
case "PERMISSION_DENIED":
|
|
999
|
+
return 403;
|
|
1000
|
+
case "UNAUTHENTICATED":
|
|
1001
|
+
return 401;
|
|
1002
|
+
case "RESOURCE_EXHAUSTED":
|
|
1003
|
+
return 429;
|
|
1004
|
+
case "UNIMPLEMENTED":
|
|
1005
|
+
return 501;
|
|
1006
|
+
case "UNKNOWN":
|
|
1007
|
+
case "INTERNAL":
|
|
1008
|
+
case "DATA_LOSS":
|
|
1009
|
+
return 500;
|
|
1010
|
+
case "UNAVAILABLE":
|
|
1011
|
+
return 503;
|
|
1012
|
+
default:
|
|
1013
|
+
return void 0;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
946
1016
|
|
|
947
1017
|
// src/google-generative-ai-options.ts
|
|
948
1018
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
@@ -1171,6 +1241,9 @@ function prepareTools({
|
|
|
1171
1241
|
modelId,
|
|
1172
1242
|
isVertexProvider = false
|
|
1173
1243
|
}) {
|
|
1244
|
+
if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
|
|
1245
|
+
tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
|
|
1246
|
+
}
|
|
1174
1247
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1175
1248
|
const toolWarnings = [];
|
|
1176
1249
|
const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
|
|
@@ -2166,10 +2239,11 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2166
2239
|
options.headers,
|
|
2167
2240
|
extraHeaders
|
|
2168
2241
|
);
|
|
2242
|
+
const url = `${this.config.baseURL}/${getModelPath(
|
|
2243
|
+
this.modelId
|
|
2244
|
+
)}:streamGenerateContent?alt=sse`;
|
|
2169
2245
|
const streamGenerateContent = (body) => (0, import_provider_utils5.postJsonToApi)({
|
|
2170
|
-
url
|
|
2171
|
-
this.modelId
|
|
2172
|
-
)}:streamGenerateContent?alt=sse`,
|
|
2246
|
+
url,
|
|
2173
2247
|
headers,
|
|
2174
2248
|
body,
|
|
2175
2249
|
failedResponseHandler: googleFailedResponseHandler,
|
|
@@ -2259,7 +2333,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2259
2333
|
controller.enqueue({ type: "stream-start", warnings });
|
|
2260
2334
|
},
|
|
2261
2335
|
transform(chunk, controller) {
|
|
2262
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
2336
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
2263
2337
|
if (options.includeRawChunks) {
|
|
2264
2338
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2265
2339
|
}
|
|
@@ -2268,6 +2342,23 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2268
2342
|
return;
|
|
2269
2343
|
}
|
|
2270
2344
|
const value = chunk.value;
|
|
2345
|
+
if (value.error != null) {
|
|
2346
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2347
|
+
const error = value.error;
|
|
2348
|
+
controller.enqueue({
|
|
2349
|
+
type: "error",
|
|
2350
|
+
error: new import_provider4.APICallError({
|
|
2351
|
+
message: error.message,
|
|
2352
|
+
url,
|
|
2353
|
+
requestBodyValues: args,
|
|
2354
|
+
statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
|
|
2355
|
+
responseHeaders,
|
|
2356
|
+
responseBody: JSON.stringify({ error }),
|
|
2357
|
+
data: error
|
|
2358
|
+
})
|
|
2359
|
+
});
|
|
2360
|
+
return;
|
|
2361
|
+
}
|
|
2271
2362
|
if (!hasEmittedResponseMetadata && value.responseId != null) {
|
|
2272
2363
|
hasEmittedResponseMetadata = true;
|
|
2273
2364
|
controller.enqueue({
|
|
@@ -2289,7 +2380,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2289
2380
|
};
|
|
2290
2381
|
}
|
|
2291
2382
|
}
|
|
2292
|
-
const candidate = (
|
|
2383
|
+
const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
|
|
2293
2384
|
if (candidate != null) {
|
|
2294
2385
|
if (candidate.groundingMetadata != null) {
|
|
2295
2386
|
lastGroundingMetadata = candidate.groundingMetadata;
|
|
@@ -2324,9 +2415,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2324
2415
|
}
|
|
2325
2416
|
}
|
|
2326
2417
|
if (content != null) {
|
|
2327
|
-
const parts = (
|
|
2418
|
+
const parts = (_c = content.parts) != null ? _c : [];
|
|
2328
2419
|
for (const part of parts) {
|
|
2329
|
-
if ("executableCode" in part && ((
|
|
2420
|
+
if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
|
|
2330
2421
|
const toolCallId = generateId2();
|
|
2331
2422
|
lastCodeExecutionToolCallId = toolCallId;
|
|
2332
2423
|
controller.enqueue({
|
|
@@ -2345,7 +2436,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2345
2436
|
toolName: "code_execution",
|
|
2346
2437
|
result: {
|
|
2347
2438
|
outcome: part.codeExecutionResult.outcome,
|
|
2348
|
-
output: (
|
|
2439
|
+
output: (_e = part.codeExecutionResult.output) != null ? _e : ""
|
|
2349
2440
|
}
|
|
2350
2441
|
});
|
|
2351
2442
|
}
|
|
@@ -2439,7 +2530,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2439
2530
|
providerMetadata: fileMeta
|
|
2440
2531
|
});
|
|
2441
2532
|
} else if ("toolCall" in part && part.toolCall) {
|
|
2442
|
-
const toolCallId = (
|
|
2533
|
+
const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId2();
|
|
2443
2534
|
lastServerToolCallId = toolCallId;
|
|
2444
2535
|
const serverMeta = {
|
|
2445
2536
|
[providerOptionsName]: {
|
|
@@ -2452,13 +2543,13 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2452
2543
|
type: "tool-call",
|
|
2453
2544
|
toolCallId,
|
|
2454
2545
|
toolName: `server:${part.toolCall.toolType}`,
|
|
2455
|
-
input: JSON.stringify((
|
|
2546
|
+
input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
|
|
2456
2547
|
providerExecuted: true,
|
|
2457
2548
|
dynamic: true,
|
|
2458
2549
|
providerMetadata: serverMeta
|
|
2459
2550
|
});
|
|
2460
2551
|
} else if ("toolResponse" in part && part.toolResponse) {
|
|
2461
|
-
const responseToolCallId = (
|
|
2552
|
+
const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId2();
|
|
2462
2553
|
const serverMeta = {
|
|
2463
2554
|
[providerOptionsName]: {
|
|
2464
2555
|
...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
|
|
@@ -2470,7 +2561,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2470
2561
|
type: "tool-result",
|
|
2471
2562
|
toolCallId: responseToolCallId,
|
|
2472
2563
|
toolName: `server:${part.toolResponse.toolType}`,
|
|
2473
|
-
result: (
|
|
2564
|
+
result: (_i = part.toolResponse.response) != null ? _i : {},
|
|
2474
2565
|
providerMetadata: serverMeta
|
|
2475
2566
|
});
|
|
2476
2567
|
lastServerToolCallId = void 0;
|
|
@@ -2489,7 +2580,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2489
2580
|
const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
|
|
2490
2581
|
if (isStreamingChunk) {
|
|
2491
2582
|
if (part.functionCall.name != null) {
|
|
2492
|
-
const toolCallId = (
|
|
2583
|
+
const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
|
|
2493
2584
|
const accumulator = new GoogleJSONAccumulator();
|
|
2494
2585
|
activeStreamingToolCalls.push({
|
|
2495
2586
|
toolCallId,
|
|
@@ -2537,9 +2628,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2537
2628
|
} else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
|
|
2538
2629
|
finishActiveStreamingToolCall(controller);
|
|
2539
2630
|
} else if (isCompleteCall) {
|
|
2540
|
-
const toolCallId = (
|
|
2631
|
+
const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId2();
|
|
2541
2632
|
const toolName = part.functionCall.name;
|
|
2542
|
-
const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((
|
|
2633
|
+
const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
|
|
2543
2634
|
controller.enqueue({
|
|
2544
2635
|
type: "tool-input-start",
|
|
2545
2636
|
id: toolCallId,
|
|
@@ -2566,7 +2657,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2566
2657
|
});
|
|
2567
2658
|
hasToolCalls = true;
|
|
2568
2659
|
} else if (isNoArgsCompleteCall) {
|
|
2569
|
-
const toolCallId = (
|
|
2660
|
+
const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId2();
|
|
2570
2661
|
const toolName = part.functionCall.name;
|
|
2571
2662
|
controller.enqueue({
|
|
2572
2663
|
type: "tool-input-start",
|
|
@@ -2937,6 +3028,7 @@ var responseSchema = (0, import_provider_utils5.lazySchema)(
|
|
|
2937
3028
|
var chunkSchema = (0, import_provider_utils5.lazySchema)(
|
|
2938
3029
|
() => (0, import_provider_utils5.zodSchema)(
|
|
2939
3030
|
import_v44.z.object({
|
|
3031
|
+
error: getGoogleErrorSchema().nullish(),
|
|
2940
3032
|
responseId: import_v44.z.string().nullish(),
|
|
2941
3033
|
candidates: import_v44.z.array(
|
|
2942
3034
|
import_v44.z.object({
|
|
@@ -5096,6 +5188,9 @@ function prepareGoogleInteractionsTools({
|
|
|
5096
5188
|
}) {
|
|
5097
5189
|
var _a, _b, _c, _d;
|
|
5098
5190
|
const toolWarnings = [];
|
|
5191
|
+
if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
|
|
5192
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
5193
|
+
}
|
|
5099
5194
|
const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
5100
5195
|
if (normalized == null) {
|
|
5101
5196
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
@@ -5217,9 +5312,6 @@ function prepareGoogleInteractionsTools({
|
|
|
5217
5312
|
case "required":
|
|
5218
5313
|
mappedToolChoice = "any";
|
|
5219
5314
|
break;
|
|
5220
|
-
case "none":
|
|
5221
|
-
mappedToolChoice = "none";
|
|
5222
|
-
break;
|
|
5223
5315
|
case "tool":
|
|
5224
5316
|
mappedToolChoice = {
|
|
5225
5317
|
allowed_tools: {
|