@ai-sdk/google 3.0.113 → 3.0.116

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.
@@ -73,6 +73,10 @@ function convertGoogleGenerativeAIUsage(usage) {
73
73
 
74
74
  // src/convert-json-schema-to-openapi-schema.ts
75
75
  var import_provider = require("@ai-sdk/provider");
76
+ var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
77
+ function isRecursiveJSONSchemaReferenceError(error) {
78
+ return import_provider.UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
79
+ }
76
80
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
77
81
  const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
78
82
  return convertJSONSchemaDefinition(jsonSchema, isRoot, {
@@ -123,9 +127,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
123
127
  if (description) result.description = description;
124
128
  if (required) result.required = required;
125
129
  if (format) result.format = format;
126
- if (constValue !== void 0) {
127
- result.enum = [constValue];
128
- }
129
130
  if (type) {
130
131
  if (Array.isArray(type)) {
131
132
  const hasNull = type.includes("null");
@@ -142,8 +143,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
142
143
  result.type = type;
143
144
  }
144
145
  }
145
- if (enumValues !== void 0) {
146
- result.enum = enumValues;
146
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
147
+ if (values !== void 0) {
148
+ addEnumToSchema({ values, type, result });
147
149
  }
148
150
  if (properties != null) {
149
151
  result.properties = Object.entries(properties).reduce(
@@ -203,6 +205,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
203
205
  }
204
206
  return result;
205
207
  }
208
+ function addEnumToSchema({
209
+ values,
210
+ type,
211
+ result
212
+ }) {
213
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
214
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
215
+ if (values.length > 0 && values.every((value) => value === null)) {
216
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
217
+ if (typeAllowsNull) {
218
+ result.type = "null";
219
+ if (Array.isArray(type)) {
220
+ delete result.anyOf;
221
+ }
222
+ return;
223
+ }
224
+ }
225
+ const enumType = getEnumType({ values: enumValues, type });
226
+ if (enumType === void 0) {
227
+ throw new import_provider.UnsupportedFunctionalityError({
228
+ functionality: "JSON Schema enum with mixed or unsupported values",
229
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
230
+ });
231
+ }
232
+ result.type = enumType;
233
+ if (Array.isArray(type)) {
234
+ delete result.anyOf;
235
+ }
236
+ if (nullable) {
237
+ result.nullable = true;
238
+ }
239
+ if (enumType === "string") {
240
+ result.enum = enumValues;
241
+ } else {
242
+ result.format = "enum";
243
+ result.enum = enumValues.map(String);
244
+ }
245
+ }
246
+ function getEnumType({
247
+ values,
248
+ type
249
+ }) {
250
+ if (values.length === 0) {
251
+ return void 0;
252
+ }
253
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
254
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
255
+ return "string";
256
+ }
257
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
258
+ if (typeAllows("number")) {
259
+ return "number";
260
+ }
261
+ if (values.every((value) => Number.isInteger(value))) {
262
+ return "integer";
263
+ }
264
+ }
265
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
266
+ return "boolean";
267
+ }
268
+ return void 0;
269
+ }
206
270
  function convertJSONSchemaReference({
207
271
  jsonSchema,
208
272
  reference,
@@ -215,7 +279,7 @@ function convertJSONSchemaReference({
215
279
  );
216
280
  if (referenceContext.resolvingReferences.has(referenceKey)) {
217
281
  throw new import_provider.UnsupportedFunctionalityError({
218
- functionality: `recursive JSON Schema reference: ${reference}`,
282
+ functionality: `${recursiveReferenceFunctionalityPrefix} ${reference}`,
219
283
  message: "Google schema conversion does not support recursive JSON Schema references."
220
284
  });
221
285
  }
@@ -889,7 +953,6 @@ function prepareTools({
889
953
  modelId,
890
954
  isVertexProvider = false
891
955
  }) {
892
- var _a, _b;
893
956
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
894
957
  const toolWarnings = [];
895
958
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -1007,11 +1070,7 @@ function prepareTools({
1007
1070
  const functionDeclarations2 = [];
1008
1071
  for (const tool of tools) {
1009
1072
  if (tool.type === "function") {
1010
- functionDeclarations2.push({
1011
- name: tool.name,
1012
- description: (_a = tool.description) != null ? _a : "",
1013
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1014
- });
1073
+ functionDeclarations2.push(prepareFunctionDeclaration(tool));
1015
1074
  }
1016
1075
  }
1017
1076
  const combinedToolConfig = {
@@ -1055,11 +1114,7 @@ function prepareTools({
1055
1114
  for (const tool of tools) {
1056
1115
  switch (tool.type) {
1057
1116
  case "function":
1058
- functionDeclarations.push({
1059
- name: tool.name,
1060
- description: (_b = tool.description) != null ? _b : "",
1061
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1062
- });
1117
+ functionDeclarations.push(prepareFunctionDeclaration(tool));
1063
1118
  if (tool.strict === true) {
1064
1119
  hasStrictTools = true;
1065
1120
  }
@@ -1126,6 +1181,27 @@ function prepareTools({
1126
1181
  }
1127
1182
  }
1128
1183
  }
1184
+ function prepareFunctionDeclaration(tool) {
1185
+ var _a;
1186
+ const declaration = {
1187
+ name: tool.name,
1188
+ description: (_a = tool.description) != null ? _a : ""
1189
+ };
1190
+ try {
1191
+ return {
1192
+ ...declaration,
1193
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1194
+ };
1195
+ } catch (error) {
1196
+ if (!isRecursiveJSONSchemaReferenceError(error)) {
1197
+ throw error;
1198
+ }
1199
+ return {
1200
+ ...declaration,
1201
+ parametersJsonSchema: tool.inputSchema
1202
+ };
1203
+ }
1204
+ }
1129
1205
 
1130
1206
  // src/google-json-accumulator.ts
1131
1207
  var GoogleJSONAccumulator = class {