@librechat/agents 3.1.67 → 3.1.68-dev.0
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 +23 -3
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +16 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +91 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +36 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +162 -0
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
- package/dist/cjs/hooks/executeHooks.cjs +276 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
- package/dist/cjs/hooks/matchers.cjs +256 -0
- package/dist/cjs/hooks/matchers.cjs.map +1 -0
- package/dist/cjs/hooks/types.cjs +27 -0
- package/dist/cjs/hooks/types.cjs.map +1 -0
- package/dist/cjs/main.cjs +54 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +74 -12
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/run.cjs +111 -0
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/summarization/index.cjs +41 -0
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +165 -19
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +175 -0
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +296 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/ReadFile.cjs +43 -0
- package/dist/cjs/tools/ReadFile.cjs.map +1 -0
- package/dist/cjs/tools/SkillTool.cjs +50 -0
- package/dist/cjs/tools/SkillTool.cjs.map +1 -0
- package/dist/cjs/tools/SubagentTool.cjs +92 -0
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +304 -140
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +84 -0
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +511 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +23 -3
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +15 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +91 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +36 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +160 -0
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
- package/dist/esm/hooks/executeHooks.mjs +273 -0
- package/dist/esm/hooks/executeHooks.mjs.map +1 -0
- package/dist/esm/hooks/matchers.mjs +251 -0
- package/dist/esm/hooks/matchers.mjs.map +1 -0
- package/dist/esm/hooks/types.mjs +25 -0
- package/dist/esm/hooks/types.mjs.map +1 -0
- package/dist/esm/main.mjs +13 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +66 -4
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/run.mjs +111 -0
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/summarization/index.mjs +41 -1
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +165 -19
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +169 -0
- package/dist/esm/tools/BashExecutor.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +287 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/ReadFile.mjs +38 -0
- package/dist/esm/tools/ReadFile.mjs.map +1 -0
- package/dist/esm/tools/SkillTool.mjs +45 -0
- package/dist/esm/tools/SkillTool.mjs.map +1 -0
- package/dist/esm/tools/SubagentTool.mjs +85 -0
- package/dist/esm/tools/SubagentTool.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +306 -142
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +82 -0
- package/dist/esm/tools/skillCatalog.mjs.map +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +505 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +6 -0
- package/dist/types/common/enum.d.ts +10 -1
- package/dist/types/graphs/Graph.d.ts +2 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +12 -0
- package/dist/types/hooks/HookRegistry.d.ts +56 -0
- package/dist/types/hooks/executeHooks.d.ts +79 -0
- package/dist/types/hooks/index.d.ts +6 -0
- package/dist/types/hooks/matchers.d.ts +95 -0
- package/dist/types/hooks/types.d.ts +320 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/messages/format.d.ts +2 -1
- package/dist/types/run.d.ts +1 -0
- package/dist/types/summarization/index.d.ts +2 -0
- package/dist/types/summarization/node.d.ts +2 -0
- package/dist/types/tools/BashExecutor.d.ts +45 -0
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
- package/dist/types/tools/ReadFile.d.ts +28 -0
- package/dist/types/tools/SkillTool.d.ts +40 -0
- package/dist/types/tools/SubagentTool.d.ts +36 -0
- package/dist/types/tools/ToolNode.d.ts +24 -2
- package/dist/types/tools/skillCatalog.d.ts +19 -0
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
- package/dist/types/tools/subagent/index.d.ts +2 -0
- package/dist/types/types/graph.d.ts +61 -2
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/run.d.ts +20 -0
- package/dist/types/types/skill.d.ts +9 -0
- package/dist/types/types/tools.d.ts +38 -1
- package/package.json +5 -1
- package/src/agents/AgentContext.ts +26 -2
- package/src/common/enum.ts +15 -0
- package/src/graphs/Graph.ts +113 -0
- package/src/graphs/MultiAgentGraph.ts +39 -0
- package/src/graphs/__tests__/MultiAgentGraph.test.ts +91 -0
- package/src/hooks/HookRegistry.ts +208 -0
- package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
- package/src/hooks/__tests__/compactHooks.test.ts +214 -0
- package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
- package/src/hooks/__tests__/integration.test.ts +337 -0
- package/src/hooks/__tests__/matchers.test.ts +238 -0
- package/src/hooks/__tests__/toolHooks.test.ts +669 -0
- package/src/hooks/executeHooks.ts +375 -0
- package/src/hooks/index.ts +57 -0
- package/src/hooks/matchers.ts +280 -0
- package/src/hooks/types.ts +404 -0
- package/src/index.ts +10 -0
- package/src/messages/format.ts +74 -4
- package/src/messages/formatAgentMessages.skills.test.ts +334 -0
- package/src/run.ts +126 -0
- package/src/scripts/multi-agent-subagent.ts +246 -0
- package/src/scripts/subagent-event-driven-debug.ts +190 -0
- package/src/scripts/subagent-tools-debug.ts +160 -0
- package/src/specs/subagent.test.ts +305 -0
- package/src/summarization/__tests__/node.test.ts +42 -0
- package/src/summarization/__tests__/trigger.test.ts +100 -1
- package/src/summarization/index.ts +47 -0
- package/src/summarization/node.ts +202 -24
- package/src/tools/BashExecutor.ts +205 -0
- package/src/tools/BashProgrammaticToolCalling.ts +397 -0
- package/src/tools/ReadFile.ts +39 -0
- package/src/tools/SkillTool.ts +46 -0
- package/src/tools/SubagentTool.ts +100 -0
- package/src/tools/ToolNode.ts +391 -169
- package/src/tools/__tests__/ReadFile.test.ts +44 -0
- package/src/tools/__tests__/SkillTool.test.ts +442 -0
- package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
- package/src/tools/__tests__/SubagentTool.test.ts +149 -0
- package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
- package/src/tools/__tests__/skillCatalog.test.ts +161 -0
- package/src/tools/__tests__/subagentHooks.test.ts +215 -0
- package/src/tools/skillCatalog.ts +126 -0
- package/src/tools/subagent/SubagentExecutor.ts +676 -0
- package/src/tools/subagent/index.ts +13 -0
- package/src/types/graph.ts +80 -1
- package/src/types/index.ts +1 -0
- package/src/types/run.ts +20 -0
- package/src/types/skill.ts +11 -0
- package/src/types/tools.ts +41 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { StructuredToolInterface } from '@langchain/core/tools';
|
|
2
2
|
import type { RunnableToolLike } from '@langchain/core/runnables';
|
|
3
3
|
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
4
|
-
import type {
|
|
4
|
+
import type { HookRegistry } from '@/hooks';
|
|
5
|
+
import type { MessageContentComplex, ToolErrorData } from './stream';
|
|
5
6
|
import { EnvVar } from '@/common';
|
|
6
7
|
/** Replacement type for `import type { ToolCall } from '@langchain/core/messages/tool'` in order to have stringified args typed */
|
|
7
8
|
export type CustomToolCall = {
|
|
@@ -39,6 +40,12 @@ export type ToolNodeOptions = {
|
|
|
39
40
|
agentId?: string;
|
|
40
41
|
/** Tool names that must be executed directly (via runTool) even in event-driven mode (e.g., graph-managed handoff tools) */
|
|
41
42
|
directToolNames?: Set<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Hook registry for PreToolUse/PostToolUse lifecycle hooks.
|
|
45
|
+
* Only fires for event-driven tool calls (`dispatchToolEvents`). Tools
|
|
46
|
+
* routed through `directToolNames` bypass hook dispatch entirely.
|
|
47
|
+
*/
|
|
48
|
+
hookRegistry?: HookRegistry;
|
|
42
49
|
/** Max context tokens for the agent — used to compute tool result truncation limits. */
|
|
43
50
|
maxContextTokens?: number;
|
|
44
51
|
/**
|
|
@@ -154,6 +161,25 @@ export type ToolExecuteBatchRequest = {
|
|
|
154
161
|
/** Promise rejector - handler calls this on fatal error */
|
|
155
162
|
reject: (error: Error) => void;
|
|
156
163
|
};
|
|
164
|
+
/**
|
|
165
|
+
* A message injected into graph state by any tool execution handler.
|
|
166
|
+
* Generic mechanism: any tool returning `injectedMessages` in its `ToolExecuteResult`
|
|
167
|
+
* will have these appended to state after the ToolMessage for this call.
|
|
168
|
+
*/
|
|
169
|
+
export type InjectedMessage = {
|
|
170
|
+
/** 'user' for skill body injection, 'system' for context hints.
|
|
171
|
+
* Both are converted to HumanMessage at runtime; the original role
|
|
172
|
+
* is preserved in additional_kwargs.role. */
|
|
173
|
+
role: 'user' | 'system';
|
|
174
|
+
/** Message content: string for simple text, array for complex multi-part content */
|
|
175
|
+
content: string | MessageContentComplex[];
|
|
176
|
+
/** When true, the message is framework-internal: not shown in UI, not counted as a user turn */
|
|
177
|
+
isMeta?: boolean;
|
|
178
|
+
/** Origin tag for downstream consumers (UI, pruner, compaction) */
|
|
179
|
+
source?: 'skill' | 'hook' | 'system';
|
|
180
|
+
/** Only set when source is 'skill', for compaction preservation */
|
|
181
|
+
skillName?: string;
|
|
182
|
+
};
|
|
157
183
|
/** Result for a single tool call in event-driven execution */
|
|
158
184
|
export type ToolExecuteResult = {
|
|
159
185
|
/** Matches ToolCallRequest.id */
|
|
@@ -166,6 +192,13 @@ export type ToolExecuteResult = {
|
|
|
166
192
|
status: 'success' | 'error';
|
|
167
193
|
/** Error message if status is 'error' */
|
|
168
194
|
errorMessage?: string;
|
|
195
|
+
/**
|
|
196
|
+
* Messages to inject into graph state after the ToolMessage for this call.
|
|
197
|
+
* Placed after tool results to respect provider message ordering (tool_call -> tool_result adjacency).
|
|
198
|
+
* The host's message formatter may merge injected user messages with the preceding tool_result turn.
|
|
199
|
+
* Generic mechanism: any tool execution handler can use this.
|
|
200
|
+
*/
|
|
201
|
+
injectedMessages?: InjectedMessage[];
|
|
169
202
|
};
|
|
170
203
|
/** Map of tool names to tool definitions */
|
|
171
204
|
export type LCToolRegistry = Map<string, LCTool>;
|
|
@@ -266,6 +299,10 @@ export type ProgrammaticExecutionArtifact = {
|
|
|
266
299
|
session_id?: string;
|
|
267
300
|
files?: FileRefs;
|
|
268
301
|
};
|
|
302
|
+
/** Parameters for creating a bash execution tool (same API as CodeExecutor, bash-only) */
|
|
303
|
+
export type BashExecutionToolParams = CodeExecutionToolParams;
|
|
304
|
+
/** Parameters for creating a bash programmatic tool calling tool (same API as PTC, bash-only) */
|
|
305
|
+
export type BashProgrammaticToolCallingParams = ProgrammaticToolCallingParams;
|
|
269
306
|
/**
|
|
270
307
|
* Initialization parameters for the PTC tool
|
|
271
308
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.68-dev.0",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -61,6 +61,9 @@
|
|
|
61
61
|
"tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
62
62
|
"search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/search.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
63
63
|
"tool_search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tool_search.ts",
|
|
64
|
+
"subagent": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-subagent.ts",
|
|
65
|
+
"subagent:events": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/subagent-event-driven-debug.ts",
|
|
66
|
+
"subagent:tools": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/subagent-tools-debug.ts",
|
|
64
67
|
"programmatic_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/programmatic_exec.ts",
|
|
65
68
|
"code_exec_ptc": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_ptc.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
66
69
|
"programmatic_exec_agent": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/programmatic_exec_agent.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
@@ -77,6 +80,7 @@
|
|
|
77
80
|
"multi-agent-chain": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-chain.ts",
|
|
78
81
|
"multi-agent-sequence": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-sequence.ts",
|
|
79
82
|
"multi-agent-conditional": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-conditional.ts",
|
|
83
|
+
"multi-agent-subagent": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-subagent.ts",
|
|
80
84
|
"multi-agent-supervisor": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-supervisor.ts",
|
|
81
85
|
"test-handoff-preamble": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-handoff-preamble.ts",
|
|
82
86
|
"multi-agent-list-handoff": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-multi-agent-list-handoff.ts",
|
|
@@ -55,6 +55,8 @@ export class AgentContext {
|
|
|
55
55
|
contextPruningConfig,
|
|
56
56
|
maxToolResultChars,
|
|
57
57
|
toolSchemaTokens,
|
|
58
|
+
subagentConfigs,
|
|
59
|
+
maxSubagentDepth,
|
|
58
60
|
} = agentConfig;
|
|
59
61
|
|
|
60
62
|
const agentContext = new AgentContext({
|
|
@@ -82,6 +84,10 @@ export class AgentContext {
|
|
|
82
84
|
maxToolResultChars,
|
|
83
85
|
});
|
|
84
86
|
|
|
87
|
+
agentContext._sourceInputs = agentConfig;
|
|
88
|
+
agentContext.subagentConfigs = subagentConfigs;
|
|
89
|
+
agentContext.maxSubagentDepth = maxSubagentDepth;
|
|
90
|
+
|
|
85
91
|
if (initialSummary?.text != null && initialSummary.text !== '') {
|
|
86
92
|
agentContext.setInitialSummary(
|
|
87
93
|
initialSummary.text,
|
|
@@ -198,6 +204,12 @@ export class AgentContext {
|
|
|
198
204
|
toolDefinitions?: t.LCTool[];
|
|
199
205
|
/** Set of tool names discovered via tool search (to be loaded) */
|
|
200
206
|
discoveredToolNames: Set<string> = new Set();
|
|
207
|
+
/** Original AgentInputs used to create this context — used for self-spawn subagent resolution. */
|
|
208
|
+
_sourceInputs?: t.AgentInputs;
|
|
209
|
+
/** Subagent configurations for hierarchical delegation. */
|
|
210
|
+
subagentConfigs?: t.SubagentConfig[];
|
|
211
|
+
/** Maximum subagent nesting depth. */
|
|
212
|
+
maxSubagentDepth?: number;
|
|
201
213
|
/** Instructions for this agent */
|
|
202
214
|
instructions?: string;
|
|
203
215
|
/** Additional instructions for this agent */
|
|
@@ -685,8 +697,20 @@ export class AgentContext {
|
|
|
685
697
|
let toolTokens = 0;
|
|
686
698
|
const countedToolNames = new Set<string>();
|
|
687
699
|
|
|
688
|
-
|
|
689
|
-
|
|
700
|
+
/**
|
|
701
|
+
* Iterate both `tools` (user-provided instance tools) and `graphTools`
|
|
702
|
+
* (graph-managed tools like handoff + subagent). `graphTools` is often
|
|
703
|
+
* populated after `fromConfig()` kicks off the initial calculation, so
|
|
704
|
+
* callers that mutate `graphTools` must re-trigger this method to
|
|
705
|
+
* refresh `toolSchemaTokens`.
|
|
706
|
+
*/
|
|
707
|
+
const instanceTools: t.GraphTools = [
|
|
708
|
+
...((this.tools as t.GenericTool[] | undefined) ?? []),
|
|
709
|
+
...((this.graphTools as t.GenericTool[] | undefined) ?? []),
|
|
710
|
+
];
|
|
711
|
+
|
|
712
|
+
if (instanceTools.length > 0) {
|
|
713
|
+
for (const tool of instanceTools) {
|
|
690
714
|
const genericTool = tool as Record<string, unknown>;
|
|
691
715
|
if (
|
|
692
716
|
genericTool.schema != null &&
|
package/src/common/enum.ts
CHANGED
|
@@ -27,6 +27,8 @@ export enum GraphEvents {
|
|
|
27
27
|
ON_SUMMARIZE_DELTA = 'on_summarize_delta',
|
|
28
28
|
/** [Custom] Emitted when the summarize node completes with the final summary */
|
|
29
29
|
ON_SUMMARIZE_COMPLETE = 'on_summarize_complete',
|
|
30
|
+
/** [Custom] Progress update from a running subagent (wraps child-graph events so hosts can display activity separately from parent). */
|
|
31
|
+
ON_SUBAGENT_UPDATE = 'on_subagent_update',
|
|
30
32
|
/** [Custom] Diagnostic logging event for context management observability */
|
|
31
33
|
ON_AGENT_LOG = 'on_agent_log',
|
|
32
34
|
|
|
@@ -182,8 +184,21 @@ export enum Constants {
|
|
|
182
184
|
MCP_DELIMITER = '_mcp_',
|
|
183
185
|
/** Anthropic server tool ID prefix (web_search, code_execution, etc.) */
|
|
184
186
|
ANTHROPIC_SERVER_TOOL_PREFIX = 'srvtoolu_',
|
|
187
|
+
SKILL_TOOL = 'skill',
|
|
188
|
+
READ_FILE = 'read_file',
|
|
189
|
+
BASH_TOOL = 'bash_tool',
|
|
190
|
+
BASH_PROGRAMMATIC_TOOL_CALLING = 'run_tools_with_bash',
|
|
191
|
+
SUBAGENT = 'subagent',
|
|
185
192
|
}
|
|
186
193
|
|
|
194
|
+
/** Tool names that use the code execution environment (shared session, file tracking). */
|
|
195
|
+
export const CODE_EXECUTION_TOOLS: ReadonlySet<string> = new Set([
|
|
196
|
+
Constants.EXECUTE_CODE,
|
|
197
|
+
Constants.BASH_TOOL,
|
|
198
|
+
Constants.PROGRAMMATIC_TOOL_CALLING,
|
|
199
|
+
Constants.BASH_PROGRAMMATIC_TOOL_CALLING,
|
|
200
|
+
]);
|
|
201
|
+
|
|
187
202
|
export enum TitleMethod {
|
|
188
203
|
STRUCTURED = 'structured',
|
|
189
204
|
FUNCTIONS = 'functions',
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import { nanoid } from 'nanoid';
|
|
3
|
+
import { tool } from '@langchain/core/tools';
|
|
3
4
|
import { ToolNode } from '@langchain/langgraph/prebuilt';
|
|
4
5
|
import { Runnable, RunnableConfig } from '@langchain/core/runnables';
|
|
5
6
|
import { ToolMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
@@ -39,6 +40,8 @@ import {
|
|
|
39
40
|
joinKeys,
|
|
40
41
|
sleep,
|
|
41
42
|
} from '@/utils';
|
|
43
|
+
import { SubagentExecutor, resolveSubagentConfigs } from '@/tools/subagent';
|
|
44
|
+
import { buildSubagentToolParams } from '@/tools/SubagentTool';
|
|
42
45
|
import { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';
|
|
43
46
|
import { safeDispatchCustomEvent, emitAgentLog } from '@/utils/events';
|
|
44
47
|
import { attemptInvoke, tryFallbackProviders } from '@/llm/invoke';
|
|
@@ -53,6 +56,7 @@ import { isThinkingEnabled } from '@/llm/request';
|
|
|
53
56
|
import { initializeModel } from '@/llm/init';
|
|
54
57
|
import { HandlerRegistry } from '@/events';
|
|
55
58
|
import { ChatOpenAI } from '@/llm/openai';
|
|
59
|
+
import type { HookRegistry } from '@/hooks';
|
|
56
60
|
|
|
57
61
|
const { AGENT, TOOLS, SUMMARIZE } = GraphNodeKeys;
|
|
58
62
|
|
|
@@ -123,6 +127,7 @@ export abstract class Graph<
|
|
|
123
127
|
/** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
|
|
124
128
|
invokedToolIds?: Set<string>;
|
|
125
129
|
handlerRegistry: HandlerRegistry | undefined;
|
|
130
|
+
hookRegistry: HookRegistry | undefined;
|
|
126
131
|
/**
|
|
127
132
|
* Tool session contexts for automatic state persistence across tool invocations.
|
|
128
133
|
* Keyed by tool name (e.g., Constants.EXECUTE_CODE).
|
|
@@ -147,6 +152,7 @@ export abstract class Graph<
|
|
|
147
152
|
this.prelimMessageIdsByStepKey = new Map();
|
|
148
153
|
this.invokedToolIds = undefined;
|
|
149
154
|
this.handlerRegistry = undefined;
|
|
155
|
+
this.hookRegistry = undefined;
|
|
150
156
|
this.sessions.clear();
|
|
151
157
|
}
|
|
152
158
|
}
|
|
@@ -506,6 +512,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
506
512
|
agentId: agentContext?.agentId,
|
|
507
513
|
toolCallStepIds: this.toolCallStepIds,
|
|
508
514
|
toolRegistry: agentContext?.toolRegistry,
|
|
515
|
+
hookRegistry: this.hookRegistry,
|
|
509
516
|
directToolNames: directToolNames.size > 0 ? directToolNames : undefined,
|
|
510
517
|
maxContextTokens: agentContext?.maxContextTokens,
|
|
511
518
|
maxToolResultChars: agentContext?.maxToolResultChars,
|
|
@@ -1148,6 +1155,111 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
1148
1155
|
throw new Error(`Agent context not found for agentId: ${agentId}`);
|
|
1149
1156
|
}
|
|
1150
1157
|
|
|
1158
|
+
/**
|
|
1159
|
+
* Depth countdown across graph boundaries: the parent's `maxSubagentDepth`
|
|
1160
|
+
* becomes this executor's `maxDepth`. When the child graph is constructed,
|
|
1161
|
+
* `buildChildInputs()` decrements `maxSubagentDepth` on the child's
|
|
1162
|
+
* `AgentInputs` (only when `allowNested: true`; otherwise subagentConfigs
|
|
1163
|
+
* are stripped entirely). The child graph's own `createAgentNode()` then
|
|
1164
|
+
* reads the decremented value here and creates a narrower executor —
|
|
1165
|
+
* recursion is bounded even though each graph has its own separate
|
|
1166
|
+
* executor instance.
|
|
1167
|
+
*/
|
|
1168
|
+
const effectiveSubagentDepth = agentContext.maxSubagentDepth ?? 1;
|
|
1169
|
+
if (
|
|
1170
|
+
agentContext.subagentConfigs != null &&
|
|
1171
|
+
agentContext.subagentConfigs.length > 0 &&
|
|
1172
|
+
effectiveSubagentDepth > 0
|
|
1173
|
+
) {
|
|
1174
|
+
const resolvedConfigs = resolveSubagentConfigs(
|
|
1175
|
+
agentContext.subagentConfigs,
|
|
1176
|
+
agentContext
|
|
1177
|
+
);
|
|
1178
|
+
if (resolvedConfigs.length > 0) {
|
|
1179
|
+
const getParentHandlerRegistry = (): HandlerRegistry | undefined =>
|
|
1180
|
+
this.handlerRegistry;
|
|
1181
|
+
const executor = new SubagentExecutor({
|
|
1182
|
+
configs: new Map(resolvedConfigs.map((c) => [c.type, c])),
|
|
1183
|
+
parentSignal: this.signal,
|
|
1184
|
+
hookRegistry: this.hookRegistry,
|
|
1185
|
+
/** Lazy — Run wires the registry onto the graph AFTER
|
|
1186
|
+
* `createWorkflow()` runs, so a direct capture here would be
|
|
1187
|
+
* `undefined` at construction time. */
|
|
1188
|
+
parentHandlerRegistry: getParentHandlerRegistry,
|
|
1189
|
+
parentRunId: this.runId ?? '',
|
|
1190
|
+
parentAgentId: agentContext.agentId,
|
|
1191
|
+
tokenCounter: agentContext.tokenCounter,
|
|
1192
|
+
maxDepth: effectiveSubagentDepth,
|
|
1193
|
+
createChildGraph: (input): StandardGraph => new StandardGraph(input),
|
|
1194
|
+
});
|
|
1195
|
+
|
|
1196
|
+
const subagentTool = tool(async (rawInput, config) => {
|
|
1197
|
+
const input = rawInput as {
|
|
1198
|
+
description?: string;
|
|
1199
|
+
subagent_type?: string;
|
|
1200
|
+
};
|
|
1201
|
+
const description =
|
|
1202
|
+
typeof input.description === 'string' &&
|
|
1203
|
+
input.description.trim().length > 0
|
|
1204
|
+
? input.description
|
|
1205
|
+
: 'No task description provided';
|
|
1206
|
+
const subagentType =
|
|
1207
|
+
typeof input.subagent_type === 'string' ? input.subagent_type : '';
|
|
1208
|
+
const threadId = config.configurable?.thread_id as string | undefined;
|
|
1209
|
+
/**
|
|
1210
|
+
* When the tool is dispatched from an LLM's `tool_call`, LangChain
|
|
1211
|
+
* threads the originating `ToolCall` onto the RunnableConfig as
|
|
1212
|
+
* `config.toolCall` (see `ToolRunnableConfig` in
|
|
1213
|
+
* `@langchain/core/tools` — internal but stable since ≥0.3.x).
|
|
1214
|
+
* Surfacing its id lets hosts correlate `SubagentUpdateEvent`s
|
|
1215
|
+
* back to the parent's `tool_call_id` deterministically — no
|
|
1216
|
+
* temporal heuristics needed. If a future LangChain version
|
|
1217
|
+
* changes the threading, the type-guarded read falls back to
|
|
1218
|
+
* `undefined` and the correlation degrades gracefully.
|
|
1219
|
+
*/
|
|
1220
|
+
const toolCall = (config as { toolCall?: { id?: string } }).toolCall;
|
|
1221
|
+
const parentToolCallId =
|
|
1222
|
+
typeof toolCall?.id === 'string' ? toolCall.id : undefined;
|
|
1223
|
+
const result = await executor.execute({
|
|
1224
|
+
description,
|
|
1225
|
+
subagentType,
|
|
1226
|
+
threadId,
|
|
1227
|
+
parentToolCallId,
|
|
1228
|
+
});
|
|
1229
|
+
return result.content;
|
|
1230
|
+
}, buildSubagentToolParams(resolvedConfigs));
|
|
1231
|
+
|
|
1232
|
+
if (!agentContext.graphTools) {
|
|
1233
|
+
agentContext.graphTools = [];
|
|
1234
|
+
}
|
|
1235
|
+
(agentContext.graphTools as t.GenericTool[]).push(subagentTool);
|
|
1236
|
+
|
|
1237
|
+
/**
|
|
1238
|
+
* Refresh toolSchemaTokens to include the subagent tool's schema.
|
|
1239
|
+
* `calculateInstructionTokens()` was kicked off in `fromConfig()`
|
|
1240
|
+
* before graphTools was populated, so its result did not count this
|
|
1241
|
+
* tool. Without this retrigger, token-budget/pruning logic
|
|
1242
|
+
* underestimates prompt overhead.
|
|
1243
|
+
*/
|
|
1244
|
+
if (agentContext.tokenCounter) {
|
|
1245
|
+
const { tokenCounter, baseIndexTokenCountMap } = agentContext;
|
|
1246
|
+
agentContext.tokenCalculationPromise = agentContext
|
|
1247
|
+
.calculateInstructionTokens(tokenCounter)
|
|
1248
|
+
.then(() => {
|
|
1249
|
+
agentContext.updateTokenMapWithInstructions(
|
|
1250
|
+
baseIndexTokenCountMap
|
|
1251
|
+
);
|
|
1252
|
+
})
|
|
1253
|
+
.catch((err) => {
|
|
1254
|
+
console.error(
|
|
1255
|
+
'Error recalculating instruction tokens after subagent tool injection:',
|
|
1256
|
+
err
|
|
1257
|
+
);
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1151
1263
|
const agentNode = `${AGENT}${agentId}` as const;
|
|
1152
1264
|
const toolNode = `${TOOLS}${agentId}` as const;
|
|
1153
1265
|
const summarizeNode = `${SUMMARIZE}${agentId}` as const;
|
|
@@ -1203,6 +1315,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
1203
1315
|
},
|
|
1204
1316
|
runId: this.runId,
|
|
1205
1317
|
isMultiAgent: this.isMultiAgentGraph(),
|
|
1318
|
+
hookRegistry: this.hookRegistry,
|
|
1206
1319
|
dispatchRunStep: async (runStep, nodeConfig) => {
|
|
1207
1320
|
this.contentData.push(runStep);
|
|
1208
1321
|
this.contentIndexMap.set(runStep.id, runStep.index);
|
|
@@ -59,11 +59,50 @@ export class MultiAgentGraph extends StandardGraph {
|
|
|
59
59
|
constructor(input: t.MultiAgentGraphInput) {
|
|
60
60
|
super(input);
|
|
61
61
|
this.edges = input.edges;
|
|
62
|
+
this.validateEdgeAgents();
|
|
62
63
|
this.categorizeEdges();
|
|
63
64
|
this.analyzeGraph();
|
|
64
65
|
this.createHandoffTools();
|
|
65
66
|
}
|
|
66
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Fails fast when an edge references an agent that is not in
|
|
70
|
+
* `agentContexts`. Without this check, the underlying LangGraph
|
|
71
|
+
* `StateGraph.compile()` would throw the opaque
|
|
72
|
+
* `Found edge ending at unknown node "<id>"` error after graph
|
|
73
|
+
* construction — far from the true root cause.
|
|
74
|
+
*
|
|
75
|
+
* This catches the common misuse of passing `edges` into a multi-agent
|
|
76
|
+
* config without also passing the corresponding sub-agent configs in
|
|
77
|
+
* `agents` (e.g. a host that forgot to pre-load handoff targets).
|
|
78
|
+
*/
|
|
79
|
+
private validateEdgeAgents(): void {
|
|
80
|
+
const known = new Set(this.agentContexts.keys());
|
|
81
|
+
const unknown = new Set<string>();
|
|
82
|
+
for (const edge of this.edges) {
|
|
83
|
+
const participants = [
|
|
84
|
+
...(Array.isArray(edge.from) ? edge.from : [edge.from]),
|
|
85
|
+
...(Array.isArray(edge.to) ? edge.to : [edge.to]),
|
|
86
|
+
];
|
|
87
|
+
for (const id of participants) {
|
|
88
|
+
if (typeof id === 'string' && !known.has(id)) {
|
|
89
|
+
unknown.add(id);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (unknown.size === 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const missing = Array.from(unknown)
|
|
97
|
+
.map((id) => `"${id}"`)
|
|
98
|
+
.join(', ');
|
|
99
|
+
throw new Error(
|
|
100
|
+
`MultiAgentGraph: edges reference agent(s) not present in agents: [${missing}]. ` +
|
|
101
|
+
'Ensure every agent referenced by an edge is also included in the `agents` array, ' +
|
|
102
|
+
'or filter orphaned edges before constructing the graph.'
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
67
106
|
/**
|
|
68
107
|
* Categorize edges into handoff and direct types
|
|
69
108
|
*/
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// src/graphs/__tests__/MultiAgentGraph.test.ts
|
|
2
|
+
import { MultiAgentGraph } from '../MultiAgentGraph';
|
|
3
|
+
import { Providers } from '@/common';
|
|
4
|
+
import type * as t from '@/types';
|
|
5
|
+
|
|
6
|
+
describe('MultiAgentGraph.validateEdgeAgents', () => {
|
|
7
|
+
const makeAgent = (agentId: string): t.AgentInputs => ({
|
|
8
|
+
agentId,
|
|
9
|
+
provider: Providers.OPENAI,
|
|
10
|
+
instructions: 'test',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('constructs without error when every edge endpoint has a matching agent', () => {
|
|
14
|
+
const input: t.MultiAgentGraphInput = {
|
|
15
|
+
runId: 'r1',
|
|
16
|
+
agents: [makeAgent('A'), makeAgent('B')],
|
|
17
|
+
edges: [{ from: 'A', to: 'B', edgeType: 'handoff' }],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
expect(() => new MultiAgentGraph(input)).not.toThrow();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('throws a descriptive error when an edge `to` points at an unknown agent', () => {
|
|
24
|
+
const input: t.MultiAgentGraphInput = {
|
|
25
|
+
runId: 'r1',
|
|
26
|
+
agents: [makeAgent('A')],
|
|
27
|
+
edges: [{ from: 'A', to: 'MISSING', edgeType: 'handoff' }],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
expect(() => new MultiAgentGraph(input)).toThrow(/MISSING/);
|
|
31
|
+
expect(() => new MultiAgentGraph(input)).toThrow(
|
|
32
|
+
/edges reference agent\(s\) not present in agents/
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('throws when an edge `from` points at an unknown agent', () => {
|
|
37
|
+
const input: t.MultiAgentGraphInput = {
|
|
38
|
+
runId: 'r1',
|
|
39
|
+
agents: [makeAgent('A')],
|
|
40
|
+
edges: [{ from: 'MISSING', to: 'A', edgeType: 'handoff' }],
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
expect(() => new MultiAgentGraph(input)).toThrow(/MISSING/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('reports all unknown agent ids in a single error', () => {
|
|
47
|
+
const input: t.MultiAgentGraphInput = {
|
|
48
|
+
runId: 'r1',
|
|
49
|
+
agents: [makeAgent('A')],
|
|
50
|
+
edges: [
|
|
51
|
+
{ from: 'A', to: 'B', edgeType: 'handoff' },
|
|
52
|
+
{ from: 'A', to: 'C', edgeType: 'handoff' },
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
let thrown: Error | undefined;
|
|
57
|
+
try {
|
|
58
|
+
new MultiAgentGraph(input);
|
|
59
|
+
} catch (err) {
|
|
60
|
+
thrown = err as Error;
|
|
61
|
+
}
|
|
62
|
+
expect(thrown).toBeDefined();
|
|
63
|
+
expect(thrown!.message).toMatch(/"B"/);
|
|
64
|
+
expect(thrown!.message).toMatch(/"C"/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('handles array `from` / `to` fields', () => {
|
|
68
|
+
const valid: t.MultiAgentGraphInput = {
|
|
69
|
+
runId: 'r1',
|
|
70
|
+
agents: [makeAgent('A'), makeAgent('B'), makeAgent('C')],
|
|
71
|
+
edges: [{ from: ['A'], to: ['B', 'C'], edgeType: 'direct' }],
|
|
72
|
+
};
|
|
73
|
+
expect(() => new MultiAgentGraph(valid)).not.toThrow();
|
|
74
|
+
|
|
75
|
+
const invalid: t.MultiAgentGraphInput = {
|
|
76
|
+
runId: 'r1',
|
|
77
|
+
agents: [makeAgent('A'), makeAgent('B')],
|
|
78
|
+
edges: [{ from: ['A'], to: ['B', 'C'], edgeType: 'direct' }],
|
|
79
|
+
};
|
|
80
|
+
expect(() => new MultiAgentGraph(invalid)).toThrow(/"C"/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('accepts an empty edges array (single-agent case with no handoffs)', () => {
|
|
84
|
+
const input: t.MultiAgentGraphInput = {
|
|
85
|
+
runId: 'r1',
|
|
86
|
+
agents: [makeAgent('A')],
|
|
87
|
+
edges: [],
|
|
88
|
+
};
|
|
89
|
+
expect(() => new MultiAgentGraph(input)).not.toThrow();
|
|
90
|
+
});
|
|
91
|
+
});
|