@downcity/agent 1.1.237 → 1.1.258
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/README.md +21 -20
- package/bin/agent/{local/Agent.d.ts → Agent.d.ts} +37 -27
- package/bin/agent/Agent.d.ts.map +1 -0
- package/bin/agent/Agent.js +211 -0
- package/bin/agent/Agent.js.map +1 -0
- package/bin/agent/AgentContext.d.ts +64 -0
- package/bin/agent/AgentContext.d.ts.map +1 -0
- package/bin/agent/AgentContext.js +53 -0
- package/bin/agent/AgentContext.js.map +1 -0
- package/bin/agent/{local/AgentInstructions.d.ts → AgentInstructions.d.ts} +1 -6
- package/bin/agent/AgentInstructions.d.ts.map +1 -0
- package/bin/agent/{local/AgentInstructions.js → AgentInstructions.js} +1 -10
- package/bin/agent/AgentInstructions.js.map +1 -0
- package/bin/{model/CityModelAdapter.d.ts → agent/AgentModel.d.ts} +3 -3
- package/bin/agent/AgentModel.d.ts.map +1 -0
- package/bin/{model/CityModelAdapter.js → agent/AgentModel.js} +11 -4
- package/bin/agent/AgentModel.js.map +1 -0
- package/bin/agent/{local/services/AgentSessions.d.ts → AgentSessions.d.ts} +30 -40
- package/bin/agent/AgentSessions.d.ts.map +1 -0
- package/bin/agent/{local/services/AgentSessions.js → AgentSessions.js} +62 -79
- package/bin/agent/AgentSessions.js.map +1 -0
- package/bin/agent/AgentState.d.ts +64 -0
- package/bin/agent/AgentState.d.ts.map +1 -0
- package/bin/agent/AgentState.js +118 -0
- package/bin/agent/AgentState.js.map +1 -0
- package/bin/config/Paths.d.ts +0 -43
- package/bin/config/Paths.d.ts.map +1 -1
- package/bin/config/Paths.js +0 -79
- package/bin/config/Paths.js.map +1 -1
- package/bin/config/ProjectSetup.d.ts.map +1 -0
- package/bin/{agent/local → config}/ProjectSetup.js +1 -1
- package/bin/config/ProjectSetup.js.map +1 -0
- package/bin/executor/Executor.d.ts +20 -86
- package/bin/executor/Executor.d.ts.map +1 -1
- package/bin/executor/Executor.js +109 -260
- package/bin/executor/Executor.js.map +1 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +1 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts.map +1 -1
- package/bin/executor/composer/system/default/SystemDomain.d.ts +1 -1
- package/bin/executor/composer/system/default/SystemDomain.d.ts.map +1 -1
- package/bin/executor/composer/system/default/SystemDomain.js +4 -1
- package/bin/executor/composer/system/default/SystemDomain.js.map +1 -1
- package/bin/executor/composer/system/default/assets/core.prompt.d.ts +1 -1
- package/bin/executor/composer/system/default/assets/core.prompt.d.ts.map +1 -1
- package/bin/executor/composer/system/default/assets/core.prompt.js +1 -1
- package/bin/executor/composer/system/default/assets/core.prompt.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.d.ts +7 -13
- package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.js +81 -34
- package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +6 -12
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -1
- package/bin/executor/services/ExecutorRecoveryPolicy.js +20 -6
- package/bin/executor/services/ExecutorRecoveryPolicy.js.map +1 -1
- package/bin/executor/tools/file/ReadImageToolBridge.d.ts +21 -0
- package/bin/executor/tools/file/ReadImageToolBridge.d.ts.map +1 -0
- package/bin/executor/tools/file/ReadImageToolBridge.js +69 -0
- package/bin/executor/tools/file/ReadImageToolBridge.js.map +1 -0
- package/bin/executor/tools/plugin/PluginToolBridge.d.ts.map +1 -1
- package/bin/executor/tools/plugin/PluginToolBridge.js +6 -5
- package/bin/executor/tools/plugin/PluginToolBridge.js.map +1 -1
- package/bin/executor/tools/plugin/PluginToolDefinition.d.ts.map +1 -1
- package/bin/executor/tools/plugin/PluginToolDefinition.js +15 -2
- package/bin/executor/tools/plugin/PluginToolDefinition.js.map +1 -1
- package/bin/executor/types/SessionHistoryMeta.d.ts +0 -2
- package/bin/executor/types/SessionHistoryMeta.d.ts.map +1 -1
- package/bin/index.d.ts +17 -21
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +9 -11
- package/bin/index.js.map +1 -1
- package/bin/plugin/core/ActionScheduleExecutor.d.ts +1 -1
- package/bin/plugin/core/ActionScheduleExecutor.d.ts.map +1 -1
- package/bin/plugin/core/ActionScheduleRuntime.d.ts +1 -1
- package/bin/plugin/core/ActionScheduleRuntime.d.ts.map +1 -1
- package/bin/plugin/core/BasePlugin.d.ts +4 -3
- package/bin/plugin/core/BasePlugin.d.ts.map +1 -1
- package/bin/plugin/core/BasePlugin.js.map +1 -1
- package/bin/plugin/core/HookRegistry.d.ts +5 -8
- package/bin/plugin/core/HookRegistry.d.ts.map +1 -1
- package/bin/plugin/core/HookRegistry.js +12 -12
- package/bin/plugin/core/HookRegistry.js.map +1 -1
- package/bin/plugin/core/PluginActionFactory.d.ts +6 -3
- package/bin/plugin/core/PluginActionFactory.d.ts.map +1 -1
- package/bin/plugin/core/PluginActionFactory.js.map +1 -1
- package/bin/plugin/core/PluginActionRunner.d.ts +1 -1
- package/bin/plugin/core/PluginActionRunner.d.ts.map +1 -1
- package/bin/plugin/core/PluginCatalog.d.ts +1 -1
- package/bin/plugin/core/PluginCatalog.d.ts.map +1 -1
- package/bin/plugin/core/PluginHttpRoutes.d.ts +1 -1
- package/bin/plugin/core/PluginHttpRoutes.d.ts.map +1 -1
- package/bin/plugin/core/PluginLocalExecution.d.ts +4 -5
- package/bin/plugin/core/PluginLocalExecution.d.ts.map +1 -1
- package/bin/plugin/core/PluginLocalExecution.js +4 -12
- package/bin/plugin/core/PluginLocalExecution.js.map +1 -1
- package/bin/plugin/core/PluginRegistry.d.ts +16 -12
- package/bin/plugin/core/PluginRegistry.d.ts.map +1 -1
- package/bin/plugin/core/PluginRegistry.js +43 -22
- package/bin/plugin/core/PluginRegistry.js.map +1 -1
- package/bin/plugin/core/PluginStateController.d.ts +1 -1
- package/bin/plugin/core/PluginStateController.d.ts.map +1 -1
- package/bin/{agent/remote → remote}/RemoteAgent.d.ts +4 -4
- package/bin/remote/RemoteAgent.d.ts.map +1 -0
- package/bin/{agent/remote → remote}/RemoteAgent.js +3 -3
- package/bin/remote/RemoteAgent.js.map +1 -0
- package/bin/{agent/remote → remote}/RemoteSession.d.ts +9 -11
- package/bin/remote/RemoteSession.d.ts.map +1 -0
- package/bin/{agent/remote → remote}/RemoteSession.js +3 -33
- package/bin/remote/RemoteSession.js.map +1 -0
- package/bin/{agent/remote → remote}/RemoteTransport.d.ts +7 -9
- package/bin/remote/RemoteTransport.d.ts.map +1 -0
- package/bin/remote/RemoteTransport.js.map +1 -0
- package/bin/{agent/remote → remote}/TransportFactory.d.ts +1 -1
- package/bin/remote/TransportFactory.d.ts.map +1 -0
- package/bin/{agent/remote → remote}/TransportFactory.js +2 -2
- package/bin/remote/TransportFactory.js.map +1 -0
- package/bin/{agent/remote → remote}/transports/HttpRemoteAgentTransport.d.ts +8 -9
- package/bin/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -0
- package/bin/{agent/remote → remote}/transports/HttpRemoteAgentTransport.js +0 -13
- package/bin/remote/transports/HttpRemoteAgentTransport.js.map +1 -0
- package/bin/{rpc/Client.d.ts → remote/transports/RpcClient.d.ts} +15 -22
- package/bin/remote/transports/RpcClient.d.ts.map +1 -0
- package/bin/{rpc/Client.js → remote/transports/RpcClient.js} +2 -13
- package/bin/remote/transports/RpcClient.js.map +1 -0
- package/bin/{agent/remote → remote}/transports/RpcRemoteAgentTransport.d.ts +8 -9
- package/bin/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -0
- package/bin/{agent/remote → remote}/transports/RpcRemoteAgentTransport.js +1 -4
- package/bin/remote/transports/RpcRemoteAgentTransport.js.map +1 -0
- package/bin/session/DefaultSessionComposer.d.ts +18 -0
- package/bin/session/DefaultSessionComposer.d.ts.map +1 -0
- package/bin/session/DefaultSessionComposer.js +62 -0
- package/bin/session/DefaultSessionComposer.js.map +1 -0
- package/bin/session/Session.d.ts +44 -33
- package/bin/session/Session.d.ts.map +1 -1
- package/bin/session/Session.js +440 -274
- package/bin/session/Session.js.map +1 -1
- package/bin/session/SessionMessages.d.ts +117 -0
- package/bin/session/SessionMessages.d.ts.map +1 -0
- package/bin/session/{recorder/SessionRecorder.js → SessionMessages.js} +194 -284
- package/bin/session/SessionMessages.js.map +1 -0
- package/bin/session/SessionQueue.d.ts +34 -0
- package/bin/session/SessionQueue.d.ts.map +1 -0
- package/bin/session/SessionQueue.js +60 -0
- package/bin/session/SessionQueue.js.map +1 -0
- package/bin/session/SessionState.d.ts +70 -0
- package/bin/session/SessionState.d.ts.map +1 -0
- package/bin/session/SessionState.js +267 -0
- package/bin/session/SessionState.js.map +1 -0
- package/bin/session/SessionSystem.d.ts +15 -0
- package/bin/session/SessionSystem.d.ts.map +1 -0
- package/bin/session/SessionSystem.js +95 -0
- package/bin/session/SessionSystem.js.map +1 -0
- package/bin/session/SessionTurn.d.ts +84 -0
- package/bin/session/SessionTurn.d.ts.map +1 -0
- package/bin/session/SessionTurn.js +588 -0
- package/bin/session/SessionTurn.js.map +1 -0
- package/bin/session/approval/SessionApprovalBroker.d.ts +48 -0
- package/bin/session/approval/SessionApprovalBroker.d.ts.map +1 -0
- package/bin/session/approval/SessionApprovalBroker.js +126 -0
- package/bin/session/approval/SessionApprovalBroker.js.map +1 -0
- package/bin/session/browse/Browse.d.ts.map +1 -1
- package/bin/session/browse/Browse.js +1 -4
- package/bin/session/browse/Browse.js.map +1 -1
- package/bin/session/{recorder → messages}/JsonlSessionMessageStore.d.ts.map +1 -1
- package/bin/session/{recorder → messages}/JsonlSessionMessageStore.js.map +1 -1
- package/bin/session/messages/SessionAssistantMessageWriter.d.ts +62 -0
- package/bin/session/messages/SessionAssistantMessageWriter.d.ts.map +1 -0
- package/bin/session/messages/SessionAssistantMessageWriter.js +287 -0
- package/bin/session/messages/SessionAssistantMessageWriter.js.map +1 -0
- package/bin/session/messages/SessionJsonValue.d.ts +10 -0
- package/bin/session/messages/SessionJsonValue.d.ts.map +1 -0
- package/bin/session/messages/SessionJsonValue.js +23 -0
- package/bin/session/messages/SessionJsonValue.js.map +1 -0
- package/bin/session/{recorder → messages}/SessionMessageCodec.d.ts +4 -1
- package/bin/session/messages/SessionMessageCodec.d.ts.map +1 -0
- package/bin/session/{recorder → messages}/SessionMessageCodec.js +33 -19
- package/bin/session/messages/SessionMessageCodec.js.map +1 -0
- package/bin/session/messages/SessionMessageCompaction.d.ts +22 -0
- package/bin/session/messages/SessionMessageCompaction.d.ts.map +1 -0
- package/bin/session/{recorder/SessionRecorderCompaction.js → messages/SessionMessageCompaction.js} +19 -51
- package/bin/session/messages/SessionMessageCompaction.js.map +1 -0
- package/bin/session/runtime/SessionEventHub.d.ts +1 -8
- package/bin/session/runtime/SessionEventHub.d.ts.map +1 -1
- package/bin/session/runtime/SessionEventHub.js +1 -6
- package/bin/session/runtime/SessionEventHub.js.map +1 -1
- package/bin/session/storage/Metadata.d.ts +23 -5
- package/bin/session/storage/Metadata.d.ts.map +1 -1
- package/bin/session/storage/Metadata.js +32 -29
- package/bin/session/storage/Metadata.js.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.d.ts +5 -10
- package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.js +2 -4
- package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
- package/bin/types/agent/AgentOptions.d.ts +6 -35
- package/bin/types/agent/AgentOptions.d.ts.map +1 -1
- package/bin/types/agent/AgentState.d.ts +28 -0
- package/bin/types/agent/AgentState.d.ts.map +1 -0
- package/bin/types/agent/AgentState.js +9 -0
- package/bin/types/agent/AgentState.js.map +1 -0
- package/bin/types/agent/SessionActor.d.ts +0 -2
- package/bin/types/agent/SessionActor.d.ts.map +1 -1
- package/bin/types/agent/SessionTypes.d.ts +5 -18
- package/bin/types/agent/SessionTypes.d.ts.map +1 -1
- package/bin/types/executor/SessionRunContext.d.ts +8 -2
- package/bin/types/executor/SessionRunContext.d.ts.map +1 -1
- package/bin/types/executor/SessionRunContext.js +1 -1
- package/bin/types/executor/SessionToolExecutionContext.d.ts +20 -0
- package/bin/types/executor/SessionToolExecutionContext.d.ts.map +1 -0
- package/bin/types/executor/SessionToolExecutionContext.js +10 -0
- package/bin/types/executor/SessionToolExecutionContext.js.map +1 -0
- package/bin/types/plugin/PluginAction.d.ts +10 -1
- package/bin/types/plugin/PluginAction.d.ts.map +1 -1
- package/bin/types/plugin/PluginCommand.d.ts +3 -11
- package/bin/types/plugin/PluginCommand.d.ts.map +1 -1
- package/bin/types/plugin/PluginConfig.d.ts +12 -0
- package/bin/types/plugin/PluginConfig.d.ts.map +1 -0
- package/bin/types/plugin/PluginConfig.js +5 -0
- package/bin/types/plugin/PluginConfig.js.map +1 -0
- package/bin/types/plugin/PluginDefinition.d.ts +10 -3
- package/bin/types/plugin/PluginDefinition.d.ts.map +1 -1
- package/bin/types/plugin/PluginHttp.d.ts +1 -1
- package/bin/types/plugin/PluginHttp.d.ts.map +1 -1
- package/bin/types/plugin/PluginRunContext.d.ts +26 -0
- package/bin/types/plugin/PluginRunContext.d.ts.map +1 -0
- package/bin/types/plugin/PluginRunContext.js +10 -0
- package/bin/types/plugin/PluginRunContext.js.map +1 -0
- package/bin/types/plugin/PluginRuntime.d.ts +9 -3
- package/bin/types/plugin/PluginRuntime.d.ts.map +1 -1
- package/bin/types/plugin/PluginToolRuntime.d.ts +11 -0
- package/bin/types/plugin/PluginToolRuntime.d.ts.map +1 -1
- package/bin/types/rpc/RpcProtocol.d.ts +1 -13
- package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
- package/bin/types/session/SessionApproval.d.ts +4 -38
- package/bin/types/session/SessionApproval.d.ts.map +1 -1
- package/bin/types/session/SessionApprovalBroker.d.ts +17 -0
- package/bin/types/session/SessionApprovalBroker.d.ts.map +1 -0
- package/bin/types/session/SessionApprovalBroker.js +7 -0
- package/bin/types/session/SessionApprovalBroker.js.map +1 -0
- package/bin/types/session/SessionComposer.d.ts +99 -0
- package/bin/types/session/SessionComposer.d.ts.map +1 -0
- package/bin/types/session/SessionComposer.js +8 -0
- package/bin/types/session/SessionComposer.js.map +1 -0
- package/bin/types/session/SessionLocalState.d.ts +6 -6
- package/bin/types/session/SessionLocalState.d.ts.map +1 -1
- package/bin/types/session/SessionLocalState.js +1 -1
- package/bin/types/session/SessionMessage.d.ts +3 -2
- package/bin/types/session/SessionMessage.d.ts.map +1 -1
- package/bin/types/session/SessionMessages.d.ts +115 -0
- package/bin/types/session/SessionMessages.d.ts.map +1 -0
- package/bin/types/session/SessionMessages.js +7 -0
- package/bin/types/session/SessionMessages.js.map +1 -0
- package/bin/types/session/SessionMutation.d.ts +1 -10
- package/bin/types/session/SessionMutation.d.ts.map +1 -1
- package/bin/types/session/SessionMutation.js.map +1 -1
- package/bin/types/session/SessionOptions.d.ts +8 -23
- package/bin/types/session/SessionOptions.d.ts.map +1 -1
- package/bin/types/session/SessionPort.d.ts +72 -0
- package/bin/types/session/SessionPort.d.ts.map +1 -0
- package/bin/types/session/SessionPort.js +9 -0
- package/bin/types/session/SessionPort.js.map +1 -0
- package/bin/types/session/SessionQueue.d.ts +104 -0
- package/bin/types/session/SessionQueue.d.ts.map +1 -0
- package/bin/types/session/SessionQueue.js +7 -0
- package/bin/types/session/SessionQueue.js.map +1 -0
- package/bin/types/session/SessionState.d.ts +43 -0
- package/bin/types/session/SessionState.d.ts.map +1 -0
- package/bin/types/session/SessionState.js +7 -0
- package/bin/types/session/SessionState.js.map +1 -0
- package/bin/types/session/SessionSystem.d.ts +26 -0
- package/bin/types/session/SessionSystem.d.ts.map +1 -0
- package/bin/types/session/SessionSystem.js +7 -0
- package/bin/types/session/SessionSystem.js.map +1 -0
- package/bin/types/session/SessionTool.d.ts +23 -0
- package/bin/types/session/SessionTool.d.ts.map +1 -0
- package/bin/types/session/SessionTool.js +7 -0
- package/bin/types/session/SessionTool.js.map +1 -0
- package/bin/types/session/SessionTurn.d.ts +52 -0
- package/bin/types/session/SessionTurn.d.ts.map +1 -0
- package/bin/types/session/SessionTurn.js +7 -0
- package/bin/types/session/SessionTurn.js.map +1 -0
- package/package.json +3 -3
- package/scripts/agent-env.test.mjs +4 -18
- package/scripts/agent-ready.test.mjs +33 -2
- package/scripts/agent-session-maintenance.test.mjs +52 -0
- package/scripts/city-model-tool-loop.test.mjs +3 -4
- package/scripts/core-engine-context-compaction.test.mjs +5 -22
- package/scripts/image-plugin-job.test.mjs +4 -6
- package/scripts/plugin-tool-bridge.test.mjs +44 -36
- package/scripts/read-image-tool-bridge.test.mjs +71 -0
- package/scripts/remote-session-reconnect.test.mjs +1 -34
- package/scripts/session-compaction-prompts.test.mjs +1 -49
- package/scripts/session-composer.test.mjs +163 -0
- package/scripts/session-config-turn-boundary.test.mjs +2 -5
- package/scripts/session-list-title.test.mjs +3 -2
- package/scripts/session-messages-error.test.mjs +57 -0
- package/scripts/{session-recorder.test.mjs → session-messages.test.mjs} +131 -90
- package/scripts/session-queue.test.mjs +64 -0
- package/scripts/session-shell-approval.test.mjs +171 -0
- package/scripts/session-system-blocks.test.mjs +10 -10
- package/scripts/session-title-event.test.mjs +1 -0
- package/src/agent/Agent.ts +241 -0
- package/src/agent/AgentContext.ts +92 -0
- package/src/agent/{local/AgentInstructions.ts → AgentInstructions.ts} +0 -11
- package/src/{model/CityModelAdapter.ts → agent/AgentModel.ts} +10 -3
- package/src/agent/{local/services/AgentSessions.ts → AgentSessions.ts} +81 -110
- package/src/agent/AgentState.ts +151 -0
- package/src/config/Paths.ts +0 -91
- package/src/executor/Executor.ts +180 -386
- package/src/executor/README.md +29 -89
- package/src/executor/composer/system/default/DefaultSessionSystemComposer.ts +1 -1
- package/src/executor/composer/system/default/SystemDomain.ts +4 -2
- package/src/executor/composer/system/default/assets/core.prompt.ts +1 -1
- package/src/executor/composer/system/default/assets/core.prompt.ts.txt +15 -2
- package/src/executor/core-engine/CoreEngineRunner.ts +95 -54
- package/src/executor/services/ExecutorRecoveryPolicy.ts +24 -19
- package/src/executor/tools/file/ReadImageToolBridge.ts +79 -0
- package/src/executor/tools/plugin/PluginToolBridge.ts +6 -8
- package/src/executor/tools/plugin/PluginToolDefinition.ts +23 -3
- package/src/executor/types/SessionHistoryMeta.ts +0 -2
- package/src/index.ts +34 -53
- package/src/plugin/core/ActionScheduleExecutor.ts +1 -1
- package/src/plugin/core/ActionScheduleRuntime.ts +1 -1
- package/src/plugin/core/BasePlugin.ts +7 -3
- package/src/plugin/core/HookRegistry.ts +15 -18
- package/src/plugin/core/PluginActionFactory.ts +9 -3
- package/src/plugin/core/PluginActionRunner.ts +1 -1
- package/src/plugin/core/PluginCatalog.ts +1 -1
- package/src/plugin/core/PluginHttpRoutes.ts +1 -1
- package/src/plugin/core/PluginLocalExecution.ts +11 -24
- package/src/plugin/core/PluginRegistry.ts +55 -32
- package/src/plugin/core/PluginStateController.ts +1 -1
- package/src/{agent/remote → remote}/RemoteAgent.ts +4 -4
- package/src/{agent/remote → remote}/RemoteSession.ts +2 -35
- package/src/{agent/remote → remote}/RemoteTransport.ts +0 -3
- package/src/{agent/remote → remote}/TransportFactory.ts +3 -3
- package/src/{agent/remote → remote}/transports/HttpRemoteAgentTransport.ts +1 -23
- package/src/{rpc/Client.ts → remote/transports/RpcClient.ts} +1 -19
- package/src/{agent/remote → remote}/transports/RpcRemoteAgentTransport.ts +2 -10
- package/src/session/DefaultSessionComposer.ts +79 -0
- package/src/session/Session.ts +496 -332
- package/src/session/{recorder/SessionRecorder.ts → SessionMessages.ts} +254 -428
- package/src/session/SessionQueue.ts +86 -0
- package/src/session/SessionState.ts +322 -0
- package/src/session/SessionSystem.ts +117 -0
- package/src/session/SessionTurn.ts +684 -0
- package/src/session/approval/SessionApprovalBroker.ts +164 -0
- package/src/session/browse/Browse.ts +1 -4
- package/src/session/messages/SessionAssistantMessageWriter.ts +345 -0
- package/src/session/messages/SessionJsonValue.ts +25 -0
- package/src/session/{recorder → messages}/SessionMessageCodec.ts +38 -20
- package/src/session/{recorder/SessionRecorderCompaction.ts → messages/SessionMessageCompaction.ts} +30 -69
- package/src/session/runtime/SessionEventHub.ts +1 -14
- package/src/session/storage/Metadata.ts +60 -34
- package/src/session/storage/RuntimeSessionPort.ts +7 -14
- package/src/types/agent/AgentOptions.ts +6 -38
- package/src/types/agent/AgentState.ts +33 -0
- package/src/types/agent/SessionActor.ts +0 -3
- package/src/types/agent/SessionTypes.ts +5 -18
- package/src/types/executor/SessionRunContext.ts +10 -2
- package/src/types/executor/SessionToolExecutionContext.ts +21 -0
- package/src/types/plugin/PluginAction.ts +10 -1
- package/src/types/plugin/PluginCommand.ts +3 -14
- package/src/types/plugin/PluginConfig.ts +13 -0
- package/src/types/plugin/PluginDefinition.ts +13 -6
- package/src/types/plugin/PluginHttp.ts +1 -1
- package/src/types/plugin/PluginRunContext.ts +31 -0
- package/src/types/plugin/PluginRuntime.ts +13 -6
- package/src/types/plugin/PluginToolRuntime.ts +13 -1
- package/src/types/rpc/RpcProtocol.ts +0 -14
- package/src/types/session/SessionApproval.ts +4 -43
- package/src/types/session/SessionApprovalBroker.ts +18 -0
- package/src/types/session/SessionComposer.ts +114 -0
- package/src/types/session/SessionLocalState.ts +6 -6
- package/src/types/session/SessionMessage.ts +3 -2
- package/src/types/session/SessionMessages.ts +127 -0
- package/src/types/session/SessionMutation.ts +0 -11
- package/src/types/session/SessionOptions.ts +8 -40
- package/src/types/session/SessionPort.ts +97 -0
- package/src/types/session/SessionQueue.ts +122 -0
- package/src/types/session/SessionState.ts +47 -0
- package/src/types/session/SessionSystem.ts +27 -0
- package/src/types/session/SessionTool.ts +25 -0
- package/src/types/session/SessionTurn.ts +56 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/agent/local/Agent.d.ts.map +0 -1
- package/bin/agent/local/Agent.js +0 -216
- package/bin/agent/local/Agent.js.map +0 -1
- package/bin/agent/local/AgentInstructions.d.ts.map +0 -1
- package/bin/agent/local/AgentInstructions.js.map +0 -1
- package/bin/agent/local/AgentPluginFactory.d.ts +0 -26
- package/bin/agent/local/AgentPluginFactory.d.ts.map +0 -1
- package/bin/agent/local/AgentPluginFactory.js +0 -33
- package/bin/agent/local/AgentPluginFactory.js.map +0 -1
- package/bin/agent/local/AgentRuntimeAssembly.d.ts +0 -10
- package/bin/agent/local/AgentRuntimeAssembly.d.ts.map +0 -1
- package/bin/agent/local/AgentRuntimeAssembly.js +0 -43
- package/bin/agent/local/AgentRuntimeAssembly.js.map +0 -1
- package/bin/agent/local/ProjectSetup.d.ts.map +0 -1
- package/bin/agent/local/ProjectSetup.js.map +0 -1
- package/bin/agent/local/services/AgentAssemblyService.d.ts +0 -105
- package/bin/agent/local/services/AgentAssemblyService.d.ts.map +0 -1
- package/bin/agent/local/services/AgentAssemblyService.js +0 -184
- package/bin/agent/local/services/AgentAssemblyService.js.map +0 -1
- package/bin/agent/local/services/AgentBackgroundService.d.ts +0 -55
- package/bin/agent/local/services/AgentBackgroundService.d.ts.map +0 -1
- package/bin/agent/local/services/AgentBackgroundService.js +0 -85
- package/bin/agent/local/services/AgentBackgroundService.js.map +0 -1
- package/bin/agent/local/services/AgentSessions.d.ts.map +0 -1
- package/bin/agent/local/services/AgentSessions.js.map +0 -1
- package/bin/agent/remote/RemoteAgent.d.ts.map +0 -1
- package/bin/agent/remote/RemoteAgent.js.map +0 -1
- package/bin/agent/remote/RemoteSession.d.ts.map +0 -1
- package/bin/agent/remote/RemoteSession.js.map +0 -1
- package/bin/agent/remote/RemoteTransport.d.ts.map +0 -1
- package/bin/agent/remote/RemoteTransport.js.map +0 -1
- package/bin/agent/remote/TransportFactory.d.ts.map +0 -1
- package/bin/agent/remote/TransportFactory.js.map +0 -1
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts.map +0 -1
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.js.map +0 -1
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts.map +0 -1
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.js.map +0 -1
- package/bin/executor/SessionRunScope.d.ts +0 -68
- package/bin/executor/SessionRunScope.d.ts.map +0 -1
- package/bin/executor/SessionRunScope.js +0 -96
- package/bin/executor/SessionRunScope.js.map +0 -1
- package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts +0 -74
- package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts.map +0 -1
- package/bin/executor/composer/compaction/SessionCompactionComposer.js +0 -9
- package/bin/executor/composer/compaction/SessionCompactionComposer.js.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.d.ts +0 -27
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.d.ts.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.js +0 -39
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.js.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts +0 -44
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js +0 -218
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts +0 -27
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js +0 -181
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js.map +0 -1
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts +0 -50
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts.map +0 -1
- package/bin/executor/composer/context/LocalSessionContextComposer.js +0 -108
- package/bin/executor/composer/context/LocalSessionContextComposer.js.map +0 -1
- package/bin/executor/composer/context/SessionContextComposer.d.ts +0 -67
- package/bin/executor/composer/context/SessionContextComposer.d.ts.map +0 -1
- package/bin/executor/composer/context/SessionContextComposer.js +0 -9
- package/bin/executor/composer/context/SessionContextComposer.js.map +0 -1
- package/bin/executor/composer/history/SessionHistoryComposer.d.ts +0 -54
- package/bin/executor/composer/history/SessionHistoryComposer.d.ts.map +0 -1
- package/bin/executor/composer/history/SessionHistoryComposer.js +0 -10
- package/bin/executor/composer/history/SessionHistoryComposer.js.map +0 -1
- package/bin/executor/composer/history/SessionHistoryWriter.d.ts +0 -46
- package/bin/executor/composer/history/SessionHistoryWriter.d.ts.map +0 -1
- package/bin/executor/composer/history/SessionHistoryWriter.js +0 -65
- package/bin/executor/composer/history/SessionHistoryWriter.js.map +0 -1
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts +0 -35
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts.map +0 -1
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js +0 -105
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js.map +0 -1
- package/bin/executor/messages/AssistantResultPersistence.d.ts +0 -66
- package/bin/executor/messages/AssistantResultPersistence.d.ts.map +0 -1
- package/bin/executor/messages/AssistantResultPersistence.js +0 -44
- package/bin/executor/messages/AssistantResultPersistence.js.map +0 -1
- package/bin/executor/services/ExecutorInflightService.d.ts +0 -34
- package/bin/executor/services/ExecutorInflightService.d.ts.map +0 -1
- package/bin/executor/services/ExecutorInflightService.js +0 -73
- package/bin/executor/services/ExecutorInflightService.js.map +0 -1
- package/bin/executor/store/history/SessionHistoryStore.d.ts +0 -140
- package/bin/executor/store/history/SessionHistoryStore.d.ts.map +0 -1
- package/bin/executor/store/history/SessionHistoryStore.js +0 -10
- package/bin/executor/store/history/SessionHistoryStore.js.map +0 -1
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts +0 -116
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts.map +0 -1
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js +0 -709
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js.map +0 -1
- package/bin/executor/types/SessionHistoryPaths.d.ts +0 -34
- package/bin/executor/types/SessionHistoryPaths.d.ts.map +0 -1
- package/bin/executor/types/SessionHistoryPaths.js +0 -2
- package/bin/executor/types/SessionHistoryPaths.js.map +0 -1
- package/bin/model/CityModelAdapter.d.ts.map +0 -1
- package/bin/model/CityModelAdapter.js.map +0 -1
- package/bin/rpc/Client.d.ts.map +0 -1
- package/bin/rpc/Client.js.map +0 -1
- package/bin/session/SessionSystemBuilder.d.ts +0 -107
- package/bin/session/SessionSystemBuilder.d.ts.map +0 -1
- package/bin/session/SessionSystemBuilder.js +0 -148
- package/bin/session/SessionSystemBuilder.js.map +0 -1
- package/bin/session/recorder/SessionMessageCodec.d.ts.map +0 -1
- package/bin/session/recorder/SessionMessageCodec.js.map +0 -1
- package/bin/session/recorder/SessionRecorder.d.ts +0 -217
- package/bin/session/recorder/SessionRecorder.d.ts.map +0 -1
- package/bin/session/recorder/SessionRecorder.js.map +0 -1
- package/bin/session/recorder/SessionRecorderCompaction.d.ts +0 -18
- package/bin/session/recorder/SessionRecorderCompaction.d.ts.map +0 -1
- package/bin/session/recorder/SessionRecorderCompaction.js.map +0 -1
- package/bin/session/recorder/SessionRecorderHistoryStore.d.ts +0 -66
- package/bin/session/recorder/SessionRecorderHistoryStore.d.ts.map +0 -1
- package/bin/session/recorder/SessionRecorderHistoryStore.js +0 -190
- package/bin/session/recorder/SessionRecorderHistoryStore.js.map +0 -1
- package/bin/session/runtime/SessionPromptRuntime.d.ts +0 -112
- package/bin/session/runtime/SessionPromptRuntime.d.ts.map +0 -1
- package/bin/session/runtime/SessionPromptRuntime.js +0 -357
- package/bin/session/runtime/SessionPromptRuntime.js.map +0 -1
- package/bin/session/services/SessionStateService.d.ts +0 -187
- package/bin/session/services/SessionStateService.d.ts.map +0 -1
- package/bin/session/services/SessionStateService.js +0 -467
- package/bin/session/services/SessionStateService.js.map +0 -1
- package/bin/session/services/SessionTurnService.d.ts +0 -90
- package/bin/session/services/SessionTurnService.d.ts.map +0 -1
- package/bin/session/services/SessionTurnService.js +0 -293
- package/bin/session/services/SessionTurnService.js.map +0 -1
- package/bin/session/services/SessionViewService.d.ts +0 -114
- package/bin/session/services/SessionViewService.d.ts.map +0 -1
- package/bin/session/services/SessionViewService.js +0 -204
- package/bin/session/services/SessionViewService.js.map +0 -1
- package/bin/session/storage/Persistence.d.ts +0 -73
- package/bin/session/storage/Persistence.d.ts.map +0 -1
- package/bin/session/storage/Persistence.js +0 -63
- package/bin/session/storage/Persistence.js.map +0 -1
- package/bin/types/agent/AgentRuntimeAssembly.d.ts +0 -72
- package/bin/types/agent/AgentRuntimeAssembly.d.ts.map +0 -1
- package/bin/types/agent/AgentRuntimeAssembly.js +0 -10
- package/bin/types/agent/AgentRuntimeAssembly.js.map +0 -1
- package/bin/types/runtime/agent/AgentContext.d.ts +0 -306
- package/bin/types/runtime/agent/AgentContext.d.ts.map +0 -1
- package/bin/types/runtime/agent/AgentContext.js +0 -128
- package/bin/types/runtime/agent/AgentContext.js.map +0 -1
- package/bin/types/session/SessionComposerOptions.d.ts +0 -90
- package/bin/types/session/SessionComposerOptions.d.ts.map +0 -1
- package/bin/types/session/SessionComposerOptions.js +0 -10
- package/bin/types/session/SessionComposerOptions.js.map +0 -1
- package/bin/types/session/SessionQueueCommand.d.ts +0 -68
- package/bin/types/session/SessionQueueCommand.d.ts.map +0 -1
- package/bin/types/session/SessionQueueCommand.js +0 -10
- package/bin/types/session/SessionQueueCommand.js.map +0 -1
- package/scripts/session-action-message.test.mjs +0 -410
- package/scripts/session-compaction-context-window.test.mjs +0 -96
- package/scripts/session-history-inflight.test.mjs +0 -146
- package/scripts/session-history-writer-error.test.mjs +0 -57
- package/scripts/session-prompt-runtime.test.mjs +0 -390
- package/src/agent/local/Agent.ts +0 -250
- package/src/agent/local/AgentPluginFactory.ts +0 -51
- package/src/agent/local/AgentRuntimeAssembly.ts +0 -65
- package/src/agent/local/services/AgentAssemblyService.ts +0 -307
- package/src/agent/local/services/AgentBackgroundService.ts +0 -115
- package/src/executor/SessionRunScope.ts +0 -122
- package/src/executor/composer/compaction/SessionCompactionComposer.ts +0 -84
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionComposer.ts +0 -51
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.ts +0 -294
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.ts +0 -197
- package/src/executor/composer/context/LocalSessionContextComposer.ts +0 -175
- package/src/executor/composer/context/SessionContextComposer.ts +0 -83
- package/src/executor/composer/history/SessionHistoryComposer.ts +0 -62
- package/src/executor/composer/history/SessionHistoryWriter.ts +0 -100
- package/src/executor/composer/history/jsonl/JsonlSessionHistoryComposer.ts +0 -133
- package/src/executor/messages/AssistantResultPersistence.ts +0 -106
- package/src/executor/services/ExecutorInflightService.ts +0 -95
- package/src/executor/store/history/SessionHistoryStore.ts +0 -172
- package/src/executor/store/history/jsonl/JsonlSessionHistoryStore.ts +0 -879
- package/src/executor/types/SessionHistoryPaths.ts +0 -38
- package/src/session/SessionSystemBuilder.ts +0 -262
- package/src/session/recorder/SessionRecorderHistoryStore.ts +0 -256
- package/src/session/runtime/SessionPromptRuntime.ts +0 -518
- package/src/session/services/SessionStateService.ts +0 -628
- package/src/session/services/SessionTurnService.ts +0 -368
- package/src/session/services/SessionViewService.ts +0 -335
- package/src/session/storage/Persistence.ts +0 -137
- package/src/types/agent/AgentRuntimeAssembly.ts +0 -74
- package/src/types/runtime/agent/AgentContext.ts +0 -387
- package/src/types/session/SessionComposerOptions.ts +0 -107
- package/src/types/session/SessionQueueCommand.ts +0 -78
- /package/bin/{agent/local → config}/ProjectSetup.d.ts +0 -0
- /package/bin/{agent/remote → remote}/RemoteTransport.js +0 -0
- /package/bin/session/{recorder → messages}/JsonlSessionMessageStore.d.ts +0 -0
- /package/bin/session/{recorder → messages}/JsonlSessionMessageStore.js +0 -0
- /package/src/{agent/local → config}/ProjectSetup.ts +0 -0
- /package/src/session/{recorder → messages}/JsonlSessionMessageStore.ts +0 -0
package/bin/session/Session.js
CHANGED
|
@@ -4,381 +4,448 @@
|
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - 面向 `new Agent(...)` 的本地会话使用场景。
|
|
6
6
|
* - 对外保留稳定 Session facade,把状态、turn、view 逻辑下沉到独立 service。
|
|
7
|
-
* - 内部使用 `
|
|
7
|
+
* - 内部使用 `SessionMessages` 统一管理 Active、Segment 与流式 Assistant 草稿。
|
|
8
8
|
*/
|
|
9
9
|
import { Executor } from "../executor/Executor.js";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import { SessionRecorder } from "../session/recorder/SessionRecorder.js";
|
|
10
|
+
import { normalizeAgentModel, read_agent_model_context_window, } from "../agent/AgentModel.js";
|
|
11
|
+
import { JsonlSessionMessageStore } from "../session/messages/JsonlSessionMessageStore.js";
|
|
12
|
+
import { SessionMessages } from "../session/SessionMessages.js";
|
|
14
13
|
import { getSdkAgentSessionAssistantMessagePath, getSdkAgentSessionDirPath, } from "../session/storage/Paths.js";
|
|
15
14
|
import { resolveSystemTimezone } from "../session/storage/Metadata.js";
|
|
16
15
|
import { createRuntimeSessionPort } from "../session/storage/RuntimeSessionPort.js";
|
|
17
|
-
import { SessionSystemBuilder } from "../session/SessionSystemBuilder.js";
|
|
18
16
|
import { SessionEventHub } from "../session/runtime/SessionEventHub.js";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
17
|
+
import { SessionState } from "../session/SessionState.js";
|
|
18
|
+
import { SessionTurn } from "../session/SessionTurn.js";
|
|
19
|
+
import { SessionApprovalBroker } from "../session/approval/SessionApprovalBroker.js";
|
|
20
|
+
import { DefaultSessionComposer } from "../session/DefaultSessionComposer.js";
|
|
21
|
+
import { generateId } from "../utils/Id.js";
|
|
22
|
+
import { nanoid } from "nanoid";
|
|
23
|
+
import { buildSessionInfo } from "../session/browse/Browse.js";
|
|
24
|
+
import { ensureSessionTitle } from "../session/SessionTitle.js";
|
|
25
|
+
import { readSessionMetadata } from "../session/storage/Metadata.js";
|
|
26
|
+
import { to_executor_history } from "../session/messages/SessionMessageCodec.js";
|
|
22
27
|
/**
|
|
23
28
|
* SDK 本地 Session。
|
|
24
29
|
*/
|
|
25
30
|
export class Session {
|
|
26
31
|
id;
|
|
27
32
|
agentId;
|
|
28
|
-
|
|
33
|
+
project_root;
|
|
29
34
|
tools;
|
|
30
35
|
logger;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
messageStore;
|
|
38
|
-
recorder;
|
|
39
|
-
historyComposer;
|
|
36
|
+
get_instruction_system_blocks;
|
|
37
|
+
get_managed_plugin_system_blocks;
|
|
38
|
+
ensure_configured_hook;
|
|
39
|
+
composer;
|
|
40
|
+
message_store;
|
|
41
|
+
session_messages;
|
|
40
42
|
executor;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
localState;
|
|
47
|
-
getAgentEnv;
|
|
43
|
+
events;
|
|
44
|
+
approval_broker;
|
|
45
|
+
local_state;
|
|
46
|
+
get_agent_env;
|
|
47
|
+
get_agent_model;
|
|
48
48
|
get_agent_plugins;
|
|
49
49
|
effective_instruction_system_blocks;
|
|
50
50
|
effective_agent_env;
|
|
51
51
|
effective_agent_plugins;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
runtimePort = null;
|
|
52
|
+
state;
|
|
53
|
+
session_turn;
|
|
54
|
+
runtime_port = null;
|
|
56
55
|
constructor(options) {
|
|
57
56
|
this.id = String(options.sessionId || "").trim();
|
|
58
57
|
this.agentId = String(options.agentId || "").trim();
|
|
59
|
-
this.
|
|
58
|
+
this.project_root = String(options.projectRoot || "").trim();
|
|
60
59
|
this.tools = options.tools;
|
|
61
60
|
this.logger = options.logger;
|
|
62
|
-
this.
|
|
63
|
-
this.
|
|
61
|
+
this.get_instruction_system_blocks = options.getInstructionSystemBlocks;
|
|
62
|
+
this.get_agent_env = options.getAgentEnv;
|
|
63
|
+
this.get_agent_model = options.getAgentModel;
|
|
64
64
|
this.get_agent_plugins = options.get_agent_plugins;
|
|
65
65
|
this.effective_instruction_system_blocks = options
|
|
66
66
|
.getInstructionSystemBlocks()
|
|
67
67
|
.map((block) => ({ ...block }));
|
|
68
68
|
this.effective_agent_env = { ...options.getAgentEnv() };
|
|
69
69
|
this.effective_agent_plugins = options.get_agent_plugins();
|
|
70
|
-
this.
|
|
71
|
-
this.
|
|
72
|
-
this.
|
|
73
|
-
this.composers = options.composers;
|
|
74
|
-
this.list_approvals = options.list_approvals;
|
|
75
|
-
this.get_approval_mode = options.get_approval_mode;
|
|
76
|
-
this.set_approval_mode_hook = options.set_approval_mode;
|
|
77
|
-
this.resolve_approval_hook = options.resolve_approval;
|
|
70
|
+
this.get_managed_plugin_system_blocks = options.getManagedPluginSystemBlocks;
|
|
71
|
+
this.ensure_configured_hook = options.ensureConfigured;
|
|
72
|
+
this.composer = options.composer || new DefaultSessionComposer();
|
|
78
73
|
if (!this.id) {
|
|
79
74
|
throw new Error("Session requires a non-empty sessionId");
|
|
80
75
|
}
|
|
81
76
|
if (!this.agentId) {
|
|
82
77
|
throw new Error("Session requires a non-empty agentId");
|
|
83
78
|
}
|
|
84
|
-
if (!this.
|
|
85
|
-
throw new Error("Session requires a non-empty
|
|
79
|
+
if (!this.project_root) {
|
|
80
|
+
throw new Error("Session requires a non-empty project_root");
|
|
86
81
|
}
|
|
87
|
-
this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const approval_id = require_approval_id(mutation);
|
|
91
|
-
return await this.resolve_approval({ approval_id, decision });
|
|
92
|
-
},
|
|
93
|
-
}),
|
|
94
|
-
});
|
|
95
|
-
this.messageStore = this.create_message_store();
|
|
96
|
-
this.recorder = new SessionRecorder({
|
|
82
|
+
this.events = new SessionEventHub();
|
|
83
|
+
this.message_store = this.create_message_store();
|
|
84
|
+
this.session_messages = new SessionMessages({
|
|
97
85
|
session_id: this.id,
|
|
98
|
-
store: this.
|
|
86
|
+
store: this.message_store,
|
|
99
87
|
publish: (mutation) => {
|
|
100
|
-
this.
|
|
88
|
+
this.events.publish(mutation);
|
|
101
89
|
},
|
|
102
90
|
});
|
|
103
|
-
this.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
this.executor = this.create_executor(system_composer, context_composer, compaction_composer);
|
|
111
|
-
this.stateService = new SessionStateService({
|
|
91
|
+
this.approval_broker = new SessionApprovalBroker({
|
|
92
|
+
session_id: this.id,
|
|
93
|
+
messages: this.session_messages,
|
|
94
|
+
});
|
|
95
|
+
this.local_state = this.create_local_state();
|
|
96
|
+
this.executor = this.create_executor();
|
|
97
|
+
this.state = new SessionState({
|
|
112
98
|
agent_id: this.agentId,
|
|
113
|
-
project_root: this.
|
|
99
|
+
project_root: this.project_root,
|
|
114
100
|
session_id: this.id,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
executor: this.executor,
|
|
118
|
-
state: this.localState,
|
|
101
|
+
messages: this.session_messages,
|
|
102
|
+
state: this.local_state,
|
|
119
103
|
logger: this.logger,
|
|
120
|
-
ensure_configured_hook: this.
|
|
104
|
+
ensure_configured_hook: this.ensure_configured_hook
|
|
121
105
|
? async () => {
|
|
122
|
-
await this.
|
|
106
|
+
await this.ensure_configured_hook?.(this);
|
|
123
107
|
}
|
|
124
108
|
: undefined,
|
|
125
|
-
|
|
109
|
+
get_model: () => this.get_model(),
|
|
126
110
|
publish_event: (event) => {
|
|
127
|
-
this.
|
|
111
|
+
this.events.publish(event);
|
|
128
112
|
},
|
|
129
113
|
});
|
|
130
|
-
this.
|
|
114
|
+
this.session_turn = new SessionTurn({
|
|
131
115
|
session_id: this.id,
|
|
132
|
-
project_root: this.
|
|
116
|
+
project_root: this.project_root,
|
|
133
117
|
executor: this.executor,
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
agent_id: this.agentId,
|
|
140
|
-
project_root: this.projectRoot,
|
|
141
|
-
session_id: this.id,
|
|
142
|
-
history_store: this.historyStore,
|
|
143
|
-
recorder: this.recorder,
|
|
144
|
-
state_service: this.stateService,
|
|
145
|
-
logger: this.logger,
|
|
146
|
-
is_executing: () => this.isExecuting(),
|
|
147
|
-
get_instruction_system_blocks: () => this.effective_instruction_system_blocks.map((block) => ({ ...block })),
|
|
148
|
-
get_managed_plugin_system_blocks: this.getManagedPluginSystemBlocks,
|
|
149
|
-
get_plugin_system_blocks: async () => await this.effective_agent_plugins.systemBlocks(),
|
|
150
|
-
...(this.composers?.systemComposer
|
|
151
|
-
? { custom_system_composer: system_composer }
|
|
152
|
-
: {}),
|
|
153
|
-
create_fork_session: async (session_id) => {
|
|
154
|
-
const session = this.create_fork_session(session_id);
|
|
155
|
-
await session.initialize();
|
|
156
|
-
return {
|
|
157
|
-
session,
|
|
158
|
-
history_store: session.historyStore,
|
|
159
|
-
recorder: session.recorder,
|
|
160
|
-
state_service: session.stateService,
|
|
161
|
-
};
|
|
162
|
-
},
|
|
118
|
+
state: this.state,
|
|
119
|
+
events: this.events,
|
|
120
|
+
messages: this.session_messages,
|
|
121
|
+
approvals: this.approval_broker,
|
|
122
|
+
apply_command: async (command, turn_id) => await this.apply_queue_command(command, turn_id),
|
|
163
123
|
});
|
|
164
124
|
}
|
|
165
125
|
/**
|
|
166
126
|
* 初始化当前 session。
|
|
167
127
|
*/
|
|
168
128
|
async initialize() {
|
|
169
|
-
await this.
|
|
170
|
-
await this.
|
|
129
|
+
await this.session_messages.initialize();
|
|
130
|
+
await this.state.initialize();
|
|
171
131
|
return this;
|
|
172
132
|
}
|
|
173
133
|
/**
|
|
174
134
|
* 读取当前 session 配置快照。
|
|
175
135
|
*/
|
|
176
136
|
get config() {
|
|
177
|
-
return this.
|
|
137
|
+
return this.state.get_config();
|
|
178
138
|
}
|
|
179
139
|
/**
|
|
180
140
|
* 写入当前 session 默认配置。
|
|
181
141
|
*/
|
|
182
142
|
async set(input) {
|
|
183
|
-
const configured = await this.
|
|
143
|
+
const configured = await this.state.set(input);
|
|
184
144
|
if (configured.command) {
|
|
185
|
-
this.
|
|
145
|
+
this.session_turn.enqueue_command(configured.command);
|
|
186
146
|
}
|
|
187
147
|
}
|
|
188
148
|
/**
|
|
189
149
|
* 追加一条新的 Session prompt。
|
|
190
150
|
*/
|
|
191
151
|
async prompt(input) {
|
|
192
|
-
return await this.
|
|
152
|
+
return await this.session_turn.prompt(input);
|
|
193
153
|
}
|
|
194
154
|
/**
|
|
195
155
|
* 停止当前 turn,并取消尚未被吸收的排队 prompt。
|
|
196
156
|
*/
|
|
197
157
|
async stop() {
|
|
198
|
-
|
|
158
|
+
await this.approval_broker.expire_all();
|
|
159
|
+
return this.session_turn.stop();
|
|
199
160
|
}
|
|
200
161
|
/**
|
|
201
162
|
* 把一次显式历史压缩加入当前 Session 的有序输入队列。
|
|
202
163
|
*/
|
|
203
164
|
async compact() {
|
|
204
|
-
await this.
|
|
165
|
+
await this.session_turn.compact();
|
|
205
166
|
}
|
|
206
167
|
/**
|
|
207
168
|
* 把 Agent configured state command 加入当前 Session 的统一输入队列。
|
|
208
169
|
*/
|
|
209
170
|
enqueue_agent_command(command) {
|
|
210
|
-
|
|
211
|
-
|
|
171
|
+
if (command.type === "instruction") {
|
|
172
|
+
this.session_turn.enqueue_command({
|
|
173
|
+
type: "agent_instruction",
|
|
174
|
+
command_id: command.command_id,
|
|
175
|
+
instruction_blocks: command.instruction_blocks,
|
|
176
|
+
});
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (command.type === "env") {
|
|
180
|
+
this.session_turn.enqueue_command({
|
|
181
|
+
type: "agent_env",
|
|
182
|
+
command_id: command.command_id,
|
|
183
|
+
env: command.env,
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.session_turn.enqueue_command({
|
|
188
|
+
type: "agent_plugins",
|
|
212
189
|
command_id: command.command_id,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
if (command.type === "env") {
|
|
228
|
-
this.effective_agent_env = { ...command.env };
|
|
229
|
-
await this.stateService.emit_config_action_event({
|
|
230
|
-
id: `agent-env:${this.id}:${command.command_id}`,
|
|
231
|
-
title: "Agent environment updated",
|
|
232
|
-
state: "completed",
|
|
233
|
-
turnId: turn_id,
|
|
234
|
-
});
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
this.effective_agent_plugins = command.plugins;
|
|
238
|
-
await this.stateService.emit_config_action_event({
|
|
239
|
-
id: `agent-plugins:${this.id}:${command.command_id}`,
|
|
240
|
-
title: command.title,
|
|
190
|
+
title: command.title,
|
|
191
|
+
plugins: command.plugins,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/** 在 Step 检查点提交明确的 Session/Agent 状态命令。 */
|
|
195
|
+
async apply_queue_command(command, turn_id) {
|
|
196
|
+
if (command.type === "session_model") {
|
|
197
|
+
await this.state.apply_model_command(command);
|
|
198
|
+
if (command.action_id && command.action_title) {
|
|
199
|
+
await this.emit_config_action_event({
|
|
200
|
+
id: command.action_id,
|
|
201
|
+
title: command.action_title,
|
|
241
202
|
state: "completed",
|
|
242
203
|
turnId: turn_id,
|
|
243
204
|
});
|
|
244
|
-
}
|
|
205
|
+
}
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (command.type === "agent_instruction") {
|
|
209
|
+
this.effective_instruction_system_blocks = command.instruction_blocks.map((block) => ({ ...block }));
|
|
210
|
+
await this.emit_config_action_event({
|
|
211
|
+
id: `agent-instruction:${this.id}:${command.command_id}`,
|
|
212
|
+
title: "Agent instruction updated",
|
|
213
|
+
state: "completed",
|
|
214
|
+
turnId: turn_id,
|
|
215
|
+
});
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (command.type === "agent_env") {
|
|
219
|
+
this.effective_agent_env = { ...command.env };
|
|
220
|
+
await this.emit_config_action_event({
|
|
221
|
+
id: `agent-env:${this.id}:${command.command_id}`,
|
|
222
|
+
title: "Agent environment updated",
|
|
223
|
+
state: "completed",
|
|
224
|
+
turnId: turn_id,
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this.effective_agent_plugins = command.plugins;
|
|
229
|
+
await this.emit_config_action_event({
|
|
230
|
+
id: `agent-plugins:${this.id}:${command.command_id}`,
|
|
231
|
+
title: command.title,
|
|
232
|
+
state: "completed",
|
|
233
|
+
turnId: turn_id,
|
|
245
234
|
});
|
|
246
235
|
}
|
|
247
236
|
/**
|
|
248
237
|
* 订阅当前 Session 的未来事件。
|
|
249
238
|
*/
|
|
250
239
|
subscribe(subscriber) {
|
|
251
|
-
return this.
|
|
240
|
+
return this.events.subscribe(subscriber);
|
|
252
241
|
}
|
|
253
242
|
/** 列出当前 Session 的 pending 工具审批。 */
|
|
254
243
|
async approvals() {
|
|
255
|
-
return
|
|
244
|
+
return this.approval_broker.list();
|
|
256
245
|
}
|
|
257
246
|
/** 读取当前 Session 的工具审批模式。 */
|
|
258
247
|
async approval_mode() {
|
|
259
|
-
return
|
|
248
|
+
return this.approval_broker.get_mode();
|
|
260
249
|
}
|
|
261
250
|
/** 更新当前 Session 的工具审批模式。 */
|
|
262
251
|
async set_approval_mode(input) {
|
|
263
|
-
return
|
|
252
|
+
return this.approval_broker.set_mode(input.mode);
|
|
264
253
|
}
|
|
265
254
|
/** 处理当前 Session 的 pending 工具审批。 */
|
|
266
255
|
async resolve_approval(input) {
|
|
267
|
-
return await this.
|
|
268
|
-
}
|
|
269
|
-
async apply_runtime_event(event) {
|
|
270
|
-
if (event.type === "tool-approval-request") {
|
|
271
|
-
await this.recorder.require_tool_approval({
|
|
272
|
-
tool_call_id: event.tool_call_id,
|
|
273
|
-
approval_id: event.approval_id,
|
|
274
|
-
});
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
await this.recorder.resolve_tool_approval({
|
|
278
|
-
tool_call_id: event.tool_call_id,
|
|
279
|
-
approval_id: event.approval_id,
|
|
280
|
-
decision: event.decision,
|
|
281
|
-
});
|
|
256
|
+
return await this.approval_broker.resolve(input);
|
|
282
257
|
}
|
|
283
258
|
/**
|
|
284
259
|
* 追加一条 user 文本消息。
|
|
285
260
|
*/
|
|
286
261
|
async append_user_message(input) {
|
|
287
|
-
await this.
|
|
262
|
+
const appended = await this.session_messages.append_external_user_message({
|
|
288
263
|
text: String(input.text || "").trim(),
|
|
289
264
|
});
|
|
265
|
+
if (!appended)
|
|
266
|
+
return;
|
|
267
|
+
await this.state.ensure_title_from_history({ generate: true });
|
|
268
|
+
await this.state.touch_metadata();
|
|
290
269
|
}
|
|
291
270
|
/**
|
|
292
271
|
* 追加一条 assistant 文本消息。
|
|
293
272
|
*/
|
|
294
273
|
async append_assistant_message(input) {
|
|
295
|
-
await this.
|
|
296
|
-
|
|
274
|
+
const appended = await this.session_messages.append_external_assistant_message({
|
|
275
|
+
fallback_text: String(input.text || "").trim(),
|
|
297
276
|
});
|
|
277
|
+
if (appended)
|
|
278
|
+
await this.state.touch_metadata();
|
|
298
279
|
}
|
|
299
280
|
/**
|
|
300
281
|
* 读取当前 session 详情。
|
|
301
282
|
*/
|
|
302
283
|
async get_info() {
|
|
303
|
-
|
|
284
|
+
const [metadata, snapshot] = await Promise.all([
|
|
285
|
+
readSessionMetadata({
|
|
286
|
+
projectRoot: this.project_root,
|
|
287
|
+
agentId: this.agentId,
|
|
288
|
+
sessionId: this.id,
|
|
289
|
+
}),
|
|
290
|
+
this.session_messages.context_snapshot(),
|
|
291
|
+
]);
|
|
292
|
+
const records = to_executor_history(this.id, snapshot);
|
|
293
|
+
const metadata_with_title = metadata.title
|
|
294
|
+
? metadata
|
|
295
|
+
: await ensureSessionTitle({
|
|
296
|
+
projectRoot: this.project_root,
|
|
297
|
+
agentId: this.agentId,
|
|
298
|
+
sessionId: this.id,
|
|
299
|
+
messages: records,
|
|
300
|
+
logger: this.logger,
|
|
301
|
+
});
|
|
302
|
+
return buildSessionInfo({
|
|
303
|
+
projectRoot: this.project_root,
|
|
304
|
+
agentId: this.agentId,
|
|
305
|
+
sessionId: this.id,
|
|
306
|
+
metadata: metadata_with_title,
|
|
307
|
+
messages: records,
|
|
308
|
+
executing: this.isExecuting(),
|
|
309
|
+
});
|
|
304
310
|
}
|
|
305
311
|
/**
|
|
306
312
|
* 读取当前 session records 分页。
|
|
307
313
|
*/
|
|
308
314
|
async messages(input) {
|
|
309
|
-
return await this.
|
|
315
|
+
return await this.session_messages.list_messages(input);
|
|
310
316
|
}
|
|
311
317
|
/**
|
|
312
318
|
* 读取当前 session 生效的 system 快照。
|
|
313
319
|
*/
|
|
314
320
|
async system() {
|
|
315
|
-
|
|
321
|
+
const composed = await this.compose_for_view();
|
|
322
|
+
const blocks = resolve_composed_system_blocks(composed);
|
|
323
|
+
return {
|
|
324
|
+
sessionId: this.id,
|
|
325
|
+
session: {
|
|
326
|
+
agentId: this.agentId,
|
|
327
|
+
sessionId: this.id,
|
|
328
|
+
projectRoot: this.project_root,
|
|
329
|
+
createdAt: new Date(this.state.get_created_at()).toISOString(),
|
|
330
|
+
timezone: this.state.get_timezone(),
|
|
331
|
+
},
|
|
332
|
+
blocks,
|
|
333
|
+
};
|
|
316
334
|
}
|
|
317
335
|
/**
|
|
318
336
|
* 返回当前 session 是否正在执行。
|
|
319
337
|
*/
|
|
320
338
|
isExecuting() {
|
|
321
|
-
return this.
|
|
339
|
+
return this.session_turn.is_active() || this.executor.isExecuting();
|
|
322
340
|
}
|
|
323
341
|
/**
|
|
324
342
|
* 从当前 session 创建一个分叉会话。
|
|
325
343
|
*/
|
|
326
344
|
async fork(input) {
|
|
327
|
-
|
|
345
|
+
const message_id = typeof input === "string"
|
|
346
|
+
? String(input || "").trim() || undefined
|
|
347
|
+
: String(input?.messageId || "").trim() || undefined;
|
|
348
|
+
const messages = await this.session_messages.list_history_messages();
|
|
349
|
+
const fork_messages = message_id
|
|
350
|
+
? this.resolve_fork_messages(messages, message_id)
|
|
351
|
+
: messages;
|
|
352
|
+
const action_id = `history-forking:${this.id}:${Date.now()}:${nanoid(8)}`;
|
|
353
|
+
await this.emit_action_event({
|
|
354
|
+
id: action_id,
|
|
355
|
+
title: "Forking session messages",
|
|
356
|
+
description: `Preparing ${String(fork_messages.length)} messages for the new session.`,
|
|
357
|
+
state: "running",
|
|
358
|
+
});
|
|
359
|
+
try {
|
|
360
|
+
const forked = this.create_fork_session(`fork-${Date.now()}-${nanoid(8)}`);
|
|
361
|
+
await forked.initialize();
|
|
362
|
+
const session_config = this.state.get_config();
|
|
363
|
+
if (session_config.model) {
|
|
364
|
+
await forked.state.set({ model: session_config.model }, { emit_action: false });
|
|
365
|
+
}
|
|
366
|
+
await forked.session_messages.import_messages(fork_messages);
|
|
367
|
+
await this.emit_action_event({
|
|
368
|
+
id: action_id,
|
|
369
|
+
title: "Session messages forked",
|
|
370
|
+
description: `Created ${forked.id} with ${String(fork_messages.length)} messages.`,
|
|
371
|
+
state: "completed",
|
|
372
|
+
});
|
|
373
|
+
return forked;
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
await this.emit_action_event({
|
|
377
|
+
id: action_id,
|
|
378
|
+
title: "Session messages fork failed",
|
|
379
|
+
description: error instanceof Error ? error.message : String(error),
|
|
380
|
+
state: "failed",
|
|
381
|
+
});
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/** 截取 Fork 目标 Message 及其之前的完整历史。 */
|
|
386
|
+
resolve_fork_messages(messages, message_id) {
|
|
387
|
+
const target_index = messages.findIndex((message) => message.message_id === message_id);
|
|
388
|
+
if (target_index < 0) {
|
|
389
|
+
throw new Error(`Cannot fork session "${this.id}": messageId "${message_id}" not found.`);
|
|
390
|
+
}
|
|
391
|
+
return messages.slice(0, target_index + 1);
|
|
328
392
|
}
|
|
329
393
|
/**
|
|
330
394
|
* 返回供受托管 plugin 使用的 session 端口。
|
|
331
395
|
*/
|
|
332
396
|
getRuntimePort() {
|
|
333
|
-
if (this.
|
|
334
|
-
return this.
|
|
335
|
-
this.
|
|
397
|
+
if (this.runtime_port)
|
|
398
|
+
return this.runtime_port;
|
|
399
|
+
this.runtime_port = createRuntimeSessionPort({
|
|
336
400
|
sessionId: this.id,
|
|
401
|
+
getModel: () => this.get_model(),
|
|
337
402
|
getExecutor: () => this.executor.getExecutor(),
|
|
338
403
|
prompt: async (input) => await this.prompt(input),
|
|
339
404
|
stop: async () => await this.stop(),
|
|
340
405
|
subscribe: (subscriber) => this.subscribe(subscriber),
|
|
341
|
-
publishEvent: async (event) => {
|
|
342
|
-
await this.apply_runtime_event(event);
|
|
343
|
-
},
|
|
344
406
|
append_user_message: async (message_params) => {
|
|
345
|
-
await this.
|
|
407
|
+
const appended = await this.session_messages.append_external_user_message(message_params);
|
|
408
|
+
if (!appended)
|
|
409
|
+
return;
|
|
410
|
+
await this.state.ensure_title_from_history({ generate: true });
|
|
411
|
+
await this.state.touch_metadata();
|
|
346
412
|
},
|
|
347
413
|
append_assistant_message: async (message_params) => {
|
|
348
|
-
await this.
|
|
414
|
+
const appended = await this.session_messages.append_external_assistant_message({
|
|
415
|
+
message: message_params.message,
|
|
416
|
+
fallback_text: message_params.fallbackText,
|
|
417
|
+
});
|
|
418
|
+
if (appended)
|
|
419
|
+
await this.state.touch_metadata();
|
|
349
420
|
},
|
|
350
421
|
isExecuting: () => this.isExecuting(),
|
|
351
|
-
|
|
422
|
+
context: async () => await this.session_messages.context_snapshot(),
|
|
352
423
|
ensureReadyForExecution: async () => {
|
|
353
424
|
await this.ensureReadyForExecution();
|
|
354
425
|
},
|
|
355
426
|
});
|
|
356
|
-
return this.
|
|
427
|
+
return this.runtime_port;
|
|
357
428
|
}
|
|
358
429
|
/**
|
|
359
430
|
* 在执行前确保 session 已完成初始化与宿主装配。
|
|
360
431
|
*/
|
|
361
432
|
async ensureReadyForExecution() {
|
|
362
|
-
await this.
|
|
433
|
+
await this.state.ensure_ready_for_execution();
|
|
363
434
|
}
|
|
364
435
|
create_fork_session(session_id) {
|
|
365
436
|
return this.create_child_session({
|
|
366
437
|
agentId: this.agentId,
|
|
367
|
-
projectRoot: this.
|
|
438
|
+
projectRoot: this.project_root,
|
|
368
439
|
sessionId: session_id,
|
|
369
440
|
tools: this.tools,
|
|
370
441
|
logger: this.logger,
|
|
371
|
-
getInstructionSystemBlocks: this.
|
|
372
|
-
getAgentEnv: this.
|
|
442
|
+
getInstructionSystemBlocks: this.get_instruction_system_blocks,
|
|
443
|
+
getAgentEnv: this.get_agent_env,
|
|
373
444
|
get_agent_plugins: this.get_agent_plugins,
|
|
374
|
-
getManagedPluginSystemBlocks: this.
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
list_approvals: this.list_approvals,
|
|
379
|
-
get_approval_mode: this.get_approval_mode,
|
|
380
|
-
set_approval_mode: this.set_approval_mode_hook,
|
|
381
|
-
resolve_approval: this.resolve_approval_hook,
|
|
445
|
+
getManagedPluginSystemBlocks: this.get_managed_plugin_system_blocks,
|
|
446
|
+
ensureConfigured: this.ensure_configured_hook,
|
|
447
|
+
getAgentModel: this.get_agent_model,
|
|
448
|
+
composer: this.composer,
|
|
382
449
|
});
|
|
383
450
|
}
|
|
384
451
|
/**
|
|
@@ -393,109 +460,208 @@ export class Session {
|
|
|
393
460
|
return new SessionClass(options);
|
|
394
461
|
}
|
|
395
462
|
create_message_store() {
|
|
396
|
-
const session_dir_path = getSdkAgentSessionDirPath(this.
|
|
463
|
+
const session_dir_path = getSdkAgentSessionDirPath(this.project_root, this.agentId, this.id);
|
|
397
464
|
const messages_dir_path = `${session_dir_path}/messages`;
|
|
398
465
|
return new JsonlSessionMessageStore({
|
|
399
466
|
session_id: this.id,
|
|
400
467
|
file_path: `${messages_dir_path}/active.jsonl`,
|
|
401
|
-
assistant_message_file_path: getSdkAgentSessionAssistantMessagePath(this.
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
create_history_store() {
|
|
405
|
-
return new SessionRecorderHistoryStore({
|
|
406
|
-
session_id: this.id,
|
|
407
|
-
recorder: this.recorder,
|
|
468
|
+
assistant_message_file_path: getSdkAgentSessionAssistantMessagePath(this.project_root, this.agentId, this.id),
|
|
408
469
|
});
|
|
409
470
|
}
|
|
410
471
|
create_local_state() {
|
|
411
472
|
return {
|
|
412
|
-
|
|
473
|
+
session_config: {},
|
|
413
474
|
effective_session_config: {},
|
|
414
|
-
|
|
475
|
+
created_at: Date.now(),
|
|
415
476
|
timezone: resolveSystemTimezone(),
|
|
416
|
-
|
|
417
|
-
|
|
477
|
+
initialize_promise: null,
|
|
478
|
+
ensure_configured_promise: null,
|
|
418
479
|
};
|
|
419
480
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
agentId: this.agentId,
|
|
423
|
-
projectRoot: this.projectRoot,
|
|
424
|
-
sessionId: this.id,
|
|
425
|
-
historyStore: this.historyStore,
|
|
426
|
-
getTools: () => this.tools,
|
|
427
|
-
getInstructionSystemBlocks: () => this.effective_instruction_system_blocks.map((block) => ({ ...block })),
|
|
428
|
-
getManagedPluginSystemBlocks: this.getManagedPluginSystemBlocks,
|
|
429
|
-
getPluginSystemBlocks: async () => await this.effective_agent_plugins.systemBlocks(),
|
|
430
|
-
getSessionCreatedAt: () => this.localState.createdAt,
|
|
431
|
-
getSessionTimezone: () => this.localState.timezone,
|
|
432
|
-
};
|
|
433
|
-
}
|
|
434
|
-
create_history_composer(context) {
|
|
435
|
-
return this.resolve_composer(this.composers?.historyComposer, context, () => new JsonlSessionHistoryComposer({
|
|
436
|
-
store: this.historyStore,
|
|
437
|
-
}));
|
|
438
|
-
}
|
|
439
|
-
create_system_composer(context) {
|
|
440
|
-
return this.resolve_composer(this.composers?.systemComposer, context, () => new SessionSystemBuilder({
|
|
441
|
-
agentId: this.agentId,
|
|
442
|
-
projectRoot: this.projectRoot,
|
|
443
|
-
getSessionCreatedAt: () => this.localState.createdAt,
|
|
444
|
-
getSessionTimezone: () => this.localState.timezone,
|
|
445
|
-
getInstructionSystemBlocks: () => this.effective_instruction_system_blocks.map((block) => ({ ...block })),
|
|
446
|
-
getManagedPluginSystemBlocks: this.getManagedPluginSystemBlocks,
|
|
447
|
-
getPluginSystemBlocks: async () => await this.effective_agent_plugins.systemBlocks(),
|
|
448
|
-
}));
|
|
449
|
-
}
|
|
450
|
-
create_context_composer(context) {
|
|
451
|
-
return this.resolve_optional_composer(this.composers?.contextComposer, context);
|
|
452
|
-
}
|
|
453
|
-
create_compaction_composer(context) {
|
|
454
|
-
return this.resolve_optional_composer(this.composers?.compactionComposer, context);
|
|
455
|
-
}
|
|
456
|
-
create_executor(system_composer, context_composer, compaction_composer) {
|
|
481
|
+
/** 创建只依赖统一 Composer 的 Turn Executor。 */
|
|
482
|
+
create_executor() {
|
|
457
483
|
return new Executor({
|
|
458
484
|
sessionId: this.id,
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
get_model_context_window: () => this.localState.effective_session_config.model
|
|
464
|
-
? this.localState.effective_session_config.model_context_window
|
|
465
|
-
: this.localState.sessionConfig.model_context_window,
|
|
485
|
+
composer: this.composer,
|
|
486
|
+
get_compose_input: async (run_context, retry_count) => await this.create_compose_input(run_context, retry_count),
|
|
487
|
+
commit_compaction: async (plan, run_context) => await this.commit_compaction_plan(plan, run_context),
|
|
488
|
+
getModel: () => this.get_model(),
|
|
466
489
|
logger: this.logger,
|
|
467
|
-
systemComposer: system_composer,
|
|
468
|
-
getTools: () => this.tools,
|
|
469
|
-
getEnv: () => ({ ...this.effective_agent_env }),
|
|
470
|
-
get_systems: () => this.effective_instruction_system_blocks.map((block) => block.content),
|
|
471
490
|
get_plugins: () => this.effective_agent_plugins,
|
|
472
|
-
...(context_composer ? { contextComposer: context_composer } : {}),
|
|
473
|
-
...(compaction_composer
|
|
474
|
-
? { compactionComposer: compaction_composer }
|
|
475
|
-
: {}),
|
|
476
491
|
});
|
|
477
492
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
493
|
+
/** 为 Composer 创建当前 Step 的只读 Session 快照。 */
|
|
494
|
+
async create_compose_input(run_context, retry_count) {
|
|
495
|
+
const plugin_system_blocks = run_context.agentPlugins
|
|
496
|
+
? await run_context.agentPlugins.systemBlocks(run_context)
|
|
497
|
+
: await this.effective_agent_plugins.systemBlocks(run_context);
|
|
498
|
+
return {
|
|
499
|
+
session: {
|
|
500
|
+
agent_id: this.agentId,
|
|
501
|
+
session_id: this.id,
|
|
502
|
+
project_root: this.project_root,
|
|
503
|
+
created_at: this.local_state.created_at,
|
|
504
|
+
timezone: this.local_state.timezone,
|
|
505
|
+
},
|
|
506
|
+
state: {
|
|
507
|
+
model: this.get_model(),
|
|
508
|
+
model_context_window: this.get_model_context_window(),
|
|
509
|
+
env: Object.freeze({ ...this.effective_agent_env }),
|
|
510
|
+
systems: Object.freeze(this.effective_instruction_system_blocks.map((block) => block.content)),
|
|
511
|
+
tools: Object.freeze({ ...this.tools }),
|
|
512
|
+
instruction_system_blocks: this.effective_instruction_system_blocks.map((block) => ({ ...block })),
|
|
513
|
+
managed_plugin_system_blocks: await this.get_managed_plugin_system_blocks(),
|
|
514
|
+
plugin_system_blocks,
|
|
515
|
+
},
|
|
516
|
+
history: await this.session_messages.context_snapshot(),
|
|
517
|
+
turn: {
|
|
518
|
+
...(run_context.turnId ? { turn_id: run_context.turnId } : {}),
|
|
519
|
+
retry_count,
|
|
520
|
+
},
|
|
521
|
+
};
|
|
481
522
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
523
|
+
/** 使用统一 Composer 生成只读 system/history 查询结果。 */
|
|
524
|
+
async compose_for_view() {
|
|
525
|
+
const run_context = {
|
|
526
|
+
sessionId: this.id,
|
|
527
|
+
injectedUserMessages: [],
|
|
528
|
+
deferredPersistedUserMessages: [],
|
|
529
|
+
pendingAssistantFileParts: [],
|
|
530
|
+
};
|
|
531
|
+
return await this.composer.compose(await this.create_compose_input(run_context, 0));
|
|
532
|
+
}
|
|
533
|
+
/** 提交 Composer 生成的 Segment 压缩计划。 */
|
|
534
|
+
async commit_compaction_plan(plan, run_context) {
|
|
535
|
+
const action_id = `compacting:${this.id}:${generateId()}`;
|
|
536
|
+
await this.emit_action_event({
|
|
537
|
+
id: action_id,
|
|
538
|
+
title: "Compacting session messages",
|
|
539
|
+
state: "running",
|
|
540
|
+
...(run_context.turnId ? { turnId: run_context.turnId } : {}),
|
|
541
|
+
});
|
|
542
|
+
try {
|
|
543
|
+
await this.session_messages.compact_active({
|
|
544
|
+
through_sequence: plan.through_sequence,
|
|
545
|
+
summary: plan.summary,
|
|
546
|
+
});
|
|
547
|
+
await this.emit_action_event({
|
|
548
|
+
id: action_id,
|
|
549
|
+
title: "Session messages compacted",
|
|
550
|
+
description: plan.used_fallback
|
|
551
|
+
? `Closed Active through Message ${plan.boundary_message_id} with deterministic fallback Summary.`
|
|
552
|
+
: `Closed Active through Message ${plan.boundary_message_id}.`,
|
|
553
|
+
state: "completed",
|
|
554
|
+
...(run_context.turnId ? { turnId: run_context.turnId } : {}),
|
|
555
|
+
});
|
|
556
|
+
await this.state.touch_metadata();
|
|
557
|
+
}
|
|
558
|
+
catch (error) {
|
|
559
|
+
await this.emit_action_event({
|
|
560
|
+
id: action_id,
|
|
561
|
+
title: "Session messages compact failed",
|
|
562
|
+
description: error instanceof Error ? error.message : String(error),
|
|
563
|
+
state: "failed",
|
|
564
|
+
...(run_context.turnId ? { turnId: run_context.turnId } : {}),
|
|
565
|
+
});
|
|
566
|
+
throw error;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* 返回当前 Session 实际使用的模型实例。
|
|
571
|
+
*
|
|
572
|
+
* 解析顺序固定为 Session 覆盖模型,其次回退到 Agent 模型。
|
|
573
|
+
*/
|
|
574
|
+
get_model() {
|
|
575
|
+
const model = this.get_selected_model();
|
|
576
|
+
return model ? normalizeAgentModel(model) : undefined;
|
|
577
|
+
}
|
|
578
|
+
/** 按 Session 优先、Agent 兜底规则读取当前配置的 AgentModel。 */
|
|
579
|
+
get_selected_model() {
|
|
580
|
+
return (this.local_state.effective_session_config.model ||
|
|
581
|
+
this.local_state.session_config.model ||
|
|
582
|
+
this.get_agent_model());
|
|
583
|
+
}
|
|
584
|
+
/** 读取当前有效模型对应的上下文窗口。 */
|
|
585
|
+
get_model_context_window() {
|
|
586
|
+
return (this.local_state.effective_session_config.model_context_window ||
|
|
587
|
+
this.local_state.session_config.model_context_window ||
|
|
588
|
+
read_agent_model_context_window(this.get_selected_model()));
|
|
589
|
+
}
|
|
590
|
+
/** 持久化并发布一条 canonical Action Message。 */
|
|
591
|
+
async emit_action_event(input) {
|
|
592
|
+
const action_id = String(input.id || "").trim() ||
|
|
593
|
+
`action:${this.id}:${Date.now()}`;
|
|
594
|
+
await this.session_messages.persist_action_record({
|
|
595
|
+
type: "action",
|
|
596
|
+
id: action_id,
|
|
597
|
+
title: input.title,
|
|
598
|
+
...(input.description ? { description: input.description } : {}),
|
|
599
|
+
state: input.state,
|
|
600
|
+
metadata: {
|
|
601
|
+
v: 1,
|
|
602
|
+
ts: Date.now(),
|
|
603
|
+
sessionId: this.id,
|
|
604
|
+
...(input.turnId ? { turnId: input.turnId } : {}),
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
await this.state.touch_metadata();
|
|
608
|
+
}
|
|
609
|
+
/** 尽力记录配置 Action,不让 timeline 故障反向改变已提交配置。 */
|
|
610
|
+
async emit_config_action_event(input) {
|
|
611
|
+
try {
|
|
612
|
+
await this.emit_action_event({
|
|
613
|
+
id: input.id,
|
|
614
|
+
title: input.title,
|
|
615
|
+
description: input.description,
|
|
616
|
+
state: input.state,
|
|
617
|
+
turnId: "turnId" in input ? input.turnId : input.metadata?.turnId,
|
|
618
|
+
});
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
catch (error) {
|
|
622
|
+
try {
|
|
623
|
+
await this.logger.log("warn", "[agent] config action persistence failed", {
|
|
624
|
+
sessionId: this.id,
|
|
625
|
+
actionId: String(input.id || ""),
|
|
626
|
+
error: error instanceof Error ? error.message : String(error),
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
catch {
|
|
630
|
+
// 配置已提交,日志失败也不能反向改变 effective state。
|
|
631
|
+
}
|
|
632
|
+
return false;
|
|
488
633
|
}
|
|
489
|
-
return input;
|
|
490
634
|
}
|
|
491
635
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
return
|
|
498
|
-
|
|
499
|
-
|
|
636
|
+
/** 把自定义 Composer 的 system content 转为可展示文本。 */
|
|
637
|
+
function stringify_system_content(content) {
|
|
638
|
+
if (typeof content === "string")
|
|
639
|
+
return content.trim();
|
|
640
|
+
if (content === null || content === undefined)
|
|
641
|
+
return "";
|
|
642
|
+
try {
|
|
643
|
+
return JSON.stringify(content);
|
|
644
|
+
}
|
|
645
|
+
catch {
|
|
646
|
+
return String(content || "").trim();
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
/** 以实际模型输入为准,保留仍与其一致的 system block 来源信息。 */
|
|
650
|
+
function resolve_composed_system_blocks(composed) {
|
|
651
|
+
const declared_blocks = composed.system_blocks || [];
|
|
652
|
+
return composed.system.flatMap((message, index) => {
|
|
653
|
+
const content = stringify_system_content(message.content);
|
|
654
|
+
if (!content)
|
|
655
|
+
return [];
|
|
656
|
+
const declared = declared_blocks[index];
|
|
657
|
+
if (declared && declared.content.trim() === content) {
|
|
658
|
+
return [{ ...declared, content }];
|
|
659
|
+
}
|
|
660
|
+
return [{
|
|
661
|
+
source: "session",
|
|
662
|
+
name: `custom_system:${index + 1}`,
|
|
663
|
+
content,
|
|
664
|
+
}];
|
|
665
|
+
});
|
|
500
666
|
}
|
|
501
667
|
//# sourceMappingURL=Session.js.map
|