@mastra/server 0.20.1-alpha.3 → 0.20.1
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 +20 -0
- package/dist/{chunk-TMU7NSW7.js → chunk-HKW2536J.js} +5852 -2557
- package/dist/chunk-HKW2536J.js.map +1 -0
- package/dist/{chunk-AFJBHPHA.cjs → chunk-KJJA7GPJ.cjs} +6308 -2994
- package/dist/chunk-KJJA7GPJ.cjs.map +1 -0
- package/dist/server/handlers/agent-builder.cjs +16 -16
- package/dist/server/handlers/agent-builder.js +1 -1
- package/dist/server/handlers.cjs +2 -2
- package/dist/server/handlers.js +1 -1
- package/package.json +6 -6
- package/dist/chunk-44GFD2TF.js +0 -419
- package/dist/chunk-44GFD2TF.js.map +0 -1
- package/dist/chunk-75KU7JB6.cjs +0 -588
- package/dist/chunk-75KU7JB6.cjs.map +0 -1
- package/dist/chunk-77TGJGDW.cjs +0 -1279
- package/dist/chunk-77TGJGDW.cjs.map +0 -1
- package/dist/chunk-AFJBHPHA.cjs.map +0 -1
- package/dist/chunk-CHDN4NEY.js +0 -1277
- package/dist/chunk-CHDN4NEY.js.map +0 -1
- package/dist/chunk-EAIAF7Z6.js +0 -571
- package/dist/chunk-EAIAF7Z6.js.map +0 -1
- package/dist/chunk-TMU7NSW7.js.map +0 -1
- package/dist/chunk-WO2SYFUI.js +0 -5945
- package/dist/chunk-WO2SYFUI.js.map +0 -1
- package/dist/chunk-XCR65STK.cjs +0 -433
- package/dist/chunk-XCR65STK.cjs.map +0 -1
- package/dist/chunk-YWACVZRO.cjs +0 -5985
- package/dist/chunk-YWACVZRO.cjs.map +0 -1
- package/dist/dist-36GPHJSB.cjs +0 -2014
- package/dist/dist-36GPHJSB.cjs.map +0 -1
- package/dist/dist-3WEYC4YO.js +0 -2007
- package/dist/dist-3WEYC4YO.js.map +0 -1
- package/dist/dist-7MBYKZSM.js +0 -846
- package/dist/dist-7MBYKZSM.js.map +0 -1
- package/dist/dist-FGYSUA65.cjs +0 -935
- package/dist/dist-FGYSUA65.cjs.map +0 -1
- package/dist/dist-FNKPY5I5.cjs +0 -1412
- package/dist/dist-FNKPY5I5.cjs.map +0 -1
- package/dist/dist-H5ZHQKYG.js +0 -3
- package/dist/dist-H5ZHQKYG.js.map +0 -1
- package/dist/dist-HBUYSRRO.cjs +0 -850
- package/dist/dist-HBUYSRRO.cjs.map +0 -1
- package/dist/dist-IUCBLZK6.js +0 -1409
- package/dist/dist-IUCBLZK6.js.map +0 -1
- package/dist/dist-M4HXCUXC.cjs +0 -20
- package/dist/dist-M4HXCUXC.cjs.map +0 -1
- package/dist/dist-P32YPL35.js +0 -932
- package/dist/dist-P32YPL35.js.map +0 -1
package/dist/dist-IUCBLZK6.js
DELETED
|
@@ -1,1409 +0,0 @@
|
|
|
1
|
-
import { createJsonErrorResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, withoutTrailingSlash, generateId, loadApiKey, parseProviderOptions, combineHeaders, resolve, postJsonToApi, createJsonResponseHandler, createEventSourceResponseHandler, convertToBase64 } from './chunk-EAIAF7Z6.js';
|
|
2
|
-
import { UnsupportedFunctionalityError, TooManyEmbeddingValuesForCallError } from './chunk-44GFD2TF.js';
|
|
3
|
-
import { z } from 'zod/v4';
|
|
4
|
-
|
|
5
|
-
var googleErrorDataSchema = z.object({
|
|
6
|
-
error: z.object({
|
|
7
|
-
code: z.number().nullable(),
|
|
8
|
-
message: z.string(),
|
|
9
|
-
status: z.string()
|
|
10
|
-
})
|
|
11
|
-
});
|
|
12
|
-
var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
13
|
-
errorSchema: googleErrorDataSchema,
|
|
14
|
-
errorToMessage: (data) => data.error.message
|
|
15
|
-
});
|
|
16
|
-
var googleGenerativeAIEmbeddingProviderOptions = z.object({
|
|
17
|
-
/**
|
|
18
|
-
* Optional. Optional reduced dimension for the output embedding.
|
|
19
|
-
* If set, excessive values in the output embedding are truncated from the end.
|
|
20
|
-
*/
|
|
21
|
-
outputDimensionality: z.number().optional(),
|
|
22
|
-
/**
|
|
23
|
-
* Optional. Specifies the task type for generating embeddings.
|
|
24
|
-
* Supported task types:
|
|
25
|
-
* - SEMANTIC_SIMILARITY: Optimized for text similarity.
|
|
26
|
-
* - CLASSIFICATION: Optimized for text classification.
|
|
27
|
-
* - CLUSTERING: Optimized for clustering texts based on similarity.
|
|
28
|
-
* - RETRIEVAL_DOCUMENT: Optimized for document retrieval.
|
|
29
|
-
* - RETRIEVAL_QUERY: Optimized for query-based retrieval.
|
|
30
|
-
* - QUESTION_ANSWERING: Optimized for answering questions.
|
|
31
|
-
* - FACT_VERIFICATION: Optimized for verifying factual information.
|
|
32
|
-
* - CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
|
|
33
|
-
*/
|
|
34
|
-
taskType: z.enum([
|
|
35
|
-
"SEMANTIC_SIMILARITY",
|
|
36
|
-
"CLASSIFICATION",
|
|
37
|
-
"CLUSTERING",
|
|
38
|
-
"RETRIEVAL_DOCUMENT",
|
|
39
|
-
"RETRIEVAL_QUERY",
|
|
40
|
-
"QUESTION_ANSWERING",
|
|
41
|
-
"FACT_VERIFICATION",
|
|
42
|
-
"CODE_RETRIEVAL_QUERY"
|
|
43
|
-
]).optional()
|
|
44
|
-
});
|
|
45
|
-
var GoogleGenerativeAIEmbeddingModel = class {
|
|
46
|
-
constructor(modelId, config) {
|
|
47
|
-
this.specificationVersion = "v2";
|
|
48
|
-
this.maxEmbeddingsPerCall = 2048;
|
|
49
|
-
this.supportsParallelCalls = true;
|
|
50
|
-
this.modelId = modelId;
|
|
51
|
-
this.config = config;
|
|
52
|
-
}
|
|
53
|
-
get provider() {
|
|
54
|
-
return this.config.provider;
|
|
55
|
-
}
|
|
56
|
-
async doEmbed({
|
|
57
|
-
values,
|
|
58
|
-
headers,
|
|
59
|
-
abortSignal,
|
|
60
|
-
providerOptions
|
|
61
|
-
}) {
|
|
62
|
-
const googleOptions = await parseProviderOptions({
|
|
63
|
-
provider: "google",
|
|
64
|
-
providerOptions,
|
|
65
|
-
schema: googleGenerativeAIEmbeddingProviderOptions
|
|
66
|
-
});
|
|
67
|
-
if (values.length > this.maxEmbeddingsPerCall) {
|
|
68
|
-
throw new TooManyEmbeddingValuesForCallError({
|
|
69
|
-
provider: this.provider,
|
|
70
|
-
modelId: this.modelId,
|
|
71
|
-
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
72
|
-
values
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
const mergedHeaders = combineHeaders(
|
|
76
|
-
await resolve(this.config.headers),
|
|
77
|
-
headers
|
|
78
|
-
);
|
|
79
|
-
if (values.length === 1) {
|
|
80
|
-
const {
|
|
81
|
-
responseHeaders: responseHeaders2,
|
|
82
|
-
value: response2,
|
|
83
|
-
rawValue: rawValue2
|
|
84
|
-
} = await postJsonToApi({
|
|
85
|
-
url: `${this.config.baseURL}/models/${this.modelId}:embedContent`,
|
|
86
|
-
headers: mergedHeaders,
|
|
87
|
-
body: {
|
|
88
|
-
model: `models/${this.modelId}`,
|
|
89
|
-
content: {
|
|
90
|
-
parts: [{ text: values[0] }]
|
|
91
|
-
},
|
|
92
|
-
outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
|
|
93
|
-
taskType: googleOptions == null ? void 0 : googleOptions.taskType
|
|
94
|
-
},
|
|
95
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
96
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
97
|
-
googleGenerativeAISingleEmbeddingResponseSchema
|
|
98
|
-
),
|
|
99
|
-
abortSignal,
|
|
100
|
-
fetch: this.config.fetch
|
|
101
|
-
});
|
|
102
|
-
return {
|
|
103
|
-
embeddings: [response2.embedding.values],
|
|
104
|
-
usage: void 0,
|
|
105
|
-
response: { headers: responseHeaders2, body: rawValue2 }
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
const {
|
|
109
|
-
responseHeaders,
|
|
110
|
-
value: response,
|
|
111
|
-
rawValue
|
|
112
|
-
} = await postJsonToApi({
|
|
113
|
-
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
|
114
|
-
headers: mergedHeaders,
|
|
115
|
-
body: {
|
|
116
|
-
requests: values.map((value) => ({
|
|
117
|
-
model: `models/${this.modelId}`,
|
|
118
|
-
content: { role: "user", parts: [{ text: value }] },
|
|
119
|
-
outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
|
|
120
|
-
taskType: googleOptions == null ? void 0 : googleOptions.taskType
|
|
121
|
-
}))
|
|
122
|
-
},
|
|
123
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
124
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
125
|
-
googleGenerativeAITextEmbeddingResponseSchema
|
|
126
|
-
),
|
|
127
|
-
abortSignal,
|
|
128
|
-
fetch: this.config.fetch
|
|
129
|
-
});
|
|
130
|
-
return {
|
|
131
|
-
embeddings: response.embeddings.map((item) => item.values),
|
|
132
|
-
usage: void 0,
|
|
133
|
-
response: { headers: responseHeaders, body: rawValue }
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
var googleGenerativeAITextEmbeddingResponseSchema = z.object({
|
|
138
|
-
embeddings: z.array(z.object({ values: z.array(z.number()) }))
|
|
139
|
-
});
|
|
140
|
-
var googleGenerativeAISingleEmbeddingResponseSchema = z.object({
|
|
141
|
-
embedding: z.object({ values: z.array(z.number()) })
|
|
142
|
-
});
|
|
143
|
-
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
|
144
|
-
if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
|
|
145
|
-
return void 0;
|
|
146
|
-
}
|
|
147
|
-
if (typeof jsonSchema === "boolean") {
|
|
148
|
-
return { type: "boolean", properties: {} };
|
|
149
|
-
}
|
|
150
|
-
const {
|
|
151
|
-
type,
|
|
152
|
-
description,
|
|
153
|
-
required,
|
|
154
|
-
properties,
|
|
155
|
-
items,
|
|
156
|
-
allOf,
|
|
157
|
-
anyOf,
|
|
158
|
-
oneOf,
|
|
159
|
-
format,
|
|
160
|
-
const: constValue,
|
|
161
|
-
minLength,
|
|
162
|
-
enum: enumValues
|
|
163
|
-
} = jsonSchema;
|
|
164
|
-
const result = {};
|
|
165
|
-
if (description)
|
|
166
|
-
result.description = description;
|
|
167
|
-
if (required)
|
|
168
|
-
result.required = required;
|
|
169
|
-
if (format)
|
|
170
|
-
result.format = format;
|
|
171
|
-
if (constValue !== void 0) {
|
|
172
|
-
result.enum = [constValue];
|
|
173
|
-
}
|
|
174
|
-
if (type) {
|
|
175
|
-
if (Array.isArray(type)) {
|
|
176
|
-
if (type.includes("null")) {
|
|
177
|
-
result.type = type.filter((t) => t !== "null")[0];
|
|
178
|
-
result.nullable = true;
|
|
179
|
-
} else {
|
|
180
|
-
result.type = type;
|
|
181
|
-
}
|
|
182
|
-
} else if (type === "null") {
|
|
183
|
-
result.type = "null";
|
|
184
|
-
} else {
|
|
185
|
-
result.type = type;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (enumValues !== void 0) {
|
|
189
|
-
result.enum = enumValues;
|
|
190
|
-
}
|
|
191
|
-
if (properties != null) {
|
|
192
|
-
result.properties = Object.entries(properties).reduce(
|
|
193
|
-
(acc, [key, value]) => {
|
|
194
|
-
acc[key] = convertJSONSchemaToOpenAPISchema(value);
|
|
195
|
-
return acc;
|
|
196
|
-
},
|
|
197
|
-
{}
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
if (items) {
|
|
201
|
-
result.items = Array.isArray(items) ? items.map(convertJSONSchemaToOpenAPISchema) : convertJSONSchemaToOpenAPISchema(items);
|
|
202
|
-
}
|
|
203
|
-
if (allOf) {
|
|
204
|
-
result.allOf = allOf.map(convertJSONSchemaToOpenAPISchema);
|
|
205
|
-
}
|
|
206
|
-
if (anyOf) {
|
|
207
|
-
if (anyOf.some(
|
|
208
|
-
(schema) => typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null"
|
|
209
|
-
)) {
|
|
210
|
-
const nonNullSchemas = anyOf.filter(
|
|
211
|
-
(schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
|
|
212
|
-
);
|
|
213
|
-
if (nonNullSchemas.length === 1) {
|
|
214
|
-
const converted = convertJSONSchemaToOpenAPISchema(nonNullSchemas[0]);
|
|
215
|
-
if (typeof converted === "object") {
|
|
216
|
-
result.nullable = true;
|
|
217
|
-
Object.assign(result, converted);
|
|
218
|
-
}
|
|
219
|
-
} else {
|
|
220
|
-
result.anyOf = nonNullSchemas.map(convertJSONSchemaToOpenAPISchema);
|
|
221
|
-
result.nullable = true;
|
|
222
|
-
}
|
|
223
|
-
} else {
|
|
224
|
-
result.anyOf = anyOf.map(convertJSONSchemaToOpenAPISchema);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (oneOf) {
|
|
228
|
-
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
|
|
229
|
-
}
|
|
230
|
-
if (minLength !== void 0) {
|
|
231
|
-
result.minLength = minLength;
|
|
232
|
-
}
|
|
233
|
-
return result;
|
|
234
|
-
}
|
|
235
|
-
function isEmptyObjectSchema(jsonSchema) {
|
|
236
|
-
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
|
237
|
-
}
|
|
238
|
-
function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
239
|
-
var _a;
|
|
240
|
-
const systemInstructionParts = [];
|
|
241
|
-
const contents = [];
|
|
242
|
-
let systemMessagesAllowed = true;
|
|
243
|
-
const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
|
|
244
|
-
for (const { role, content } of prompt) {
|
|
245
|
-
switch (role) {
|
|
246
|
-
case "system": {
|
|
247
|
-
if (!systemMessagesAllowed) {
|
|
248
|
-
throw new UnsupportedFunctionalityError({
|
|
249
|
-
functionality: "system messages are only supported at the beginning of the conversation"
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
systemInstructionParts.push({ text: content });
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
case "user": {
|
|
256
|
-
systemMessagesAllowed = false;
|
|
257
|
-
const parts = [];
|
|
258
|
-
for (const part of content) {
|
|
259
|
-
switch (part.type) {
|
|
260
|
-
case "text": {
|
|
261
|
-
parts.push({ text: part.text });
|
|
262
|
-
break;
|
|
263
|
-
}
|
|
264
|
-
case "file": {
|
|
265
|
-
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
266
|
-
parts.push(
|
|
267
|
-
part.data instanceof URL ? {
|
|
268
|
-
fileData: {
|
|
269
|
-
mimeType: mediaType,
|
|
270
|
-
fileUri: part.data.toString()
|
|
271
|
-
}
|
|
272
|
-
} : {
|
|
273
|
-
inlineData: {
|
|
274
|
-
mimeType: mediaType,
|
|
275
|
-
data: convertToBase64(part.data)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
);
|
|
279
|
-
break;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
contents.push({ role: "user", parts });
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
case "assistant": {
|
|
287
|
-
systemMessagesAllowed = false;
|
|
288
|
-
contents.push({
|
|
289
|
-
role: "model",
|
|
290
|
-
parts: content.map((part) => {
|
|
291
|
-
var _a2, _b, _c, _d, _e, _f;
|
|
292
|
-
switch (part.type) {
|
|
293
|
-
case "text": {
|
|
294
|
-
return part.text.length === 0 ? void 0 : {
|
|
295
|
-
text: part.text,
|
|
296
|
-
thoughtSignature: (_b = (_a2 = part.providerOptions) == null ? void 0 : _a2.google) == null ? void 0 : _b.thoughtSignature
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
case "reasoning": {
|
|
300
|
-
return part.text.length === 0 ? void 0 : {
|
|
301
|
-
text: part.text,
|
|
302
|
-
thought: true,
|
|
303
|
-
thoughtSignature: (_d = (_c = part.providerOptions) == null ? void 0 : _c.google) == null ? void 0 : _d.thoughtSignature
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
case "file": {
|
|
307
|
-
if (part.mediaType !== "image/png") {
|
|
308
|
-
throw new UnsupportedFunctionalityError({
|
|
309
|
-
functionality: "Only PNG images are supported in assistant messages"
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
if (part.data instanceof URL) {
|
|
313
|
-
throw new UnsupportedFunctionalityError({
|
|
314
|
-
functionality: "File data URLs in assistant messages are not supported"
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
return {
|
|
318
|
-
inlineData: {
|
|
319
|
-
mimeType: part.mediaType,
|
|
320
|
-
data: convertToBase64(part.data)
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
case "tool-call": {
|
|
325
|
-
return {
|
|
326
|
-
functionCall: {
|
|
327
|
-
name: part.toolName,
|
|
328
|
-
args: part.input
|
|
329
|
-
},
|
|
330
|
-
thoughtSignature: (_f = (_e = part.providerOptions) == null ? void 0 : _e.google) == null ? void 0 : _f.thoughtSignature
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}).filter((part) => part !== void 0)
|
|
335
|
-
});
|
|
336
|
-
break;
|
|
337
|
-
}
|
|
338
|
-
case "tool": {
|
|
339
|
-
systemMessagesAllowed = false;
|
|
340
|
-
contents.push({
|
|
341
|
-
role: "user",
|
|
342
|
-
parts: content.map((part) => ({
|
|
343
|
-
functionResponse: {
|
|
344
|
-
name: part.toolName,
|
|
345
|
-
response: {
|
|
346
|
-
name: part.toolName,
|
|
347
|
-
content: part.output.value
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}))
|
|
351
|
-
});
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
if (isGemmaModel && systemInstructionParts.length > 0 && contents.length > 0 && contents[0].role === "user") {
|
|
357
|
-
const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
|
|
358
|
-
contents[0].parts.unshift({ text: systemText + "\n\n" });
|
|
359
|
-
}
|
|
360
|
-
return {
|
|
361
|
-
systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
|
|
362
|
-
contents
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
function getModelPath(modelId) {
|
|
366
|
-
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
|
367
|
-
}
|
|
368
|
-
var googleGenerativeAIProviderOptions = z.object({
|
|
369
|
-
responseModalities: z.array(z.enum(["TEXT", "IMAGE"])).optional(),
|
|
370
|
-
thinkingConfig: z.object({
|
|
371
|
-
thinkingBudget: z.number().optional(),
|
|
372
|
-
includeThoughts: z.boolean().optional()
|
|
373
|
-
}).optional(),
|
|
374
|
-
/**
|
|
375
|
-
Optional.
|
|
376
|
-
The name of the cached content used as context to serve the prediction.
|
|
377
|
-
Format: cachedContents/{cachedContent}
|
|
378
|
-
*/
|
|
379
|
-
cachedContent: z.string().optional(),
|
|
380
|
-
/**
|
|
381
|
-
* Optional. Enable structured output. Default is true.
|
|
382
|
-
*
|
|
383
|
-
* This is useful when the JSON Schema contains elements that are
|
|
384
|
-
* not supported by the OpenAPI schema version that
|
|
385
|
-
* Google Generative AI uses. You can use this to disable
|
|
386
|
-
* structured outputs if you need to.
|
|
387
|
-
*/
|
|
388
|
-
structuredOutputs: z.boolean().optional(),
|
|
389
|
-
/**
|
|
390
|
-
Optional. A list of unique safety settings for blocking unsafe content.
|
|
391
|
-
*/
|
|
392
|
-
safetySettings: z.array(
|
|
393
|
-
z.object({
|
|
394
|
-
category: z.enum([
|
|
395
|
-
"HARM_CATEGORY_UNSPECIFIED",
|
|
396
|
-
"HARM_CATEGORY_HATE_SPEECH",
|
|
397
|
-
"HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
398
|
-
"HARM_CATEGORY_HARASSMENT",
|
|
399
|
-
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
400
|
-
"HARM_CATEGORY_CIVIC_INTEGRITY"
|
|
401
|
-
]),
|
|
402
|
-
threshold: z.enum([
|
|
403
|
-
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
|
404
|
-
"BLOCK_LOW_AND_ABOVE",
|
|
405
|
-
"BLOCK_MEDIUM_AND_ABOVE",
|
|
406
|
-
"BLOCK_ONLY_HIGH",
|
|
407
|
-
"BLOCK_NONE",
|
|
408
|
-
"OFF"
|
|
409
|
-
])
|
|
410
|
-
})
|
|
411
|
-
).optional(),
|
|
412
|
-
threshold: z.enum([
|
|
413
|
-
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
|
414
|
-
"BLOCK_LOW_AND_ABOVE",
|
|
415
|
-
"BLOCK_MEDIUM_AND_ABOVE",
|
|
416
|
-
"BLOCK_ONLY_HIGH",
|
|
417
|
-
"BLOCK_NONE",
|
|
418
|
-
"OFF"
|
|
419
|
-
]).optional(),
|
|
420
|
-
/**
|
|
421
|
-
* Optional. Enables timestamp understanding for audio-only files.
|
|
422
|
-
*
|
|
423
|
-
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
|
|
424
|
-
*/
|
|
425
|
-
audioTimestamp: z.boolean().optional(),
|
|
426
|
-
/**
|
|
427
|
-
* Optional. Defines labels used in billing reports. Available on Vertex AI only.
|
|
428
|
-
*
|
|
429
|
-
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/add-labels-to-api-calls
|
|
430
|
-
*/
|
|
431
|
-
labels: z.record(z.string(), z.string()).optional()
|
|
432
|
-
});
|
|
433
|
-
function prepareTools({
|
|
434
|
-
tools,
|
|
435
|
-
toolChoice,
|
|
436
|
-
modelId
|
|
437
|
-
}) {
|
|
438
|
-
var _a;
|
|
439
|
-
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
440
|
-
const toolWarnings = [];
|
|
441
|
-
const isGemini2 = modelId.includes("gemini-2");
|
|
442
|
-
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
|
443
|
-
if (tools == null) {
|
|
444
|
-
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
|
445
|
-
}
|
|
446
|
-
const hasFunctionTools = tools.some((tool) => tool.type === "function");
|
|
447
|
-
const hasProviderDefinedTools = tools.some(
|
|
448
|
-
(tool) => tool.type === "provider-defined"
|
|
449
|
-
);
|
|
450
|
-
if (hasFunctionTools && hasProviderDefinedTools) {
|
|
451
|
-
toolWarnings.push({
|
|
452
|
-
type: "unsupported-tool",
|
|
453
|
-
tool: tools.find((tool) => tool.type === "function"),
|
|
454
|
-
details: "Cannot mix function tools with provider-defined tools in the same request. Please use either function tools or provider-defined tools, but not both."
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
if (hasProviderDefinedTools) {
|
|
458
|
-
const googleTools2 = {};
|
|
459
|
-
const providerDefinedTools = tools.filter(
|
|
460
|
-
(tool) => tool.type === "provider-defined"
|
|
461
|
-
);
|
|
462
|
-
providerDefinedTools.forEach((tool) => {
|
|
463
|
-
switch (tool.id) {
|
|
464
|
-
case "google.google_search":
|
|
465
|
-
if (isGemini2) {
|
|
466
|
-
googleTools2.googleSearch = {};
|
|
467
|
-
} else if (supportsDynamicRetrieval) {
|
|
468
|
-
googleTools2.googleSearchRetrieval = {
|
|
469
|
-
dynamicRetrievalConfig: {
|
|
470
|
-
mode: tool.args.mode,
|
|
471
|
-
dynamicThreshold: tool.args.dynamicThreshold
|
|
472
|
-
}
|
|
473
|
-
};
|
|
474
|
-
} else {
|
|
475
|
-
googleTools2.googleSearchRetrieval = {};
|
|
476
|
-
}
|
|
477
|
-
break;
|
|
478
|
-
case "google.url_context":
|
|
479
|
-
if (isGemini2) {
|
|
480
|
-
googleTools2.urlContext = {};
|
|
481
|
-
} else {
|
|
482
|
-
toolWarnings.push({
|
|
483
|
-
type: "unsupported-tool",
|
|
484
|
-
tool,
|
|
485
|
-
details: "The URL context tool is not supported with other Gemini models than Gemini 2."
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
break;
|
|
489
|
-
case "google.code_execution":
|
|
490
|
-
if (isGemini2) {
|
|
491
|
-
googleTools2.codeExecution = {};
|
|
492
|
-
} else {
|
|
493
|
-
toolWarnings.push({
|
|
494
|
-
type: "unsupported-tool",
|
|
495
|
-
tool,
|
|
496
|
-
details: "The code execution tools is not supported with other Gemini models than Gemini 2."
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
break;
|
|
500
|
-
default:
|
|
501
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
502
|
-
break;
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
return {
|
|
506
|
-
tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
|
|
507
|
-
toolConfig: void 0,
|
|
508
|
-
toolWarnings
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
const functionDeclarations = [];
|
|
512
|
-
for (const tool of tools) {
|
|
513
|
-
switch (tool.type) {
|
|
514
|
-
case "function":
|
|
515
|
-
functionDeclarations.push({
|
|
516
|
-
name: tool.name,
|
|
517
|
-
description: (_a = tool.description) != null ? _a : "",
|
|
518
|
-
parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
|
|
519
|
-
});
|
|
520
|
-
break;
|
|
521
|
-
default:
|
|
522
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
523
|
-
break;
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
if (toolChoice == null) {
|
|
527
|
-
return {
|
|
528
|
-
tools: { functionDeclarations },
|
|
529
|
-
toolConfig: void 0,
|
|
530
|
-
toolWarnings
|
|
531
|
-
};
|
|
532
|
-
}
|
|
533
|
-
const type = toolChoice.type;
|
|
534
|
-
switch (type) {
|
|
535
|
-
case "auto":
|
|
536
|
-
return {
|
|
537
|
-
tools: { functionDeclarations },
|
|
538
|
-
toolConfig: { functionCallingConfig: { mode: "AUTO" } },
|
|
539
|
-
toolWarnings
|
|
540
|
-
};
|
|
541
|
-
case "none":
|
|
542
|
-
return {
|
|
543
|
-
tools: { functionDeclarations },
|
|
544
|
-
toolConfig: { functionCallingConfig: { mode: "NONE" } },
|
|
545
|
-
toolWarnings
|
|
546
|
-
};
|
|
547
|
-
case "required":
|
|
548
|
-
return {
|
|
549
|
-
tools: { functionDeclarations },
|
|
550
|
-
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
|
551
|
-
toolWarnings
|
|
552
|
-
};
|
|
553
|
-
case "tool":
|
|
554
|
-
return {
|
|
555
|
-
tools: { functionDeclarations },
|
|
556
|
-
toolConfig: {
|
|
557
|
-
functionCallingConfig: {
|
|
558
|
-
mode: "ANY",
|
|
559
|
-
allowedFunctionNames: [toolChoice.toolName]
|
|
560
|
-
}
|
|
561
|
-
},
|
|
562
|
-
toolWarnings
|
|
563
|
-
};
|
|
564
|
-
default: {
|
|
565
|
-
const _exhaustiveCheck = type;
|
|
566
|
-
throw new UnsupportedFunctionalityError({
|
|
567
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
function mapGoogleGenerativeAIFinishReason({
|
|
573
|
-
finishReason,
|
|
574
|
-
hasToolCalls
|
|
575
|
-
}) {
|
|
576
|
-
switch (finishReason) {
|
|
577
|
-
case "STOP":
|
|
578
|
-
return hasToolCalls ? "tool-calls" : "stop";
|
|
579
|
-
case "MAX_TOKENS":
|
|
580
|
-
return "length";
|
|
581
|
-
case "IMAGE_SAFETY":
|
|
582
|
-
case "RECITATION":
|
|
583
|
-
case "SAFETY":
|
|
584
|
-
case "BLOCKLIST":
|
|
585
|
-
case "PROHIBITED_CONTENT":
|
|
586
|
-
case "SPII":
|
|
587
|
-
return "content-filter";
|
|
588
|
-
case "FINISH_REASON_UNSPECIFIED":
|
|
589
|
-
case "OTHER":
|
|
590
|
-
return "other";
|
|
591
|
-
case "MALFORMED_FUNCTION_CALL":
|
|
592
|
-
return "error";
|
|
593
|
-
default:
|
|
594
|
-
return "unknown";
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
var groundingChunkSchema = z.object({
|
|
598
|
-
web: z.object({ uri: z.string(), title: z.string() }).nullish(),
|
|
599
|
-
retrievedContext: z.object({ uri: z.string(), title: z.string() }).nullish()
|
|
600
|
-
});
|
|
601
|
-
var groundingMetadataSchema = z.object({
|
|
602
|
-
webSearchQueries: z.array(z.string()).nullish(),
|
|
603
|
-
retrievalQueries: z.array(z.string()).nullish(),
|
|
604
|
-
searchEntryPoint: z.object({ renderedContent: z.string() }).nullish(),
|
|
605
|
-
groundingChunks: z.array(groundingChunkSchema).nullish(),
|
|
606
|
-
groundingSupports: z.array(
|
|
607
|
-
z.object({
|
|
608
|
-
segment: z.object({
|
|
609
|
-
startIndex: z.number().nullish(),
|
|
610
|
-
endIndex: z.number().nullish(),
|
|
611
|
-
text: z.string().nullish()
|
|
612
|
-
}),
|
|
613
|
-
segment_text: z.string().nullish(),
|
|
614
|
-
groundingChunkIndices: z.array(z.number()).nullish(),
|
|
615
|
-
supportChunkIndices: z.array(z.number()).nullish(),
|
|
616
|
-
confidenceScores: z.array(z.number()).nullish(),
|
|
617
|
-
confidenceScore: z.array(z.number()).nullish()
|
|
618
|
-
})
|
|
619
|
-
).nullish(),
|
|
620
|
-
retrievalMetadata: z.union([
|
|
621
|
-
z.object({
|
|
622
|
-
webDynamicRetrievalScore: z.number()
|
|
623
|
-
}),
|
|
624
|
-
z.object({})
|
|
625
|
-
]).nullish()
|
|
626
|
-
});
|
|
627
|
-
var googleSearch = createProviderDefinedToolFactory({
|
|
628
|
-
id: "google.google_search",
|
|
629
|
-
name: "google_search",
|
|
630
|
-
inputSchema: z.object({
|
|
631
|
-
mode: z.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
|
|
632
|
-
dynamicThreshold: z.number().default(1)
|
|
633
|
-
})
|
|
634
|
-
});
|
|
635
|
-
var urlMetadataSchema = z.object({
|
|
636
|
-
retrievedUrl: z.string(),
|
|
637
|
-
urlRetrievalStatus: z.string()
|
|
638
|
-
});
|
|
639
|
-
var urlContextMetadataSchema = z.object({
|
|
640
|
-
urlMetadata: z.array(urlMetadataSchema)
|
|
641
|
-
});
|
|
642
|
-
var urlContext = createProviderDefinedToolFactory({
|
|
643
|
-
id: "google.url_context",
|
|
644
|
-
name: "url_context",
|
|
645
|
-
inputSchema: z.object({})
|
|
646
|
-
});
|
|
647
|
-
var GoogleGenerativeAILanguageModel = class {
|
|
648
|
-
constructor(modelId, config) {
|
|
649
|
-
this.specificationVersion = "v2";
|
|
650
|
-
var _a;
|
|
651
|
-
this.modelId = modelId;
|
|
652
|
-
this.config = config;
|
|
653
|
-
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
654
|
-
}
|
|
655
|
-
get provider() {
|
|
656
|
-
return this.config.provider;
|
|
657
|
-
}
|
|
658
|
-
get supportedUrls() {
|
|
659
|
-
var _a, _b, _c;
|
|
660
|
-
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
661
|
-
}
|
|
662
|
-
async getArgs({
|
|
663
|
-
prompt,
|
|
664
|
-
maxOutputTokens,
|
|
665
|
-
temperature,
|
|
666
|
-
topP,
|
|
667
|
-
topK,
|
|
668
|
-
frequencyPenalty,
|
|
669
|
-
presencePenalty,
|
|
670
|
-
stopSequences,
|
|
671
|
-
responseFormat,
|
|
672
|
-
seed,
|
|
673
|
-
tools,
|
|
674
|
-
toolChoice,
|
|
675
|
-
providerOptions
|
|
676
|
-
}) {
|
|
677
|
-
var _a, _b;
|
|
678
|
-
const warnings = [];
|
|
679
|
-
const googleOptions = await parseProviderOptions({
|
|
680
|
-
provider: "google",
|
|
681
|
-
providerOptions,
|
|
682
|
-
schema: googleGenerativeAIProviderOptions
|
|
683
|
-
});
|
|
684
|
-
if (((_a = googleOptions == null ? void 0 : googleOptions.thinkingConfig) == null ? void 0 : _a.includeThoughts) === true && !this.config.provider.startsWith("google.vertex.")) {
|
|
685
|
-
warnings.push({
|
|
686
|
-
type: "other",
|
|
687
|
-
message: `The 'includeThoughts' option is only supported with the Google Vertex provider and might not be supported or could behave unexpectedly with the current Google provider (${this.config.provider}).`
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
|
|
691
|
-
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
|
692
|
-
prompt,
|
|
693
|
-
{ isGemmaModel }
|
|
694
|
-
);
|
|
695
|
-
const {
|
|
696
|
-
tools: googleTools2,
|
|
697
|
-
toolConfig: googleToolConfig,
|
|
698
|
-
toolWarnings
|
|
699
|
-
} = prepareTools({
|
|
700
|
-
tools,
|
|
701
|
-
toolChoice,
|
|
702
|
-
modelId: this.modelId
|
|
703
|
-
});
|
|
704
|
-
return {
|
|
705
|
-
args: {
|
|
706
|
-
generationConfig: {
|
|
707
|
-
// standardized settings:
|
|
708
|
-
maxOutputTokens,
|
|
709
|
-
temperature,
|
|
710
|
-
topK,
|
|
711
|
-
topP,
|
|
712
|
-
frequencyPenalty,
|
|
713
|
-
presencePenalty,
|
|
714
|
-
stopSequences,
|
|
715
|
-
seed,
|
|
716
|
-
// response format:
|
|
717
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
|
718
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
|
719
|
-
// so this is needed as an escape hatch:
|
|
720
|
-
// TODO convert into provider option
|
|
721
|
-
((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
|
722
|
-
...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
|
|
723
|
-
audioTimestamp: googleOptions.audioTimestamp
|
|
724
|
-
},
|
|
725
|
-
// provider options:
|
|
726
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
|
|
727
|
-
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
|
728
|
-
},
|
|
729
|
-
contents,
|
|
730
|
-
systemInstruction: isGemmaModel ? void 0 : systemInstruction,
|
|
731
|
-
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
|
732
|
-
tools: googleTools2,
|
|
733
|
-
toolConfig: googleToolConfig,
|
|
734
|
-
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
|
|
735
|
-
labels: googleOptions == null ? void 0 : googleOptions.labels
|
|
736
|
-
},
|
|
737
|
-
warnings: [...warnings, ...toolWarnings]
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
async doGenerate(options) {
|
|
741
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
742
|
-
const { args, warnings } = await this.getArgs(options);
|
|
743
|
-
const body = JSON.stringify(args);
|
|
744
|
-
const mergedHeaders = combineHeaders(
|
|
745
|
-
await resolve(this.config.headers),
|
|
746
|
-
options.headers
|
|
747
|
-
);
|
|
748
|
-
const {
|
|
749
|
-
responseHeaders,
|
|
750
|
-
value: response,
|
|
751
|
-
rawValue: rawResponse
|
|
752
|
-
} = await postJsonToApi({
|
|
753
|
-
url: `${this.config.baseURL}/${getModelPath(
|
|
754
|
-
this.modelId
|
|
755
|
-
)}:generateContent`,
|
|
756
|
-
headers: mergedHeaders,
|
|
757
|
-
body: args,
|
|
758
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
759
|
-
successfulResponseHandler: createJsonResponseHandler(responseSchema),
|
|
760
|
-
abortSignal: options.abortSignal,
|
|
761
|
-
fetch: this.config.fetch
|
|
762
|
-
});
|
|
763
|
-
const candidate = response.candidates[0];
|
|
764
|
-
const content = [];
|
|
765
|
-
const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
|
|
766
|
-
const usageMetadata = response.usageMetadata;
|
|
767
|
-
let lastCodeExecutionToolCallId;
|
|
768
|
-
for (const part of parts) {
|
|
769
|
-
if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
|
|
770
|
-
const toolCallId = this.config.generateId();
|
|
771
|
-
lastCodeExecutionToolCallId = toolCallId;
|
|
772
|
-
content.push({
|
|
773
|
-
type: "tool-call",
|
|
774
|
-
toolCallId,
|
|
775
|
-
toolName: "code_execution",
|
|
776
|
-
input: JSON.stringify(part.executableCode),
|
|
777
|
-
providerExecuted: true
|
|
778
|
-
});
|
|
779
|
-
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
|
780
|
-
content.push({
|
|
781
|
-
type: "tool-result",
|
|
782
|
-
// Assumes a result directly follows its corresponding call part.
|
|
783
|
-
toolCallId: lastCodeExecutionToolCallId,
|
|
784
|
-
toolName: "code_execution",
|
|
785
|
-
result: {
|
|
786
|
-
outcome: part.codeExecutionResult.outcome,
|
|
787
|
-
output: part.codeExecutionResult.output
|
|
788
|
-
},
|
|
789
|
-
providerExecuted: true
|
|
790
|
-
});
|
|
791
|
-
lastCodeExecutionToolCallId = void 0;
|
|
792
|
-
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
|
793
|
-
content.push({
|
|
794
|
-
type: part.thought === true ? "reasoning" : "text",
|
|
795
|
-
text: part.text,
|
|
796
|
-
providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
|
|
797
|
-
});
|
|
798
|
-
} else if ("functionCall" in part) {
|
|
799
|
-
content.push({
|
|
800
|
-
type: "tool-call",
|
|
801
|
-
toolCallId: this.config.generateId(),
|
|
802
|
-
toolName: part.functionCall.name,
|
|
803
|
-
input: JSON.stringify(part.functionCall.args),
|
|
804
|
-
providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
|
|
805
|
-
});
|
|
806
|
-
} else if ("inlineData" in part) {
|
|
807
|
-
content.push({
|
|
808
|
-
type: "file",
|
|
809
|
-
data: part.inlineData.data,
|
|
810
|
-
mediaType: part.inlineData.mimeType
|
|
811
|
-
});
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
const sources = (_d = extractSources({
|
|
815
|
-
groundingMetadata: candidate.groundingMetadata,
|
|
816
|
-
generateId: this.config.generateId
|
|
817
|
-
})) != null ? _d : [];
|
|
818
|
-
for (const source of sources) {
|
|
819
|
-
content.push(source);
|
|
820
|
-
}
|
|
821
|
-
return {
|
|
822
|
-
content,
|
|
823
|
-
finishReason: mapGoogleGenerativeAIFinishReason({
|
|
824
|
-
finishReason: candidate.finishReason,
|
|
825
|
-
hasToolCalls: content.some((part) => part.type === "tool-call")
|
|
826
|
-
}),
|
|
827
|
-
usage: {
|
|
828
|
-
inputTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : void 0,
|
|
829
|
-
outputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : void 0,
|
|
830
|
-
totalTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _g : void 0,
|
|
831
|
-
reasoningTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _h : void 0,
|
|
832
|
-
cachedInputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _i : void 0
|
|
833
|
-
},
|
|
834
|
-
warnings,
|
|
835
|
-
providerMetadata: {
|
|
836
|
-
google: {
|
|
837
|
-
groundingMetadata: (_j = candidate.groundingMetadata) != null ? _j : null,
|
|
838
|
-
urlContextMetadata: (_k = candidate.urlContextMetadata) != null ? _k : null,
|
|
839
|
-
safetyRatings: (_l = candidate.safetyRatings) != null ? _l : null,
|
|
840
|
-
usageMetadata: usageMetadata != null ? usageMetadata : null
|
|
841
|
-
}
|
|
842
|
-
},
|
|
843
|
-
request: { body },
|
|
844
|
-
response: {
|
|
845
|
-
// TODO timestamp, model id, id
|
|
846
|
-
headers: responseHeaders,
|
|
847
|
-
body: rawResponse
|
|
848
|
-
}
|
|
849
|
-
};
|
|
850
|
-
}
|
|
851
|
-
async doStream(options) {
|
|
852
|
-
const { args, warnings } = await this.getArgs(options);
|
|
853
|
-
const body = JSON.stringify(args);
|
|
854
|
-
const headers = combineHeaders(
|
|
855
|
-
await resolve(this.config.headers),
|
|
856
|
-
options.headers
|
|
857
|
-
);
|
|
858
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
859
|
-
url: `${this.config.baseURL}/${getModelPath(
|
|
860
|
-
this.modelId
|
|
861
|
-
)}:streamGenerateContent?alt=sse`,
|
|
862
|
-
headers,
|
|
863
|
-
body: args,
|
|
864
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
865
|
-
successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),
|
|
866
|
-
abortSignal: options.abortSignal,
|
|
867
|
-
fetch: this.config.fetch
|
|
868
|
-
});
|
|
869
|
-
let finishReason = "unknown";
|
|
870
|
-
const usage = {
|
|
871
|
-
inputTokens: void 0,
|
|
872
|
-
outputTokens: void 0,
|
|
873
|
-
totalTokens: void 0
|
|
874
|
-
};
|
|
875
|
-
let providerMetadata = void 0;
|
|
876
|
-
const generateId3 = this.config.generateId;
|
|
877
|
-
let hasToolCalls = false;
|
|
878
|
-
let currentTextBlockId = null;
|
|
879
|
-
let currentReasoningBlockId = null;
|
|
880
|
-
let blockCounter = 0;
|
|
881
|
-
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
|
882
|
-
let lastCodeExecutionToolCallId;
|
|
883
|
-
return {
|
|
884
|
-
stream: response.pipeThrough(
|
|
885
|
-
new TransformStream({
|
|
886
|
-
start(controller) {
|
|
887
|
-
controller.enqueue({ type: "stream-start", warnings });
|
|
888
|
-
},
|
|
889
|
-
transform(chunk, controller) {
|
|
890
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
891
|
-
if (options.includeRawChunks) {
|
|
892
|
-
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
893
|
-
}
|
|
894
|
-
if (!chunk.success) {
|
|
895
|
-
controller.enqueue({ type: "error", error: chunk.error });
|
|
896
|
-
return;
|
|
897
|
-
}
|
|
898
|
-
const value = chunk.value;
|
|
899
|
-
const usageMetadata = value.usageMetadata;
|
|
900
|
-
if (usageMetadata != null) {
|
|
901
|
-
usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
|
|
902
|
-
usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
|
|
903
|
-
usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
|
|
904
|
-
usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
|
|
905
|
-
usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
|
|
906
|
-
}
|
|
907
|
-
const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
|
|
908
|
-
if (candidate == null) {
|
|
909
|
-
return;
|
|
910
|
-
}
|
|
911
|
-
const content = candidate.content;
|
|
912
|
-
const sources = extractSources({
|
|
913
|
-
groundingMetadata: candidate.groundingMetadata,
|
|
914
|
-
generateId: generateId3
|
|
915
|
-
});
|
|
916
|
-
if (sources != null) {
|
|
917
|
-
for (const source of sources) {
|
|
918
|
-
if (source.sourceType === "url" && !emittedSourceUrls.has(source.url)) {
|
|
919
|
-
emittedSourceUrls.add(source.url);
|
|
920
|
-
controller.enqueue(source);
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
if (content != null) {
|
|
925
|
-
const parts = (_g = content.parts) != null ? _g : [];
|
|
926
|
-
for (const part of parts) {
|
|
927
|
-
if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
|
|
928
|
-
const toolCallId = generateId3();
|
|
929
|
-
lastCodeExecutionToolCallId = toolCallId;
|
|
930
|
-
controller.enqueue({
|
|
931
|
-
type: "tool-call",
|
|
932
|
-
toolCallId,
|
|
933
|
-
toolName: "code_execution",
|
|
934
|
-
input: JSON.stringify(part.executableCode),
|
|
935
|
-
providerExecuted: true
|
|
936
|
-
});
|
|
937
|
-
hasToolCalls = true;
|
|
938
|
-
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
|
939
|
-
const toolCallId = lastCodeExecutionToolCallId;
|
|
940
|
-
if (toolCallId) {
|
|
941
|
-
controller.enqueue({
|
|
942
|
-
type: "tool-result",
|
|
943
|
-
toolCallId,
|
|
944
|
-
toolName: "code_execution",
|
|
945
|
-
result: {
|
|
946
|
-
outcome: part.codeExecutionResult.outcome,
|
|
947
|
-
output: part.codeExecutionResult.output
|
|
948
|
-
},
|
|
949
|
-
providerExecuted: true
|
|
950
|
-
});
|
|
951
|
-
lastCodeExecutionToolCallId = void 0;
|
|
952
|
-
}
|
|
953
|
-
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
|
954
|
-
if (part.thought === true) {
|
|
955
|
-
if (currentTextBlockId !== null) {
|
|
956
|
-
controller.enqueue({
|
|
957
|
-
type: "text-end",
|
|
958
|
-
id: currentTextBlockId
|
|
959
|
-
});
|
|
960
|
-
currentTextBlockId = null;
|
|
961
|
-
}
|
|
962
|
-
if (currentReasoningBlockId === null) {
|
|
963
|
-
currentReasoningBlockId = String(blockCounter++);
|
|
964
|
-
controller.enqueue({
|
|
965
|
-
type: "reasoning-start",
|
|
966
|
-
id: currentReasoningBlockId,
|
|
967
|
-
providerMetadata: part.thoughtSignature ? {
|
|
968
|
-
google: {
|
|
969
|
-
thoughtSignature: part.thoughtSignature
|
|
970
|
-
}
|
|
971
|
-
} : void 0
|
|
972
|
-
});
|
|
973
|
-
}
|
|
974
|
-
controller.enqueue({
|
|
975
|
-
type: "reasoning-delta",
|
|
976
|
-
id: currentReasoningBlockId,
|
|
977
|
-
delta: part.text,
|
|
978
|
-
providerMetadata: part.thoughtSignature ? {
|
|
979
|
-
google: { thoughtSignature: part.thoughtSignature }
|
|
980
|
-
} : void 0
|
|
981
|
-
});
|
|
982
|
-
} else {
|
|
983
|
-
if (currentReasoningBlockId !== null) {
|
|
984
|
-
controller.enqueue({
|
|
985
|
-
type: "reasoning-end",
|
|
986
|
-
id: currentReasoningBlockId
|
|
987
|
-
});
|
|
988
|
-
currentReasoningBlockId = null;
|
|
989
|
-
}
|
|
990
|
-
if (currentTextBlockId === null) {
|
|
991
|
-
currentTextBlockId = String(blockCounter++);
|
|
992
|
-
controller.enqueue({
|
|
993
|
-
type: "text-start",
|
|
994
|
-
id: currentTextBlockId,
|
|
995
|
-
providerMetadata: part.thoughtSignature ? {
|
|
996
|
-
google: {
|
|
997
|
-
thoughtSignature: part.thoughtSignature
|
|
998
|
-
}
|
|
999
|
-
} : void 0
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
controller.enqueue({
|
|
1003
|
-
type: "text-delta",
|
|
1004
|
-
id: currentTextBlockId,
|
|
1005
|
-
delta: part.text,
|
|
1006
|
-
providerMetadata: part.thoughtSignature ? {
|
|
1007
|
-
google: { thoughtSignature: part.thoughtSignature }
|
|
1008
|
-
} : void 0
|
|
1009
|
-
});
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
const inlineDataParts = getInlineDataParts(content.parts);
|
|
1014
|
-
if (inlineDataParts != null) {
|
|
1015
|
-
for (const part of inlineDataParts) {
|
|
1016
|
-
controller.enqueue({
|
|
1017
|
-
type: "file",
|
|
1018
|
-
mediaType: part.inlineData.mimeType,
|
|
1019
|
-
data: part.inlineData.data
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
const toolCallDeltas = getToolCallsFromParts({
|
|
1024
|
-
parts: content.parts,
|
|
1025
|
-
generateId: generateId3
|
|
1026
|
-
});
|
|
1027
|
-
if (toolCallDeltas != null) {
|
|
1028
|
-
for (const toolCall of toolCallDeltas) {
|
|
1029
|
-
controller.enqueue({
|
|
1030
|
-
type: "tool-input-start",
|
|
1031
|
-
id: toolCall.toolCallId,
|
|
1032
|
-
toolName: toolCall.toolName,
|
|
1033
|
-
providerMetadata: toolCall.providerMetadata
|
|
1034
|
-
});
|
|
1035
|
-
controller.enqueue({
|
|
1036
|
-
type: "tool-input-delta",
|
|
1037
|
-
id: toolCall.toolCallId,
|
|
1038
|
-
delta: toolCall.args,
|
|
1039
|
-
providerMetadata: toolCall.providerMetadata
|
|
1040
|
-
});
|
|
1041
|
-
controller.enqueue({
|
|
1042
|
-
type: "tool-input-end",
|
|
1043
|
-
id: toolCall.toolCallId,
|
|
1044
|
-
providerMetadata: toolCall.providerMetadata
|
|
1045
|
-
});
|
|
1046
|
-
controller.enqueue({
|
|
1047
|
-
type: "tool-call",
|
|
1048
|
-
toolCallId: toolCall.toolCallId,
|
|
1049
|
-
toolName: toolCall.toolName,
|
|
1050
|
-
input: toolCall.args,
|
|
1051
|
-
providerMetadata: toolCall.providerMetadata
|
|
1052
|
-
});
|
|
1053
|
-
hasToolCalls = true;
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
if (candidate.finishReason != null) {
|
|
1058
|
-
finishReason = mapGoogleGenerativeAIFinishReason({
|
|
1059
|
-
finishReason: candidate.finishReason,
|
|
1060
|
-
hasToolCalls
|
|
1061
|
-
});
|
|
1062
|
-
providerMetadata = {
|
|
1063
|
-
google: {
|
|
1064
|
-
groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
|
|
1065
|
-
urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
|
|
1066
|
-
safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
|
|
1067
|
-
}
|
|
1068
|
-
};
|
|
1069
|
-
if (usageMetadata != null) {
|
|
1070
|
-
providerMetadata.google.usageMetadata = usageMetadata;
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
},
|
|
1074
|
-
flush(controller) {
|
|
1075
|
-
if (currentTextBlockId !== null) {
|
|
1076
|
-
controller.enqueue({
|
|
1077
|
-
type: "text-end",
|
|
1078
|
-
id: currentTextBlockId
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
if (currentReasoningBlockId !== null) {
|
|
1082
|
-
controller.enqueue({
|
|
1083
|
-
type: "reasoning-end",
|
|
1084
|
-
id: currentReasoningBlockId
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
controller.enqueue({
|
|
1088
|
-
type: "finish",
|
|
1089
|
-
finishReason,
|
|
1090
|
-
usage,
|
|
1091
|
-
providerMetadata
|
|
1092
|
-
});
|
|
1093
|
-
}
|
|
1094
|
-
})
|
|
1095
|
-
),
|
|
1096
|
-
response: { headers: responseHeaders },
|
|
1097
|
-
request: { body }
|
|
1098
|
-
};
|
|
1099
|
-
}
|
|
1100
|
-
};
|
|
1101
|
-
function getToolCallsFromParts({
|
|
1102
|
-
parts,
|
|
1103
|
-
generateId: generateId3
|
|
1104
|
-
}) {
|
|
1105
|
-
const functionCallParts = parts == null ? void 0 : parts.filter(
|
|
1106
|
-
(part) => "functionCall" in part
|
|
1107
|
-
);
|
|
1108
|
-
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
|
1109
|
-
type: "tool-call",
|
|
1110
|
-
toolCallId: generateId3(),
|
|
1111
|
-
toolName: part.functionCall.name,
|
|
1112
|
-
args: JSON.stringify(part.functionCall.args),
|
|
1113
|
-
providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
|
|
1114
|
-
}));
|
|
1115
|
-
}
|
|
1116
|
-
function getInlineDataParts(parts) {
|
|
1117
|
-
return parts == null ? void 0 : parts.filter(
|
|
1118
|
-
(part) => "inlineData" in part
|
|
1119
|
-
);
|
|
1120
|
-
}
|
|
1121
|
-
function extractSources({
|
|
1122
|
-
groundingMetadata,
|
|
1123
|
-
generateId: generateId3
|
|
1124
|
-
}) {
|
|
1125
|
-
var _a;
|
|
1126
|
-
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
|
1127
|
-
(chunk) => chunk.web != null
|
|
1128
|
-
).map((chunk) => ({
|
|
1129
|
-
type: "source",
|
|
1130
|
-
sourceType: "url",
|
|
1131
|
-
id: generateId3(),
|
|
1132
|
-
url: chunk.web.uri,
|
|
1133
|
-
title: chunk.web.title
|
|
1134
|
-
}));
|
|
1135
|
-
}
|
|
1136
|
-
var contentSchema = z.object({
|
|
1137
|
-
parts: z.array(
|
|
1138
|
-
z.union([
|
|
1139
|
-
// note: order matters since text can be fully empty
|
|
1140
|
-
z.object({
|
|
1141
|
-
functionCall: z.object({
|
|
1142
|
-
name: z.string(),
|
|
1143
|
-
args: z.unknown()
|
|
1144
|
-
}),
|
|
1145
|
-
thoughtSignature: z.string().nullish()
|
|
1146
|
-
}),
|
|
1147
|
-
z.object({
|
|
1148
|
-
inlineData: z.object({
|
|
1149
|
-
mimeType: z.string(),
|
|
1150
|
-
data: z.string()
|
|
1151
|
-
})
|
|
1152
|
-
}),
|
|
1153
|
-
z.object({
|
|
1154
|
-
executableCode: z.object({
|
|
1155
|
-
language: z.string(),
|
|
1156
|
-
code: z.string()
|
|
1157
|
-
}).nullish(),
|
|
1158
|
-
codeExecutionResult: z.object({
|
|
1159
|
-
outcome: z.string(),
|
|
1160
|
-
output: z.string()
|
|
1161
|
-
}).nullish(),
|
|
1162
|
-
text: z.string().nullish(),
|
|
1163
|
-
thought: z.boolean().nullish(),
|
|
1164
|
-
thoughtSignature: z.string().nullish()
|
|
1165
|
-
})
|
|
1166
|
-
])
|
|
1167
|
-
).nullish()
|
|
1168
|
-
});
|
|
1169
|
-
var safetyRatingSchema = z.object({
|
|
1170
|
-
category: z.string().nullish(),
|
|
1171
|
-
probability: z.string().nullish(),
|
|
1172
|
-
probabilityScore: z.number().nullish(),
|
|
1173
|
-
severity: z.string().nullish(),
|
|
1174
|
-
severityScore: z.number().nullish(),
|
|
1175
|
-
blocked: z.boolean().nullish()
|
|
1176
|
-
});
|
|
1177
|
-
var usageSchema = z.object({
|
|
1178
|
-
cachedContentTokenCount: z.number().nullish(),
|
|
1179
|
-
thoughtsTokenCount: z.number().nullish(),
|
|
1180
|
-
promptTokenCount: z.number().nullish(),
|
|
1181
|
-
candidatesTokenCount: z.number().nullish(),
|
|
1182
|
-
totalTokenCount: z.number().nullish()
|
|
1183
|
-
});
|
|
1184
|
-
var responseSchema = z.object({
|
|
1185
|
-
candidates: z.array(
|
|
1186
|
-
z.object({
|
|
1187
|
-
content: contentSchema.nullish().or(z.object({}).strict()),
|
|
1188
|
-
finishReason: z.string().nullish(),
|
|
1189
|
-
safetyRatings: z.array(safetyRatingSchema).nullish(),
|
|
1190
|
-
groundingMetadata: groundingMetadataSchema.nullish(),
|
|
1191
|
-
urlContextMetadata: urlContextMetadataSchema.nullish()
|
|
1192
|
-
})
|
|
1193
|
-
),
|
|
1194
|
-
usageMetadata: usageSchema.nullish()
|
|
1195
|
-
});
|
|
1196
|
-
var chunkSchema = z.object({
|
|
1197
|
-
candidates: z.array(
|
|
1198
|
-
z.object({
|
|
1199
|
-
content: contentSchema.nullish(),
|
|
1200
|
-
finishReason: z.string().nullish(),
|
|
1201
|
-
safetyRatings: z.array(safetyRatingSchema).nullish(),
|
|
1202
|
-
groundingMetadata: groundingMetadataSchema.nullish(),
|
|
1203
|
-
urlContextMetadata: urlContextMetadataSchema.nullish()
|
|
1204
|
-
})
|
|
1205
|
-
).nullish(),
|
|
1206
|
-
usageMetadata: usageSchema.nullish()
|
|
1207
|
-
});
|
|
1208
|
-
var codeExecution = createProviderDefinedToolFactoryWithOutputSchema({
|
|
1209
|
-
id: "google.code_execution",
|
|
1210
|
-
name: "code_execution",
|
|
1211
|
-
inputSchema: z.object({
|
|
1212
|
-
language: z.string().describe("The programming language of the code."),
|
|
1213
|
-
code: z.string().describe("The code to be executed.")
|
|
1214
|
-
}),
|
|
1215
|
-
outputSchema: z.object({
|
|
1216
|
-
outcome: z.string().describe('The outcome of the execution (e.g., "OUTCOME_OK").'),
|
|
1217
|
-
output: z.string().describe("The output from the code execution.")
|
|
1218
|
-
})
|
|
1219
|
-
});
|
|
1220
|
-
var googleTools = {
|
|
1221
|
-
/**
|
|
1222
|
-
* Creates a Google search tool that gives Google direct access to real-time web content.
|
|
1223
|
-
* Must have name "google_search".
|
|
1224
|
-
*/
|
|
1225
|
-
googleSearch,
|
|
1226
|
-
/**
|
|
1227
|
-
* Creates a URL context tool that gives Google direct access to real-time web content.
|
|
1228
|
-
* Must have name "url_context".
|
|
1229
|
-
*/
|
|
1230
|
-
urlContext,
|
|
1231
|
-
/**
|
|
1232
|
-
* A tool that enables the model to generate and run Python code.
|
|
1233
|
-
* Must have name "code_execution".
|
|
1234
|
-
*
|
|
1235
|
-
* @note Ensure the selected model supports Code Execution.
|
|
1236
|
-
* Multi-tool usage with the code execution tool is typically compatible with Gemini >=2 models.
|
|
1237
|
-
*
|
|
1238
|
-
* @see https://ai.google.dev/gemini-api/docs/code-execution (Google AI)
|
|
1239
|
-
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/code-execution-api (Vertex AI)
|
|
1240
|
-
*/
|
|
1241
|
-
codeExecution
|
|
1242
|
-
};
|
|
1243
|
-
var GoogleGenerativeAIImageModel = class {
|
|
1244
|
-
constructor(modelId, settings, config) {
|
|
1245
|
-
this.modelId = modelId;
|
|
1246
|
-
this.settings = settings;
|
|
1247
|
-
this.config = config;
|
|
1248
|
-
this.specificationVersion = "v2";
|
|
1249
|
-
}
|
|
1250
|
-
get maxImagesPerCall() {
|
|
1251
|
-
var _a;
|
|
1252
|
-
return (_a = this.settings.maxImagesPerCall) != null ? _a : 4;
|
|
1253
|
-
}
|
|
1254
|
-
get provider() {
|
|
1255
|
-
return this.config.provider;
|
|
1256
|
-
}
|
|
1257
|
-
async doGenerate(options) {
|
|
1258
|
-
var _a, _b, _c;
|
|
1259
|
-
const {
|
|
1260
|
-
prompt,
|
|
1261
|
-
n = 1,
|
|
1262
|
-
size = "1024x1024",
|
|
1263
|
-
aspectRatio = "1:1",
|
|
1264
|
-
seed,
|
|
1265
|
-
providerOptions,
|
|
1266
|
-
headers,
|
|
1267
|
-
abortSignal
|
|
1268
|
-
} = options;
|
|
1269
|
-
const warnings = [];
|
|
1270
|
-
if (size != null) {
|
|
1271
|
-
warnings.push({
|
|
1272
|
-
type: "unsupported-setting",
|
|
1273
|
-
setting: "size",
|
|
1274
|
-
details: "This model does not support the `size` option. Use `aspectRatio` instead."
|
|
1275
|
-
});
|
|
1276
|
-
}
|
|
1277
|
-
if (seed != null) {
|
|
1278
|
-
warnings.push({
|
|
1279
|
-
type: "unsupported-setting",
|
|
1280
|
-
setting: "seed",
|
|
1281
|
-
details: "This model does not support the `seed` option through this provider."
|
|
1282
|
-
});
|
|
1283
|
-
}
|
|
1284
|
-
const googleOptions = await parseProviderOptions({
|
|
1285
|
-
provider: "google",
|
|
1286
|
-
providerOptions,
|
|
1287
|
-
schema: googleImageProviderOptionsSchema
|
|
1288
|
-
});
|
|
1289
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1290
|
-
const parameters = {
|
|
1291
|
-
sampleCount: n
|
|
1292
|
-
};
|
|
1293
|
-
if (aspectRatio != null) {
|
|
1294
|
-
parameters.aspectRatio = aspectRatio;
|
|
1295
|
-
}
|
|
1296
|
-
if (googleOptions) {
|
|
1297
|
-
Object.assign(parameters, googleOptions);
|
|
1298
|
-
}
|
|
1299
|
-
const body = {
|
|
1300
|
-
instances: [{ prompt }],
|
|
1301
|
-
parameters
|
|
1302
|
-
};
|
|
1303
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1304
|
-
url: `${this.config.baseURL}/models/${this.modelId}:predict`,
|
|
1305
|
-
headers: combineHeaders(await resolve(this.config.headers), headers),
|
|
1306
|
-
body,
|
|
1307
|
-
failedResponseHandler: googleFailedResponseHandler,
|
|
1308
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
1309
|
-
googleImageResponseSchema
|
|
1310
|
-
),
|
|
1311
|
-
abortSignal,
|
|
1312
|
-
fetch: this.config.fetch
|
|
1313
|
-
});
|
|
1314
|
-
return {
|
|
1315
|
-
images: response.predictions.map(
|
|
1316
|
-
(p) => p.bytesBase64Encoded
|
|
1317
|
-
),
|
|
1318
|
-
warnings: warnings != null ? warnings : [],
|
|
1319
|
-
providerMetadata: {
|
|
1320
|
-
google: {
|
|
1321
|
-
images: response.predictions.map((prediction) => ({
|
|
1322
|
-
// Add any prediction-specific metadata here
|
|
1323
|
-
}))
|
|
1324
|
-
}
|
|
1325
|
-
},
|
|
1326
|
-
response: {
|
|
1327
|
-
timestamp: currentDate,
|
|
1328
|
-
modelId: this.modelId,
|
|
1329
|
-
headers: responseHeaders
|
|
1330
|
-
}
|
|
1331
|
-
};
|
|
1332
|
-
}
|
|
1333
|
-
};
|
|
1334
|
-
var googleImageResponseSchema = z.object({
|
|
1335
|
-
predictions: z.array(z.object({ bytesBase64Encoded: z.string() })).default([])
|
|
1336
|
-
});
|
|
1337
|
-
var googleImageProviderOptionsSchema = z.object({
|
|
1338
|
-
personGeneration: z.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
|
|
1339
|
-
aspectRatio: z.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish()
|
|
1340
|
-
});
|
|
1341
|
-
function createGoogleGenerativeAI(options = {}) {
|
|
1342
|
-
var _a;
|
|
1343
|
-
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
|
1344
|
-
const getHeaders = () => ({
|
|
1345
|
-
"x-goog-api-key": loadApiKey({
|
|
1346
|
-
apiKey: options.apiKey,
|
|
1347
|
-
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
|
1348
|
-
description: "Google Generative AI"
|
|
1349
|
-
}),
|
|
1350
|
-
...options.headers
|
|
1351
|
-
});
|
|
1352
|
-
const createChatModel = (modelId) => {
|
|
1353
|
-
var _a2;
|
|
1354
|
-
return new GoogleGenerativeAILanguageModel(modelId, {
|
|
1355
|
-
provider: "google.generative-ai",
|
|
1356
|
-
baseURL,
|
|
1357
|
-
headers: getHeaders,
|
|
1358
|
-
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
|
1359
|
-
supportedUrls: () => ({
|
|
1360
|
-
"*": [
|
|
1361
|
-
// Google Generative Language "files" endpoint
|
|
1362
|
-
// e.g. https://generativelanguage.googleapis.com/v1beta/files/...
|
|
1363
|
-
new RegExp(`^${baseURL}/files/.*$`),
|
|
1364
|
-
// YouTube URLs (public or unlisted videos)
|
|
1365
|
-
new RegExp(
|
|
1366
|
-
`^https://(?:www\\.)?youtube\\.com/watch\\?v=[\\w-]+(?:&[\\w=&.-]*)?$`
|
|
1367
|
-
),
|
|
1368
|
-
new RegExp(`^https://youtu\\.be/[\\w-]+(?:\\?[\\w=&.-]*)?$`)
|
|
1369
|
-
]
|
|
1370
|
-
}),
|
|
1371
|
-
fetch: options.fetch
|
|
1372
|
-
});
|
|
1373
|
-
};
|
|
1374
|
-
const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
|
|
1375
|
-
provider: "google.generative-ai",
|
|
1376
|
-
baseURL,
|
|
1377
|
-
headers: getHeaders,
|
|
1378
|
-
fetch: options.fetch
|
|
1379
|
-
});
|
|
1380
|
-
const createImageModel = (modelId, settings = {}) => new GoogleGenerativeAIImageModel(modelId, settings, {
|
|
1381
|
-
provider: "google.generative-ai",
|
|
1382
|
-
baseURL,
|
|
1383
|
-
headers: getHeaders,
|
|
1384
|
-
fetch: options.fetch
|
|
1385
|
-
});
|
|
1386
|
-
const provider = function(modelId) {
|
|
1387
|
-
if (new.target) {
|
|
1388
|
-
throw new Error(
|
|
1389
|
-
"The Google Generative AI model function cannot be called with the new keyword."
|
|
1390
|
-
);
|
|
1391
|
-
}
|
|
1392
|
-
return createChatModel(modelId);
|
|
1393
|
-
};
|
|
1394
|
-
provider.languageModel = createChatModel;
|
|
1395
|
-
provider.chat = createChatModel;
|
|
1396
|
-
provider.generativeAI = createChatModel;
|
|
1397
|
-
provider.embedding = createEmbeddingModel;
|
|
1398
|
-
provider.textEmbedding = createEmbeddingModel;
|
|
1399
|
-
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1400
|
-
provider.image = createImageModel;
|
|
1401
|
-
provider.imageModel = createImageModel;
|
|
1402
|
-
provider.tools = googleTools;
|
|
1403
|
-
return provider;
|
|
1404
|
-
}
|
|
1405
|
-
var google = createGoogleGenerativeAI();
|
|
1406
|
-
|
|
1407
|
-
export { createGoogleGenerativeAI, google };
|
|
1408
|
-
//# sourceMappingURL=dist-IUCBLZK6.js.map
|
|
1409
|
-
//# sourceMappingURL=dist-IUCBLZK6.js.map
|