@ai-sdk/google 4.0.39 → 4.0.41

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.
@@ -46,6 +46,9 @@ function convertGoogleUsage(usage) {
46
46
  }
47
47
 
48
48
  // src/convert-json-schema-to-openapi-schema.ts
49
+ import {
50
+ UnsupportedFunctionalityError
51
+ } from "@ai-sdk/provider";
49
52
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
50
53
  if (jsonSchema == null) {
51
54
  return void 0;
@@ -80,9 +83,6 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
80
83
  if (description) result.description = description;
81
84
  if (required) result.required = required;
82
85
  if (format) result.format = format;
83
- if (constValue !== void 0) {
84
- result.enum = [constValue];
85
- }
86
86
  if (type) {
87
87
  if (Array.isArray(type)) {
88
88
  const hasNull = type.includes("null");
@@ -99,8 +99,9 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
99
99
  result.type = type;
100
100
  }
101
101
  }
102
- if (enumValues !== void 0) {
103
- result.enum = enumValues;
102
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
103
+ if (values !== void 0) {
104
+ addEnumToSchema({ values, type, result });
104
105
  }
105
106
  if (properties != null) {
106
107
  result.properties = Object.entries(properties).reduce(
@@ -157,13 +158,75 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
157
158
  }
158
159
  return result;
159
160
  }
161
+ function addEnumToSchema({
162
+ values,
163
+ type,
164
+ result
165
+ }) {
166
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
167
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
168
+ if (values.length > 0 && values.every((value) => value === null)) {
169
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
170
+ if (typeAllowsNull) {
171
+ result.type = "null";
172
+ if (Array.isArray(type)) {
173
+ delete result.anyOf;
174
+ }
175
+ return;
176
+ }
177
+ }
178
+ const enumType = getEnumType({ values: enumValues, type });
179
+ if (enumType === void 0) {
180
+ throw new UnsupportedFunctionalityError({
181
+ functionality: "JSON Schema enum with mixed or unsupported values",
182
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
183
+ });
184
+ }
185
+ result.type = enumType;
186
+ if (Array.isArray(type)) {
187
+ delete result.anyOf;
188
+ }
189
+ if (nullable) {
190
+ result.nullable = true;
191
+ }
192
+ if (enumType === "string") {
193
+ result.enum = enumValues;
194
+ } else {
195
+ result.format = "enum";
196
+ result.enum = enumValues.map(String);
197
+ }
198
+ }
199
+ function getEnumType({
200
+ values,
201
+ type
202
+ }) {
203
+ if (values.length === 0) {
204
+ return void 0;
205
+ }
206
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
207
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
208
+ return "string";
209
+ }
210
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
211
+ if (typeAllows("number")) {
212
+ return "number";
213
+ }
214
+ if (values.every((value) => Number.isInteger(value))) {
215
+ return "integer";
216
+ }
217
+ }
218
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
219
+ return "boolean";
220
+ }
221
+ return void 0;
222
+ }
160
223
  function isEmptyObjectSchema(jsonSchema) {
161
224
  return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
162
225
  }
163
226
 
164
227
  // src/convert-to-google-messages.ts
165
228
  import {
166
- UnsupportedFunctionalityError
229
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
167
230
  } from "@ai-sdk/provider";
168
231
  import {
169
232
  convertToBase64,
@@ -320,7 +383,7 @@ function convertToGoogleMessages(prompt, options) {
320
383
  switch (role) {
321
384
  case "system": {
322
385
  if (!systemMessagesAllowed) {
323
- throw new UnsupportedFunctionalityError({
386
+ throw new UnsupportedFunctionalityError2({
324
387
  functionality: "system messages are only supported at the beginning of the conversation"
325
388
  });
326
389
  }
@@ -349,7 +412,7 @@ function convertToGoogleMessages(prompt, options) {
349
412
  }
350
413
  case "reference": {
351
414
  if (isVertexLike) {
352
- throw new UnsupportedFunctionalityError({
415
+ throw new UnsupportedFunctionalityError2({
353
416
  functionality: "file parts with provider references"
354
417
  });
355
418
  }
@@ -417,7 +480,7 @@ function convertToGoogleMessages(prompt, options) {
417
480
  case "reasoning-file": {
418
481
  switch (part.data.type) {
419
482
  case "url": {
420
- throw new UnsupportedFunctionalityError({
483
+ throw new UnsupportedFunctionalityError2({
421
484
  functionality: "File data URLs in assistant messages are not supported"
422
485
  });
423
486
  }
@@ -437,13 +500,13 @@ function convertToGoogleMessages(prompt, options) {
437
500
  case "file": {
438
501
  switch (part.data.type) {
439
502
  case "url": {
440
- throw new UnsupportedFunctionalityError({
503
+ throw new UnsupportedFunctionalityError2({
441
504
  functionality: "File data URLs in assistant messages are not supported"
442
505
  });
443
506
  }
444
507
  case "reference": {
445
508
  if (isVertexLike) {
446
- throw new UnsupportedFunctionalityError({
509
+ throw new UnsupportedFunctionalityError2({
447
510
  functionality: "file parts with provider references"
448
511
  });
449
512
  }
@@ -640,7 +703,8 @@ var googleErrorDataSchema = lazySchema(
640
703
  error: z.object({
641
704
  code: z.number().nullable(),
642
705
  message: z.string(),
643
- status: z.string()
706
+ status: z.string(),
707
+ details: z.array(z.unknown()).nullish()
644
708
  })
645
709
  })
646
710
  )
@@ -864,7 +928,7 @@ function getGoogleModelCapabilities(modelId) {
864
928
 
865
929
  // src/google-prepare-tools.ts
866
930
  import {
867
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
931
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
868
932
  } from "@ai-sdk/provider";
869
933
  function prepareTools({
870
934
  tools,
@@ -1103,7 +1167,7 @@ function prepareTools({
1103
1167
  };
1104
1168
  default: {
1105
1169
  const _exhaustiveCheck = type;
1106
- throw new UnsupportedFunctionalityError2({
1170
+ throw new UnsupportedFunctionalityError3({
1107
1171
  functionality: `tool choice type: ${_exhaustiveCheck}`
1108
1172
  });
1109
1173
  }