@librechat/agents 3.2.32 → 3.2.34
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/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/agents/AgentContext.cjs +844 -1046
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +13 -13
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +233 -240
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/common/index.cjs +2 -0
- package/dist/cjs/events.cjs +121 -169
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +1389 -1807
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +713 -945
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/index.cjs +2 -0
- package/dist/cjs/hitl/askUserQuestion.cjs +60 -62
- package/dist/cjs/hitl/askUserQuestion.cjs.map +1 -1
- package/dist/cjs/hitl/index.cjs +1 -0
- package/dist/cjs/hooks/HookRegistry.cjs +176 -202
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/createToolPolicyHook.cjs +71 -101
- package/dist/cjs/hooks/createToolPolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +170 -273
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +227 -282
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +6 -0
- package/dist/cjs/hooks/matchers.cjs +196 -230
- package/dist/cjs/hooks/matchers.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +24 -24
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/instrumentation.cjs +110 -137
- package/dist/cjs/instrumentation.cjs.map +1 -1
- package/dist/cjs/langchain/google-common.cjs +0 -3
- package/dist/cjs/langchain/index.cjs +80 -43
- package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
- package/dist/cjs/langchain/messages/tool.cjs +0 -3
- package/dist/cjs/langchain/messages.cjs +35 -18
- package/dist/cjs/langchain/openai.cjs +0 -3
- package/dist/cjs/langchain/prompts.cjs +5 -8
- package/dist/cjs/langchain/runnables.cjs +11 -10
- package/dist/cjs/langchain/tools.cjs +14 -11
- package/dist/cjs/langchain/utils/env.cjs +5 -8
- package/dist/cjs/langfuse.cjs +60 -79
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs +267 -399
- package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +432 -562
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs +23 -47
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +441 -731
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +171 -256
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/output_parsers.cjs +2 -0
- package/dist/cjs/llm/anthropic/utils/tools.cjs +12 -26
- package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +214 -240
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/toolCache.cjs +84 -106
- package/dist/cjs/llm/bedrock/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/index.cjs +2 -0
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +357 -620
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +141 -149
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/fake.cjs +86 -96
- package/dist/cjs/llm/fake.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +183 -237
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +404 -674
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/zod_to_genai_parameters.cjs +2 -0
- package/dist/cjs/llm/init.cjs +44 -53
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +142 -182
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1035 -1273
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +189 -316
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +102 -153
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/toolCache.cjs +35 -44
- package/dist/cjs/llm/openrouter/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +29 -37
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/request.cjs +20 -33
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +446 -453
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +547 -528
- package/dist/cjs/messages/anthropicToolCache.cjs +68 -119
- package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +305 -418
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/content.cjs +36 -49
- package/dist/cjs/messages/content.cjs.map +1 -1
- package/dist/cjs/messages/contextPruning.cjs +112 -145
- package/dist/cjs/messages/contextPruning.cjs.map +1 -1
- package/dist/cjs/messages/contextPruningSettings.cjs +36 -46
- package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +256 -397
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +904 -1387
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/ids.cjs +16 -20
- package/dist/cjs/messages/ids.cjs.map +1 -1
- package/dist/cjs/messages/index.cjs +12 -0
- package/dist/cjs/messages/langchain.cjs +18 -18
- package/dist/cjs/messages/langchain.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +1054 -1517
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/messages/recency.cjs +77 -95
- package/dist/cjs/messages/recency.cjs.map +1 -1
- package/dist/cjs/messages/reducer.cjs +63 -78
- package/dist/cjs/messages/reducer.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +51 -79
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/openai/index.cjs +171 -217
- package/dist/cjs/openai/index.cjs.map +1 -1
- package/dist/cjs/responses/index.cjs +302 -391
- package/dist/cjs/responses/index.cjs.map +1 -1
- package/dist/cjs/run.cjs +903 -1113
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +805 -986
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs +327 -410
- package/dist/cjs/session/JsonlSessionStore.cjs.map +1 -1
- package/dist/cjs/session/handlers.cjs +192 -208
- package/dist/cjs/session/handlers.cjs.map +1 -1
- package/dist/cjs/session/ids.cjs +9 -10
- package/dist/cjs/session/ids.cjs.map +1 -1
- package/dist/cjs/session/index.cjs +4 -0
- package/dist/cjs/session/messageSerialization.cjs +94 -156
- package/dist/cjs/session/messageSerialization.cjs.map +1 -1
- package/dist/cjs/splitStream.cjs +147 -206
- package/dist/cjs/splitStream.cjs.map +1 -1
- package/dist/cjs/stream.cjs +874 -1344
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/index.cjs +57 -101
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +643 -796
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +110 -136
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +165 -245
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs +36 -57
- package/dist/cjs/tools/Calculator.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +126 -168
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/CodeSessionFileSummary.cjs +36 -46
- package/dist/cjs/tools/CodeSessionFileSummary.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +459 -649
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs +17 -20
- package/dist/cjs/tools/ReadFile.cjs.map +1 -1
- package/dist/cjs/tools/SkillTool.cjs +26 -27
- package/dist/cjs/tools/SkillTool.cjs.map +1 -1
- package/dist/cjs/tools/SubagentTool.cjs +59 -61
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +2146 -2686
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +663 -825
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs +248 -340
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs +170 -197
- package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs +425 -520
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs +91 -124
- package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/index.cjs +4 -0
- package/dist/cjs/tools/eagerEventExecution.cjs +75 -99
- package/dist/cjs/tools/eagerEventExecution.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +200 -262
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/local/CompileCheckTool.cjs +150 -212
- package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -1
- package/dist/cjs/tools/local/FileCheckpointer.cjs +77 -85
- package/dist/cjs/tools/local/FileCheckpointer.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +763 -1022
- package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +666 -941
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionTools.cjs +49 -92
- package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +286 -354
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/local/attachments.cjs +108 -165
- package/dist/cjs/tools/local/attachments.cjs.map +1 -1
- package/dist/cjs/tools/local/bashAst.cjs +99 -113
- package/dist/cjs/tools/local/bashAst.cjs.map +1 -1
- package/dist/cjs/tools/local/editStrategies.cjs +126 -169
- package/dist/cjs/tools/local/editStrategies.cjs.map +1 -1
- package/dist/cjs/tools/local/index.cjs +12 -0
- package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs +136 -218
- package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/syntaxCheck.cjs +142 -161
- package/dist/cjs/tools/local/syntaxCheck.cjs.map +1 -1
- package/dist/cjs/tools/local/textEncoding.cjs +25 -23
- package/dist/cjs/tools/local/textEncoding.cjs.map +1 -1
- package/dist/cjs/tools/local/workspaceFS.cjs +38 -46
- package/dist/cjs/tools/local/workspaceFS.cjs.map +1 -1
- package/dist/cjs/tools/ptcTimeout.cjs +27 -47
- package/dist/cjs/tools/ptcTimeout.cjs.map +1 -1
- package/dist/cjs/tools/schema.cjs +24 -23
- package/dist/cjs/tools/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/anthropic.cjs +24 -33
- package/dist/cjs/tools/search/anthropic.cjs.map +1 -1
- package/dist/cjs/tools/search/content.cjs +95 -137
- package/dist/cjs/tools/search/content.cjs.map +1 -1
- package/dist/cjs/tools/search/firecrawl.cjs +141 -172
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +128 -196
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/highlights.cjs +165 -232
- package/dist/cjs/tools/search/highlights.cjs.map +1 -1
- package/dist/cjs/tools/search/index.cjs +2 -0
- package/dist/cjs/tools/search/rerankers.cjs +151 -174
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +40 -39
- package/dist/cjs/tools/search/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +428 -530
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/serper-scraper.cjs +106 -127
- package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs +129 -181
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-search.cjs +295 -359
- package/dist/cjs/tools/search/tavily-search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +260 -299
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +74 -117
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +54 -72
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -1
- package/dist/cjs/tools/streamedToolCallSeals.cjs +46 -34
- package/dist/cjs/tools/streamedToolCallSeals.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +612 -771
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/index.cjs +1 -0
- package/dist/cjs/tools/toolOutputReferences.cjs +523 -630
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/cjs/utils/callbacks.cjs +11 -21
- package/dist/cjs/utils/callbacks.cjs.map +1 -1
- package/dist/cjs/utils/errors.cjs +70 -95
- package/dist/cjs/utils/errors.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +32 -42
- package/dist/cjs/utils/events.cjs.map +1 -1
- package/dist/cjs/utils/graph.cjs +8 -12
- package/dist/cjs/utils/graph.cjs.map +1 -1
- package/dist/cjs/utils/handlers.cjs +60 -82
- package/dist/cjs/utils/handlers.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +9 -0
- package/dist/cjs/utils/llm.cjs +19 -27
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/cjs/utils/misc.cjs +30 -46
- package/dist/cjs/utils/misc.cjs.map +1 -1
- package/dist/cjs/utils/run.cjs +50 -66
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/schema.cjs +11 -19
- package/dist/cjs/utils/schema.cjs.map +1 -1
- package/dist/cjs/utils/title.cjs +71 -106
- package/dist/cjs/utils/title.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +186 -283
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/cjs/utils/truncation.cjs +95 -114
- package/dist/cjs/utils/truncation.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +844 -1044
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +13 -11
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +221 -238
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/common/index.mjs +3 -0
- package/dist/esm/events.mjs +121 -167
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +1388 -1804
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +713 -943
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/index.mjs +3 -0
- package/dist/esm/hitl/askUserQuestion.mjs +60 -60
- package/dist/esm/hitl/askUserQuestion.mjs.map +1 -1
- package/dist/esm/hitl/index.mjs +2 -0
- package/dist/esm/hooks/HookRegistry.mjs +176 -200
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/createToolPolicyHook.mjs +71 -99
- package/dist/esm/hooks/createToolPolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs +170 -271
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +227 -280
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +7 -0
- package/dist/esm/hooks/matchers.mjs +196 -228
- package/dist/esm/hooks/matchers.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +24 -22
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/instrumentation.mjs +109 -132
- package/dist/esm/instrumentation.mjs.map +1 -1
- package/dist/esm/langchain/google-common.mjs +1 -2
- package/dist/esm/langchain/index.mjs +5 -5
- package/dist/esm/langchain/language_models/chat_models.mjs +1 -2
- package/dist/esm/langchain/messages/tool.mjs +1 -2
- package/dist/esm/langchain/messages.mjs +2 -2
- package/dist/esm/langchain/openai.mjs +1 -2
- package/dist/esm/langchain/prompts.mjs +2 -2
- package/dist/esm/langchain/runnables.mjs +2 -2
- package/dist/esm/langchain/tools.mjs +2 -2
- package/dist/esm/langchain/utils/env.mjs +2 -2
- package/dist/esm/langfuse.mjs +60 -76
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/langfuseToolOutputTracing.mjs +267 -395
- package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +432 -559
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs +23 -45
- package/dist/esm/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +439 -725
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +171 -253
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/output_parsers.mjs +3 -0
- package/dist/esm/llm/anthropic/utils/tools.mjs +12 -24
- package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +214 -238
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/bedrock/toolCache.mjs +84 -104
- package/dist/esm/llm/bedrock/toolCache.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/index.mjs +3 -0
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs +357 -618
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs +140 -147
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/fake.mjs +86 -94
- package/dist/esm/llm/fake.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +183 -235
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +403 -666
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/llm/google/utils/zod_to_genai_parameters.mjs +3 -0
- package/dist/esm/llm/init.mjs +44 -51
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +142 -180
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1035 -1268
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +188 -312
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +102 -151
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/toolCache.mjs +35 -42
- package/dist/esm/llm/openrouter/toolCache.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +29 -34
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/request.mjs +20 -31
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +446 -449
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +99 -87
- package/dist/esm/messages/anthropicToolCache.mjs +68 -117
- package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +305 -416
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/content.mjs +36 -47
- package/dist/esm/messages/content.mjs.map +1 -1
- package/dist/esm/messages/contextPruning.mjs +112 -143
- package/dist/esm/messages/contextPruning.mjs.map +1 -1
- package/dist/esm/messages/contextPruningSettings.mjs +36 -44
- package/dist/esm/messages/contextPruningSettings.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +254 -393
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +902 -1383
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/ids.mjs +16 -18
- package/dist/esm/messages/ids.mjs.map +1 -1
- package/dist/esm/messages/index.mjs +13 -0
- package/dist/esm/messages/langchain.mjs +18 -16
- package/dist/esm/messages/langchain.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +1053 -1514
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/messages/recency.mjs +77 -93
- package/dist/esm/messages/recency.mjs.map +1 -1
- package/dist/esm/messages/reducer.mjs +63 -76
- package/dist/esm/messages/reducer.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +49 -75
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/openai/index.mjs +170 -215
- package/dist/esm/openai/index.mjs.map +1 -1
- package/dist/esm/responses/index.mjs +301 -389
- package/dist/esm/responses/index.mjs.map +1 -1
- package/dist/esm/run.mjs +903 -1111
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +806 -985
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs +326 -407
- package/dist/esm/session/JsonlSessionStore.mjs.map +1 -1
- package/dist/esm/session/handlers.mjs +192 -206
- package/dist/esm/session/handlers.mjs.map +1 -1
- package/dist/esm/session/ids.mjs +9 -8
- package/dist/esm/session/ids.mjs.map +1 -1
- package/dist/esm/session/index.mjs +5 -0
- package/dist/esm/session/messageSerialization.mjs +94 -154
- package/dist/esm/session/messageSerialization.mjs.map +1 -1
- package/dist/esm/splitStream.mjs +147 -204
- package/dist/esm/splitStream.mjs.map +1 -1
- package/dist/esm/stream.mjs +872 -1341
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/index.mjs +57 -99
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +640 -790
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +103 -129
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +162 -239
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs +34 -36
- package/dist/esm/tools/Calculator.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +123 -164
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/CodeSessionFileSummary.mjs +36 -44
- package/dist/esm/tools/CodeSessionFileSummary.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +454 -644
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs +17 -18
- package/dist/esm/tools/ReadFile.mjs.map +1 -1
- package/dist/esm/tools/SkillTool.mjs +26 -25
- package/dist/esm/tools/SkillTool.mjs.map +1 -1
- package/dist/esm/tools/SubagentTool.mjs +59 -59
- package/dist/esm/tools/SubagentTool.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +2144 -2684
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +659 -804
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs +248 -338
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs +170 -195
- package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs +424 -517
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs +91 -122
- package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/index.mjs +5 -0
- package/dist/esm/tools/eagerEventExecution.mjs +75 -96
- package/dist/esm/tools/eagerEventExecution.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +200 -260
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/local/CompileCheckTool.mjs +150 -210
- package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -1
- package/dist/esm/tools/local/FileCheckpointer.mjs +77 -83
- package/dist/esm/tools/local/FileCheckpointer.mjs.map +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +760 -1017
- package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +663 -936
- package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionTools.mjs +49 -90
- package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +283 -349
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/local/attachments.mjs +108 -163
- package/dist/esm/tools/local/attachments.mjs.map +1 -1
- package/dist/esm/tools/local/bashAst.mjs +99 -111
- package/dist/esm/tools/local/bashAst.mjs.map +1 -1
- package/dist/esm/tools/local/editStrategies.mjs +126 -167
- package/dist/esm/tools/local/editStrategies.mjs.map +1 -1
- package/dist/esm/tools/local/index.mjs +13 -0
- package/dist/esm/tools/local/resolveLocalExecutionTools.mjs +136 -216
- package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/syntaxCheck.mjs +138 -155
- package/dist/esm/tools/local/syntaxCheck.mjs.map +1 -1
- package/dist/esm/tools/local/textEncoding.mjs +25 -21
- package/dist/esm/tools/local/textEncoding.mjs.map +1 -1
- package/dist/esm/tools/local/workspaceFS.mjs +38 -44
- package/dist/esm/tools/local/workspaceFS.mjs.map +1 -1
- package/dist/esm/tools/ptcTimeout.mjs +27 -42
- package/dist/esm/tools/ptcTimeout.mjs.map +1 -1
- package/dist/esm/tools/schema.mjs +24 -21
- package/dist/esm/tools/schema.mjs.map +1 -1
- package/dist/esm/tools/search/anthropic.mjs +24 -31
- package/dist/esm/tools/search/anthropic.mjs.map +1 -1
- package/dist/esm/tools/search/content.mjs +93 -116
- package/dist/esm/tools/search/content.mjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +139 -169
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +128 -194
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/highlights.mjs +165 -230
- package/dist/esm/tools/search/highlights.mjs.map +1 -1
- package/dist/esm/tools/search/index.mjs +3 -0
- package/dist/esm/tools/search/rerankers.mjs +149 -168
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +39 -37
- package/dist/esm/tools/search/schema.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +426 -528
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/serper-scraper.mjs +104 -124
- package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-scraper.mjs +127 -178
- package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-search.mjs +293 -357
- package/dist/esm/tools/search/tavily-search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +259 -297
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +74 -115
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +54 -70
- package/dist/esm/tools/skillCatalog.mjs.map +1 -1
- package/dist/esm/tools/streamedToolCallSeals.mjs +42 -31
- package/dist/esm/tools/streamedToolCallSeals.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +612 -768
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/index.mjs +2 -0
- package/dist/esm/tools/toolOutputReferences.mjs +523 -624
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/esm/utils/callbacks.mjs +11 -19
- package/dist/esm/utils/callbacks.mjs.map +1 -1
- package/dist/esm/utils/errors.mjs +70 -93
- package/dist/esm/utils/errors.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +32 -40
- package/dist/esm/utils/events.mjs.map +1 -1
- package/dist/esm/utils/graph.mjs +8 -10
- package/dist/esm/utils/graph.mjs.map +1 -1
- package/dist/esm/utils/handlers.mjs +60 -80
- package/dist/esm/utils/handlers.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +10 -0
- package/dist/esm/utils/llm.mjs +19 -25
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/esm/utils/misc.mjs +30 -44
- package/dist/esm/utils/misc.mjs.map +1 -1
- package/dist/esm/utils/run.mjs +50 -64
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/schema.mjs +11 -17
- package/dist/esm/utils/schema.mjs.map +1 -1
- package/dist/esm/utils/title.mjs +71 -104
- package/dist/esm/utils/title.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +186 -281
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/esm/utils/truncation.mjs +95 -112
- package/dist/esm/utils/truncation.mjs.map +1 -1
- package/dist/types/llm/bedrock/utils/index.d.ts +1 -1
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +9 -0
- package/dist/types/llm/vertexai/index.d.ts +10 -0
- package/dist/types/tools/ToolNode.d.ts +8 -0
- package/dist/types/tools/search/tool.d.ts +17 -0
- package/dist/types/tools/search/types.d.ts +4 -0
- package/dist/types/tools/streamedToolCallSeals.d.ts +5 -1
- package/dist/types/types/tools.d.ts +10 -0
- package/package.json +4 -10
- package/src/__tests__/stream.eagerEventExecution.test.ts +703 -0
- package/src/llm/bedrock/index.ts +40 -0
- package/src/llm/bedrock/streamSealDispatch.test.ts +158 -0
- package/src/llm/bedrock/utils/index.ts +1 -0
- package/src/llm/bedrock/utils/message_outputs.test.ts +85 -0
- package/src/llm/bedrock/utils/message_outputs.ts +43 -0
- package/src/llm/google/utils/common.test.ts +64 -0
- package/src/llm/google/utils/common.ts +18 -0
- package/src/llm/openai/index.ts +95 -1
- package/src/llm/openai/sequentialToolCallSeals.test.ts +199 -0
- package/src/llm/vertexai/index.ts +31 -0
- package/src/llm/vertexai/sealStreamedToolCalls.test.ts +88 -0
- package/src/llm/vertexai/streamSealDispatch.test.ts +148 -0
- package/src/stream.ts +40 -6
- package/src/tools/ToolNode.ts +85 -3
- package/src/tools/__tests__/ToolNode.onResultCompletion.test.ts +368 -0
- package/src/tools/search/highlights.ts +9 -1
- package/src/tools/search/search.ts +41 -3
- package/src/tools/search/source-processing.test.ts +373 -0
- package/src/tools/search/tool.ts +22 -2
- package/src/tools/search/types.ts +4 -0
- package/src/tools/streamedToolCallSeals.ts +37 -9
- package/src/types/tools.ts +10 -0
- package/dist/cjs/langchain/google-common.cjs.map +0 -1
- package/dist/cjs/langchain/index.cjs.map +0 -1
- package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
- package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
- package/dist/cjs/langchain/messages.cjs.map +0 -1
- package/dist/cjs/langchain/openai.cjs.map +0 -1
- package/dist/cjs/langchain/prompts.cjs.map +0 -1
- package/dist/cjs/langchain/runnables.cjs.map +0 -1
- package/dist/cjs/langchain/tools.cjs.map +0 -1
- package/dist/cjs/langchain/utils/env.cjs.map +0 -1
- package/dist/cjs/main.cjs.map +0 -1
- package/dist/esm/langchain/google-common.mjs.map +0 -1
- package/dist/esm/langchain/index.mjs.map +0 -1
- package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
- package/dist/esm/langchain/messages/tool.mjs.map +0 -1
- package/dist/esm/langchain/messages.mjs.map +0 -1
- package/dist/esm/langchain/openai.mjs.map +0 -1
- package/dist/esm/langchain/prompts.mjs.map +0 -1
- package/dist/esm/langchain/runnables.mjs.map +0 -1
- package/dist/esm/langchain/tools.mjs.map +0 -1
- package/dist/esm/langchain/utils/env.mjs.map +0 -1
- package/dist/esm/main.mjs.map +0 -1
|
@@ -1,1839 +1,1421 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
require(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
require(
|
|
24
|
-
require(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
require(
|
|
34
|
-
require(
|
|
35
|
-
require(
|
|
36
|
-
require(
|
|
37
|
-
require(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
require(
|
|
44
|
-
require(
|
|
45
|
-
require(
|
|
46
|
-
require(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
require('../tools/ProgrammaticToolCalling.cjs');
|
|
50
|
-
require('../tools/BashProgrammaticToolCalling.cjs');
|
|
51
|
-
require('../hooks/createWorkspacePolicyHook.cjs');
|
|
52
|
-
var resolveLocalExecutionTools = require('../tools/local/resolveLocalExecutionTools.cjs');
|
|
53
|
-
var node = require('../summarization/node.cjs');
|
|
54
|
-
var schema = require('../tools/schema.cjs');
|
|
55
|
-
var AgentContext = require('../agents/AgentContext.cjs');
|
|
56
|
-
var fake = require('../llm/fake.cjs');
|
|
57
|
-
var handlers = require('../tools/handlers.cjs');
|
|
58
|
-
var request = require('../llm/request.cjs');
|
|
59
|
-
var init = require('../llm/init.cjs');
|
|
60
|
-
|
|
61
|
-
/* eslint-disable no-console */
|
|
62
|
-
const { AGENT, TOOLS, SUMMARIZE } = _enum.GraphNodeKeys;
|
|
1
|
+
const require_langfuse = require("../langfuse.cjs");
|
|
2
|
+
const require_langfuseToolOutputTracing = require("../langfuseToolOutputTracing.cjs");
|
|
3
|
+
const require_callbacks = require("../utils/callbacks.cjs");
|
|
4
|
+
const require_enum = require("../common/enum.cjs");
|
|
5
|
+
require("../common/index.cjs");
|
|
6
|
+
const require_instrumentation = require("../instrumentation.cjs");
|
|
7
|
+
const require_core = require("../messages/core.cjs");
|
|
8
|
+
const require_ids = require("../messages/ids.cjs");
|
|
9
|
+
const require_prune = require("../messages/prune.cjs");
|
|
10
|
+
const require_events = require("../utils/events.cjs");
|
|
11
|
+
const require_format = require("../messages/format.cjs");
|
|
12
|
+
const require_cache = require("../messages/cache.cjs");
|
|
13
|
+
const require_anthropicToolCache = require("../messages/anthropicToolCache.cjs");
|
|
14
|
+
const require_content = require("../messages/content.cjs");
|
|
15
|
+
const require_tools = require("../messages/tools.cjs");
|
|
16
|
+
const require_reducer = require("../messages/reducer.cjs");
|
|
17
|
+
require("../messages/index.cjs");
|
|
18
|
+
const require_graph = require("../utils/graph.cjs");
|
|
19
|
+
const require_llm = require("../utils/llm.cjs");
|
|
20
|
+
const require_handlers = require("../tools/handlers.cjs");
|
|
21
|
+
const require_toolOutputReferences = require("../tools/toolOutputReferences.cjs");
|
|
22
|
+
const require_run = require("../utils/run.cjs");
|
|
23
|
+
require("../utils/index.cjs");
|
|
24
|
+
const require_toolCache = require("../llm/openrouter/toolCache.cjs");
|
|
25
|
+
const require_LocalCodingTools = require("../tools/local/LocalCodingTools.cjs");
|
|
26
|
+
const require_CloudflareSandboxTools = require("../tools/cloudflare/CloudflareSandboxTools.cjs");
|
|
27
|
+
require("../tools/cloudflare/index.cjs");
|
|
28
|
+
const require_resolveLocalExecutionTools = require("../tools/local/resolveLocalExecutionTools.cjs");
|
|
29
|
+
require("../tools/local/index.cjs");
|
|
30
|
+
const require_ToolNode = require("../tools/ToolNode.cjs");
|
|
31
|
+
const require_SubagentExecutor = require("../tools/subagent/SubagentExecutor.cjs");
|
|
32
|
+
require("../tools/subagent/index.cjs");
|
|
33
|
+
const require_toolCache$1 = require("../llm/bedrock/toolCache.cjs");
|
|
34
|
+
const require_init = require("../llm/init.cjs");
|
|
35
|
+
const require_invoke = require("../llm/invoke.cjs");
|
|
36
|
+
const require_SubagentTool = require("../tools/SubagentTool.cjs");
|
|
37
|
+
const require_index$6 = require("../summarization/index.cjs");
|
|
38
|
+
const require_request = require("../llm/request.cjs");
|
|
39
|
+
const require_node = require("../summarization/node.cjs");
|
|
40
|
+
const require_schema = require("../tools/schema.cjs");
|
|
41
|
+
const require_AgentContext = require("../agents/AgentContext.cjs");
|
|
42
|
+
const require_fake = require("../llm/fake.cjs");
|
|
43
|
+
let _langchain_core_messages = require("@langchain/core/messages");
|
|
44
|
+
let _langchain_langgraph = require("@langchain/langgraph");
|
|
45
|
+
let _langchain_core_tools = require("@langchain/core/tools");
|
|
46
|
+
let nanoid = require("nanoid");
|
|
47
|
+
//#region src/graphs/Graph.ts
|
|
48
|
+
const { AGENT, TOOLS, SUMMARIZE } = require_enum.GraphNodeKeys;
|
|
63
49
|
/** Minimum relative variance before calibrated toolSchemaTokens overrides current value. */
|
|
64
|
-
const CALIBRATION_VARIANCE_THRESHOLD =
|
|
50
|
+
const CALIBRATION_VARIANCE_THRESHOLD = .15;
|
|
65
51
|
function getHandlerDispatchedEventKey(eventName, stepId) {
|
|
66
|
-
|
|
52
|
+
return `${eventName}:${stepId}`;
|
|
67
53
|
}
|
|
68
54
|
function getReasoningText(value) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const summaryText = value?.summary
|
|
73
|
-
?.map((summary) => summary.text ?? '')
|
|
74
|
-
.filter((text) => text !== '')
|
|
75
|
-
.join('');
|
|
76
|
-
return summaryText != null && summaryText !== '' ? summaryText : undefined;
|
|
55
|
+
if (typeof value === "string") return value !== "" ? value : void 0;
|
|
56
|
+
const summaryText = value?.summary?.map((summary) => summary.text ?? "").filter((text) => text !== "").join("");
|
|
57
|
+
return summaryText != null && summaryText !== "" ? summaryText : void 0;
|
|
77
58
|
}
|
|
78
59
|
function getReasoningDetailsText(value) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const reasoningText = value
|
|
83
|
-
.filter((detail) => detail.type === 'reasoning.text')
|
|
84
|
-
.map((detail) => detail.text ?? '')
|
|
85
|
-
.filter((text) => text !== '')
|
|
86
|
-
.join('');
|
|
87
|
-
return reasoningText !== '' ? reasoningText : undefined;
|
|
60
|
+
if (!Array.isArray(value)) return;
|
|
61
|
+
const reasoningText = value.filter((detail) => detail.type === "reasoning.text").map((detail) => detail.text ?? "").filter((text) => text !== "").join("");
|
|
62
|
+
return reasoningText !== "" ? reasoningText : void 0;
|
|
88
63
|
}
|
|
89
|
-
function getResponseReasoningContent({ responseMessage, reasoningKey
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (reasoningContent != null) {
|
|
100
|
-
return reasoningContent;
|
|
101
|
-
}
|
|
102
|
-
const reasoning = getReasoningText(additionalKwargs.reasoning);
|
|
103
|
-
if (reasoning != null) {
|
|
104
|
-
return reasoning;
|
|
105
|
-
}
|
|
106
|
-
return getReasoningDetailsText(additionalKwargs.reasoning_details);
|
|
64
|
+
function getResponseReasoningContent({ responseMessage, reasoningKey }) {
|
|
65
|
+
const additionalKwargs = responseMessage?.additional_kwargs;
|
|
66
|
+
if (additionalKwargs == null) return;
|
|
67
|
+
const keyedReasoning = getReasoningText(additionalKwargs[reasoningKey]);
|
|
68
|
+
if (keyedReasoning != null) return keyedReasoning;
|
|
69
|
+
const reasoningContent = getReasoningText(additionalKwargs.reasoning_content);
|
|
70
|
+
if (reasoningContent != null) return reasoningContent;
|
|
71
|
+
const reasoning = getReasoningText(additionalKwargs.reasoning);
|
|
72
|
+
if (reasoning != null) return reasoning;
|
|
73
|
+
return getReasoningDetailsText(additionalKwargs.reasoning_details);
|
|
107
74
|
}
|
|
108
75
|
function isTextMessageContentPart(contentPart) {
|
|
109
|
-
|
|
110
|
-
'type' in contentPart &&
|
|
111
|
-
typeof contentPart.type === 'string' &&
|
|
112
|
-
contentPart.type.startsWith('text'));
|
|
76
|
+
return typeof contentPart === "object" && "type" in contentPart && typeof contentPart.type === "string" && contentPart.type.startsWith("text");
|
|
113
77
|
}
|
|
114
78
|
function isGoogleServerSideToolMessageContentPart(contentPart) {
|
|
115
|
-
|
|
116
|
-
'type' in contentPart &&
|
|
117
|
-
(contentPart.type === 'toolCall' || contentPart.type === 'toolResponse'));
|
|
79
|
+
return typeof contentPart === "object" && "type" in contentPart && (contentPart.type === "toolCall" || contentPart.type === "toolResponse");
|
|
118
80
|
}
|
|
119
81
|
function hasGoogleServerSideToolDeltaContent(provider, content) {
|
|
120
|
-
|
|
121
|
-
Array.isArray(content) &&
|
|
122
|
-
content.some((contentPart) => isGoogleServerSideToolMessageContentPart(contentPart)));
|
|
82
|
+
return require_llm.isGoogleLike(provider) && Array.isArray(content) && content.some((contentPart) => isGoogleServerSideToolMessageContentPart(contentPart));
|
|
123
83
|
}
|
|
124
84
|
function getMessageDeltaContent(provider, content) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const hasGoogleServerSideToolPart = llm.isGoogleLike(provider) &&
|
|
137
|
-
content.some((contentPart) => isGoogleServerSideToolMessageContentPart(contentPart));
|
|
138
|
-
if (content.every((contentPart) => isTextMessageContentPart(contentPart))) {
|
|
139
|
-
return content;
|
|
140
|
-
}
|
|
141
|
-
if (!hasGoogleServerSideToolPart) {
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
const messageContent = content.filter((contentPart) => isTextMessageContentPart(contentPart) ||
|
|
145
|
-
isGoogleServerSideToolMessageContentPart(contentPart));
|
|
146
|
-
return messageContent.length > 0
|
|
147
|
-
? messageContent
|
|
148
|
-
: undefined;
|
|
85
|
+
if (content == null) return;
|
|
86
|
+
if (typeof content === "string") return content !== "" ? [{
|
|
87
|
+
type: "text",
|
|
88
|
+
text: content
|
|
89
|
+
}] : void 0;
|
|
90
|
+
if (content.length === 0) return;
|
|
91
|
+
const hasGoogleServerSideToolPart = require_llm.isGoogleLike(provider) && content.some((contentPart) => isGoogleServerSideToolMessageContentPart(contentPart));
|
|
92
|
+
if (content.every((contentPart) => isTextMessageContentPart(contentPart))) return content;
|
|
93
|
+
if (!hasGoogleServerSideToolPart) return;
|
|
94
|
+
const messageContent = content.filter((contentPart) => isTextMessageContentPart(contentPart) || isGoogleServerSideToolMessageContentPart(contentPart));
|
|
95
|
+
return messageContent.length > 0 ? messageContent : void 0;
|
|
149
96
|
}
|
|
150
97
|
function hasTextDeltaContent(content) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
const text = contentPart.text;
|
|
159
|
-
return typeof text === 'string' && text !== '';
|
|
160
|
-
});
|
|
98
|
+
if (content == null) return false;
|
|
99
|
+
return content.some((contentPart) => {
|
|
100
|
+
if (contentPart.type?.startsWith("text") !== true) return false;
|
|
101
|
+
const text = contentPart.text;
|
|
102
|
+
return typeof text === "string" && text !== "";
|
|
103
|
+
});
|
|
161
104
|
}
|
|
162
105
|
function hasReasoningDeltaContent(content) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
return content.some((contentPart) => contentPart.type === _enum.ContentTypes.THINK && contentPart.think !== '');
|
|
106
|
+
if (content == null) return false;
|
|
107
|
+
return content.some((contentPart) => contentPart.type === "think" && contentPart.think !== "");
|
|
167
108
|
}
|
|
168
|
-
function getCurrentStepIds({ graph, metadata
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
return currentStepIds;
|
|
109
|
+
function getCurrentStepIds({ graph, metadata }) {
|
|
110
|
+
const baseStepKey = graph.getStepBaseKey(metadata);
|
|
111
|
+
const currentStepIds = [];
|
|
112
|
+
for (const [stepKey, stepIds] of graph.stepKeyIds) {
|
|
113
|
+
if (stepKey !== baseStepKey && !stepKey.startsWith(`${baseStepKey}_`)) continue;
|
|
114
|
+
currentStepIds.push(...stepIds);
|
|
115
|
+
}
|
|
116
|
+
return currentStepIds;
|
|
178
117
|
}
|
|
179
|
-
function hasCurrentTextDeltaStep({ graph, metadata
|
|
180
|
-
|
|
118
|
+
function hasCurrentTextDeltaStep({ graph, metadata }) {
|
|
119
|
+
return getCurrentStepIds({
|
|
120
|
+
graph,
|
|
121
|
+
metadata
|
|
122
|
+
}).some((stepId) => graph.messageStepHasTextDeltas.has(stepId));
|
|
181
123
|
}
|
|
182
|
-
function hasCurrentReasoningDeltaStep({ graph, metadata
|
|
183
|
-
|
|
124
|
+
function hasCurrentReasoningDeltaStep({ graph, metadata }) {
|
|
125
|
+
return getCurrentStepIds({
|
|
126
|
+
graph,
|
|
127
|
+
metadata
|
|
128
|
+
}).some((stepId) => graph.reasoningStepHasDeltas.has(stepId));
|
|
184
129
|
}
|
|
185
|
-
function clearCurrentDeltaStepMarkers({ graph, metadata
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
130
|
+
function clearCurrentDeltaStepMarkers({ graph, metadata }) {
|
|
131
|
+
for (const stepId of getCurrentStepIds({
|
|
132
|
+
graph,
|
|
133
|
+
metadata
|
|
134
|
+
})) {
|
|
135
|
+
graph.messageStepHasTextDeltas.delete(stepId);
|
|
136
|
+
graph.reasoningStepHasDeltas.delete(stepId);
|
|
137
|
+
}
|
|
190
138
|
}
|
|
191
|
-
async function dispatchMessageCreationStep({ graph, stepKey, messageId, metadata
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
139
|
+
async function dispatchMessageCreationStep({ graph, stepKey, messageId, metadata }) {
|
|
140
|
+
await graph.dispatchRunStep(stepKey, {
|
|
141
|
+
type: "message_creation",
|
|
142
|
+
message_creation: { message_id: messageId }
|
|
143
|
+
}, metadata);
|
|
144
|
+
return graph.getStepIdByKey(stepKey);
|
|
197
145
|
}
|
|
198
|
-
async function dispatchTextMessageContent({ graph, stepKey, provider, content, metadata
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
await graph.dispatchMessageDelta(stepId, { content }, metadata);
|
|
222
|
-
return true;
|
|
146
|
+
async function dispatchTextMessageContent({ graph, stepKey, provider, content, metadata }) {
|
|
147
|
+
const messageId = require_ids.getMessageId(stepKey, graph) ?? "";
|
|
148
|
+
if (!messageId) return false;
|
|
149
|
+
if (hasGoogleServerSideToolDeltaContent(provider, content)) {
|
|
150
|
+
for (const contentPart of content) {
|
|
151
|
+
const stepId = await dispatchMessageCreationStep({
|
|
152
|
+
graph,
|
|
153
|
+
stepKey,
|
|
154
|
+
messageId,
|
|
155
|
+
metadata
|
|
156
|
+
});
|
|
157
|
+
await graph.dispatchMessageDelta(stepId, { content: [contentPart] }, metadata);
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
const stepId = await dispatchMessageCreationStep({
|
|
162
|
+
graph,
|
|
163
|
+
stepKey,
|
|
164
|
+
messageId,
|
|
165
|
+
metadata
|
|
166
|
+
});
|
|
167
|
+
await graph.dispatchMessageDelta(stepId, { content }, metadata);
|
|
168
|
+
return true;
|
|
223
169
|
}
|
|
224
|
-
async function dispatchReasoningContent({ graph, agentContext, reasoningContent, metadata
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
170
|
+
async function dispatchReasoningContent({ graph, agentContext, reasoningContent, metadata }) {
|
|
171
|
+
const previousTokenType = agentContext.currentTokenType;
|
|
172
|
+
const previousTokenTypeSwitch = agentContext.tokenTypeSwitch;
|
|
173
|
+
const previousTransitionCount = agentContext.reasoningTransitionCount;
|
|
174
|
+
agentContext.currentTokenType = "think";
|
|
175
|
+
agentContext.tokenTypeSwitch = "reasoning";
|
|
176
|
+
const stepKey = graph.getStepKey(metadata);
|
|
177
|
+
const messageId = require_ids.getMessageId(stepKey, graph) ?? "";
|
|
178
|
+
if (!messageId) {
|
|
179
|
+
agentContext.currentTokenType = previousTokenType;
|
|
180
|
+
agentContext.tokenTypeSwitch = previousTokenTypeSwitch;
|
|
181
|
+
agentContext.reasoningTransitionCount = previousTransitionCount;
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
await graph.dispatchRunStep(stepKey, {
|
|
185
|
+
type: "message_creation",
|
|
186
|
+
message_creation: { message_id: messageId }
|
|
187
|
+
}, metadata);
|
|
188
|
+
const stepId = graph.getStepIdByKey(stepKey);
|
|
189
|
+
await graph.dispatchReasoningDelta(stepId, { content: [{
|
|
190
|
+
type: "think",
|
|
191
|
+
think: reasoningContent
|
|
192
|
+
}] }, metadata);
|
|
193
|
+
return true;
|
|
247
194
|
}
|
|
248
195
|
function markPostReasoningContent(agentContext) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
agentContext.currentTokenType = _enum.ContentTypes.TEXT;
|
|
254
|
-
agentContext.tokenTypeSwitch = 'content';
|
|
255
|
-
agentContext.reasoningTransitionCount++;
|
|
256
|
-
}
|
|
257
|
-
function getDispatchableFinalReasoningContent({ agentContext, responseReasoningContent, hasStreamedTextDeltaStep, hasStreamedReasoningDeltaStep, }) {
|
|
258
|
-
if (responseReasoningContent == null || hasStreamedReasoningDeltaStep) {
|
|
259
|
-
return undefined;
|
|
260
|
-
}
|
|
261
|
-
if (agentContext.provider === _enum.Providers.OPENROUTER &&
|
|
262
|
-
hasStreamedTextDeltaStep) {
|
|
263
|
-
return undefined;
|
|
264
|
-
}
|
|
265
|
-
return responseReasoningContent;
|
|
196
|
+
if (agentContext.tokenTypeSwitch !== "reasoning" || agentContext.currentTokenType === "text") return;
|
|
197
|
+
agentContext.currentTokenType = "text";
|
|
198
|
+
agentContext.tokenTypeSwitch = "content";
|
|
199
|
+
agentContext.reasoningTransitionCount++;
|
|
266
200
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
prelimMessageIdsByStepKey = new Map();
|
|
272
|
-
config;
|
|
273
|
-
contentData = [];
|
|
274
|
-
stepKeyIds = new Map();
|
|
275
|
-
contentIndexMap = new Map();
|
|
276
|
-
toolCallStepIds = new Map();
|
|
277
|
-
/**
|
|
278
|
-
* Step IDs dispatched through the handler registry during this run.
|
|
279
|
-
* Event echo suppression is tracked separately so repeated deltas for
|
|
280
|
-
* the same step are scoped to the active custom event dispatch.
|
|
281
|
-
*/
|
|
282
|
-
handlerDispatchedStepIds = new Set();
|
|
283
|
-
reasoningStepHasDeltas = new Set();
|
|
284
|
-
handlerDispatchedEventCounts = new Map();
|
|
285
|
-
signal;
|
|
286
|
-
/** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
|
|
287
|
-
invokedToolIds;
|
|
288
|
-
handlerRegistry;
|
|
289
|
-
/**
|
|
290
|
-
* True when event-driven tool execution can be routed through callbacks even
|
|
291
|
-
* though this graph intentionally does not own the full handler registry.
|
|
292
|
-
* Self-spawned subagent graphs use this shape: their callback forwarder sends
|
|
293
|
-
* `ON_TOOL_EXECUTE` to the parent's handler, while child run-step events stay
|
|
294
|
-
* wrapped as `ON_SUBAGENT_UPDATE` instead of leaking as parent events.
|
|
295
|
-
*/
|
|
296
|
-
eventToolExecutionAvailable = false;
|
|
297
|
-
hookRegistry;
|
|
298
|
-
/**
|
|
299
|
-
* Run-scoped HITL configuration. When `humanInTheLoop?.enabled` is
|
|
300
|
-
* `true`, `ToolNode` raises a real `interrupt()` for `PreToolUse`
|
|
301
|
-
* `ask` decisions instead of treating them as a synchronous deny.
|
|
302
|
-
* Threaded from `RunConfig.humanInTheLoop`.
|
|
303
|
-
*/
|
|
304
|
-
humanInTheLoop;
|
|
305
|
-
/**
|
|
306
|
-
* Run-scoped config for the tool output reference registry. Threaded
|
|
307
|
-
* from `RunConfig.toolOutputReferences` down into every ToolNode this
|
|
308
|
-
* graph compiles.
|
|
309
|
-
*/
|
|
310
|
-
toolOutputReferences;
|
|
311
|
-
/**
|
|
312
|
-
* Run-scoped Langfuse defaults. Per-agent config wins when present.
|
|
313
|
-
*/
|
|
314
|
-
langfuse;
|
|
315
|
-
/**
|
|
316
|
-
* Run-scoped opt-in for eager event-driven tool execution. The stream
|
|
317
|
-
* handler may prestart eligible event-driven tools; ToolNode later
|
|
318
|
-
* consumes the settled promises while preserving final ToolMessage order.
|
|
319
|
-
*/
|
|
320
|
-
eagerEventToolExecution;
|
|
321
|
-
eagerEventToolExecutions = new Map();
|
|
322
|
-
eagerEventToolUsageCount = new Map();
|
|
323
|
-
eagerEventToolUsageCountsByAgentId = new Map();
|
|
324
|
-
eagerEventToolCallChunks = new Map();
|
|
325
|
-
/**
|
|
326
|
-
* Run-scoped execution backend for built-in code tools. Defaults to the
|
|
327
|
-
* remote Code API sandbox when unset.
|
|
328
|
-
*/
|
|
329
|
-
toolExecution;
|
|
330
|
-
/**
|
|
331
|
-
* Shared registry instance used by every ToolNode compiled from this
|
|
332
|
-
* graph. Lazily constructed on first access so multi-agent graphs
|
|
333
|
-
* produce one registry per run (not one per agent), letting cross-
|
|
334
|
-
* agent `{{tool<i>turn<n>}}` substitutions resolve.
|
|
335
|
-
*/
|
|
336
|
-
_toolOutputRegistry;
|
|
337
|
-
/**
|
|
338
|
-
* Tool session contexts for automatic state persistence across tool invocations.
|
|
339
|
-
* Keyed by tool name (e.g., Constants.EXECUTE_CODE).
|
|
340
|
-
* Currently supports code execution session tracking (session_id, files).
|
|
341
|
-
*/
|
|
342
|
-
sessions = new Map();
|
|
343
|
-
/**
|
|
344
|
-
* Clears heavy references to allow GC to reclaim memory held by
|
|
345
|
-
* LangGraph's internal config / AsyncLocalStorage RunTree chain.
|
|
346
|
-
* Call after a run completes and content has been extracted.
|
|
347
|
-
*/
|
|
348
|
-
clearHeavyState() {
|
|
349
|
-
this.config = undefined;
|
|
350
|
-
this.signal = undefined;
|
|
351
|
-
this.contentData = [];
|
|
352
|
-
this.contentIndexMap = new Map();
|
|
353
|
-
this.stepKeyIds = new Map();
|
|
354
|
-
this.toolCallStepIds.clear();
|
|
355
|
-
this.messageIdsByStepKey = new Map();
|
|
356
|
-
this.messageStepHasTextDeltas = new Set();
|
|
357
|
-
this.reasoningStepHasDeltas = new Set();
|
|
358
|
-
this.messageStepHasToolCalls = new Map();
|
|
359
|
-
this.prelimMessageIdsByStepKey = new Map();
|
|
360
|
-
this.invokedToolIds = undefined;
|
|
361
|
-
this.handlerRegistry = undefined;
|
|
362
|
-
this.hookRegistry = undefined;
|
|
363
|
-
this.humanInTheLoop = undefined;
|
|
364
|
-
this.toolOutputReferences = undefined;
|
|
365
|
-
this.eagerEventToolExecution = undefined;
|
|
366
|
-
this.eagerEventToolExecutions.clear();
|
|
367
|
-
this.clearEagerEventToolUsageCounts();
|
|
368
|
-
this.eagerEventToolCallChunks.clear();
|
|
369
|
-
this.toolExecution = undefined;
|
|
370
|
-
this.handlerDispatchedEventCounts.clear();
|
|
371
|
-
/**
|
|
372
|
-
* ToolNodes compiled from this graph captured the registry
|
|
373
|
-
* instance at construction time, so simply dropping the Graph's
|
|
374
|
-
* own reference would leave their captured reference — and every
|
|
375
|
-
* stored `tool<i>turn<n>` entry, plus up to `maxTotalSize` of raw
|
|
376
|
-
* output — alive across subsequent `processStream()` calls. Wipe
|
|
377
|
-
* the registry's contents first so subsequent runs start fresh.
|
|
378
|
-
*/
|
|
379
|
-
this._toolOutputRegistry?.clear();
|
|
380
|
-
this._toolOutputRegistry = undefined;
|
|
381
|
-
// NB: `_fileCheckpointer` is intentionally NOT cleared here.
|
|
382
|
-
// `Run.processStream()` calls `clearHeavyState()` in its
|
|
383
|
-
// finally block on natural-completion / error paths — exactly
|
|
384
|
-
// when the host is most likely to want `Run.rewindFiles()` (for
|
|
385
|
-
// rollback after a failed batch). Per-Run isolation is already
|
|
386
|
-
// automatic because each `Run.create()` constructs a brand-new
|
|
387
|
-
// Graph instance, so the next Run gets its own checkpointer
|
|
388
|
-
// without us needing to reset this field. Codex P1 #32: pre-fix
|
|
389
|
-
// the checkpointer was nulled before the caller could reach it.
|
|
390
|
-
// Flush each compiled ToolNode's direct-path turn cache so it
|
|
391
|
-
// doesn't leak across Runs (Codex P2 #33). The cache survives
|
|
392
|
-
// `run()` re-entry by design (resume-stable), but end-of-Run
|
|
393
|
-
// is the right point to reset it.
|
|
394
|
-
for (const node of this._compiledToolNodes) {
|
|
395
|
-
node.clearDirectPathTurns();
|
|
396
|
-
}
|
|
397
|
-
this._compiledToolNodes.clear();
|
|
398
|
-
this.sessions.clear();
|
|
399
|
-
}
|
|
400
|
-
getEagerEventToolUsageCount(agentId) {
|
|
401
|
-
if (agentId == null || agentId === '') {
|
|
402
|
-
return this.eagerEventToolUsageCount;
|
|
403
|
-
}
|
|
404
|
-
let usageCount = this.eagerEventToolUsageCountsByAgentId.get(agentId);
|
|
405
|
-
if (usageCount == null) {
|
|
406
|
-
usageCount = new Map();
|
|
407
|
-
this.eagerEventToolUsageCountsByAgentId.set(agentId, usageCount);
|
|
408
|
-
}
|
|
409
|
-
return usageCount;
|
|
410
|
-
}
|
|
411
|
-
clearEagerEventToolUsageCounts() {
|
|
412
|
-
this.eagerEventToolUsageCount.clear();
|
|
413
|
-
for (const usageCount of this.eagerEventToolUsageCountsByAgentId.values()) {
|
|
414
|
-
usageCount.clear();
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
markHandlerDispatchedEvent(eventName, stepId) {
|
|
418
|
-
const key = getHandlerDispatchedEventKey(eventName, stepId);
|
|
419
|
-
this.handlerDispatchedEventCounts.set(key, (this.handlerDispatchedEventCounts.get(key) ?? 0) + 1);
|
|
420
|
-
return () => {
|
|
421
|
-
const count = this.handlerDispatchedEventCounts.get(key) ?? 0;
|
|
422
|
-
if (count <= 1) {
|
|
423
|
-
this.handlerDispatchedEventCounts.delete(key);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
this.handlerDispatchedEventCounts.set(key, count - 1);
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
hasHandlerDispatchedEvent(eventName, stepId) {
|
|
430
|
-
const key = getHandlerDispatchedEventKey(eventName, stepId);
|
|
431
|
-
return (this.handlerDispatchedEventCounts.get(key) ?? 0) > 0;
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Subclass hook to register a freshly compiled ToolNode so
|
|
435
|
-
* `clearHeavyState` can flush its per-Run direct-path turn cache
|
|
436
|
-
* at end-of-Run. Internal — called from `initializeTools` in the
|
|
437
|
-
* concrete graph subclasses.
|
|
438
|
-
*/
|
|
439
|
-
registerCompiledToolNode(node) {
|
|
440
|
-
this._compiledToolNodes.add(node);
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Returns the shared `ToolOutputReferenceRegistry` for this run,
|
|
444
|
-
* constructing it on first access. Returns `undefined` when the
|
|
445
|
-
* feature is disabled. All ToolNodes compiled from this graph share
|
|
446
|
-
* this single instance so cross-agent `{{…}}` references resolve.
|
|
447
|
-
*
|
|
448
|
-
* @internal Public so `attemptInvoke` can read it through the typed
|
|
449
|
-
* `InvokeContext` and project ToolMessages into LLM-facing annotated
|
|
450
|
-
* copies right before each provider call (see
|
|
451
|
-
* `annotateMessagesForLLM`). Host code should not call this directly
|
|
452
|
-
* — registry mutations outside the ToolNode lifecycle break the
|
|
453
|
-
* partitioning, eviction, and turn-counter invariants.
|
|
454
|
-
*/
|
|
455
|
-
getOrCreateToolOutputRegistry() {
|
|
456
|
-
if (this.toolOutputReferences?.enabled !== true) {
|
|
457
|
-
return undefined;
|
|
458
|
-
}
|
|
459
|
-
if (this._toolOutputRegistry == null) {
|
|
460
|
-
this._toolOutputRegistry = new toolOutputReferences.ToolOutputReferenceRegistry({
|
|
461
|
-
maxOutputSize: this.toolOutputReferences.maxOutputSize,
|
|
462
|
-
maxTotalSize: this.toolOutputReferences.maxTotalSize,
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
return this._toolOutputRegistry;
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Single per-Run file checkpointer shared across every ToolNode the
|
|
469
|
-
* graph compiles. Lazily constructed when
|
|
470
|
-
* `toolExecution.local.fileCheckpointing === true` or
|
|
471
|
-
* `toolExecution.cloudflare.fileCheckpointing === true` so
|
|
472
|
-
* multi-agent graphs see ONE snapshot store, not one-per-agent.
|
|
473
|
-
* Returns undefined when checkpointing is disabled or a supported
|
|
474
|
-
* coding-tool engine isn't selected. Exposed via
|
|
475
|
-
* `Run.getFileCheckpointer()` / `Run.rewindFiles()`.
|
|
476
|
-
*/
|
|
477
|
-
_fileCheckpointer;
|
|
478
|
-
/**
|
|
479
|
-
* ToolNodes compiled into this Graph's workflow. Tracked so
|
|
480
|
-
* `clearHeavyState()` can flush their per-Run direct-path turn
|
|
481
|
-
* cache (`directPathTurns`) at end-of-Run — that map intentionally
|
|
482
|
-
* survives `run()` re-entry (resume-stable per Codex P2 #30) but
|
|
483
|
-
* would otherwise grow linearly with tool calls and could collide
|
|
484
|
-
* across Runs if a provider reuses call ids (Codex P2 #33).
|
|
485
|
-
*/
|
|
486
|
-
_compiledToolNodes = new Set();
|
|
487
|
-
getOrCreateFileCheckpointer() {
|
|
488
|
-
// Return the cached instance unconditionally if one exists. The
|
|
489
|
-
// toolExecution check below decides whether to *create* a new
|
|
490
|
-
// one — `clearHeavyState` nulls `this.toolExecution` at end-of-
|
|
491
|
-
// Run, but we want post-Run `Run.rewindFiles()` to still resolve
|
|
492
|
-
// to the checkpointer that captured the writes. Codex P1 #32.
|
|
493
|
-
if (this._fileCheckpointer != null) {
|
|
494
|
-
return this._fileCheckpointer;
|
|
495
|
-
}
|
|
496
|
-
// Eagerly create via the bundle factory so the construction path
|
|
497
|
-
// matches the bundle-only callers (and future bundle-internal
|
|
498
|
-
// cleanup hooks fire). The bundle factory itself accepts a pre-
|
|
499
|
-
// supplied checkpointer when present, so re-injecting this one
|
|
500
|
-
// into every ToolNode is idempotent.
|
|
501
|
-
if (this.toolExecution?.engine === 'local' &&
|
|
502
|
-
this.toolExecution.local?.fileCheckpointing === true) {
|
|
503
|
-
const bundle = LocalCodingTools.createLocalCodingToolBundle(this.toolExecution.local ?? {});
|
|
504
|
-
this._fileCheckpointer = bundle.checkpointer;
|
|
505
|
-
return this._fileCheckpointer;
|
|
506
|
-
}
|
|
507
|
-
if (this.toolExecution?.engine === 'cloudflare-sandbox' &&
|
|
508
|
-
this.toolExecution.cloudflare?.fileCheckpointing === true) {
|
|
509
|
-
const bundle = CloudflareSandboxTools.createCloudflareCodingToolBundle(this.toolExecution.cloudflare);
|
|
510
|
-
this._fileCheckpointer = bundle.checkpointer;
|
|
511
|
-
return this._fileCheckpointer;
|
|
512
|
-
}
|
|
513
|
-
return undefined;
|
|
514
|
-
}
|
|
201
|
+
function getDispatchableFinalReasoningContent({ agentContext, responseReasoningContent, hasStreamedTextDeltaStep, hasStreamedReasoningDeltaStep }) {
|
|
202
|
+
if (responseReasoningContent == null || hasStreamedReasoningDeltaStep) return;
|
|
203
|
+
if (agentContext.provider === "openrouter" && hasStreamedTextDeltaStep) return;
|
|
204
|
+
return responseReasoningContent;
|
|
515
205
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
const runStep = graph.getRunStep(stepId);
|
|
1729
|
-
if (!runStep) {
|
|
1730
|
-
throw new Error(`No run step found for stepId ${stepId}`);
|
|
1731
|
-
}
|
|
1732
|
-
const tool_call = {
|
|
1733
|
-
id: data.id,
|
|
1734
|
-
name: name || '',
|
|
1735
|
-
args: typeof args === 'string' ? args : JSON.stringify(args),
|
|
1736
|
-
output: `Error processing tool${error?.message != null ? `: ${error.message}` : ''}`,
|
|
1737
|
-
progress: 1,
|
|
1738
|
-
};
|
|
1739
|
-
await graph.handlerRegistry
|
|
1740
|
-
?.getHandler(_enum.GraphEvents.ON_RUN_STEP_COMPLETED)
|
|
1741
|
-
?.handle(_enum.GraphEvents.ON_RUN_STEP_COMPLETED, {
|
|
1742
|
-
result: {
|
|
1743
|
-
id: stepId,
|
|
1744
|
-
index: runStep.index,
|
|
1745
|
-
type: 'tool_call',
|
|
1746
|
-
tool_call,
|
|
1747
|
-
},
|
|
1748
|
-
}, metadata, graph);
|
|
1749
|
-
}
|
|
1750
|
-
/**
|
|
1751
|
-
* Instance method that delegates to the static method
|
|
1752
|
-
* Kept for backward compatibility
|
|
1753
|
-
*/
|
|
1754
|
-
async handleToolCallError(data, metadata) {
|
|
1755
|
-
await StandardGraph.handleToolCallErrorStatic(this, data, metadata);
|
|
1756
|
-
}
|
|
1757
|
-
async dispatchRunStepDelta(id, delta, metadata) {
|
|
1758
|
-
if (!this.config) {
|
|
1759
|
-
throw new Error('No config provided');
|
|
1760
|
-
}
|
|
1761
|
-
else if (!id) {
|
|
1762
|
-
throw new Error('No step ID found');
|
|
1763
|
-
}
|
|
1764
|
-
const runStepDelta = {
|
|
1765
|
-
id,
|
|
1766
|
-
delta,
|
|
1767
|
-
};
|
|
1768
|
-
const handler = this.handlerRegistry?.getHandler(_enum.GraphEvents.ON_RUN_STEP_DELTA);
|
|
1769
|
-
if (handler) {
|
|
1770
|
-
await handler.handle(_enum.GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, metadata, this);
|
|
1771
|
-
this.handlerDispatchedStepIds.add(id);
|
|
1772
|
-
}
|
|
1773
|
-
const unmarkHandlerDispatchedEvent = handler
|
|
1774
|
-
? this.markHandlerDispatchedEvent(_enum.GraphEvents.ON_RUN_STEP_DELTA, id)
|
|
1775
|
-
: undefined;
|
|
1776
|
-
try {
|
|
1777
|
-
await events.safeDispatchCustomEvent(_enum.GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, this.config);
|
|
1778
|
-
}
|
|
1779
|
-
finally {
|
|
1780
|
-
unmarkHandlerDispatchedEvent?.();
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
async dispatchMessageDelta(id, delta, metadata) {
|
|
1784
|
-
if (!this.config) {
|
|
1785
|
-
throw new Error('No config provided');
|
|
1786
|
-
}
|
|
1787
|
-
const messageDelta = {
|
|
1788
|
-
id,
|
|
1789
|
-
delta,
|
|
1790
|
-
};
|
|
1791
|
-
if (hasTextDeltaContent(delta.content)) {
|
|
1792
|
-
this.messageStepHasTextDeltas.add(id);
|
|
1793
|
-
}
|
|
1794
|
-
const handler = this.handlerRegistry?.getHandler(_enum.GraphEvents.ON_MESSAGE_DELTA);
|
|
1795
|
-
if (handler) {
|
|
1796
|
-
await handler.handle(_enum.GraphEvents.ON_MESSAGE_DELTA, messageDelta, metadata, this);
|
|
1797
|
-
this.handlerDispatchedStepIds.add(id);
|
|
1798
|
-
}
|
|
1799
|
-
const unmarkHandlerDispatchedEvent = handler
|
|
1800
|
-
? this.markHandlerDispatchedEvent(_enum.GraphEvents.ON_MESSAGE_DELTA, id)
|
|
1801
|
-
: undefined;
|
|
1802
|
-
try {
|
|
1803
|
-
await events.safeDispatchCustomEvent(_enum.GraphEvents.ON_MESSAGE_DELTA, messageDelta, this.config);
|
|
1804
|
-
}
|
|
1805
|
-
finally {
|
|
1806
|
-
unmarkHandlerDispatchedEvent?.();
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
dispatchReasoningDelta = async (stepId, delta, metadata) => {
|
|
1810
|
-
if (!this.config) {
|
|
1811
|
-
throw new Error('No config provided');
|
|
1812
|
-
}
|
|
1813
|
-
const reasoningDelta = {
|
|
1814
|
-
id: stepId,
|
|
1815
|
-
delta,
|
|
1816
|
-
};
|
|
1817
|
-
if (hasReasoningDeltaContent(delta.content)) {
|
|
1818
|
-
this.reasoningStepHasDeltas.add(stepId);
|
|
1819
|
-
}
|
|
1820
|
-
const handler = this.handlerRegistry?.getHandler(_enum.GraphEvents.ON_REASONING_DELTA);
|
|
1821
|
-
if (handler) {
|
|
1822
|
-
await handler.handle(_enum.GraphEvents.ON_REASONING_DELTA, reasoningDelta, metadata, this);
|
|
1823
|
-
this.handlerDispatchedStepIds.add(stepId);
|
|
1824
|
-
}
|
|
1825
|
-
const unmarkHandlerDispatchedEvent = handler
|
|
1826
|
-
? this.markHandlerDispatchedEvent(_enum.GraphEvents.ON_REASONING_DELTA, stepId)
|
|
1827
|
-
: undefined;
|
|
1828
|
-
try {
|
|
1829
|
-
await events.safeDispatchCustomEvent(_enum.GraphEvents.ON_REASONING_DELTA, reasoningDelta, this.config);
|
|
1830
|
-
}
|
|
1831
|
-
finally {
|
|
1832
|
-
unmarkHandlerDispatchedEvent?.();
|
|
1833
|
-
}
|
|
1834
|
-
};
|
|
1835
|
-
}
|
|
1836
|
-
|
|
206
|
+
var Graph = class {
|
|
207
|
+
messageStepHasTextDeltas = /* @__PURE__ */ new Set();
|
|
208
|
+
messageStepHasToolCalls = /* @__PURE__ */ new Map();
|
|
209
|
+
messageIdsByStepKey = /* @__PURE__ */ new Map();
|
|
210
|
+
prelimMessageIdsByStepKey = /* @__PURE__ */ new Map();
|
|
211
|
+
config;
|
|
212
|
+
contentData = [];
|
|
213
|
+
stepKeyIds = /* @__PURE__ */ new Map();
|
|
214
|
+
contentIndexMap = /* @__PURE__ */ new Map();
|
|
215
|
+
toolCallStepIds = /* @__PURE__ */ new Map();
|
|
216
|
+
/**
|
|
217
|
+
* Step IDs dispatched through the handler registry during this run.
|
|
218
|
+
* Event echo suppression is tracked separately so repeated deltas for
|
|
219
|
+
* the same step are scoped to the active custom event dispatch.
|
|
220
|
+
*/
|
|
221
|
+
handlerDispatchedStepIds = /* @__PURE__ */ new Set();
|
|
222
|
+
reasoningStepHasDeltas = /* @__PURE__ */ new Set();
|
|
223
|
+
handlerDispatchedEventCounts = /* @__PURE__ */ new Map();
|
|
224
|
+
signal;
|
|
225
|
+
/** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
|
|
226
|
+
invokedToolIds;
|
|
227
|
+
handlerRegistry;
|
|
228
|
+
/**
|
|
229
|
+
* True when event-driven tool execution can be routed through callbacks even
|
|
230
|
+
* though this graph intentionally does not own the full handler registry.
|
|
231
|
+
* Self-spawned subagent graphs use this shape: their callback forwarder sends
|
|
232
|
+
* `ON_TOOL_EXECUTE` to the parent's handler, while child run-step events stay
|
|
233
|
+
* wrapped as `ON_SUBAGENT_UPDATE` instead of leaking as parent events.
|
|
234
|
+
*/
|
|
235
|
+
eventToolExecutionAvailable = false;
|
|
236
|
+
hookRegistry;
|
|
237
|
+
/**
|
|
238
|
+
* Run-scoped HITL configuration. When `humanInTheLoop?.enabled` is
|
|
239
|
+
* `true`, `ToolNode` raises a real `interrupt()` for `PreToolUse`
|
|
240
|
+
* `ask` decisions instead of treating them as a synchronous deny.
|
|
241
|
+
* Threaded from `RunConfig.humanInTheLoop`.
|
|
242
|
+
*/
|
|
243
|
+
humanInTheLoop;
|
|
244
|
+
/**
|
|
245
|
+
* Run-scoped config for the tool output reference registry. Threaded
|
|
246
|
+
* from `RunConfig.toolOutputReferences` down into every ToolNode this
|
|
247
|
+
* graph compiles.
|
|
248
|
+
*/
|
|
249
|
+
toolOutputReferences;
|
|
250
|
+
/**
|
|
251
|
+
* Run-scoped Langfuse defaults. Per-agent config wins when present.
|
|
252
|
+
*/
|
|
253
|
+
langfuse;
|
|
254
|
+
/**
|
|
255
|
+
* Run-scoped opt-in for eager event-driven tool execution. The stream
|
|
256
|
+
* handler may prestart eligible event-driven tools; ToolNode later
|
|
257
|
+
* consumes the settled promises while preserving final ToolMessage order.
|
|
258
|
+
*/
|
|
259
|
+
eagerEventToolExecution;
|
|
260
|
+
eagerEventToolExecutions = /* @__PURE__ */ new Map();
|
|
261
|
+
eagerEventToolUsageCount = /* @__PURE__ */ new Map();
|
|
262
|
+
eagerEventToolUsageCountsByAgentId = /* @__PURE__ */ new Map();
|
|
263
|
+
eagerEventToolCallChunks = /* @__PURE__ */ new Map();
|
|
264
|
+
/**
|
|
265
|
+
* Run-scoped execution backend for built-in code tools. Defaults to the
|
|
266
|
+
* remote Code API sandbox when unset.
|
|
267
|
+
*/
|
|
268
|
+
toolExecution;
|
|
269
|
+
/**
|
|
270
|
+
* Shared registry instance used by every ToolNode compiled from this
|
|
271
|
+
* graph. Lazily constructed on first access so multi-agent graphs
|
|
272
|
+
* produce one registry per run (not one per agent), letting cross-
|
|
273
|
+
* agent `{{tool<i>turn<n>}}` substitutions resolve.
|
|
274
|
+
*/
|
|
275
|
+
_toolOutputRegistry;
|
|
276
|
+
/**
|
|
277
|
+
* Tool session contexts for automatic state persistence across tool invocations.
|
|
278
|
+
* Keyed by tool name (e.g., Constants.EXECUTE_CODE).
|
|
279
|
+
* Currently supports code execution session tracking (session_id, files).
|
|
280
|
+
*/
|
|
281
|
+
sessions = /* @__PURE__ */ new Map();
|
|
282
|
+
/**
|
|
283
|
+
* Clears heavy references to allow GC to reclaim memory held by
|
|
284
|
+
* LangGraph's internal config / AsyncLocalStorage RunTree chain.
|
|
285
|
+
* Call after a run completes and content has been extracted.
|
|
286
|
+
*/
|
|
287
|
+
clearHeavyState() {
|
|
288
|
+
this.config = void 0;
|
|
289
|
+
this.signal = void 0;
|
|
290
|
+
this.contentData = [];
|
|
291
|
+
this.contentIndexMap = /* @__PURE__ */ new Map();
|
|
292
|
+
this.stepKeyIds = /* @__PURE__ */ new Map();
|
|
293
|
+
this.toolCallStepIds.clear();
|
|
294
|
+
this.messageIdsByStepKey = /* @__PURE__ */ new Map();
|
|
295
|
+
this.messageStepHasTextDeltas = /* @__PURE__ */ new Set();
|
|
296
|
+
this.reasoningStepHasDeltas = /* @__PURE__ */ new Set();
|
|
297
|
+
this.messageStepHasToolCalls = /* @__PURE__ */ new Map();
|
|
298
|
+
this.prelimMessageIdsByStepKey = /* @__PURE__ */ new Map();
|
|
299
|
+
this.invokedToolIds = void 0;
|
|
300
|
+
this.handlerRegistry = void 0;
|
|
301
|
+
this.hookRegistry = void 0;
|
|
302
|
+
this.humanInTheLoop = void 0;
|
|
303
|
+
this.toolOutputReferences = void 0;
|
|
304
|
+
this.eagerEventToolExecution = void 0;
|
|
305
|
+
this.eagerEventToolExecutions.clear();
|
|
306
|
+
this.clearEagerEventToolUsageCounts();
|
|
307
|
+
this.eagerEventToolCallChunks.clear();
|
|
308
|
+
this.toolExecution = void 0;
|
|
309
|
+
this.handlerDispatchedEventCounts.clear();
|
|
310
|
+
/**
|
|
311
|
+
* ToolNodes compiled from this graph captured the registry
|
|
312
|
+
* instance at construction time, so simply dropping the Graph's
|
|
313
|
+
* own reference would leave their captured reference — and every
|
|
314
|
+
* stored `tool<i>turn<n>` entry, plus up to `maxTotalSize` of raw
|
|
315
|
+
* output — alive across subsequent `processStream()` calls. Wipe
|
|
316
|
+
* the registry's contents first so subsequent runs start fresh.
|
|
317
|
+
*/
|
|
318
|
+
this._toolOutputRegistry?.clear();
|
|
319
|
+
this._toolOutputRegistry = void 0;
|
|
320
|
+
for (const node of this._compiledToolNodes) node.clearDirectPathTurns();
|
|
321
|
+
this._compiledToolNodes.clear();
|
|
322
|
+
this.sessions.clear();
|
|
323
|
+
}
|
|
324
|
+
getEagerEventToolUsageCount(agentId) {
|
|
325
|
+
if (agentId == null || agentId === "") return this.eagerEventToolUsageCount;
|
|
326
|
+
let usageCount = this.eagerEventToolUsageCountsByAgentId.get(agentId);
|
|
327
|
+
if (usageCount == null) {
|
|
328
|
+
usageCount = /* @__PURE__ */ new Map();
|
|
329
|
+
this.eagerEventToolUsageCountsByAgentId.set(agentId, usageCount);
|
|
330
|
+
}
|
|
331
|
+
return usageCount;
|
|
332
|
+
}
|
|
333
|
+
clearEagerEventToolUsageCounts() {
|
|
334
|
+
this.eagerEventToolUsageCount.clear();
|
|
335
|
+
for (const usageCount of this.eagerEventToolUsageCountsByAgentId.values()) usageCount.clear();
|
|
336
|
+
}
|
|
337
|
+
markHandlerDispatchedEvent(eventName, stepId) {
|
|
338
|
+
const key = getHandlerDispatchedEventKey(eventName, stepId);
|
|
339
|
+
this.handlerDispatchedEventCounts.set(key, (this.handlerDispatchedEventCounts.get(key) ?? 0) + 1);
|
|
340
|
+
return () => {
|
|
341
|
+
const count = this.handlerDispatchedEventCounts.get(key) ?? 0;
|
|
342
|
+
if (count <= 1) {
|
|
343
|
+
this.handlerDispatchedEventCounts.delete(key);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
this.handlerDispatchedEventCounts.set(key, count - 1);
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
hasHandlerDispatchedEvent(eventName, stepId) {
|
|
350
|
+
const key = getHandlerDispatchedEventKey(eventName, stepId);
|
|
351
|
+
return (this.handlerDispatchedEventCounts.get(key) ?? 0) > 0;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Subclass hook to register a freshly compiled ToolNode so
|
|
355
|
+
* `clearHeavyState` can flush its per-Run direct-path turn cache
|
|
356
|
+
* at end-of-Run. Internal — called from `initializeTools` in the
|
|
357
|
+
* concrete graph subclasses.
|
|
358
|
+
*/
|
|
359
|
+
registerCompiledToolNode(node) {
|
|
360
|
+
this._compiledToolNodes.add(node);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Returns the shared `ToolOutputReferenceRegistry` for this run,
|
|
364
|
+
* constructing it on first access. Returns `undefined` when the
|
|
365
|
+
* feature is disabled. All ToolNodes compiled from this graph share
|
|
366
|
+
* this single instance so cross-agent `{{…}}` references resolve.
|
|
367
|
+
*
|
|
368
|
+
* @internal Public so `attemptInvoke` can read it through the typed
|
|
369
|
+
* `InvokeContext` and project ToolMessages into LLM-facing annotated
|
|
370
|
+
* copies right before each provider call (see
|
|
371
|
+
* `annotateMessagesForLLM`). Host code should not call this directly
|
|
372
|
+
* — registry mutations outside the ToolNode lifecycle break the
|
|
373
|
+
* partitioning, eviction, and turn-counter invariants.
|
|
374
|
+
*/
|
|
375
|
+
getOrCreateToolOutputRegistry() {
|
|
376
|
+
if (this.toolOutputReferences?.enabled !== true) return;
|
|
377
|
+
if (this._toolOutputRegistry == null) this._toolOutputRegistry = new require_toolOutputReferences.ToolOutputReferenceRegistry({
|
|
378
|
+
maxOutputSize: this.toolOutputReferences.maxOutputSize,
|
|
379
|
+
maxTotalSize: this.toolOutputReferences.maxTotalSize
|
|
380
|
+
});
|
|
381
|
+
return this._toolOutputRegistry;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Single per-Run file checkpointer shared across every ToolNode the
|
|
385
|
+
* graph compiles. Lazily constructed when
|
|
386
|
+
* `toolExecution.local.fileCheckpointing === true` or
|
|
387
|
+
* `toolExecution.cloudflare.fileCheckpointing === true` so
|
|
388
|
+
* multi-agent graphs see ONE snapshot store, not one-per-agent.
|
|
389
|
+
* Returns undefined when checkpointing is disabled or a supported
|
|
390
|
+
* coding-tool engine isn't selected. Exposed via
|
|
391
|
+
* `Run.getFileCheckpointer()` / `Run.rewindFiles()`.
|
|
392
|
+
*/
|
|
393
|
+
_fileCheckpointer;
|
|
394
|
+
/**
|
|
395
|
+
* ToolNodes compiled into this Graph's workflow. Tracked so
|
|
396
|
+
* `clearHeavyState()` can flush their per-Run direct-path turn
|
|
397
|
+
* cache (`directPathTurns`) at end-of-Run — that map intentionally
|
|
398
|
+
* survives `run()` re-entry (resume-stable per Codex P2 #30) but
|
|
399
|
+
* would otherwise grow linearly with tool calls and could collide
|
|
400
|
+
* across Runs if a provider reuses call ids (Codex P2 #33).
|
|
401
|
+
*/
|
|
402
|
+
_compiledToolNodes = /* @__PURE__ */ new Set();
|
|
403
|
+
getOrCreateFileCheckpointer() {
|
|
404
|
+
if (this._fileCheckpointer != null) return this._fileCheckpointer;
|
|
405
|
+
if (this.toolExecution?.engine === "local" && this.toolExecution.local?.fileCheckpointing === true) {
|
|
406
|
+
const bundle = require_LocalCodingTools.createLocalCodingToolBundle(this.toolExecution.local ?? {});
|
|
407
|
+
this._fileCheckpointer = bundle.checkpointer;
|
|
408
|
+
return this._fileCheckpointer;
|
|
409
|
+
}
|
|
410
|
+
if (this.toolExecution?.engine === "cloudflare-sandbox" && this.toolExecution.cloudflare?.fileCheckpointing === true) {
|
|
411
|
+
const bundle = require_CloudflareSandboxTools.createCloudflareCodingToolBundle(this.toolExecution.cloudflare);
|
|
412
|
+
this._fileCheckpointer = bundle.checkpointer;
|
|
413
|
+
return this._fileCheckpointer;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
var StandardGraph = class StandardGraph extends Graph {
|
|
418
|
+
overrideModel;
|
|
419
|
+
/** Optional compile options passed into workflow.compile() */
|
|
420
|
+
compileOptions;
|
|
421
|
+
messages = [];
|
|
422
|
+
/** Cached run messages preserved before clearHeavyState() so getRunMessages() works after cleanup. */
|
|
423
|
+
cachedRunMessages;
|
|
424
|
+
runId;
|
|
425
|
+
/**
|
|
426
|
+
* Boundary between historical messages (loaded from conversation state)
|
|
427
|
+
* and messages produced during the current run. Set once in the state
|
|
428
|
+
* reducer when messages first arrive. Used by `getRunMessages()` and
|
|
429
|
+
* multi-agent message filtering — NOT for pruner token counting (the
|
|
430
|
+
* pruner maintains its own `lastTurnStartIndex` in its closure).
|
|
431
|
+
*/
|
|
432
|
+
startIndex = 0;
|
|
433
|
+
signal;
|
|
434
|
+
/** Map of agent contexts by agent ID */
|
|
435
|
+
agentContexts = /* @__PURE__ */ new Map();
|
|
436
|
+
/** Default agent ID to use */
|
|
437
|
+
defaultAgentId;
|
|
438
|
+
constructor({ runId, signal, agents, langfuse, tokenCounter, indexTokenCountMap, calibrationRatio }) {
|
|
439
|
+
super();
|
|
440
|
+
this.runId = runId;
|
|
441
|
+
this.signal = signal;
|
|
442
|
+
this.langfuse = langfuse;
|
|
443
|
+
if (agents.length === 0) throw new Error("At least one agent configuration is required");
|
|
444
|
+
for (const agentConfig of agents) {
|
|
445
|
+
const agentContext = require_AgentContext.AgentContext.fromConfig(agentConfig, tokenCounter, indexTokenCountMap);
|
|
446
|
+
if (calibrationRatio != null && calibrationRatio > 0) agentContext.calibrationRatio = calibrationRatio;
|
|
447
|
+
this.agentContexts.set(agentConfig.agentId, agentContext);
|
|
448
|
+
}
|
|
449
|
+
this.defaultAgentId = agents[0].agentId;
|
|
450
|
+
}
|
|
451
|
+
resetValues(keepContent) {
|
|
452
|
+
this.messages = [];
|
|
453
|
+
this.cachedRunMessages = void 0;
|
|
454
|
+
this.config = require_graph.resetIfNotEmpty(this.config, void 0);
|
|
455
|
+
if (keepContent !== true) {
|
|
456
|
+
this.contentData = require_graph.resetIfNotEmpty(this.contentData, []);
|
|
457
|
+
this.contentIndexMap = require_graph.resetIfNotEmpty(this.contentIndexMap, /* @__PURE__ */ new Map());
|
|
458
|
+
}
|
|
459
|
+
this.stepKeyIds = require_graph.resetIfNotEmpty(this.stepKeyIds, /* @__PURE__ */ new Map());
|
|
460
|
+
/**
|
|
461
|
+
* Clear in-place instead of replacing with a new Map to preserve the
|
|
462
|
+
* shared reference held by ToolNode (passed at construction time).
|
|
463
|
+
* Using resetIfNotEmpty would create a new Map, leaving ToolNode with
|
|
464
|
+
* a stale reference on 2nd+ processStream calls.
|
|
465
|
+
*/
|
|
466
|
+
this.toolCallStepIds.clear();
|
|
467
|
+
this.eagerEventToolExecutions.clear();
|
|
468
|
+
this.clearEagerEventToolUsageCounts();
|
|
469
|
+
this.eagerEventToolCallChunks.clear();
|
|
470
|
+
this.handlerDispatchedStepIds = require_graph.resetIfNotEmpty(this.handlerDispatchedStepIds, /* @__PURE__ */ new Set());
|
|
471
|
+
this.handlerDispatchedEventCounts = require_graph.resetIfNotEmpty(this.handlerDispatchedEventCounts, /* @__PURE__ */ new Map());
|
|
472
|
+
this.messageIdsByStepKey = require_graph.resetIfNotEmpty(this.messageIdsByStepKey, /* @__PURE__ */ new Map());
|
|
473
|
+
this.messageStepHasToolCalls = require_graph.resetIfNotEmpty(this.messageStepHasToolCalls, /* @__PURE__ */ new Map());
|
|
474
|
+
this.messageStepHasTextDeltas = require_graph.resetIfNotEmpty(this.messageStepHasTextDeltas, /* @__PURE__ */ new Set());
|
|
475
|
+
this.reasoningStepHasDeltas = require_graph.resetIfNotEmpty(this.reasoningStepHasDeltas, /* @__PURE__ */ new Set());
|
|
476
|
+
this.prelimMessageIdsByStepKey = require_graph.resetIfNotEmpty(this.prelimMessageIdsByStepKey, /* @__PURE__ */ new Map());
|
|
477
|
+
this.invokedToolIds = require_graph.resetIfNotEmpty(this.invokedToolIds, void 0);
|
|
478
|
+
for (const context of this.agentContexts.values()) context.reset();
|
|
479
|
+
}
|
|
480
|
+
clearHeavyState() {
|
|
481
|
+
this.cachedRunMessages = this.messages.slice(this.startIndex);
|
|
482
|
+
super.clearHeavyState();
|
|
483
|
+
this.messages = [];
|
|
484
|
+
this.overrideModel = void 0;
|
|
485
|
+
for (const context of this.agentContexts.values()) context.reset();
|
|
486
|
+
}
|
|
487
|
+
getRunStep(stepId) {
|
|
488
|
+
const index = this.contentIndexMap.get(stepId);
|
|
489
|
+
if (index !== void 0) return this.contentData[index];
|
|
490
|
+
}
|
|
491
|
+
getAgentContext(metadata) {
|
|
492
|
+
if (!metadata) throw new Error("No metadata provided to retrieve agent context");
|
|
493
|
+
const currentNode = metadata.langgraph_node;
|
|
494
|
+
if (!currentNode) throw new Error("No langgraph_node in metadata to retrieve agent context");
|
|
495
|
+
let agentId;
|
|
496
|
+
if (currentNode.startsWith(AGENT)) agentId = currentNode.substring(AGENT.length);
|
|
497
|
+
else if (currentNode.startsWith(TOOLS)) agentId = currentNode.substring(TOOLS.length);
|
|
498
|
+
else if (currentNode.startsWith(SUMMARIZE)) agentId = currentNode.substring(SUMMARIZE.length);
|
|
499
|
+
const agentContext = this.agentContexts.get(agentId ?? "");
|
|
500
|
+
if (!agentContext) throw new Error(`No agent context found for agent ID ${agentId}`);
|
|
501
|
+
return agentContext;
|
|
502
|
+
}
|
|
503
|
+
getStepBaseKey(metadata) {
|
|
504
|
+
if (!metadata) return "";
|
|
505
|
+
const keyList = this.getInvocationKeyList(metadata);
|
|
506
|
+
if (this.checkKeyList(keyList)) throw new Error("Missing metadata");
|
|
507
|
+
return require_graph.joinKeys(keyList);
|
|
508
|
+
}
|
|
509
|
+
getStepKey(metadata) {
|
|
510
|
+
if (!metadata) return "";
|
|
511
|
+
const keyList = this.getKeyList(metadata);
|
|
512
|
+
if (this.checkKeyList(keyList)) throw new Error("Missing metadata");
|
|
513
|
+
return require_graph.joinKeys(keyList);
|
|
514
|
+
}
|
|
515
|
+
getStepIdByKey(stepKey, index) {
|
|
516
|
+
const stepIds = this.stepKeyIds.get(stepKey);
|
|
517
|
+
if (!stepIds) throw new Error(`No step IDs found for stepKey ${stepKey}`);
|
|
518
|
+
if (index === void 0) return stepIds[stepIds.length - 1];
|
|
519
|
+
return stepIds[index];
|
|
520
|
+
}
|
|
521
|
+
generateStepId(stepKey) {
|
|
522
|
+
const stepIds = this.stepKeyIds.get(stepKey);
|
|
523
|
+
let newStepId;
|
|
524
|
+
let stepIndex = 0;
|
|
525
|
+
if (stepIds) {
|
|
526
|
+
stepIndex = stepIds.length;
|
|
527
|
+
newStepId = `step_${(0, nanoid.nanoid)()}`;
|
|
528
|
+
stepIds.push(newStepId);
|
|
529
|
+
this.stepKeyIds.set(stepKey, stepIds);
|
|
530
|
+
} else {
|
|
531
|
+
newStepId = `step_${(0, nanoid.nanoid)()}`;
|
|
532
|
+
this.stepKeyIds.set(stepKey, [newStepId]);
|
|
533
|
+
}
|
|
534
|
+
return [newStepId, stepIndex];
|
|
535
|
+
}
|
|
536
|
+
getKeyList(metadata) {
|
|
537
|
+
if (!metadata) return [];
|
|
538
|
+
const keyList = this.getInvocationKeyList(metadata);
|
|
539
|
+
const agentContext = this.getAgentContext(metadata);
|
|
540
|
+
if (agentContext.currentTokenType === "think" || agentContext.currentTokenType === "think_and_text") keyList.push("reasoning");
|
|
541
|
+
else if (agentContext.tokenTypeSwitch === "content") keyList.push(`post-reasoning-${agentContext.reasoningTransitionCount}`);
|
|
542
|
+
return keyList;
|
|
543
|
+
}
|
|
544
|
+
getInvocationKeyList(metadata) {
|
|
545
|
+
const keyList = this.getBaseKeyList(metadata);
|
|
546
|
+
if (this.invokedToolIds != null && this.invokedToolIds.size > 0) keyList.push(this.invokedToolIds.size + "");
|
|
547
|
+
return keyList;
|
|
548
|
+
}
|
|
549
|
+
getBaseKeyList(metadata) {
|
|
550
|
+
const configurable = this.config?.configurable;
|
|
551
|
+
const runId = metadata.run_id ?? configurable?.run_id ?? this.runId;
|
|
552
|
+
const threadId = metadata.thread_id ?? configurable?.thread_id ?? runId;
|
|
553
|
+
const checkpointNs = metadata.checkpoint_ns ?? metadata.langgraph_checkpoint_ns ?? "";
|
|
554
|
+
return [
|
|
555
|
+
runId,
|
|
556
|
+
threadId,
|
|
557
|
+
metadata.langgraph_node,
|
|
558
|
+
metadata.langgraph_step,
|
|
559
|
+
checkpointNs
|
|
560
|
+
];
|
|
561
|
+
}
|
|
562
|
+
checkKeyList(keyList) {
|
|
563
|
+
return keyList.some((key) => key === void 0);
|
|
564
|
+
}
|
|
565
|
+
getRunMessages() {
|
|
566
|
+
if (this.messages.length === 0 && this.cachedRunMessages != null) return this.cachedRunMessages;
|
|
567
|
+
return this.messages.slice(this.startIndex);
|
|
568
|
+
}
|
|
569
|
+
getContentParts() {
|
|
570
|
+
return require_core.convertMessagesToContent(this.messages.slice(this.startIndex));
|
|
571
|
+
}
|
|
572
|
+
getCalibrationRatio() {
|
|
573
|
+
return this.agentContexts.get(this.defaultAgentId)?.calibrationRatio ?? 1;
|
|
574
|
+
}
|
|
575
|
+
getResolvedInstructionOverhead() {
|
|
576
|
+
return this.agentContexts.get(this.defaultAgentId)?.resolvedInstructionOverhead;
|
|
577
|
+
}
|
|
578
|
+
getToolCount() {
|
|
579
|
+
const context = this.agentContexts.get(this.defaultAgentId);
|
|
580
|
+
return (context?.tools?.length ?? 0) + (context?.toolDefinitions?.length ?? 0);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Get all run steps, optionally filtered by agent ID
|
|
584
|
+
*/
|
|
585
|
+
getRunSteps(agentId) {
|
|
586
|
+
if (agentId == null || agentId === "") return [...this.contentData];
|
|
587
|
+
return this.contentData.filter((step) => step.agentId === agentId);
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Get run steps grouped by agent ID
|
|
591
|
+
*/
|
|
592
|
+
getRunStepsByAgent() {
|
|
593
|
+
const stepsByAgent = /* @__PURE__ */ new Map();
|
|
594
|
+
for (const step of this.contentData) {
|
|
595
|
+
if (step.agentId == null || step.agentId === "") continue;
|
|
596
|
+
const steps = stepsByAgent.get(step.agentId) ?? [];
|
|
597
|
+
steps.push(step);
|
|
598
|
+
stepsByAgent.set(step.agentId, steps);
|
|
599
|
+
}
|
|
600
|
+
return stepsByAgent;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Get agent IDs that participated in this run
|
|
604
|
+
*/
|
|
605
|
+
getActiveAgentIds() {
|
|
606
|
+
const agentIds = /* @__PURE__ */ new Set();
|
|
607
|
+
for (const step of this.contentData) if (step.agentId != null && step.agentId !== "") agentIds.add(step.agentId);
|
|
608
|
+
return Array.from(agentIds);
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Maps contentPart indices to agent IDs for post-run analysis
|
|
612
|
+
* Returns a map where key is the contentPart index and value is the agentId
|
|
613
|
+
*/
|
|
614
|
+
getContentPartAgentMap() {
|
|
615
|
+
const contentPartAgentMap = /* @__PURE__ */ new Map();
|
|
616
|
+
for (const step of this.contentData) if (step.agentId != null && step.agentId !== "" && Number.isFinite(step.index)) contentPartAgentMap.set(step.index, step.agentId);
|
|
617
|
+
return contentPartAgentMap;
|
|
618
|
+
}
|
|
619
|
+
initializeTools({ currentTools, currentToolMap, agentContext }) {
|
|
620
|
+
const toolDefinitions = agentContext?.toolDefinitions;
|
|
621
|
+
const eventDrivenMode = toolDefinitions != null && toolDefinitions.length > 0;
|
|
622
|
+
const traceToolNode = require_langfuseToolOutputTracing.shouldTraceToolNodeForLangfuse({
|
|
623
|
+
runLangfuse: this.langfuse,
|
|
624
|
+
agentLangfuse: agentContext?.langfuse
|
|
625
|
+
});
|
|
626
|
+
if (eventDrivenMode) {
|
|
627
|
+
const schemaTools = require_schema.createSchemaOnlyTools(toolDefinitions);
|
|
628
|
+
const toolDefMap = new Map(toolDefinitions.map((def) => [def.name, def]));
|
|
629
|
+
const graphTools = agentContext?.graphTools;
|
|
630
|
+
const directToolNames = /* @__PURE__ */ new Set();
|
|
631
|
+
const allTools = [...schemaTools];
|
|
632
|
+
const allToolMap = new Map(schemaTools.map((tool) => [tool.name, tool]));
|
|
633
|
+
if (graphTools && graphTools.length > 0) {
|
|
634
|
+
for (const tool of graphTools) if ("name" in tool) {
|
|
635
|
+
allTools.push(tool);
|
|
636
|
+
allToolMap.set(tool.name, tool);
|
|
637
|
+
directToolNames.add(tool.name);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
const node = new require_ToolNode.ToolNode({
|
|
641
|
+
tools: allTools,
|
|
642
|
+
toolMap: allToolMap,
|
|
643
|
+
trace: traceToolNode,
|
|
644
|
+
runLangfuse: this.langfuse,
|
|
645
|
+
agentLangfuse: agentContext?.langfuse,
|
|
646
|
+
eventDrivenMode: true,
|
|
647
|
+
sessions: this.sessions,
|
|
648
|
+
toolDefinitions: toolDefMap,
|
|
649
|
+
agentId: agentContext?.agentId,
|
|
650
|
+
executingAgentId: agentContext?.agentId,
|
|
651
|
+
toolCallStepIds: this.toolCallStepIds,
|
|
652
|
+
toolRegistry: agentContext?.toolRegistry,
|
|
653
|
+
hookRegistry: this.hookRegistry,
|
|
654
|
+
humanInTheLoop: this.humanInTheLoop,
|
|
655
|
+
eagerEventToolExecution: this.eagerEventToolExecution,
|
|
656
|
+
eagerEventToolExecutions: this.eagerEventToolExecutions,
|
|
657
|
+
eagerEventToolUsageCount: this.getEagerEventToolUsageCount(agentContext?.agentId),
|
|
658
|
+
toolExecution: this.toolExecution,
|
|
659
|
+
directToolNames: directToolNames.size > 0 ? directToolNames : void 0,
|
|
660
|
+
maxContextTokens: agentContext?.maxContextTokens,
|
|
661
|
+
maxToolResultChars: agentContext?.maxToolResultChars,
|
|
662
|
+
toolOutputRegistry: this.getOrCreateToolOutputRegistry(),
|
|
663
|
+
fileCheckpointer: this.getOrCreateFileCheckpointer(),
|
|
664
|
+
errorHandler: (data, metadata) => StandardGraph.handleToolCallErrorStatic(this, data, metadata)
|
|
665
|
+
});
|
|
666
|
+
this.registerCompiledToolNode(node);
|
|
667
|
+
return node;
|
|
668
|
+
}
|
|
669
|
+
const graphTools = agentContext?.graphTools;
|
|
670
|
+
const baseTools = currentTools ?? [];
|
|
671
|
+
const node = new require_ToolNode.ToolNode({
|
|
672
|
+
tools: graphTools && graphTools.length > 0 ? [...baseTools, ...graphTools] : baseTools,
|
|
673
|
+
toolMap: graphTools && graphTools.length > 0 ? new Map([...currentToolMap ?? /* @__PURE__ */ new Map(), ...graphTools.filter((t) => "name" in t).map((t) => [t.name, t])]) : currentToolMap,
|
|
674
|
+
trace: traceToolNode,
|
|
675
|
+
runLangfuse: this.langfuse,
|
|
676
|
+
agentLangfuse: agentContext?.langfuse,
|
|
677
|
+
executingAgentId: agentContext?.agentId,
|
|
678
|
+
toolCallStepIds: this.toolCallStepIds,
|
|
679
|
+
errorHandler: (data, metadata) => StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
680
|
+
toolRegistry: agentContext?.toolRegistry,
|
|
681
|
+
sessions: this.sessions,
|
|
682
|
+
toolExecution: this.toolExecution,
|
|
683
|
+
hookRegistry: this.hookRegistry,
|
|
684
|
+
humanInTheLoop: this.humanInTheLoop,
|
|
685
|
+
maxContextTokens: agentContext?.maxContextTokens,
|
|
686
|
+
maxToolResultChars: agentContext?.maxToolResultChars,
|
|
687
|
+
toolOutputRegistry: this.getOrCreateToolOutputRegistry(),
|
|
688
|
+
fileCheckpointer: this.getOrCreateFileCheckpointer()
|
|
689
|
+
});
|
|
690
|
+
this.registerCompiledToolNode(node);
|
|
691
|
+
return node;
|
|
692
|
+
}
|
|
693
|
+
overrideTestModel(responses, sleep, toolCalls) {
|
|
694
|
+
this.overrideModel = require_fake.createFakeStreamingLLM({
|
|
695
|
+
responses,
|
|
696
|
+
sleep,
|
|
697
|
+
toolCalls
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
getUsageMetadata(finalMessage) {
|
|
701
|
+
if (finalMessage && "usage_metadata" in finalMessage && finalMessage.usage_metadata != null) return finalMessage.usage_metadata;
|
|
702
|
+
}
|
|
703
|
+
cleanupSignalListener(currentModel) {
|
|
704
|
+
if (!this.signal) return;
|
|
705
|
+
const model = this.overrideModel ?? currentModel;
|
|
706
|
+
if (!model) return;
|
|
707
|
+
const client = model?.exposedClient;
|
|
708
|
+
if (!client?.abortHandler) return;
|
|
709
|
+
this.signal.removeEventListener("abort", client.abortHandler);
|
|
710
|
+
client.abortHandler = void 0;
|
|
711
|
+
}
|
|
712
|
+
createCallModel(agentId = "default") {
|
|
713
|
+
return async (state, config) => {
|
|
714
|
+
const agentContext = this.agentContexts.get(agentId);
|
|
715
|
+
if (!agentContext) throw new Error(`Agent context not found for agentId: ${agentId}`);
|
|
716
|
+
if (!config) throw new Error("No config provided");
|
|
717
|
+
const { messages } = state;
|
|
718
|
+
const discoveredNames = require_tools.extractToolDiscoveries(messages);
|
|
719
|
+
if (discoveredNames.length > 0) agentContext.markToolsAsDiscovered(discoveredNames);
|
|
720
|
+
const rawToolsForBinding = require_resolveLocalExecutionTools.resolveLocalToolsForBinding({
|
|
721
|
+
tools: agentContext.getToolsForBinding(),
|
|
722
|
+
toolExecution: this.toolExecution
|
|
723
|
+
});
|
|
724
|
+
/**
|
|
725
|
+
* Anthropic prompt-cache breakpoint on the tool definitions.
|
|
726
|
+
*
|
|
727
|
+
* Without this, the (often static) tool inventory shows up as
|
|
728
|
+
* fresh input on every turn — measured at ~28k tokens/turn for
|
|
729
|
+
* the local engine's coding-tool bundle, dominating per-turn
|
|
730
|
+
* cost even when message-level caching is on.
|
|
731
|
+
*
|
|
732
|
+
* Strategy: partition tools into [static, deferred] and stamp
|
|
733
|
+
* `cache_control: ephemeral` on the last static tool.
|
|
734
|
+
* Discovered deferred tools that arrive across turns sit *after*
|
|
735
|
+
* the breakpoint and don't invalidate the prefix.
|
|
736
|
+
*/
|
|
737
|
+
let toolsForBinding = rawToolsForBinding;
|
|
738
|
+
if (agentContext.provider === "anthropic" && agentContext.clientOptions?.promptCache === true) toolsForBinding = require_anthropicToolCache.partitionAndMarkAnthropicToolCache(rawToolsForBinding, require_anthropicToolCache.makeIsDeferred(agentContext.toolDefinitions)) ?? rawToolsForBinding;
|
|
739
|
+
else if (agentContext.provider === "openrouter" && agentContext.clientOptions?.promptCache === true) toolsForBinding = require_toolCache.partitionAndMarkOpenRouterToolCache(rawToolsForBinding, require_anthropicToolCache.makeIsDeferred(agentContext.toolDefinitions)) ?? rawToolsForBinding;
|
|
740
|
+
else if (agentContext.provider === "bedrock" && agentContext.clientOptions?.promptCache === true) toolsForBinding = require_toolCache$1.partitionAndMarkBedrockToolCache(rawToolsForBinding, require_anthropicToolCache.makeIsDeferred(agentContext.toolDefinitions)) ?? rawToolsForBinding;
|
|
741
|
+
let model = this.overrideModel ?? require_init.initializeModel({
|
|
742
|
+
tools: toolsForBinding,
|
|
743
|
+
provider: agentContext.provider,
|
|
744
|
+
clientOptions: agentContext.clientOptions
|
|
745
|
+
});
|
|
746
|
+
if (agentContext.systemRunnable) model = agentContext.systemRunnable.pipe(model);
|
|
747
|
+
if (agentContext.tokenCalculationPromise) await agentContext.tokenCalculationPromise;
|
|
748
|
+
if (!config.signal) config.signal = this.signal;
|
|
749
|
+
this.config = config;
|
|
750
|
+
let messagesToUse = messages;
|
|
751
|
+
if (!agentContext.pruneMessages && agentContext.tokenCounter && agentContext.maxContextTokens != null) agentContext.pruneMessages = require_prune.createPruneMessages({
|
|
752
|
+
startIndex: agentContext.indexTokenCountMap[0] != null ? this.startIndex : 0,
|
|
753
|
+
provider: agentContext.provider,
|
|
754
|
+
tokenCounter: agentContext.tokenCounter,
|
|
755
|
+
maxTokens: agentContext.maxContextTokens,
|
|
756
|
+
thinkingEnabled: require_request.isThinkingEnabled(agentContext.provider, agentContext.clientOptions),
|
|
757
|
+
indexTokenCountMap: agentContext.indexTokenCountMap,
|
|
758
|
+
contextPruningConfig: agentContext.contextPruningConfig,
|
|
759
|
+
summarizationEnabled: agentContext.summarizationEnabled,
|
|
760
|
+
reserveRatio: agentContext.summarizationConfig?.reserveRatio,
|
|
761
|
+
calibrationRatio: agentContext.calibrationRatio,
|
|
762
|
+
getInstructionTokens: () => agentContext.instructionTokens,
|
|
763
|
+
log: (level, message, data) => {
|
|
764
|
+
require_events.emitAgentLog(config, level, "prune", message, data, {
|
|
765
|
+
runId: this.runId,
|
|
766
|
+
agentId
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
if (agentContext.pruneMessages) {
|
|
771
|
+
const { context, indexTokenCountMap, messagesToRefine, prePruneContextTokens, remainingContextTokens, originalToolContent, calibrationRatio, resolvedInstructionOverhead } = agentContext.pruneMessages({
|
|
772
|
+
messages,
|
|
773
|
+
usageMetadata: agentContext.currentUsage,
|
|
774
|
+
lastCallUsage: agentContext.lastCallUsage,
|
|
775
|
+
totalTokensFresh: agentContext.totalTokensFresh
|
|
776
|
+
});
|
|
777
|
+
agentContext.indexTokenCountMap = indexTokenCountMap;
|
|
778
|
+
if (calibrationRatio != null && calibrationRatio > 0) agentContext.calibrationRatio = calibrationRatio;
|
|
779
|
+
if (resolvedInstructionOverhead != null) {
|
|
780
|
+
agentContext.resolvedInstructionOverhead = resolvedInstructionOverhead;
|
|
781
|
+
const nonToolOverhead = agentContext.instructionTokens - agentContext.toolSchemaTokens;
|
|
782
|
+
const calibratedToolTokens = Math.max(0, resolvedInstructionOverhead - nonToolOverhead);
|
|
783
|
+
const currentToolTokens = agentContext.toolSchemaTokens;
|
|
784
|
+
if ((currentToolTokens > 0 ? Math.abs(calibratedToolTokens - currentToolTokens) / currentToolTokens : 1) > CALIBRATION_VARIANCE_THRESHOLD) agentContext.toolSchemaTokens = calibratedToolTokens;
|
|
785
|
+
}
|
|
786
|
+
messagesToUse = context;
|
|
787
|
+
if (agentContext.summarizationEnabled === true && Array.isArray(messagesToRefine) && messagesToRefine.length > 0) {
|
|
788
|
+
const shouldSkip = agentContext.shouldSkipSummarization(messages.length);
|
|
789
|
+
if (!shouldSkip && require_index$6.shouldTriggerSummarization({
|
|
790
|
+
trigger: agentContext.summarizationConfig?.trigger,
|
|
791
|
+
maxContextTokens: agentContext.maxContextTokens,
|
|
792
|
+
prePruneContextTokens: prePruneContextTokens != null ? prePruneContextTokens + agentContext.instructionTokens : void 0,
|
|
793
|
+
remainingContextTokens,
|
|
794
|
+
messagesToRefineCount: messagesToRefine.length
|
|
795
|
+
})) {
|
|
796
|
+
if (originalToolContent != null && originalToolContent.size > 0)
|
|
797
|
+
/**
|
|
798
|
+
* Merge — never overwrite — the pruner's masking record
|
|
799
|
+
* into pendingOriginalToolContent. Carry-over entries
|
|
800
|
+
* from a prior summarize (preserved by the recency
|
|
801
|
+
* window for masked tool messages still in the tail) and
|
|
802
|
+
* the current pruner's new entries are both keyed by
|
|
803
|
+
* indices in the current `state.messages`, so a key-wise
|
|
804
|
+
* union is correct. Overwriting would discard the
|
|
805
|
+
* carry-over and reduce summary fidelity when those
|
|
806
|
+
* masked tail messages eventually move into the head.
|
|
807
|
+
*/
|
|
808
|
+
if (agentContext.pendingOriginalToolContent == null) agentContext.pendingOriginalToolContent = originalToolContent;
|
|
809
|
+
else {
|
|
810
|
+
for (const [idx, content] of originalToolContent) agentContext.pendingOriginalToolContent.set(idx, content);
|
|
811
|
+
/**
|
|
812
|
+
* Re-apply the per-store char cap after the union. The
|
|
813
|
+
* pruner enforces ORIGINAL_CONTENT_MAX_CHARS inside its
|
|
814
|
+
* own map via the onContentStored callback, but a
|
|
815
|
+
* key-wise merge with recency carry-over bypasses that
|
|
816
|
+
* accounting and could let the merged map grow without
|
|
817
|
+
* bound across long sessions.
|
|
818
|
+
*/
|
|
819
|
+
require_prune.enforceOriginalContentCap(agentContext.pendingOriginalToolContent);
|
|
820
|
+
}
|
|
821
|
+
require_events.emitAgentLog(config, "info", "graph", "Summarization triggered", void 0, {
|
|
822
|
+
runId: this.runId,
|
|
823
|
+
agentId
|
|
824
|
+
});
|
|
825
|
+
require_events.emitAgentLog(config, "debug", "graph", "Summarization trigger details", {
|
|
826
|
+
totalMessages: messages.length,
|
|
827
|
+
remainingContextTokens: remainingContextTokens ?? 0,
|
|
828
|
+
summaryVersion: agentContext.summaryVersion + 1,
|
|
829
|
+
toolSchemaTokens: agentContext.toolSchemaTokens,
|
|
830
|
+
instructionTokens: agentContext.instructionTokens,
|
|
831
|
+
systemMessageTokens: agentContext.systemMessageTokens
|
|
832
|
+
}, {
|
|
833
|
+
runId: this.runId,
|
|
834
|
+
agentId
|
|
835
|
+
});
|
|
836
|
+
agentContext.markSummarizationTriggered(messages.length);
|
|
837
|
+
return { summarizationRequest: {
|
|
838
|
+
remainingContextTokens: remainingContextTokens ?? 0,
|
|
839
|
+
agentId: agentId || agentContext.agentId
|
|
840
|
+
} };
|
|
841
|
+
}
|
|
842
|
+
if (shouldSkip) require_events.emitAgentLog(config, "debug", "graph", "Summarization skipped — no new messages or per-run cap reached", {
|
|
843
|
+
messageCount: messages.length,
|
|
844
|
+
messagesToRefineCount: messagesToRefine.length,
|
|
845
|
+
contextLength: context.length
|
|
846
|
+
}, {
|
|
847
|
+
runId: this.runId,
|
|
848
|
+
agentId
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
let finalMessages = messagesToUse;
|
|
853
|
+
if (agentContext.useLegacyContent) finalMessages = require_content.formatContentStrings(finalMessages);
|
|
854
|
+
const lastMessageX = finalMessages.length >= 2 ? finalMessages[finalMessages.length - 2] : null;
|
|
855
|
+
const lastMessageY = finalMessages.length >= 1 ? finalMessages[finalMessages.length - 1] : null;
|
|
856
|
+
const anthropicLike = require_llm.isAnthropicLike(agentContext.provider, agentContext.clientOptions);
|
|
857
|
+
if (agentContext.provider === "bedrock" && lastMessageX instanceof _langchain_core_messages.AIMessageChunk && lastMessageY instanceof _langchain_core_messages.ToolMessage && typeof lastMessageX.content === "string") {
|
|
858
|
+
const trimmed = lastMessageX.content.trim();
|
|
859
|
+
finalMessages[finalMessages.length - 2].content = trimmed.length > 0 ? [{
|
|
860
|
+
type: "text",
|
|
861
|
+
text: trimmed
|
|
862
|
+
}] : "";
|
|
863
|
+
}
|
|
864
|
+
if (lastMessageY instanceof _langchain_core_messages.ToolMessage) {
|
|
865
|
+
if (anthropicLike) require_core.formatAnthropicArtifactContent(finalMessages);
|
|
866
|
+
else if (require_llm.isOpenAILike(agentContext.provider) && agentContext.provider !== "deepseek" || require_llm.isGoogleLike(agentContext.provider)) require_core.formatArtifactPayload(finalMessages);
|
|
867
|
+
}
|
|
868
|
+
if (agentContext.provider === "anthropic") {
|
|
869
|
+
if (agentContext.clientOptions?.promptCache === true && !agentContext.systemRunnable) finalMessages = require_cache.addCacheControl(finalMessages);
|
|
870
|
+
} else if (agentContext.provider === "bedrock") {
|
|
871
|
+
if (agentContext.clientOptions?.promptCache === true) finalMessages = require_cache.addBedrockCacheControl(finalMessages);
|
|
872
|
+
} else if (agentContext.provider === "openrouter") {
|
|
873
|
+
if (agentContext.clientOptions?.promptCache === true && !agentContext.systemRunnable) finalMessages = require_cache.addCacheControl(finalMessages);
|
|
874
|
+
}
|
|
875
|
+
if (require_request.isThinkingEnabled(agentContext.provider, agentContext.clientOptions))
|
|
876
|
+
/**
|
|
877
|
+
* Pass `this.startIndex` so the function can distinguish CURRENT-run
|
|
878
|
+
* AI messages (the agent's own iterations — possibly without a
|
|
879
|
+
* leading thinking block, which Claude is allowed to skip) from
|
|
880
|
+
* historical context that genuinely needs the
|
|
881
|
+
* `[Previous agent context]` placeholder. Without this signal the
|
|
882
|
+
* function would convert the agent's own in-run tool_use messages,
|
|
883
|
+
* polluting the next iteration's prompt with a placeholder the
|
|
884
|
+
* model treats as suspicious injected content.
|
|
885
|
+
*/
|
|
886
|
+
finalMessages = require_format.ensureThinkingBlockInMessages(finalMessages, agentContext.provider, config, this.startIndex);
|
|
887
|
+
if (anthropicLike && (!agentContext.pruneMessages || finalMessages !== messagesToUse)) {
|
|
888
|
+
const beforeSanitize = finalMessages.length;
|
|
889
|
+
finalMessages = require_prune.sanitizeOrphanToolBlocks(finalMessages);
|
|
890
|
+
if (finalMessages.length !== beforeSanitize) require_events.emitAgentLog(config, "warn", "sanitize", "Orphan tool blocks removed", {
|
|
891
|
+
before: beforeSanitize,
|
|
892
|
+
after: finalMessages.length,
|
|
893
|
+
dropped: beforeSanitize - finalMessages.length
|
|
894
|
+
}, {
|
|
895
|
+
runId: this.runId,
|
|
896
|
+
agentId
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
if (agentContext.lastStreamCall != null && agentContext.streamBuffer != null) {
|
|
900
|
+
const timeSinceLastCall = Date.now() - agentContext.lastStreamCall;
|
|
901
|
+
if (timeSinceLastCall < agentContext.streamBuffer) await require_run.sleep(Math.ceil((agentContext.streamBuffer - timeSinceLastCall) / 1e3) * 1e3);
|
|
902
|
+
}
|
|
903
|
+
agentContext.lastStreamCall = Date.now();
|
|
904
|
+
agentContext.markTokensStale();
|
|
905
|
+
let result;
|
|
906
|
+
const fallbacks = agentContext.clientOptions?.fallbacks ?? [];
|
|
907
|
+
if (finalMessages.length === 0 && !agentContext.hasPendingCompactionSummary()) {
|
|
908
|
+
const budgetBreakdown = agentContext.getTokenBudgetBreakdown(messages);
|
|
909
|
+
const breakdown = agentContext.formatTokenBudgetBreakdown(messages);
|
|
910
|
+
const instructionsExceedBudget = budgetBreakdown.instructionTokens > budgetBreakdown.maxContextTokens;
|
|
911
|
+
let guidance;
|
|
912
|
+
if (instructionsExceedBudget) {
|
|
913
|
+
const toolPct = budgetBreakdown.toolSchemaTokens > 0 ? Math.round(budgetBreakdown.toolSchemaTokens / budgetBreakdown.instructionTokens * 100) : 0;
|
|
914
|
+
guidance = toolPct > 50 ? `Tool definitions consume ${budgetBreakdown.toolSchemaTokens} tokens (${toolPct}% of instructions) across ${budgetBreakdown.toolCount} tools, exceeding maxContextTokens (${budgetBreakdown.maxContextTokens}). Reduce the number of tools or increase maxContextTokens.` : `Instructions (${budgetBreakdown.instructionTokens} tokens) exceed maxContextTokens (${budgetBreakdown.maxContextTokens}). Increase maxContextTokens or shorten the system prompt.`;
|
|
915
|
+
if (agentContext.summarizationEnabled === true) guidance += " Summarization was skipped because the summary would further increase the instruction overhead.";
|
|
916
|
+
} else guidance = "Please increase the context window size or make your message shorter.";
|
|
917
|
+
require_events.emitAgentLog(config, "error", "graph", "Empty messages after pruning", {
|
|
918
|
+
messageCount: messages.length,
|
|
919
|
+
instructionsExceedBudget,
|
|
920
|
+
breakdown
|
|
921
|
+
}, {
|
|
922
|
+
runId: this.runId,
|
|
923
|
+
agentId
|
|
924
|
+
});
|
|
925
|
+
throw new Error(JSON.stringify({
|
|
926
|
+
type: "empty_messages",
|
|
927
|
+
info: `Message pruning removed all messages as none fit in the context window. ${guidance}\n${breakdown}`
|
|
928
|
+
}));
|
|
929
|
+
}
|
|
930
|
+
const invokeStart = Date.now();
|
|
931
|
+
const invokeMeta = {
|
|
932
|
+
runId: this.runId,
|
|
933
|
+
agentId
|
|
934
|
+
};
|
|
935
|
+
require_events.emitAgentLog(config, "debug", "graph", "Invoking LLM", {
|
|
936
|
+
messageCount: finalMessages.length,
|
|
937
|
+
provider: agentContext.provider
|
|
938
|
+
}, invokeMeta, { force: true });
|
|
939
|
+
const langfuse = require_langfuseToolOutputTracing.resolveLangfuseConfig(this.langfuse, agentContext.langfuse);
|
|
940
|
+
const traceMetadata = require_langfuse.createLangfuseTraceMetadata({
|
|
941
|
+
messageId: this.runId,
|
|
942
|
+
parentMessageId: config.configurable?.requestBody?.parentMessageId,
|
|
943
|
+
agentId,
|
|
944
|
+
agentName: agentContext.name
|
|
945
|
+
});
|
|
946
|
+
let langfuseHandler;
|
|
947
|
+
let invokeConfig = {
|
|
948
|
+
...config,
|
|
949
|
+
metadata: {
|
|
950
|
+
...config.metadata ?? {},
|
|
951
|
+
...traceMetadata
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
require_instrumentation.initializeLangfuseTracing(langfuse);
|
|
955
|
+
if (require_callbacks.findCallback(config.callbacks, require_langfuse.isLangfuseCallbackHandler) == null) {
|
|
956
|
+
langfuseHandler = require_langfuse.createLangfuseHandler({
|
|
957
|
+
langfuse,
|
|
958
|
+
userId: config.configurable?.user_id,
|
|
959
|
+
sessionId: config.configurable?.thread_id,
|
|
960
|
+
traceMetadata,
|
|
961
|
+
tags: ["librechat", "agent"]
|
|
962
|
+
});
|
|
963
|
+
if (langfuseHandler != null) invokeConfig = {
|
|
964
|
+
...invokeConfig,
|
|
965
|
+
callbacks: require_callbacks.appendCallbacks(invokeConfig.callbacks, [langfuseHandler])
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
const metadata = config.metadata;
|
|
969
|
+
try {
|
|
970
|
+
result = await require_langfuseToolOutputTracing.withLangfuseToolOutputTracingConfig(this.langfuse, () => require_invoke.attemptInvoke({
|
|
971
|
+
model: this.overrideModel ?? model,
|
|
972
|
+
messages: finalMessages,
|
|
973
|
+
provider: agentContext.provider,
|
|
974
|
+
context: this
|
|
975
|
+
}, invokeConfig), agentContext.langfuse);
|
|
976
|
+
} catch (primaryError) {
|
|
977
|
+
clearCurrentDeltaStepMarkers({
|
|
978
|
+
graph: this,
|
|
979
|
+
metadata
|
|
980
|
+
});
|
|
981
|
+
result = await require_langfuseToolOutputTracing.withLangfuseToolOutputTracingConfig(this.langfuse, () => require_invoke.tryFallbackProviders({
|
|
982
|
+
fallbacks,
|
|
983
|
+
tools: agentContext.tools,
|
|
984
|
+
messages: finalMessages,
|
|
985
|
+
config: invokeConfig,
|
|
986
|
+
primaryError,
|
|
987
|
+
context: this
|
|
988
|
+
}), agentContext.langfuse);
|
|
989
|
+
} finally {
|
|
990
|
+
await require_langfuse.disposeLangfuseHandler(langfuseHandler);
|
|
991
|
+
}
|
|
992
|
+
if (!result) throw new Error("No result after model invocation");
|
|
993
|
+
/**
|
|
994
|
+
* Fallback: populate toolCallStepIds in the graph execution context.
|
|
995
|
+
*
|
|
996
|
+
* When model.stream() is available (the common case), attemptInvoke
|
|
997
|
+
* processes all chunks through a local ChatModelStreamHandler which
|
|
998
|
+
* creates run steps and populates toolCallStepIds before returning.
|
|
999
|
+
* The code below is a fallback for the rare case where model.stream
|
|
1000
|
+
* is unavailable and model.invoke() was used instead.
|
|
1001
|
+
*
|
|
1002
|
+
* Text content is dispatched FIRST so that MESSAGE_CREATION is the
|
|
1003
|
+
* current step when handleToolCalls runs. handleToolCalls then creates
|
|
1004
|
+
* TOOL_CALLS on top of it. The dedup in getMessageId and
|
|
1005
|
+
* toolCallStepIds.has makes this safe when attemptInvoke already
|
|
1006
|
+
* handled everything — both paths become no-ops.
|
|
1007
|
+
*/
|
|
1008
|
+
const responseMessage = result.messages?.[0];
|
|
1009
|
+
const toolCalls = responseMessage?.tool_calls;
|
|
1010
|
+
const hasToolCalls = Array.isArray(toolCalls) && toolCalls.length > 0;
|
|
1011
|
+
const responseReasoningContent = getResponseReasoningContent({
|
|
1012
|
+
responseMessage,
|
|
1013
|
+
reasoningKey: agentContext.reasoningKey
|
|
1014
|
+
});
|
|
1015
|
+
const textMessageContent = getMessageDeltaContent(agentContext.provider, responseMessage?.content);
|
|
1016
|
+
const hasStreamedTextDeltaStep = hasCurrentTextDeltaStep({
|
|
1017
|
+
graph: this,
|
|
1018
|
+
metadata
|
|
1019
|
+
});
|
|
1020
|
+
const dispatchableFinalReasoningContent = getDispatchableFinalReasoningContent({
|
|
1021
|
+
agentContext,
|
|
1022
|
+
responseReasoningContent,
|
|
1023
|
+
hasStreamedTextDeltaStep,
|
|
1024
|
+
hasStreamedReasoningDeltaStep: hasCurrentReasoningDeltaStep({
|
|
1025
|
+
graph: this,
|
|
1026
|
+
metadata
|
|
1027
|
+
})
|
|
1028
|
+
});
|
|
1029
|
+
if (hasToolCalls) {
|
|
1030
|
+
if (dispatchableFinalReasoningContent != null && await dispatchReasoningContent({
|
|
1031
|
+
graph: this,
|
|
1032
|
+
agentContext,
|
|
1033
|
+
reasoningContent: dispatchableFinalReasoningContent,
|
|
1034
|
+
metadata
|
|
1035
|
+
})) markPostReasoningContent(agentContext);
|
|
1036
|
+
if (textMessageContent != null && !hasStreamedTextDeltaStep) {
|
|
1037
|
+
const stepKey = this.getStepKey(metadata);
|
|
1038
|
+
if (await dispatchTextMessageContent({
|
|
1039
|
+
graph: this,
|
|
1040
|
+
stepKey,
|
|
1041
|
+
provider: agentContext.provider,
|
|
1042
|
+
content: textMessageContent,
|
|
1043
|
+
metadata
|
|
1044
|
+
})) markPostReasoningContent(agentContext);
|
|
1045
|
+
}
|
|
1046
|
+
await require_handlers.handleToolCalls(toolCalls, metadata, this);
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* When streaming events are unavailable, ChatModelStreamHandler never
|
|
1050
|
+
* fires. Dispatch final reasoning/text content here. getMessageId makes
|
|
1051
|
+
* this a no-op when the streaming path already handled the same step.
|
|
1052
|
+
*/
|
|
1053
|
+
if (!hasToolCalls && responseMessage != null) {
|
|
1054
|
+
if (dispatchableFinalReasoningContent != null && await dispatchReasoningContent({
|
|
1055
|
+
graph: this,
|
|
1056
|
+
agentContext,
|
|
1057
|
+
reasoningContent: dispatchableFinalReasoningContent,
|
|
1058
|
+
metadata
|
|
1059
|
+
}) && textMessageContent != null) markPostReasoningContent(agentContext);
|
|
1060
|
+
if (textMessageContent != null && !hasStreamedTextDeltaStep) {
|
|
1061
|
+
const stepKey = this.getStepKey(metadata);
|
|
1062
|
+
await dispatchTextMessageContent({
|
|
1063
|
+
graph: this,
|
|
1064
|
+
stepKey,
|
|
1065
|
+
provider: agentContext.provider,
|
|
1066
|
+
content: textMessageContent,
|
|
1067
|
+
metadata
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
const invokeElapsed = ((Date.now() - invokeStart) / 1e3).toFixed(2);
|
|
1072
|
+
agentContext.currentUsage = this.getUsageMetadata(result.messages?.[0]);
|
|
1073
|
+
if (agentContext.currentUsage) {
|
|
1074
|
+
agentContext.updateLastCallUsage(agentContext.currentUsage);
|
|
1075
|
+
require_events.emitAgentLog(config, "debug", "graph", `LLM call complete (${invokeElapsed}s)`, {
|
|
1076
|
+
...agentContext.currentUsage,
|
|
1077
|
+
elapsedSeconds: Number(invokeElapsed),
|
|
1078
|
+
instructionTokens: agentContext.instructionTokens,
|
|
1079
|
+
toolSchemaTokens: agentContext.toolSchemaTokens,
|
|
1080
|
+
messageCount: finalMessages.length
|
|
1081
|
+
}, invokeMeta, { force: true });
|
|
1082
|
+
} else require_events.emitAgentLog(config, "debug", "graph", `LLM call complete (${invokeElapsed}s)`, {
|
|
1083
|
+
elapsedSeconds: Number(invokeElapsed),
|
|
1084
|
+
messageCount: finalMessages.length
|
|
1085
|
+
}, invokeMeta, { force: true });
|
|
1086
|
+
this.cleanupSignalListener();
|
|
1087
|
+
return result;
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
createAgentNode(agentId) {
|
|
1091
|
+
const getConfig = () => this.config;
|
|
1092
|
+
const agentContext = this.agentContexts.get(agentId);
|
|
1093
|
+
if (!agentContext) throw new Error(`Agent context not found for agentId: ${agentId}`);
|
|
1094
|
+
/**
|
|
1095
|
+
* Depth countdown across graph boundaries: the parent's `maxSubagentDepth`
|
|
1096
|
+
* becomes this executor's `maxDepth`. When the child graph is constructed,
|
|
1097
|
+
* `buildChildInputs()` decrements `maxSubagentDepth` on the child's
|
|
1098
|
+
* `AgentInputs` (only when `allowNested: true`; otherwise subagentConfigs
|
|
1099
|
+
* are stripped entirely). The child graph's own `createAgentNode()` then
|
|
1100
|
+
* reads the decremented value here and creates a narrower executor —
|
|
1101
|
+
* recursion is bounded even though each graph has its own separate
|
|
1102
|
+
* executor instance.
|
|
1103
|
+
*/
|
|
1104
|
+
const effectiveSubagentDepth = agentContext.maxSubagentDepth ?? 1;
|
|
1105
|
+
if (agentContext.subagentConfigs != null && agentContext.subagentConfigs.length > 0 && effectiveSubagentDepth > 0) {
|
|
1106
|
+
const resolvedConfigs = require_SubagentExecutor.resolveSubagentConfigs(agentContext.subagentConfigs, agentContext);
|
|
1107
|
+
if (resolvedConfigs.length > 0) {
|
|
1108
|
+
const getParentHandlerRegistry = () => this.handlerRegistry;
|
|
1109
|
+
const executor = new require_SubagentExecutor.SubagentExecutor({
|
|
1110
|
+
configs: new Map(resolvedConfigs.map((c) => [c.type, c])),
|
|
1111
|
+
parentSignal: this.signal,
|
|
1112
|
+
hookRegistry: this.hookRegistry,
|
|
1113
|
+
/** Lazy — Run wires the registry onto the graph AFTER
|
|
1114
|
+
* `createWorkflow()` runs, so a direct capture here would be
|
|
1115
|
+
* `undefined` at construction time. */
|
|
1116
|
+
parentHandlerRegistry: getParentHandlerRegistry,
|
|
1117
|
+
parentRunId: this.runId ?? "",
|
|
1118
|
+
parentAgentId: agentContext.agentId,
|
|
1119
|
+
langfuse: this.langfuse,
|
|
1120
|
+
tokenCounter: agentContext.tokenCounter,
|
|
1121
|
+
maxDepth: effectiveSubagentDepth,
|
|
1122
|
+
createChildGraph: (input) => {
|
|
1123
|
+
const childGraph = new StandardGraph(input);
|
|
1124
|
+
childGraph.hookRegistry = this.hookRegistry;
|
|
1125
|
+
/**
|
|
1126
|
+
* Do not propagate `humanInTheLoop` into the child graph yet:
|
|
1127
|
+
* nested subagent interrupts need a stable child checkpoint and
|
|
1128
|
+
* resume bridge. Child hooks still fire; `ask` decisions fail
|
|
1129
|
+
* closed inside the subagent until that flow is implemented.
|
|
1130
|
+
*/
|
|
1131
|
+
childGraph.toolOutputReferences = this.toolOutputReferences;
|
|
1132
|
+
childGraph.eagerEventToolExecution = this.eagerEventToolExecution;
|
|
1133
|
+
childGraph.toolExecution = this.toolExecution;
|
|
1134
|
+
childGraph.eventToolExecutionAvailable = this.handlerRegistry?.getHandler("on_tool_execute") != null;
|
|
1135
|
+
return childGraph;
|
|
1136
|
+
}
|
|
1137
|
+
});
|
|
1138
|
+
const subagentTool = (0, _langchain_core_tools.tool)(async (rawInput, config) => {
|
|
1139
|
+
const input = rawInput;
|
|
1140
|
+
const description = typeof input.description === "string" && input.description.trim().length > 0 ? input.description : "No task description provided";
|
|
1141
|
+
const subagentType = typeof input.subagent_type === "string" ? input.subagent_type : "";
|
|
1142
|
+
const threadId = config.configurable?.thread_id;
|
|
1143
|
+
/**
|
|
1144
|
+
* When the tool is dispatched from an LLM's `tool_call`, LangChain
|
|
1145
|
+
* threads the originating `ToolCall` onto the RunnableConfig as
|
|
1146
|
+
* `config.toolCall` (see `ToolRunnableConfig` in
|
|
1147
|
+
* `@langchain/core/tools` — internal but stable since ≥0.3.x).
|
|
1148
|
+
* Surfacing its id lets hosts correlate `SubagentUpdateEvent`s
|
|
1149
|
+
* back to the parent's `tool_call_id` deterministically — no
|
|
1150
|
+
* temporal heuristics needed. If a future LangChain version
|
|
1151
|
+
* changes the threading, the type-guarded read falls back to
|
|
1152
|
+
* `undefined` and the correlation degrades gracefully.
|
|
1153
|
+
*/
|
|
1154
|
+
const toolCall = config.toolCall;
|
|
1155
|
+
const parentToolCallId = typeof toolCall?.id === "string" ? toolCall.id : void 0;
|
|
1156
|
+
return (await executor.execute({
|
|
1157
|
+
description,
|
|
1158
|
+
subagentType,
|
|
1159
|
+
threadId,
|
|
1160
|
+
parentToolCallId,
|
|
1161
|
+
/**
|
|
1162
|
+
* Forward the parent's `configurable` so host-set fields
|
|
1163
|
+
* (`requestBody`, `user`, etc.) propagate into the child
|
|
1164
|
+
* workflow. The executor scrubs run-identity fields before
|
|
1165
|
+
* forwarding — see `SubagentExecuteParams.parentConfigurable`.
|
|
1166
|
+
*/
|
|
1167
|
+
parentConfigurable: config.configurable
|
|
1168
|
+
})).content;
|
|
1169
|
+
}, require_SubagentTool.buildSubagentToolParams(resolvedConfigs));
|
|
1170
|
+
if (!agentContext.graphTools) agentContext.graphTools = [];
|
|
1171
|
+
agentContext.graphTools.push(subagentTool);
|
|
1172
|
+
/**
|
|
1173
|
+
* Refresh toolSchemaTokens to include the subagent tool's schema.
|
|
1174
|
+
* `calculateInstructionTokens()` was kicked off in `fromConfig()`
|
|
1175
|
+
* before graphTools was populated, so its result did not count this
|
|
1176
|
+
* tool. Without this retrigger, token-budget/pruning logic
|
|
1177
|
+
* underestimates prompt overhead.
|
|
1178
|
+
*/
|
|
1179
|
+
if (agentContext.tokenCounter) {
|
|
1180
|
+
const { tokenCounter, baseIndexTokenCountMap } = agentContext;
|
|
1181
|
+
agentContext.tokenCalculationPromise = agentContext.calculateInstructionTokens(tokenCounter).then(() => {
|
|
1182
|
+
agentContext.updateTokenMapWithInstructions(baseIndexTokenCountMap);
|
|
1183
|
+
}).catch((err) => {
|
|
1184
|
+
console.error("Error recalculating instruction tokens after subagent tool injection:", err);
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
const agentNode = `${AGENT}${agentId}`;
|
|
1190
|
+
const toolNode = `${TOOLS}${agentId}`;
|
|
1191
|
+
const summarizeNode = `${SUMMARIZE}${agentId}`;
|
|
1192
|
+
const routeMessage = (state, config) => {
|
|
1193
|
+
this.config = config;
|
|
1194
|
+
if (state.summarizationRequest != null) return summarizeNode;
|
|
1195
|
+
return require_ToolNode.toolsCondition(state, toolNode, this.invokedToolIds);
|
|
1196
|
+
};
|
|
1197
|
+
return new _langchain_langgraph.StateGraph(_langchain_langgraph.Annotation.Root({
|
|
1198
|
+
messages: (0, _langchain_langgraph.Annotation)({
|
|
1199
|
+
reducer: require_reducer.messagesStateReducer,
|
|
1200
|
+
default: () => []
|
|
1201
|
+
}),
|
|
1202
|
+
summarizationRequest: (0, _langchain_langgraph.Annotation)({
|
|
1203
|
+
reducer: (_, b) => b,
|
|
1204
|
+
default: () => void 0
|
|
1205
|
+
})
|
|
1206
|
+
})).addNode(agentNode, this.createCallModel(agentId)).addNode(toolNode, this.initializeTools({
|
|
1207
|
+
currentTools: agentContext.tools,
|
|
1208
|
+
currentToolMap: agentContext.toolMap,
|
|
1209
|
+
agentContext
|
|
1210
|
+
})).addNode(summarizeNode, require_node.createSummarizeNode({
|
|
1211
|
+
agentContext,
|
|
1212
|
+
graph: {
|
|
1213
|
+
contentData: this.contentData,
|
|
1214
|
+
contentIndexMap: this.contentIndexMap,
|
|
1215
|
+
get config() {
|
|
1216
|
+
return getConfig();
|
|
1217
|
+
},
|
|
1218
|
+
runId: this.runId,
|
|
1219
|
+
isMultiAgent: this.isMultiAgentGraph(),
|
|
1220
|
+
hookRegistry: this.hookRegistry,
|
|
1221
|
+
dispatchRunStep: async (runStep, nodeConfig) => {
|
|
1222
|
+
this.contentData.push(runStep);
|
|
1223
|
+
this.contentIndexMap.set(runStep.id, runStep.index);
|
|
1224
|
+
const resolvedConfig = nodeConfig ?? this.config;
|
|
1225
|
+
const handler = this.handlerRegistry?.getHandler("on_run_step");
|
|
1226
|
+
if (handler) {
|
|
1227
|
+
await handler.handle("on_run_step", runStep, resolvedConfig?.configurable, this);
|
|
1228
|
+
this.handlerDispatchedStepIds.add(runStep.id);
|
|
1229
|
+
}
|
|
1230
|
+
const unmarkHandlerDispatchedEvent = handler ? this.markHandlerDispatchedEvent("on_run_step", runStep.id) : void 0;
|
|
1231
|
+
try {
|
|
1232
|
+
if (resolvedConfig) await require_events.safeDispatchCustomEvent("on_run_step", runStep, resolvedConfig);
|
|
1233
|
+
} finally {
|
|
1234
|
+
unmarkHandlerDispatchedEvent?.();
|
|
1235
|
+
}
|
|
1236
|
+
},
|
|
1237
|
+
dispatchRunStepCompleted: async (stepId, result, nodeConfig) => {
|
|
1238
|
+
const resolvedConfig = nodeConfig ?? this.config;
|
|
1239
|
+
const runStep = this.contentData.find((s) => s.id === stepId);
|
|
1240
|
+
const handler = this.handlerRegistry?.getHandler("on_run_step_completed");
|
|
1241
|
+
if (handler) await handler.handle("on_run_step_completed", { result: {
|
|
1242
|
+
...result,
|
|
1243
|
+
id: stepId,
|
|
1244
|
+
index: runStep?.index ?? 0
|
|
1245
|
+
} }, resolvedConfig?.configurable, this);
|
|
1246
|
+
}
|
|
1247
|
+
},
|
|
1248
|
+
generateStepId: (stepKey) => this.generateStepId(stepKey)
|
|
1249
|
+
})).addEdge(_langchain_langgraph.START, agentNode).addConditionalEdges(agentNode, routeMessage).addEdge(summarizeNode, agentNode).addEdge(toolNode, agentContext.toolEnd ? _langchain_langgraph.END : agentNode).compile();
|
|
1250
|
+
}
|
|
1251
|
+
createWorkflow() {
|
|
1252
|
+
const agentNode = this.createAgentNode(this.defaultAgentId);
|
|
1253
|
+
return new _langchain_langgraph.StateGraph(_langchain_langgraph.Annotation.Root({ messages: (0, _langchain_langgraph.Annotation)({
|
|
1254
|
+
reducer: (a, b) => {
|
|
1255
|
+
if (!this.messages.length) this.startIndex = a.length + b.length;
|
|
1256
|
+
const result = require_reducer.messagesStateReducer(a, b);
|
|
1257
|
+
this.messages = result;
|
|
1258
|
+
return result;
|
|
1259
|
+
},
|
|
1260
|
+
default: () => []
|
|
1261
|
+
}) })).addNode(this.defaultAgentId, agentNode, { ends: [_langchain_langgraph.END] }).addEdge(_langchain_langgraph.START, this.defaultAgentId).compile(this.compileOptions);
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Indicates if this is a multi-agent graph.
|
|
1265
|
+
* Override in MultiAgentGraph to return true.
|
|
1266
|
+
* Used to conditionally include agentId in RunStep for frontend rendering.
|
|
1267
|
+
*/
|
|
1268
|
+
isMultiAgentGraph() {
|
|
1269
|
+
return false;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Get the parallel group ID for an agent, if any.
|
|
1273
|
+
* Override in MultiAgentGraph to provide actual group IDs.
|
|
1274
|
+
* Group IDs are incrementing numbers (1, 2, 3...) reflecting execution order.
|
|
1275
|
+
* @param _agentId - The agent ID to look up
|
|
1276
|
+
* @returns undefined for StandardGraph (no parallel groups), or group number for MultiAgentGraph
|
|
1277
|
+
*/
|
|
1278
|
+
getParallelGroupIdForAgent(_agentId) {}
|
|
1279
|
+
/**
|
|
1280
|
+
* Dispatches a run step to the client, returns the step ID
|
|
1281
|
+
*/
|
|
1282
|
+
async dispatchRunStep(stepKey, stepDetails, metadata) {
|
|
1283
|
+
if (!this.config) throw new Error("No config provided");
|
|
1284
|
+
const [stepId, stepIndex] = this.generateStepId(stepKey);
|
|
1285
|
+
if (stepDetails.type === "tool_calls" && stepDetails.tool_calls) for (const tool_call of stepDetails.tool_calls) {
|
|
1286
|
+
const toolCallId = tool_call.id ?? "";
|
|
1287
|
+
if (!toolCallId || this.toolCallStepIds.has(toolCallId)) continue;
|
|
1288
|
+
this.toolCallStepIds.set(toolCallId, stepId);
|
|
1289
|
+
}
|
|
1290
|
+
const runStep = {
|
|
1291
|
+
stepIndex,
|
|
1292
|
+
id: stepId,
|
|
1293
|
+
type: stepDetails.type,
|
|
1294
|
+
index: this.contentData.length,
|
|
1295
|
+
stepDetails,
|
|
1296
|
+
usage: null
|
|
1297
|
+
};
|
|
1298
|
+
const runId = this.runId ?? "";
|
|
1299
|
+
if (runId) runStep.runId = runId;
|
|
1300
|
+
if (metadata) try {
|
|
1301
|
+
const agentContext = this.getAgentContext(metadata);
|
|
1302
|
+
if (this.isMultiAgentGraph() && agentContext.agentId) {
|
|
1303
|
+
runStep.agentId = agentContext.agentId;
|
|
1304
|
+
const groupId = this.getParallelGroupIdForAgent(agentContext.agentId);
|
|
1305
|
+
if (groupId != null) runStep.groupId = groupId;
|
|
1306
|
+
}
|
|
1307
|
+
} catch (_e) {}
|
|
1308
|
+
this.contentData.push(runStep);
|
|
1309
|
+
this.contentIndexMap.set(stepId, runStep.index);
|
|
1310
|
+
const handler = this.handlerRegistry?.getHandler("on_run_step");
|
|
1311
|
+
if (handler) {
|
|
1312
|
+
await handler.handle("on_run_step", runStep, metadata, this);
|
|
1313
|
+
this.handlerDispatchedStepIds.add(stepId);
|
|
1314
|
+
}
|
|
1315
|
+
const unmarkHandlerDispatchedEvent = handler ? this.markHandlerDispatchedEvent("on_run_step", stepId) : void 0;
|
|
1316
|
+
try {
|
|
1317
|
+
await require_events.safeDispatchCustomEvent("on_run_step", runStep, this.config);
|
|
1318
|
+
} finally {
|
|
1319
|
+
unmarkHandlerDispatchedEvent?.();
|
|
1320
|
+
}
|
|
1321
|
+
return stepId;
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Static version of handleToolCallError to avoid creating strong references
|
|
1325
|
+
* that prevent garbage collection
|
|
1326
|
+
*/
|
|
1327
|
+
static async handleToolCallErrorStatic(graph, data, metadata) {
|
|
1328
|
+
if (!graph.config) throw new Error("No config provided");
|
|
1329
|
+
if (!data.id) {
|
|
1330
|
+
console.warn("No Tool ID provided for Tool Error");
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
const stepId = graph.toolCallStepIds.get(data.id) ?? "";
|
|
1334
|
+
if (!stepId) throw new Error(`No stepId found for tool_call_id ${data.id}`);
|
|
1335
|
+
const { name, input: args, error } = data;
|
|
1336
|
+
const runStep = graph.getRunStep(stepId);
|
|
1337
|
+
if (!runStep) throw new Error(`No run step found for stepId ${stepId}`);
|
|
1338
|
+
const tool_call = {
|
|
1339
|
+
id: data.id,
|
|
1340
|
+
name: name || "",
|
|
1341
|
+
args: typeof args === "string" ? args : JSON.stringify(args),
|
|
1342
|
+
output: `Error processing tool${error?.message != null ? `: ${error.message}` : ""}`,
|
|
1343
|
+
progress: 1
|
|
1344
|
+
};
|
|
1345
|
+
await graph.handlerRegistry?.getHandler("on_run_step_completed")?.handle("on_run_step_completed", { result: {
|
|
1346
|
+
id: stepId,
|
|
1347
|
+
index: runStep.index,
|
|
1348
|
+
type: "tool_call",
|
|
1349
|
+
tool_call
|
|
1350
|
+
} }, metadata, graph);
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Instance method that delegates to the static method
|
|
1354
|
+
* Kept for backward compatibility
|
|
1355
|
+
*/
|
|
1356
|
+
async handleToolCallError(data, metadata) {
|
|
1357
|
+
await StandardGraph.handleToolCallErrorStatic(this, data, metadata);
|
|
1358
|
+
}
|
|
1359
|
+
async dispatchRunStepDelta(id, delta, metadata) {
|
|
1360
|
+
if (!this.config) throw new Error("No config provided");
|
|
1361
|
+
else if (!id) throw new Error("No step ID found");
|
|
1362
|
+
const runStepDelta = {
|
|
1363
|
+
id,
|
|
1364
|
+
delta
|
|
1365
|
+
};
|
|
1366
|
+
const handler = this.handlerRegistry?.getHandler("on_run_step_delta");
|
|
1367
|
+
if (handler) {
|
|
1368
|
+
await handler.handle("on_run_step_delta", runStepDelta, metadata, this);
|
|
1369
|
+
this.handlerDispatchedStepIds.add(id);
|
|
1370
|
+
}
|
|
1371
|
+
const unmarkHandlerDispatchedEvent = handler ? this.markHandlerDispatchedEvent("on_run_step_delta", id) : void 0;
|
|
1372
|
+
try {
|
|
1373
|
+
await require_events.safeDispatchCustomEvent("on_run_step_delta", runStepDelta, this.config);
|
|
1374
|
+
} finally {
|
|
1375
|
+
unmarkHandlerDispatchedEvent?.();
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
async dispatchMessageDelta(id, delta, metadata) {
|
|
1379
|
+
if (!this.config) throw new Error("No config provided");
|
|
1380
|
+
const messageDelta = {
|
|
1381
|
+
id,
|
|
1382
|
+
delta
|
|
1383
|
+
};
|
|
1384
|
+
if (hasTextDeltaContent(delta.content)) this.messageStepHasTextDeltas.add(id);
|
|
1385
|
+
const handler = this.handlerRegistry?.getHandler("on_message_delta");
|
|
1386
|
+
if (handler) {
|
|
1387
|
+
await handler.handle("on_message_delta", messageDelta, metadata, this);
|
|
1388
|
+
this.handlerDispatchedStepIds.add(id);
|
|
1389
|
+
}
|
|
1390
|
+
const unmarkHandlerDispatchedEvent = handler ? this.markHandlerDispatchedEvent("on_message_delta", id) : void 0;
|
|
1391
|
+
try {
|
|
1392
|
+
await require_events.safeDispatchCustomEvent("on_message_delta", messageDelta, this.config);
|
|
1393
|
+
} finally {
|
|
1394
|
+
unmarkHandlerDispatchedEvent?.();
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
dispatchReasoningDelta = async (stepId, delta, metadata) => {
|
|
1398
|
+
if (!this.config) throw new Error("No config provided");
|
|
1399
|
+
const reasoningDelta = {
|
|
1400
|
+
id: stepId,
|
|
1401
|
+
delta
|
|
1402
|
+
};
|
|
1403
|
+
if (hasReasoningDeltaContent(delta.content)) this.reasoningStepHasDeltas.add(stepId);
|
|
1404
|
+
const handler = this.handlerRegistry?.getHandler("on_reasoning_delta");
|
|
1405
|
+
if (handler) {
|
|
1406
|
+
await handler.handle("on_reasoning_delta", reasoningDelta, metadata, this);
|
|
1407
|
+
this.handlerDispatchedStepIds.add(stepId);
|
|
1408
|
+
}
|
|
1409
|
+
const unmarkHandlerDispatchedEvent = handler ? this.markHandlerDispatchedEvent("on_reasoning_delta", stepId) : void 0;
|
|
1410
|
+
try {
|
|
1411
|
+
await require_events.safeDispatchCustomEvent("on_reasoning_delta", reasoningDelta, this.config);
|
|
1412
|
+
} finally {
|
|
1413
|
+
unmarkHandlerDispatchedEvent?.();
|
|
1414
|
+
}
|
|
1415
|
+
};
|
|
1416
|
+
};
|
|
1417
|
+
//#endregion
|
|
1837
1418
|
exports.Graph = Graph;
|
|
1838
1419
|
exports.StandardGraph = StandardGraph;
|
|
1839
|
-
|
|
1420
|
+
|
|
1421
|
+
//# sourceMappingURL=Graph.cjs.map
|