@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/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.141" : "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 {
@@ -2618,10 +2691,11 @@ var GoogleGenerativeAILanguageModel = class {
2618
2691
  options.headers,
2619
2692
  extraHeaders
2620
2693
  );
2694
+ const url = `${this.config.baseURL}/${getModelPath(
2695
+ this.modelId
2696
+ )}:streamGenerateContent?alt=sse`;
2621
2697
  const streamGenerateContent = (body) => postJsonToApi3({
2622
- url: `${this.config.baseURL}/${getModelPath(
2623
- this.modelId
2624
- )}:streamGenerateContent?alt=sse`,
2698
+ url,
2625
2699
  headers,
2626
2700
  body,
2627
2701
  failedResponseHandler: googleFailedResponseHandler,
@@ -2711,7 +2785,7 @@ var GoogleGenerativeAILanguageModel = class {
2711
2785
  controller.enqueue({ type: "stream-start", warnings });
2712
2786
  },
2713
2787
  transform(chunk, controller) {
2714
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2788
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2715
2789
  if (options.includeRawChunks) {
2716
2790
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2717
2791
  }
@@ -2720,6 +2794,23 @@ var GoogleGenerativeAILanguageModel = class {
2720
2794
  return;
2721
2795
  }
2722
2796
  const value = chunk.value;
2797
+ if (value.error != null) {
2798
+ finishReason = { unified: "error", raw: void 0 };
2799
+ const error = value.error;
2800
+ controller.enqueue({
2801
+ type: "error",
2802
+ error: new APICallError({
2803
+ message: error.message,
2804
+ url,
2805
+ requestBodyValues: args,
2806
+ statusCode: (_a2 = error.code) != null ? _a2 : mapGoogleErrorStatusToHttpStatusCode(error.status),
2807
+ responseHeaders,
2808
+ responseBody: JSON.stringify({ error }),
2809
+ data: error
2810
+ })
2811
+ });
2812
+ return;
2813
+ }
2723
2814
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2724
2815
  hasEmittedResponseMetadata = true;
2725
2816
  controller.enqueue({
@@ -2741,7 +2832,7 @@ var GoogleGenerativeAILanguageModel = class {
2741
2832
  };
2742
2833
  }
2743
2834
  }
2744
- const candidate = (_a2 = value.candidates) == null ? void 0 : _a2[0];
2835
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2745
2836
  if (candidate != null) {
2746
2837
  if (candidate.groundingMetadata != null) {
2747
2838
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2776,9 +2867,9 @@ var GoogleGenerativeAILanguageModel = class {
2776
2867
  }
2777
2868
  }
2778
2869
  if (content != null) {
2779
- const parts = (_b = content.parts) != null ? _b : [];
2870
+ const parts = (_c = content.parts) != null ? _c : [];
2780
2871
  for (const part of parts) {
2781
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2872
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2782
2873
  const toolCallId = generateId3();
2783
2874
  lastCodeExecutionToolCallId = toolCallId;
2784
2875
  controller.enqueue({
@@ -2797,7 +2888,7 @@ var GoogleGenerativeAILanguageModel = class {
2797
2888
  toolName: "code_execution",
2798
2889
  result: {
2799
2890
  outcome: part.codeExecutionResult.outcome,
2800
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2891
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2801
2892
  }
2802
2893
  });
2803
2894
  }
@@ -2891,7 +2982,7 @@ var GoogleGenerativeAILanguageModel = class {
2891
2982
  providerMetadata: fileMeta
2892
2983
  });
2893
2984
  } else if ("toolCall" in part && part.toolCall) {
2894
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2985
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId3();
2895
2986
  lastServerToolCallId = toolCallId;
2896
2987
  const serverMeta = {
2897
2988
  [providerOptionsName]: {
@@ -2904,13 +2995,13 @@ var GoogleGenerativeAILanguageModel = class {
2904
2995
  type: "tool-call",
2905
2996
  toolCallId,
2906
2997
  toolName: `server:${part.toolCall.toolType}`,
2907
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2998
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2908
2999
  providerExecuted: true,
2909
3000
  dynamic: true,
2910
3001
  providerMetadata: serverMeta
2911
3002
  });
2912
3003
  } else if ("toolResponse" in part && part.toolResponse) {
2913
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
3004
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId3();
2914
3005
  const serverMeta = {
2915
3006
  [providerOptionsName]: {
2916
3007
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2922,7 +3013,7 @@ var GoogleGenerativeAILanguageModel = class {
2922
3013
  type: "tool-result",
2923
3014
  toolCallId: responseToolCallId,
2924
3015
  toolName: `server:${part.toolResponse.toolType}`,
2925
- result: (_h = part.toolResponse.response) != null ? _h : {},
3016
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2926
3017
  providerMetadata: serverMeta
2927
3018
  });
2928
3019
  lastServerToolCallId = void 0;
@@ -2941,7 +3032,7 @@ var GoogleGenerativeAILanguageModel = class {
2941
3032
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2942
3033
  if (isStreamingChunk) {
2943
3034
  if (part.functionCall.name != null) {
2944
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
3035
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2945
3036
  const accumulator = new GoogleJSONAccumulator();
2946
3037
  activeStreamingToolCalls.push({
2947
3038
  toolCallId,
@@ -2989,9 +3080,9 @@ var GoogleGenerativeAILanguageModel = class {
2989
3080
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2990
3081
  finishActiveStreamingToolCall(controller);
2991
3082
  } else if (isCompleteCall) {
2992
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
3083
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId3();
2993
3084
  const toolName = part.functionCall.name;
2994
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
3085
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2995
3086
  controller.enqueue({
2996
3087
  type: "tool-input-start",
2997
3088
  id: toolCallId,
@@ -3018,7 +3109,7 @@ var GoogleGenerativeAILanguageModel = class {
3018
3109
  });
3019
3110
  hasToolCalls = true;
3020
3111
  } else if (isNoArgsCompleteCall) {
3021
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
3112
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
3022
3113
  const toolName = part.functionCall.name;
3023
3114
  controller.enqueue({
3024
3115
  type: "tool-input-start",
@@ -3389,6 +3480,7 @@ var responseSchema = lazySchema7(
3389
3480
  var chunkSchema = lazySchema7(
3390
3481
  () => zodSchema7(
3391
3482
  z8.object({
3483
+ error: getGoogleErrorSchema().nullish(),
3392
3484
  responseId: z8.string().nullish(),
3393
3485
  candidates: z8.array(
3394
3486
  z8.object({
@@ -6211,6 +6303,9 @@ function prepareGoogleInteractionsTools({
6211
6303
  }) {
6212
6304
  var _a2, _b, _c, _d;
6213
6305
  const toolWarnings = [];
6306
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
6307
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
6308
+ }
6214
6309
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
6215
6310
  if (normalized == null) {
6216
6311
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -6332,9 +6427,6 @@ function prepareGoogleInteractionsTools({
6332
6427
  case "required":
6333
6428
  mappedToolChoice = "any";
6334
6429
  break;
6335
- case "none":
6336
- mappedToolChoice = "none";
6337
- break;
6338
6430
  case "tool":
6339
6431
  mappedToolChoice = {
6340
6432
  allowed_tools: {