@librechat/agents 4.2.12 → 4.2.14
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 +56 -6
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/harness/providerText.cjs +1 -0
- package/dist/cjs/harness/providerText.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/google/utils/signatures.cjs +22 -14
- package/dist/cjs/llm/google/utils/signatures.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/invoke.cjs +122 -13
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +9 -3
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/typedContent.cjs +94 -0
- package/dist/cjs/llm/openai/typedContent.cjs.map +1 -0
- package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
- package/dist/cjs/llm/prepareProviderRequest.cjs +81 -4
- package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
- package/dist/cjs/llm/providerTextBatch.cjs +128 -0
- package/dist/cjs/llm/providerTextBatch.cjs.map +1 -0
- package/dist/cjs/llm/providerTextBoundary.cjs +81 -12
- package/dist/cjs/llm/providerTextBoundary.cjs.map +1 -1
- package/dist/cjs/main.cjs +8 -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/core.cjs +3 -2
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +2 -2
- 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 +60 -6
- package/dist/cjs/protection/providerText.cjs.map +1 -1
- package/dist/cjs/protection/providerTextAdmission.cjs +5 -0
- package/dist/cjs/protection/providerTextAdmission.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 +14 -3
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/runtime/client.cjs +4 -0
- package/dist/cjs/runtime/client.cjs.map +1 -1
- package/dist/cjs/stream.cjs +10 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +1 -1
- package/dist/cjs/tools/ToolNode.cjs +46 -13
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +47 -1
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/search/citations.cjs +194 -0
- package/dist/cjs/tools/search/citations.cjs.map +1 -0
- package/dist/cjs/tools/search/content.cjs +163 -42
- package/dist/cjs/tools/search/content.cjs.map +1 -1
- package/dist/cjs/tools/search/firecrawl.cjs +4 -9
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/google.cjs +68 -0
- package/dist/cjs/tools/search/google.cjs.map +1 -0
- package/dist/cjs/tools/search/openai.cjs +80 -0
- package/dist/cjs/tools/search/openai.cjs.map +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +4 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +12 -2
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +57 -7
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/harness/providerText.mjs +1 -0
- package/dist/esm/harness/providerText.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/google/utils/signatures.mjs +22 -14
- package/dist/esm/llm/google/utils/signatures.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/invoke.mjs +124 -15
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +9 -3
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/typedContent.mjs +93 -0
- package/dist/esm/llm/openai/typedContent.mjs.map +1 -0
- package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
- package/dist/esm/llm/prepareProviderRequest.mjs +81 -4
- package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
- package/dist/esm/llm/providerTextBatch.mjs +128 -0
- package/dist/esm/llm/providerTextBatch.mjs.map +1 -0
- package/dist/esm/llm/providerTextBoundary.mjs +81 -12
- package/dist/esm/llm/providerTextBoundary.mjs.map +1 -1
- package/dist/esm/main.mjs +7 -6
- 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/core.mjs +3 -2
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +2 -2
- 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 +58 -7
- package/dist/esm/protection/providerText.mjs.map +1 -1
- package/dist/esm/protection/providerTextAdmission.mjs +5 -0
- package/dist/esm/protection/providerTextAdmission.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 +14 -3
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/runtime/client.mjs +4 -0
- package/dist/esm/runtime/client.mjs.map +1 -1
- package/dist/esm/stream.mjs +10 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +1 -1
- package/dist/esm/tools/ToolNode.mjs +46 -13
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +47 -2
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/search/citations.mjs +190 -0
- package/dist/esm/tools/search/citations.mjs.map +1 -0
- package/dist/esm/tools/search/content.mjs +163 -42
- package/dist/esm/tools/search/content.mjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +4 -9
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/google.mjs +67 -0
- package/dist/esm/tools/search/google.mjs.map +1 -0
- package/dist/esm/tools/search/openai.mjs +80 -0
- package/dist/esm/tools/search/openai.mjs.map +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +4 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +12 -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 +18 -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/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 +1 -1
- package/dist/types/llm/openai/typedContent.d.ts +14 -0
- package/dist/types/llm/providerRegistry.d.ts +1 -1
- package/dist/types/llm/providerTextBatch.d.ts +26 -0
- package/dist/types/llm/providerTextBoundary.d.ts +7 -1
- package/dist/types/llm/providers.d.ts +1 -1
- package/dist/types/messages/anthropicServerTools.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 +32 -2
- package/dist/types/protection/providerTextAdmission.d.ts +1 -0
- 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 +1 -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 +3 -3
- 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/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/handlers.d.ts +13 -0
- 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/citations.d.ts +88 -0
- 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 +4 -2
- package/dist/types/tools/search/format.d.ts +1 -1
- package/dist/types/tools/search/google.d.ts +11 -0
- 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/openai.d.ts +3 -0
- 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 +26 -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 +6 -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 +1 -1
- package/dist/types/types/graph.d.ts +4 -1
- package/dist/types/types/index.d.ts +14 -14
- package/dist/types/types/llm.d.ts +2 -2
- package/dist/types/types/run.d.ts +3 -1
- package/dist/types/types/stream.d.ts +12 -0
- package/dist/types/types/subagentTasks.d.ts +2 -2
- package/dist/types/types/tools.d.ts +6 -3
- package/dist/types/utils/index.d.ts +10 -10
- package/dist/types/utils/tokens.d.ts +2 -0
- package/package.json +3 -2
- package/src/graphs/Graph.ts +125 -3
- package/src/harness/providerText.ts +1 -0
- package/src/index.ts +10 -0
- 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/google/utils/signatures.ts +52 -36
- package/src/llm/init.ts +35 -3
- package/src/llm/invoke.ts +160 -16
- package/src/llm/openai/index.ts +18 -5
- package/src/llm/openai/typedContent.ts +110 -0
- package/src/llm/prepareProviderRequest.ts +154 -4
- package/src/llm/providerTextBatch.ts +158 -0
- package/src/llm/providerTextBoundary.ts +119 -13
- package/src/messages/anthropicServerTools.ts +24 -0
- package/src/messages/core.ts +12 -11
- package/src/messages/toolResultTypes.ts +80 -88
- package/src/protection/providerText.ts +222 -46
- package/src/protection/providerTextAdmission.ts +6 -0
- package/src/protection/toolResult.ts +272 -0
- package/src/run.ts +37 -5
- package/src/runtime/client.ts +6 -0
- package/src/stream.ts +23 -2
- package/src/tools/ToolNode.ts +112 -17
- package/src/tools/handlers.ts +110 -5
- package/src/tools/search/citations.ts +375 -0
- package/src/tools/search/content.ts +282 -62
- package/src/tools/search/firecrawl.ts +13 -16
- package/src/tools/search/google.ts +122 -0
- package/src/tools/search/openai.ts +118 -0
- package/src/tools/search/types.ts +21 -0
- package/src/tools/subagent/SubagentExecutor.ts +6 -0
- package/src/types/graph.ts +3 -0
- package/src/types/run.ts +3 -0
- package/src/types/stream.ts +9 -0
- package/src/types/tools.ts +3 -0
- package/src/utils/tokens.ts +37 -2
|
@@ -5,6 +5,10 @@ import type {
|
|
|
5
5
|
ToolMessage,
|
|
6
6
|
} from '@langchain/core/messages';
|
|
7
7
|
import type { ProviderName } from '@/types/llm';
|
|
8
|
+
import {
|
|
9
|
+
ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE,
|
|
10
|
+
isAnthropicServerToolResultType,
|
|
11
|
+
} from './anthropicServerTools';
|
|
8
12
|
import { getProviderFamily } from '@/llm/providers';
|
|
9
13
|
|
|
10
14
|
export type ProviderToolCallKind =
|
|
@@ -61,16 +65,6 @@ const MCP_CALL_KINDS = ['mcp'] as const;
|
|
|
61
65
|
const GOOGLE_CALL_KINDS = ['google'] as const;
|
|
62
66
|
const BEDROCK_CALL_KINDS = ['bedrock', 'tool'] as const;
|
|
63
67
|
|
|
64
|
-
const ADVISOR_TOOL_NAMES = ['advisor'] as const;
|
|
65
|
-
const BASH_TOOL_NAMES = ['bash_code_execution'] as const;
|
|
66
|
-
const CODE_EXECUTION_TOOL_NAMES = ['code_execution'] as const;
|
|
67
|
-
const TEXT_EDITOR_TOOL_NAMES = ['text_editor_code_execution'] as const;
|
|
68
|
-
const TOOL_SEARCH_TOOL_NAMES = [
|
|
69
|
-
'tool_search_tool_regex',
|
|
70
|
-
'tool_search_tool_bm25',
|
|
71
|
-
] as const;
|
|
72
|
-
const WEB_FETCH_TOOL_NAMES = ['web_fetch'] as const;
|
|
73
|
-
const WEB_SEARCH_TOOL_NAMES = ['web_search'] as const;
|
|
74
68
|
const ANTHROPIC_SERVER_TOOL_ID_PREFIX = 'srvtoolu_';
|
|
75
69
|
|
|
76
70
|
function stringSet(values: readonly string[]): ReadonlySet<string> {
|
|
@@ -95,15 +89,9 @@ const PROVIDER_TOOL_CALL_KINDS = stringSet([
|
|
|
95
89
|
'server',
|
|
96
90
|
'tool',
|
|
97
91
|
]);
|
|
98
|
-
const ANTHROPIC_SERVER_TOOL_NAMES = stringSet(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
...CODE_EXECUTION_TOOL_NAMES,
|
|
102
|
-
...TEXT_EDITOR_TOOL_NAMES,
|
|
103
|
-
...TOOL_SEARCH_TOOL_NAMES,
|
|
104
|
-
...WEB_FETCH_TOOL_NAMES,
|
|
105
|
-
...WEB_SEARCH_TOOL_NAMES,
|
|
106
|
-
]);
|
|
92
|
+
const ANTHROPIC_SERVER_TOOL_NAMES = stringSet(
|
|
93
|
+
Object.values(ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE).flat()
|
|
94
|
+
);
|
|
107
95
|
|
|
108
96
|
function getRecord(value: unknown): Record<string, unknown> | undefined {
|
|
109
97
|
if (value == null || typeof value !== 'object') {
|
|
@@ -399,17 +387,6 @@ function hasValidProviderMetadata(record: Record<string, unknown>): boolean {
|
|
|
399
387
|
);
|
|
400
388
|
}
|
|
401
389
|
|
|
402
|
-
const ANTHROPIC_STRUCTURED_RESULT_TOOL_NAMES: Readonly<
|
|
403
|
-
Partial<Record<string, readonly string[]>>
|
|
404
|
-
> = {
|
|
405
|
-
advisor_tool_result: ADVISOR_TOOL_NAMES,
|
|
406
|
-
bash_code_execution_tool_result: BASH_TOOL_NAMES,
|
|
407
|
-
code_execution_tool_result: CODE_EXECUTION_TOOL_NAMES,
|
|
408
|
-
text_editor_code_execution_tool_result: TEXT_EDITOR_TOOL_NAMES,
|
|
409
|
-
tool_search_tool_result: TOOL_SEARCH_TOOL_NAMES,
|
|
410
|
-
web_fetch_tool_result: WEB_FETCH_TOOL_NAMES,
|
|
411
|
-
};
|
|
412
|
-
|
|
413
390
|
/** Designated provider output is opaque once its exact envelope and matching
|
|
414
391
|
* call establish authorship. Only its container size/proxy identity is read;
|
|
415
392
|
* nested values are deliberately not walked on the formatting hot path. */
|
|
@@ -601,42 +578,36 @@ const LOCAL_RESULT_METADATA_FIELDS = [
|
|
|
601
578
|
'agentId',
|
|
602
579
|
'groupId',
|
|
603
580
|
] as const;
|
|
604
|
-
const TOOL_RESULT_ENVELOPE_FIELDS: Readonly<
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
'thoughtSignature',
|
|
635
|
-
'agentId',
|
|
636
|
-
'groupId',
|
|
637
|
-
],
|
|
638
|
-
toolResult: ['type', 'toolResult', 'agentId', 'groupId'],
|
|
639
|
-
};
|
|
581
|
+
const TOOL_RESULT_ENVELOPE_FIELDS: Readonly<
|
|
582
|
+
Partial<Record<string, readonly string[]>>
|
|
583
|
+
> = {
|
|
584
|
+
mcp_tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
|
|
585
|
+
tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
|
|
586
|
+
server_tool_call_result: [
|
|
587
|
+
'type',
|
|
588
|
+
'toolCallId',
|
|
589
|
+
'status',
|
|
590
|
+
'output',
|
|
591
|
+
...LOCAL_RESULT_METADATA_FIELDS,
|
|
592
|
+
],
|
|
593
|
+
server_tool_result: [
|
|
594
|
+
'type',
|
|
595
|
+
'tool_call_id',
|
|
596
|
+
'status',
|
|
597
|
+
'output',
|
|
598
|
+
...LOCAL_RESULT_METADATA_FIELDS,
|
|
599
|
+
],
|
|
600
|
+
codeExecutionResult: ['type', 'codeExecutionResult', 'agentId', 'groupId'],
|
|
601
|
+
toolResponse: [
|
|
602
|
+
'type',
|
|
603
|
+
'toolResponse',
|
|
604
|
+
'thought',
|
|
605
|
+
'thoughtSignature',
|
|
606
|
+
'agentId',
|
|
607
|
+
'groupId',
|
|
608
|
+
],
|
|
609
|
+
toolResult: ['type', 'toolResult', 'agentId', 'groupId'],
|
|
610
|
+
};
|
|
640
611
|
|
|
641
612
|
function anthropicDescriptor(
|
|
642
613
|
record: Record<string, unknown>,
|
|
@@ -663,10 +634,18 @@ export function getProviderToolResultPartDescriptor(
|
|
|
663
634
|
return undefined;
|
|
664
635
|
}
|
|
665
636
|
const type = readString(record, 'type');
|
|
666
|
-
|
|
667
|
-
|
|
637
|
+
if (type == null) {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
const isAnthropicResult = isAnthropicServerToolResultType(type);
|
|
641
|
+
let allowedFields = TOOL_RESULT_ENVELOPE_FIELDS[type];
|
|
642
|
+
if (isAnthropicResult) {
|
|
643
|
+
allowedFields =
|
|
644
|
+
type === 'web_search_tool_result' || type === 'web_fetch_tool_result'
|
|
645
|
+
? ANTHROPIC_SERVER_RESULT_FIELDS
|
|
646
|
+
: ANTHROPIC_RESULT_FIELDS;
|
|
647
|
+
}
|
|
668
648
|
if (
|
|
669
|
-
type == null ||
|
|
670
649
|
allowedFields == null ||
|
|
671
650
|
!hasOnlyOwnDataProperties(record, allowedFields) ||
|
|
672
651
|
!hasValidProviderMetadata(record) ||
|
|
@@ -675,9 +654,12 @@ export function getProviderToolResultPartDescriptor(
|
|
|
675
654
|
) {
|
|
676
655
|
return undefined;
|
|
677
656
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
657
|
+
if (isAnthropicResult && type !== 'web_search_tool_result') {
|
|
658
|
+
return anthropicDescriptor(
|
|
659
|
+
record,
|
|
660
|
+
type,
|
|
661
|
+
ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE[type]
|
|
662
|
+
);
|
|
681
663
|
}
|
|
682
664
|
if (type === 'web_search_tool_result') {
|
|
683
665
|
const toolCallId = readPairingString(record, 'tool_use_id');
|
|
@@ -689,7 +671,7 @@ export function getProviderToolResultPartDescriptor(
|
|
|
689
671
|
type,
|
|
690
672
|
toolCallId,
|
|
691
673
|
compatibleCallKinds: ANTHROPIC_SERVER_CALL_KINDS,
|
|
692
|
-
expectedToolNames:
|
|
674
|
+
expectedToolNames: ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE[type],
|
|
693
675
|
}
|
|
694
676
|
: undefined;
|
|
695
677
|
}
|
|
@@ -776,7 +758,8 @@ export function isProviderToolContentPart(part: unknown): boolean {
|
|
|
776
758
|
return (
|
|
777
759
|
type != null &&
|
|
778
760
|
(PROVIDER_TOOL_CALL_CONTENT_TYPES.has(type) ||
|
|
779
|
-
|
|
761
|
+
isAnthropicServerToolResultType(type) ||
|
|
762
|
+
Object.hasOwn(TOOL_RESULT_ENVELOPE_FIELDS, type))
|
|
780
763
|
);
|
|
781
764
|
}
|
|
782
765
|
|
|
@@ -847,6 +830,10 @@ const STANDARD_CALL_FIELDS = [
|
|
|
847
830
|
'index',
|
|
848
831
|
...LOCAL_CALL_METADATA_FIELDS,
|
|
849
832
|
] as const;
|
|
833
|
+
const STANDARD_CALL_WITH_CALLER_FIELDS = [
|
|
834
|
+
...STANDARD_CALL_FIELDS,
|
|
835
|
+
'caller',
|
|
836
|
+
] as const;
|
|
850
837
|
const LIBRECHAT_CALL_FIELDS = [
|
|
851
838
|
'type',
|
|
852
839
|
'id',
|
|
@@ -927,7 +914,8 @@ export function getProviderToolCallPartDescriptor(
|
|
|
927
914
|
['type', 'tool_call', 'agentId', 'groupId'],
|
|
928
915
|
['tool_call']
|
|
929
916
|
)
|
|
930
|
-
: hasExactCallShape(record,
|
|
917
|
+
: hasExactCallShape(record, STANDARD_CALL_WITH_CALLER_FIELDS, ['id', 'name', 'args']) &&
|
|
918
|
+
hasValidCallerProperty(record);
|
|
931
919
|
const callIsValid = nested.found
|
|
932
920
|
? call != null &&
|
|
933
921
|
hasExactCallShape(call, LIBRECHAT_CALL_FIELDS, [
|
|
@@ -993,23 +981,27 @@ export function getProviderToolCallPartDescriptor(
|
|
|
993
981
|
};
|
|
994
982
|
}
|
|
995
983
|
if (type === 'server_tool_call') {
|
|
984
|
+
const callId = readPairingString(record, 'id');
|
|
985
|
+
const name = optionalName(record);
|
|
986
|
+
if (callId == null || name == null) {
|
|
987
|
+
return undefined;
|
|
988
|
+
}
|
|
989
|
+
const isAnthropic = isAnthropicServerToolCall(callId, name.name);
|
|
990
|
+
const fields = isAnthropic
|
|
991
|
+
? STANDARD_CALL_WITH_CALLER_FIELDS
|
|
992
|
+
: STANDARD_CALL_FIELDS;
|
|
996
993
|
if (
|
|
997
|
-
!hasExactCallShape(record,
|
|
994
|
+
!hasExactCallShape(record, fields, ['id', 'name', 'args']) ||
|
|
995
|
+
!hasValidCallerProperty(record)
|
|
998
996
|
) {
|
|
999
997
|
return undefined;
|
|
1000
998
|
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
kind: isAnthropicServerToolCall(callId, name.name)
|
|
1008
|
-
? 'anthropic-server'
|
|
1009
|
-
: 'server',
|
|
1010
|
-
sourceType: type,
|
|
1011
|
-
...name,
|
|
1012
|
-
};
|
|
999
|
+
return {
|
|
1000
|
+
callId,
|
|
1001
|
+
kind: isAnthropic ? 'anthropic-server' : 'server',
|
|
1002
|
+
sourceType: type,
|
|
1003
|
+
...name,
|
|
1004
|
+
};
|
|
1013
1005
|
}
|
|
1014
1006
|
if (type === 'toolCall' || type === 'toolUse') {
|
|
1015
1007
|
const outerFields =
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { performance } from 'node:perf_hooks';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ProviderTextAdmission,
|
|
4
|
+
ProviderTextAdmissionLease,
|
|
5
|
+
} from './providerTextAdmission';
|
|
3
6
|
import { PreparedSubagentError } from '@/tools/preparedSubagents';
|
|
4
7
|
import { StreamLimitExceededError } from '@/llm/streamLimits';
|
|
5
8
|
|
|
6
9
|
export const PROVIDER_TEXT_PROTECTION_VERSION = 1;
|
|
10
|
+
export const PROVIDER_TEXT_BATCHING_VERSION = 1;
|
|
11
|
+
export const BATCHED_PROVIDER_EVENT_OWNER = 'librechat:batched-provider-event-owner';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Opt in only when every rule is substring-local with a certified maximum match width.
|
|
15
|
+
* No anchors, surrounding-context assertions, or partial rewrites. Classification and
|
|
16
|
+
* inspection must be valid over overlapping UTF-16 windows. Omit this option to inspect
|
|
17
|
+
* the whole reply without releasing any prose early.
|
|
18
|
+
*/
|
|
19
|
+
export interface ProviderTextBatching {
|
|
20
|
+
readonly version: 1;
|
|
21
|
+
readonly batchBytes: number;
|
|
22
|
+
readonly flushMs: number;
|
|
23
|
+
readonly maxMatchChars: number;
|
|
24
|
+
}
|
|
7
25
|
|
|
8
26
|
export type ProviderTextProtectionErrorCode =
|
|
9
27
|
| 'blocked'
|
|
@@ -40,6 +58,7 @@ export interface ProviderTextProtection {
|
|
|
40
58
|
readonly maxAttemptBytes: number;
|
|
41
59
|
/** Shared across all attempts using this policy, including child graphs. */
|
|
42
60
|
readonly maxBufferedBytes: number;
|
|
61
|
+
readonly batching?: ProviderTextBatching;
|
|
43
62
|
readonly classify: (content: string) => 'prose' | 'unsupported';
|
|
44
63
|
readonly inspect: (input: {
|
|
45
64
|
readonly version: 1;
|
|
@@ -72,14 +91,28 @@ export function createProviderTextRestartCancellation(): ProviderTextProtectionE
|
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
export function isProviderTextRestartCancellation(error: unknown): boolean {
|
|
75
|
-
return
|
|
94
|
+
return (
|
|
95
|
+
error instanceof ProviderTextProtectionError &&
|
|
96
|
+
restartCancellations.has(error)
|
|
97
|
+
);
|
|
76
98
|
}
|
|
77
99
|
|
|
78
|
-
function protectionAbortError(
|
|
100
|
+
function protectionAbortError(
|
|
101
|
+
parent?: AbortSignal
|
|
102
|
+
):
|
|
103
|
+
| ProviderTextProtectionError
|
|
104
|
+
| StreamLimitExceededError
|
|
105
|
+
| PreparedSubagentError {
|
|
79
106
|
const reason: unknown = parent?.reason;
|
|
80
|
-
if (
|
|
107
|
+
if (
|
|
108
|
+
reason instanceof StreamLimitExceededError ||
|
|
109
|
+
reason instanceof PreparedSubagentError
|
|
110
|
+
)
|
|
111
|
+
return reason;
|
|
81
112
|
if (reason instanceof ProviderTextProtectionError) {
|
|
82
|
-
return isProviderTextRestartCancellation(reason)
|
|
113
|
+
return isProviderTextRestartCancellation(reason)
|
|
114
|
+
? reason
|
|
115
|
+
: new ProviderTextProtectionError(reason.code);
|
|
83
116
|
}
|
|
84
117
|
return new ProviderTextProtectionError('cancelled');
|
|
85
118
|
}
|
|
@@ -102,19 +135,34 @@ const errorCodes = new Set<ProviderTextProtectionErrorCode>([
|
|
|
102
135
|
'incompatible',
|
|
103
136
|
]);
|
|
104
137
|
|
|
105
|
-
export type ProviderTextProtectionFailure = Extract<
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
138
|
+
export type ProviderTextProtectionFailure = Extract<
|
|
139
|
+
ProviderTextProtectionResult,
|
|
140
|
+
{ ok: false }
|
|
141
|
+
>;
|
|
142
|
+
|
|
143
|
+
function isProtectionErrorCode(
|
|
144
|
+
code: unknown
|
|
145
|
+
): code is ProviderTextProtectionErrorCode {
|
|
146
|
+
return (
|
|
147
|
+
typeof code === 'string' && (errorCodes as ReadonlySet<string>).has(code)
|
|
148
|
+
);
|
|
109
149
|
}
|
|
110
150
|
|
|
111
151
|
/** Safe wire failure, including errors raised by a separate CJS/ESM copy of the SDK. */
|
|
112
|
-
export function serializeProviderTextProtectionError(
|
|
152
|
+
export function serializeProviderTextProtectionError(
|
|
153
|
+
error: unknown
|
|
154
|
+
): ProviderTextProtectionFailure {
|
|
113
155
|
let code: ProviderTextProtectionErrorCode = 'unavailable';
|
|
114
156
|
try {
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
157
|
+
if (
|
|
158
|
+
error != null &&
|
|
159
|
+
typeof error === 'object' &&
|
|
160
|
+
'name' in error &&
|
|
161
|
+
error.name === 'ProviderTextProtectionError' &&
|
|
162
|
+
'code' in error &&
|
|
163
|
+
isProtectionErrorCode(error.code)
|
|
164
|
+
)
|
|
165
|
+
code = error.code;
|
|
118
166
|
} catch {
|
|
119
167
|
// Never serialize arbitrary exception messages, stacks, causes, or getters.
|
|
120
168
|
}
|
|
@@ -122,13 +170,23 @@ export function serializeProviderTextProtectionError(error: unknown): ProviderTe
|
|
|
122
170
|
}
|
|
123
171
|
|
|
124
172
|
/** Decode only an authenticated host's failure envelope; unknown versions/codes fail closed. */
|
|
125
|
-
export function deserializeProviderTextProtectionError(
|
|
173
|
+
export function deserializeProviderTextProtectionError(
|
|
174
|
+
failure: unknown
|
|
175
|
+
): ProviderTextProtectionError {
|
|
126
176
|
try {
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
177
|
+
if (
|
|
178
|
+
failure != null &&
|
|
179
|
+
typeof failure === 'object' &&
|
|
180
|
+
'version' in failure &&
|
|
181
|
+
failure.version === 1 &&
|
|
182
|
+
'ok' in failure &&
|
|
183
|
+
failure.ok === false &&
|
|
184
|
+
'error' in failure &&
|
|
185
|
+
failure.error != null &&
|
|
186
|
+
typeof failure.error === 'object' &&
|
|
187
|
+
'code' in failure.error &&
|
|
188
|
+
isProtectionErrorCode(failure.error.code)
|
|
189
|
+
) {
|
|
132
190
|
return new ProviderTextProtectionError(failure.error.code);
|
|
133
191
|
}
|
|
134
192
|
} catch {
|
|
@@ -138,7 +196,11 @@ export function deserializeProviderTextProtectionError(failure: unknown): Provid
|
|
|
138
196
|
}
|
|
139
197
|
|
|
140
198
|
export function validateProviderTextTimeout(timeoutMs: number): void {
|
|
141
|
-
if (
|
|
199
|
+
if (
|
|
200
|
+
!Number.isSafeInteger(timeoutMs) ||
|
|
201
|
+
timeoutMs <= 0 ||
|
|
202
|
+
timeoutMs > 2_147_483_647
|
|
203
|
+
)
|
|
142
204
|
throw new ProviderTextProtectionError('incompatible');
|
|
143
205
|
}
|
|
144
206
|
|
|
@@ -146,8 +208,7 @@ export function validateProviderTextProtection(
|
|
|
146
208
|
policy: ProviderTextProtection
|
|
147
209
|
): void {
|
|
148
210
|
const version: number = policy.version;
|
|
149
|
-
if (version !== 1)
|
|
150
|
-
throw new ProviderTextProtectionError('incompatible');
|
|
211
|
+
if (version !== 1) throw new ProviderTextProtectionError('incompatible');
|
|
151
212
|
if (
|
|
152
213
|
typeof policy.inspect !== 'function' ||
|
|
153
214
|
typeof policy.classify !== 'function'
|
|
@@ -169,6 +230,25 @@ export function validateProviderTextProtection(
|
|
|
169
230
|
) {
|
|
170
231
|
throw new ProviderTextProtectionError('incompatible');
|
|
171
232
|
}
|
|
233
|
+
const batch = policy.batching as
|
|
234
|
+
| (Omit<ProviderTextBatching, 'version'> & { version: number })
|
|
235
|
+
| null
|
|
236
|
+
| undefined;
|
|
237
|
+
if (
|
|
238
|
+
batch !== undefined &&
|
|
239
|
+
(batch == null ||
|
|
240
|
+
batch.version !== 1 ||
|
|
241
|
+
!Number.isSafeInteger(batch.batchBytes) ||
|
|
242
|
+
batch.batchBytes < 2 ||
|
|
243
|
+
!Number.isSafeInteger(batch.maxMatchChars) ||
|
|
244
|
+
batch.maxMatchChars < 1 ||
|
|
245
|
+
(batch.maxMatchChars - 1) * 2 > batch.batchBytes ||
|
|
246
|
+
batch.batchBytes > policy.maxAttemptBytes ||
|
|
247
|
+
!Number.isSafeInteger(batch.flushMs) ||
|
|
248
|
+
batch.flushMs < 1 ||
|
|
249
|
+
batch.flushMs > policy.timeoutMs)
|
|
250
|
+
)
|
|
251
|
+
throw new ProviderTextProtectionError('incompatible');
|
|
172
252
|
}
|
|
173
253
|
|
|
174
254
|
export interface ProviderTextAttemptOptions {
|
|
@@ -188,6 +268,9 @@ export class ProviderTextAttempt {
|
|
|
188
268
|
private readonly timer: ReturnType<typeof setTimeout>;
|
|
189
269
|
private readonly abort: () => void;
|
|
190
270
|
private finished = false;
|
|
271
|
+
private publishedText = false;
|
|
272
|
+
private finishHeld = false;
|
|
273
|
+
private finishPending = false;
|
|
191
274
|
private readonly admission?: ProviderTextAdmissionLease;
|
|
192
275
|
|
|
193
276
|
constructor(
|
|
@@ -209,9 +292,12 @@ export class ProviderTextAttempt {
|
|
|
209
292
|
this.charge(512);
|
|
210
293
|
parent?.addEventListener('abort', this.abort, { once: true });
|
|
211
294
|
this.check();
|
|
212
|
-
this.timer = setTimeout(
|
|
213
|
-
|
|
214
|
-
|
|
295
|
+
this.timer = setTimeout(
|
|
296
|
+
() => {
|
|
297
|
+
this.controller.abort(new ProviderTextProtectionError('timeout'));
|
|
298
|
+
},
|
|
299
|
+
Math.min(timeoutMs, policy.timeoutMs)
|
|
300
|
+
);
|
|
215
301
|
this.timer.unref();
|
|
216
302
|
protectedSignals.add(this.signal);
|
|
217
303
|
} catch (error) {
|
|
@@ -223,7 +309,12 @@ export class ProviderTextAttempt {
|
|
|
223
309
|
} catch {
|
|
224
310
|
// Malformed transport signals must not prevent admission rollback.
|
|
225
311
|
}
|
|
226
|
-
if (
|
|
312
|
+
if (
|
|
313
|
+
error instanceof ProviderTextProtectionError ||
|
|
314
|
+
error instanceof StreamLimitExceededError ||
|
|
315
|
+
error instanceof PreparedSubagentError
|
|
316
|
+
)
|
|
317
|
+
throw error;
|
|
227
318
|
throw new ProviderTextProtectionError('incompatible');
|
|
228
319
|
}
|
|
229
320
|
}
|
|
@@ -232,11 +323,29 @@ export class ProviderTextAttempt {
|
|
|
232
323
|
return this.controller.signal;
|
|
233
324
|
}
|
|
234
325
|
|
|
326
|
+
followSignal(signal?: AbortSignal): (() => void) | undefined {
|
|
327
|
+
if (signal == null || signal === this.signal || signal === this.parent) return;
|
|
328
|
+
const abort = (): void => this.controller.abort(protectionAbortError(signal));
|
|
329
|
+
if (signal.aborted) {
|
|
330
|
+
abort();
|
|
331
|
+
this.check();
|
|
332
|
+
}
|
|
333
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
334
|
+
return (): void => signal.removeEventListener('abort', abort);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
get hasPublishedText(): boolean {
|
|
338
|
+
return this.publishedText;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
markTextPublished(): void {
|
|
342
|
+
this.publishedText = true;
|
|
343
|
+
}
|
|
344
|
+
|
|
235
345
|
check(): void {
|
|
236
346
|
if (this.finished) throw new ProviderTextProtectionError('incompatible');
|
|
237
347
|
if (this.signal.aborted) throw this.signal.reason;
|
|
238
|
-
if (this.parent?.aborted === true)
|
|
239
|
-
throw protectionAbortError(this.parent);
|
|
348
|
+
if (this.parent?.aborted === true) throw protectionAbortError(this.parent);
|
|
240
349
|
if (performance.now() >= this.deadline) {
|
|
241
350
|
this.controller.abort(new ProviderTextProtectionError('timeout'));
|
|
242
351
|
throw this.signal.reason;
|
|
@@ -263,7 +372,8 @@ export class ProviderTextAttempt {
|
|
|
263
372
|
|
|
264
373
|
append(text: string, cumulativeReplay = false): void {
|
|
265
374
|
this.check();
|
|
266
|
-
if (cumulativeReplay && this.hasCandidatePrefix(text))
|
|
375
|
+
if (cumulativeReplay && this.hasCandidatePrefix(text))
|
|
376
|
+
text = text.slice(this.textLength);
|
|
267
377
|
if (!text) return;
|
|
268
378
|
const bytes = text.length * 2;
|
|
269
379
|
this.charge(bytes * 2 + 64);
|
|
@@ -283,7 +393,10 @@ export class ProviderTextAttempt {
|
|
|
283
393
|
}
|
|
284
394
|
|
|
285
395
|
matchesText(text: string): boolean {
|
|
286
|
-
return
|
|
396
|
+
return (
|
|
397
|
+
text.length === this.textLength &&
|
|
398
|
+
(text.length === 0 || this.hasCandidatePrefix(text))
|
|
399
|
+
);
|
|
287
400
|
}
|
|
288
401
|
|
|
289
402
|
retain(bytes: number): void {
|
|
@@ -291,13 +404,25 @@ export class ProviderTextAttempt {
|
|
|
291
404
|
this.charge(bytes);
|
|
292
405
|
}
|
|
293
406
|
|
|
407
|
+
/** Release staged window charges after its raw references and inspector have settled. */
|
|
408
|
+
releaseWindow(bytes: number): void {
|
|
409
|
+
this.admission?.uncharge(bytes);
|
|
410
|
+
this.bytes -= bytes;
|
|
411
|
+
this.budget.bytes -= bytes;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
stop(error: ProviderTextProtectionError): void {
|
|
415
|
+
this.controller.abort(error);
|
|
416
|
+
}
|
|
417
|
+
|
|
294
418
|
observeChunk(): void {
|
|
295
419
|
this.check();
|
|
296
420
|
this.charge(128);
|
|
297
421
|
}
|
|
298
422
|
|
|
299
|
-
|
|
300
|
-
|
|
423
|
+
/** Approved-text callbacks can stop blocking cleanup without retaining raw capacity. */
|
|
424
|
+
async wait<T>(work: Promise<T>, retain = true): Promise<T> {
|
|
425
|
+
if (retain) this.pending.add(work);
|
|
301
426
|
return new Promise<T>((resolve, reject) => {
|
|
302
427
|
const abort = (): void => {
|
|
303
428
|
this.signal.removeEventListener('abort', abort);
|
|
@@ -307,18 +432,23 @@ export class ProviderTextAttempt {
|
|
|
307
432
|
if (this.signal.aborted) abort();
|
|
308
433
|
const settle = (): void => {
|
|
309
434
|
this.signal.removeEventListener('abort', abort);
|
|
310
|
-
this.pending.delete(work);
|
|
435
|
+
if (retain) this.pending.delete(work);
|
|
311
436
|
};
|
|
312
437
|
void work.then(
|
|
313
|
-
(value) => {
|
|
314
|
-
|
|
438
|
+
(value) => {
|
|
439
|
+
settle();
|
|
440
|
+
resolve(value);
|
|
441
|
+
},
|
|
442
|
+
(error: Error) => {
|
|
443
|
+
settle();
|
|
444
|
+
reject(error);
|
|
445
|
+
}
|
|
315
446
|
);
|
|
316
447
|
});
|
|
317
448
|
}
|
|
318
449
|
|
|
319
|
-
async
|
|
450
|
+
async inspect(content: string): Promise<string> {
|
|
320
451
|
this.check();
|
|
321
|
-
const content = this.fragments.join('');
|
|
322
452
|
if (!content) return '';
|
|
323
453
|
let result: ProviderTextProtectionResult;
|
|
324
454
|
try {
|
|
@@ -340,17 +470,40 @@ export class ProviderTextAttempt {
|
|
|
340
470
|
} catch (error) {
|
|
341
471
|
this.check();
|
|
342
472
|
if (isProviderTextRestartCancellation(error)) throw error;
|
|
343
|
-
throw deserializeProviderTextProtectionError(
|
|
473
|
+
throw deserializeProviderTextProtectionError(
|
|
474
|
+
serializeProviderTextProtectionError(error)
|
|
475
|
+
);
|
|
344
476
|
}
|
|
345
477
|
this.check();
|
|
346
478
|
const canonical = validateResult(result);
|
|
347
479
|
this.check();
|
|
348
|
-
this.charge(canonical.length * 2);
|
|
349
480
|
return canonical;
|
|
350
481
|
}
|
|
351
482
|
|
|
483
|
+
async release(): Promise<string> {
|
|
484
|
+
this.check();
|
|
485
|
+
const content = this.fragments.join('');
|
|
486
|
+
if (!content) return '';
|
|
487
|
+
const canonical = await this.inspect(content);
|
|
488
|
+
this.retain(canonical.length * 2);
|
|
489
|
+
return canonical;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Native prefetch can end the iterator while the invocation still dispatches its final chunk. */
|
|
493
|
+
holdFinish(): () => void {
|
|
494
|
+
this.finishHeld = true;
|
|
495
|
+
return (): void => {
|
|
496
|
+
this.finishHeld = false;
|
|
497
|
+
if (this.finishPending) this.finish();
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
|
|
352
501
|
finish(): void {
|
|
353
502
|
if (this.finished) return;
|
|
503
|
+
if (this.finishHeld) {
|
|
504
|
+
this.finishPending = true;
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
354
507
|
this.finished = true;
|
|
355
508
|
clearTimeout(this.timer);
|
|
356
509
|
try {
|
|
@@ -375,10 +528,21 @@ export class ProviderTextAttempt {
|
|
|
375
528
|
interface UncheckedResult {
|
|
376
529
|
version?: number;
|
|
377
530
|
ok?: boolean;
|
|
378
|
-
value?: {
|
|
531
|
+
value?: {
|
|
532
|
+
content?: string;
|
|
533
|
+
replacements?: number;
|
|
534
|
+
categories?: readonly { category?: string; count?: number }[];
|
|
535
|
+
};
|
|
379
536
|
error?: { code?: ProviderTextProtectionErrorCode };
|
|
380
537
|
}
|
|
381
538
|
|
|
539
|
+
/** Returns the canonical text of a host inspection result, or throws its failure code. */
|
|
540
|
+
export function readProviderTextResult(
|
|
541
|
+
result: ProviderTextProtectionResult
|
|
542
|
+
): string {
|
|
543
|
+
return validateResult(result);
|
|
544
|
+
}
|
|
545
|
+
|
|
382
546
|
function validateResult(result: UncheckedResult | undefined): string {
|
|
383
547
|
try {
|
|
384
548
|
if (result?.version !== 1 || typeof result.ok !== 'boolean') {
|
|
@@ -386,14 +550,26 @@ function validateResult(result: UncheckedResult | undefined): string {
|
|
|
386
550
|
}
|
|
387
551
|
if (!result.ok) {
|
|
388
552
|
const code = result.error?.code;
|
|
389
|
-
throw new ProviderTextProtectionError(
|
|
553
|
+
throw new ProviderTextProtectionError(
|
|
554
|
+
code != null && errorCodes.has(code) ? code : 'incompatible'
|
|
555
|
+
);
|
|
390
556
|
}
|
|
391
557
|
const value = result.value;
|
|
392
|
-
if (
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
558
|
+
if (
|
|
559
|
+
typeof value?.content !== 'string' ||
|
|
560
|
+
typeof value.replacements !== 'number' ||
|
|
561
|
+
!Number.isSafeInteger(value.replacements) ||
|
|
562
|
+
value.replacements < 0 ||
|
|
563
|
+
!Array.isArray(value.categories) ||
|
|
564
|
+
value.categories.length > 32 ||
|
|
565
|
+
value.categories.some(
|
|
566
|
+
(entry) =>
|
|
567
|
+
typeof entry.category !== 'string' ||
|
|
568
|
+
typeof entry.count !== 'number' ||
|
|
569
|
+
!Number.isSafeInteger(entry.count) ||
|
|
570
|
+
entry.count <= 0
|
|
571
|
+
)
|
|
572
|
+
) {
|
|
397
573
|
throw new ProviderTextProtectionError('incompatible');
|
|
398
574
|
}
|
|
399
575
|
return value.content;
|