@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/executor/Executor.ts
CHANGED
|
@@ -11,23 +11,15 @@ import type {
|
|
|
11
11
|
ModelClient,
|
|
12
12
|
RuntimeTool as Tool,
|
|
13
13
|
RuntimeToolExecutionOptions as ToolExecutionOptions,
|
|
14
|
+
SessionHookRuntime,
|
|
14
15
|
} from "@downcity/type";
|
|
15
16
|
import { CoreEngineRunner } from "@executor/core-engine/CoreEngineRunner.js";
|
|
16
17
|
import { ExecutorRecoveryPolicy } from "@executor/services/ExecutorRecoveryPolicy.js";
|
|
17
18
|
import type { Logger } from "@/utils/logger/Logger.js";
|
|
18
|
-
import type {
|
|
19
|
-
SessionCompactHistory,
|
|
20
|
-
SessionExecutor,
|
|
21
|
-
} from "@/types/session/SessionExecution.js";
|
|
19
|
+
import type { SessionExecutor } from "@/types/session/SessionExecution.js";
|
|
22
20
|
import type { SessionTurnContext } from "@/types/executor/SessionTurnContext.js";
|
|
23
21
|
import type { SessionToolExecutionContext } from "@/types/executor/SessionToolExecutionContext.js";
|
|
24
|
-
import type { SessionHookRuntime } from "@downcity/type";
|
|
25
22
|
import { is_action_result } from "@/types/action/ActionResult.js";
|
|
26
|
-
import { generate_id } from "@/utils/Id.js";
|
|
27
|
-
import {
|
|
28
|
-
normalize_session_context_content,
|
|
29
|
-
normalize_session_context_tag,
|
|
30
|
-
} from "@/session/messages/SessionUserContext.js";
|
|
31
23
|
import type {
|
|
32
24
|
SessionStepExecutionInput,
|
|
33
25
|
SessionTurnExecutionResult,
|
|
@@ -37,6 +29,7 @@ import type {
|
|
|
37
29
|
SessionComposeInput,
|
|
38
30
|
SessionStepInput,
|
|
39
31
|
} from "@/types/session/SessionComposer.js";
|
|
32
|
+
import type { SessionContextRecoveryReason } from "@/types/session/SessionContextPolicy.js";
|
|
40
33
|
|
|
41
34
|
type ExecutorOptions = {
|
|
42
35
|
/**
|
|
@@ -49,21 +42,16 @@ type ExecutorOptions = {
|
|
|
49
42
|
|
|
50
43
|
/** 为 Composer 创建当前 Step 的只读输入快照。 */
|
|
51
44
|
get_compose_input: (
|
|
52
|
-
turn_context: SessionTurnContext
|
|
45
|
+
turn_context: SessionTurnContext,
|
|
53
46
|
retry_count: number,
|
|
54
47
|
) => Promise<SessionComposeInput>;
|
|
55
48
|
|
|
49
|
+
/** 请求当前 Composer 推进派生上下文状态。 */
|
|
50
|
+
recover_context: (reason: SessionContextRecoveryReason) => Promise<boolean>;
|
|
51
|
+
|
|
56
52
|
/** 应用 Session 级固定 system snapshot。 */
|
|
57
53
|
apply_system_snapshot?: (input: SessionStepInput) => SessionStepInput;
|
|
58
54
|
|
|
59
|
-
/** 请求 Session 领域生成并提交 canonical 历史压缩。 */
|
|
60
|
-
compact_history: SessionCompactHistory;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 读取当前 session 使用的模型实例。
|
|
64
|
-
*/
|
|
65
|
-
get_model: () => ModelClient | undefined;
|
|
66
|
-
|
|
67
55
|
/**
|
|
68
56
|
* 统一日志器。
|
|
69
57
|
*/
|
|
@@ -84,9 +72,8 @@ export class Executor implements SessionExecutor {
|
|
|
84
72
|
|
|
85
73
|
private readonly composer: SessionComposer;
|
|
86
74
|
private readonly get_compose_input: ExecutorOptions["get_compose_input"];
|
|
75
|
+
private readonly recover_context: ExecutorOptions["recover_context"];
|
|
87
76
|
private readonly apply_system_snapshot?: ExecutorOptions["apply_system_snapshot"];
|
|
88
|
-
private readonly compact_history: ExecutorOptions["compact_history"];
|
|
89
|
-
private readonly get_model: ExecutorOptions["get_model"];
|
|
90
77
|
private readonly get_hooks: ExecutorOptions["get_hooks"];
|
|
91
78
|
private readonly logger: Logger;
|
|
92
79
|
private readonly recovery_policy: ExecutorRecoveryPolicy;
|
|
@@ -103,21 +90,23 @@ export class Executor implements SessionExecutor {
|
|
|
103
90
|
this.session_id = session_id;
|
|
104
91
|
this.composer = options.composer;
|
|
105
92
|
this.get_compose_input = options.get_compose_input;
|
|
93
|
+
this.recover_context = options.recover_context;
|
|
106
94
|
this.apply_system_snapshot = options.apply_system_snapshot;
|
|
107
|
-
this.compact_history = options.compact_history;
|
|
108
|
-
this.get_model = options.get_model;
|
|
109
95
|
this.get_hooks = options.get_hooks;
|
|
110
96
|
this.logger = options.logger;
|
|
111
97
|
this.recovery_policy = new ExecutorRecoveryPolicy({
|
|
112
98
|
session_id: this.session_id,
|
|
113
|
-
|
|
99
|
+
recover_context: async (error) =>
|
|
100
|
+
is_provider_context_limit_error(error)
|
|
101
|
+
? await this.recover_context("provider_context_limit")
|
|
102
|
+
: false,
|
|
114
103
|
logger: this.logger,
|
|
115
104
|
});
|
|
116
105
|
this.core_engine_runner = new CoreEngineRunner({
|
|
117
106
|
session_id: this.session_id,
|
|
118
107
|
logger: this.logger,
|
|
119
108
|
should_compact_on_error: (error) =>
|
|
120
|
-
|
|
109
|
+
is_provider_context_limit_error(error),
|
|
121
110
|
});
|
|
122
111
|
}
|
|
123
112
|
|
|
@@ -136,7 +125,6 @@ export class Executor implements SessionExecutor {
|
|
|
136
125
|
* - scope 绑定、assistant step 持久化、executing 状态都收在实例内部。
|
|
137
126
|
*/
|
|
138
127
|
async execute(params: {
|
|
139
|
-
query: string;
|
|
140
128
|
turn_context: SessionTurnContext;
|
|
141
129
|
}): Promise<SessionTurnExecutionResult> {
|
|
142
130
|
if (this.executing) {
|
|
@@ -144,125 +132,46 @@ export class Executor implements SessionExecutor {
|
|
|
144
132
|
// 否则 step 回调、scope 与执行器状态都会互相污染。
|
|
145
133
|
throw new Error("Executor.execute does not support concurrent execution");
|
|
146
134
|
}
|
|
147
|
-
const query = String(params.query || "").trim();
|
|
148
135
|
const turn_context = params.turn_context;
|
|
149
136
|
this.executing = true;
|
|
150
|
-
this.recovery_policy.reset_execution_state();
|
|
151
137
|
try {
|
|
152
138
|
const result = await this.recovery_policy.execute_with_retry({
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
turn_context: next_turn_context,
|
|
160
|
-
retry_count,
|
|
161
|
-
}) =>
|
|
162
|
-
await this.prepare_execute_input(
|
|
163
|
-
next_query,
|
|
164
|
-
model,
|
|
165
|
-
next_turn_context,
|
|
166
|
-
retry_count,
|
|
167
|
-
),
|
|
168
|
-
execute_prepared_input: async ({
|
|
169
|
-
execute_input,
|
|
170
|
-
model,
|
|
171
|
-
turn_context: next_turn_context,
|
|
172
|
-
}) =>
|
|
173
|
-
await this.execute_prepared_input(
|
|
174
|
-
execute_input,
|
|
175
|
-
model,
|
|
176
|
-
next_turn_context,
|
|
177
|
-
),
|
|
139
|
+
execute_turn: async (retry_count) =>
|
|
140
|
+
await this.core_engine_runner.execute({
|
|
141
|
+
turn_context,
|
|
142
|
+
resolve_step_input: async () =>
|
|
143
|
+
await this.resolve_step_input(turn_context, retry_count),
|
|
144
|
+
}),
|
|
178
145
|
});
|
|
179
146
|
return result;
|
|
180
147
|
} finally {
|
|
181
|
-
this.recovery_policy.reset_execution_state();
|
|
182
148
|
this.executing = false;
|
|
183
149
|
}
|
|
184
150
|
}
|
|
185
151
|
|
|
186
152
|
/**
|
|
187
|
-
*
|
|
188
|
-
*/
|
|
189
|
-
private async prepare_execute_input(
|
|
190
|
-
query: string,
|
|
191
|
-
_model: ModelClient,
|
|
192
|
-
turn_context: SessionTurnContext,
|
|
193
|
-
retry_count: number,
|
|
194
|
-
): Promise<SessionStepExecutionInput> {
|
|
195
|
-
if (retry_count > 0) {
|
|
196
|
-
await this.logger.log("info", "[agent] compacting", {
|
|
197
|
-
retryCount: retry_count,
|
|
198
|
-
});
|
|
199
|
-
await this.compact_history({
|
|
200
|
-
turn_id: turn_context.session.turn_id,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
const step = await this.compose_step(turn_context, retry_count, true);
|
|
204
|
-
return {
|
|
205
|
-
query,
|
|
206
|
-
system: step.input.system,
|
|
207
|
-
messages: step.input.messages,
|
|
208
|
-
...(step.compose_input.history.summary?.summary_id
|
|
209
|
-
? { history_summary_id: step.compose_input.history.summary.summary_id }
|
|
210
|
-
: {}),
|
|
211
|
-
tools: step.input.tools,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* 执行一次已装配完成的 Step 输入。
|
|
217
|
-
*/
|
|
218
|
-
private async execute_prepared_input(
|
|
219
|
-
input: SessionStepExecutionInput,
|
|
220
|
-
model: ModelClient,
|
|
221
|
-
turn_context: SessionTurnContext,
|
|
222
|
-
): Promise<SessionTurnExecutionResult> {
|
|
223
|
-
return await this.core_engine_runner.execute({
|
|
224
|
-
execute_input: input,
|
|
225
|
-
model,
|
|
226
|
-
turn_context,
|
|
227
|
-
resolve_step_inputs: async () =>
|
|
228
|
-
await this.resolve_step_inputs(turn_context),
|
|
229
|
-
reload_history: async () => {
|
|
230
|
-
const step = await this.compose_step(turn_context, 0, false);
|
|
231
|
-
return {
|
|
232
|
-
messages: step.input.messages,
|
|
233
|
-
...(step.compose_input.history.summary?.summary_id
|
|
234
|
-
? { summary_id: step.compose_input.history.summary.summary_id }
|
|
235
|
-
: {}),
|
|
236
|
-
};
|
|
237
|
-
},
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* 解析下一 Session step 实际使用的运行配置。
|
|
153
|
+
* 为下一 Provider Step 生成唯一一份完整输入。
|
|
243
154
|
*
|
|
244
155
|
* 关键点(中文)
|
|
245
156
|
* - 调用方必须先提交 Session 统一输入队列,再调用本方法。
|
|
246
157
|
* - 每次调用只读取一次 model、system 与 tools,并把它们传给同一个模型 step。
|
|
247
158
|
*/
|
|
248
|
-
private async
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}> {
|
|
254
|
-
const composed = await this.compose_step(turn_context, 0, true);
|
|
159
|
+
private async resolve_step_input(
|
|
160
|
+
turn_context: SessionTurnContext,
|
|
161
|
+
retry_count: number,
|
|
162
|
+
): Promise<SessionStepExecutionInput> {
|
|
163
|
+
const composed = await this.compose_step(turn_context, retry_count);
|
|
255
164
|
return {
|
|
256
165
|
model: composed.model,
|
|
257
166
|
system: composed.input.system,
|
|
167
|
+
messages: composed.input.messages,
|
|
258
168
|
tools: this.bind_turn_context_to_tools(
|
|
259
169
|
composed.input.tools,
|
|
260
170
|
turn_context,
|
|
261
171
|
),
|
|
262
|
-
...(composed.
|
|
172
|
+
...(composed.context_window !== undefined
|
|
263
173
|
? {
|
|
264
|
-
context_window:
|
|
265
|
-
composed.compose_input.state.model_context_window,
|
|
174
|
+
context_window: composed.context_window,
|
|
266
175
|
}
|
|
267
176
|
: {}),
|
|
268
177
|
};
|
|
@@ -270,32 +179,33 @@ export class Executor implements SessionExecutor {
|
|
|
270
179
|
|
|
271
180
|
/** 读取只读 Session 快照并交给统一 Composer。 */
|
|
272
181
|
private async compose_step(
|
|
273
|
-
turn_context: SessionTurnContext
|
|
182
|
+
turn_context: SessionTurnContext,
|
|
274
183
|
retry_count: number,
|
|
275
|
-
refresh_plugins: boolean,
|
|
276
184
|
): Promise<{
|
|
277
|
-
compose_input: SessionComposeInput;
|
|
278
185
|
input: SessionStepInput;
|
|
279
186
|
model: ModelClient;
|
|
187
|
+
context_window?: number;
|
|
280
188
|
}> {
|
|
281
|
-
|
|
189
|
+
await this.refresh_step_runtime(turn_context);
|
|
282
190
|
const compose_input = await this.get_compose_input(
|
|
283
191
|
turn_context,
|
|
284
192
|
retry_count,
|
|
285
193
|
);
|
|
286
194
|
const model = compose_input.state.model;
|
|
287
195
|
if (!model) throw new Error("requires a configured model.");
|
|
288
|
-
turn_context
|
|
196
|
+
turn_context.step.commit({
|
|
289
197
|
workspace_env: compose_input.state.env,
|
|
290
198
|
agent_systems: compose_input.state.systems,
|
|
291
199
|
});
|
|
292
200
|
const raw_input = await this.composer.compose(compose_input);
|
|
293
201
|
return {
|
|
294
|
-
compose_input,
|
|
295
202
|
input: this.apply_system_snapshot
|
|
296
203
|
? this.apply_system_snapshot(raw_input)
|
|
297
204
|
: raw_input,
|
|
298
205
|
model,
|
|
206
|
+
...(compose_input.state.model_context_window !== undefined
|
|
207
|
+
? { context_window: compose_input.state.model_context_window }
|
|
208
|
+
: {}),
|
|
299
209
|
};
|
|
300
210
|
}
|
|
301
211
|
|
|
@@ -384,57 +294,11 @@ export class Executor implements SessionExecutor {
|
|
|
384
294
|
turn_context.effects.append(output.effects);
|
|
385
295
|
}
|
|
386
296
|
for (const message of output.messages) {
|
|
387
|
-
if (message.role === "
|
|
297
|
+
if (message.role === "agent") {
|
|
388
298
|
turn_context.output.enqueue_assistant_parts(message.parts);
|
|
389
299
|
continue;
|
|
390
300
|
}
|
|
391
|
-
|
|
392
|
-
turn_context.input.inject_user_message({
|
|
393
|
-
message_id: `runtime-user:${turn_context.session.session_id}:${generate_id()}`,
|
|
394
|
-
session_id: turn_context.session.session_id,
|
|
395
|
-
turn_id: turn_context.session.turn_id,
|
|
396
|
-
sequence: 0,
|
|
397
|
-
revision: 1,
|
|
398
|
-
visibility: "internal",
|
|
399
|
-
created_at: now,
|
|
400
|
-
updated_at: now,
|
|
401
|
-
type: "user",
|
|
402
|
-
input_type: "steer",
|
|
403
|
-
parts: message.parts.map((part, index) => {
|
|
404
|
-
if (part.type === "text") {
|
|
405
|
-
return {
|
|
406
|
-
part_id: `runtime-text:${index + 1}`,
|
|
407
|
-
type: "text" as const,
|
|
408
|
-
text: part.text,
|
|
409
|
-
state: "done" as const,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
if (part.type === "context") {
|
|
413
|
-
return {
|
|
414
|
-
part_id: `runtime-context:${index + 1}`,
|
|
415
|
-
type: "context" as const,
|
|
416
|
-
tag: normalize_session_context_tag(part.tag),
|
|
417
|
-
context: normalize_session_context_content(part.context),
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
if (part.type === "file") {
|
|
421
|
-
return {
|
|
422
|
-
part_id: `runtime-file:${index + 1}`,
|
|
423
|
-
type: "file" as const,
|
|
424
|
-
media_type: part.media_type,
|
|
425
|
-
url: part.url,
|
|
426
|
-
...(part.filename ? { filename: part.filename } : {}),
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
return {
|
|
430
|
-
part_id: `runtime-data:${index + 1}`,
|
|
431
|
-
type: "data" as const,
|
|
432
|
-
data_type: part.data_type,
|
|
433
|
-
data: part.data,
|
|
434
|
-
...(part.data_id ? { data_id: part.data_id } : {}),
|
|
435
|
-
};
|
|
436
|
-
}),
|
|
437
|
-
});
|
|
301
|
+
await turn_context.input.append_internal(message.parts);
|
|
438
302
|
}
|
|
439
303
|
return output.output;
|
|
440
304
|
},
|
|
@@ -443,14 +307,13 @@ export class Executor implements SessionExecutor {
|
|
|
443
307
|
return wrapped;
|
|
444
308
|
}
|
|
445
309
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Core Engine 的单 Step 内恢复仍需同步识别 Provider 上下文错误。 */
|
|
313
|
+
function is_provider_context_limit_error(error: unknown): boolean {
|
|
314
|
+
const message = String(error ?? "").toLowerCase();
|
|
315
|
+
return message.includes("context_length") ||
|
|
316
|
+
message.includes("too long") ||
|
|
317
|
+
message.includes("maximum context") ||
|
|
318
|
+
message.includes("context window");
|
|
456
319
|
}
|
package/src/executor/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
- `SessionComposer` 根据只读 Session 快照组装 system、messages 和 tools。
|
|
12
12
|
- `Executor` 管理一次 Turn 的输入装配、上下文超限恢复和 Step Plugin Lease;它只对外提供 `execute()`。
|
|
13
13
|
- `ModelRequestRunner` 唯一拥有普通模型请求的五次重试、退避和逐次失败通知。
|
|
14
|
-
- `CoreEngineRunner` 执行 Tool Loop
|
|
14
|
+
- `CoreEngineRunner` 执行 Tool Loop、续写和真实 usage 观测,不自行改写模型历史,也不维护普通请求重试状态。
|
|
15
15
|
- `SessionMessages` 是 Message 唯一事实源;Executor 不写文件、不持有 Store。
|
|
16
16
|
|
|
17
17
|
## 执行关系
|
|
@@ -55,29 +55,28 @@ Session.prompt()
|
|
|
55
55
|
| 旧能力 | 当前归属 |
|
|
56
56
|
| --- | --- |
|
|
57
57
|
| `SystemComposer` | `DefaultSessionComposer.compose()` + `SessionSystem` |
|
|
58
|
-
| `HistoryComposer` | `
|
|
58
|
+
| `HistoryComposer` | `SessionComposer` + `SessionContextPolicy` + `SessionModelMessages` |
|
|
59
59
|
| `ContextComposer` 的 tools | `Session.create_compose_input()` + `SessionComposer.compose()` |
|
|
60
60
|
| `ContextComposer` 的 Step Callback | `SessionLoop` + `CoreEngineRunner` |
|
|
61
61
|
| `ContextComposer` 的 fallback Assistant | `CoreEngineRunner` + `ExecutorRecoveryPolicy` |
|
|
62
|
-
| `CompactionComposer` | `SessionComposer.
|
|
62
|
+
| `CompactionComposer` | `SessionComposer.recover_context()` + Context Policy 派生表 |
|
|
63
63
|
|
|
64
|
-
统一 Composer
|
|
64
|
+
统一 Composer 只回答一个策略问题:
|
|
65
65
|
|
|
66
|
-
1. 当前 Step 的 system、messages 和 tools
|
|
67
|
-
2. 当前只读 Message 快照应生成什么压缩计划?
|
|
66
|
+
1. 当前 Step 的 system、messages 和 tools 是什么?上下文超限时是否能由当前 Policy 推进派生状态?
|
|
68
67
|
|
|
69
|
-
Queue 消费、Turn 控制、Message
|
|
68
|
+
Queue 消费、Turn 控制、Message 写入和 Mutation 发布都不是 Composer 职责。
|
|
70
69
|
|
|
71
|
-
##
|
|
70
|
+
## Context recovery
|
|
72
71
|
|
|
73
72
|
```text
|
|
74
|
-
|
|
75
|
-
->
|
|
76
|
-
->
|
|
77
|
-
->
|
|
73
|
+
SessionLoop / CoreEngine
|
|
74
|
+
-> SessionComposer.recover_context()
|
|
75
|
+
-> SessionContextPolicy 写入自己的派生表
|
|
76
|
+
-> Composer 下一次 compose() 读取最新派生上下文
|
|
78
77
|
```
|
|
79
78
|
|
|
80
|
-
Composer 可以调用模型生成 Summary,但不能修改 Message、Metadata 或发布事件。持久化提交始终由
|
|
79
|
+
Composer 可以调用模型生成 Summary,但不能修改 canonical Message、Metadata 或发布事件。持久化提交始终由 `SessionStorage` 事务完成。
|
|
81
80
|
|
|
82
81
|
## 目录
|
|
83
82
|
|
|
@@ -7,14 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import coreSystemPromptText from "@executor/composer/system/default/assets/core.prompt.js";
|
|
10
|
-
import taskSystemPromptText from "@executor/composer/system/default/assets/task.prompt.js";
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* 默认 core system prompt。
|
|
14
13
|
*/
|
|
15
14
|
export const CORE_SYSTEM_PROMPT = coreSystemPromptText.trim();
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* task 模式专用 system prompt。
|
|
19
|
-
*/
|
|
20
|
-
export const TASK_SYSTEM_PROMPT = taskSystemPromptText.trim();
|
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* CoreEngine Downcity 模型上下文压缩模块。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* 本模块只判断真实 Provider usage 是否达到恢复阈值;具体选择、摘要和持久化全部
|
|
5
|
+
* 归属于 SessionComposer 的 Context Policy。
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
|
-
import type { ModelContent, ModelMessage } from "@downcity/type";
|
|
8
|
-
|
|
9
8
|
/** usage 达到模型上下文窗口 95% 时安排下一 step 压缩。 */
|
|
10
9
|
export const MODEL_CONTEXT_COMPACTION_TRIGGER_RATIO = 0.95;
|
|
11
|
-
/** 压缩后的真实 usage 需要回落到模型上下文窗口 50% 以内。 */
|
|
12
|
-
export const MODEL_CONTEXT_COMPACTION_TARGET_RATIO = 0.5;
|
|
13
|
-
/** 历史 checkpoint 的基础最大字符数。 */
|
|
14
|
-
const INITIAL_HISTORY_CHECKPOINT_CHARS = 24_000;
|
|
15
|
-
/** 单条保留消息的基础字符预算。 */
|
|
16
|
-
const INITIAL_RETAINED_MESSAGE_CHARS = 16_000;
|
|
17
|
-
/** 折叠后至少保留的字符数。 */
|
|
18
|
-
const MIN_FOLDED_PART_CHARS = 64;
|
|
19
10
|
|
|
20
11
|
/** 读取 Provider usage 中的实际总 token。 */
|
|
21
12
|
export function resolve_model_usage_tokens(usage: unknown): number | null {
|
|
@@ -42,132 +33,9 @@ export function resolve_model_usage_ratio(
|
|
|
42
33
|
/** 判断一次真实 usage 是否要求下一 step 继续压缩。 */
|
|
43
34
|
export function should_compact_after_usage(
|
|
44
35
|
usage_ratio: number | null,
|
|
45
|
-
validating_compaction: boolean,
|
|
46
36
|
): boolean {
|
|
47
37
|
if (usage_ratio === null || !Number.isFinite(usage_ratio)) return false;
|
|
48
|
-
return
|
|
49
|
-
? usage_ratio > MODEL_CONTEXT_COMPACTION_TARGET_RATIO
|
|
50
|
-
: usage_ratio >= MODEL_CONTEXT_COMPACTION_TRIGGER_RATIO;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 对当前模型消息做一次确定性深度压缩。 */
|
|
54
|
-
export function deep_compact_model_messages(
|
|
55
|
-
messages: ModelMessage[],
|
|
56
|
-
compact_depth = 0,
|
|
57
|
-
): ModelMessage[] {
|
|
58
|
-
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
59
|
-
const depth = Math.max(0, Math.min(8, Math.floor(compact_depth)));
|
|
60
|
-
const last_user_index = find_last_user_index(messages);
|
|
61
|
-
if (depth === 0) {
|
|
62
|
-
const limit = resolve_depth_budget(INITIAL_RETAINED_MESSAGE_CHARS, depth);
|
|
63
|
-
return messages.flatMap((message, index) => {
|
|
64
|
-
const compacted = compact_message(message, limit, index >= last_user_index);
|
|
65
|
-
return compacted ? [compacted] : [];
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const system_messages = messages.filter((message) => message.role === "system");
|
|
69
|
-
const retained_start = last_user_index >= 0 ? last_user_index : Math.max(0, messages.length - 2);
|
|
70
|
-
const history = messages.filter((message, index) => message.role !== "system" && index < retained_start);
|
|
71
|
-
const retained = messages.filter((message, index) => message.role !== "system" && index >= retained_start);
|
|
72
|
-
const checkpoint = build_checkpoint(
|
|
73
|
-
history,
|
|
74
|
-
resolve_depth_budget(INITIAL_HISTORY_CHECKPOINT_CHARS, depth),
|
|
75
|
-
);
|
|
76
|
-
const limit = resolve_depth_budget(INITIAL_RETAINED_MESSAGE_CHARS, depth);
|
|
77
|
-
return [
|
|
78
|
-
...system_messages.flatMap((message) => {
|
|
79
|
-
const compacted = compact_message(message, limit, false);
|
|
80
|
-
return compacted ? [compacted] : [];
|
|
81
|
-
}),
|
|
82
|
-
...(checkpoint ? [{ role: "assistant", content: [{ type: "text", text: checkpoint }] } as ModelMessage] : []),
|
|
83
|
-
...retained.flatMap((message) => {
|
|
84
|
-
const compacted = compact_message(message, limit, true);
|
|
85
|
-
return compacted ? [compacted] : [];
|
|
86
|
-
}),
|
|
87
|
-
];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** 对文本做确定性的 head/tail 折叠。 */
|
|
91
|
-
export function fold_compacted_text(text: string, max_chars: number): string {
|
|
92
|
-
const value = String(text || "");
|
|
93
|
-
const limit = Math.max(MIN_FOLDED_PART_CHARS, Math.floor(max_chars));
|
|
94
|
-
if (value.length <= limit) return value;
|
|
95
|
-
const marker = `\n...[compacted ${String(value.length - limit)} chars]...\n`;
|
|
96
|
-
const available = Math.max(0, limit - marker.length);
|
|
97
|
-
const head = Math.ceil(available / 2);
|
|
98
|
-
return `${value.slice(0, head)}${marker}${value.slice(-(available - head))}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** 压缩单条消息中的高体积内容。 */
|
|
102
|
-
function compact_message(
|
|
103
|
-
message: ModelMessage,
|
|
104
|
-
max_chars: number,
|
|
105
|
-
keep_tools: boolean,
|
|
106
|
-
): ModelMessage | undefined {
|
|
107
|
-
const part_limit = Math.max(MIN_FOLDED_PART_CHARS, Math.floor(max_chars / Math.max(1, message.content.length)));
|
|
108
|
-
const content = message.content.flatMap<ModelContent>((part) => {
|
|
109
|
-
// 推理过程不属于压缩后仍需保留的稳定事实。
|
|
110
|
-
if (part.type === "reasoning") return [];
|
|
111
|
-
if (part.type === "text") {
|
|
112
|
-
return [{ ...part, text: fold_compacted_text(part.text, part_limit) }];
|
|
113
|
-
}
|
|
114
|
-
if (part.type === "tool_call" && !keep_tools) return [];
|
|
115
|
-
if (part.type === "tool_result") {
|
|
116
|
-
if (!keep_tools) return [];
|
|
117
|
-
return [{
|
|
118
|
-
...part,
|
|
119
|
-
content: part.content.map((result_part) => {
|
|
120
|
-
if (result_part.type === "text") {
|
|
121
|
-
return {
|
|
122
|
-
...result_part,
|
|
123
|
-
text: fold_compacted_text(result_part.text, part_limit),
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
if (result_part.type !== "json") return result_part;
|
|
127
|
-
const serialized = JSON.stringify(result_part.value);
|
|
128
|
-
if (serialized.length <= part_limit) return result_part;
|
|
129
|
-
return {
|
|
130
|
-
type: "json" as const,
|
|
131
|
-
value: {
|
|
132
|
-
compacted: true,
|
|
133
|
-
preview: fold_compacted_text(serialized, part_limit),
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
}),
|
|
137
|
-
}];
|
|
138
|
-
}
|
|
139
|
-
return [part];
|
|
140
|
-
});
|
|
141
|
-
if (content.length === 0) return undefined;
|
|
142
|
-
return { ...message, content };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** 生成历史 checkpoint 文本。 */
|
|
146
|
-
function build_checkpoint(messages: ModelMessage[], max_chars: number): string {
|
|
147
|
-
const text = messages.map((message) => {
|
|
148
|
-
const content = message.content.map((part) => {
|
|
149
|
-
if (part.type === "text") return part.text;
|
|
150
|
-
if (part.type === "reasoning") return "";
|
|
151
|
-
if (part.type === "file") return `[file ${part.media_type}${part.filename ? ` ${part.filename}` : ""}]`;
|
|
152
|
-
if (part.type === "tool_call") return `[tool_call ${part.tool_name}]`;
|
|
153
|
-
return `[tool_result ${part.tool_name} ${part.outcome}]`;
|
|
154
|
-
}).join("\n");
|
|
155
|
-
return `${message.role}: ${content}`;
|
|
156
|
-
}).join("\n\n");
|
|
157
|
-
return text ? `Earlier conversation checkpoint:\n${fold_compacted_text(text, max_chars)}` : "";
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** 查找最新用户消息索引。 */
|
|
161
|
-
function find_last_user_index(messages: ModelMessage[]): number {
|
|
162
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
163
|
-
if (messages[index]?.role === "user") return index;
|
|
164
|
-
}
|
|
165
|
-
return -1;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** 按压缩深度计算当前字符预算。 */
|
|
169
|
-
function resolve_depth_budget(initial_budget: number, depth: number): number {
|
|
170
|
-
return Math.max(MIN_FOLDED_PART_CHARS, Math.floor(initial_budget / (2 ** depth)));
|
|
38
|
+
return usage_ratio >= MODEL_CONTEXT_COMPACTION_TRIGGER_RATIO;
|
|
171
39
|
}
|
|
172
40
|
|
|
173
41
|
/** 读取非负数字。 */
|