@ai-sdk/google 1.0.4 → 1.0.5
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 +8 -0
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +35 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -15
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +61 -0
- package/internal/dist/index.d.ts +61 -0
- package/internal/dist/index.js +676 -0
- package/internal/dist/index.js.map +1 -0
- package/internal/dist/index.mjs +659 -0
- package/internal/dist/index.mjs.map +1 -0
- package/package.json +10 -3
@@ -0,0 +1,676 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/internal/index.ts
|
21
|
+
var internal_exports = {};
|
22
|
+
__export(internal_exports, {
|
23
|
+
GoogleGenerativeAILanguageModel: () => GoogleGenerativeAILanguageModel
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(internal_exports);
|
26
|
+
|
27
|
+
// src/google-generative-ai-language-model.ts
|
28
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
29
|
+
var import_zod2 = require("zod");
|
30
|
+
|
31
|
+
// src/convert-json-schema-to-openapi-schema.ts
|
32
|
+
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
33
|
+
if (isEmptyObjectSchema(jsonSchema)) {
|
34
|
+
return void 0;
|
35
|
+
}
|
36
|
+
if (typeof jsonSchema === "boolean") {
|
37
|
+
return { type: "boolean", properties: {} };
|
38
|
+
}
|
39
|
+
const {
|
40
|
+
type,
|
41
|
+
description,
|
42
|
+
required,
|
43
|
+
properties,
|
44
|
+
items,
|
45
|
+
allOf,
|
46
|
+
anyOf,
|
47
|
+
oneOf,
|
48
|
+
format,
|
49
|
+
const: constValue,
|
50
|
+
minLength
|
51
|
+
} = jsonSchema;
|
52
|
+
const result = {};
|
53
|
+
if (description)
|
54
|
+
result.description = description;
|
55
|
+
if (required)
|
56
|
+
result.required = required;
|
57
|
+
if (format)
|
58
|
+
result.format = format;
|
59
|
+
if (constValue !== void 0) {
|
60
|
+
result.enum = [constValue];
|
61
|
+
}
|
62
|
+
if (type) {
|
63
|
+
if (Array.isArray(type)) {
|
64
|
+
if (type.includes("null")) {
|
65
|
+
result.type = type.filter((t) => t !== "null")[0];
|
66
|
+
result.nullable = true;
|
67
|
+
} else {
|
68
|
+
result.type = type;
|
69
|
+
}
|
70
|
+
} else if (type === "null") {
|
71
|
+
result.type = "null";
|
72
|
+
} else {
|
73
|
+
result.type = type;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
if (properties != null) {
|
77
|
+
result.properties = Object.entries(properties).reduce(
|
78
|
+
(acc, [key, value]) => {
|
79
|
+
acc[key] = convertJSONSchemaToOpenAPISchema(value);
|
80
|
+
return acc;
|
81
|
+
},
|
82
|
+
{}
|
83
|
+
);
|
84
|
+
}
|
85
|
+
if (items) {
|
86
|
+
result.items = Array.isArray(items) ? items.map(convertJSONSchemaToOpenAPISchema) : convertJSONSchemaToOpenAPISchema(items);
|
87
|
+
}
|
88
|
+
if (allOf) {
|
89
|
+
result.allOf = allOf.map(convertJSONSchemaToOpenAPISchema);
|
90
|
+
}
|
91
|
+
if (anyOf) {
|
92
|
+
result.anyOf = anyOf.map(convertJSONSchemaToOpenAPISchema);
|
93
|
+
}
|
94
|
+
if (oneOf) {
|
95
|
+
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
|
96
|
+
}
|
97
|
+
if (minLength !== void 0)
|
98
|
+
result.minLength = minLength;
|
99
|
+
return result;
|
100
|
+
}
|
101
|
+
function isEmptyObjectSchema(jsonSchema) {
|
102
|
+
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0);
|
103
|
+
}
|
104
|
+
|
105
|
+
// src/convert-to-google-generative-ai-messages.ts
|
106
|
+
var import_provider = require("@ai-sdk/provider");
|
107
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
108
|
+
function convertToGoogleGenerativeAIMessages(prompt) {
|
109
|
+
var _a, _b;
|
110
|
+
const systemInstructionParts = [];
|
111
|
+
const contents = [];
|
112
|
+
let systemMessagesAllowed = true;
|
113
|
+
for (const { role, content } of prompt) {
|
114
|
+
switch (role) {
|
115
|
+
case "system": {
|
116
|
+
if (!systemMessagesAllowed) {
|
117
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
118
|
+
functionality: "system messages are only supported at the beginning of the conversation"
|
119
|
+
});
|
120
|
+
}
|
121
|
+
systemInstructionParts.push({ text: content });
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
case "user": {
|
125
|
+
systemMessagesAllowed = false;
|
126
|
+
const parts = [];
|
127
|
+
for (const part of content) {
|
128
|
+
switch (part.type) {
|
129
|
+
case "text": {
|
130
|
+
parts.push({ text: part.text });
|
131
|
+
break;
|
132
|
+
}
|
133
|
+
case "image": {
|
134
|
+
parts.push(
|
135
|
+
part.image instanceof URL ? {
|
136
|
+
fileData: {
|
137
|
+
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
138
|
+
fileUri: part.image.toString()
|
139
|
+
}
|
140
|
+
} : {
|
141
|
+
inlineData: {
|
142
|
+
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
143
|
+
data: (0, import_provider_utils.convertUint8ArrayToBase64)(part.image)
|
144
|
+
}
|
145
|
+
}
|
146
|
+
);
|
147
|
+
break;
|
148
|
+
}
|
149
|
+
case "file": {
|
150
|
+
parts.push(
|
151
|
+
part.data instanceof URL ? {
|
152
|
+
fileData: {
|
153
|
+
mimeType: part.mimeType,
|
154
|
+
fileUri: part.data.toString()
|
155
|
+
}
|
156
|
+
} : {
|
157
|
+
inlineData: {
|
158
|
+
mimeType: part.mimeType,
|
159
|
+
data: part.data
|
160
|
+
}
|
161
|
+
}
|
162
|
+
);
|
163
|
+
break;
|
164
|
+
}
|
165
|
+
default: {
|
166
|
+
const _exhaustiveCheck = part;
|
167
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
168
|
+
functionality: `prompt part: ${_exhaustiveCheck}`
|
169
|
+
});
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
contents.push({ role: "user", parts });
|
174
|
+
break;
|
175
|
+
}
|
176
|
+
case "assistant": {
|
177
|
+
systemMessagesAllowed = false;
|
178
|
+
contents.push({
|
179
|
+
role: "model",
|
180
|
+
parts: content.map((part) => {
|
181
|
+
switch (part.type) {
|
182
|
+
case "text": {
|
183
|
+
return part.text.length === 0 ? void 0 : { text: part.text };
|
184
|
+
}
|
185
|
+
case "tool-call": {
|
186
|
+
return {
|
187
|
+
functionCall: {
|
188
|
+
name: part.toolName,
|
189
|
+
args: part.args
|
190
|
+
}
|
191
|
+
};
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}).filter(
|
195
|
+
(part) => part !== void 0
|
196
|
+
)
|
197
|
+
});
|
198
|
+
break;
|
199
|
+
}
|
200
|
+
case "tool": {
|
201
|
+
systemMessagesAllowed = false;
|
202
|
+
contents.push({
|
203
|
+
role: "user",
|
204
|
+
parts: content.map((part) => ({
|
205
|
+
functionResponse: {
|
206
|
+
name: part.toolName,
|
207
|
+
response: {
|
208
|
+
name: part.toolName,
|
209
|
+
content: part.result
|
210
|
+
}
|
211
|
+
}
|
212
|
+
}))
|
213
|
+
});
|
214
|
+
break;
|
215
|
+
}
|
216
|
+
default: {
|
217
|
+
const _exhaustiveCheck = role;
|
218
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
return {
|
223
|
+
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
|
224
|
+
contents
|
225
|
+
};
|
226
|
+
}
|
227
|
+
|
228
|
+
// src/get-model-path.ts
|
229
|
+
function getModelPath(modelId) {
|
230
|
+
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
231
|
+
}
|
232
|
+
|
233
|
+
// src/google-error.ts
|
234
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
235
|
+
var import_zod = require("zod");
|
236
|
+
var googleErrorDataSchema = import_zod.z.object({
|
237
|
+
error: import_zod.z.object({
|
238
|
+
code: import_zod.z.number().nullable(),
|
239
|
+
message: import_zod.z.string(),
|
240
|
+
status: import_zod.z.string()
|
241
|
+
})
|
242
|
+
});
|
243
|
+
var googleFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
244
|
+
errorSchema: googleErrorDataSchema,
|
245
|
+
errorToMessage: (data) => data.error.message
|
246
|
+
});
|
247
|
+
|
248
|
+
// src/google-prepare-tools.ts
|
249
|
+
var import_provider2 = require("@ai-sdk/provider");
|
250
|
+
function prepareTools(mode, useSearchGrounding) {
|
251
|
+
var _a, _b;
|
252
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
253
|
+
const toolWarnings = [];
|
254
|
+
if (useSearchGrounding) {
|
255
|
+
return {
|
256
|
+
tools: { googleSearchRetrieval: {} },
|
257
|
+
toolConfig: void 0,
|
258
|
+
toolWarnings
|
259
|
+
};
|
260
|
+
}
|
261
|
+
if (tools == null) {
|
262
|
+
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
263
|
+
}
|
264
|
+
const functionDeclarations = [];
|
265
|
+
for (const tool of tools) {
|
266
|
+
if (tool.type === "provider-defined") {
|
267
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
268
|
+
} else {
|
269
|
+
functionDeclarations.push({
|
270
|
+
name: tool.name,
|
271
|
+
description: (_b = tool.description) != null ? _b : "",
|
272
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
273
|
+
});
|
274
|
+
}
|
275
|
+
}
|
276
|
+
const toolChoice = mode.toolChoice;
|
277
|
+
if (toolChoice == null) {
|
278
|
+
return {
|
279
|
+
tools: { functionDeclarations },
|
280
|
+
toolConfig: void 0,
|
281
|
+
toolWarnings
|
282
|
+
};
|
283
|
+
}
|
284
|
+
const type = toolChoice.type;
|
285
|
+
switch (type) {
|
286
|
+
case "auto":
|
287
|
+
return {
|
288
|
+
tools: { functionDeclarations },
|
289
|
+
toolConfig: { functionCallingConfig: { mode: "AUTO" } },
|
290
|
+
toolWarnings
|
291
|
+
};
|
292
|
+
case "none":
|
293
|
+
return {
|
294
|
+
tools: { functionDeclarations },
|
295
|
+
toolConfig: { functionCallingConfig: { mode: "NONE" } },
|
296
|
+
toolWarnings
|
297
|
+
};
|
298
|
+
case "required":
|
299
|
+
return {
|
300
|
+
tools: { functionDeclarations },
|
301
|
+
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
302
|
+
toolWarnings
|
303
|
+
};
|
304
|
+
case "tool":
|
305
|
+
return {
|
306
|
+
tools: { functionDeclarations },
|
307
|
+
toolConfig: {
|
308
|
+
functionCallingConfig: {
|
309
|
+
mode: "ANY",
|
310
|
+
allowedFunctionNames: [toolChoice.toolName]
|
311
|
+
}
|
312
|
+
},
|
313
|
+
toolWarnings
|
314
|
+
};
|
315
|
+
default: {
|
316
|
+
const _exhaustiveCheck = type;
|
317
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
318
|
+
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
319
|
+
});
|
320
|
+
}
|
321
|
+
}
|
322
|
+
}
|
323
|
+
|
324
|
+
// src/map-google-generative-ai-finish-reason.ts
|
325
|
+
function mapGoogleGenerativeAIFinishReason({
|
326
|
+
finishReason,
|
327
|
+
hasToolCalls
|
328
|
+
}) {
|
329
|
+
switch (finishReason) {
|
330
|
+
case "STOP":
|
331
|
+
return hasToolCalls ? "tool-calls" : "stop";
|
332
|
+
case "MAX_TOKENS":
|
333
|
+
return "length";
|
334
|
+
case "RECITATION":
|
335
|
+
case "SAFETY":
|
336
|
+
return "content-filter";
|
337
|
+
case "FINISH_REASON_UNSPECIFIED":
|
338
|
+
case "OTHER":
|
339
|
+
return "other";
|
340
|
+
default:
|
341
|
+
return "unknown";
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
// src/google-generative-ai-language-model.ts
|
346
|
+
var GoogleGenerativeAILanguageModel = class {
|
347
|
+
constructor(modelId, settings, config) {
|
348
|
+
this.specificationVersion = "v1";
|
349
|
+
this.defaultObjectGenerationMode = "json";
|
350
|
+
this.supportsImageUrls = false;
|
351
|
+
this.modelId = modelId;
|
352
|
+
this.settings = settings;
|
353
|
+
this.config = config;
|
354
|
+
}
|
355
|
+
get supportsStructuredOutputs() {
|
356
|
+
var _a;
|
357
|
+
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
358
|
+
}
|
359
|
+
get provider() {
|
360
|
+
return this.config.provider;
|
361
|
+
}
|
362
|
+
async getArgs({
|
363
|
+
mode,
|
364
|
+
prompt,
|
365
|
+
maxTokens,
|
366
|
+
temperature,
|
367
|
+
topP,
|
368
|
+
topK,
|
369
|
+
frequencyPenalty,
|
370
|
+
presencePenalty,
|
371
|
+
stopSequences,
|
372
|
+
responseFormat,
|
373
|
+
seed
|
374
|
+
}) {
|
375
|
+
var _a, _b;
|
376
|
+
const type = mode.type;
|
377
|
+
const warnings = [];
|
378
|
+
if (seed != null) {
|
379
|
+
warnings.push({
|
380
|
+
type: "unsupported-setting",
|
381
|
+
setting: "seed"
|
382
|
+
});
|
383
|
+
}
|
384
|
+
const generationConfig = {
|
385
|
+
// standardized settings:
|
386
|
+
maxOutputTokens: maxTokens,
|
387
|
+
temperature,
|
388
|
+
topK,
|
389
|
+
topP,
|
390
|
+
frequencyPenalty,
|
391
|
+
presencePenalty,
|
392
|
+
stopSequences,
|
393
|
+
// response format:
|
394
|
+
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
395
|
+
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
396
|
+
// so this is needed as an escape hatch:
|
397
|
+
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0
|
398
|
+
};
|
399
|
+
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
400
|
+
switch (type) {
|
401
|
+
case "regular": {
|
402
|
+
const { tools, toolConfig, toolWarnings } = prepareTools(
|
403
|
+
mode,
|
404
|
+
(_a = this.settings.useSearchGrounding) != null ? _a : false
|
405
|
+
);
|
406
|
+
return {
|
407
|
+
args: {
|
408
|
+
generationConfig,
|
409
|
+
contents,
|
410
|
+
systemInstruction,
|
411
|
+
safetySettings: this.settings.safetySettings,
|
412
|
+
tools,
|
413
|
+
toolConfig,
|
414
|
+
cachedContent: this.settings.cachedContent
|
415
|
+
},
|
416
|
+
warnings: [...warnings, ...toolWarnings]
|
417
|
+
};
|
418
|
+
}
|
419
|
+
case "object-json": {
|
420
|
+
return {
|
421
|
+
args: {
|
422
|
+
generationConfig: {
|
423
|
+
...generationConfig,
|
424
|
+
responseMimeType: "application/json",
|
425
|
+
responseSchema: mode.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
426
|
+
// so this is needed as an escape hatch:
|
427
|
+
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(mode.schema) : void 0
|
428
|
+
},
|
429
|
+
contents,
|
430
|
+
systemInstruction,
|
431
|
+
safetySettings: this.settings.safetySettings,
|
432
|
+
cachedContent: this.settings.cachedContent
|
433
|
+
},
|
434
|
+
warnings
|
435
|
+
};
|
436
|
+
}
|
437
|
+
case "object-tool": {
|
438
|
+
return {
|
439
|
+
args: {
|
440
|
+
generationConfig,
|
441
|
+
contents,
|
442
|
+
tools: {
|
443
|
+
functionDeclarations: [
|
444
|
+
{
|
445
|
+
name: mode.tool.name,
|
446
|
+
description: (_b = mode.tool.description) != null ? _b : "",
|
447
|
+
parameters: convertJSONSchemaToOpenAPISchema(
|
448
|
+
mode.tool.parameters
|
449
|
+
)
|
450
|
+
}
|
451
|
+
]
|
452
|
+
},
|
453
|
+
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
454
|
+
safetySettings: this.settings.safetySettings,
|
455
|
+
cachedContent: this.settings.cachedContent
|
456
|
+
},
|
457
|
+
warnings
|
458
|
+
};
|
459
|
+
}
|
460
|
+
default: {
|
461
|
+
const _exhaustiveCheck = type;
|
462
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
463
|
+
}
|
464
|
+
}
|
465
|
+
}
|
466
|
+
supportsUrl(url) {
|
467
|
+
return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
|
468
|
+
}
|
469
|
+
async doGenerate(options) {
|
470
|
+
var _a, _b, _c, _d, _e, _f;
|
471
|
+
const { args, warnings } = await this.getArgs(options);
|
472
|
+
const body = JSON.stringify(args);
|
473
|
+
const mergedHeaders = (0, import_provider_utils3.combineHeaders)(
|
474
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
475
|
+
options.headers
|
476
|
+
);
|
477
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
478
|
+
url: `${this.config.baseURL}/${getModelPath(
|
479
|
+
this.modelId
|
480
|
+
)}:generateContent`,
|
481
|
+
headers: mergedHeaders,
|
482
|
+
body: args,
|
483
|
+
failedResponseHandler: googleFailedResponseHandler,
|
484
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(responseSchema),
|
485
|
+
abortSignal: options.abortSignal,
|
486
|
+
fetch: this.config.fetch
|
487
|
+
});
|
488
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
489
|
+
const candidate = response.candidates[0];
|
490
|
+
const toolCalls = getToolCallsFromParts({
|
491
|
+
parts: (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [],
|
492
|
+
generateId: this.config.generateId
|
493
|
+
});
|
494
|
+
const usageMetadata = response.usageMetadata;
|
495
|
+
return {
|
496
|
+
text: getTextFromParts((_d = (_c = candidate.content) == null ? void 0 : _c.parts) != null ? _d : []),
|
497
|
+
toolCalls,
|
498
|
+
finishReason: mapGoogleGenerativeAIFinishReason({
|
499
|
+
finishReason: candidate.finishReason,
|
500
|
+
hasToolCalls: toolCalls != null && toolCalls.length > 0
|
501
|
+
}),
|
502
|
+
usage: {
|
503
|
+
promptTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : NaN,
|
504
|
+
completionTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : NaN
|
505
|
+
},
|
506
|
+
rawCall: { rawPrompt, rawSettings },
|
507
|
+
rawResponse: { headers: responseHeaders },
|
508
|
+
warnings,
|
509
|
+
request: { body }
|
510
|
+
};
|
511
|
+
}
|
512
|
+
async doStream(options) {
|
513
|
+
const { args, warnings } = await this.getArgs(options);
|
514
|
+
const body = JSON.stringify(args);
|
515
|
+
const headers = (0, import_provider_utils3.combineHeaders)(
|
516
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
517
|
+
options.headers
|
518
|
+
);
|
519
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
520
|
+
url: `${this.config.baseURL}/${getModelPath(
|
521
|
+
this.modelId
|
522
|
+
)}:streamGenerateContent?alt=sse`,
|
523
|
+
headers,
|
524
|
+
body: args,
|
525
|
+
failedResponseHandler: googleFailedResponseHandler,
|
526
|
+
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(chunkSchema),
|
527
|
+
abortSignal: options.abortSignal,
|
528
|
+
fetch: this.config.fetch
|
529
|
+
});
|
530
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
531
|
+
let finishReason = "unknown";
|
532
|
+
let usage = {
|
533
|
+
promptTokens: Number.NaN,
|
534
|
+
completionTokens: Number.NaN
|
535
|
+
};
|
536
|
+
const generateId = this.config.generateId;
|
537
|
+
let hasToolCalls = false;
|
538
|
+
return {
|
539
|
+
stream: response.pipeThrough(
|
540
|
+
new TransformStream({
|
541
|
+
transform(chunk, controller) {
|
542
|
+
var _a, _b, _c;
|
543
|
+
if (!chunk.success) {
|
544
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
545
|
+
return;
|
546
|
+
}
|
547
|
+
const value = chunk.value;
|
548
|
+
const usageMetadata = value.usageMetadata;
|
549
|
+
if (usageMetadata != null) {
|
550
|
+
usage = {
|
551
|
+
promptTokens: (_a = usageMetadata.promptTokenCount) != null ? _a : NaN,
|
552
|
+
completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
|
553
|
+
};
|
554
|
+
}
|
555
|
+
const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
|
556
|
+
if (candidate == null) {
|
557
|
+
return;
|
558
|
+
}
|
559
|
+
if (candidate.finishReason != null) {
|
560
|
+
finishReason = mapGoogleGenerativeAIFinishReason({
|
561
|
+
finishReason: candidate.finishReason,
|
562
|
+
hasToolCalls
|
563
|
+
});
|
564
|
+
}
|
565
|
+
const content = candidate.content;
|
566
|
+
if (content == null) {
|
567
|
+
return;
|
568
|
+
}
|
569
|
+
const deltaText = getTextFromParts(content.parts);
|
570
|
+
if (deltaText != null) {
|
571
|
+
controller.enqueue({
|
572
|
+
type: "text-delta",
|
573
|
+
textDelta: deltaText
|
574
|
+
});
|
575
|
+
}
|
576
|
+
const toolCallDeltas = getToolCallsFromParts({
|
577
|
+
parts: content.parts,
|
578
|
+
generateId
|
579
|
+
});
|
580
|
+
if (toolCallDeltas != null) {
|
581
|
+
for (const toolCall of toolCallDeltas) {
|
582
|
+
controller.enqueue({
|
583
|
+
type: "tool-call-delta",
|
584
|
+
toolCallType: "function",
|
585
|
+
toolCallId: toolCall.toolCallId,
|
586
|
+
toolName: toolCall.toolName,
|
587
|
+
argsTextDelta: toolCall.args
|
588
|
+
});
|
589
|
+
controller.enqueue({
|
590
|
+
type: "tool-call",
|
591
|
+
toolCallType: "function",
|
592
|
+
toolCallId: toolCall.toolCallId,
|
593
|
+
toolName: toolCall.toolName,
|
594
|
+
args: toolCall.args
|
595
|
+
});
|
596
|
+
hasToolCalls = true;
|
597
|
+
}
|
598
|
+
}
|
599
|
+
},
|
600
|
+
flush(controller) {
|
601
|
+
controller.enqueue({ type: "finish", finishReason, usage });
|
602
|
+
}
|
603
|
+
})
|
604
|
+
),
|
605
|
+
rawCall: { rawPrompt, rawSettings },
|
606
|
+
rawResponse: { headers: responseHeaders },
|
607
|
+
warnings,
|
608
|
+
request: { body }
|
609
|
+
};
|
610
|
+
}
|
611
|
+
};
|
612
|
+
function getToolCallsFromParts({
|
613
|
+
parts,
|
614
|
+
generateId
|
615
|
+
}) {
|
616
|
+
const functionCallParts = parts.filter(
|
617
|
+
(part) => "functionCall" in part
|
618
|
+
);
|
619
|
+
return functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
620
|
+
toolCallType: "function",
|
621
|
+
toolCallId: generateId(),
|
622
|
+
toolName: part.functionCall.name,
|
623
|
+
args: JSON.stringify(part.functionCall.args)
|
624
|
+
}));
|
625
|
+
}
|
626
|
+
function getTextFromParts(parts) {
|
627
|
+
const textParts = parts.filter((part) => "text" in part);
|
628
|
+
return textParts.length === 0 ? void 0 : textParts.map((part) => part.text).join("");
|
629
|
+
}
|
630
|
+
var contentSchema = import_zod2.z.object({
|
631
|
+
role: import_zod2.z.string(),
|
632
|
+
parts: import_zod2.z.array(
|
633
|
+
import_zod2.z.union([
|
634
|
+
import_zod2.z.object({
|
635
|
+
text: import_zod2.z.string()
|
636
|
+
}),
|
637
|
+
import_zod2.z.object({
|
638
|
+
functionCall: import_zod2.z.object({
|
639
|
+
name: import_zod2.z.string(),
|
640
|
+
args: import_zod2.z.unknown()
|
641
|
+
})
|
642
|
+
})
|
643
|
+
])
|
644
|
+
)
|
645
|
+
});
|
646
|
+
var responseSchema = import_zod2.z.object({
|
647
|
+
candidates: import_zod2.z.array(
|
648
|
+
import_zod2.z.object({
|
649
|
+
content: contentSchema.optional(),
|
650
|
+
finishReason: import_zod2.z.string().optional()
|
651
|
+
})
|
652
|
+
),
|
653
|
+
usageMetadata: import_zod2.z.object({
|
654
|
+
promptTokenCount: import_zod2.z.number(),
|
655
|
+
candidatesTokenCount: import_zod2.z.number().nullish(),
|
656
|
+
totalTokenCount: import_zod2.z.number()
|
657
|
+
}).optional()
|
658
|
+
});
|
659
|
+
var chunkSchema = import_zod2.z.object({
|
660
|
+
candidates: import_zod2.z.array(
|
661
|
+
import_zod2.z.object({
|
662
|
+
content: contentSchema.optional(),
|
663
|
+
finishReason: import_zod2.z.string().optional()
|
664
|
+
})
|
665
|
+
).nullish(),
|
666
|
+
usageMetadata: import_zod2.z.object({
|
667
|
+
promptTokenCount: import_zod2.z.number(),
|
668
|
+
candidatesTokenCount: import_zod2.z.number().nullish(),
|
669
|
+
totalTokenCount: import_zod2.z.number()
|
670
|
+
}).nullish()
|
671
|
+
});
|
672
|
+
// Annotate the CommonJS export names for ESM import in node:
|
673
|
+
0 && (module.exports = {
|
674
|
+
GoogleGenerativeAILanguageModel
|
675
|
+
});
|
676
|
+
//# sourceMappingURL=index.js.map
|