@ai-sdk/google 3.0.114 → 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.
@@ -109,9 +109,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
109
109
  if (description) result.description = description;
110
110
  if (required) result.required = required;
111
111
  if (format) result.format = format;
112
- if (constValue !== void 0) {
113
- result.enum = [constValue];
114
- }
115
112
  if (type) {
116
113
  if (Array.isArray(type)) {
117
114
  const hasNull = type.includes("null");
@@ -128,8 +125,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
128
125
  result.type = type;
129
126
  }
130
127
  }
131
- if (enumValues !== void 0) {
132
- result.enum = enumValues;
128
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
129
+ if (values !== void 0) {
130
+ addEnumToSchema({ values, type, result });
133
131
  }
134
132
  if (properties != null) {
135
133
  result.properties = Object.entries(properties).reduce(
@@ -189,6 +187,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
189
187
  }
190
188
  return result;
191
189
  }
190
+ function addEnumToSchema({
191
+ values,
192
+ type,
193
+ result
194
+ }) {
195
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
196
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
197
+ if (values.length > 0 && values.every((value) => value === null)) {
198
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
199
+ if (typeAllowsNull) {
200
+ result.type = "null";
201
+ if (Array.isArray(type)) {
202
+ delete result.anyOf;
203
+ }
204
+ return;
205
+ }
206
+ }
207
+ const enumType = getEnumType({ values: enumValues, type });
208
+ if (enumType === void 0) {
209
+ throw new UnsupportedFunctionalityError({
210
+ functionality: "JSON Schema enum with mixed or unsupported values",
211
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
212
+ });
213
+ }
214
+ result.type = enumType;
215
+ if (Array.isArray(type)) {
216
+ delete result.anyOf;
217
+ }
218
+ if (nullable) {
219
+ result.nullable = true;
220
+ }
221
+ if (enumType === "string") {
222
+ result.enum = enumValues;
223
+ } else {
224
+ result.format = "enum";
225
+ result.enum = enumValues.map(String);
226
+ }
227
+ }
228
+ function getEnumType({
229
+ values,
230
+ type
231
+ }) {
232
+ if (values.length === 0) {
233
+ return void 0;
234
+ }
235
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
236
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
237
+ return "string";
238
+ }
239
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
240
+ if (typeAllows("number")) {
241
+ return "number";
242
+ }
243
+ if (values.every((value) => Number.isInteger(value))) {
244
+ return "integer";
245
+ }
246
+ }
247
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
248
+ return "boolean";
249
+ }
250
+ return void 0;
251
+ }
192
252
  function convertJSONSchemaReference({
193
253
  jsonSchema,
194
254
  reference,