@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
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AgentBackgroundService:本地 Agent 后台能力生命周期服务。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 统一管理 plugins 与 ActionSchedule 的启动 / 停止。
|
|
6
|
-
* - 在 Agent 构造阶段自动启动后台能力,调用方通过 `await agent.ready()` 等待启动完成。
|
|
7
|
-
* - RPC / HTTP 等 transport 不再属于 Agent 内部职责,由 `@downcity/server` 等外部包按需挂载。
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { AgentContext } from "@/types/runtime/agent/AgentContext.js";
|
|
11
|
-
import type { Logger } from "@/utils/logger/Logger.js";
|
|
12
|
-
import type { ActionScheduleRuntimeHandle } from "@/plugin/core/ActionScheduleRuntime.js";
|
|
13
|
-
import { startActionScheduleRuntime } from "@/plugin/core/ActionScheduleRuntime.js";
|
|
14
|
-
import type { Shell } from "@downcity/shell";
|
|
15
|
-
|
|
16
|
-
type AgentBackgroundServiceOptions = {
|
|
17
|
-
/**
|
|
18
|
-
* 当前统一日志器。
|
|
19
|
-
*/
|
|
20
|
-
logger: Logger;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 当前 agent context。
|
|
24
|
-
*/
|
|
25
|
-
agent_context: AgentContext;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 读取当前 agent 挂载的 Shell。
|
|
29
|
-
*/
|
|
30
|
-
get_shell?: () => Shell | undefined;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 本地 Agent 后台能力生命周期服务。
|
|
35
|
-
*
|
|
36
|
-
* 关键点(中文)
|
|
37
|
-
* - 构造时自动触发 plugin lifecycle 与 ActionSchedule 启动。
|
|
38
|
-
* - 失败由 logger.error 记录,不抛错;调用方通过 `ready()` 等待启动完成。
|
|
39
|
-
*/
|
|
40
|
-
export class AgentBackgroundService {
|
|
41
|
-
private readonly logger: Logger;
|
|
42
|
-
private readonly agent_context: AgentContext;
|
|
43
|
-
private readonly get_shell: AgentBackgroundServiceOptions["get_shell"];
|
|
44
|
-
|
|
45
|
-
private plugins_started = false;
|
|
46
|
-
private action_schedule_runtime: ActionScheduleRuntimeHandle | null = null;
|
|
47
|
-
private ready_promise: Promise<void> | null = null;
|
|
48
|
-
|
|
49
|
-
constructor(options: AgentBackgroundServiceOptions) {
|
|
50
|
-
this.logger = options.logger;
|
|
51
|
-
this.agent_context = options.agent_context;
|
|
52
|
-
this.get_shell = options.get_shell;
|
|
53
|
-
// 关键点(中文):Agent 构造完成即触发后台启动,不再要求外部显式 `start()`。
|
|
54
|
-
this.ready_promise = this.boot();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 等待后台能力启动完成。
|
|
59
|
-
*/
|
|
60
|
-
async ready(): Promise<void> {
|
|
61
|
-
if (!this.ready_promise) return;
|
|
62
|
-
await this.ready_promise;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 释放当前 Agent 的后台能力。
|
|
67
|
-
*/
|
|
68
|
-
async dispose(): Promise<void> {
|
|
69
|
-
if (this.ready_promise) {
|
|
70
|
-
await this.ready_promise.catch(() => undefined);
|
|
71
|
-
this.ready_promise = null;
|
|
72
|
-
}
|
|
73
|
-
if (this.plugins_started) {
|
|
74
|
-
await this.stop_action_schedule_runtime();
|
|
75
|
-
await this.agent_context.plugins.unregisterAll();
|
|
76
|
-
this.plugins_started = false;
|
|
77
|
-
}
|
|
78
|
-
await this.get_shell?.()?.dispose();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private async boot(): Promise<void> {
|
|
82
|
-
await this.ensure_plugins_started();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private async ensure_plugins_started(): Promise<void> {
|
|
86
|
-
if (this.plugins_started) return;
|
|
87
|
-
const snapshots = await this.agent_context.plugins.startAll();
|
|
88
|
-
this.plugins_started = true;
|
|
89
|
-
for (const item of snapshots) {
|
|
90
|
-
if (item.status === "error") {
|
|
91
|
-
this.logger.error(
|
|
92
|
-
`Plugin start failed: ${item.name} - ${item.last_error || "unknown error"}`,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
await this.ensure_action_schedule_runtime_started();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private async ensure_action_schedule_runtime_started(): Promise<void> {
|
|
100
|
-
if (this.action_schedule_runtime) return;
|
|
101
|
-
try {
|
|
102
|
-
this.action_schedule_runtime = await startActionScheduleRuntime(
|
|
103
|
-
this.agent_context,
|
|
104
|
-
);
|
|
105
|
-
} catch (error) {
|
|
106
|
-
this.logger.error(`ActionSchedule start failed: ${String(error)}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private async stop_action_schedule_runtime(): Promise<void> {
|
|
111
|
-
const runtime = this.action_schedule_runtime;
|
|
112
|
-
this.action_schedule_runtime = null;
|
|
113
|
-
runtime?.stop();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
import type { FileUIPart } from "ai";
|
|
3
|
-
import type { SessionUserMessageV1 } from "@/executor/types/SessionRecords.js";
|
|
4
|
-
import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* SessionRunScope(单次请求作用域)。
|
|
8
|
-
*
|
|
9
|
-
* 关键点(中文)
|
|
10
|
-
* - 这是 session run 层请求作用域上下文,不属于具体 plugin 业务模块。
|
|
11
|
-
* - 当前阶段仅作为深层工具回调读取 `runContext` 的薄包装。
|
|
12
|
-
*/
|
|
13
|
-
export type SessionRunScope = {
|
|
14
|
-
/**
|
|
15
|
-
* 当前显式运行上下文。
|
|
16
|
-
*/
|
|
17
|
-
runContext: SessionRunContext;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* AsyncLocalStorage 容器(请求作用域)。
|
|
22
|
-
*
|
|
23
|
-
* 关键点(中文)
|
|
24
|
-
* - 同一条异步调用链内可读取一致的 SessionRunScope。
|
|
25
|
-
* - 当前主要用于 tool 深层桥接层读取活跃 runContext。
|
|
26
|
-
*/
|
|
27
|
-
export const sessionRunScopeStorage = new AsyncLocalStorage<SessionRunScope>();
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 在当前异步调用链内绑定 session run 作用域。
|
|
31
|
-
*/
|
|
32
|
-
export function withSessionRunScope<T>(ctx: SessionRunScope, fn: () => T): T {
|
|
33
|
-
return sessionRunScopeStorage.run(ctx, fn);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 读取当前异步链路上的 session run 作用域。
|
|
38
|
-
*/
|
|
39
|
-
export function getSessionRunScope(): SessionRunScope | undefined {
|
|
40
|
-
return sessionRunScopeStorage.getStore();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 读取当前异步链路上的显式 runContext。
|
|
45
|
-
*/
|
|
46
|
-
export function getSessionRunContext(): SessionRunContext | undefined {
|
|
47
|
-
return sessionRunScopeStorage.getStore()?.runContext;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 入队一条“待注入 user 消息”。
|
|
52
|
-
*
|
|
53
|
-
* 关键点(中文)
|
|
54
|
-
* - 若当前不在 session run 作用域内,静默忽略(fail-open)。
|
|
55
|
-
*/
|
|
56
|
-
export function enqueueInjectedUserMessage(
|
|
57
|
-
message: SessionUserMessageV1,
|
|
58
|
-
): void {
|
|
59
|
-
const run_context = getSessionRunContext();
|
|
60
|
-
if (!run_context || !message) return;
|
|
61
|
-
run_context.injectedUserMessages.push(message);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 读取并清空“待注入 user 消息”队列。
|
|
66
|
-
*
|
|
67
|
-
* 关键点(中文)
|
|
68
|
-
* - 采用 drain 语义,确保每条注入消息只在下一 step 使用一次。
|
|
69
|
-
*/
|
|
70
|
-
export function drainInjectedUserMessages(): SessionUserMessageV1[] {
|
|
71
|
-
const run_context = getSessionRunContext();
|
|
72
|
-
if (!run_context) return [];
|
|
73
|
-
const out = [...run_context.injectedUserMessages];
|
|
74
|
-
run_context.injectedUserMessages = [];
|
|
75
|
-
return out;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 入队一条“待持久化 user 消息”。
|
|
80
|
-
*/
|
|
81
|
-
export function enqueueDeferredPersistedUserMessage(
|
|
82
|
-
sessionId: string,
|
|
83
|
-
message: SessionUserMessageV1,
|
|
84
|
-
): void {
|
|
85
|
-
const run_context = getSessionRunContext();
|
|
86
|
-
const key = String(sessionId || "").trim();
|
|
87
|
-
if (!run_context || !key || !message) return;
|
|
88
|
-
if (run_context.sessionId !== key) return;
|
|
89
|
-
run_context.deferredPersistedUserMessages.push(message);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* 读取并清空指定 session 的待持久化 user 消息。
|
|
94
|
-
*/
|
|
95
|
-
export function drainDeferredPersistedUserMessages(
|
|
96
|
-
sessionId: string,
|
|
97
|
-
): SessionUserMessageV1[] {
|
|
98
|
-
const run_context = getSessionRunContext();
|
|
99
|
-
const key = String(sessionId || "").trim();
|
|
100
|
-
if (!run_context || !key || run_context.sessionId !== key) return [];
|
|
101
|
-
const current = [...run_context.deferredPersistedUserMessages];
|
|
102
|
-
run_context.deferredPersistedUserMessages = [];
|
|
103
|
-
return current;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* 入队一组“待并入 assistant 消息的 file parts”。
|
|
108
|
-
*/
|
|
109
|
-
export function enqueueAssistantFileParts(parts: FileUIPart[]): void {
|
|
110
|
-
const run_context = getSessionRunContext();
|
|
111
|
-
if (!run_context || !Array.isArray(parts) || parts.length === 0) return;
|
|
112
|
-
run_context.pendingAssistantFileParts.push(...parts);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 读取当前 run 内待并入 assistant 消息的 file parts。
|
|
117
|
-
*/
|
|
118
|
-
export function readPendingAssistantFileParts(): FileUIPart[] {
|
|
119
|
-
const run_context = getSessionRunContext();
|
|
120
|
-
if (!run_context) return [];
|
|
121
|
-
return [...run_context.pendingAssistantFileParts];
|
|
122
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SessionCompactionComposer:上下文压缩 Composer 抽象。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 负责“何时压缩 + 压缩策略参数”决策。
|
|
6
|
-
* - 具体读写由 SessionHistoryStore 执行(CompactionComposer 不直接落盘)。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { LanguageModel, SystemModelMessage } from "ai";
|
|
10
|
-
import type { SessionHistoryStore } from "@/executor/store/history/SessionHistoryStore.js";
|
|
11
|
-
import type { SessionActionRecordV1 } from "@/executor/types/SessionRecords.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* compaction Composer 执行输入。
|
|
15
|
-
*/
|
|
16
|
-
export type SessionCompactionInput = {
|
|
17
|
-
/**
|
|
18
|
-
* 当前会话 history Store。
|
|
19
|
-
*/
|
|
20
|
-
historyStore: SessionHistoryStore;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 当前模型实例。
|
|
24
|
-
*/
|
|
25
|
-
model: LanguageModel;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 当前模型支持的总上下文窗口长度,单位为 token。
|
|
29
|
-
*/
|
|
30
|
-
context_window?: number;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 当前轮 system messages。
|
|
34
|
-
*/
|
|
35
|
-
system: SystemModelMessage[];
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 当前重试次数(由 Executor 递增)。
|
|
39
|
-
*/
|
|
40
|
-
retryCount: number;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 可选 action 发布回调。
|
|
44
|
-
*
|
|
45
|
-
* 说明(中文)
|
|
46
|
-
* - 默认 compaction Composer 可在真正开始压缩时发布 action。
|
|
47
|
-
* - 回调由上层 Session 负责转成订阅事件与 action record。
|
|
48
|
-
*/
|
|
49
|
-
onAction?: (action: SessionActionRecordV1) => Promise<void>;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 是否明确要求持久化 deep compact。
|
|
53
|
-
*
|
|
54
|
-
* 普通模型调用不应因为历史字符数量而触发 compact;仅 usage 反馈或超限恢复会设置此值。
|
|
55
|
-
*/
|
|
56
|
-
force?: boolean;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Compaction Composer 协议。
|
|
61
|
-
*/
|
|
62
|
-
export interface SessionCompactionComposer {
|
|
63
|
-
/**
|
|
64
|
-
* Composer 名(由具体实现声明)。
|
|
65
|
-
*/
|
|
66
|
-
readonly name: string;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 执行 compact(best-effort)。
|
|
70
|
-
*/
|
|
71
|
-
run(input: SessionCompactionInput): Promise<{
|
|
72
|
-
compacted: boolean;
|
|
73
|
-
reason?: string;
|
|
74
|
-
}>;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 判断某次执行错误是否应该触发“压缩后重试”。
|
|
78
|
-
*
|
|
79
|
-
* 关键点(中文)
|
|
80
|
-
* - 由 compaction Composer 实现侧维护错误识别策略。
|
|
81
|
-
* - Executor 不感知具体错误文案,只按该布尔结果决定是否重试。
|
|
82
|
-
*/
|
|
83
|
-
shouldCompactOnError(error: unknown): boolean;
|
|
84
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JsonlSessionCompactionComposer:默认上下文压缩 Composer。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 只负责压缩参数策略(retry 越多,窗口越紧)。
|
|
6
|
-
* - 真正的读写与 compact 执行交由 history Store。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
SessionCompactionComposer,
|
|
11
|
-
SessionCompactionInput,
|
|
12
|
-
} from "@executor/composer/compaction/SessionCompactionComposer.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* JsonlSessionCompactionComposer 默认实现。
|
|
16
|
-
*/
|
|
17
|
-
export class JsonlSessionCompactionComposer implements SessionCompactionComposer {
|
|
18
|
-
readonly name = "summary_compaction_composer";
|
|
19
|
-
|
|
20
|
-
async run(input: SessionCompactionInput): Promise<{
|
|
21
|
-
compacted: boolean;
|
|
22
|
-
reason?: string;
|
|
23
|
-
}> {
|
|
24
|
-
if (!input.force && input.retryCount <= 0) {
|
|
25
|
-
return { compacted: false, reason: "not_requested" };
|
|
26
|
-
}
|
|
27
|
-
return await input.historyStore.compact({
|
|
28
|
-
model: input.model,
|
|
29
|
-
system: input.system,
|
|
30
|
-
force: true,
|
|
31
|
-
...(input.onAction ? { onAction: input.onAction } : {}),
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 判断错误是否属于“应先 compact 再重试”的类型。
|
|
37
|
-
*
|
|
38
|
-
* 关键点(中文)
|
|
39
|
-
* - 当前实现基于主流模型 provider 的超限关键词。
|
|
40
|
-
* - 后续若接入新 provider,可在此处统一扩展识别规则。
|
|
41
|
-
*/
|
|
42
|
-
shouldCompactOnError(error: unknown): boolean {
|
|
43
|
-
const errorMsg = String(error ?? "");
|
|
44
|
-
return (
|
|
45
|
-
errorMsg.includes("context_length") ||
|
|
46
|
-
errorMsg.includes("too long") ||
|
|
47
|
-
errorMsg.includes("maximum context") ||
|
|
48
|
-
errorMsg.includes("context window")
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JsonlSessionCompactionExecutor:JSONL session 压缩执行模块。
|
|
3
|
-
*
|
|
4
|
-
* 关键职责(中文)
|
|
5
|
-
* - 评估当前消息是否超出输入预算。
|
|
6
|
-
* - 在锁外生成“更早历史摘要”,降低锁持有时间。
|
|
7
|
-
* - 在锁内完成归档与 messages.jsonl 重写,保证并发安全。
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import fs from "fs-extra";
|
|
11
|
-
import path from "node:path";
|
|
12
|
-
import {
|
|
13
|
-
generateText,
|
|
14
|
-
isTextUIPart,
|
|
15
|
-
type LanguageModel,
|
|
16
|
-
type SystemModelMessage,
|
|
17
|
-
} from "ai";
|
|
18
|
-
import { generateId } from "@/utils/Id.js";
|
|
19
|
-
import { getLogger } from "@/utils/logger/Logger.js";
|
|
20
|
-
import type {
|
|
21
|
-
SessionActionRecordV1,
|
|
22
|
-
SessionRecordV1,
|
|
23
|
-
SessionMetadataV1,
|
|
24
|
-
SessionMessageRecordV1,
|
|
25
|
-
} from "@/executor/types/SessionRecords.js";
|
|
26
|
-
import {
|
|
27
|
-
is_session_message_record,
|
|
28
|
-
} from "@/executor/types/SessionRecords.js";
|
|
29
|
-
import {
|
|
30
|
-
append_session_compaction_file_operations,
|
|
31
|
-
build_initial_session_compaction_prompt,
|
|
32
|
-
build_update_session_compaction_prompt,
|
|
33
|
-
SESSION_COMPACTION_SYSTEM_PROMPT,
|
|
34
|
-
} from "@executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js";
|
|
35
|
-
import { format_session_compaction_file_operations } from "@executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js";
|
|
36
|
-
|
|
37
|
-
export type SessionCompactParams = {
|
|
38
|
-
model: LanguageModel;
|
|
39
|
-
system: Array<SystemModelMessage>;
|
|
40
|
-
archiveOnCompact: boolean;
|
|
41
|
-
force?: boolean;
|
|
42
|
-
onAction?: (action: SessionActionRecordV1) => Promise<void>;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
type SessionCompactDeps = {
|
|
46
|
-
rootPath: string;
|
|
47
|
-
sessionId: string;
|
|
48
|
-
withWriteLock: <T>(fn: () => Promise<T>) => Promise<T>;
|
|
49
|
-
loadAll: () => Promise<SessionRecordV1[]>;
|
|
50
|
-
createSummaryMessage: (params: {
|
|
51
|
-
text: string;
|
|
52
|
-
archiveId?: string;
|
|
53
|
-
sourceRange?: SessionMetadataV1["sourceRange"];
|
|
54
|
-
}) => SessionRecordV1;
|
|
55
|
-
getArchiveDirPath: () => string;
|
|
56
|
-
getMessagesFilePath: () => string;
|
|
57
|
-
writeHistorySummaryUnsafe: (records: SessionRecordV1[]) => Promise<void>;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 对当前 session messages 做一次 best-effort compact(必要时)。
|
|
62
|
-
*
|
|
63
|
-
* 注意(中文)
|
|
64
|
-
* - compact 会 rewrite `messages.jsonl`(不是纯 append-only),因此必须防并发覆盖
|
|
65
|
-
* - 这里做两阶段锁:先 snapshot 再生成摘要,最后再锁定写入,降低锁持有时间
|
|
66
|
-
*/
|
|
67
|
-
export async function compactSessionMessagesIfNeeded(
|
|
68
|
-
deps: SessionCompactDeps,
|
|
69
|
-
params: SessionCompactParams,
|
|
70
|
-
): Promise<{ compacted: boolean; reason?: string }> {
|
|
71
|
-
const logger = getLogger(deps.rootPath, "info");
|
|
72
|
-
const action_id = `compacting:${deps.sessionId}:${Date.now()}:${generateId()}`;
|
|
73
|
-
let action_started = false;
|
|
74
|
-
|
|
75
|
-
const publish_action = async (
|
|
76
|
-
action: Omit<SessionActionRecordV1, "type" | "id" | "metadata">,
|
|
77
|
-
): Promise<void> => {
|
|
78
|
-
if (typeof params.onAction !== "function") return;
|
|
79
|
-
await params.onAction({
|
|
80
|
-
type: "action",
|
|
81
|
-
id: action_id,
|
|
82
|
-
...action,
|
|
83
|
-
metadata: {
|
|
84
|
-
v: 1,
|
|
85
|
-
ts: Date.now(),
|
|
86
|
-
sessionId: deps.sessionId,
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
// 算法阶段(中文)
|
|
92
|
-
// phase 1:snapshot(短锁)
|
|
93
|
-
// - 仅负责拿一致性快照,不做耗时的模型调用。
|
|
94
|
-
// - 目的是把锁持有时间降到最低。
|
|
95
|
-
let snapshot: SessionMessageRecordV1[] = [];
|
|
96
|
-
let snapshotTailId = "";
|
|
97
|
-
await deps.withWriteLock(async () => {
|
|
98
|
-
snapshot = (await deps.loadAll()).filter(is_session_message_record);
|
|
99
|
-
snapshotTailId =
|
|
100
|
-
snapshot.length > 0
|
|
101
|
-
? String(snapshot[snapshot.length - 1].id || "")
|
|
102
|
-
: "";
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
if (snapshot.length === 0) {
|
|
106
|
-
return { compacted: false, reason: "nothing_to_compact" };
|
|
107
|
-
}
|
|
108
|
-
if (!params.force) {
|
|
109
|
-
return { compacted: false, reason: "not_requested" };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 关键点(中文):明确 compact 时归档全部当前模型历史,避免单条巨大 assistant 永远留在 Active。
|
|
113
|
-
const older = snapshot;
|
|
114
|
-
if (older.length === 0) return { compacted: false, reason: "nothing_to_compact" };
|
|
115
|
-
|
|
116
|
-
action_started = true;
|
|
117
|
-
await publish_action({
|
|
118
|
-
title: "Compacting session records",
|
|
119
|
-
state: "running",
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const previous_summary = extract_existing_compaction_summary(older[0]);
|
|
123
|
-
const conversation_messages = previous_summary ? older.slice(1) : older;
|
|
124
|
-
const olderTextAll = extractPlainTextFromMessages(conversation_messages);
|
|
125
|
-
const maxOlderChars = 24_000;
|
|
126
|
-
const olderText =
|
|
127
|
-
olderTextAll.length > maxOlderChars
|
|
128
|
-
? "(Note: earlier history was too long and was truncated to keep the tail.)\n"
|
|
129
|
-
+ olderTextAll.slice(-maxOlderChars)
|
|
130
|
-
: olderTextAll;
|
|
131
|
-
const prompt = previous_summary
|
|
132
|
-
? build_update_session_compaction_prompt({
|
|
133
|
-
previous_summary,
|
|
134
|
-
new_conversation_text: olderText || "(none)",
|
|
135
|
-
})
|
|
136
|
-
: build_initial_session_compaction_prompt({
|
|
137
|
-
conversation_text: olderText || "(none)",
|
|
138
|
-
});
|
|
139
|
-
const file_operations_xml = format_session_compaction_file_operations(older);
|
|
140
|
-
|
|
141
|
-
// phase 1.5:生成摘要(不持锁)
|
|
142
|
-
// - 这一步最耗时,必须在锁外执行,避免阻塞 append。
|
|
143
|
-
let summary = "";
|
|
144
|
-
try {
|
|
145
|
-
const r = await generateText({
|
|
146
|
-
model: params.model,
|
|
147
|
-
system: [
|
|
148
|
-
{
|
|
149
|
-
role: "system",
|
|
150
|
-
content: SESSION_COMPACTION_SYSTEM_PROMPT,
|
|
151
|
-
},
|
|
152
|
-
],
|
|
153
|
-
prompt,
|
|
154
|
-
});
|
|
155
|
-
summary = append_session_compaction_file_operations({
|
|
156
|
-
summary: String(r.text || "").trim(),
|
|
157
|
-
file_operations_xml,
|
|
158
|
-
});
|
|
159
|
-
} catch (e) {
|
|
160
|
-
await logger.log(
|
|
161
|
-
"warn",
|
|
162
|
-
"Session messages compact summary failed, fallback to lossy truncation",
|
|
163
|
-
{
|
|
164
|
-
sessionId: deps.sessionId,
|
|
165
|
-
error: String(e),
|
|
166
|
-
},
|
|
167
|
-
);
|
|
168
|
-
summary = append_session_compaction_file_operations({
|
|
169
|
-
summary:
|
|
170
|
-
"(Automatic session compaction: summary generation failed, so earlier history was discarded and only recent conversation was retained.)",
|
|
171
|
-
file_operations_xml,
|
|
172
|
-
});
|
|
173
|
-
await publish_action({
|
|
174
|
-
title: "Compacting session records",
|
|
175
|
-
description: "Summary generation failed; using a fallback summary.",
|
|
176
|
-
state: "running",
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const fromId = String(older[0]?.id || "");
|
|
181
|
-
const toId = String(older[older.length - 1]?.id || "");
|
|
182
|
-
const archiveId = `compact-${Date.now()}-${generateId()}`;
|
|
183
|
-
const summaryMsg = deps.createSummaryMessage({
|
|
184
|
-
text: summary,
|
|
185
|
-
archiveId,
|
|
186
|
-
sourceRange: fromId && toId ? { fromId, toId, count: older.length } : undefined,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// phase 2:写入(短锁,且避免覆盖新追加)
|
|
190
|
-
// - 以“当前最新 session messages”为准重算 currentOlder / currentKept,避免覆盖并发新消息。
|
|
191
|
-
await deps.withWriteLock(async () => {
|
|
192
|
-
const current = await deps.loadAll();
|
|
193
|
-
if (!current.length) return;
|
|
194
|
-
|
|
195
|
-
// 如果 tail 不同,说明期间有新消息追加;我们仍可安全 compact:按“当前长度 + 相同比例”重算。
|
|
196
|
-
// snapshotTailId 用于 debug,不作为强一致性依赖。
|
|
197
|
-
void snapshotTailId;
|
|
198
|
-
|
|
199
|
-
const current_model_messages = current.filter(
|
|
200
|
-
is_session_message_record,
|
|
201
|
-
);
|
|
202
|
-
const currentOlder = current_model_messages;
|
|
203
|
-
if (currentOlder.length === 0) return;
|
|
204
|
-
const compacted_ids = new Set(
|
|
205
|
-
currentOlder
|
|
206
|
-
.map((message) => String(message.id || "").trim())
|
|
207
|
-
.filter(Boolean),
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
if (params.archiveOnCompact) {
|
|
211
|
-
const archivePath = path.join(
|
|
212
|
-
deps.getArchiveDirPath(),
|
|
213
|
-
`${encodeURIComponent(String(archiveId || "").trim())}.json`,
|
|
214
|
-
);
|
|
215
|
-
await fs.writeJson(
|
|
216
|
-
archivePath,
|
|
217
|
-
{
|
|
218
|
-
v: 1,
|
|
219
|
-
sessionId: deps.sessionId,
|
|
220
|
-
archivedAt: Date.now(),
|
|
221
|
-
messages: currentOlder,
|
|
222
|
-
},
|
|
223
|
-
{ spaces: 2 },
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const next = [
|
|
228
|
-
summaryMsg,
|
|
229
|
-
...current.filter((message) => {
|
|
230
|
-
const id = String(message.id || "").trim();
|
|
231
|
-
return !id || !compacted_ids.has(id);
|
|
232
|
-
}),
|
|
233
|
-
];
|
|
234
|
-
|
|
235
|
-
const messagesPath = deps.getMessagesFilePath();
|
|
236
|
-
const tmp = messagesPath + ".tmp";
|
|
237
|
-
await fs.writeFile(tmp, next.map((m) => JSON.stringify(m)).join("\n") + "\n", "utf8");
|
|
238
|
-
await fs.move(tmp, messagesPath, { overwrite: true });
|
|
239
|
-
|
|
240
|
-
await deps.writeHistorySummaryUnsafe(next);
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
if (action_started) {
|
|
244
|
-
await publish_action({
|
|
245
|
-
title: "Session records compacted",
|
|
246
|
-
description: `Compacted ${String(older.length)} earlier messages into ${archiveId}.`,
|
|
247
|
-
state: "completed",
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return { compacted: true };
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* 从 UIMessage 提取可摘要的纯文本。
|
|
256
|
-
*
|
|
257
|
-
* 关键点(中文)
|
|
258
|
-
* - 统一把 user / assistant 内容线性化,作为 compact 摘要输入。
|
|
259
|
-
* - tool 原始结构不会原样输出,避免把噪声日志喂给摘要模型。
|
|
260
|
-
*/
|
|
261
|
-
function extractPlainTextFromMessages(messages: SessionMessageRecordV1[]): string {
|
|
262
|
-
const lines: string[] = [];
|
|
263
|
-
for (const m of messages) {
|
|
264
|
-
if (!m || typeof m !== "object") continue;
|
|
265
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
266
|
-
const parts = Array.isArray(m.parts) ? m.parts : [];
|
|
267
|
-
const textParts = parts.filter(isTextUIPart).map((p) => String(p.text ?? ""));
|
|
268
|
-
const text = textParts.join("\n").trim();
|
|
269
|
-
if (!text) continue;
|
|
270
|
-
lines.push(`${role}: ${text}`);
|
|
271
|
-
}
|
|
272
|
-
return lines.join("\n");
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* 从已有 compact summary 消息中提取旧摘要。
|
|
277
|
-
*
|
|
278
|
-
* 关键点(中文)
|
|
279
|
-
* - 旧摘要通常位于 messages.jsonl 第一条,后续 compact 应基于它迭代更新。
|
|
280
|
-
* - 只识别 compact 生成的 summary,避免误把普通 assistant 文本当作旧摘要。
|
|
281
|
-
*/
|
|
282
|
-
function extract_existing_compaction_summary(
|
|
283
|
-
message: SessionMessageRecordV1 | undefined,
|
|
284
|
-
): string {
|
|
285
|
-
if (!message || typeof message !== "object") return "";
|
|
286
|
-
const metadata = message.metadata;
|
|
287
|
-
if (metadata?.source !== "compact" || metadata.kind !== "summary") return "";
|
|
288
|
-
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
289
|
-
return parts
|
|
290
|
-
.filter(isTextUIPart)
|
|
291
|
-
.map((part) => String(part.text ?? ""))
|
|
292
|
-
.join("\n")
|
|
293
|
-
.trim();
|
|
294
|
-
}
|