@librechat/agents 3.0.35 → 3.0.40
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 +71 -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/events.cjs +3 -0
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +7 -2
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/instrumentation.cjs +1 -1
- package/dist/cjs/instrumentation.cjs.map +1 -1
- package/dist/cjs/main.cjs +12 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +329 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +34 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearchRegex.cjs +455 -0
- package/dist/cjs/tools/ToolSearchRegex.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +71 -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/events.mjs +4 -1
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +7 -2
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/instrumentation.mjs +1 -1
- package/dist/esm/instrumentation.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +324 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +34 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearchRegex.mjs +448 -0
- package/dist/esm/tools/ToolSearchRegex.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +25 -1
- package/dist/types/common/enum.d.ts +2 -0
- package/dist/types/graphs/Graph.d.ts +2 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/test/mockTools.d.ts +28 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +86 -0
- package/dist/types/tools/ToolNode.d.ts +7 -1
- package/dist/types/tools/ToolSearchRegex.d.ts +80 -0
- package/dist/types/types/graph.d.ts +7 -1
- package/dist/types/types/tools.d.ts +136 -0
- package/package.json +5 -1
- package/src/agents/AgentContext.ts +86 -0
- package/src/common/enum.ts +2 -0
- package/src/events.ts +5 -1
- package/src/graphs/Graph.ts +8 -1
- package/src/index.ts +2 -0
- package/src/instrumentation.ts +1 -1
- package/src/llm/google/llm.spec.ts +3 -1
- package/src/scripts/code_exec_ptc.ts +277 -0
- package/src/scripts/programmatic_exec.ts +396 -0
- package/src/scripts/programmatic_exec_agent.ts +231 -0
- package/src/scripts/tool_search_regex.ts +162 -0
- package/src/test/mockTools.ts +366 -0
- package/src/tools/ProgrammaticToolCalling.ts +423 -0
- package/src/tools/ToolNode.ts +38 -4
- package/src/tools/ToolSearchRegex.ts +535 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +318 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +613 -0
- package/src/tools/__tests__/ToolSearchRegex.integration.test.ts +161 -0
- package/src/tools/__tests__/ToolSearchRegex.test.ts +232 -0
- package/src/types/graph.ts +7 -1
- package/src/types/tools.ts +166 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
+
import type * as t from '@/types';
|
|
4
|
+
declare const ProgrammaticToolCallingSchema: z.ZodObject<{
|
|
5
|
+
code: z.ZodString;
|
|
6
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
7
|
+
name: z.ZodString;
|
|
8
|
+
description: z.ZodOptional<z.ZodString>;
|
|
9
|
+
parameters: z.ZodAny;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
name: string;
|
|
12
|
+
description?: string | undefined;
|
|
13
|
+
parameters?: any;
|
|
14
|
+
}, {
|
|
15
|
+
name: string;
|
|
16
|
+
description?: string | undefined;
|
|
17
|
+
parameters?: any;
|
|
18
|
+
}>, "many">>;
|
|
19
|
+
session_id: z.ZodOptional<z.ZodString>;
|
|
20
|
+
timeout: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
21
|
+
}, "strip", z.ZodTypeAny, {
|
|
22
|
+
code: string;
|
|
23
|
+
timeout: number;
|
|
24
|
+
tools?: {
|
|
25
|
+
name: string;
|
|
26
|
+
description?: string | undefined;
|
|
27
|
+
parameters?: any;
|
|
28
|
+
}[] | undefined;
|
|
29
|
+
session_id?: string | undefined;
|
|
30
|
+
}, {
|
|
31
|
+
code: string;
|
|
32
|
+
tools?: {
|
|
33
|
+
name: string;
|
|
34
|
+
description?: string | undefined;
|
|
35
|
+
parameters?: any;
|
|
36
|
+
}[] | undefined;
|
|
37
|
+
timeout?: number | undefined;
|
|
38
|
+
session_id?: string | undefined;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Makes an HTTP request to the Code API.
|
|
42
|
+
* @param endpoint - The API endpoint URL
|
|
43
|
+
* @param apiKey - The API key for authentication
|
|
44
|
+
* @param body - The request body
|
|
45
|
+
* @param proxy - Optional HTTP proxy URL
|
|
46
|
+
* @returns The parsed API response
|
|
47
|
+
*/
|
|
48
|
+
export declare function makeRequest(endpoint: string, apiKey: string, body: Record<string, unknown>, proxy?: string): Promise<t.ProgrammaticExecutionResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* Executes tools in parallel when requested by the API.
|
|
51
|
+
* Uses Promise.all for parallel execution, catching individual errors.
|
|
52
|
+
* @param toolCalls - Array of tool calls from the API
|
|
53
|
+
* @param toolMap - Map of tool names to executable tools
|
|
54
|
+
* @returns Array of tool results
|
|
55
|
+
*/
|
|
56
|
+
export declare function executeTools(toolCalls: t.PTCToolCall[], toolMap: t.ToolMap): Promise<t.PTCToolResult[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Formats the completed response for the agent.
|
|
59
|
+
* @param response - The completed API response
|
|
60
|
+
* @returns Tuple of [formatted string, artifact]
|
|
61
|
+
*/
|
|
62
|
+
export declare function formatCompletedResponse(response: t.ProgrammaticExecutionResponse): [string, t.ProgrammaticExecutionArtifact];
|
|
63
|
+
/**
|
|
64
|
+
* Creates a Programmatic Tool Calling tool for complex multi-tool workflows.
|
|
65
|
+
*
|
|
66
|
+
* This tool enables AI agents to write Python code that orchestrates multiple
|
|
67
|
+
* tool calls programmatically, reducing LLM round-trips and token usage.
|
|
68
|
+
*
|
|
69
|
+
* The tool map must be provided at runtime via config.configurable.toolMap.
|
|
70
|
+
*
|
|
71
|
+
* @param params - Configuration parameters (apiKey, baseUrl, maxRoundTrips, proxy)
|
|
72
|
+
* @returns A LangChain DynamicStructuredTool for programmatic tool calling
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const ptcTool = createProgrammaticToolCallingTool({
|
|
76
|
+
* apiKey: process.env.CODE_API_KEY,
|
|
77
|
+
* maxRoundTrips: 20
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* const [output, artifact] = await ptcTool.invoke(
|
|
81
|
+
* { code, tools },
|
|
82
|
+
* { configurable: { toolMap } }
|
|
83
|
+
* );
|
|
84
|
+
*/
|
|
85
|
+
export declare function createProgrammaticToolCallingTool(initParams?: t.ProgrammaticToolCallingParams): DynamicStructuredTool<typeof ProgrammaticToolCallingSchema>;
|
|
86
|
+
export {};
|
|
@@ -13,7 +13,13 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
13
13
|
toolCallStepIds?: Map<string, string>;
|
|
14
14
|
errorHandler?: t.ToolNodeConstructorParams['errorHandler'];
|
|
15
15
|
private toolUsageCount;
|
|
16
|
-
|
|
16
|
+
/** Tools available for programmatic code execution */
|
|
17
|
+
private programmaticToolMap?;
|
|
18
|
+
/** Tool definitions for programmatic code execution (sent to Code API) */
|
|
19
|
+
private programmaticToolDefs?;
|
|
20
|
+
/** Tool registry for tool search (deferred tools) */
|
|
21
|
+
private toolRegistry?;
|
|
22
|
+
constructor({ tools, toolMap, name, tags, errorHandler, toolCallStepIds, handleToolErrors, loadRuntimeTools, programmaticToolMap, programmaticToolDefs, toolRegistry, }: t.ToolNodeConstructorParams);
|
|
17
23
|
/**
|
|
18
24
|
* Returns a snapshot of the current tool usage counts.
|
|
19
25
|
* @returns A ReadonlyMap where keys are tool names and values are their usage counts.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
+
import type * as t from '@/types';
|
|
4
|
+
declare const ToolSearchRegexSchema: z.ZodObject<{
|
|
5
|
+
query: z.ZodString;
|
|
6
|
+
fields: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodEnum<["name", "description", "parameters"]>, "many">>>;
|
|
7
|
+
max_results: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
query: string;
|
|
10
|
+
fields: ("name" | "description" | "parameters")[];
|
|
11
|
+
max_results: number;
|
|
12
|
+
}, {
|
|
13
|
+
query: string;
|
|
14
|
+
fields?: ("name" | "description" | "parameters")[] | undefined;
|
|
15
|
+
max_results?: number | undefined;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Escapes special regex characters in a string to use as a literal pattern.
|
|
19
|
+
* @param pattern - The string to escape
|
|
20
|
+
* @returns The escaped string safe for use in a RegExp
|
|
21
|
+
*/
|
|
22
|
+
declare function escapeRegexSpecialChars(pattern: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Counts the maximum nesting depth of groups in a regex pattern.
|
|
25
|
+
* @param pattern - The regex pattern to analyze
|
|
26
|
+
* @returns The maximum nesting depth
|
|
27
|
+
*/
|
|
28
|
+
declare function countNestedGroups(pattern: string): number;
|
|
29
|
+
/**
|
|
30
|
+
* Detects nested quantifiers that can cause catastrophic backtracking.
|
|
31
|
+
* Patterns like (a+)+, (a*)*, (a+)*, etc.
|
|
32
|
+
* @param pattern - The regex pattern to check
|
|
33
|
+
* @returns True if nested quantifiers are detected
|
|
34
|
+
*/
|
|
35
|
+
declare function hasNestedQuantifiers(pattern: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a regex pattern contains potentially dangerous constructs.
|
|
38
|
+
* @param pattern - The regex pattern to validate
|
|
39
|
+
* @returns True if the pattern is dangerous
|
|
40
|
+
*/
|
|
41
|
+
declare function isDangerousPattern(pattern: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Sanitizes a regex pattern for safe execution.
|
|
44
|
+
* If the pattern is dangerous, it will be escaped to a literal string search.
|
|
45
|
+
* @param pattern - The regex pattern to sanitize
|
|
46
|
+
* @returns Object containing the safe pattern and whether it was escaped
|
|
47
|
+
*/
|
|
48
|
+
declare function sanitizeRegex(pattern: string): {
|
|
49
|
+
safe: string;
|
|
50
|
+
wasEscaped: boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Creates a Tool Search Regex tool for discovering tools from a large registry.
|
|
54
|
+
*
|
|
55
|
+
* This tool enables AI agents to dynamically discover tools from a large library
|
|
56
|
+
* without loading all tool definitions into the LLM context window. The agent
|
|
57
|
+
* can search for relevant tools on-demand using regex patterns.
|
|
58
|
+
*
|
|
59
|
+
* The tool registry can be provided either:
|
|
60
|
+
* 1. At initialization time via params.toolRegistry
|
|
61
|
+
* 2. At runtime via config.configurable.toolRegistry when invoking
|
|
62
|
+
*
|
|
63
|
+
* @param params - Configuration parameters for the tool (toolRegistry is optional)
|
|
64
|
+
* @returns A LangChain DynamicStructuredTool for tool searching
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // Option 1: Registry at initialization
|
|
68
|
+
* const tool = createToolSearchRegexTool({ apiKey, toolRegistry });
|
|
69
|
+
* await tool.invoke({ query: 'expense' });
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Option 2: Registry at runtime
|
|
73
|
+
* const tool = createToolSearchRegexTool({ apiKey });
|
|
74
|
+
* await tool.invoke(
|
|
75
|
+
* { query: 'expense' },
|
|
76
|
+
* { configurable: { toolRegistry, onlyDeferred: true } }
|
|
77
|
+
* );
|
|
78
|
+
*/
|
|
79
|
+
declare function createToolSearchRegexTool(initParams?: t.ToolSearchRegexParams): DynamicStructuredTool<typeof ToolSearchRegexSchema>;
|
|
80
|
+
export { createToolSearchRegexTool, sanitizeRegex, escapeRegexSpecialChars, isDangerousPattern, countNestedGroups, hasNestedQuantifiers, };
|
|
@@ -4,7 +4,7 @@ import type { BaseMessage, AIMessageChunk, SystemMessage } from '@langchain/core
|
|
|
4
4
|
import type { RunnableConfig, Runnable } from '@langchain/core/runnables';
|
|
5
5
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
6
6
|
import type { GoogleAIToolType } from '@langchain/google-common';
|
|
7
|
-
import type { ToolMap, ToolEndEvent, GenericTool } from '@/types/tools';
|
|
7
|
+
import type { ToolMap, ToolEndEvent, GenericTool, LCTool } from '@/types/tools';
|
|
8
8
|
import type { Providers, Callback, GraphNodeKeys } from '@/common';
|
|
9
9
|
import type { StandardGraph, MultiAgentGraph } from '@/graphs';
|
|
10
10
|
import type { ClientOptions } from '@/types/llm';
|
|
@@ -248,4 +248,10 @@ export interface AgentInputs {
|
|
|
248
248
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
249
249
|
/** Format content blocks as strings (for legacy compatibility i.e. Ollama/Azure Serverless) */
|
|
250
250
|
useLegacyContent?: boolean;
|
|
251
|
+
/**
|
|
252
|
+
* Tool definitions for all tools, including deferred and programmatic.
|
|
253
|
+
* Used for tool search and programmatic tool calling.
|
|
254
|
+
* Maps tool name to LCTool definition.
|
|
255
|
+
*/
|
|
256
|
+
toolRegistry?: Map<string, LCTool>;
|
|
251
257
|
}
|
|
@@ -25,6 +25,12 @@ export type ToolNodeOptions = {
|
|
|
25
25
|
loadRuntimeTools?: ToolRefGenerator;
|
|
26
26
|
toolCallStepIds?: Map<string, string>;
|
|
27
27
|
errorHandler?: (data: ToolErrorData, metadata?: Record<string, unknown>) => Promise<void>;
|
|
28
|
+
/** Tools available for programmatic code execution (allowed_callers includes 'code_execution') */
|
|
29
|
+
programmaticToolMap?: ToolMap;
|
|
30
|
+
/** Tool definitions for programmatic code execution (sent to Code API for stub generation) */
|
|
31
|
+
programmaticToolDefs?: LCTool[];
|
|
32
|
+
/** Tool registry for tool search (deferred tool definitions) */
|
|
33
|
+
toolRegistry?: LCToolRegistry;
|
|
28
34
|
};
|
|
29
35
|
export type ToolNodeConstructorParams = ToolRefs & ToolNodeOptions;
|
|
30
36
|
export type ToolEndEvent = {
|
|
@@ -59,3 +65,133 @@ export type ExecuteResult = {
|
|
|
59
65
|
stderr: string;
|
|
60
66
|
files?: FileRefs;
|
|
61
67
|
};
|
|
68
|
+
/** JSON Schema type definition for tool parameters */
|
|
69
|
+
export type JsonSchemaType = {
|
|
70
|
+
type: 'string' | 'number' | 'integer' | 'float' | 'boolean' | 'array' | 'object';
|
|
71
|
+
enum?: string[];
|
|
72
|
+
items?: JsonSchemaType;
|
|
73
|
+
properties?: Record<string, JsonSchemaType>;
|
|
74
|
+
required?: string[];
|
|
75
|
+
description?: string;
|
|
76
|
+
additionalProperties?: boolean | JsonSchemaType;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Specifies which contexts can invoke a tool (inspired by Anthropic's allowed_callers)
|
|
80
|
+
* - 'direct': Only callable directly by the LLM (default if omitted)
|
|
81
|
+
* - 'code_execution': Only callable from within programmatic code execution
|
|
82
|
+
*/
|
|
83
|
+
export type AllowedCaller = 'direct' | 'code_execution';
|
|
84
|
+
/** Tool definition with optional deferred loading and caller restrictions */
|
|
85
|
+
export type LCTool = {
|
|
86
|
+
name: string;
|
|
87
|
+
description?: string;
|
|
88
|
+
parameters?: JsonSchemaType;
|
|
89
|
+
/** When true, tool is not loaded into context initially (for tool search) */
|
|
90
|
+
defer_loading?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Which contexts can invoke this tool.
|
|
93
|
+
* Default: ['direct'] (only callable directly by LLM)
|
|
94
|
+
* Options: 'direct', 'code_execution'
|
|
95
|
+
*/
|
|
96
|
+
allowed_callers?: AllowedCaller[];
|
|
97
|
+
};
|
|
98
|
+
/** Map of tool names to tool definitions */
|
|
99
|
+
export type LCToolRegistry = Map<string, LCTool>;
|
|
100
|
+
/** Parameters for creating a Tool Search Regex tool */
|
|
101
|
+
export type ToolSearchRegexParams = {
|
|
102
|
+
apiKey?: string;
|
|
103
|
+
toolRegistry?: LCToolRegistry;
|
|
104
|
+
onlyDeferred?: boolean;
|
|
105
|
+
baseUrl?: string;
|
|
106
|
+
[key: string]: unknown;
|
|
107
|
+
};
|
|
108
|
+
/** Simplified tool metadata for search purposes */
|
|
109
|
+
export type ToolMetadata = {
|
|
110
|
+
name: string;
|
|
111
|
+
description: string;
|
|
112
|
+
parameters?: JsonSchemaType;
|
|
113
|
+
};
|
|
114
|
+
/** Individual search result for a matching tool */
|
|
115
|
+
export type ToolSearchResult = {
|
|
116
|
+
tool_name: string;
|
|
117
|
+
match_score: number;
|
|
118
|
+
matched_field: string;
|
|
119
|
+
snippet: string;
|
|
120
|
+
};
|
|
121
|
+
/** Response from the tool search operation */
|
|
122
|
+
export type ToolSearchResponse = {
|
|
123
|
+
tool_references: ToolSearchResult[];
|
|
124
|
+
total_tools_searched: number;
|
|
125
|
+
pattern_used: string;
|
|
126
|
+
};
|
|
127
|
+
/** Artifact returned alongside the formatted search results */
|
|
128
|
+
export type ToolSearchArtifact = {
|
|
129
|
+
tool_references: ToolSearchResult[];
|
|
130
|
+
metadata: {
|
|
131
|
+
total_searched: number;
|
|
132
|
+
pattern: string;
|
|
133
|
+
error?: string;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Tool call requested by the Code API during programmatic execution
|
|
138
|
+
*/
|
|
139
|
+
export type PTCToolCall = {
|
|
140
|
+
/** Unique ID like "call_001" */
|
|
141
|
+
id: string;
|
|
142
|
+
/** Tool name */
|
|
143
|
+
name: string;
|
|
144
|
+
/** Input parameters */
|
|
145
|
+
input: Record<string, any>;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Tool result sent back to the Code API
|
|
149
|
+
*/
|
|
150
|
+
export type PTCToolResult = {
|
|
151
|
+
/** Matches PTCToolCall.id */
|
|
152
|
+
call_id: string;
|
|
153
|
+
/** Tool execution result (any JSON-serializable value) */
|
|
154
|
+
result: any;
|
|
155
|
+
/** Whether tool execution failed */
|
|
156
|
+
is_error: boolean;
|
|
157
|
+
/** Error details if is_error=true */
|
|
158
|
+
error_message?: string;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Response from the Code API for programmatic execution
|
|
162
|
+
*/
|
|
163
|
+
export type ProgrammaticExecutionResponse = {
|
|
164
|
+
status: 'tool_call_required' | 'completed' | 'error' | unknown;
|
|
165
|
+
session_id?: string;
|
|
166
|
+
/** Present when status='tool_call_required' */
|
|
167
|
+
continuation_token?: string;
|
|
168
|
+
tool_calls?: PTCToolCall[];
|
|
169
|
+
/** Present when status='completed' */
|
|
170
|
+
stdout?: string;
|
|
171
|
+
stderr?: string;
|
|
172
|
+
files?: FileRefs;
|
|
173
|
+
/** Present when status='error' */
|
|
174
|
+
error?: string;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Artifact returned by the PTC tool
|
|
178
|
+
*/
|
|
179
|
+
export type ProgrammaticExecutionArtifact = {
|
|
180
|
+
session_id?: string;
|
|
181
|
+
files?: FileRefs;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Initialization parameters for the PTC tool
|
|
185
|
+
*/
|
|
186
|
+
export type ProgrammaticToolCallingParams = {
|
|
187
|
+
/** Code API key (or use CODE_API_KEY env var) */
|
|
188
|
+
apiKey?: string;
|
|
189
|
+
/** Code API base URL (or use CODE_BASEURL env var) */
|
|
190
|
+
baseUrl?: string;
|
|
191
|
+
/** Safety limit for round-trips (default: 20) */
|
|
192
|
+
maxRoundTrips?: number;
|
|
193
|
+
/** HTTP proxy URL */
|
|
194
|
+
proxy?: string;
|
|
195
|
+
/** Environment variable key for API key */
|
|
196
|
+
[key: string]: unknown;
|
|
197
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.40",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -53,6 +53,10 @@
|
|
|
53
53
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
54
54
|
"tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'openrouter' --name 'Jo' --location 'New York, NY'",
|
|
55
55
|
"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'",
|
|
56
|
+
"tool_search_regex": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tool_search_regex.ts",
|
|
57
|
+
"programmatic_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/programmatic_exec.ts",
|
|
58
|
+
"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'",
|
|
59
|
+
"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'",
|
|
56
60
|
"ant_web_search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/ant_web_search.ts --name 'Jo' --location 'New York, NY'",
|
|
57
61
|
"ant_web_search_edge_case": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/ant_web_search_edge_case.ts --name 'Jo' --location 'New York, NY'",
|
|
58
62
|
"ant_web_search_error_edge_case": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/ant_web_search_error_edge_case.ts --name 'Jo' --location 'New York, NY'",
|
|
@@ -13,6 +13,19 @@ import type * as t from '@/types';
|
|
|
13
13
|
import type { createPruneMessages } from '@/messages';
|
|
14
14
|
import { ContentTypes, Providers } from '@/common';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a tool allows the specified caller type.
|
|
18
|
+
* Default is 'direct' only if allowed_callers is not specified.
|
|
19
|
+
*/
|
|
20
|
+
function toolAllowsCaller(
|
|
21
|
+
toolDef: t.LCTool | undefined,
|
|
22
|
+
caller: t.AllowedCaller
|
|
23
|
+
): boolean {
|
|
24
|
+
if (!toolDef) return false;
|
|
25
|
+
const allowedCallers = toolDef.allowed_callers ?? ['direct'];
|
|
26
|
+
return allowedCallers.includes(caller);
|
|
27
|
+
}
|
|
28
|
+
|
|
16
29
|
/**
|
|
17
30
|
* Encapsulates agent-specific state that can vary between agents in a multi-agent system
|
|
18
31
|
*/
|
|
@@ -32,6 +45,7 @@ export class AgentContext {
|
|
|
32
45
|
tools,
|
|
33
46
|
toolMap,
|
|
34
47
|
toolEnd,
|
|
48
|
+
toolRegistry,
|
|
35
49
|
instructions,
|
|
36
50
|
additional_instructions,
|
|
37
51
|
streamBuffer,
|
|
@@ -48,6 +62,7 @@ export class AgentContext {
|
|
|
48
62
|
streamBuffer,
|
|
49
63
|
tools,
|
|
50
64
|
toolMap,
|
|
65
|
+
toolRegistry,
|
|
51
66
|
instructions,
|
|
52
67
|
additionalInstructions: additional_instructions,
|
|
53
68
|
reasoningKey,
|
|
@@ -102,6 +117,11 @@ export class AgentContext {
|
|
|
102
117
|
tools?: t.GraphTools;
|
|
103
118
|
/** Tool map for this agent */
|
|
104
119
|
toolMap?: t.ToolMap;
|
|
120
|
+
/**
|
|
121
|
+
* Tool definitions registry (includes deferred and programmatic tool metadata).
|
|
122
|
+
* Used for tool search and programmatic tool calling.
|
|
123
|
+
*/
|
|
124
|
+
toolRegistry?: t.LCToolRegistry;
|
|
105
125
|
/** Instructions for this agent */
|
|
106
126
|
instructions?: string;
|
|
107
127
|
/** Additional instructions for this agent */
|
|
@@ -137,6 +157,7 @@ export class AgentContext {
|
|
|
137
157
|
tokenCounter,
|
|
138
158
|
tools,
|
|
139
159
|
toolMap,
|
|
160
|
+
toolRegistry,
|
|
140
161
|
instructions,
|
|
141
162
|
additionalInstructions,
|
|
142
163
|
reasoningKey,
|
|
@@ -152,6 +173,7 @@ export class AgentContext {
|
|
|
152
173
|
tokenCounter?: t.TokenCounter;
|
|
153
174
|
tools?: t.GraphTools;
|
|
154
175
|
toolMap?: t.ToolMap;
|
|
176
|
+
toolRegistry?: t.LCToolRegistry;
|
|
155
177
|
instructions?: string;
|
|
156
178
|
additionalInstructions?: string;
|
|
157
179
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
@@ -167,6 +189,7 @@ export class AgentContext {
|
|
|
167
189
|
this.tokenCounter = tokenCounter;
|
|
168
190
|
this.tools = tools;
|
|
169
191
|
this.toolMap = toolMap;
|
|
192
|
+
this.toolRegistry = toolRegistry;
|
|
170
193
|
this.instructions = instructions;
|
|
171
194
|
this.additionalInstructions = additionalInstructions;
|
|
172
195
|
if (reasoningKey) {
|
|
@@ -320,4 +343,67 @@ export class AgentContext {
|
|
|
320
343
|
// Add tool tokens to existing instruction tokens (which may already include system message tokens)
|
|
321
344
|
this.instructionTokens += toolTokens;
|
|
322
345
|
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Gets a map of tools that allow programmatic (code_execution) calling.
|
|
349
|
+
* Filters toolMap based on toolRegistry's allowed_callers settings.
|
|
350
|
+
* @returns ToolMap containing only tools that allow code_execution
|
|
351
|
+
*/
|
|
352
|
+
getProgrammaticToolMap(): t.ToolMap {
|
|
353
|
+
const programmaticMap: t.ToolMap = new Map();
|
|
354
|
+
|
|
355
|
+
if (!this.toolMap) {
|
|
356
|
+
return programmaticMap;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
for (const [name, tool] of this.toolMap) {
|
|
360
|
+
const toolDef = this.toolRegistry?.get(name);
|
|
361
|
+
if (toolAllowsCaller(toolDef, 'code_execution')) {
|
|
362
|
+
programmaticMap.set(name, tool);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return programmaticMap;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Gets tool definitions for tools that allow programmatic calling.
|
|
371
|
+
* Used to send to the Code API for stub generation.
|
|
372
|
+
* @returns Array of LCTool definitions for programmatic tools
|
|
373
|
+
*/
|
|
374
|
+
getProgrammaticToolDefs(): t.LCTool[] {
|
|
375
|
+
if (!this.toolRegistry) {
|
|
376
|
+
return [];
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const defs: t.LCTool[] = [];
|
|
380
|
+
for (const [_name, toolDef] of this.toolRegistry) {
|
|
381
|
+
if (toolAllowsCaller(toolDef, 'code_execution')) {
|
|
382
|
+
defs.push(toolDef);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return defs;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Gets the tool registry for deferred tools (for tool search).
|
|
391
|
+
* @param onlyDeferred If true, only returns tools with defer_loading=true
|
|
392
|
+
* @returns LCToolRegistry with tool definitions
|
|
393
|
+
*/
|
|
394
|
+
getDeferredToolRegistry(onlyDeferred: boolean = true): t.LCToolRegistry {
|
|
395
|
+
const registry: t.LCToolRegistry = new Map();
|
|
396
|
+
|
|
397
|
+
if (!this.toolRegistry) {
|
|
398
|
+
return registry;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
for (const [name, toolDef] of this.toolRegistry) {
|
|
402
|
+
if (!onlyDeferred || toolDef.defer_loading === true) {
|
|
403
|
+
registry.set(name, toolDef);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return registry;
|
|
408
|
+
}
|
|
323
409
|
}
|
package/src/common/enum.ts
CHANGED
|
@@ -159,6 +159,8 @@ export enum Callback {
|
|
|
159
159
|
export enum Constants {
|
|
160
160
|
OFFICIAL_CODE_BASEURL = 'https://api.librechat.ai/v1',
|
|
161
161
|
EXECUTE_CODE = 'execute_code',
|
|
162
|
+
TOOL_SEARCH_REGEX = 'tool_search_regex',
|
|
163
|
+
PROGRAMMATIC_TOOL_CALLING = 'programmatic_code_execution',
|
|
162
164
|
WEB_SEARCH = 'web_search',
|
|
163
165
|
CONTENT_AND_ARTIFACT = 'content_and_artifact',
|
|
164
166
|
LC_TRANSFER_TO_ = 'lc_transfer_to_',
|
package/src/events.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { MultiAgentGraph, StandardGraph } from '@/graphs';
|
|
|
9
9
|
import type { Logger } from 'winston';
|
|
10
10
|
import type * as t from '@/types';
|
|
11
11
|
import { handleToolCalls } from '@/tools/handlers';
|
|
12
|
-
import { Providers } from '@/common';
|
|
12
|
+
import { Constants, Providers } from '@/common';
|
|
13
13
|
|
|
14
14
|
export class HandlerRegistry {
|
|
15
15
|
private handlers: Map<string, t.EventHandler> = new Map();
|
|
@@ -112,6 +112,10 @@ export class ToolEndHandler implements t.EventHandler {
|
|
|
112
112
|
return;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
if (metadata[Constants.PROGRAMMATIC_TOOL_CALLING] === true) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
115
119
|
if (this.callback) {
|
|
116
120
|
await this.callback(toolEndData, metadata);
|
|
117
121
|
}
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -443,9 +443,11 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
443
443
|
initializeTools({
|
|
444
444
|
currentTools,
|
|
445
445
|
currentToolMap,
|
|
446
|
+
agentContext,
|
|
446
447
|
}: {
|
|
447
448
|
currentTools?: t.GraphTools;
|
|
448
449
|
currentToolMap?: t.ToolMap;
|
|
450
|
+
agentContext?: AgentContext;
|
|
449
451
|
}): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {
|
|
450
452
|
return new CustomToolNode<t.BaseGraphState>({
|
|
451
453
|
tools: (currentTools as t.GenericTool[] | undefined) ?? [],
|
|
@@ -453,6 +455,9 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
453
455
|
toolCallStepIds: this.toolCallStepIds,
|
|
454
456
|
errorHandler: (data, metadata) =>
|
|
455
457
|
StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
458
|
+
toolRegistry: agentContext?.toolRegistry,
|
|
459
|
+
programmaticToolMap: agentContext?.getProgrammaticToolMap(),
|
|
460
|
+
programmaticToolDefs: agentContext?.getProgrammaticToolDefs(),
|
|
456
461
|
});
|
|
457
462
|
}
|
|
458
463
|
|
|
@@ -705,7 +710,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
705
710
|
formatAnthropicArtifactContent(finalMessages);
|
|
706
711
|
} else if (
|
|
707
712
|
isLatestToolMessage &&
|
|
708
|
-
(isOpenAILike(agentContext.provider)
|
|
713
|
+
((isOpenAILike(agentContext.provider) &&
|
|
714
|
+
agentContext.provider !== Providers.DEEPSEEK) ||
|
|
709
715
|
isGoogleLike(agentContext.provider))
|
|
710
716
|
) {
|
|
711
717
|
formatArtifactPayload(finalMessages);
|
|
@@ -878,6 +884,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
878
884
|
this.initializeTools({
|
|
879
885
|
currentTools: agentContext.tools,
|
|
880
886
|
currentToolMap: agentContext.toolMap,
|
|
887
|
+
agentContext,
|
|
881
888
|
})
|
|
882
889
|
)
|
|
883
890
|
.addEdge(START, agentNode)
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,8 @@ export * from './graphs';
|
|
|
11
11
|
/* Tools */
|
|
12
12
|
export * from './tools/Calculator';
|
|
13
13
|
export * from './tools/CodeExecutor';
|
|
14
|
+
export * from './tools/ProgrammaticToolCalling';
|
|
15
|
+
export * from './tools/ToolSearchRegex';
|
|
14
16
|
export * from './tools/handlers';
|
|
15
17
|
export * from './tools/search';
|
|
16
18
|
|
package/src/instrumentation.ts
CHANGED
|
@@ -11,7 +11,7 @@ if (
|
|
|
11
11
|
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
12
12
|
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
13
13
|
baseUrl: process.env.LANGFUSE_BASE_URL,
|
|
14
|
-
environment: process.env.NODE_ENV ?? 'development',
|
|
14
|
+
environment: process.env.LANGFUSE_TRACING_ENVIRONMENT ?? process.env.NODE_ENV ?? 'development',
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
const sdk = new NodeSDK({
|