@ai-sdk/google 2.0.87 → 2.0.89

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.
@@ -13,22 +13,41 @@ import {
13
13
  import { z as z3 } from "zod/v4";
14
14
 
15
15
  // src/convert-json-schema-to-openapi-schema.ts
16
+ import {
17
+ UnsupportedFunctionalityError
18
+ } from "@ai-sdk/provider";
16
19
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
20
+ const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
21
+ return convertJSONSchemaDefinition(jsonSchema, isRoot, {
22
+ definitions: rootSchema == null ? void 0 : rootSchema.definitions,
23
+ dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
24
+ resolvingReferences: /* @__PURE__ */ new Set()
25
+ });
26
+ }
27
+ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
17
28
  if (jsonSchema == null) {
18
29
  return void 0;
19
30
  }
31
+ if (typeof jsonSchema === "boolean") {
32
+ return { type: "boolean", properties: {} };
33
+ }
34
+ if (jsonSchema.$ref != null) {
35
+ return convertJSONSchemaReference({
36
+ jsonSchema,
37
+ reference: jsonSchema.$ref,
38
+ isRoot,
39
+ referenceContext
40
+ });
41
+ }
20
42
  if (isEmptyObjectSchema(jsonSchema)) {
21
43
  if (isRoot) {
22
44
  return void 0;
23
45
  }
24
- if (typeof jsonSchema === "object" && jsonSchema.description) {
46
+ if (jsonSchema.description) {
25
47
  return { type: "object", description: jsonSchema.description };
26
48
  }
27
49
  return { type: "object" };
28
50
  }
29
- if (typeof jsonSchema === "boolean") {
30
- return { type: "boolean", properties: {} };
31
- }
32
51
  const {
33
52
  type,
34
53
  description,
@@ -72,18 +91,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
72
91
  if (properties != null) {
73
92
  result.properties = Object.entries(properties).reduce(
74
93
  (acc, [key, value]) => {
75
- acc[key] = convertJSONSchemaToOpenAPISchema(value, false);
94
+ acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
76
95
  return acc;
77
96
  },
78
97
  {}
79
98
  );
80
99
  }
81
100
  if (items) {
82
- result.items = Array.isArray(items) ? items.map((item) => convertJSONSchemaToOpenAPISchema(item, false)) : convertJSONSchemaToOpenAPISchema(items, false);
101
+ result.items = Array.isArray(items) ? items.map(
102
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
103
+ ) : convertJSONSchemaDefinition(items, false, referenceContext);
83
104
  }
84
105
  if (allOf) {
85
106
  result.allOf = allOf.map(
86
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
107
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
87
108
  );
88
109
  }
89
110
  if (anyOf) {
@@ -94,9 +115,10 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
94
115
  (schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
95
116
  );
96
117
  if (nonNullSchemas.length === 1) {
97
- const converted = convertJSONSchemaToOpenAPISchema(
118
+ const converted = convertJSONSchemaDefinition(
98
119
  nonNullSchemas[0],
99
- false
120
+ false,
121
+ referenceContext
100
122
  );
101
123
  if (typeof converted === "object") {
102
124
  result.nullable = true;
@@ -104,19 +126,19 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
104
126
  }
105
127
  } else {
106
128
  result.anyOf = nonNullSchemas.map(
107
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
129
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
108
130
  );
109
131
  result.nullable = true;
110
132
  }
111
133
  } else {
112
134
  result.anyOf = anyOf.map(
113
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
135
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
114
136
  );
115
137
  }
116
138
  }
117
139
  if (oneOf) {
118
140
  result.oneOf = oneOf.map(
119
- (item) => convertJSONSchemaToOpenAPISchema(item, false)
141
+ (item) => convertJSONSchemaDefinition(item, false, referenceContext)
120
142
  );
121
143
  }
122
144
  if (minLength !== void 0) {
@@ -124,13 +146,83 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
124
146
  }
125
147
  return result;
126
148
  }
149
+ function convertJSONSchemaReference({
150
+ jsonSchema,
151
+ reference,
152
+ isRoot,
153
+ referenceContext
154
+ }) {
155
+ const { definition, referenceKey } = getReferencedDefinition(
156
+ reference,
157
+ referenceContext
158
+ );
159
+ if (referenceContext.resolvingReferences.has(referenceKey)) {
160
+ throw new UnsupportedFunctionalityError({
161
+ functionality: `recursive JSON Schema reference: ${reference}`,
162
+ message: "Google schema conversion does not support recursive JSON Schema references."
163
+ });
164
+ }
165
+ const resolvingReferences = new Set(referenceContext.resolvingReferences);
166
+ resolvingReferences.add(referenceKey);
167
+ const { $ref: _reference, ...siblingSchema } = jsonSchema;
168
+ const resolvedSchema = typeof definition === "boolean" ? definition ? siblingSchema : false : { ...definition, ...siblingSchema };
169
+ return convertJSONSchemaDefinition(resolvedSchema, isRoot, {
170
+ ...referenceContext,
171
+ resolvingReferences
172
+ });
173
+ }
174
+ function getReferencedDefinition(reference, referenceContext) {
175
+ const definitionSources = [
176
+ {
177
+ prefix: "#/$defs/",
178
+ definitions: referenceContext.dollarDefinitions
179
+ },
180
+ {
181
+ prefix: "#/definitions/",
182
+ definitions: referenceContext.definitions
183
+ }
184
+ ];
185
+ const source = definitionSources.find(
186
+ ({ prefix }) => reference.startsWith(prefix)
187
+ );
188
+ const encodedDefinitionName = source ? reference.slice(source.prefix.length) : void 0;
189
+ if (source == null || encodedDefinitionName == null || encodedDefinitionName.length === 0 || encodedDefinitionName.includes("/")) {
190
+ throwUnsupportedReference(reference);
191
+ }
192
+ let decodedDefinitionName;
193
+ try {
194
+ decodedDefinitionName = decodeURIComponent(encodedDefinitionName);
195
+ } catch (e) {
196
+ throwUnsupportedReference(reference);
197
+ }
198
+ if (decodedDefinitionName.includes("/") || /~(?![01])/u.test(decodedDefinitionName) || source.definitions == null) {
199
+ throwUnsupportedReference(reference);
200
+ }
201
+ const definitionName = decodedDefinitionName.replace(
202
+ /~[01]/g,
203
+ (match) => match === "~1" ? "/" : "~"
204
+ );
205
+ if (!Object.prototype.hasOwnProperty.call(source.definitions, definitionName)) {
206
+ throwUnsupportedReference(reference);
207
+ }
208
+ return {
209
+ definition: source.definitions[definitionName],
210
+ referenceKey: `${source.prefix}${definitionName}`
211
+ };
212
+ }
213
+ function throwUnsupportedReference(reference) {
214
+ throw new UnsupportedFunctionalityError({
215
+ functionality: `JSON Schema reference: ${reference}`,
216
+ message: "Google schema conversion only supports references to direct children of root-level $defs or definitions."
217
+ });
218
+ }
127
219
  function isEmptyObjectSchema(jsonSchema) {
128
220
  return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
129
221
  }
130
222
 
131
223
  // src/convert-to-google-generative-ai-messages.ts
132
224
  import {
133
- UnsupportedFunctionalityError
225
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
134
226
  } from "@ai-sdk/provider";
135
227
  import { convertToBase64 } from "@ai-sdk/provider-utils";
136
228
  var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
@@ -146,7 +238,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
146
238
  switch (role) {
147
239
  case "system": {
148
240
  if (!systemMessagesAllowed) {
149
- throw new UnsupportedFunctionalityError({
241
+ throw new UnsupportedFunctionalityError2({
150
242
  functionality: "system messages are only supported at the beginning of the conversation"
151
243
  });
152
244
  }
@@ -208,12 +300,12 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
208
300
  }
209
301
  case "file": {
210
302
  if (part.mediaType !== "image/png") {
211
- throw new UnsupportedFunctionalityError({
303
+ throw new UnsupportedFunctionalityError2({
212
304
  functionality: "Only PNG images are supported in assistant messages"
213
305
  });
214
306
  }
215
307
  if (part.data instanceof URL) {
216
- throw new UnsupportedFunctionalityError({
308
+ throw new UnsupportedFunctionalityError2({
217
309
  functionality: "File data URLs in assistant messages are not supported"
218
310
  });
219
311
  }
@@ -575,7 +667,7 @@ function getGoogleModelCapabilities(modelId) {
575
667
 
576
668
  // src/google-prepare-tools.ts
577
669
  import {
578
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
670
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
579
671
  } from "@ai-sdk/provider";
580
672
  function prepareTools({
581
673
  tools,
@@ -814,7 +906,7 @@ function prepareTools({
814
906
  };
815
907
  default: {
816
908
  const _exhaustiveCheck = type;
817
- throw new UnsupportedFunctionalityError2({
909
+ throw new UnsupportedFunctionalityError3({
818
910
  functionality: `tool choice type: ${_exhaustiveCheck}`
819
911
  });
820
912
  }