Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@librechat/agents",
"version": "3.2.57",
"version": "3.2.59",
"main": "./dist/cjs/main.cjs",
"module": "./dist/esm/main.mjs",
"types": "./dist/types/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/agents/AgentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class AgentContext {
toolSchemaTokens,
subagentConfigs,
maxSubagentDepth,
graphTools,
} = agentConfig;

const agentContext = new AgentContext({
Expand Down Expand Up @@ -116,6 +117,14 @@ export class AgentContext {
agentContext._sourceInputs = agentConfig;
agentContext.subagentConfigs = subagentConfigs;
agentContext.maxSubagentDepth = maxSubagentDepth;
/**
* Host-supplied direct tools (see `AgentInputs.graphTools`). Copied — never
* aliased — because the SDK later pushes graph-managed tools (handoff /
* subagent) into this same array and must not mutate the host's input.
*/
if (graphTools && graphTools.length > 0) {
agentContext.graphTools = [...graphTools];
}

if (initialSummary?.text != null && initialSummary.text !== '') {
agentContext.setInitialSummary(
Expand Down
40 changes: 40 additions & 0 deletions src/agents/__tests__/AgentContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ describe('AgentContext', () => {
schema: { type: 'object', properties: {} },
}) as unknown as t.GenericTool;

describe('fromConfig — host-supplied graphTools', () => {
it('copies graphTools onto the context without aliasing the host array', () => {
const askTool = createMockTool('ask_user_question');
const hostGraphTools = [askTool];
const ctx = createBasicContext({
agentConfig: { graphTools: hostGraphTools },
});

expect(ctx.graphTools).toEqual([askTool]);
expect(ctx.graphTools).not.toBe(hostGraphTools);

/** The SDK pushes graph-managed tools (handoff/subagent) into this
* array later — that must never mutate the host's input. */
(ctx.graphTools as t.GenericTool[]).push(createMockTool('subagent'));
expect(hostGraphTools).toHaveLength(1);
});

it('leaves graphTools undefined when the host supplies none (or an empty list)', () => {
expect(createBasicContext().graphTools).toBeUndefined();
expect(
createBasicContext({ agentConfig: { graphTools: [] } }).graphTools
).toBeUndefined();
});

it('binds host graphTools to the model in event-driven mode', () => {
const askTool = createMockTool('ask_user_question');
const ctx = createBasicContext({
agentConfig: {
graphTools: [askTool],
toolDefinitions: [{ name: 'echo', description: 'host event tool' }],
},
});

const bound = ctx.getToolsForBinding() ?? [];
const names = (bound as Array<{ name?: string }>).map((t) => t.name);
expect(names).toContain('ask_user_question');
expect(names).toContain('echo');
});
});

describe('System Runnable - Lazy Creation', () => {
it('creates system runnable on first access', () => {
const ctx = createBasicContext({
Expand Down
27 changes: 25 additions & 2 deletions src/graphs/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,15 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
getToolCount(): number {
const context = this.agentContexts.get(this.defaultAgentId);
return (
(context?.tools?.length ?? 0) + (context?.toolDefinitions?.length ?? 0)
(context?.tools?.length ?? 0) +
(context?.toolDefinitions?.length ?? 0) +
/**
* Graph-managed + host-supplied direct tools (handoff, subagent,
* `AgentInputs.graphTools`) are bound to the model and token-accounted,
* so a count that omits them under-reports the run's tool surface
* (Codex #289 P3).
*/
(context?.graphTools?.length ?? 0)
);
}

Expand Down Expand Up @@ -1286,10 +1294,25 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
graphTools && graphTools.length > 0
? [...baseTools, ...graphTools]
: baseTools;
/**
* ToolNode treats a supplied `toolMap` as authoritative (it only derives
* one from `tools` when the param is undefined), so when graphTools force
* us to build a merged map here, an absent `currentToolMap` must be
* seeded from the BASE tools first — otherwise ordinary tools stay bound
* to the model but vanish from the execution map and every call to them
* fails as an unknown tool (Codex #289 round 2).
*/
const traditionalToolMap =
graphTools && graphTools.length > 0
? new Map([
...(currentToolMap ?? new Map()),
...(currentToolMap ??
new Map(
baseTools
.filter(
(t): t is t.GenericTool & { name: string } => 'name' in t
)
.map((t) => [t.name, t] as [string, t.GenericTool])
)),
...graphTools
.filter((t): t is t.GenericTool & { name: string } => 'name' in t)
.map((t) => [t.name, t] as [string, t.GenericTool]),
Expand Down
53 changes: 53 additions & 0 deletions src/graphs/__tests__/composition.smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,59 @@ const expectCompiledWorkflow = (
};

describe('LangGraph composition smoke tests', () => {
it('getToolCount includes direct graph tools (host-supplied graphTools) alongside instances and definitions', () => {
const askLikeTool = { name: 'ask_user_question' } as unknown as NonNullable<
t.AgentInputs['graphTools']
>[number];
const graph = new StandardGraph({
runId: 'toolcount-smoke',
agents: [
{
...makeAgent('agent'),
toolDefinitions: [
{ name: 'evt1', description: 'event tool one' },
{ name: 'evt2', description: 'event tool two' },
],
graphTools: [askLikeTool],
},
],
});
// 2 schema-only event tools + 1 in-process direct tool — all bound to the
// model and token-accounted, so all must be counted (Codex #289 P3).
expect(graph.getToolCount()).toBe(3);
});

it('keeps ordinary tools executable when graphTools are added without a host toolMap (traditional mode)', () => {
type HostTool = NonNullable<t.AgentInputs['graphTools']>[number];
const echoTool = { name: 'echo_tool' } as unknown as HostTool;
const askLikeTool = { name: 'ask_user_question' } as unknown as HostTool;
const graph = new StandardGraph({
runId: 'toolmap-merge-smoke',
agents: [
{
...makeAgent('agent'),
tools: [echoTool],
graphTools: [askLikeTool],
},
],
});
const agentContext = graph.agentContexts.get('agent');
const node = graph.initializeTools({
currentTools: agentContext?.tools,
currentToolMap: undefined,
agentContext,
});
/**
* ToolNode treats a provided toolMap as authoritative — if the merged map
* built for graphTools drops the base tools, they stay bound to the model
* but every call fails as an unknown tool (Codex #289 round 2).
*/
const toolMap = (node as unknown as { toolMap: Map<string, unknown> })
.toolMap;
expect(toolMap.has('echo_tool')).toBe(true);
expect(toolMap.has('ask_user_question')).toBe(true);
});

it('clears run-scoped eager tool state on reset', () => {
const graph = new StandardGraph({
runId: 'standard-eager-reset',
Expand Down
11 changes: 11 additions & 0 deletions src/langfuseToolOutputTracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
resolveLangfuseConfig,
resolveToolOutputTracingConfig,
} from '@/langfuseConfig';
import {
shapeLangfuseSpan,
shouldDropLangfuseSpan,
} from '@/langfuseTraceShaping';
import { resolveToolOutputTracingConfigForSpan } from '@/langfuseRuntimeScope';

export { LANGFUSE_TOOL_OUTPUT_REDACTION_TEXT, resolveLangfuseConfig };
Expand Down Expand Up @@ -407,6 +411,9 @@ class ToolOutputRedactingLangfuseSpanProcessor implements SpanProcessor {
}

onStart(span: Span, parentContext: Context): void {
if (shouldDropLangfuseSpan(span.name)) {
return;
}
const config =
resolveToolOutputTracingConfigForSpan(parentContext) ??
this.fallbackConfig;
Expand All @@ -417,7 +424,11 @@ class ToolOutputRedactingLangfuseSpanProcessor implements SpanProcessor {
}

onEnd(span: ReadableSpan): void {
if (shouldDropLangfuseSpan(span.name)) {
return;
}
classifyLangfuseToolNodeSpan(span);
shapeLangfuseSpan(span);
const config = this.spanConfigs.get(span) ?? this.fallbackConfig;
if (config != null) {
redactLangfuseSpanToolOutputs(span, config);
Expand Down
Loading
Loading