@downcity/agent 1.1.430 → 1.1.437
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 +13 -11
- package/bin/agent/Agent.d.ts +3 -3
- package/bin/agent/Agent.d.ts.map +1 -1
- package/bin/agent/Agent.js +38 -8
- package/bin/agent/Agent.js.map +1 -1
- package/bin/agent/AgentInstructions.js +2 -2
- package/bin/agent/AgentInstructions.js.map +1 -1
- package/bin/agent/AgentMemoryStorage.d.ts.map +1 -1
- package/bin/agent/AgentMemoryStorage.js +1 -0
- package/bin/agent/AgentMemoryStorage.js.map +1 -1
- package/bin/agent/AgentSessions.d.ts +5 -1
- package/bin/agent/AgentSessions.d.ts.map +1 -1
- package/bin/agent/AgentSessions.js +17 -9
- package/bin/agent/AgentSessions.js.map +1 -1
- package/bin/executor/Executor.d.ts +9 -27
- package/bin/executor/Executor.d.ts.map +1 -1
- package/bin/executor/Executor.js +32 -127
- package/bin/executor/Executor.js.map +1 -1
- package/bin/executor/composer/system/default/SystemPromptAssets.d.ts +0 -4
- package/bin/executor/composer/system/default/SystemPromptAssets.d.ts.map +1 -1
- package/bin/executor/composer/system/default/SystemPromptAssets.js +0 -5
- package/bin/executor/composer/system/default/SystemPromptAssets.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts +3 -9
- package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineContextCompaction.js +4 -130
- package/bin/executor/core-engine/CoreEngineContextCompaction.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.d.ts +3 -30
- package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.js +38 -194
- package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineSignals.d.ts +6 -6
- package/bin/executor/core-engine/CoreEngineSignals.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineSignals.js +1 -1
- package/bin/executor/core-engine/CoreEngineSignals.js.map +1 -1
- package/bin/executor/messages/SessionAttachmentMapper.d.ts +2 -2
- package/bin/executor/messages/SessionAttachmentMapper.d.ts.map +1 -1
- package/bin/executor/messages/SessionAttachmentMapper.js.map +1 -1
- package/bin/executor/messages/SessionMessageLog.d.ts +3 -3
- package/bin/executor/messages/SessionMessageLog.d.ts.map +1 -1
- package/bin/executor/messages/SessionMessageLog.js.map +1 -1
- package/bin/executor/messages/SessionModelMessages.d.ts +0 -3
- package/bin/executor/messages/SessionModelMessages.d.ts.map +1 -1
- package/bin/executor/messages/SessionModelMessages.js +80 -97
- package/bin/executor/messages/SessionModelMessages.js.map +1 -1
- package/bin/executor/model/ModelStepRunner.d.ts +2 -6
- package/bin/executor/model/ModelStepRunner.d.ts.map +1 -1
- package/bin/executor/model/ModelStepRunner.js +0 -28
- package/bin/executor/model/ModelStepRunner.js.map +1 -1
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +7 -64
- package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -1
- package/bin/executor/services/ExecutorRecoveryPolicy.js +29 -45
- package/bin/executor/services/ExecutorRecoveryPolicy.js.map +1 -1
- package/bin/executor/types/SessionHistoryMeta.d.ts +1 -3
- package/bin/executor/types/SessionHistoryMeta.d.ts.map +1 -1
- package/bin/executor/types/SessionHistoryMeta.js +1 -1
- package/bin/index.d.ts +9 -13
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +2 -4
- package/bin/index.js.map +1 -1
- package/bin/session/DefaultSessionComposer.d.ts +13 -7
- package/bin/session/DefaultSessionComposer.d.ts.map +1 -1
- package/bin/session/DefaultSessionComposer.js +26 -21
- package/bin/session/DefaultSessionComposer.js.map +1 -1
- package/bin/session/Session.d.ts +12 -19
- package/bin/session/Session.d.ts.map +1 -1
- package/bin/session/Session.js +70 -202
- package/bin/session/Session.js.map +1 -1
- package/bin/session/SessionComposition.d.ts +0 -2
- package/bin/session/SessionComposition.d.ts.map +1 -1
- package/bin/session/SessionComposition.js +30 -54
- package/bin/session/SessionComposition.js.map +1 -1
- package/bin/session/SessionLoop.d.ts +17 -16
- package/bin/session/SessionLoop.d.ts.map +1 -1
- package/bin/session/SessionLoop.js +217 -320
- package/bin/session/SessionLoop.js.map +1 -1
- package/bin/session/SessionMessages.d.ts +52 -44
- package/bin/session/SessionMessages.d.ts.map +1 -1
- package/bin/session/SessionMessages.js +285 -187
- package/bin/session/SessionMessages.js.map +1 -1
- package/bin/session/SessionQueue.d.ts +1 -1
- package/bin/session/SessionQueue.d.ts.map +1 -1
- package/bin/session/SessionQueue.js +2 -1
- package/bin/session/SessionQueue.js.map +1 -1
- package/bin/session/SessionState.d.ts +3 -7
- package/bin/session/SessionState.d.ts.map +1 -1
- package/bin/session/SessionState.js +50 -89
- package/bin/session/SessionState.js.map +1 -1
- package/bin/session/SessionSystem.d.ts +5 -1
- package/bin/session/SessionSystem.d.ts.map +1 -1
- package/bin/session/SessionSystem.js +40 -0
- package/bin/session/SessionSystem.js.map +1 -1
- package/bin/session/SessionTitle.d.ts +3 -3
- package/bin/session/SessionTitle.d.ts.map +1 -1
- package/bin/session/SessionTitle.js +2 -2
- package/bin/session/browse/Browse.d.ts +32 -46
- package/bin/session/browse/Browse.d.ts.map +1 -1
- package/bin/session/browse/Browse.js +104 -360
- package/bin/session/browse/Browse.js.map +1 -1
- package/bin/session/browse/SessionMessageTimeline.d.ts.map +1 -1
- package/bin/session/browse/SessionMessageTimeline.js +61 -37
- package/bin/session/browse/SessionMessageTimeline.js.map +1 -1
- package/bin/session/composer/policies/AdaptivePartContextPolicy.d.ts +35 -0
- package/bin/session/composer/policies/AdaptivePartContextPolicy.d.ts.map +1 -0
- package/bin/session/composer/policies/AdaptivePartContextPolicy.js +256 -0
- package/bin/session/composer/policies/AdaptivePartContextPolicy.js.map +1 -0
- package/bin/session/composer/policies/FullHistoryContextPolicy.d.ts +24 -0
- package/bin/session/composer/policies/FullHistoryContextPolicy.d.ts.map +1 -0
- package/bin/session/composer/policies/FullHistoryContextPolicy.js +31 -0
- package/bin/session/composer/policies/FullHistoryContextPolicy.js.map +1 -0
- package/bin/session/composer/policies/SessionSummaryPrompts.d.ts +16 -0
- package/bin/session/composer/policies/SessionSummaryPrompts.d.ts.map +1 -0
- package/bin/session/composer/policies/SessionSummaryPrompts.js +32 -0
- package/bin/session/composer/policies/SessionSummaryPrompts.js.map +1 -0
- package/bin/session/execution/SessionAssistantOutputAdapter.d.ts +15 -4
- package/bin/session/execution/SessionAssistantOutputAdapter.d.ts.map +1 -1
- package/bin/session/execution/SessionAssistantOutputAdapter.js +7 -3
- package/bin/session/execution/SessionAssistantOutputAdapter.js.map +1 -1
- package/bin/session/messages/{SessionActionMessageWriter.d.ts → SessionAgentActionPartWriter.d.ts} +7 -7
- package/bin/session/messages/SessionAgentActionPartWriter.d.ts.map +1 -0
- package/bin/session/messages/{SessionActionMessageWriter.js → SessionAgentActionPartWriter.js} +7 -7
- package/bin/session/messages/SessionAgentActionPartWriter.js.map +1 -0
- package/bin/session/messages/SessionAgentContent.d.ts +10 -0
- package/bin/session/messages/SessionAgentContent.d.ts.map +1 -0
- package/bin/session/messages/SessionAgentContent.js +44 -0
- package/bin/session/messages/SessionAgentContent.js.map +1 -0
- package/bin/session/messages/SessionAgentMessageState.d.ts +50 -0
- package/bin/session/messages/SessionAgentMessageState.d.ts.map +1 -0
- package/bin/session/messages/SessionAgentMessageState.js +283 -0
- package/bin/session/messages/SessionAgentMessageState.js.map +1 -0
- package/bin/session/messages/{SessionAssistantMessageWriter.d.ts → SessionAgentMessageWriter.d.ts} +22 -8
- package/bin/session/messages/SessionAgentMessageWriter.d.ts.map +1 -0
- package/bin/session/messages/{SessionAssistantMessageWriter.js → SessionAgentMessageWriter.js} +54 -42
- package/bin/session/messages/SessionAgentMessageWriter.js.map +1 -0
- package/bin/session/messages/SessionForkMessageFiles.d.ts +11 -0
- package/bin/session/messages/SessionForkMessageFiles.d.ts.map +1 -1
- package/bin/session/messages/SessionForkMessageFiles.js +8 -2
- package/bin/session/messages/SessionForkMessageFiles.js.map +1 -1
- package/bin/session/messages/SessionMessageInteractionWriter.d.ts +6 -6
- package/bin/session/messages/SessionMessageInteractionWriter.d.ts.map +1 -1
- package/bin/session/messages/SessionMessageInteractionWriter.js +4 -4
- package/bin/session/messages/SessionMessageInteractionWriter.js.map +1 -1
- package/bin/session/messages/SessionMessageText.d.ts +3 -3
- package/bin/session/messages/SessionMessageText.d.ts.map +1 -1
- package/bin/session/messages/SessionMessageText.js +0 -2
- package/bin/session/messages/SessionMessageText.js.map +1 -1
- package/bin/session/messages/SessionTurnFileDiffData.d.ts +3 -3
- package/bin/session/messages/SessionTurnFileDiffData.d.ts.map +1 -1
- package/bin/session/messages/SessionTurnFileDiffData.js.map +1 -1
- package/bin/session/messages/SessionUserMessage.d.ts +3 -3
- package/bin/session/messages/SessionUserMessage.d.ts.map +1 -1
- package/bin/session/messages/SessionUserMessage.js +6 -3
- package/bin/session/messages/SessionUserMessage.js.map +1 -1
- package/bin/session/preview/SessionMessagePreview.d.ts.map +1 -1
- package/bin/session/preview/SessionMessagePreview.js +25 -9
- package/bin/session/preview/SessionMessagePreview.js.map +1 -1
- package/bin/session/runtime/SessionTurnCompletion.d.ts +15 -0
- package/bin/session/runtime/SessionTurnCompletion.d.ts.map +1 -0
- package/bin/session/runtime/SessionTurnCompletion.js +126 -0
- package/bin/session/runtime/SessionTurnCompletion.js.map +1 -0
- package/bin/session/runtime/SessionTurnContext.d.ts.map +1 -1
- package/bin/session/runtime/SessionTurnContext.js +14 -19
- package/bin/session/runtime/SessionTurnContext.js.map +1 -1
- package/bin/session/runtime/SessionTurnFileDiff.d.ts +12 -0
- package/bin/session/runtime/SessionTurnFileDiff.d.ts.map +1 -0
- package/bin/session/runtime/SessionTurnFileDiff.js +71 -0
- package/bin/session/runtime/SessionTurnFileDiff.js.map +1 -0
- package/bin/session/storage/LocalSessionDataStore.d.ts +6 -41
- package/bin/session/storage/LocalSessionDataStore.d.ts.map +1 -1
- package/bin/session/storage/LocalSessionDataStore.js +19 -101
- package/bin/session/storage/LocalSessionDataStore.js.map +1 -1
- package/bin/session/storage/LocalSessionStore.d.ts +13 -5
- package/bin/session/storage/LocalSessionStore.d.ts.map +1 -1
- package/bin/session/storage/LocalSessionStore.js +153 -30
- package/bin/session/storage/LocalSessionStore.js.map +1 -1
- package/bin/session/storage/LocalStorePaths.d.ts +4 -16
- package/bin/session/storage/LocalStorePaths.d.ts.map +1 -1
- package/bin/session/storage/LocalStorePaths.js +7 -31
- package/bin/session/storage/LocalStorePaths.js.map +1 -1
- package/bin/session/storage/Metadata.d.ts +4 -4
- package/bin/session/storage/Metadata.d.ts.map +1 -1
- package/bin/session/storage/Metadata.js +3 -11
- package/bin/session/storage/Metadata.js.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.d.ts +4 -4
- package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.js +3 -3
- package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
- package/bin/session/storage/SessionStorageCodec.d.ts +59 -0
- package/bin/session/storage/SessionStorageCodec.d.ts.map +1 -0
- package/bin/session/storage/SessionStorageCodec.js +331 -0
- package/bin/session/storage/SessionStorageCodec.js.map +1 -0
- package/bin/session/storage/SessionStorageSchema.d.ts +11 -0
- package/bin/session/storage/SessionStorageSchema.d.ts.map +1 -0
- package/bin/session/storage/SessionStorageSchema.js +131 -0
- package/bin/session/storage/SessionStorageSchema.js.map +1 -0
- package/bin/session/storage/SqliteSessionStorage.d.ts +120 -0
- package/bin/session/storage/SqliteSessionStorage.d.ts.map +1 -0
- package/bin/session/storage/SqliteSessionStorage.js +581 -0
- package/bin/session/storage/SqliteSessionStorage.js.map +1 -0
- package/bin/tools/ask/AskQuestionsTool.d.ts.map +1 -1
- package/bin/tools/ask/AskQuestionsTool.js +3 -1
- package/bin/tools/ask/AskQuestionsTool.js.map +1 -1
- package/bin/types/action/ActionResult.d.ts +9 -9
- package/bin/types/action/ActionResult.d.ts.map +1 -1
- package/bin/types/action/ActionResult.js +1 -1
- package/bin/types/agent/AgentOptions.d.ts +10 -0
- package/bin/types/agent/AgentOptions.d.ts.map +1 -1
- package/bin/types/agent/AgentStorage.d.ts +3 -1
- package/bin/types/agent/AgentStorage.d.ts.map +1 -1
- package/bin/types/agent/SessionActor.d.ts +0 -9
- package/bin/types/agent/SessionActor.d.ts.map +1 -1
- package/bin/types/agent/SessionTypes.d.ts +5 -5
- package/bin/types/agent/SessionTypes.d.ts.map +1 -1
- package/bin/types/executor/SessionAssistantOutput.d.ts +15 -4
- package/bin/types/executor/SessionAssistantOutput.d.ts.map +1 -1
- package/bin/types/executor/SessionTurnContext.d.ts +17 -19
- package/bin/types/executor/SessionTurnContext.d.ts.map +1 -1
- package/bin/types/sdk/AgentSessionAction.d.ts +1 -1
- package/bin/types/sdk/AgentSessionAction.js +1 -1
- package/bin/types/sdk/AgentSessionPrompt.d.ts +14 -5
- package/bin/types/sdk/AgentSessionPrompt.d.ts.map +1 -1
- package/bin/types/sdk/AgentSessionPrompt.js +1 -1
- package/bin/types/sdk/AgentSessionPrompt.js.map +1 -1
- package/bin/types/session/AdaptivePartContextPolicy.d.ts +31 -0
- package/bin/types/session/AdaptivePartContextPolicy.d.ts.map +1 -0
- package/bin/types/session/AdaptivePartContextPolicy.js +7 -0
- package/bin/types/session/AdaptivePartContextPolicy.js.map +1 -0
- package/bin/types/session/{SessionAssistantMessageState.d.ts → SessionAgentMessageState.d.ts} +7 -7
- package/bin/types/session/SessionAgentMessageState.d.ts.map +1 -0
- package/bin/types/session/{SessionAssistantMessageState.js → SessionAgentMessageState.js} +1 -1
- package/bin/types/session/SessionAgentMessageState.js.map +1 -0
- package/bin/types/session/SessionCommand.d.ts +19 -7
- package/bin/types/session/SessionCommand.d.ts.map +1 -1
- package/bin/types/session/SessionComposer.d.ts +43 -43
- package/bin/types/session/SessionComposer.d.ts.map +1 -1
- package/bin/types/session/SessionComposer.js +2 -2
- package/bin/types/session/SessionComposition.d.ts +2 -5
- package/bin/types/session/SessionComposition.d.ts.map +1 -1
- package/bin/types/session/SessionContextPolicy.d.ts +70 -0
- package/bin/types/session/SessionContextPolicy.d.ts.map +1 -0
- package/bin/types/session/SessionContextPolicy.js +8 -0
- package/bin/types/session/SessionContextPolicy.js.map +1 -0
- package/bin/types/session/SessionExecution.d.ts +5 -32
- package/bin/types/session/SessionExecution.d.ts.map +1 -1
- package/bin/types/session/SessionLoop.d.ts +3 -5
- package/bin/types/session/SessionLoop.d.ts.map +1 -1
- package/bin/types/session/SessionMessages.d.ts +18 -28
- package/bin/types/session/SessionMessages.d.ts.map +1 -1
- package/bin/types/session/SessionOptions.d.ts +5 -5
- package/bin/types/session/SessionOptions.d.ts.map +1 -1
- package/bin/types/session/SessionPort.d.ts +8 -9
- package/bin/types/session/SessionPort.d.ts.map +1 -1
- package/bin/types/session/SessionState.d.ts +2 -5
- package/bin/types/session/SessionState.d.ts.map +1 -1
- package/bin/types/session/SessionSystem.d.ts +14 -0
- package/bin/types/session/SessionSystem.d.ts.map +1 -1
- package/bin/types/session/SessionTool.d.ts +2 -2
- package/bin/types/session/SessionTool.d.ts.map +1 -1
- package/bin/types/session/SessionTurnCompletion.d.ts +23 -0
- package/bin/types/session/SessionTurnCompletion.d.ts.map +1 -0
- package/bin/types/session/SessionTurnCompletion.js +8 -0
- package/bin/types/session/SessionTurnCompletion.js.map +1 -0
- package/bin/types/session/SessionTurnFileDiff.d.ts +42 -0
- package/bin/types/session/SessionTurnFileDiff.d.ts.map +1 -0
- package/bin/types/session/SessionTurnFileDiff.js +8 -0
- package/bin/types/session/SessionTurnFileDiff.js.map +1 -0
- package/bin/types/store/LocalStore.d.ts +6 -13
- package/bin/types/store/LocalStore.d.ts.map +1 -1
- package/bin/types/store/LocalStore.js +1 -1
- package/bin/types/store/SessionStorage.d.ts +103 -0
- package/bin/types/store/SessionStorage.d.ts.map +1 -0
- package/bin/types/store/SessionStorage.js +8 -0
- package/bin/types/store/SessionStorage.js.map +1 -0
- package/bin/types/store/SessionStore.d.ts +5 -3
- package/bin/types/store/SessionStore.d.ts.map +1 -1
- package/package.json +2 -2
- package/scripts/core-engine-context-compaction.test.mjs +96 -219
- package/scripts/core-engine-model-stream.test.mjs +2 -4
- package/scripts/executor-failure-result.test.mjs +14 -17
- package/scripts/session-agent-message-part-order.test.mjs +63 -0
- package/scripts/session-initialization.test.mjs +141 -0
- package/scripts/session-message-timeline.test.mjs +5 -6
- package/scripts/session-messages-error-part.test.mjs +175 -0
- package/scripts/session-messages-error.test.mjs +47 -6
- package/scripts/session-prompt-idempotency.test.mjs +91 -0
- package/scripts/session-queue.test.mjs +13 -12
- package/scripts/session-storage-sqlite.test.mjs +209 -0
- package/scripts/session-turn-context.test.mjs +21 -33
- package/src/agent/Agent.ts +48 -12
- package/src/agent/AgentInstructions.ts +2 -2
- package/src/agent/AgentMemoryStorage.ts +1 -0
- package/src/agent/AgentSessions.ts +31 -12
- package/src/executor/Executor.ts +47 -184
- package/src/executor/README.md +12 -13
- package/src/executor/composer/system/default/SystemPromptAssets.ts +0 -6
- package/src/executor/core-engine/CoreEngineContextCompaction.ts +3 -135
- package/src/executor/core-engine/CoreEngineRunner.ts +51 -273
- package/src/executor/core-engine/CoreEngineSignals.ts +9 -9
- package/src/executor/messages/SessionAttachmentMapper.ts +4 -4
- package/src/executor/messages/SessionMessageLog.ts +3 -3
- package/src/executor/messages/SessionModelMessages.ts +85 -103
- package/src/executor/model/ModelStepRunner.ts +9 -41
- package/src/executor/services/ExecutorRecoveryPolicy.ts +34 -127
- package/src/executor/types/SessionHistoryMeta.ts +1 -3
- package/src/index.ts +39 -47
- package/src/session/DefaultSessionComposer.ts +33 -30
- package/src/session/Session.ts +96 -227
- package/src/session/SessionComposition.ts +28 -65
- package/src/session/SessionLoop.ts +267 -370
- package/src/session/SessionMessages.ts +363 -242
- package/src/session/SessionQueue.ts +3 -2
- package/src/session/SessionState.ts +55 -101
- package/src/session/SessionSystem.ts +51 -1
- package/src/session/SessionTitle.ts +4 -4
- package/src/session/browse/Browse.ts +182 -468
- package/src/session/browse/SessionMessageTimeline.ts +63 -38
- package/src/session/composer/policies/AdaptivePartContextPolicy.ts +307 -0
- package/src/session/composer/policies/FullHistoryContextPolicy.ts +36 -0
- package/src/session/composer/policies/SessionSummaryPrompts.ts +42 -0
- package/src/session/execution/SessionAssistantOutputAdapter.ts +25 -11
- package/src/session/messages/{SessionActionMessageWriter.ts → SessionAgentActionPartWriter.ts} +8 -8
- package/src/session/messages/SessionAgentContent.ts +56 -0
- package/src/session/messages/SessionAgentMessageState.ts +378 -0
- package/src/session/messages/{SessionAssistantMessageWriter.ts → SessionAgentMessageWriter.ts} +89 -60
- package/src/session/messages/SessionForkMessageFiles.ts +25 -1
- package/src/session/messages/SessionMessageInteractionWriter.ts +19 -19
- package/src/session/messages/SessionMessageText.ts +3 -4
- package/src/session/messages/SessionTurnFileDiffData.ts +3 -3
- package/src/session/messages/SessionUserMessage.ts +10 -7
- package/src/session/preview/SessionMessagePreview.ts +26 -9
- package/src/session/runtime/SessionTurnCompletion.ts +159 -0
- package/src/session/runtime/SessionTurnContext.ts +20 -21
- package/src/session/runtime/SessionTurnFileDiff.ts +105 -0
- package/src/session/storage/LocalSessionDataStore.ts +27 -176
- package/src/session/storage/LocalSessionStore.ts +170 -38
- package/src/session/storage/LocalStorePaths.ts +14 -86
- package/src/session/storage/Metadata.ts +4 -13
- package/src/session/storage/RuntimeSessionPort.ts +7 -7
- package/src/session/storage/SessionStorageCodec.ts +465 -0
- package/src/session/storage/SessionStorageSchema.ts +132 -0
- package/src/session/storage/SqliteSessionStorage.ts +776 -0
- package/src/tools/ask/AskQuestionsTool.ts +3 -1
- package/src/types/action/ActionResult.ts +10 -10
- package/src/types/agent/AgentOptions.ts +11 -0
- package/src/types/agent/AgentStorage.ts +3 -1
- package/src/types/agent/SessionActor.ts +0 -10
- package/src/types/agent/SessionTypes.ts +5 -5
- package/src/types/executor/SessionAssistantOutput.ts +15 -4
- package/src/types/executor/SessionTurnContext.ts +25 -22
- package/src/types/sdk/AgentSessionAction.ts +1 -1
- package/src/types/sdk/AgentSessionPrompt.ts +15 -5
- package/src/types/session/AdaptivePartContextPolicy.ts +37 -0
- package/src/types/session/{SessionAssistantMessageState.ts → SessionAgentMessageState.ts} +6 -6
- package/src/types/session/SessionCommand.ts +21 -7
- package/src/types/session/SessionComposer.ts +48 -48
- package/src/types/session/SessionComposition.ts +2 -5
- package/src/types/session/SessionContextPolicy.ts +77 -0
- package/src/types/session/SessionExecution.ts +11 -36
- package/src/types/session/SessionLoop.ts +3 -8
- package/src/types/session/SessionMessages.ts +19 -29
- package/src/types/session/SessionOptions.ts +5 -5
- package/src/types/session/SessionPort.ts +10 -10
- package/src/types/session/SessionState.ts +2 -5
- package/src/types/session/SessionSystem.ts +19 -0
- package/src/types/session/SessionTool.ts +2 -2
- package/src/types/session/SessionTurnCompletion.ts +24 -0
- package/src/types/session/SessionTurnFileDiff.ts +44 -0
- package/src/types/store/LocalStore.ts +6 -14
- package/src/types/store/SessionStorage.ts +121 -0
- package/src/types/store/SessionStore.ts +6 -3
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts +0 -45
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts.map +0 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js +0 -144
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js.map +0 -1
- package/bin/executor/composer/system/SessionSystemComposer.d.ts +0 -23
- package/bin/executor/composer/system/SessionSystemComposer.d.ts.map +0 -1
- package/bin/executor/composer/system/SessionSystemComposer.js +0 -9
- package/bin/executor/composer/system/SessionSystemComposer.js.map +0 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +0 -43
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts.map +0 -1
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js +0 -42
- package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js.map +0 -1
- package/bin/executor/composer/system/default/PromptRenderer.d.ts +0 -35
- package/bin/executor/composer/system/default/PromptRenderer.d.ts.map +0 -1
- package/bin/executor/composer/system/default/PromptRenderer.js +0 -23
- package/bin/executor/composer/system/default/PromptRenderer.js.map +0 -1
- package/bin/executor/composer/system/default/SystemDomain.d.ts +0 -151
- package/bin/executor/composer/system/default/SystemDomain.d.ts.map +0 -1
- package/bin/executor/composer/system/default/SystemDomain.js +0 -162
- package/bin/executor/composer/system/default/SystemDomain.js.map +0 -1
- package/bin/executor/composer/system/default/assets/task.prompt.d.ts +0 -7
- package/bin/executor/composer/system/default/assets/task.prompt.d.ts.map +0 -1
- package/bin/executor/composer/system/default/assets/task.prompt.js +0 -8
- package/bin/executor/composer/system/default/assets/task.prompt.js.map +0 -1
- package/bin/executor/composer/system/default/variables/GeoContext.d.ts +0 -14
- package/bin/executor/composer/system/default/variables/GeoContext.d.ts.map +0 -1
- package/bin/executor/composer/system/default/variables/GeoContext.js +0 -150
- package/bin/executor/composer/system/default/variables/GeoContext.js.map +0 -1
- package/bin/executor/composer/system/default/variables/PromptTypes.d.ts +0 -36
- package/bin/executor/composer/system/default/variables/PromptTypes.d.ts.map +0 -1
- package/bin/executor/composer/system/default/variables/PromptTypes.js +0 -9
- package/bin/executor/composer/system/default/variables/PromptTypes.js.map +0 -1
- package/bin/executor/composer/system/default/variables/VariableReplacer.d.ts +0 -61
- package/bin/executor/composer/system/default/variables/VariableReplacer.d.ts.map +0 -1
- package/bin/executor/composer/system/default/variables/VariableReplacer.js +0 -93
- package/bin/executor/composer/system/default/variables/VariableReplacer.js.map +0 -1
- package/bin/executor/core-engine/CoreEngineMessageState.d.ts +0 -63
- package/bin/executor/core-engine/CoreEngineMessageState.d.ts.map +0 -1
- package/bin/executor/core-engine/CoreEngineMessageState.js +0 -81
- package/bin/executor/core-engine/CoreEngineMessageState.js.map +0 -1
- package/bin/session/SessionCommand.d.ts +0 -24
- package/bin/session/SessionCommand.d.ts.map +0 -1
- package/bin/session/SessionCommand.js +0 -35
- package/bin/session/SessionCommand.js.map +0 -1
- package/bin/session/messages/SessionActionMessageWriter.d.ts.map +0 -1
- package/bin/session/messages/SessionActionMessageWriter.js.map +0 -1
- package/bin/session/messages/SessionAssistantMessageState.d.ts +0 -49
- package/bin/session/messages/SessionAssistantMessageState.d.ts.map +0 -1
- package/bin/session/messages/SessionAssistantMessageState.js +0 -296
- package/bin/session/messages/SessionAssistantMessageState.js.map +0 -1
- package/bin/session/messages/SessionAssistantMessageWriter.d.ts.map +0 -1
- package/bin/session/messages/SessionAssistantMessageWriter.js.map +0 -1
- package/bin/session/messages/SessionMessageCompaction.d.ts +0 -29
- package/bin/session/messages/SessionMessageCompaction.d.ts.map +0 -1
- package/bin/session/messages/SessionMessageCompaction.js +0 -128
- package/bin/session/messages/SessionMessageCompaction.js.map +0 -1
- package/bin/session/runtime/SessionCompactOperation.d.ts +0 -29
- package/bin/session/runtime/SessionCompactOperation.d.ts.map +0 -1
- package/bin/session/runtime/SessionCompactOperation.js +0 -66
- package/bin/session/runtime/SessionCompactOperation.js.map +0 -1
- package/bin/session/runtime/SessionHistoryCompaction.d.ts +0 -22
- package/bin/session/runtime/SessionHistoryCompaction.d.ts.map +0 -1
- package/bin/session/runtime/SessionHistoryCompaction.js +0 -31
- package/bin/session/runtime/SessionHistoryCompaction.js.map +0 -1
- package/bin/session/storage/JsonlSessionMessageStore.d.ts +0 -77
- package/bin/session/storage/JsonlSessionMessageStore.d.ts.map +0 -1
- package/bin/session/storage/JsonlSessionMessageStore.js +0 -419
- package/bin/session/storage/JsonlSessionMessageStore.js.map +0 -1
- package/bin/session/storage/SessionMessageStoreFactory.d.ts +0 -12
- package/bin/session/storage/SessionMessageStoreFactory.d.ts.map +0 -1
- package/bin/session/storage/SessionMessageStoreFactory.js +0 -13
- package/bin/session/storage/SessionMessageStoreFactory.js.map +0 -1
- package/bin/types/common/Template.d.ts +0 -9
- package/bin/types/common/Template.d.ts.map +0 -1
- package/bin/types/common/Template.js +0 -9
- package/bin/types/common/Template.js.map +0 -1
- package/bin/types/sdk/AgentSessionCompact.d.ts +0 -30
- package/bin/types/sdk/AgentSessionCompact.d.ts.map +0 -1
- package/bin/types/sdk/AgentSessionCompact.js +0 -8
- package/bin/types/sdk/AgentSessionCompact.js.map +0 -1
- package/bin/types/session/SessionAssistantMessageState.d.ts.map +0 -1
- package/bin/types/session/SessionAssistantMessageState.js.map +0 -1
- package/bin/types/session/SessionSegment.d.ts +0 -57
- package/bin/types/session/SessionSegment.d.ts.map +0 -1
- package/bin/types/session/SessionSegment.js +0 -8
- package/bin/types/session/SessionSegment.js.map +0 -1
- package/bin/types/store/SessionDataStore.d.ts +0 -88
- package/bin/types/store/SessionDataStore.d.ts.map +0 -1
- package/bin/types/store/SessionDataStore.js +0 -9
- package/bin/types/store/SessionDataStore.js.map +0 -1
- package/bin/utils/Template.d.ts +0 -17
- package/bin/utils/Template.d.ts.map +0 -1
- package/bin/utils/Template.js +0 -24
- package/bin/utils/Template.js.map +0 -1
- package/scripts/session-compaction-prompts.test.mjs +0 -49
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.ts +0 -162
- package/src/executor/composer/system/SessionSystemComposer.ts +0 -25
- package/src/executor/composer/system/default/DefaultSessionSystemComposer.ts +0 -76
- package/src/executor/composer/system/default/PromptRenderer.ts +0 -52
- package/src/executor/composer/system/default/SystemDomain.ts +0 -301
- package/src/executor/composer/system/default/assets/task.prompt.ts +0 -9
- package/src/executor/composer/system/default/assets/task.prompt.ts.txt +0 -33
- package/src/executor/composer/system/default/variables/GeoContext.ts +0 -180
- package/src/executor/composer/system/default/variables/PromptTypes.ts +0 -37
- package/src/executor/composer/system/default/variables/VariableReplacer.ts +0 -154
- package/src/executor/core-engine/CoreEngineMessageState.ts +0 -117
- package/src/session/SessionCommand.ts +0 -42
- package/src/session/messages/SessionAssistantMessageState.ts +0 -392
- package/src/session/messages/SessionMessageCompaction.ts +0 -160
- package/src/session/runtime/SessionCompactOperation.ts +0 -93
- package/src/session/runtime/SessionHistoryCompaction.ts +0 -45
- package/src/session/storage/JsonlSessionMessageStore.ts +0 -513
- package/src/session/storage/SessionMessageStoreFactory.ts +0 -18
- package/src/types/common/Template.ts +0 -9
- package/src/types/sdk/AgentSessionCompact.ts +0 -32
- package/src/types/session/SessionSegment.ts +0 -62
- package/src/types/store/SessionDataStore.ts +0 -113
- package/src/utils/Template.ts +0 -28
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 无身份 Agent Content 到 canonical Agent Part 的确定性转换。
|
|
3
|
+
*
|
|
4
|
+
* 本模块只补齐 Message 聚合拥有的 identity、sequence 与文本终态,不持有 Writer、
|
|
5
|
+
* Store 或 Turn 生命周期。所有非模型 Agent Content 入口共用这一条转换规则。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
SessionAgentContent,
|
|
10
|
+
SessionAgentMessagePart,
|
|
11
|
+
} from "@downcity/type";
|
|
12
|
+
|
|
13
|
+
/** 把单个无身份 Agent Content 转换为 canonical Agent Part。 */
|
|
14
|
+
export function create_session_agent_content_part(
|
|
15
|
+
content: SessionAgentContent,
|
|
16
|
+
part_id: string,
|
|
17
|
+
sequence: number,
|
|
18
|
+
): SessionAgentMessagePart {
|
|
19
|
+
switch (content.type) {
|
|
20
|
+
case "text":
|
|
21
|
+
return {
|
|
22
|
+
part_id,
|
|
23
|
+
sequence,
|
|
24
|
+
type: "text",
|
|
25
|
+
text: content.text,
|
|
26
|
+
state: "done",
|
|
27
|
+
};
|
|
28
|
+
case "file":
|
|
29
|
+
return {
|
|
30
|
+
part_id,
|
|
31
|
+
sequence,
|
|
32
|
+
type: "file",
|
|
33
|
+
media_type: content.media_type,
|
|
34
|
+
url: content.url,
|
|
35
|
+
...(content.filename ? { filename: content.filename } : {}),
|
|
36
|
+
};
|
|
37
|
+
case "data":
|
|
38
|
+
return {
|
|
39
|
+
part_id,
|
|
40
|
+
sequence,
|
|
41
|
+
type: "data",
|
|
42
|
+
data_type: content.data_type,
|
|
43
|
+
data: content.data,
|
|
44
|
+
...(content.data_id ? { data_id: content.data_id } : {}),
|
|
45
|
+
};
|
|
46
|
+
default:
|
|
47
|
+
return assert_never(content);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 联合类型增加成员时强制 canonical 转换同步处理。 */
|
|
52
|
+
function assert_never(value: never): never {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Unsupported Agent content: ${String((value as { type?: unknown }).type)}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canonical Agent Message 的运行投影与检查点提交器。
|
|
3
|
+
*
|
|
4
|
+
* Model stream 只更新内存投影并发布实时 Mutation;完整 Step、Tool、Interaction
|
|
5
|
+
* 与 Message 收口才通过一次 SQLite 事务提交稳定快照。数据库不承担流事件日志职责。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { generate_id } from "@/utils/Id.js";
|
|
9
|
+
import { SessionMessageInteractionWriter } from "@/session/messages/SessionMessageInteractionWriter.js";
|
|
10
|
+
import type {
|
|
11
|
+
SessionAgentErrorPart,
|
|
12
|
+
SessionAgentInteractionPart,
|
|
13
|
+
SessionAgentMessage,
|
|
14
|
+
SessionAgentMessagePart,
|
|
15
|
+
SessionAgentToolPart,
|
|
16
|
+
SessionMessage,
|
|
17
|
+
SessionMutation,
|
|
18
|
+
} from "@downcity/type";
|
|
19
|
+
import type {
|
|
20
|
+
SessionInteractionCloseInput,
|
|
21
|
+
SessionInteractionRequest,
|
|
22
|
+
SessionInteractionResponse,
|
|
23
|
+
} from "@downcity/type";
|
|
24
|
+
import type { SessionStreamingToolLocation } from "@/types/session/SessionTool.js";
|
|
25
|
+
import type { SessionAgentMessageStateOptions } from "@/types/session/SessionAgentMessageState.js";
|
|
26
|
+
|
|
27
|
+
/** 管理 Agent Message 运行投影、语义检查点与 Interaction 原子状态。 */
|
|
28
|
+
export class SessionAgentMessageState {
|
|
29
|
+
private readonly session_id: string;
|
|
30
|
+
private readonly options: SessionAgentMessageStateOptions;
|
|
31
|
+
/** 同一 Agent Message 的检查点严格串行,不同 Message 可以独立提交。 */
|
|
32
|
+
private readonly write_chains = new Map<string, Promise<void>>();
|
|
33
|
+
private readonly interaction_writer: SessionMessageInteractionWriter;
|
|
34
|
+
|
|
35
|
+
constructor(options: SessionAgentMessageStateOptions) {
|
|
36
|
+
this.session_id = options.session_id;
|
|
37
|
+
this.options = options;
|
|
38
|
+
this.interaction_writer = new SessionMessageInteractionWriter({
|
|
39
|
+
list_messages: options.list_messages,
|
|
40
|
+
find_streaming_tool: (tool_call_id) => this.find_streaming_tool(tool_call_id),
|
|
41
|
+
enqueue_assistant_write: (message_id, operation) =>
|
|
42
|
+
this.enqueue_write(message_id, operation),
|
|
43
|
+
commit_assistant_snapshot: (current, parts) =>
|
|
44
|
+
this.persist_snapshot(current.message_id, parts),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 只在内存中创建或替换一个 Part,并立即发布实时完整 Part。 */
|
|
49
|
+
project_part(message_id: string, part: SessionAgentMessagePart): void {
|
|
50
|
+
const current = this.require_streaming_agent(message_id);
|
|
51
|
+
const existing = current.parts.find((item) => item.part_id === part.part_id);
|
|
52
|
+
if (existing && existing.sequence !== part.sequence) {
|
|
53
|
+
throw new Error(`Agent Part sequence changed: ${part.part_id}`);
|
|
54
|
+
}
|
|
55
|
+
const projected: SessionAgentMessage = {
|
|
56
|
+
...current,
|
|
57
|
+
updated_at: Date.now(),
|
|
58
|
+
parts: (existing
|
|
59
|
+
? current.parts.map((item) => item.part_id === part.part_id ? structuredClone(part) : item)
|
|
60
|
+
: [...current.parts, structuredClone(part)]
|
|
61
|
+
).sort((left, right) => left.sequence - right.sequence),
|
|
62
|
+
};
|
|
63
|
+
this.options.project_mutation({
|
|
64
|
+
mutation_id: generate_id(),
|
|
65
|
+
variant: "part",
|
|
66
|
+
type: part.type,
|
|
67
|
+
message_id,
|
|
68
|
+
revision: current.revision,
|
|
69
|
+
session_id: this.session_id,
|
|
70
|
+
...(current.turn_id ? { turn_id: current.turn_id } : {}),
|
|
71
|
+
created_at: projected.updated_at,
|
|
72
|
+
part_id: part.part_id,
|
|
73
|
+
part: structuredClone(part),
|
|
74
|
+
} as SessionMutation, projected);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 只在内存中追加文本、推理或 Tool 输入增量,并立即发布 delta。 */
|
|
78
|
+
project_delta(
|
|
79
|
+
message_id: string,
|
|
80
|
+
part_id: string,
|
|
81
|
+
type: "text" | "reasoning" | "tool_input",
|
|
82
|
+
delta: string,
|
|
83
|
+
tool_call_id?: string,
|
|
84
|
+
): void {
|
|
85
|
+
if (!delta) return;
|
|
86
|
+
const current = this.require_streaming_agent(message_id);
|
|
87
|
+
let matched = false;
|
|
88
|
+
const projected: SessionAgentMessage = {
|
|
89
|
+
...current,
|
|
90
|
+
updated_at: Date.now(),
|
|
91
|
+
parts: current.parts.map((part) => {
|
|
92
|
+
if (part.part_id !== part_id) return part;
|
|
93
|
+
if (
|
|
94
|
+
(type === "text" || type === "reasoning") &&
|
|
95
|
+
(part.type === "text" || part.type === "reasoning") &&
|
|
96
|
+
part.type === type
|
|
97
|
+
) {
|
|
98
|
+
matched = true;
|
|
99
|
+
return { ...part, text: part.text + delta };
|
|
100
|
+
}
|
|
101
|
+
if (
|
|
102
|
+
type === "tool_input" && part.type === "tool" &&
|
|
103
|
+
part.tool_call_id === tool_call_id && part.state === "input-streaming"
|
|
104
|
+
) {
|
|
105
|
+
matched = true;
|
|
106
|
+
return { ...part, input_text: `${part.input_text || ""}${delta}` };
|
|
107
|
+
}
|
|
108
|
+
throw new Error(`Delta target Part is incompatible: ${part_id}`);
|
|
109
|
+
}),
|
|
110
|
+
};
|
|
111
|
+
if (!matched) throw new Error(`Delta target Part does not exist: ${part_id}`);
|
|
112
|
+
this.options.project_mutation({
|
|
113
|
+
mutation_id: generate_id(),
|
|
114
|
+
variant: "delta",
|
|
115
|
+
type,
|
|
116
|
+
message_id,
|
|
117
|
+
revision: current.revision,
|
|
118
|
+
session_id: this.session_id,
|
|
119
|
+
...(current.turn_id ? { turn_id: current.turn_id } : {}),
|
|
120
|
+
created_at: projected.updated_at,
|
|
121
|
+
part_id,
|
|
122
|
+
...(tool_call_id ? { tool_call_id } : {}),
|
|
123
|
+
delta,
|
|
124
|
+
} as SessionMutation, projected);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 将当前内存投影作为一个稳定语义检查点原子提交。 */
|
|
128
|
+
async checkpoint(message_id: string): Promise<void> {
|
|
129
|
+
await this.enqueue_write(message_id, async () => {
|
|
130
|
+
const current = this.require_streaming_agent(message_id);
|
|
131
|
+
await this.persist_snapshot(message_id, current.parts);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 校验 Step Part identity 后原子提交模型给出的最终快照。 */
|
|
136
|
+
async commit_step(
|
|
137
|
+
message_id: string,
|
|
138
|
+
parts: SessionAgentMessagePart[],
|
|
139
|
+
): Promise<void> {
|
|
140
|
+
await this.enqueue_write(message_id, async () => {
|
|
141
|
+
const current = this.require_streaming_agent(message_id);
|
|
142
|
+
if (
|
|
143
|
+
parts.length !== current.parts.length ||
|
|
144
|
+
parts.some((part, index) =>
|
|
145
|
+
part.part_id !== current.parts[index]?.part_id ||
|
|
146
|
+
part.sequence !== current.parts[index]?.sequence
|
|
147
|
+
)
|
|
148
|
+
) {
|
|
149
|
+
throw new Error(`Agent step changed canonical Part identity: ${message_id}`);
|
|
150
|
+
}
|
|
151
|
+
await this.persist_snapshot(message_id, parts);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 原子追加一组非流式 Agent Parts。 */
|
|
156
|
+
async commit_parts(
|
|
157
|
+
message_id: string,
|
|
158
|
+
parts: readonly SessionAgentMessagePart[],
|
|
159
|
+
): Promise<void> {
|
|
160
|
+
if (parts.length === 0) return;
|
|
161
|
+
await this.enqueue_write(message_id, async () => {
|
|
162
|
+
const current = this.require_streaming_agent(message_id);
|
|
163
|
+
const replacements = new Map(parts.map((part) => [part.part_id, structuredClone(part)]));
|
|
164
|
+
const existing_ids = new Set(current.parts.map((part) => part.part_id));
|
|
165
|
+
const merged = current.parts.map((part) => replacements.get(part.part_id) ?? part);
|
|
166
|
+
for (const part of parts) {
|
|
167
|
+
if (!existing_ids.has(part.part_id)) merged.push(structuredClone(part));
|
|
168
|
+
}
|
|
169
|
+
await this.persist_snapshot(
|
|
170
|
+
message_id,
|
|
171
|
+
merged.sort((left, right) => left.sequence - right.sequence),
|
|
172
|
+
);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 丢弃当前未提交 Step 投影,并向订阅方恢复最近持久化快照。 */
|
|
177
|
+
async rollback_projection(message_id: string): Promise<void> {
|
|
178
|
+
await this.enqueue_write(message_id, async () => {
|
|
179
|
+
const persisted = await this.options.store.read_message(message_id);
|
|
180
|
+
if (!persisted || persisted.role !== "agent") {
|
|
181
|
+
throw new Error(`Persisted Agent Message not found: ${message_id}`);
|
|
182
|
+
}
|
|
183
|
+
this.options.accept_message(persisted);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 收口 Agent Message,并将局部非终态与失败原因一并原子提交。 */
|
|
188
|
+
async complete(
|
|
189
|
+
message_id: string,
|
|
190
|
+
outcome: "completed" | "stopped" | "failed",
|
|
191
|
+
error?: string,
|
|
192
|
+
): Promise<void> {
|
|
193
|
+
await this.enqueue_write(message_id, async () => {
|
|
194
|
+
const current = this.require_streaming_agent(message_id);
|
|
195
|
+
const completed_at = Date.now();
|
|
196
|
+
const interrupted_tool_ids = new Set(
|
|
197
|
+
current.parts.flatMap((part) =>
|
|
198
|
+
part.type === "interaction" && part.status === "pending" &&
|
|
199
|
+
part.request.source.tool_call_id
|
|
200
|
+
? [part.request.source.tool_call_id]
|
|
201
|
+
: [],
|
|
202
|
+
),
|
|
203
|
+
);
|
|
204
|
+
const parts: SessionAgentMessagePart[] = current.parts.map((part) => {
|
|
205
|
+
if (part.type === "text" || part.type === "reasoning") {
|
|
206
|
+
return { ...part, state: "done" as const };
|
|
207
|
+
}
|
|
208
|
+
if (part.type === "action" && part.state === "running") {
|
|
209
|
+
return {
|
|
210
|
+
...part,
|
|
211
|
+
state: outcome === "completed" ? "completed" as const : "failed" as const,
|
|
212
|
+
...(outcome === "completed" || part.description
|
|
213
|
+
? {}
|
|
214
|
+
: { description: error || "Action did not complete before Agent Message closed" }),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (part.type === "interaction" && part.status === "pending") {
|
|
218
|
+
return {
|
|
219
|
+
...part,
|
|
220
|
+
status: "cancelled" as const,
|
|
221
|
+
cancel_reason: "runtime_interrupted" as const,
|
|
222
|
+
resolved_at: completed_at,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (part.type === "tool" && part.state !== "completed" && part.state !== "failed") {
|
|
226
|
+
return {
|
|
227
|
+
...part,
|
|
228
|
+
state: "failed" as const,
|
|
229
|
+
error: outcome === "stopped" && part.state === "waiting-user" &&
|
|
230
|
+
interrupted_tool_ids.has(part.tool_call_id)
|
|
231
|
+
? "Interaction cancelled"
|
|
232
|
+
: error || "Tool did not complete before Agent Message closed",
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return part;
|
|
236
|
+
});
|
|
237
|
+
if (outcome === "stopped" && !parts.some((part) =>
|
|
238
|
+
part.type === "error" && part.code === "turn_stopped"
|
|
239
|
+
)) {
|
|
240
|
+
parts.push(create_terminal_error_part(parts, "turn_stopped", error || "Turn stopped"));
|
|
241
|
+
} else if (outcome === "failed" && error && !parts.some((part) => part.type === "error")) {
|
|
242
|
+
parts.push(create_terminal_error_part(parts, "turn_execution_failed", error));
|
|
243
|
+
}
|
|
244
|
+
await this.persist_snapshot(message_id, parts, "done");
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 读取当前流式 Agent Message 中的指定 Tool Part。 */
|
|
249
|
+
find_streaming_tool(tool_call_id: string): SessionStreamingToolLocation | undefined {
|
|
250
|
+
for (const message of this.options.list_messages()) {
|
|
251
|
+
if (message.role !== "agent" || message.state !== "streaming") continue;
|
|
252
|
+
const part = message.parts.find(
|
|
253
|
+
(item): item is SessionAgentToolPart =>
|
|
254
|
+
item.type === "tool" && item.tool_call_id === tool_call_id,
|
|
255
|
+
);
|
|
256
|
+
if (part) return { message_id: message.message_id, part };
|
|
257
|
+
}
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 返回当前 Session 全部待响应 Interaction。 */
|
|
262
|
+
list_pending_interactions(): SessionAgentInteractionPart[] {
|
|
263
|
+
return this.interaction_writer.list_pending();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 持久化 Interaction 请求及关联 Tool 状态。 */
|
|
267
|
+
async request_interaction(
|
|
268
|
+
request: SessionInteractionRequest,
|
|
269
|
+
): Promise<SessionAgentInteractionPart> {
|
|
270
|
+
return await this.interaction_writer.request(request);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 持久化 Interaction 响应及关联 Tool 状态。 */
|
|
274
|
+
async resolve_interaction(
|
|
275
|
+
interaction_id: string,
|
|
276
|
+
response: SessionInteractionResponse,
|
|
277
|
+
): Promise<SessionAgentInteractionPart> {
|
|
278
|
+
return await this.interaction_writer.resolve(interaction_id, response);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** 持久化 Interaction 关闭及关联 Tool 状态。 */
|
|
282
|
+
async close_interaction(
|
|
283
|
+
interaction_id: string,
|
|
284
|
+
input: SessionInteractionCloseInput,
|
|
285
|
+
): Promise<SessionAgentInteractionPart> {
|
|
286
|
+
return await this.interaction_writer.close(interaction_id, input);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** 原子保存完整 Message 快照;变更比较以数据库事实而非运行投影为准。 */
|
|
290
|
+
private async persist_snapshot(
|
|
291
|
+
message_id: string,
|
|
292
|
+
parts: SessionAgentMessagePart[],
|
|
293
|
+
state: SessionAgentMessage["state"] = "streaming",
|
|
294
|
+
): Promise<void> {
|
|
295
|
+
const persisted = await this.options.store.read_message(message_id);
|
|
296
|
+
if (!persisted || persisted.role !== "agent" || persisted.state !== "streaming") {
|
|
297
|
+
throw new Error(`Persisted streaming Agent Message not found: ${message_id}`);
|
|
298
|
+
}
|
|
299
|
+
const committed_at = Date.now();
|
|
300
|
+
const message: SessionAgentMessage = {
|
|
301
|
+
...persisted,
|
|
302
|
+
state,
|
|
303
|
+
revision: persisted.revision + 1,
|
|
304
|
+
updated_at: committed_at,
|
|
305
|
+
parts: structuredClone(parts).sort((left, right) => left.sequence - right.sequence),
|
|
306
|
+
};
|
|
307
|
+
const changed_parts = resolve_changed_parts(persisted.parts, message.parts);
|
|
308
|
+
await this.options.store.update_message({
|
|
309
|
+
message,
|
|
310
|
+
expected_revision: persisted.revision,
|
|
311
|
+
changed_parts,
|
|
312
|
+
});
|
|
313
|
+
this.options.accept_message(message);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** 串行执行同一 Message 的语义检查点。 */
|
|
317
|
+
private async enqueue_write<T>(
|
|
318
|
+
message_id: string,
|
|
319
|
+
operation: () => Promise<T>,
|
|
320
|
+
): Promise<T> {
|
|
321
|
+
const previous = this.write_chains.get(message_id) || Promise.resolve();
|
|
322
|
+
const result = previous.then(operation, operation);
|
|
323
|
+
const chain = result.then(() => undefined, () => undefined);
|
|
324
|
+
this.write_chains.set(message_id, chain);
|
|
325
|
+
try {
|
|
326
|
+
return await result;
|
|
327
|
+
} finally {
|
|
328
|
+
if (this.write_chains.get(message_id) === chain) this.write_chains.delete(message_id);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** 读取当前运行投影中的可写 Agent Message。 */
|
|
333
|
+
private require_streaming_agent(message_id: string): SessionAgentMessage {
|
|
334
|
+
const message = [...this.options.list_messages()].find(
|
|
335
|
+
(item) => item.message_id === message_id,
|
|
336
|
+
);
|
|
337
|
+
if (!message || message.role !== "agent") {
|
|
338
|
+
throw new Error(`Agent Message not found: ${message_id}`);
|
|
339
|
+
}
|
|
340
|
+
if (message.state !== "streaming") {
|
|
341
|
+
throw new Error(`Agent Message is already done: ${message_id}`);
|
|
342
|
+
}
|
|
343
|
+
return message;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** 为 Message 终止原因创建最后一个 Error Part。 */
|
|
348
|
+
function create_terminal_error_part(
|
|
349
|
+
parts: readonly SessionAgentMessagePart[],
|
|
350
|
+
code: string,
|
|
351
|
+
message: string,
|
|
352
|
+
): SessionAgentErrorPart {
|
|
353
|
+
return {
|
|
354
|
+
part_id: `error:${generate_id()}`,
|
|
355
|
+
sequence: parts.reduce(
|
|
356
|
+
(sequence, part) => Math.max(sequence, part.sequence + 1),
|
|
357
|
+
1,
|
|
358
|
+
),
|
|
359
|
+
type: "error",
|
|
360
|
+
scope: "turn",
|
|
361
|
+
code,
|
|
362
|
+
message,
|
|
363
|
+
recoverable: true,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** 返回相较上次稳定检查点新增或变化的 Parts。 */
|
|
368
|
+
function resolve_changed_parts(
|
|
369
|
+
current_parts: readonly SessionAgentMessagePart[],
|
|
370
|
+
next_parts: readonly SessionAgentMessagePart[],
|
|
371
|
+
): SessionAgentMessagePart[] {
|
|
372
|
+
const current_by_id = new Map(
|
|
373
|
+
current_parts.map((part) => [part.part_id, JSON.stringify(part)]),
|
|
374
|
+
);
|
|
375
|
+
return next_parts.filter(
|
|
376
|
+
(part) => current_by_id.get(part.part_id) !== JSON.stringify(part),
|
|
377
|
+
);
|
|
378
|
+
}
|