@librechat/agents 4.2.11 → 4.2.13
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/graphs/Graph.cjs +15 -3
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +63 -35
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/langfuseConfig.cjs +6 -0
- package/dist/cjs/langfuseConfig.cjs.map +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
- package/dist/cjs/langfuseTraceShaping.cjs +18 -8
- package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +4 -3
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/container.cjs +37 -0
- package/dist/cjs/llm/anthropic/utils/container.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +25 -23
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +15 -21
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/stream_events.cjs +16 -14
- package/dist/cjs/llm/anthropic/utils/stream_events.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +1 -1
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/toolCache.cjs +1 -1
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +19 -13
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +59 -13
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/llm/init.cjs +12 -2
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +15 -2
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/responseToolArguments.cjs +62 -0
- package/dist/cjs/llm/openai/responseToolArguments.cjs.map +1 -0
- package/dist/cjs/llm/openrouter/index.cjs +11 -2
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
- package/dist/cjs/llm/prepareProviderRequest.cjs +35 -7
- package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
- package/dist/cjs/main.cjs +9 -3
- package/dist/cjs/messages/alternation.cjs +1 -1
- package/dist/cjs/messages/anthropicServerTools.cjs +20 -0
- package/dist/cjs/messages/anthropicServerTools.cjs.map +1 -0
- package/dist/cjs/messages/budget.cjs +1 -1
- package/dist/cjs/messages/cache.cjs +14 -1
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +4 -3
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +3 -3
- package/dist/cjs/messages/index.cjs +1 -1
- package/dist/cjs/messages/prune.cjs +1 -1
- package/dist/cjs/messages/recency.cjs +1 -1
- package/dist/cjs/messages/toolResultTypes.cjs +21 -45
- package/dist/cjs/messages/toolResultTypes.cjs.map +1 -1
- package/dist/cjs/protection/providerText.cjs +4 -0
- package/dist/cjs/protection/providerText.cjs.map +1 -1
- package/dist/cjs/protection/toolResult.cjs +119 -0
- package/dist/cjs/protection/toolResult.cjs.map +1 -0
- package/dist/cjs/run.cjs +31 -13
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/runtime/admission.cjs +1 -1
- package/dist/cjs/runtime/client.cjs +3 -0
- package/dist/cjs/runtime/client.cjs.map +1 -1
- package/dist/cjs/stream.cjs +2 -1
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +9 -4
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +1 -1
- package/dist/cjs/tools/ToolNode.cjs +47 -14
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/local/CompileCheckTool.cjs +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +1 -1
- package/dist/cjs/tools/search/content.cjs +163 -42
- package/dist/cjs/tools/search/content.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +8 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +2 -2
- package/dist/cjs/utils/events.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +7 -2
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +15 -3
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +63 -35
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/langfuseConfig.mjs +6 -1
- package/dist/esm/langfuseConfig.mjs.map +1 -1
- package/dist/esm/langfuseToolOutputTracing.mjs +2 -2
- package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
- package/dist/esm/langfuseTraceShaping.mjs +18 -9
- package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +4 -3
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/container.mjs +37 -0
- package/dist/esm/llm/anthropic/utils/container.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +25 -23
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +15 -21
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/stream_events.mjs +16 -14
- package/dist/esm/llm/anthropic/utils/stream_events.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +2 -2
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/bedrock/toolCache.mjs +1 -1
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs +19 -14
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +59 -13
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/llm/init.mjs +12 -2
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +16 -3
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/responseToolArguments.mjs +62 -0
- package/dist/esm/llm/openai/responseToolArguments.mjs.map +1 -0
- package/dist/esm/llm/openrouter/index.mjs +11 -2
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
- package/dist/esm/llm/prepareProviderRequest.mjs +35 -7
- package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
- package/dist/esm/main.mjs +5 -4
- package/dist/esm/messages/alternation.mjs +1 -1
- package/dist/esm/messages/anthropicServerTools.mjs +18 -0
- package/dist/esm/messages/anthropicServerTools.mjs.map +1 -0
- package/dist/esm/messages/budget.mjs +1 -1
- package/dist/esm/messages/cache.mjs +12 -2
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +4 -3
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +3 -3
- package/dist/esm/messages/index.mjs +1 -1
- package/dist/esm/messages/prune.mjs +1 -1
- package/dist/esm/messages/recency.mjs +1 -1
- package/dist/esm/messages/toolResultTypes.mjs +21 -45
- package/dist/esm/messages/toolResultTypes.mjs.map +1 -1
- package/dist/esm/protection/providerText.mjs +4 -1
- package/dist/esm/protection/providerText.mjs.map +1 -1
- package/dist/esm/protection/toolResult.mjs +114 -0
- package/dist/esm/protection/toolResult.mjs.map +1 -0
- package/dist/esm/run.mjs +32 -14
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/runtime/admission.mjs +1 -1
- package/dist/esm/runtime/client.mjs +3 -0
- package/dist/esm/runtime/client.mjs.map +1 -1
- package/dist/esm/stream.mjs +2 -1
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +9 -4
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +1 -1
- package/dist/esm/tools/ToolNode.mjs +47 -14
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/local/CompileCheckTool.mjs +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +1 -1
- package/dist/esm/tools/search/content.mjs +163 -42
- package/dist/esm/tools/search/content.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +8 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +2 -2
- package/dist/esm/utils/events.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +7 -2
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/common/index.d.ts +2 -2
- package/dist/types/decisions/breaker.d.ts +2 -2
- package/dist/types/decisions/core.d.ts +13 -13
- package/dist/types/decisions/dialect.d.ts +1 -1
- package/dist/types/decisions/http.d.ts +2 -2
- package/dist/types/decisions/index.d.ts +5 -5
- package/dist/types/decisions/node.d.ts +2 -2
- package/dist/types/decisions/presets.d.ts +2 -2
- package/dist/types/decisions/probe.d.ts +1 -1
- package/dist/types/decisions/questions.d.ts +1 -1
- package/dist/types/decisions/structuredChat.d.ts +2 -2
- package/dist/types/decisions/transport.d.ts +1 -1
- package/dist/types/eventActor/EventActorExecutor.d.ts +1 -1
- package/dist/types/eventActor/index.d.ts +2 -2
- package/dist/types/graphs/Graph.d.ts +6 -2
- package/dist/types/graphs/MultiAgentGraph.d.ts +1 -1
- package/dist/types/graphs/applyGraphRuntimeConfig.d.ts +1 -1
- package/dist/types/graphs/graphFactory.d.ts +1 -1
- package/dist/types/graphs/index.d.ts +5 -5
- package/dist/types/harness/fileBindings.d.ts +2 -2
- package/dist/types/harness/fileService.d.ts +2 -2
- package/dist/types/harness/fileStream.d.ts +1 -1
- package/dist/types/harness/index.d.ts +15 -15
- package/dist/types/harness/model.d.ts +1 -1
- package/dist/types/harness/modelService.d.ts +2 -2
- package/dist/types/harness/modelStreamCallbacks.d.ts +1 -1
- package/dist/types/harness/providerText.d.ts +1 -1
- package/dist/types/hitl/index.d.ts +3 -3
- package/dist/types/hooks/HookRegistry.d.ts +1 -1
- package/dist/types/hooks/createToolPolicyHook.d.ts +1 -1
- package/dist/types/hooks/createWorkspacePolicyHook.d.ts +1 -1
- package/dist/types/hooks/executeHooks.d.ts +2 -2
- package/dist/types/hooks/index.d.ts +11 -11
- package/dist/types/index.d.ts +62 -60
- package/dist/types/langchain/index.d.ts +8 -8
- package/dist/types/langfuse.d.ts +7 -4
- package/dist/types/langfuseConfig.d.ts +5 -0
- package/dist/types/langfuseTraceShaping.d.ts +5 -0
- package/dist/types/llm/anthropic/utils/container.d.ts +4 -0
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +1 -1
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -1
- package/dist/types/llm/anthropic/utils/stream_events.d.ts +1 -1
- package/dist/types/llm/bedrock/index.d.ts +2 -3
- package/dist/types/llm/bedrock/utils/harmony.d.ts +1 -1
- package/dist/types/llm/bedrock/utils/index.d.ts +2 -2
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +3 -12
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +1 -1
- package/dist/types/llm/google/index.d.ts +2 -2
- package/dist/types/llm/google/utils/common.d.ts +1 -1
- package/dist/types/llm/google/utils/tools.d.ts +1 -1
- package/dist/types/llm/openai/index.d.ts +2 -1
- package/dist/types/llm/openai/responseToolArguments.d.ts +7 -0
- package/dist/types/llm/openrouter/index.d.ts +1 -0
- package/dist/types/llm/prepareProviderRequest.d.ts +4 -1
- package/dist/types/llm/providerRegistry.d.ts +1 -1
- package/dist/types/llm/providers.d.ts +1 -1
- package/dist/types/messages/anthropicServerTools.d.ts +13 -0
- package/dist/types/messages/cache.d.ts +13 -0
- package/dist/types/messages/contextPruning.d.ts +1 -1
- package/dist/types/messages/core.d.ts +4 -4
- package/dist/types/messages/format.d.ts +1 -1
- package/dist/types/messages/index.d.ts +21 -21
- package/dist/types/messages/prune.d.ts +1 -1
- package/dist/types/openai/index.d.ts +2 -2
- package/dist/types/openai/toolProjection.d.ts +1 -1
- package/dist/types/prompts/index.d.ts +2 -2
- package/dist/types/protection/providerText.d.ts +3 -1
- package/dist/types/protection/toolResult.d.ts +69 -0
- package/dist/types/provider-registration.d.ts +1 -1
- package/dist/types/rerank/bm25.d.ts +1 -1
- package/dist/types/rerank/core.d.ts +17 -17
- package/dist/types/rerank/crossEncoder.d.ts +3 -3
- package/dist/types/rerank/fallback.d.ts +1 -1
- package/dist/types/rerank/index.d.ts +2 -2
- package/dist/types/rerank/outcome.d.ts +2 -2
- package/dist/types/rerank/pin.d.ts +2 -2
- package/dist/types/rerank/search.d.ts +1 -1
- package/dist/types/rerank/systemone.d.ts +4 -4
- package/dist/types/rerank/types.d.ts +1 -1
- package/dist/types/run.d.ts +2 -0
- package/dist/types/runtime/admission.d.ts +1 -1
- package/dist/types/runtime/checkpoint.d.ts +1 -1
- package/dist/types/runtime/client.d.ts +2 -2
- package/dist/types/runtime/files.d.ts +4 -4
- package/dist/types/runtime/hostTools.d.ts +2 -2
- package/dist/types/runtime/index.d.ts +12 -12
- package/dist/types/runtime/model.d.ts +5 -5
- package/dist/types/runtime/policy.d.ts +3 -3
- package/dist/types/runtime/recovery/client.d.ts +3 -3
- package/dist/types/runtime/recovery/log.d.ts +2 -2
- package/dist/types/runtime/recovery/redis.d.ts +1 -1
- package/dist/types/runtime/recovery/server.d.ts +3 -3
- package/dist/types/runtime/resume.d.ts +1 -1
- package/dist/types/runtime/server.d.ts +3 -3
- package/dist/types/runtime/snapshot.d.ts +1 -1
- package/dist/types/runtime/types.d.ts +2 -2
- package/dist/types/session/AgentSession.d.ts +2 -2
- package/dist/types/session/JsonlSessionStore.d.ts +1 -1
- package/dist/types/session/deriveMessages.d.ts +1 -1
- package/dist/types/session/handlers.d.ts +1 -1
- package/dist/types/session/index.d.ts +7 -7
- package/dist/types/session/messageSerialization.d.ts +1 -1
- package/dist/types/session/sessionProjection.d.ts +3 -3
- package/dist/types/summarization/index.d.ts +2 -2
- package/dist/types/summarization/node.d.ts +2 -0
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/GitHubCompare.d.ts +6 -6
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/ReadFile.d.ts +6 -6
- package/dist/types/tools/SkillTool.d.ts +6 -6
- package/dist/types/tools/ToolNode.d.ts +5 -1
- package/dist/types/tools/cloudflare/index.d.ts +4 -4
- package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
- package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
- package/dist/types/tools/local/attachments.d.ts +1 -1
- package/dist/types/tools/local/index.d.ts +12 -12
- package/dist/types/tools/search/anthropic.d.ts +1 -1
- package/dist/types/tools/search/content.d.ts +38 -1
- package/dist/types/tools/search/crw-scraper.d.ts +1 -1
- package/dist/types/tools/search/crw-search.d.ts +1 -1
- package/dist/types/tools/search/firecrawl.d.ts +1 -1
- package/dist/types/tools/search/format.d.ts +1 -1
- package/dist/types/tools/search/highlights.d.ts +1 -1
- package/dist/types/tools/search/index.d.ts +3 -3
- package/dist/types/tools/search/keenable-scraper.d.ts +1 -1
- package/dist/types/tools/search/keenable-search.d.ts +1 -1
- package/dist/types/tools/search/metrics.d.ts +1 -1
- package/dist/types/tools/search/rerankers.d.ts +1 -1
- package/dist/types/tools/search/schema.d.ts +6 -6
- package/dist/types/tools/search/search.d.ts +1 -1
- package/dist/types/tools/search/serper-scraper.d.ts +1 -1
- package/dist/types/tools/search/tavily-scraper.d.ts +1 -1
- package/dist/types/tools/search/tavily-search.d.ts +1 -1
- package/dist/types/tools/search/tool.d.ts +3 -3
- package/dist/types/tools/search/types.d.ts +2 -2
- package/dist/types/tools/search/utils.d.ts +3 -3
- package/dist/types/tools/subagent/SubagentExecutionRegistry.d.ts +1 -1
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +9 -3
- package/dist/types/tools/subagent/diagnostics.d.ts +1 -1
- package/dist/types/tools/subagent/index.d.ts +7 -7
- package/dist/types/types/attachments.d.ts +25 -0
- package/dist/types/types/graph.d.ts +15 -0
- package/dist/types/types/index.d.ts +14 -13
- package/dist/types/types/llm.d.ts +2 -2
- package/dist/types/types/run.d.ts +7 -2
- package/dist/types/types/subagentTasks.d.ts +2 -2
- package/dist/types/types/tools.d.ts +6 -3
- package/dist/types/utils/events.d.ts +2 -1
- package/dist/types/utils/index.d.ts +10 -10
- package/dist/types/utils/tokens.d.ts +1 -0
- package/package.json +14 -12
- package/src/graphs/Graph.ts +32 -1
- package/src/index.ts +8 -0
- package/src/langfuse.ts +129 -41
- package/src/langfuseConfig.ts +14 -0
- package/src/langfuseToolOutputTracing.ts +5 -4
- package/src/langfuseTraceShaping.ts +49 -12
- package/src/llm/anthropic/index.ts +11 -3
- package/src/llm/anthropic/utils/container.ts +63 -0
- package/src/llm/anthropic/utils/message_inputs.ts +57 -51
- package/src/llm/anthropic/utils/message_outputs.ts +12 -39
- package/src/llm/anthropic/utils/stream_events.ts +17 -12
- package/src/llm/bedrock/index.ts +7 -5
- package/src/llm/bedrock/utils/message_inputs.ts +28 -35
- package/src/llm/google/utils/common.ts +88 -36
- package/src/llm/init.ts +35 -3
- package/src/llm/openai/index.ts +17 -2
- package/src/llm/openai/responseToolArguments.ts +104 -0
- package/src/llm/openrouter/index.ts +13 -0
- package/src/llm/prepareProviderRequest.ts +75 -13
- package/src/messages/anthropicServerTools.ts +24 -0
- package/src/messages/cache.ts +25 -5
- package/src/messages/core.ts +12 -11
- package/src/messages/toolResultTypes.ts +80 -88
- package/src/protection/providerText.ts +127 -40
- package/src/protection/toolResult.ts +272 -0
- package/src/run.ts +112 -47
- package/src/runtime/client.ts +7 -0
- package/src/stream.ts +7 -1
- package/src/summarization/node.ts +17 -4
- package/src/tools/ToolNode.ts +114 -18
- package/src/tools/search/content.ts +282 -62
- package/src/tools/subagent/SubagentExecutor.ts +12 -0
- package/src/types/attachments.ts +31 -0
- package/src/types/graph.ts +12 -0
- package/src/types/index.ts +1 -0
- package/src/types/run.ts +8 -1
- package/src/types/tools.ts +3 -0
- package/src/utils/events.ts +9 -2
- package/src/utils/tokens.ts +28 -2
|
@@ -956,6 +956,7 @@ async function runSummarizationAttempts(
|
|
|
956
956
|
onUsage: (attempt: t.SummarizationAttemptUsage) => void;
|
|
957
957
|
/** Carries the run's stream limits so the event cap covers summary streams. */
|
|
958
958
|
graph?: StreamLimitState & {
|
|
959
|
+
resolveAttachment?: t.AttachmentResolver;
|
|
959
960
|
getBreakerSignal?: () => AbortSignal;
|
|
960
961
|
getToolsForBinding?: (
|
|
961
962
|
provider: t.ProviderName,
|
|
@@ -1235,6 +1236,7 @@ async function runSummarizationAttempts(
|
|
|
1235
1236
|
model,
|
|
1236
1237
|
messages: [...history, new HumanMessage(instruction)],
|
|
1237
1238
|
provider,
|
|
1239
|
+
context: graph,
|
|
1238
1240
|
config,
|
|
1239
1241
|
});
|
|
1240
1242
|
fallbackIdentity = {
|
|
@@ -1463,6 +1465,8 @@ interface CreateSummarizeNodeParams {
|
|
|
1463
1465
|
contentIndexMap: Map<string, number>;
|
|
1464
1466
|
config?: RunnableConfig;
|
|
1465
1467
|
runId?: string;
|
|
1468
|
+
/** The graph's attachment resolver: a summary request carries the same references. */
|
|
1469
|
+
resolveAttachment?: t.AttachmentResolver;
|
|
1466
1470
|
isMultiAgent: boolean;
|
|
1467
1471
|
getSubagentExecutionContext?: () => t.SubagentExecutionContext | undefined;
|
|
1468
1472
|
hookRegistry?: HookRegistry;
|
|
@@ -2460,7 +2464,10 @@ async function summarizeWithCacheHit({
|
|
|
2460
2464
|
config?: RunnableConfig;
|
|
2461
2465
|
stepId?: string;
|
|
2462
2466
|
provider: t.ProviderName;
|
|
2463
|
-
graph?: StreamLimitState & {
|
|
2467
|
+
graph?: StreamLimitState & {
|
|
2468
|
+
resolveAttachment?: t.AttachmentResolver;
|
|
2469
|
+
getBreakerSignal?: () => AbortSignal;
|
|
2470
|
+
};
|
|
2464
2471
|
deltas?: SummaryDeltaStream;
|
|
2465
2472
|
usePromptCache?: boolean;
|
|
2466
2473
|
promptCacheTtl?: PromptCacheTtl;
|
|
@@ -2540,11 +2547,17 @@ async function summarizeWithCacheHit({
|
|
|
2540
2547
|
/** Each pass is a new attempt: what an earlier one streamed is not its output. */
|
|
2541
2548
|
deltas?.abandon();
|
|
2542
2549
|
try {
|
|
2550
|
+
/** Prepared here because this attempt passes no invocation context, which is where the attachment resolver would otherwise come from. */
|
|
2551
|
+
const request = prepareProviderRequest({
|
|
2552
|
+
model,
|
|
2553
|
+
messages: [...cachedHistory, new HumanMessage(instruction)],
|
|
2554
|
+
provider,
|
|
2555
|
+
context: graph,
|
|
2556
|
+
config: signalConfig,
|
|
2557
|
+
});
|
|
2543
2558
|
result = await attemptInvoke(
|
|
2544
2559
|
{
|
|
2545
|
-
|
|
2546
|
-
messages: [...cachedHistory, new HumanMessage(instruction)],
|
|
2547
|
-
provider,
|
|
2560
|
+
request,
|
|
2548
2561
|
onModelComplete: (response) => {
|
|
2549
2562
|
const usage = readSummarizationUsage(response);
|
|
2550
2563
|
if (usage != null) onUsage(usage);
|
package/src/tools/ToolNode.ts
CHANGED
|
@@ -50,6 +50,7 @@ import type {
|
|
|
50
50
|
} from '@/hooks';
|
|
51
51
|
import type { SettledToolBatchResult } from '@/tools/toolBatchReplay';
|
|
52
52
|
import type { PreparedSubagents } from '@/tools/preparedSubagents';
|
|
53
|
+
import type { ToolResultGate } from '@/protection/toolResult';
|
|
53
54
|
import type { RunBreakerScope } from '@/llm/streamLimits';
|
|
54
55
|
import type * as t from '@/types';
|
|
55
56
|
import {
|
|
@@ -114,6 +115,12 @@ import {
|
|
|
114
115
|
isIntentLabelProperty,
|
|
115
116
|
outcomeFieldsFromResult,
|
|
116
117
|
} from '@/tools/intentArg';
|
|
118
|
+
import {
|
|
119
|
+
inspectToolExecuteResult,
|
|
120
|
+
createToolResultGate,
|
|
121
|
+
inspectToolOutput,
|
|
122
|
+
inspectToolText,
|
|
123
|
+
} from '@/protection/toolResult';
|
|
117
124
|
import {
|
|
118
125
|
snapshotResolvedArgs,
|
|
119
126
|
buildReferenceKey,
|
|
@@ -310,6 +317,20 @@ function withSubagentReplayBatch(
|
|
|
310
317
|
};
|
|
311
318
|
}
|
|
312
319
|
|
|
320
|
+
/**
|
|
321
|
+
* A stream circuit breaker or protection failure raised under a tool (for
|
|
322
|
+
* example by a subagent's run) is a safety abort, not a tool failure to
|
|
323
|
+
* report back to the model. It passes through like an interrupt so the whole
|
|
324
|
+
* run rejects.
|
|
325
|
+
*/
|
|
326
|
+
function isRunSafetyError(error: unknown): boolean {
|
|
327
|
+
return (
|
|
328
|
+
error instanceof StreamLimitExceededError ||
|
|
329
|
+
error instanceof PreparedSubagentError ||
|
|
330
|
+
error instanceof ProviderTextProtectionError
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
313
334
|
type SettledDirectToolResult = SettledToolBatchResult;
|
|
314
335
|
|
|
315
336
|
/** Positional identity is safe within the immutable full-proposal batch key. */
|
|
@@ -883,6 +904,7 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
883
904
|
private agentLangfuse?: t.LangfuseConfig;
|
|
884
905
|
toolCallStepIds?: Map<string, string>;
|
|
885
906
|
errorHandler?: t.ToolNodeConstructorParams['errorHandler'];
|
|
907
|
+
private toolResultGate?: ToolResultGate;
|
|
886
908
|
/**
|
|
887
909
|
* Fallback error-completion ownership for calls that reach `runTool`
|
|
888
910
|
* outside a batch context (direct `runTool` use in tests / embedders).
|
|
@@ -1053,6 +1075,7 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1053
1075
|
humanInTheLoop,
|
|
1054
1076
|
toolOutputReferences,
|
|
1055
1077
|
toolOutputRegistry,
|
|
1078
|
+
toolResultProtection,
|
|
1056
1079
|
toolExecution,
|
|
1057
1080
|
fileCheckpointer,
|
|
1058
1081
|
getBreakerSignal,
|
|
@@ -1314,6 +1337,10 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1314
1337
|
this.handleToolErrors = handleToolErrors ?? this.handleToolErrors;
|
|
1315
1338
|
this.loadRuntimeTools = loadRuntimeTools;
|
|
1316
1339
|
this.errorHandler = errorHandler;
|
|
1340
|
+
this.toolResultGate =
|
|
1341
|
+
toolResultProtection == null
|
|
1342
|
+
? undefined
|
|
1343
|
+
: createToolResultGate(toolResultProtection);
|
|
1317
1344
|
this.toolUsageCount = new Map<string, number>();
|
|
1318
1345
|
this.toolRegistry = resolveLocalToolRegistry({
|
|
1319
1346
|
toolRegistry,
|
|
@@ -1622,7 +1649,12 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1622
1649
|
this.toolRegistry?.values() ?? [],
|
|
1623
1650
|
(toolDef) => isToolDefinitionActive(toolDef, discoveredToolNames)
|
|
1624
1651
|
);
|
|
1652
|
+
const protectedToolDefs: t.LCTool[] = [];
|
|
1625
1653
|
for (const toolDef of executableCapabilities.codeExecutionTools) {
|
|
1654
|
+
if (this.protectedResultGate(toolDef.name) != null) {
|
|
1655
|
+
protectedToolDefs.push({ name: toolDef.name });
|
|
1656
|
+
continue;
|
|
1657
|
+
}
|
|
1626
1658
|
if (
|
|
1627
1659
|
this.eventDrivenMode &&
|
|
1628
1660
|
this.directToolNames?.has(toolDef.name) !== true &&
|
|
@@ -1643,7 +1675,8 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1643
1675
|
toolDefs,
|
|
1644
1676
|
disallowedToolDefs: activeCapabilities.directOnlyTools
|
|
1645
1677
|
.filter((toolDef) => !isProgrammaticControlTool(toolDef.name))
|
|
1646
|
-
.map((toolDef) => ({ name: toolDef.name }))
|
|
1678
|
+
.map((toolDef) => ({ name: toolDef.name }))
|
|
1679
|
+
.concat(protectedToolDefs),
|
|
1647
1680
|
};
|
|
1648
1681
|
}
|
|
1649
1682
|
|
|
@@ -1788,7 +1821,8 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1788
1821
|
this.humanInTheLoop?.enabled === true ||
|
|
1789
1822
|
this.hookRegistry?.hasResultAlteringHooks(runId) === true ||
|
|
1790
1823
|
this.eagerEventToolExecution?.excludeToolNames?.includes(call.name) ===
|
|
1791
|
-
true
|
|
1824
|
+
true ||
|
|
1825
|
+
this.protectedResultGate(call.name) != null
|
|
1792
1826
|
) {
|
|
1793
1827
|
return;
|
|
1794
1828
|
}
|
|
@@ -2111,10 +2145,20 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2111
2145
|
}
|
|
2112
2146
|
|
|
2113
2147
|
this.throwIfBreakerTripped(config);
|
|
2114
|
-
const
|
|
2148
|
+
const returned = await (this.preparedSubagents?.take(
|
|
2115
2149
|
this.executingAgentId ?? '',
|
|
2116
2150
|
{ ...call, args }
|
|
2117
2151
|
) ?? this.invokeWithRuntime(tool, invokeParams, call, config, runInput));
|
|
2152
|
+
const gate = this.protectedResultGate(call.name);
|
|
2153
|
+
const output =
|
|
2154
|
+
gate == null
|
|
2155
|
+
? returned
|
|
2156
|
+
: await inspectToolOutput(
|
|
2157
|
+
gate,
|
|
2158
|
+
{ name: call.name, id: call.id ?? '' },
|
|
2159
|
+
returned,
|
|
2160
|
+
config.signal
|
|
2161
|
+
);
|
|
2118
2162
|
if (isCommand(output)) {
|
|
2119
2163
|
return output;
|
|
2120
2164
|
}
|
|
@@ -2195,23 +2239,32 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2195
2239
|
}),
|
|
2196
2240
|
});
|
|
2197
2241
|
} catch (_e: unknown) {
|
|
2198
|
-
const
|
|
2242
|
+
const thrown = _e as Error;
|
|
2243
|
+
/** Control flow and safety aborts pass through; any other failure of a
|
|
2244
|
+
* protected tool is inspected before it can leave, rethrown or not. */
|
|
2245
|
+
const gate =
|
|
2246
|
+
isGraphInterrupt(thrown) || isRunSafetyError(thrown)
|
|
2247
|
+
? undefined
|
|
2248
|
+
: this.protectedResultGate(call.name);
|
|
2249
|
+
const e =
|
|
2250
|
+
gate == null
|
|
2251
|
+
? thrown
|
|
2252
|
+
: new Error(
|
|
2253
|
+
await inspectToolText(
|
|
2254
|
+
gate,
|
|
2255
|
+
{ name: call.name, id: call.id ?? '' },
|
|
2256
|
+
typeof thrown.message === 'string' ? thrown.message : '',
|
|
2257
|
+
'error',
|
|
2258
|
+
config.signal
|
|
2259
|
+
)
|
|
2260
|
+
);
|
|
2199
2261
|
if (!this.handleToolErrors) {
|
|
2200
2262
|
throw e;
|
|
2201
2263
|
}
|
|
2202
2264
|
if (isGraphInterrupt(e)) {
|
|
2203
2265
|
throw e;
|
|
2204
2266
|
}
|
|
2205
|
-
|
|
2206
|
-
* A stream circuit breaker tripped by a child run (subagent) is a
|
|
2207
|
-
* safety abort, not a tool failure to report back to the model. It
|
|
2208
|
-
* passes through like an interrupt so the whole run rejects.
|
|
2209
|
-
*/
|
|
2210
|
-
if (
|
|
2211
|
-
e instanceof StreamLimitExceededError ||
|
|
2212
|
-
e instanceof PreparedSubagentError ||
|
|
2213
|
-
e instanceof ProviderTextProtectionError
|
|
2214
|
-
) {
|
|
2267
|
+
if (isRunSafetyError(e)) {
|
|
2215
2268
|
throw e;
|
|
2216
2269
|
}
|
|
2217
2270
|
if (this.errorHandler) {
|
|
@@ -2293,6 +2346,35 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2293
2346
|
}
|
|
2294
2347
|
}
|
|
2295
2348
|
|
|
2349
|
+
/** The gate when host result protection selects `toolName`. */
|
|
2350
|
+
private protectedResultGate(toolName: string): ToolResultGate | undefined {
|
|
2351
|
+
return this.toolResultGate?.toolNames.has(toolName) === true
|
|
2352
|
+
? this.toolResultGate
|
|
2353
|
+
: undefined;
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
private inspectHostResults(
|
|
2357
|
+
results: t.ToolExecuteResult[],
|
|
2358
|
+
requests: ReadonlyMap<string, t.ToolCallRequest>,
|
|
2359
|
+
config: RunnableConfig
|
|
2360
|
+
): Promise<t.ToolExecuteResult[]> {
|
|
2361
|
+
return Promise.all(
|
|
2362
|
+
results.map((result) => {
|
|
2363
|
+
const request = requests.get(result.toolCallId);
|
|
2364
|
+
const gate =
|
|
2365
|
+
request == null ? undefined : this.protectedResultGate(request.name);
|
|
2366
|
+
return gate == null || request == null
|
|
2367
|
+
? result
|
|
2368
|
+
: inspectToolExecuteResult(
|
|
2369
|
+
gate,
|
|
2370
|
+
{ name: request.name, id: request.id },
|
|
2371
|
+
result,
|
|
2372
|
+
config.signal
|
|
2373
|
+
);
|
|
2374
|
+
})
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2296
2378
|
private unresolvedReferenceMessage(
|
|
2297
2379
|
call: ToolCall,
|
|
2298
2380
|
resolution: Pick<ResolveResult<ToolCall['args']>, 'unresolved'>,
|
|
@@ -4512,7 +4594,8 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
4512
4594
|
: undefined;
|
|
4513
4595
|
if (
|
|
4514
4596
|
request == null ||
|
|
4515
|
-
earlyCompletionDispatchedIds.has(result.toolCallId)
|
|
4597
|
+
earlyCompletionDispatchedIds.has(result.toolCallId) ||
|
|
4598
|
+
this.protectedResultGate(request.name) != null
|
|
4516
4599
|
) {
|
|
4517
4600
|
return;
|
|
4518
4601
|
}
|
|
@@ -4600,7 +4683,8 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
4600
4683
|
void safeDispatchCustomEvent(
|
|
4601
4684
|
GraphEvents.ON_TOOL_EXECUTE,
|
|
4602
4685
|
batchRequest,
|
|
4603
|
-
config
|
|
4686
|
+
config,
|
|
4687
|
+
toolCalls
|
|
4604
4688
|
)
|
|
4605
4689
|
.then(() => {
|
|
4606
4690
|
dispatchSettled = true;
|
|
@@ -4662,10 +4746,22 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
4662
4746
|
const flattenedEagerResults = eagerResults.flatMap(
|
|
4663
4747
|
(result) => result.results
|
|
4664
4748
|
);
|
|
4749
|
+
const hostResults = [...flattenedEagerResults, ...dispatchedResults];
|
|
4665
4750
|
const results = [
|
|
4666
4751
|
...plan.rejectedResults,
|
|
4667
|
-
...
|
|
4668
|
-
|
|
4752
|
+
...(this.toolResultGate == null
|
|
4753
|
+
? hostResults
|
|
4754
|
+
: await this.inspectHostResults(
|
|
4755
|
+
hostResults,
|
|
4756
|
+
requestMap,
|
|
4757
|
+
config
|
|
4758
|
+
).catch((error: unknown) => {
|
|
4759
|
+
this.retainCodeSessionInputsFromRequests(
|
|
4760
|
+
requestMap.values(),
|
|
4761
|
+
codeSessionBaselineByRequestId
|
|
4762
|
+
);
|
|
4763
|
+
throw error;
|
|
4764
|
+
})),
|
|
4669
4765
|
];
|
|
4670
4766
|
/* Hosts may lazily refresh codeSessionContext while dispatching. The
|
|
4671
4767
|
* pre-dispatch snapshot above remains the authority for detecting those
|
|
@@ -1,6 +1,280 @@
|
|
|
1
1
|
import * as cheerio from 'cheerio';
|
|
2
2
|
import type { References, MediaReference } from './types';
|
|
3
3
|
|
|
4
|
+
type ReferenceType = 'image' | 'video' | 'iframe' | 'link';
|
|
5
|
+
|
|
6
|
+
interface ReferenceTarget {
|
|
7
|
+
type: ReferenceType;
|
|
8
|
+
idx: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ReferenceEntry extends ReferenceTarget {
|
|
12
|
+
url: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Aho-Corasick automaton over `(` followed by every referenced URL, held in
|
|
17
|
+
* typed arrays: 18 bytes per node and no object per node. Nodes are numbered
|
|
18
|
+
* breadth first, so the children of `node` are the nodes from
|
|
19
|
+
* `firstChild[node]` up to `firstChild[node + 1]`, sorted by `code`.
|
|
20
|
+
*/
|
|
21
|
+
export interface ReferenceMatcher {
|
|
22
|
+
firstChild: Int32Array;
|
|
23
|
+
code: Uint16Array;
|
|
24
|
+
depth: Int32Array;
|
|
25
|
+
fail: Int32Array;
|
|
26
|
+
/** Nearest node on the fail chain, itself included, that ends a URL, or -1. */
|
|
27
|
+
output: Int32Array;
|
|
28
|
+
targets: Map<number, ReferenceTarget>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface ReferenceClose {
|
|
32
|
+
end: number;
|
|
33
|
+
title?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Largest automaton built for one page. Nodes are the distinct URL prefixes, at
|
|
38
|
+
* most the URL characters on the page: 2^21 of them measured 36 MiB and 0.15 s
|
|
39
|
+
* to build, about 100 times a long Wikipedia article. A page above the cap
|
|
40
|
+
* keeps its markdown as is.
|
|
41
|
+
*/
|
|
42
|
+
export const MAX_MATCHER_NODES = 1 << 21;
|
|
43
|
+
const OPEN_PAREN = 0x28;
|
|
44
|
+
const CLOSE_PAREN = 0x29;
|
|
45
|
+
const QUOTE = 0x22;
|
|
46
|
+
const WHITESPACE = /\s/;
|
|
47
|
+
|
|
48
|
+
function compareUrls(a: ReferenceEntry, b: ReferenceEntry): number {
|
|
49
|
+
if (a.url === b.url) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
return a.url < b.url ? -1 : 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function sharedPrefixLength(a: string, b: string): number {
|
|
56
|
+
const limit = Math.min(a.length, b.length);
|
|
57
|
+
let length = 0;
|
|
58
|
+
while (length < limit && a.charCodeAt(length) === b.charCodeAt(length)) {
|
|
59
|
+
length++;
|
|
60
|
+
}
|
|
61
|
+
return length;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Indexes every referenced URL once, or returns `undefined` when the automaton
|
|
66
|
+
* would exceed `MAX_MATCHER_NODES`. A URL listed under several types keeps the
|
|
67
|
+
* first, in image, video, iframe, link order, which is the order the replacement
|
|
68
|
+
* has always preferred. Sorting the URLs gives the exact node count before
|
|
69
|
+
* anything is allocated, and lets each node take its children from one
|
|
70
|
+
* contiguous run of URLs.
|
|
71
|
+
*/
|
|
72
|
+
export function buildMatcher(
|
|
73
|
+
groups: ReadonlyArray<[ReferenceType, Map<string, MediaReference>]>
|
|
74
|
+
): ReferenceMatcher | undefined {
|
|
75
|
+
const entries: ReferenceEntry[] = [];
|
|
76
|
+
for (const [type, references] of groups) {
|
|
77
|
+
let idx = 0;
|
|
78
|
+
for (const url of references.keys()) {
|
|
79
|
+
entries.push({ url, type, idx: ++idx });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
entries.sort(compareUrls);
|
|
83
|
+
let size = 2;
|
|
84
|
+
for (let i = 0; i < entries.length; i++) {
|
|
85
|
+
const { url } = entries[i];
|
|
86
|
+
size +=
|
|
87
|
+
url.length - (i === 0 ? 0 : sharedPrefixLength(entries[i - 1].url, url));
|
|
88
|
+
}
|
|
89
|
+
if (size > MAX_MATCHER_NODES) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const matcher: ReferenceMatcher = {
|
|
94
|
+
firstChild: new Int32Array(size + 1),
|
|
95
|
+
code: new Uint16Array(size),
|
|
96
|
+
depth: new Int32Array(size),
|
|
97
|
+
fail: new Int32Array(size),
|
|
98
|
+
output: new Int32Array(size).fill(-1),
|
|
99
|
+
targets: new Map<number, ReferenceTarget>(),
|
|
100
|
+
};
|
|
101
|
+
const { firstChild, code, depth, fail, output, targets } = matcher;
|
|
102
|
+
/** Nodes awaiting their children span two trie levels, each at most one node per URL. */
|
|
103
|
+
const pending = 2 * entries.length + 2;
|
|
104
|
+
const from = new Int32Array(pending);
|
|
105
|
+
const to = new Int32Array(pending);
|
|
106
|
+
firstChild[0] = 1;
|
|
107
|
+
code[1] = OPEN_PAREN;
|
|
108
|
+
depth[1] = 1;
|
|
109
|
+
to[1] = entries.length;
|
|
110
|
+
let next = 2;
|
|
111
|
+
for (let node = 1; node < next; node++) {
|
|
112
|
+
const consumed = depth[node] - 1;
|
|
113
|
+
const end = to[node % pending];
|
|
114
|
+
let i = from[node % pending];
|
|
115
|
+
const endsUrl = i < end && entries[i].url.length === consumed;
|
|
116
|
+
if (endsUrl) {
|
|
117
|
+
targets.set(node, entries[i]);
|
|
118
|
+
}
|
|
119
|
+
while (i < end && entries[i].url.length === consumed) {
|
|
120
|
+
i++;
|
|
121
|
+
}
|
|
122
|
+
output[node] = endsUrl ? node : output[fail[node]];
|
|
123
|
+
firstChild[node] = next;
|
|
124
|
+
while (i < end) {
|
|
125
|
+
const charCode = entries[i].url.charCodeAt(consumed);
|
|
126
|
+
code[next] = charCode;
|
|
127
|
+
depth[next] = consumed + 2;
|
|
128
|
+
fail[next] = transition(matcher, fail[node], charCode);
|
|
129
|
+
from[next % pending] = i;
|
|
130
|
+
do {
|
|
131
|
+
i++;
|
|
132
|
+
} while (i < end && entries[i].url.charCodeAt(consumed) === charCode);
|
|
133
|
+
to[next % pending] = i;
|
|
134
|
+
next++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
firstChild[next] = next;
|
|
138
|
+
return matcher;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function childOf(
|
|
142
|
+
matcher: ReferenceMatcher,
|
|
143
|
+
node: number,
|
|
144
|
+
charCode: number
|
|
145
|
+
): number {
|
|
146
|
+
const { firstChild, code } = matcher;
|
|
147
|
+
const end = firstChild[node + 1];
|
|
148
|
+
let low = firstChild[node];
|
|
149
|
+
let high = end;
|
|
150
|
+
while (low < high) {
|
|
151
|
+
const middle = (low + high) >>> 1;
|
|
152
|
+
if (code[middle] < charCode) {
|
|
153
|
+
low = middle + 1;
|
|
154
|
+
} else {
|
|
155
|
+
high = middle;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return low < end && code[low] === charCode ? low : -1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function transition(
|
|
162
|
+
matcher: ReferenceMatcher,
|
|
163
|
+
from: number,
|
|
164
|
+
charCode: number
|
|
165
|
+
): number {
|
|
166
|
+
let state = from;
|
|
167
|
+
let next = childOf(matcher, state, charCode);
|
|
168
|
+
while (next === -1 && state !== 0) {
|
|
169
|
+
state = matcher.fail[state];
|
|
170
|
+
next = childOf(matcher, state, charCode);
|
|
171
|
+
}
|
|
172
|
+
return next === -1 ? 0 : next;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Closes a reference whose URL ends just before `urlEnd` with `)` or
|
|
177
|
+
* ` "title")`. Whitespace runs and quote pairs are resolved once each and
|
|
178
|
+
* cached, so checking many URL ends never rescans the same text.
|
|
179
|
+
*/
|
|
180
|
+
function createCloser(
|
|
181
|
+
markdown: string
|
|
182
|
+
): (urlEnd: number) => ReferenceClose | undefined {
|
|
183
|
+
const closingQuote = new Map<number, number>();
|
|
184
|
+
let nonSpaceFrom: Int32Array | undefined;
|
|
185
|
+
const firstNonSpace = (index: number): number => {
|
|
186
|
+
if (nonSpaceFrom == null) {
|
|
187
|
+
nonSpaceFrom = new Int32Array(markdown.length + 1);
|
|
188
|
+
nonSpaceFrom[markdown.length] = markdown.length;
|
|
189
|
+
for (let i = markdown.length - 1; i >= 0; i--) {
|
|
190
|
+
nonSpaceFrom[i] = WHITESPACE.test(markdown[i])
|
|
191
|
+
? nonSpaceFrom[i + 1]
|
|
192
|
+
: i;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return nonSpaceFrom[index];
|
|
196
|
+
};
|
|
197
|
+
return (urlEnd) => {
|
|
198
|
+
const next = markdown.charCodeAt(urlEnd);
|
|
199
|
+
if (next === CLOSE_PAREN) {
|
|
200
|
+
return { end: urlEnd + 1 };
|
|
201
|
+
}
|
|
202
|
+
if (!WHITESPACE.test(markdown[urlEnd] ?? '')) {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
const open = firstNonSpace(urlEnd);
|
|
206
|
+
if (markdown.charCodeAt(open) !== QUOTE) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
let close = closingQuote.get(open);
|
|
210
|
+
if (close == null) {
|
|
211
|
+
close = markdown.indexOf('"', open + 1);
|
|
212
|
+
closingQuote.set(open, close);
|
|
213
|
+
}
|
|
214
|
+
if (close === -1 || markdown.charCodeAt(close + 1) !== CLOSE_PAREN) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
return { end: close + 2, title: markdown.slice(open + 1, close) };
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Rewrites every markdown reference to a known URL. One scan finds each
|
|
223
|
+
* `(url` occurrence that is closed as a reference; the longest at each opening
|
|
224
|
+
* parenthesis is applied left to right. The work is linear in the markdown
|
|
225
|
+
* plus the occurrences found, where one regex per URL over the whole page grew
|
|
226
|
+
* with links times page size and blocked the event loop for seconds.
|
|
227
|
+
* Occurrences exceed the markdown length only when URLs are suffixes of one
|
|
228
|
+
* another; n such URLs need about n * n characters of HTML, so the worst case
|
|
229
|
+
* stays near the input size to the power 1.5.
|
|
230
|
+
*/
|
|
231
|
+
function replaceReferences(
|
|
232
|
+
markdown: string,
|
|
233
|
+
matcher: ReferenceMatcher
|
|
234
|
+
): string {
|
|
235
|
+
if (matcher.targets.size === 0) {
|
|
236
|
+
return markdown;
|
|
237
|
+
}
|
|
238
|
+
const { depth, fail, output, targets } = matcher;
|
|
239
|
+
const closeAt = createCloser(markdown);
|
|
240
|
+
const longest = new Map<number, { node: number; close: ReferenceClose }>();
|
|
241
|
+
let state = 0;
|
|
242
|
+
for (let i = 0; i < markdown.length; i++) {
|
|
243
|
+
state = transition(matcher, state, markdown.charCodeAt(i));
|
|
244
|
+
let node = output[state];
|
|
245
|
+
if (node === -1) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const close = closeAt(i + 1);
|
|
249
|
+
if (close == null) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
for (; node !== -1; node = output[fail[node]]) {
|
|
253
|
+
const start = i - depth[node] + 1;
|
|
254
|
+
const current = longest.get(start);
|
|
255
|
+
if (current == null || depth[current.node] < depth[node]) {
|
|
256
|
+
longest.set(start, { node, close });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let result = '';
|
|
262
|
+
let copied = 0;
|
|
263
|
+
const ordered = Array.from(longest.entries()).sort((a, b) => a[0] - b[0]);
|
|
264
|
+
for (const [start, { node, close }] of ordered) {
|
|
265
|
+
const target = targets.get(node);
|
|
266
|
+
if (start < copied || target == null) {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const label = `${target.type}#${target.idx}`;
|
|
270
|
+
const text =
|
|
271
|
+
close.title == null ? `(${label})` : `(${label} "${close.title}")`;
|
|
272
|
+
result += markdown.slice(copied, start) + text;
|
|
273
|
+
copied = close.end;
|
|
274
|
+
}
|
|
275
|
+
return result + markdown.slice(copied);
|
|
276
|
+
}
|
|
277
|
+
|
|
4
278
|
export function processContent(
|
|
5
279
|
html: string,
|
|
6
280
|
markdown: string
|
|
@@ -67,68 +341,14 @@ export function processContent(
|
|
|
67
341
|
}
|
|
68
342
|
});
|
|
69
343
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
imageIndexMap.set(url, i + 1)
|
|
79
|
-
);
|
|
80
|
-
Array.from(videoMap.keys()).forEach((url, i) =>
|
|
81
|
-
videoIndexMap.set(url, i + 1)
|
|
82
|
-
);
|
|
83
|
-
Array.from(iframeMap.keys()).forEach((url, i) =>
|
|
84
|
-
iframeIndexMap.set(url, i + 1)
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
// Process the markdown
|
|
88
|
-
let result = markdown;
|
|
89
|
-
|
|
90
|
-
// Replace each URL one by one, starting with the longest URLs first to avoid partial matches
|
|
91
|
-
const allUrls = [
|
|
92
|
-
...Array.from(imageMap.keys()).map((url) => ({
|
|
93
|
-
url,
|
|
94
|
-
type: 'image',
|
|
95
|
-
idx: imageIndexMap.get(url),
|
|
96
|
-
})),
|
|
97
|
-
...Array.from(videoMap.keys()).map((url) => ({
|
|
98
|
-
url,
|
|
99
|
-
type: 'video',
|
|
100
|
-
idx: videoIndexMap.get(url),
|
|
101
|
-
})),
|
|
102
|
-
...Array.from(iframeMap.keys()).map((url) => ({
|
|
103
|
-
url,
|
|
104
|
-
type: 'iframe',
|
|
105
|
-
idx: iframeIndexMap.get(url),
|
|
106
|
-
})),
|
|
107
|
-
...Array.from(linkMap.keys()).map((url) => ({
|
|
108
|
-
url,
|
|
109
|
-
type: 'link',
|
|
110
|
-
idx: linkIndexMap.get(url),
|
|
111
|
-
})),
|
|
112
|
-
].sort((a, b) => b.url.length - a.url.length);
|
|
113
|
-
|
|
114
|
-
// Create a function to escape special characters in URLs for regex
|
|
115
|
-
function escapeRegex(string: string): string {
|
|
116
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Replace each URL in the markdown
|
|
120
|
-
for (const { url, type, idx } of allUrls) {
|
|
121
|
-
// Create a regex that captures URLs in markdown links
|
|
122
|
-
const regex = new RegExp(`\\(${escapeRegex(url)}(?:\\s+"[^"]*")?\\)`, 'g');
|
|
123
|
-
|
|
124
|
-
result = result.replace(regex, (match) => {
|
|
125
|
-
// Keep any title attribute that might exist
|
|
126
|
-
const titleMatch = match.match(/\s+"([^"]*)"/);
|
|
127
|
-
const titlePart = titleMatch ? ` "${titleMatch[1]}"` : '';
|
|
128
|
-
|
|
129
|
-
return `(${type}#${idx}${titlePart})`;
|
|
130
|
-
});
|
|
131
|
-
}
|
|
344
|
+
const matcher = buildMatcher([
|
|
345
|
+
['image', imageMap],
|
|
346
|
+
['video', videoMap],
|
|
347
|
+
['iframe', iframeMap],
|
|
348
|
+
['link', linkMap],
|
|
349
|
+
]);
|
|
350
|
+
const result =
|
|
351
|
+
matcher == null ? markdown : replaceReferences(markdown, matcher);
|
|
132
352
|
|
|
133
353
|
iframeMap.clear();
|
|
134
354
|
const links = Array.from(linkMap.values());
|
|
@@ -1174,6 +1174,10 @@ export type SubagentExecutorOptions = {
|
|
|
1174
1174
|
* revert to the defaults.
|
|
1175
1175
|
*/
|
|
1176
1176
|
providerTextProtection?: StandardGraphInput['providerTextProtection'];
|
|
1177
|
+
/** The parent's tool result protection, so a child inspects the same tools. */
|
|
1178
|
+
toolResultProtection?: StandardGraphInput['toolResultProtection'];
|
|
1179
|
+
/** The parent's attachment resolver, so a child resolves the references it inherits. */
|
|
1180
|
+
resolveAttachment?: StandardGraphInput['resolveAttachment'];
|
|
1177
1181
|
streamLimits?: StandardGraphInput['streamLimits'];
|
|
1178
1182
|
humanInTheLoop?: HumanInTheLoopConfig;
|
|
1179
1183
|
/** Shared durable saver used to recover outer tool lifecycle results before
|
|
@@ -1252,6 +1256,8 @@ export class SubagentExecutor {
|
|
|
1252
1256
|
private readonly langfuse?: StandardGraphInput['langfuse'];
|
|
1253
1257
|
private readonly tokenCounter?: TokenCounter;
|
|
1254
1258
|
private readonly providerTextProtection?: StandardGraphInput['providerTextProtection'];
|
|
1259
|
+
private readonly toolResultProtection?: StandardGraphInput['toolResultProtection'];
|
|
1260
|
+
private readonly resolveAttachment?: StandardGraphInput['resolveAttachment'];
|
|
1255
1261
|
private readonly streamLimits?: StandardGraphInput['streamLimits'];
|
|
1256
1262
|
private readonly humanInTheLoop?: HumanInTheLoopConfig;
|
|
1257
1263
|
private readonly checkpointer?: BaseCheckpointSaver;
|
|
@@ -1293,6 +1299,8 @@ export class SubagentExecutor {
|
|
|
1293
1299
|
this.tokenCounter = options.tokenCounter;
|
|
1294
1300
|
this.streamLimits = options.streamLimits;
|
|
1295
1301
|
this.providerTextProtection = options.providerTextProtection;
|
|
1302
|
+
this.toolResultProtection = options.toolResultProtection;
|
|
1303
|
+
this.resolveAttachment = options.resolveAttachment;
|
|
1296
1304
|
this.humanInTheLoop = options.humanInTheLoop;
|
|
1297
1305
|
this.checkpointer = isCheckpointSaver(options.checkpointer)
|
|
1298
1306
|
? options.checkpointer
|
|
@@ -1578,6 +1586,8 @@ export class SubagentExecutor {
|
|
|
1578
1586
|
onResolutionFailure: this.onResolutionFailure,
|
|
1579
1587
|
streamLimits: this.streamLimits,
|
|
1580
1588
|
providerTextProtection: this.providerTextProtection,
|
|
1589
|
+
toolResultProtection: this.toolResultProtection,
|
|
1590
|
+
resolveAttachment: this.resolveAttachment,
|
|
1581
1591
|
humanInTheLoop:
|
|
1582
1592
|
this.humanInTheLoop?.enabled === true ||
|
|
1583
1593
|
this.humanInTheLoop?.backgroundPausePolicy === 'deny'
|
|
@@ -3187,6 +3197,8 @@ export class SubagentExecutor {
|
|
|
3187
3197
|
tokenCounter: this.tokenCounter,
|
|
3188
3198
|
streamLimits: this.streamLimits,
|
|
3189
3199
|
providerTextProtection: this.providerTextProtection,
|
|
3200
|
+
toolResultProtection: this.toolResultProtection,
|
|
3201
|
+
resolveAttachment: this.resolveAttachment,
|
|
3190
3202
|
subagentScope: true,
|
|
3191
3203
|
subagentExecutionContext: childExecutionContext,
|
|
3192
3204
|
subagentContext: this.subagentContext,
|