@ai-sdk/google 4.0.47 → 4.0.48

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.
@@ -50,21 +50,37 @@ import {
50
50
  UnsupportedFunctionalityError
51
51
  } from "@ai-sdk/provider";
52
52
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
53
+ const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
54
+ return convertJSONSchemaDefinition(jsonSchema, isRoot, {
55
+ definitions: rootSchema == null ? void 0 : rootSchema.definitions,
56
+ dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
57
+ resolvingReferences: /* @__PURE__ */ new Set()
58
+ });
59
+ }
60
+ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
53
61
  if (jsonSchema == null) {
54
62
  return void 0;
55
63
  }
64
+ if (typeof jsonSchema === "boolean") {
65
+ return { type: "boolean", properties: {} };
66
+ }
67
+ if (jsonSchema.$ref != null) {
68
+ return convertJSONSchemaReference({
69
+ jsonSchema,
70
+ reference: jsonSchema.$ref,
71
+ isRoot,
72
+ referenceContext
73
+ });
74
+ }
56
75
  if (isEmptyObjectSchema(jsonSchema)) {
57
76
  if (isRoot) {
58
77
  return void 0;
59
78
  }
60
- if (typeof jsonSchema === "object" && jsonSchema.description) {
79
+ if (jsonSchema.description) {
61
80
  return { type: "object", description: jsonSchema.description };
62
81
  }
63
82
  return { type: "object" };
64
83
  }
65
- if (typeof jsonSchema === "boolean") {
66
- return { type: "boolean", properties: {} };
67
- }
68
84
  const {
69
85
  type,
70
86
  description,
@@ -106,18 +122,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
106
122
  if (properties != null) {
107
123
  result.properties = Object.entries(properties).reduce(
108
124
  (acc, [key, value]) => {
109
- acc[key] = convertJSONSchemaToOpenAPISchema(value, false);
125
+ acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
110
126
  return acc;
111
127
  },
112
128
  {}
113
129
  );
114
130
  }
115
131
  if (items) {
116
- result.items = Array.isArray(items) ? items.map((item) => convertJSONSchemaToOpenAPISchema(item, false)) : convertJSONSchemaToOpenAPISchema(items, false);
132
+ result.items = Array.isArray(items) ? items.map(
133
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
134
+ ) : convertJSONSchemaDefinition(items, false, referenceContext);
117
135
  }
118
136
  if (allOf) {
119
137
  result.allOf = allOf.map(
120
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
138
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
121
139
  );
122
140
  }
123
141
  if (anyOf) {
@@ -128,9 +146,10 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
128
146
  (schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
129
147
  );
130
148
  if (nonNullSchemas.length === 1) {
131
- const converted = convertJSONSchemaToOpenAPISchema(
149
+ const converted = convertJSONSchemaDefinition(
132
150
  nonNullSchemas[0],
133
- false
151
+ false,
152
+ referenceContext
134
153
  );
135
154
  if (typeof converted === "object") {
136
155
  result.nullable = true;
@@ -138,19 +157,19 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
138
157
  }
139
158
  } else {
140
159
  result.anyOf = nonNullSchemas.map(
141
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
160
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
142
161
  );
143
162
  result.nullable = true;
144
163
  }
145
164
  } else {
146
165
  result.anyOf = anyOf.map(
147
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
166
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
148
167
  );
149
168
  }
150
169
  }
151
170
  if (oneOf) {
152
171
  result.oneOf = oneOf.map(
153
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
172
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
154
173
  );
155
174
  }
156
175
  if (minLength !== void 0) {
@@ -158,6 +177,76 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
158
177
  }
159
178
  return result;
160
179
  }
180
+ function convertJSONSchemaReference({
181
+ jsonSchema,
182
+ reference,
183
+ isRoot,
184
+ referenceContext
185
+ }) {
186
+ const { definition, referenceKey } = getReferencedDefinition(
187
+ reference,
188
+ referenceContext
189
+ );
190
+ if (referenceContext.resolvingReferences.has(referenceKey)) {
191
+ throw new UnsupportedFunctionalityError({
192
+ functionality: `recursive JSON Schema reference: ${reference}`,
193
+ message: "Google schema conversion does not support recursive JSON Schema references."
194
+ });
195
+ }
196
+ const resolvingReferences = new Set(referenceContext.resolvingReferences);
197
+ resolvingReferences.add(referenceKey);
198
+ const { $ref: _reference, ...siblingSchema } = jsonSchema;
199
+ const resolvedSchema = typeof definition === "boolean" ? definition ? siblingSchema : false : { ...definition, ...siblingSchema };
200
+ return convertJSONSchemaDefinition(resolvedSchema, isRoot, {
201
+ ...referenceContext,
202
+ resolvingReferences
203
+ });
204
+ }
205
+ function getReferencedDefinition(reference, referenceContext) {
206
+ const definitionSources = [
207
+ {
208
+ prefix: "#/$defs/",
209
+ definitions: referenceContext.dollarDefinitions
210
+ },
211
+ {
212
+ prefix: "#/definitions/",
213
+ definitions: referenceContext.definitions
214
+ }
215
+ ];
216
+ const source = definitionSources.find(
217
+ ({ prefix }) => reference.startsWith(prefix)
218
+ );
219
+ const encodedDefinitionName = source ? reference.slice(source.prefix.length) : void 0;
220
+ if (source == null || encodedDefinitionName == null || encodedDefinitionName.length === 0 || encodedDefinitionName.includes("/")) {
221
+ throwUnsupportedReference(reference);
222
+ }
223
+ let decodedDefinitionName;
224
+ try {
225
+ decodedDefinitionName = decodeURIComponent(encodedDefinitionName);
226
+ } catch (e) {
227
+ throwUnsupportedReference(reference);
228
+ }
229
+ if (decodedDefinitionName.includes("/") || /~(?![01])/u.test(decodedDefinitionName) || source.definitions == null) {
230
+ throwUnsupportedReference(reference);
231
+ }
232
+ const definitionName = decodedDefinitionName.replace(
233
+ /~[01]/g,
234
+ (match) => match === "~1" ? "/" : "~"
235
+ );
236
+ if (!Object.prototype.hasOwnProperty.call(source.definitions, definitionName)) {
237
+ throwUnsupportedReference(reference);
238
+ }
239
+ return {
240
+ definition: source.definitions[definitionName],
241
+ referenceKey: `${source.prefix}${definitionName}`
242
+ };
243
+ }
244
+ function throwUnsupportedReference(reference) {
245
+ throw new UnsupportedFunctionalityError({
246
+ functionality: `JSON Schema reference: ${reference}`,
247
+ message: "Google schema conversion only supports references to direct children of root-level $defs or definitions."
248
+ });
249
+ }
161
250
  function addEnumToSchema({
162
251
  values,
163
252
  type,