@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
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
* CoreEngineRunner:模型与 tool-loop 主循环执行器。
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
|
-
* -
|
|
6
|
-
* -
|
|
5
|
+
* - 每个 Provider Step 都通过回调取得 Composer 生成的完整输入。
|
|
6
|
+
* - 只负责 step 循环、续写恢复与最终 assistant 汇总,不持有历史副本。
|
|
7
7
|
* - 保持失败返回结构稳定,避免对外 Session 行为变化。
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import type { ModelClient, ModelMessage } from "@downcity/type";
|
|
11
10
|
import type { RuntimeTool as Tool } from "@downcity/type";
|
|
12
11
|
import { log_assistant_message_now } from "@executor/messages/SessionMessageLog.js";
|
|
13
12
|
import {
|
|
@@ -27,7 +26,6 @@ import {
|
|
|
27
26
|
} from "@executor/core-engine/CoreEngineLoopDecision.js";
|
|
28
27
|
import {
|
|
29
28
|
resolve_effective_core_engine_error,
|
|
30
|
-
summarize_stream_error,
|
|
31
29
|
} from "@executor/core-engine/CoreEngineError.js";
|
|
32
30
|
import {
|
|
33
31
|
run_model_step,
|
|
@@ -35,31 +33,23 @@ import {
|
|
|
35
33
|
type ModelStepToolCall,
|
|
36
34
|
} from "@executor/model/ModelStepRunner.js";
|
|
37
35
|
import { execute_model_request } from "@executor/model/ModelRequestRunner.js";
|
|
38
|
-
import { CoreEngineMessageState } from "@executor/core-engine/CoreEngineMessageState.js";
|
|
39
36
|
import {
|
|
40
|
-
deep_compact_model_messages,
|
|
41
37
|
resolve_model_usage_ratio,
|
|
42
38
|
should_compact_after_usage,
|
|
43
39
|
} from "@executor/core-engine/CoreEngineContextCompaction.js";
|
|
44
40
|
import type { Logger } from "@/utils/logger/Logger.js";
|
|
45
|
-
import type { JsonObject } from "@downcity/type";
|
|
46
41
|
import type { SessionTurnContext } from "@/types/executor/SessionTurnContext.js";
|
|
47
42
|
import { to_session_json_value } from "@/session/messages/SessionJsonValue.js";
|
|
48
43
|
import type {
|
|
49
44
|
SessionStepExecutionInput,
|
|
50
45
|
SessionTurnExecutionResult,
|
|
51
46
|
} from "@/types/session/SessionExecution.js";
|
|
52
|
-
import type {
|
|
53
|
-
import
|
|
54
|
-
|
|
55
|
-
SessionUserMessage,
|
|
56
|
-
} from "@downcity/type";
|
|
47
|
+
import type { SessionAgentMessagePart } from "@downcity/type";
|
|
48
|
+
import { create_session_agent_content_part } from "@/session/messages/SessionAgentContent.js";
|
|
49
|
+
import { generate_id } from "@/utils/Id.js";
|
|
57
50
|
|
|
58
51
|
const TURN_STOPPED_MESSAGE = "Turn stopped";
|
|
59
52
|
|
|
60
|
-
/** Provider context-length error 在当前 step 内最多压缩重试三次。 */
|
|
61
|
-
const MAX_CONTEXT_ERROR_COMPACTION_RETRIES = 3;
|
|
62
|
-
|
|
63
53
|
interface CoreEngineRunnerOptions {
|
|
64
54
|
/** 当前 Session 稳定标识。 */
|
|
65
55
|
session_id: string;
|
|
@@ -76,16 +66,6 @@ interface CoreEngineRunnerOptions {
|
|
|
76
66
|
}
|
|
77
67
|
|
|
78
68
|
interface CoreEngineTurnInput {
|
|
79
|
-
/**
|
|
80
|
-
* 已装配好的执行输入。
|
|
81
|
-
*/
|
|
82
|
-
execute_input: SessionStepExecutionInput;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 当前轮模型实例。
|
|
86
|
-
*/
|
|
87
|
-
model: ModelClient;
|
|
88
|
-
|
|
89
69
|
/**
|
|
90
70
|
* 当前显式运行上下文。
|
|
91
71
|
*/
|
|
@@ -94,24 +74,7 @@ interface CoreEngineTurnInput {
|
|
|
94
74
|
/**
|
|
95
75
|
* 在统一输入队列提交后解析当前 Session step 的 effective 配置。
|
|
96
76
|
*/
|
|
97
|
-
|
|
98
|
-
/** 当前 Session step 使用的模型。 */
|
|
99
|
-
model: ModelClient;
|
|
100
|
-
/** 当前 Session step 使用的 system messages。 */
|
|
101
|
-
system: SessionStepExecutionInput["system"];
|
|
102
|
-
/** 当前 Session step 使用的工具集合。 */
|
|
103
|
-
tools: SessionStepExecutionInput["tools"];
|
|
104
|
-
/** 当前 Session step 模型支持的总上下文窗口长度。 */
|
|
105
|
-
context_window?: number;
|
|
106
|
-
}>;
|
|
107
|
-
|
|
108
|
-
/** 持久化 compact 后重新读取 Composer 生成的模型历史与 Summary 身份。 */
|
|
109
|
-
reload_history: () => Promise<{
|
|
110
|
-
/** Composer 重新生成的标准模型消息。 */
|
|
111
|
-
messages: ModelMessage[];
|
|
112
|
-
/** 当前模型历史包含的最新持久化 Summary 标识。 */
|
|
113
|
-
summary_id?: string;
|
|
114
|
-
}>;
|
|
77
|
+
resolve_step_input: () => Promise<SessionStepExecutionInput>;
|
|
115
78
|
}
|
|
116
79
|
|
|
117
80
|
/**
|
|
@@ -122,9 +85,6 @@ export class CoreEngineRunner {
|
|
|
122
85
|
private readonly logger: Logger;
|
|
123
86
|
private readonly should_compact_on_error: CoreEngineRunnerOptions["should_compact_on_error"];
|
|
124
87
|
|
|
125
|
-
/** 最近一次已经通过真实 usage 验收的持久化 Summary 标识。 */
|
|
126
|
-
private validated_compaction_summary_id = "";
|
|
127
|
-
|
|
128
88
|
constructor(options: CoreEngineRunnerOptions) {
|
|
129
89
|
this.session_id = String(options.session_id || "").trim();
|
|
130
90
|
this.logger = options.logger;
|
|
@@ -140,25 +100,11 @@ export class CoreEngineRunner {
|
|
|
140
100
|
async execute(input: CoreEngineTurnInput): Promise<SessionTurnExecutionResult> {
|
|
141
101
|
const start_time = Date.now();
|
|
142
102
|
const session_id = this.session_id;
|
|
143
|
-
let system = Array.isArray(input.execute_input.system)
|
|
144
|
-
? input.execute_input.system
|
|
145
|
-
: [];
|
|
146
|
-
let tools = input.execute_input.tools;
|
|
147
103
|
let last_observed_stream_error: unknown = undefined;
|
|
148
|
-
let final_assistant_parts:
|
|
104
|
+
let final_assistant_parts: SessionAgentMessagePart[] = [];
|
|
149
105
|
let compact_required = false;
|
|
150
106
|
|
|
151
107
|
try {
|
|
152
|
-
const message_state = await CoreEngineMessageState.create({
|
|
153
|
-
messages: input.execute_input.messages,
|
|
154
|
-
project_root: input.turn_context.session.project_root,
|
|
155
|
-
});
|
|
156
|
-
let persisted_compaction_summary_id =
|
|
157
|
-
input.execute_input.history_summary_id || "";
|
|
158
|
-
|
|
159
|
-
const append_merged_user_messages = (messages: SessionUserMessage[]) =>
|
|
160
|
-
message_state.append_merged_user_messages(messages);
|
|
161
|
-
|
|
162
108
|
let step_count = 0;
|
|
163
109
|
let total_tool_call_count = 0;
|
|
164
110
|
let total_tool_result_count = 0;
|
|
@@ -181,63 +127,15 @@ export class CoreEngineRunner {
|
|
|
181
127
|
};
|
|
182
128
|
|
|
183
129
|
let incomplete_response_recovery_count = 0;
|
|
184
|
-
let context_error_compaction_retries = 0;
|
|
185
|
-
let compact_pending = false;
|
|
186
|
-
let compact_validation_pending = Boolean(
|
|
187
|
-
persisted_compaction_summary_id &&
|
|
188
|
-
persisted_compaction_summary_id !==
|
|
189
|
-
this.validated_compaction_summary_id,
|
|
190
|
-
);
|
|
191
|
-
let compact_depth = 0;
|
|
192
|
-
|
|
193
130
|
while (step_count < MAX_TOOL_LOOP_STEPS) {
|
|
194
131
|
// 关键点(中文):steer 与 command 在同一个 Session step 检查点执行。
|
|
195
132
|
// 当前流与 tool callback 保持原执行视图,下一 step 再统一读取 effective 配置。
|
|
196
|
-
await
|
|
197
|
-
|
|
198
|
-
);
|
|
199
|
-
const step_inputs = await input.resolve_step_inputs();
|
|
200
|
-
system = Array.isArray(step_inputs.system) ? step_inputs.system : [];
|
|
201
|
-
tools = step_inputs.tools;
|
|
202
|
-
if (input.turn_context.input.consume_history_reload()) {
|
|
203
|
-
const reloaded_history = await input.reload_history();
|
|
204
|
-
message_state.replace_model_history(reloaded_history.messages);
|
|
205
|
-
persisted_compaction_summary_id = reloaded_history.summary_id || "";
|
|
206
|
-
compact_validation_pending = Boolean(
|
|
207
|
-
persisted_compaction_summary_id &&
|
|
208
|
-
persisted_compaction_summary_id !==
|
|
209
|
-
this.validated_compaction_summary_id,
|
|
210
|
-
);
|
|
211
|
-
await this.logger.log("info", "[agent] context.history_reloaded", {
|
|
212
|
-
session_id: session_id,
|
|
213
|
-
recordCount: reloaded_history.messages.length,
|
|
214
|
-
compactionSummaryId: persisted_compaction_summary_id || undefined,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
if (compact_pending) {
|
|
218
|
-
const previous_message_count = message_state.model_messages.length;
|
|
219
|
-
message_state.replace_model_messages(
|
|
220
|
-
deep_compact_model_messages(
|
|
221
|
-
message_state.model_messages,
|
|
222
|
-
compact_depth,
|
|
223
|
-
),
|
|
224
|
-
);
|
|
225
|
-
compact_depth += 1;
|
|
226
|
-
compact_pending = false;
|
|
227
|
-
compact_validation_pending = true;
|
|
228
|
-
compact_required = true;
|
|
229
|
-
await this.logger.log("info", "[agent] context.compacted", {
|
|
230
|
-
session_id: session_id,
|
|
231
|
-
reason: "usage_threshold",
|
|
232
|
-
compactDepth: compact_depth,
|
|
233
|
-
previousMessageCount: previous_message_count,
|
|
234
|
-
nextMessageCount: message_state.model_messages.length,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
133
|
+
await input.turn_context.input.checkpoint();
|
|
134
|
+
const step_input = await input.resolve_step_input();
|
|
237
135
|
|
|
238
136
|
last_observed_stream_error = undefined;
|
|
239
|
-
let step_assistant_parts:
|
|
240
|
-
let
|
|
137
|
+
let step_assistant_parts: SessionAgentMessagePart[];
|
|
138
|
+
let step_result: ModelStepResult;
|
|
241
139
|
try {
|
|
242
140
|
const result = await execute_model_request({
|
|
243
141
|
request_kind: "turn",
|
|
@@ -245,16 +143,13 @@ export class CoreEngineRunner {
|
|
|
245
143
|
should_retry: (error) => !this.should_compact_on_error(error),
|
|
246
144
|
on_failure: async (notice, error) => {
|
|
247
145
|
const is_compact_error = this.should_compact_on_error(error);
|
|
248
|
-
const can_compact = is_compact_error &&
|
|
249
|
-
context_error_compaction_retries <
|
|
250
|
-
MAX_CONTEXT_ERROR_COMPACTION_RETRIES;
|
|
251
146
|
input.turn_context.output.report_model_request_failure({
|
|
252
147
|
...notice,
|
|
253
148
|
...(is_compact_error
|
|
254
149
|
? {
|
|
255
|
-
attempt:
|
|
256
|
-
max_attempts:
|
|
257
|
-
will_retry:
|
|
150
|
+
attempt: 1,
|
|
151
|
+
max_attempts: 1,
|
|
152
|
+
will_retry: false,
|
|
258
153
|
}
|
|
259
154
|
: {}),
|
|
260
155
|
});
|
|
@@ -272,10 +167,10 @@ export class CoreEngineRunner {
|
|
|
272
167
|
await input.turn_context.output.assistant?.begin_step();
|
|
273
168
|
try {
|
|
274
169
|
return await run_model_step({
|
|
275
|
-
model:
|
|
276
|
-
system,
|
|
277
|
-
messages:
|
|
278
|
-
tools,
|
|
170
|
+
model: step_input.model,
|
|
171
|
+
system: step_input.system,
|
|
172
|
+
messages: step_input.messages,
|
|
173
|
+
tools: step_input.tools,
|
|
279
174
|
abort_signal: input.turn_context.lifecycle.abort_signal,
|
|
280
175
|
...(input.turn_context.output.assistant
|
|
281
176
|
? { assistant_output: input.turn_context.output.assistant }
|
|
@@ -303,94 +198,60 @@ export class CoreEngineRunner {
|
|
|
303
198
|
const action_assistant_parts =
|
|
304
199
|
input.turn_context.output.take_assistant_parts();
|
|
305
200
|
if (action_assistant_parts.length > 0) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
201
|
+
const persisted_parts = input.turn_context.output.assistant
|
|
202
|
+
? await input.turn_context.output.assistant.append_result_parts(
|
|
203
|
+
action_assistant_parts,
|
|
204
|
+
)
|
|
205
|
+
: action_assistant_parts.map((part, index) =>
|
|
206
|
+
create_session_agent_content_part(
|
|
207
|
+
part,
|
|
208
|
+
`${part.type}:${generate_id()}`,
|
|
209
|
+
index + 1,
|
|
210
|
+
)
|
|
211
|
+
);
|
|
309
212
|
step_assistant_parts = merge_assistant_parts(
|
|
310
213
|
step_assistant_parts,
|
|
311
|
-
|
|
214
|
+
persisted_parts,
|
|
312
215
|
);
|
|
313
216
|
}
|
|
314
217
|
final_assistant_parts = merge_assistant_parts(
|
|
315
218
|
final_assistant_parts,
|
|
316
219
|
step_assistant_parts,
|
|
317
220
|
);
|
|
318
|
-
|
|
221
|
+
step_result = result.step_result;
|
|
319
222
|
} catch (error) {
|
|
320
223
|
const compact_error = this.should_compact_on_error(error)
|
|
321
224
|
? error
|
|
322
225
|
: last_observed_stream_error;
|
|
323
|
-
|
|
324
|
-
const can_retry_after_compaction =
|
|
325
|
-
is_compact_error &&
|
|
326
|
-
context_error_compaction_retries <
|
|
327
|
-
MAX_CONTEXT_ERROR_COMPACTION_RETRIES;
|
|
328
|
-
if (can_retry_after_compaction) {
|
|
329
|
-
context_error_compaction_retries += 1;
|
|
330
|
-
const previous_message_count = message_state.model_messages.length;
|
|
331
|
-
message_state.replace_model_messages(
|
|
332
|
-
deep_compact_model_messages(
|
|
333
|
-
message_state.model_messages,
|
|
334
|
-
compact_depth,
|
|
335
|
-
),
|
|
336
|
-
);
|
|
337
|
-
compact_depth += 1;
|
|
338
|
-
compact_pending = false;
|
|
339
|
-
compact_validation_pending = true;
|
|
340
|
-
compact_required = true;
|
|
341
|
-
await this.logger.log("warn", "[agent] context.compacted", {
|
|
342
|
-
session_id: session_id,
|
|
343
|
-
reason: "provider_context_error",
|
|
344
|
-
retryCount: context_error_compaction_retries,
|
|
345
|
-
compactDepth: compact_depth,
|
|
346
|
-
previousMessageCount: previous_message_count,
|
|
347
|
-
nextMessageCount: message_state.model_messages.length,
|
|
348
|
-
...summarize_stream_error(compact_error),
|
|
349
|
-
});
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
226
|
+
if (this.should_compact_on_error(compact_error)) throw compact_error;
|
|
352
227
|
throw error;
|
|
353
228
|
}
|
|
354
229
|
|
|
355
|
-
context_error_compaction_retries = 0;
|
|
356
|
-
const last_step = executed_steps[executed_steps.length - 1];
|
|
357
|
-
if (!last_step) break;
|
|
358
|
-
|
|
359
230
|
const usage_ratio = resolve_model_usage_ratio(
|
|
360
|
-
|
|
361
|
-
|
|
231
|
+
step_result.usage,
|
|
232
|
+
step_input.context_window,
|
|
362
233
|
);
|
|
363
234
|
if (usage_ratio !== null) {
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
usage_ratio,
|
|
367
|
-
validating_compaction,
|
|
368
|
-
);
|
|
369
|
-
compact_validation_pending = false;
|
|
370
|
-
if (validating_compaction && persisted_compaction_summary_id) {
|
|
371
|
-
this.validated_compaction_summary_id =
|
|
372
|
-
persisted_compaction_summary_id;
|
|
373
|
-
}
|
|
374
|
-
if (compact_pending) compact_required = true;
|
|
235
|
+
const pressure_detected = should_compact_after_usage(usage_ratio);
|
|
236
|
+
if (pressure_detected) compact_required = true;
|
|
375
237
|
await this.logger.log("info", "[agent] context.usage", {
|
|
376
238
|
session_id: session_id,
|
|
377
239
|
step_index: step_count,
|
|
378
240
|
usageRatio: usage_ratio,
|
|
379
|
-
contextWindow:
|
|
380
|
-
|
|
381
|
-
compactPending: compact_pending,
|
|
241
|
+
contextWindow: step_input.context_window,
|
|
242
|
+
compactPending: pressure_detected,
|
|
382
243
|
});
|
|
383
244
|
}
|
|
384
245
|
|
|
385
246
|
const incomplete_response = detect_incomplete_response({
|
|
386
|
-
step_result
|
|
247
|
+
step_result,
|
|
387
248
|
assistant_parts: step_assistant_parts,
|
|
388
249
|
});
|
|
389
250
|
const loop_decision = evaluate_core_engine_loop_decision({
|
|
390
251
|
hasIncompleteResponse: incomplete_response !== null,
|
|
391
252
|
incompleteRecoveryCount: incomplete_response_recovery_count,
|
|
392
253
|
maxIncompleteRecoveries: MAX_INCOMPLETE_RESPONSE_RECOVERIES,
|
|
393
|
-
toolCallCount:
|
|
254
|
+
toolCallCount: step_result.tool_calls.length,
|
|
394
255
|
});
|
|
395
256
|
|
|
396
257
|
await this.logger.log("info", "[agent] loop.decision", {
|
|
@@ -402,10 +263,10 @@ export class CoreEngineRunner {
|
|
|
402
263
|
decisionKind: loop_decision.kind,
|
|
403
264
|
incompleteResponseReason: incomplete_response?.reason ?? null,
|
|
404
265
|
incompleteResponseRecoveryCount: incomplete_response_recovery_count,
|
|
405
|
-
toolCallCount:
|
|
406
|
-
toolResultCount:
|
|
407
|
-
finishReason:
|
|
408
|
-
textPreview: to_inline_preview(
|
|
266
|
+
toolCallCount: step_result.tool_calls.length,
|
|
267
|
+
toolResultCount: step_result.tool_results.length,
|
|
268
|
+
finishReason: step_result.finish_reason,
|
|
269
|
+
textPreview: to_inline_preview(step_result.text),
|
|
409
270
|
});
|
|
410
271
|
|
|
411
272
|
if (
|
|
@@ -420,18 +281,12 @@ export class CoreEngineRunner {
|
|
|
420
281
|
reason: incomplete_response.reason,
|
|
421
282
|
...incomplete_response.details,
|
|
422
283
|
});
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
internal: "agent_incomplete_response_recover",
|
|
430
|
-
reason: incomplete_response.reason,
|
|
431
|
-
step_index: step_count,
|
|
432
|
-
},
|
|
433
|
-
});
|
|
434
|
-
await message_state.append_user_message(recovery_message);
|
|
284
|
+
await input.turn_context.input.append_internal([{
|
|
285
|
+
type: "text",
|
|
286
|
+
text: build_incomplete_response_recovery_nudge(
|
|
287
|
+
incomplete_response_recovery_count,
|
|
288
|
+
),
|
|
289
|
+
}]);
|
|
435
290
|
continue;
|
|
436
291
|
}
|
|
437
292
|
|
|
@@ -448,11 +303,6 @@ export class CoreEngineRunner {
|
|
|
448
303
|
);
|
|
449
304
|
}
|
|
450
305
|
|
|
451
|
-
const response_messages = Array.isArray(last_step.response?.messages)
|
|
452
|
-
? last_step.response.messages
|
|
453
|
-
: [];
|
|
454
|
-
message_state.append_model_messages(response_messages);
|
|
455
|
-
|
|
456
306
|
if (loop_decision.continueForToolCalls) {
|
|
457
307
|
incomplete_response_recovery_count = 0;
|
|
458
308
|
continue;
|
|
@@ -511,9 +361,6 @@ export class CoreEngineRunner {
|
|
|
511
361
|
success: true,
|
|
512
362
|
text: extract_assistant_text(final_parts),
|
|
513
363
|
...(compact_required ? { compact_required: true } : {}),
|
|
514
|
-
deferred_persisted_user_messages: [
|
|
515
|
-
...input.turn_context.input.deferred_user_messages(),
|
|
516
|
-
],
|
|
517
364
|
};
|
|
518
365
|
} catch (error) {
|
|
519
366
|
if (input.turn_context.lifecycle.abort_signal.aborted) {
|
|
@@ -526,9 +373,6 @@ export class CoreEngineRunner {
|
|
|
526
373
|
text: extract_assistant_text(final_assistant_parts),
|
|
527
374
|
error: error_text,
|
|
528
375
|
...(compact_required ? { compact_required: true } : {}),
|
|
529
|
-
deferred_persisted_user_messages: [
|
|
530
|
-
...input.turn_context.input.deferred_user_messages(),
|
|
531
|
-
],
|
|
532
376
|
};
|
|
533
377
|
}
|
|
534
378
|
|
|
@@ -550,9 +394,6 @@ export class CoreEngineRunner {
|
|
|
550
394
|
text: extract_assistant_text(final_assistant_parts),
|
|
551
395
|
error: error_text,
|
|
552
396
|
...(compact_required ? { compact_required: true } : {}),
|
|
553
|
-
deferred_persisted_user_messages: [
|
|
554
|
-
...input.turn_context.input.deferred_user_messages(),
|
|
555
|
-
],
|
|
556
397
|
};
|
|
557
398
|
}
|
|
558
399
|
}
|
|
@@ -603,73 +444,10 @@ async function resolve_tool_approval(input: {
|
|
|
603
444
|
payload?.decision === "approved";
|
|
604
445
|
}
|
|
605
446
|
|
|
606
|
-
/** 构造仅在当前 Turn 内使用的内部 User Message。 */
|
|
607
|
-
function build_internal_user_message(input: {
|
|
608
|
-
session_id: string;
|
|
609
|
-
text: string;
|
|
610
|
-
extra: JsonObject;
|
|
611
|
-
}): SessionUserMessage {
|
|
612
|
-
void input.extra;
|
|
613
|
-
const now = Date.now();
|
|
614
|
-
return {
|
|
615
|
-
message_id: `runtime-user:${input.session_id}:${now}`,
|
|
616
|
-
session_id: input.session_id,
|
|
617
|
-
sequence: 0,
|
|
618
|
-
revision: 1,
|
|
619
|
-
visibility: "internal",
|
|
620
|
-
created_at: now,
|
|
621
|
-
updated_at: now,
|
|
622
|
-
type: "user",
|
|
623
|
-
input_type: "steer",
|
|
624
|
-
parts: [{
|
|
625
|
-
part_id: "runtime-text:1",
|
|
626
|
-
type: "text",
|
|
627
|
-
text: input.text,
|
|
628
|
-
state: "done",
|
|
629
|
-
}],
|
|
630
|
-
};
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
/** 把 Action 结果内容转换为当前 Turn 使用的 canonical Assistant Parts。 */
|
|
634
|
-
function action_parts_to_canonical(
|
|
635
|
-
parts: readonly SessionAssistantResultPart[],
|
|
636
|
-
): SessionAssistantMessagePart[] {
|
|
637
|
-
return parts.map((part, index) => {
|
|
638
|
-
const sequence = index + 1;
|
|
639
|
-
if (part.type === "text") {
|
|
640
|
-
return {
|
|
641
|
-
part_id: `action-text:${sequence}`,
|
|
642
|
-
sequence,
|
|
643
|
-
type: "text",
|
|
644
|
-
text: part.text,
|
|
645
|
-
state: "done",
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
if (part.type === "file") {
|
|
649
|
-
return {
|
|
650
|
-
part_id: `action-file:${sequence}`,
|
|
651
|
-
sequence,
|
|
652
|
-
type: "file",
|
|
653
|
-
media_type: part.media_type,
|
|
654
|
-
url: part.url,
|
|
655
|
-
...(part.filename ? { filename: part.filename } : {}),
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
return {
|
|
659
|
-
part_id: `action-data:${sequence}`,
|
|
660
|
-
sequence,
|
|
661
|
-
type: "data",
|
|
662
|
-
data_type: part.data_type,
|
|
663
|
-
data: part.data,
|
|
664
|
-
...(part.data_id ? { data_id: part.data_id } : {}),
|
|
665
|
-
};
|
|
666
|
-
});
|
|
667
|
-
}
|
|
668
|
-
|
|
669
447
|
/** 构造成功执行但缺少最终内容时使用的 canonical Assistant Parts。 */
|
|
670
448
|
function build_fallback_assistant_parts(
|
|
671
449
|
text: string,
|
|
672
|
-
):
|
|
450
|
+
): SessionAgentMessagePart[] {
|
|
673
451
|
return [{
|
|
674
452
|
part_id: "fallback-text:1",
|
|
675
453
|
sequence: 1,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* CoreEngine Tool Loop 的纯信号与诊断模块。
|
|
3
3
|
*
|
|
4
|
-
* 所有消息判断都直接读取 canonical `
|
|
4
|
+
* 所有消息判断都直接读取 canonical `SessionAgentMessagePart`,不依赖 UI 投影。
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { JsonObject } from "@downcity/type";
|
|
8
|
-
import type {
|
|
8
|
+
import type { SessionAgentMessagePart } from "@downcity/type";
|
|
9
9
|
|
|
10
10
|
/** 单次 Tool Loop 允许的最大 Step 数。 */
|
|
11
11
|
export const MAX_TOOL_LOOP_STEPS = 64;
|
|
@@ -60,7 +60,7 @@ export function summarize_step_for_debug(step_result: unknown): JsonObject {
|
|
|
60
60
|
|
|
61
61
|
/** 汇总最终 canonical Assistant Parts。 */
|
|
62
62
|
export function summarize_assistant_parts_for_debug(
|
|
63
|
-
parts: readonly
|
|
63
|
+
parts: readonly SessionAgentMessagePart[],
|
|
64
64
|
): JsonObject {
|
|
65
65
|
const text = extract_assistant_text(parts);
|
|
66
66
|
const tool_names = parts
|
|
@@ -79,9 +79,9 @@ export function summarize_assistant_parts_for_debug(
|
|
|
79
79
|
|
|
80
80
|
/** 按 Step 顺序合并 canonical Assistant Parts。 */
|
|
81
81
|
export function merge_assistant_parts(
|
|
82
|
-
base: readonly
|
|
83
|
-
incoming: readonly
|
|
84
|
-
):
|
|
82
|
+
base: readonly SessionAgentMessagePart[],
|
|
83
|
+
incoming: readonly SessionAgentMessagePart[],
|
|
84
|
+
): SessionAgentMessagePart[] {
|
|
85
85
|
return [...base, ...incoming].map((part, index) => ({
|
|
86
86
|
...part,
|
|
87
87
|
sequence: index + 1,
|
|
@@ -90,7 +90,7 @@ export function merge_assistant_parts(
|
|
|
90
90
|
|
|
91
91
|
/** 从 canonical Assistant Parts 提取用户可见文本。 */
|
|
92
92
|
export function extract_assistant_text(
|
|
93
|
-
parts: readonly
|
|
93
|
+
parts: readonly SessionAgentMessagePart[],
|
|
94
94
|
): string {
|
|
95
95
|
const chat_send = [...parts].reverse().find(
|
|
96
96
|
(part) => part.type === "tool" && part.tool_name === "chat_send",
|
|
@@ -124,7 +124,7 @@ export function detect_incomplete_response(input: {
|
|
|
124
124
|
/** 当前 Step 结果。 */
|
|
125
125
|
step_result: unknown;
|
|
126
126
|
/** 当前 Step canonical Assistant Parts。 */
|
|
127
|
-
assistant_parts: readonly
|
|
127
|
+
assistant_parts: readonly SessionAgentMessagePart[];
|
|
128
128
|
}): { reason: string; details: JsonObject } | null {
|
|
129
129
|
const record = to_json_object(input.step_result) ?? {};
|
|
130
130
|
const finish_reason = typeof record.finish_reason === "string"
|
|
@@ -132,7 +132,7 @@ export function detect_incomplete_response(input: {
|
|
|
132
132
|
: "";
|
|
133
133
|
const text = typeof record.text === "string" ? record.text.trim() : "";
|
|
134
134
|
const incomplete_tools = input.assistant_parts
|
|
135
|
-
.filter((part): part is Extract<
|
|
135
|
+
.filter((part): part is Extract<SessionAgentMessagePart, { type: "tool" }> =>
|
|
136
136
|
part.type === "tool" &&
|
|
137
137
|
part.state !== "completed" &&
|
|
138
138
|
part.state !== "failed"
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* 模型执行阶段的文件读取与 `ModelFileContent` 转换由 `SessionModelMessages` 负责。
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {
|
|
8
|
+
import type { SessionUserContent } from "@downcity/type";
|
|
9
9
|
import type { SessionAttachmentStore } from "@/types/store/SessionAttachmentStore.js";
|
|
10
10
|
|
|
11
11
|
/** 在用户 prompt 入库前持久化 Data URL 文件。 */
|
|
12
12
|
export async function persist_user_prompt_file_parts(
|
|
13
|
-
parts:
|
|
13
|
+
parts: SessionUserContent[],
|
|
14
14
|
attachment_store: SessionAttachmentStore,
|
|
15
|
-
): Promise<
|
|
16
|
-
const output:
|
|
15
|
+
): Promise<SessionUserContent[]> {
|
|
16
|
+
const output: SessionUserContent[] = [];
|
|
17
17
|
for (const part of parts) {
|
|
18
18
|
if (part.type !== "file" || !part.url.startsWith("data:")) {
|
|
19
19
|
output.push(part);
|
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* 该模块只提取可见文本并写入统一 Logger,不参与协议转换或持久化。
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type {
|
|
7
|
+
import type { SessionAgentMessagePart } from "@downcity/type";
|
|
8
8
|
import type { Logger } from "@/utils/logger/Logger.js";
|
|
9
9
|
|
|
10
10
|
/** 从 canonical Assistant Parts 提取可见文本。 */
|
|
11
11
|
export function extract_assistant_text_for_log(
|
|
12
|
-
parts: readonly
|
|
12
|
+
parts: readonly SessionAgentMessagePart[],
|
|
13
13
|
): string {
|
|
14
14
|
return parts
|
|
15
15
|
.flatMap((part) => part.type === "text" ? [part.text] : [])
|
|
@@ -20,7 +20,7 @@ export function extract_assistant_text_for_log(
|
|
|
20
20
|
/** 立即输出 Assistant 文本日志。 */
|
|
21
21
|
export async function log_assistant_message_now(
|
|
22
22
|
logger: Logger,
|
|
23
|
-
parts: readonly
|
|
23
|
+
parts: readonly SessionAgentMessagePart[],
|
|
24
24
|
): Promise<void> {
|
|
25
25
|
const text = extract_assistant_text_for_log(parts) || "-";
|
|
26
26
|
const lines = text.replace(/\r\n/gu, "\n").split("\n");
|