@mcp-use/inspector 0.9.0-canary.4 → 0.9.0-canary.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.
Files changed (40) hide show
  1. package/dist/client/assets/__vite-browser-external-DFygW7-s.js +1 -0
  2. package/dist/client/assets/browser-DHySQ_a8.js +408 -0
  3. package/dist/client/assets/display-A5IEINAP-Diuf_-in.js +17 -0
  4. package/dist/client/assets/embeddings-Duw58exF.js +1 -0
  5. package/dist/client/assets/index-8BCSxqXJ.js +20 -0
  6. package/dist/client/assets/index-BDY1EpHw.js +25 -0
  7. package/dist/client/assets/index-C9XlRV8D.js +152 -0
  8. package/dist/client/assets/index-CHOOqKAT.js +1626 -0
  9. package/dist/client/assets/index-Cf47sZoD.css +1 -0
  10. package/dist/client/assets/index-Crh4icJt.js +2 -0
  11. package/dist/client/assets/index-D7lutfXP.js +8 -0
  12. package/dist/client/assets/index-DLT0Msg0.js +7 -0
  13. package/dist/client/assets/index-DRz5BQNA.js +1 -0
  14. package/dist/client/assets/index-JiKw9JSs.js +5 -0
  15. package/dist/client/assets/index-YgPHcJHh.js +33 -0
  16. package/dist/client/assets/path-C9FudP8b.js +1 -0
  17. package/dist/client/assets/transport-wrapper-browser-BI4dDu3b.js +1 -0
  18. package/dist/client/assets/util-t-trnxQI.js +1 -0
  19. package/dist/client/assets/winston-B2ZRVknI.js +37 -0
  20. package/dist/client/index.html +3 -3
  21. package/package.json +2 -2
  22. package/dist/client/assets/__vite-browser-external-CHS79mP1.js +0 -8
  23. package/dist/client/assets/browser-CwknRi82.js +0 -17967
  24. package/dist/client/assets/display-A5IEINAP-DNDlZQ8k.js +0 -480
  25. package/dist/client/assets/embeddings-DSM58WFd.js +0 -26
  26. package/dist/client/assets/index-BMuvzxLw.js +0 -400
  27. package/dist/client/assets/index-BWKKRrN4.js +0 -43965
  28. package/dist/client/assets/index-Br7cLVs6.css +0 -5812
  29. package/dist/client/assets/index-BumNh2YP.js +0 -725
  30. package/dist/client/assets/index-D-pAR_RA.js +0 -26733
  31. package/dist/client/assets/index-DD0wWmuA.js +0 -9991
  32. package/dist/client/assets/index-DEA0KU_h.js +0 -105533
  33. package/dist/client/assets/index-DX0TIfSM.js +0 -102
  34. package/dist/client/assets/index-DctLeaKS.js +0 -2817
  35. package/dist/client/assets/index-DgPlosep.js +0 -1812
  36. package/dist/client/assets/index-DiEpOjl1.js +0 -5370
  37. package/dist/client/assets/path-QsnVvLoj.js +0 -62
  38. package/dist/client/assets/transport-wrapper-browser-ChPHVnHg.js +0 -165
  39. package/dist/client/assets/util-D59LNlyU.js +0 -23
  40. package/dist/client/assets/winston-B_texDHP.js +0 -12307
@@ -1,1812 +0,0 @@
1
- import { j as isInteropZodSchema, H as toJsonSchema, I as isBaseMessage, J as AIMessage, K as ChatGenerationChunk, L as AIMessageChunk, M as ChatMessage, N as isToolMessage, a as isAIMessage, t as isLangChainTool, P as isOpenAITool, Q as isDataContentBlock, S as convertToProviderContentBlock, U as parseBase64DataUrl, V as BaseLLMOutputParser, i as interopSafeParseAsync, O as OutputParserException, W as BaseChatModel, X as getEnvironmentVariable, Y as JsonOutputParser, Z as RunnablePassthrough, $ as RunnableSequence } from "./index-D-pAR_RA.js";
2
- import "./embeddings-DSM58WFd.js";
3
- import "./index-DEA0KU_h.js";
4
- import "./index-DX0TIfSM.js";
5
- function removeAdditionalProperties(obj) {
6
- if (typeof obj === "object" && obj !== null) {
7
- const newObj = { ...obj };
8
- if ("additionalProperties" in newObj) delete newObj.additionalProperties;
9
- if ("$schema" in newObj) delete newObj.$schema;
10
- if ("strict" in newObj) delete newObj.strict;
11
- for (const key in newObj) if (key in newObj) {
12
- if (Array.isArray(newObj[key])) newObj[key] = newObj[key].map(removeAdditionalProperties);
13
- else if (typeof newObj[key] === "object" && newObj[key] !== null) newObj[key] = removeAdditionalProperties(newObj[key]);
14
- }
15
- return newObj;
16
- }
17
- return obj;
18
- }
19
- function schemaToGenerativeAIParameters(schema) {
20
- const jsonSchema = removeAdditionalProperties(isInteropZodSchema(schema) ? toJsonSchema(schema) : schema);
21
- const { $schema, ...rest } = jsonSchema;
22
- return rest;
23
- }
24
- function jsonSchemaToGeminiParameters(schema) {
25
- const jsonSchema = removeAdditionalProperties(schema);
26
- const { $schema, ...rest } = jsonSchema;
27
- return rest;
28
- }
29
- const byteToHex = [];
30
- for (let i = 0; i < 256; ++i) {
31
- byteToHex.push((i + 256).toString(16).slice(1));
32
- }
33
- function unsafeStringify(arr, offset = 0) {
34
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
35
- }
36
- let getRandomValues;
37
- const rnds8 = new Uint8Array(16);
38
- function rng() {
39
- if (!getRandomValues) {
40
- if (typeof crypto === "undefined" || !crypto.getRandomValues) {
41
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
42
- }
43
- getRandomValues = crypto.getRandomValues.bind(crypto);
44
- }
45
- return getRandomValues(rnds8);
46
- }
47
- const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
48
- const native = { randomUUID };
49
- function v4(options, buf, offset) {
50
- if (native.randomUUID && true && !options) {
51
- return native.randomUUID();
52
- }
53
- options = options || {};
54
- const rnds = options.random ?? options.rng?.() ?? rng();
55
- if (rnds.length < 16) {
56
- throw new Error("Random bytes length must be >= 16");
57
- }
58
- rnds[6] = rnds[6] & 15 | 64;
59
- rnds[8] = rnds[8] & 63 | 128;
60
- return unsafeStringify(rnds);
61
- }
62
- function getMessageAuthor(message) {
63
- const type = message._getType();
64
- if (ChatMessage.isInstance(message)) return message.role;
65
- if (type === "tool") return type;
66
- return message.name ?? type;
67
- }
68
- function convertAuthorToRole(author) {
69
- switch (author) {
70
- case "supervisor":
71
- case "ai":
72
- case "model":
73
- return "model";
74
- case "system":
75
- return "system";
76
- case "human":
77
- return "user";
78
- case "tool":
79
- case "function":
80
- return "function";
81
- default:
82
- throw new Error(`Unknown / unsupported author: ${author}`);
83
- }
84
- }
85
- function messageContentMedia(content) {
86
- if ("mimeType" in content && "data" in content) return { inlineData: {
87
- mimeType: content.mimeType,
88
- data: content.data
89
- } };
90
- if ("mimeType" in content && "fileUri" in content) return { fileData: {
91
- mimeType: content.mimeType,
92
- fileUri: content.fileUri
93
- } };
94
- throw new Error("Invalid media content");
95
- }
96
- function inferToolNameFromPreviousMessages(message, previousMessages) {
97
- return previousMessages.map((msg) => {
98
- if (isAIMessage(msg)) return msg.tool_calls ?? [];
99
- return [];
100
- }).flat().find((toolCall) => {
101
- return toolCall.id === message.tool_call_id;
102
- })?.name;
103
- }
104
- function _getStandardContentBlockConverter(isMultimodalModel) {
105
- const standardContentBlockConverter = {
106
- providerName: "Google Gemini",
107
- fromStandardTextBlock(block) {
108
- return { text: block.text };
109
- },
110
- fromStandardImageBlock(block) {
111
- if (!isMultimodalModel) throw new Error("This model does not support images");
112
- if (block.source_type === "url") {
113
- const data = parseBase64DataUrl({ dataUrl: block.url });
114
- if (data) return { inlineData: {
115
- mimeType: data.mime_type,
116
- data: data.data
117
- } };
118
- else return { fileData: {
119
- mimeType: block.mime_type ?? "",
120
- fileUri: block.url
121
- } };
122
- }
123
- if (block.source_type === "base64") return { inlineData: {
124
- mimeType: block.mime_type ?? "",
125
- data: block.data
126
- } };
127
- throw new Error(`Unsupported source type: ${block.source_type}`);
128
- },
129
- fromStandardAudioBlock(block) {
130
- if (!isMultimodalModel) throw new Error("This model does not support audio");
131
- if (block.source_type === "url") {
132
- const data = parseBase64DataUrl({ dataUrl: block.url });
133
- if (data) return { inlineData: {
134
- mimeType: data.mime_type,
135
- data: data.data
136
- } };
137
- else return { fileData: {
138
- mimeType: block.mime_type ?? "",
139
- fileUri: block.url
140
- } };
141
- }
142
- if (block.source_type === "base64") return { inlineData: {
143
- mimeType: block.mime_type ?? "",
144
- data: block.data
145
- } };
146
- throw new Error(`Unsupported source type: ${block.source_type}`);
147
- },
148
- fromStandardFileBlock(block) {
149
- if (!isMultimodalModel) throw new Error("This model does not support files");
150
- if (block.source_type === "text") return { text: block.text };
151
- if (block.source_type === "url") {
152
- const data = parseBase64DataUrl({ dataUrl: block.url });
153
- if (data) return { inlineData: {
154
- mimeType: data.mime_type,
155
- data: data.data
156
- } };
157
- else return { fileData: {
158
- mimeType: block.mime_type ?? "",
159
- fileUri: block.url
160
- } };
161
- }
162
- if (block.source_type === "base64") return { inlineData: {
163
- mimeType: block.mime_type ?? "",
164
- data: block.data
165
- } };
166
- throw new Error(`Unsupported source type: ${block.source_type}`);
167
- }
168
- };
169
- return standardContentBlockConverter;
170
- }
171
- function _convertLangChainContentToPart(content, isMultimodalModel) {
172
- if (isDataContentBlock(content)) return convertToProviderContentBlock(content, _getStandardContentBlockConverter(isMultimodalModel));
173
- if (content.type === "text") return { text: content.text };
174
- else if (content.type === "executableCode") return { executableCode: content.executableCode };
175
- else if (content.type === "codeExecutionResult") return { codeExecutionResult: content.codeExecutionResult };
176
- else if (content.type === "image_url") {
177
- if (!isMultimodalModel) throw new Error(`This model does not support images`);
178
- let source;
179
- if (typeof content.image_url === "string") source = content.image_url;
180
- else if (typeof content.image_url === "object" && "url" in content.image_url) source = content.image_url.url;
181
- else throw new Error("Please provide image as base64 encoded data URL");
182
- const [dm, data] = source.split(",");
183
- if (!dm.startsWith("data:")) throw new Error("Please provide image as base64 encoded data URL");
184
- const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
185
- if (encoding !== "base64") throw new Error("Please provide image as base64 encoded data URL");
186
- return { inlineData: {
187
- data,
188
- mimeType
189
- } };
190
- } else if (content.type === "media") return messageContentMedia(content);
191
- else if (content.type === "tool_use") return { functionCall: {
192
- name: content.name,
193
- args: content.input
194
- } };
195
- else if (content.type?.includes("/") && content.type.split("/").length === 2 && "data" in content && typeof content.data === "string") return { inlineData: {
196
- mimeType: content.type,
197
- data: content.data
198
- } };
199
- else if ("functionCall" in content) return void 0;
200
- else if ("type" in content) throw new Error(`Unknown content type ${content.type}`);
201
- else throw new Error(`Unknown content ${JSON.stringify(content)}`);
202
- }
203
- function convertMessageContentToParts(message, isMultimodalModel, previousMessages) {
204
- if (isToolMessage(message)) {
205
- const messageName = message.name ?? inferToolNameFromPreviousMessages(message, previousMessages);
206
- if (messageName === void 0) throw new Error(`Google requires a tool name for each tool call response, and we could not infer a called tool name for ToolMessage "${message.id}" from your passed messages. Please populate a "name" field on that ToolMessage explicitly.`);
207
- const result = Array.isArray(message.content) ? message.content.map((c) => _convertLangChainContentToPart(c, isMultimodalModel)).filter((p) => p !== void 0) : message.content;
208
- if (message.status === "error") return [{ functionResponse: {
209
- name: messageName,
210
- response: { error: { details: result } }
211
- } }];
212
- return [{ functionResponse: {
213
- name: messageName,
214
- response: { result }
215
- } }];
216
- }
217
- let functionCalls = [];
218
- const messageParts = [];
219
- if (typeof message.content === "string" && message.content) messageParts.push({ text: message.content });
220
- if (Array.isArray(message.content)) messageParts.push(...message.content.map((c) => _convertLangChainContentToPart(c, isMultimodalModel)).filter((p) => p !== void 0));
221
- if (isAIMessage(message) && message.tool_calls?.length) functionCalls = message.tool_calls.map((tc) => {
222
- return { functionCall: {
223
- name: tc.name,
224
- args: tc.args
225
- } };
226
- });
227
- return [...messageParts, ...functionCalls];
228
- }
229
- function convertBaseMessagesToContent(messages, isMultimodalModel, convertSystemMessageToHumanContent = false) {
230
- return messages.reduce((acc, message, index) => {
231
- if (!isBaseMessage(message)) throw new Error("Unsupported message input");
232
- const author = getMessageAuthor(message);
233
- if (author === "system" && index !== 0) throw new Error("System message should be the first one");
234
- const role = convertAuthorToRole(author);
235
- const prevContent = acc.content[acc.content.length];
236
- if (!acc.mergeWithPreviousContent && prevContent && prevContent.role === role) throw new Error("Google Generative AI requires alternate messages between authors");
237
- const parts = convertMessageContentToParts(message, isMultimodalModel, messages.slice(0, index));
238
- if (acc.mergeWithPreviousContent) {
239
- const prevContent$1 = acc.content[acc.content.length - 1];
240
- if (!prevContent$1) throw new Error("There was a problem parsing your system message. Please try a prompt without one.");
241
- prevContent$1.parts.push(...parts);
242
- return {
243
- mergeWithPreviousContent: false,
244
- content: acc.content
245
- };
246
- }
247
- let actualRole = role;
248
- if (actualRole === "function" || actualRole === "system" && !convertSystemMessageToHumanContent) actualRole = "user";
249
- const content = {
250
- role: actualRole,
251
- parts
252
- };
253
- return {
254
- mergeWithPreviousContent: author === "system" && !convertSystemMessageToHumanContent,
255
- content: [...acc.content, content]
256
- };
257
- }, {
258
- content: [],
259
- mergeWithPreviousContent: false
260
- }).content;
261
- }
262
- function mapGenerateContentResultToChatResult(response, extra) {
263
- if (!response.candidates || response.candidates.length === 0 || !response.candidates[0]) return {
264
- generations: [],
265
- llmOutput: { filters: response.promptFeedback }
266
- };
267
- const functionCalls = response.functionCalls();
268
- const [candidate] = response.candidates;
269
- const { content: candidateContent, ...generationInfo } = candidate;
270
- let content;
271
- if (Array.isArray(candidateContent?.parts) && candidateContent.parts.length === 1 && candidateContent.parts[0].text) content = candidateContent.parts[0].text;
272
- else if (Array.isArray(candidateContent?.parts) && candidateContent.parts.length > 0) content = candidateContent.parts.map((p) => {
273
- if ("text" in p) return {
274
- type: "text",
275
- text: p.text
276
- };
277
- else if ("inlineData" in p) return {
278
- type: "inlineData",
279
- inlineData: p.inlineData
280
- };
281
- else if ("functionCall" in p) return {
282
- type: "functionCall",
283
- functionCall: p.functionCall
284
- };
285
- else if ("functionResponse" in p) return {
286
- type: "functionResponse",
287
- functionResponse: p.functionResponse
288
- };
289
- else if ("fileData" in p) return {
290
- type: "fileData",
291
- fileData: p.fileData
292
- };
293
- else if ("executableCode" in p) return {
294
- type: "executableCode",
295
- executableCode: p.executableCode
296
- };
297
- else if ("codeExecutionResult" in p) return {
298
- type: "codeExecutionResult",
299
- codeExecutionResult: p.codeExecutionResult
300
- };
301
- return p;
302
- });
303
- else content = [];
304
- let text = "";
305
- if (typeof content === "string") text = content;
306
- else if (Array.isArray(content) && content.length > 0) {
307
- const block = content.find((b) => "text" in b);
308
- text = block?.text ?? text;
309
- }
310
- const generation = {
311
- text,
312
- message: new AIMessage({
313
- content: content ?? "",
314
- tool_calls: functionCalls?.map((fc) => {
315
- return {
316
- ...fc,
317
- type: "tool_call",
318
- id: "id" in fc && typeof fc.id === "string" ? fc.id : v4()
319
- };
320
- }),
321
- additional_kwargs: { ...generationInfo },
322
- usage_metadata: extra?.usageMetadata
323
- }),
324
- generationInfo
325
- };
326
- return {
327
- generations: [generation],
328
- llmOutput: { tokenUsage: {
329
- promptTokens: extra?.usageMetadata?.input_tokens,
330
- completionTokens: extra?.usageMetadata?.output_tokens,
331
- totalTokens: extra?.usageMetadata?.total_tokens
332
- } }
333
- };
334
- }
335
- function convertResponseContentToChatGenerationChunk(response, extra) {
336
- if (!response.candidates || response.candidates.length === 0) return null;
337
- const functionCalls = response.functionCalls();
338
- const [candidate] = response.candidates;
339
- const { content: candidateContent, ...generationInfo } = candidate;
340
- let content;
341
- if (Array.isArray(candidateContent?.parts) && candidateContent.parts.every((p) => "text" in p)) content = candidateContent.parts.map((p) => p.text).join("");
342
- else if (Array.isArray(candidateContent?.parts)) content = candidateContent.parts.map((p) => {
343
- if ("text" in p) return {
344
- type: "text",
345
- text: p.text
346
- };
347
- else if ("inlineData" in p) return {
348
- type: "inlineData",
349
- inlineData: p.inlineData
350
- };
351
- else if ("functionCall" in p) return {
352
- type: "functionCall",
353
- functionCall: p.functionCall
354
- };
355
- else if ("functionResponse" in p) return {
356
- type: "functionResponse",
357
- functionResponse: p.functionResponse
358
- };
359
- else if ("fileData" in p) return {
360
- type: "fileData",
361
- fileData: p.fileData
362
- };
363
- else if ("executableCode" in p) return {
364
- type: "executableCode",
365
- executableCode: p.executableCode
366
- };
367
- else if ("codeExecutionResult" in p) return {
368
- type: "codeExecutionResult",
369
- codeExecutionResult: p.codeExecutionResult
370
- };
371
- return p;
372
- });
373
- else content = [];
374
- let text = "";
375
- if (content && typeof content === "string") text = content;
376
- else if (Array.isArray(content)) {
377
- const block = content.find((b) => "text" in b);
378
- text = block?.text ?? "";
379
- }
380
- const toolCallChunks = [];
381
- if (functionCalls) toolCallChunks.push(...functionCalls.map((fc) => ({
382
- ...fc,
383
- args: JSON.stringify(fc.args),
384
- index: extra.index,
385
- type: "tool_call_chunk",
386
- id: "id" in fc && typeof fc.id === "string" ? fc.id : v4()
387
- })));
388
- return new ChatGenerationChunk({
389
- text,
390
- message: new AIMessageChunk({
391
- content: content || "",
392
- name: !candidateContent ? void 0 : candidateContent.role,
393
- tool_call_chunks: toolCallChunks,
394
- additional_kwargs: {},
395
- response_metadata: { model_provider: "google-genai" },
396
- usage_metadata: extra.usageMetadata
397
- }),
398
- generationInfo
399
- });
400
- }
401
- function convertToGenerativeAITools(tools) {
402
- if (tools.every((tool) => "functionDeclarations" in tool && Array.isArray(tool.functionDeclarations))) return tools;
403
- return [{ functionDeclarations: tools.map((tool) => {
404
- if (isLangChainTool(tool)) {
405
- const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
406
- if (jsonSchema.type === "object" && "properties" in jsonSchema && Object.keys(jsonSchema.properties).length === 0) return {
407
- name: tool.name,
408
- description: tool.description
409
- };
410
- return {
411
- name: tool.name,
412
- description: tool.description,
413
- parameters: jsonSchema
414
- };
415
- }
416
- if (isOpenAITool(tool)) return {
417
- name: tool.function.name,
418
- description: tool.function.description ?? `A function available to call.`,
419
- parameters: jsonSchemaToGeminiParameters(tool.function.parameters)
420
- };
421
- return tool;
422
- }) }];
423
- }
424
- var GoogleGenerativeAIToolsOutputParser = class extends BaseLLMOutputParser {
425
- static lc_name() {
426
- return "GoogleGenerativeAIToolsOutputParser";
427
- }
428
- lc_namespace = [
429
- "langchain",
430
- "google_genai",
431
- "output_parsers"
432
- ];
433
- returnId = false;
434
- /** The type of tool calls to return. */
435
- keyName;
436
- /** Whether to return only the first tool call. */
437
- returnSingle = false;
438
- zodSchema;
439
- constructor(params) {
440
- super(params);
441
- this.keyName = params.keyName;
442
- this.returnSingle = params.returnSingle ?? this.returnSingle;
443
- this.zodSchema = params.zodSchema;
444
- }
445
- async _validateResult(result) {
446
- if (this.zodSchema === void 0) return result;
447
- const zodParsedResult = await interopSafeParseAsync(this.zodSchema, result);
448
- if (zodParsedResult.success) return zodParsedResult.data;
449
- else throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.issues)}`, JSON.stringify(result, null, 2));
450
- }
451
- async parseResult(generations) {
452
- const tools = generations.flatMap((generation) => {
453
- const { message } = generation;
454
- if (!("tool_calls" in message) || !Array.isArray(message.tool_calls)) return [];
455
- return message.tool_calls;
456
- });
457
- if (tools[0] === void 0) throw new Error("No parseable tool calls provided to GoogleGenerativeAIToolsOutputParser.");
458
- const [tool] = tools;
459
- const validatedResult = await this._validateResult(tool.args);
460
- return validatedResult;
461
- }
462
- };
463
- var SchemaType;
464
- (function(SchemaType2) {
465
- SchemaType2["STRING"] = "string";
466
- SchemaType2["NUMBER"] = "number";
467
- SchemaType2["INTEGER"] = "integer";
468
- SchemaType2["BOOLEAN"] = "boolean";
469
- SchemaType2["ARRAY"] = "array";
470
- SchemaType2["OBJECT"] = "object";
471
- })(SchemaType || (SchemaType = {}));
472
- var ExecutableCodeLanguage;
473
- (function(ExecutableCodeLanguage2) {
474
- ExecutableCodeLanguage2["LANGUAGE_UNSPECIFIED"] = "language_unspecified";
475
- ExecutableCodeLanguage2["PYTHON"] = "python";
476
- })(ExecutableCodeLanguage || (ExecutableCodeLanguage = {}));
477
- var Outcome;
478
- (function(Outcome2) {
479
- Outcome2["OUTCOME_UNSPECIFIED"] = "outcome_unspecified";
480
- Outcome2["OUTCOME_OK"] = "outcome_ok";
481
- Outcome2["OUTCOME_FAILED"] = "outcome_failed";
482
- Outcome2["OUTCOME_DEADLINE_EXCEEDED"] = "outcome_deadline_exceeded";
483
- })(Outcome || (Outcome = {}));
484
- const POSSIBLE_ROLES = ["user", "model", "function", "system"];
485
- var HarmCategory;
486
- (function(HarmCategory2) {
487
- HarmCategory2["HARM_CATEGORY_UNSPECIFIED"] = "HARM_CATEGORY_UNSPECIFIED";
488
- HarmCategory2["HARM_CATEGORY_HATE_SPEECH"] = "HARM_CATEGORY_HATE_SPEECH";
489
- HarmCategory2["HARM_CATEGORY_SEXUALLY_EXPLICIT"] = "HARM_CATEGORY_SEXUALLY_EXPLICIT";
490
- HarmCategory2["HARM_CATEGORY_HARASSMENT"] = "HARM_CATEGORY_HARASSMENT";
491
- HarmCategory2["HARM_CATEGORY_DANGEROUS_CONTENT"] = "HARM_CATEGORY_DANGEROUS_CONTENT";
492
- HarmCategory2["HARM_CATEGORY_CIVIC_INTEGRITY"] = "HARM_CATEGORY_CIVIC_INTEGRITY";
493
- })(HarmCategory || (HarmCategory = {}));
494
- var HarmBlockThreshold;
495
- (function(HarmBlockThreshold2) {
496
- HarmBlockThreshold2["HARM_BLOCK_THRESHOLD_UNSPECIFIED"] = "HARM_BLOCK_THRESHOLD_UNSPECIFIED";
497
- HarmBlockThreshold2["BLOCK_LOW_AND_ABOVE"] = "BLOCK_LOW_AND_ABOVE";
498
- HarmBlockThreshold2["BLOCK_MEDIUM_AND_ABOVE"] = "BLOCK_MEDIUM_AND_ABOVE";
499
- HarmBlockThreshold2["BLOCK_ONLY_HIGH"] = "BLOCK_ONLY_HIGH";
500
- HarmBlockThreshold2["BLOCK_NONE"] = "BLOCK_NONE";
501
- })(HarmBlockThreshold || (HarmBlockThreshold = {}));
502
- var HarmProbability;
503
- (function(HarmProbability2) {
504
- HarmProbability2["HARM_PROBABILITY_UNSPECIFIED"] = "HARM_PROBABILITY_UNSPECIFIED";
505
- HarmProbability2["NEGLIGIBLE"] = "NEGLIGIBLE";
506
- HarmProbability2["LOW"] = "LOW";
507
- HarmProbability2["MEDIUM"] = "MEDIUM";
508
- HarmProbability2["HIGH"] = "HIGH";
509
- })(HarmProbability || (HarmProbability = {}));
510
- var BlockReason;
511
- (function(BlockReason2) {
512
- BlockReason2["BLOCKED_REASON_UNSPECIFIED"] = "BLOCKED_REASON_UNSPECIFIED";
513
- BlockReason2["SAFETY"] = "SAFETY";
514
- BlockReason2["OTHER"] = "OTHER";
515
- })(BlockReason || (BlockReason = {}));
516
- var FinishReason;
517
- (function(FinishReason2) {
518
- FinishReason2["FINISH_REASON_UNSPECIFIED"] = "FINISH_REASON_UNSPECIFIED";
519
- FinishReason2["STOP"] = "STOP";
520
- FinishReason2["MAX_TOKENS"] = "MAX_TOKENS";
521
- FinishReason2["SAFETY"] = "SAFETY";
522
- FinishReason2["RECITATION"] = "RECITATION";
523
- FinishReason2["LANGUAGE"] = "LANGUAGE";
524
- FinishReason2["BLOCKLIST"] = "BLOCKLIST";
525
- FinishReason2["PROHIBITED_CONTENT"] = "PROHIBITED_CONTENT";
526
- FinishReason2["SPII"] = "SPII";
527
- FinishReason2["MALFORMED_FUNCTION_CALL"] = "MALFORMED_FUNCTION_CALL";
528
- FinishReason2["OTHER"] = "OTHER";
529
- })(FinishReason || (FinishReason = {}));
530
- var TaskType;
531
- (function(TaskType2) {
532
- TaskType2["TASK_TYPE_UNSPECIFIED"] = "TASK_TYPE_UNSPECIFIED";
533
- TaskType2["RETRIEVAL_QUERY"] = "RETRIEVAL_QUERY";
534
- TaskType2["RETRIEVAL_DOCUMENT"] = "RETRIEVAL_DOCUMENT";
535
- TaskType2["SEMANTIC_SIMILARITY"] = "SEMANTIC_SIMILARITY";
536
- TaskType2["CLASSIFICATION"] = "CLASSIFICATION";
537
- TaskType2["CLUSTERING"] = "CLUSTERING";
538
- })(TaskType || (TaskType = {}));
539
- var FunctionCallingMode;
540
- (function(FunctionCallingMode2) {
541
- FunctionCallingMode2["MODE_UNSPECIFIED"] = "MODE_UNSPECIFIED";
542
- FunctionCallingMode2["AUTO"] = "AUTO";
543
- FunctionCallingMode2["ANY"] = "ANY";
544
- FunctionCallingMode2["NONE"] = "NONE";
545
- })(FunctionCallingMode || (FunctionCallingMode = {}));
546
- var DynamicRetrievalMode;
547
- (function(DynamicRetrievalMode2) {
548
- DynamicRetrievalMode2["MODE_UNSPECIFIED"] = "MODE_UNSPECIFIED";
549
- DynamicRetrievalMode2["MODE_DYNAMIC"] = "MODE_DYNAMIC";
550
- })(DynamicRetrievalMode || (DynamicRetrievalMode = {}));
551
- class GoogleGenerativeAIError extends Error {
552
- constructor(message) {
553
- super(`[GoogleGenerativeAI Error]: ${message}`);
554
- }
555
- }
556
- class GoogleGenerativeAIResponseError extends GoogleGenerativeAIError {
557
- constructor(message, response) {
558
- super(message);
559
- this.response = response;
560
- }
561
- }
562
- class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError {
563
- constructor(message, status, statusText, errorDetails) {
564
- super(message);
565
- this.status = status;
566
- this.statusText = statusText;
567
- this.errorDetails = errorDetails;
568
- }
569
- }
570
- class GoogleGenerativeAIRequestInputError extends GoogleGenerativeAIError {
571
- }
572
- class GoogleGenerativeAIAbortError extends GoogleGenerativeAIError {
573
- }
574
- const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com";
575
- const DEFAULT_API_VERSION = "v1beta";
576
- const PACKAGE_VERSION = "0.24.1";
577
- const PACKAGE_LOG_HEADER = "genai-js";
578
- var Task;
579
- (function(Task2) {
580
- Task2["GENERATE_CONTENT"] = "generateContent";
581
- Task2["STREAM_GENERATE_CONTENT"] = "streamGenerateContent";
582
- Task2["COUNT_TOKENS"] = "countTokens";
583
- Task2["EMBED_CONTENT"] = "embedContent";
584
- Task2["BATCH_EMBED_CONTENTS"] = "batchEmbedContents";
585
- })(Task || (Task = {}));
586
- class RequestUrl {
587
- constructor(model, task, apiKey, stream, requestOptions) {
588
- this.model = model;
589
- this.task = task;
590
- this.apiKey = apiKey;
591
- this.stream = stream;
592
- this.requestOptions = requestOptions;
593
- }
594
- toString() {
595
- var _a, _b;
596
- const apiVersion = ((_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.apiVersion) || DEFAULT_API_VERSION;
597
- const baseUrl = ((_b = this.requestOptions) === null || _b === void 0 ? void 0 : _b.baseUrl) || DEFAULT_BASE_URL;
598
- let url = `${baseUrl}/${apiVersion}/${this.model}:${this.task}`;
599
- if (this.stream) {
600
- url += "?alt=sse";
601
- }
602
- return url;
603
- }
604
- }
605
- function getClientHeaders(requestOptions) {
606
- const clientHeaders = [];
607
- if (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient) {
608
- clientHeaders.push(requestOptions.apiClient);
609
- }
610
- clientHeaders.push(`${PACKAGE_LOG_HEADER}/${PACKAGE_VERSION}`);
611
- return clientHeaders.join(" ");
612
- }
613
- async function getHeaders(url) {
614
- var _a;
615
- const headers = new Headers();
616
- headers.append("Content-Type", "application/json");
617
- headers.append("x-goog-api-client", getClientHeaders(url.requestOptions));
618
- headers.append("x-goog-api-key", url.apiKey);
619
- let customHeaders = (_a = url.requestOptions) === null || _a === void 0 ? void 0 : _a.customHeaders;
620
- if (customHeaders) {
621
- if (!(customHeaders instanceof Headers)) {
622
- try {
623
- customHeaders = new Headers(customHeaders);
624
- } catch (e) {
625
- throw new GoogleGenerativeAIRequestInputError(`unable to convert customHeaders value ${JSON.stringify(customHeaders)} to Headers: ${e.message}`);
626
- }
627
- }
628
- for (const [headerName, headerValue] of customHeaders.entries()) {
629
- if (headerName === "x-goog-api-key") {
630
- throw new GoogleGenerativeAIRequestInputError(`Cannot set reserved header name ${headerName}`);
631
- } else if (headerName === "x-goog-api-client") {
632
- throw new GoogleGenerativeAIRequestInputError(`Header name ${headerName} can only be set using the apiClient field`);
633
- }
634
- headers.append(headerName, headerValue);
635
- }
636
- }
637
- return headers;
638
- }
639
- async function constructModelRequest(model, task, apiKey, stream, body, requestOptions) {
640
- const url = new RequestUrl(model, task, apiKey, stream, requestOptions);
641
- return {
642
- url: url.toString(),
643
- fetchOptions: Object.assign(Object.assign({}, buildFetchOptions(requestOptions)), { method: "POST", headers: await getHeaders(url), body })
644
- };
645
- }
646
- async function makeModelRequest(model, task, apiKey, stream, body, requestOptions = {}, fetchFn = fetch) {
647
- const { url, fetchOptions } = await constructModelRequest(model, task, apiKey, stream, body, requestOptions);
648
- return makeRequest(url, fetchOptions, fetchFn);
649
- }
650
- async function makeRequest(url, fetchOptions, fetchFn = fetch) {
651
- let response;
652
- try {
653
- response = await fetchFn(url, fetchOptions);
654
- } catch (e) {
655
- handleResponseError(e, url);
656
- }
657
- if (!response.ok) {
658
- await handleResponseNotOk(response, url);
659
- }
660
- return response;
661
- }
662
- function handleResponseError(e, url) {
663
- let err = e;
664
- if (err.name === "AbortError") {
665
- err = new GoogleGenerativeAIAbortError(`Request aborted when fetching ${url.toString()}: ${e.message}`);
666
- err.stack = e.stack;
667
- } else if (!(e instanceof GoogleGenerativeAIFetchError || e instanceof GoogleGenerativeAIRequestInputError)) {
668
- err = new GoogleGenerativeAIError(`Error fetching from ${url.toString()}: ${e.message}`);
669
- err.stack = e.stack;
670
- }
671
- throw err;
672
- }
673
- async function handleResponseNotOk(response, url) {
674
- let message = "";
675
- let errorDetails;
676
- try {
677
- const json = await response.json();
678
- message = json.error.message;
679
- if (json.error.details) {
680
- message += ` ${JSON.stringify(json.error.details)}`;
681
- errorDetails = json.error.details;
682
- }
683
- } catch (e) {
684
- }
685
- throw new GoogleGenerativeAIFetchError(`Error fetching from ${url.toString()}: [${response.status} ${response.statusText}] ${message}`, response.status, response.statusText, errorDetails);
686
- }
687
- function buildFetchOptions(requestOptions) {
688
- const fetchOptions = {};
689
- if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.signal) !== void 0 || (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeout) >= 0) {
690
- const controller = new AbortController();
691
- if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeout) >= 0) {
692
- setTimeout(() => controller.abort(), requestOptions.timeout);
693
- }
694
- if (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.signal) {
695
- requestOptions.signal.addEventListener("abort", () => {
696
- controller.abort();
697
- });
698
- }
699
- fetchOptions.signal = controller.signal;
700
- }
701
- return fetchOptions;
702
- }
703
- function addHelpers(response) {
704
- response.text = () => {
705
- if (response.candidates && response.candidates.length > 0) {
706
- if (response.candidates.length > 1) {
707
- console.warn(`This response had ${response.candidates.length} candidates. Returning text from the first candidate only. Access response.candidates directly to use the other candidates.`);
708
- }
709
- if (hadBadFinishReason(response.candidates[0])) {
710
- throw new GoogleGenerativeAIResponseError(`${formatBlockErrorMessage(response)}`, response);
711
- }
712
- return getText(response);
713
- } else if (response.promptFeedback) {
714
- throw new GoogleGenerativeAIResponseError(`Text not available. ${formatBlockErrorMessage(response)}`, response);
715
- }
716
- return "";
717
- };
718
- response.functionCall = () => {
719
- if (response.candidates && response.candidates.length > 0) {
720
- if (response.candidates.length > 1) {
721
- console.warn(`This response had ${response.candidates.length} candidates. Returning function calls from the first candidate only. Access response.candidates directly to use the other candidates.`);
722
- }
723
- if (hadBadFinishReason(response.candidates[0])) {
724
- throw new GoogleGenerativeAIResponseError(`${formatBlockErrorMessage(response)}`, response);
725
- }
726
- console.warn(`response.functionCall() is deprecated. Use response.functionCalls() instead.`);
727
- return getFunctionCalls(response)[0];
728
- } else if (response.promptFeedback) {
729
- throw new GoogleGenerativeAIResponseError(`Function call not available. ${formatBlockErrorMessage(response)}`, response);
730
- }
731
- return void 0;
732
- };
733
- response.functionCalls = () => {
734
- if (response.candidates && response.candidates.length > 0) {
735
- if (response.candidates.length > 1) {
736
- console.warn(`This response had ${response.candidates.length} candidates. Returning function calls from the first candidate only. Access response.candidates directly to use the other candidates.`);
737
- }
738
- if (hadBadFinishReason(response.candidates[0])) {
739
- throw new GoogleGenerativeAIResponseError(`${formatBlockErrorMessage(response)}`, response);
740
- }
741
- return getFunctionCalls(response);
742
- } else if (response.promptFeedback) {
743
- throw new GoogleGenerativeAIResponseError(`Function call not available. ${formatBlockErrorMessage(response)}`, response);
744
- }
745
- return void 0;
746
- };
747
- return response;
748
- }
749
- function getText(response) {
750
- var _a, _b, _c, _d;
751
- const textStrings = [];
752
- if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
753
- for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) {
754
- if (part.text) {
755
- textStrings.push(part.text);
756
- }
757
- if (part.executableCode) {
758
- textStrings.push("\n```" + part.executableCode.language + "\n" + part.executableCode.code + "\n```\n");
759
- }
760
- if (part.codeExecutionResult) {
761
- textStrings.push("\n```\n" + part.codeExecutionResult.output + "\n```\n");
762
- }
763
- }
764
- }
765
- if (textStrings.length > 0) {
766
- return textStrings.join("");
767
- } else {
768
- return "";
769
- }
770
- }
771
- function getFunctionCalls(response) {
772
- var _a, _b, _c, _d;
773
- const functionCalls = [];
774
- if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) {
775
- for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) {
776
- if (part.functionCall) {
777
- functionCalls.push(part.functionCall);
778
- }
779
- }
780
- }
781
- if (functionCalls.length > 0) {
782
- return functionCalls;
783
- } else {
784
- return void 0;
785
- }
786
- }
787
- const badFinishReasons = [
788
- FinishReason.RECITATION,
789
- FinishReason.SAFETY,
790
- FinishReason.LANGUAGE
791
- ];
792
- function hadBadFinishReason(candidate) {
793
- return !!candidate.finishReason && badFinishReasons.includes(candidate.finishReason);
794
- }
795
- function formatBlockErrorMessage(response) {
796
- var _a, _b, _c;
797
- let message = "";
798
- if ((!response.candidates || response.candidates.length === 0) && response.promptFeedback) {
799
- message += "Response was blocked";
800
- if ((_a = response.promptFeedback) === null || _a === void 0 ? void 0 : _a.blockReason) {
801
- message += ` due to ${response.promptFeedback.blockReason}`;
802
- }
803
- if ((_b = response.promptFeedback) === null || _b === void 0 ? void 0 : _b.blockReasonMessage) {
804
- message += `: ${response.promptFeedback.blockReasonMessage}`;
805
- }
806
- } else if ((_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0]) {
807
- const firstCandidate = response.candidates[0];
808
- if (hadBadFinishReason(firstCandidate)) {
809
- message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
810
- if (firstCandidate.finishMessage) {
811
- message += `: ${firstCandidate.finishMessage}`;
812
- }
813
- }
814
- }
815
- return message;
816
- }
817
- function __await(v) {
818
- return this instanceof __await ? (this.v = v, this) : new __await(v);
819
- }
820
- function __asyncGenerator(thisArg, _arguments, generator) {
821
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
822
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
823
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function() {
824
- return this;
825
- }, i;
826
- function verb(n) {
827
- if (g[n]) i[n] = function(v) {
828
- return new Promise(function(a, b) {
829
- q.push([n, v, a, b]) > 1 || resume(n, v);
830
- });
831
- };
832
- }
833
- function resume(n, v) {
834
- try {
835
- step(g[n](v));
836
- } catch (e) {
837
- settle(q[0][3], e);
838
- }
839
- }
840
- function step(r) {
841
- r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r);
842
- }
843
- function fulfill(value) {
844
- resume("next", value);
845
- }
846
- function reject(value) {
847
- resume("throw", value);
848
- }
849
- function settle(f, v) {
850
- if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]);
851
- }
852
- }
853
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
854
- var e = new Error(message);
855
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
856
- };
857
- const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
858
- function processStream(response) {
859
- const inputStream = response.body.pipeThrough(new TextDecoderStream("utf8", { fatal: true }));
860
- const responseStream = getResponseStream(inputStream);
861
- const [stream1, stream2] = responseStream.tee();
862
- return {
863
- stream: generateResponseSequence(stream1),
864
- response: getResponsePromise(stream2)
865
- };
866
- }
867
- async function getResponsePromise(stream) {
868
- const allResponses = [];
869
- const reader = stream.getReader();
870
- while (true) {
871
- const { done, value } = await reader.read();
872
- if (done) {
873
- return addHelpers(aggregateResponses(allResponses));
874
- }
875
- allResponses.push(value);
876
- }
877
- }
878
- function generateResponseSequence(stream) {
879
- return __asyncGenerator(this, arguments, function* generateResponseSequence_1() {
880
- const reader = stream.getReader();
881
- while (true) {
882
- const { value, done } = yield __await(reader.read());
883
- if (done) {
884
- break;
885
- }
886
- yield yield __await(addHelpers(value));
887
- }
888
- });
889
- }
890
- function getResponseStream(inputStream) {
891
- const reader = inputStream.getReader();
892
- const stream = new ReadableStream({
893
- start(controller) {
894
- let currentText = "";
895
- return pump();
896
- function pump() {
897
- return reader.read().then(({ value, done }) => {
898
- if (done) {
899
- if (currentText.trim()) {
900
- controller.error(new GoogleGenerativeAIError("Failed to parse stream"));
901
- return;
902
- }
903
- controller.close();
904
- return;
905
- }
906
- currentText += value;
907
- let match = currentText.match(responseLineRE);
908
- let parsedResponse;
909
- while (match) {
910
- try {
911
- parsedResponse = JSON.parse(match[1]);
912
- } catch (e) {
913
- controller.error(new GoogleGenerativeAIError(`Error parsing JSON response: "${match[1]}"`));
914
- return;
915
- }
916
- controller.enqueue(parsedResponse);
917
- currentText = currentText.substring(match[0].length);
918
- match = currentText.match(responseLineRE);
919
- }
920
- return pump();
921
- }).catch((e) => {
922
- let err = e;
923
- err.stack = e.stack;
924
- if (err.name === "AbortError") {
925
- err = new GoogleGenerativeAIAbortError("Request aborted when reading from the stream");
926
- } else {
927
- err = new GoogleGenerativeAIError("Error reading from the stream");
928
- }
929
- throw err;
930
- });
931
- }
932
- }
933
- });
934
- return stream;
935
- }
936
- function aggregateResponses(responses) {
937
- const lastResponse = responses[responses.length - 1];
938
- const aggregatedResponse = {
939
- promptFeedback: lastResponse === null || lastResponse === void 0 ? void 0 : lastResponse.promptFeedback
940
- };
941
- for (const response of responses) {
942
- if (response.candidates) {
943
- let candidateIndex = 0;
944
- for (const candidate of response.candidates) {
945
- if (!aggregatedResponse.candidates) {
946
- aggregatedResponse.candidates = [];
947
- }
948
- if (!aggregatedResponse.candidates[candidateIndex]) {
949
- aggregatedResponse.candidates[candidateIndex] = {
950
- index: candidateIndex
951
- };
952
- }
953
- aggregatedResponse.candidates[candidateIndex].citationMetadata = candidate.citationMetadata;
954
- aggregatedResponse.candidates[candidateIndex].groundingMetadata = candidate.groundingMetadata;
955
- aggregatedResponse.candidates[candidateIndex].finishReason = candidate.finishReason;
956
- aggregatedResponse.candidates[candidateIndex].finishMessage = candidate.finishMessage;
957
- aggregatedResponse.candidates[candidateIndex].safetyRatings = candidate.safetyRatings;
958
- if (candidate.content && candidate.content.parts) {
959
- if (!aggregatedResponse.candidates[candidateIndex].content) {
960
- aggregatedResponse.candidates[candidateIndex].content = {
961
- role: candidate.content.role || "user",
962
- parts: []
963
- };
964
- }
965
- const newPart = {};
966
- for (const part of candidate.content.parts) {
967
- if (part.text) {
968
- newPart.text = part.text;
969
- }
970
- if (part.functionCall) {
971
- newPart.functionCall = part.functionCall;
972
- }
973
- if (part.executableCode) {
974
- newPart.executableCode = part.executableCode;
975
- }
976
- if (part.codeExecutionResult) {
977
- newPart.codeExecutionResult = part.codeExecutionResult;
978
- }
979
- if (Object.keys(newPart).length === 0) {
980
- newPart.text = "";
981
- }
982
- aggregatedResponse.candidates[candidateIndex].content.parts.push(newPart);
983
- }
984
- }
985
- }
986
- candidateIndex++;
987
- }
988
- if (response.usageMetadata) {
989
- aggregatedResponse.usageMetadata = response.usageMetadata;
990
- }
991
- }
992
- return aggregatedResponse;
993
- }
994
- async function generateContentStream(apiKey, model, params, requestOptions) {
995
- const response = await makeModelRequest(
996
- model,
997
- Task.STREAM_GENERATE_CONTENT,
998
- apiKey,
999
- /* stream */
1000
- true,
1001
- JSON.stringify(params),
1002
- requestOptions
1003
- );
1004
- return processStream(response);
1005
- }
1006
- async function generateContent(apiKey, model, params, requestOptions) {
1007
- const response = await makeModelRequest(
1008
- model,
1009
- Task.GENERATE_CONTENT,
1010
- apiKey,
1011
- /* stream */
1012
- false,
1013
- JSON.stringify(params),
1014
- requestOptions
1015
- );
1016
- const responseJson = await response.json();
1017
- const enhancedResponse = addHelpers(responseJson);
1018
- return {
1019
- response: enhancedResponse
1020
- };
1021
- }
1022
- function formatSystemInstruction(input) {
1023
- if (input == null) {
1024
- return void 0;
1025
- } else if (typeof input === "string") {
1026
- return { role: "system", parts: [{ text: input }] };
1027
- } else if (input.text) {
1028
- return { role: "system", parts: [input] };
1029
- } else if (input.parts) {
1030
- if (!input.role) {
1031
- return { role: "system", parts: input.parts };
1032
- } else {
1033
- return input;
1034
- }
1035
- }
1036
- }
1037
- function formatNewContent(request) {
1038
- let newParts = [];
1039
- if (typeof request === "string") {
1040
- newParts = [{ text: request }];
1041
- } else {
1042
- for (const partOrString of request) {
1043
- if (typeof partOrString === "string") {
1044
- newParts.push({ text: partOrString });
1045
- } else {
1046
- newParts.push(partOrString);
1047
- }
1048
- }
1049
- }
1050
- return assignRoleToPartsAndValidateSendMessageRequest(newParts);
1051
- }
1052
- function assignRoleToPartsAndValidateSendMessageRequest(parts) {
1053
- const userContent = { role: "user", parts: [] };
1054
- const functionContent = { role: "function", parts: [] };
1055
- let hasUserContent = false;
1056
- let hasFunctionContent = false;
1057
- for (const part of parts) {
1058
- if ("functionResponse" in part) {
1059
- functionContent.parts.push(part);
1060
- hasFunctionContent = true;
1061
- } else {
1062
- userContent.parts.push(part);
1063
- hasUserContent = true;
1064
- }
1065
- }
1066
- if (hasUserContent && hasFunctionContent) {
1067
- throw new GoogleGenerativeAIError("Within a single message, FunctionResponse cannot be mixed with other type of part in the request for sending chat message.");
1068
- }
1069
- if (!hasUserContent && !hasFunctionContent) {
1070
- throw new GoogleGenerativeAIError("No content is provided for sending chat message.");
1071
- }
1072
- if (hasUserContent) {
1073
- return userContent;
1074
- }
1075
- return functionContent;
1076
- }
1077
- function formatCountTokensInput(params, modelParams) {
1078
- var _a;
1079
- let formattedGenerateContentRequest = {
1080
- model: modelParams === null || modelParams === void 0 ? void 0 : modelParams.model,
1081
- generationConfig: modelParams === null || modelParams === void 0 ? void 0 : modelParams.generationConfig,
1082
- safetySettings: modelParams === null || modelParams === void 0 ? void 0 : modelParams.safetySettings,
1083
- tools: modelParams === null || modelParams === void 0 ? void 0 : modelParams.tools,
1084
- toolConfig: modelParams === null || modelParams === void 0 ? void 0 : modelParams.toolConfig,
1085
- systemInstruction: modelParams === null || modelParams === void 0 ? void 0 : modelParams.systemInstruction,
1086
- cachedContent: (_a = modelParams === null || modelParams === void 0 ? void 0 : modelParams.cachedContent) === null || _a === void 0 ? void 0 : _a.name,
1087
- contents: []
1088
- };
1089
- const containsGenerateContentRequest = params.generateContentRequest != null;
1090
- if (params.contents) {
1091
- if (containsGenerateContentRequest) {
1092
- throw new GoogleGenerativeAIRequestInputError("CountTokensRequest must have one of contents or generateContentRequest, not both.");
1093
- }
1094
- formattedGenerateContentRequest.contents = params.contents;
1095
- } else if (containsGenerateContentRequest) {
1096
- formattedGenerateContentRequest = Object.assign(Object.assign({}, formattedGenerateContentRequest), params.generateContentRequest);
1097
- } else {
1098
- const content = formatNewContent(params);
1099
- formattedGenerateContentRequest.contents = [content];
1100
- }
1101
- return { generateContentRequest: formattedGenerateContentRequest };
1102
- }
1103
- function formatGenerateContentInput(params) {
1104
- let formattedRequest;
1105
- if (params.contents) {
1106
- formattedRequest = params;
1107
- } else {
1108
- const content = formatNewContent(params);
1109
- formattedRequest = { contents: [content] };
1110
- }
1111
- if (params.systemInstruction) {
1112
- formattedRequest.systemInstruction = formatSystemInstruction(params.systemInstruction);
1113
- }
1114
- return formattedRequest;
1115
- }
1116
- function formatEmbedContentInput(params) {
1117
- if (typeof params === "string" || Array.isArray(params)) {
1118
- const content = formatNewContent(params);
1119
- return { content };
1120
- }
1121
- return params;
1122
- }
1123
- const VALID_PART_FIELDS = [
1124
- "text",
1125
- "inlineData",
1126
- "functionCall",
1127
- "functionResponse",
1128
- "executableCode",
1129
- "codeExecutionResult"
1130
- ];
1131
- const VALID_PARTS_PER_ROLE = {
1132
- user: ["text", "inlineData"],
1133
- function: ["functionResponse"],
1134
- model: ["text", "functionCall", "executableCode", "codeExecutionResult"],
1135
- // System instructions shouldn't be in history anyway.
1136
- system: ["text"]
1137
- };
1138
- function validateChatHistory(history) {
1139
- let prevContent = false;
1140
- for (const currContent of history) {
1141
- const { role, parts } = currContent;
1142
- if (!prevContent && role !== "user") {
1143
- throw new GoogleGenerativeAIError(`First content should be with role 'user', got ${role}`);
1144
- }
1145
- if (!POSSIBLE_ROLES.includes(role)) {
1146
- throw new GoogleGenerativeAIError(`Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
1147
- }
1148
- if (!Array.isArray(parts)) {
1149
- throw new GoogleGenerativeAIError("Content should have 'parts' property with an array of Parts");
1150
- }
1151
- if (parts.length === 0) {
1152
- throw new GoogleGenerativeAIError("Each Content should have at least one part");
1153
- }
1154
- const countFields = {
1155
- text: 0,
1156
- inlineData: 0,
1157
- functionCall: 0,
1158
- functionResponse: 0,
1159
- fileData: 0,
1160
- executableCode: 0,
1161
- codeExecutionResult: 0
1162
- };
1163
- for (const part of parts) {
1164
- for (const key of VALID_PART_FIELDS) {
1165
- if (key in part) {
1166
- countFields[key] += 1;
1167
- }
1168
- }
1169
- }
1170
- const validParts = VALID_PARTS_PER_ROLE[role];
1171
- for (const key of VALID_PART_FIELDS) {
1172
- if (!validParts.includes(key) && countFields[key] > 0) {
1173
- throw new GoogleGenerativeAIError(`Content with role '${role}' can't contain '${key}' part`);
1174
- }
1175
- }
1176
- prevContent = true;
1177
- }
1178
- }
1179
- function isValidResponse(response) {
1180
- var _a;
1181
- if (response.candidates === void 0 || response.candidates.length === 0) {
1182
- return false;
1183
- }
1184
- const content = (_a = response.candidates[0]) === null || _a === void 0 ? void 0 : _a.content;
1185
- if (content === void 0) {
1186
- return false;
1187
- }
1188
- if (content.parts === void 0 || content.parts.length === 0) {
1189
- return false;
1190
- }
1191
- for (const part of content.parts) {
1192
- if (part === void 0 || Object.keys(part).length === 0) {
1193
- return false;
1194
- }
1195
- if (part.text !== void 0 && part.text === "") {
1196
- return false;
1197
- }
1198
- }
1199
- return true;
1200
- }
1201
- const SILENT_ERROR = "SILENT_ERROR";
1202
- class ChatSession {
1203
- constructor(apiKey, model, params, _requestOptions = {}) {
1204
- this.model = model;
1205
- this.params = params;
1206
- this._requestOptions = _requestOptions;
1207
- this._history = [];
1208
- this._sendPromise = Promise.resolve();
1209
- this._apiKey = apiKey;
1210
- if (params === null || params === void 0 ? void 0 : params.history) {
1211
- validateChatHistory(params.history);
1212
- this._history = params.history;
1213
- }
1214
- }
1215
- /**
1216
- * Gets the chat history so far. Blocked prompts are not added to history.
1217
- * Blocked candidates are not added to history, nor are the prompts that
1218
- * generated them.
1219
- */
1220
- async getHistory() {
1221
- await this._sendPromise;
1222
- return this._history;
1223
- }
1224
- /**
1225
- * Sends a chat message and receives a non-streaming
1226
- * {@link GenerateContentResult}.
1227
- *
1228
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1229
- * take precedence over the {@link RequestOptions} values provided to
1230
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1231
- */
1232
- async sendMessage(request, requestOptions = {}) {
1233
- var _a, _b, _c, _d, _e, _f;
1234
- await this._sendPromise;
1235
- const newContent = formatNewContent(request);
1236
- const generateContentRequest = {
1237
- safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
1238
- generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
1239
- tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
1240
- toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
1241
- systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
1242
- cachedContent: (_f = this.params) === null || _f === void 0 ? void 0 : _f.cachedContent,
1243
- contents: [...this._history, newContent]
1244
- };
1245
- const chatSessionRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1246
- let finalResult;
1247
- this._sendPromise = this._sendPromise.then(() => generateContent(this._apiKey, this.model, generateContentRequest, chatSessionRequestOptions)).then((result) => {
1248
- var _a2;
1249
- if (isValidResponse(result.response)) {
1250
- this._history.push(newContent);
1251
- const responseContent = Object.assign({
1252
- parts: [],
1253
- // Response seems to come back without a role set.
1254
- role: "model"
1255
- }, (_a2 = result.response.candidates) === null || _a2 === void 0 ? void 0 : _a2[0].content);
1256
- this._history.push(responseContent);
1257
- } else {
1258
- const blockErrorMessage = formatBlockErrorMessage(result.response);
1259
- if (blockErrorMessage) {
1260
- console.warn(`sendMessage() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
1261
- }
1262
- }
1263
- finalResult = result;
1264
- }).catch((e) => {
1265
- this._sendPromise = Promise.resolve();
1266
- throw e;
1267
- });
1268
- await this._sendPromise;
1269
- return finalResult;
1270
- }
1271
- /**
1272
- * Sends a chat message and receives the response as a
1273
- * {@link GenerateContentStreamResult} containing an iterable stream
1274
- * and a response promise.
1275
- *
1276
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1277
- * take precedence over the {@link RequestOptions} values provided to
1278
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1279
- */
1280
- async sendMessageStream(request, requestOptions = {}) {
1281
- var _a, _b, _c, _d, _e, _f;
1282
- await this._sendPromise;
1283
- const newContent = formatNewContent(request);
1284
- const generateContentRequest = {
1285
- safetySettings: (_a = this.params) === null || _a === void 0 ? void 0 : _a.safetySettings,
1286
- generationConfig: (_b = this.params) === null || _b === void 0 ? void 0 : _b.generationConfig,
1287
- tools: (_c = this.params) === null || _c === void 0 ? void 0 : _c.tools,
1288
- toolConfig: (_d = this.params) === null || _d === void 0 ? void 0 : _d.toolConfig,
1289
- systemInstruction: (_e = this.params) === null || _e === void 0 ? void 0 : _e.systemInstruction,
1290
- cachedContent: (_f = this.params) === null || _f === void 0 ? void 0 : _f.cachedContent,
1291
- contents: [...this._history, newContent]
1292
- };
1293
- const chatSessionRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1294
- const streamPromise = generateContentStream(this._apiKey, this.model, generateContentRequest, chatSessionRequestOptions);
1295
- this._sendPromise = this._sendPromise.then(() => streamPromise).catch((_ignored) => {
1296
- throw new Error(SILENT_ERROR);
1297
- }).then((streamResult) => streamResult.response).then((response) => {
1298
- if (isValidResponse(response)) {
1299
- this._history.push(newContent);
1300
- const responseContent = Object.assign({}, response.candidates[0].content);
1301
- if (!responseContent.role) {
1302
- responseContent.role = "model";
1303
- }
1304
- this._history.push(responseContent);
1305
- } else {
1306
- const blockErrorMessage = formatBlockErrorMessage(response);
1307
- if (blockErrorMessage) {
1308
- console.warn(`sendMessageStream() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
1309
- }
1310
- }
1311
- }).catch((e) => {
1312
- if (e.message !== SILENT_ERROR) {
1313
- console.error(e);
1314
- }
1315
- });
1316
- return streamPromise;
1317
- }
1318
- }
1319
- async function countTokens(apiKey, model, params, singleRequestOptions) {
1320
- const response = await makeModelRequest(model, Task.COUNT_TOKENS, apiKey, false, JSON.stringify(params), singleRequestOptions);
1321
- return response.json();
1322
- }
1323
- async function embedContent(apiKey, model, params, requestOptions) {
1324
- const response = await makeModelRequest(model, Task.EMBED_CONTENT, apiKey, false, JSON.stringify(params), requestOptions);
1325
- return response.json();
1326
- }
1327
- async function batchEmbedContents(apiKey, model, params, requestOptions) {
1328
- const requestsWithModel = params.requests.map((request) => {
1329
- return Object.assign(Object.assign({}, request), { model });
1330
- });
1331
- const response = await makeModelRequest(model, Task.BATCH_EMBED_CONTENTS, apiKey, false, JSON.stringify({ requests: requestsWithModel }), requestOptions);
1332
- return response.json();
1333
- }
1334
- class GenerativeModel {
1335
- constructor(apiKey, modelParams, _requestOptions = {}) {
1336
- this.apiKey = apiKey;
1337
- this._requestOptions = _requestOptions;
1338
- if (modelParams.model.includes("/")) {
1339
- this.model = modelParams.model;
1340
- } else {
1341
- this.model = `models/${modelParams.model}`;
1342
- }
1343
- this.generationConfig = modelParams.generationConfig || {};
1344
- this.safetySettings = modelParams.safetySettings || [];
1345
- this.tools = modelParams.tools;
1346
- this.toolConfig = modelParams.toolConfig;
1347
- this.systemInstruction = formatSystemInstruction(modelParams.systemInstruction);
1348
- this.cachedContent = modelParams.cachedContent;
1349
- }
1350
- /**
1351
- * Makes a single non-streaming call to the model
1352
- * and returns an object containing a single {@link GenerateContentResponse}.
1353
- *
1354
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1355
- * take precedence over the {@link RequestOptions} values provided to
1356
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1357
- */
1358
- async generateContent(request, requestOptions = {}) {
1359
- var _a;
1360
- const formattedParams = formatGenerateContentInput(request);
1361
- const generativeModelRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1362
- return generateContent(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings, tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: (_a = this.cachedContent) === null || _a === void 0 ? void 0 : _a.name }, formattedParams), generativeModelRequestOptions);
1363
- }
1364
- /**
1365
- * Makes a single streaming call to the model and returns an object
1366
- * containing an iterable stream that iterates over all chunks in the
1367
- * streaming response as well as a promise that returns the final
1368
- * aggregated response.
1369
- *
1370
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1371
- * take precedence over the {@link RequestOptions} values provided to
1372
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1373
- */
1374
- async generateContentStream(request, requestOptions = {}) {
1375
- var _a;
1376
- const formattedParams = formatGenerateContentInput(request);
1377
- const generativeModelRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1378
- return generateContentStream(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings, tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: (_a = this.cachedContent) === null || _a === void 0 ? void 0 : _a.name }, formattedParams), generativeModelRequestOptions);
1379
- }
1380
- /**
1381
- * Gets a new {@link ChatSession} instance which can be used for
1382
- * multi-turn chats.
1383
- */
1384
- startChat(startChatParams) {
1385
- var _a;
1386
- return new ChatSession(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings, tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: (_a = this.cachedContent) === null || _a === void 0 ? void 0 : _a.name }, startChatParams), this._requestOptions);
1387
- }
1388
- /**
1389
- * Counts the tokens in the provided request.
1390
- *
1391
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1392
- * take precedence over the {@link RequestOptions} values provided to
1393
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1394
- */
1395
- async countTokens(request, requestOptions = {}) {
1396
- const formattedParams = formatCountTokensInput(request, {
1397
- model: this.model,
1398
- generationConfig: this.generationConfig,
1399
- safetySettings: this.safetySettings,
1400
- tools: this.tools,
1401
- toolConfig: this.toolConfig,
1402
- systemInstruction: this.systemInstruction,
1403
- cachedContent: this.cachedContent
1404
- });
1405
- const generativeModelRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1406
- return countTokens(this.apiKey, this.model, formattedParams, generativeModelRequestOptions);
1407
- }
1408
- /**
1409
- * Embeds the provided content.
1410
- *
1411
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1412
- * take precedence over the {@link RequestOptions} values provided to
1413
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1414
- */
1415
- async embedContent(request, requestOptions = {}) {
1416
- const formattedParams = formatEmbedContentInput(request);
1417
- const generativeModelRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1418
- return embedContent(this.apiKey, this.model, formattedParams, generativeModelRequestOptions);
1419
- }
1420
- /**
1421
- * Embeds an array of {@link EmbedContentRequest}s.
1422
- *
1423
- * Fields set in the optional {@link SingleRequestOptions} parameter will
1424
- * take precedence over the {@link RequestOptions} values provided to
1425
- * {@link GoogleGenerativeAI.getGenerativeModel }.
1426
- */
1427
- async batchEmbedContents(batchEmbedContentRequest, requestOptions = {}) {
1428
- const generativeModelRequestOptions = Object.assign(Object.assign({}, this._requestOptions), requestOptions);
1429
- return batchEmbedContents(this.apiKey, this.model, batchEmbedContentRequest, generativeModelRequestOptions);
1430
- }
1431
- }
1432
- class GoogleGenerativeAI {
1433
- constructor(apiKey) {
1434
- this.apiKey = apiKey;
1435
- }
1436
- /**
1437
- * Gets a {@link GenerativeModel} instance for the provided model name.
1438
- */
1439
- getGenerativeModel(modelParams, requestOptions) {
1440
- if (!modelParams.model) {
1441
- throw new GoogleGenerativeAIError(`Must provide a model name. Example: genai.getGenerativeModel({ model: 'my-model-name' })`);
1442
- }
1443
- return new GenerativeModel(this.apiKey, modelParams, requestOptions);
1444
- }
1445
- /**
1446
- * Creates a {@link GenerativeModel} instance from provided content cache.
1447
- */
1448
- getGenerativeModelFromCachedContent(cachedContent, modelParams, requestOptions) {
1449
- if (!cachedContent.name) {
1450
- throw new GoogleGenerativeAIRequestInputError("Cached content must contain a `name` field.");
1451
- }
1452
- if (!cachedContent.model) {
1453
- throw new GoogleGenerativeAIRequestInputError("Cached content must contain a `model` field.");
1454
- }
1455
- const disallowedDuplicates = ["model", "systemInstruction"];
1456
- for (const key of disallowedDuplicates) {
1457
- if ((modelParams === null || modelParams === void 0 ? void 0 : modelParams[key]) && cachedContent[key] && (modelParams === null || modelParams === void 0 ? void 0 : modelParams[key]) !== cachedContent[key]) {
1458
- if (key === "model") {
1459
- const modelParamsComp = modelParams.model.startsWith("models/") ? modelParams.model.replace("models/", "") : modelParams.model;
1460
- const cachedContentComp = cachedContent.model.startsWith("models/") ? cachedContent.model.replace("models/", "") : cachedContent.model;
1461
- if (modelParamsComp === cachedContentComp) {
1462
- continue;
1463
- }
1464
- }
1465
- throw new GoogleGenerativeAIRequestInputError(`Different value for "${key}" specified in modelParams (${modelParams[key]}) and cachedContent (${cachedContent[key]})`);
1466
- }
1467
- }
1468
- const modelParamsFromCache = Object.assign(Object.assign({}, modelParams), { model: cachedContent.model, tools: cachedContent.tools, toolConfig: cachedContent.toolConfig, systemInstruction: cachedContent.systemInstruction, cachedContent });
1469
- return new GenerativeModel(this.apiKey, modelParamsFromCache, requestOptions);
1470
- }
1471
- }
1472
- function convertToolsToGenAI(tools, extra) {
1473
- const genAITools = processTools(tools);
1474
- const toolConfig = createToolConfig(genAITools, extra);
1475
- return {
1476
- tools: genAITools,
1477
- toolConfig
1478
- };
1479
- }
1480
- function processTools(tools) {
1481
- let functionDeclarationTools = [];
1482
- const genAITools = [];
1483
- tools.forEach((tool) => {
1484
- if (isLangChainTool(tool)) {
1485
- const [convertedTool] = convertToGenerativeAITools([tool]);
1486
- if (convertedTool.functionDeclarations) functionDeclarationTools.push(...convertedTool.functionDeclarations);
1487
- } else if (isOpenAITool(tool)) {
1488
- const { functionDeclarations } = convertOpenAIToolToGenAI(tool);
1489
- if (functionDeclarations) functionDeclarationTools.push(...functionDeclarations);
1490
- else throw new Error("Failed to convert OpenAI structured tool to GenerativeAI tool");
1491
- } else genAITools.push(tool);
1492
- });
1493
- const genAIFunctionDeclaration = genAITools.find((t) => "functionDeclarations" in t);
1494
- if (genAIFunctionDeclaration) return genAITools.map((tool) => {
1495
- if (functionDeclarationTools?.length > 0 && "functionDeclarations" in tool) {
1496
- const newTool = { functionDeclarations: [...tool.functionDeclarations || [], ...functionDeclarationTools] };
1497
- functionDeclarationTools = [];
1498
- return newTool;
1499
- }
1500
- return tool;
1501
- });
1502
- return [...genAITools, ...functionDeclarationTools.length > 0 ? [{ functionDeclarations: functionDeclarationTools }] : []];
1503
- }
1504
- function convertOpenAIToolToGenAI(tool) {
1505
- return { functionDeclarations: [{
1506
- name: tool.function.name,
1507
- description: tool.function.description,
1508
- parameters: removeAdditionalProperties(tool.function.parameters)
1509
- }] };
1510
- }
1511
- function createToolConfig(genAITools, extra) {
1512
- if (!genAITools.length || !extra) return void 0;
1513
- const { toolChoice, allowedFunctionNames } = extra;
1514
- const modeMap = {
1515
- any: FunctionCallingMode.ANY,
1516
- auto: FunctionCallingMode.AUTO,
1517
- none: FunctionCallingMode.NONE
1518
- };
1519
- if (toolChoice && [
1520
- "any",
1521
- "auto",
1522
- "none"
1523
- ].includes(toolChoice)) return { functionCallingConfig: {
1524
- mode: modeMap[toolChoice] ?? "MODE_UNSPECIFIED",
1525
- allowedFunctionNames
1526
- } };
1527
- if (typeof toolChoice === "string" || allowedFunctionNames) return { functionCallingConfig: {
1528
- mode: FunctionCallingMode.ANY,
1529
- allowedFunctionNames: [...allowedFunctionNames ?? [], ...toolChoice && typeof toolChoice === "string" ? [toolChoice] : []]
1530
- } };
1531
- return void 0;
1532
- }
1533
- var ChatGoogleGenerativeAI = class extends BaseChatModel {
1534
- static lc_name() {
1535
- return "ChatGoogleGenerativeAI";
1536
- }
1537
- lc_serializable = true;
1538
- get lc_secrets() {
1539
- return { apiKey: "GOOGLE_API_KEY" };
1540
- }
1541
- lc_namespace = [
1542
- "langchain",
1543
- "chat_models",
1544
- "google_genai"
1545
- ];
1546
- get lc_aliases() {
1547
- return { apiKey: "google_api_key" };
1548
- }
1549
- model;
1550
- temperature;
1551
- maxOutputTokens;
1552
- topP;
1553
- topK;
1554
- stopSequences = [];
1555
- safetySettings;
1556
- apiKey;
1557
- streaming = false;
1558
- json;
1559
- streamUsage = true;
1560
- convertSystemMessageToHumanContent;
1561
- client;
1562
- get _isMultimodalModel() {
1563
- return this.model.includes("vision") || this.model.startsWith("gemini-1.5") || this.model.startsWith("gemini-2") || this.model.startsWith("gemma-3-") && !this.model.startsWith("gemma-3-1b");
1564
- }
1565
- constructor(fields) {
1566
- super(fields);
1567
- this.model = fields.model.replace(/^models\//, "");
1568
- this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;
1569
- if (this.maxOutputTokens && this.maxOutputTokens < 0) throw new Error("`maxOutputTokens` must be a positive integer");
1570
- this.temperature = fields.temperature ?? this.temperature;
1571
- if (this.temperature && (this.temperature < 0 || this.temperature > 2)) throw new Error("`temperature` must be in the range of [0.0,2.0]");
1572
- this.topP = fields.topP ?? this.topP;
1573
- if (this.topP && this.topP < 0) throw new Error("`topP` must be a positive integer");
1574
- if (this.topP && this.topP > 1) throw new Error("`topP` must be below 1.");
1575
- this.topK = fields.topK ?? this.topK;
1576
- if (this.topK && this.topK < 0) throw new Error("`topK` must be a positive integer");
1577
- this.stopSequences = fields.stopSequences ?? this.stopSequences;
1578
- this.apiKey = fields.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
1579
- if (!this.apiKey) throw new Error("Please set an API key for Google GenerativeAI in the environment variable GOOGLE_API_KEY or in the `apiKey` field of the ChatGoogleGenerativeAI constructor");
1580
- this.safetySettings = fields.safetySettings ?? this.safetySettings;
1581
- if (this.safetySettings && this.safetySettings.length > 0) {
1582
- const safetySettingsSet = new Set(this.safetySettings.map((s) => s.category));
1583
- if (safetySettingsSet.size !== this.safetySettings.length) throw new Error("The categories in `safetySettings` array must be unique");
1584
- }
1585
- this.streaming = fields.streaming ?? this.streaming;
1586
- this.json = fields.json;
1587
- this.client = new GoogleGenerativeAI(this.apiKey).getGenerativeModel({
1588
- model: this.model,
1589
- safetySettings: this.safetySettings,
1590
- generationConfig: {
1591
- stopSequences: this.stopSequences,
1592
- maxOutputTokens: this.maxOutputTokens,
1593
- temperature: this.temperature,
1594
- topP: this.topP,
1595
- topK: this.topK,
1596
- ...this.json ? { responseMimeType: "application/json" } : {}
1597
- }
1598
- }, {
1599
- apiVersion: fields.apiVersion,
1600
- baseUrl: fields.baseUrl
1601
- });
1602
- this.streamUsage = fields.streamUsage ?? this.streamUsage;
1603
- }
1604
- useCachedContent(cachedContent, modelParams, requestOptions) {
1605
- if (!this.apiKey) return;
1606
- this.client = new GoogleGenerativeAI(this.apiKey).getGenerativeModelFromCachedContent(cachedContent, modelParams, requestOptions);
1607
- }
1608
- get useSystemInstruction() {
1609
- return typeof this.convertSystemMessageToHumanContent === "boolean" ? !this.convertSystemMessageToHumanContent : this.computeUseSystemInstruction;
1610
- }
1611
- get computeUseSystemInstruction() {
1612
- if (this.model === "gemini-1.0-pro-001") return false;
1613
- else if (this.model.startsWith("gemini-pro-vision")) return false;
1614
- else if (this.model.startsWith("gemini-1.0-pro-vision")) return false;
1615
- else if (this.model === "gemini-pro") return false;
1616
- return true;
1617
- }
1618
- getLsParams(options) {
1619
- return {
1620
- ls_provider: "google_genai",
1621
- ls_model_name: this.model,
1622
- ls_model_type: "chat",
1623
- ls_temperature: this.client.generationConfig.temperature,
1624
- ls_max_tokens: this.client.generationConfig.maxOutputTokens,
1625
- ls_stop: options.stop
1626
- };
1627
- }
1628
- _combineLLMOutput() {
1629
- return [];
1630
- }
1631
- _llmType() {
1632
- return "googlegenerativeai";
1633
- }
1634
- bindTools(tools, kwargs) {
1635
- return this.withConfig({
1636
- tools: convertToolsToGenAI(tools)?.tools,
1637
- ...kwargs
1638
- });
1639
- }
1640
- invocationParams(options) {
1641
- const toolsAndConfig = options?.tools?.length ? convertToolsToGenAI(options.tools, {
1642
- toolChoice: options.tool_choice,
1643
- allowedFunctionNames: options.allowedFunctionNames
1644
- }) : void 0;
1645
- if (options?.responseSchema) {
1646
- this.client.generationConfig.responseSchema = options.responseSchema;
1647
- this.client.generationConfig.responseMimeType = "application/json";
1648
- } else {
1649
- this.client.generationConfig.responseSchema = void 0;
1650
- this.client.generationConfig.responseMimeType = this.json ? "application/json" : void 0;
1651
- }
1652
- return {
1653
- ...toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {},
1654
- ...toolsAndConfig?.toolConfig ? { toolConfig: toolsAndConfig.toolConfig } : {}
1655
- };
1656
- }
1657
- async _generate(messages, options, runManager) {
1658
- const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
1659
- let actualPrompt = prompt;
1660
- if (prompt[0].role === "system") {
1661
- const [systemInstruction] = prompt;
1662
- this.client.systemInstruction = systemInstruction;
1663
- actualPrompt = prompt.slice(1);
1664
- }
1665
- const parameters = this.invocationParams(options);
1666
- if (this.streaming) {
1667
- const tokenUsage = {};
1668
- const stream = this._streamResponseChunks(messages, options, runManager);
1669
- const finalChunks = {};
1670
- for await (const chunk of stream) {
1671
- const index = chunk.generationInfo?.completion ?? 0;
1672
- if (finalChunks[index] === void 0) finalChunks[index] = chunk;
1673
- else finalChunks[index] = finalChunks[index].concat(chunk);
1674
- }
1675
- const generations = Object.entries(finalChunks).sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10)).map(([_, value]) => value);
1676
- return {
1677
- generations,
1678
- llmOutput: { estimatedTokenUsage: tokenUsage }
1679
- };
1680
- }
1681
- const res = await this.completionWithRetry({
1682
- ...parameters,
1683
- contents: actualPrompt
1684
- });
1685
- let usageMetadata;
1686
- if ("usageMetadata" in res.response) {
1687
- const genAIUsageMetadata = res.response.usageMetadata;
1688
- usageMetadata = {
1689
- input_tokens: genAIUsageMetadata.promptTokenCount ?? 0,
1690
- output_tokens: genAIUsageMetadata.candidatesTokenCount ?? 0,
1691
- total_tokens: genAIUsageMetadata.totalTokenCount ?? 0
1692
- };
1693
- }
1694
- const generationResult = mapGenerateContentResultToChatResult(res.response, { usageMetadata });
1695
- if (generationResult.generations?.length > 0) await runManager?.handleLLMNewToken(generationResult.generations[0]?.text ?? "");
1696
- return generationResult;
1697
- }
1698
- async *_streamResponseChunks(messages, options, runManager) {
1699
- const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
1700
- let actualPrompt = prompt;
1701
- if (prompt[0].role === "system") {
1702
- const [systemInstruction] = prompt;
1703
- this.client.systemInstruction = systemInstruction;
1704
- actualPrompt = prompt.slice(1);
1705
- }
1706
- const parameters = this.invocationParams(options);
1707
- const request = {
1708
- ...parameters,
1709
- contents: actualPrompt
1710
- };
1711
- const stream = await this.caller.callWithOptions({ signal: options?.signal }, async () => {
1712
- const { stream: stream$1 } = await this.client.generateContentStream(request);
1713
- return stream$1;
1714
- });
1715
- let usageMetadata;
1716
- let prevPromptTokenCount = 0;
1717
- let prevCandidatesTokenCount = 0;
1718
- let prevTotalTokenCount = 0;
1719
- let index = 0;
1720
- for await (const response of stream) {
1721
- if ("usageMetadata" in response && response.usageMetadata !== void 0 && this.streamUsage !== false && options.streamUsage !== false) {
1722
- usageMetadata = {
1723
- input_tokens: response.usageMetadata.promptTokenCount ?? 0,
1724
- output_tokens: response.usageMetadata.candidatesTokenCount ?? 0,
1725
- total_tokens: response.usageMetadata.totalTokenCount ?? 0
1726
- };
1727
- const newPromptTokenCount = response.usageMetadata.promptTokenCount ?? 0;
1728
- usageMetadata.input_tokens = Math.max(0, newPromptTokenCount - prevPromptTokenCount);
1729
- prevPromptTokenCount = newPromptTokenCount;
1730
- const newCandidatesTokenCount = response.usageMetadata.candidatesTokenCount ?? 0;
1731
- usageMetadata.output_tokens = Math.max(0, newCandidatesTokenCount - prevCandidatesTokenCount);
1732
- prevCandidatesTokenCount = newCandidatesTokenCount;
1733
- const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;
1734
- usageMetadata.total_tokens = Math.max(0, newTotalTokenCount - prevTotalTokenCount);
1735
- prevTotalTokenCount = newTotalTokenCount;
1736
- }
1737
- const chunk = convertResponseContentToChatGenerationChunk(response, {
1738
- usageMetadata,
1739
- index
1740
- });
1741
- index += 1;
1742
- if (!chunk) continue;
1743
- yield chunk;
1744
- await runManager?.handleLLMNewToken(chunk.text ?? "");
1745
- }
1746
- }
1747
- async completionWithRetry(request, options) {
1748
- return this.caller.callWithOptions({ signal: options?.signal }, async () => {
1749
- try {
1750
- return await this.client.generateContent(request);
1751
- } catch (e) {
1752
- if (e.message?.includes("400 Bad Request")) e.status = 400;
1753
- throw e;
1754
- }
1755
- });
1756
- }
1757
- withStructuredOutput(outputSchema, config) {
1758
- const schema = outputSchema;
1759
- const name = config?.name;
1760
- const method = config?.method;
1761
- const includeRaw = config?.includeRaw;
1762
- if (method === "jsonMode") throw new Error(`ChatGoogleGenerativeAI only supports "jsonSchema" or "functionCalling" as a method.`);
1763
- let llm;
1764
- let outputParser;
1765
- if (method === "functionCalling") {
1766
- let functionName = name ?? "extract";
1767
- let tools;
1768
- if (isInteropZodSchema(schema)) {
1769
- const jsonSchema = schemaToGenerativeAIParameters(schema);
1770
- tools = [{ functionDeclarations: [{
1771
- name: functionName,
1772
- description: jsonSchema.description ?? "A function available to call.",
1773
- parameters: jsonSchema
1774
- }] }];
1775
- outputParser = new GoogleGenerativeAIToolsOutputParser({
1776
- returnSingle: true,
1777
- keyName: functionName,
1778
- zodSchema: schema
1779
- });
1780
- } else {
1781
- let geminiFunctionDefinition;
1782
- if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
1783
- geminiFunctionDefinition = schema;
1784
- geminiFunctionDefinition.parameters = removeAdditionalProperties(schema.parameters);
1785
- functionName = schema.name;
1786
- } else geminiFunctionDefinition = {
1787
- name: functionName,
1788
- description: schema.description ?? "",
1789
- parameters: removeAdditionalProperties(schema)
1790
- };
1791
- tools = [{ functionDeclarations: [geminiFunctionDefinition] }];
1792
- outputParser = new GoogleGenerativeAIToolsOutputParser({
1793
- returnSingle: true,
1794
- keyName: functionName
1795
- });
1796
- }
1797
- llm = this.bindTools(tools).withConfig({ allowedFunctionNames: [functionName] });
1798
- } else {
1799
- const jsonSchema = schemaToGenerativeAIParameters(schema);
1800
- llm = this.withConfig({ responseSchema: jsonSchema });
1801
- outputParser = new JsonOutputParser();
1802
- }
1803
- if (!includeRaw) return llm.pipe(outputParser).withConfig({ runName: "ChatGoogleGenerativeAIStructuredOutput" });
1804
- const parserAssign = RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
1805
- const parserNone = RunnablePassthrough.assign({ parsed: () => null });
1806
- const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
1807
- return RunnableSequence.from([{ raw: llm }, parsedWithFallback]).withConfig({ runName: "StructuredOutputRunnable" });
1808
- }
1809
- };
1810
- export {
1811
- ChatGoogleGenerativeAI
1812
- };