@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.
@@ -150,13 +150,15 @@ function isEmptyObjectSchema(jsonSchema) {
150
150
  // src/convert-to-google-generative-ai-messages.ts
151
151
  var import_provider = require("@ai-sdk/provider");
152
152
  var import_provider_utils = require("@ai-sdk/provider-utils");
153
+ var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
153
154
  function convertToGoogleGenerativeAIMessages(prompt, options) {
154
- var _a, _b;
155
+ var _a, _b, _c;
155
156
  const systemInstructionParts = [];
156
157
  const contents = [];
157
158
  let systemMessagesAllowed = true;
158
159
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
159
- const supportsFunctionResponseParts = (_b = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _b : true;
160
+ const isGemini3Model = (_b = options == null ? void 0 : options.isGemini3Model) != null ? _b : false;
161
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
160
162
  for (const { role, content } of prompt) {
161
163
  switch (role) {
162
164
  case "system": {
@@ -201,11 +203,12 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
201
203
  }
202
204
  case "assistant": {
203
205
  systemMessagesAllowed = false;
206
+ let modelResponseHasSignedFunctionCall = false;
204
207
  contents.push({
205
208
  role: "model",
206
209
  parts: content.map((part) => {
207
- var _a2, _b2, _c;
208
- 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;
210
+ var _a2, _b2, _c2;
211
+ 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;
209
212
  switch (part.type) {
210
213
  case "text": {
211
214
  return part.text.length === 0 ? void 0 : {
@@ -239,13 +242,17 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
239
242
  };
240
243
  }
241
244
  case "tool-call": {
245
+ const effectiveThoughtSignature = thoughtSignature != null ? thoughtSignature : isGemini3Model && !modelResponseHasSignedFunctionCall ? SKIP_THOUGHT_SIGNATURE_VALIDATOR : void 0;
246
+ if (thoughtSignature != null) {
247
+ modelResponseHasSignedFunctionCall = true;
248
+ }
242
249
  return {
243
250
  functionCall: {
244
251
  ...part.toolCallId != null ? { id: part.toolCallId } : {},
245
252
  name: part.toolName,
246
253
  args: part.input
247
254
  },
248
- thoughtSignature
255
+ thoughtSignature: effectiveThoughtSignature
249
256
  };
250
257
  }
251
258
  }
@@ -554,23 +561,39 @@ var VertexServiceTierMap = {
554
561
  priority: "SERVICE_TIER_PRIORITY"
555
562
  };
556
563
 
564
+ // src/google-model-capabilities.ts
565
+ var gemini1ModelPattern = /(^|\/)gemini-1(?:[.-]|$)/i;
566
+ var gemini2ModelPattern = /(^|\/)gemini-2(?:[.-]|$)/i;
567
+ var gemini25ModelPattern = /(^|\/)gemini-2\.5(?:[.-]|$)/i;
568
+ var geminiModelPattern = /(^|\/)gemini-/i;
569
+ function isKnownPreGemini2Model(modelId) {
570
+ return gemini1ModelPattern.test(modelId) || /(^|\/)gemini-pro(?:-vision)?$/i.test(modelId) || /(^|\/)gemini-robotics-er-1\.5(?:[.-]|$)/i.test(modelId);
571
+ }
572
+ function getGoogleModelCapabilities(modelId) {
573
+ const isGeminiModel = geminiModelPattern.test(modelId);
574
+ const isGemini2Model = gemini2ModelPattern.test(modelId);
575
+ const isKnownPreGemini2 = isKnownPreGemini2Model(modelId);
576
+ const isKnownOlderModel = isKnownPreGemini2 || isGemini2Model;
577
+ const usesGemini3Features = isGeminiModel && !isKnownOlderModel;
578
+ return {
579
+ supportsGemini2Tools: isGeminiModel && !isKnownPreGemini2 || modelId.toLowerCase().includes("nano-banana"),
580
+ supportsFileSearch: gemini25ModelPattern.test(modelId) || usesGemini3Features,
581
+ usesGemini3Features
582
+ };
583
+ }
584
+
557
585
  // src/google-prepare-tools.ts
558
586
  var import_provider2 = require("@ai-sdk/provider");
559
587
  function prepareTools({
560
588
  tools,
561
589
  toolChoice,
562
- modelId
590
+ modelId,
591
+ isVertexProvider = false
563
592
  }) {
564
- var _a;
593
+ var _a, _b;
565
594
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
566
595
  const toolWarnings = [];
567
- const isLatest = [
568
- "gemini-flash-latest",
569
- "gemini-flash-lite-latest",
570
- "gemini-pro-latest"
571
- ].some((id) => id === modelId);
572
- const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
573
- const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
596
+ const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
574
597
  if (tools == null) {
575
598
  return { tools: void 0, toolConfig: void 0, toolWarnings };
576
599
  }
@@ -578,7 +601,7 @@ function prepareTools({
578
601
  const hasProviderDefinedTools = tools.some(
579
602
  (tool) => tool.type === "provider-defined"
580
603
  );
581
- if (hasFunctionTools && hasProviderDefinedTools) {
604
+ if (hasFunctionTools && hasProviderDefinedTools && !usesGemini3Features) {
582
605
  const functionTools = tools.filter((tool) => tool.type === "function");
583
606
  toolWarnings.push({
584
607
  type: "unsupported-tool",
@@ -594,7 +617,7 @@ function prepareTools({
594
617
  providerDefinedTools.forEach((tool) => {
595
618
  switch (tool.id) {
596
619
  case "google.google_search":
597
- if (isGemini2orNewer) {
620
+ if (supportsGemini2Tools) {
598
621
  googleTools2.push({ googleSearch: { ...tool.args } });
599
622
  } else {
600
623
  toolWarnings.push({
@@ -605,7 +628,7 @@ function prepareTools({
605
628
  }
606
629
  break;
607
630
  case "google.enterprise_web_search":
608
- if (isGemini2orNewer) {
631
+ if (supportsGemini2Tools) {
609
632
  googleTools2.push({ enterpriseWebSearch: {} });
610
633
  } else {
611
634
  toolWarnings.push({
@@ -616,7 +639,7 @@ function prepareTools({
616
639
  }
617
640
  break;
618
641
  case "google.url_context":
619
- if (isGemini2orNewer) {
642
+ if (supportsGemini2Tools) {
620
643
  googleTools2.push({ urlContext: {} });
621
644
  } else {
622
645
  toolWarnings.push({
@@ -627,7 +650,7 @@ function prepareTools({
627
650
  }
628
651
  break;
629
652
  case "google.code_execution":
630
- if (isGemini2orNewer) {
653
+ if (supportsGemini2Tools) {
631
654
  googleTools2.push({ codeExecution: {} });
632
655
  } else {
633
656
  toolWarnings.push({
@@ -649,7 +672,7 @@ function prepareTools({
649
672
  }
650
673
  break;
651
674
  case "google.vertex_rag_store":
652
- if (isGemini2orNewer) {
675
+ if (supportsGemini2Tools) {
653
676
  googleTools2.push({
654
677
  retrieval: {
655
678
  vertex_rag_store: {
@@ -669,7 +692,7 @@ function prepareTools({
669
692
  }
670
693
  break;
671
694
  case "google.google_maps":
672
- if (isGemini2orNewer) {
695
+ if (supportsGemini2Tools) {
673
696
  googleTools2.push({ googleMaps: {} });
674
697
  } else {
675
698
  toolWarnings.push({
@@ -684,6 +707,47 @@ function prepareTools({
684
707
  break;
685
708
  }
686
709
  });
710
+ if (hasFunctionTools && usesGemini3Features && googleTools2.length > 0) {
711
+ const functionDeclarations2 = [];
712
+ for (const tool of tools) {
713
+ if (tool.type === "function") {
714
+ functionDeclarations2.push({
715
+ name: tool.name,
716
+ description: (_a = tool.description) != null ? _a : "",
717
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
718
+ });
719
+ }
720
+ }
721
+ const combinedToolConfig = {
722
+ functionCallingConfig: { mode: "VALIDATED" },
723
+ ...!isVertexProvider && {
724
+ includeServerSideToolInvocations: true
725
+ }
726
+ };
727
+ if (toolChoice != null) {
728
+ switch (toolChoice.type) {
729
+ case "auto":
730
+ break;
731
+ case "none":
732
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
733
+ break;
734
+ case "required":
735
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
736
+ break;
737
+ case "tool":
738
+ combinedToolConfig.functionCallingConfig = {
739
+ mode: "ANY",
740
+ allowedFunctionNames: [toolChoice.toolName]
741
+ };
742
+ break;
743
+ }
744
+ }
745
+ return {
746
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
747
+ toolConfig: combinedToolConfig,
748
+ toolWarnings
749
+ };
750
+ }
687
751
  return {
688
752
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
689
753
  toolConfig: void 0,
@@ -697,7 +761,7 @@ function prepareTools({
697
761
  case "function":
698
762
  functionDeclarations.push({
699
763
  name: tool.name,
700
- description: (_a = tool.description) != null ? _a : "",
764
+ description: (_b = tool.description) != null ? _b : "",
701
765
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
702
766
  });
703
767
  if (tool.strict === true) {
@@ -875,10 +939,14 @@ var GoogleGenerativeAILanguageModel = class {
875
939
  threshold: safetyThreshold
876
940
  })) : void 0;
877
941
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
878
- const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
942
+ const { usesGemini3Features } = getGoogleModelCapabilities(this.modelId);
879
943
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
880
944
  prompt,
881
- { isGemmaModel, supportsFunctionResponseParts }
945
+ {
946
+ isGemmaModel,
947
+ isGemini3Model: usesGemini3Features,
948
+ supportsFunctionResponseParts: usesGemini3Features
949
+ }
882
950
  );
883
951
  const {
884
952
  tools: googleTools2,
@@ -887,7 +955,8 @@ var GoogleGenerativeAILanguageModel = class {
887
955
  } = prepareTools({
888
956
  tools,
889
957
  toolChoice,
890
- modelId: this.modelId
958
+ modelId: this.modelId,
959
+ isVertexProvider
891
960
  });
892
961
  return {
893
962
  args: {