@librechat/agents 3.1.67 → 3.1.68-dev.1
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 -1
- 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 +165 -0
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +287 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +0 -9
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +7 -23
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- 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/ToolSearch.cjs +2 -13
- package/dist/cjs/tools/ToolSearch.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 -2
- 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 +159 -0
- package/dist/esm/tools/BashExecutor.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +278 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +0 -9
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +8 -24
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- 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/ToolSearch.mjs +3 -14
- package/dist/esm/tools/ToolSearch.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 -2
- 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/ProgrammaticToolCalling.d.ts +4 -9
- 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/ToolSearch.d.ts +2 -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 -10
- package/package.json +5 -1
- package/src/agents/AgentContext.ts +26 -2
- package/src/common/enum.ts +15 -1
- 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/programmatic_exec.ts +1 -10
- package/src/scripts/subagent-event-driven-debug.ts +190 -0
- package/src/scripts/subagent-tools-debug.ts +160 -0
- package/src/scripts/test_code_api.ts +0 -7
- package/src/scripts/tool_search.ts +1 -10
- 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 +193 -0
- package/src/tools/BashProgrammaticToolCalling.ts +381 -0
- package/src/tools/CodeExecutor.ts +0 -11
- package/src/tools/ProgrammaticToolCalling.ts +4 -29
- 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/ToolSearch.ts +3 -19
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +7 -8
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -1
- 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__/ToolSearch.integration.test.ts +7 -8
- 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 -10
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
import { tool, DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
+
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
4
|
+
import type * as t from '@/types';
|
|
5
|
+
import {
|
|
6
|
+
makeRequest,
|
|
7
|
+
executeTools,
|
|
8
|
+
fetchSessionFiles,
|
|
9
|
+
formatCompletedResponse,
|
|
10
|
+
} from './ProgrammaticToolCalling';
|
|
11
|
+
import { getCodeBaseURL } from './CodeExecutor';
|
|
12
|
+
import { Constants } from '@/common';
|
|
13
|
+
|
|
14
|
+
config();
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Constants
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
const DEFAULT_MAX_ROUND_TRIPS = 20;
|
|
21
|
+
const DEFAULT_TIMEOUT = 60000;
|
|
22
|
+
|
|
23
|
+
/** Bash reserved words that get `_tool` suffix when used as function names */
|
|
24
|
+
const BASH_RESERVED = new Set([
|
|
25
|
+
'if',
|
|
26
|
+
'then',
|
|
27
|
+
'else',
|
|
28
|
+
'elif',
|
|
29
|
+
'fi',
|
|
30
|
+
'case',
|
|
31
|
+
'esac',
|
|
32
|
+
'for',
|
|
33
|
+
'while',
|
|
34
|
+
'until',
|
|
35
|
+
'do',
|
|
36
|
+
'done',
|
|
37
|
+
'in',
|
|
38
|
+
'function',
|
|
39
|
+
'select',
|
|
40
|
+
'time',
|
|
41
|
+
'coproc',
|
|
42
|
+
'declare',
|
|
43
|
+
'typeset',
|
|
44
|
+
'local',
|
|
45
|
+
'readonly',
|
|
46
|
+
'export',
|
|
47
|
+
'unset',
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// Description Components
|
|
52
|
+
// ============================================================================
|
|
53
|
+
|
|
54
|
+
const STATELESS_WARNING = `CRITICAL - STATELESS EXECUTION:
|
|
55
|
+
Each call is a fresh bash shell. Variables and state do NOT persist between calls.
|
|
56
|
+
You MUST complete your entire workflow in ONE code block.
|
|
57
|
+
DO NOT split work across multiple calls expecting to reuse variables.`;
|
|
58
|
+
|
|
59
|
+
const CORE_RULES = `Rules:
|
|
60
|
+
- EVERYTHING in one call—no state persists between executions
|
|
61
|
+
- Tools are pre-defined as bash functions—DO NOT redefine them
|
|
62
|
+
- Each tool function accepts a JSON string argument
|
|
63
|
+
- Only echo/printf output returns to the model
|
|
64
|
+
- Generated files are automatically available in /mnt/data/ for subsequent executions`;
|
|
65
|
+
|
|
66
|
+
const ADDITIONAL_RULES =
|
|
67
|
+
'- Tool names normalized: hyphens→underscores, reserved words get `_tool` suffix';
|
|
68
|
+
|
|
69
|
+
const EXAMPLES = `Example (Complete workflow in one call):
|
|
70
|
+
# Query data and process
|
|
71
|
+
data=$(query_database '{"sql": "SELECT * FROM users"}')
|
|
72
|
+
echo "$data" | jq '.[] | .name'
|
|
73
|
+
|
|
74
|
+
Example (Parallel calls):
|
|
75
|
+
web_search '{"query": "SF weather"}' > /tmp/sf.txt &
|
|
76
|
+
web_search '{"query": "NY weather"}' > /tmp/ny.txt &
|
|
77
|
+
wait
|
|
78
|
+
echo "SF: $(cat /tmp/sf.txt)"
|
|
79
|
+
echo "NY: $(cat /tmp/ny.txt)"`;
|
|
80
|
+
|
|
81
|
+
const CODE_PARAM_DESCRIPTION = `Bash code that calls tools programmatically. Tools are available as bash functions.
|
|
82
|
+
|
|
83
|
+
${STATELESS_WARNING}
|
|
84
|
+
|
|
85
|
+
Each tool function accepts a JSON string as its argument.
|
|
86
|
+
Example: tool_name '{"key": "value"}'
|
|
87
|
+
|
|
88
|
+
${EXAMPLES}
|
|
89
|
+
|
|
90
|
+
${CORE_RULES}`;
|
|
91
|
+
|
|
92
|
+
// ============================================================================
|
|
93
|
+
// Schema
|
|
94
|
+
// ============================================================================
|
|
95
|
+
|
|
96
|
+
export const BashProgrammaticToolCallingSchema = {
|
|
97
|
+
type: 'object',
|
|
98
|
+
properties: {
|
|
99
|
+
code: {
|
|
100
|
+
type: 'string',
|
|
101
|
+
minLength: 1,
|
|
102
|
+
description: CODE_PARAM_DESCRIPTION,
|
|
103
|
+
},
|
|
104
|
+
timeout: {
|
|
105
|
+
type: 'integer',
|
|
106
|
+
minimum: 1000,
|
|
107
|
+
maximum: 300000,
|
|
108
|
+
default: DEFAULT_TIMEOUT,
|
|
109
|
+
description:
|
|
110
|
+
'Maximum execution time in milliseconds. Default: 60 seconds. Max: 5 minutes.',
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
required: ['code'],
|
|
114
|
+
} as const;
|
|
115
|
+
|
|
116
|
+
export const BashProgrammaticToolCallingName =
|
|
117
|
+
Constants.BASH_PROGRAMMATIC_TOOL_CALLING;
|
|
118
|
+
|
|
119
|
+
export const BashProgrammaticToolCallingDescription = `
|
|
120
|
+
Run tools via bash code. Tools are available as bash functions that accept JSON string arguments.
|
|
121
|
+
|
|
122
|
+
${STATELESS_WARNING}
|
|
123
|
+
|
|
124
|
+
${CORE_RULES}
|
|
125
|
+
${ADDITIONAL_RULES}
|
|
126
|
+
|
|
127
|
+
When to use: shell pipelines, parallel execution (& and wait), file processing, text manipulation.
|
|
128
|
+
|
|
129
|
+
${EXAMPLES}
|
|
130
|
+
`.trim();
|
|
131
|
+
|
|
132
|
+
export const BashProgrammaticToolCallingDefinition = {
|
|
133
|
+
name: BashProgrammaticToolCallingName,
|
|
134
|
+
description: BashProgrammaticToolCallingDescription,
|
|
135
|
+
schema: BashProgrammaticToolCallingSchema,
|
|
136
|
+
} as const;
|
|
137
|
+
|
|
138
|
+
// ============================================================================
|
|
139
|
+
// Helper Functions
|
|
140
|
+
// ============================================================================
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Normalizes a tool name to a valid bash function identifier.
|
|
144
|
+
* 1. Replace hyphens, spaces, dots with underscores
|
|
145
|
+
* 2. Remove any other invalid characters
|
|
146
|
+
* 3. Prefix with underscore if starts with number
|
|
147
|
+
* 4. Append `_tool` if it's a bash reserved word
|
|
148
|
+
*/
|
|
149
|
+
export function normalizeToBashIdentifier(name: string): string {
|
|
150
|
+
let normalized = name.replace(/[-\s.]/g, '_');
|
|
151
|
+
normalized = normalized.replace(/[^a-zA-Z0-9_]/g, '');
|
|
152
|
+
|
|
153
|
+
if (/^[0-9]/.test(normalized)) {
|
|
154
|
+
normalized = '_' + normalized;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (BASH_RESERVED.has(normalized)) {
|
|
158
|
+
normalized = normalized + '_tool';
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return normalized;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Extracts tool names that are actually called in the bash code.
|
|
166
|
+
* Bash functions are invoked as commands (no parentheses), so we match
|
|
167
|
+
* the normalized name as a word boundary.
|
|
168
|
+
*/
|
|
169
|
+
export function extractUsedBashToolNames(
|
|
170
|
+
code: string,
|
|
171
|
+
toolNameMap: Map<string, string>
|
|
172
|
+
): Set<string> {
|
|
173
|
+
const usedTools = new Set<string>();
|
|
174
|
+
|
|
175
|
+
for (const [bashName, originalName] of toolNameMap) {
|
|
176
|
+
const escapedName = bashName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
177
|
+
const pattern = new RegExp(`\\b${escapedName}\\b`, 'g');
|
|
178
|
+
|
|
179
|
+
if (pattern.test(code)) {
|
|
180
|
+
usedTools.add(originalName);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return usedTools;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Filters tool definitions to only include tools actually used in the bash code.
|
|
189
|
+
*/
|
|
190
|
+
export function filterBashToolsByUsage(
|
|
191
|
+
toolDefs: t.LCTool[],
|
|
192
|
+
code: string,
|
|
193
|
+
debug = false
|
|
194
|
+
): t.LCTool[] {
|
|
195
|
+
const toolNameMap = new Map<string, string>();
|
|
196
|
+
for (const def of toolDefs) {
|
|
197
|
+
const bashName = normalizeToBashIdentifier(def.name);
|
|
198
|
+
toolNameMap.set(bashName, def.name);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const usedToolNames = extractUsedBashToolNames(code, toolNameMap);
|
|
202
|
+
|
|
203
|
+
if (debug) {
|
|
204
|
+
// eslint-disable-next-line no-console
|
|
205
|
+
console.log(
|
|
206
|
+
`[BashPTC Debug] Tool filtering: found ${usedToolNames.size}/${toolDefs.length} tools in code`
|
|
207
|
+
);
|
|
208
|
+
if (usedToolNames.size > 0) {
|
|
209
|
+
// eslint-disable-next-line no-console
|
|
210
|
+
console.log(
|
|
211
|
+
`[BashPTC Debug] Matched tools: ${Array.from(usedToolNames).join(', ')}`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (usedToolNames.size === 0) {
|
|
217
|
+
if (debug) {
|
|
218
|
+
// eslint-disable-next-line no-console
|
|
219
|
+
console.log(
|
|
220
|
+
'[BashPTC Debug] No tools detected in code - sending all tools as fallback'
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return toolDefs;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return toolDefs.filter((def) => usedToolNames.has(def.name));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ============================================================================
|
|
230
|
+
// Tool Factory
|
|
231
|
+
// ============================================================================
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Creates a Bash Programmatic Tool Calling tool for multi-tool orchestration.
|
|
235
|
+
*
|
|
236
|
+
* This tool enables AI agents to write bash scripts that orchestrate multiple
|
|
237
|
+
* tool calls programmatically via the remote Code API, reducing LLM round-trips.
|
|
238
|
+
*
|
|
239
|
+
* The tool map must be provided at runtime via config.toolCall (injected by ToolNode).
|
|
240
|
+
*/
|
|
241
|
+
export function createBashProgrammaticToolCallingTool(
|
|
242
|
+
initParams: t.BashProgrammaticToolCallingParams = {}
|
|
243
|
+
): DynamicStructuredTool {
|
|
244
|
+
const baseUrl = initParams.baseUrl ?? getCodeBaseURL();
|
|
245
|
+
const maxRoundTrips = initParams.maxRoundTrips ?? DEFAULT_MAX_ROUND_TRIPS;
|
|
246
|
+
const proxy = initParams.proxy ?? process.env.PROXY;
|
|
247
|
+
const debug = initParams.debug ?? process.env.BASH_PTC_DEBUG === 'true';
|
|
248
|
+
const EXEC_ENDPOINT = `${baseUrl}/exec/programmatic`;
|
|
249
|
+
|
|
250
|
+
return tool(
|
|
251
|
+
async (rawParams, config) => {
|
|
252
|
+
const params = rawParams as { code: string; timeout?: number };
|
|
253
|
+
const { code, timeout = DEFAULT_TIMEOUT } = params;
|
|
254
|
+
|
|
255
|
+
const { toolMap, toolDefs, session_id, _injected_files } =
|
|
256
|
+
(config.toolCall ?? {}) as ToolCall &
|
|
257
|
+
Partial<t.ProgrammaticCache> & {
|
|
258
|
+
session_id?: string;
|
|
259
|
+
_injected_files?: t.CodeEnvFile[];
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
if (toolMap == null || toolMap.size === 0) {
|
|
263
|
+
throw new Error(
|
|
264
|
+
'No toolMap provided. ' +
|
|
265
|
+
'ToolNode should inject this from AgentContext when invoked through the graph.'
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (toolDefs == null || toolDefs.length === 0) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
'No tool definitions provided. ' +
|
|
272
|
+
'Either pass tools in the input or ensure ToolNode injects toolDefs.'
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
let roundTrip = 0;
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
// ====================================================================
|
|
280
|
+
// Phase 1: Filter tools and make initial request
|
|
281
|
+
// ====================================================================
|
|
282
|
+
|
|
283
|
+
const effectiveTools = filterBashToolsByUsage(toolDefs, code, debug);
|
|
284
|
+
|
|
285
|
+
if (debug) {
|
|
286
|
+
// eslint-disable-next-line no-console
|
|
287
|
+
console.log(
|
|
288
|
+
`[BashPTC Debug] Sending ${effectiveTools.length} tools to API ` +
|
|
289
|
+
`(filtered from ${toolDefs.length})`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let files: t.CodeEnvFile[] | undefined;
|
|
294
|
+
if (_injected_files && _injected_files.length > 0) {
|
|
295
|
+
files = _injected_files;
|
|
296
|
+
} else if (session_id != null && session_id.length > 0) {
|
|
297
|
+
files = await fetchSessionFiles(baseUrl, session_id, proxy);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
let response = await makeRequest(
|
|
301
|
+
EXEC_ENDPOINT,
|
|
302
|
+
{
|
|
303
|
+
lang: 'bash',
|
|
304
|
+
code,
|
|
305
|
+
tools: effectiveTools,
|
|
306
|
+
session_id,
|
|
307
|
+
timeout,
|
|
308
|
+
...(files && files.length > 0 ? { files } : {}),
|
|
309
|
+
},
|
|
310
|
+
proxy
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
// ====================================================================
|
|
314
|
+
// Phase 2: Handle response loop
|
|
315
|
+
// ====================================================================
|
|
316
|
+
|
|
317
|
+
while (response.status === 'tool_call_required') {
|
|
318
|
+
roundTrip++;
|
|
319
|
+
|
|
320
|
+
if (roundTrip > maxRoundTrips) {
|
|
321
|
+
throw new Error(
|
|
322
|
+
`Exceeded maximum round trips (${maxRoundTrips}). ` +
|
|
323
|
+
'This may indicate an infinite loop, excessive tool calls, ' +
|
|
324
|
+
'or a logic error in your code.'
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (debug) {
|
|
329
|
+
// eslint-disable-next-line no-console
|
|
330
|
+
console.log(
|
|
331
|
+
`[BashPTC Debug] Round trip ${roundTrip}: ${response.tool_calls?.length ?? 0} tool(s) to execute`
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const toolResults = await executeTools(
|
|
336
|
+
response.tool_calls ?? [],
|
|
337
|
+
toolMap
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
response = await makeRequest(
|
|
341
|
+
EXEC_ENDPOINT,
|
|
342
|
+
{
|
|
343
|
+
continuation_token: response.continuation_token,
|
|
344
|
+
tool_results: toolResults,
|
|
345
|
+
},
|
|
346
|
+
proxy
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ====================================================================
|
|
351
|
+
// Phase 3: Handle final state
|
|
352
|
+
// ====================================================================
|
|
353
|
+
|
|
354
|
+
if (response.status === 'completed') {
|
|
355
|
+
return formatCompletedResponse(response);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (response.status === 'error') {
|
|
359
|
+
throw new Error(
|
|
360
|
+
`Execution error: ${response.error}` +
|
|
361
|
+
(response.stderr != null && response.stderr !== ''
|
|
362
|
+
? `\n\nStderr:\n${response.stderr}`
|
|
363
|
+
: '')
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
throw new Error(`Unexpected response status: ${response.status}`);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
`Bash programmatic execution failed: ${(error as Error).message}`
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
name: Constants.BASH_PROGRAMMATIC_TOOL_CALLING,
|
|
376
|
+
description: BashProgrammaticToolCallingDescription,
|
|
377
|
+
schema: BashProgrammaticToolCallingSchema,
|
|
378
|
+
responseFormat: Constants.CONTENT_AND_ARTIFACT,
|
|
379
|
+
}
|
|
380
|
+
);
|
|
381
|
+
}
|
|
@@ -95,15 +95,6 @@ export const CodeExecutionToolDefinition = {
|
|
|
95
95
|
function createCodeExecutionTool(
|
|
96
96
|
params: t.CodeExecutionToolParams = {}
|
|
97
97
|
): DynamicStructuredTool {
|
|
98
|
-
const apiKey =
|
|
99
|
-
params[EnvVar.CODE_API_KEY] ??
|
|
100
|
-
params.apiKey ??
|
|
101
|
-
getEnvironmentVariable(EnvVar.CODE_API_KEY) ??
|
|
102
|
-
'';
|
|
103
|
-
if (!apiKey) {
|
|
104
|
-
throw new Error('No API key provided for code execution tool.');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
98
|
return tool(
|
|
108
99
|
async (rawInput, config) => {
|
|
109
100
|
const { lang, code, ...rest } = rawInput as {
|
|
@@ -143,7 +134,6 @@ function createCodeExecutionTool(
|
|
|
143
134
|
method: 'GET',
|
|
144
135
|
headers: {
|
|
145
136
|
'User-Agent': 'LibreChat/1.0',
|
|
146
|
-
'X-API-Key': apiKey,
|
|
147
137
|
},
|
|
148
138
|
};
|
|
149
139
|
|
|
@@ -185,7 +175,6 @@ function createCodeExecutionTool(
|
|
|
185
175
|
headers: {
|
|
186
176
|
'Content-Type': 'application/json',
|
|
187
177
|
'User-Agent': 'LibreChat/1.0',
|
|
188
|
-
'X-API-Key': apiKey,
|
|
189
178
|
},
|
|
190
179
|
body: JSON.stringify(postData),
|
|
191
180
|
};
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
import { config } from 'dotenv';
|
|
3
3
|
import fetch, { RequestInit } from 'node-fetch';
|
|
4
4
|
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
5
|
-
import { getEnvironmentVariable } from '@langchain/core/utils/env';
|
|
6
5
|
import { tool, DynamicStructuredTool } from '@langchain/core/tools';
|
|
7
6
|
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
8
7
|
import type * as t from '@/types';
|
|
9
8
|
import { imageExtRegex, getCodeBaseURL } from './CodeExecutor';
|
|
10
|
-
import {
|
|
9
|
+
import { Constants } from '@/common';
|
|
11
10
|
|
|
12
11
|
config();
|
|
13
12
|
|
|
@@ -261,14 +260,12 @@ export function filterToolsByUsage(
|
|
|
261
260
|
* Fetches files from a previous session to make them available for the current execution.
|
|
262
261
|
* Files are returned as CodeEnvFile references to be included in the request.
|
|
263
262
|
* @param baseUrl - The base URL for the Code API
|
|
264
|
-
* @param apiKey - The API key for authentication
|
|
265
263
|
* @param sessionId - The session ID to fetch files from
|
|
266
264
|
* @param proxy - Optional HTTP proxy URL
|
|
267
265
|
* @returns Array of CodeEnvFile references, or empty array if fetch fails
|
|
268
266
|
*/
|
|
269
267
|
export async function fetchSessionFiles(
|
|
270
268
|
baseUrl: string,
|
|
271
|
-
apiKey: string,
|
|
272
269
|
sessionId: string,
|
|
273
270
|
proxy?: string
|
|
274
271
|
): Promise<t.CodeEnvFile[]> {
|
|
@@ -278,7 +275,6 @@ export async function fetchSessionFiles(
|
|
|
278
275
|
method: 'GET',
|
|
279
276
|
headers: {
|
|
280
277
|
'User-Agent': 'LibreChat/1.0',
|
|
281
|
-
'X-API-Key': apiKey,
|
|
282
278
|
},
|
|
283
279
|
};
|
|
284
280
|
|
|
@@ -321,14 +317,12 @@ export async function fetchSessionFiles(
|
|
|
321
317
|
/**
|
|
322
318
|
* Makes an HTTP request to the Code API.
|
|
323
319
|
* @param endpoint - The API endpoint URL
|
|
324
|
-
* @param apiKey - The API key for authentication
|
|
325
320
|
* @param body - The request body
|
|
326
321
|
* @param proxy - Optional HTTP proxy URL
|
|
327
322
|
* @returns The parsed API response
|
|
328
323
|
*/
|
|
329
324
|
export async function makeRequest(
|
|
330
325
|
endpoint: string,
|
|
331
|
-
apiKey: string,
|
|
332
326
|
body: Record<string, unknown>,
|
|
333
327
|
proxy?: string
|
|
334
328
|
): Promise<t.ProgrammaticExecutionResponse> {
|
|
@@ -337,7 +331,6 @@ export async function makeRequest(
|
|
|
337
331
|
headers: {
|
|
338
332
|
'Content-Type': 'application/json',
|
|
339
333
|
'User-Agent': 'LibreChat/1.0',
|
|
340
|
-
'X-API-Key': apiKey,
|
|
341
334
|
},
|
|
342
335
|
body: JSON.stringify(body),
|
|
343
336
|
};
|
|
@@ -596,14 +589,11 @@ export function formatCompletedResponse(
|
|
|
596
589
|
*
|
|
597
590
|
* The tool map must be provided at runtime via config.configurable.toolMap.
|
|
598
591
|
*
|
|
599
|
-
* @param params - Configuration parameters (
|
|
592
|
+
* @param params - Configuration parameters (baseUrl, maxRoundTrips, proxy)
|
|
600
593
|
* @returns A LangChain DynamicStructuredTool for programmatic tool calling
|
|
601
594
|
*
|
|
602
595
|
* @example
|
|
603
|
-
* const ptcTool = createProgrammaticToolCallingTool({
|
|
604
|
-
* apiKey: process.env.CODE_API_KEY,
|
|
605
|
-
* maxRoundTrips: 20
|
|
606
|
-
* });
|
|
596
|
+
* const ptcTool = createProgrammaticToolCallingTool({ maxRoundTrips: 20 });
|
|
607
597
|
*
|
|
608
598
|
* const [output, artifact] = await ptcTool.invoke(
|
|
609
599
|
* { code, tools },
|
|
@@ -613,19 +603,6 @@ export function formatCompletedResponse(
|
|
|
613
603
|
export function createProgrammaticToolCallingTool(
|
|
614
604
|
initParams: t.ProgrammaticToolCallingParams = {}
|
|
615
605
|
): DynamicStructuredTool {
|
|
616
|
-
const apiKey =
|
|
617
|
-
(initParams[EnvVar.CODE_API_KEY] as string | undefined) ??
|
|
618
|
-
initParams.apiKey ??
|
|
619
|
-
getEnvironmentVariable(EnvVar.CODE_API_KEY) ??
|
|
620
|
-
'';
|
|
621
|
-
|
|
622
|
-
if (!apiKey) {
|
|
623
|
-
throw new Error(
|
|
624
|
-
'No API key provided for programmatic tool calling. ' +
|
|
625
|
-
'Set CODE_API_KEY environment variable or pass apiKey in initParams.'
|
|
626
|
-
);
|
|
627
|
-
}
|
|
628
|
-
|
|
629
606
|
const baseUrl = initParams.baseUrl ?? getCodeBaseURL();
|
|
630
607
|
const maxRoundTrips = initParams.maxRoundTrips ?? DEFAULT_MAX_ROUND_TRIPS;
|
|
631
608
|
const proxy = initParams.proxy ?? process.env.PROXY;
|
|
@@ -685,12 +662,11 @@ export function createProgrammaticToolCallingTool(
|
|
|
685
662
|
if (_injected_files && _injected_files.length > 0) {
|
|
686
663
|
files = _injected_files;
|
|
687
664
|
} else if (session_id != null && session_id.length > 0) {
|
|
688
|
-
files = await fetchSessionFiles(baseUrl,
|
|
665
|
+
files = await fetchSessionFiles(baseUrl, session_id, proxy);
|
|
689
666
|
}
|
|
690
667
|
|
|
691
668
|
let response = await makeRequest(
|
|
692
669
|
EXEC_ENDPOINT,
|
|
693
|
-
apiKey,
|
|
694
670
|
{
|
|
695
671
|
code,
|
|
696
672
|
tools: effectiveTools,
|
|
@@ -730,7 +706,6 @@ export function createProgrammaticToolCallingTool(
|
|
|
730
706
|
|
|
731
707
|
response = await makeRequest(
|
|
732
708
|
EXEC_ENDPOINT,
|
|
733
|
-
apiKey,
|
|
734
709
|
{
|
|
735
710
|
continuation_token: response.continuation_token,
|
|
736
711
|
tool_results: toolResults,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/tools/ReadFile.ts
|
|
2
|
+
import { Constants } from '@/common';
|
|
3
|
+
|
|
4
|
+
export const ReadFileToolName = Constants.READ_FILE;
|
|
5
|
+
|
|
6
|
+
export const ReadFileToolDescription = `Read the contents of a file. Returns text content with line numbers for easy reference.
|
|
7
|
+
|
|
8
|
+
For skill files, use the path format: {skillName}/{filePath} (e.g. "pdf-analyzer/src/utils.py", "code-review/SKILL.md").
|
|
9
|
+
|
|
10
|
+
BEHAVIOR:
|
|
11
|
+
- Text files: returned with numbered lines.
|
|
12
|
+
- Images (png, jpeg, gif, webp): returned as visual content the model can see.
|
|
13
|
+
- PDFs: returned as document content.
|
|
14
|
+
- Other binary files: metadata returned with a note to use bash for processing.
|
|
15
|
+
- Large files (>256KB text, >10MB binary): metadata only.
|
|
16
|
+
- SKILL.md: returns the skill's instructions directly.
|
|
17
|
+
|
|
18
|
+
CONSTRAINTS:
|
|
19
|
+
- Only files from invoked skills or code execution output are accessible.
|
|
20
|
+
- Do not guess file paths. Use paths from the skill documentation or tool output.`;
|
|
21
|
+
|
|
22
|
+
export const ReadFileToolSchema = {
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
file_path: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description:
|
|
28
|
+
'Path to the file. For skill files: "{skillName}/{path}" (e.g. "pdf-analyzer/src/utils.py"). For code execution output: the path as returned by the execution tool.',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
required: ['file_path'],
|
|
32
|
+
} as const;
|
|
33
|
+
|
|
34
|
+
export const ReadFileToolDefinition = {
|
|
35
|
+
name: ReadFileToolName,
|
|
36
|
+
description: ReadFileToolDescription,
|
|
37
|
+
parameters: ReadFileToolSchema,
|
|
38
|
+
responseFormat: 'content_and_artifact' as const,
|
|
39
|
+
} as const;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/tools/SkillTool.ts
|
|
2
|
+
import { Constants } from '@/common';
|
|
3
|
+
|
|
4
|
+
export const SkillToolName = Constants.SKILL_TOOL;
|
|
5
|
+
|
|
6
|
+
export const SkillToolDescription = `Invoke a skill from the user's library. Skills provide domain-specific instructions loaded into the conversation context, and may also provide files accessible via available tools depending on the runtime environment.
|
|
7
|
+
|
|
8
|
+
WHEN TO USE:
|
|
9
|
+
- The user's request matches a skill listed in the "Available Skills" section of the system prompt.
|
|
10
|
+
- You MUST invoke the matching skill BEFORE attempting the task yourself.
|
|
11
|
+
|
|
12
|
+
WHAT HAPPENS:
|
|
13
|
+
- The skill's full instructions are loaded into the conversation as context.
|
|
14
|
+
- Files bundled with the skill may become accessible via available tools.
|
|
15
|
+
- Follow the skill's instructions to complete the task.
|
|
16
|
+
|
|
17
|
+
CONSTRAINTS:
|
|
18
|
+
- Do not invoke a skill that is already active in this conversation.
|
|
19
|
+
- Skill names come from the catalog only. Do not guess names.`;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* JSON Schema for the SkillTool parameters.
|
|
23
|
+
* Single source of truth used by both SkillToolDefinition (LCTool registry)
|
|
24
|
+
* and createSkillTool() (DynamicStructuredTool instance).
|
|
25
|
+
*/
|
|
26
|
+
export const SkillToolSchema = {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
skillName: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description:
|
|
32
|
+
'The kebab-case identifier of the skill to invoke (e.g. "financial-analyzer", "meeting-notes"). Must match a name from the "Available Skills" section.',
|
|
33
|
+
},
|
|
34
|
+
args: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
description: 'Optional freeform arguments string passed to the skill.',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
required: ['skillName'],
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
export const SkillToolDefinition = {
|
|
43
|
+
name: SkillToolName,
|
|
44
|
+
description: SkillToolDescription,
|
|
45
|
+
parameters: SkillToolSchema,
|
|
46
|
+
} as const;
|