@downcity/agent 1.1.430 → 1.1.438
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
package/src/session/messages/{SessionAssistantMessageWriter.ts → SessionAgentMessageWriter.ts}
RENAMED
|
@@ -9,20 +9,22 @@ import type { ModelStreamEvent } from "@downcity/type";
|
|
|
9
9
|
import type { SessionMessages } from "@/session/SessionMessages.js";
|
|
10
10
|
import { to_session_json_value } from "@/session/messages/SessionJsonValue.js";
|
|
11
11
|
import { SessionToolPartGate } from "@/session/messages/SessionToolPartGate.js";
|
|
12
|
-
import type {
|
|
12
|
+
import type { SessionAgentContent } from "@downcity/type";
|
|
13
13
|
import type {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
SessionAgentErrorPart,
|
|
15
|
+
SessionAgentMessage,
|
|
16
|
+
SessionAgentMessagePart,
|
|
17
|
+
SessionAgentToolPart,
|
|
17
18
|
} from "@downcity/type";
|
|
18
19
|
import type {
|
|
19
20
|
SessionToolExecutionResult,
|
|
20
21
|
SessionToolInputReady,
|
|
21
22
|
} from "@/types/session/SessionTool.js";
|
|
22
23
|
import { generate_id } from "@/utils/Id.js";
|
|
24
|
+
import { create_session_agent_content_part } from "@/session/messages/SessionAgentContent.js";
|
|
23
25
|
|
|
24
26
|
/** 单个 Assistant Message 的流式 Writer。 */
|
|
25
|
-
export class
|
|
27
|
+
export class SessionAgentMessageWriter {
|
|
26
28
|
/** 当前 canonical Assistant Message 标识。 */
|
|
27
29
|
readonly message_id: string;
|
|
28
30
|
|
|
@@ -33,6 +35,7 @@ export class SessionAssistantMessageWriter {
|
|
|
33
35
|
private readonly tool_part_gate = new SessionToolPartGate();
|
|
34
36
|
private write_chain: Promise<void> = Promise.resolve();
|
|
35
37
|
private step_index = 0;
|
|
38
|
+
private current_step_id: string | null = null;
|
|
36
39
|
private step_active = false;
|
|
37
40
|
private closed = false;
|
|
38
41
|
|
|
@@ -49,6 +52,7 @@ export class SessionAssistantMessageWriter {
|
|
|
49
52
|
throw new Error("Assistant canonical step is already active");
|
|
50
53
|
}
|
|
51
54
|
this.step_index += 1;
|
|
55
|
+
this.current_step_id = `step:${this.message_id}:${this.step_index}`;
|
|
52
56
|
this.step_active = true;
|
|
53
57
|
this.content_part_ids.clear();
|
|
54
58
|
this.tool_call_ids.clear();
|
|
@@ -81,6 +85,7 @@ export class SessionAssistantMessageWriter {
|
|
|
81
85
|
state: "ready",
|
|
82
86
|
input: to_session_json_value(input.input),
|
|
83
87
|
});
|
|
88
|
+
await this.recorder.checkpoint_agent_message(this.message_id);
|
|
84
89
|
});
|
|
85
90
|
}
|
|
86
91
|
|
|
@@ -100,6 +105,7 @@ export class SessionAssistantMessageWriter {
|
|
|
100
105
|
state: "failed",
|
|
101
106
|
error: read_tool_error(result.output),
|
|
102
107
|
});
|
|
108
|
+
await this.recorder.checkpoint_agent_message(this.message_id);
|
|
103
109
|
});
|
|
104
110
|
}
|
|
105
111
|
|
|
@@ -108,7 +114,7 @@ export class SessionAssistantMessageWriter {
|
|
|
108
114
|
*
|
|
109
115
|
* 最终快照不能创建、删除或重排模型 Part;不一致意味着事件链不完整。
|
|
110
116
|
*/
|
|
111
|
-
async finish_step(parts:
|
|
117
|
+
async finish_step(parts: SessionAgentMessagePart[]): Promise<void> {
|
|
112
118
|
await this.enqueue_write(async () => {
|
|
113
119
|
if (!this.step_active) {
|
|
114
120
|
throw new Error("Assistant canonical step is not active");
|
|
@@ -122,7 +128,7 @@ export class SessionAssistantMessageWriter {
|
|
|
122
128
|
`part count ${current_parts.length} != ${parts.length}`,
|
|
123
129
|
);
|
|
124
130
|
}
|
|
125
|
-
const merged_parts = new Map<string,
|
|
131
|
+
const merged_parts = new Map<string, SessionAgentMessagePart>();
|
|
126
132
|
for (let index = 0; index < current_parts.length; index += 1) {
|
|
127
133
|
const current_part = current_parts[index];
|
|
128
134
|
const final_part = parts[index];
|
|
@@ -131,7 +137,7 @@ export class SessionAssistantMessageWriter {
|
|
|
131
137
|
this.merge_step_part(current_part, final_part, index),
|
|
132
138
|
);
|
|
133
139
|
}
|
|
134
|
-
await this.recorder.
|
|
140
|
+
await this.recorder.commit_agent_step(
|
|
135
141
|
this.message_id,
|
|
136
142
|
current.parts.map((part) => merged_parts.get(part.part_id) ?? part),
|
|
137
143
|
);
|
|
@@ -139,53 +145,66 @@ export class SessionAssistantMessageWriter {
|
|
|
139
145
|
});
|
|
140
146
|
}
|
|
141
147
|
|
|
142
|
-
/**
|
|
148
|
+
/** 释放异常 Step,并丢弃最近检查点之后的运行投影。 */
|
|
143
149
|
async abort_step(): Promise<void> {
|
|
144
150
|
await this.enqueue_write(async () => {
|
|
145
151
|
this.tool_part_gate.reject_pending("Assistant canonical step was aborted");
|
|
146
|
-
if (this.step_active)
|
|
152
|
+
if (this.step_active) {
|
|
153
|
+
await this.recorder.rollback_agent_projection(this.message_id);
|
|
154
|
+
this.reset_step_state();
|
|
155
|
+
}
|
|
147
156
|
});
|
|
148
157
|
}
|
|
149
158
|
|
|
150
159
|
/** 把 Action 产生的封闭内容追加到当前 Assistant Message。 */
|
|
151
|
-
async append_result_parts(parts: readonly
|
|
152
|
-
await this.enqueue_write(async () => {
|
|
160
|
+
async append_result_parts(parts: readonly SessionAgentContent[]): Promise<SessionAgentMessagePart[]> {
|
|
161
|
+
return await this.enqueue_write(async () => {
|
|
153
162
|
if (this.closed) throw new Error("Assistant Message writer is closed");
|
|
163
|
+
const appended_parts: SessionAgentMessagePart[] = [];
|
|
164
|
+
let sequence = this.next_part_sequence();
|
|
154
165
|
for (const part of parts) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
});
|
|
163
|
-
} else if (part.type === "file") {
|
|
164
|
-
await this.upsert_part({
|
|
165
|
-
part_id: `file:${generate_id()}`,
|
|
166
|
-
sequence: this.next_part_sequence(),
|
|
167
|
-
type: "file",
|
|
168
|
-
media_type: part.media_type,
|
|
169
|
-
url: part.url,
|
|
170
|
-
...(part.filename ? { filename: part.filename } : {}),
|
|
171
|
-
});
|
|
172
|
-
} else {
|
|
173
|
-
await this.upsert_part({
|
|
174
|
-
part_id: `data:${generate_id()}`,
|
|
175
|
-
sequence: this.next_part_sequence(),
|
|
176
|
-
type: "data",
|
|
177
|
-
data_type: part.data_type,
|
|
178
|
-
data: part.data,
|
|
179
|
-
...(part.data_id ? { data_id: part.data_id } : {}),
|
|
180
|
-
});
|
|
181
|
-
}
|
|
166
|
+
const canonical = create_session_agent_content_part(
|
|
167
|
+
part,
|
|
168
|
+
`${part.type}:${generate_id()}`,
|
|
169
|
+
sequence,
|
|
170
|
+
);
|
|
171
|
+
appended_parts.push(canonical);
|
|
172
|
+
sequence += 1;
|
|
182
173
|
}
|
|
174
|
+
await this.recorder.commit_agent_parts(this.message_id, appended_parts);
|
|
175
|
+
return appended_parts;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 在 Turn 收口产物之前追加用户可见的 canonical Error Part。 */
|
|
180
|
+
async append_error(input: {
|
|
181
|
+
/** 错误影响范围。 */
|
|
182
|
+
scope: "session" | "turn";
|
|
183
|
+
/** 稳定错误码。 */
|
|
184
|
+
code: string;
|
|
185
|
+
/** 用户可见错误信息。 */
|
|
186
|
+
message: string;
|
|
187
|
+
/** 当前错误是否允许重试恢复。 */
|
|
188
|
+
recoverable: boolean;
|
|
189
|
+
}): Promise<void> {
|
|
190
|
+
await this.enqueue_write(async () => {
|
|
191
|
+
if (this.closed) throw new Error("Assistant Message writer is closed");
|
|
192
|
+
const part = {
|
|
193
|
+
part_id: `error:${generate_id()}`,
|
|
194
|
+
sequence: this.next_part_sequence(),
|
|
195
|
+
type: "error",
|
|
196
|
+
scope: input.scope,
|
|
197
|
+
code: input.code,
|
|
198
|
+
message: input.message,
|
|
199
|
+
recoverable: input.recoverable,
|
|
200
|
+
} satisfies SessionAgentErrorPart;
|
|
201
|
+
await this.recorder.commit_agent_parts(this.message_id, [part]);
|
|
183
202
|
});
|
|
184
203
|
}
|
|
185
204
|
|
|
186
205
|
/** 写入一个完整 canonical Assistant Part。 */
|
|
187
|
-
async upsert_part(part:
|
|
188
|
-
|
|
206
|
+
async upsert_part(part: SessionAgentMessagePart): Promise<void> {
|
|
207
|
+
this.recorder.project_agent_part(this.message_id, part);
|
|
189
208
|
if (this.step_active) this.current_step_part_ids.add(part.part_id);
|
|
190
209
|
}
|
|
191
210
|
|
|
@@ -224,6 +243,7 @@ export class SessionAssistantMessageWriter {
|
|
|
224
243
|
await this.upsert_part({
|
|
225
244
|
part_id,
|
|
226
245
|
sequence: this.next_part_sequence(),
|
|
246
|
+
step_id: this.require_step_id(),
|
|
227
247
|
type,
|
|
228
248
|
text: "",
|
|
229
249
|
state: "streaming",
|
|
@@ -234,7 +254,7 @@ export class SessionAssistantMessageWriter {
|
|
|
234
254
|
if (!event.delta) return;
|
|
235
255
|
const type = event.type === "text_delta" ? "text" : "reasoning";
|
|
236
256
|
const part_id = this.require_content_part_id(event.content_id);
|
|
237
|
-
|
|
257
|
+
this.recorder.project_agent_delta(
|
|
238
258
|
this.message_id,
|
|
239
259
|
part_id,
|
|
240
260
|
type,
|
|
@@ -271,7 +291,7 @@ export class SessionAssistantMessageWriter {
|
|
|
271
291
|
if (!event.input_delta) return;
|
|
272
292
|
const tool_call_id = this.require_tool_call_id(event.content_id);
|
|
273
293
|
const tool = this.require_tool(tool_call_id);
|
|
274
|
-
|
|
294
|
+
this.recorder.project_agent_tool_input_delta(
|
|
275
295
|
this.message_id,
|
|
276
296
|
tool.part_id,
|
|
277
297
|
tool_call_id,
|
|
@@ -292,24 +312,24 @@ export class SessionAssistantMessageWriter {
|
|
|
292
312
|
}
|
|
293
313
|
|
|
294
314
|
/** 读取当前 Assistant Message 快照。 */
|
|
295
|
-
private current_message():
|
|
315
|
+
private current_message(): SessionAgentMessage {
|
|
296
316
|
const message = this.recorder.get_message(this.message_id);
|
|
297
|
-
if (!message || message.
|
|
317
|
+
if (!message || message.role !== "agent") {
|
|
298
318
|
throw new Error(`Assistant Message not found: ${this.message_id}`);
|
|
299
319
|
}
|
|
300
320
|
return message;
|
|
301
321
|
}
|
|
302
322
|
|
|
303
323
|
/** 查找当前 Assistant 中的指定 Tool Part。 */
|
|
304
|
-
private find_tool(tool_call_id: string):
|
|
324
|
+
private find_tool(tool_call_id: string): SessionAgentToolPart | undefined {
|
|
305
325
|
return this.current_message().parts.find(
|
|
306
|
-
(part): part is
|
|
326
|
+
(part): part is SessionAgentToolPart =>
|
|
307
327
|
part.type === "tool" && part.tool_call_id === tool_call_id,
|
|
308
328
|
);
|
|
309
329
|
}
|
|
310
330
|
|
|
311
331
|
/** 读取指定 Tool Part,否则抛出稳定错误。 */
|
|
312
|
-
private require_tool(tool_call_id: string):
|
|
332
|
+
private require_tool(tool_call_id: string): SessionAgentToolPart {
|
|
313
333
|
const tool = this.find_tool(tool_call_id);
|
|
314
334
|
if (tool) return tool;
|
|
315
335
|
throw new Error(`Assistant canonical Tool Part not found: ${tool_call_id}`);
|
|
@@ -332,8 +352,8 @@ export class SessionAssistantMessageWriter {
|
|
|
332
352
|
/** 更新已经由模型事件创建的 Tool Part。 */
|
|
333
353
|
private async upsert_tool(
|
|
334
354
|
tool_call_id: string,
|
|
335
|
-
changes: Pick<
|
|
336
|
-
Partial<Omit<
|
|
355
|
+
changes: Pick<SessionAgentToolPart, "tool_name" | "state"> &
|
|
356
|
+
Partial<Omit<SessionAgentToolPart, "part_id" | "type" | "tool_call_id" | "tool_name" | "state">>,
|
|
337
357
|
): Promise<void> {
|
|
338
358
|
const current = this.require_tool(tool_call_id);
|
|
339
359
|
await this.upsert_part({ ...current, ...changes });
|
|
@@ -342,8 +362,8 @@ export class SessionAssistantMessageWriter {
|
|
|
342
362
|
/** 创建由模型事件声明的 Tool Part。 */
|
|
343
363
|
private async create_tool(
|
|
344
364
|
tool_call_id: string,
|
|
345
|
-
changes: Pick<
|
|
346
|
-
Partial<Omit<
|
|
365
|
+
changes: Pick<SessionAgentToolPart, "tool_name" | "state"> &
|
|
366
|
+
Partial<Omit<SessionAgentToolPart, "part_id" | "type" | "tool_call_id" | "tool_name" | "state">>,
|
|
347
367
|
): Promise<void> {
|
|
348
368
|
if (this.find_tool(tool_call_id)) {
|
|
349
369
|
throw new Error(`Assistant canonical Tool Part already exists: ${tool_call_id}`);
|
|
@@ -351,6 +371,7 @@ export class SessionAssistantMessageWriter {
|
|
|
351
371
|
await this.upsert_part({
|
|
352
372
|
part_id: `tool:${tool_call_id}`,
|
|
353
373
|
sequence: this.next_part_sequence(),
|
|
374
|
+
step_id: this.require_step_id(),
|
|
354
375
|
type: "tool",
|
|
355
376
|
tool_call_id,
|
|
356
377
|
...changes,
|
|
@@ -359,10 +380,10 @@ export class SessionAssistantMessageWriter {
|
|
|
359
380
|
|
|
360
381
|
/** 校验并合并同一位置的 canonical Part 与 Step 最终快照。 */
|
|
361
382
|
private merge_step_part(
|
|
362
|
-
current_part:
|
|
363
|
-
final_part:
|
|
383
|
+
current_part: SessionAgentMessagePart,
|
|
384
|
+
final_part: SessionAgentMessagePart,
|
|
364
385
|
index: number,
|
|
365
|
-
):
|
|
386
|
+
): SessionAgentMessagePart {
|
|
366
387
|
if (current_part.type !== final_part.type) {
|
|
367
388
|
throw this.step_snapshot_error(
|
|
368
389
|
`part ${index + 1} type ${current_part.type} != ${final_part.type}`,
|
|
@@ -387,7 +408,8 @@ export class SessionAssistantMessageWriter {
|
|
|
387
408
|
...final_part,
|
|
388
409
|
part_id: current_part.part_id,
|
|
389
410
|
sequence: current_part.sequence,
|
|
390
|
-
|
|
411
|
+
step_id: current_part.step_id,
|
|
412
|
+
} as SessionAgentMessagePart;
|
|
391
413
|
}
|
|
392
414
|
|
|
393
415
|
/** 构造不包含正文与 Tool 输出的结构化 Step 快照错误。 */
|
|
@@ -400,11 +422,18 @@ export class SessionAssistantMessageWriter {
|
|
|
400
422
|
/** 清理当前 Step 的临时关联状态。 */
|
|
401
423
|
private reset_step_state(): void {
|
|
402
424
|
this.step_active = false;
|
|
425
|
+
this.current_step_id = null;
|
|
403
426
|
this.content_part_ids.clear();
|
|
404
427
|
this.tool_call_ids.clear();
|
|
405
428
|
this.current_step_part_ids.clear();
|
|
406
429
|
}
|
|
407
430
|
|
|
431
|
+
/** 返回当前模型 Step 的稳定标识。 */
|
|
432
|
+
private require_step_id(): string {
|
|
433
|
+
if (this.step_active && this.current_step_id) return this.current_step_id;
|
|
434
|
+
throw new Error("Assistant canonical step is not active");
|
|
435
|
+
}
|
|
436
|
+
|
|
408
437
|
/** 计算下一个不可变 Part 顺序号。 */
|
|
409
438
|
private next_part_sequence(): number {
|
|
410
439
|
return this.current_message().parts.reduce(
|
|
@@ -414,10 +443,10 @@ export class SessionAssistantMessageWriter {
|
|
|
414
443
|
}
|
|
415
444
|
|
|
416
445
|
/** 串行执行对当前 Assistant Message 的全部写操作。 */
|
|
417
|
-
private async enqueue_write(operation: () => Promise<
|
|
446
|
+
private async enqueue_write<TResult>(operation: () => Promise<TResult>): Promise<TResult> {
|
|
418
447
|
const current = this.write_chain.then(operation, operation);
|
|
419
|
-
this.write_chain = current.
|
|
420
|
-
await current;
|
|
448
|
+
this.write_chain = current.then(() => undefined, () => undefined);
|
|
449
|
+
return await current;
|
|
421
450
|
}
|
|
422
451
|
|
|
423
452
|
/** 在队列内关闭当前 Assistant Message。 */
|
|
@@ -430,7 +459,7 @@ export class SessionAssistantMessageWriter {
|
|
|
430
459
|
`Assistant Message writer closed with status ${status}`,
|
|
431
460
|
);
|
|
432
461
|
this.reset_step_state();
|
|
433
|
-
await this.recorder.
|
|
462
|
+
await this.recorder.complete_agent_message(this.message_id, status, error);
|
|
434
463
|
this.closed = true;
|
|
435
464
|
}
|
|
436
465
|
}
|
|
@@ -4,6 +4,31 @@ import path from "node:path";
|
|
|
4
4
|
import type { SessionMessage } from "@downcity/type";
|
|
5
5
|
import type { SessionAttachmentStore } from "@/types/store/SessionAttachmentStore.js";
|
|
6
6
|
|
|
7
|
+
/** 截取 Fork 锚点之前的历史,并按调用语义决定是否包含锚点。 */
|
|
8
|
+
export function resolve_session_fork_messages(input: {
|
|
9
|
+
/** 当前源 Session 的稳定标识。 */
|
|
10
|
+
session_id: string;
|
|
11
|
+
/** 当前源 Session 的完整 canonical 历史。 */
|
|
12
|
+
messages: SessionMessage[];
|
|
13
|
+
/** Fork 使用的锚点 Message 标识。 */
|
|
14
|
+
message_id: string;
|
|
15
|
+
/** Fork 历史是否包含锚点 Message 本身。 */
|
|
16
|
+
include_message: boolean;
|
|
17
|
+
}): SessionMessage[] {
|
|
18
|
+
const target_index = input.messages.findIndex(
|
|
19
|
+
(message) => message.message_id === input.message_id,
|
|
20
|
+
);
|
|
21
|
+
if (target_index < 0) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Cannot fork session "${input.session_id}": message_id "${input.message_id}" not found.`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return input.messages.slice(
|
|
27
|
+
0,
|
|
28
|
+
input.include_message ? target_index + 1 : target_index,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
7
32
|
/**
|
|
8
33
|
* 将 fork 历史中的本地绝对路径复制到目标 Session 的附件 Store。
|
|
9
34
|
*
|
|
@@ -17,7 +42,6 @@ export async function relocate_fork_message_files(
|
|
|
17
42
|
): Promise<SessionMessage[]> {
|
|
18
43
|
const relocated_urls = new Map<string, Promise<string>>();
|
|
19
44
|
return await Promise.all(messages.map(async (message) => {
|
|
20
|
-
if (message.type !== "user" && message.type !== "assistant") return structuredClone(message);
|
|
21
45
|
const parts = await Promise.all(message.parts.map(async (part) => {
|
|
22
46
|
if (part.type !== "file") return structuredClone(part);
|
|
23
47
|
if (!part.url.startsWith("data:") && (!path.isAbsolute(part.url) || !source_store.owns_local_file(part.url))) {
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
SessionAgentInteractionPart,
|
|
11
|
+
SessionAgentMessage,
|
|
12
|
+
SessionAgentMessagePart,
|
|
13
13
|
SessionMessage,
|
|
14
14
|
} from "@downcity/type";
|
|
15
15
|
import type {
|
|
@@ -34,8 +34,8 @@ interface SessionMessageInteractionWriterOptions {
|
|
|
34
34
|
) => Promise<T>;
|
|
35
35
|
/** 原子持久化 Assistant 完整快照并发布发生变化的 Part。 */
|
|
36
36
|
commit_assistant_snapshot: (
|
|
37
|
-
current:
|
|
38
|
-
parts:
|
|
37
|
+
current: SessionAgentMessage,
|
|
38
|
+
parts: SessionAgentMessagePart[],
|
|
39
39
|
) => Promise<void>;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -48,9 +48,9 @@ export class SessionMessageInteractionWriter {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/** 返回当前 Session 中全部等待用户响应的 canonical Interaction。 */
|
|
51
|
-
list_pending():
|
|
51
|
+
list_pending(): SessionAgentInteractionPart[] {
|
|
52
52
|
return [...this.options.list_messages()].flatMap((message) =>
|
|
53
|
-
message.
|
|
53
|
+
message.role === "agent" && message.state === "streaming"
|
|
54
54
|
? message.parts.flatMap((part) =>
|
|
55
55
|
part.type === "interaction" && part.status === "pending"
|
|
56
56
|
? [structuredClone(part)]
|
|
@@ -63,7 +63,7 @@ export class SessionMessageInteractionWriter {
|
|
|
63
63
|
/** 原子创建 Interaction,并把关联 Tool 转为 waiting-user。 */
|
|
64
64
|
async request(
|
|
65
65
|
request: SessionInteractionRequest,
|
|
66
|
-
): Promise<
|
|
66
|
+
): Promise<SessionAgentInteractionPart> {
|
|
67
67
|
const tool_call_id = request.source.tool_call_id;
|
|
68
68
|
const message_id = tool_call_id
|
|
69
69
|
? this.require_streaming_tool(tool_call_id).message_id
|
|
@@ -102,7 +102,7 @@ export class SessionMessageInteractionWriter {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
const interaction:
|
|
105
|
+
const interaction: SessionAgentInteractionPart = {
|
|
106
106
|
part_id: `interaction:${request.interaction_id}`,
|
|
107
107
|
sequence: parts.reduce(
|
|
108
108
|
(sequence, part) => Math.max(sequence, part.sequence + 1),
|
|
@@ -123,7 +123,7 @@ export class SessionMessageInteractionWriter {
|
|
|
123
123
|
async resolve(
|
|
124
124
|
interaction_id: string,
|
|
125
125
|
response: SessionInteractionResponse,
|
|
126
|
-
): Promise<
|
|
126
|
+
): Promise<SessionAgentInteractionPart> {
|
|
127
127
|
const { message_id } = this.require_pending_interaction(interaction_id);
|
|
128
128
|
await this.options.enqueue_assistant_write(message_id, async () => {
|
|
129
129
|
const current = this.require_streaming_assistant(message_id);
|
|
@@ -171,7 +171,7 @@ export class SessionMessageInteractionWriter {
|
|
|
171
171
|
async close(
|
|
172
172
|
interaction_id: string,
|
|
173
173
|
input: SessionInteractionCloseInput,
|
|
174
|
-
): Promise<
|
|
174
|
+
): Promise<SessionAgentInteractionPart> {
|
|
175
175
|
const { message_id } = this.require_pending_interaction(interaction_id);
|
|
176
176
|
await this.options.enqueue_assistant_write(message_id, async () => {
|
|
177
177
|
const current = this.require_streaming_assistant(message_id);
|
|
@@ -209,15 +209,15 @@ export class SessionMessageInteractionWriter {
|
|
|
209
209
|
/** 读取指定或当前唯一的流式 Assistant Message。 */
|
|
210
210
|
private require_streaming_assistant(
|
|
211
211
|
message_id?: string,
|
|
212
|
-
):
|
|
212
|
+
): SessionAgentMessage {
|
|
213
213
|
const message = message_id
|
|
214
214
|
? [...this.options.list_messages()].find(
|
|
215
215
|
(item) => item.message_id === message_id,
|
|
216
216
|
)
|
|
217
217
|
: [...this.options.list_messages()].find(
|
|
218
|
-
(item) => item.
|
|
218
|
+
(item) => item.role === "agent" && item.state === "streaming",
|
|
219
219
|
);
|
|
220
|
-
if (!message || message.
|
|
220
|
+
if (!message || message.role !== "agent" || message.state !== "streaming") {
|
|
221
221
|
throw new Error(
|
|
222
222
|
message_id
|
|
223
223
|
? `Streaming Assistant Message not found: ${message_id}`
|
|
@@ -231,13 +231,13 @@ export class SessionMessageInteractionWriter {
|
|
|
231
231
|
private find_interaction(interaction_id: string):
|
|
232
232
|
| {
|
|
233
233
|
message_id: string;
|
|
234
|
-
part:
|
|
234
|
+
part: SessionAgentInteractionPart;
|
|
235
235
|
}
|
|
236
236
|
| undefined {
|
|
237
237
|
for (const message of this.options.list_messages()) {
|
|
238
|
-
if (message.
|
|
238
|
+
if (message.role !== "agent" || message.state !== "streaming") continue;
|
|
239
239
|
const part = message.parts.find(
|
|
240
|
-
(item): item is
|
|
240
|
+
(item): item is SessionAgentInteractionPart =>
|
|
241
241
|
item.type === "interaction" &&
|
|
242
242
|
item.interaction_id === interaction_id,
|
|
243
243
|
);
|
|
@@ -249,7 +249,7 @@ export class SessionMessageInteractionWriter {
|
|
|
249
249
|
/** 读取指定 Interaction,否则抛出稳定领域错误。 */
|
|
250
250
|
private require_interaction(interaction_id: string): {
|
|
251
251
|
message_id: string;
|
|
252
|
-
part:
|
|
252
|
+
part: SessionAgentInteractionPart;
|
|
253
253
|
} {
|
|
254
254
|
const interaction = this.find_interaction(interaction_id);
|
|
255
255
|
if (interaction) return interaction;
|
|
@@ -259,7 +259,7 @@ export class SessionMessageInteractionWriter {
|
|
|
259
259
|
/** 读取指定 pending Interaction,否则拒绝重复响应终态 Interaction。 */
|
|
260
260
|
private require_pending_interaction(interaction_id: string): {
|
|
261
261
|
message_id: string;
|
|
262
|
-
part:
|
|
262
|
+
part: SessionAgentInteractionPart;
|
|
263
263
|
} {
|
|
264
264
|
const interaction = this.require_interaction(interaction_id);
|
|
265
265
|
if (interaction.part.status !== "pending") {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
|
-
|
|
8
|
+
SessionAgentMessage,
|
|
9
9
|
SessionMessage,
|
|
10
10
|
} from "@downcity/type";
|
|
11
11
|
import type { JsonObject, JsonValue } from "@downcity/type";
|
|
@@ -24,7 +24,6 @@ export interface SessionToolCallSummary {
|
|
|
24
24
|
|
|
25
25
|
/** 提取单条 canonical Message 的全部普通文本。 */
|
|
26
26
|
export function extract_session_message_text(message: SessionMessage): string {
|
|
27
|
-
if (message.type !== "user" && message.type !== "assistant") return "";
|
|
28
27
|
return message.parts
|
|
29
28
|
.flatMap((part) => part.type === "text" ? [part.text] : [])
|
|
30
29
|
.map((text) => text.trim())
|
|
@@ -35,7 +34,7 @@ export function extract_session_message_text(message: SessionMessage): string {
|
|
|
35
34
|
|
|
36
35
|
/** 提取 Assistant Message 中的 canonical 工具调用摘要。 */
|
|
37
36
|
export function extract_session_tool_calls(
|
|
38
|
-
message:
|
|
37
|
+
message: SessionAgentMessage,
|
|
39
38
|
): SessionToolCallSummary[] {
|
|
40
39
|
return message.parts.flatMap((part) => {
|
|
41
40
|
if (part.type !== "tool" || part.input === undefined) return [];
|
|
@@ -52,7 +51,7 @@ export function extract_session_tool_calls(
|
|
|
52
51
|
|
|
53
52
|
/** 读取最终一次成功 `chat_send` 的可见文本,并回退到普通 Assistant 文本。 */
|
|
54
53
|
export function resolve_session_assistant_visible_text(
|
|
55
|
-
message:
|
|
54
|
+
message: SessionAgentMessage,
|
|
56
55
|
): string {
|
|
57
56
|
const calls = extract_session_tool_calls(message);
|
|
58
57
|
for (let index = calls.length - 1; index >= 0; index -= 1) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 任意 JSON 结构。
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {
|
|
8
|
+
import type { SessionAgentDataPart } from "@downcity/type";
|
|
9
9
|
import type {
|
|
10
10
|
SessionTurnFileDiff,
|
|
11
11
|
SessionTurnFileDiffData,
|
|
@@ -17,7 +17,7 @@ export const SESSION_TURN_FILE_DIFF_DATA_TYPE = "data-session-turn-file-diff";
|
|
|
17
17
|
|
|
18
18
|
/** 从 Assistant data part 中读取经过校验的 Turn 文件改动。 */
|
|
19
19
|
export function read_session_turn_file_diff_data(
|
|
20
|
-
part:
|
|
20
|
+
part: SessionAgentDataPart,
|
|
21
21
|
): SessionTurnFileDiffData | undefined {
|
|
22
22
|
if (part.data_type !== SESSION_TURN_FILE_DIFF_DATA_TYPE) return undefined;
|
|
23
23
|
if (!is_record(part.data) || !Array.isArray(part.data.files)) return undefined;
|
|
@@ -34,7 +34,7 @@ export function read_session_turn_file_diff_data(
|
|
|
34
34
|
|
|
35
35
|
/** 判断 data part 是否为有效的 Turn 文件改动。 */
|
|
36
36
|
export function is_session_turn_file_diff_data_part(
|
|
37
|
-
part:
|
|
37
|
+
part: SessionAgentDataPart,
|
|
38
38
|
): boolean {
|
|
39
39
|
return read_session_turn_file_diff_data(part) !== undefined;
|
|
40
40
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
SessionAgentContent,
|
|
9
|
+
SessionUserContent,
|
|
10
10
|
} from "@downcity/type";
|
|
11
11
|
import type { SessionUserMessagePart } from "@downcity/type";
|
|
12
12
|
import { to_session_json_value } from "@/session/messages/SessionJsonValue.js";
|
|
@@ -17,21 +17,22 @@ import {
|
|
|
17
17
|
|
|
18
18
|
/** 把 Downcity Session User parts 归一为 canonical User parts。 */
|
|
19
19
|
export function normalize_session_user_parts(
|
|
20
|
-
parts:
|
|
20
|
+
parts: SessionUserContent[] | null | undefined,
|
|
21
21
|
): SessionUserMessagePart[] {
|
|
22
22
|
if (!Array.isArray(parts)) return [];
|
|
23
23
|
return parts.flatMap<SessionUserMessagePart>((part, index) => {
|
|
24
24
|
if (part.type === "text") {
|
|
25
25
|
return [{
|
|
26
26
|
part_id: `user-text:${index + 1}`,
|
|
27
|
+
sequence: index + 1,
|
|
27
28
|
type: "text",
|
|
28
29
|
text: part.text,
|
|
29
|
-
state: "done",
|
|
30
30
|
}];
|
|
31
31
|
}
|
|
32
32
|
if (part.type === "context") {
|
|
33
33
|
return [{
|
|
34
34
|
part_id: `user-context:${index + 1}`,
|
|
35
|
+
sequence: index + 1,
|
|
35
36
|
type: "context",
|
|
36
37
|
tag: normalize_session_context_tag(part.tag),
|
|
37
38
|
context: normalize_session_context_content(part.context),
|
|
@@ -40,6 +41,7 @@ export function normalize_session_user_parts(
|
|
|
40
41
|
if (part.type === "file") {
|
|
41
42
|
return [{
|
|
42
43
|
part_id: `user-file:${index + 1}`,
|
|
44
|
+
sequence: index + 1,
|
|
43
45
|
type: "file",
|
|
44
46
|
url: part.url,
|
|
45
47
|
media_type: part.media_type,
|
|
@@ -48,6 +50,7 @@ export function normalize_session_user_parts(
|
|
|
48
50
|
}
|
|
49
51
|
return [{
|
|
50
52
|
part_id: `user-data:${index + 1}`,
|
|
53
|
+
sequence: index + 1,
|
|
51
54
|
type: "data",
|
|
52
55
|
data_type: part.data_type,
|
|
53
56
|
data: to_session_json_value(part.data),
|
|
@@ -60,8 +63,8 @@ export function normalize_session_user_parts(
|
|
|
60
63
|
export function normalize_canonical_session_user_parts(
|
|
61
64
|
parts: readonly SessionUserMessagePart[],
|
|
62
65
|
): SessionUserMessagePart[] {
|
|
63
|
-
return parts.map((part) => {
|
|
64
|
-
const canonical = structuredClone(part);
|
|
66
|
+
return parts.map((part, index) => {
|
|
67
|
+
const canonical = { ...structuredClone(part), sequence: index + 1 };
|
|
65
68
|
if (canonical.type !== "context") return canonical;
|
|
66
69
|
return {
|
|
67
70
|
...canonical,
|
|
@@ -73,7 +76,7 @@ export function normalize_canonical_session_user_parts(
|
|
|
73
76
|
|
|
74
77
|
/** 判断 Assistant 结果中是否包含可持久化内容。 */
|
|
75
78
|
export function has_assistant_result_content(
|
|
76
|
-
parts: readonly
|
|
79
|
+
parts: readonly SessionAgentContent[],
|
|
77
80
|
): boolean {
|
|
78
81
|
return parts.some((part) => {
|
|
79
82
|
if (part.type === "text") return Boolean(part.text.trim());
|