@ai-sdk/google 3.0.110 → 3.0.111

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.
@@ -52,22 +52,41 @@ function convertGoogleGenerativeAIUsage(usage) {
52
52
  }
53
53
 
54
54
  // src/convert-json-schema-to-openapi-schema.ts
55
+ import {
56
+ UnsupportedFunctionalityError
57
+ } from "@ai-sdk/provider";
55
58
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
59
+ const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
60
+ return convertJSONSchemaDefinition(jsonSchema, isRoot, {
61
+ definitions: rootSchema == null ? void 0 : rootSchema.definitions,
62
+ dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
63
+ resolvingReferences: /* @__PURE__ */ new Set()
64
+ });
65
+ }
66
+ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
56
67
  if (jsonSchema == null) {
57
68
  return void 0;
58
69
  }
70
+ if (typeof jsonSchema === "boolean") {
71
+ return { type: "boolean", properties: {} };
72
+ }
73
+ if (jsonSchema.$ref != null) {
74
+ return convertJSONSchemaReference({
75
+ jsonSchema,
76
+ reference: jsonSchema.$ref,
77
+ isRoot,
78
+ referenceContext
79
+ });
80
+ }
59
81
  if (isEmptyObjectSchema(jsonSchema)) {
60
82
  if (isRoot) {
61
83
  return void 0;
62
84
  }
63
- if (typeof jsonSchema === "object" && jsonSchema.description) {
85
+ if (jsonSchema.description) {
64
86
  return { type: "object", description: jsonSchema.description };
65
87
  }
66
88
  return { type: "object" };
67
89
  }
68
- if (typeof jsonSchema === "boolean") {
69
- return { type: "boolean", properties: {} };
70
- }
71
90
  const {
72
91
  type,
73
92
  description,
@@ -111,18 +130,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
111
130
  if (properties != null) {
112
131
  result.properties = Object.entries(properties).reduce(
113
132
  (acc, [key, value]) => {
114
- acc[key] = convertJSONSchemaToOpenAPISchema(value, false);
133
+ acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
115
134
  return acc;
116
135
  },
117
136
  {}
118
137
  );
119
138
  }
120
139
  if (items) {
121
- result.items = Array.isArray(items) ? items.map((item) => convertJSONSchemaToOpenAPISchema(item, false)) : convertJSONSchemaToOpenAPISchema(items, false);
140
+ result.items = Array.isArray(items) ? items.map(
141
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
142
+ ) : convertJSONSchemaDefinition(items, false, referenceContext);
122
143
  }
123
144
  if (allOf) {
124
145
  result.allOf = allOf.map(
125
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
146
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
126
147
  );
127
148
  }
128
149
  if (anyOf) {
@@ -133,9 +154,10 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
133
154
  (schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
134
155
  );
135
156
  if (nonNullSchemas.length === 1) {
136
- const converted = convertJSONSchemaToOpenAPISchema(
157
+ const converted = convertJSONSchemaDefinition(
137
158
  nonNullSchemas[0],
138
- false
159
+ false,
160
+ referenceContext
139
161
  );
140
162
  if (typeof converted === "object") {
141
163
  result.nullable = true;
@@ -143,19 +165,19 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
143
165
  }
144
166
  } else {
145
167
  result.anyOf = nonNullSchemas.map(
146
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
168
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
147
169
  );
148
170
  result.nullable = true;
149
171
  }
150
172
  } else {
151
173
  result.anyOf = anyOf.map(
152
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
174
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
153
175
  );
154
176
  }
155
177
  }
156
178
  if (oneOf) {
157
179
  result.oneOf = oneOf.map(
158
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
180
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
159
181
  );
160
182
  }
161
183
  if (minLength !== void 0) {
@@ -163,13 +185,83 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
163
185
  }
164
186
  return result;
165
187
  }
188
+ function convertJSONSchemaReference({
189
+ jsonSchema,
190
+ reference,
191
+ isRoot,
192
+ referenceContext
193
+ }) {
194
+ const { definition, referenceKey } = getReferencedDefinition(
195
+ reference,
196
+ referenceContext
197
+ );
198
+ if (referenceContext.resolvingReferences.has(referenceKey)) {
199
+ throw new UnsupportedFunctionalityError({
200
+ functionality: `recursive JSON Schema reference: ${reference}`,
201
+ message: "Google schema conversion does not support recursive JSON Schema references."
202
+ });
203
+ }
204
+ const resolvingReferences = new Set(referenceContext.resolvingReferences);
205
+ resolvingReferences.add(referenceKey);
206
+ const { $ref: _reference, ...siblingSchema } = jsonSchema;
207
+ const resolvedSchema = typeof definition === "boolean" ? definition ? siblingSchema : false : { ...definition, ...siblingSchema };
208
+ return convertJSONSchemaDefinition(resolvedSchema, isRoot, {
209
+ ...referenceContext,
210
+ resolvingReferences
211
+ });
212
+ }
213
+ function getReferencedDefinition(reference, referenceContext) {
214
+ const definitionSources = [
215
+ {
216
+ prefix: "#/$defs/",
217
+ definitions: referenceContext.dollarDefinitions
218
+ },
219
+ {
220
+ prefix: "#/definitions/",
221
+ definitions: referenceContext.definitions
222
+ }
223
+ ];
224
+ const source = definitionSources.find(
225
+ ({ prefix }) => reference.startsWith(prefix)
226
+ );
227
+ const encodedDefinitionName = source ? reference.slice(source.prefix.length) : void 0;
228
+ if (source == null || encodedDefinitionName == null || encodedDefinitionName.length === 0 || encodedDefinitionName.includes("/")) {
229
+ throwUnsupportedReference(reference);
230
+ }
231
+ let decodedDefinitionName;
232
+ try {
233
+ decodedDefinitionName = decodeURIComponent(encodedDefinitionName);
234
+ } catch (e) {
235
+ throwUnsupportedReference(reference);
236
+ }
237
+ if (decodedDefinitionName.includes("/") || /~(?![01])/u.test(decodedDefinitionName) || source.definitions == null) {
238
+ throwUnsupportedReference(reference);
239
+ }
240
+ const definitionName = decodedDefinitionName.replace(
241
+ /~[01]/g,
242
+ (match) => match === "~1" ? "/" : "~"
243
+ );
244
+ if (!Object.prototype.hasOwnProperty.call(source.definitions, definitionName)) {
245
+ throwUnsupportedReference(reference);
246
+ }
247
+ return {
248
+ definition: source.definitions[definitionName],
249
+ referenceKey: `${source.prefix}${definitionName}`
250
+ };
251
+ }
252
+ function throwUnsupportedReference(reference) {
253
+ throw new UnsupportedFunctionalityError({
254
+ functionality: `JSON Schema reference: ${reference}`,
255
+ message: "Google schema conversion only supports references to direct children of root-level $defs or definitions."
256
+ });
257
+ }
166
258
  function isEmptyObjectSchema(jsonSchema) {
167
259
  return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
168
260
  }
169
261
 
170
262
  // src/convert-to-google-generative-ai-messages.ts
171
263
  import {
172
- UnsupportedFunctionalityError
264
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
173
265
  } from "@ai-sdk/provider";
174
266
  import { convertToBase64, secureJsonParse } from "@ai-sdk/provider-utils";
175
267
  var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
@@ -320,7 +412,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
320
412
  switch (role) {
321
413
  case "system": {
322
414
  if (!systemMessagesAllowed) {
323
- throw new UnsupportedFunctionalityError({
415
+ throw new UnsupportedFunctionalityError2({
324
416
  functionality: "system messages are only supported at the beginning of the conversation"
325
417
  });
326
418
  }
@@ -385,7 +477,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
385
477
  }
386
478
  case "file": {
387
479
  if (part.data instanceof URL) {
388
- throw new UnsupportedFunctionalityError({
480
+ throw new UnsupportedFunctionalityError2({
389
481
  functionality: "File data URLs in assistant messages are not supported"
390
482
  });
391
483
  }
@@ -782,7 +874,7 @@ function getGoogleModelCapabilities(modelId) {
782
874
 
783
875
  // src/google-prepare-tools.ts
784
876
  import {
785
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
877
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
786
878
  } from "@ai-sdk/provider";
787
879
  function prepareTools({
788
880
  tools,
@@ -1021,7 +1113,7 @@ function prepareTools({
1021
1113
  };
1022
1114
  default: {
1023
1115
  const _exhaustiveCheck = type;
1024
- throw new UnsupportedFunctionalityError2({
1116
+ throw new UnsupportedFunctionalityError3({
1025
1117
  functionality: `tool choice type: ${_exhaustiveCheck}`
1026
1118
  });
1027
1119
  }