@librechat/agents 4.2.9 → 4.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/AgentContext.cjs +32 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/harness/fileBindings.cjs +93 -0
- package/dist/cjs/harness/fileBindings.cjs.map +1 -0
- package/dist/cjs/harness/fileService.cjs +141 -0
- package/dist/cjs/harness/fileService.cjs.map +1 -0
- package/dist/cjs/harness/fileStream.cjs +76 -0
- package/dist/cjs/harness/fileStream.cjs.map +1 -0
- package/dist/cjs/harness/files.cjs +106 -0
- package/dist/cjs/harness/files.cjs.map +1 -0
- package/dist/cjs/harness/index.cjs +3 -0
- package/dist/cjs/harness/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/signatures.cjs +5 -3
- package/dist/cjs/llm/google/utils/signatures.cjs.map +1 -1
- package/dist/cjs/main.cjs +13 -0
- package/dist/cjs/runtime/admission.cjs +135 -0
- package/dist/cjs/runtime/admission.cjs.map +1 -0
- package/dist/cjs/runtime/checkpoint.cjs +40 -0
- package/dist/cjs/runtime/checkpoint.cjs.map +1 -0
- package/dist/cjs/runtime/client.cjs +435 -0
- package/dist/cjs/runtime/client.cjs.map +1 -0
- package/dist/cjs/runtime/files.cjs +301 -0
- package/dist/cjs/runtime/files.cjs.map +1 -0
- package/dist/cjs/runtime/hostTools.cjs +165 -0
- package/dist/cjs/runtime/hostTools.cjs.map +1 -0
- package/dist/cjs/runtime/index.cjs +11 -0
- package/dist/cjs/runtime/model.cjs +303 -0
- package/dist/cjs/runtime/model.cjs.map +1 -0
- package/dist/cjs/runtime/peer.cjs +209 -0
- package/dist/cjs/runtime/peer.cjs.map +1 -0
- package/dist/cjs/runtime/policy.cjs +77 -0
- package/dist/cjs/runtime/policy.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/client.cjs +172 -0
- package/dist/cjs/runtime/recovery/client.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/log.cjs +47 -0
- package/dist/cjs/runtime/recovery/log.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/redis.cjs +239 -0
- package/dist/cjs/runtime/recovery/redis.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/server.cjs +190 -0
- package/dist/cjs/runtime/recovery/server.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/socket.cjs +298 -0
- package/dist/cjs/runtime/recovery/socket.cjs.map +1 -0
- package/dist/cjs/runtime/resume.cjs +115 -0
- package/dist/cjs/runtime/resume.cjs.map +1 -0
- package/dist/cjs/runtime/sealed.cjs +107 -0
- package/dist/cjs/runtime/sealed.cjs.map +1 -0
- package/dist/cjs/runtime/server.cjs +415 -0
- package/dist/cjs/runtime/server.cjs.map +1 -0
- package/dist/cjs/runtime/snapshot.cjs +61 -0
- package/dist/cjs/runtime/snapshot.cjs.map +1 -0
- package/dist/cjs/runtime/types.cjs +20 -0
- package/dist/cjs/runtime/types.cjs.map +1 -0
- package/dist/cjs/runtime/wire.cjs +84 -0
- package/dist/cjs/runtime/wire.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +3 -2
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticCallerPolicy.cjs +24 -1
- package/dist/cjs/tools/ProgrammaticCallerPolicy.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +3 -2
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +2 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +15 -3
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +13 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
- package/dist/cjs/tools/subagent/hostArgs.cjs +2 -0
- package/dist/cjs/tools/subagent/hostArgs.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +32 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/harness/fileBindings.mjs +93 -0
- package/dist/esm/harness/fileBindings.mjs.map +1 -0
- package/dist/esm/harness/fileService.mjs +140 -0
- package/dist/esm/harness/fileService.mjs.map +1 -0
- package/dist/esm/harness/fileStream.mjs +76 -0
- package/dist/esm/harness/fileStream.mjs.map +1 -0
- package/dist/esm/harness/files.mjs +97 -0
- package/dist/esm/harness/files.mjs.map +1 -0
- package/dist/esm/harness/index.mjs +3 -0
- package/dist/esm/harness/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/signatures.mjs +5 -3
- package/dist/esm/llm/google/utils/signatures.mjs.map +1 -1
- package/dist/esm/main.mjs +5 -2
- package/dist/esm/runtime/admission.mjs +133 -0
- package/dist/esm/runtime/admission.mjs.map +1 -0
- package/dist/esm/runtime/checkpoint.mjs +39 -0
- package/dist/esm/runtime/checkpoint.mjs.map +1 -0
- package/dist/esm/runtime/client.mjs +432 -0
- package/dist/esm/runtime/client.mjs.map +1 -0
- package/dist/esm/runtime/files.mjs +299 -0
- package/dist/esm/runtime/files.mjs.map +1 -0
- package/dist/esm/runtime/hostTools.mjs +164 -0
- package/dist/esm/runtime/hostTools.mjs.map +1 -0
- package/dist/esm/runtime/index.mjs +5 -0
- package/dist/esm/runtime/model.mjs +301 -0
- package/dist/esm/runtime/model.mjs.map +1 -0
- package/dist/esm/runtime/peer.mjs +209 -0
- package/dist/esm/runtime/peer.mjs.map +1 -0
- package/dist/esm/runtime/policy.mjs +75 -0
- package/dist/esm/runtime/policy.mjs.map +1 -0
- package/dist/esm/runtime/recovery/client.mjs +172 -0
- package/dist/esm/runtime/recovery/client.mjs.map +1 -0
- package/dist/esm/runtime/recovery/log.mjs +46 -0
- package/dist/esm/runtime/recovery/log.mjs.map +1 -0
- package/dist/esm/runtime/recovery/redis.mjs +239 -0
- package/dist/esm/runtime/recovery/redis.mjs.map +1 -0
- package/dist/esm/runtime/recovery/server.mjs +189 -0
- package/dist/esm/runtime/recovery/server.mjs.map +1 -0
- package/dist/esm/runtime/recovery/socket.mjs +297 -0
- package/dist/esm/runtime/recovery/socket.mjs.map +1 -0
- package/dist/esm/runtime/resume.mjs +111 -0
- package/dist/esm/runtime/resume.mjs.map +1 -0
- package/dist/esm/runtime/sealed.mjs +106 -0
- package/dist/esm/runtime/sealed.mjs.map +1 -0
- package/dist/esm/runtime/server.mjs +413 -0
- package/dist/esm/runtime/server.mjs.map +1 -0
- package/dist/esm/runtime/snapshot.mjs +60 -0
- package/dist/esm/runtime/snapshot.mjs.map +1 -0
- package/dist/esm/runtime/types.mjs +19 -0
- package/dist/esm/runtime/types.mjs.map +1 -0
- package/dist/esm/runtime/wire.mjs +80 -0
- package/dist/esm/runtime/wire.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +3 -2
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticCallerPolicy.mjs +24 -1
- package/dist/esm/tools/ProgrammaticCallerPolicy.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +3 -2
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +2 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +15 -3
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs +13 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
- package/dist/esm/tools/subagent/hostArgs.mjs +2 -1
- package/dist/esm/tools/subagent/hostArgs.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +4 -4
- package/dist/types/agents/projection.d.ts +1 -1
- package/dist/types/events.d.ts +2 -2
- package/dist/types/graphs/Graph.d.ts +13 -13
- package/dist/types/graphs/MultiAgentGraph.d.ts +2 -2
- package/dist/types/graphs/acceptedModelResponse.d.ts +1 -1
- package/dist/types/graphs/createGraph.d.ts +4 -4
- package/dist/types/graphs/graphFactory.d.ts +1 -1
- package/dist/types/graphs/handoff.d.ts +1 -1
- package/dist/types/harness/fileBindings.d.ts +13 -0
- package/dist/types/harness/fileService.d.ts +37 -0
- package/dist/types/harness/fileStream.d.ts +7 -0
- package/dist/types/harness/files.d.ts +108 -0
- package/dist/types/harness/index.d.ts +8 -2
- package/dist/types/harness/model.d.ts +3 -3
- package/dist/types/harness/providerText.d.ts +2 -2
- package/dist/types/hitl/approvalReview.d.ts +1 -1
- package/dist/types/hitl/askUserQuestion.d.ts +1 -1
- package/dist/types/hitl/askUserQuestions.d.ts +1 -1
- package/dist/types/hitl/askUserQuestionsInterrupt.d.ts +1 -1
- package/dist/types/hooks/types.d.ts +2 -2
- package/dist/types/instrumentation.d.ts +1 -1
- package/dist/types/langfuse.d.ts +3 -3
- package/dist/types/langfuseConfig.d.ts +2 -2
- package/dist/types/langfuseRuntimeContext.d.ts +1 -1
- package/dist/types/langfuseRuntimeScope.d.ts +2 -2
- package/dist/types/langfuseSpanRegistry.d.ts +1 -1
- package/dist/types/langfuseToolOutputTracing.d.ts +3 -3
- package/dist/types/llm/anthropic/index.d.ts +1 -1
- package/dist/types/llm/bedrock/index.d.ts +1 -1
- package/dist/types/llm/bedrock/toolCache.d.ts +2 -2
- package/dist/types/llm/contextOverflowRecovery.d.ts +2 -2
- package/dist/types/llm/contextPressureMeter.d.ts +2 -2
- package/dist/types/llm/google/index.d.ts +1 -1
- package/dist/types/llm/init.d.ts +1 -1
- package/dist/types/llm/invoke.d.ts +9 -9
- package/dist/types/llm/mistral/index.d.ts +1 -1
- package/dist/types/llm/openai/index.d.ts +2 -2
- package/dist/types/llm/openrouter/index.d.ts +2 -2
- package/dist/types/llm/openrouter/toolCache.d.ts +2 -2
- package/dist/types/llm/prepareProviderRequest.d.ts +3 -3
- package/dist/types/llm/promptCacheTools.d.ts +1 -1
- package/dist/types/llm/providerRegistry.d.ts +1 -1
- package/dist/types/llm/providerTextBoundary.d.ts +2 -2
- package/dist/types/llm/providerTextControls.d.ts +1 -1
- package/dist/types/llm/providers.d.ts +2 -2
- package/dist/types/llm/request.d.ts +1 -1
- package/dist/types/llm/stream/chunkAdapters.d.ts +1 -1
- package/dist/types/llm/streamLimits.d.ts +1 -1
- package/dist/types/llm/truncation.d.ts +1 -1
- package/dist/types/llm/vertexai/index.d.ts +1 -1
- package/dist/types/messages/alternation.d.ts +1 -1
- package/dist/types/messages/anthropicToolCache.d.ts +2 -2
- package/dist/types/messages/assistantPhase.d.ts +3 -3
- package/dist/types/messages/budget.d.ts +1 -1
- package/dist/types/messages/cache.d.ts +1 -1
- package/dist/types/messages/contextPruning.d.ts +2 -2
- package/dist/types/messages/core.d.ts +1 -1
- package/dist/types/messages/fading.d.ts +1 -1
- package/dist/types/messages/format.d.ts +2 -2
- package/dist/types/messages/ids.d.ts +2 -2
- package/dist/types/messages/injected.d.ts +1 -1
- package/dist/types/messages/langchain.d.ts +1 -1
- package/dist/types/messages/prune.d.ts +4 -4
- package/dist/types/messages/recency.d.ts +1 -1
- package/dist/types/messages/toolResultTypes.d.ts +1 -1
- package/dist/types/openai/arguments.d.ts +1 -1
- package/dist/types/openai/index.d.ts +1 -1
- package/dist/types/openai/toolProjection.d.ts +1 -1
- package/dist/types/prompts/activityLabel.d.ts +2 -2
- package/dist/types/prompts/reasoningLabel.d.ts +1 -1
- package/dist/types/rerank/search.d.ts +1 -1
- package/dist/types/responses/index.d.ts +1 -1
- package/dist/types/run.d.ts +3 -3
- package/dist/types/runtime/admission.d.ts +30 -0
- package/dist/types/runtime/checkpoint.d.ts +14 -0
- package/dist/types/runtime/client.d.ts +12 -0
- package/dist/types/runtime/files.d.ts +75 -0
- package/dist/types/runtime/hostTools.d.ts +22 -0
- package/dist/types/runtime/identity.d.ts +2 -0
- package/dist/types/runtime/index.d.ts +12 -0
- package/dist/types/runtime/model.d.ts +42 -0
- package/dist/types/runtime/peer.d.ts +55 -0
- package/dist/types/runtime/policy.d.ts +43 -0
- package/dist/types/runtime/recovery/client.d.ts +12 -0
- package/dist/types/runtime/recovery/journal.d.ts +62 -0
- package/dist/types/runtime/recovery/log.d.ts +9 -0
- package/dist/types/runtime/recovery/redis.d.ts +17 -0
- package/dist/types/runtime/recovery/server.d.ts +37 -0
- package/dist/types/runtime/recovery/socket.d.ts +72 -0
- package/dist/types/runtime/resume.d.ts +66 -0
- package/dist/types/runtime/sealed.d.ts +19 -0
- package/dist/types/runtime/server.d.ts +21 -0
- package/dist/types/runtime/snapshot.d.ts +12 -0
- package/dist/types/runtime/types.d.ts +125 -0
- package/dist/types/runtime/wire.d.ts +3 -0
- package/dist/types/session/handlers.d.ts +1 -1
- package/dist/types/session/types.d.ts +1 -1
- package/dist/types/stream.d.ts +3 -3
- package/dist/types/summarization/index.d.ts +1 -1
- package/dist/types/summarization/input.d.ts +1 -1
- package/dist/types/summarization/node.d.ts +6 -6
- package/dist/types/summarization/retry.d.ts +2 -2
- package/dist/types/summarization/semanticIndex.d.ts +2 -2
- package/dist/types/summarization/toolBudget.d.ts +1 -1
- package/dist/types/summarization/usage.d.ts +1 -1
- package/dist/types/tools/ArtifactDelivery.d.ts +1 -1
- package/dist/types/tools/ArtifactTruncation.d.ts +1 -1
- package/dist/types/tools/BashExecutor.d.ts +2 -2
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/CallerCapabilities.d.ts +1 -1
- package/dist/types/tools/CodeExecutor.d.ts +4 -4
- package/dist/types/tools/CodeSessionFileSummary.d.ts +1 -1
- package/dist/types/tools/ProgrammaticCallerPolicy.d.ts +2 -1
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/ReadFile.d.ts +1 -1
- package/dist/types/tools/SkillTool.d.ts +1 -1
- package/dist/types/tools/SubagentTool.d.ts +3 -3
- package/dist/types/tools/ToolNode.d.ts +5 -5
- package/dist/types/tools/ToolSearch.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareBridgeRuntime.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareSandboxExecutionEngine.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareSandboxTools.d.ts +1 -1
- package/dist/types/tools/diagnostics.d.ts +1 -1
- package/dist/types/tools/eagerEventExecution.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +3 -3
- package/dist/types/tools/intentArg.d.ts +1 -1
- package/dist/types/tools/local/CompileCheckTool.d.ts +2 -2
- package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
- package/dist/types/tools/local/LocalCodingTools.d.ts +2 -2
- package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
- package/dist/types/tools/local/LocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/LocalProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/local/bashAst.d.ts +1 -1
- package/dist/types/tools/local/resolveLocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/syntaxCheck.d.ts +1 -1
- package/dist/types/tools/ptcTimeout.d.ts +1 -1
- package/dist/types/tools/runStepResume.d.ts +1 -1
- package/dist/types/tools/schema.d.ts +1 -1
- package/dist/types/tools/search/anthropic.d.ts +1 -1
- package/dist/types/tools/skillCatalog.d.ts +1 -1
- package/dist/types/tools/subagent/InMemorySubagentTaskStore.d.ts +1 -1
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +5 -5
- package/dist/types/tools/subagent/SubagentReplay.d.ts +3 -3
- package/dist/types/tools/subagent/childGraphConfig.d.ts +2 -2
- package/dist/types/tools/subagent/diagnostics.d.ts +2 -2
- package/dist/types/tools/subagent/hostArgs.d.ts +8 -2
- package/dist/types/tools/subagent/index.d.ts +1 -1
- package/dist/types/tools/toolBatchReplay.d.ts +3 -3
- package/dist/types/types/activityLabel.d.ts +2 -2
- package/dist/types/types/graph.d.ts +16 -13
- package/dist/types/types/llm.d.ts +9 -9
- package/dist/types/types/reasoningLabel.d.ts +1 -1
- package/dist/types/types/run.d.ts +8 -8
- package/dist/types/types/stream.d.ts +5 -5
- package/dist/types/types/summarize.d.ts +2 -2
- package/dist/types/types/tools.d.ts +8 -8
- package/dist/types/utils/errors.d.ts +1 -1
- package/dist/types/utils/events.d.ts +3 -3
- package/dist/types/utils/handlers.d.ts +3 -3
- package/dist/types/utils/llm.d.ts +2 -2
- package/dist/types/utils/llmConfig.d.ts +1 -1
- package/dist/types/utils/title.d.ts +1 -1
- package/dist/types/utils/toolSessions.d.ts +1 -1
- package/package.json +22 -3
- package/src/agents/AgentContext.ts +68 -2
- package/src/harness/fileBindings.ts +156 -0
- package/src/harness/fileService.ts +217 -0
- package/src/harness/fileStream.ts +99 -0
- package/src/harness/files.ts +185 -0
- package/src/harness/index.ts +24 -0
- package/src/llm/google/utils/signatures.ts +10 -3
- package/src/runtime/admission.ts +224 -0
- package/src/runtime/checkpoint.ts +66 -0
- package/src/runtime/client.ts +674 -0
- package/src/runtime/files.ts +503 -0
- package/src/runtime/hostTools.ts +243 -0
- package/src/runtime/identity.ts +3 -0
- package/src/runtime/index.ts +33 -0
- package/src/runtime/model.ts +452 -0
- package/src/runtime/peer.ts +262 -0
- package/src/runtime/policy.ts +150 -0
- package/src/runtime/recovery/client.ts +232 -0
- package/src/runtime/recovery/journal.ts +88 -0
- package/src/runtime/recovery/log.ts +70 -0
- package/src/runtime/recovery/redis.ts +342 -0
- package/src/runtime/recovery/server.ts +280 -0
- package/src/runtime/recovery/socket.ts +422 -0
- package/src/runtime/resume.ts +268 -0
- package/src/runtime/sealed.ts +144 -0
- package/src/runtime/server.ts +653 -0
- package/src/runtime/snapshot.ts +87 -0
- package/src/runtime/types.ts +184 -0
- package/src/runtime/wire.ts +96 -0
- package/src/tools/BashProgrammaticToolCalling.ts +5 -2
- package/src/tools/ProgrammaticCallerPolicy.ts +42 -3
- package/src/tools/ProgrammaticToolCalling.ts +4 -1
- package/src/tools/local/LocalProgrammaticToolCalling.ts +4 -0
- package/src/tools/subagent/SubagentExecutor.ts +98 -32
- package/src/tools/subagent/childGraphConfig.ts +33 -1
- package/src/tools/subagent/hostArgs.ts +7 -0
- package/src/tools/subagent/index.ts +1 -0
- package/src/types/graph.ts +6 -3
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
5
|
+
import type { Duplex } from 'node:stream';
|
|
6
|
+
import type { Server } from 'node:http';
|
|
7
|
+
import type {
|
|
8
|
+
HostModelDescriptor,
|
|
9
|
+
EventHandler,
|
|
10
|
+
AggregatedHookResult,
|
|
11
|
+
HookOutput,
|
|
12
|
+
} from '@/index';
|
|
13
|
+
import type {
|
|
14
|
+
RemoteSdkFactoryInput,
|
|
15
|
+
RemoteSdkLimits,
|
|
16
|
+
RemoteSdkStart,
|
|
17
|
+
SdkRun,
|
|
18
|
+
} from './types';
|
|
19
|
+
import type { RuntimeRecoveryOptions } from './recovery/server';
|
|
20
|
+
import type { SdkAgentIdentity } from './identity';
|
|
21
|
+
import {
|
|
22
|
+
Run,
|
|
23
|
+
createHostToolBindings,
|
|
24
|
+
HookRegistry,
|
|
25
|
+
HOOK_EVENTS,
|
|
26
|
+
GraphEvents,
|
|
27
|
+
} from '@/index';
|
|
28
|
+
import {
|
|
29
|
+
createWorkerModelLifecycle,
|
|
30
|
+
supportsModelInvocationContext,
|
|
31
|
+
} from './admission';
|
|
32
|
+
import {
|
|
33
|
+
policyDescriptorSchema,
|
|
34
|
+
createWorkerProviderTextPolicy,
|
|
35
|
+
} from './policy';
|
|
36
|
+
import { createRuntimeRecovery, recoveryClaimSchema } from './recovery/server';
|
|
37
|
+
import { createWorkerFileChannel, fileChannelSchema } from './files';
|
|
38
|
+
import { remoteSdkLimits, isRemoteSdkSecret } from './types';
|
|
39
|
+
import { RECOVERY_HEADER_BYTES } from './recovery/socket';
|
|
40
|
+
import { createWorkerToolService } from './hostTools';
|
|
41
|
+
import { RemoteSdkCheckpointer } from './checkpoint';
|
|
42
|
+
import { createWorkerModelChannel } from './model';
|
|
43
|
+
import { sendSnapshot } from './snapshot';
|
|
44
|
+
import { createSdkPeer } from './peer';
|
|
45
|
+
|
|
46
|
+
const identitySchema = z
|
|
47
|
+
.object({
|
|
48
|
+
runtimeId: z.string().min(1),
|
|
49
|
+
agentId: z.string().min(1),
|
|
50
|
+
runId: z.string().min(1),
|
|
51
|
+
userId: z.string().min(1),
|
|
52
|
+
tenantId: z.string().min(1).optional(),
|
|
53
|
+
conversationId: z.string().min(1).optional(),
|
|
54
|
+
})
|
|
55
|
+
.strict();
|
|
56
|
+
const claimSchema = z.object({
|
|
57
|
+
protocolVersion: z.literal(1),
|
|
58
|
+
recovery: recoveryClaimSchema.optional(),
|
|
59
|
+
identity: identitySchema,
|
|
60
|
+
threadId: z.string().min(1),
|
|
61
|
+
invocationId: z.string().uuid(),
|
|
62
|
+
iat: z.number(),
|
|
63
|
+
exp: z.number(),
|
|
64
|
+
});
|
|
65
|
+
const startSchema = z.object({
|
|
66
|
+
transportVersion: z.literal(2),
|
|
67
|
+
files: fileChannelSchema.optional(),
|
|
68
|
+
modelAdmission: z
|
|
69
|
+
.object({
|
|
70
|
+
version: z.literal(1),
|
|
71
|
+
maxEntries: z.number().int().min(16).max(65536),
|
|
72
|
+
})
|
|
73
|
+
.strict()
|
|
74
|
+
.optional(),
|
|
75
|
+
// The SDK HostChatModel constructor owns descriptor validation.
|
|
76
|
+
model: z
|
|
77
|
+
.object({
|
|
78
|
+
version: z.literal(1),
|
|
79
|
+
publicKey: z.string().length(60),
|
|
80
|
+
descriptor: z.custom<HostModelDescriptor>(),
|
|
81
|
+
})
|
|
82
|
+
.strict()
|
|
83
|
+
.optional(),
|
|
84
|
+
providerText: policyDescriptorSchema.optional(),
|
|
85
|
+
hostTools: z
|
|
86
|
+
.array(
|
|
87
|
+
z.object({
|
|
88
|
+
reference: z.string().min(1),
|
|
89
|
+
definition: z.object({ name: z.string().min(1) }).passthrough(),
|
|
90
|
+
})
|
|
91
|
+
)
|
|
92
|
+
.optional(),
|
|
93
|
+
operation: z.enum(['start', 'resume']),
|
|
94
|
+
input: z.object({ messages: z.array(z.unknown()) }).passthrough(),
|
|
95
|
+
resume: z.unknown().optional(),
|
|
96
|
+
commandOptions: z
|
|
97
|
+
.object({ update: z.unknown().optional(), goto: z.unknown().optional() })
|
|
98
|
+
.optional(),
|
|
99
|
+
options: z.object({
|
|
100
|
+
configurable: z.record(z.string(), z.unknown()),
|
|
101
|
+
recursionLimit: z.number().int().positive().optional(),
|
|
102
|
+
}),
|
|
103
|
+
handlers: z.array(z.nativeEnum(GraphEvents)).max(128),
|
|
104
|
+
hooks: z.array(z.enum(HOOK_EVENTS)),
|
|
105
|
+
checkpoint: z.boolean(),
|
|
106
|
+
streamLimits: z
|
|
107
|
+
.object({
|
|
108
|
+
maxToolCallArgBytes: z.number().nonnegative().optional(),
|
|
109
|
+
maxToolCallArgBytesByTool: z
|
|
110
|
+
.record(z.string(), z.number().nonnegative())
|
|
111
|
+
.optional(),
|
|
112
|
+
maxDeltaEventsPerTurn: z.number().nonnegative().optional(),
|
|
113
|
+
})
|
|
114
|
+
.strict()
|
|
115
|
+
.optional(),
|
|
116
|
+
humanInTheLoop: z.object({ enabled: z.boolean() }).passthrough().optional(),
|
|
117
|
+
agent: z.object({
|
|
118
|
+
id: z.string(),
|
|
119
|
+
name: z.string().optional(),
|
|
120
|
+
instructions: z.string().optional(),
|
|
121
|
+
model: z
|
|
122
|
+
.object({
|
|
123
|
+
provider: z.string().min(1),
|
|
124
|
+
name: z.string().min(1),
|
|
125
|
+
streaming: z.boolean(),
|
|
126
|
+
})
|
|
127
|
+
.strict()
|
|
128
|
+
.optional(),
|
|
129
|
+
}),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
export interface RemoteSdkServerOptions extends Partial<RemoteSdkLimits> {
|
|
133
|
+
recovery?: RuntimeRecoveryOptions;
|
|
134
|
+
runtimeId: string;
|
|
135
|
+
secret: string;
|
|
136
|
+
/** Authorize the signed saved-agent identity against this worker's deployment policy. */
|
|
137
|
+
authorize: (
|
|
138
|
+
identity: Readonly<SdkAgentIdentity>,
|
|
139
|
+
signal: AbortSignal
|
|
140
|
+
) => boolean | Promise<boolean>;
|
|
141
|
+
createRun: (input: RemoteSdkFactoryInput) => Promise<SdkRun>;
|
|
142
|
+
maxConcurrentRuns?: number;
|
|
143
|
+
maxReplayEntries?: number;
|
|
144
|
+
/** Trusted operator diagnostics; errors are never forwarded over the bridge. */
|
|
145
|
+
onError?: (error: unknown) => void;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A separately hosted, operator-trusted SDK runtime. The caller owns listen/TLS and shutdown. */
|
|
149
|
+
export function createRemoteSdkServer(options: RemoteSdkServerOptions): {
|
|
150
|
+
server: Server;
|
|
151
|
+
close: () => Promise<void>;
|
|
152
|
+
} {
|
|
153
|
+
if (!isRemoteSdkSecret(options.secret) || !options.runtimeId)
|
|
154
|
+
throw new Error('Invalid SDK runtime server configuration');
|
|
155
|
+
const limits = remoteSdkLimits(options);
|
|
156
|
+
if (
|
|
157
|
+
[options.maxConcurrentRuns ?? 32, options.maxReplayEntries ?? 10_000].some(
|
|
158
|
+
(value) => !Number.isSafeInteger(value) || value <= 0
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
throw new Error('Invalid SDK runtime admission limits');
|
|
162
|
+
const server = createServer((req, res) => {
|
|
163
|
+
if (req.method === 'GET' && req.url === '/healthz') {
|
|
164
|
+
res
|
|
165
|
+
.writeHead(200, { 'Content-Type': 'application/json' })
|
|
166
|
+
.end(JSON.stringify({ protocolVersion: 1 }));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
res.writeHead(404).end();
|
|
170
|
+
});
|
|
171
|
+
const recovery = options.recovery
|
|
172
|
+
? createRuntimeRecovery({
|
|
173
|
+
...options.recovery,
|
|
174
|
+
...limits,
|
|
175
|
+
onError: options.onError,
|
|
176
|
+
})
|
|
177
|
+
: undefined;
|
|
178
|
+
const sockets = new WebSocketServer({
|
|
179
|
+
noServer: true,
|
|
180
|
+
maxPayload: limits.maxFrameBytes + (recovery ? RECOVERY_HEADER_BYTES : 0),
|
|
181
|
+
perMessageDeflate: false,
|
|
182
|
+
});
|
|
183
|
+
const seen = new Map<string, number>();
|
|
184
|
+
const active = new Set<AbortController>();
|
|
185
|
+
const admitting = new Map<Duplex, AbortController>();
|
|
186
|
+
let closing = false;
|
|
187
|
+
server.on('upgrade', (req, socket, head) => {
|
|
188
|
+
if (
|
|
189
|
+
Boolean(closing) ||
|
|
190
|
+
admitting.size >= (options.maxConcurrentRuns ?? 32) ||
|
|
191
|
+
sockets.clients.size + admitting.size >=
|
|
192
|
+
2 * (options.maxConcurrentRuns ?? 32)
|
|
193
|
+
) {
|
|
194
|
+
socket.destroy();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const admission = new AbortController();
|
|
198
|
+
admitting.set(socket, admission);
|
|
199
|
+
const abortAdmission = (): void => admission.abort();
|
|
200
|
+
const unavailable = (): boolean =>
|
|
201
|
+
closing || admission.signal.aborted || socket.destroyed;
|
|
202
|
+
socket.once('close', abortAdmission);
|
|
203
|
+
const deadline = setTimeout(() => {
|
|
204
|
+
admission.abort();
|
|
205
|
+
socket.destroy();
|
|
206
|
+
}, limits.connectTimeoutMs);
|
|
207
|
+
deadline.unref();
|
|
208
|
+
void (async (): Promise<void> => {
|
|
209
|
+
const deny = (): void => {
|
|
210
|
+
if (!socket.destroyed)
|
|
211
|
+
socket.write(
|
|
212
|
+
'HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n'
|
|
213
|
+
);
|
|
214
|
+
socket.destroy();
|
|
215
|
+
};
|
|
216
|
+
if (req.url !== '/sdk') {
|
|
217
|
+
deny();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
let claim: z.infer<typeof claimSchema>;
|
|
221
|
+
try {
|
|
222
|
+
const auth = req.headers.authorization;
|
|
223
|
+
if (auth?.startsWith('Bearer ') !== true) {
|
|
224
|
+
deny();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
claim = claimSchema.parse(
|
|
228
|
+
// jsonwebtoken has no named ESM exports; the default import is required.
|
|
229
|
+
// eslint-disable-next-line import-x/no-named-as-default-member
|
|
230
|
+
jwt.verify(auth.slice(7), options.secret, {
|
|
231
|
+
algorithms: ['HS256'],
|
|
232
|
+
audience: options.runtimeId,
|
|
233
|
+
issuer: 'librechat-sdk',
|
|
234
|
+
maxAge: '60s',
|
|
235
|
+
})
|
|
236
|
+
);
|
|
237
|
+
if (
|
|
238
|
+
claim.identity.runtimeId !== options.runtimeId ||
|
|
239
|
+
!(await options.authorize(
|
|
240
|
+
Object.freeze(claim.identity),
|
|
241
|
+
admission.signal
|
|
242
|
+
))
|
|
243
|
+
) {
|
|
244
|
+
deny();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
} catch {
|
|
248
|
+
deny();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const now = Date.now();
|
|
252
|
+
for (const [id, expiry] of seen) if (expiry <= now) seen.delete(id);
|
|
253
|
+
if (
|
|
254
|
+
unavailable() ||
|
|
255
|
+
claim.exp * 1000 <= now ||
|
|
256
|
+
seen.has(claim.invocationId) ||
|
|
257
|
+
seen.size >= (options.maxReplayEntries ?? 10_000) ||
|
|
258
|
+
(!claim.recovery && active.size >= (options.maxConcurrentRuns ?? 32))
|
|
259
|
+
) {
|
|
260
|
+
deny();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
seen.set(claim.invocationId, now + 61_000);
|
|
264
|
+
if (claim.recovery && !recovery) {
|
|
265
|
+
deny();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const recovered = claim.recovery
|
|
269
|
+
? await recovery!.prepare(
|
|
270
|
+
Object.freeze(claim.identity),
|
|
271
|
+
claim.threadId,
|
|
272
|
+
claim.recovery
|
|
273
|
+
)
|
|
274
|
+
: undefined;
|
|
275
|
+
const needsEngine = recovered?.needsEngine ?? true;
|
|
276
|
+
if (
|
|
277
|
+
unavailable() ||
|
|
278
|
+
(needsEngine && active.size >= (options.maxConcurrentRuns ?? 32))
|
|
279
|
+
) {
|
|
280
|
+
if (needsEngine) recovered?.dispose();
|
|
281
|
+
deny();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
sockets.handleUpgrade(req, socket, head, (ws) => {
|
|
285
|
+
if (recovered && !needsEngine) {
|
|
286
|
+
recovered.attach(ws, claim.recovery!.after);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const controller = recovered?.controller ?? new AbortController();
|
|
290
|
+
active.add(controller);
|
|
291
|
+
if (recovered) recovered.needsEngine = false;
|
|
292
|
+
const transport = recovered?.socket ?? ws;
|
|
293
|
+
let outcome: 'completed' | 'paused' | 'failed' | 'cancelled' = 'failed';
|
|
294
|
+
let started = false;
|
|
295
|
+
let running = false;
|
|
296
|
+
const timer = setTimeout(() => {
|
|
297
|
+
controller.abort();
|
|
298
|
+
transport.close(1000);
|
|
299
|
+
}, limits.maxRunMs);
|
|
300
|
+
timer.unref();
|
|
301
|
+
const cleanup = (): void => {
|
|
302
|
+
controller.abort();
|
|
303
|
+
workerFiles.dispose();
|
|
304
|
+
clearTimeout(timer);
|
|
305
|
+
if (!running) active.delete(controller);
|
|
306
|
+
};
|
|
307
|
+
transport.once('close', cleanup);
|
|
308
|
+
const workerTools = createWorkerToolService({
|
|
309
|
+
call: (method, data, _timeoutMs, signal) =>
|
|
310
|
+
peer.call(
|
|
311
|
+
method,
|
|
312
|
+
data,
|
|
313
|
+
method === 'tools' ? limits.maxRunMs : undefined,
|
|
314
|
+
signal
|
|
315
|
+
),
|
|
316
|
+
});
|
|
317
|
+
const workerFiles = createWorkerFileChannel({
|
|
318
|
+
peer: { call: (...args) => peer.call(...args) },
|
|
319
|
+
limits,
|
|
320
|
+
signal: controller.signal,
|
|
321
|
+
});
|
|
322
|
+
const workerModel = createWorkerModelChannel({
|
|
323
|
+
peer: { call: (...args) => peer.call(...args) },
|
|
324
|
+
limits,
|
|
325
|
+
signal: controller.signal,
|
|
326
|
+
});
|
|
327
|
+
const peer = createSdkPeer(transport, {
|
|
328
|
+
...limits,
|
|
329
|
+
timeoutMs: limits.rpcTimeoutMs,
|
|
330
|
+
onClose: cleanup,
|
|
331
|
+
onError: options.onError,
|
|
332
|
+
onFrame: async () => {
|
|
333
|
+
throw new Error('Unexpected runtime frame');
|
|
334
|
+
},
|
|
335
|
+
onCall: async (method, data, _timeoutMs, signal) => {
|
|
336
|
+
if (method === 'capabilities')
|
|
337
|
+
return {
|
|
338
|
+
transportVersion: 2,
|
|
339
|
+
hostTools: 2,
|
|
340
|
+
hostFiles: 1,
|
|
341
|
+
providerText: 1,
|
|
342
|
+
hostModel: 1,
|
|
343
|
+
builderModel: 1,
|
|
344
|
+
modelAdmission: 1,
|
|
345
|
+
...(supportsModelInvocationContext && {
|
|
346
|
+
modelAdmissionWithProviderText: 1,
|
|
347
|
+
}),
|
|
348
|
+
};
|
|
349
|
+
if (method === 'files.chunk')
|
|
350
|
+
return workerFiles.accept(data, signal);
|
|
351
|
+
if (method === 'model.chunk')
|
|
352
|
+
return workerModel.accept(data, signal);
|
|
353
|
+
if (method === 'tool.result') return workerTools.accept(data);
|
|
354
|
+
if (method === 'tool.session')
|
|
355
|
+
return workerTools.accept(data, 'session');
|
|
356
|
+
if (method !== 'run' || started)
|
|
357
|
+
throw new Error('SDK execution already admitted');
|
|
358
|
+
started = true;
|
|
359
|
+
const request = startSchema.parse(data) as RemoteSdkStart;
|
|
360
|
+
if (
|
|
361
|
+
request.modelAdmission &&
|
|
362
|
+
request.providerText &&
|
|
363
|
+
!supportsModelInvocationContext
|
|
364
|
+
)
|
|
365
|
+
throw new Error(
|
|
366
|
+
'Combined model admission and provider-text protection is unsupported'
|
|
367
|
+
);
|
|
368
|
+
const identity = Object.freeze(claim.identity);
|
|
369
|
+
if (
|
|
370
|
+
request.agent.id !== identity.agentId ||
|
|
371
|
+
request.options.configurable?.thread_id !== claim.threadId ||
|
|
372
|
+
request.options.configurable.run_id !== identity.runId ||
|
|
373
|
+
request.options.configurable.user_id !== identity.userId ||
|
|
374
|
+
(claim.recovery != null &&
|
|
375
|
+
request.options.configurable.__librechat_checkpoint_ns !==
|
|
376
|
+
claim.recovery.generation)
|
|
377
|
+
) {
|
|
378
|
+
throw new Error('SDK runtime identity mismatch');
|
|
379
|
+
}
|
|
380
|
+
const handlers: Record<string, EventHandler> = {};
|
|
381
|
+
for (const name of request.handlers) {
|
|
382
|
+
if (name === GraphEvents.ON_TOOL_EXECUTE)
|
|
383
|
+
throw new Error('Host tool delegation is not supported');
|
|
384
|
+
handlers[name] = {
|
|
385
|
+
handle: async (event, value, metadata): Promise<void> => {
|
|
386
|
+
peer.send({
|
|
387
|
+
type: 'event',
|
|
388
|
+
name: event,
|
|
389
|
+
data: value,
|
|
390
|
+
metadata,
|
|
391
|
+
});
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
let hooks: HookRegistry | undefined;
|
|
396
|
+
if (request.hooks.length) {
|
|
397
|
+
hooks = new HookRegistry();
|
|
398
|
+
for (const event of request.hooks)
|
|
399
|
+
hooks.register(event, {
|
|
400
|
+
internal: true,
|
|
401
|
+
timeout: limits.rpcTimeoutMs,
|
|
402
|
+
hooks: [
|
|
403
|
+
async (input, hookSignal): Promise<HookOutput> => {
|
|
404
|
+
const abort = (): void => controller.abort();
|
|
405
|
+
hookSignal.addEventListener('abort', abort, {
|
|
406
|
+
once: true,
|
|
407
|
+
});
|
|
408
|
+
try {
|
|
409
|
+
let matchQuery =
|
|
410
|
+
'toolName' in input ? input.toolName : undefined;
|
|
411
|
+
if (
|
|
412
|
+
input.hook_event_name === 'SubagentStart' ||
|
|
413
|
+
input.hook_event_name === 'SubagentStop'
|
|
414
|
+
) {
|
|
415
|
+
matchQuery = input.agentType;
|
|
416
|
+
}
|
|
417
|
+
const {
|
|
418
|
+
errors,
|
|
419
|
+
hasHookFailures,
|
|
420
|
+
additionalContexts,
|
|
421
|
+
stopDecision,
|
|
422
|
+
...result
|
|
423
|
+
} = (await peer.call('hook', {
|
|
424
|
+
input,
|
|
425
|
+
matchQuery,
|
|
426
|
+
})) as AggregatedHookResult;
|
|
427
|
+
if (errors.length || hasHookFailures)
|
|
428
|
+
throw new Error('Host hook failed');
|
|
429
|
+
return {
|
|
430
|
+
...result,
|
|
431
|
+
...(stopDecision ? { decision: stopDecision } : {}),
|
|
432
|
+
additionalContext: additionalContexts.join('\n'),
|
|
433
|
+
};
|
|
434
|
+
} catch {
|
|
435
|
+
controller.abort();
|
|
436
|
+
throw new Error('Host policy unavailable');
|
|
437
|
+
} finally {
|
|
438
|
+
hookSignal.removeEventListener('abort', abort);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
],
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
const hostFiles = request.files
|
|
445
|
+
? workerFiles.bind(identity, request.files)
|
|
446
|
+
: undefined;
|
|
447
|
+
const hostTools =
|
|
448
|
+
request.hostTools != null && request.hostTools.length > 0
|
|
449
|
+
? createHostToolBindings({
|
|
450
|
+
identity,
|
|
451
|
+
tools: request.hostTools,
|
|
452
|
+
service: workerTools.service,
|
|
453
|
+
})
|
|
454
|
+
: undefined;
|
|
455
|
+
const providerTextProtection = request.providerText
|
|
456
|
+
? createWorkerProviderTextPolicy({
|
|
457
|
+
descriptor: request.providerText,
|
|
458
|
+
identity,
|
|
459
|
+
peer,
|
|
460
|
+
limits,
|
|
461
|
+
})
|
|
462
|
+
: undefined;
|
|
463
|
+
if (request.modelAdmission && !request.model)
|
|
464
|
+
throw new Error('Model admission requires a host model');
|
|
465
|
+
const modelLifecycle = request.modelAdmission
|
|
466
|
+
? createWorkerModelLifecycle(
|
|
467
|
+
identity.agentId,
|
|
468
|
+
request.modelAdmission.maxEntries,
|
|
469
|
+
peer
|
|
470
|
+
)
|
|
471
|
+
: undefined;
|
|
472
|
+
const hostModel = request.model
|
|
473
|
+
? workerModel.bind(identity, request.model, modelLifecycle)
|
|
474
|
+
: undefined;
|
|
475
|
+
const profile = request.agent.model;
|
|
476
|
+
if (
|
|
477
|
+
profile &&
|
|
478
|
+
(!hostModel ||
|
|
479
|
+
request.model?.descriptor.provider !== profile.provider ||
|
|
480
|
+
request.model.descriptor.model !== profile.name ||
|
|
481
|
+
request.model.descriptor.streaming !== profile.streaming)
|
|
482
|
+
) {
|
|
483
|
+
throw new Error('SDK runtime builder model mismatch');
|
|
484
|
+
}
|
|
485
|
+
const execute = async (): Promise<void> => {
|
|
486
|
+
await recovered?.begin();
|
|
487
|
+
const run = await options.createRun({
|
|
488
|
+
identity,
|
|
489
|
+
hostTools,
|
|
490
|
+
hostFiles,
|
|
491
|
+
hostModel,
|
|
492
|
+
config: {
|
|
493
|
+
runId: identity.runId,
|
|
494
|
+
customHandlers: { ...handlers, ...hostTools?.customHandlers },
|
|
495
|
+
hooks,
|
|
496
|
+
humanInTheLoop: request.humanInTheLoop,
|
|
497
|
+
streamLimits: request.streamLimits,
|
|
498
|
+
providerTextProtection,
|
|
499
|
+
},
|
|
500
|
+
compileOptions: request.checkpoint
|
|
501
|
+
? { checkpointer: new RemoteSdkCheckpointer(peer.call) }
|
|
502
|
+
: {},
|
|
503
|
+
signal: controller.signal,
|
|
504
|
+
agent: request.agent,
|
|
505
|
+
});
|
|
506
|
+
if (
|
|
507
|
+
!(run instanceof Run) ||
|
|
508
|
+
run.id !== identity.runId ||
|
|
509
|
+
run.Graph?.defaultAgentId !== identity.agentId
|
|
510
|
+
)
|
|
511
|
+
throw new Error('SDK runtime run identity mismatch');
|
|
512
|
+
if (
|
|
513
|
+
providerTextProtection &&
|
|
514
|
+
run.Graph.providerTextProtection !== providerTextProtection
|
|
515
|
+
)
|
|
516
|
+
throw new Error(
|
|
517
|
+
'SDK runtime omitted required provider protection'
|
|
518
|
+
);
|
|
519
|
+
if (hostModel && run.Graph.overrideModel !== hostModel)
|
|
520
|
+
throw new Error('SDK runtime omitted required host model');
|
|
521
|
+
if (profile) {
|
|
522
|
+
const context = run.Graph.agentContexts.get(identity.agentId);
|
|
523
|
+
const modelOptions = context?.clientOptions;
|
|
524
|
+
if (
|
|
525
|
+
context?.provider !== profile.provider ||
|
|
526
|
+
!modelOptions ||
|
|
527
|
+
!('model' in modelOptions) ||
|
|
528
|
+
modelOptions.model !== profile.name ||
|
|
529
|
+
(!('streaming' in modelOptions) ||
|
|
530
|
+
modelOptions.streaming !== false) !== profile.streaming
|
|
531
|
+
) {
|
|
532
|
+
throw new Error(
|
|
533
|
+
'SDK runtime omitted required builder model configuration'
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (hostTools) {
|
|
538
|
+
const names = new Set(
|
|
539
|
+
run.Graph.agentContexts
|
|
540
|
+
.get(identity.agentId)
|
|
541
|
+
?.toolDefinitions?.map((t) => t.name)
|
|
542
|
+
);
|
|
543
|
+
if (hostTools.toolDefinitions.some((t) => !names.has(t.name)))
|
|
544
|
+
throw new Error('SDK runtime omitted required host tools');
|
|
545
|
+
}
|
|
546
|
+
const callerOptions = {
|
|
547
|
+
...request.options,
|
|
548
|
+
...(modelLifecycle && { callbacks: [modelLifecycle.callback] }),
|
|
549
|
+
version: 'v2' as const,
|
|
550
|
+
signal: controller.signal,
|
|
551
|
+
};
|
|
552
|
+
run.returnContent = true;
|
|
553
|
+
const content =
|
|
554
|
+
request.operation === 'resume'
|
|
555
|
+
? await run.resume(
|
|
556
|
+
request.resume,
|
|
557
|
+
callerOptions,
|
|
558
|
+
undefined,
|
|
559
|
+
request.commandOptions
|
|
560
|
+
)
|
|
561
|
+
: await run.processStream(request.input, callerOptions);
|
|
562
|
+
outcome = run.getInterrupt() ? 'paused' : 'completed';
|
|
563
|
+
await sendSnapshot(
|
|
564
|
+
peer,
|
|
565
|
+
{
|
|
566
|
+
content,
|
|
567
|
+
messages: run.getRunMessages(),
|
|
568
|
+
state: {
|
|
569
|
+
getDiscoveredTools: run.getDiscoveredTools(),
|
|
570
|
+
getCalibrationRatio: run.getCalibrationRatio(),
|
|
571
|
+
getFadingTier: run.getFadingTier(),
|
|
572
|
+
getFadingTiers: run.getFadingTiers(),
|
|
573
|
+
getFadingTierResetAgentIds:
|
|
574
|
+
run.getFadingTierResetAgentIds(),
|
|
575
|
+
getResolvedInstructionOverhead:
|
|
576
|
+
run.getResolvedInstructionOverhead(),
|
|
577
|
+
getToolCount: run.getToolCount(),
|
|
578
|
+
getOutputTruncated: run.getOutputTruncated(),
|
|
579
|
+
getHandoffOutcome: run.getHandoffOutcome(),
|
|
580
|
+
},
|
|
581
|
+
interrupt: run.getInterrupt(),
|
|
582
|
+
haltReason: run.getHaltReason(),
|
|
583
|
+
runSteps: run.Graph.createRunStepResumeState(),
|
|
584
|
+
},
|
|
585
|
+
limits,
|
|
586
|
+
recovered
|
|
587
|
+
? (): Promise<void> => recovered.socket.flush()
|
|
588
|
+
: undefined
|
|
589
|
+
);
|
|
590
|
+
};
|
|
591
|
+
running = true;
|
|
592
|
+
void execute()
|
|
593
|
+
.catch((error) => {
|
|
594
|
+
try {
|
|
595
|
+
options.onError?.(error);
|
|
596
|
+
} catch {
|
|
597
|
+
/* Diagnostics must not prevent cleanup. */
|
|
598
|
+
}
|
|
599
|
+
try {
|
|
600
|
+
if (transport.readyState === WebSocket.OPEN)
|
|
601
|
+
peer.send({ type: 'error' });
|
|
602
|
+
} catch {
|
|
603
|
+
controller.abort();
|
|
604
|
+
} finally {
|
|
605
|
+
transport.close(1011);
|
|
606
|
+
}
|
|
607
|
+
})
|
|
608
|
+
.finally(async () => {
|
|
609
|
+
running = false;
|
|
610
|
+
if (recovered) {
|
|
611
|
+
try {
|
|
612
|
+
await recovered.complete(
|
|
613
|
+
controller.signal.aborted ? 'cancelled' : outcome
|
|
614
|
+
);
|
|
615
|
+
} catch {
|
|
616
|
+
recovered.dispose();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
if (transport.readyState === WebSocket.CLOSED)
|
|
620
|
+
active.delete(controller);
|
|
621
|
+
});
|
|
622
|
+
return { accepted: true };
|
|
623
|
+
},
|
|
624
|
+
});
|
|
625
|
+
recovered?.attach(ws, claim.recovery!.after);
|
|
626
|
+
});
|
|
627
|
+
})()
|
|
628
|
+
.catch(() => socket.destroy())
|
|
629
|
+
.finally(() => {
|
|
630
|
+
clearTimeout(deadline);
|
|
631
|
+
socket.removeListener('close', abortAdmission);
|
|
632
|
+
admitting.delete(socket);
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
return {
|
|
636
|
+
server,
|
|
637
|
+
close: async (): Promise<void> => {
|
|
638
|
+
closing = true;
|
|
639
|
+
recovery?.close();
|
|
640
|
+
for (const [socket, admission] of admitting) {
|
|
641
|
+
admission.abort();
|
|
642
|
+
socket.destroy();
|
|
643
|
+
}
|
|
644
|
+
for (const controller of active) controller.abort();
|
|
645
|
+
for (const socket of sockets.clients) socket.terminate();
|
|
646
|
+
await new Promise<void>((resolve) => sockets.close(() => resolve()));
|
|
647
|
+
if (server.listening)
|
|
648
|
+
await new Promise<void>((resolve, reject) =>
|
|
649
|
+
server.close((error) => (error ? reject(error) : resolve()))
|
|
650
|
+
);
|
|
651
|
+
},
|
|
652
|
+
};
|
|
653
|
+
}
|