@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
@@ -1,71 +1,57 @@
1
1
  /**
2
- * SDK Session 浏览辅助。
2
+ * Session 浏览投影。
3
3
  *
4
- * 关键点(中文)
5
- * - 统一负责 session 列表摘要、session 详情与 history 分页的投影逻辑。
6
- * - session title 允许为空;浏览层不会再从首条 user message 推导 fallback title。
7
- * - 面向 SDK / RemoteAgent / downcity gateway route 复用,避免在多个入口重复拼列表与分页语义。
8
- * - 这里不持有运行态状态;执行状态等动态信息通过调用参数显式注入。
4
+ * 本模块只从每个 Session 的 `session.db` 读取列表所需的轻量状态;Message 正文仍由
5
+ * SessionStorage 聚合读取。浏览逻辑不创建 schema,也不尝试迁移旧文件格式。
9
6
  */
10
7
  import type { AgentListSessionsInput, AgentSessionInfo, AgentSessionSummaryPage } from "../../types/agent/SessionTypes.js";
11
8
  import type { SessionHistoryMeta } from "../../executor/types/SessionHistoryMeta.js";
12
- import type { SessionMessage } from "@downcity/type";
13
- import type { FileSystem } from "@downcity/type";
14
- type SessionBrowseBaseInput = {
15
- /**
16
- * 当前项目根目录。
17
- */
9
+ import { type FileSystem, type SessionMessage } from "@downcity/type";
10
+ /** Session 详情投影输入。 */
11
+ interface SessionBrowseBaseInput {
12
+ /** 当前项目或私有存储根目录。 */
18
13
  project_root: string;
19
- /**
20
- * 当前 agent_id。
21
- */
14
+ /** 当前 Agent 标识。 */
22
15
  agent_id: string;
23
- /**
24
- * 当前 session_id。
25
- */
16
+ /** 当前 Session 标识。 */
26
17
  session_id: string;
27
- /**
28
- * 当前 session 已读取到的 metadata。
29
- */
18
+ /** 已读取的 Session 状态。 */
30
19
  metadata: SessionHistoryMeta;
31
- /**
32
- * 当前 session 已读取到的完整消息。
33
- *
34
- * 说明(中文):列表查询已有 metadata 摘要时可省略,详情查询仍传完整记录。
35
- */
20
+ /** 可选完整 Message,用于即时生成详情预览。 */
36
21
  messages?: SessionMessage[];
37
- /**
38
- * 当前 session 是否正在执行。
39
- */
22
+ /** 当前 Session 是否正在执行。 */
40
23
  executing?: boolean;
41
- };
42
- /**
43
- * 读取指定 JSONL 消息文件。
44
- */
45
- export declare function load_session_messages_from_path(filePath: string, files: FileSystem): Promise<SessionMessage[]>;
46
- /**
47
- * 基于 metadata + messages 构建 SDK session 详情。
48
- */
24
+ }
25
+ /** 基于 metadata 与可选 Message 构建 SDK Session 详情。 */
49
26
  export declare function build_session_info(input: SessionBrowseBaseInput): AgentSessionInfo;
50
- /**
51
- * 列出指定 agent session 摘要页。
52
- */
53
- export declare function list_agent_session_summary_page(params: {
27
+ /** 只读打开一个现有数据库并读取 Session 状态;失败时不改写磁盘。 */
28
+ export declare function read_session_metadata_from_database(database_path: string, fallback_origin_type: string): SessionHistoryMeta | null;
29
+ /** 列出活动 Session 摘要页。 */
30
+ export declare function list_agent_session_summary_page(input: {
31
+ /** Agent 私有存储根目录。 */
54
32
  project_root: string;
33
+ /** 当前 Agent 标识。 */
55
34
  agent_id: string;
35
+ /** 可选 Workspace 过滤。 */
56
36
  workspace_id?: string;
37
+ /** 列表查询。 */
57
38
  input?: AgentListSessionsInput;
58
- executingSessionIds?: Set<string>;
39
+ /** 当前执行中的 Session。 */
40
+ executing_session_ids?: ReadonlySet<string>;
41
+ /** Agent 私有文件能力。 */
59
42
  files: FileSystem;
60
43
  }): Promise<AgentSessionSummaryPage>;
61
- /**
62
- * 列出指定 agent 的已归档 session 摘要页。
63
- */
64
- export declare function list_archived_agent_session_summary_page(params: {
44
+ /** 列出归档 Session 摘要页。 */
45
+ export declare function list_archived_agent_session_summary_page(input: {
46
+ /** Agent 私有存储根目录。 */
65
47
  project_root: string;
48
+ /** 当前 Agent 标识。 */
66
49
  agent_id: string;
50
+ /** 可选 Workspace 过滤。 */
67
51
  workspace_id?: string;
52
+ /** 列表查询。 */
68
53
  input?: AgentListSessionsInput;
54
+ /** Agent 私有文件能力。 */
69
55
  files: FileSystem;
70
56
  }): Promise<AgentSessionSummaryPage>;
71
57
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"Browse.d.ts","sourceRoot":"","sources":["../../../src/session/browse/Browse.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EACV,sBAAsB,EACtB,gBAAgB,EAEhB,uBAAuB,EACxB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAYjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,KAAK,sBAAsB,GAAG;IAC5B;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,QAAQ,EAAE,kBAAkB,CAAC;IAE7B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAC;IAE5B;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAyCF;;GAEG;AACH,wBAAsB,+BAA+B,CACnD,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,cAAc,EAAE,CAAC,CAmC3B;AA6BD;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,sBAAsB,GAC5B,gBAAgB,CAsClB;AA8HD;;GAEG;AACH,wBAAsB,+BAA+B,CAAC,MAAM,EAAE;IAC5D,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,sBAAsB,CAAC;IAC/B,mBAAmB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAClC,KAAK,EAAE,UAAU,CAAC;CACnB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA+FnC;AAED;;GAEG;AACH,wBAAsB,wCAAwC,CAAC,MAAM,EAAE;IACrE,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,sBAAsB,CAAC;IAC/B,KAAK,EAAE,UAAU,CAAC;CACnB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA+FnC"}
1
+ {"version":3,"file":"Browse.d.ts","sourceRoot":"","sources":["../../../src/session/browse/Browse.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EACV,sBAAsB,EACtB,gBAAgB,EAEhB,uBAAuB,EACxB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAQjF,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AA8BxB,sBAAsB;AACtB,UAAU,sBAAsB;IAC9B,oBAAoB;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,mBAAmB;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,qBAAqB;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,uBAAuB;IACvB,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAC;IAC5B,yBAAyB;IACzB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAoCD,iDAAiD;AACjD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,sBAAsB,GAAG,gBAAgB,CAuBlF;AAED,0CAA0C;AAC1C,wBAAgB,mCAAmC,CACjD,aAAa,EAAE,MAAM,EACrB,oBAAoB,EAAE,MAAM,GAC3B,kBAAkB,GAAG,IAAI,CA+B3B;AAED,wBAAwB;AACxB,wBAAsB,+BAA+B,CAAC,KAAK,EAAE;IAC3D,qBAAqB;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,mBAAmB;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,uBAAuB;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY;IACZ,KAAK,CAAC,EAAE,sBAAsB,CAAC;IAC/B,sBAAsB;IACtB,qBAAqB,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC5C,oBAAoB;IACpB,KAAK,EAAE,UAAU,CAAC;CACnB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAEnC;AAED,wBAAwB;AACxB,wBAAsB,wCAAwC,CAAC,KAAK,EAAE;IACpE,qBAAqB;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,mBAAmB;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,uBAAuB;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY;IACZ,KAAK,CAAC,EAAE,sBAAsB,CAAC;IAC/B,oBAAoB;IACpB,KAAK,EAAE,UAAU,CAAC;CACnB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAEnC"}
@@ -1,18 +1,15 @@
1
1
  /**
2
- * SDK Session 浏览辅助。
2
+ * Session 浏览投影。
3
3
  *
4
- * 关键点(中文)
5
- * - 统一负责 session 列表摘要、session 详情与 history 分页的投影逻辑。
6
- * - session title 允许为空;浏览层不会再从首条 user message 推导 fallback title。
7
- * - 面向 SDK / RemoteAgent / downcity gateway route 复用,避免在多个入口重复拼列表与分页语义。
8
- * - 这里不持有运行态状态;执行状态等动态信息通过调用参数显式注入。
4
+ * 本模块只从每个 Session 的 `session.db` 读取列表所需的轻量状态;Message 正文仍由
5
+ * SessionStorage 聚合读取。浏览逻辑不创建 schema,也不尝试迁移旧文件格式。
9
6
  */
10
- import path from "node:path";
7
+ import { DatabaseSync } from "node:sqlite";
11
8
  import { resolve_session_message_preview } from "../../session/preview/SessionMessagePreview.js";
12
- import { get_agent_archived_session_active_messages_path, get_agent_archived_session_meta_path, get_agent_archived_sessions_path, get_agent_session_active_messages_path, get_agent_session_meta_path, get_agent_sessions_path, } from "../../session/storage/LocalStorePaths.js";
13
- import { read_session_metadata_from_path } from "../../session/storage/Metadata.js";
14
- import { normalize_session_origin_type } from "@downcity/type";
15
- function decodeMaybe(input) {
9
+ import { get_agent_archived_session_database_path, get_agent_archived_sessions_path, get_agent_session_database_path, get_agent_sessions_path, } from "../../session/storage/LocalStorePaths.js";
10
+ import { normalize_session_origin_type, restore_session_origin, } from "@downcity/type";
11
+ /** 从目录名恢复 Session 标识。 */
12
+ function decode_session_id(input) {
16
13
  try {
17
14
  return decodeURIComponent(input);
18
15
  }
@@ -20,402 +17,149 @@ function decodeMaybe(input) {
20
17
  return input;
21
18
  }
22
19
  }
23
- function normalizeLimit(input, fallback, max) {
20
+ /** 规范化分页上限。 */
21
+ function normalize_limit(input, fallback, max) {
24
22
  const value = typeof input === "number" && Number.isFinite(input)
25
23
  ? input
26
24
  : typeof input === "string" && input.trim()
27
25
  ? Number(input)
28
26
  : NaN;
29
- if (!Number.isFinite(value))
30
- return fallback;
31
- return Math.max(1, Math.min(max, Math.floor(value)));
27
+ return Number.isFinite(value)
28
+ ? Math.max(1, Math.min(max, Math.floor(value)))
29
+ : fallback;
32
30
  }
33
- function truncateText(input, maxChars) {
31
+ /** 规范化透明 offset cursor。 */
32
+ function normalize_cursor(input) {
33
+ const value = Number(String(input || "").trim());
34
+ return Number.isFinite(value) && value >= 0 ? Math.floor(value) : 0;
35
+ }
36
+ /** 裁剪用户可见预览。 */
37
+ function truncate_text(input, max_chars) {
34
38
  const value = String(input || "").trim();
35
- if (!value)
36
- return "";
37
- if (value.length <= maxChars)
39
+ if (value.length <= max_chars)
38
40
  return value;
39
- return `${value.slice(0, Math.max(0, maxChars - 1)).trimEnd()}…`;
40
- }
41
- function normalizeCursor(input) {
42
- const raw = String(input || "").trim();
43
- if (!raw)
44
- return 0;
45
- const value = Number(raw);
46
- if (!Number.isFinite(value) || value < 0)
47
- return 0;
48
- return Math.floor(value);
49
- }
50
- function encodeCursor(offset) {
51
- if (!Number.isFinite(offset) || offset <= 0)
52
- return undefined;
53
- return String(Math.floor(offset));
54
- }
55
- /**
56
- * 读取指定 JSONL 消息文件。
57
- */
58
- export async function load_session_messages_from_path(filePath, files) {
59
- const messages_by_id = new Map();
60
- if (await files.path_exists(filePath)) {
61
- const raw = (await files.read_file(filePath)).toString("utf8");
62
- const lines = raw.split("\n").filter(Boolean);
63
- for (const line of lines) {
64
- try {
65
- const message = JSON.parse(line);
66
- if (!is_canonical_session_message(message))
67
- continue;
68
- const previous = messages_by_id.get(message.message_id);
69
- if (!previous || message.revision > previous.revision) {
70
- messages_by_id.set(message.message_id, message);
71
- }
72
- }
73
- catch {
74
- // 关键点(中文):单行损坏不影响整个 session 的可读性。
75
- }
76
- }
77
- }
78
- const inflight_path = path.join(path.dirname(filePath), "assistant_message.json");
79
- if (await files.path_exists(inflight_path)) {
80
- try {
81
- const message = JSON.parse((await files.read_file(inflight_path)).toString("utf8"));
82
- if (is_canonical_session_message(message) && message.type === "assistant") {
83
- messages_by_id.set(message.message_id, message);
84
- }
85
- }
86
- catch {
87
- // 运行中快照损坏时仍返回已经完成的历史。
88
- }
89
- }
90
- return [...messages_by_id.values()]
91
- .sort((left, right) => left.sequence - right.sequence);
92
- }
93
- function is_canonical_session_message(input) {
94
- if (!input || typeof input !== "object")
95
- return false;
96
- const candidate = input;
97
- return (typeof candidate.message_id === "string" &&
98
- typeof candidate.session_id === "string" &&
99
- typeof candidate.sequence === "number" &&
100
- typeof candidate.revision === "number" &&
101
- (candidate.type === "user" ||
102
- candidate.type === "assistant" ||
103
- candidate.type === "action" ||
104
- candidate.type === "error"));
41
+ return `${value.slice(0, Math.max(0, max_chars - 1)).trimEnd()}…`;
105
42
  }
106
- function is_compact_summary_message(message) {
107
- return message.type === "assistant" && message.kind === "summary";
108
- }
109
- function filter_user_visible_history_messages(messages) {
110
- return messages.filter((message) => message.visibility === "visible" && !is_compact_summary_message(message));
111
- }
112
- /**
113
- * 基于 metadata + messages 构建 SDK session 详情。
114
- */
43
+ /** 基于 metadata 与可选 Message 构建 SDK Session 详情。 */
115
44
  export function build_session_info(input) {
116
- const messages = input.messages;
117
- const preview_text = messages && messages.length > 0
118
- ? truncateText(resolve_session_message_preview(messages[messages.length - 1]), 180)
45
+ const latest_message = input.messages?.at(-1);
46
+ const preview_text = latest_message
47
+ ? truncate_text(resolve_session_message_preview(latest_message), 180)
119
48
  : input.metadata.preview_text;
120
- const message_count = typeof input.metadata.message_count === "number"
121
- ? input.metadata.message_count
122
- : messages
123
- ? filter_user_visible_history_messages(messages).length
124
- : 0;
125
- const title = typeof input.metadata.title === "string" && input.metadata.title.trim()
126
- ? input.metadata.title.trim()
127
- : undefined;
128
49
  return {
129
50
  agent_id: input.agent_id,
130
51
  session_id: input.session_id,
131
- ...(title ? { title } : {}),
52
+ ...(input.metadata.title?.trim() ? { title: input.metadata.title.trim() } : {}),
132
53
  ...(preview_text ? { preview_text } : {}),
133
- message_count: message_count,
54
+ message_count: input.metadata.message_count ?? input.messages?.length ?? 0,
134
55
  ...(typeof input.metadata.created_at === "number"
135
56
  ? { created_at: input.metadata.created_at }
136
57
  : {}),
137
58
  ...(typeof input.metadata.updated_at === "number"
138
59
  ? { updated_at: input.metadata.updated_at }
139
60
  : {}),
140
- ...(input.metadata.model_label
141
- ? { model_label: input.metadata.model_label }
142
- : {}),
61
+ ...(input.metadata.model_label ? { model_label: input.metadata.model_label } : {}),
143
62
  origin: input.metadata.origin,
144
- ...(typeof input.metadata.timezone === "string" && input.metadata.timezone.trim()
145
- ? { timezone: input.metadata.timezone.trim() }
146
- : {}),
63
+ ...(input.metadata.workspace_id ? { workspace_id: input.metadata.workspace_id } : {}),
64
+ ...(input.metadata.timezone ? { timezone: input.metadata.timezone } : {}),
147
65
  ...(input.executing ? { executing: true } : {}),
148
66
  };
149
67
  }
150
- /**
151
- * 读取列表所需的轻量 session 摘要。
152
- *
153
- * 关键点(中文)
154
- * - 新记录直接使用 metadata 摘要,不扫描 active.jsonl。
155
- * - 旧记录或执行中记录回退读取一次完整历史,并把摘要补写回 metadata。
156
- */
157
- async function resolve_session_summary_metadata(input) {
158
- const storage_stats = await resolve_session_disk_stats(input.messagesPath, input.files);
159
- const history_bytes = storage_stats.history_bytes;
160
- const inflight_path = path.join(path.dirname(input.messagesPath), "assistant_message.json");
161
- const has_inflight = await input.files.path_exists(inflight_path);
162
- if (!input.refresh &&
163
- !has_inflight &&
164
- typeof input.metadata.message_count === "number" &&
165
- input.metadata.historyBytes === history_bytes) {
166
- return input.metadata;
167
- }
168
- const messages = await load_session_messages_from_path(input.messagesPath, input.files);
169
- const last_message = messages[messages.length - 1];
170
- const preview_text = last_message
171
- ? truncateText(resolve_session_message_preview(last_message), 180)
172
- : "";
173
- const { preview_text: _previous_preview, ...metadata_without_preview } = input.metadata;
174
- void _previous_preview;
175
- const next_metadata = {
176
- ...metadata_without_preview,
177
- message_count: storage_stats.message_count,
178
- historyBytes: history_bytes,
179
- ...(preview_text || input.metadata.preview_text
180
- ? { preview_text: preview_text || input.metadata.preview_text }
181
- : {}),
182
- };
183
- await input.files.write_file_atomically(input.metaPath, `${JSON.stringify(next_metadata, null, 2)}\n`);
184
- return next_metadata;
185
- }
186
- /**
187
- * 只读取 Active 和 Segment 文件索引,计算列表所需的持久化统计。
188
- *
189
- * 关键点(中文)
190
- * - Segment 的结束 sequence 直接来自文件名,不解析历史正文。
191
- * - Active 需要逐行读取,以同时覆盖 revision 行与运行中 Assistant sequence。
192
- */
193
- async function resolve_session_disk_stats(messages_path, files) {
194
- const messages_dir_path = path.dirname(messages_path);
195
- const segments_dir_path = path.join(messages_dir_path, "segments");
196
- const segment_entries = await files.read_directory(segments_dir_path)
197
- .catch(() => []);
198
- const segment_files = segment_entries.flatMap((entry) => {
199
- if (!entry.is_file)
200
- return [];
201
- const match = /^(\d+)-(\d+)\.jsonl$/.exec(entry.name);
202
- if (!match)
203
- return [];
204
- return [{
205
- file_path: path.join(segments_dir_path, entry.name),
206
- end_sequence: Number(match[2]),
207
- }];
208
- });
209
- const active_raw = await files.read_file(messages_path)
210
- .then((value) => value.toString("utf8"))
211
- .catch(() => "");
212
- let latest_active_sequence = 0;
213
- for (const line of active_raw.split("\n")) {
214
- if (!line.trim())
215
- continue;
216
- try {
217
- const message = JSON.parse(line);
218
- if (Number.isInteger(message.sequence)) {
219
- latest_active_sequence = Math.max(latest_active_sequence, Number(message.sequence));
220
- }
221
- }
222
- catch {
223
- // 单行损坏不阻断 Session 列表,其正文读取时会按既有规则忽略。
224
- }
225
- }
226
- const inflight_path = path.join(messages_dir_path, "assistant_message.json");
68
+ /** 只读打开一个现有数据库并读取 Session 状态;失败时不改写磁盘。 */
69
+ export function read_session_metadata_from_database(database_path, fallback_origin_type) {
70
+ let database = null;
227
71
  try {
228
- const inflight = JSON.parse((await files.read_file(inflight_path)).toString("utf8"));
229
- if (Number.isInteger(inflight.sequence)) {
230
- latest_active_sequence = Math.max(latest_active_sequence, Number(inflight.sequence));
231
- }
232
- }
233
- catch {
234
- // 草稿不存在或损坏时只统计已完成历史。
235
- }
236
- const segment_sizes = await Promise.all(segment_files.map(({ file_path }) => files.file_size(file_path)
237
- .catch(() => 0)));
238
- const latest_segment_sequence = segment_files.reduce((latest, segment) => Math.max(latest, segment.end_sequence), 0);
239
- return {
240
- history_bytes: Buffer.byteLength(active_raw, "utf8") +
241
- segment_sizes.reduce((total, size) => total + size, 0),
242
- message_count: Math.max(latest_segment_sequence, latest_active_sequence),
243
- };
244
- }
245
- /**
246
- * 列出指定 agent 的 session 摘要页。
247
- */
248
- export async function list_agent_session_summary_page(params) {
249
- const limit = normalizeLimit(params.input?.limit, 50, 500);
250
- const cursor = normalizeCursor(params.input?.cursor);
251
- const query = String(params.input?.query || "").trim().toLowerCase();
252
- const origin_type = normalize_session_origin_type(params.input?.origin_type ?? "chat");
253
- const sessionsRoot = get_agent_sessions_path(params.project_root, origin_type);
254
- if (!(await params.files.path_exists(sessionsRoot))) {
72
+ database = new DatabaseSync(database_path, { readOnly: true });
73
+ const row = database.prepare(`
74
+ SELECT session_id, agent_id, workspace_id, origin, timezone, title,
75
+ model_label, approval_mode, message_count, preview_text,
76
+ created_at, updated_at
77
+ FROM session_state WHERE singleton_id = 1
78
+ `).get();
79
+ if (!row)
80
+ return null;
255
81
  return {
256
- items: [],
257
- total: 0,
258
- has_more: false,
82
+ v: 2,
83
+ session_id: row.session_id,
84
+ agent_id: row.agent_id,
85
+ ...(row.workspace_id ? { workspace_id: row.workspace_id } : {}),
86
+ origin: restore_session_origin(JSON.parse(row.origin), fallback_origin_type),
87
+ timezone: row.timezone,
88
+ ...(row.title ? { title: row.title } : {}),
89
+ ...(row.model_label ? { model_label: row.model_label } : {}),
90
+ ...(row.approval_mode ? { approval_mode: row.approval_mode } : {}),
91
+ message_count: row.message_count,
92
+ ...(row.preview_text ? { preview_text: row.preview_text } : {}),
93
+ created_at: row.created_at,
94
+ updated_at: row.updated_at,
259
95
  };
260
96
  }
261
- const entries = await params.files.read_directory(sessionsRoot);
262
- const summaries = [];
263
- for (const entry of entries) {
264
- if (!entry.is_directory)
265
- continue;
266
- const session_id = decodeMaybe(entry.name);
267
- if (!session_id)
268
- continue;
269
- const meta_path = get_agent_session_meta_path(params.project_root, origin_type, session_id);
270
- const messages_path = get_agent_session_active_messages_path(params.project_root, origin_type, session_id);
271
- const persisted_metadata = await read_session_metadata_from_path({
272
- filePath: meta_path,
273
- session_id,
274
- agent_id: params.agent_id,
275
- workspace_id: params.workspace_id,
276
- origin_type,
277
- files: params.files,
278
- }).catch(() => null);
279
- if (!persisted_metadata)
280
- continue;
281
- const metadata = await resolve_session_summary_metadata({
282
- metadata: persisted_metadata,
283
- messagesPath: messages_path,
284
- metaPath: meta_path,
285
- refresh: params.executingSessionIds?.has(session_id) === true,
286
- files: params.files,
287
- });
288
- const info = build_session_info({
289
- project_root: params.project_root,
290
- agent_id: params.agent_id,
291
- session_id,
292
- metadata,
293
- executing: params.executingSessionIds?.has(session_id),
294
- });
295
- const summary = {
296
- agent_id: info.agent_id,
297
- session_id: info.session_id,
298
- ...(info.title ? { title: info.title } : {}),
299
- ...(info.preview_text ? { preview_text: info.preview_text } : {}),
300
- message_count: info.message_count,
301
- ...(typeof info.created_at === "number" ? { created_at: info.created_at } : {}),
302
- ...(typeof info.updated_at === "number" ? { updated_at: info.updated_at } : {}),
303
- ...(info.model_label ? { model_label: info.model_label } : {}),
304
- origin: info.origin,
305
- ...(persisted_metadata.workspace_id ? { workspace_id: persisted_metadata.workspace_id } : {}),
306
- ...(info.executing ? { executing: true } : {}),
307
- };
308
- if (query) {
309
- const haystack = [
310
- summary.session_id,
311
- summary.title || "",
312
- summary.preview_text || "",
313
- ]
314
- .join("\n")
315
- .toLowerCase();
316
- if (!haystack.includes(query))
317
- continue;
318
- }
319
- summaries.push(summary);
97
+ catch {
98
+ return null;
99
+ }
100
+ finally {
101
+ database?.close();
320
102
  }
321
- summaries.sort((left, right) => (right.updated_at || 0) - (left.updated_at || 0));
322
- const items = summaries.slice(cursor, cursor + limit);
323
- const nextOffset = cursor + items.length;
324
- return {
325
- items,
326
- total: summaries.length,
327
- ...(nextOffset < summaries.length
328
- ? { next_cursor: encodeCursor(nextOffset) }
329
- : {}),
330
- has_more: nextOffset < summaries.length,
331
- };
332
103
  }
333
- /**
334
- * 列出指定 agent 的已归档 session 摘要页。
335
- */
336
- export async function list_archived_agent_session_summary_page(params) {
337
- const limit = normalizeLimit(params.input?.limit, 50, 500);
338
- const cursor = normalizeCursor(params.input?.cursor);
339
- const query = String(params.input?.query || "").trim().toLowerCase();
340
- const origin_type = normalize_session_origin_type(params.input?.origin_type ?? "chat");
341
- const archivedRoot = get_agent_archived_sessions_path(params.project_root, origin_type);
342
- if (!(await params.files.path_exists(archivedRoot))) {
343
- return {
344
- items: [],
345
- total: 0,
346
- has_more: false,
347
- };
104
+ /** 列出活动 Session 摘要页。 */
105
+ export async function list_agent_session_summary_page(input) {
106
+ return await list_session_summary_page({ ...input, archived: false });
107
+ }
108
+ /** 列出归档 Session 摘要页。 */
109
+ export async function list_archived_agent_session_summary_page(input) {
110
+ return await list_session_summary_page({ ...input, archived: true });
111
+ }
112
+ /** 扫描单个来源分区中的 Session 数据库并建立轻量列表。 */
113
+ async function list_session_summary_page(input) {
114
+ const origin_type = normalize_session_origin_type(input.input?.origin_type ?? "chat");
115
+ const sessions_path = input.archived
116
+ ? get_agent_archived_sessions_path(input.project_root, origin_type)
117
+ : get_agent_sessions_path(input.project_root, origin_type);
118
+ if (!(await input.files.path_exists(sessions_path))) {
119
+ return { items: [], total: 0, has_more: false };
348
120
  }
349
- const entries = await params.files.read_directory(archivedRoot);
121
+ const query = String(input.input?.query || "").trim().toLowerCase();
350
122
  const summaries = [];
351
- for (const entry of entries) {
123
+ for (const entry of await input.files.read_directory(sessions_path)) {
352
124
  if (!entry.is_directory)
353
125
  continue;
354
- const session_id = decodeMaybe(entry.name);
355
- if (!session_id)
126
+ const session_id = decode_session_id(entry.name);
127
+ const database_path = input.archived
128
+ ? get_agent_archived_session_database_path(input.project_root, origin_type, session_id)
129
+ : get_agent_session_database_path(input.project_root, origin_type, session_id);
130
+ if (!(await input.files.path_exists(database_path)))
356
131
  continue;
357
- const meta_path = get_agent_archived_session_meta_path(params.project_root, origin_type, session_id);
358
- const messages_path = get_agent_archived_session_active_messages_path(params.project_root, origin_type, session_id);
359
- const persisted_metadata = await read_session_metadata_from_path({
360
- filePath: meta_path,
361
- session_id,
362
- agent_id: params.agent_id,
363
- workspace_id: params.workspace_id,
364
- origin_type,
365
- files: params.files,
366
- }).catch(() => null);
367
- if (!persisted_metadata)
132
+ const metadata = read_session_metadata_from_database(database_path, origin_type);
133
+ if (!metadata ||
134
+ metadata.agent_id !== input.agent_id ||
135
+ metadata.origin.type !== origin_type ||
136
+ (input.workspace_id && metadata.workspace_id !== input.workspace_id))
368
137
  continue;
369
- const metadata = await resolve_session_summary_metadata({
370
- metadata: persisted_metadata,
371
- messagesPath: messages_path,
372
- metaPath: meta_path,
373
- refresh: false,
374
- files: params.files,
375
- });
376
- // 关键点(中文):归档 session 不再生成新 title,仅读取归档目录内已有 meta。
377
- const info = build_session_info({
378
- project_root: params.project_root,
379
- agent_id: params.agent_id,
138
+ const summary = build_session_info({
139
+ project_root: input.project_root,
140
+ agent_id: input.agent_id,
380
141
  session_id,
381
142
  metadata,
382
- executing: false,
143
+ executing: !input.archived && input.executing_session_ids?.has(session_id) === true,
383
144
  });
384
- const summary = {
385
- agent_id: info.agent_id,
386
- session_id: info.session_id,
387
- ...(info.title ? { title: info.title } : {}),
388
- ...(info.preview_text ? { preview_text: info.preview_text } : {}),
389
- message_count: info.message_count,
390
- ...(typeof info.created_at === "number" ? { created_at: info.created_at } : {}),
391
- ...(typeof info.updated_at === "number" ? { updated_at: info.updated_at } : {}),
392
- ...(info.model_label ? { model_label: info.model_label } : {}),
393
- origin: info.origin,
394
- ...(persisted_metadata.workspace_id ? { workspace_id: persisted_metadata.workspace_id } : {}),
395
- };
396
- if (query) {
397
- const haystack = [
398
- summary.session_id,
399
- summary.title || "",
400
- summary.preview_text || "",
401
- ]
402
- .join("\n")
403
- .toLowerCase();
404
- if (!haystack.includes(query))
405
- continue;
406
- }
145
+ if (query && ![
146
+ summary.session_id,
147
+ summary.title || "",
148
+ summary.preview_text || "",
149
+ ].join("\n").toLowerCase().includes(query))
150
+ continue;
407
151
  summaries.push(summary);
408
152
  }
409
153
  summaries.sort((left, right) => (right.updated_at || 0) - (left.updated_at || 0));
154
+ const cursor = normalize_cursor(input.input?.cursor);
155
+ const limit = normalize_limit(input.input?.limit, 50, 500);
410
156
  const items = summaries.slice(cursor, cursor + limit);
411
- const nextOffset = cursor + items.length;
157
+ const next_cursor = cursor + items.length;
412
158
  return {
413
159
  items,
414
160
  total: summaries.length,
415
- ...(nextOffset < summaries.length
416
- ? { next_cursor: encodeCursor(nextOffset) }
417
- : {}),
418
- has_more: nextOffset < summaries.length,
161
+ ...(next_cursor < summaries.length ? { next_cursor: String(next_cursor) } : {}),
162
+ has_more: next_cursor < summaries.length,
419
163
  };
420
164
  }
421
165
  //# sourceMappingURL=Browse.js.map