@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.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +115 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -18
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +107 -17
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +109 -17
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/convert-json-schema-to-openapi-schema.ts +175 -15
package/dist/internal/index.js
CHANGED
|
@@ -72,22 +72,39 @@ function convertGoogleGenerativeAIUsage(usage) {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
// src/convert-json-schema-to-openapi-schema.ts
|
|
75
|
+
var import_provider = require("@ai-sdk/provider");
|
|
75
76
|
function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
77
|
+
const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
|
|
78
|
+
return convertJSONSchemaDefinition(jsonSchema, isRoot, {
|
|
79
|
+
definitions: rootSchema == null ? void 0 : rootSchema.definitions,
|
|
80
|
+
dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
|
|
81
|
+
resolvingReferences: /* @__PURE__ */ new Set()
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
76
85
|
if (jsonSchema == null) {
|
|
77
86
|
return void 0;
|
|
78
87
|
}
|
|
88
|
+
if (typeof jsonSchema === "boolean") {
|
|
89
|
+
return { type: "boolean", properties: {} };
|
|
90
|
+
}
|
|
91
|
+
if (jsonSchema.$ref != null) {
|
|
92
|
+
return convertJSONSchemaReference({
|
|
93
|
+
jsonSchema,
|
|
94
|
+
reference: jsonSchema.$ref,
|
|
95
|
+
isRoot,
|
|
96
|
+
referenceContext
|
|
97
|
+
});
|
|
98
|
+
}
|
|
79
99
|
if (isEmptyObjectSchema(jsonSchema)) {
|
|
80
100
|
if (isRoot) {
|
|
81
101
|
return void 0;
|
|
82
102
|
}
|
|
83
|
-
if (
|
|
103
|
+
if (jsonSchema.description) {
|
|
84
104
|
return { type: "object", description: jsonSchema.description };
|
|
85
105
|
}
|
|
86
106
|
return { type: "object" };
|
|
87
107
|
}
|
|
88
|
-
if (typeof jsonSchema === "boolean") {
|
|
89
|
-
return { type: "boolean", properties: {} };
|
|
90
|
-
}
|
|
91
108
|
const {
|
|
92
109
|
type,
|
|
93
110
|
description,
|
|
@@ -131,18 +148,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
|
131
148
|
if (properties != null) {
|
|
132
149
|
result.properties = Object.entries(properties).reduce(
|
|
133
150
|
(acc, [key, value]) => {
|
|
134
|
-
acc[key] =
|
|
151
|
+
acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
|
|
135
152
|
return acc;
|
|
136
153
|
},
|
|
137
154
|
{}
|
|
138
155
|
);
|
|
139
156
|
}
|
|
140
157
|
if (items) {
|
|
141
|
-
result.items = Array.isArray(items) ? items.map(
|
|
158
|
+
result.items = Array.isArray(items) ? items.map(
|
|
159
|
+
(item) => convertJSONSchemaDefinition(item, false, referenceContext)
|
|
160
|
+
) : convertJSONSchemaDefinition(items, false, referenceContext);
|
|
142
161
|
}
|
|
143
162
|
if (allOf) {
|
|
144
163
|
result.allOf = allOf.map(
|
|
145
|
-
(item) =>
|
|
164
|
+
(item) => convertJSONSchemaDefinition(item, false, referenceContext)
|
|
146
165
|
);
|
|
147
166
|
}
|
|
148
167
|
if (anyOf) {
|
|
@@ -153,9 +172,10 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
|
153
172
|
(schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
|
|
154
173
|
);
|
|
155
174
|
if (nonNullSchemas.length === 1) {
|
|
156
|
-
const converted =
|
|
175
|
+
const converted = convertJSONSchemaDefinition(
|
|
157
176
|
nonNullSchemas[0],
|
|
158
|
-
false
|
|
177
|
+
false,
|
|
178
|
+
referenceContext
|
|
159
179
|
);
|
|
160
180
|
if (typeof converted === "object") {
|
|
161
181
|
result.nullable = true;
|
|
@@ -163,19 +183,19 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
|
163
183
|
}
|
|
164
184
|
} else {
|
|
165
185
|
result.anyOf = nonNullSchemas.map(
|
|
166
|
-
(item) =>
|
|
186
|
+
(item) => convertJSONSchemaDefinition(item, false, referenceContext)
|
|
167
187
|
);
|
|
168
188
|
result.nullable = true;
|
|
169
189
|
}
|
|
170
190
|
} else {
|
|
171
191
|
result.anyOf = anyOf.map(
|
|
172
|
-
(item) =>
|
|
192
|
+
(item) => convertJSONSchemaDefinition(item, false, referenceContext)
|
|
173
193
|
);
|
|
174
194
|
}
|
|
175
195
|
}
|
|
176
196
|
if (oneOf) {
|
|
177
197
|
result.oneOf = oneOf.map(
|
|
178
|
-
(item) =>
|
|
198
|
+
(item) => convertJSONSchemaDefinition(item, false, referenceContext)
|
|
179
199
|
);
|
|
180
200
|
}
|
|
181
201
|
if (minLength !== void 0) {
|
|
@@ -183,12 +203,82 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
|
183
203
|
}
|
|
184
204
|
return result;
|
|
185
205
|
}
|
|
206
|
+
function convertJSONSchemaReference({
|
|
207
|
+
jsonSchema,
|
|
208
|
+
reference,
|
|
209
|
+
isRoot,
|
|
210
|
+
referenceContext
|
|
211
|
+
}) {
|
|
212
|
+
const { definition, referenceKey } = getReferencedDefinition(
|
|
213
|
+
reference,
|
|
214
|
+
referenceContext
|
|
215
|
+
);
|
|
216
|
+
if (referenceContext.resolvingReferences.has(referenceKey)) {
|
|
217
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
218
|
+
functionality: `recursive JSON Schema reference: ${reference}`,
|
|
219
|
+
message: "Google schema conversion does not support recursive JSON Schema references."
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const resolvingReferences = new Set(referenceContext.resolvingReferences);
|
|
223
|
+
resolvingReferences.add(referenceKey);
|
|
224
|
+
const { $ref: _reference, ...siblingSchema } = jsonSchema;
|
|
225
|
+
const resolvedSchema = typeof definition === "boolean" ? definition ? siblingSchema : false : { ...definition, ...siblingSchema };
|
|
226
|
+
return convertJSONSchemaDefinition(resolvedSchema, isRoot, {
|
|
227
|
+
...referenceContext,
|
|
228
|
+
resolvingReferences
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function getReferencedDefinition(reference, referenceContext) {
|
|
232
|
+
const definitionSources = [
|
|
233
|
+
{
|
|
234
|
+
prefix: "#/$defs/",
|
|
235
|
+
definitions: referenceContext.dollarDefinitions
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
prefix: "#/definitions/",
|
|
239
|
+
definitions: referenceContext.definitions
|
|
240
|
+
}
|
|
241
|
+
];
|
|
242
|
+
const source = definitionSources.find(
|
|
243
|
+
({ prefix }) => reference.startsWith(prefix)
|
|
244
|
+
);
|
|
245
|
+
const encodedDefinitionName = source ? reference.slice(source.prefix.length) : void 0;
|
|
246
|
+
if (source == null || encodedDefinitionName == null || encodedDefinitionName.length === 0 || encodedDefinitionName.includes("/")) {
|
|
247
|
+
throwUnsupportedReference(reference);
|
|
248
|
+
}
|
|
249
|
+
let decodedDefinitionName;
|
|
250
|
+
try {
|
|
251
|
+
decodedDefinitionName = decodeURIComponent(encodedDefinitionName);
|
|
252
|
+
} catch (e) {
|
|
253
|
+
throwUnsupportedReference(reference);
|
|
254
|
+
}
|
|
255
|
+
if (decodedDefinitionName.includes("/") || /~(?![01])/u.test(decodedDefinitionName) || source.definitions == null) {
|
|
256
|
+
throwUnsupportedReference(reference);
|
|
257
|
+
}
|
|
258
|
+
const definitionName = decodedDefinitionName.replace(
|
|
259
|
+
/~[01]/g,
|
|
260
|
+
(match) => match === "~1" ? "/" : "~"
|
|
261
|
+
);
|
|
262
|
+
if (!Object.prototype.hasOwnProperty.call(source.definitions, definitionName)) {
|
|
263
|
+
throwUnsupportedReference(reference);
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
definition: source.definitions[definitionName],
|
|
267
|
+
referenceKey: `${source.prefix}${definitionName}`
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
function throwUnsupportedReference(reference) {
|
|
271
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
272
|
+
functionality: `JSON Schema reference: ${reference}`,
|
|
273
|
+
message: "Google schema conversion only supports references to direct children of root-level $defs or definitions."
|
|
274
|
+
});
|
|
275
|
+
}
|
|
186
276
|
function isEmptyObjectSchema(jsonSchema) {
|
|
187
277
|
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
|
188
278
|
}
|
|
189
279
|
|
|
190
280
|
// src/convert-to-google-generative-ai-messages.ts
|
|
191
|
-
var
|
|
281
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
192
282
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
193
283
|
var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
|
|
194
284
|
function getGoogleProviderOptions(providerOptions, providerOptionsName) {
|
|
@@ -338,7 +428,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
338
428
|
switch (role) {
|
|
339
429
|
case "system": {
|
|
340
430
|
if (!systemMessagesAllowed) {
|
|
341
|
-
throw new
|
|
431
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
342
432
|
functionality: "system messages are only supported at the beginning of the conversation"
|
|
343
433
|
});
|
|
344
434
|
}
|
|
@@ -403,7 +493,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
403
493
|
}
|
|
404
494
|
case "file": {
|
|
405
495
|
if (part.data instanceof URL) {
|
|
406
|
-
throw new
|
|
496
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
407
497
|
functionality: "File data URLs in assistant messages are not supported"
|
|
408
498
|
});
|
|
409
499
|
}
|
|
@@ -792,7 +882,7 @@ function getGoogleModelCapabilities(modelId) {
|
|
|
792
882
|
}
|
|
793
883
|
|
|
794
884
|
// src/google-prepare-tools.ts
|
|
795
|
-
var
|
|
885
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
796
886
|
function prepareTools({
|
|
797
887
|
tools,
|
|
798
888
|
toolChoice,
|
|
@@ -1030,7 +1120,7 @@ function prepareTools({
|
|
|
1030
1120
|
};
|
|
1031
1121
|
default: {
|
|
1032
1122
|
const _exhaustiveCheck = type;
|
|
1033
|
-
throw new
|
|
1123
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1034
1124
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1035
1125
|
});
|
|
1036
1126
|
}
|