@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.
@@ -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(JSON.stringify(contentPart));
573
+ responseTextParts.push((0, import_provider_utils.stringifyWithBigIntAndCycles)(contentPart));
548
574
  }
549
575
  break;
550
576
  }
551
577
  default: {
552
- responseTextParts.push(JSON.stringify(contentPart));
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: JSON.stringify(contentPart) });
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: import_v42.z.object({
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");
@@ -2166,10 +2236,11 @@ var GoogleGenerativeAILanguageModel = class {
2166
2236
  options.headers,
2167
2237
  extraHeaders
2168
2238
  );
2239
+ const url = `${this.config.baseURL}/${getModelPath(
2240
+ this.modelId
2241
+ )}:streamGenerateContent?alt=sse`;
2169
2242
  const streamGenerateContent = (body) => (0, import_provider_utils5.postJsonToApi)({
2170
- url: `${this.config.baseURL}/${getModelPath(
2171
- this.modelId
2172
- )}:streamGenerateContent?alt=sse`,
2243
+ url,
2173
2244
  headers,
2174
2245
  body,
2175
2246
  failedResponseHandler: googleFailedResponseHandler,
@@ -2259,7 +2330,7 @@ var GoogleGenerativeAILanguageModel = class {
2259
2330
  controller.enqueue({ type: "stream-start", warnings });
2260
2331
  },
2261
2332
  transform(chunk, controller) {
2262
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2333
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2263
2334
  if (options.includeRawChunks) {
2264
2335
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2265
2336
  }
@@ -2268,6 +2339,23 @@ var GoogleGenerativeAILanguageModel = class {
2268
2339
  return;
2269
2340
  }
2270
2341
  const value = chunk.value;
2342
+ if (value.error != null) {
2343
+ finishReason = { unified: "error", raw: void 0 };
2344
+ const error = value.error;
2345
+ controller.enqueue({
2346
+ type: "error",
2347
+ error: new import_provider4.APICallError({
2348
+ message: error.message,
2349
+ url,
2350
+ requestBodyValues: args,
2351
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
2352
+ responseHeaders,
2353
+ responseBody: JSON.stringify({ error }),
2354
+ data: error
2355
+ })
2356
+ });
2357
+ return;
2358
+ }
2271
2359
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2272
2360
  hasEmittedResponseMetadata = true;
2273
2361
  controller.enqueue({
@@ -2289,7 +2377,7 @@ var GoogleGenerativeAILanguageModel = class {
2289
2377
  };
2290
2378
  }
2291
2379
  }
2292
- const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2380
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2293
2381
  if (candidate != null) {
2294
2382
  if (candidate.groundingMetadata != null) {
2295
2383
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2324,9 +2412,9 @@ var GoogleGenerativeAILanguageModel = class {
2324
2412
  }
2325
2413
  }
2326
2414
  if (content != null) {
2327
- const parts = (_b = content.parts) != null ? _b : [];
2415
+ const parts = (_c = content.parts) != null ? _c : [];
2328
2416
  for (const part of parts) {
2329
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2417
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2330
2418
  const toolCallId = generateId2();
2331
2419
  lastCodeExecutionToolCallId = toolCallId;
2332
2420
  controller.enqueue({
@@ -2345,7 +2433,7 @@ var GoogleGenerativeAILanguageModel = class {
2345
2433
  toolName: "code_execution",
2346
2434
  result: {
2347
2435
  outcome: part.codeExecutionResult.outcome,
2348
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2436
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2349
2437
  }
2350
2438
  });
2351
2439
  }
@@ -2439,7 +2527,7 @@ var GoogleGenerativeAILanguageModel = class {
2439
2527
  providerMetadata: fileMeta
2440
2528
  });
2441
2529
  } else if ("toolCall" in part && part.toolCall) {
2442
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
2530
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId2();
2443
2531
  lastServerToolCallId = toolCallId;
2444
2532
  const serverMeta = {
2445
2533
  [providerOptionsName]: {
@@ -2452,13 +2540,13 @@ var GoogleGenerativeAILanguageModel = class {
2452
2540
  type: "tool-call",
2453
2541
  toolCallId,
2454
2542
  toolName: `server:${part.toolCall.toolType}`,
2455
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2543
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2456
2544
  providerExecuted: true,
2457
2545
  dynamic: true,
2458
2546
  providerMetadata: serverMeta
2459
2547
  });
2460
2548
  } else if ("toolResponse" in part && part.toolResponse) {
2461
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
2549
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId2();
2462
2550
  const serverMeta = {
2463
2551
  [providerOptionsName]: {
2464
2552
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2470,7 +2558,7 @@ var GoogleGenerativeAILanguageModel = class {
2470
2558
  type: "tool-result",
2471
2559
  toolCallId: responseToolCallId,
2472
2560
  toolName: `server:${part.toolResponse.toolType}`,
2473
- result: (_h = part.toolResponse.response) != null ? _h : {},
2561
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2474
2562
  providerMetadata: serverMeta
2475
2563
  });
2476
2564
  lastServerToolCallId = void 0;
@@ -2489,7 +2577,7 @@ var GoogleGenerativeAILanguageModel = class {
2489
2577
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2490
2578
  if (isStreamingChunk) {
2491
2579
  if (part.functionCall.name != null) {
2492
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId2();
2580
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
2493
2581
  const accumulator = new GoogleJSONAccumulator();
2494
2582
  activeStreamingToolCalls.push({
2495
2583
  toolCallId,
@@ -2537,9 +2625,9 @@ var GoogleGenerativeAILanguageModel = class {
2537
2625
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2538
2626
  finishActiveStreamingToolCall(controller);
2539
2627
  } else if (isCompleteCall) {
2540
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
2628
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId2();
2541
2629
  const toolName = part.functionCall.name;
2542
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2630
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2543
2631
  controller.enqueue({
2544
2632
  type: "tool-input-start",
2545
2633
  id: toolCallId,
@@ -2566,7 +2654,7 @@ var GoogleGenerativeAILanguageModel = class {
2566
2654
  });
2567
2655
  hasToolCalls = true;
2568
2656
  } else if (isNoArgsCompleteCall) {
2569
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId2();
2657
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId2();
2570
2658
  const toolName = part.functionCall.name;
2571
2659
  controller.enqueue({
2572
2660
  type: "tool-input-start",
@@ -2937,6 +3025,7 @@ var responseSchema = (0, import_provider_utils5.lazySchema)(
2937
3025
  var chunkSchema = (0, import_provider_utils5.lazySchema)(
2938
3026
  () => (0, import_provider_utils5.zodSchema)(
2939
3027
  import_v44.z.object({
3028
+ error: getGoogleErrorSchema().nullish(),
2940
3029
  responseId: import_v44.z.string().nullish(),
2941
3030
  candidates: import_v44.z.array(
2942
3031
  import_v44.z.object({
@@ -5096,6 +5185,9 @@ function prepareGoogleInteractionsTools({
5096
5185
  }) {
5097
5186
  var _a, _b, _c, _d;
5098
5187
  const toolWarnings = [];
5188
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
5189
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
5190
+ }
5099
5191
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
5100
5192
  if (normalized == null) {
5101
5193
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -5217,9 +5309,6 @@ function prepareGoogleInteractionsTools({
5217
5309
  case "required":
5218
5310
  mappedToolChoice = "any";
5219
5311
  break;
5220
- case "none":
5221
- mappedToolChoice = "none";
5222
- break;
5223
5312
  case "tool":
5224
5313
  mappedToolChoice = {
5225
5314
  allowed_tools: {