@ai-sdk/google 2.0.89 → 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.
@@ -33,6 +33,10 @@ var import_v43 = require("zod/v4");
33
33
 
34
34
  // src/convert-json-schema-to-openapi-schema.ts
35
35
  var import_provider = require("@ai-sdk/provider");
36
+ var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
37
+ function isRecursiveJSONSchemaReferenceError(error) {
38
+ return import_provider.UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
39
+ }
36
40
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
37
41
  const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
38
42
  return convertJSONSchemaDefinition(jsonSchema, isRoot, {
@@ -83,9 +87,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
83
87
  if (description) result.description = description;
84
88
  if (required) result.required = required;
85
89
  if (format) result.format = format;
86
- if (constValue !== void 0) {
87
- result.enum = [constValue];
88
- }
89
90
  if (type) {
90
91
  if (Array.isArray(type)) {
91
92
  const hasNull = type.includes("null");
@@ -102,8 +103,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
102
103
  result.type = type;
103
104
  }
104
105
  }
105
- if (enumValues !== void 0) {
106
- result.enum = enumValues;
106
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
107
+ if (values !== void 0) {
108
+ addEnumToSchema({ values, type, result });
107
109
  }
108
110
  if (properties != null) {
109
111
  result.properties = Object.entries(properties).reduce(
@@ -163,6 +165,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
163
165
  }
164
166
  return result;
165
167
  }
168
+ function addEnumToSchema({
169
+ values,
170
+ type,
171
+ result
172
+ }) {
173
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
174
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
175
+ if (values.length > 0 && values.every((value) => value === null)) {
176
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
177
+ if (typeAllowsNull) {
178
+ result.type = "null";
179
+ if (Array.isArray(type)) {
180
+ delete result.anyOf;
181
+ }
182
+ return;
183
+ }
184
+ }
185
+ const enumType = getEnumType({ values: enumValues, type });
186
+ if (enumType === void 0) {
187
+ throw new import_provider.UnsupportedFunctionalityError({
188
+ functionality: "JSON Schema enum with mixed or unsupported values",
189
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
190
+ });
191
+ }
192
+ result.type = enumType;
193
+ if (Array.isArray(type)) {
194
+ delete result.anyOf;
195
+ }
196
+ if (nullable) {
197
+ result.nullable = true;
198
+ }
199
+ if (enumType === "string") {
200
+ result.enum = enumValues;
201
+ } else {
202
+ result.format = "enum";
203
+ result.enum = enumValues.map(String);
204
+ }
205
+ }
206
+ function getEnumType({
207
+ values,
208
+ type
209
+ }) {
210
+ if (values.length === 0) {
211
+ return void 0;
212
+ }
213
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
214
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
215
+ return "string";
216
+ }
217
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
218
+ if (typeAllows("number")) {
219
+ return "number";
220
+ }
221
+ if (values.every((value) => Number.isInteger(value))) {
222
+ return "integer";
223
+ }
224
+ }
225
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
226
+ return "boolean";
227
+ }
228
+ return void 0;
229
+ }
166
230
  function convertJSONSchemaReference({
167
231
  jsonSchema,
168
232
  reference,
@@ -175,7 +239,7 @@ function convertJSONSchemaReference({
175
239
  );
176
240
  if (referenceContext.resolvingReferences.has(referenceKey)) {
177
241
  throw new import_provider.UnsupportedFunctionalityError({
178
- functionality: `recursive JSON Schema reference: ${reference}`,
242
+ functionality: `${recursiveReferenceFunctionalityPrefix} ${reference}`,
179
243
  message: "Google schema conversion does not support recursive JSON Schema references."
180
244
  });
181
245
  }
@@ -681,7 +745,6 @@ function prepareTools({
681
745
  modelId,
682
746
  isVertexProvider = false
683
747
  }) {
684
- var _a, _b;
685
748
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
686
749
  const toolWarnings = [];
687
750
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -802,11 +865,7 @@ function prepareTools({
802
865
  const functionDeclarations2 = [];
803
866
  for (const tool of tools) {
804
867
  if (tool.type === "function") {
805
- functionDeclarations2.push({
806
- name: tool.name,
807
- description: (_a = tool.description) != null ? _a : "",
808
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
809
- });
868
+ functionDeclarations2.push(prepareFunctionDeclaration(tool));
810
869
  }
811
870
  }
812
871
  const combinedToolConfig = {
@@ -850,11 +909,7 @@ function prepareTools({
850
909
  for (const tool of tools) {
851
910
  switch (tool.type) {
852
911
  case "function":
853
- functionDeclarations.push({
854
- name: tool.name,
855
- description: (_b = tool.description) != null ? _b : "",
856
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
857
- });
912
+ functionDeclarations.push(prepareFunctionDeclaration(tool));
858
913
  if (tool.strict === true) {
859
914
  hasStrictTools = true;
860
915
  }
@@ -918,6 +973,27 @@ function prepareTools({
918
973
  }
919
974
  }
920
975
  }
976
+ function prepareFunctionDeclaration(tool) {
977
+ var _a;
978
+ const declaration = {
979
+ name: tool.name,
980
+ description: (_a = tool.description) != null ? _a : ""
981
+ };
982
+ try {
983
+ return {
984
+ ...declaration,
985
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
986
+ };
987
+ } catch (error) {
988
+ if (!isRecursiveJSONSchemaReferenceError(error)) {
989
+ throw error;
990
+ }
991
+ return {
992
+ ...declaration,
993
+ parametersJsonSchema: tool.inputSchema
994
+ };
995
+ }
996
+ }
921
997
 
922
998
  // src/map-google-generative-ai-finish-reason.ts
923
999
  function mapGoogleGenerativeAIFinishReason({