@librechat/agents 3.4.6 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/AgentContext.cjs +0 -356
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/agents/projection.cjs +0 -9
- package/dist/cjs/agents/projection.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +0 -26
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +0 -85
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/events.cjs +0 -14
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +1 -909
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +7 -272
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/applyGraphRuntimeConfig.cjs.map +1 -1
- package/dist/cjs/graphs/createGraph.cjs.map +1 -1
- package/dist/cjs/hitl/askUserQuestion.cjs +0 -53
- package/dist/cjs/hitl/askUserQuestion.cjs.map +1 -1
- package/dist/cjs/hitl/askUserQuestions.cjs +0 -20
- package/dist/cjs/hitl/askUserQuestions.cjs.map +1 -1
- package/dist/cjs/hitl/askUserQuestionsInterrupt.cjs +0 -6
- package/dist/cjs/hitl/askUserQuestionsInterrupt.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +0 -127
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/createToolPolicyHook.cjs +0 -38
- package/dist/cjs/hooks/createToolPolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +0 -77
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +0 -79
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +0 -16
- package/dist/cjs/hooks/index.cjs.map +1 -1
- package/dist/cjs/hooks/matchers.cjs +0 -104
- package/dist/cjs/hooks/matchers.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +0 -8
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/instrumentation.cjs +0 -14
- package/dist/cjs/instrumentation.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +0 -75
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/langfuseConfig.cjs.map +1 -1
- package/dist/cjs/langfuseRuntimeContext.cjs +0 -13
- package/dist/cjs/langfuseRuntimeContext.cjs.map +1 -1
- package/dist/cjs/langfuseRuntimeScope.cjs +0 -4
- package/dist/cjs/langfuseRuntimeScope.cjs.map +1 -1
- package/dist/cjs/langfuseSpanRegistry.cjs +0 -28
- package/dist/cjs/langfuseSpanRegistry.cjs.map +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs +0 -3
- package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
- package/dist/cjs/langfuseTraceShaping.cjs +0 -74
- package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +0 -3
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -111
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +0 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/stream_events.cjs +0 -8
- package/dist/cjs/llm/anthropic/utils/stream_events.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/cachePoints.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +1 -95
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +0 -78
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +0 -24
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/contextOverflowRecovery.cjs +0 -62
- package/dist/cjs/llm/contextOverflowRecovery.cjs.map +1 -1
- package/dist/cjs/llm/fake.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +2 -21
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +5 -35
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/llm/init.cjs +0 -9
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +1 -270
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/mistral/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +0 -99
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/streamMetadata.cjs +0 -21
- package/dist/cjs/llm/openai/streamMetadata.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +0 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/toolCache.cjs +0 -6
- package/dist/cjs/llm/openrouter/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/preempt.cjs +0 -75
- package/dist/cjs/llm/preempt.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/request.cjs +0 -10
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/llm/stream/chunkAdapters.cjs +0 -62
- package/dist/cjs/llm/stream/chunkAdapters.cjs.map +1 -1
- package/dist/cjs/llm/stream/smoother.cjs +0 -58
- package/dist/cjs/llm/stream/smoother.cjs.map +1 -1
- package/dist/cjs/llm/streamLimits.cjs +4 -293
- package/dist/cjs/llm/streamLimits.cjs.map +1 -1
- package/dist/cjs/llm/truncation.cjs +0 -36
- package/dist/cjs/llm/truncation.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +0 -330
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/messages/alternation.cjs +0 -43
- package/dist/cjs/messages/alternation.cjs.map +1 -1
- package/dist/cjs/messages/anthropicToolCache.cjs +0 -45
- package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
- package/dist/cjs/messages/assistantPhase.cjs +0 -12
- package/dist/cjs/messages/assistantPhase.cjs.map +1 -1
- package/dist/cjs/messages/budget.cjs +0 -9
- package/dist/cjs/messages/budget.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +0 -172
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/content.cjs +0 -9
- package/dist/cjs/messages/content.cjs.map +1 -1
- package/dist/cjs/messages/contextPruning.cjs +0 -15
- package/dist/cjs/messages/contextPruning.cjs.map +1 -1
- package/dist/cjs/messages/contextPruningSettings.cjs +0 -3
- package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +0 -48
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +1 -345
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/handoffCue.cjs +0 -36
- package/dist/cjs/messages/handoffCue.cjs.map +1 -1
- package/dist/cjs/messages/ids.cjs.map +1 -1
- package/dist/cjs/messages/injected.cjs +0 -33
- package/dist/cjs/messages/injected.cjs.map +1 -1
- package/dist/cjs/messages/langchain.cjs +0 -14
- package/dist/cjs/messages/langchain.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +1 -184
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/messages/recency.cjs +0 -32
- package/dist/cjs/messages/recency.cjs.map +1 -1
- package/dist/cjs/messages/reducer.cjs +0 -30
- package/dist/cjs/messages/reducer.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +0 -14
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/openai/index.cjs.map +1 -1
- package/dist/cjs/prompts/activityLabel.cjs +9 -79
- package/dist/cjs/prompts/activityLabel.cjs.map +1 -1
- package/dist/cjs/responses/index.cjs.map +1 -1
- package/dist/cjs/run.cjs +2 -538
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +1 -17
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs.map +1 -1
- package/dist/cjs/session/handlers.cjs +0 -7
- package/dist/cjs/session/handlers.cjs.map +1 -1
- package/dist/cjs/session/ids.cjs.map +1 -1
- package/dist/cjs/session/messageSerialization.cjs +0 -4
- package/dist/cjs/session/messageSerialization.cjs.map +1 -1
- package/dist/cjs/stream.cjs +1 -93
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/index.cjs +0 -27
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +0 -273
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +0 -35
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +0 -24
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +0 -13
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/CodeSessionFileSummary.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +0 -101
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs.map +1 -1
- package/dist/cjs/tools/SkillTool.cjs +0 -5
- package/dist/cjs/tools/SkillTool.cjs.map +1 -1
- package/dist/cjs/tools/SubagentTool.cjs +0 -10
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +6 -1038
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +0 -179
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs +0 -39
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs.map +1 -1
- package/dist/cjs/tools/eagerEventExecution.cjs +0 -6
- package/dist/cjs/tools/eagerEventExecution.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +1 -41
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/intentArg.cjs +1 -132
- package/dist/cjs/tools/intentArg.cjs.map +1 -1
- package/dist/cjs/tools/local/CompileCheckTool.cjs +0 -25
- package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -1
- package/dist/cjs/tools/local/FileCheckpointer.cjs +0 -23
- package/dist/cjs/tools/local/FileCheckpointer.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +0 -80
- package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +0 -126
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +0 -12
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/local/attachments.cjs +0 -44
- package/dist/cjs/tools/local/attachments.cjs.map +1 -1
- package/dist/cjs/tools/local/bashAst.cjs +0 -16
- package/dist/cjs/tools/local/bashAst.cjs.map +1 -1
- package/dist/cjs/tools/local/editStrategies.cjs +0 -14
- package/dist/cjs/tools/local/editStrategies.cjs.map +1 -1
- package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/syntaxCheck.cjs +0 -31
- package/dist/cjs/tools/local/syntaxCheck.cjs.map +1 -1
- package/dist/cjs/tools/local/textEncoding.cjs +0 -11
- package/dist/cjs/tools/local/textEncoding.cjs.map +1 -1
- package/dist/cjs/tools/local/workspaceFS.cjs +0 -33
- package/dist/cjs/tools/local/workspaceFS.cjs.map +1 -1
- package/dist/cjs/tools/ptcTimeout.cjs.map +1 -1
- package/dist/cjs/tools/schema.cjs +0 -8
- package/dist/cjs/tools/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/anthropic.cjs +0 -9
- package/dist/cjs/tools/search/anthropic.cjs.map +1 -1
- package/dist/cjs/tools/search/content.cjs.map +1 -1
- package/dist/cjs/tools/search/crw-scraper.cjs +0 -33
- package/dist/cjs/tools/search/crw-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/crw-search.cjs +0 -2
- package/dist/cjs/tools/search/crw-search.cjs.map +1 -1
- package/dist/cjs/tools/search/firecrawl.cjs +0 -25
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +0 -30
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/highlights.cjs +0 -16
- package/dist/cjs/tools/search/highlights.cjs.map +1 -1
- package/dist/cjs/tools/search/keenable-scraper.cjs +0 -8
- package/dist/cjs/tools/search/keenable-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/keenable-search.cjs +0 -7
- package/dist/cjs/tools/search/keenable-search.cjs.map +1 -1
- package/dist/cjs/tools/search/rerankers.cjs +0 -35
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +0 -1
- package/dist/cjs/tools/search/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +1 -14
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/serper-scraper.cjs +0 -46
- package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +0 -25
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +0 -8
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +0 -6
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -1
- package/dist/cjs/tools/streamedToolCallSeals.cjs +0 -6
- package/dist/cjs/tools/streamedToolCallSeals.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutionRegistry.cjs +0 -2
- package/dist/cjs/tools/subagent/SubagentExecutionRegistry.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +3 -213
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentReplay.cjs.map +1 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +0 -4
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
- package/dist/cjs/tools/subagent/runtimeLimits.cjs.map +1 -1
- package/dist/cjs/tools/toolOutputReferences.cjs +1 -322
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/cjs/types/hitl.cjs +0 -5
- package/dist/cjs/types/hitl.cjs.map +1 -1
- package/dist/cjs/utils/callbacks.cjs.map +1 -1
- package/dist/cjs/utils/errors.cjs +14 -165
- package/dist/cjs/utils/errors.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +0 -10
- package/dist/cjs/utils/events.cjs.map +1 -1
- package/dist/cjs/utils/graph.cjs.map +1 -1
- package/dist/cjs/utils/handlers.cjs +0 -3
- package/dist/cjs/utils/handlers.cjs.map +1 -1
- package/dist/cjs/utils/llm.cjs +0 -1
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/cjs/utils/misc.cjs +0 -16
- package/dist/cjs/utils/misc.cjs.map +1 -1
- package/dist/cjs/utils/proxy.cjs +0 -26
- package/dist/cjs/utils/proxy.cjs.map +1 -1
- package/dist/cjs/utils/run.cjs +0 -6
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/schema.cjs +0 -5
- package/dist/cjs/utils/schema.cjs.map +1 -1
- package/dist/cjs/utils/title.cjs +0 -6
- package/dist/cjs/utils/title.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +0 -113
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/cjs/utils/toolContent.cjs +0 -41
- package/dist/cjs/utils/toolContent.cjs.map +1 -1
- package/dist/cjs/utils/truncation.cjs +0 -61
- package/dist/cjs/utils/truncation.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +0 -356
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/agents/projection.mjs +0 -9
- package/dist/esm/agents/projection.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +0 -26
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +0 -85
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/events.mjs +0 -14
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +1 -909
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +7 -272
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/applyGraphRuntimeConfig.mjs.map +1 -1
- package/dist/esm/graphs/createGraph.mjs.map +1 -1
- package/dist/esm/hitl/askUserQuestion.mjs +0 -53
- package/dist/esm/hitl/askUserQuestion.mjs.map +1 -1
- package/dist/esm/hitl/askUserQuestions.mjs +0 -20
- package/dist/esm/hitl/askUserQuestions.mjs.map +1 -1
- package/dist/esm/hitl/askUserQuestionsInterrupt.mjs +0 -6
- package/dist/esm/hitl/askUserQuestionsInterrupt.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +0 -127
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/createToolPolicyHook.mjs +0 -38
- package/dist/esm/hooks/createToolPolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs +0 -77
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +0 -79
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +0 -16
- package/dist/esm/hooks/index.mjs.map +1 -1
- package/dist/esm/hooks/matchers.mjs +0 -104
- package/dist/esm/hooks/matchers.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +0 -8
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/instrumentation.mjs +0 -14
- package/dist/esm/instrumentation.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +0 -75
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/langfuseConfig.mjs.map +1 -1
- package/dist/esm/langfuseRuntimeContext.mjs +0 -13
- package/dist/esm/langfuseRuntimeContext.mjs.map +1 -1
- package/dist/esm/langfuseRuntimeScope.mjs +0 -4
- package/dist/esm/langfuseRuntimeScope.mjs.map +1 -1
- package/dist/esm/langfuseSpanRegistry.mjs +0 -28
- package/dist/esm/langfuseSpanRegistry.mjs.map +1 -1
- package/dist/esm/langfuseToolOutputTracing.mjs +0 -3
- package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
- package/dist/esm/langfuseTraceShaping.mjs +0 -74
- package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +0 -3
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -111
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +0 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/stream_events.mjs +0 -8
- package/dist/esm/llm/anthropic/utils/stream_events.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
- package/dist/esm/llm/bedrock/cachePoints.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +1 -95
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/bedrock/toolCache.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs +0 -78
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs +0 -24
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/contextOverflowRecovery.mjs +0 -62
- package/dist/esm/llm/contextOverflowRecovery.mjs.map +1 -1
- package/dist/esm/llm/fake.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +2 -21
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +5 -35
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/llm/init.mjs +0 -9
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +1 -270
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/mistral/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +0 -99
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/streamMetadata.mjs +0 -21
- package/dist/esm/llm/openai/streamMetadata.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +0 -1
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/toolCache.mjs +0 -6
- package/dist/esm/llm/openrouter/toolCache.mjs.map +1 -1
- package/dist/esm/llm/preempt.mjs +0 -75
- package/dist/esm/llm/preempt.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/request.mjs +0 -10
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/llm/stream/chunkAdapters.mjs +0 -62
- package/dist/esm/llm/stream/chunkAdapters.mjs.map +1 -1
- package/dist/esm/llm/stream/smoother.mjs +0 -58
- package/dist/esm/llm/stream/smoother.mjs.map +1 -1
- package/dist/esm/llm/streamLimits.mjs +4 -293
- package/dist/esm/llm/streamLimits.mjs.map +1 -1
- package/dist/esm/llm/truncation.mjs +0 -36
- package/dist/esm/llm/truncation.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +0 -330
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/messages/alternation.mjs +0 -43
- package/dist/esm/messages/alternation.mjs.map +1 -1
- package/dist/esm/messages/anthropicToolCache.mjs +0 -45
- package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
- package/dist/esm/messages/assistantPhase.mjs +0 -12
- package/dist/esm/messages/assistantPhase.mjs.map +1 -1
- package/dist/esm/messages/budget.mjs +0 -9
- package/dist/esm/messages/budget.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +0 -172
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/content.mjs +0 -9
- package/dist/esm/messages/content.mjs.map +1 -1
- package/dist/esm/messages/contextPruning.mjs +0 -15
- package/dist/esm/messages/contextPruning.mjs.map +1 -1
- package/dist/esm/messages/contextPruningSettings.mjs +0 -3
- package/dist/esm/messages/contextPruningSettings.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +0 -48
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +1 -345
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/handoffCue.mjs +0 -36
- package/dist/esm/messages/handoffCue.mjs.map +1 -1
- package/dist/esm/messages/ids.mjs.map +1 -1
- package/dist/esm/messages/injected.mjs +0 -33
- package/dist/esm/messages/injected.mjs.map +1 -1
- package/dist/esm/messages/langchain.mjs +0 -14
- package/dist/esm/messages/langchain.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +1 -184
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/messages/recency.mjs +0 -32
- package/dist/esm/messages/recency.mjs.map +1 -1
- package/dist/esm/messages/reducer.mjs +0 -30
- package/dist/esm/messages/reducer.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +0 -14
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/openai/index.mjs.map +1 -1
- package/dist/esm/prompts/activityLabel.mjs +9 -79
- package/dist/esm/prompts/activityLabel.mjs.map +1 -1
- package/dist/esm/responses/index.mjs.map +1 -1
- package/dist/esm/run.mjs +2 -538
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +1 -17
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs.map +1 -1
- package/dist/esm/session/handlers.mjs +0 -7
- package/dist/esm/session/handlers.mjs.map +1 -1
- package/dist/esm/session/ids.mjs.map +1 -1
- package/dist/esm/session/messageSerialization.mjs +0 -4
- package/dist/esm/session/messageSerialization.mjs.map +1 -1
- package/dist/esm/stream.mjs +1 -93
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/index.mjs +0 -27
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +0 -273
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +0 -35
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +0 -24
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +0 -13
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/CodeSessionFileSummary.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +0 -101
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs.map +1 -1
- package/dist/esm/tools/SkillTool.mjs +0 -5
- package/dist/esm/tools/SkillTool.mjs.map +1 -1
- package/dist/esm/tools/SubagentTool.mjs +0 -10
- package/dist/esm/tools/SubagentTool.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +6 -1038
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +0 -179
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs +0 -39
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs.map +1 -1
- package/dist/esm/tools/eagerEventExecution.mjs +0 -6
- package/dist/esm/tools/eagerEventExecution.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +1 -41
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/intentArg.mjs +1 -132
- package/dist/esm/tools/intentArg.mjs.map +1 -1
- package/dist/esm/tools/local/CompileCheckTool.mjs +0 -25
- package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -1
- package/dist/esm/tools/local/FileCheckpointer.mjs +0 -23
- package/dist/esm/tools/local/FileCheckpointer.mjs.map +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +0 -80
- package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +0 -126
- package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +0 -12
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/local/attachments.mjs +0 -44
- package/dist/esm/tools/local/attachments.mjs.map +1 -1
- package/dist/esm/tools/local/bashAst.mjs +0 -16
- package/dist/esm/tools/local/bashAst.mjs.map +1 -1
- package/dist/esm/tools/local/editStrategies.mjs +0 -14
- package/dist/esm/tools/local/editStrategies.mjs.map +1 -1
- package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/syntaxCheck.mjs +0 -31
- package/dist/esm/tools/local/syntaxCheck.mjs.map +1 -1
- package/dist/esm/tools/local/textEncoding.mjs +0 -11
- package/dist/esm/tools/local/textEncoding.mjs.map +1 -1
- package/dist/esm/tools/local/workspaceFS.mjs +0 -33
- package/dist/esm/tools/local/workspaceFS.mjs.map +1 -1
- package/dist/esm/tools/ptcTimeout.mjs.map +1 -1
- package/dist/esm/tools/schema.mjs +0 -8
- package/dist/esm/tools/schema.mjs.map +1 -1
- package/dist/esm/tools/search/anthropic.mjs +0 -9
- package/dist/esm/tools/search/anthropic.mjs.map +1 -1
- package/dist/esm/tools/search/content.mjs.map +1 -1
- package/dist/esm/tools/search/crw-scraper.mjs +0 -33
- package/dist/esm/tools/search/crw-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/crw-search.mjs +0 -2
- package/dist/esm/tools/search/crw-search.mjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +0 -25
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +0 -30
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/highlights.mjs +0 -16
- package/dist/esm/tools/search/highlights.mjs.map +1 -1
- package/dist/esm/tools/search/keenable-scraper.mjs +0 -8
- package/dist/esm/tools/search/keenable-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/keenable-search.mjs +0 -7
- package/dist/esm/tools/search/keenable-search.mjs.map +1 -1
- package/dist/esm/tools/search/rerankers.mjs +0 -35
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +0 -1
- package/dist/esm/tools/search/schema.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +1 -14
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/serper-scraper.mjs +0 -46
- package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +0 -25
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +0 -8
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +0 -6
- package/dist/esm/tools/skillCatalog.mjs.map +1 -1
- package/dist/esm/tools/streamedToolCallSeals.mjs +0 -6
- package/dist/esm/tools/streamedToolCallSeals.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutionRegistry.mjs +0 -2
- package/dist/esm/tools/subagent/SubagentExecutionRegistry.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +3 -213
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentReplay.mjs.map +1 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs +0 -4
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
- package/dist/esm/tools/subagent/runtimeLimits.mjs.map +1 -1
- package/dist/esm/tools/toolOutputReferences.mjs +1 -322
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/esm/types/hitl.mjs +0 -5
- package/dist/esm/types/hitl.mjs.map +1 -1
- package/dist/esm/utils/callbacks.mjs.map +1 -1
- package/dist/esm/utils/errors.mjs +14 -165
- package/dist/esm/utils/errors.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +0 -10
- package/dist/esm/utils/events.mjs.map +1 -1
- package/dist/esm/utils/graph.mjs.map +1 -1
- package/dist/esm/utils/handlers.mjs +0 -3
- package/dist/esm/utils/handlers.mjs.map +1 -1
- package/dist/esm/utils/llm.mjs +0 -1
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/esm/utils/misc.mjs +0 -16
- package/dist/esm/utils/misc.mjs.map +1 -1
- package/dist/esm/utils/proxy.mjs +0 -26
- package/dist/esm/utils/proxy.mjs.map +1 -1
- package/dist/esm/utils/run.mjs +0 -6
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/schema.mjs +0 -5
- package/dist/esm/utils/schema.mjs.map +1 -1
- package/dist/esm/utils/title.mjs +0 -6
- package/dist/esm/utils/title.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +0 -113
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/esm/utils/toolContent.mjs +0 -41
- package/dist/esm/utils/toolContent.mjs.map +1 -1
- package/dist/esm/utils/truncation.mjs +0 -61
- package/dist/esm/utils/truncation.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +3 -3
- package/dist/types/agents/__tests__/promptCacheLiveHelpers.d.ts +3 -3
- package/dist/types/agents/projection.d.ts +1 -1
- package/dist/types/events.d.ts +2 -2
- package/dist/types/graphs/Graph.d.ts +11 -11
- package/dist/types/graphs/MultiAgentGraph.d.ts +2 -2
- package/dist/types/graphs/createGraph.d.ts +4 -4
- package/dist/types/graphs/graphFactory.d.ts +1 -1
- package/dist/types/hitl/askUserQuestion.d.ts +1 -1
- package/dist/types/hitl/askUserQuestions.d.ts +1 -1
- package/dist/types/hitl/askUserQuestionsInterrupt.d.ts +1 -1
- package/dist/types/hooks/types.d.ts +1 -1
- package/dist/types/instrumentation.d.ts +1 -1
- package/dist/types/langfuse.d.ts +3 -3
- package/dist/types/langfuseConfig.d.ts +2 -2
- package/dist/types/langfuseRuntimeContext.d.ts +1 -1
- package/dist/types/langfuseRuntimeScope.d.ts +2 -2
- package/dist/types/langfuseSpanRegistry.d.ts +1 -1
- package/dist/types/langfuseToolOutputTracing.d.ts +3 -3
- package/dist/types/llm/anthropic/index.d.ts +1 -1
- package/dist/types/llm/bedrock/index.d.ts +1 -1
- package/dist/types/llm/bedrock/toolCache.d.ts +2 -2
- package/dist/types/llm/contextOverflowRecovery.d.ts +2 -2
- package/dist/types/llm/google/index.d.ts +1 -1
- package/dist/types/llm/init.d.ts +2 -2
- package/dist/types/llm/invoke.d.ts +6 -6
- package/dist/types/llm/mistral/index.d.ts +1 -1
- package/dist/types/llm/openai/index.d.ts +2 -2
- package/dist/types/llm/openrouter/index.d.ts +2 -2
- package/dist/types/llm/openrouter/toolCache.d.ts +2 -2
- package/dist/types/llm/providers.d.ts +2 -2
- package/dist/types/llm/request.d.ts +2 -2
- package/dist/types/llm/stream/chunkAdapters.d.ts +1 -1
- package/dist/types/llm/streamLimits.d.ts +1 -1
- package/dist/types/llm/truncation.d.ts +1 -1
- package/dist/types/llm/vertexai/index.d.ts +1 -1
- package/dist/types/messages/alternation.d.ts +1 -1
- package/dist/types/messages/anthropicToolCache.d.ts +2 -2
- package/dist/types/messages/assistantPhase.d.ts +3 -3
- package/dist/types/messages/budget.d.ts +1 -1
- package/dist/types/messages/cache.d.ts +1 -1
- package/dist/types/messages/contextPruning.d.ts +2 -2
- package/dist/types/messages/core.d.ts +2 -2
- package/dist/types/messages/format.d.ts +2 -2
- package/dist/types/messages/ids.d.ts +2 -2
- package/dist/types/messages/injected.d.ts +1 -1
- package/dist/types/messages/langchain.d.ts +1 -1
- package/dist/types/messages/prune.d.ts +3 -3
- package/dist/types/openai/index.d.ts +1 -1
- package/dist/types/prompts/activityLabel.d.ts +2 -2
- package/dist/types/responses/index.d.ts +1 -1
- package/dist/types/run.d.ts +3 -3
- package/dist/types/session/handlers.d.ts +1 -1
- package/dist/types/session/types.d.ts +1 -1
- package/dist/types/stream.d.ts +4 -4
- package/dist/types/summarization/index.d.ts +1 -1
- package/dist/types/summarization/node.d.ts +6 -6
- package/dist/types/test/mockTools.d.ts +1 -1
- package/dist/types/tools/BashExecutor.d.ts +2 -2
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/CodeExecutor.d.ts +3 -3
- package/dist/types/tools/CodeSessionFileSummary.d.ts +1 -1
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/ReadFile.d.ts +1 -1
- package/dist/types/tools/SkillTool.d.ts +1 -1
- package/dist/types/tools/SubagentTool.d.ts +3 -3
- package/dist/types/tools/ToolNode.d.ts +5 -5
- package/dist/types/tools/ToolSearch.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareBridgeRuntime.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareSandboxExecutionEngine.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareSandboxTools.d.ts +1 -1
- package/dist/types/tools/eagerEventExecution.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +3 -3
- package/dist/types/tools/intentArg.d.ts +1 -1
- package/dist/types/tools/local/CompileCheckTool.d.ts +2 -2
- package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
- package/dist/types/tools/local/LocalCodingTools.d.ts +2 -2
- package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
- package/dist/types/tools/local/LocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/LocalProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/local/bashAst.d.ts +1 -1
- package/dist/types/tools/local/resolveLocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/syntaxCheck.d.ts +1 -1
- package/dist/types/tools/ptcTimeout.d.ts +1 -1
- package/dist/types/tools/schema.d.ts +1 -1
- package/dist/types/tools/search/anthropic.d.ts +1 -1
- package/dist/types/tools/skillCatalog.d.ts +1 -1
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +5 -5
- package/dist/types/tools/subagent/SubagentReplay.d.ts +3 -3
- package/dist/types/tools/subagent/childGraphConfig.d.ts +2 -2
- package/dist/types/types/activityLabel.d.ts +3 -3
- package/dist/types/types/graph.d.ts +7 -7
- package/dist/types/types/llm.d.ts +9 -9
- package/dist/types/types/run.d.ts +7 -7
- package/dist/types/types/stream.d.ts +5 -5
- package/dist/types/types/summarize.d.ts +2 -2
- package/dist/types/types/tools.d.ts +5 -5
- package/dist/types/utils/__tests__/fixtures/contextOverflowSignatures.d.ts +1 -1
- package/dist/types/utils/errors.d.ts +1 -1
- package/dist/types/utils/events.d.ts +1 -1
- package/dist/types/utils/handlers.d.ts +3 -3
- package/dist/types/utils/llm.d.ts +1 -1
- package/dist/types/utils/llmConfig.d.ts +1 -1
- package/dist/types/utils/title.d.ts +1 -1
- package/package.json +10 -3
- package/src/llm/google/utils/common.ts +6 -4
- package/src/__tests__/stream.eagerArgsDivergence.test.ts +0 -911
- package/src/__tests__/stream.eagerEventExecution.test.ts +0 -5046
- package/src/__tests__/stream.streamLimits.test.ts +0 -1982
- package/src/agents/__tests__/AgentContext.anthropic.live.test.ts +0 -448
- package/src/agents/__tests__/AgentContext.bedrock.live.test.ts +0 -655
- package/src/agents/__tests__/AgentContext.openrouter.live.test.ts +0 -128
- package/src/agents/__tests__/AgentContext.overflow.test.ts +0 -205
- package/src/agents/__tests__/AgentContext.test.ts +0 -2425
- package/src/agents/__tests__/projection.test.ts +0 -73
- package/src/agents/__tests__/promptCacheLiveHelpers.ts +0 -175
- package/src/aggregator.test.ts +0 -1214
- package/src/graphs/__tests__/Graph.breakerLifecycle.test.ts +0 -273
- package/src/graphs/__tests__/Graph.closeRunStep.test.ts +0 -385
- package/src/graphs/__tests__/Graph.contextOverflow.test.ts +0 -1766
- package/src/graphs/__tests__/Graph.preemptSignal.test.ts +0 -126
- package/src/graphs/__tests__/Graph.reasoning.test.ts +0 -1717
- package/src/graphs/__tests__/Graph.subagentResumeState.test.ts +0 -80
- package/src/graphs/__tests__/MultiAgentGraph.test.ts +0 -177
- package/src/graphs/__tests__/composition.smoke.test.ts +0 -509
- package/src/graphs/__tests__/createGraph.test.ts +0 -206
- package/src/hooks/__tests__/HookRegistry.test.ts +0 -334
- package/src/hooks/__tests__/compactHooks.test.ts +0 -250
- package/src/hooks/__tests__/createToolPolicyHook.test.ts +0 -268
- package/src/hooks/__tests__/createWorkspacePolicyHook.test.ts +0 -405
- package/src/hooks/__tests__/executeHooks.test.ts +0 -1294
- package/src/hooks/__tests__/integration.test.ts +0 -337
- package/src/hooks/__tests__/matchers.test.ts +0 -238
- package/src/hooks/__tests__/preemptBoundary.test.ts +0 -152
- package/src/hooks/__tests__/toolHooks.test.ts +0 -669
- package/src/llm/__tests__/contextOverflowRecovery.test.ts +0 -401
- package/src/llm/__tests__/fallbackOverflow.test.ts +0 -287
- package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
- package/src/llm/anthropic/inherited-content-utils.spec.ts +0 -249
- package/src/llm/anthropic/inherited-stream-events.spec.ts +0 -1259
- package/src/llm/anthropic/inherited-strict.spec.ts +0 -302
- package/src/llm/anthropic/llm.spec.ts +0 -3394
- package/src/llm/anthropic/utils/cross-provider-reasoning.test.ts +0 -383
- package/src/llm/anthropic/utils/cross-provider-server-tools.test.ts +0 -110
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +0 -508
- package/src/llm/anthropic/utils/streaming-tool-input.test.ts +0 -361
- package/src/llm/anthropic/utils/stripPrefillCache.test.ts +0 -111
- package/src/llm/anthropic/utils/tool-id-normalization.test.ts +0 -287
- package/src/llm/bedrock/inherited-cache.spec.ts +0 -144
- package/src/llm/bedrock/inherited.spec.ts +0 -724
- package/src/llm/bedrock/llm.spec.ts +0 -1533
- package/src/llm/bedrock/streamSealDispatch.test.ts +0 -331
- package/src/llm/bedrock/toolCache.test.ts +0 -194
- package/src/llm/bedrock/utils/cross-provider-reasoning.test.ts +0 -131
- package/src/llm/bedrock/utils/cross-provider-server-tools.test.ts +0 -122
- package/src/llm/bedrock/utils/message_inputs.test.ts +0 -650
- package/src/llm/bedrock/utils/message_outputs.test.ts +0 -85
- package/src/llm/bedrock/utils/toolResultCachePoint.test.ts +0 -118
- package/src/llm/custom-chat-models.smoke.test.ts +0 -1966
- package/src/llm/google/data/gettysburg10.wav +0 -0
- package/src/llm/google/data/hotdog.jpg +0 -0
- package/src/llm/google/inherited-stream-events.spec.ts +0 -350
- package/src/llm/google/llm.spec.ts +0 -1388
- package/src/llm/google/streamSmoothing.test.ts +0 -121
- package/src/llm/google/utils/common.test.ts +0 -185
- package/src/llm/invoke.alternation.test.ts +0 -87
- package/src/llm/invoke.handoffCue.test.ts +0 -112
- package/src/llm/invoke.streamLimits.test.ts +0 -142
- package/src/llm/invoke.test.ts +0 -1198
- package/src/llm/mistral/streamSmoothing.test.ts +0 -97
- package/src/llm/openai/cacheWriteTokens.test.ts +0 -112
- package/src/llm/openai/contentBlocks.test.ts +0 -381
- package/src/llm/openai/deepseek.test.ts +0 -747
- package/src/llm/openai/inherited-deepseek.spec.ts +0 -347
- package/src/llm/openai/inherited-xai.spec.ts +0 -416
- package/src/llm/openai/llm.spec.ts +0 -1774
- package/src/llm/openai/managedRequests.test.ts +0 -182
- package/src/llm/openai/sequentialToolCallSeals.test.ts +0 -199
- package/src/llm/openai/streamMetadata.spec.ts +0 -86
- package/src/llm/openai/streamMetadataDedup.spec.ts +0 -166
- package/src/llm/openai/utils/isReasoningModel.test.ts +0 -90
- package/src/llm/openai/utils/messages.test.ts +0 -488
- package/src/llm/openrouter/reasoning.test.ts +0 -561
- package/src/llm/openrouter/toolCache.test.ts +0 -134
- package/src/llm/preempt.test.ts +0 -323
- package/src/llm/stream/chunkAdapters.test.ts +0 -202
- package/src/llm/stream/reassembly.test.ts +0 -241
- package/src/llm/stream/smoother.bench.test.ts +0 -155
- package/src/llm/stream/smoother.test.ts +0 -519
- package/src/llm/streamLimits.test.ts +0 -450
- package/src/llm/truncation.test.ts +0 -242
- package/src/llm/vertexai/fixThoughtSignatures.test.ts +0 -154
- package/src/llm/vertexai/inherited-stream-events.spec.ts +0 -271
- package/src/llm/vertexai/llm.spec.ts +0 -159
- package/src/llm/vertexai/repairUsageMetadata.test.ts +0 -54
- package/src/llm/vertexai/sealStreamedToolCalls.test.ts +0 -88
- package/src/llm/vertexai/streamSealDispatch.test.ts +0 -148
- package/src/llm/vertexai/streamSmoothing.test.ts +0 -109
- package/src/messages/__tests__/anthropicToolCache.test.ts +0 -339
- package/src/messages/__tests__/observationMasking.test.ts +0 -312
- package/src/messages/__tests__/recency.test.ts +0 -267
- package/src/messages/__tests__/tools.test.ts +0 -473
- package/src/messages/alternation.test.ts +0 -212
- package/src/messages/assistantPhase.test.ts +0 -75
- package/src/messages/cache.tail.test.ts +0 -533
- package/src/messages/cache.test.ts +0 -2166
- package/src/messages/content.test.ts +0 -362
- package/src/messages/contextPruning.test.ts +0 -184
- package/src/messages/ensureThinkingBlock.test.ts +0 -1427
- package/src/messages/foldToollessToolBlocks.test.ts +0 -596
- package/src/messages/formatAgentMessages.reducer.test.ts +0 -162
- package/src/messages/formatAgentMessages.skills.test.ts +0 -513
- package/src/messages/formatAgentMessages.steer.test.ts +0 -598
- package/src/messages/formatAgentMessages.test.ts +0 -8111
- package/src/messages/formatAgentMessages.tools.test.ts +0 -522
- package/src/messages/formatMessage.test.ts +0 -693
- package/src/messages/handoffCue.test.ts +0 -96
- package/src/messages/injected.test.ts +0 -107
- package/src/messages/labelContentByAgent.test.ts +0 -962
- package/src/messages/reducer.spec.ts +0 -32
- package/src/messages/shiftIndexTokenCountMap.test.ts +0 -81
- package/src/messages/tailCacheConversion.test.ts +0 -161
- package/src/openai/__tests__/openai.test.ts +0 -337
- package/src/responses/__tests__/responses.test.ts +0 -652
- package/src/scripts/abort.ts +0 -157
- package/src/scripts/activity-labels/captured.json +0 -56
- package/src/scripts/activity-labels/checks.cjs +0 -205
- package/src/scripts/activity-labels/corpus.cjs +0 -473
- package/src/scripts/activity-labels/report.cjs +0 -203
- package/src/scripts/activity-labels/rescore.cjs +0 -102
- package/src/scripts/activity-labels/run.ts +0 -705
- package/src/scripts/activity-labels/variants.ts +0 -71
- package/src/scripts/ant_web_search.ts +0 -159
- package/src/scripts/ant_web_search_edge_case.ts +0 -163
- package/src/scripts/ant_web_search_error_edge_case.ts +0 -149
- package/src/scripts/args.ts +0 -48
- package/src/scripts/bedrock-cache-debug.ts +0 -250
- package/src/scripts/bedrock-content-aggregation-test.ts +0 -266
- package/src/scripts/bedrock-merge-test.ts +0 -107
- package/src/scripts/bedrock-parallel-tools-test.ts +0 -204
- package/src/scripts/bench-prompt-cache.ts +0 -479
- package/src/scripts/caching.ts +0 -132
- package/src/scripts/cli.ts +0 -172
- package/src/scripts/cli2.ts +0 -133
- package/src/scripts/cli3.ts +0 -184
- package/src/scripts/cli4.ts +0 -191
- package/src/scripts/cli5.ts +0 -191
- package/src/scripts/code_exec.ts +0 -214
- package/src/scripts/code_exec_files.ts +0 -237
- package/src/scripts/code_exec_multi_session.ts +0 -237
- package/src/scripts/code_exec_ptc.ts +0 -335
- package/src/scripts/code_exec_session.ts +0 -283
- package/src/scripts/code_exec_simple.ts +0 -148
- package/src/scripts/compare_pi_vs_ours.ts +0 -1259
- package/src/scripts/content.ts +0 -139
- package/src/scripts/context-overflow-probe.ts +0 -997
- package/src/scripts/empty_input.ts +0 -137
- package/src/scripts/handoff-test.ts +0 -134
- package/src/scripts/local_engine.ts +0 -166
- package/src/scripts/local_engine_checkpointer.ts +0 -205
- package/src/scripts/local_engine_compile.ts +0 -263
- package/src/scripts/local_engine_hooks.ts +0 -226
- package/src/scripts/local_engine_image.ts +0 -201
- package/src/scripts/local_engine_ptc.ts +0 -151
- package/src/scripts/local_engine_workspace.ts +0 -258
- package/src/scripts/memory.ts +0 -107
- package/src/scripts/multi-agent-chain.ts +0 -332
- package/src/scripts/multi-agent-conditional.ts +0 -222
- package/src/scripts/multi-agent-document-review-chain.ts +0 -198
- package/src/scripts/multi-agent-hybrid-flow.ts +0 -311
- package/src/scripts/multi-agent-parallel-start.ts +0 -265
- package/src/scripts/multi-agent-parallel.ts +0 -395
- package/src/scripts/multi-agent-sequence.ts +0 -217
- package/src/scripts/multi-agent-subagent.ts +0 -246
- package/src/scripts/multi-agent-supervisor.ts +0 -366
- package/src/scripts/multi-agent-test.ts +0 -187
- package/src/scripts/openrouter_prompt_cache_live.ts +0 -310
- package/src/scripts/parallel-asymmetric-tools-test.ts +0 -275
- package/src/scripts/parallel-full-metadata-test.ts +0 -241
- package/src/scripts/parallel-tools-test.ts +0 -341
- package/src/scripts/preempt-probe.ts +0 -330
- package/src/scripts/preempt-scenarios.ts +0 -388
- package/src/scripts/programmatic_exec.ts +0 -387
- package/src/scripts/programmatic_exec_agent.ts +0 -232
- package/src/scripts/search.ts +0 -147
- package/src/scripts/sequential-full-metadata-test.ts +0 -198
- package/src/scripts/session_live.ts +0 -548
- package/src/scripts/simple.ts +0 -226
- package/src/scripts/single-agent-metadata-test.ts +0 -196
- package/src/scripts/stream.ts +0 -141
- package/src/scripts/subagent-configurable-inheritance.ts +0 -252
- package/src/scripts/subagent-event-driven-debug.ts +0 -190
- package/src/scripts/subagent-tools-debug.ts +0 -160
- package/src/scripts/subagent-usage-sink.ts +0 -176
- package/src/scripts/summarization-recency.ts +0 -462
- package/src/scripts/test-custom-prompt-key.ts +0 -145
- package/src/scripts/test-handoff-input.ts +0 -170
- package/src/scripts/test-handoff-preamble.ts +0 -278
- package/src/scripts/test-handoff-steering.ts +0 -430
- package/src/scripts/test-multi-agent-list-handoff.ts +0 -418
- package/src/scripts/test-parallel-agent-labeling.ts +0 -327
- package/src/scripts/test-parallel-handoffs.ts +0 -292
- package/src/scripts/test-thinking-handoff-bedrock.ts +0 -154
- package/src/scripts/test-thinking-handoff.ts +0 -156
- package/src/scripts/test-thinking-to-thinking-handoff-bedrock.ts +0 -166
- package/src/scripts/test-tool-before-handoff-role-order.ts +0 -276
- package/src/scripts/test-tools-before-handoff.ts +0 -222
- package/src/scripts/test_code_api.ts +0 -354
- package/src/scripts/thinking-bedrock.ts +0 -160
- package/src/scripts/thinking-vertexai.ts +0 -168
- package/src/scripts/thinking.ts +0 -172
- package/src/scripts/tool_search.ts +0 -153
- package/src/scripts/tools.ts +0 -163
- package/src/session/__tests__/JsonlSessionStore.test.ts +0 -1463
- package/src/session/__tests__/handlers.test.ts +0 -161
- package/src/specs/activity-label-observability.live.test.ts +0 -262
- package/src/specs/activity-label-observability.test.ts +0 -167
- package/src/specs/activity-label-prompt.test.ts +0 -359
- package/src/specs/activity-label-trace-seed.test.ts +0 -47
- package/src/specs/activity-phase-label.test.ts +0 -306
- package/src/specs/agent-handoffs.live.test.ts +0 -140
- package/src/specs/agent-handoffs.test.ts +0 -2465
- package/src/specs/anthropic.simple.test.ts +0 -453
- package/src/specs/ask-user-question-batch.test.ts +0 -289
- package/src/specs/ask-user-questions.live.test.ts +0 -185
- package/src/specs/ask-user-questions.test.ts +0 -293
- package/src/specs/azure.simple.test.ts +0 -502
- package/src/specs/bedrock-toolless.live.test.ts +0 -123
- package/src/specs/bedrock-truncation.live.test.ts +0 -191
- package/src/specs/cache.simple.test.ts +0 -416
- package/src/specs/context-accuracy.live.test.ts +0 -409
- package/src/specs/context-overflow-recovery.live.test.ts +0 -213
- package/src/specs/context-usage-event.test.ts +0 -117
- package/src/specs/context-usage.live.test.ts +0 -297
- package/src/specs/custom-event-await.test.ts +0 -304
- package/src/specs/deepseek.simple.test.ts +0 -290
- package/src/specs/deterministic-trace-id.test.ts +0 -43
- package/src/specs/discovered-tools.test.ts +0 -217
- package/src/specs/durability-checkpoint.integration.test.ts +0 -243
- package/src/specs/graph-subagent.live.test.ts +0 -242
- package/src/specs/graph-subagent.test.ts +0 -322
- package/src/specs/handoffCue.test.ts +0 -165
- package/src/specs/langfuse-callbacks.test.ts +0 -1578
- package/src/specs/langfuse-config.test.ts +0 -247
- package/src/specs/langfuse-instrumentation.test.ts +0 -393
- package/src/specs/langfuse-metadata.test.ts +0 -245
- package/src/specs/langfuse-routing.integration.test.ts +0 -844
- package/src/specs/langfuse-runtime-context.test.ts +0 -92
- package/src/specs/langfuse-span-registry.test.ts +0 -87
- package/src/specs/langfuse-tool-output-tracing.test.ts +0 -1858
- package/src/specs/langfuse-trace-shaping.test.ts +0 -591
- package/src/specs/moonshot.simple.test.ts +0 -367
- package/src/specs/multi-agent-summarization.test.ts +0 -396
- package/src/specs/openai.simple.test.ts +0 -315
- package/src/specs/openrouter.simple.test.ts +0 -275
- package/src/specs/preemptSeal.test.ts +0 -678
- package/src/specs/prune.test.ts +0 -3741
- package/src/specs/reasoning.test.ts +0 -201
- package/src/specs/run-step-timestamps.test.ts +0 -414
- package/src/specs/spec.utils.ts +0 -15
- package/src/specs/subagent.test.ts +0 -864
- package/src/specs/summarization-unit.test.ts +0 -973
- package/src/specs/summarization.test.ts +0 -3823
- package/src/specs/summarize-prune.test.ts +0 -376
- package/src/specs/thinking-handoff.test.ts +0 -621
- package/src/specs/thinking-prune.test.ts +0 -827
- package/src/specs/token-accounting-e2e.test.ts +0 -1034
- package/src/specs/token-accounting-pipeline.test.ts +0 -1008
- package/src/specs/token-distribution-edge-case.test.ts +0 -315
- package/src/specs/token-memoization.test.ts +0 -40
- package/src/specs/tokens.test.ts +0 -1000
- package/src/specs/tool-error-resume.test.ts +0 -194
- package/src/specs/tool-error.test.ts +0 -198
- package/src/specs/vllm-reasoning-toolcalls.test.ts +0 -340
- package/src/stream.dispatch.test.ts +0 -63
- package/src/stream.test.ts +0 -144
- package/src/summarization/__tests__/aggregator.test.ts +0 -236
- package/src/summarization/__tests__/node.test.ts +0 -1996
- package/src/summarization/__tests__/trigger.test.ts +0 -149
- package/src/summarization/chunkHandler.test.ts +0 -196
- package/src/test/mockTools.ts +0 -386
- package/src/tools/Calculator.test.ts +0 -278
- package/src/tools/__tests__/BashExecutor.test.ts +0 -108
- package/src/tools/__tests__/CloudflareSandboxExecution.test.ts +0 -939
- package/src/tools/__tests__/CodeApiAuthHeaders.test.ts +0 -858
- package/src/tools/__tests__/CodeExecutor.stateful.test.ts +0 -124
- package/src/tools/__tests__/LocalExecutionRoots.test.ts +0 -8
- package/src/tools/__tests__/LocalExecutionTools.test.ts +0 -2733
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +0 -318
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -1557
- package/src/tools/__tests__/ReadFile.test.ts +0 -44
- package/src/tools/__tests__/SkillTool.test.ts +0 -442
- package/src/tools/__tests__/SubagentExecutor.test.ts +0 -3728
- package/src/tools/__tests__/SubagentReplay.test.ts +0 -329
- package/src/tools/__tests__/SubagentTool.test.ts +0 -149
- package/src/tools/__tests__/ToolNode.breakerSignal.test.ts +0 -595
- package/src/tools/__tests__/ToolNode.eagerEventExecution.test.ts +0 -1191
- package/src/tools/__tests__/ToolNode.invalidToolCalls.test.ts +0 -757
- package/src/tools/__tests__/ToolNode.langfuse.test.ts +0 -57
- package/src/tools/__tests__/ToolNode.onResultCompletion.test.ts +0 -736
- package/src/tools/__tests__/ToolNode.outputReferences.test.ts +0 -1710
- package/src/tools/__tests__/ToolNode.runtimeState.test.ts +0 -120
- package/src/tools/__tests__/ToolNode.session.test.ts +0 -1352
- package/src/tools/__tests__/ToolNode.streamLimits.test.ts +0 -69
- package/src/tools/__tests__/ToolSearch.integration.test.ts +0 -161
- package/src/tools/__tests__/ToolSearch.test.ts +0 -1227
- package/src/tools/__tests__/annotateMessagesForLLM.test.ts +0 -561
- package/src/tools/__tests__/directToolHITLResumeScope.test.ts +0 -980
- package/src/tools/__tests__/directToolHooks.test.ts +0 -700
- package/src/tools/__tests__/eagerEventExecution.session.test.ts +0 -92
- package/src/tools/__tests__/handlers.test.ts +0 -997
- package/src/tools/__tests__/hitl.test.ts +0 -4663
- package/src/tools/__tests__/intentArg.test.ts +0 -357
- package/src/tools/__tests__/intentCoverage.test.ts +0 -139
- package/src/tools/__tests__/localToolNames.test.ts +0 -75
- package/src/tools/__tests__/skillCatalog.test.ts +0 -161
- package/src/tools/__tests__/subagentHooks.test.ts +0 -1651
- package/src/tools/__tests__/toolOutputReferences.test.ts +0 -515
- package/src/tools/__tests__/workspaceSeam.test.ts +0 -147
- package/src/tools/local/__tests__/FileCheckpointer.test.ts +0 -162
- package/src/tools/local/__tests__/editStrategies.test.ts +0 -134
- package/src/tools/search/content.test.ts +0 -173
- package/src/tools/search/crw.test.ts +0 -836
- package/src/tools/search/format.test.ts +0 -242
- package/src/tools/search/http-agent.test.ts +0 -133
- package/src/tools/search/jina-reranker.test.ts +0 -306
- package/src/tools/search/keenable-scraper.test.ts +0 -153
- package/src/tools/search/keenable.test.ts +0 -183
- package/src/tools/search/outcome.test.ts +0 -90
- package/src/tools/search/output.md +0 -2775
- package/src/tools/search/rag-api-reranker.test.ts +0 -891
- package/src/tools/search/source-processing.test.ts +0 -516
- package/src/tools/search/tavily.test.ts +0 -965
- package/src/tools/search/test.html +0 -884
- package/src/tools/search/test.md +0 -643
- package/src/tools/subagent/__tests__/SubagentExecutionRegistry.test.ts +0 -637
- package/src/tools/subagent/__tests__/SubagentExecutor.lazy.test.ts +0 -3054
- package/src/tools/subagent/__tests__/childGraphConfig.test.ts +0 -521
- package/src/utils/__tests__/apportion.test.ts +0 -32
- package/src/utils/__tests__/errors.test.ts +0 -270
- package/src/utils/__tests__/fixtures/contextOverflowSignatures.ts +0 -336
- package/src/utils/__tests__/redactSecrets.test.ts +0 -56
- package/src/utils/__tests__/truncation.test.ts +0 -66
- package/src/utils/llm.test.ts +0 -18
- package/src/utils/proxy.test.ts +0 -176
- package/src/utils/toolContent.test.ts +0 -1432
|
@@ -1,5046 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, jest, afterEach } from '@jest/globals';
|
|
2
|
-
import type { AgentContext } from '@/agents/AgentContext';
|
|
3
|
-
import type { StandardGraph } from '@/graphs';
|
|
4
|
-
import type * as t from '@/types';
|
|
5
|
-
import {
|
|
6
|
-
STREAMED_TOOL_CALL_SEAL_METADATA_KEY,
|
|
7
|
-
STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY,
|
|
8
|
-
OPENAI_RESPONSES_STREAMED_TOOL_CALL_ADAPTER,
|
|
9
|
-
BEDROCK_CONVERSE_STREAMED_TOOL_CALL_ADAPTER,
|
|
10
|
-
GOOGLE_STREAMED_TOOL_CALL_ADAPTER,
|
|
11
|
-
OPENAI_CHAT_SEQUENTIAL_STREAMED_TOOL_CALL_ADAPTER,
|
|
12
|
-
} from '@/tools/streamedToolCallSeals';
|
|
13
|
-
import {
|
|
14
|
-
Constants,
|
|
15
|
-
ContentTypes,
|
|
16
|
-
GraphEvents,
|
|
17
|
-
Providers,
|
|
18
|
-
StepTypes,
|
|
19
|
-
ToolCallTypes,
|
|
20
|
-
} from '@/common';
|
|
21
|
-
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
22
|
-
import { HandlerRegistry } from '@/events';
|
|
23
|
-
import * as events from '@/utils/events';
|
|
24
|
-
import { HookRegistry } from '@/hooks';
|
|
25
|
-
|
|
26
|
-
function createResultAlteringRegistry(): HookRegistry {
|
|
27
|
-
const registry = new HookRegistry();
|
|
28
|
-
registry.register('PreToolUse', { hooks: [async () => ({})] });
|
|
29
|
-
return registry;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function createGraph(overrides: Partial<StandardGraph> = {}): StandardGraph {
|
|
33
|
-
const runSteps = new Map<string, t.RunStep>();
|
|
34
|
-
const stepIdsByKey = new Map<string, string>();
|
|
35
|
-
let stepCounter = 0;
|
|
36
|
-
const handlerRegistry = new HandlerRegistry();
|
|
37
|
-
handlerRegistry.register(GraphEvents.ON_TOOL_EXECUTE, {
|
|
38
|
-
handle: async () => undefined,
|
|
39
|
-
});
|
|
40
|
-
const eagerUsageCount = new Map<string, number>();
|
|
41
|
-
|
|
42
|
-
const graph = {
|
|
43
|
-
config: {
|
|
44
|
-
configurable: { user_id: 'user_1' },
|
|
45
|
-
metadata: { run_id: 'run_1' },
|
|
46
|
-
},
|
|
47
|
-
breakerAbort: new AbortController(),
|
|
48
|
-
eagerEventToolExecution: { enabled: true },
|
|
49
|
-
eagerEventToolExecutions: new Map(),
|
|
50
|
-
eagerEventToolUsageCount: eagerUsageCount,
|
|
51
|
-
getEagerEventToolUsageCount: jest.fn(() => eagerUsageCount),
|
|
52
|
-
eagerEventToolCallChunks: new Map(),
|
|
53
|
-
handlerRegistry,
|
|
54
|
-
hookRegistry: undefined,
|
|
55
|
-
humanInTheLoop: undefined,
|
|
56
|
-
toolOutputReferences: undefined,
|
|
57
|
-
sessions: new Map(),
|
|
58
|
-
toolCallStepIds: new Map(),
|
|
59
|
-
messageIdsByStepKey: new Map(),
|
|
60
|
-
messageStepHasToolCalls: new Map(),
|
|
61
|
-
prelimMessageIdsByStepKey: new Map(),
|
|
62
|
-
getAgentContext: jest.fn(
|
|
63
|
-
(): Partial<AgentContext> => ({
|
|
64
|
-
provider: Providers.ANTHROPIC,
|
|
65
|
-
reasoningKey: 'reasoning',
|
|
66
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
67
|
-
graphTools: [],
|
|
68
|
-
agentId: 'agent_1',
|
|
69
|
-
})
|
|
70
|
-
),
|
|
71
|
-
getStepKey: jest.fn(() => 'step-key'),
|
|
72
|
-
getStepIdByKey: jest.fn((stepKey: string) => {
|
|
73
|
-
const stepId = stepIdsByKey.get(stepKey);
|
|
74
|
-
if (stepId == null) {
|
|
75
|
-
throw new Error('no current step');
|
|
76
|
-
}
|
|
77
|
-
return stepId;
|
|
78
|
-
}),
|
|
79
|
-
getRunStep: jest.fn((stepId: string) => runSteps.get(stepId)),
|
|
80
|
-
dispatchRunStep: jest.fn(async (stepKey: string, details: unknown) => {
|
|
81
|
-
const id = `step_${++stepCounter}`;
|
|
82
|
-
if (
|
|
83
|
-
(details as t.StepDetails).type === StepTypes.TOOL_CALLS &&
|
|
84
|
-
Array.isArray((details as t.ToolCallsDetails).tool_calls)
|
|
85
|
-
) {
|
|
86
|
-
for (const toolCall of (details as t.ToolCallsDetails).tool_calls ??
|
|
87
|
-
[]) {
|
|
88
|
-
if (toolCall.id != null && toolCall.id !== '') {
|
|
89
|
-
graph.toolCallStepIds.set(toolCall.id, id);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
stepIdsByKey.set(stepKey, id);
|
|
94
|
-
runSteps.set(id, {
|
|
95
|
-
id,
|
|
96
|
-
type: (details as { type: t.RunStep['type'] }).type,
|
|
97
|
-
stepDetails: details as t.RunStep['stepDetails'],
|
|
98
|
-
} as t.RunStep);
|
|
99
|
-
return id;
|
|
100
|
-
}),
|
|
101
|
-
dispatchRunStepDelta: jest.fn(async () => undefined),
|
|
102
|
-
...overrides,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
return graph as unknown as StandardGraph;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function chunkStateKey(stepKey: string, chunkKey: string | number): string {
|
|
109
|
-
return `${stepKey}\u0000${String(chunkKey)}`;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function attachContentAggregator(graph: StandardGraph): {
|
|
113
|
-
contentParts: Array<t.MessageContentComplex | undefined>;
|
|
114
|
-
} {
|
|
115
|
-
const { contentParts, aggregateContent } = createContentAggregator();
|
|
116
|
-
const dispatchRunStep = graph.dispatchRunStep.bind(graph);
|
|
117
|
-
const stepIndices = new Map<string, number>();
|
|
118
|
-
graph.dispatchRunStep = jest.fn<StandardGraph['dispatchRunStep']>(
|
|
119
|
-
async (stepKey, stepDetails, metadata) => {
|
|
120
|
-
const stepId = await dispatchRunStep(stepKey, stepDetails, metadata);
|
|
121
|
-
const runStep = graph.getRunStep(stepId);
|
|
122
|
-
if (runStep != null) {
|
|
123
|
-
if (!stepIndices.has(stepId)) {
|
|
124
|
-
stepIndices.set(stepId, stepIndices.size);
|
|
125
|
-
}
|
|
126
|
-
aggregateContent({
|
|
127
|
-
event: GraphEvents.ON_RUN_STEP,
|
|
128
|
-
data: {
|
|
129
|
-
...runStep,
|
|
130
|
-
index: stepIndices.get(stepId) ?? 0,
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return stepId;
|
|
135
|
-
}
|
|
136
|
-
);
|
|
137
|
-
graph.dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
138
|
-
async (id, delta) => {
|
|
139
|
-
aggregateContent({
|
|
140
|
-
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
141
|
-
data: { id, delta } as t.MessageDeltaEvent,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
);
|
|
145
|
-
return { contentParts };
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const finalToolCallResponseMetadata = { finish_reason: 'tool_calls' };
|
|
149
|
-
const openAIResponsesToolCallMetadata = {
|
|
150
|
-
[STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY]:
|
|
151
|
-
OPENAI_RESPONSES_STREAMED_TOOL_CALL_ADAPTER,
|
|
152
|
-
};
|
|
153
|
-
const bedrockConverseToolCallMetadata = {
|
|
154
|
-
[STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY]:
|
|
155
|
-
BEDROCK_CONVERSE_STREAMED_TOOL_CALL_ADAPTER,
|
|
156
|
-
};
|
|
157
|
-
const googleOnArrivalToolCallMetadata = {
|
|
158
|
-
[STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY]: GOOGLE_STREAMED_TOOL_CALL_ADAPTER,
|
|
159
|
-
[STREAMED_TOOL_CALL_SEAL_METADATA_KEY]: { kind: 'all' },
|
|
160
|
-
};
|
|
161
|
-
const openAIChatSequentialToolCallMetadata = {
|
|
162
|
-
[STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY]:
|
|
163
|
-
OPENAI_CHAT_SEQUENTIAL_STREAMED_TOOL_CALL_ADAPTER,
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
describe('ChatModelStreamHandler eager event tool execution', () => {
|
|
167
|
-
afterEach(() => {
|
|
168
|
-
jest.restoreAllMocks();
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('prestarts a complete event-driven tool call from the stream', async () => {
|
|
172
|
-
const graph = createGraph();
|
|
173
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
174
|
-
jest
|
|
175
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
176
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
177
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
181
|
-
toolExecuteCalls.push(batch);
|
|
182
|
-
batch.resolve([
|
|
183
|
-
{
|
|
184
|
-
toolCallId: 'call_weather',
|
|
185
|
-
status: 'success',
|
|
186
|
-
content: 'sunny',
|
|
187
|
-
},
|
|
188
|
-
]);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
await new ChatModelStreamHandler().handle(
|
|
192
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
193
|
-
{
|
|
194
|
-
chunk: {
|
|
195
|
-
content: '',
|
|
196
|
-
tool_calls: [
|
|
197
|
-
{
|
|
198
|
-
id: 'call_weather',
|
|
199
|
-
name: 'weather',
|
|
200
|
-
args: { city: 'NYC' },
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
204
|
-
} as unknown as t.StreamChunk,
|
|
205
|
-
},
|
|
206
|
-
{ langgraph_node: 'agent' },
|
|
207
|
-
graph
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
211
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
212
|
-
id: 'call_weather',
|
|
213
|
-
name: 'weather',
|
|
214
|
-
args: { city: 'NYC' },
|
|
215
|
-
stepId: expect.stringMatching(/^step_/),
|
|
216
|
-
turn: 0,
|
|
217
|
-
});
|
|
218
|
-
expect(graph.eagerEventToolExecutions.get('call_weather')).toMatchObject({
|
|
219
|
-
toolCallId: 'call_weather',
|
|
220
|
-
toolName: 'weather',
|
|
221
|
-
args: { city: 'NYC' },
|
|
222
|
-
});
|
|
223
|
-
expect(graph.toolCallStepIds.has('call_weather')).toBe(true);
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('prestarts when subagent callback forwarding can execute tools without a handler registry', async () => {
|
|
227
|
-
const graph = createGraph({
|
|
228
|
-
handlerRegistry: undefined,
|
|
229
|
-
eventToolExecutionAvailable: true,
|
|
230
|
-
});
|
|
231
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
232
|
-
jest
|
|
233
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
234
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
235
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
239
|
-
toolExecuteCalls.push(batch);
|
|
240
|
-
batch.resolve([
|
|
241
|
-
{
|
|
242
|
-
toolCallId: 'call_weather',
|
|
243
|
-
status: 'success',
|
|
244
|
-
content: 'sunny',
|
|
245
|
-
},
|
|
246
|
-
]);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
await new ChatModelStreamHandler().handle(
|
|
250
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
251
|
-
{
|
|
252
|
-
chunk: {
|
|
253
|
-
content: '',
|
|
254
|
-
tool_calls: [
|
|
255
|
-
{
|
|
256
|
-
id: 'call_weather',
|
|
257
|
-
name: 'weather',
|
|
258
|
-
args: { city: 'NYC' },
|
|
259
|
-
},
|
|
260
|
-
],
|
|
261
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
262
|
-
} as unknown as t.StreamChunk,
|
|
263
|
-
},
|
|
264
|
-
{ langgraph_node: 'agent' },
|
|
265
|
-
graph
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
269
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
270
|
-
id: 'call_weather',
|
|
271
|
-
name: 'weather',
|
|
272
|
-
args: { city: 'NYC' },
|
|
273
|
-
stepId: expect.stringMatching(/^step_/),
|
|
274
|
-
turn: 0,
|
|
275
|
-
});
|
|
276
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it('does not prestart parseable tool calls before a final tool-call signal', async () => {
|
|
280
|
-
const graph = createGraph();
|
|
281
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
282
|
-
jest
|
|
283
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
284
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
285
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
289
|
-
toolExecuteCalls.push(batch);
|
|
290
|
-
batch.resolve([
|
|
291
|
-
{
|
|
292
|
-
toolCallId: 'call_weather',
|
|
293
|
-
status: 'success',
|
|
294
|
-
content: 'sunny',
|
|
295
|
-
},
|
|
296
|
-
]);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
const handler = new ChatModelStreamHandler();
|
|
300
|
-
const metadata = { langgraph_node: 'agent' };
|
|
301
|
-
|
|
302
|
-
await handler.handle(
|
|
303
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
304
|
-
{
|
|
305
|
-
chunk: {
|
|
306
|
-
content: '',
|
|
307
|
-
tool_calls: [
|
|
308
|
-
{
|
|
309
|
-
id: 'call_weather',
|
|
310
|
-
name: 'weather',
|
|
311
|
-
args: { city: 'N' },
|
|
312
|
-
},
|
|
313
|
-
],
|
|
314
|
-
} as unknown as t.StreamChunk,
|
|
315
|
-
},
|
|
316
|
-
metadata,
|
|
317
|
-
graph
|
|
318
|
-
);
|
|
319
|
-
|
|
320
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
321
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(false);
|
|
322
|
-
|
|
323
|
-
await handler.handle(
|
|
324
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
325
|
-
{
|
|
326
|
-
chunk: {
|
|
327
|
-
content: '',
|
|
328
|
-
tool_calls: [
|
|
329
|
-
{
|
|
330
|
-
id: 'call_weather',
|
|
331
|
-
name: 'weather',
|
|
332
|
-
args: { city: 'NYC' },
|
|
333
|
-
},
|
|
334
|
-
],
|
|
335
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
336
|
-
} as unknown as t.StreamChunk,
|
|
337
|
-
},
|
|
338
|
-
metadata,
|
|
339
|
-
graph
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
343
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
344
|
-
id: 'call_weather',
|
|
345
|
-
name: 'weather',
|
|
346
|
-
args: { city: 'NYC' },
|
|
347
|
-
stepId: expect.stringMatching(/^step_/),
|
|
348
|
-
turn: 0,
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
it('prestarts multiple complete event-driven tool calls as one batch', async () => {
|
|
353
|
-
const graph = createGraph({
|
|
354
|
-
getAgentContext: jest.fn(
|
|
355
|
-
(): Partial<AgentContext> => ({
|
|
356
|
-
provider: Providers.ANTHROPIC,
|
|
357
|
-
reasoningKey: 'reasoning',
|
|
358
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'calendar' }],
|
|
359
|
-
graphTools: [],
|
|
360
|
-
agentId: 'agent_1',
|
|
361
|
-
})
|
|
362
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
363
|
-
});
|
|
364
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
365
|
-
jest
|
|
366
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
367
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
368
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
372
|
-
toolExecuteCalls.push(batch);
|
|
373
|
-
batch.resolve(
|
|
374
|
-
batch.toolCalls.map((request) => ({
|
|
375
|
-
toolCallId: request.id,
|
|
376
|
-
status: 'success' as const,
|
|
377
|
-
content: `${request.name} result`,
|
|
378
|
-
}))
|
|
379
|
-
);
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
await new ChatModelStreamHandler().handle(
|
|
383
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
384
|
-
{
|
|
385
|
-
chunk: {
|
|
386
|
-
content: '',
|
|
387
|
-
tool_calls: [
|
|
388
|
-
{
|
|
389
|
-
id: 'call_weather',
|
|
390
|
-
name: 'weather',
|
|
391
|
-
args: { city: 'NYC' },
|
|
392
|
-
},
|
|
393
|
-
{
|
|
394
|
-
id: 'call_calendar',
|
|
395
|
-
name: 'calendar',
|
|
396
|
-
args: { date: 'today' },
|
|
397
|
-
},
|
|
398
|
-
],
|
|
399
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
400
|
-
} as unknown as t.StreamChunk,
|
|
401
|
-
},
|
|
402
|
-
{ langgraph_node: 'agent' },
|
|
403
|
-
graph
|
|
404
|
-
);
|
|
405
|
-
|
|
406
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
407
|
-
expect(toolExecuteCalls[0].toolCalls).toHaveLength(2);
|
|
408
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
409
|
-
expect.objectContaining({
|
|
410
|
-
id: 'call_weather',
|
|
411
|
-
name: 'weather',
|
|
412
|
-
args: { city: 'NYC' },
|
|
413
|
-
stepId: expect.stringMatching(/^step_/),
|
|
414
|
-
turn: 0,
|
|
415
|
-
}),
|
|
416
|
-
expect.objectContaining({
|
|
417
|
-
id: 'call_calendar',
|
|
418
|
-
name: 'calendar',
|
|
419
|
-
args: { date: 'today' },
|
|
420
|
-
stepId: expect.stringMatching(/^step_/),
|
|
421
|
-
turn: 0,
|
|
422
|
-
}),
|
|
423
|
-
]);
|
|
424
|
-
const weatherExecution = graph.eagerEventToolExecutions.get('call_weather');
|
|
425
|
-
const calendarExecution =
|
|
426
|
-
graph.eagerEventToolExecutions.get('call_calendar');
|
|
427
|
-
expect(weatherExecution).toMatchObject({
|
|
428
|
-
toolCallId: 'call_weather',
|
|
429
|
-
toolName: 'weather',
|
|
430
|
-
args: { city: 'NYC' },
|
|
431
|
-
});
|
|
432
|
-
expect(calendarExecution).toMatchObject({
|
|
433
|
-
toolCallId: 'call_calendar',
|
|
434
|
-
toolName: 'calendar',
|
|
435
|
-
args: { date: 'today' },
|
|
436
|
-
});
|
|
437
|
-
expect(weatherExecution?.promise).toBe(calendarExecution?.promise);
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
it('assigns same-tool eager turns in model emission order', async () => {
|
|
441
|
-
const graph = createGraph();
|
|
442
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
443
|
-
jest
|
|
444
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
445
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
446
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
450
|
-
toolExecuteCalls.push(batch);
|
|
451
|
-
batch.resolve(
|
|
452
|
-
batch.toolCalls.map((request) => ({
|
|
453
|
-
toolCallId: request.id,
|
|
454
|
-
status: 'success' as const,
|
|
455
|
-
content: `${request.args.city} weather`,
|
|
456
|
-
}))
|
|
457
|
-
);
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
await new ChatModelStreamHandler().handle(
|
|
461
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
462
|
-
{
|
|
463
|
-
chunk: {
|
|
464
|
-
content: '',
|
|
465
|
-
tool_calls: [
|
|
466
|
-
{
|
|
467
|
-
id: 'call_weather_1',
|
|
468
|
-
name: 'weather',
|
|
469
|
-
args: { city: 'NYC' },
|
|
470
|
-
},
|
|
471
|
-
{
|
|
472
|
-
id: 'call_weather_2',
|
|
473
|
-
name: 'weather',
|
|
474
|
-
args: { city: 'Boston' },
|
|
475
|
-
},
|
|
476
|
-
],
|
|
477
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
478
|
-
} as unknown as t.StreamChunk,
|
|
479
|
-
},
|
|
480
|
-
{ langgraph_node: 'agent' },
|
|
481
|
-
graph
|
|
482
|
-
);
|
|
483
|
-
|
|
484
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
485
|
-
expect(toolExecuteCalls[0].toolCalls.map((call) => call.turn)).toEqual([
|
|
486
|
-
0, 1,
|
|
487
|
-
]);
|
|
488
|
-
expect(graph.eagerEventToolUsageCount.get('weather')).toBe(2);
|
|
489
|
-
expect(
|
|
490
|
-
graph.eagerEventToolExecutions.get('call_weather_1')?.request.turn
|
|
491
|
-
).toBe(0);
|
|
492
|
-
expect(
|
|
493
|
-
graph.eagerEventToolExecutions.get('call_weather_2')?.request.turn
|
|
494
|
-
).toBe(1);
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
it('scopes eager turn reservation by agent', async () => {
|
|
498
|
-
const usageByAgent = new Map<string, Map<string, number>>();
|
|
499
|
-
const getUsageCount = (agentId?: string): Map<string, number> => {
|
|
500
|
-
const key = agentId ?? 'default';
|
|
501
|
-
let usage = usageByAgent.get(key);
|
|
502
|
-
if (usage == null) {
|
|
503
|
-
usage = new Map<string, number>();
|
|
504
|
-
usageByAgent.set(key, usage);
|
|
505
|
-
}
|
|
506
|
-
return usage;
|
|
507
|
-
};
|
|
508
|
-
const graph = createGraph({
|
|
509
|
-
getEagerEventToolUsageCount: jest.fn(getUsageCount),
|
|
510
|
-
getAgentContext: jest.fn(
|
|
511
|
-
(metadata?: Record<string, unknown>): AgentContext =>
|
|
512
|
-
({
|
|
513
|
-
provider: Providers.ANTHROPIC,
|
|
514
|
-
reasoningKey: 'reasoning',
|
|
515
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
516
|
-
graphTools: [],
|
|
517
|
-
agentId:
|
|
518
|
-
metadata?.langgraph_node === 'agent_2' ? 'agent_2' : 'agent_1',
|
|
519
|
-
}) as unknown as AgentContext
|
|
520
|
-
),
|
|
521
|
-
});
|
|
522
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
523
|
-
jest
|
|
524
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
525
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
526
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
530
|
-
toolExecuteCalls.push(batch);
|
|
531
|
-
batch.resolve(
|
|
532
|
-
batch.toolCalls.map((request) => ({
|
|
533
|
-
toolCallId: request.id,
|
|
534
|
-
status: 'success' as const,
|
|
535
|
-
content: 'ok',
|
|
536
|
-
}))
|
|
537
|
-
);
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
const handler = new ChatModelStreamHandler();
|
|
541
|
-
await handler.handle(
|
|
542
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
543
|
-
{
|
|
544
|
-
chunk: {
|
|
545
|
-
content: '',
|
|
546
|
-
tool_calls: [
|
|
547
|
-
{
|
|
548
|
-
id: 'call_agent_1_weather',
|
|
549
|
-
name: 'weather',
|
|
550
|
-
args: { city: 'NYC' },
|
|
551
|
-
},
|
|
552
|
-
],
|
|
553
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
554
|
-
} as unknown as t.StreamChunk,
|
|
555
|
-
},
|
|
556
|
-
{ langgraph_node: 'agent_1' },
|
|
557
|
-
graph
|
|
558
|
-
);
|
|
559
|
-
await handler.handle(
|
|
560
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
561
|
-
{
|
|
562
|
-
chunk: {
|
|
563
|
-
content: '',
|
|
564
|
-
tool_calls: [
|
|
565
|
-
{
|
|
566
|
-
id: 'call_agent_2_weather',
|
|
567
|
-
name: 'weather',
|
|
568
|
-
args: { city: 'Boston' },
|
|
569
|
-
},
|
|
570
|
-
],
|
|
571
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
572
|
-
} as unknown as t.StreamChunk,
|
|
573
|
-
},
|
|
574
|
-
{ langgraph_node: 'agent_2' },
|
|
575
|
-
graph
|
|
576
|
-
);
|
|
577
|
-
|
|
578
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
579
|
-
expect(toolExecuteCalls.map((call) => call.toolCalls[0].turn)).toEqual([
|
|
580
|
-
0, 0,
|
|
581
|
-
]);
|
|
582
|
-
expect(usageByAgent.get('agent_1')?.get('weather')).toBe(1);
|
|
583
|
-
expect(usageByAgent.get('agent_2')?.get('weather')).toBe(1);
|
|
584
|
-
expect(
|
|
585
|
-
graph.eagerEventToolExecutions.get('call_agent_1_weather')?.request.turn
|
|
586
|
-
).toBe(0);
|
|
587
|
-
expect(
|
|
588
|
-
graph.eagerEventToolExecutions.get('call_agent_2_weather')?.request.turn
|
|
589
|
-
).toBe(0);
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
it('skips eager for the whole batch if any call is not request-plannable', async () => {
|
|
593
|
-
const graph = createGraph();
|
|
594
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
595
|
-
jest
|
|
596
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
597
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
598
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
602
|
-
toolExecuteCalls.push(batch);
|
|
603
|
-
batch.resolve([]);
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
await new ChatModelStreamHandler().handle(
|
|
607
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
608
|
-
{
|
|
609
|
-
chunk: {
|
|
610
|
-
content: '',
|
|
611
|
-
tool_calls: [
|
|
612
|
-
{
|
|
613
|
-
id: 'call_weather_bad',
|
|
614
|
-
name: 'weather',
|
|
615
|
-
args: '{"city":',
|
|
616
|
-
},
|
|
617
|
-
{
|
|
618
|
-
id: 'call_weather_good',
|
|
619
|
-
name: 'weather',
|
|
620
|
-
args: { city: 'Boston' },
|
|
621
|
-
},
|
|
622
|
-
],
|
|
623
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
624
|
-
} as unknown as t.StreamChunk,
|
|
625
|
-
},
|
|
626
|
-
{ langgraph_node: 'agent' },
|
|
627
|
-
graph
|
|
628
|
-
);
|
|
629
|
-
|
|
630
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
631
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
632
|
-
expect(graph.eagerEventToolUsageCount.size).toBe(0);
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
it('records complete chunk-only tool calls after creating a tool step', async () => {
|
|
636
|
-
const graph = createGraph();
|
|
637
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
638
|
-
jest
|
|
639
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
640
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
641
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
645
|
-
toolExecuteCalls.push(batch);
|
|
646
|
-
batch.resolve([
|
|
647
|
-
{
|
|
648
|
-
toolCallId: 'call_weather',
|
|
649
|
-
status: 'success',
|
|
650
|
-
content: 'sunny',
|
|
651
|
-
},
|
|
652
|
-
]);
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
await new ChatModelStreamHandler().handle(
|
|
656
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
657
|
-
{
|
|
658
|
-
chunk: {
|
|
659
|
-
content: '',
|
|
660
|
-
tool_call_chunks: [
|
|
661
|
-
{
|
|
662
|
-
id: 'call_weather',
|
|
663
|
-
name: 'weather',
|
|
664
|
-
args: '{"city":"NYC"}',
|
|
665
|
-
index: 0,
|
|
666
|
-
},
|
|
667
|
-
],
|
|
668
|
-
} as unknown as t.StreamChunk,
|
|
669
|
-
},
|
|
670
|
-
{ langgraph_node: 'agent' },
|
|
671
|
-
graph
|
|
672
|
-
);
|
|
673
|
-
|
|
674
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
675
|
-
expect(graph.toolCallStepIds.has('call_weather')).toBe(true);
|
|
676
|
-
expect(
|
|
677
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
678
|
-
).toBe('{"city":"NYC"}');
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
it('prestarts OpenAI Responses streamed tool calls on explicit arguments done', async () => {
|
|
682
|
-
const graph = createGraph({
|
|
683
|
-
getAgentContext: jest.fn(
|
|
684
|
-
(): Partial<AgentContext> => ({
|
|
685
|
-
provider: Providers.OPENAI,
|
|
686
|
-
reasoningKey: 'reasoning_content',
|
|
687
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
688
|
-
graphTools: [],
|
|
689
|
-
agentId: 'agent_1',
|
|
690
|
-
})
|
|
691
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
692
|
-
});
|
|
693
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
694
|
-
jest
|
|
695
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
696
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
697
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
698
|
-
return;
|
|
699
|
-
}
|
|
700
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
701
|
-
toolExecuteCalls.push(batch);
|
|
702
|
-
batch.resolve([
|
|
703
|
-
{
|
|
704
|
-
toolCallId: 'call_weather',
|
|
705
|
-
status: 'success',
|
|
706
|
-
content: 'sunny',
|
|
707
|
-
},
|
|
708
|
-
]);
|
|
709
|
-
});
|
|
710
|
-
|
|
711
|
-
const handler = new ChatModelStreamHandler();
|
|
712
|
-
const metadata = { langgraph_node: 'agent' };
|
|
713
|
-
|
|
714
|
-
await handler.handle(
|
|
715
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
716
|
-
{
|
|
717
|
-
chunk: {
|
|
718
|
-
content: '',
|
|
719
|
-
tool_call_chunks: [
|
|
720
|
-
{
|
|
721
|
-
id: 'call_weather',
|
|
722
|
-
name: 'weather',
|
|
723
|
-
args: '',
|
|
724
|
-
index: 0,
|
|
725
|
-
},
|
|
726
|
-
],
|
|
727
|
-
response_metadata: openAIResponsesToolCallMetadata,
|
|
728
|
-
} as unknown as t.StreamChunk,
|
|
729
|
-
},
|
|
730
|
-
metadata,
|
|
731
|
-
graph
|
|
732
|
-
);
|
|
733
|
-
|
|
734
|
-
await handler.handle(
|
|
735
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
736
|
-
{
|
|
737
|
-
chunk: {
|
|
738
|
-
content: '',
|
|
739
|
-
tool_call_chunks: [
|
|
740
|
-
{
|
|
741
|
-
args: '{"city":"N',
|
|
742
|
-
index: 0,
|
|
743
|
-
},
|
|
744
|
-
],
|
|
745
|
-
response_metadata: openAIResponsesToolCallMetadata,
|
|
746
|
-
} as unknown as t.StreamChunk,
|
|
747
|
-
},
|
|
748
|
-
metadata,
|
|
749
|
-
graph
|
|
750
|
-
);
|
|
751
|
-
|
|
752
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
753
|
-
|
|
754
|
-
await handler.handle(
|
|
755
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
756
|
-
{
|
|
757
|
-
chunk: {
|
|
758
|
-
content: '',
|
|
759
|
-
tool_call_chunks: [
|
|
760
|
-
{
|
|
761
|
-
id: 'call_weather',
|
|
762
|
-
args: '{"city":"NYC"}',
|
|
763
|
-
index: 0,
|
|
764
|
-
},
|
|
765
|
-
],
|
|
766
|
-
response_metadata: {
|
|
767
|
-
...openAIResponsesToolCallMetadata,
|
|
768
|
-
[STREAMED_TOOL_CALL_SEAL_METADATA_KEY]: {
|
|
769
|
-
kind: 'single',
|
|
770
|
-
id: 'call_weather',
|
|
771
|
-
index: 0,
|
|
772
|
-
},
|
|
773
|
-
},
|
|
774
|
-
} as unknown as t.StreamChunk,
|
|
775
|
-
},
|
|
776
|
-
metadata,
|
|
777
|
-
graph
|
|
778
|
-
);
|
|
779
|
-
|
|
780
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
781
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
782
|
-
id: 'call_weather',
|
|
783
|
-
name: 'weather',
|
|
784
|
-
args: { city: 'NYC' },
|
|
785
|
-
stepId: expect.stringMatching(/^step_/),
|
|
786
|
-
turn: 0,
|
|
787
|
-
});
|
|
788
|
-
expect(
|
|
789
|
-
graph.eagerEventToolCallChunks.has(chunkStateKey('step-key', 0))
|
|
790
|
-
).toBe(false);
|
|
791
|
-
});
|
|
792
|
-
|
|
793
|
-
it('keeps OpenAI Chat Completions streamed chunks on the final tool_calls path', async () => {
|
|
794
|
-
const graph = createGraph({
|
|
795
|
-
getAgentContext: jest.fn(
|
|
796
|
-
(): Partial<AgentContext> => ({
|
|
797
|
-
provider: Providers.OPENAI,
|
|
798
|
-
reasoningKey: 'reasoning_content',
|
|
799
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
800
|
-
graphTools: [],
|
|
801
|
-
agentId: 'agent_1',
|
|
802
|
-
})
|
|
803
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
804
|
-
});
|
|
805
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
806
|
-
jest
|
|
807
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
808
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
809
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
810
|
-
return;
|
|
811
|
-
}
|
|
812
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
813
|
-
toolExecuteCalls.push(batch);
|
|
814
|
-
batch.resolve(
|
|
815
|
-
batch.toolCalls.map((call) => ({
|
|
816
|
-
toolCallId: call.id,
|
|
817
|
-
status: 'success',
|
|
818
|
-
content: `ok ${call.name}`,
|
|
819
|
-
}))
|
|
820
|
-
);
|
|
821
|
-
});
|
|
822
|
-
|
|
823
|
-
const handler = new ChatModelStreamHandler();
|
|
824
|
-
const metadata = { langgraph_node: 'agent' };
|
|
825
|
-
|
|
826
|
-
await handler.handle(
|
|
827
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
828
|
-
{
|
|
829
|
-
chunk: {
|
|
830
|
-
content: '',
|
|
831
|
-
tool_call_chunks: [
|
|
832
|
-
{
|
|
833
|
-
id: 'call_weather',
|
|
834
|
-
name: 'weather',
|
|
835
|
-
args: '{"city":"NYC"}',
|
|
836
|
-
index: 0,
|
|
837
|
-
},
|
|
838
|
-
],
|
|
839
|
-
} as unknown as t.StreamChunk,
|
|
840
|
-
},
|
|
841
|
-
metadata,
|
|
842
|
-
graph
|
|
843
|
-
);
|
|
844
|
-
await handler.handle(
|
|
845
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
846
|
-
{
|
|
847
|
-
chunk: {
|
|
848
|
-
content: '',
|
|
849
|
-
tool_call_chunks: [
|
|
850
|
-
{
|
|
851
|
-
id: 'call_stock',
|
|
852
|
-
name: 'stock',
|
|
853
|
-
args: '{"ticker":"CH"}',
|
|
854
|
-
index: 1,
|
|
855
|
-
},
|
|
856
|
-
],
|
|
857
|
-
} as unknown as t.StreamChunk,
|
|
858
|
-
},
|
|
859
|
-
metadata,
|
|
860
|
-
graph
|
|
861
|
-
);
|
|
862
|
-
|
|
863
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
864
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
865
|
-
|
|
866
|
-
await handler.handle(
|
|
867
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
868
|
-
{
|
|
869
|
-
chunk: {
|
|
870
|
-
content: '',
|
|
871
|
-
tool_calls: [
|
|
872
|
-
{
|
|
873
|
-
id: 'call_weather',
|
|
874
|
-
name: 'weather',
|
|
875
|
-
args: { city: 'NYC' },
|
|
876
|
-
},
|
|
877
|
-
{
|
|
878
|
-
id: 'call_stock',
|
|
879
|
-
name: 'stock',
|
|
880
|
-
args: { ticker: 'CH' },
|
|
881
|
-
},
|
|
882
|
-
],
|
|
883
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
884
|
-
} as unknown as t.StreamChunk,
|
|
885
|
-
},
|
|
886
|
-
metadata,
|
|
887
|
-
graph
|
|
888
|
-
);
|
|
889
|
-
|
|
890
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
891
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
892
|
-
expect.objectContaining({
|
|
893
|
-
id: 'call_weather',
|
|
894
|
-
args: { city: 'NYC' },
|
|
895
|
-
turn: 0,
|
|
896
|
-
}),
|
|
897
|
-
expect.objectContaining({
|
|
898
|
-
id: 'call_stock',
|
|
899
|
-
args: { ticker: 'CH' },
|
|
900
|
-
turn: 0,
|
|
901
|
-
}),
|
|
902
|
-
]);
|
|
903
|
-
});
|
|
904
|
-
|
|
905
|
-
it('prestarts final tool calls even when the final chunk also has tool-call chunks', async () => {
|
|
906
|
-
const graph = createGraph();
|
|
907
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
908
|
-
jest
|
|
909
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
910
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
911
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
912
|
-
return;
|
|
913
|
-
}
|
|
914
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
915
|
-
toolExecuteCalls.push(batch);
|
|
916
|
-
batch.resolve([
|
|
917
|
-
{
|
|
918
|
-
toolCallId: 'call_weather',
|
|
919
|
-
status: 'success',
|
|
920
|
-
content: 'sunny',
|
|
921
|
-
},
|
|
922
|
-
]);
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
await new ChatModelStreamHandler().handle(
|
|
926
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
927
|
-
{
|
|
928
|
-
chunk: {
|
|
929
|
-
content: '',
|
|
930
|
-
tool_calls: [
|
|
931
|
-
{
|
|
932
|
-
id: 'call_weather',
|
|
933
|
-
name: 'weather',
|
|
934
|
-
args: { city: 'NYC' },
|
|
935
|
-
},
|
|
936
|
-
],
|
|
937
|
-
tool_call_chunks: [
|
|
938
|
-
{
|
|
939
|
-
index: 0,
|
|
940
|
-
id: 'call_weather',
|
|
941
|
-
name: 'weather',
|
|
942
|
-
args: '{"city":"NYC"}',
|
|
943
|
-
},
|
|
944
|
-
],
|
|
945
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
946
|
-
} as unknown as t.StreamChunk,
|
|
947
|
-
},
|
|
948
|
-
{ langgraph_node: 'agent' },
|
|
949
|
-
graph
|
|
950
|
-
);
|
|
951
|
-
|
|
952
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
953
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
954
|
-
id: 'call_weather',
|
|
955
|
-
name: 'weather',
|
|
956
|
-
args: { city: 'NYC' },
|
|
957
|
-
turn: 0,
|
|
958
|
-
});
|
|
959
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
960
|
-
});
|
|
961
|
-
|
|
962
|
-
it('waits for final tool calls before prestarting streamed chunk calls', async () => {
|
|
963
|
-
const graph = createGraph();
|
|
964
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
965
|
-
jest
|
|
966
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
967
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
968
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
969
|
-
return;
|
|
970
|
-
}
|
|
971
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
972
|
-
toolExecuteCalls.push(batch);
|
|
973
|
-
batch.resolve([
|
|
974
|
-
{
|
|
975
|
-
toolCallId: 'call_weather',
|
|
976
|
-
status: 'success',
|
|
977
|
-
content: 'sunny',
|
|
978
|
-
},
|
|
979
|
-
]);
|
|
980
|
-
});
|
|
981
|
-
|
|
982
|
-
const handler = new ChatModelStreamHandler();
|
|
983
|
-
|
|
984
|
-
await handler.handle(
|
|
985
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
986
|
-
{
|
|
987
|
-
chunk: {
|
|
988
|
-
content: '',
|
|
989
|
-
tool_calls: [
|
|
990
|
-
{
|
|
991
|
-
id: 'call_weather',
|
|
992
|
-
name: 'weather',
|
|
993
|
-
args: {},
|
|
994
|
-
},
|
|
995
|
-
],
|
|
996
|
-
tool_call_chunks: [
|
|
997
|
-
{
|
|
998
|
-
id: 'call_weather',
|
|
999
|
-
name: 'weather',
|
|
1000
|
-
args: '',
|
|
1001
|
-
index: 0,
|
|
1002
|
-
},
|
|
1003
|
-
],
|
|
1004
|
-
} as unknown as t.StreamChunk,
|
|
1005
|
-
},
|
|
1006
|
-
{ langgraph_node: 'agent' },
|
|
1007
|
-
graph
|
|
1008
|
-
);
|
|
1009
|
-
|
|
1010
|
-
await handler.handle(
|
|
1011
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1012
|
-
{
|
|
1013
|
-
chunk: {
|
|
1014
|
-
content: '',
|
|
1015
|
-
tool_call_chunks: [
|
|
1016
|
-
{
|
|
1017
|
-
args: '{"city"',
|
|
1018
|
-
index: 0,
|
|
1019
|
-
},
|
|
1020
|
-
],
|
|
1021
|
-
} as unknown as t.StreamChunk,
|
|
1022
|
-
},
|
|
1023
|
-
{ langgraph_node: 'agent' },
|
|
1024
|
-
graph
|
|
1025
|
-
);
|
|
1026
|
-
|
|
1027
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1028
|
-
|
|
1029
|
-
await handler.handle(
|
|
1030
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1031
|
-
{
|
|
1032
|
-
chunk: {
|
|
1033
|
-
content: '',
|
|
1034
|
-
tool_call_chunks: [
|
|
1035
|
-
{
|
|
1036
|
-
args: ':"NYC"}',
|
|
1037
|
-
index: 0,
|
|
1038
|
-
},
|
|
1039
|
-
],
|
|
1040
|
-
} as unknown as t.StreamChunk,
|
|
1041
|
-
},
|
|
1042
|
-
{ langgraph_node: 'agent' },
|
|
1043
|
-
graph
|
|
1044
|
-
);
|
|
1045
|
-
|
|
1046
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1047
|
-
|
|
1048
|
-
await handler.handle(
|
|
1049
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1050
|
-
{
|
|
1051
|
-
chunk: {
|
|
1052
|
-
content: '',
|
|
1053
|
-
tool_calls: [
|
|
1054
|
-
{
|
|
1055
|
-
id: 'call_weather',
|
|
1056
|
-
name: 'weather',
|
|
1057
|
-
args: { city: 'NYC' },
|
|
1058
|
-
},
|
|
1059
|
-
],
|
|
1060
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
1061
|
-
} as unknown as t.StreamChunk,
|
|
1062
|
-
},
|
|
1063
|
-
{ langgraph_node: 'agent' },
|
|
1064
|
-
graph
|
|
1065
|
-
);
|
|
1066
|
-
|
|
1067
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
1068
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
1069
|
-
id: 'call_weather',
|
|
1070
|
-
name: 'weather',
|
|
1071
|
-
args: { city: 'NYC' },
|
|
1072
|
-
stepId: expect.stringMatching(/^step_/),
|
|
1073
|
-
turn: 0,
|
|
1074
|
-
});
|
|
1075
|
-
|
|
1076
|
-
await handler.handle(
|
|
1077
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1078
|
-
{
|
|
1079
|
-
chunk: {
|
|
1080
|
-
content: '',
|
|
1081
|
-
tool_calls: [
|
|
1082
|
-
{
|
|
1083
|
-
id: 'call_stock',
|
|
1084
|
-
name: 'stock',
|
|
1085
|
-
args: {},
|
|
1086
|
-
},
|
|
1087
|
-
],
|
|
1088
|
-
tool_call_chunks: [
|
|
1089
|
-
{
|
|
1090
|
-
id: 'call_stock',
|
|
1091
|
-
name: 'stock',
|
|
1092
|
-
args: '',
|
|
1093
|
-
index: 0,
|
|
1094
|
-
},
|
|
1095
|
-
],
|
|
1096
|
-
} as unknown as t.StreamChunk,
|
|
1097
|
-
},
|
|
1098
|
-
{ langgraph_node: 'agent' },
|
|
1099
|
-
graph
|
|
1100
|
-
);
|
|
1101
|
-
|
|
1102
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
1103
|
-
|
|
1104
|
-
await handler.handle(
|
|
1105
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1106
|
-
{
|
|
1107
|
-
chunk: {
|
|
1108
|
-
content: '',
|
|
1109
|
-
tool_call_chunks: [
|
|
1110
|
-
{
|
|
1111
|
-
args: '{"ticker":"CH"}',
|
|
1112
|
-
index: 0,
|
|
1113
|
-
},
|
|
1114
|
-
],
|
|
1115
|
-
} as unknown as t.StreamChunk,
|
|
1116
|
-
},
|
|
1117
|
-
{ langgraph_node: 'agent' },
|
|
1118
|
-
graph
|
|
1119
|
-
);
|
|
1120
|
-
|
|
1121
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
1122
|
-
|
|
1123
|
-
await handler.handle(
|
|
1124
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1125
|
-
{
|
|
1126
|
-
chunk: {
|
|
1127
|
-
content: '',
|
|
1128
|
-
tool_calls: [
|
|
1129
|
-
{
|
|
1130
|
-
id: 'call_stock',
|
|
1131
|
-
name: 'stock',
|
|
1132
|
-
args: { ticker: 'CH' },
|
|
1133
|
-
},
|
|
1134
|
-
],
|
|
1135
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
1136
|
-
} as unknown as t.StreamChunk,
|
|
1137
|
-
},
|
|
1138
|
-
{ langgraph_node: 'agent' },
|
|
1139
|
-
graph
|
|
1140
|
-
);
|
|
1141
|
-
|
|
1142
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
1143
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
1144
|
-
id: 'call_stock',
|
|
1145
|
-
name: 'stock',
|
|
1146
|
-
args: { ticker: 'CH' },
|
|
1147
|
-
stepId: expect.stringMatching(/^step_/),
|
|
1148
|
-
turn: 0,
|
|
1149
|
-
});
|
|
1150
|
-
});
|
|
1151
|
-
|
|
1152
|
-
it('preserves repeated adjacent argument deltas', async () => {
|
|
1153
|
-
const graph = createGraph();
|
|
1154
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
1155
|
-
jest
|
|
1156
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
1157
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
1158
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
1159
|
-
return;
|
|
1160
|
-
}
|
|
1161
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
1162
|
-
toolExecuteCalls.push(batch);
|
|
1163
|
-
batch.resolve([
|
|
1164
|
-
{
|
|
1165
|
-
toolCallId: 'call_repeat',
|
|
1166
|
-
status: 'success',
|
|
1167
|
-
content: 'ok',
|
|
1168
|
-
},
|
|
1169
|
-
]);
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
const handler = new ChatModelStreamHandler();
|
|
1173
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1174
|
-
|
|
1175
|
-
await handler.handle(
|
|
1176
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1177
|
-
{
|
|
1178
|
-
chunk: {
|
|
1179
|
-
content: '',
|
|
1180
|
-
tool_call_chunks: [
|
|
1181
|
-
{
|
|
1182
|
-
id: 'call_repeat',
|
|
1183
|
-
name: 'weather',
|
|
1184
|
-
args: '{"word":"b',
|
|
1185
|
-
index: 0,
|
|
1186
|
-
},
|
|
1187
|
-
],
|
|
1188
|
-
} as unknown as t.StreamChunk,
|
|
1189
|
-
},
|
|
1190
|
-
metadata,
|
|
1191
|
-
graph
|
|
1192
|
-
);
|
|
1193
|
-
|
|
1194
|
-
await handler.handle(
|
|
1195
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1196
|
-
{
|
|
1197
|
-
chunk: {
|
|
1198
|
-
content: '',
|
|
1199
|
-
tool_call_chunks: [
|
|
1200
|
-
{
|
|
1201
|
-
args: 'o',
|
|
1202
|
-
index: 0,
|
|
1203
|
-
},
|
|
1204
|
-
],
|
|
1205
|
-
} as unknown as t.StreamChunk,
|
|
1206
|
-
},
|
|
1207
|
-
metadata,
|
|
1208
|
-
graph
|
|
1209
|
-
);
|
|
1210
|
-
|
|
1211
|
-
await handler.handle(
|
|
1212
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1213
|
-
{
|
|
1214
|
-
chunk: {
|
|
1215
|
-
content: '',
|
|
1216
|
-
tool_call_chunks: [
|
|
1217
|
-
{
|
|
1218
|
-
args: 'o',
|
|
1219
|
-
index: 0,
|
|
1220
|
-
},
|
|
1221
|
-
],
|
|
1222
|
-
} as unknown as t.StreamChunk,
|
|
1223
|
-
},
|
|
1224
|
-
metadata,
|
|
1225
|
-
graph
|
|
1226
|
-
);
|
|
1227
|
-
|
|
1228
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1229
|
-
|
|
1230
|
-
await handler.handle(
|
|
1231
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1232
|
-
{
|
|
1233
|
-
chunk: {
|
|
1234
|
-
content: '',
|
|
1235
|
-
tool_call_chunks: [
|
|
1236
|
-
{
|
|
1237
|
-
args: 'k"}',
|
|
1238
|
-
index: 0,
|
|
1239
|
-
},
|
|
1240
|
-
],
|
|
1241
|
-
} as unknown as t.StreamChunk,
|
|
1242
|
-
},
|
|
1243
|
-
metadata,
|
|
1244
|
-
graph
|
|
1245
|
-
);
|
|
1246
|
-
|
|
1247
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1248
|
-
expect(
|
|
1249
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1250
|
-
).toBe('{"word":"book"}');
|
|
1251
|
-
|
|
1252
|
-
await handler.handle(
|
|
1253
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1254
|
-
{
|
|
1255
|
-
chunk: {
|
|
1256
|
-
content: '',
|
|
1257
|
-
tool_calls: [
|
|
1258
|
-
{
|
|
1259
|
-
id: 'call_repeat',
|
|
1260
|
-
name: 'weather',
|
|
1261
|
-
args: { word: 'book' },
|
|
1262
|
-
},
|
|
1263
|
-
],
|
|
1264
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
1265
|
-
} as unknown as t.StreamChunk,
|
|
1266
|
-
},
|
|
1267
|
-
metadata,
|
|
1268
|
-
graph
|
|
1269
|
-
);
|
|
1270
|
-
|
|
1271
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
1272
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
1273
|
-
id: 'call_repeat',
|
|
1274
|
-
name: 'weather',
|
|
1275
|
-
args: { word: 'book' },
|
|
1276
|
-
stepId: expect.stringMatching(/^step_/),
|
|
1277
|
-
turn: 0,
|
|
1278
|
-
});
|
|
1279
|
-
});
|
|
1280
|
-
|
|
1281
|
-
it('preserves identical incremental argument fragments', async () => {
|
|
1282
|
-
const graph = createGraph();
|
|
1283
|
-
const handler = new ChatModelStreamHandler();
|
|
1284
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1285
|
-
|
|
1286
|
-
await handler.handle(
|
|
1287
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1288
|
-
{
|
|
1289
|
-
chunk: {
|
|
1290
|
-
content: '',
|
|
1291
|
-
tool_call_chunks: [
|
|
1292
|
-
{
|
|
1293
|
-
id: 'call_repeat',
|
|
1294
|
-
name: 'weather',
|
|
1295
|
-
args: 'o',
|
|
1296
|
-
index: 0,
|
|
1297
|
-
},
|
|
1298
|
-
],
|
|
1299
|
-
} as unknown as t.StreamChunk,
|
|
1300
|
-
},
|
|
1301
|
-
metadata,
|
|
1302
|
-
graph
|
|
1303
|
-
);
|
|
1304
|
-
|
|
1305
|
-
await handler.handle(
|
|
1306
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1307
|
-
{
|
|
1308
|
-
chunk: {
|
|
1309
|
-
content: '',
|
|
1310
|
-
tool_call_chunks: [
|
|
1311
|
-
{
|
|
1312
|
-
args: 'o',
|
|
1313
|
-
index: 0,
|
|
1314
|
-
},
|
|
1315
|
-
],
|
|
1316
|
-
} as unknown as t.StreamChunk,
|
|
1317
|
-
},
|
|
1318
|
-
metadata,
|
|
1319
|
-
graph
|
|
1320
|
-
);
|
|
1321
|
-
|
|
1322
|
-
expect(
|
|
1323
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1324
|
-
).toBe('oo');
|
|
1325
|
-
});
|
|
1326
|
-
|
|
1327
|
-
it('deduplicates repeated observed multi-character fragments', async () => {
|
|
1328
|
-
const graph = createGraph();
|
|
1329
|
-
const handler = new ChatModelStreamHandler();
|
|
1330
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1331
|
-
|
|
1332
|
-
await handler.handle(
|
|
1333
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1334
|
-
{
|
|
1335
|
-
chunk: {
|
|
1336
|
-
content: '',
|
|
1337
|
-
tool_call_chunks: [
|
|
1338
|
-
{
|
|
1339
|
-
id: 'call_repeat',
|
|
1340
|
-
name: 'weather',
|
|
1341
|
-
args: '{"titl',
|
|
1342
|
-
index: 0,
|
|
1343
|
-
},
|
|
1344
|
-
],
|
|
1345
|
-
} as unknown as t.StreamChunk,
|
|
1346
|
-
},
|
|
1347
|
-
metadata,
|
|
1348
|
-
graph
|
|
1349
|
-
);
|
|
1350
|
-
|
|
1351
|
-
await handler.handle(
|
|
1352
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1353
|
-
{
|
|
1354
|
-
chunk: {
|
|
1355
|
-
content: '',
|
|
1356
|
-
tool_call_chunks: [
|
|
1357
|
-
{
|
|
1358
|
-
args: '{"titl',
|
|
1359
|
-
index: 0,
|
|
1360
|
-
},
|
|
1361
|
-
],
|
|
1362
|
-
} as unknown as t.StreamChunk,
|
|
1363
|
-
},
|
|
1364
|
-
metadata,
|
|
1365
|
-
graph
|
|
1366
|
-
);
|
|
1367
|
-
|
|
1368
|
-
expect(
|
|
1369
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1370
|
-
).toBe('{"titl');
|
|
1371
|
-
});
|
|
1372
|
-
|
|
1373
|
-
it('does not prestart from cumulative streamed args before final tool calls', async () => {
|
|
1374
|
-
const graph = createGraph();
|
|
1375
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
1376
|
-
jest
|
|
1377
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
1378
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
1379
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
1380
|
-
return;
|
|
1381
|
-
}
|
|
1382
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
1383
|
-
toolExecuteCalls.push(batch);
|
|
1384
|
-
batch.resolve([
|
|
1385
|
-
{
|
|
1386
|
-
toolCallId: 'call_weather',
|
|
1387
|
-
status: 'success',
|
|
1388
|
-
content: 'sunny',
|
|
1389
|
-
},
|
|
1390
|
-
]);
|
|
1391
|
-
});
|
|
1392
|
-
|
|
1393
|
-
const handler = new ChatModelStreamHandler();
|
|
1394
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1395
|
-
|
|
1396
|
-
await handler.handle(
|
|
1397
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1398
|
-
{
|
|
1399
|
-
chunk: {
|
|
1400
|
-
content: '',
|
|
1401
|
-
tool_call_chunks: [
|
|
1402
|
-
{
|
|
1403
|
-
id: 'call_weather',
|
|
1404
|
-
name: 'weather',
|
|
1405
|
-
args: '{"ci',
|
|
1406
|
-
index: 0,
|
|
1407
|
-
},
|
|
1408
|
-
],
|
|
1409
|
-
} as unknown as t.StreamChunk,
|
|
1410
|
-
},
|
|
1411
|
-
metadata,
|
|
1412
|
-
graph
|
|
1413
|
-
);
|
|
1414
|
-
|
|
1415
|
-
await handler.handle(
|
|
1416
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1417
|
-
{
|
|
1418
|
-
chunk: {
|
|
1419
|
-
content: '',
|
|
1420
|
-
tool_call_chunks: [
|
|
1421
|
-
{
|
|
1422
|
-
args: '{"city":"N',
|
|
1423
|
-
index: 0,
|
|
1424
|
-
},
|
|
1425
|
-
],
|
|
1426
|
-
} as unknown as t.StreamChunk,
|
|
1427
|
-
},
|
|
1428
|
-
metadata,
|
|
1429
|
-
graph
|
|
1430
|
-
);
|
|
1431
|
-
|
|
1432
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1433
|
-
|
|
1434
|
-
await handler.handle(
|
|
1435
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1436
|
-
{
|
|
1437
|
-
chunk: {
|
|
1438
|
-
content: '',
|
|
1439
|
-
tool_call_chunks: [
|
|
1440
|
-
{
|
|
1441
|
-
args: '{"city":"NYC"}',
|
|
1442
|
-
index: 0,
|
|
1443
|
-
},
|
|
1444
|
-
],
|
|
1445
|
-
} as unknown as t.StreamChunk,
|
|
1446
|
-
},
|
|
1447
|
-
metadata,
|
|
1448
|
-
graph
|
|
1449
|
-
);
|
|
1450
|
-
|
|
1451
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1452
|
-
|
|
1453
|
-
await handler.handle(
|
|
1454
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1455
|
-
{
|
|
1456
|
-
chunk: {
|
|
1457
|
-
content: '',
|
|
1458
|
-
tool_call_chunks: [
|
|
1459
|
-
{
|
|
1460
|
-
args: '{"city":"NYC","unit":"C"}',
|
|
1461
|
-
index: 0,
|
|
1462
|
-
},
|
|
1463
|
-
],
|
|
1464
|
-
} as unknown as t.StreamChunk,
|
|
1465
|
-
},
|
|
1466
|
-
metadata,
|
|
1467
|
-
graph
|
|
1468
|
-
);
|
|
1469
|
-
|
|
1470
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
1471
|
-
expect(
|
|
1472
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1473
|
-
).toBe('{"city":"NYC","unit":"C"}');
|
|
1474
|
-
|
|
1475
|
-
await handler.handle(
|
|
1476
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1477
|
-
{
|
|
1478
|
-
chunk: {
|
|
1479
|
-
content: '',
|
|
1480
|
-
tool_calls: [
|
|
1481
|
-
{
|
|
1482
|
-
id: 'call_weather',
|
|
1483
|
-
name: 'weather',
|
|
1484
|
-
args: { city: 'NYC', unit: 'C' },
|
|
1485
|
-
},
|
|
1486
|
-
],
|
|
1487
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
1488
|
-
} as unknown as t.StreamChunk,
|
|
1489
|
-
},
|
|
1490
|
-
metadata,
|
|
1491
|
-
graph
|
|
1492
|
-
);
|
|
1493
|
-
|
|
1494
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
1495
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
1496
|
-
id: 'call_weather',
|
|
1497
|
-
name: 'weather',
|
|
1498
|
-
args: { city: 'NYC', unit: 'C' },
|
|
1499
|
-
stepId: expect.stringMatching(/^step_/),
|
|
1500
|
-
turn: 0,
|
|
1501
|
-
});
|
|
1502
|
-
});
|
|
1503
|
-
|
|
1504
|
-
it('merges overlapping cumulative streamed args without duplicating suffixes', async () => {
|
|
1505
|
-
const graph = createGraph();
|
|
1506
|
-
const handler = new ChatModelStreamHandler();
|
|
1507
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1508
|
-
|
|
1509
|
-
await handler.handle(
|
|
1510
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1511
|
-
{
|
|
1512
|
-
chunk: {
|
|
1513
|
-
content: '',
|
|
1514
|
-
tool_call_chunks: [
|
|
1515
|
-
{
|
|
1516
|
-
id: 'call_weather',
|
|
1517
|
-
name: 'weather',
|
|
1518
|
-
args: '{"tit',
|
|
1519
|
-
index: 0,
|
|
1520
|
-
},
|
|
1521
|
-
],
|
|
1522
|
-
} as unknown as t.StreamChunk,
|
|
1523
|
-
},
|
|
1524
|
-
metadata,
|
|
1525
|
-
graph
|
|
1526
|
-
);
|
|
1527
|
-
|
|
1528
|
-
await handler.handle(
|
|
1529
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1530
|
-
{
|
|
1531
|
-
chunk: {
|
|
1532
|
-
content: '',
|
|
1533
|
-
tool_call_chunks: [
|
|
1534
|
-
{
|
|
1535
|
-
args: '{"title":"alpha"',
|
|
1536
|
-
index: 0,
|
|
1537
|
-
},
|
|
1538
|
-
],
|
|
1539
|
-
} as unknown as t.StreamChunk,
|
|
1540
|
-
},
|
|
1541
|
-
metadata,
|
|
1542
|
-
graph
|
|
1543
|
-
);
|
|
1544
|
-
|
|
1545
|
-
await handler.handle(
|
|
1546
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1547
|
-
{
|
|
1548
|
-
chunk: {
|
|
1549
|
-
content: '',
|
|
1550
|
-
tool_call_chunks: [
|
|
1551
|
-
{
|
|
1552
|
-
args: 'le":"alpha","city":"NYC"}',
|
|
1553
|
-
index: 0,
|
|
1554
|
-
},
|
|
1555
|
-
],
|
|
1556
|
-
} as unknown as t.StreamChunk,
|
|
1557
|
-
},
|
|
1558
|
-
metadata,
|
|
1559
|
-
graph
|
|
1560
|
-
);
|
|
1561
|
-
|
|
1562
|
-
expect(
|
|
1563
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1564
|
-
).toBe('{"title":"alpha","city":"NYC"}');
|
|
1565
|
-
});
|
|
1566
|
-
|
|
1567
|
-
it('preserves repeated deltas from a reused stream chunk object', async () => {
|
|
1568
|
-
const graph = createGraph();
|
|
1569
|
-
const handler = new ChatModelStreamHandler();
|
|
1570
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1571
|
-
const reusableToolChunk: {
|
|
1572
|
-
id?: string;
|
|
1573
|
-
name?: string;
|
|
1574
|
-
args: string;
|
|
1575
|
-
index: number;
|
|
1576
|
-
} = {
|
|
1577
|
-
id: 'call_repeat',
|
|
1578
|
-
name: 'weather',
|
|
1579
|
-
args: '{"word":"b',
|
|
1580
|
-
index: 0,
|
|
1581
|
-
};
|
|
1582
|
-
const reusableChunk = {
|
|
1583
|
-
content: '',
|
|
1584
|
-
tool_call_chunks: [reusableToolChunk],
|
|
1585
|
-
} as unknown as t.StreamChunk;
|
|
1586
|
-
|
|
1587
|
-
await handler.handle(
|
|
1588
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1589
|
-
{ chunk: reusableChunk },
|
|
1590
|
-
metadata,
|
|
1591
|
-
graph
|
|
1592
|
-
);
|
|
1593
|
-
|
|
1594
|
-
reusableToolChunk.id = undefined;
|
|
1595
|
-
reusableToolChunk.name = undefined;
|
|
1596
|
-
reusableToolChunk.args = 'o';
|
|
1597
|
-
|
|
1598
|
-
await handler.handle(
|
|
1599
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1600
|
-
{ chunk: reusableChunk },
|
|
1601
|
-
metadata,
|
|
1602
|
-
graph
|
|
1603
|
-
);
|
|
1604
|
-
await handler.handle(
|
|
1605
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1606
|
-
{ chunk: reusableChunk },
|
|
1607
|
-
metadata,
|
|
1608
|
-
graph
|
|
1609
|
-
);
|
|
1610
|
-
|
|
1611
|
-
reusableToolChunk.args = 'k"}';
|
|
1612
|
-
|
|
1613
|
-
await handler.handle(
|
|
1614
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1615
|
-
{ chunk: reusableChunk },
|
|
1616
|
-
metadata,
|
|
1617
|
-
graph
|
|
1618
|
-
);
|
|
1619
|
-
|
|
1620
|
-
expect(
|
|
1621
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
1622
|
-
).toBe('{"word":"book"}');
|
|
1623
|
-
});
|
|
1624
|
-
|
|
1625
|
-
it('preserves repeated text deltas from a reused stream chunk object', async () => {
|
|
1626
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1627
|
-
async () => undefined
|
|
1628
|
-
);
|
|
1629
|
-
const graph = createGraph({
|
|
1630
|
-
dispatchMessageDelta,
|
|
1631
|
-
getAgentContext: jest.fn(
|
|
1632
|
-
(): Partial<AgentContext> => ({
|
|
1633
|
-
provider: Providers.OPENAI,
|
|
1634
|
-
reasoningKey: 'reasoning_content',
|
|
1635
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1636
|
-
toolDefinitions: [],
|
|
1637
|
-
graphTools: [],
|
|
1638
|
-
agentId: 'agent_1',
|
|
1639
|
-
})
|
|
1640
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1641
|
-
});
|
|
1642
|
-
const handler = new ChatModelStreamHandler();
|
|
1643
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1644
|
-
const reusableChunk = { content: 'ha' } as unknown as t.StreamChunk;
|
|
1645
|
-
|
|
1646
|
-
await handler.handle(
|
|
1647
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1648
|
-
{ chunk: reusableChunk },
|
|
1649
|
-
metadata,
|
|
1650
|
-
graph
|
|
1651
|
-
);
|
|
1652
|
-
await handler.handle(
|
|
1653
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1654
|
-
{ chunk: reusableChunk },
|
|
1655
|
-
metadata,
|
|
1656
|
-
graph
|
|
1657
|
-
);
|
|
1658
|
-
|
|
1659
|
-
expect(dispatchMessageDelta).toHaveBeenCalledTimes(2);
|
|
1660
|
-
expect(dispatchMessageDelta).toHaveBeenNthCalledWith(
|
|
1661
|
-
1,
|
|
1662
|
-
expect.stringMatching(/^step_/),
|
|
1663
|
-
{ content: [{ type: ContentTypes.TEXT, text: 'ha' }] },
|
|
1664
|
-
metadata
|
|
1665
|
-
);
|
|
1666
|
-
expect(dispatchMessageDelta).toHaveBeenNthCalledWith(
|
|
1667
|
-
2,
|
|
1668
|
-
expect.stringMatching(/^step_/),
|
|
1669
|
-
{ content: [{ type: ContentTypes.TEXT, text: 'ha' }] },
|
|
1670
|
-
metadata
|
|
1671
|
-
);
|
|
1672
|
-
});
|
|
1673
|
-
|
|
1674
|
-
it('defers disableStreaming mixed hidden reasoning and visible text chunks', async () => {
|
|
1675
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1676
|
-
async () => undefined
|
|
1677
|
-
);
|
|
1678
|
-
const dispatchReasoningDelta = jest.fn<
|
|
1679
|
-
StandardGraph['dispatchReasoningDelta']
|
|
1680
|
-
>(async () => undefined);
|
|
1681
|
-
const agentContext: Partial<AgentContext> = {
|
|
1682
|
-
provider: Providers.OPENAI,
|
|
1683
|
-
reasoningKey: 'reasoning_content',
|
|
1684
|
-
clientOptions: { disableStreaming: true } as t.OpenAIClientOptions,
|
|
1685
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1686
|
-
toolDefinitions: [],
|
|
1687
|
-
graphTools: [],
|
|
1688
|
-
agentId: 'agent_1',
|
|
1689
|
-
};
|
|
1690
|
-
const graph = createGraph({
|
|
1691
|
-
dispatchMessageDelta,
|
|
1692
|
-
dispatchReasoningDelta,
|
|
1693
|
-
getAgentContext: jest.fn(
|
|
1694
|
-
() => agentContext
|
|
1695
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1696
|
-
});
|
|
1697
|
-
const handler = new ChatModelStreamHandler();
|
|
1698
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1699
|
-
|
|
1700
|
-
const mixedChunk = {
|
|
1701
|
-
content: 'final answer',
|
|
1702
|
-
additional_kwargs: {
|
|
1703
|
-
reasoning_content: 'hidden reasoning',
|
|
1704
|
-
},
|
|
1705
|
-
} as unknown as t.StreamChunk;
|
|
1706
|
-
|
|
1707
|
-
await handler.handle(
|
|
1708
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1709
|
-
{ chunk: mixedChunk },
|
|
1710
|
-
metadata,
|
|
1711
|
-
graph
|
|
1712
|
-
);
|
|
1713
|
-
await handler.handle(
|
|
1714
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1715
|
-
{ chunk: mixedChunk },
|
|
1716
|
-
metadata,
|
|
1717
|
-
graph
|
|
1718
|
-
);
|
|
1719
|
-
|
|
1720
|
-
expect(dispatchReasoningDelta).not.toHaveBeenCalled();
|
|
1721
|
-
expect(dispatchMessageDelta).not.toHaveBeenCalled();
|
|
1722
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
1723
|
-
});
|
|
1724
|
-
|
|
1725
|
-
it('leaves mixed hidden reasoning and visible text chunks on the streaming path', async () => {
|
|
1726
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1727
|
-
async () => undefined
|
|
1728
|
-
);
|
|
1729
|
-
const dispatchReasoningDelta = jest.fn<
|
|
1730
|
-
StandardGraph['dispatchReasoningDelta']
|
|
1731
|
-
>(async () => undefined);
|
|
1732
|
-
const graph = createGraph({
|
|
1733
|
-
dispatchMessageDelta,
|
|
1734
|
-
dispatchReasoningDelta,
|
|
1735
|
-
getAgentContext: jest.fn(
|
|
1736
|
-
(): Partial<AgentContext> => ({
|
|
1737
|
-
provider: Providers.OPENAI,
|
|
1738
|
-
reasoningKey: 'reasoning_content',
|
|
1739
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1740
|
-
toolDefinitions: [],
|
|
1741
|
-
graphTools: [],
|
|
1742
|
-
agentId: 'agent_1',
|
|
1743
|
-
})
|
|
1744
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1745
|
-
});
|
|
1746
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1747
|
-
|
|
1748
|
-
await new ChatModelStreamHandler().handle(
|
|
1749
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1750
|
-
{
|
|
1751
|
-
chunk: {
|
|
1752
|
-
content: 'streamed answer',
|
|
1753
|
-
additional_kwargs: {
|
|
1754
|
-
reasoning_content: 'hidden reasoning',
|
|
1755
|
-
},
|
|
1756
|
-
} as unknown as t.StreamChunk,
|
|
1757
|
-
},
|
|
1758
|
-
metadata,
|
|
1759
|
-
graph
|
|
1760
|
-
);
|
|
1761
|
-
|
|
1762
|
-
expect(dispatchReasoningDelta).not.toHaveBeenCalled();
|
|
1763
|
-
expect(dispatchMessageDelta).toHaveBeenCalledTimes(1);
|
|
1764
|
-
expect(dispatchMessageDelta).toHaveBeenCalledWith(
|
|
1765
|
-
expect.stringMatching(/^step_/),
|
|
1766
|
-
{
|
|
1767
|
-
content: [{ type: ContentTypes.TEXT, text: 'streamed answer' }],
|
|
1768
|
-
},
|
|
1769
|
-
metadata
|
|
1770
|
-
);
|
|
1771
|
-
});
|
|
1772
|
-
|
|
1773
|
-
it('dispatches Gemini server-side tool context blocks from mixed stream content', async () => {
|
|
1774
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1775
|
-
async () => undefined
|
|
1776
|
-
);
|
|
1777
|
-
const graph = createGraph({
|
|
1778
|
-
dispatchMessageDelta,
|
|
1779
|
-
getAgentContext: jest.fn(
|
|
1780
|
-
(): Partial<AgentContext> => ({
|
|
1781
|
-
provider: Providers.GOOGLE,
|
|
1782
|
-
reasoningKey: 'reasoning',
|
|
1783
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1784
|
-
toolDefinitions: [],
|
|
1785
|
-
graphTools: [],
|
|
1786
|
-
agentId: 'agent_1',
|
|
1787
|
-
})
|
|
1788
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1789
|
-
});
|
|
1790
|
-
const handler = new ChatModelStreamHandler();
|
|
1791
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1792
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
1793
|
-
type: 'toolCall',
|
|
1794
|
-
toolCall: {
|
|
1795
|
-
id: 'server-search-1',
|
|
1796
|
-
name: 'google_search',
|
|
1797
|
-
args: {},
|
|
1798
|
-
},
|
|
1799
|
-
};
|
|
1800
|
-
const textPart: t.MessageContentComplex = {
|
|
1801
|
-
type: ContentTypes.TEXT,
|
|
1802
|
-
text: 'Search complete.',
|
|
1803
|
-
};
|
|
1804
|
-
const toolResponsePart: t.MessageContentComplex = {
|
|
1805
|
-
type: 'toolResponse',
|
|
1806
|
-
toolResponse: {
|
|
1807
|
-
id: 'server-search-1',
|
|
1808
|
-
name: 'google_search',
|
|
1809
|
-
response: { results: [] },
|
|
1810
|
-
},
|
|
1811
|
-
};
|
|
1812
|
-
|
|
1813
|
-
await handler.handle(
|
|
1814
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1815
|
-
{
|
|
1816
|
-
chunk: {
|
|
1817
|
-
content: [toolCallPart, textPart, toolResponsePart],
|
|
1818
|
-
} as unknown as t.StreamChunk,
|
|
1819
|
-
},
|
|
1820
|
-
metadata,
|
|
1821
|
-
graph
|
|
1822
|
-
);
|
|
1823
|
-
|
|
1824
|
-
expect(dispatchMessageDelta).toHaveBeenCalledTimes(3);
|
|
1825
|
-
expect(dispatchMessageDelta).toHaveBeenNthCalledWith(
|
|
1826
|
-
1,
|
|
1827
|
-
expect.stringMatching(/^step_/),
|
|
1828
|
-
{ content: [toolCallPart] },
|
|
1829
|
-
metadata
|
|
1830
|
-
);
|
|
1831
|
-
expect(dispatchMessageDelta).toHaveBeenNthCalledWith(
|
|
1832
|
-
2,
|
|
1833
|
-
expect.stringMatching(/^step_/),
|
|
1834
|
-
{ content: [textPart] },
|
|
1835
|
-
metadata
|
|
1836
|
-
);
|
|
1837
|
-
expect(dispatchMessageDelta).toHaveBeenNthCalledWith(
|
|
1838
|
-
3,
|
|
1839
|
-
expect.stringMatching(/^step_/),
|
|
1840
|
-
{ content: [toolResponsePart] },
|
|
1841
|
-
metadata
|
|
1842
|
-
);
|
|
1843
|
-
});
|
|
1844
|
-
|
|
1845
|
-
it('dispatches Gemini server-side tool context onto message steps after tool chunks', async () => {
|
|
1846
|
-
const messageDeltas: Array<{
|
|
1847
|
-
id: string;
|
|
1848
|
-
delta: t.MessageDelta;
|
|
1849
|
-
}> = [];
|
|
1850
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1851
|
-
async (id, delta) => {
|
|
1852
|
-
messageDeltas.push({
|
|
1853
|
-
id,
|
|
1854
|
-
delta,
|
|
1855
|
-
});
|
|
1856
|
-
}
|
|
1857
|
-
);
|
|
1858
|
-
const graph = createGraph({
|
|
1859
|
-
dispatchMessageDelta,
|
|
1860
|
-
getAgentContext: jest.fn(
|
|
1861
|
-
(): Partial<AgentContext> => ({
|
|
1862
|
-
provider: Providers.GOOGLE,
|
|
1863
|
-
reasoningKey: 'reasoning',
|
|
1864
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1865
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
1866
|
-
graphTools: [],
|
|
1867
|
-
agentId: 'agent_1',
|
|
1868
|
-
})
|
|
1869
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1870
|
-
});
|
|
1871
|
-
const handler = new ChatModelStreamHandler();
|
|
1872
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1873
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
1874
|
-
type: 'toolCall',
|
|
1875
|
-
toolCall: {
|
|
1876
|
-
id: 'server-search-1',
|
|
1877
|
-
name: 'google_search',
|
|
1878
|
-
args: {},
|
|
1879
|
-
},
|
|
1880
|
-
};
|
|
1881
|
-
|
|
1882
|
-
await handler.handle(
|
|
1883
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1884
|
-
{
|
|
1885
|
-
chunk: {
|
|
1886
|
-
content: [toolCallPart],
|
|
1887
|
-
tool_call_chunks: [
|
|
1888
|
-
{
|
|
1889
|
-
id: 'call_weather',
|
|
1890
|
-
name: 'weather',
|
|
1891
|
-
args: '{"city":"NYC"}',
|
|
1892
|
-
index: 0,
|
|
1893
|
-
},
|
|
1894
|
-
],
|
|
1895
|
-
} as unknown as t.StreamChunk,
|
|
1896
|
-
},
|
|
1897
|
-
metadata,
|
|
1898
|
-
graph
|
|
1899
|
-
);
|
|
1900
|
-
|
|
1901
|
-
expect(dispatchMessageDelta).toHaveBeenCalledTimes(1);
|
|
1902
|
-
expect(graph.getRunStep(messageDeltas[0]?.id ?? '')?.type).toBe(
|
|
1903
|
-
StepTypes.MESSAGE_CREATION
|
|
1904
|
-
);
|
|
1905
|
-
expect(messageDeltas[0]?.delta).toEqual({ content: [toolCallPart] });
|
|
1906
|
-
});
|
|
1907
|
-
|
|
1908
|
-
it('dispatches Gemini server-side tool context when reasoning is present', async () => {
|
|
1909
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1910
|
-
async () => undefined
|
|
1911
|
-
);
|
|
1912
|
-
const dispatchReasoningDelta = jest.fn<
|
|
1913
|
-
StandardGraph['dispatchReasoningDelta']
|
|
1914
|
-
>(async () => undefined);
|
|
1915
|
-
const graph = createGraph({
|
|
1916
|
-
dispatchMessageDelta,
|
|
1917
|
-
dispatchReasoningDelta,
|
|
1918
|
-
getAgentContext: jest.fn(
|
|
1919
|
-
(): Partial<AgentContext> => ({
|
|
1920
|
-
provider: Providers.GOOGLE,
|
|
1921
|
-
reasoningKey: 'reasoning',
|
|
1922
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1923
|
-
toolDefinitions: [],
|
|
1924
|
-
graphTools: [],
|
|
1925
|
-
agentId: 'agent_1',
|
|
1926
|
-
})
|
|
1927
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1928
|
-
});
|
|
1929
|
-
const handler = new ChatModelStreamHandler();
|
|
1930
|
-
const metadata = { langgraph_node: 'agent' };
|
|
1931
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
1932
|
-
type: 'toolCall',
|
|
1933
|
-
toolCall: {
|
|
1934
|
-
id: 'server-search-1',
|
|
1935
|
-
name: 'google_search',
|
|
1936
|
-
args: {},
|
|
1937
|
-
},
|
|
1938
|
-
};
|
|
1939
|
-
|
|
1940
|
-
await handler.handle(
|
|
1941
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
1942
|
-
{
|
|
1943
|
-
chunk: {
|
|
1944
|
-
content: [toolCallPart],
|
|
1945
|
-
additional_kwargs: {
|
|
1946
|
-
reasoning: 'Need to search before answering.',
|
|
1947
|
-
},
|
|
1948
|
-
} as unknown as t.StreamChunk,
|
|
1949
|
-
},
|
|
1950
|
-
metadata,
|
|
1951
|
-
graph
|
|
1952
|
-
);
|
|
1953
|
-
|
|
1954
|
-
expect(dispatchMessageDelta).toHaveBeenCalledTimes(1);
|
|
1955
|
-
expect(dispatchMessageDelta).toHaveBeenCalledWith(
|
|
1956
|
-
expect.stringMatching(/^step_/),
|
|
1957
|
-
{ content: [toolCallPart] },
|
|
1958
|
-
metadata
|
|
1959
|
-
);
|
|
1960
|
-
expect(dispatchReasoningDelta).toHaveBeenCalledWith(
|
|
1961
|
-
expect.stringMatching(/^step_/),
|
|
1962
|
-
{
|
|
1963
|
-
content: [
|
|
1964
|
-
{
|
|
1965
|
-
type: ContentTypes.THINK,
|
|
1966
|
-
think: 'Need to search before answering.',
|
|
1967
|
-
},
|
|
1968
|
-
],
|
|
1969
|
-
},
|
|
1970
|
-
metadata
|
|
1971
|
-
);
|
|
1972
|
-
});
|
|
1973
|
-
|
|
1974
|
-
it('dispatches Gemini server-side tool context before client tool calls in mixed chunks', async () => {
|
|
1975
|
-
const dispatchOrder: string[] = [];
|
|
1976
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
1977
|
-
async (_id, delta) => {
|
|
1978
|
-
if (
|
|
1979
|
-
delta.content?.some(
|
|
1980
|
-
(contentPart) => contentPart.type === 'toolCall'
|
|
1981
|
-
) === true
|
|
1982
|
-
) {
|
|
1983
|
-
dispatchOrder.push('server-context');
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
);
|
|
1987
|
-
const graph = createGraph({
|
|
1988
|
-
dispatchMessageDelta,
|
|
1989
|
-
getAgentContext: jest.fn(
|
|
1990
|
-
(): Partial<AgentContext> => ({
|
|
1991
|
-
provider: Providers.GOOGLE,
|
|
1992
|
-
reasoningKey: 'reasoning',
|
|
1993
|
-
currentTokenType: ContentTypes.TEXT,
|
|
1994
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
1995
|
-
graphTools: [],
|
|
1996
|
-
agentId: 'agent_1',
|
|
1997
|
-
})
|
|
1998
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
1999
|
-
});
|
|
2000
|
-
const dispatchRunStep = graph.dispatchRunStep.bind(graph);
|
|
2001
|
-
graph.dispatchRunStep = jest.fn<StandardGraph['dispatchRunStep']>(
|
|
2002
|
-
async (stepKey, stepDetails, metadata) => {
|
|
2003
|
-
const stepId = await dispatchRunStep(stepKey, stepDetails, metadata);
|
|
2004
|
-
if ((stepDetails as t.StepDetails).type === StepTypes.TOOL_CALLS) {
|
|
2005
|
-
dispatchOrder.push('client-tool-calls');
|
|
2006
|
-
}
|
|
2007
|
-
return stepId;
|
|
2008
|
-
}
|
|
2009
|
-
);
|
|
2010
|
-
const handler = new ChatModelStreamHandler();
|
|
2011
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2012
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
2013
|
-
type: 'toolCall',
|
|
2014
|
-
toolCall: {
|
|
2015
|
-
id: 'server-search-1',
|
|
2016
|
-
name: 'google_search',
|
|
2017
|
-
args: {},
|
|
2018
|
-
},
|
|
2019
|
-
};
|
|
2020
|
-
|
|
2021
|
-
await handler.handle(
|
|
2022
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2023
|
-
{
|
|
2024
|
-
chunk: {
|
|
2025
|
-
content: [toolCallPart],
|
|
2026
|
-
tool_calls: [
|
|
2027
|
-
{
|
|
2028
|
-
type: ToolCallTypes.TOOL_CALL,
|
|
2029
|
-
id: 'call_weather',
|
|
2030
|
-
name: 'weather',
|
|
2031
|
-
args: { city: 'NYC' },
|
|
2032
|
-
},
|
|
2033
|
-
],
|
|
2034
|
-
} as unknown as t.StreamChunk,
|
|
2035
|
-
},
|
|
2036
|
-
metadata,
|
|
2037
|
-
graph
|
|
2038
|
-
);
|
|
2039
|
-
|
|
2040
|
-
expect(dispatchOrder).toEqual(['server-context', 'client-tool-calls']);
|
|
2041
|
-
});
|
|
2042
|
-
|
|
2043
|
-
it('does not dispatch Gemini server-side tool context blocks for non-Google providers', async () => {
|
|
2044
|
-
const dispatchMessageDelta = jest.fn<StandardGraph['dispatchMessageDelta']>(
|
|
2045
|
-
async () => undefined
|
|
2046
|
-
);
|
|
2047
|
-
const graph = createGraph({
|
|
2048
|
-
dispatchMessageDelta,
|
|
2049
|
-
getAgentContext: jest.fn(
|
|
2050
|
-
(): Partial<AgentContext> => ({
|
|
2051
|
-
provider: Providers.OPENAI,
|
|
2052
|
-
reasoningKey: 'reasoning_content',
|
|
2053
|
-
currentTokenType: ContentTypes.TEXT,
|
|
2054
|
-
toolDefinitions: [],
|
|
2055
|
-
graphTools: [],
|
|
2056
|
-
agentId: 'agent_1',
|
|
2057
|
-
})
|
|
2058
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2059
|
-
});
|
|
2060
|
-
const handler = new ChatModelStreamHandler();
|
|
2061
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2062
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
2063
|
-
type: 'toolCall',
|
|
2064
|
-
toolCall: {
|
|
2065
|
-
id: 'server-search-1',
|
|
2066
|
-
name: 'google_search',
|
|
2067
|
-
args: {},
|
|
2068
|
-
},
|
|
2069
|
-
};
|
|
2070
|
-
|
|
2071
|
-
await handler.handle(
|
|
2072
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2073
|
-
{
|
|
2074
|
-
chunk: {
|
|
2075
|
-
content: [toolCallPart],
|
|
2076
|
-
} as unknown as t.StreamChunk,
|
|
2077
|
-
},
|
|
2078
|
-
metadata,
|
|
2079
|
-
graph
|
|
2080
|
-
);
|
|
2081
|
-
|
|
2082
|
-
expect(dispatchMessageDelta).not.toHaveBeenCalled();
|
|
2083
|
-
});
|
|
2084
|
-
|
|
2085
|
-
it('keeps separately streamed Gemini server-side tool context blocks on separate message steps', async () => {
|
|
2086
|
-
const graph = createGraph({
|
|
2087
|
-
getAgentContext: jest.fn(
|
|
2088
|
-
(): Partial<AgentContext> => ({
|
|
2089
|
-
provider: Providers.GOOGLE,
|
|
2090
|
-
reasoningKey: 'reasoning',
|
|
2091
|
-
currentTokenType: ContentTypes.TEXT,
|
|
2092
|
-
toolDefinitions: [],
|
|
2093
|
-
graphTools: [],
|
|
2094
|
-
agentId: 'agent_1',
|
|
2095
|
-
})
|
|
2096
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2097
|
-
});
|
|
2098
|
-
const { contentParts } = attachContentAggregator(graph);
|
|
2099
|
-
const handler = new ChatModelStreamHandler();
|
|
2100
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2101
|
-
const toolCallPart: t.MessageContentComplex = {
|
|
2102
|
-
type: 'toolCall',
|
|
2103
|
-
toolCall: {
|
|
2104
|
-
id: 'server-search-1',
|
|
2105
|
-
name: 'google_search',
|
|
2106
|
-
args: {},
|
|
2107
|
-
},
|
|
2108
|
-
};
|
|
2109
|
-
const toolResponsePart: t.MessageContentComplex = {
|
|
2110
|
-
type: 'toolResponse',
|
|
2111
|
-
toolResponse: {
|
|
2112
|
-
id: 'server-search-1',
|
|
2113
|
-
name: 'google_search',
|
|
2114
|
-
response: { results: [] },
|
|
2115
|
-
},
|
|
2116
|
-
};
|
|
2117
|
-
|
|
2118
|
-
await handler.handle(
|
|
2119
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2120
|
-
{
|
|
2121
|
-
chunk: {
|
|
2122
|
-
content: [toolCallPart],
|
|
2123
|
-
} as unknown as t.StreamChunk,
|
|
2124
|
-
},
|
|
2125
|
-
metadata,
|
|
2126
|
-
graph
|
|
2127
|
-
);
|
|
2128
|
-
await handler.handle(
|
|
2129
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2130
|
-
{
|
|
2131
|
-
chunk: {
|
|
2132
|
-
content: [toolResponsePart],
|
|
2133
|
-
} as unknown as t.StreamChunk,
|
|
2134
|
-
},
|
|
2135
|
-
metadata,
|
|
2136
|
-
graph
|
|
2137
|
-
);
|
|
2138
|
-
await handler.handle(
|
|
2139
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2140
|
-
{
|
|
2141
|
-
chunk: {
|
|
2142
|
-
content: [
|
|
2143
|
-
{
|
|
2144
|
-
type: ContentTypes.TEXT,
|
|
2145
|
-
text: 'Done',
|
|
2146
|
-
},
|
|
2147
|
-
],
|
|
2148
|
-
} as unknown as t.StreamChunk,
|
|
2149
|
-
},
|
|
2150
|
-
metadata,
|
|
2151
|
-
graph
|
|
2152
|
-
);
|
|
2153
|
-
await handler.handle(
|
|
2154
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2155
|
-
{
|
|
2156
|
-
chunk: {
|
|
2157
|
-
content: ' again.',
|
|
2158
|
-
} as unknown as t.StreamChunk,
|
|
2159
|
-
},
|
|
2160
|
-
metadata,
|
|
2161
|
-
graph
|
|
2162
|
-
);
|
|
2163
|
-
|
|
2164
|
-
expect(contentParts).toEqual([
|
|
2165
|
-
toolCallPart,
|
|
2166
|
-
toolResponsePart,
|
|
2167
|
-
{
|
|
2168
|
-
type: ContentTypes.TEXT,
|
|
2169
|
-
text: 'Done again.',
|
|
2170
|
-
},
|
|
2171
|
-
]);
|
|
2172
|
-
});
|
|
2173
|
-
|
|
2174
|
-
it('processes a reused chunk object when its streamed payload changes', async () => {
|
|
2175
|
-
const graph = createGraph();
|
|
2176
|
-
const handler = new ChatModelStreamHandler();
|
|
2177
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2178
|
-
const reusableToolChunk = {
|
|
2179
|
-
id: 'call_weather',
|
|
2180
|
-
name: 'weather',
|
|
2181
|
-
args: '{"city"',
|
|
2182
|
-
index: 0,
|
|
2183
|
-
};
|
|
2184
|
-
const reusableChunk = {
|
|
2185
|
-
content: '',
|
|
2186
|
-
tool_call_chunks: [reusableToolChunk],
|
|
2187
|
-
} as unknown as t.StreamChunk;
|
|
2188
|
-
|
|
2189
|
-
await handler.handle(
|
|
2190
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2191
|
-
{ chunk: reusableChunk },
|
|
2192
|
-
metadata,
|
|
2193
|
-
graph
|
|
2194
|
-
);
|
|
2195
|
-
|
|
2196
|
-
reusableToolChunk.args = ':"NYC"}';
|
|
2197
|
-
|
|
2198
|
-
await handler.handle(
|
|
2199
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2200
|
-
{ chunk: reusableChunk },
|
|
2201
|
-
metadata,
|
|
2202
|
-
graph
|
|
2203
|
-
);
|
|
2204
|
-
|
|
2205
|
-
expect(
|
|
2206
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 0))?.argsText
|
|
2207
|
-
).toBe('{"city":"NYC"}');
|
|
2208
|
-
});
|
|
2209
|
-
|
|
2210
|
-
it('does not share chunk object de-duplication across graphs', async () => {
|
|
2211
|
-
const graphA = createGraph();
|
|
2212
|
-
const graphB = createGraph();
|
|
2213
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
2214
|
-
jest
|
|
2215
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2216
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2217
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2218
|
-
return;
|
|
2219
|
-
}
|
|
2220
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2221
|
-
toolExecuteCalls.push(batch);
|
|
2222
|
-
batch.resolve(
|
|
2223
|
-
batch.toolCalls.map((request) => ({
|
|
2224
|
-
toolCallId: request.id,
|
|
2225
|
-
status: 'success' as const,
|
|
2226
|
-
content: `${request.id} result`,
|
|
2227
|
-
}))
|
|
2228
|
-
);
|
|
2229
|
-
});
|
|
2230
|
-
|
|
2231
|
-
const handler = new ChatModelStreamHandler();
|
|
2232
|
-
const sharedChunk = {
|
|
2233
|
-
content: '',
|
|
2234
|
-
tool_calls: [
|
|
2235
|
-
{
|
|
2236
|
-
id: 'call_weather',
|
|
2237
|
-
name: 'weather',
|
|
2238
|
-
args: { city: 'NYC' },
|
|
2239
|
-
},
|
|
2240
|
-
],
|
|
2241
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
2242
|
-
} as unknown as t.StreamChunk;
|
|
2243
|
-
|
|
2244
|
-
await handler.handle(
|
|
2245
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2246
|
-
{ chunk: sharedChunk },
|
|
2247
|
-
{ langgraph_node: 'agent' },
|
|
2248
|
-
graphA
|
|
2249
|
-
);
|
|
2250
|
-
await handler.handle(
|
|
2251
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2252
|
-
{ chunk: sharedChunk },
|
|
2253
|
-
{ langgraph_node: 'agent' },
|
|
2254
|
-
graphB
|
|
2255
|
-
);
|
|
2256
|
-
|
|
2257
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
2258
|
-
expect(graphA.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
2259
|
-
expect(graphB.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
2260
|
-
});
|
|
2261
|
-
|
|
2262
|
-
it('prestarts a completed streamed tool when a later tool call begins', async () => {
|
|
2263
|
-
const graph = createGraph({
|
|
2264
|
-
getAgentContext: jest.fn(
|
|
2265
|
-
(): Partial<AgentContext> => ({
|
|
2266
|
-
provider: Providers.ANTHROPIC,
|
|
2267
|
-
reasoningKey: 'reasoning',
|
|
2268
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2269
|
-
graphTools: [],
|
|
2270
|
-
agentId: 'agent_1',
|
|
2271
|
-
})
|
|
2272
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2273
|
-
});
|
|
2274
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
2275
|
-
jest
|
|
2276
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2277
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2278
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2279
|
-
return;
|
|
2280
|
-
}
|
|
2281
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2282
|
-
toolExecuteCalls.push(batch);
|
|
2283
|
-
batch.resolve(
|
|
2284
|
-
batch.toolCalls.map((call) => ({
|
|
2285
|
-
toolCallId: call.id,
|
|
2286
|
-
status: 'success',
|
|
2287
|
-
content: `ok ${call.name}`,
|
|
2288
|
-
}))
|
|
2289
|
-
);
|
|
2290
|
-
});
|
|
2291
|
-
|
|
2292
|
-
const handler = new ChatModelStreamHandler();
|
|
2293
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2294
|
-
|
|
2295
|
-
await handler.handle(
|
|
2296
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2297
|
-
{
|
|
2298
|
-
chunk: {
|
|
2299
|
-
content: '',
|
|
2300
|
-
tool_call_chunks: [
|
|
2301
|
-
{
|
|
2302
|
-
id: 'call_weather',
|
|
2303
|
-
name: 'weather',
|
|
2304
|
-
args: '{"city":"NYC"}',
|
|
2305
|
-
index: 0,
|
|
2306
|
-
},
|
|
2307
|
-
],
|
|
2308
|
-
} as unknown as t.StreamChunk,
|
|
2309
|
-
},
|
|
2310
|
-
metadata,
|
|
2311
|
-
graph
|
|
2312
|
-
);
|
|
2313
|
-
|
|
2314
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
2315
|
-
|
|
2316
|
-
await handler.handle(
|
|
2317
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2318
|
-
{
|
|
2319
|
-
chunk: {
|
|
2320
|
-
content: '',
|
|
2321
|
-
tool_call_chunks: [
|
|
2322
|
-
{
|
|
2323
|
-
id: 'call_stock',
|
|
2324
|
-
name: 'stock',
|
|
2325
|
-
args: '{"ticker":"C',
|
|
2326
|
-
index: 1,
|
|
2327
|
-
},
|
|
2328
|
-
],
|
|
2329
|
-
} as unknown as t.StreamChunk,
|
|
2330
|
-
},
|
|
2331
|
-
metadata,
|
|
2332
|
-
graph
|
|
2333
|
-
);
|
|
2334
|
-
|
|
2335
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
2336
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
2337
|
-
expect.objectContaining({
|
|
2338
|
-
id: 'call_weather',
|
|
2339
|
-
name: 'weather',
|
|
2340
|
-
args: { city: 'NYC' },
|
|
2341
|
-
stepId: expect.stringMatching(/^step_/),
|
|
2342
|
-
turn: 0,
|
|
2343
|
-
}),
|
|
2344
|
-
]);
|
|
2345
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
2346
|
-
expect(graph.eagerEventToolExecutions.has('call_stock')).toBe(false);
|
|
2347
|
-
expect(
|
|
2348
|
-
graph.eagerEventToolCallChunks.has(chunkStateKey('step-key', 0))
|
|
2349
|
-
).toBe(false);
|
|
2350
|
-
expect(
|
|
2351
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('step-key', 1))?.argsText
|
|
2352
|
-
).toBe('{"ticker":"C');
|
|
2353
|
-
|
|
2354
|
-
await handler.handle(
|
|
2355
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2356
|
-
{
|
|
2357
|
-
chunk: {
|
|
2358
|
-
content: '',
|
|
2359
|
-
tool_call_chunks: [
|
|
2360
|
-
{
|
|
2361
|
-
args: 'H"}',
|
|
2362
|
-
index: 1,
|
|
2363
|
-
},
|
|
2364
|
-
],
|
|
2365
|
-
} as unknown as t.StreamChunk,
|
|
2366
|
-
},
|
|
2367
|
-
metadata,
|
|
2368
|
-
graph
|
|
2369
|
-
);
|
|
2370
|
-
|
|
2371
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
2372
|
-
|
|
2373
|
-
await handler.handle(
|
|
2374
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2375
|
-
{
|
|
2376
|
-
chunk: {
|
|
2377
|
-
content: '',
|
|
2378
|
-
tool_calls: [
|
|
2379
|
-
{
|
|
2380
|
-
id: 'call_weather',
|
|
2381
|
-
name: 'weather',
|
|
2382
|
-
args: { city: 'NYC' },
|
|
2383
|
-
},
|
|
2384
|
-
{
|
|
2385
|
-
id: 'call_stock',
|
|
2386
|
-
name: 'stock',
|
|
2387
|
-
args: { ticker: 'CH' },
|
|
2388
|
-
},
|
|
2389
|
-
],
|
|
2390
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
2391
|
-
} as unknown as t.StreamChunk,
|
|
2392
|
-
},
|
|
2393
|
-
metadata,
|
|
2394
|
-
graph
|
|
2395
|
-
);
|
|
2396
|
-
|
|
2397
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
2398
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
2399
|
-
id: 'call_stock',
|
|
2400
|
-
name: 'stock',
|
|
2401
|
-
args: { ticker: 'CH' },
|
|
2402
|
-
stepId: expect.stringMatching(/^step_/),
|
|
2403
|
-
turn: 0,
|
|
2404
|
-
});
|
|
2405
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
2406
|
-
});
|
|
2407
|
-
|
|
2408
|
-
it('emits a completed event when an eager streamed tool result settles', async () => {
|
|
2409
|
-
const graph = createGraph({
|
|
2410
|
-
getAgentContext: jest.fn(
|
|
2411
|
-
(): Partial<AgentContext> => ({
|
|
2412
|
-
provider: Providers.ANTHROPIC,
|
|
2413
|
-
reasoningKey: 'reasoning',
|
|
2414
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2415
|
-
graphTools: [],
|
|
2416
|
-
agentId: 'agent_1',
|
|
2417
|
-
})
|
|
2418
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2419
|
-
});
|
|
2420
|
-
const completedEvents: Array<{ result: t.ToolEndEvent }> = [];
|
|
2421
|
-
jest
|
|
2422
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2423
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2424
|
-
if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
2425
|
-
completedEvents.push(data as { result: t.ToolEndEvent });
|
|
2426
|
-
return;
|
|
2427
|
-
}
|
|
2428
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2429
|
-
return;
|
|
2430
|
-
}
|
|
2431
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2432
|
-
batch.resolve(
|
|
2433
|
-
batch.toolCalls.map((call) => ({
|
|
2434
|
-
toolCallId: call.id,
|
|
2435
|
-
status: 'success',
|
|
2436
|
-
content: `ok ${call.name}`,
|
|
2437
|
-
}))
|
|
2438
|
-
);
|
|
2439
|
-
});
|
|
2440
|
-
|
|
2441
|
-
const handler = new ChatModelStreamHandler();
|
|
2442
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2443
|
-
|
|
2444
|
-
await handler.handle(
|
|
2445
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2446
|
-
{
|
|
2447
|
-
chunk: {
|
|
2448
|
-
content: '',
|
|
2449
|
-
tool_call_chunks: [
|
|
2450
|
-
{
|
|
2451
|
-
id: 'call_weather',
|
|
2452
|
-
name: 'weather',
|
|
2453
|
-
args: '{"city":"NYC"}',
|
|
2454
|
-
index: 0,
|
|
2455
|
-
},
|
|
2456
|
-
],
|
|
2457
|
-
} as unknown as t.StreamChunk,
|
|
2458
|
-
},
|
|
2459
|
-
metadata,
|
|
2460
|
-
graph
|
|
2461
|
-
);
|
|
2462
|
-
|
|
2463
|
-
await handler.handle(
|
|
2464
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2465
|
-
{
|
|
2466
|
-
chunk: {
|
|
2467
|
-
content: '',
|
|
2468
|
-
tool_call_chunks: [
|
|
2469
|
-
{
|
|
2470
|
-
id: 'call_stock',
|
|
2471
|
-
name: 'stock',
|
|
2472
|
-
args: '{"ticker":"C',
|
|
2473
|
-
index: 1,
|
|
2474
|
-
},
|
|
2475
|
-
],
|
|
2476
|
-
} as unknown as t.StreamChunk,
|
|
2477
|
-
},
|
|
2478
|
-
metadata,
|
|
2479
|
-
graph
|
|
2480
|
-
);
|
|
2481
|
-
|
|
2482
|
-
await graph.eagerEventToolExecutions.get('call_weather')?.promise;
|
|
2483
|
-
|
|
2484
|
-
expect(completedEvents).toHaveLength(1);
|
|
2485
|
-
expect(completedEvents[0].result).toMatchObject({
|
|
2486
|
-
id: expect.stringMatching(/^step_/),
|
|
2487
|
-
type: 'tool_call',
|
|
2488
|
-
eager: true,
|
|
2489
|
-
tool_call: {
|
|
2490
|
-
id: 'call_weather',
|
|
2491
|
-
name: 'weather',
|
|
2492
|
-
args: '{"city":"NYC"}',
|
|
2493
|
-
output: 'ok weather',
|
|
2494
|
-
progress: 1,
|
|
2495
|
-
},
|
|
2496
|
-
});
|
|
2497
|
-
expect(
|
|
2498
|
-
graph.eagerEventToolExecutions.get('call_weather')?.completionDispatched
|
|
2499
|
-
).toBe(true);
|
|
2500
|
-
});
|
|
2501
|
-
|
|
2502
|
-
it('serializes bigint output before eager completion dispatch', async () => {
|
|
2503
|
-
const graph = createGraph();
|
|
2504
|
-
const completedEvents: Array<{ result: t.ToolEndEvent }> = [];
|
|
2505
|
-
jest
|
|
2506
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2507
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2508
|
-
if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
2509
|
-
completedEvents.push(data as { result: t.ToolEndEvent });
|
|
2510
|
-
return;
|
|
2511
|
-
}
|
|
2512
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2513
|
-
return;
|
|
2514
|
-
}
|
|
2515
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2516
|
-
batch.resolve([
|
|
2517
|
-
{
|
|
2518
|
-
toolCallId: 'call_weather',
|
|
2519
|
-
status: 'success',
|
|
2520
|
-
content: [{ rowsRead: BigInt(42) }],
|
|
2521
|
-
},
|
|
2522
|
-
]);
|
|
2523
|
-
});
|
|
2524
|
-
|
|
2525
|
-
const handler = new ChatModelStreamHandler();
|
|
2526
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2527
|
-
await handler.handle(
|
|
2528
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2529
|
-
{
|
|
2530
|
-
chunk: {
|
|
2531
|
-
content: '',
|
|
2532
|
-
tool_call_chunks: [
|
|
2533
|
-
{
|
|
2534
|
-
id: 'call_weather',
|
|
2535
|
-
name: 'weather',
|
|
2536
|
-
args: '{"city":"NYC"}',
|
|
2537
|
-
index: 0,
|
|
2538
|
-
},
|
|
2539
|
-
],
|
|
2540
|
-
} as unknown as t.StreamChunk,
|
|
2541
|
-
},
|
|
2542
|
-
metadata,
|
|
2543
|
-
graph
|
|
2544
|
-
);
|
|
2545
|
-
await handler.handle(
|
|
2546
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2547
|
-
{
|
|
2548
|
-
chunk: {
|
|
2549
|
-
content: '',
|
|
2550
|
-
tool_call_chunks: [
|
|
2551
|
-
{
|
|
2552
|
-
id: 'call_stock',
|
|
2553
|
-
name: 'stock',
|
|
2554
|
-
args: '{"ticker":"C',
|
|
2555
|
-
index: 1,
|
|
2556
|
-
},
|
|
2557
|
-
],
|
|
2558
|
-
} as unknown as t.StreamChunk,
|
|
2559
|
-
},
|
|
2560
|
-
metadata,
|
|
2561
|
-
graph
|
|
2562
|
-
);
|
|
2563
|
-
|
|
2564
|
-
await graph.eagerEventToolExecutions.get('call_weather')?.promise;
|
|
2565
|
-
|
|
2566
|
-
expect(completedEvents).toHaveLength(1);
|
|
2567
|
-
expect(
|
|
2568
|
-
(
|
|
2569
|
-
completedEvents[0].result as {
|
|
2570
|
-
tool_call?: { output?: string };
|
|
2571
|
-
}
|
|
2572
|
-
).tool_call?.output
|
|
2573
|
-
).toBe('[{"rowsRead":"42"}]');
|
|
2574
|
-
});
|
|
2575
|
-
|
|
2576
|
-
it('does not emit a stale eager completion after the active execution is invalidated', async () => {
|
|
2577
|
-
const graph = createGraph({
|
|
2578
|
-
getAgentContext: jest.fn(
|
|
2579
|
-
(): Partial<AgentContext> => ({
|
|
2580
|
-
provider: Providers.ANTHROPIC,
|
|
2581
|
-
reasoningKey: 'reasoning',
|
|
2582
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2583
|
-
graphTools: [],
|
|
2584
|
-
agentId: 'agent_1',
|
|
2585
|
-
})
|
|
2586
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2587
|
-
});
|
|
2588
|
-
const completedEvents: Array<{ result: t.ToolEndEvent }> = [];
|
|
2589
|
-
let pendingBatch: t.ToolExecuteBatchRequest | undefined;
|
|
2590
|
-
jest
|
|
2591
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2592
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2593
|
-
if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
2594
|
-
completedEvents.push(data as { result: t.ToolEndEvent });
|
|
2595
|
-
return;
|
|
2596
|
-
}
|
|
2597
|
-
if (event === GraphEvents.ON_TOOL_EXECUTE) {
|
|
2598
|
-
pendingBatch = data as t.ToolExecuteBatchRequest;
|
|
2599
|
-
}
|
|
2600
|
-
});
|
|
2601
|
-
|
|
2602
|
-
const handler = new ChatModelStreamHandler();
|
|
2603
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2604
|
-
|
|
2605
|
-
await handler.handle(
|
|
2606
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2607
|
-
{
|
|
2608
|
-
chunk: {
|
|
2609
|
-
content: '',
|
|
2610
|
-
tool_call_chunks: [
|
|
2611
|
-
{
|
|
2612
|
-
id: 'call_weather',
|
|
2613
|
-
name: 'weather',
|
|
2614
|
-
args: '{"city":"NYC"}',
|
|
2615
|
-
index: 0,
|
|
2616
|
-
},
|
|
2617
|
-
],
|
|
2618
|
-
} as unknown as t.StreamChunk,
|
|
2619
|
-
},
|
|
2620
|
-
metadata,
|
|
2621
|
-
graph
|
|
2622
|
-
);
|
|
2623
|
-
await handler.handle(
|
|
2624
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2625
|
-
{
|
|
2626
|
-
chunk: {
|
|
2627
|
-
content: '',
|
|
2628
|
-
tool_call_chunks: [
|
|
2629
|
-
{
|
|
2630
|
-
id: 'call_stock',
|
|
2631
|
-
name: 'stock',
|
|
2632
|
-
args: '{"ticker":"C',
|
|
2633
|
-
index: 1,
|
|
2634
|
-
},
|
|
2635
|
-
],
|
|
2636
|
-
} as unknown as t.StreamChunk,
|
|
2637
|
-
},
|
|
2638
|
-
metadata,
|
|
2639
|
-
graph
|
|
2640
|
-
);
|
|
2641
|
-
|
|
2642
|
-
const staleRecord = graph.eagerEventToolExecutions.get('call_weather');
|
|
2643
|
-
expect(staleRecord).toBeDefined();
|
|
2644
|
-
expect(pendingBatch?.toolCalls).toHaveLength(1);
|
|
2645
|
-
|
|
2646
|
-
graph.eagerEventToolExecutions.delete('call_weather');
|
|
2647
|
-
pendingBatch?.resolve([
|
|
2648
|
-
{
|
|
2649
|
-
toolCallId: 'call_weather',
|
|
2650
|
-
status: 'success',
|
|
2651
|
-
content: 'stale weather',
|
|
2652
|
-
},
|
|
2653
|
-
]);
|
|
2654
|
-
await staleRecord?.promise;
|
|
2655
|
-
|
|
2656
|
-
expect(completedEvents).toHaveLength(0);
|
|
2657
|
-
expect(staleRecord?.completionDispatched).toBeUndefined();
|
|
2658
|
-
});
|
|
2659
|
-
|
|
2660
|
-
it('does not mark eager completion dispatched when event delivery is swallowed', async () => {
|
|
2661
|
-
const graph = createGraph({
|
|
2662
|
-
getAgentContext: jest.fn(
|
|
2663
|
-
(): Partial<AgentContext> => ({
|
|
2664
|
-
provider: Providers.ANTHROPIC,
|
|
2665
|
-
reasoningKey: 'reasoning',
|
|
2666
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2667
|
-
graphTools: [],
|
|
2668
|
-
agentId: 'agent_1',
|
|
2669
|
-
})
|
|
2670
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2671
|
-
});
|
|
2672
|
-
jest
|
|
2673
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2674
|
-
.mockImplementation(async (event, data): Promise<boolean | void> => {
|
|
2675
|
-
if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
2676
|
-
return false;
|
|
2677
|
-
}
|
|
2678
|
-
if (event === GraphEvents.ON_TOOL_EXECUTE) {
|
|
2679
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2680
|
-
batch.resolve([
|
|
2681
|
-
{
|
|
2682
|
-
toolCallId: 'call_weather',
|
|
2683
|
-
status: 'success',
|
|
2684
|
-
content: 'weather result',
|
|
2685
|
-
},
|
|
2686
|
-
]);
|
|
2687
|
-
}
|
|
2688
|
-
});
|
|
2689
|
-
|
|
2690
|
-
const handler = new ChatModelStreamHandler();
|
|
2691
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2692
|
-
|
|
2693
|
-
await handler.handle(
|
|
2694
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2695
|
-
{
|
|
2696
|
-
chunk: {
|
|
2697
|
-
content: '',
|
|
2698
|
-
tool_call_chunks: [
|
|
2699
|
-
{
|
|
2700
|
-
id: 'call_weather',
|
|
2701
|
-
name: 'weather',
|
|
2702
|
-
args: '{"city":"NYC"}',
|
|
2703
|
-
index: 0,
|
|
2704
|
-
},
|
|
2705
|
-
],
|
|
2706
|
-
} as unknown as t.StreamChunk,
|
|
2707
|
-
},
|
|
2708
|
-
metadata,
|
|
2709
|
-
graph
|
|
2710
|
-
);
|
|
2711
|
-
await handler.handle(
|
|
2712
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2713
|
-
{
|
|
2714
|
-
chunk: {
|
|
2715
|
-
content: '',
|
|
2716
|
-
tool_call_chunks: [
|
|
2717
|
-
{
|
|
2718
|
-
id: 'call_stock',
|
|
2719
|
-
name: 'stock',
|
|
2720
|
-
args: '{"ticker":"C',
|
|
2721
|
-
index: 1,
|
|
2722
|
-
},
|
|
2723
|
-
],
|
|
2724
|
-
} as unknown as t.StreamChunk,
|
|
2725
|
-
},
|
|
2726
|
-
metadata,
|
|
2727
|
-
graph
|
|
2728
|
-
);
|
|
2729
|
-
|
|
2730
|
-
const record = graph.eagerEventToolExecutions.get('call_weather');
|
|
2731
|
-
await record?.promise;
|
|
2732
|
-
|
|
2733
|
-
expect(record?.completionDispatched).toBeUndefined();
|
|
2734
|
-
});
|
|
2735
|
-
|
|
2736
|
-
it('does not overwrite a completed tool output with later streamed deltas', () => {
|
|
2737
|
-
const { contentParts, aggregateContent } = createContentAggregator();
|
|
2738
|
-
|
|
2739
|
-
aggregateContent({
|
|
2740
|
-
event: GraphEvents.ON_RUN_STEP,
|
|
2741
|
-
data: {
|
|
2742
|
-
id: 'step_weather',
|
|
2743
|
-
type: StepTypes.TOOL_CALLS,
|
|
2744
|
-
index: 0,
|
|
2745
|
-
stepDetails: {
|
|
2746
|
-
type: StepTypes.TOOL_CALLS,
|
|
2747
|
-
tool_calls: [
|
|
2748
|
-
{
|
|
2749
|
-
id: 'call_weather',
|
|
2750
|
-
name: 'weather',
|
|
2751
|
-
args: {},
|
|
2752
|
-
},
|
|
2753
|
-
],
|
|
2754
|
-
},
|
|
2755
|
-
usage: null,
|
|
2756
|
-
} as t.RunStep,
|
|
2757
|
-
});
|
|
2758
|
-
aggregateContent({
|
|
2759
|
-
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
2760
|
-
data: {
|
|
2761
|
-
result: {
|
|
2762
|
-
id: 'step_weather',
|
|
2763
|
-
index: 0,
|
|
2764
|
-
type: 'tool_call',
|
|
2765
|
-
tool_call: {
|
|
2766
|
-
id: 'call_weather',
|
|
2767
|
-
name: 'weather',
|
|
2768
|
-
args: '{"city":"NYC"}',
|
|
2769
|
-
output: 'sunny',
|
|
2770
|
-
progress: 1,
|
|
2771
|
-
} as t.ProcessedToolCall,
|
|
2772
|
-
},
|
|
2773
|
-
} as { result: t.ToolEndEvent },
|
|
2774
|
-
});
|
|
2775
|
-
aggregateContent({
|
|
2776
|
-
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
2777
|
-
data: {
|
|
2778
|
-
id: 'step_weather',
|
|
2779
|
-
delta: {
|
|
2780
|
-
type: StepTypes.TOOL_CALLS,
|
|
2781
|
-
tool_calls: [
|
|
2782
|
-
{
|
|
2783
|
-
id: 'call_weather',
|
|
2784
|
-
name: 'weather',
|
|
2785
|
-
args: '{"city":"NYC revised"}',
|
|
2786
|
-
},
|
|
2787
|
-
],
|
|
2788
|
-
},
|
|
2789
|
-
} as t.RunStepDeltaEvent,
|
|
2790
|
-
});
|
|
2791
|
-
|
|
2792
|
-
expect(contentParts[0]).toMatchObject({
|
|
2793
|
-
type: ContentTypes.TOOL_CALL,
|
|
2794
|
-
tool_call: {
|
|
2795
|
-
id: 'call_weather',
|
|
2796
|
-
name: 'weather',
|
|
2797
|
-
args: '{"city":"NYC"}',
|
|
2798
|
-
output: 'sunny',
|
|
2799
|
-
progress: 1,
|
|
2800
|
-
},
|
|
2801
|
-
});
|
|
2802
|
-
});
|
|
2803
|
-
|
|
2804
|
-
it('does not use next-index sealing for Moonshot streamed tool chunks', async () => {
|
|
2805
|
-
const graph = createGraph({
|
|
2806
|
-
getAgentContext: jest.fn(
|
|
2807
|
-
(): Partial<AgentContext> => ({
|
|
2808
|
-
provider: Providers.MOONSHOT,
|
|
2809
|
-
reasoningKey: 'reasoning',
|
|
2810
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2811
|
-
graphTools: [],
|
|
2812
|
-
agentId: 'agent_1',
|
|
2813
|
-
})
|
|
2814
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2815
|
-
});
|
|
2816
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
2817
|
-
jest
|
|
2818
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2819
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2820
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2821
|
-
return;
|
|
2822
|
-
}
|
|
2823
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2824
|
-
toolExecuteCalls.push(batch);
|
|
2825
|
-
batch.resolve(
|
|
2826
|
-
batch.toolCalls.map((call) => ({
|
|
2827
|
-
toolCallId: call.id,
|
|
2828
|
-
status: 'success',
|
|
2829
|
-
content: `ok ${call.name}`,
|
|
2830
|
-
}))
|
|
2831
|
-
);
|
|
2832
|
-
});
|
|
2833
|
-
|
|
2834
|
-
const handler = new ChatModelStreamHandler();
|
|
2835
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2836
|
-
|
|
2837
|
-
await handler.handle(
|
|
2838
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2839
|
-
{
|
|
2840
|
-
chunk: {
|
|
2841
|
-
content: '',
|
|
2842
|
-
tool_call_chunks: [
|
|
2843
|
-
{
|
|
2844
|
-
id: 'call_weather',
|
|
2845
|
-
name: 'weather',
|
|
2846
|
-
args: '{"city":"NYC"}',
|
|
2847
|
-
index: 0,
|
|
2848
|
-
},
|
|
2849
|
-
],
|
|
2850
|
-
} as unknown as t.StreamChunk,
|
|
2851
|
-
},
|
|
2852
|
-
metadata,
|
|
2853
|
-
graph
|
|
2854
|
-
);
|
|
2855
|
-
await handler.handle(
|
|
2856
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2857
|
-
{
|
|
2858
|
-
chunk: {
|
|
2859
|
-
content: '',
|
|
2860
|
-
tool_call_chunks: [
|
|
2861
|
-
{
|
|
2862
|
-
id: 'call_stock',
|
|
2863
|
-
name: 'stock',
|
|
2864
|
-
args: '{"ticker":"C',
|
|
2865
|
-
index: 1,
|
|
2866
|
-
},
|
|
2867
|
-
],
|
|
2868
|
-
} as unknown as t.StreamChunk,
|
|
2869
|
-
},
|
|
2870
|
-
metadata,
|
|
2871
|
-
graph
|
|
2872
|
-
);
|
|
2873
|
-
|
|
2874
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
2875
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
2876
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
2877
|
-
|
|
2878
|
-
await handler.handle(
|
|
2879
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2880
|
-
{
|
|
2881
|
-
chunk: {
|
|
2882
|
-
content: '',
|
|
2883
|
-
tool_calls: [
|
|
2884
|
-
{
|
|
2885
|
-
id: 'call_weather',
|
|
2886
|
-
name: 'weather',
|
|
2887
|
-
args: { city: 'NYC revised' },
|
|
2888
|
-
},
|
|
2889
|
-
{
|
|
2890
|
-
id: 'call_stock',
|
|
2891
|
-
name: 'stock',
|
|
2892
|
-
args: { ticker: 'CH' },
|
|
2893
|
-
},
|
|
2894
|
-
],
|
|
2895
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
2896
|
-
} as unknown as t.StreamChunk,
|
|
2897
|
-
},
|
|
2898
|
-
metadata,
|
|
2899
|
-
graph
|
|
2900
|
-
);
|
|
2901
|
-
|
|
2902
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
2903
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
2904
|
-
expect.objectContaining({
|
|
2905
|
-
id: 'call_weather',
|
|
2906
|
-
name: 'weather',
|
|
2907
|
-
args: { city: 'NYC revised' },
|
|
2908
|
-
}),
|
|
2909
|
-
expect.objectContaining({
|
|
2910
|
-
id: 'call_stock',
|
|
2911
|
-
name: 'stock',
|
|
2912
|
-
args: { ticker: 'CH' },
|
|
2913
|
-
}),
|
|
2914
|
-
]);
|
|
2915
|
-
});
|
|
2916
|
-
|
|
2917
|
-
it('does not seal a streamed tool when the same chunk also carries its own index', async () => {
|
|
2918
|
-
const graph = createGraph({
|
|
2919
|
-
getAgentContext: jest.fn(
|
|
2920
|
-
(): Partial<AgentContext> => ({
|
|
2921
|
-
provider: Providers.ANTHROPIC,
|
|
2922
|
-
reasoningKey: 'reasoning',
|
|
2923
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
2924
|
-
graphTools: [],
|
|
2925
|
-
agentId: 'agent_1',
|
|
2926
|
-
})
|
|
2927
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
2928
|
-
});
|
|
2929
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
2930
|
-
jest
|
|
2931
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
2932
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
2933
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
2934
|
-
return;
|
|
2935
|
-
}
|
|
2936
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
2937
|
-
toolExecuteCalls.push(batch);
|
|
2938
|
-
batch.resolve(
|
|
2939
|
-
batch.toolCalls.map((call) => ({
|
|
2940
|
-
toolCallId: call.id,
|
|
2941
|
-
status: 'success',
|
|
2942
|
-
content: `ok ${call.name}`,
|
|
2943
|
-
}))
|
|
2944
|
-
);
|
|
2945
|
-
});
|
|
2946
|
-
|
|
2947
|
-
const handler = new ChatModelStreamHandler();
|
|
2948
|
-
const metadata = { langgraph_node: 'agent' };
|
|
2949
|
-
|
|
2950
|
-
await handler.handle(
|
|
2951
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2952
|
-
{
|
|
2953
|
-
chunk: {
|
|
2954
|
-
content: '',
|
|
2955
|
-
tool_call_chunks: [
|
|
2956
|
-
{
|
|
2957
|
-
id: 'call_weather',
|
|
2958
|
-
name: 'weather',
|
|
2959
|
-
args: '{"city":"NYC"}',
|
|
2960
|
-
index: 0,
|
|
2961
|
-
},
|
|
2962
|
-
],
|
|
2963
|
-
} as unknown as t.StreamChunk,
|
|
2964
|
-
},
|
|
2965
|
-
metadata,
|
|
2966
|
-
graph
|
|
2967
|
-
);
|
|
2968
|
-
|
|
2969
|
-
await handler.handle(
|
|
2970
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2971
|
-
{
|
|
2972
|
-
chunk: {
|
|
2973
|
-
content: '',
|
|
2974
|
-
tool_call_chunks: [
|
|
2975
|
-
{
|
|
2976
|
-
args: '',
|
|
2977
|
-
index: 0,
|
|
2978
|
-
},
|
|
2979
|
-
{
|
|
2980
|
-
id: 'call_stock',
|
|
2981
|
-
name: 'stock',
|
|
2982
|
-
args: '{"ticker":"C',
|
|
2983
|
-
index: 1,
|
|
2984
|
-
},
|
|
2985
|
-
],
|
|
2986
|
-
} as unknown as t.StreamChunk,
|
|
2987
|
-
},
|
|
2988
|
-
metadata,
|
|
2989
|
-
graph
|
|
2990
|
-
);
|
|
2991
|
-
|
|
2992
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
2993
|
-
|
|
2994
|
-
await handler.handle(
|
|
2995
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
2996
|
-
{
|
|
2997
|
-
chunk: {
|
|
2998
|
-
content: '',
|
|
2999
|
-
tool_call_chunks: [
|
|
3000
|
-
{
|
|
3001
|
-
args: 'H"}',
|
|
3002
|
-
index: 1,
|
|
3003
|
-
},
|
|
3004
|
-
],
|
|
3005
|
-
} as unknown as t.StreamChunk,
|
|
3006
|
-
},
|
|
3007
|
-
metadata,
|
|
3008
|
-
graph
|
|
3009
|
-
);
|
|
3010
|
-
|
|
3011
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
3012
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
3013
|
-
id: 'call_weather',
|
|
3014
|
-
name: 'weather',
|
|
3015
|
-
args: { city: 'NYC' },
|
|
3016
|
-
});
|
|
3017
|
-
});
|
|
3018
|
-
|
|
3019
|
-
it('preserves same-tool turns across per-call streamed eager starts', async () => {
|
|
3020
|
-
const graph = createGraph();
|
|
3021
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3022
|
-
jest
|
|
3023
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3024
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3025
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3026
|
-
return;
|
|
3027
|
-
}
|
|
3028
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3029
|
-
toolExecuteCalls.push(batch);
|
|
3030
|
-
batch.resolve(
|
|
3031
|
-
batch.toolCalls.map((call) => ({
|
|
3032
|
-
toolCallId: call.id,
|
|
3033
|
-
status: 'success',
|
|
3034
|
-
content: `ok ${call.args.city}`,
|
|
3035
|
-
}))
|
|
3036
|
-
);
|
|
3037
|
-
});
|
|
3038
|
-
|
|
3039
|
-
const handler = new ChatModelStreamHandler();
|
|
3040
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3041
|
-
|
|
3042
|
-
await handler.handle(
|
|
3043
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3044
|
-
{
|
|
3045
|
-
chunk: {
|
|
3046
|
-
content: '',
|
|
3047
|
-
tool_call_chunks: [
|
|
3048
|
-
{
|
|
3049
|
-
id: 'call_weather_1',
|
|
3050
|
-
name: 'weather',
|
|
3051
|
-
args: '{"city":"NYC"}',
|
|
3052
|
-
index: 0,
|
|
3053
|
-
},
|
|
3054
|
-
],
|
|
3055
|
-
} as unknown as t.StreamChunk,
|
|
3056
|
-
},
|
|
3057
|
-
metadata,
|
|
3058
|
-
graph
|
|
3059
|
-
);
|
|
3060
|
-
|
|
3061
|
-
await handler.handle(
|
|
3062
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3063
|
-
{
|
|
3064
|
-
chunk: {
|
|
3065
|
-
content: '',
|
|
3066
|
-
tool_call_chunks: [
|
|
3067
|
-
{
|
|
3068
|
-
id: 'call_weather_2',
|
|
3069
|
-
name: 'weather',
|
|
3070
|
-
args: '{"city":"B',
|
|
3071
|
-
index: 1,
|
|
3072
|
-
},
|
|
3073
|
-
],
|
|
3074
|
-
} as unknown as t.StreamChunk,
|
|
3075
|
-
},
|
|
3076
|
-
metadata,
|
|
3077
|
-
graph
|
|
3078
|
-
);
|
|
3079
|
-
|
|
3080
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
3081
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
3082
|
-
id: 'call_weather_1',
|
|
3083
|
-
name: 'weather',
|
|
3084
|
-
args: { city: 'NYC' },
|
|
3085
|
-
turn: 0,
|
|
3086
|
-
});
|
|
3087
|
-
|
|
3088
|
-
await handler.handle(
|
|
3089
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3090
|
-
{
|
|
3091
|
-
chunk: {
|
|
3092
|
-
content: '',
|
|
3093
|
-
tool_call_chunks: [
|
|
3094
|
-
{
|
|
3095
|
-
args: 'oston"}',
|
|
3096
|
-
index: 1,
|
|
3097
|
-
},
|
|
3098
|
-
],
|
|
3099
|
-
} as unknown as t.StreamChunk,
|
|
3100
|
-
},
|
|
3101
|
-
metadata,
|
|
3102
|
-
graph
|
|
3103
|
-
);
|
|
3104
|
-
|
|
3105
|
-
await handler.handle(
|
|
3106
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3107
|
-
{
|
|
3108
|
-
chunk: {
|
|
3109
|
-
content: '',
|
|
3110
|
-
tool_calls: [
|
|
3111
|
-
{
|
|
3112
|
-
id: 'call_weather_1',
|
|
3113
|
-
name: 'weather',
|
|
3114
|
-
args: { city: 'NYC' },
|
|
3115
|
-
},
|
|
3116
|
-
{
|
|
3117
|
-
id: 'call_weather_2',
|
|
3118
|
-
name: 'weather',
|
|
3119
|
-
args: { city: 'Boston' },
|
|
3120
|
-
},
|
|
3121
|
-
],
|
|
3122
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
3123
|
-
} as unknown as t.StreamChunk,
|
|
3124
|
-
},
|
|
3125
|
-
metadata,
|
|
3126
|
-
graph
|
|
3127
|
-
);
|
|
3128
|
-
|
|
3129
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
3130
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
3131
|
-
id: 'call_weather_2',
|
|
3132
|
-
name: 'weather',
|
|
3133
|
-
args: { city: 'Boston' },
|
|
3134
|
-
turn: 1,
|
|
3135
|
-
});
|
|
3136
|
-
expect(graph.eagerEventToolUsageCount.get('weather')).toBe(2);
|
|
3137
|
-
});
|
|
3138
|
-
|
|
3139
|
-
it('scopes streamed chunk accumulation by step key', async () => {
|
|
3140
|
-
const graph = createGraph({
|
|
3141
|
-
getStepKey: jest.fn((metadata?: Record<string, unknown>) =>
|
|
3142
|
-
String(metadata?.langgraph_node ?? 'step-key')
|
|
3143
|
-
),
|
|
3144
|
-
});
|
|
3145
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3146
|
-
jest
|
|
3147
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3148
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3149
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3150
|
-
return;
|
|
3151
|
-
}
|
|
3152
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3153
|
-
toolExecuteCalls.push(batch);
|
|
3154
|
-
batch.resolve(
|
|
3155
|
-
batch.toolCalls.map((call) => ({
|
|
3156
|
-
toolCallId: call.id,
|
|
3157
|
-
status: 'success',
|
|
3158
|
-
content: `ok ${call.name}`,
|
|
3159
|
-
}))
|
|
3160
|
-
);
|
|
3161
|
-
});
|
|
3162
|
-
|
|
3163
|
-
const handler = new ChatModelStreamHandler();
|
|
3164
|
-
|
|
3165
|
-
await handler.handle(
|
|
3166
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3167
|
-
{
|
|
3168
|
-
chunk: {
|
|
3169
|
-
content: '',
|
|
3170
|
-
tool_call_chunks: [
|
|
3171
|
-
{
|
|
3172
|
-
id: 'call_agent_a',
|
|
3173
|
-
name: 'weather',
|
|
3174
|
-
args: '{"city":"N',
|
|
3175
|
-
index: 0,
|
|
3176
|
-
},
|
|
3177
|
-
],
|
|
3178
|
-
} as unknown as t.StreamChunk,
|
|
3179
|
-
},
|
|
3180
|
-
{ langgraph_node: 'agent_a' },
|
|
3181
|
-
graph
|
|
3182
|
-
);
|
|
3183
|
-
|
|
3184
|
-
await handler.handle(
|
|
3185
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3186
|
-
{
|
|
3187
|
-
chunk: {
|
|
3188
|
-
content: '',
|
|
3189
|
-
tool_call_chunks: [
|
|
3190
|
-
{
|
|
3191
|
-
id: 'call_agent_b',
|
|
3192
|
-
name: 'weather',
|
|
3193
|
-
args: '{"city":"S',
|
|
3194
|
-
index: 0,
|
|
3195
|
-
},
|
|
3196
|
-
],
|
|
3197
|
-
} as unknown as t.StreamChunk,
|
|
3198
|
-
},
|
|
3199
|
-
{ langgraph_node: 'agent_b' },
|
|
3200
|
-
graph
|
|
3201
|
-
);
|
|
3202
|
-
|
|
3203
|
-
await handler.handle(
|
|
3204
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3205
|
-
{
|
|
3206
|
-
chunk: {
|
|
3207
|
-
content: '',
|
|
3208
|
-
tool_call_chunks: [
|
|
3209
|
-
{
|
|
3210
|
-
args: 'F"}',
|
|
3211
|
-
index: 0,
|
|
3212
|
-
},
|
|
3213
|
-
],
|
|
3214
|
-
} as unknown as t.StreamChunk,
|
|
3215
|
-
},
|
|
3216
|
-
{ langgraph_node: 'agent_b' },
|
|
3217
|
-
graph
|
|
3218
|
-
);
|
|
3219
|
-
|
|
3220
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
3221
|
-
|
|
3222
|
-
await handler.handle(
|
|
3223
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3224
|
-
{
|
|
3225
|
-
chunk: {
|
|
3226
|
-
content: '',
|
|
3227
|
-
tool_call_chunks: [
|
|
3228
|
-
{
|
|
3229
|
-
args: 'YC"}',
|
|
3230
|
-
index: 0,
|
|
3231
|
-
},
|
|
3232
|
-
],
|
|
3233
|
-
} as unknown as t.StreamChunk,
|
|
3234
|
-
},
|
|
3235
|
-
{ langgraph_node: 'agent_a' },
|
|
3236
|
-
graph
|
|
3237
|
-
);
|
|
3238
|
-
|
|
3239
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
3240
|
-
expect(
|
|
3241
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('agent_a', 0))?.argsText
|
|
3242
|
-
).toBe('{"city":"NYC"}');
|
|
3243
|
-
expect(
|
|
3244
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('agent_b', 0))?.argsText
|
|
3245
|
-
).toBe('{"city":"SF"}');
|
|
3246
|
-
|
|
3247
|
-
await handler.handle(
|
|
3248
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3249
|
-
{
|
|
3250
|
-
chunk: {
|
|
3251
|
-
content: '',
|
|
3252
|
-
tool_calls: [
|
|
3253
|
-
{
|
|
3254
|
-
id: 'call_agent_b',
|
|
3255
|
-
name: 'weather',
|
|
3256
|
-
args: { city: 'SF' },
|
|
3257
|
-
},
|
|
3258
|
-
],
|
|
3259
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
3260
|
-
} as unknown as t.StreamChunk,
|
|
3261
|
-
},
|
|
3262
|
-
{ langgraph_node: 'agent_b' },
|
|
3263
|
-
graph
|
|
3264
|
-
);
|
|
3265
|
-
|
|
3266
|
-
expect(
|
|
3267
|
-
graph.eagerEventToolCallChunks.has(chunkStateKey('agent_b', 0))
|
|
3268
|
-
).toBe(false);
|
|
3269
|
-
expect(
|
|
3270
|
-
graph.eagerEventToolCallChunks.get(chunkStateKey('agent_a', 0))?.argsText
|
|
3271
|
-
).toBe('{"city":"NYC"}');
|
|
3272
|
-
|
|
3273
|
-
await handler.handle(
|
|
3274
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3275
|
-
{
|
|
3276
|
-
chunk: {
|
|
3277
|
-
content: '',
|
|
3278
|
-
tool_calls: [
|
|
3279
|
-
{
|
|
3280
|
-
id: 'call_agent_a',
|
|
3281
|
-
name: 'weather',
|
|
3282
|
-
args: { city: 'NYC' },
|
|
3283
|
-
},
|
|
3284
|
-
],
|
|
3285
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
3286
|
-
} as unknown as t.StreamChunk,
|
|
3287
|
-
},
|
|
3288
|
-
{ langgraph_node: 'agent_a' },
|
|
3289
|
-
graph
|
|
3290
|
-
);
|
|
3291
|
-
|
|
3292
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
3293
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
3294
|
-
id: 'call_agent_b',
|
|
3295
|
-
name: 'weather',
|
|
3296
|
-
args: { city: 'SF' },
|
|
3297
|
-
});
|
|
3298
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
3299
|
-
id: 'call_agent_a',
|
|
3300
|
-
name: 'weather',
|
|
3301
|
-
args: { city: 'NYC' },
|
|
3302
|
-
});
|
|
3303
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3304
|
-
});
|
|
3305
|
-
|
|
3306
|
-
it('does not prestart when parent-run session hooks alter results', async () => {
|
|
3307
|
-
// A subagent child graph has its own runId, but ToolNode executes hooks
|
|
3308
|
-
// under the PARENT run id from `configurable.run_id` ONLY — the prestart
|
|
3309
|
-
// gate must read the same source (a differing metadata run id here is a
|
|
3310
|
-
// deliberate distractor pinning the precedence).
|
|
3311
|
-
const sessionRegistry = new HookRegistry();
|
|
3312
|
-
sessionRegistry.registerSession('parent-run', 'PreToolUse', {
|
|
3313
|
-
hooks: [async () => ({ decision: 'allow' as const })],
|
|
3314
|
-
});
|
|
3315
|
-
const graph = createGraph({
|
|
3316
|
-
runId: 'child-run',
|
|
3317
|
-
hookRegistry: sessionRegistry,
|
|
3318
|
-
config: {
|
|
3319
|
-
configurable: { user_id: 'user_1', run_id: 'parent-run' },
|
|
3320
|
-
metadata: { run_id: 'run_1' },
|
|
3321
|
-
},
|
|
3322
|
-
} as Partial<StandardGraph>);
|
|
3323
|
-
const sessionDispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3324
|
-
|
|
3325
|
-
await new ChatModelStreamHandler().handle(
|
|
3326
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3327
|
-
{
|
|
3328
|
-
chunk: {
|
|
3329
|
-
content: '',
|
|
3330
|
-
tool_calls: [
|
|
3331
|
-
{ id: 'call_weather', name: 'weather', args: { city: 'NYC' } },
|
|
3332
|
-
],
|
|
3333
|
-
} as unknown as t.StreamChunk,
|
|
3334
|
-
},
|
|
3335
|
-
{ langgraph_node: 'agent', run_id: 'some-other-run' },
|
|
3336
|
-
graph
|
|
3337
|
-
);
|
|
3338
|
-
|
|
3339
|
-
expect(sessionDispatchSpy).not.toHaveBeenCalledWith(
|
|
3340
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3341
|
-
expect.anything(),
|
|
3342
|
-
expect.anything()
|
|
3343
|
-
);
|
|
3344
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3345
|
-
});
|
|
3346
|
-
|
|
3347
|
-
it('does not prestart when batch-sensitive hooks are configured', async () => {
|
|
3348
|
-
const graph = createGraph({
|
|
3349
|
-
hookRegistry: createResultAlteringRegistry(),
|
|
3350
|
-
});
|
|
3351
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3352
|
-
|
|
3353
|
-
await new ChatModelStreamHandler().handle(
|
|
3354
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3355
|
-
{
|
|
3356
|
-
chunk: {
|
|
3357
|
-
content: '',
|
|
3358
|
-
tool_calls: [
|
|
3359
|
-
{
|
|
3360
|
-
id: 'call_weather',
|
|
3361
|
-
name: 'weather',
|
|
3362
|
-
args: { city: 'NYC' },
|
|
3363
|
-
},
|
|
3364
|
-
],
|
|
3365
|
-
} as unknown as t.StreamChunk,
|
|
3366
|
-
},
|
|
3367
|
-
{ langgraph_node: 'agent' },
|
|
3368
|
-
graph
|
|
3369
|
-
);
|
|
3370
|
-
|
|
3371
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3372
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3373
|
-
expect.anything(),
|
|
3374
|
-
expect.anything()
|
|
3375
|
-
);
|
|
3376
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3377
|
-
});
|
|
3378
|
-
|
|
3379
|
-
it('prestarts when the registry only has observation hooks (PostToolBatch)', async () => {
|
|
3380
|
-
const observationRegistry = new HookRegistry();
|
|
3381
|
-
observationRegistry.register('PostToolBatch', {
|
|
3382
|
-
hooks: [async () => ({})],
|
|
3383
|
-
});
|
|
3384
|
-
const graph = createGraph({ hookRegistry: observationRegistry });
|
|
3385
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3386
|
-
jest
|
|
3387
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3388
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3389
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3390
|
-
return;
|
|
3391
|
-
}
|
|
3392
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3393
|
-
toolExecuteCalls.push(batch);
|
|
3394
|
-
batch.resolve([
|
|
3395
|
-
{
|
|
3396
|
-
toolCallId: 'call_weather',
|
|
3397
|
-
status: 'success',
|
|
3398
|
-
content: 'sunny',
|
|
3399
|
-
},
|
|
3400
|
-
]);
|
|
3401
|
-
});
|
|
3402
|
-
|
|
3403
|
-
await new ChatModelStreamHandler().handle(
|
|
3404
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3405
|
-
{
|
|
3406
|
-
chunk: {
|
|
3407
|
-
content: '',
|
|
3408
|
-
tool_calls: [
|
|
3409
|
-
{
|
|
3410
|
-
id: 'call_weather',
|
|
3411
|
-
name: 'weather',
|
|
3412
|
-
args: { city: 'NYC' },
|
|
3413
|
-
},
|
|
3414
|
-
],
|
|
3415
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
3416
|
-
} as unknown as t.StreamChunk,
|
|
3417
|
-
},
|
|
3418
|
-
{ langgraph_node: 'agent' },
|
|
3419
|
-
graph
|
|
3420
|
-
);
|
|
3421
|
-
|
|
3422
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
3423
|
-
expect(graph.eagerEventToolExecutions.get('call_weather')).toMatchObject({
|
|
3424
|
-
toolCallId: 'call_weather',
|
|
3425
|
-
toolName: 'weather',
|
|
3426
|
-
});
|
|
3427
|
-
});
|
|
3428
|
-
|
|
3429
|
-
it('does not buffer streamed chunks when eager execution is disabled', async () => {
|
|
3430
|
-
const graph = createGraph({
|
|
3431
|
-
eagerEventToolExecution: { enabled: false },
|
|
3432
|
-
} as Partial<StandardGraph>);
|
|
3433
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3434
|
-
|
|
3435
|
-
await new ChatModelStreamHandler().handle(
|
|
3436
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3437
|
-
{
|
|
3438
|
-
chunk: {
|
|
3439
|
-
content: '',
|
|
3440
|
-
tool_call_chunks: [
|
|
3441
|
-
{
|
|
3442
|
-
id: 'call_weather',
|
|
3443
|
-
name: 'weather',
|
|
3444
|
-
args: '{"city":"NYC"}',
|
|
3445
|
-
index: 0,
|
|
3446
|
-
},
|
|
3447
|
-
],
|
|
3448
|
-
} as unknown as t.StreamChunk,
|
|
3449
|
-
},
|
|
3450
|
-
{ langgraph_node: 'agent' },
|
|
3451
|
-
graph
|
|
3452
|
-
);
|
|
3453
|
-
|
|
3454
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3455
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3456
|
-
expect.anything(),
|
|
3457
|
-
expect.anything()
|
|
3458
|
-
);
|
|
3459
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3460
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3461
|
-
});
|
|
3462
|
-
|
|
3463
|
-
it('does not prestart local-engine direct coding tools', async () => {
|
|
3464
|
-
const graph = createGraph({
|
|
3465
|
-
toolExecution: {
|
|
3466
|
-
engine: 'local',
|
|
3467
|
-
} as StandardGraph['toolExecution'],
|
|
3468
|
-
getAgentContext: jest.fn(
|
|
3469
|
-
(): Partial<AgentContext> => ({
|
|
3470
|
-
provider: Providers.OPENAI,
|
|
3471
|
-
reasoningKey: 'reasoning_content',
|
|
3472
|
-
toolDefinitions: [{ name: Constants.EXECUTE_CODE }],
|
|
3473
|
-
graphTools: [],
|
|
3474
|
-
agentId: 'agent_1',
|
|
3475
|
-
})
|
|
3476
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3477
|
-
});
|
|
3478
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3479
|
-
|
|
3480
|
-
await new ChatModelStreamHandler().handle(
|
|
3481
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3482
|
-
{
|
|
3483
|
-
chunk: {
|
|
3484
|
-
content: '',
|
|
3485
|
-
tool_calls: [
|
|
3486
|
-
{
|
|
3487
|
-
id: 'call_code',
|
|
3488
|
-
name: Constants.EXECUTE_CODE,
|
|
3489
|
-
args: { code: 'print(1)' },
|
|
3490
|
-
},
|
|
3491
|
-
],
|
|
3492
|
-
} as unknown as t.StreamChunk,
|
|
3493
|
-
},
|
|
3494
|
-
{ langgraph_node: 'agent' },
|
|
3495
|
-
graph
|
|
3496
|
-
);
|
|
3497
|
-
|
|
3498
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3499
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3500
|
-
expect.anything(),
|
|
3501
|
-
expect.anything()
|
|
3502
|
-
);
|
|
3503
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3504
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3505
|
-
});
|
|
3506
|
-
|
|
3507
|
-
it('does not prestart streamed local-engine direct coding tools', async () => {
|
|
3508
|
-
const graph = createGraph({
|
|
3509
|
-
toolExecution: {
|
|
3510
|
-
engine: 'local',
|
|
3511
|
-
} as StandardGraph['toolExecution'],
|
|
3512
|
-
getAgentContext: jest.fn(
|
|
3513
|
-
(): Partial<AgentContext> => ({
|
|
3514
|
-
provider: Providers.OPENAI,
|
|
3515
|
-
reasoningKey: 'reasoning_content',
|
|
3516
|
-
toolDefinitions: [
|
|
3517
|
-
{ name: Constants.EXECUTE_CODE },
|
|
3518
|
-
{ name: 'weather' },
|
|
3519
|
-
],
|
|
3520
|
-
graphTools: [],
|
|
3521
|
-
agentId: 'agent_1',
|
|
3522
|
-
})
|
|
3523
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3524
|
-
});
|
|
3525
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3526
|
-
const handler = new ChatModelStreamHandler();
|
|
3527
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3528
|
-
|
|
3529
|
-
await handler.handle(
|
|
3530
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3531
|
-
{
|
|
3532
|
-
chunk: {
|
|
3533
|
-
content: '',
|
|
3534
|
-
tool_call_chunks: [
|
|
3535
|
-
{
|
|
3536
|
-
id: 'call_weather',
|
|
3537
|
-
name: 'weather',
|
|
3538
|
-
args: '{"city":"NYC"}',
|
|
3539
|
-
index: 0,
|
|
3540
|
-
},
|
|
3541
|
-
],
|
|
3542
|
-
} as unknown as t.StreamChunk,
|
|
3543
|
-
},
|
|
3544
|
-
metadata,
|
|
3545
|
-
graph
|
|
3546
|
-
);
|
|
3547
|
-
await handler.handle(
|
|
3548
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3549
|
-
{
|
|
3550
|
-
chunk: {
|
|
3551
|
-
content: '',
|
|
3552
|
-
tool_call_chunks: [
|
|
3553
|
-
{
|
|
3554
|
-
id: 'call_code',
|
|
3555
|
-
name: Constants.EXECUTE_CODE,
|
|
3556
|
-
args: '{"code":"print(1)"}',
|
|
3557
|
-
index: 1,
|
|
3558
|
-
},
|
|
3559
|
-
],
|
|
3560
|
-
} as unknown as t.StreamChunk,
|
|
3561
|
-
},
|
|
3562
|
-
metadata,
|
|
3563
|
-
graph
|
|
3564
|
-
);
|
|
3565
|
-
|
|
3566
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3567
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3568
|
-
expect.anything(),
|
|
3569
|
-
expect.anything()
|
|
3570
|
-
);
|
|
3571
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3572
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3573
|
-
});
|
|
3574
|
-
|
|
3575
|
-
it('does not prestart streamed Cloudflare sandbox direct coding tools', async () => {
|
|
3576
|
-
const graph = createGraph({
|
|
3577
|
-
toolExecution: {
|
|
3578
|
-
engine: 'cloudflare-sandbox',
|
|
3579
|
-
cloudflare: { sandbox: {} },
|
|
3580
|
-
} as StandardGraph['toolExecution'],
|
|
3581
|
-
getAgentContext: jest.fn(
|
|
3582
|
-
(): Partial<AgentContext> => ({
|
|
3583
|
-
provider: Providers.OPENAI,
|
|
3584
|
-
reasoningKey: 'reasoning_content',
|
|
3585
|
-
toolDefinitions: [{ name: Constants.BASH_TOOL }, { name: 'weather' }],
|
|
3586
|
-
graphTools: [],
|
|
3587
|
-
agentId: 'agent_1',
|
|
3588
|
-
})
|
|
3589
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3590
|
-
});
|
|
3591
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3592
|
-
const handler = new ChatModelStreamHandler();
|
|
3593
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3594
|
-
|
|
3595
|
-
await handler.handle(
|
|
3596
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3597
|
-
{
|
|
3598
|
-
chunk: {
|
|
3599
|
-
content: '',
|
|
3600
|
-
tool_call_chunks: [
|
|
3601
|
-
{
|
|
3602
|
-
id: 'call_weather',
|
|
3603
|
-
name: 'weather',
|
|
3604
|
-
args: '{"city":"NYC"}',
|
|
3605
|
-
index: 0,
|
|
3606
|
-
},
|
|
3607
|
-
],
|
|
3608
|
-
} as unknown as t.StreamChunk,
|
|
3609
|
-
},
|
|
3610
|
-
metadata,
|
|
3611
|
-
graph
|
|
3612
|
-
);
|
|
3613
|
-
await handler.handle(
|
|
3614
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3615
|
-
{
|
|
3616
|
-
chunk: {
|
|
3617
|
-
content: '',
|
|
3618
|
-
tool_call_chunks: [
|
|
3619
|
-
{
|
|
3620
|
-
id: 'call_bash',
|
|
3621
|
-
name: Constants.BASH_TOOL,
|
|
3622
|
-
args: '{"command":"echo ok"}',
|
|
3623
|
-
index: 1,
|
|
3624
|
-
},
|
|
3625
|
-
],
|
|
3626
|
-
} as unknown as t.StreamChunk,
|
|
3627
|
-
},
|
|
3628
|
-
metadata,
|
|
3629
|
-
graph
|
|
3630
|
-
);
|
|
3631
|
-
|
|
3632
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3633
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3634
|
-
expect.anything(),
|
|
3635
|
-
expect.anything()
|
|
3636
|
-
);
|
|
3637
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3638
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3639
|
-
});
|
|
3640
|
-
|
|
3641
|
-
it('prestarts streamed remote bash tools when the next Anthropic tool call begins', async () => {
|
|
3642
|
-
const graph = createGraph({
|
|
3643
|
-
getAgentContext: jest.fn(
|
|
3644
|
-
(): Partial<AgentContext> => ({
|
|
3645
|
-
provider: Providers.ANTHROPIC,
|
|
3646
|
-
reasoningKey: 'reasoning',
|
|
3647
|
-
toolDefinitions: [
|
|
3648
|
-
{ name: Constants.BASH_TOOL },
|
|
3649
|
-
{ name: Constants.READ_FILE },
|
|
3650
|
-
],
|
|
3651
|
-
graphTools: [],
|
|
3652
|
-
agentId: 'agent_1',
|
|
3653
|
-
})
|
|
3654
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3655
|
-
});
|
|
3656
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3657
|
-
jest
|
|
3658
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3659
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3660
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3661
|
-
return;
|
|
3662
|
-
}
|
|
3663
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3664
|
-
toolExecuteCalls.push(batch);
|
|
3665
|
-
batch.resolve(
|
|
3666
|
-
batch.toolCalls.map((call) => ({
|
|
3667
|
-
toolCallId: call.id,
|
|
3668
|
-
status: 'success',
|
|
3669
|
-
content: `ok ${call.name}`,
|
|
3670
|
-
}))
|
|
3671
|
-
);
|
|
3672
|
-
});
|
|
3673
|
-
|
|
3674
|
-
const handler = new ChatModelStreamHandler();
|
|
3675
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3676
|
-
|
|
3677
|
-
await handler.handle(
|
|
3678
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3679
|
-
{
|
|
3680
|
-
chunk: {
|
|
3681
|
-
content: '',
|
|
3682
|
-
tool_call_chunks: [
|
|
3683
|
-
{
|
|
3684
|
-
id: 'toolu_env',
|
|
3685
|
-
name: Constants.BASH_TOOL,
|
|
3686
|
-
args: '{"command":"echo env"}',
|
|
3687
|
-
index: 2,
|
|
3688
|
-
},
|
|
3689
|
-
],
|
|
3690
|
-
} as unknown as t.StreamChunk,
|
|
3691
|
-
},
|
|
3692
|
-
metadata,
|
|
3693
|
-
graph
|
|
3694
|
-
);
|
|
3695
|
-
|
|
3696
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
3697
|
-
|
|
3698
|
-
await handler.handle(
|
|
3699
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3700
|
-
{
|
|
3701
|
-
chunk: {
|
|
3702
|
-
content: '',
|
|
3703
|
-
tool_call_chunks: [
|
|
3704
|
-
{
|
|
3705
|
-
id: 'toolu_net',
|
|
3706
|
-
name: Constants.BASH_TOOL,
|
|
3707
|
-
args: '{"command":"echo net"}',
|
|
3708
|
-
index: 3,
|
|
3709
|
-
},
|
|
3710
|
-
],
|
|
3711
|
-
} as unknown as t.StreamChunk,
|
|
3712
|
-
},
|
|
3713
|
-
metadata,
|
|
3714
|
-
graph
|
|
3715
|
-
);
|
|
3716
|
-
|
|
3717
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
3718
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
3719
|
-
expect.objectContaining({
|
|
3720
|
-
id: 'toolu_env',
|
|
3721
|
-
name: Constants.BASH_TOOL,
|
|
3722
|
-
args: { command: 'echo env' },
|
|
3723
|
-
stepId: expect.stringMatching(/^step_/),
|
|
3724
|
-
turn: 0,
|
|
3725
|
-
}),
|
|
3726
|
-
]);
|
|
3727
|
-
expect(graph.eagerEventToolExecutions.has('toolu_env')).toBe(true);
|
|
3728
|
-
expect(graph.eagerEventToolExecutions.has('toolu_net')).toBe(false);
|
|
3729
|
-
});
|
|
3730
|
-
|
|
3731
|
-
it('does not prestart streamed remote tools when graph tools may appear later', async () => {
|
|
3732
|
-
const graph = createGraph({
|
|
3733
|
-
getAgentContext: jest.fn(
|
|
3734
|
-
(): Partial<AgentContext> => ({
|
|
3735
|
-
provider: Providers.ANTHROPIC,
|
|
3736
|
-
reasoningKey: 'reasoning',
|
|
3737
|
-
toolDefinitions: [
|
|
3738
|
-
{ name: Constants.BASH_TOOL },
|
|
3739
|
-
{ name: Constants.READ_FILE },
|
|
3740
|
-
],
|
|
3741
|
-
graphTools: [
|
|
3742
|
-
{ name: 'transfer_to_researcher' } as unknown as t.GenericTool,
|
|
3743
|
-
],
|
|
3744
|
-
agentId: 'agent_1',
|
|
3745
|
-
})
|
|
3746
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3747
|
-
});
|
|
3748
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3749
|
-
jest
|
|
3750
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3751
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3752
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3753
|
-
return;
|
|
3754
|
-
}
|
|
3755
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3756
|
-
toolExecuteCalls.push(batch);
|
|
3757
|
-
batch.resolve(
|
|
3758
|
-
batch.toolCalls.map((call) => ({
|
|
3759
|
-
toolCallId: call.id,
|
|
3760
|
-
status: 'success',
|
|
3761
|
-
content: `ok ${call.name}`,
|
|
3762
|
-
}))
|
|
3763
|
-
);
|
|
3764
|
-
});
|
|
3765
|
-
|
|
3766
|
-
const handler = new ChatModelStreamHandler();
|
|
3767
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3768
|
-
|
|
3769
|
-
await handler.handle(
|
|
3770
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3771
|
-
{
|
|
3772
|
-
chunk: {
|
|
3773
|
-
content: '',
|
|
3774
|
-
tool_call_chunks: [
|
|
3775
|
-
{
|
|
3776
|
-
id: 'toolu_env',
|
|
3777
|
-
name: Constants.BASH_TOOL,
|
|
3778
|
-
args: '{"command":"echo env"}',
|
|
3779
|
-
index: 2,
|
|
3780
|
-
},
|
|
3781
|
-
],
|
|
3782
|
-
} as unknown as t.StreamChunk,
|
|
3783
|
-
},
|
|
3784
|
-
metadata,
|
|
3785
|
-
graph
|
|
3786
|
-
);
|
|
3787
|
-
|
|
3788
|
-
await handler.handle(
|
|
3789
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3790
|
-
{
|
|
3791
|
-
chunk: {
|
|
3792
|
-
content: '',
|
|
3793
|
-
tool_call_chunks: [
|
|
3794
|
-
{
|
|
3795
|
-
id: 'toolu_net',
|
|
3796
|
-
name: Constants.BASH_TOOL,
|
|
3797
|
-
args: '{"command":"echo net"}',
|
|
3798
|
-
index: 3,
|
|
3799
|
-
},
|
|
3800
|
-
],
|
|
3801
|
-
} as unknown as t.StreamChunk,
|
|
3802
|
-
},
|
|
3803
|
-
metadata,
|
|
3804
|
-
graph
|
|
3805
|
-
);
|
|
3806
|
-
|
|
3807
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
3808
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
3809
|
-
expect(graph.eagerEventToolCallChunks.size).toBe(0);
|
|
3810
|
-
});
|
|
3811
|
-
|
|
3812
|
-
it('prestarts streamed remote bash tools when code output references are enabled', async () => {
|
|
3813
|
-
const graph = createGraph({
|
|
3814
|
-
toolOutputReferences: { enabled: true },
|
|
3815
|
-
getAgentContext: jest.fn(
|
|
3816
|
-
(): Partial<AgentContext> => ({
|
|
3817
|
-
provider: Providers.ANTHROPIC,
|
|
3818
|
-
reasoningKey: 'reasoning',
|
|
3819
|
-
toolDefinitions: [
|
|
3820
|
-
{ name: Constants.BASH_TOOL },
|
|
3821
|
-
{ name: Constants.READ_FILE },
|
|
3822
|
-
],
|
|
3823
|
-
graphTools: [],
|
|
3824
|
-
agentId: 'agent_1',
|
|
3825
|
-
})
|
|
3826
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3827
|
-
});
|
|
3828
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
3829
|
-
jest
|
|
3830
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
3831
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
3832
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
3833
|
-
return;
|
|
3834
|
-
}
|
|
3835
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
3836
|
-
toolExecuteCalls.push(batch);
|
|
3837
|
-
batch.resolve(
|
|
3838
|
-
batch.toolCalls.map((call) => ({
|
|
3839
|
-
toolCallId: call.id,
|
|
3840
|
-
status: 'success',
|
|
3841
|
-
content: `ok ${call.name}`,
|
|
3842
|
-
}))
|
|
3843
|
-
);
|
|
3844
|
-
});
|
|
3845
|
-
|
|
3846
|
-
const handler = new ChatModelStreamHandler();
|
|
3847
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3848
|
-
|
|
3849
|
-
for (const args of ['{"command":"echo ', 'env && ', 'pwd"}']) {
|
|
3850
|
-
await handler.handle(
|
|
3851
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3852
|
-
{
|
|
3853
|
-
chunk: {
|
|
3854
|
-
content: '',
|
|
3855
|
-
tool_call_chunks: [
|
|
3856
|
-
{
|
|
3857
|
-
id: 'toolu_env',
|
|
3858
|
-
name: Constants.BASH_TOOL,
|
|
3859
|
-
args,
|
|
3860
|
-
index: 2,
|
|
3861
|
-
},
|
|
3862
|
-
],
|
|
3863
|
-
} as unknown as t.StreamChunk,
|
|
3864
|
-
},
|
|
3865
|
-
metadata,
|
|
3866
|
-
graph
|
|
3867
|
-
);
|
|
3868
|
-
}
|
|
3869
|
-
|
|
3870
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
3871
|
-
|
|
3872
|
-
await handler.handle(
|
|
3873
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3874
|
-
{
|
|
3875
|
-
chunk: {
|
|
3876
|
-
content: '',
|
|
3877
|
-
tool_call_chunks: [
|
|
3878
|
-
{
|
|
3879
|
-
id: 'toolu_net',
|
|
3880
|
-
name: Constants.BASH_TOOL,
|
|
3881
|
-
args: '{"command":"echo net"}',
|
|
3882
|
-
index: 3,
|
|
3883
|
-
},
|
|
3884
|
-
],
|
|
3885
|
-
} as unknown as t.StreamChunk,
|
|
3886
|
-
},
|
|
3887
|
-
metadata,
|
|
3888
|
-
graph
|
|
3889
|
-
);
|
|
3890
|
-
|
|
3891
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
3892
|
-
expect(toolExecuteCalls[0].toolCalls).toEqual([
|
|
3893
|
-
expect.objectContaining({
|
|
3894
|
-
id: 'toolu_env',
|
|
3895
|
-
name: Constants.BASH_TOOL,
|
|
3896
|
-
args: { command: 'echo env && pwd' },
|
|
3897
|
-
}),
|
|
3898
|
-
]);
|
|
3899
|
-
});
|
|
3900
|
-
|
|
3901
|
-
it('does not prestart streamed code tools whose args contain output references', async () => {
|
|
3902
|
-
const graph = createGraph({
|
|
3903
|
-
toolOutputReferences: { enabled: true },
|
|
3904
|
-
getAgentContext: jest.fn(
|
|
3905
|
-
(): Partial<AgentContext> => ({
|
|
3906
|
-
provider: Providers.ANTHROPIC,
|
|
3907
|
-
reasoningKey: 'reasoning',
|
|
3908
|
-
toolDefinitions: [
|
|
3909
|
-
{ name: Constants.BASH_TOOL },
|
|
3910
|
-
{ name: Constants.READ_FILE },
|
|
3911
|
-
],
|
|
3912
|
-
graphTools: [],
|
|
3913
|
-
agentId: 'agent_1',
|
|
3914
|
-
})
|
|
3915
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3916
|
-
});
|
|
3917
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3918
|
-
const handler = new ChatModelStreamHandler();
|
|
3919
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3920
|
-
|
|
3921
|
-
await handler.handle(
|
|
3922
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3923
|
-
{
|
|
3924
|
-
chunk: {
|
|
3925
|
-
content: '',
|
|
3926
|
-
tool_call_chunks: [
|
|
3927
|
-
{
|
|
3928
|
-
id: 'toolu_ref',
|
|
3929
|
-
name: Constants.BASH_TOOL,
|
|
3930
|
-
args: '{"command":"cat <<EOF\\n{{tool0turn0}}\\nEOF"}',
|
|
3931
|
-
index: 2,
|
|
3932
|
-
},
|
|
3933
|
-
],
|
|
3934
|
-
} as unknown as t.StreamChunk,
|
|
3935
|
-
},
|
|
3936
|
-
metadata,
|
|
3937
|
-
graph
|
|
3938
|
-
);
|
|
3939
|
-
await handler.handle(
|
|
3940
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3941
|
-
{
|
|
3942
|
-
chunk: {
|
|
3943
|
-
content: '',
|
|
3944
|
-
tool_call_chunks: [
|
|
3945
|
-
{
|
|
3946
|
-
id: 'toolu_next',
|
|
3947
|
-
name: Constants.BASH_TOOL,
|
|
3948
|
-
args: '{"command":"echo next"}',
|
|
3949
|
-
index: 3,
|
|
3950
|
-
},
|
|
3951
|
-
],
|
|
3952
|
-
} as unknown as t.StreamChunk,
|
|
3953
|
-
},
|
|
3954
|
-
metadata,
|
|
3955
|
-
graph
|
|
3956
|
-
);
|
|
3957
|
-
|
|
3958
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
3959
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
3960
|
-
expect.anything(),
|
|
3961
|
-
expect.anything()
|
|
3962
|
-
);
|
|
3963
|
-
});
|
|
3964
|
-
|
|
3965
|
-
it('does not prestart streamed tools when the next Anthropic tool call is a graph tool', async () => {
|
|
3966
|
-
const handoffToolName = `${Constants.LC_TRANSFER_TO_}researcher`;
|
|
3967
|
-
const graph = createGraph({
|
|
3968
|
-
getAgentContext: jest.fn(
|
|
3969
|
-
(): Partial<AgentContext> => ({
|
|
3970
|
-
provider: Providers.ANTHROPIC,
|
|
3971
|
-
reasoningKey: 'reasoning',
|
|
3972
|
-
toolDefinitions: [
|
|
3973
|
-
{ name: Constants.BASH_TOOL },
|
|
3974
|
-
{ name: handoffToolName },
|
|
3975
|
-
],
|
|
3976
|
-
graphTools: [{ name: handoffToolName } as unknown as t.GenericTool],
|
|
3977
|
-
agentId: 'agent_1',
|
|
3978
|
-
})
|
|
3979
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
3980
|
-
});
|
|
3981
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
3982
|
-
const handler = new ChatModelStreamHandler();
|
|
3983
|
-
const metadata = { langgraph_node: 'agent' };
|
|
3984
|
-
|
|
3985
|
-
await handler.handle(
|
|
3986
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
3987
|
-
{
|
|
3988
|
-
chunk: {
|
|
3989
|
-
content: '',
|
|
3990
|
-
tool_call_chunks: [
|
|
3991
|
-
{
|
|
3992
|
-
id: 'toolu_env',
|
|
3993
|
-
name: Constants.BASH_TOOL,
|
|
3994
|
-
args: '{"command":"echo env"}',
|
|
3995
|
-
index: 2,
|
|
3996
|
-
},
|
|
3997
|
-
],
|
|
3998
|
-
} as unknown as t.StreamChunk,
|
|
3999
|
-
},
|
|
4000
|
-
metadata,
|
|
4001
|
-
graph
|
|
4002
|
-
);
|
|
4003
|
-
|
|
4004
|
-
await handler.handle(
|
|
4005
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4006
|
-
{
|
|
4007
|
-
chunk: {
|
|
4008
|
-
content: '',
|
|
4009
|
-
tool_call_chunks: [
|
|
4010
|
-
{
|
|
4011
|
-
id: 'toolu_handoff',
|
|
4012
|
-
name: handoffToolName,
|
|
4013
|
-
args: '{"message":"check this"}',
|
|
4014
|
-
index: 3,
|
|
4015
|
-
},
|
|
4016
|
-
],
|
|
4017
|
-
} as unknown as t.StreamChunk,
|
|
4018
|
-
},
|
|
4019
|
-
metadata,
|
|
4020
|
-
graph
|
|
4021
|
-
);
|
|
4022
|
-
|
|
4023
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4024
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4025
|
-
expect.anything(),
|
|
4026
|
-
expect.anything()
|
|
4027
|
-
);
|
|
4028
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4029
|
-
});
|
|
4030
|
-
|
|
4031
|
-
it('does not prestart streamed tools after a graph tool appeared earlier in the same step', async () => {
|
|
4032
|
-
const handoffToolName = `${Constants.LC_TRANSFER_TO_}researcher`;
|
|
4033
|
-
const graph = createGraph({
|
|
4034
|
-
getAgentContext: jest.fn(
|
|
4035
|
-
(): Partial<AgentContext> => ({
|
|
4036
|
-
provider: Providers.ANTHROPIC,
|
|
4037
|
-
reasoningKey: 'reasoning',
|
|
4038
|
-
toolDefinitions: [
|
|
4039
|
-
{ name: Constants.BASH_TOOL },
|
|
4040
|
-
{ name: handoffToolName },
|
|
4041
|
-
],
|
|
4042
|
-
graphTools: [{ name: handoffToolName } as unknown as t.GenericTool],
|
|
4043
|
-
agentId: 'agent_1',
|
|
4044
|
-
})
|
|
4045
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4046
|
-
});
|
|
4047
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
4048
|
-
const handler = new ChatModelStreamHandler();
|
|
4049
|
-
const metadata = { langgraph_node: 'agent' };
|
|
4050
|
-
|
|
4051
|
-
await handler.handle(
|
|
4052
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4053
|
-
{
|
|
4054
|
-
chunk: {
|
|
4055
|
-
content: '',
|
|
4056
|
-
tool_call_chunks: [
|
|
4057
|
-
{
|
|
4058
|
-
id: 'toolu_env',
|
|
4059
|
-
name: Constants.BASH_TOOL,
|
|
4060
|
-
args: '{"command":"echo env"}',
|
|
4061
|
-
index: 2,
|
|
4062
|
-
},
|
|
4063
|
-
],
|
|
4064
|
-
} as unknown as t.StreamChunk,
|
|
4065
|
-
},
|
|
4066
|
-
metadata,
|
|
4067
|
-
graph
|
|
4068
|
-
);
|
|
4069
|
-
await handler.handle(
|
|
4070
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4071
|
-
{
|
|
4072
|
-
chunk: {
|
|
4073
|
-
content: '',
|
|
4074
|
-
tool_call_chunks: [
|
|
4075
|
-
{
|
|
4076
|
-
id: 'toolu_handoff',
|
|
4077
|
-
name: handoffToolName,
|
|
4078
|
-
args: '{"message":"partial',
|
|
4079
|
-
index: 3,
|
|
4080
|
-
},
|
|
4081
|
-
],
|
|
4082
|
-
} as unknown as t.StreamChunk,
|
|
4083
|
-
},
|
|
4084
|
-
metadata,
|
|
4085
|
-
graph
|
|
4086
|
-
);
|
|
4087
|
-
await handler.handle(
|
|
4088
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4089
|
-
{
|
|
4090
|
-
chunk: {
|
|
4091
|
-
content: '',
|
|
4092
|
-
tool_call_chunks: [
|
|
4093
|
-
{
|
|
4094
|
-
id: 'toolu_next',
|
|
4095
|
-
name: Constants.BASH_TOOL,
|
|
4096
|
-
args: '{"command":"echo next"}',
|
|
4097
|
-
index: 4,
|
|
4098
|
-
},
|
|
4099
|
-
],
|
|
4100
|
-
} as unknown as t.StreamChunk,
|
|
4101
|
-
},
|
|
4102
|
-
metadata,
|
|
4103
|
-
graph
|
|
4104
|
-
);
|
|
4105
|
-
|
|
4106
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4107
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4108
|
-
expect.anything(),
|
|
4109
|
-
expect.anything()
|
|
4110
|
-
);
|
|
4111
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4112
|
-
});
|
|
4113
|
-
|
|
4114
|
-
it('does not prestart event tools in a mixed direct-tool batch', async () => {
|
|
4115
|
-
const graph = createGraph({
|
|
4116
|
-
toolExecution: {
|
|
4117
|
-
engine: 'local',
|
|
4118
|
-
} as StandardGraph['toolExecution'],
|
|
4119
|
-
getAgentContext: jest.fn(
|
|
4120
|
-
(): Partial<AgentContext> => ({
|
|
4121
|
-
provider: Providers.OPENAI,
|
|
4122
|
-
reasoningKey: 'reasoning_content',
|
|
4123
|
-
toolDefinitions: [
|
|
4124
|
-
{ name: Constants.EXECUTE_CODE },
|
|
4125
|
-
{ name: 'weather' },
|
|
4126
|
-
],
|
|
4127
|
-
graphTools: [],
|
|
4128
|
-
agentId: 'agent_1',
|
|
4129
|
-
})
|
|
4130
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4131
|
-
});
|
|
4132
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
4133
|
-
|
|
4134
|
-
await new ChatModelStreamHandler().handle(
|
|
4135
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4136
|
-
{
|
|
4137
|
-
chunk: {
|
|
4138
|
-
content: '',
|
|
4139
|
-
tool_calls: [
|
|
4140
|
-
{
|
|
4141
|
-
id: 'call_code',
|
|
4142
|
-
name: Constants.EXECUTE_CODE,
|
|
4143
|
-
args: { code: 'print(1)' },
|
|
4144
|
-
},
|
|
4145
|
-
{
|
|
4146
|
-
id: 'call_weather',
|
|
4147
|
-
name: 'weather',
|
|
4148
|
-
args: { city: 'NYC' },
|
|
4149
|
-
},
|
|
4150
|
-
],
|
|
4151
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
4152
|
-
} as unknown as t.StreamChunk,
|
|
4153
|
-
},
|
|
4154
|
-
{ langgraph_node: 'agent' },
|
|
4155
|
-
graph
|
|
4156
|
-
);
|
|
4157
|
-
|
|
4158
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4159
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4160
|
-
expect.anything(),
|
|
4161
|
-
expect.anything()
|
|
4162
|
-
);
|
|
4163
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4164
|
-
});
|
|
4165
|
-
|
|
4166
|
-
it('continues eager turns after normal event-dispatch usage', async () => {
|
|
4167
|
-
const graph = createGraph();
|
|
4168
|
-
graph.eagerEventToolUsageCount.set('weather', 1);
|
|
4169
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4170
|
-
jest
|
|
4171
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4172
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4173
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4174
|
-
return;
|
|
4175
|
-
}
|
|
4176
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4177
|
-
toolExecuteCalls.push(batch);
|
|
4178
|
-
batch.resolve([
|
|
4179
|
-
{
|
|
4180
|
-
toolCallId: 'call_weather_2',
|
|
4181
|
-
status: 'success',
|
|
4182
|
-
content: 'sunny',
|
|
4183
|
-
},
|
|
4184
|
-
]);
|
|
4185
|
-
});
|
|
4186
|
-
|
|
4187
|
-
await new ChatModelStreamHandler().handle(
|
|
4188
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4189
|
-
{
|
|
4190
|
-
chunk: {
|
|
4191
|
-
content: '',
|
|
4192
|
-
tool_calls: [
|
|
4193
|
-
{
|
|
4194
|
-
id: 'call_weather_2',
|
|
4195
|
-
name: 'weather',
|
|
4196
|
-
args: { city: 'NYC' },
|
|
4197
|
-
},
|
|
4198
|
-
],
|
|
4199
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
4200
|
-
} as unknown as t.StreamChunk,
|
|
4201
|
-
},
|
|
4202
|
-
{ langgraph_node: 'agent' },
|
|
4203
|
-
graph
|
|
4204
|
-
);
|
|
4205
|
-
|
|
4206
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4207
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
4208
|
-
id: 'call_weather_2',
|
|
4209
|
-
name: 'weather',
|
|
4210
|
-
turn: 1,
|
|
4211
|
-
});
|
|
4212
|
-
expect(graph.eagerEventToolUsageCount.get('weather')).toBe(2);
|
|
4213
|
-
});
|
|
4214
|
-
|
|
4215
|
-
it('prestarts Bedrock Converse streamed tool calls on content block stop seals', async () => {
|
|
4216
|
-
const graph = createGraph({
|
|
4217
|
-
getAgentContext: jest.fn(
|
|
4218
|
-
(): Partial<AgentContext> => ({
|
|
4219
|
-
provider: Providers.BEDROCK,
|
|
4220
|
-
reasoningKey: 'reasoning_content',
|
|
4221
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
4222
|
-
graphTools: [],
|
|
4223
|
-
agentId: 'agent_1',
|
|
4224
|
-
})
|
|
4225
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4226
|
-
});
|
|
4227
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4228
|
-
jest
|
|
4229
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4230
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4231
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4232
|
-
return;
|
|
4233
|
-
}
|
|
4234
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4235
|
-
toolExecuteCalls.push(batch);
|
|
4236
|
-
batch.resolve([
|
|
4237
|
-
{
|
|
4238
|
-
toolCallId: 'call_weather',
|
|
4239
|
-
status: 'success',
|
|
4240
|
-
content: 'sunny',
|
|
4241
|
-
},
|
|
4242
|
-
]);
|
|
4243
|
-
});
|
|
4244
|
-
|
|
4245
|
-
const handler = new ChatModelStreamHandler();
|
|
4246
|
-
const metadata = { langgraph_node: 'agent' };
|
|
4247
|
-
|
|
4248
|
-
await handler.handle(
|
|
4249
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4250
|
-
{
|
|
4251
|
-
chunk: {
|
|
4252
|
-
content: '',
|
|
4253
|
-
tool_call_chunks: [
|
|
4254
|
-
{
|
|
4255
|
-
id: 'call_weather',
|
|
4256
|
-
name: 'weather',
|
|
4257
|
-
index: 1,
|
|
4258
|
-
},
|
|
4259
|
-
],
|
|
4260
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4261
|
-
} as unknown as t.StreamChunk,
|
|
4262
|
-
},
|
|
4263
|
-
metadata,
|
|
4264
|
-
graph
|
|
4265
|
-
);
|
|
4266
|
-
await handler.handle(
|
|
4267
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4268
|
-
{
|
|
4269
|
-
chunk: {
|
|
4270
|
-
content: '',
|
|
4271
|
-
tool_call_chunks: [
|
|
4272
|
-
{
|
|
4273
|
-
args: '{"city":"N',
|
|
4274
|
-
index: 1,
|
|
4275
|
-
},
|
|
4276
|
-
],
|
|
4277
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4278
|
-
} as unknown as t.StreamChunk,
|
|
4279
|
-
},
|
|
4280
|
-
metadata,
|
|
4281
|
-
graph
|
|
4282
|
-
);
|
|
4283
|
-
await handler.handle(
|
|
4284
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4285
|
-
{
|
|
4286
|
-
chunk: {
|
|
4287
|
-
content: '',
|
|
4288
|
-
tool_call_chunks: [
|
|
4289
|
-
{
|
|
4290
|
-
args: 'YC"}',
|
|
4291
|
-
index: 1,
|
|
4292
|
-
},
|
|
4293
|
-
],
|
|
4294
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4295
|
-
} as unknown as t.StreamChunk,
|
|
4296
|
-
},
|
|
4297
|
-
metadata,
|
|
4298
|
-
graph
|
|
4299
|
-
);
|
|
4300
|
-
|
|
4301
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4302
|
-
|
|
4303
|
-
await handler.handle(
|
|
4304
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4305
|
-
{
|
|
4306
|
-
chunk: {
|
|
4307
|
-
content: '',
|
|
4308
|
-
tool_call_chunks: [
|
|
4309
|
-
{
|
|
4310
|
-
args: '',
|
|
4311
|
-
index: 1,
|
|
4312
|
-
},
|
|
4313
|
-
],
|
|
4314
|
-
response_metadata: {
|
|
4315
|
-
...bedrockConverseToolCallMetadata,
|
|
4316
|
-
[STREAMED_TOOL_CALL_SEAL_METADATA_KEY]: {
|
|
4317
|
-
kind: 'single',
|
|
4318
|
-
index: 1,
|
|
4319
|
-
},
|
|
4320
|
-
},
|
|
4321
|
-
} as unknown as t.StreamChunk,
|
|
4322
|
-
},
|
|
4323
|
-
metadata,
|
|
4324
|
-
graph
|
|
4325
|
-
);
|
|
4326
|
-
|
|
4327
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4328
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
4329
|
-
id: 'call_weather',
|
|
4330
|
-
name: 'weather',
|
|
4331
|
-
args: { city: 'NYC' },
|
|
4332
|
-
stepId: expect.stringMatching(/^step_/),
|
|
4333
|
-
turn: 0,
|
|
4334
|
-
});
|
|
4335
|
-
expect(
|
|
4336
|
-
graph.eagerEventToolCallChunks.has(chunkStateKey('step-key', 1))
|
|
4337
|
-
).toBe(false);
|
|
4338
|
-
});
|
|
4339
|
-
|
|
4340
|
-
it('prestarts each Bedrock tool call at its own stop seal in multi-call turns', async () => {
|
|
4341
|
-
const graph = createGraph({
|
|
4342
|
-
getAgentContext: jest.fn(
|
|
4343
|
-
(): Partial<AgentContext> => ({
|
|
4344
|
-
provider: Providers.BEDROCK,
|
|
4345
|
-
reasoningKey: 'reasoning_content',
|
|
4346
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
4347
|
-
graphTools: [],
|
|
4348
|
-
agentId: 'agent_1',
|
|
4349
|
-
})
|
|
4350
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4351
|
-
});
|
|
4352
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4353
|
-
jest
|
|
4354
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4355
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4356
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4357
|
-
return;
|
|
4358
|
-
}
|
|
4359
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4360
|
-
toolExecuteCalls.push(batch);
|
|
4361
|
-
batch.resolve(
|
|
4362
|
-
batch.toolCalls.map((call) => ({
|
|
4363
|
-
toolCallId: call.id,
|
|
4364
|
-
status: 'success',
|
|
4365
|
-
content: `ok ${call.name}`,
|
|
4366
|
-
}))
|
|
4367
|
-
);
|
|
4368
|
-
});
|
|
4369
|
-
|
|
4370
|
-
const handler = new ChatModelStreamHandler();
|
|
4371
|
-
const metadata = { langgraph_node: 'agent' };
|
|
4372
|
-
const sendChunk = async (chunk: Record<string, unknown>): Promise<void> =>
|
|
4373
|
-
handler.handle(
|
|
4374
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4375
|
-
{ chunk: chunk as unknown as t.StreamChunk },
|
|
4376
|
-
metadata,
|
|
4377
|
-
graph
|
|
4378
|
-
);
|
|
4379
|
-
|
|
4380
|
-
await sendChunk({
|
|
4381
|
-
content: '',
|
|
4382
|
-
tool_call_chunks: [{ id: 'call_weather', name: 'weather', index: 1 }],
|
|
4383
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4384
|
-
});
|
|
4385
|
-
await sendChunk({
|
|
4386
|
-
content: '',
|
|
4387
|
-
tool_call_chunks: [{ args: '{"city":"NYC"}', index: 1 }],
|
|
4388
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4389
|
-
});
|
|
4390
|
-
await sendChunk({
|
|
4391
|
-
content: '',
|
|
4392
|
-
tool_call_chunks: [{ args: '', index: 1 }],
|
|
4393
|
-
response_metadata: {
|
|
4394
|
-
...bedrockConverseToolCallMetadata,
|
|
4395
|
-
[STREAMED_TOOL_CALL_SEAL_METADATA_KEY]: { kind: 'single', index: 1 },
|
|
4396
|
-
},
|
|
4397
|
-
});
|
|
4398
|
-
|
|
4399
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4400
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
4401
|
-
id: 'call_weather',
|
|
4402
|
-
args: { city: 'NYC' },
|
|
4403
|
-
});
|
|
4404
|
-
|
|
4405
|
-
await sendChunk({
|
|
4406
|
-
content: '',
|
|
4407
|
-
tool_call_chunks: [{ id: 'call_stock', name: 'stock', index: 2 }],
|
|
4408
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4409
|
-
});
|
|
4410
|
-
await sendChunk({
|
|
4411
|
-
content: '',
|
|
4412
|
-
tool_call_chunks: [{ args: '{"ticker":"CH"}', index: 2 }],
|
|
4413
|
-
response_metadata: bedrockConverseToolCallMetadata,
|
|
4414
|
-
});
|
|
4415
|
-
|
|
4416
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4417
|
-
|
|
4418
|
-
await sendChunk({
|
|
4419
|
-
content: '',
|
|
4420
|
-
tool_call_chunks: [{ args: '', index: 2 }],
|
|
4421
|
-
response_metadata: {
|
|
4422
|
-
...bedrockConverseToolCallMetadata,
|
|
4423
|
-
[STREAMED_TOOL_CALL_SEAL_METADATA_KEY]: { kind: 'single', index: 2 },
|
|
4424
|
-
},
|
|
4425
|
-
});
|
|
4426
|
-
|
|
4427
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
4428
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
4429
|
-
id: 'call_stock',
|
|
4430
|
-
args: { ticker: 'CH' },
|
|
4431
|
-
turn: 0,
|
|
4432
|
-
});
|
|
4433
|
-
});
|
|
4434
|
-
|
|
4435
|
-
it('prestarts Google tool calls sealed on arrival without a final tool-call signal', async () => {
|
|
4436
|
-
const graph = createGraph({
|
|
4437
|
-
getAgentContext: jest.fn(
|
|
4438
|
-
(): Partial<AgentContext> => ({
|
|
4439
|
-
provider: Providers.GOOGLE,
|
|
4440
|
-
reasoningKey: 'reasoning',
|
|
4441
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
4442
|
-
graphTools: [],
|
|
4443
|
-
agentId: 'agent_1',
|
|
4444
|
-
})
|
|
4445
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4446
|
-
});
|
|
4447
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4448
|
-
jest
|
|
4449
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4450
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4451
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4452
|
-
return;
|
|
4453
|
-
}
|
|
4454
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4455
|
-
toolExecuteCalls.push(batch);
|
|
4456
|
-
batch.resolve(
|
|
4457
|
-
batch.toolCalls.map((call) => ({
|
|
4458
|
-
toolCallId: call.id,
|
|
4459
|
-
status: 'success',
|
|
4460
|
-
content: `ok ${call.name}`,
|
|
4461
|
-
}))
|
|
4462
|
-
);
|
|
4463
|
-
});
|
|
4464
|
-
|
|
4465
|
-
const handler = new ChatModelStreamHandler();
|
|
4466
|
-
const metadata = { langgraph_node: 'agent' };
|
|
4467
|
-
|
|
4468
|
-
await handler.handle(
|
|
4469
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4470
|
-
{
|
|
4471
|
-
chunk: {
|
|
4472
|
-
content: '',
|
|
4473
|
-
tool_calls: [
|
|
4474
|
-
{
|
|
4475
|
-
id: 'call_weather',
|
|
4476
|
-
name: 'weather',
|
|
4477
|
-
args: { city: 'NYC' },
|
|
4478
|
-
},
|
|
4479
|
-
],
|
|
4480
|
-
tool_call_chunks: [
|
|
4481
|
-
{
|
|
4482
|
-
id: 'call_weather',
|
|
4483
|
-
name: 'weather',
|
|
4484
|
-
args: '{"city":"NYC"}',
|
|
4485
|
-
},
|
|
4486
|
-
],
|
|
4487
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4488
|
-
} as unknown as t.StreamChunk,
|
|
4489
|
-
},
|
|
4490
|
-
metadata,
|
|
4491
|
-
graph
|
|
4492
|
-
);
|
|
4493
|
-
|
|
4494
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4495
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
4496
|
-
id: 'call_weather',
|
|
4497
|
-
name: 'weather',
|
|
4498
|
-
args: { city: 'NYC' },
|
|
4499
|
-
stepId: expect.stringMatching(/^step_/),
|
|
4500
|
-
turn: 0,
|
|
4501
|
-
});
|
|
4502
|
-
|
|
4503
|
-
// A later chunk with another sealed call prestarts independently.
|
|
4504
|
-
await handler.handle(
|
|
4505
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4506
|
-
{
|
|
4507
|
-
chunk: {
|
|
4508
|
-
content: '',
|
|
4509
|
-
tool_calls: [
|
|
4510
|
-
{
|
|
4511
|
-
id: 'call_stock',
|
|
4512
|
-
name: 'stock',
|
|
4513
|
-
args: { ticker: 'CH' },
|
|
4514
|
-
},
|
|
4515
|
-
],
|
|
4516
|
-
tool_call_chunks: [
|
|
4517
|
-
{
|
|
4518
|
-
id: 'call_stock',
|
|
4519
|
-
name: 'stock',
|
|
4520
|
-
args: '{"ticker":"CH"}',
|
|
4521
|
-
},
|
|
4522
|
-
],
|
|
4523
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4524
|
-
} as unknown as t.StreamChunk,
|
|
4525
|
-
},
|
|
4526
|
-
metadata,
|
|
4527
|
-
graph
|
|
4528
|
-
);
|
|
4529
|
-
|
|
4530
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
4531
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
4532
|
-
id: 'call_stock',
|
|
4533
|
-
name: 'stock',
|
|
4534
|
-
args: { ticker: 'CH' },
|
|
4535
|
-
turn: 0,
|
|
4536
|
-
});
|
|
4537
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(true);
|
|
4538
|
-
expect(graph.eagerEventToolExecutions.has('call_stock')).toBe(true);
|
|
4539
|
-
});
|
|
4540
|
-
|
|
4541
|
-
it('keeps Google tool calls lazy without an on-arrival seal', async () => {
|
|
4542
|
-
const graph = createGraph({
|
|
4543
|
-
getAgentContext: jest.fn(
|
|
4544
|
-
(): Partial<AgentContext> => ({
|
|
4545
|
-
provider: Providers.GOOGLE,
|
|
4546
|
-
reasoningKey: 'reasoning',
|
|
4547
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
4548
|
-
graphTools: [],
|
|
4549
|
-
agentId: 'agent_1',
|
|
4550
|
-
})
|
|
4551
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4552
|
-
});
|
|
4553
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4554
|
-
jest
|
|
4555
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4556
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4557
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4558
|
-
return;
|
|
4559
|
-
}
|
|
4560
|
-
toolExecuteCalls.push(data as t.ToolExecuteBatchRequest);
|
|
4561
|
-
});
|
|
4562
|
-
|
|
4563
|
-
await new ChatModelStreamHandler().handle(
|
|
4564
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4565
|
-
{
|
|
4566
|
-
chunk: {
|
|
4567
|
-
content: '',
|
|
4568
|
-
tool_calls: [
|
|
4569
|
-
{
|
|
4570
|
-
id: 'call_weather',
|
|
4571
|
-
name: 'weather',
|
|
4572
|
-
args: { city: 'NYC' },
|
|
4573
|
-
},
|
|
4574
|
-
],
|
|
4575
|
-
tool_call_chunks: [
|
|
4576
|
-
{
|
|
4577
|
-
id: 'call_weather',
|
|
4578
|
-
name: 'weather',
|
|
4579
|
-
args: '{"city":"NYC"}',
|
|
4580
|
-
},
|
|
4581
|
-
],
|
|
4582
|
-
} as unknown as t.StreamChunk,
|
|
4583
|
-
},
|
|
4584
|
-
{ langgraph_node: 'agent' },
|
|
4585
|
-
graph
|
|
4586
|
-
);
|
|
4587
|
-
|
|
4588
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4589
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4590
|
-
});
|
|
4591
|
-
|
|
4592
|
-
it('does not prestart on-arrival sealed calls when batch-sensitive hooks are configured', async () => {
|
|
4593
|
-
const graph = createGraph({
|
|
4594
|
-
hookRegistry: createResultAlteringRegistry(),
|
|
4595
|
-
});
|
|
4596
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
4597
|
-
|
|
4598
|
-
await new ChatModelStreamHandler().handle(
|
|
4599
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4600
|
-
{
|
|
4601
|
-
chunk: {
|
|
4602
|
-
content: '',
|
|
4603
|
-
tool_calls: [
|
|
4604
|
-
{
|
|
4605
|
-
id: 'call_weather',
|
|
4606
|
-
name: 'weather',
|
|
4607
|
-
args: { city: 'NYC' },
|
|
4608
|
-
},
|
|
4609
|
-
],
|
|
4610
|
-
tool_call_chunks: [
|
|
4611
|
-
{
|
|
4612
|
-
id: 'call_weather',
|
|
4613
|
-
name: 'weather',
|
|
4614
|
-
args: '{"city":"NYC"}',
|
|
4615
|
-
},
|
|
4616
|
-
],
|
|
4617
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4618
|
-
} as unknown as t.StreamChunk,
|
|
4619
|
-
},
|
|
4620
|
-
{ langgraph_node: 'agent' },
|
|
4621
|
-
graph
|
|
4622
|
-
);
|
|
4623
|
-
|
|
4624
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4625
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4626
|
-
expect.anything(),
|
|
4627
|
-
expect.anything()
|
|
4628
|
-
);
|
|
4629
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4630
|
-
});
|
|
4631
|
-
|
|
4632
|
-
it('does not prestart on-arrival sealed calls for programmatic tool calling batches', async () => {
|
|
4633
|
-
const graph = createGraph();
|
|
4634
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
4635
|
-
|
|
4636
|
-
await new ChatModelStreamHandler().handle(
|
|
4637
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4638
|
-
{
|
|
4639
|
-
chunk: {
|
|
4640
|
-
content: '',
|
|
4641
|
-
tool_calls: [
|
|
4642
|
-
{
|
|
4643
|
-
id: 'call_weather',
|
|
4644
|
-
name: 'weather',
|
|
4645
|
-
args: { city: 'NYC' },
|
|
4646
|
-
},
|
|
4647
|
-
],
|
|
4648
|
-
tool_call_chunks: [
|
|
4649
|
-
{
|
|
4650
|
-
id: 'call_weather',
|
|
4651
|
-
name: 'weather',
|
|
4652
|
-
args: '{"city":"NYC"}',
|
|
4653
|
-
},
|
|
4654
|
-
],
|
|
4655
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4656
|
-
} as unknown as t.StreamChunk,
|
|
4657
|
-
},
|
|
4658
|
-
{
|
|
4659
|
-
langgraph_node: 'agent',
|
|
4660
|
-
[Constants.PROGRAMMATIC_TOOL_CALLING]: true,
|
|
4661
|
-
},
|
|
4662
|
-
graph
|
|
4663
|
-
);
|
|
4664
|
-
|
|
4665
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4666
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4667
|
-
expect.anything(),
|
|
4668
|
-
expect.anything()
|
|
4669
|
-
);
|
|
4670
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4671
|
-
});
|
|
4672
|
-
|
|
4673
|
-
it('does not prestart on-arrival sealed calls when eager execution is disabled', async () => {
|
|
4674
|
-
const graph = createGraph({
|
|
4675
|
-
eagerEventToolExecution: { enabled: false },
|
|
4676
|
-
} as Partial<StandardGraph>);
|
|
4677
|
-
const dispatchSpy = jest.spyOn(events, 'safeDispatchCustomEvent');
|
|
4678
|
-
|
|
4679
|
-
await new ChatModelStreamHandler().handle(
|
|
4680
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4681
|
-
{
|
|
4682
|
-
chunk: {
|
|
4683
|
-
content: '',
|
|
4684
|
-
tool_calls: [
|
|
4685
|
-
{
|
|
4686
|
-
id: 'call_weather',
|
|
4687
|
-
name: 'weather',
|
|
4688
|
-
args: { city: 'NYC' },
|
|
4689
|
-
},
|
|
4690
|
-
],
|
|
4691
|
-
tool_call_chunks: [
|
|
4692
|
-
{
|
|
4693
|
-
id: 'call_weather',
|
|
4694
|
-
name: 'weather',
|
|
4695
|
-
args: '{"city":"NYC"}',
|
|
4696
|
-
},
|
|
4697
|
-
],
|
|
4698
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4699
|
-
} as unknown as t.StreamChunk,
|
|
4700
|
-
},
|
|
4701
|
-
{ langgraph_node: 'agent' },
|
|
4702
|
-
graph
|
|
4703
|
-
);
|
|
4704
|
-
|
|
4705
|
-
expect(dispatchSpy).not.toHaveBeenCalledWith(
|
|
4706
|
-
GraphEvents.ON_TOOL_EXECUTE,
|
|
4707
|
-
expect.anything(),
|
|
4708
|
-
expect.anything()
|
|
4709
|
-
);
|
|
4710
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4711
|
-
});
|
|
4712
|
-
|
|
4713
|
-
it('prestarts official OpenAI sequential streamed tool calls when a later index begins', async () => {
|
|
4714
|
-
const graph = createGraph({
|
|
4715
|
-
getAgentContext: jest.fn(
|
|
4716
|
-
(): Partial<AgentContext> => ({
|
|
4717
|
-
provider: Providers.OPENAI,
|
|
4718
|
-
reasoningKey: 'reasoning_content',
|
|
4719
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'stock' }],
|
|
4720
|
-
graphTools: [],
|
|
4721
|
-
agentId: 'agent_1',
|
|
4722
|
-
})
|
|
4723
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4724
|
-
});
|
|
4725
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4726
|
-
jest
|
|
4727
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4728
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4729
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4730
|
-
return;
|
|
4731
|
-
}
|
|
4732
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4733
|
-
toolExecuteCalls.push(batch);
|
|
4734
|
-
batch.resolve(
|
|
4735
|
-
batch.toolCalls.map((call) => ({
|
|
4736
|
-
toolCallId: call.id,
|
|
4737
|
-
status: 'success',
|
|
4738
|
-
content: `ok ${call.name}`,
|
|
4739
|
-
}))
|
|
4740
|
-
);
|
|
4741
|
-
});
|
|
4742
|
-
|
|
4743
|
-
const handler = new ChatModelStreamHandler();
|
|
4744
|
-
const metadata = { langgraph_node: 'agent' };
|
|
4745
|
-
|
|
4746
|
-
await handler.handle(
|
|
4747
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4748
|
-
{
|
|
4749
|
-
chunk: {
|
|
4750
|
-
content: '',
|
|
4751
|
-
tool_call_chunks: [
|
|
4752
|
-
{
|
|
4753
|
-
id: 'call_weather',
|
|
4754
|
-
name: 'weather',
|
|
4755
|
-
args: '{"city":"NYC"}',
|
|
4756
|
-
index: 0,
|
|
4757
|
-
},
|
|
4758
|
-
],
|
|
4759
|
-
response_metadata: openAIChatSequentialToolCallMetadata,
|
|
4760
|
-
} as unknown as t.StreamChunk,
|
|
4761
|
-
},
|
|
4762
|
-
metadata,
|
|
4763
|
-
graph
|
|
4764
|
-
);
|
|
4765
|
-
|
|
4766
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4767
|
-
|
|
4768
|
-
await handler.handle(
|
|
4769
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4770
|
-
{
|
|
4771
|
-
chunk: {
|
|
4772
|
-
content: '',
|
|
4773
|
-
tool_call_chunks: [
|
|
4774
|
-
{
|
|
4775
|
-
id: 'call_stock',
|
|
4776
|
-
name: 'stock',
|
|
4777
|
-
index: 1,
|
|
4778
|
-
},
|
|
4779
|
-
],
|
|
4780
|
-
response_metadata: openAIChatSequentialToolCallMetadata,
|
|
4781
|
-
} as unknown as t.StreamChunk,
|
|
4782
|
-
},
|
|
4783
|
-
metadata,
|
|
4784
|
-
graph
|
|
4785
|
-
);
|
|
4786
|
-
|
|
4787
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4788
|
-
expect(toolExecuteCalls[0].toolCalls[0]).toMatchObject({
|
|
4789
|
-
id: 'call_weather',
|
|
4790
|
-
name: 'weather',
|
|
4791
|
-
args: { city: 'NYC' },
|
|
4792
|
-
stepId: expect.stringMatching(/^step_/),
|
|
4793
|
-
turn: 0,
|
|
4794
|
-
});
|
|
4795
|
-
|
|
4796
|
-
await handler.handle(
|
|
4797
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4798
|
-
{
|
|
4799
|
-
chunk: {
|
|
4800
|
-
content: '',
|
|
4801
|
-
tool_call_chunks: [
|
|
4802
|
-
{
|
|
4803
|
-
args: '{"ticker":"CH"}',
|
|
4804
|
-
index: 1,
|
|
4805
|
-
},
|
|
4806
|
-
],
|
|
4807
|
-
response_metadata: openAIChatSequentialToolCallMetadata,
|
|
4808
|
-
} as unknown as t.StreamChunk,
|
|
4809
|
-
},
|
|
4810
|
-
metadata,
|
|
4811
|
-
graph
|
|
4812
|
-
);
|
|
4813
|
-
|
|
4814
|
-
expect(toolExecuteCalls).toHaveLength(1);
|
|
4815
|
-
|
|
4816
|
-
// The final cumulative tool_calls chunk dispatches only the unstarted call.
|
|
4817
|
-
await handler.handle(
|
|
4818
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4819
|
-
{
|
|
4820
|
-
chunk: {
|
|
4821
|
-
content: '',
|
|
4822
|
-
tool_calls: [
|
|
4823
|
-
{
|
|
4824
|
-
id: 'call_weather',
|
|
4825
|
-
name: 'weather',
|
|
4826
|
-
args: { city: 'NYC' },
|
|
4827
|
-
},
|
|
4828
|
-
{
|
|
4829
|
-
id: 'call_stock',
|
|
4830
|
-
name: 'stock',
|
|
4831
|
-
args: { ticker: 'CH' },
|
|
4832
|
-
},
|
|
4833
|
-
],
|
|
4834
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
4835
|
-
} as unknown as t.StreamChunk,
|
|
4836
|
-
},
|
|
4837
|
-
metadata,
|
|
4838
|
-
graph
|
|
4839
|
-
);
|
|
4840
|
-
|
|
4841
|
-
expect(toolExecuteCalls).toHaveLength(2);
|
|
4842
|
-
expect(toolExecuteCalls[1].toolCalls).toHaveLength(1);
|
|
4843
|
-
expect(toolExecuteCalls[1].toolCalls[0]).toMatchObject({
|
|
4844
|
-
id: 'call_stock',
|
|
4845
|
-
name: 'stock',
|
|
4846
|
-
args: { ticker: 'CH' },
|
|
4847
|
-
turn: 0,
|
|
4848
|
-
});
|
|
4849
|
-
});
|
|
4850
|
-
|
|
4851
|
-
it('does not prestart on-arrival sealed calls when direct tools may stream', async () => {
|
|
4852
|
-
const graph = createGraph({
|
|
4853
|
-
getAgentContext: jest.fn(
|
|
4854
|
-
(): Partial<AgentContext> => ({
|
|
4855
|
-
provider: Providers.GOOGLE,
|
|
4856
|
-
reasoningKey: 'reasoning',
|
|
4857
|
-
toolDefinitions: [{ name: 'weather' }],
|
|
4858
|
-
graphTools: [{ name: 'lookup' }],
|
|
4859
|
-
agentId: 'agent_1',
|
|
4860
|
-
})
|
|
4861
|
-
) as unknown as StandardGraph['getAgentContext'],
|
|
4862
|
-
});
|
|
4863
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4864
|
-
jest
|
|
4865
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4866
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4867
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4868
|
-
return;
|
|
4869
|
-
}
|
|
4870
|
-
toolExecuteCalls.push(data as t.ToolExecuteBatchRequest);
|
|
4871
|
-
});
|
|
4872
|
-
|
|
4873
|
-
await new ChatModelStreamHandler().handle(
|
|
4874
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4875
|
-
{
|
|
4876
|
-
chunk: {
|
|
4877
|
-
content: '',
|
|
4878
|
-
tool_calls: [
|
|
4879
|
-
{
|
|
4880
|
-
id: 'call_weather',
|
|
4881
|
-
name: 'weather',
|
|
4882
|
-
args: { city: 'NYC' },
|
|
4883
|
-
},
|
|
4884
|
-
],
|
|
4885
|
-
tool_call_chunks: [
|
|
4886
|
-
{
|
|
4887
|
-
id: 'call_weather',
|
|
4888
|
-
name: 'weather',
|
|
4889
|
-
args: '{"city":"NYC"}',
|
|
4890
|
-
},
|
|
4891
|
-
],
|
|
4892
|
-
response_metadata: googleOnArrivalToolCallMetadata,
|
|
4893
|
-
} as unknown as t.StreamChunk,
|
|
4894
|
-
},
|
|
4895
|
-
{ langgraph_node: 'agent' },
|
|
4896
|
-
graph
|
|
4897
|
-
);
|
|
4898
|
-
|
|
4899
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4900
|
-
expect(graph.eagerEventToolExecutions.size).toBe(0);
|
|
4901
|
-
});
|
|
4902
|
-
|
|
4903
|
-
it('does not prestart tools listed in excludeToolNames', async () => {
|
|
4904
|
-
const graph = createGraph({
|
|
4905
|
-
eagerEventToolExecution: {
|
|
4906
|
-
enabled: true,
|
|
4907
|
-
excludeToolNames: ['create_file'],
|
|
4908
|
-
},
|
|
4909
|
-
});
|
|
4910
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4911
|
-
jest
|
|
4912
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4913
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4914
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4915
|
-
return;
|
|
4916
|
-
}
|
|
4917
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4918
|
-
toolExecuteCalls.push(batch);
|
|
4919
|
-
batch.resolve([
|
|
4920
|
-
{ toolCallId: 'call_file', status: 'success', content: 'ok' },
|
|
4921
|
-
]);
|
|
4922
|
-
});
|
|
4923
|
-
|
|
4924
|
-
await new ChatModelStreamHandler().handle(
|
|
4925
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4926
|
-
{
|
|
4927
|
-
chunk: {
|
|
4928
|
-
content: '',
|
|
4929
|
-
tool_calls: [
|
|
4930
|
-
{
|
|
4931
|
-
id: 'call_file',
|
|
4932
|
-
name: 'create_file',
|
|
4933
|
-
args: { path: '/mnt/data/x.py', content: 'print(1)' },
|
|
4934
|
-
},
|
|
4935
|
-
],
|
|
4936
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
4937
|
-
} as unknown as t.StreamChunk,
|
|
4938
|
-
},
|
|
4939
|
-
{ langgraph_node: 'agent' },
|
|
4940
|
-
graph
|
|
4941
|
-
);
|
|
4942
|
-
|
|
4943
|
-
// Excluded: no eager execution started; the call falls through to ToolNode.
|
|
4944
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4945
|
-
expect(graph.eagerEventToolExecutions.has('call_file')).toBe(false);
|
|
4946
|
-
});
|
|
4947
|
-
|
|
4948
|
-
it('does not prestart codeSessionToolNames tools even without excludeToolNames', async () => {
|
|
4949
|
-
// A declared session-writing host tool is side-effecting, so it must not be
|
|
4950
|
-
// eagerly prestarted even when the host didn't also list it in excludeToolNames.
|
|
4951
|
-
const graph = createGraph({
|
|
4952
|
-
eagerEventToolExecution: { enabled: true },
|
|
4953
|
-
codeSessionToolNames: ['create_file', 'edit_file'],
|
|
4954
|
-
});
|
|
4955
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
4956
|
-
jest
|
|
4957
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
4958
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
4959
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
4960
|
-
return;
|
|
4961
|
-
}
|
|
4962
|
-
const batch = data as t.ToolExecuteBatchRequest;
|
|
4963
|
-
toolExecuteCalls.push(batch);
|
|
4964
|
-
batch.resolve([
|
|
4965
|
-
{ toolCallId: 'call_cf2', status: 'success', content: 'ok' },
|
|
4966
|
-
]);
|
|
4967
|
-
});
|
|
4968
|
-
|
|
4969
|
-
await new ChatModelStreamHandler().handle(
|
|
4970
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
4971
|
-
{
|
|
4972
|
-
chunk: {
|
|
4973
|
-
content: '',
|
|
4974
|
-
tool_calls: [
|
|
4975
|
-
{
|
|
4976
|
-
id: 'call_cf2',
|
|
4977
|
-
name: 'create_file',
|
|
4978
|
-
args: { path: '/mnt/data/y.py', content: 'print(2)' },
|
|
4979
|
-
},
|
|
4980
|
-
],
|
|
4981
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
4982
|
-
} as unknown as t.StreamChunk,
|
|
4983
|
-
},
|
|
4984
|
-
{ langgraph_node: 'agent' },
|
|
4985
|
-
graph
|
|
4986
|
-
);
|
|
4987
|
-
|
|
4988
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
4989
|
-
expect(graph.eagerEventToolExecutions.has('call_cf2')).toBe(false);
|
|
4990
|
-
});
|
|
4991
|
-
|
|
4992
|
-
it('keeps the direct-tool batch guard when an excluded tool is also direct', async () => {
|
|
4993
|
-
// edit_file is both a direct graph tool AND excluded from eager. A mixed
|
|
4994
|
-
// batch with a direct tool must suppress eager for the whole batch, so the
|
|
4995
|
-
// sibling event tool must NOT prestart — excluding edit_file must not hide
|
|
4996
|
-
// it from the batch-level direct-tool guard.
|
|
4997
|
-
const graph = createGraph({
|
|
4998
|
-
eagerEventToolExecution: {
|
|
4999
|
-
enabled: true,
|
|
5000
|
-
excludeToolNames: ['edit_file'],
|
|
5001
|
-
},
|
|
5002
|
-
getAgentContext: jest.fn(() => ({
|
|
5003
|
-
provider: Providers.ANTHROPIC,
|
|
5004
|
-
reasoningKey: 'reasoning',
|
|
5005
|
-
toolDefinitions: [{ name: 'weather' }, { name: 'edit_file' }],
|
|
5006
|
-
graphTools: [{ name: 'edit_file' }],
|
|
5007
|
-
agentId: 'agent_1',
|
|
5008
|
-
})) as unknown as StandardGraph['getAgentContext'],
|
|
5009
|
-
});
|
|
5010
|
-
const toolExecuteCalls: t.ToolExecuteBatchRequest[] = [];
|
|
5011
|
-
jest
|
|
5012
|
-
.spyOn(events, 'safeDispatchCustomEvent')
|
|
5013
|
-
.mockImplementation(async (event, data): Promise<void> => {
|
|
5014
|
-
if (event !== GraphEvents.ON_TOOL_EXECUTE) {
|
|
5015
|
-
return;
|
|
5016
|
-
}
|
|
5017
|
-
toolExecuteCalls.push(data as t.ToolExecuteBatchRequest);
|
|
5018
|
-
(data as t.ToolExecuteBatchRequest).resolve([]);
|
|
5019
|
-
});
|
|
5020
|
-
|
|
5021
|
-
await new ChatModelStreamHandler().handle(
|
|
5022
|
-
GraphEvents.CHAT_MODEL_STREAM,
|
|
5023
|
-
{
|
|
5024
|
-
chunk: {
|
|
5025
|
-
content: '',
|
|
5026
|
-
tool_calls: [
|
|
5027
|
-
{ id: 'call_weather', name: 'weather', args: { city: 'NYC' } },
|
|
5028
|
-
{
|
|
5029
|
-
id: 'call_edit',
|
|
5030
|
-
name: 'edit_file',
|
|
5031
|
-
args: { path: '/mnt/data/x.py' },
|
|
5032
|
-
},
|
|
5033
|
-
],
|
|
5034
|
-
response_metadata: finalToolCallResponseMetadata,
|
|
5035
|
-
} as unknown as t.StreamChunk,
|
|
5036
|
-
},
|
|
5037
|
-
{ langgraph_node: 'agent' },
|
|
5038
|
-
graph
|
|
5039
|
-
);
|
|
5040
|
-
|
|
5041
|
-
// Direct tool in batch suppresses eager for the whole batch.
|
|
5042
|
-
expect(toolExecuteCalls).toHaveLength(0);
|
|
5043
|
-
expect(graph.eagerEventToolExecutions.has('call_weather')).toBe(false);
|
|
5044
|
-
expect(graph.eagerEventToolExecutions.has('call_edit')).toBe(false);
|
|
5045
|
-
});
|
|
5046
|
-
});
|