@ai-sdk/google 3.0.78 → 3.0.80

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
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "3.0.78" : "0.0.0-test";
10
+ var VERSION = true ? "3.0.80" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -412,6 +412,23 @@ import {
412
412
  UnsupportedFunctionalityError
413
413
  } from "@ai-sdk/provider";
414
414
  import { convertToBase64 } from "@ai-sdk/provider-utils";
415
+ var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
416
+ function getGoogleProviderOptions(providerOptions, providerOptionsName) {
417
+ const namespaces = [
418
+ providerOptionsName,
419
+ "google",
420
+ "googleVertex",
421
+ "vertex"
422
+ ].filter((namespace, index, allNamespaces) => {
423
+ return allNamespaces.indexOf(namespace) === index;
424
+ });
425
+ for (const namespace of namespaces) {
426
+ const options = providerOptions == null ? void 0 : providerOptions[namespace];
427
+ if (options != null) {
428
+ return options;
429
+ }
430
+ }
431
+ }
415
432
  var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
416
433
  function parseBase64DataUrl(value) {
417
434
  const match = dataUrlRegex.exec(value);
@@ -519,13 +536,22 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId) {
519
536
  }
520
537
  }
521
538
  function convertToGoogleGenerativeAIMessages(prompt, options) {
522
- var _a, _b, _c, _d, _e, _f, _g, _h;
539
+ var _a, _b, _c, _d, _e;
523
540
  const systemInstructionParts = [];
524
541
  const contents = [];
525
542
  let systemMessagesAllowed = true;
526
543
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
527
- const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
528
- const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
544
+ const isGemini3Model = (_b = options == null ? void 0 : options.isGemini3Model) != null ? _b : false;
545
+ const providerOptionsName = (_c = options == null ? void 0 : options.providerOptionsName) != null ? _c : "google";
546
+ const supportsFunctionResponseParts = (_d = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _d : true;
547
+ const onWarning = options == null ? void 0 : options.onWarning;
548
+ let sentinelInjected = false;
549
+ const missingSignatureToolNames = [];
550
+ const injectSkipSignature = (toolName) => {
551
+ missingSignatureToolNames.push(toolName);
552
+ sentinelInjected = true;
553
+ return SKIP_THOUGHT_SIGNATURE_VALIDATOR;
554
+ };
529
555
  for (const { role, content } of prompt) {
530
556
  switch (role) {
531
557
  case "system": {
@@ -573,8 +599,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
573
599
  contents.push({
574
600
  role: "model",
575
601
  parts: content.map((part) => {
576
- var _a2, _b2, _c2, _d2;
577
- const providerOpts = (_d2 = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) != null ? _d2 : providerOptionsName !== "google" ? (_b2 = part.providerOptions) == null ? void 0 : _b2.google : (_c2 = part.providerOptions) == null ? void 0 : _c2.vertex;
602
+ const providerOpts = getGoogleProviderOptions(
603
+ part.providerOptions,
604
+ providerOptionsName
605
+ );
578
606
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
579
607
  switch (part.type) {
580
608
  case "text": {
@@ -608,6 +636,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
608
636
  case "tool-call": {
609
637
  const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
610
638
  const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
639
+ const effectiveThoughtSignature = thoughtSignature != null ? thoughtSignature : isGemini3Model ? injectSkipSignature(part.toolName) : void 0;
611
640
  if (serverToolCallId && serverToolType) {
612
641
  return {
613
642
  toolCall: {
@@ -615,7 +644,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
615
644
  args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
616
645
  id: serverToolCallId
617
646
  },
618
- thoughtSignature
647
+ thoughtSignature: effectiveThoughtSignature
619
648
  };
620
649
  }
621
650
  return {
@@ -624,7 +653,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
624
653
  name: part.toolName,
625
654
  args: part.input
626
655
  },
627
- thoughtSignature
656
+ thoughtSignature: effectiveThoughtSignature
628
657
  };
629
658
  }
630
659
  case "tool-result": {
@@ -654,7 +683,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
654
683
  if (part.type === "tool-approval-response") {
655
684
  continue;
656
685
  }
657
- const partProviderOpts = (_g = (_d = part.providerOptions) == null ? void 0 : _d[providerOptionsName]) != null ? _g : providerOptionsName !== "google" ? (_e = part.providerOptions) == null ? void 0 : _e.google : (_f = part.providerOptions) == null ? void 0 : _f.vertex;
686
+ const partProviderOpts = getGoogleProviderOptions(
687
+ part.providerOptions,
688
+ providerOptionsName
689
+ );
658
690
  const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
659
691
  const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
660
692
  if (serverToolCallId && serverToolType) {
@@ -698,7 +730,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
698
730
  name: part.toolName,
699
731
  response: {
700
732
  name: part.toolName,
701
- content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
733
+ content: output.type === "execution-denied" ? (_e = output.reason) != null ? _e : "Tool execution denied." : output.value
702
734
  }
703
735
  }
704
736
  });
@@ -716,6 +748,13 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
716
748
  const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
717
749
  contents[0].parts.unshift({ text: systemText + "\n\n" });
718
750
  }
751
+ if (sentinelInjected && onWarning != null) {
752
+ const uniqueToolNames = Array.from(new Set(missingSignatureToolNames));
753
+ onWarning({
754
+ type: "other",
755
+ message: `Replayed ${missingSignatureToolNames.length} \`functionCall\` part(s) for a Gemini 3 model without a \`thoughtSignature\` (tools: ${uniqueToolNames.map((name) => `\`${name}\``).join(", ")}). Injected the documented \`skip_thought_signature_validator\` sentinel to keep the request from failing with HTTP 400. The likely cause is application code that drops \`providerOptions.google.thoughtSignature\` when persisting or serializing assistant tool-call messages. See https://ai.google.dev/gemini-api/docs/thought-signatures.`
756
+ });
757
+ }
719
758
  return {
720
759
  systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
721
760
  contents
@@ -1474,13 +1513,16 @@ var GoogleGenerativeAILanguageModel = class {
1474
1513
  } : void 0;
1475
1514
  const bodyServiceTier = isVertexProvider ? void 0 : googleOptions == null ? void 0 : googleOptions.serviceTier;
1476
1515
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
1477
- const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
1516
+ const isGemini3Model = /^gemini-3[.-]/.test(this.modelId);
1517
+ const supportsFunctionResponseParts = isGemini3Model;
1478
1518
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
1479
1519
  prompt,
1480
1520
  {
1481
1521
  isGemmaModel,
1522
+ isGemini3Model,
1482
1523
  providerOptionsName,
1483
- supportsFunctionResponseParts
1524
+ supportsFunctionResponseParts,
1525
+ onWarning: (warning) => warnings.push(warning)
1484
1526
  }
1485
1527
  );
1486
1528
  const {
@@ -2708,7 +2750,15 @@ var GoogleGenerativeAIImageModel = class {
2708
2750
  parameters.aspectRatio = aspectRatio;
2709
2751
  }
2710
2752
  if (googleOptions) {
2711
- Object.assign(parameters, googleOptions);
2753
+ const { googleSearch: imagenGoogleSearch, ...imagenOptions } = googleOptions;
2754
+ if (imagenGoogleSearch != null) {
2755
+ warnings.push({
2756
+ type: "unsupported",
2757
+ feature: "googleSearch",
2758
+ details: "Google Search grounding is only supported on Gemini image models."
2759
+ });
2760
+ }
2761
+ Object.assign(parameters, imagenOptions);
2712
2762
  }
2713
2763
  const body = {
2714
2764
  instances: [{ prompt }],
@@ -2745,7 +2795,7 @@ var GoogleGenerativeAIImageModel = class {
2745
2795
  };
2746
2796
  }
2747
2797
  async doGenerateGemini(options) {
2748
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2798
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2749
2799
  const {
2750
2800
  prompt,
2751
2801
  n,
@@ -2800,12 +2850,18 @@ var GoogleGenerativeAIImageModel = class {
2800
2850
  const languageModelPrompt = [
2801
2851
  { role: "user", content: userContent }
2802
2852
  ];
2853
+ const googleImageOptions = await parseProviderOptions3({
2854
+ provider: "google",
2855
+ providerOptions,
2856
+ schema: googleImageModelOptionsSchema
2857
+ });
2858
+ const { googleSearch: _strippedGoogleSearch, ...passthroughGoogleOptions } = (_a = providerOptions == null ? void 0 : providerOptions.google) != null ? _a : {};
2803
2859
  const languageModel = new GoogleGenerativeAILanguageModel(this.modelId, {
2804
2860
  provider: this.config.provider,
2805
2861
  baseURL: this.config.baseURL,
2806
- headers: (_a = this.config.headers) != null ? _a : {},
2862
+ headers: (_b = this.config.headers) != null ? _b : {},
2807
2863
  fetch: this.config.fetch,
2808
- generateId: (_b = this.config.generateId) != null ? _b : defaultGenerateId
2864
+ generateId: (_c = this.config.generateId) != null ? _c : defaultGenerateId
2809
2865
  });
2810
2866
  const result = await languageModel.doGenerate({
2811
2867
  prompt: languageModelPrompt,
@@ -2816,9 +2872,17 @@ var GoogleGenerativeAIImageModel = class {
2816
2872
  imageConfig: aspectRatio ? {
2817
2873
  aspectRatio
2818
2874
  } : void 0,
2819
- ...(_c = providerOptions == null ? void 0 : providerOptions.google) != null ? _c : {}
2875
+ ...passthroughGoogleOptions
2820
2876
  }
2821
2877
  },
2878
+ tools: (googleImageOptions == null ? void 0 : googleImageOptions.googleSearch) != null ? [
2879
+ {
2880
+ type: "provider",
2881
+ id: "google.google_search",
2882
+ name: "google_search",
2883
+ args: googleImageOptions.googleSearch
2884
+ }
2885
+ ] : void 0,
2822
2886
  headers,
2823
2887
  abortSignal
2824
2888
  });
@@ -2829,23 +2893,25 @@ var GoogleGenerativeAIImageModel = class {
2829
2893
  images.push(convertToBase642(part.data));
2830
2894
  }
2831
2895
  }
2896
+ const languageModelGoogleMetadata = (_h = (_g = result.providerMetadata) == null ? void 0 : _g.google) != null ? _h : {};
2832
2897
  return {
2833
2898
  images,
2834
2899
  warnings,
2835
2900
  providerMetadata: {
2836
2901
  google: {
2902
+ ...languageModelGoogleMetadata,
2837
2903
  images: images.map(() => ({}))
2838
2904
  }
2839
2905
  },
2840
2906
  response: {
2841
2907
  timestamp: currentDate,
2842
2908
  modelId: this.modelId,
2843
- headers: (_g = result.response) == null ? void 0 : _g.headers
2909
+ headers: (_i = result.response) == null ? void 0 : _i.headers
2844
2910
  },
2845
2911
  usage: result.usage ? {
2846
2912
  inputTokens: result.usage.inputTokens.total,
2847
2913
  outputTokens: result.usage.outputTokens.total,
2848
- totalTokens: ((_h = result.usage.inputTokens.total) != null ? _h : 0) + ((_i = result.usage.outputTokens.total) != null ? _i : 0)
2914
+ totalTokens: ((_j = result.usage.inputTokens.total) != null ? _j : 0) + ((_k = result.usage.outputTokens.total) != null ? _k : 0)
2849
2915
  } : void 0
2850
2916
  };
2851
2917
  }
@@ -2864,7 +2930,17 @@ var googleImageModelOptionsSchema = lazySchema11(
2864
2930
  () => zodSchema11(
2865
2931
  z13.object({
2866
2932
  personGeneration: z13.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
2867
- aspectRatio: z13.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish()
2933
+ aspectRatio: z13.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish(),
2934
+ /**
2935
+ * Enable Google Search grounding for Gemini image models. The value is
2936
+ * forwarded as the args of the `google.tools.googleSearch` provider
2937
+ * tool on the underlying language-model call. Pass `{}` for defaults.
2938
+ *
2939
+ * `generateImage` does not accept a `tools` parameter, so this is the
2940
+ * dedicated escape hatch for grounding image generation the same way
2941
+ * `generateText` does.
2942
+ */
2943
+ googleSearch: googleSearchToolArgsBaseSchema.optional()
2868
2944
  })
2869
2945
  )
2870
2946
  );