@librechat/agents 3.1.1 → 3.1.21
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/agents/AgentContext.cjs +9 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +17 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/main.cjs +6 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +66 -5
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/createSchemaOnlyTool.cjs +31 -0
- package/dist/cjs/tools/createSchemaOnlyTool.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +9 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +17 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +67 -6
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/createSchemaOnlyTool.mjs +28 -0
- package/dist/esm/tools/createSchemaOnlyTool.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +7 -1
- package/dist/types/common/enum.d.ts +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/tools/ToolNode.d.ts +10 -1
- package/dist/types/tools/createSchemaOnlyTool.d.ts +12 -0
- package/dist/types/types/graph.d.ts +6 -0
- package/dist/types/types/tools.d.ts +49 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +10 -0
- package/src/common/enum.ts +2 -0
- package/src/graphs/Graph.ts +22 -0
- package/src/index.ts +2 -0
- package/src/specs/azure.simple.test.ts +214 -175
- package/src/tools/ToolNode.ts +95 -15
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +10 -9
- package/src/tools/__tests__/ToolSearch.integration.test.ts +10 -9
- package/src/tools/createSchemaOnlyTool.ts +37 -0
- package/src/types/graph.ts +6 -0
- package/src/types/tools.ts +52 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSchemaOnlyTool.mjs","sources":["../../../src/tools/createSchemaOnlyTool.ts"],"sourcesContent":["import { tool, type StructuredToolInterface } from '@langchain/core/tools';\nimport type { LCTool } from '@/types';\n\n/**\n * Creates a schema-only tool for LLM binding in event-driven mode.\n * These tools have valid schemas for the LLM to understand but should\n * never be invoked directly - ToolNode handles execution via events.\n */\nexport function createSchemaOnlyTool(\n definition: LCTool\n): StructuredToolInterface {\n const { name, description, parameters, responseFormat } = definition;\n\n return tool(\n async () => {\n throw new Error(\n `Tool \"${name}\" should not be invoked directly in event-driven mode. ` +\n 'ToolNode should dispatch ON_TOOL_EXECUTE events instead.'\n );\n },\n {\n name,\n description: description ?? '',\n schema: parameters ?? { type: 'object', properties: {} },\n responseFormat: responseFormat ?? 'content_and_artifact',\n }\n );\n}\n\n/**\n * Creates schema-only tools for all definitions in an array.\n */\nexport function createSchemaOnlyTools(\n definitions: LCTool[]\n): StructuredToolInterface[] {\n return definitions.map((def) => createSchemaOnlyTool(def));\n}\n"],"names":[],"mappings":";;AAGA;;;;AAIG;AACG,SAAU,oBAAoB,CAClC,UAAkB,EAAA;IAElB,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,cAAc,EAAE,GAAG,UAAU;AAEpE,IAAA,OAAO,IAAI,CACT,YAAW;AACT,QAAA,MAAM,IAAI,KAAK,CACb,CAAA,MAAA,EAAS,IAAI,CAAyD,uDAAA,CAAA;AACpE,YAAA,0DAA0D,CAC7D;AACH,KAAC,EACD;QACE,IAAI;QACJ,WAAW,EAAE,WAAW,IAAI,EAAE;QAC9B,MAAM,EAAE,UAAU,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;QACxD,cAAc,EAAE,cAAc,IAAI,sBAAsB;AACzD,KAAA,CACF;AACH;AAEA;;AAEG;AACG,SAAU,qBAAqB,CACnC,WAAqB,EAAA;AAErB,IAAA,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAAC;AAC5D;;;;"}
|
|
@@ -45,6 +45,11 @@ export declare class AgentContext {
|
|
|
45
45
|
* Used for tool search and programmatic tool calling.
|
|
46
46
|
*/
|
|
47
47
|
toolRegistry?: t.LCToolRegistry;
|
|
48
|
+
/**
|
|
49
|
+
* Serializable tool definitions for event-driven execution.
|
|
50
|
+
* When provided, ToolNode operates in event-driven mode.
|
|
51
|
+
*/
|
|
52
|
+
toolDefinitions?: t.LCTool[];
|
|
48
53
|
/** Set of tool names discovered via tool search (to be loaded) */
|
|
49
54
|
discoveredToolNames: Set<string>;
|
|
50
55
|
/** Instructions for this agent */
|
|
@@ -81,7 +86,7 @@ export declare class AgentContext {
|
|
|
81
86
|
/** Names of sibling agents executing in parallel (empty if sequential) */
|
|
82
87
|
parallelSiblings: string[];
|
|
83
88
|
};
|
|
84
|
-
constructor({ agentId, name, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, instructions, additionalInstructions, reasoningKey, toolEnd, instructionTokens, useLegacyContent, }: {
|
|
89
|
+
constructor({ agentId, name, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, toolDefinitions, instructions, additionalInstructions, reasoningKey, toolEnd, instructionTokens, useLegacyContent, }: {
|
|
85
90
|
agentId: string;
|
|
86
91
|
name?: string;
|
|
87
92
|
provider: Providers;
|
|
@@ -92,6 +97,7 @@ export declare class AgentContext {
|
|
|
92
97
|
tools?: t.GraphTools;
|
|
93
98
|
toolMap?: t.ToolMap;
|
|
94
99
|
toolRegistry?: t.LCToolRegistry;
|
|
100
|
+
toolDefinitions?: t.LCTool[];
|
|
95
101
|
instructions?: string;
|
|
96
102
|
additionalInstructions?: string;
|
|
97
103
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
@@ -17,6 +17,8 @@ export declare enum GraphEvents {
|
|
|
17
17
|
ON_MESSAGE_DELTA = "on_message_delta",
|
|
18
18
|
/** [Custom] Reasoning Delta events for messages */
|
|
19
19
|
ON_REASONING_DELTA = "on_reasoning_delta",
|
|
20
|
+
/** [Custom] Request to execute tools - dispatched by ToolNode, handled by host */
|
|
21
|
+
ON_TOOL_EXECUTE = "on_tool_execute",
|
|
20
22
|
/** Custom event, emitted by system */
|
|
21
23
|
ON_CUSTOM_EVENT = "on_custom_event",
|
|
22
24
|
/** Emitted when a chat model starts processing. */
|
package/dist/types/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export * from './tools/Calculator';
|
|
|
8
8
|
export * from './tools/CodeExecutor';
|
|
9
9
|
export * from './tools/ProgrammaticToolCalling';
|
|
10
10
|
export * from './tools/ToolSearch';
|
|
11
|
+
export * from './tools/ToolNode';
|
|
12
|
+
export * from './tools/createSchemaOnlyTool';
|
|
11
13
|
export * from './tools/handlers';
|
|
12
14
|
export * from './tools/search';
|
|
13
15
|
export * from './common';
|
|
@@ -18,7 +18,11 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
18
18
|
private programmaticCache?;
|
|
19
19
|
/** Reference to Graph's sessions map for automatic session injection */
|
|
20
20
|
private sessions?;
|
|
21
|
-
|
|
21
|
+
/** When true, dispatches ON_TOOL_EXECUTE events instead of invoking tools directly */
|
|
22
|
+
private eventDrivenMode;
|
|
23
|
+
/** Tool definitions for event-driven mode */
|
|
24
|
+
private toolDefinitions?;
|
|
25
|
+
constructor({ tools, toolMap, name, tags, errorHandler, toolCallStepIds, handleToolErrors, loadRuntimeTools, toolRegistry, sessions, eventDrivenMode, toolDefinitions, }: t.ToolNodeConstructorParams);
|
|
22
26
|
/**
|
|
23
27
|
* Returns cached programmatic tools, computing once on first access.
|
|
24
28
|
* Single iteration builds both toolMap and toolDefs simultaneously.
|
|
@@ -33,6 +37,11 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
33
37
|
* Runs a single tool call with error handling
|
|
34
38
|
*/
|
|
35
39
|
protected runTool(call: ToolCall, config: RunnableConfig): Promise<BaseMessage | Command>;
|
|
40
|
+
/**
|
|
41
|
+
* Execute all tool calls via ON_TOOL_EXECUTE event dispatch.
|
|
42
|
+
* Used in event-driven mode where the host handles actual tool execution.
|
|
43
|
+
*/
|
|
44
|
+
private executeViaEvent;
|
|
36
45
|
protected run(input: any, config: RunnableConfig): Promise<T>;
|
|
37
46
|
private isSendInput;
|
|
38
47
|
private isMessagesState;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type StructuredToolInterface } from '@langchain/core/tools';
|
|
2
|
+
import type { LCTool } from '@/types';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a schema-only tool for LLM binding in event-driven mode.
|
|
5
|
+
* These tools have valid schemas for the LLM to understand but should
|
|
6
|
+
* never be invoked directly - ToolNode handles execution via events.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createSchemaOnlyTool(definition: LCTool): StructuredToolInterface;
|
|
9
|
+
/**
|
|
10
|
+
* Creates schema-only tools for all definitions in an array.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createSchemaOnlyTools(definitions: LCTool[]): StructuredToolInterface[];
|
|
@@ -256,4 +256,10 @@ export interface AgentInputs {
|
|
|
256
256
|
* Maps tool name to LCTool definition.
|
|
257
257
|
*/
|
|
258
258
|
toolRegistry?: Map<string, LCTool>;
|
|
259
|
+
/**
|
|
260
|
+
* Serializable tool definitions for event-driven execution.
|
|
261
|
+
* When provided, ToolNode operates in event-driven mode, dispatching
|
|
262
|
+
* ON_TOOL_EXECUTE events instead of invoking tools directly.
|
|
263
|
+
*/
|
|
264
|
+
toolDefinitions?: LCTool[];
|
|
259
265
|
}
|
|
@@ -31,6 +31,10 @@ export type ToolNodeOptions = {
|
|
|
31
31
|
toolRegistry?: LCToolRegistry;
|
|
32
32
|
/** Reference to Graph's sessions map for automatic session injection */
|
|
33
33
|
sessions?: ToolSessionMap;
|
|
34
|
+
/** When true, dispatches ON_TOOL_EXECUTE events instead of invoking tools directly */
|
|
35
|
+
eventDrivenMode?: boolean;
|
|
36
|
+
/** Tool definitions for event-driven mode (used for context, not invocation) */
|
|
37
|
+
toolDefinitions?: Map<string, LCTool>;
|
|
34
38
|
};
|
|
35
39
|
export type ToolNodeConstructorParams = ToolRefs & ToolNodeOptions;
|
|
36
40
|
export type ToolEndEvent = {
|
|
@@ -96,6 +100,51 @@ export type LCTool = {
|
|
|
96
100
|
* Options: 'direct', 'code_execution'
|
|
97
101
|
*/
|
|
98
102
|
allowed_callers?: AllowedCaller[];
|
|
103
|
+
/** Response format for the tool output */
|
|
104
|
+
responseFormat?: 'content' | 'content_and_artifact';
|
|
105
|
+
/** Server name for MCP tools */
|
|
106
|
+
serverName?: string;
|
|
107
|
+
/** Tool type classification */
|
|
108
|
+
toolType?: 'builtin' | 'mcp' | 'action';
|
|
109
|
+
};
|
|
110
|
+
/** Single tool call within a batch request for event-driven execution */
|
|
111
|
+
export type ToolCallRequest = {
|
|
112
|
+
/** Tool call ID from the LLM */
|
|
113
|
+
id: string;
|
|
114
|
+
/** Tool name */
|
|
115
|
+
name: string;
|
|
116
|
+
/** Tool arguments */
|
|
117
|
+
args: Record<string, unknown>;
|
|
118
|
+
/** Step ID for tracking */
|
|
119
|
+
stepId?: string;
|
|
120
|
+
/** Usage turn count for this tool */
|
|
121
|
+
turn?: number;
|
|
122
|
+
};
|
|
123
|
+
/** Batch request containing ALL tool calls for a graph step */
|
|
124
|
+
export type ToolExecuteBatchRequest = {
|
|
125
|
+
/** All tool calls from the AIMessage */
|
|
126
|
+
toolCalls: ToolCallRequest[];
|
|
127
|
+
/** User ID for context */
|
|
128
|
+
userId?: string;
|
|
129
|
+
/** Agent ID for context */
|
|
130
|
+
agentId?: string;
|
|
131
|
+
/** Promise resolver - handler calls this with ALL results */
|
|
132
|
+
resolve: (results: ToolExecuteResult[]) => void;
|
|
133
|
+
/** Promise rejector - handler calls this on fatal error */
|
|
134
|
+
reject: (error: Error) => void;
|
|
135
|
+
};
|
|
136
|
+
/** Result for a single tool call in event-driven execution */
|
|
137
|
+
export type ToolExecuteResult = {
|
|
138
|
+
/** Matches ToolCallRequest.id */
|
|
139
|
+
toolCallId: string;
|
|
140
|
+
/** Tool output content */
|
|
141
|
+
content: string | unknown[];
|
|
142
|
+
/** Optional artifact (for content_and_artifact format) */
|
|
143
|
+
artifact?: unknown;
|
|
144
|
+
/** Execution status */
|
|
145
|
+
status: 'success' | 'error';
|
|
146
|
+
/** Error message if status is 'error' */
|
|
147
|
+
errorMessage?: string;
|
|
99
148
|
};
|
|
100
149
|
/** Map of tool names to tool definitions */
|
|
101
150
|
export type LCToolRegistry = Map<string, LCTool>;
|
package/package.json
CHANGED
|
@@ -34,6 +34,7 @@ export class AgentContext {
|
|
|
34
34
|
toolMap,
|
|
35
35
|
toolEnd,
|
|
36
36
|
toolRegistry,
|
|
37
|
+
toolDefinitions,
|
|
37
38
|
instructions,
|
|
38
39
|
additional_instructions,
|
|
39
40
|
streamBuffer,
|
|
@@ -52,6 +53,7 @@ export class AgentContext {
|
|
|
52
53
|
tools,
|
|
53
54
|
toolMap,
|
|
54
55
|
toolRegistry,
|
|
56
|
+
toolDefinitions,
|
|
55
57
|
instructions,
|
|
56
58
|
additionalInstructions: additional_instructions,
|
|
57
59
|
reasoningKey,
|
|
@@ -118,6 +120,11 @@ export class AgentContext {
|
|
|
118
120
|
* Used for tool search and programmatic tool calling.
|
|
119
121
|
*/
|
|
120
122
|
toolRegistry?: t.LCToolRegistry;
|
|
123
|
+
/**
|
|
124
|
+
* Serializable tool definitions for event-driven execution.
|
|
125
|
+
* When provided, ToolNode operates in event-driven mode.
|
|
126
|
+
*/
|
|
127
|
+
toolDefinitions?: t.LCTool[];
|
|
121
128
|
/** Set of tool names discovered via tool search (to be loaded) */
|
|
122
129
|
discoveredToolNames: Set<string> = new Set();
|
|
123
130
|
/** Instructions for this agent */
|
|
@@ -171,6 +178,7 @@ export class AgentContext {
|
|
|
171
178
|
tools,
|
|
172
179
|
toolMap,
|
|
173
180
|
toolRegistry,
|
|
181
|
+
toolDefinitions,
|
|
174
182
|
instructions,
|
|
175
183
|
additionalInstructions,
|
|
176
184
|
reasoningKey,
|
|
@@ -188,6 +196,7 @@ export class AgentContext {
|
|
|
188
196
|
tools?: t.GraphTools;
|
|
189
197
|
toolMap?: t.ToolMap;
|
|
190
198
|
toolRegistry?: t.LCToolRegistry;
|
|
199
|
+
toolDefinitions?: t.LCTool[];
|
|
191
200
|
instructions?: string;
|
|
192
201
|
additionalInstructions?: string;
|
|
193
202
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
@@ -205,6 +214,7 @@ export class AgentContext {
|
|
|
205
214
|
this.tools = tools;
|
|
206
215
|
this.toolMap = toolMap;
|
|
207
216
|
this.toolRegistry = toolRegistry;
|
|
217
|
+
this.toolDefinitions = toolDefinitions;
|
|
208
218
|
this.instructions = instructions;
|
|
209
219
|
this.additionalInstructions = additionalInstructions;
|
|
210
220
|
if (reasoningKey) {
|
package/src/common/enum.ts
CHANGED
|
@@ -19,6 +19,8 @@ export enum GraphEvents {
|
|
|
19
19
|
ON_MESSAGE_DELTA = 'on_message_delta',
|
|
20
20
|
/** [Custom] Reasoning Delta events for messages */
|
|
21
21
|
ON_REASONING_DELTA = 'on_reasoning_delta',
|
|
22
|
+
/** [Custom] Request to execute tools - dispatched by ToolNode, handled by host */
|
|
23
|
+
ON_TOOL_EXECUTE = 'on_tool_execute',
|
|
22
24
|
|
|
23
25
|
/* Official Events */
|
|
24
26
|
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -58,6 +58,7 @@ import {
|
|
|
58
58
|
} from '@/utils';
|
|
59
59
|
import { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';
|
|
60
60
|
import { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';
|
|
61
|
+
import { createSchemaOnlyTools } from '@/tools/createSchemaOnlyTool';
|
|
61
62
|
import { ChatOpenAI, AzureChatOpenAI } from '@/llm/openai';
|
|
62
63
|
import { safeDispatchCustomEvent } from '@/utils/events';
|
|
63
64
|
import { AgentContext } from '@/agents/AgentContext';
|
|
@@ -453,6 +454,27 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
453
454
|
currentToolMap?: t.ToolMap;
|
|
454
455
|
agentContext?: AgentContext;
|
|
455
456
|
}): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {
|
|
457
|
+
const toolDefinitions = agentContext?.toolDefinitions;
|
|
458
|
+
const eventDrivenMode =
|
|
459
|
+
toolDefinitions != null && toolDefinitions.length > 0;
|
|
460
|
+
|
|
461
|
+
if (eventDrivenMode) {
|
|
462
|
+
const schemaTools = createSchemaOnlyTools(toolDefinitions);
|
|
463
|
+
const toolDefMap = new Map(toolDefinitions.map((def) => [def.name, def]));
|
|
464
|
+
|
|
465
|
+
return new CustomToolNode<t.BaseGraphState>({
|
|
466
|
+
tools: schemaTools as t.GenericTool[],
|
|
467
|
+
toolMap: new Map(schemaTools.map((tool) => [tool.name, tool])),
|
|
468
|
+
toolCallStepIds: this.toolCallStepIds,
|
|
469
|
+
errorHandler: (data, metadata) =>
|
|
470
|
+
StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
471
|
+
toolRegistry: agentContext?.toolRegistry,
|
|
472
|
+
sessions: this.sessions,
|
|
473
|
+
eventDrivenMode: true,
|
|
474
|
+
toolDefinitions: toolDefMap,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
456
478
|
return new CustomToolNode<t.BaseGraphState>({
|
|
457
479
|
tools: (currentTools as t.GenericTool[] | undefined) ?? [],
|
|
458
480
|
toolMap: currentToolMap,
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,8 @@ export * from './tools/Calculator';
|
|
|
13
13
|
export * from './tools/CodeExecutor';
|
|
14
14
|
export * from './tools/ProgrammaticToolCalling';
|
|
15
15
|
export * from './tools/ToolSearch';
|
|
16
|
+
export * from './tools/ToolNode';
|
|
17
|
+
export * from './tools/createSchemaOnlyTool';
|
|
16
18
|
export * from './tools/handlers';
|
|
17
19
|
export * from './tools/search';
|
|
18
20
|
|