@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.
@@ -55,6 +55,10 @@ function convertGoogleGenerativeAIUsage(usage) {
55
55
  import {
56
56
  UnsupportedFunctionalityError
57
57
  } from "@ai-sdk/provider";
58
+ var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
59
+ function isRecursiveJSONSchemaReferenceError(error) {
60
+ return UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
61
+ }
58
62
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
59
63
  const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
60
64
  return convertJSONSchemaDefinition(jsonSchema, isRoot, {
@@ -105,9 +109,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
105
109
  if (description) result.description = description;
106
110
  if (required) result.required = required;
107
111
  if (format) result.format = format;
108
- if (constValue !== void 0) {
109
- result.enum = [constValue];
110
- }
111
112
  if (type) {
112
113
  if (Array.isArray(type)) {
113
114
  const hasNull = type.includes("null");
@@ -124,8 +125,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
124
125
  result.type = type;
125
126
  }
126
127
  }
127
- if (enumValues !== void 0) {
128
- 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 });
129
131
  }
130
132
  if (properties != null) {
131
133
  result.properties = Object.entries(properties).reduce(
@@ -185,6 +187,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
185
187
  }
186
188
  return result;
187
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
+ }
188
252
  function convertJSONSchemaReference({
189
253
  jsonSchema,
190
254
  reference,
@@ -197,7 +261,7 @@ function convertJSONSchemaReference({
197
261
  );
198
262
  if (referenceContext.resolvingReferences.has(referenceKey)) {
199
263
  throw new UnsupportedFunctionalityError({
200
- functionality: `recursive JSON Schema reference: ${reference}`,
264
+ functionality: `${recursiveReferenceFunctionalityPrefix} ${reference}`,
201
265
  message: "Google schema conversion does not support recursive JSON Schema references."
202
266
  });
203
267
  }
@@ -882,7 +946,6 @@ function prepareTools({
882
946
  modelId,
883
947
  isVertexProvider = false
884
948
  }) {
885
- var _a, _b;
886
949
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
887
950
  const toolWarnings = [];
888
951
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -1000,11 +1063,7 @@ function prepareTools({
1000
1063
  const functionDeclarations2 = [];
1001
1064
  for (const tool of tools) {
1002
1065
  if (tool.type === "function") {
1003
- functionDeclarations2.push({
1004
- name: tool.name,
1005
- description: (_a = tool.description) != null ? _a : "",
1006
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1007
- });
1066
+ functionDeclarations2.push(prepareFunctionDeclaration(tool));
1008
1067
  }
1009
1068
  }
1010
1069
  const combinedToolConfig = {
@@ -1048,11 +1107,7 @@ function prepareTools({
1048
1107
  for (const tool of tools) {
1049
1108
  switch (tool.type) {
1050
1109
  case "function":
1051
- functionDeclarations.push({
1052
- name: tool.name,
1053
- description: (_b = tool.description) != null ? _b : "",
1054
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1055
- });
1110
+ functionDeclarations.push(prepareFunctionDeclaration(tool));
1056
1111
  if (tool.strict === true) {
1057
1112
  hasStrictTools = true;
1058
1113
  }
@@ -1119,6 +1174,27 @@ function prepareTools({
1119
1174
  }
1120
1175
  }
1121
1176
  }
1177
+ function prepareFunctionDeclaration(tool) {
1178
+ var _a;
1179
+ const declaration = {
1180
+ name: tool.name,
1181
+ description: (_a = tool.description) != null ? _a : ""
1182
+ };
1183
+ try {
1184
+ return {
1185
+ ...declaration,
1186
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1187
+ };
1188
+ } catch (error) {
1189
+ if (!isRecursiveJSONSchemaReferenceError(error)) {
1190
+ throw error;
1191
+ }
1192
+ return {
1193
+ ...declaration,
1194
+ parametersJsonSchema: tool.inputSchema
1195
+ };
1196
+ }
1197
+ }
1122
1198
 
1123
1199
  // src/google-json-accumulator.ts
1124
1200
  var GoogleJSONAccumulator = class {