@ai-sdk/google 2.0.84 → 2.0.85

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.
@@ -133,13 +133,15 @@ import {
133
133
  UnsupportedFunctionalityError
134
134
  } from "@ai-sdk/provider";
135
135
  import { convertToBase64 } from "@ai-sdk/provider-utils";
136
+ var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
136
137
  function convertToGoogleGenerativeAIMessages(prompt, options) {
137
- var _a, _b;
138
+ var _a, _b, _c;
138
139
  const systemInstructionParts = [];
139
140
  const contents = [];
140
141
  let systemMessagesAllowed = true;
141
142
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
142
- const supportsFunctionResponseParts = (_b = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _b : true;
143
+ const isGemini3Model = (_b = options == null ? void 0 : options.isGemini3Model) != null ? _b : false;
144
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
143
145
  for (const { role, content } of prompt) {
144
146
  switch (role) {
145
147
  case "system": {
@@ -184,11 +186,12 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
184
186
  }
185
187
  case "assistant": {
186
188
  systemMessagesAllowed = false;
189
+ let modelResponseHasSignedFunctionCall = false;
187
190
  contents.push({
188
191
  role: "model",
189
192
  parts: content.map((part) => {
190
- var _a2, _b2, _c;
191
- const thoughtSignature = ((_b2 = (_a2 = part.providerOptions) == null ? void 0 : _a2.google) == null ? void 0 : _b2.thoughtSignature) != null ? String((_c = part.providerOptions.google) == null ? void 0 : _c.thoughtSignature) : void 0;
193
+ var _a2, _b2, _c2;
194
+ const thoughtSignature = ((_b2 = (_a2 = part.providerOptions) == null ? void 0 : _a2.google) == null ? void 0 : _b2.thoughtSignature) != null ? String((_c2 = part.providerOptions.google) == null ? void 0 : _c2.thoughtSignature) : void 0;
192
195
  switch (part.type) {
193
196
  case "text": {
194
197
  return part.text.length === 0 ? void 0 : {
@@ -222,13 +225,17 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
222
225
  };
223
226
  }
224
227
  case "tool-call": {
228
+ const effectiveThoughtSignature = thoughtSignature != null ? thoughtSignature : isGemini3Model && !modelResponseHasSignedFunctionCall ? SKIP_THOUGHT_SIGNATURE_VALIDATOR : void 0;
229
+ if (thoughtSignature != null) {
230
+ modelResponseHasSignedFunctionCall = true;
231
+ }
225
232
  return {
226
233
  functionCall: {
227
234
  ...part.toolCallId != null ? { id: part.toolCallId } : {},
228
235
  name: part.toolName,
229
236
  args: part.input
230
237
  },
231
- thoughtSignature
238
+ thoughtSignature: effectiveThoughtSignature
232
239
  };
233
240
  }
234
241
  }
@@ -544,6 +551,27 @@ var VertexServiceTierMap = {
544
551
  priority: "SERVICE_TIER_PRIORITY"
545
552
  };
546
553
 
554
+ // src/google-model-capabilities.ts
555
+ var gemini1ModelPattern = /(^|\/)gemini-1(?:[.-]|$)/i;
556
+ var gemini2ModelPattern = /(^|\/)gemini-2(?:[.-]|$)/i;
557
+ var gemini25ModelPattern = /(^|\/)gemini-2\.5(?:[.-]|$)/i;
558
+ var geminiModelPattern = /(^|\/)gemini-/i;
559
+ function isKnownPreGemini2Model(modelId) {
560
+ return gemini1ModelPattern.test(modelId) || /(^|\/)gemini-pro(?:-vision)?$/i.test(modelId) || /(^|\/)gemini-robotics-er-1\.5(?:[.-]|$)/i.test(modelId);
561
+ }
562
+ function getGoogleModelCapabilities(modelId) {
563
+ const isGeminiModel = geminiModelPattern.test(modelId);
564
+ const isGemini2Model = gemini2ModelPattern.test(modelId);
565
+ const isKnownPreGemini2 = isKnownPreGemini2Model(modelId);
566
+ const isKnownOlderModel = isKnownPreGemini2 || isGemini2Model;
567
+ const usesGemini3Features = isGeminiModel && !isKnownOlderModel;
568
+ return {
569
+ supportsGemini2Tools: isGeminiModel && !isKnownPreGemini2 || modelId.toLowerCase().includes("nano-banana"),
570
+ supportsFileSearch: gemini25ModelPattern.test(modelId) || usesGemini3Features,
571
+ usesGemini3Features
572
+ };
573
+ }
574
+
547
575
  // src/google-prepare-tools.ts
548
576
  import {
549
577
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
@@ -551,18 +579,13 @@ import {
551
579
  function prepareTools({
552
580
  tools,
553
581
  toolChoice,
554
- modelId
582
+ modelId,
583
+ isVertexProvider = false
555
584
  }) {
556
- var _a;
585
+ var _a, _b;
557
586
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
558
587
  const toolWarnings = [];
559
- const isLatest = [
560
- "gemini-flash-latest",
561
- "gemini-flash-lite-latest",
562
- "gemini-pro-latest"
563
- ].some((id) => id === modelId);
564
- const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
565
- const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
588
+ const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
566
589
  if (tools == null) {
567
590
  return { tools: void 0, toolConfig: void 0, toolWarnings };
568
591
  }
@@ -570,7 +593,7 @@ function prepareTools({
570
593
  const hasProviderDefinedTools = tools.some(
571
594
  (tool) => tool.type === "provider-defined"
572
595
  );
573
- if (hasFunctionTools && hasProviderDefinedTools) {
596
+ if (hasFunctionTools && hasProviderDefinedTools && !usesGemini3Features) {
574
597
  const functionTools = tools.filter((tool) => tool.type === "function");
575
598
  toolWarnings.push({
576
599
  type: "unsupported-tool",
@@ -586,7 +609,7 @@ function prepareTools({
586
609
  providerDefinedTools.forEach((tool) => {
587
610
  switch (tool.id) {
588
611
  case "google.google_search":
589
- if (isGemini2orNewer) {
612
+ if (supportsGemini2Tools) {
590
613
  googleTools2.push({ googleSearch: { ...tool.args } });
591
614
  } else {
592
615
  toolWarnings.push({
@@ -597,7 +620,7 @@ function prepareTools({
597
620
  }
598
621
  break;
599
622
  case "google.enterprise_web_search":
600
- if (isGemini2orNewer) {
623
+ if (supportsGemini2Tools) {
601
624
  googleTools2.push({ enterpriseWebSearch: {} });
602
625
  } else {
603
626
  toolWarnings.push({
@@ -608,7 +631,7 @@ function prepareTools({
608
631
  }
609
632
  break;
610
633
  case "google.url_context":
611
- if (isGemini2orNewer) {
634
+ if (supportsGemini2Tools) {
612
635
  googleTools2.push({ urlContext: {} });
613
636
  } else {
614
637
  toolWarnings.push({
@@ -619,7 +642,7 @@ function prepareTools({
619
642
  }
620
643
  break;
621
644
  case "google.code_execution":
622
- if (isGemini2orNewer) {
645
+ if (supportsGemini2Tools) {
623
646
  googleTools2.push({ codeExecution: {} });
624
647
  } else {
625
648
  toolWarnings.push({
@@ -641,7 +664,7 @@ function prepareTools({
641
664
  }
642
665
  break;
643
666
  case "google.vertex_rag_store":
644
- if (isGemini2orNewer) {
667
+ if (supportsGemini2Tools) {
645
668
  googleTools2.push({
646
669
  retrieval: {
647
670
  vertex_rag_store: {
@@ -661,7 +684,7 @@ function prepareTools({
661
684
  }
662
685
  break;
663
686
  case "google.google_maps":
664
- if (isGemini2orNewer) {
687
+ if (supportsGemini2Tools) {
665
688
  googleTools2.push({ googleMaps: {} });
666
689
  } else {
667
690
  toolWarnings.push({
@@ -676,6 +699,47 @@ function prepareTools({
676
699
  break;
677
700
  }
678
701
  });
702
+ if (hasFunctionTools && usesGemini3Features && googleTools2.length > 0) {
703
+ const functionDeclarations2 = [];
704
+ for (const tool of tools) {
705
+ if (tool.type === "function") {
706
+ functionDeclarations2.push({
707
+ name: tool.name,
708
+ description: (_a = tool.description) != null ? _a : "",
709
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
710
+ });
711
+ }
712
+ }
713
+ const combinedToolConfig = {
714
+ functionCallingConfig: { mode: "VALIDATED" },
715
+ ...!isVertexProvider && {
716
+ includeServerSideToolInvocations: true
717
+ }
718
+ };
719
+ if (toolChoice != null) {
720
+ switch (toolChoice.type) {
721
+ case "auto":
722
+ break;
723
+ case "none":
724
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
725
+ break;
726
+ case "required":
727
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
728
+ break;
729
+ case "tool":
730
+ combinedToolConfig.functionCallingConfig = {
731
+ mode: "ANY",
732
+ allowedFunctionNames: [toolChoice.toolName]
733
+ };
734
+ break;
735
+ }
736
+ }
737
+ return {
738
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
739
+ toolConfig: combinedToolConfig,
740
+ toolWarnings
741
+ };
742
+ }
679
743
  return {
680
744
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
681
745
  toolConfig: void 0,
@@ -689,7 +753,7 @@ function prepareTools({
689
753
  case "function":
690
754
  functionDeclarations.push({
691
755
  name: tool.name,
692
- description: (_a = tool.description) != null ? _a : "",
756
+ description: (_b = tool.description) != null ? _b : "",
693
757
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
694
758
  });
695
759
  if (tool.strict === true) {
@@ -867,10 +931,14 @@ var GoogleGenerativeAILanguageModel = class {
867
931
  threshold: safetyThreshold
868
932
  })) : void 0;
869
933
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
870
- const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
934
+ const { usesGemini3Features } = getGoogleModelCapabilities(this.modelId);
871
935
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
872
936
  prompt,
873
- { isGemmaModel, supportsFunctionResponseParts }
937
+ {
938
+ isGemmaModel,
939
+ isGemini3Model: usesGemini3Features,
940
+ supportsFunctionResponseParts: usesGemini3Features
941
+ }
874
942
  );
875
943
  const {
876
944
  tools: googleTools2,
@@ -879,7 +947,8 @@ var GoogleGenerativeAILanguageModel = class {
879
947
  } = prepareTools({
880
948
  tools,
881
949
  toolChoice,
882
- modelId: this.modelId
950
+ modelId: this.modelId,
951
+ isVertexProvider
883
952
  });
884
953
  return {
885
954
  args: {