@downcity/agent 1.1.430 → 1.1.437

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