@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.
@@ -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(JSON.stringify(contentPart));
564
+ responseTextParts.push(stringifyWithBigIntAndCycles(contentPart));
536
565
  }
537
566
  break;
538
567
  }
539
568
  default: {
540
- responseTextParts.push(JSON.stringify(contentPart));
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: JSON.stringify(contentPart) });
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: z2.object({
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 {
@@ -1172,6 +1245,9 @@ function prepareTools({
1172
1245
  modelId,
1173
1246
  isVertexProvider = false
1174
1247
  }) {
1248
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
1249
+ tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
1250
+ }
1175
1251
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1176
1252
  const toolWarnings = [];
1177
1253
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -2167,10 +2243,11 @@ var GoogleGenerativeAILanguageModel = class {
2167
2243
  options.headers,
2168
2244
  extraHeaders
2169
2245
  );
2246
+ const url = `${this.config.baseURL}/${getModelPath(
2247
+ this.modelId
2248
+ )}:streamGenerateContent?alt=sse`;
2170
2249
  const streamGenerateContent = (body) => postJsonToApi({
2171
- url: `${this.config.baseURL}/${getModelPath(
2172
- this.modelId
2173
- )}:streamGenerateContent?alt=sse`,
2250
+ url,
2174
2251
  headers,
2175
2252
  body,
2176
2253
  failedResponseHandler: googleFailedResponseHandler,
@@ -2260,7 +2337,7 @@ var GoogleGenerativeAILanguageModel = class {
2260
2337
  controller.enqueue({ type: "stream-start", warnings });
2261
2338
  },
2262
2339
  transform(chunk, controller) {
2263
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2340
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2264
2341
  if (options.includeRawChunks) {
2265
2342
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2266
2343
  }
@@ -2269,6 +2346,23 @@ var GoogleGenerativeAILanguageModel = class {
2269
2346
  return;
2270
2347
  }
2271
2348
  const value = chunk.value;
2349
+ if (value.error != null) {
2350
+ finishReason = { unified: "error", raw: void 0 };
2351
+ const error = value.error;
2352
+ controller.enqueue({
2353
+ type: "error",
2354
+ error: new APICallError({
2355
+ message: error.message,
2356
+ url,
2357
+ requestBodyValues: args,
2358
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
2359
+ responseHeaders,
2360
+ responseBody: JSON.stringify({ error }),
2361
+ data: error
2362
+ })
2363
+ });
2364
+ return;
2365
+ }
2272
2366
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2273
2367
  hasEmittedResponseMetadata = true;
2274
2368
  controller.enqueue({
@@ -2290,7 +2384,7 @@ var GoogleGenerativeAILanguageModel = class {
2290
2384
  };
2291
2385
  }
2292
2386
  }
2293
- const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2387
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2294
2388
  if (candidate != null) {
2295
2389
  if (candidate.groundingMetadata != null) {
2296
2390
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2325,9 +2419,9 @@ var GoogleGenerativeAILanguageModel = class {
2325
2419
  }
2326
2420
  }
2327
2421
  if (content != null) {
2328
- const parts = (_b = content.parts) != null ? _b : [];
2422
+ const parts = (_c = content.parts) != null ? _c : [];
2329
2423
  for (const part of parts) {
2330
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2424
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2331
2425
  const toolCallId = generateId2();
2332
2426
  lastCodeExecutionToolCallId = toolCallId;
2333
2427
  controller.enqueue({
@@ -2346,7 +2440,7 @@ var GoogleGenerativeAILanguageModel = class {
2346
2440
  toolName: "code_execution",
2347
2441
  result: {
2348
2442
  outcome: part.codeExecutionResult.outcome,
2349
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2443
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2350
2444
  }
2351
2445
  });
2352
2446
  }
@@ -2440,7 +2534,7 @@ var GoogleGenerativeAILanguageModel = class {
2440
2534
  providerMetadata: fileMeta
2441
2535
  });
2442
2536
  } else if ("toolCall" in part && part.toolCall) {
2443
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
2537
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId2();
2444
2538
  lastServerToolCallId = toolCallId;
2445
2539
  const serverMeta = {
2446
2540
  [providerOptionsName]: {
@@ -2453,13 +2547,13 @@ var GoogleGenerativeAILanguageModel = class {
2453
2547
  type: "tool-call",
2454
2548
  toolCallId,
2455
2549
  toolName: `server:${part.toolCall.toolType}`,
2456
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2550
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2457
2551
  providerExecuted: true,
2458
2552
  dynamic: true,
2459
2553
  providerMetadata: serverMeta
2460
2554
  });
2461
2555
  } else if ("toolResponse" in part && part.toolResponse) {
2462
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
2556
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId2();
2463
2557
  const serverMeta = {
2464
2558
  [providerOptionsName]: {
2465
2559
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2471,7 +2565,7 @@ var GoogleGenerativeAILanguageModel = class {
2471
2565
  type: "tool-result",
2472
2566
  toolCallId: responseToolCallId,
2473
2567
  toolName: `server:${part.toolResponse.toolType}`,
2474
- result: (_h = part.toolResponse.response) != null ? _h : {},
2568
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2475
2569
  providerMetadata: serverMeta
2476
2570
  });
2477
2571
  lastServerToolCallId = void 0;
@@ -2490,7 +2584,7 @@ var GoogleGenerativeAILanguageModel = class {
2490
2584
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2491
2585
  if (isStreamingChunk) {
2492
2586
  if (part.functionCall.name != null) {
2493
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId2();
2587
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
2494
2588
  const accumulator = new GoogleJSONAccumulator();
2495
2589
  activeStreamingToolCalls.push({
2496
2590
  toolCallId,
@@ -2538,9 +2632,9 @@ var GoogleGenerativeAILanguageModel = class {
2538
2632
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2539
2633
  finishActiveStreamingToolCall(controller);
2540
2634
  } else if (isCompleteCall) {
2541
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
2635
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId2();
2542
2636
  const toolName = part.functionCall.name;
2543
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2637
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2544
2638
  controller.enqueue({
2545
2639
  type: "tool-input-start",
2546
2640
  id: toolCallId,
@@ -2567,7 +2661,7 @@ var GoogleGenerativeAILanguageModel = class {
2567
2661
  });
2568
2662
  hasToolCalls = true;
2569
2663
  } else if (isNoArgsCompleteCall) {
2570
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId2();
2664
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId2();
2571
2665
  const toolName = part.functionCall.name;
2572
2666
  controller.enqueue({
2573
2667
  type: "tool-input-start",
@@ -2938,6 +3032,7 @@ var responseSchema = lazySchema3(
2938
3032
  var chunkSchema = lazySchema3(
2939
3033
  () => zodSchema3(
2940
3034
  z4.object({
3035
+ error: getGoogleErrorSchema().nullish(),
2941
3036
  responseId: z4.string().nullish(),
2942
3037
  candidates: z4.array(
2943
3038
  z4.object({
@@ -5140,6 +5235,9 @@ function prepareGoogleInteractionsTools({
5140
5235
  }) {
5141
5236
  var _a, _b, _c, _d;
5142
5237
  const toolWarnings = [];
5238
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
5239
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
5240
+ }
5143
5241
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
5144
5242
  if (normalized == null) {
5145
5243
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -5261,9 +5359,6 @@ function prepareGoogleInteractionsTools({
5261
5359
  case "required":
5262
5360
  mappedToolChoice = "any";
5263
5361
  break;
5264
- case "none":
5265
- mappedToolChoice = "none";
5266
- break;
5267
5362
  case "tool":
5268
5363
  mappedToolChoice = {
5269
5364
  allowed_tools: {