@librechat/agents 2.4.42 → 2.4.43
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/dist/cjs/common/enum.cjs +4 -2
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +2 -2
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +73 -1
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +469 -0
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
- package/dist/cjs/stream.cjs +5 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +4 -2
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +2 -2
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +73 -1
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +463 -0
- package/dist/esm/llm/google/utils/common.mjs.map +1 -0
- package/dist/esm/stream.mjs +5 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +5 -3
- package/dist/types/llm/google/index.d.ts +10 -5
- package/dist/types/llm/google/types.d.ts +32 -0
- package/dist/types/llm/google/utils/common.d.ts +19 -0
- package/dist/types/llm/google/utils/tools.d.ts +10 -0
- package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
- package/dist/types/types/llm.d.ts +2 -0
- package/dist/types/types/stream.d.ts +5 -0
- package/package.json +1 -1
- package/src/common/enum.ts +4 -2
- package/src/graphs/Graph.ts +10 -6
- package/src/llm/google/index.ts +118 -8
- package/src/llm/google/types.ts +43 -0
- package/src/llm/google/utils/common.ts +632 -0
- package/src/llm/google/utils/tools.ts +160 -0
- package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
- package/src/stream.ts +5 -2
- package/src/types/llm.ts +2 -0
- package/src/types/stream.ts +6 -0
- package/src/utils/llmConfig.ts +2 -2
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Tool as GenerativeAITool,
|
|
3
|
+
ToolConfig,
|
|
4
|
+
FunctionCallingMode,
|
|
5
|
+
FunctionDeclaration,
|
|
6
|
+
FunctionDeclarationsTool,
|
|
7
|
+
FunctionDeclarationSchema,
|
|
8
|
+
} from '@google/generative-ai';
|
|
9
|
+
import { ToolChoice } from '@langchain/core/language_models/chat_models';
|
|
10
|
+
import { StructuredToolInterface } from '@langchain/core/tools';
|
|
11
|
+
import { isLangChainTool } from '@langchain/core/utils/function_calling';
|
|
12
|
+
import {
|
|
13
|
+
isOpenAITool,
|
|
14
|
+
ToolDefinition,
|
|
15
|
+
} from '@langchain/core/language_models/base';
|
|
16
|
+
import { convertToGenerativeAITools } from './common';
|
|
17
|
+
import { GoogleGenerativeAIToolType } from '../types';
|
|
18
|
+
import { removeAdditionalProperties } from './zod_to_genai_parameters';
|
|
19
|
+
|
|
20
|
+
export function convertToolsToGenAI(
|
|
21
|
+
tools: GoogleGenerativeAIToolType[],
|
|
22
|
+
extra?: {
|
|
23
|
+
toolChoice?: ToolChoice;
|
|
24
|
+
allowedFunctionNames?: string[];
|
|
25
|
+
}
|
|
26
|
+
): {
|
|
27
|
+
tools: GenerativeAITool[];
|
|
28
|
+
toolConfig?: ToolConfig;
|
|
29
|
+
} {
|
|
30
|
+
// Extract function declaration processing to a separate function
|
|
31
|
+
const genAITools = processTools(tools);
|
|
32
|
+
|
|
33
|
+
// Simplify tool config creation
|
|
34
|
+
const toolConfig = createToolConfig(genAITools, extra);
|
|
35
|
+
|
|
36
|
+
return { tools: genAITools, toolConfig };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function processTools(tools: GoogleGenerativeAIToolType[]): GenerativeAITool[] {
|
|
40
|
+
let functionDeclarationTools: FunctionDeclaration[] = [];
|
|
41
|
+
const genAITools: GenerativeAITool[] = [];
|
|
42
|
+
|
|
43
|
+
tools.forEach((tool) => {
|
|
44
|
+
if (isLangChainTool(tool)) {
|
|
45
|
+
const [convertedTool] = convertToGenerativeAITools([
|
|
46
|
+
tool as StructuredToolInterface,
|
|
47
|
+
]);
|
|
48
|
+
if (convertedTool.functionDeclarations) {
|
|
49
|
+
functionDeclarationTools.push(...convertedTool.functionDeclarations);
|
|
50
|
+
}
|
|
51
|
+
} else if (isOpenAITool(tool)) {
|
|
52
|
+
const { functionDeclarations } = convertOpenAIToolToGenAI(tool);
|
|
53
|
+
if (functionDeclarations) {
|
|
54
|
+
functionDeclarationTools.push(...functionDeclarations);
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Failed to convert OpenAI structured tool to GenerativeAI tool'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
genAITools.push(tool as GenerativeAITool);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const genAIFunctionDeclaration = genAITools.find(
|
|
66
|
+
(t) => 'functionDeclarations' in t
|
|
67
|
+
);
|
|
68
|
+
if (genAIFunctionDeclaration) {
|
|
69
|
+
return genAITools.map((tool) => {
|
|
70
|
+
if (
|
|
71
|
+
functionDeclarationTools.length > 0 &&
|
|
72
|
+
'functionDeclarations' in tool
|
|
73
|
+
) {
|
|
74
|
+
const newTool = {
|
|
75
|
+
functionDeclarations: [
|
|
76
|
+
...(tool.functionDeclarations || []),
|
|
77
|
+
...functionDeclarationTools,
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
// Clear the functionDeclarationTools array so it is not passed again
|
|
81
|
+
functionDeclarationTools = [];
|
|
82
|
+
return newTool;
|
|
83
|
+
}
|
|
84
|
+
return tool;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [
|
|
89
|
+
...genAITools,
|
|
90
|
+
...(functionDeclarationTools.length > 0
|
|
91
|
+
? [
|
|
92
|
+
{
|
|
93
|
+
functionDeclarations: functionDeclarationTools,
|
|
94
|
+
},
|
|
95
|
+
]
|
|
96
|
+
: []),
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function convertOpenAIToolToGenAI(
|
|
101
|
+
tool: ToolDefinition
|
|
102
|
+
): FunctionDeclarationsTool {
|
|
103
|
+
return {
|
|
104
|
+
functionDeclarations: [
|
|
105
|
+
{
|
|
106
|
+
name: tool.function.name,
|
|
107
|
+
description: tool.function.description,
|
|
108
|
+
parameters: removeAdditionalProperties(
|
|
109
|
+
tool.function.parameters
|
|
110
|
+
) as FunctionDeclarationSchema,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createToolConfig(
|
|
117
|
+
genAITools: GenerativeAITool[],
|
|
118
|
+
extra?: {
|
|
119
|
+
toolChoice?: ToolChoice;
|
|
120
|
+
allowedFunctionNames?: string[];
|
|
121
|
+
}
|
|
122
|
+
): ToolConfig | undefined {
|
|
123
|
+
if (!genAITools.length || !extra) return undefined;
|
|
124
|
+
|
|
125
|
+
const { toolChoice, allowedFunctionNames } = extra;
|
|
126
|
+
|
|
127
|
+
const modeMap: Record<string, FunctionCallingMode> = {
|
|
128
|
+
any: FunctionCallingMode.ANY,
|
|
129
|
+
auto: FunctionCallingMode.AUTO,
|
|
130
|
+
none: FunctionCallingMode.NONE,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
toolChoice != null &&
|
|
135
|
+
['any', 'auto', 'none'].includes(toolChoice as string)
|
|
136
|
+
) {
|
|
137
|
+
return {
|
|
138
|
+
functionCallingConfig: {
|
|
139
|
+
mode: modeMap[toolChoice as keyof typeof modeMap] ?? 'MODE_UNSPECIFIED',
|
|
140
|
+
allowedFunctionNames,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (typeof toolChoice === 'string' || allowedFunctionNames) {
|
|
146
|
+
return {
|
|
147
|
+
functionCallingConfig: {
|
|
148
|
+
mode: FunctionCallingMode.ANY,
|
|
149
|
+
allowedFunctionNames: [
|
|
150
|
+
...(allowedFunctionNames ?? []),
|
|
151
|
+
...(toolChoice != null && typeof toolChoice === 'string'
|
|
152
|
+
? [toolChoice]
|
|
153
|
+
: []),
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,
|
|
5
|
+
type SchemaType as FunctionDeclarationSchemaType,
|
|
6
|
+
} from '@google/generative-ai';
|
|
7
|
+
import {
|
|
8
|
+
InteropZodType,
|
|
9
|
+
isInteropZodSchema,
|
|
10
|
+
} from '@langchain/core/utils/types';
|
|
11
|
+
import {
|
|
12
|
+
type JsonSchema7Type,
|
|
13
|
+
toJsonSchema,
|
|
14
|
+
} from '@langchain/core/utils/json_schema';
|
|
15
|
+
|
|
16
|
+
export interface GenerativeAIJsonSchema extends Record<string, unknown> {
|
|
17
|
+
properties?: Record<string, GenerativeAIJsonSchema>;
|
|
18
|
+
type: FunctionDeclarationSchemaType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GenerativeAIJsonSchemaDirty extends GenerativeAIJsonSchema {
|
|
22
|
+
properties?: Record<string, GenerativeAIJsonSchemaDirty>;
|
|
23
|
+
additionalProperties?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function removeAdditionalProperties(
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
obj: Record<string, any>
|
|
29
|
+
): GenerativeAIJsonSchema {
|
|
30
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
31
|
+
const newObj = { ...obj };
|
|
32
|
+
|
|
33
|
+
if ('additionalProperties' in newObj) {
|
|
34
|
+
delete newObj.additionalProperties;
|
|
35
|
+
}
|
|
36
|
+
if ('$schema' in newObj) {
|
|
37
|
+
delete newObj.$schema;
|
|
38
|
+
}
|
|
39
|
+
if ('strict' in newObj) {
|
|
40
|
+
delete newObj.strict;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const key in newObj) {
|
|
44
|
+
if (key in newObj) {
|
|
45
|
+
if (Array.isArray(newObj[key])) {
|
|
46
|
+
newObj[key] = newObj[key].map(removeAdditionalProperties);
|
|
47
|
+
} else if (typeof newObj[key] === 'object' && newObj[key] !== null) {
|
|
48
|
+
newObj[key] = removeAdditionalProperties(newObj[key]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return newObj as GenerativeAIJsonSchema;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return obj as GenerativeAIJsonSchema;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function schemaToGenerativeAIParameters<
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
RunOutput extends Record<string, any> = Record<string, any>,
|
|
62
|
+
>(
|
|
63
|
+
schema: InteropZodType<RunOutput> | JsonSchema7Type
|
|
64
|
+
): GenerativeAIFunctionDeclarationSchema {
|
|
65
|
+
// GenerativeAI doesn't accept either the $schema or additionalProperties
|
|
66
|
+
// attributes, so we need to explicitly remove them.
|
|
67
|
+
const jsonSchema = removeAdditionalProperties(
|
|
68
|
+
isInteropZodSchema(schema) ? toJsonSchema(schema) : schema
|
|
69
|
+
);
|
|
70
|
+
const { $schema, ...rest } = jsonSchema;
|
|
71
|
+
|
|
72
|
+
return rest as GenerativeAIFunctionDeclarationSchema;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function jsonSchemaToGeminiParameters(
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
schema: Record<string, any>
|
|
78
|
+
): GenerativeAIFunctionDeclarationSchema {
|
|
79
|
+
// Gemini doesn't accept either the $schema or additionalProperties
|
|
80
|
+
// attributes, so we need to explicitly remove them.
|
|
81
|
+
|
|
82
|
+
const jsonSchema = removeAdditionalProperties(
|
|
83
|
+
schema as GenerativeAIJsonSchemaDirty
|
|
84
|
+
);
|
|
85
|
+
const { $schema, ...rest } = jsonSchema;
|
|
86
|
+
|
|
87
|
+
return rest as GenerativeAIFunctionDeclarationSchema;
|
|
88
|
+
}
|
package/src/stream.ts
CHANGED
|
@@ -244,6 +244,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
244
244
|
content.every(
|
|
245
245
|
(c) =>
|
|
246
246
|
(c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
|
|
247
|
+
(c.type?.startsWith(ContentTypes.REASONING) ?? false) ||
|
|
247
248
|
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
248
249
|
)
|
|
249
250
|
) {
|
|
@@ -252,6 +253,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
252
253
|
type: ContentTypes.THINK,
|
|
253
254
|
think:
|
|
254
255
|
(c as t.ThinkingContentText).thinking ??
|
|
256
|
+
(c as Partial<t.GoogleReasoningContentText>).reasoning ??
|
|
255
257
|
(c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ??
|
|
256
258
|
'',
|
|
257
259
|
})),
|
|
@@ -264,8 +266,9 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
264
266
|
| undefined;
|
|
265
267
|
if (
|
|
266
268
|
Array.isArray(chunk.content) &&
|
|
267
|
-
(chunk.content[0]?.type ===
|
|
268
|
-
chunk.content[0]?.type ===
|
|
269
|
+
(chunk.content[0]?.type === ContentTypes.THINKING ||
|
|
270
|
+
chunk.content[0]?.type === ContentTypes.REASONING ||
|
|
271
|
+
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)
|
|
269
272
|
) {
|
|
270
273
|
reasoning_content = 'valid';
|
|
271
274
|
}
|
package/src/types/llm.ts
CHANGED
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
} from '@langchain/openai';
|
|
18
18
|
import type { BedrockChatFields } from '@langchain/community/chat_models/bedrock/web';
|
|
19
19
|
import type { GoogleGenerativeAIChatInput } from '@langchain/google-genai';
|
|
20
|
+
import type { GeminiGenerationConfig } from '@langchain/google-common';
|
|
20
21
|
import type { ChatVertexAIInput } from '@langchain/google-vertexai';
|
|
21
22
|
import type { ChatDeepSeekCallOptions } from '@langchain/deepseek';
|
|
22
23
|
import type { ChatOpenRouterCallOptions } from '@/llm/openrouter';
|
|
@@ -70,6 +71,7 @@ export type BedrockAnthropicInput = ChatBedrockConverseInput & {
|
|
|
70
71
|
export type BedrockConverseClientOptions = ChatBedrockConverseInput;
|
|
71
72
|
export type GoogleClientOptions = GoogleGenerativeAIChatInput & {
|
|
72
73
|
customHeaders?: RequestOptions['customHeaders'];
|
|
74
|
+
thinkingConfig?: GeminiGenerationConfig['thinkingConfig'];
|
|
73
75
|
};
|
|
74
76
|
export type DeepSeekClientOptions = ChatDeepSeekCallOptions;
|
|
75
77
|
export type XAIClientOptions = ChatXAIInput;
|
package/src/types/stream.ts
CHANGED
|
@@ -251,6 +251,12 @@ export type ReasoningContentText = {
|
|
|
251
251
|
think: string;
|
|
252
252
|
};
|
|
253
253
|
|
|
254
|
+
/** Vertex AI / Google Common - Reasoning Content Block Format */
|
|
255
|
+
export type GoogleReasoningContentText = {
|
|
256
|
+
type: ContentTypes.REASONING;
|
|
257
|
+
reasoning: string;
|
|
258
|
+
};
|
|
259
|
+
|
|
254
260
|
/** Anthropic's Reasoning Content Block Format */
|
|
255
261
|
export type ThinkingContentText = {
|
|
256
262
|
type: ContentTypes.THINKING;
|
package/src/utils/llmConfig.ts
CHANGED
|
@@ -93,14 +93,14 @@ export const llmConfigs: Record<string, t.LLMConfig | undefined> = {
|
|
|
93
93
|
},
|
|
94
94
|
[Providers.VERTEXAI]: {
|
|
95
95
|
provider: Providers.VERTEXAI,
|
|
96
|
-
|
|
96
|
+
model: 'gemini-2.5-flash',
|
|
97
97
|
streaming: true,
|
|
98
98
|
streamUsage: true,
|
|
99
99
|
keyFile: process.env.VERTEXAI_KEY_FILE,
|
|
100
100
|
} as t.VertexAIClientOptions & t.LLMConfig,
|
|
101
101
|
[Providers.GOOGLE]: {
|
|
102
102
|
provider: Providers.GOOGLE,
|
|
103
|
-
model: 'gemini-2.5-flash
|
|
103
|
+
model: 'gemini-2.5-flash',
|
|
104
104
|
streaming: true,
|
|
105
105
|
streamUsage: true,
|
|
106
106
|
},
|