@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/dist/index.mjs CHANGED
@@ -21,15 +21,16 @@ import {
21
21
  zodSchema
22
22
  } from "@ai-sdk/provider-utils";
23
23
  import { z } from "zod/v4";
24
+ var getGoogleErrorSchema = () => z.object({
25
+ code: z.number().nullable(),
26
+ message: z.string(),
27
+ status: z.string(),
28
+ details: z.array(z.unknown()).nullish()
29
+ });
24
30
  var googleErrorDataSchema = lazySchema(
25
31
  () => zodSchema(
26
32
  z.object({
27
- error: z.object({
28
- code: z.number().nullable(),
29
- message: z.string(),
30
- status: z.string(),
31
- details: z.array(z.unknown()).nullish()
32
- })
33
+ error: getGoogleErrorSchema()
33
34
  })
34
35
  )
35
36
  );
@@ -37,6 +38,39 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
37
38
  errorSchema: googleErrorDataSchema,
38
39
  errorToMessage: (data) => data.error.message
39
40
  });
41
+ function mapGoogleErrorStatusToHttpStatusCode(status) {
42
+ switch (status) {
43
+ case "CANCELLED":
44
+ return 499;
45
+ case "INVALID_ARGUMENT":
46
+ case "FAILED_PRECONDITION":
47
+ case "OUT_OF_RANGE":
48
+ return 400;
49
+ case "DEADLINE_EXCEEDED":
50
+ return 504;
51
+ case "NOT_FOUND":
52
+ return 404;
53
+ case "ALREADY_EXISTS":
54
+ case "ABORTED":
55
+ return 409;
56
+ case "PERMISSION_DENIED":
57
+ return 403;
58
+ case "UNAUTHENTICATED":
59
+ return 401;
60
+ case "RESOURCE_EXHAUSTED":
61
+ return 429;
62
+ case "UNIMPLEMENTED":
63
+ return 501;
64
+ case "UNKNOWN":
65
+ case "INTERNAL":
66
+ case "DATA_LOSS":
67
+ return 500;
68
+ case "UNAVAILABLE":
69
+ return 503;
70
+ default:
71
+ return void 0;
72
+ }
73
+ }
40
74
 
41
75
  // src/transcription/google-transcription-model-options.ts
42
76
  import {
@@ -238,7 +272,7 @@ import {
238
272
  } from "@ai-sdk/provider-utils";
239
273
 
240
274
  // src/version.ts
241
- var VERSION = true ? "3.0.140" : "0.0.0-test";
275
+ var VERSION = true ? "3.0.142" : "0.0.0-test";
242
276
 
243
277
  // src/google-generative-ai-embedding-model.ts
244
278
  import {
@@ -474,6 +508,9 @@ function validateMultimodalContentLength({
474
508
  }
475
509
 
476
510
  // src/google-generative-ai-language-model.ts
511
+ import {
512
+ APICallError
513
+ } from "@ai-sdk/provider";
477
514
  import {
478
515
  combineHeaders as combineHeaders3,
479
516
  createEventSourceResponseHandler,
@@ -912,6 +949,7 @@ import {
912
949
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
913
950
  } from "@ai-sdk/provider";
914
951
  import {
952
+ stringifyWithBigIntAndCycles,
915
953
  convertToBase64 as convertToBase642,
916
954
  isUrlSupported,
917
955
  secureJsonParse
@@ -978,6 +1016,31 @@ function convertUrlToolResultPart(url, mediaType, supportedFunctionResponseUrls)
978
1016
  }
979
1017
  } : void 0;
980
1018
  }
1019
+ function serializeFunctionResponseContent(value) {
1020
+ return containsNonJsonValue(value) ? secureJsonParse(stringifyWithBigIntAndCycles(value)) : value;
1021
+ }
1022
+ function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
1023
+ if (typeof value === "bigint") {
1024
+ return true;
1025
+ }
1026
+ if (typeof value !== "object" || value === null) {
1027
+ return false;
1028
+ }
1029
+ if (typeof value.toJSON === "function") {
1030
+ return true;
1031
+ }
1032
+ if (ancestors.has(value)) {
1033
+ return true;
1034
+ }
1035
+ ancestors.add(value);
1036
+ try {
1037
+ return Object.values(value).some(
1038
+ (item) => containsNonJsonValue(item, ancestors)
1039
+ );
1040
+ } finally {
1041
+ ancestors.delete(value);
1042
+ }
1043
+ }
981
1044
  function appendToolResultParts(parts, toolName, outputValue, toolCallId, includeFunctionCallIds = true, supportedFunctionResponseUrls = {}) {
982
1045
  const functionResponseParts = [];
983
1046
  const responseTextParts = [];
@@ -1007,12 +1070,12 @@ function appendToolResultParts(parts, toolName, outputValue, toolCallId, include
1007
1070
  if (functionResponsePart != null) {
1008
1071
  functionResponseParts.push(functionResponsePart);
1009
1072
  } else {
1010
- responseTextParts.push(JSON.stringify(contentPart));
1073
+ responseTextParts.push(stringifyWithBigIntAndCycles(contentPart));
1011
1074
  }
1012
1075
  break;
1013
1076
  }
1014
1077
  default: {
1015
- responseTextParts.push(JSON.stringify(contentPart));
1078
+ responseTextParts.push(stringifyWithBigIntAndCycles(contentPart));
1016
1079
  break;
1017
1080
  }
1018
1081
  }
@@ -1061,7 +1124,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId, i
1061
1124
  break;
1062
1125
  }
1063
1126
  default:
1064
- parts.push({ text: JSON.stringify(contentPart) });
1127
+ parts.push({ text: stringifyWithBigIntAndCycles(contentPart) });
1065
1128
  break;
1066
1129
  }
1067
1130
  }
@@ -1203,7 +1266,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1203
1266
  functionCall: {
1204
1267
  ...includeFunctionCallIds && part.toolCallId != null ? { id: part.toolCallId } : {},
1205
1268
  name: part.toolName,
1206
- args: part.input
1269
+ args: parseToolCallInput(part.input)
1207
1270
  },
1208
1271
  thoughtSignature: effectiveThoughtSignature
1209
1272
  };
@@ -1285,7 +1348,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1285
1348
  name: part.toolName,
1286
1349
  response: {
1287
1350
  name: part.toolName,
1288
- content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : output.value
1351
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : serializeFunctionResponseContent(output.value)
1289
1352
  }
1290
1353
  }
1291
1354
  });
@@ -1315,6 +1378,16 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1315
1378
  contents
1316
1379
  };
1317
1380
  }
1381
+ function parseToolCallInput(input) {
1382
+ if (typeof input !== "string") {
1383
+ return input;
1384
+ }
1385
+ try {
1386
+ return secureJsonParse(input);
1387
+ } catch (e) {
1388
+ return input;
1389
+ }
1390
+ }
1318
1391
 
1319
1392
  // src/download-tool-result-files.ts
1320
1393
  import {
@@ -1623,6 +1696,9 @@ function prepareTools({
1623
1696
  modelId,
1624
1697
  isVertexProvider = false
1625
1698
  }) {
1699
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
1700
+ tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
1701
+ }
1626
1702
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1627
1703
  const toolWarnings = [];
1628
1704
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -2618,10 +2694,11 @@ var GoogleGenerativeAILanguageModel = class {
2618
2694
  options.headers,
2619
2695
  extraHeaders
2620
2696
  );
2697
+ const url = `${this.config.baseURL}/${getModelPath(
2698
+ this.modelId
2699
+ )}:streamGenerateContent?alt=sse`;
2621
2700
  const streamGenerateContent = (body) => postJsonToApi3({
2622
- url: `${this.config.baseURL}/${getModelPath(
2623
- this.modelId
2624
- )}:streamGenerateContent?alt=sse`,
2701
+ url,
2625
2702
  headers,
2626
2703
  body,
2627
2704
  failedResponseHandler: googleFailedResponseHandler,
@@ -2711,7 +2788,7 @@ var GoogleGenerativeAILanguageModel = class {
2711
2788
  controller.enqueue({ type: "stream-start", warnings });
2712
2789
  },
2713
2790
  transform(chunk, controller) {
2714
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2791
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2715
2792
  if (options.includeRawChunks) {
2716
2793
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2717
2794
  }
@@ -2720,6 +2797,23 @@ var GoogleGenerativeAILanguageModel = class {
2720
2797
  return;
2721
2798
  }
2722
2799
  const value = chunk.value;
2800
+ if (value.error != null) {
2801
+ finishReason = { unified: "error", raw: void 0 };
2802
+ const error = value.error;
2803
+ controller.enqueue({
2804
+ type: "error",
2805
+ error: new APICallError({
2806
+ message: error.message,
2807
+ url,
2808
+ requestBodyValues: args,
2809
+ statusCode: (_a2 = error.code) != null ? _a2 : mapGoogleErrorStatusToHttpStatusCode(error.status),
2810
+ responseHeaders,
2811
+ responseBody: JSON.stringify({ error }),
2812
+ data: error
2813
+ })
2814
+ });
2815
+ return;
2816
+ }
2723
2817
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2724
2818
  hasEmittedResponseMetadata = true;
2725
2819
  controller.enqueue({
@@ -2741,7 +2835,7 @@ var GoogleGenerativeAILanguageModel = class {
2741
2835
  };
2742
2836
  }
2743
2837
  }
2744
- const candidate = (_a2 = value.candidates) == null ? void 0 : _a2[0];
2838
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2745
2839
  if (candidate != null) {
2746
2840
  if (candidate.groundingMetadata != null) {
2747
2841
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2776,9 +2870,9 @@ var GoogleGenerativeAILanguageModel = class {
2776
2870
  }
2777
2871
  }
2778
2872
  if (content != null) {
2779
- const parts = (_b = content.parts) != null ? _b : [];
2873
+ const parts = (_c = content.parts) != null ? _c : [];
2780
2874
  for (const part of parts) {
2781
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2875
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2782
2876
  const toolCallId = generateId3();
2783
2877
  lastCodeExecutionToolCallId = toolCallId;
2784
2878
  controller.enqueue({
@@ -2797,7 +2891,7 @@ var GoogleGenerativeAILanguageModel = class {
2797
2891
  toolName: "code_execution",
2798
2892
  result: {
2799
2893
  outcome: part.codeExecutionResult.outcome,
2800
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2894
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2801
2895
  }
2802
2896
  });
2803
2897
  }
@@ -2891,7 +2985,7 @@ var GoogleGenerativeAILanguageModel = class {
2891
2985
  providerMetadata: fileMeta
2892
2986
  });
2893
2987
  } else if ("toolCall" in part && part.toolCall) {
2894
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2988
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId3();
2895
2989
  lastServerToolCallId = toolCallId;
2896
2990
  const serverMeta = {
2897
2991
  [providerOptionsName]: {
@@ -2904,13 +2998,13 @@ var GoogleGenerativeAILanguageModel = class {
2904
2998
  type: "tool-call",
2905
2999
  toolCallId,
2906
3000
  toolName: `server:${part.toolCall.toolType}`,
2907
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
3001
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2908
3002
  providerExecuted: true,
2909
3003
  dynamic: true,
2910
3004
  providerMetadata: serverMeta
2911
3005
  });
2912
3006
  } else if ("toolResponse" in part && part.toolResponse) {
2913
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
3007
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId3();
2914
3008
  const serverMeta = {
2915
3009
  [providerOptionsName]: {
2916
3010
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2922,7 +3016,7 @@ var GoogleGenerativeAILanguageModel = class {
2922
3016
  type: "tool-result",
2923
3017
  toolCallId: responseToolCallId,
2924
3018
  toolName: `server:${part.toolResponse.toolType}`,
2925
- result: (_h = part.toolResponse.response) != null ? _h : {},
3019
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2926
3020
  providerMetadata: serverMeta
2927
3021
  });
2928
3022
  lastServerToolCallId = void 0;
@@ -2941,7 +3035,7 @@ var GoogleGenerativeAILanguageModel = class {
2941
3035
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2942
3036
  if (isStreamingChunk) {
2943
3037
  if (part.functionCall.name != null) {
2944
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
3038
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2945
3039
  const accumulator = new GoogleJSONAccumulator();
2946
3040
  activeStreamingToolCalls.push({
2947
3041
  toolCallId,
@@ -2989,9 +3083,9 @@ var GoogleGenerativeAILanguageModel = class {
2989
3083
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2990
3084
  finishActiveStreamingToolCall(controller);
2991
3085
  } else if (isCompleteCall) {
2992
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
3086
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId3();
2993
3087
  const toolName = part.functionCall.name;
2994
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
3088
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2995
3089
  controller.enqueue({
2996
3090
  type: "tool-input-start",
2997
3091
  id: toolCallId,
@@ -3018,7 +3112,7 @@ var GoogleGenerativeAILanguageModel = class {
3018
3112
  });
3019
3113
  hasToolCalls = true;
3020
3114
  } else if (isNoArgsCompleteCall) {
3021
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
3115
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
3022
3116
  const toolName = part.functionCall.name;
3023
3117
  controller.enqueue({
3024
3118
  type: "tool-input-start",
@@ -3389,6 +3483,7 @@ var responseSchema = lazySchema7(
3389
3483
  var chunkSchema = lazySchema7(
3390
3484
  () => zodSchema7(
3391
3485
  z8.object({
3486
+ error: getGoogleErrorSchema().nullish(),
3392
3487
  responseId: z8.string().nullish(),
3393
3488
  candidates: z8.array(
3394
3489
  z8.object({
@@ -6211,6 +6306,9 @@ function prepareGoogleInteractionsTools({
6211
6306
  }) {
6212
6307
  var _a2, _b, _c, _d;
6213
6308
  const toolWarnings = [];
6309
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
6310
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
6311
+ }
6214
6312
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
6215
6313
  if (normalized == null) {
6216
6314
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -6332,9 +6430,6 @@ function prepareGoogleInteractionsTools({
6332
6430
  case "required":
6333
6431
  mappedToolChoice = "any";
6334
6432
  break;
6335
- case "none":
6336
- mappedToolChoice = "none";
6337
- break;
6338
6433
  case "tool":
6339
6434
  mappedToolChoice = {
6340
6435
  allowed_tools: {