@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.
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
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * 职责说明(中文)
5
5
  * - 统一管理 Session 创建判断、删除、列表、归档与清理。
6
- * - 缓存稳定的 LocalSessionDataStore,避免同一 Session 重复创建 Message Store
6
+ * - 缓存稳定的 LocalSessionDataStore,避免同一 Session 重复创建 SQLite Storage
7
7
  * - 使用来源类型建立确定性一级分区,避免跨来源扫描。
8
8
  */
9
9
 
@@ -14,23 +14,25 @@ import type {
14
14
  AgentArchiveSessionsResult,
15
15
  AgentCleanArchiveResult,
16
16
  AgentListSessionsInput,
17
+ AgentSessionSummary,
17
18
  AgentSessionSummaryPage,
18
19
  } from "@/types/agent/SessionTypes.js";
19
20
  import type { SessionStore } from "@/types/store/SessionStore.js";
20
- import type { SessionDataStore } from "@/types/store/SessionDataStore.js";
21
+ import type { SessionStorage } from "@/types/store/SessionStorage.js";
21
22
  import { LocalSessionDataStore } from "@/session/storage/LocalSessionDataStore.js";
22
23
  import {
23
- get_agent_archived_session_meta_path,
24
+ get_agent_archived_session_database_path,
24
25
  get_agent_archived_session_origins_path,
25
26
  get_agent_archived_session_path,
26
27
  get_agent_archived_sessions_path,
27
- get_agent_session_messages_path,
28
- get_agent_session_meta_path,
28
+ get_agent_session_database_path,
29
29
  get_agent_session_path,
30
30
  } from "@/session/storage/LocalStorePaths.js";
31
31
  import {
32
+ build_session_info,
32
33
  list_archived_agent_session_summary_page,
33
34
  list_agent_session_summary_page,
35
+ read_session_metadata_from_database,
34
36
  } from "@/session/browse/Browse.js";
35
37
  import type { FileSystem } from "@downcity/type";
36
38
  import type { LocalSessionStoreOptions } from "@/types/store/LocalStore.js";
@@ -60,25 +62,32 @@ export class LocalSessionStore implements SessionStore {
60
62
  /** 当前 Agent 内部数据根路径。 */
61
63
  private readonly storage_root_path: string;
62
64
 
65
+ /** Session 数据库使用本地文件还是进程内存。 */
66
+ private readonly database_location: LocalSessionStoreOptions["database_location"];
67
+
63
68
  /** 已创建的 Session Store 缓存。 */
64
69
  private readonly sessions = new Map<string, LocalSessionDataStore>();
65
70
 
71
+ /** 内存模式中已归档但仍由 Store 持有的 Session。 */
72
+ private readonly archived_sessions = new Map<string, LocalSessionDataStore>();
73
+
66
74
  constructor(options: LocalSessionStoreOptions) {
67
75
  this.files = options.files;
68
76
  this.agent_id = options.agent_id;
69
77
  this.workspace_id = options.workspace_id;
70
78
  this.storage_root_path = options.storage_root_path;
79
+ this.database_location = options.database_location;
71
80
  }
72
81
 
73
- /** 返回指定 Session 的稳定持久化视图。 */
74
- session(
82
+ /** 为新 Session 创建稳定持久化视图。 */
83
+ create_session(
75
84
  session_id: string,
76
85
  origin: SessionOrigin,
77
86
  workspace_id = this.workspace_id,
78
- ): SessionDataStore {
87
+ ): SessionStorage {
79
88
  const resolved_session_id = String(session_id || "").trim();
80
89
  if (!resolved_session_id) {
81
- throw new Error("SessionStore.session requires a non-empty session_id");
90
+ throw new Error("SessionStore.create_session requires a non-empty session_id");
82
91
  }
83
92
  const resolved_origin = normalize_session_origin(origin);
84
93
  const cache_key = this.session_cache_key(resolved_session_id, resolved_origin.type);
@@ -94,6 +103,7 @@ export class LocalSessionStore implements SessionStore {
94
103
  files: this.files,
95
104
  storage_root_path: this.storage_root_path,
96
105
  agent_id: this.agent_id,
106
+ database_location: this.database_location,
97
107
  workspace_id: workspace_id,
98
108
  session_id: resolved_session_id,
99
109
  origin: resolved_origin,
@@ -102,11 +112,55 @@ export class LocalSessionStore implements SessionStore {
102
112
  return created;
103
113
  }
104
114
 
115
+ /** 打开已有 Session,并以数据库中的完整 origin 作为唯一事实。 */
116
+ async open_session(session_id: string, origin_type = "chat"): Promise<SessionStorage> {
117
+ const resolved_session_id = String(session_id || "").trim();
118
+ if (!resolved_session_id) {
119
+ throw new Error("SessionStore.open_session requires a non-empty session_id");
120
+ }
121
+ const resolved_origin_type = normalize_session_origin_type(origin_type);
122
+ const cache_key = this.session_cache_key(resolved_session_id, resolved_origin_type);
123
+ const cached = this.sessions.get(cache_key);
124
+ if (cached) return cached;
125
+ if (this.database_location.type === "memory") {
126
+ throw new Error(`Session "${resolved_session_id}" not found`);
127
+ }
128
+ const database_path = get_agent_session_database_path(
129
+ this.storage_root_path,
130
+ resolved_origin_type,
131
+ resolved_session_id,
132
+ );
133
+ if (!(await this.files.path_exists(database_path))) {
134
+ throw new Error(`Session "${resolved_session_id}" not found`);
135
+ }
136
+ const metadata = read_session_metadata_from_database(database_path, resolved_origin_type);
137
+ if (!metadata || metadata.agent_id !== this.agent_id) {
138
+ throw new Error(`Session "${resolved_session_id}" belongs to another Agent`);
139
+ }
140
+ const opened = new LocalSessionDataStore({
141
+ files: this.files,
142
+ storage_root_path: this.storage_root_path,
143
+ agent_id: this.agent_id,
144
+ database_location: this.database_location,
145
+ ...(metadata.workspace_id ? { workspace_id: metadata.workspace_id } : {}),
146
+ session_id: resolved_session_id,
147
+ origin: metadata.origin,
148
+ });
149
+ this.sessions.set(cache_key, opened);
150
+ return opened;
151
+ }
152
+
105
153
  /** 判断活动 Session 是否存在。 */
106
154
  async has_session(session_id: string, origin_type = "chat"): Promise<boolean> {
107
155
  const resolved_origin_type = normalize_session_origin_type(origin_type);
108
- const session_path = this.session_path(session_id, resolved_origin_type);
109
- if (!(await this.files.path_exists(session_path))) return false;
156
+ const cache_key = this.session_cache_key(session_id, resolved_origin_type);
157
+ if (this.database_location.type === "memory") return this.sessions.has(cache_key);
158
+ const database_path = get_agent_session_database_path(
159
+ this.storage_root_path,
160
+ resolved_origin_type,
161
+ session_id,
162
+ );
163
+ if (!(await this.files.path_exists(database_path))) return false;
110
164
  await this.assert_session_owner(session_id, resolved_origin_type, false);
111
165
  return true;
112
166
  }
@@ -115,30 +169,31 @@ export class LocalSessionStore implements SessionStore {
115
169
  async remove_session(session_id: string, origin_type = "chat"): Promise<boolean> {
116
170
  const resolved_origin_type = normalize_session_origin_type(origin_type);
117
171
  const session_path = this.session_path(session_id, resolved_origin_type);
118
- const existed = await this.files.path_exists(session_path);
172
+ const cache_key = this.session_cache_key(session_id, resolved_origin_type);
173
+ const cached = this.sessions.get(cache_key);
174
+ const existed = this.database_location.type === "memory"
175
+ ? Boolean(cached)
176
+ : await this.files.path_exists(get_agent_session_database_path(
177
+ this.storage_root_path,
178
+ resolved_origin_type,
179
+ session_id,
180
+ ));
119
181
  if (existed) {
120
182
  await this.assert_session_owner(session_id, resolved_origin_type, false);
183
+ await cached?.dispose();
121
184
  await this.files.remove_path(session_path);
122
185
  }
123
- this.sessions.delete(this.session_cache_key(session_id, resolved_origin_type));
186
+ this.sessions.delete(cache_key);
124
187
  return existed;
125
188
  }
126
189
 
127
190
  /** 清空活动 Session Message 数据。 */
128
191
  async clear_session_messages(session_id: string, origin_type = "chat"): Promise<boolean> {
129
192
  const resolved_origin_type = normalize_session_origin_type(origin_type);
130
- const messages_path = get_agent_session_messages_path(
131
- this.storage_root_path,
132
- resolved_origin_type,
133
- session_id,
134
- );
135
- const existed = await this.files.path_exists(messages_path);
136
- if (existed) {
137
- await this.assert_session_owner(session_id, resolved_origin_type, false);
138
- await this.files.remove_path(messages_path);
139
- }
140
- this.sessions.delete(this.session_cache_key(session_id, resolved_origin_type));
141
- return existed;
193
+ if (!(await this.has_session(session_id, resolved_origin_type))) return false;
194
+ const storage = await this.open_session(session_id, resolved_origin_type);
195
+ await storage.clear_messages();
196
+ return true;
142
197
  }
143
198
 
144
199
  /** 返回活动 Session 摘要页。 */
@@ -146,12 +201,15 @@ export class LocalSessionStore implements SessionStore {
146
201
  input: AgentListSessionsInput | undefined,
147
202
  executing_session_ids: ReadonlySet<string>,
148
203
  ): Promise<AgentSessionSummaryPage> {
204
+ if (this.database_location.type === "memory") {
205
+ return await this.list_cached_sessions(input, executing_session_ids);
206
+ }
149
207
  return await list_agent_session_summary_page({
150
208
  project_root: this.storage_root_path,
151
209
  agent_id: this.agent_id,
152
210
  ...(this.workspace_id || input?.workspace_id ? { workspace_id: this.workspace_id || input?.workspace_id } : {}),
153
211
  input,
154
- executingSessionIds: new Set(executing_session_ids),
212
+ executing_session_ids,
155
213
  files: this.files,
156
214
  });
157
215
  }
@@ -159,11 +217,23 @@ export class LocalSessionStore implements SessionStore {
159
217
  /** 将活动 Session 迁入归档区。 */
160
218
  async archive_session(session_id: string, origin_type = "chat"): Promise<AgentArchiveSessionResult> {
161
219
  const resolved_origin_type = normalize_session_origin_type(origin_type);
220
+ const cache_key = this.session_cache_key(session_id, resolved_origin_type);
221
+ if (this.database_location.type === "memory") {
222
+ const storage = this.sessions.get(cache_key);
223
+ if (!storage) throw new Error(`Session "${session_id}" not found`);
224
+ if (this.archived_sessions.has(cache_key)) {
225
+ throw new Error(`Archived session "${session_id}" already exists`);
226
+ }
227
+ this.sessions.delete(cache_key);
228
+ this.archived_sessions.set(cache_key, storage);
229
+ return { session_id, archived_at: Date.now() };
230
+ }
162
231
  const source_path = this.session_path(session_id, resolved_origin_type);
163
232
  if (!(await this.files.path_exists(source_path))) {
164
233
  throw new Error(`Session "${session_id}" not found`);
165
234
  }
166
235
  await this.assert_session_owner(session_id, resolved_origin_type, false);
236
+ await this.sessions.get(cache_key)?.dispose();
167
237
  const target_path = get_agent_archived_session_path(
168
238
  this.storage_root_path,
169
239
  resolved_origin_type,
@@ -177,7 +247,7 @@ export class LocalSessionStore implements SessionStore {
177
247
  resolved_origin_type,
178
248
  ));
179
249
  await this.files.move_path(source_path, target_path);
180
- this.sessions.delete(this.session_cache_key(session_id, resolved_origin_type));
250
+ this.sessions.delete(cache_key);
181
251
  return {
182
252
  session_id: session_id,
183
253
  archived_at: Date.now(),
@@ -188,6 +258,9 @@ export class LocalSessionStore implements SessionStore {
188
258
  async list_archived_sessions(
189
259
  input?: AgentArchiveSessionsInput,
190
260
  ): Promise<AgentArchiveSessionsResult> {
261
+ if (this.database_location.type === "memory") {
262
+ return await this.list_cached_sessions(input, new Set(), this.archived_sessions);
263
+ }
191
264
  return await list_archived_agent_session_summary_page({
192
265
  project_root: this.storage_root_path,
193
266
  agent_id: this.agent_id,
@@ -199,6 +272,12 @@ export class LocalSessionStore implements SessionStore {
199
272
 
200
273
  /** 永久删除全部归档 Session。 */
201
274
  async clean_archive(): Promise<AgentCleanArchiveResult> {
275
+ if (this.database_location.type === "memory") {
276
+ const archived = [...this.archived_sessions.values()];
277
+ this.archived_sessions.clear();
278
+ await Promise.all(archived.map(async (storage) => await storage.dispose()));
279
+ return { removed_session_ids: archived.map((storage) => storage.session_id) };
280
+ }
202
281
  const archive_root_path = get_agent_archived_session_origins_path(this.storage_root_path);
203
282
  if (!(await this.files.path_exists(archive_root_path))) {
204
283
  return { removed_session_ids: [] };
@@ -227,8 +306,13 @@ export class LocalSessionStore implements SessionStore {
227
306
  return { removed_session_ids: removed_session_ids };
228
307
  }
229
308
 
230
- /** 本地 JSONL Store 当前没有常驻句柄。 */
231
- async dispose(): Promise<void> {}
309
+ /** 关闭全部 Session SQLite 连接并清空缓存。 */
310
+ async dispose(): Promise<void> {
311
+ const storages = [...this.sessions.values(), ...this.archived_sessions.values()];
312
+ this.sessions.clear();
313
+ this.archived_sessions.clear();
314
+ await Promise.all(storages.map(async (storage) => await storage.dispose()));
315
+ }
232
316
 
233
317
  /** 返回活动 Session 物理目录,仅供本地实现内部使用。 */
234
318
  private session_path(session_id: string, origin_type: string): string {
@@ -257,22 +341,70 @@ export class LocalSessionStore implements SessionStore {
257
341
  origin_type: string,
258
342
  archived: boolean,
259
343
  ): Promise<boolean> {
344
+ const cache_key = this.session_cache_key(session_id, origin_type);
345
+ if (this.database_location.type === "memory") {
346
+ const cached = (archived ? this.archived_sessions : this.sessions).get(cache_key);
347
+ if (cached) {
348
+ try {
349
+ const metadata = await cached.read_metadata();
350
+ return metadata.agent_id === this.agent_id && metadata.origin.type === origin_type;
351
+ } catch {
352
+ return false;
353
+ }
354
+ }
355
+ return false;
356
+ }
260
357
  const file_path = archived
261
- ? get_agent_archived_session_meta_path(
358
+ ? get_agent_archived_session_database_path(
262
359
  this.storage_root_path,
263
360
  origin_type,
264
361
  session_id,
265
362
  )
266
- : get_agent_session_meta_path(this.storage_root_path, origin_type, session_id);
363
+ : get_agent_session_database_path(this.storage_root_path, origin_type, session_id);
267
364
  if (!(await this.files.path_exists(file_path))) return false;
268
- try {
269
- const metadata = JSON.parse(
270
- (await this.files.read_file(file_path)).toString("utf8"),
271
- ) as { agent_id?: unknown; origin?: { type?: unknown } };
272
- return metadata.agent_id === this.agent_id && metadata.origin?.type === origin_type;
273
- } catch {
274
- return false;
365
+ const metadata = read_session_metadata_from_database(file_path, origin_type);
366
+ return metadata?.agent_id === this.agent_id && metadata.origin.type === origin_type;
367
+ }
368
+
369
+ /** 从内存数据库缓存投影 Session 列表。 */
370
+ private async list_cached_sessions(
371
+ input: AgentListSessionsInput | AgentArchiveSessionsInput | undefined,
372
+ executing_session_ids: ReadonlySet<string>,
373
+ storages = this.sessions,
374
+ ): Promise<AgentSessionSummaryPage> {
375
+ const origin_type = normalize_session_origin_type(input?.origin_type ?? "chat");
376
+ const query = String(input?.query || "").trim().toLowerCase();
377
+ const summaries: AgentSessionSummary[] = [];
378
+ for (const storage of storages.values()) {
379
+ if (storage.origin.type !== origin_type) continue;
380
+ const metadata = await storage.read_metadata();
381
+ if ((this.workspace_id || input?.workspace_id) &&
382
+ metadata.workspace_id !== (this.workspace_id || input?.workspace_id)) continue;
383
+ const summary = build_session_info({
384
+ project_root: this.storage_root_path,
385
+ agent_id: this.agent_id,
386
+ session_id: storage.session_id,
387
+ metadata,
388
+ executing: executing_session_ids.has(storage.session_id),
389
+ });
390
+ if (query && ![
391
+ summary.session_id,
392
+ summary.title || "",
393
+ summary.preview_text || "",
394
+ ].join("\n").toLowerCase().includes(query)) continue;
395
+ summaries.push(summary);
275
396
  }
397
+ summaries.sort((left, right) => (right.updated_at || 0) - (left.updated_at || 0));
398
+ const cursor = Math.max(0, Number(input?.cursor) || 0);
399
+ const limit = Math.min(Math.max(input?.limit ?? 50, 1), 500);
400
+ const items = summaries.slice(cursor, cursor + limit);
401
+ const next_cursor = cursor + items.length;
402
+ return {
403
+ items,
404
+ total: summaries.length,
405
+ ...(next_cursor < summaries.length ? { next_cursor: String(next_cursor) } : {}),
406
+ has_more: next_cursor < summaries.length,
407
+ };
276
408
  }
277
409
 
278
410
  /** 返回来源分区内唯一的 Session Store 缓存键。 */
@@ -74,122 +74,50 @@ export function get_agent_session_path(
74
74
  );
75
75
  }
76
76
 
77
- /** 返回单个归档 Session 目录。 */
78
- export function get_agent_archived_session_path(
79
- storage_root_path: string,
80
- origin_type: string,
81
- session_id: string,
82
- ): string {
83
- return path.join(
84
- get_agent_archived_sessions_path(storage_root_path, origin_type),
85
- encode_path_segment(String(session_id || "").trim()),
86
- );
87
- }
88
-
89
- /** 返回单个 Session 的 instruction.md 路径。 */
90
- export function get_agent_session_instruction_path(
91
- storage_root_path: string,
92
- origin_type: string,
93
- session_id: string,
94
- ): string {
95
- return path.join(
96
- get_agent_session_path(storage_root_path, origin_type, session_id),
97
- "instruction.md",
98
- );
99
- }
100
-
101
- /** 返回单个 Session 的附件目录。 */
102
- export function get_agent_session_attachments_path(
77
+ /** 返回单个活动 Session SQLite 数据库路径。 */
78
+ export function get_agent_session_database_path(
103
79
  storage_root_path: string,
104
80
  origin_type: string,
105
81
  session_id: string,
106
82
  ): string {
107
83
  return path.join(
108
84
  get_agent_session_path(storage_root_path, origin_type, session_id),
109
- "attachments",
85
+ "session.db",
110
86
  );
111
87
  }
112
88
 
113
- /** 返回单个 Session 的消息目录。 */
114
- export function get_agent_session_messages_path(
115
- storage_root_path: string,
116
- origin_type: string,
117
- session_id: string,
118
- ): string {
119
- return path.join(
120
- get_agent_session_path(storage_root_path, origin_type, session_id),
121
- "messages",
122
- );
123
- }
124
-
125
- /** 返回单个 Session 的 Active JSONL 路径。 */
126
- export function get_agent_session_active_messages_path(
127
- storage_root_path: string,
128
- origin_type: string,
129
- session_id: string,
130
- ): string {
131
- return path.join(
132
- get_agent_session_messages_path(storage_root_path, origin_type, session_id),
133
- "active.jsonl",
134
- );
135
- }
136
-
137
- /** 返回单个 Session 的运行中 Assistant 快照路径。 */
138
- export function get_agent_session_assistant_message_path(
139
- storage_root_path: string,
140
- origin_type: string,
141
- session_id: string,
142
- ): string {
143
- return path.join(
144
- get_agent_session_messages_path(storage_root_path, origin_type, session_id),
145
- "assistant_message.json",
146
- );
147
- }
148
-
149
- /** 返回单个 Session 的 meta.json 路径。 */
150
- export function get_agent_session_meta_path(
89
+ /** 返回单个归档 Session 目录。 */
90
+ export function get_agent_archived_session_path(
151
91
  storage_root_path: string,
152
92
  origin_type: string,
153
93
  session_id: string,
154
94
  ): string {
155
95
  return path.join(
156
- get_agent_session_path(storage_root_path, origin_type, session_id),
157
- "meta.json",
96
+ get_agent_archived_sessions_path(storage_root_path, origin_type),
97
+ encode_path_segment(String(session_id || "").trim()),
158
98
  );
159
99
  }
160
100
 
161
- /** 返回单个归档 Session 的消息目录。 */
162
- export function get_agent_archived_session_messages_path(
101
+ /** 返回单个归档 Session SQLite 数据库路径。 */
102
+ export function get_agent_archived_session_database_path(
163
103
  storage_root_path: string,
164
104
  origin_type: string,
165
105
  session_id: string,
166
106
  ): string {
167
107
  return path.join(
168
108
  get_agent_archived_session_path(storage_root_path, origin_type, session_id),
169
- "messages",
170
- );
171
- }
172
-
173
- /** 返回单个归档 Session 的 Active JSONL 路径。 */
174
- export function get_agent_archived_session_active_messages_path(
175
- storage_root_path: string,
176
- origin_type: string,
177
- session_id: string,
178
- ): string {
179
- return path.join(
180
- get_agent_archived_session_messages_path(storage_root_path, origin_type, session_id),
181
- "active.jsonl",
109
+ "session.db",
182
110
  );
183
111
  }
184
112
 
185
- /** 返回单个归档 Session meta.json 路径。 */
186
- export function get_agent_archived_session_meta_path(
113
+ /** 返回单个 Session 的附件目录。 */
114
+ export function get_agent_session_attachments_path(
187
115
  storage_root_path: string,
188
116
  origin_type: string,
189
117
  session_id: string,
190
118
  ): string {
191
119
  return path.join(
192
- get_agent_archived_session_path(storage_root_path, origin_type, session_id),
193
- "meta.json",
120
+ get_agent_session_path(storage_root_path, origin_type, session_id),
121
+ "attachments",
194
122
  );
195
123
  }
@@ -2,8 +2,8 @@
2
2
  * SDK Session 元数据辅助。
3
3
  *
4
4
  * 关键点(中文)
5
- * - 统一负责 Agent Storage 内 `sessions/<origin_type>/<session_id>/meta.json` 的规范化读取。
6
- * - 仅处理轻量配置摘要与索引信息,不负责消息 JSONL 的读写。
5
+ * - 统一负责 Session SQLite 中轻量配置摘要与索引字段的规范化。
6
+ * - 不负责数据库连接、消息聚合或附件读写。
7
7
  */
8
8
 
9
9
  import type { SessionHistoryMeta } from "@/executor/types/SessionHistoryMeta.js";
@@ -57,21 +57,15 @@ function normalize_preview_text(input: unknown): string | undefined {
57
57
  return preview_text || undefined;
58
58
  }
59
59
 
60
- function normalize_history_bytes(input: unknown): number | undefined {
61
- return typeof input === "number" && Number.isInteger(input) && input >= 0
62
- ? input
63
- : undefined;
64
- }
65
-
66
60
  /**
67
- * 从指定路径读取 session meta.json
61
+ * 从指定路径读取旧版 Session metadata
68
62
  *
69
63
  * 关键点(中文)
70
64
  * - 供归档 session 等需要脱离默认 `sessions/` 目录的场景复用。
71
65
  * - 路径本身不做校验,调用方需保证可访问。
72
66
  */
73
67
  export async function read_session_metadata_from_path(input: {
74
- /** meta.json 文件路径。 */
68
+ /** 旧版 metadata 文件路径。 */
75
69
  filePath: string;
76
70
  /** 当前 session_id。 */
77
71
  session_id: string;
@@ -143,8 +137,5 @@ export function normalize_session_metadata(
143
137
  ...(normalize_preview_text(raw.preview_text)
144
138
  ? { preview_text: normalize_preview_text(raw.preview_text) }
145
139
  : {}),
146
- ...(normalize_history_bytes(raw.historyBytes) !== undefined
147
- ? { historyBytes: normalize_history_bytes(raw.historyBytes) }
148
- : {}),
149
140
  };
150
141
  }
@@ -29,6 +29,8 @@ export interface CreateRuntimeSessionPortParams {
29
29
  * 读取当前 session 底层执行端口。
30
30
  */
31
31
  get_executor: SessionPort["get_executor"];
32
+ /** 读取全部 canonical Session Message。 */
33
+ messages: SessionPort["messages"];
32
34
  /**
33
35
  * 追加一条新的 session prompt。
34
36
  */
@@ -48,15 +50,13 @@ export interface CreateRuntimeSessionPortParams {
48
50
  */
49
51
  append_user_message: SessionPort["append_user_message"];
50
52
  /**
51
- * 追加 assistant 消息到底层历史。
53
+ * 追加 Agent 消息到底层历史。
52
54
  */
53
- append_assistant_message: SessionPort["append_assistant_message"];
55
+ append_agent_message: SessionPort["append_agent_message"];
54
56
  /**
55
57
  * 返回当前 session 是否正在执行。
56
58
  */
57
59
  is_executing: () => boolean;
58
- /** 读取当前 Session 的只读上下文快照。 */
59
- context: SessionPort["context"];
60
60
  /**
61
61
  * 在执行前确保当前 session 已完成初始化与宿主级配置。
62
62
  */
@@ -73,7 +73,7 @@ export function create_runtime_session_port(
73
73
  session_id: params.session_id,
74
74
  get_model: () => params.get_model(),
75
75
  get_executor: () => params.get_executor(),
76
- context: async () => await params.context(),
76
+ messages: async () => await params.messages(),
77
77
  prompt: async (input) => {
78
78
  await params.ensure_ready_for_execution();
79
79
  return await params.prompt(input);
@@ -88,8 +88,8 @@ export function create_runtime_session_port(
88
88
  append_user_message: async (messageParams) => {
89
89
  await params.append_user_message(messageParams);
90
90
  },
91
- append_assistant_message: async (messageParams) => {
92
- await params.append_assistant_message(messageParams);
91
+ append_agent_message: async (message_params) => {
92
+ await params.append_agent_message(message_params);
93
93
  },
94
94
  is_executing: () => params.is_executing(),
95
95
  };