@ai-sdk/google 2.0.90 → 2.0.93

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.
@@ -16,6 +16,10 @@ import { z as z3 } from "zod/v4";
16
16
  import {
17
17
  UnsupportedFunctionalityError
18
18
  } from "@ai-sdk/provider";
19
+ var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
20
+ function isRecursiveJSONSchemaReferenceError(error) {
21
+ return UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
22
+ }
19
23
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
20
24
  const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
21
25
  return convertJSONSchemaDefinition(jsonSchema, isRoot, {
@@ -66,9 +70,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
66
70
  if (description) result.description = description;
67
71
  if (required) result.required = required;
68
72
  if (format) result.format = format;
69
- if (constValue !== void 0) {
70
- result.enum = [constValue];
71
- }
72
73
  if (type) {
73
74
  if (Array.isArray(type)) {
74
75
  const hasNull = type.includes("null");
@@ -85,8 +86,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
85
86
  result.type = type;
86
87
  }
87
88
  }
88
- if (enumValues !== void 0) {
89
- result.enum = enumValues;
89
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
90
+ if (values !== void 0) {
91
+ addEnumToSchema({ values, type, result });
90
92
  }
91
93
  if (properties != null) {
92
94
  result.properties = Object.entries(properties).reduce(
@@ -146,6 +148,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
146
148
  }
147
149
  return result;
148
150
  }
151
+ function addEnumToSchema({
152
+ values,
153
+ type,
154
+ result
155
+ }) {
156
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
157
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
158
+ if (values.length > 0 && values.every((value) => value === null)) {
159
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
160
+ if (typeAllowsNull) {
161
+ result.type = "null";
162
+ if (Array.isArray(type)) {
163
+ delete result.anyOf;
164
+ }
165
+ return;
166
+ }
167
+ }
168
+ const enumType = getEnumType({ values: enumValues, type });
169
+ if (enumType === void 0) {
170
+ throw new UnsupportedFunctionalityError({
171
+ functionality: "JSON Schema enum with mixed or unsupported values",
172
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
173
+ });
174
+ }
175
+ result.type = enumType;
176
+ if (Array.isArray(type)) {
177
+ delete result.anyOf;
178
+ }
179
+ if (nullable) {
180
+ result.nullable = true;
181
+ }
182
+ if (enumType === "string") {
183
+ result.enum = enumValues;
184
+ } else {
185
+ result.format = "enum";
186
+ result.enum = enumValues.map(String);
187
+ }
188
+ }
189
+ function getEnumType({
190
+ values,
191
+ type
192
+ }) {
193
+ if (values.length === 0) {
194
+ return void 0;
195
+ }
196
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
197
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
198
+ return "string";
199
+ }
200
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
201
+ if (typeAllows("number")) {
202
+ return "number";
203
+ }
204
+ if (values.every((value) => Number.isInteger(value))) {
205
+ return "integer";
206
+ }
207
+ }
208
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
209
+ return "boolean";
210
+ }
211
+ return void 0;
212
+ }
149
213
  function convertJSONSchemaReference({
150
214
  jsonSchema,
151
215
  reference,
@@ -158,7 +222,7 @@ function convertJSONSchemaReference({
158
222
  );
159
223
  if (referenceContext.resolvingReferences.has(referenceKey)) {
160
224
  throw new UnsupportedFunctionalityError({
161
- functionality: `recursive JSON Schema reference: ${reference}`,
225
+ functionality: `${recursiveReferenceFunctionalityPrefix} ${reference}`,
162
226
  message: "Google schema conversion does not support recursive JSON Schema references."
163
227
  });
164
228
  }
@@ -675,7 +739,6 @@ function prepareTools({
675
739
  modelId,
676
740
  isVertexProvider = false
677
741
  }) {
678
- var _a, _b;
679
742
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
680
743
  const toolWarnings = [];
681
744
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -796,11 +859,7 @@ function prepareTools({
796
859
  const functionDeclarations2 = [];
797
860
  for (const tool of tools) {
798
861
  if (tool.type === "function") {
799
- functionDeclarations2.push({
800
- name: tool.name,
801
- description: (_a = tool.description) != null ? _a : "",
802
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
803
- });
862
+ functionDeclarations2.push(prepareFunctionDeclaration(tool));
804
863
  }
805
864
  }
806
865
  const combinedToolConfig = {
@@ -844,11 +903,7 @@ function prepareTools({
844
903
  for (const tool of tools) {
845
904
  switch (tool.type) {
846
905
  case "function":
847
- functionDeclarations.push({
848
- name: tool.name,
849
- description: (_b = tool.description) != null ? _b : "",
850
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
851
- });
906
+ functionDeclarations.push(prepareFunctionDeclaration(tool));
852
907
  if (tool.strict === true) {
853
908
  hasStrictTools = true;
854
909
  }
@@ -912,6 +967,27 @@ function prepareTools({
912
967
  }
913
968
  }
914
969
  }
970
+ function prepareFunctionDeclaration(tool) {
971
+ var _a;
972
+ const declaration = {
973
+ name: tool.name,
974
+ description: (_a = tool.description) != null ? _a : ""
975
+ };
976
+ try {
977
+ return {
978
+ ...declaration,
979
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
980
+ };
981
+ } catch (error) {
982
+ if (!isRecursiveJSONSchemaReferenceError(error)) {
983
+ throw error;
984
+ }
985
+ return {
986
+ ...declaration,
987
+ parametersJsonSchema: tool.inputSchema
988
+ };
989
+ }
990
+ }
915
991
 
916
992
  // src/map-google-generative-ai-finish-reason.ts
917
993
  function mapGoogleGenerativeAIFinishReason({