dal-code 1.0.9 → 1.0.11

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 (458) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/core/keybindings.d.ts +1 -1
  3. package/dist/core/keybindings.d.ts.map +1 -1
  4. package/dist/core/keybindings.js +1 -1
  5. package/dist/core/keybindings.js.map +1 -1
  6. package/dist/core/model-config.d.ts +85 -0
  7. package/dist/core/model-config.d.ts.map +1 -1
  8. package/dist/core/model-config.js +19 -14
  9. package/dist/core/model-config.js.map +1 -1
  10. package/dist/core/session-manager.d.ts +7 -0
  11. package/dist/core/session-manager.d.ts.map +1 -1
  12. package/dist/core/session-manager.js +28 -0
  13. package/dist/core/session-manager.js.map +1 -1
  14. package/dist/extensions/llama/client.d.ts +2 -0
  15. package/dist/extensions/llama/client.d.ts.map +1 -1
  16. package/dist/extensions/llama/client.js +7 -3
  17. package/dist/extensions/llama/client.js.map +1 -1
  18. package/dist/extensions/llama/provider.d.ts.map +1 -1
  19. package/dist/extensions/llama/provider.js +20 -5
  20. package/dist/extensions/llama/provider.js.map +1 -1
  21. package/dist/main.d.ts.map +1 -1
  22. package/dist/main.js +5 -6
  23. package/dist/main.js.map +1 -1
  24. package/dist/modes/interactive/interactive-mode.d.ts +2 -1
  25. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  26. package/dist/modes/interactive/interactive-mode.js +31 -21
  27. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  28. package/dist/node_modules/@dalcode/dal-bridge/package.json +3 -3
  29. package/dist/node_modules/@dalcode/dal-gateway/package.json +3 -3
  30. package/dist/node_modules/@dalcode/dal-guard/package.json +5 -5
  31. package/dist/node_modules/@dalcode/dal-halo/package.json +2 -2
  32. package/dist/node_modules/@dalcode/dal-lang/package.json +4 -4
  33. package/dist/node_modules/@dalcode/dal-lsp/package.json +4 -4
  34. package/dist/node_modules/@dalcode/dal-mcp/package.json +6 -6
  35. package/dist/node_modules/@dalcode/dal-office/package.json +3 -3
  36. package/dist/node_modules/@dalcode/dal-pi-kit/package.json +1 -1
  37. package/dist/node_modules/@dalcode/dal-skills/package.json +4 -4
  38. package/dist/node_modules/@dalcode/dal-subagent/package.json +6 -6
  39. package/dist/node_modules/@dalcode/dal-telemetry/package.json +3 -3
  40. package/dist/node_modules/@dalcode/deepailab/package.json +4 -4
  41. package/dist/node_modules/@earendil-works/chord/LICENSE +21 -0
  42. package/dist/node_modules/@earendil-works/chord/README.md +205 -0
  43. package/dist/node_modules/@earendil-works/chord/dist/api.d.ts +15 -0
  44. package/dist/node_modules/@earendil-works/chord/dist/api.d.ts.map +1 -0
  45. package/dist/node_modules/@earendil-works/chord/dist/api.js +72 -0
  46. package/dist/node_modules/@earendil-works/chord/dist/api.js.map +1 -0
  47. package/dist/node_modules/@earendil-works/chord/dist/bundler.d.ts +6 -0
  48. package/dist/node_modules/@earendil-works/chord/dist/bundler.d.ts.map +1 -0
  49. package/dist/node_modules/@earendil-works/chord/dist/bundler.js +3 -0
  50. package/dist/node_modules/@earendil-works/chord/dist/bundler.js.map +1 -0
  51. package/dist/node_modules/@earendil-works/chord/dist/context/index.d.ts +24 -0
  52. package/dist/node_modules/@earendil-works/chord/dist/context/index.d.ts.map +1 -0
  53. package/dist/node_modules/@earendil-works/chord/dist/context/index.js +97 -0
  54. package/dist/node_modules/@earendil-works/chord/dist/context/index.js.map +1 -0
  55. package/dist/node_modules/@earendil-works/chord/dist/delta/index.d.ts +119 -0
  56. package/dist/node_modules/@earendil-works/chord/dist/delta/index.d.ts.map +1 -0
  57. package/dist/node_modules/@earendil-works/chord/dist/delta/index.js +1906 -0
  58. package/dist/node_modules/@earendil-works/chord/dist/delta/index.js.map +1 -0
  59. package/dist/node_modules/@earendil-works/chord/dist/facets/host.d.ts +12 -0
  60. package/dist/node_modules/@earendil-works/chord/dist/facets/host.d.ts.map +1 -0
  61. package/dist/node_modules/@earendil-works/chord/dist/facets/host.js +763 -0
  62. package/dist/node_modules/@earendil-works/chord/dist/facets/host.js.map +1 -0
  63. package/dist/node_modules/@earendil-works/chord/dist/facets/loader.d.ts +3 -0
  64. package/dist/node_modules/@earendil-works/chord/dist/facets/loader.d.ts.map +1 -0
  65. package/dist/node_modules/@earendil-works/chord/dist/facets/loader.js +5 -0
  66. package/dist/node_modules/@earendil-works/chord/dist/facets/loader.js.map +1 -0
  67. package/dist/node_modules/@earendil-works/chord/dist/index.d.ts +11 -0
  68. package/dist/node_modules/@earendil-works/chord/dist/index.d.ts.map +1 -0
  69. package/dist/node_modules/@earendil-works/chord/dist/index.js +10 -0
  70. package/dist/node_modules/@earendil-works/chord/dist/index.js.map +1 -0
  71. package/dist/node_modules/@earendil-works/chord/dist/json.d.ts +4 -0
  72. package/dist/node_modules/@earendil-works/chord/dist/json.d.ts.map +1 -0
  73. package/dist/node_modules/@earendil-works/chord/dist/json.js +57 -0
  74. package/dist/node_modules/@earendil-works/chord/dist/json.js.map +1 -0
  75. package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.d.ts +30 -0
  76. package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.d.ts.map +1 -0
  77. package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.js +377 -0
  78. package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.js.map +1 -0
  79. package/dist/node_modules/@earendil-works/chord/dist/node/bundle.d.ts +26 -0
  80. package/dist/node_modules/@earendil-works/chord/dist/node/bundle.d.ts.map +1 -0
  81. package/dist/node_modules/@earendil-works/chord/dist/node/bundle.js +183 -0
  82. package/dist/node_modules/@earendil-works/chord/dist/node/bundle.js.map +1 -0
  83. package/dist/node_modules/@earendil-works/chord/dist/node/manifest.d.ts +36 -0
  84. package/dist/node_modules/@earendil-works/chord/dist/node/manifest.d.ts.map +1 -0
  85. package/dist/node_modules/@earendil-works/chord/dist/node/manifest.js +6 -0
  86. package/dist/node_modules/@earendil-works/chord/dist/node/manifest.js.map +1 -0
  87. package/dist/node_modules/@earendil-works/chord/dist/node/package.d.ts +15 -0
  88. package/dist/node_modules/@earendil-works/chord/dist/node/package.d.ts.map +1 -0
  89. package/dist/node_modules/@earendil-works/chord/dist/node/package.js +199 -0
  90. package/dist/node_modules/@earendil-works/chord/dist/node/package.js.map +1 -0
  91. package/dist/node_modules/@earendil-works/chord/dist/node.d.ts +5 -0
  92. package/dist/node_modules/@earendil-works/chord/dist/node.d.ts.map +1 -0
  93. package/dist/node_modules/@earendil-works/chord/dist/node.js +3 -0
  94. package/dist/node_modules/@earendil-works/chord/dist/node.js.map +1 -0
  95. package/dist/node_modules/@earendil-works/chord/dist/services/consumer.d.ts +11 -0
  96. package/dist/node_modules/@earendil-works/chord/dist/services/consumer.d.ts.map +1 -0
  97. package/dist/node_modules/@earendil-works/chord/dist/services/consumer.js +537 -0
  98. package/dist/node_modules/@earendil-works/chord/dist/services/consumer.js.map +1 -0
  99. package/dist/node_modules/@earendil-works/chord/dist/services/errors.d.ts +8 -0
  100. package/dist/node_modules/@earendil-works/chord/dist/services/errors.d.ts.map +1 -0
  101. package/dist/node_modules/@earendil-works/chord/dist/services/errors.js +22 -0
  102. package/dist/node_modules/@earendil-works/chord/dist/services/errors.js.map +1 -0
  103. package/dist/node_modules/@earendil-works/chord/dist/services/handle.d.ts +15 -0
  104. package/dist/node_modules/@earendil-works/chord/dist/services/handle.d.ts.map +1 -0
  105. package/dist/node_modules/@earendil-works/chord/dist/services/handle.js +99 -0
  106. package/dist/node_modules/@earendil-works/chord/dist/services/handle.js.map +1 -0
  107. package/dist/node_modules/@earendil-works/chord/dist/services/instances.d.ts +25 -0
  108. package/dist/node_modules/@earendil-works/chord/dist/services/instances.d.ts.map +1 -0
  109. package/dist/node_modules/@earendil-works/chord/dist/services/instances.js +137 -0
  110. package/dist/node_modules/@earendil-works/chord/dist/services/instances.js.map +1 -0
  111. package/dist/node_modules/@earendil-works/chord/dist/services/loopback.d.ts +5 -0
  112. package/dist/node_modules/@earendil-works/chord/dist/services/loopback.d.ts.map +1 -0
  113. package/dist/node_modules/@earendil-works/chord/dist/services/loopback.js +15 -0
  114. package/dist/node_modules/@earendil-works/chord/dist/services/loopback.js.map +1 -0
  115. package/dist/node_modules/@earendil-works/chord/dist/services/provider.d.ts +37 -0
  116. package/dist/node_modules/@earendil-works/chord/dist/services/provider.d.ts.map +1 -0
  117. package/dist/node_modules/@earendil-works/chord/dist/services/provider.js +472 -0
  118. package/dist/node_modules/@earendil-works/chord/dist/services/provider.js.map +1 -0
  119. package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.d.ts +15 -0
  120. package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.d.ts.map +1 -0
  121. package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.js +115 -0
  122. package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.js.map +1 -0
  123. package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.d.ts +11 -0
  124. package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.d.ts.map +1 -0
  125. package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.js +10 -0
  126. package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.js.map +1 -0
  127. package/dist/node_modules/@earendil-works/chord/dist/services/state.d.ts +23 -0
  128. package/dist/node_modules/@earendil-works/chord/dist/services/state.d.ts.map +1 -0
  129. package/dist/node_modules/@earendil-works/chord/dist/services/state.js +124 -0
  130. package/dist/node_modules/@earendil-works/chord/dist/services/state.js.map +1 -0
  131. package/dist/node_modules/@earendil-works/chord/dist/services/wire.d.ts +60 -0
  132. package/dist/node_modules/@earendil-works/chord/dist/services/wire.d.ts.map +1 -0
  133. package/dist/node_modules/@earendil-works/chord/dist/services/wire.js +173 -0
  134. package/dist/node_modules/@earendil-works/chord/dist/services/wire.js.map +1 -0
  135. package/dist/node_modules/@earendil-works/chord/dist/types.d.ts +203 -0
  136. package/dist/node_modules/@earendil-works/chord/dist/types.d.ts.map +1 -0
  137. package/dist/node_modules/@earendil-works/chord/dist/types.js +2 -0
  138. package/dist/node_modules/@earendil-works/chord/dist/types.js.map +1 -0
  139. package/dist/node_modules/@earendil-works/chord/package.json +62 -0
  140. package/dist/node_modules/@earendil-works/chord/src/delta/README.md +404 -0
  141. package/dist/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
  142. package/dist/node_modules/@earendil-works/pi-ai/README.md +3 -1
  143. package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.d.ts.map +1 -1
  144. package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +15 -6
  145. package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js.map +1 -1
  146. package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  147. package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +1 -0
  148. package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js.map +1 -1
  149. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.d.ts.map +1 -1
  150. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js +9 -66
  151. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js.map +1 -1
  152. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.d.ts +12 -3
  153. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.d.ts.map +1 -1
  154. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.js +50 -3
  155. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.js.map +1 -1
  156. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.d.ts.map +1 -1
  157. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.js +13 -58
  158. package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.js.map +1 -1
  159. package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.d.ts.map +1 -1
  160. package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +5 -1
  161. package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js.map +1 -1
  162. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.d.ts.map +1 -1
  163. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js +6 -1
  164. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js.map +1 -1
  165. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.d.ts.map +1 -1
  166. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +1 -1
  167. package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js.map +1 -1
  168. package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts +30 -0
  169. package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts.map +1 -1
  170. package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +34 -4
  171. package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js.map +1 -1
  172. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
  173. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
  174. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/deepseek.json +1 -1
  175. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
  176. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openai-codex.json +1 -1
  177. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
  178. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
  179. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
  180. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-cn.json +1 -1
  181. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
  182. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
  183. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/zai-coding-cn.json +1 -1
  184. package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/zai.json +1 -1
  185. package/dist/node_modules/@earendil-works/pi-ai/dist/types.d.ts +4 -2
  186. package/dist/node_modules/@earendil-works/pi-ai/dist/types.d.ts.map +1 -1
  187. package/dist/node_modules/@earendil-works/pi-ai/dist/types.js.map +1 -1
  188. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.d.ts.map +1 -1
  189. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js +8 -3
  190. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js.map +1 -1
  191. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.d.ts.map +1 -1
  192. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +2 -0
  193. package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.js.map +1 -1
  194. package/dist/node_modules/@earendil-works/pi-ai/package.json +2 -2
  195. package/dist/node_modules/@earendil-works/pi-client/package.json +2 -2
  196. package/dist/node_modules/@earendil-works/pi-protocol/package.json +2 -2
  197. package/dist/node_modules/@earendil-works/pi-server/CHANGELOG.md +57 -0
  198. package/dist/node_modules/@earendil-works/pi-server/LICENSE +21 -0
  199. package/dist/node_modules/@earendil-works/pi-server/README.md +79 -0
  200. package/dist/node_modules/@earendil-works/pi-server/dist/cli.d.ts +3 -0
  201. package/dist/node_modules/@earendil-works/pi-server/dist/cli.d.ts.map +1 -0
  202. package/dist/node_modules/@earendil-works/pi-server/dist/cli.js +136 -0
  203. package/dist/node_modules/@earendil-works/pi-server/dist/cli.js.map +1 -0
  204. package/dist/node_modules/@earendil-works/pi-server/dist/config.d.ts +12 -0
  205. package/dist/node_modules/@earendil-works/pi-server/dist/config.d.ts.map +1 -0
  206. package/dist/node_modules/@earendil-works/pi-server/dist/config.js +55 -0
  207. package/dist/node_modules/@earendil-works/pi-server/dist/config.js.map +1 -0
  208. package/dist/node_modules/@earendil-works/pi-server/dist/connection.d.ts +35 -0
  209. package/dist/node_modules/@earendil-works/pi-server/dist/connection.d.ts.map +1 -0
  210. package/dist/node_modules/@earendil-works/pi-server/dist/connection.js +4 -0
  211. package/dist/node_modules/@earendil-works/pi-server/dist/connection.js.map +1 -0
  212. package/dist/node_modules/@earendil-works/pi-server/dist/errors.d.ts +25 -0
  213. package/dist/node_modules/@earendil-works/pi-server/dist/errors.d.ts.map +1 -0
  214. package/dist/node_modules/@earendil-works/pi-server/dist/errors.js +41 -0
  215. package/dist/node_modules/@earendil-works/pi-server/dist/errors.js.map +1 -0
  216. package/dist/node_modules/@earendil-works/pi-server/dist/handler.d.ts +14 -0
  217. package/dist/node_modules/@earendil-works/pi-server/dist/handler.d.ts.map +1 -0
  218. package/dist/node_modules/@earendil-works/pi-server/dist/handler.js +105 -0
  219. package/dist/node_modules/@earendil-works/pi-server/dist/handler.js.map +1 -0
  220. package/dist/node_modules/@earendil-works/pi-server/dist/index.d.ts +5 -0
  221. package/dist/node_modules/@earendil-works/pi-server/dist/index.d.ts.map +1 -0
  222. package/dist/node_modules/@earendil-works/pi-server/dist/index.js +5 -0
  223. package/dist/node_modules/@earendil-works/pi-server/dist/index.js.map +1 -0
  224. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.d.ts +3 -0
  225. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.d.ts.map +1 -0
  226. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.js +56 -0
  227. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.js.map +1 -0
  228. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.d.ts +99 -0
  229. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.d.ts.map +1 -0
  230. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.js +12 -0
  231. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.js.map +1 -0
  232. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.d.ts +20 -0
  233. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.d.ts.map +1 -0
  234. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.js +145 -0
  235. package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.js.map +1 -0
  236. package/dist/node_modules/@earendil-works/pi-server/dist/listener.d.ts +8 -0
  237. package/dist/node_modules/@earendil-works/pi-server/dist/listener.d.ts.map +1 -0
  238. package/dist/node_modules/@earendil-works/pi-server/dist/listener.js +2 -0
  239. package/dist/node_modules/@earendil-works/pi-server/dist/listener.js.map +1 -0
  240. package/dist/node_modules/@earendil-works/pi-server/dist/protocol.d.ts +22 -0
  241. package/dist/node_modules/@earendil-works/pi-server/dist/protocol.d.ts.map +1 -0
  242. package/dist/node_modules/@earendil-works/pi-server/dist/protocol.js +266 -0
  243. package/dist/node_modules/@earendil-works/pi-server/dist/protocol.js.map +1 -0
  244. package/dist/node_modules/@earendil-works/pi-server/dist/radius.d.ts +36 -0
  245. package/dist/node_modules/@earendil-works/pi-server/dist/radius.d.ts.map +1 -0
  246. package/dist/node_modules/@earendil-works/pi-server/dist/radius.js +360 -0
  247. package/dist/node_modules/@earendil-works/pi-server/dist/radius.js.map +1 -0
  248. package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.d.ts +31 -0
  249. package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.d.ts.map +1 -0
  250. package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.js +168 -0
  251. package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.js.map +1 -0
  252. package/dist/node_modules/@earendil-works/pi-server/dist/serve.d.ts +2 -0
  253. package/dist/node_modules/@earendil-works/pi-server/dist/serve.d.ts.map +1 -0
  254. package/dist/node_modules/@earendil-works/pi-server/dist/serve.js +70 -0
  255. package/dist/node_modules/@earendil-works/pi-server/dist/serve.js.map +1 -0
  256. package/dist/node_modules/@earendil-works/pi-server/dist/server.d.ts +46 -0
  257. package/dist/node_modules/@earendil-works/pi-server/dist/server.d.ts.map +1 -0
  258. package/dist/node_modules/@earendil-works/pi-server/dist/server.js +549 -0
  259. package/dist/node_modules/@earendil-works/pi-server/dist/server.js.map +1 -0
  260. package/dist/node_modules/@earendil-works/pi-server/dist/session-router.d.ts +40 -0
  261. package/dist/node_modules/@earendil-works/pi-server/dist/session-router.d.ts.map +1 -0
  262. package/dist/node_modules/@earendil-works/pi-server/dist/session-router.js +264 -0
  263. package/dist/node_modules/@earendil-works/pi-server/dist/session-router.js.map +1 -0
  264. package/dist/node_modules/@earendil-works/pi-server/dist/sessions.d.ts +2195 -0
  265. package/dist/node_modules/@earendil-works/pi-server/dist/sessions.d.ts.map +1 -0
  266. package/dist/node_modules/@earendil-works/pi-server/dist/sessions.js +332 -0
  267. package/dist/node_modules/@earendil-works/pi-server/dist/sessions.js.map +1 -0
  268. package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.d.ts +24 -0
  269. package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.d.ts.map +1 -0
  270. package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.js +39 -0
  271. package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.js.map +1 -0
  272. package/dist/node_modules/@earendil-works/pi-server/dist/storage.d.ts +10 -0
  273. package/dist/node_modules/@earendil-works/pi-server/dist/storage.d.ts.map +1 -0
  274. package/dist/node_modules/@earendil-works/pi-server/dist/storage.js +58 -0
  275. package/dist/node_modules/@earendil-works/pi-server/dist/storage.js.map +1 -0
  276. package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.d.ts +34 -0
  277. package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.d.ts.map +1 -0
  278. package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.js +290 -0
  279. package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.js.map +1 -0
  280. package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.d.ts +35 -0
  281. package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.d.ts.map +1 -0
  282. package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.js +138 -0
  283. package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.js.map +1 -0
  284. package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.d.ts +57 -0
  285. package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.d.ts.map +1 -0
  286. package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.js +189 -0
  287. package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.js.map +1 -0
  288. package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.d.ts +6 -0
  289. package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.d.ts.map +1 -0
  290. package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.js +4 -0
  291. package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.js.map +1 -0
  292. package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.d.ts +13 -0
  293. package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.d.ts.map +1 -0
  294. package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.js +17 -0
  295. package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.js.map +1 -0
  296. package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.d.ts +66 -0
  297. package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.d.ts.map +1 -0
  298. package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.js +254 -0
  299. package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.js.map +1 -0
  300. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.d.ts +3 -0
  301. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.d.ts.map +1 -0
  302. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.js +9 -0
  303. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.js.map +1 -0
  304. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.d.ts +5 -0
  305. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.d.ts.map +1 -0
  306. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.js +4 -0
  307. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.js.map +1 -0
  308. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.d.ts +24 -0
  309. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.d.ts.map +1 -0
  310. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.js +421 -0
  311. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.js.map +1 -0
  312. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.d.ts +7 -0
  313. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.d.ts.map +1 -0
  314. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.js +22 -0
  315. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.js.map +1 -0
  316. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.d.ts +15 -0
  317. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.d.ts.map +1 -0
  318. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.js +2 -0
  319. package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.js.map +1 -0
  320. package/dist/node_modules/@earendil-works/pi-server/dist/types.d.ts +49 -0
  321. package/dist/node_modules/@earendil-works/pi-server/dist/types.d.ts.map +1 -0
  322. package/dist/node_modules/@earendil-works/pi-server/dist/types.js +2 -0
  323. package/dist/node_modules/@earendil-works/pi-server/dist/types.js.map +1 -0
  324. package/dist/node_modules/@earendil-works/pi-server/package.json +45 -0
  325. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/CHANGELOG.md +46 -0
  326. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/LICENSE +21 -0
  327. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/README.md +33 -0
  328. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.d.ts +6 -0
  329. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.d.ts.map +1 -0
  330. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.js +103 -0
  331. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.js.map +1 -0
  332. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.d.ts +6 -0
  333. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.d.ts.map +1 -0
  334. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.js +70 -0
  335. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.js.map +1 -0
  336. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.d.ts +5 -0
  337. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.d.ts.map +1 -0
  338. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.js +4 -0
  339. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.js.map +1 -0
  340. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations/001_initial.sql +121 -0
  341. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.d.ts +3 -0
  342. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.d.ts.map +1 -0
  343. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.js +7 -0
  344. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.js.map +1 -0
  345. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.d.ts +41 -0
  346. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.d.ts.map +1 -0
  347. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.js +408 -0
  348. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.js.map +1 -0
  349. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.d.ts +14 -0
  350. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.d.ts.map +1 -0
  351. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.js +153 -0
  352. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.js.map +1 -0
  353. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.d.ts +6 -0
  354. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.d.ts.map +1 -0
  355. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.js +213 -0
  356. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.js.map +1 -0
  357. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.d.ts +24 -0
  358. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.d.ts.map +1 -0
  359. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.js +126 -0
  360. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.js.map +1 -0
  361. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.d.ts +23 -0
  362. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.d.ts.map +1 -0
  363. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.js +69 -0
  364. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.js.map +1 -0
  365. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.d.ts +4 -0
  366. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.d.ts.map +1 -0
  367. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.js +13 -0
  368. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.js.map +1 -0
  369. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.d.ts +6 -0
  370. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.d.ts.map +1 -0
  371. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.js +39 -0
  372. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.js.map +1 -0
  373. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.d.ts +20 -0
  374. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.d.ts.map +1 -0
  375. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.js +50 -0
  376. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.js.map +1 -0
  377. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.d.ts +23 -0
  378. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.d.ts.map +1 -0
  379. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.js +91 -0
  380. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.js.map +1 -0
  381. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.d.ts +42 -0
  382. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.d.ts.map +1 -0
  383. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.js +163 -0
  384. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.js.map +1 -0
  385. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.d.ts +19 -0
  386. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.d.ts.map +1 -0
  387. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.js +58 -0
  388. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.js.map +1 -0
  389. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.d.ts +41 -0
  390. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.d.ts.map +1 -0
  391. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.js +112 -0
  392. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.js.map +1 -0
  393. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.d.ts +7 -0
  394. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.d.ts.map +1 -0
  395. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.js +22 -0
  396. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.js.map +1 -0
  397. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.d.ts +34 -0
  398. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.d.ts.map +1 -0
  399. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.js +37 -0
  400. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.js.map +1 -0
  401. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.d.ts +20 -0
  402. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.d.ts.map +1 -0
  403. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.js +38 -0
  404. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.js.map +1 -0
  405. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.d.ts +35 -0
  406. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.d.ts.map +1 -0
  407. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.js +95 -0
  408. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.js.map +1 -0
  409. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.d.ts +38 -0
  410. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.d.ts.map +1 -0
  411. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.js +49 -0
  412. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.js.map +1 -0
  413. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.d.ts +7 -0
  414. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.d.ts.map +1 -0
  415. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.js +22 -0
  416. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.js.map +1 -0
  417. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.d.ts +17 -0
  418. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.d.ts.map +1 -0
  419. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.js +40 -0
  420. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.js.map +1 -0
  421. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.d.ts +26 -0
  422. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.d.ts.map +1 -0
  423. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.js +99 -0
  424. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.js.map +1 -0
  425. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.d.ts +15 -0
  426. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.d.ts.map +1 -0
  427. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.js +31 -0
  428. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.js.map +1 -0
  429. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.d.ts +38 -0
  430. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.d.ts.map +1 -0
  431. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.js +161 -0
  432. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.js.map +1 -0
  433. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.d.ts +29 -0
  434. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.d.ts.map +1 -0
  435. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.js +2 -0
  436. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.js.map +1 -0
  437. package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/package.json +33 -0
  438. package/dist/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
  439. package/dist/node_modules/@earendil-works/pi-tui/dist/layout.d.ts.map +1 -1
  440. package/dist/node_modules/@earendil-works/pi-tui/dist/layout.js +5 -2
  441. package/dist/node_modules/@earendil-works/pi-tui/dist/layout.js.map +1 -1
  442. package/dist/node_modules/@earendil-works/pi-tui/dist/tui.d.ts.map +1 -1
  443. package/dist/node_modules/@earendil-works/pi-tui/dist/tui.js +17 -4
  444. package/dist/node_modules/@earendil-works/pi-tui/dist/tui.js.map +1 -1
  445. package/dist/node_modules/@earendil-works/pi-tui/package.json +1 -1
  446. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  447. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  448. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  449. package/examples/extensions/gondolin/package-lock.json +2 -2
  450. package/examples/extensions/gondolin/package.json +1 -1
  451. package/examples/extensions/official/package.json +1 -1
  452. package/examples/extensions/sandbox/package-lock.json +2 -2
  453. package/examples/extensions/sandbox/package.json +1 -1
  454. package/examples/extensions/with-deps/package-lock.json +2 -2
  455. package/examples/extensions/with-deps/package.json +1 -1
  456. package/examples/plugins/pi-example-plugin/package.json +1 -1
  457. package/npm-shrinkwrap.json +3 -14
  458. package/package.json +2 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repo.d.ts","sourceRoot":"","sources":["../../src/sqlite/repo.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAS,WAAW,EA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,EAAE,OAAO,GAAG,OAAO,CAAC,qBAAqB,EAAE,CAAC,CAmCnF;IAEK,MAAM,CAAC,QAAQ,EAAE,qBAAqB,EAAE,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAuB9E;IAEK,IAAI,CAAC,MAAM,EAAE,qBAAqB,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAgE5G;IAED,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAKrC;YAEa,oCAAoC;YAcpC,iBAAiB;IAO/B,OAAO,CAAC,wBAAwB;YAoBlB,yBAAyB;IAWvC,OAAO,CAAC,SAAS;IAKjB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,UAAU;CAGlB","sourcesContent":["import { mkdir, open as openFile, readdir, realpath, rm } from \"node:fs/promises\";\nimport { dirname, join } from \"node:path\";\nimport type { Context, Entry, ForkOptions, SessionCreateOptions, StoredValue } from \"@earendil-works/pi-agent-core\";\nimport { branchTip, createForkSnapshot, StorageBackedSession } from \"@earendil-works/pi-agent-core\";\nimport { uuidv7 } from \"@earendil-works/pi-ai\";\nimport { applyInitialSchema } from \"./migrations.ts\";\nimport { appendEntryToBranchIndex, scanBranchEntries } from \"./session/branch-entries.ts\";\nimport { decodeEntryRow, type EntryRow, EntryRowWriter } from \"./session/entries.ts\";\nimport {\n\tdeleteSessionRows,\n\thasSessionRow,\n\tinsertSessionRow,\n\tmetadataFromSessionRow,\n\treadAllSessionRows,\n\treadSessionRow,\n\ttype SqliteSessionMetadata,\n} from \"./session/session-row.ts\";\nimport { readAllScalarValueRows, setScalarValueRow } from \"./session/values.ts\";\nimport { SqliteOpenSession } from \"./session.ts\";\nimport { sql } from \"./sql.ts\";\nimport { SqliteStorage, type SqliteStorageSnapshot } from \"./storage.ts\";\nimport type { SqliteDatabase, SqliteDatabaseFactory } from \"./types.ts\";\n\nexport const SQLITE_STORAGE_VERSION = 1;\nexport const SQLITE_SESSION_EXTENSION = \".sqlite\";\n\nconst FIRST_AVAILABLE_COMMIT_SEQ = 1;\nconst SAFE_SESSION_FILE_ID = /^[A-Za-z0-9_-]+$/;\n\nexport type SqliteSessionCreateOptions = SessionCreateOptions;\n\nexport interface SqliteSessionRepoOptions {\n\tdirectory: string;\n\t/** Optional single container path. Defaults to one encoded `${id}.sqlite` file per session under directory. */\n\tdatabasePath?: string;\n\tdatabaseFactory: SqliteDatabaseFactory;\n\tnow?: () => number;\n}\n\nfunction sessionFileName(id: string): string {\n\tif (SAFE_SESSION_FILE_ID.test(id)) return `${id}${SQLITE_SESSION_EXTENSION}`;\n\tconst encoded = Buffer.from(id, \"utf16le\").toString(\"base64url\");\n\treturn `~${encoded}${SQLITE_SESSION_EXTENSION}`;\n}\n\nfunction sessionPath(directory: string, id: string): string {\n\treturn join(directory, sessionFileName(id));\n}\n\nfunction storageIdentity(path: string, sessionId: string): string {\n\treturn JSON.stringify([path, sessionId]);\n}\n\nfunction isErrorWithCode(error: unknown, code: string): boolean {\n\treturn typeof error === \"object\" && error !== null && \"code\" in error && error.code === code;\n}\n\nasync function removeSessionFiles(path: string, options: { force: boolean }): Promise<void> {\n\tawait rm(path, { force: options.force });\n\tawait rm(`${path}-wal`, { force: true });\n\tawait rm(`${path}-shm`, { force: true });\n}\n\nfunction configureWritableConnection(db: SqliteDatabase): void {\n\tdb.exec(\"PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;\");\n}\n\nfunction configureReadOnlyConnection(db: SqliteDatabase): void {\n\tdb.exec(\"PRAGMA busy_timeout = 5000;\");\n}\n\ninterface ForkSnapshot {\n\tentries: Entry[];\n\tscalarValues: StoredValue<unknown>[];\n\tmessageCount: number;\n\tnextSeq: number;\n}\n\nfunction readSourceEntries(db: SqliteDatabase, sessionId: string): Entry[] {\n\treturn sql`SELECT id, parent_id, seq, type, custom_type, timestamp, payload\n\t\tFROM entries WHERE session_id = ${sessionId} ORDER BY seq ASC`\n\t\t.all<EntryRow>(db)\n\t\t.map(decodeEntryRow);\n}\n\nfunction buildForkSnapshot(source: SqliteStorageSnapshot, options: ForkOptions): ForkSnapshot {\n\tconst snapshot = createForkSnapshot(\n\t\t{\n\t\t\tentries: source.entries,\n\t\t\tscalarValues: source.scalarValues,\n\t\t\tentriesComplete: source.entriesComplete,\n\t\t},\n\t\toptions,\n\t);\n\tconst entries = [...snapshot.entries.values()].sort((left, right) => left.seq - right.seq);\n\treturn {\n\t\tentries,\n\t\tscalarValues: snapshot.scalarValues,\n\t\tmessageCount: entries.filter((entry) => entry.type === \"message\").length,\n\t\tnextSeq: snapshot.nextSeq,\n\t};\n}\n\n// TODO(WP08): Remove this snapshot path when SQLite forks use streaming staging.\nfunction readForkSourceEntries(\n\tdb: SqliteDatabase,\n\tsessionId: string,\n\tscalarValues: readonly StoredValue<unknown>[],\n\toptions: ForkOptions,\n): Entry[] {\n\tif (options.scope === \"tree\") return readSourceEntries(db, sessionId);\n\tconst sourceAddress = branchTip(options.branch);\n\tconst sourceTip = scalarValues.find(\n\t\t(stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key,\n\t) as StoredValue<string | null> | undefined;\n\tif (sourceTip === undefined) throw new Error(`Unknown source branch: ${options.branch}`);\n\treturn sourceTip.value === null\n\t\t? []\n\t\t: scanBranchEntries(db, sessionId, { start: sourceTip.value, order: \"oldestFirst\" });\n}\n\nfunction createSqliteForkSnapshot(\n\tsourceDb: SqliteDatabase,\n\tsource: SqliteSessionMetadata,\n\toptions: ForkOptions,\n): ForkSnapshot {\n\tsourceDb.exec(\"BEGIN\");\n\tlet committed = false;\n\ttry {\n\t\tmetadataFromSessionRow(source.path, readSessionRow(sourceDb, source.id), SQLITE_STORAGE_VERSION);\n\t\tconst scalarValues = readAllScalarValueRows(sourceDb, source.id);\n\t\tconst snapshot = buildForkSnapshot(\n\t\t\t{\n\t\t\t\tentries: readForkSourceEntries(sourceDb, source.id, scalarValues, options),\n\t\t\t\tscalarValues,\n\t\t\t\tentriesComplete: options.scope === \"tree\",\n\t\t\t},\n\t\t\toptions,\n\t\t);\n\t\tsourceDb.exec(\"COMMIT\");\n\t\tcommitted = true;\n\t\treturn snapshot;\n\t} catch (error) {\n\t\tif (!committed) sourceDb.exec(\"ROLLBACK\");\n\t\tthrow error;\n\t}\n}\n\nfunction insertForkValue(db: SqliteDatabase, sessionId: string, stored: StoredValue<unknown>): void {\n\tsetScalarValueRow(db, sessionId, stored.address.namespace, stored.address.key, stored.seq, stored.value);\n}\n\nfunction updateForkSessionStats(db: SqliteDatabase, sessionId: string, messageCount: number): void {\n\tsql`UPDATE sessions SET message_count = ${messageCount} WHERE id = ${sessionId}`.run(db);\n}\n\nexport class SqliteSessionRepo {\n\tprivate readonly directory: string;\n\tprivate readonly databasePath: string | undefined;\n\tprivate readonly databaseFactory: SqliteDatabaseFactory;\n\tprivate readonly now: () => number;\n\tprivate readonly pendingIds = new Set<string>();\n\tprivate readonly openStorages = new Map<string, SqliteStorage>();\n\tprivate readonly openSessions = new Set<SqliteOpenSession>();\n\tprivate closed = false;\n\tprivate closePromise: Promise<void> | undefined;\n\n\tconstructor(options: SqliteSessionRepoOptions) {\n\t\tthis.directory = options.directory;\n\t\tthis.databasePath = options.databasePath;\n\t\tthis.databaseFactory = options.databaseFactory;\n\t\tthis.now = options.now ?? Date.now;\n\t}\n\n\tasync create(options: SqliteSessionCreateOptions | undefined, _context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\toptions ??= {};\n\t\tconst createdAt = this.now();\n\t\tconst id = options.id ?? uuidv7(createdAt);\n\t\tthis.reserveId(id);\n\t\tconst path = this.pathForSession(id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet reservedFile = false;\n\t\tlet initialized = false;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tawait mkdir(dirname(path), { recursive: true });\n\t\t\tif (!this.usesSharedDatabase()) {\n\t\t\t\tconst file = await openFile(path, \"wx\");\n\t\t\t\tawait file.close();\n\t\t\t\treservedFile = true;\n\t\t\t}\n\t\t\tconst activeDb = await this.databaseFactory.open(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tawait applyInitialSchema(activeDb);\n\t\t\tconst canonicalPath = await realpath(path);\n\t\t\tconst metadata: SqliteSessionMetadata = {\n\t\t\t\tid,\n\t\t\t\tcreatedAt,\n\t\t\t\tstorageVersion: SQLITE_STORAGE_VERSION,\n\t\t\t\t...(options.parentSessionId === undefined ? {} : { parentSessionId: options.parentSessionId }),\n\t\t\t\tpath: canonicalPath,\n\t\t\t};\n\t\t\tactiveDb.transaction(() => {\n\t\t\t\tif (hasSessionRow(activeDb, id)) throw new Error(`SQLite session already exists: ${id}`);\n\t\t\t\tinsertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, FIRST_AVAILABLE_COMMIT_SEQ);\n\t\t\t});\n\t\t\tinitialized = true;\n\t\t\tsession = this.openStorageBackedSession(metadata, activeDb);\n\t\t\treturn session;\n\t\t} catch (error) {\n\t\t\tif (reservedFile && !initialized) await removeSessionFiles(path, { force: true });\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tasync open(metadata: SqliteSessionMetadata, _context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\tthis.reserveId(metadata.id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tconst path = await this.repositoryPathForMetadata(metadata);\n\t\t\tconst activeDb = await this.databaseFactory.openExisting(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tconst stored = metadataFromSessionRow(path, readSessionRow(activeDb, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\tsession = this.openStorageBackedSession(stored, activeDb);\n\t\t\treturn session;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tasync list(_options: undefined, _context: Context): Promise<SqliteSessionMetadata[]> {\n\t\tthis.assertOpen();\n\t\tlet paths: string[];\n\t\tif (this.usesSharedDatabase()) {\n\t\t\tpaths = [this.databasePath!];\n\t\t} else {\n\t\t\tlet names: string[];\n\t\t\ttry {\n\t\t\t\tnames = await readdir(this.directory);\n\t\t\t} catch (error) {\n\t\t\t\tif (isErrorWithCode(error, \"ENOENT\")) return [];\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tpaths = names\n\t\t\t\t.filter((name) => name.endsWith(SQLITE_SESSION_EXTENSION))\n\t\t\t\t.map((name) => join(this.directory, name));\n\t\t}\n\t\tconst sessions: SqliteSessionMetadata[] = [];\n\t\tfor (const path of paths) {\n\t\t\tlet db: SqliteDatabase | undefined;\n\t\t\ttry {\n\t\t\t\tconst canonicalPath = await realpath(path);\n\t\t\t\tdb = await this.databaseFactory.openReadOnly(canonicalPath);\n\t\t\t\tconfigureReadOnlyConnection(db);\n\t\t\t\tfor (const row of readAllSessionRows(db)) {\n\t\t\t\t\tsessions.push(metadataFromSessionRow(canonicalPath, row, SQLITE_STORAGE_VERSION));\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Discovery is best-effort: corrupt files, incompatible versions, and\n\t\t\t\t// unrelated *.sqlite files are reported when explicitly opened.\n\t\t\t} finally {\n\t\t\t\tdb?.close();\n\t\t\t}\n\t\t}\n\t\treturn sessions.sort((left, right) => right.createdAt - left.createdAt);\n\t}\n\n\tasync delete(metadata: SqliteSessionMetadata, _context: Context): Promise<void> {\n\t\tthis.assertOpen();\n\t\tthis.reserveId(metadata.id);\n\t\ttry {\n\t\t\tconst path = await this.repositoryPathForMetadata(metadata);\n\t\t\tconst db = await this.databaseFactory.openExisting(path);\n\t\t\ttry {\n\t\t\t\tconfigureWritableConnection(db);\n\t\t\t\tif (this.usesSharedDatabase()) {\n\t\t\t\t\tdb.transaction(() => {\n\t\t\t\t\t\tmetadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\t\t\t\tdeleteSessionRows(db, metadata.id);\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tmetadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tdb.close();\n\t\t\t}\n\t\t\tif (!this.usesSharedDatabase()) await removeSessionFiles(path, { force: false });\n\t\t} finally {\n\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t}\n\t}\n\n\tasync fork(source: SqliteSessionMetadata, options: ForkOptions, context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\tconst createdAt = this.now();\n\t\tconst id = options.id ?? uuidv7(createdAt);\n\t\tthis.reserveId(id);\n\t\tconst sourceStorage = this.openStorages.get(storageIdentity(source.path, source.id));\n\t\tconst activeSourceSnapshot = sourceStorage?.snapshot(options, context);\n\t\tvoid activeSourceSnapshot?.catch(() => undefined);\n\t\tconst path = this.pathForSession(id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet reservedFile = false;\n\t\tlet initialized = false;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tawait mkdir(dirname(path), { recursive: true });\n\t\t\tif (!this.usesSharedDatabase()) {\n\t\t\t\tconst file = await openFile(path, \"wx\");\n\t\t\t\tawait file.close();\n\t\t\t\treservedFile = true;\n\t\t\t}\n\n\t\t\tconst snapshot =\n\t\t\t\tactiveSourceSnapshot === undefined\n\t\t\t\t\t? await this.createForkSnapshotFromExternalSource(source, options)\n\t\t\t\t\t: buildForkSnapshot(await activeSourceSnapshot, options);\n\n\t\t\tconst activeDb = await this.databaseFactory.open(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tawait applyInitialSchema(activeDb);\n\t\t\tconst canonicalPath = await realpath(path);\n\t\t\tconst metadata: SqliteSessionMetadata = {\n\t\t\t\tid,\n\t\t\t\tcreatedAt,\n\t\t\t\tstorageVersion: SQLITE_STORAGE_VERSION,\n\t\t\t\tparentSessionId: source.id,\n\t\t\t\tpath: canonicalPath,\n\t\t\t};\n\t\t\tactiveDb.transaction(() => {\n\t\t\t\tif (hasSessionRow(activeDb, id)) throw new Error(`SQLite session already exists: ${id}`);\n\t\t\t\tinsertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, snapshot.nextSeq);\n\t\t\t\tconst entryWriter = new EntryRowWriter(activeDb, id);\n\t\t\t\tfor (const entry of snapshot.entries) {\n\t\t\t\t\tentryWriter.insert(entry);\n\t\t\t\t\tappendEntryToBranchIndex(activeDb, id, entry);\n\t\t\t\t}\n\t\t\t\tfor (const stored of snapshot.scalarValues) insertForkValue(activeDb, id, stored);\n\t\t\t\tupdateForkSessionStats(activeDb, id, snapshot.messageCount);\n\t\t\t});\n\t\t\tinitialized = true;\n\t\t\tsession = this.openStorageBackedSession(metadata, activeDb);\n\t\t\treturn session;\n\t\t} catch (error) {\n\t\t\tif (reservedFile && !initialized) await removeSessionFiles(path, { force: true });\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tclose(context: Context): Promise<void> {\n\t\tif (this.closePromise !== undefined) return this.closePromise;\n\t\tthis.closed = true;\n\t\tthis.closePromise = this.closeOpenSessions(context);\n\t\treturn this.closePromise;\n\t}\n\n\tprivate async createForkSnapshotFromExternalSource(\n\t\tsource: SqliteSessionMetadata,\n\t\toptions: ForkOptions,\n\t): Promise<ForkSnapshot> {\n\t\tconst path = await realpath(source.path);\n\t\tconst sourceDb = await this.databaseFactory.openReadOnly(path);\n\t\ttry {\n\t\t\tconfigureReadOnlyConnection(sourceDb);\n\t\t\treturn createSqliteForkSnapshot(sourceDb, { ...source, path }, options);\n\t\t} finally {\n\t\t\tsourceDb.close();\n\t\t}\n\t}\n\n\tprivate async closeOpenSessions(context: Context): Promise<void> {\n\t\tconst results = await Promise.allSettled([...this.openSessions].map((session) => session.close(context)));\n\t\tconst errors = results.flatMap((result) => (result.status === \"rejected\" ? [result.reason] : []));\n\t\tif (errors.length === 1) throw errors[0];\n\t\tif (errors.length > 1) throw new AggregateError(errors, \"Failed to close SQLite Sessions\");\n\t}\n\n\tprivate openStorageBackedSession(metadata: SqliteSessionMetadata, db: SqliteDatabase): SqliteOpenSession {\n\t\tconst key = storageIdentity(metadata.path, metadata.id);\n\t\tconst storage = new SqliteStorage(db, { sessionId: metadata.id, now: this.now });\n\t\tthis.openStorages.set(key, storage);\n\t\tconst session = new StorageBackedSession(metadata, storage);\n\t\tconst openSession = new SqliteOpenSession(session, {\n\t\t\tonClose: () => {\n\t\t\t\ttry {\n\t\t\t\t\tdb.close();\n\t\t\t\t} finally {\n\t\t\t\t\tif (this.openStorages.get(key) === storage) this.openStorages.delete(key);\n\t\t\t\t\tthis.openSessions.delete(openSession);\n\t\t\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tthis.openSessions.add(openSession);\n\t\treturn openSession;\n\t}\n\n\tprivate async repositoryPathForMetadata(metadata: SqliteSessionMetadata): Promise<string> {\n\t\tconst [expected, actual] = await Promise.all([\n\t\t\trealpath(this.pathForSession(metadata.id)),\n\t\t\trealpath(metadata.path),\n\t\t]);\n\t\tif (expected !== actual) {\n\t\t\tthrow new Error(`SQLite session metadata path is outside this repository: ${metadata.path}`);\n\t\t}\n\t\treturn actual;\n\t}\n\n\tprivate reserveId(id: string): void {\n\t\tif (this.pendingIds.has(id)) throw new Error(`Session is already open: ${id}`);\n\t\tthis.pendingIds.add(id);\n\t}\n\n\tprivate pathForSession(id: string): string {\n\t\treturn this.databasePath ?? sessionPath(this.directory, id);\n\t}\n\n\tprivate usesSharedDatabase(): boolean {\n\t\treturn this.databasePath !== undefined;\n\t}\n\n\tprivate assertOpen(): void {\n\t\tif (this.closed) throw new Error(\"SqliteSessionRepo is closed\");\n\t}\n}\n"]}
@@ -0,0 +1,408 @@
1
+ import { mkdir, open as openFile, readdir, realpath, rm } from "node:fs/promises";
2
+ import { dirname, join } from "node:path";
3
+ import { branchTip, createForkSnapshot, StorageBackedSession } from "@earendil-works/pi-agent-core";
4
+ import { uuidv7 } from "@earendil-works/pi-ai";
5
+ import { applyInitialSchema } from "./migrations.js";
6
+ import { appendEntryToBranchIndex, scanBranchEntries } from "./session/branch-entries.js";
7
+ import { decodeEntryRow, EntryRowWriter } from "./session/entries.js";
8
+ import { deleteSessionRows, hasSessionRow, insertSessionRow, metadataFromSessionRow, readAllSessionRows, readSessionRow, } from "./session/session-row.js";
9
+ import { readAllScalarValueRows, setScalarValueRow } from "./session/values.js";
10
+ import { SqliteOpenSession } from "./session.js";
11
+ import { sql } from "./sql.js";
12
+ import { SqliteStorage } from "./storage.js";
13
+ export const SQLITE_STORAGE_VERSION = 1;
14
+ export const SQLITE_SESSION_EXTENSION = ".sqlite";
15
+ const FIRST_AVAILABLE_COMMIT_SEQ = 1;
16
+ const SAFE_SESSION_FILE_ID = /^[A-Za-z0-9_-]+$/;
17
+ function sessionFileName(id) {
18
+ if (SAFE_SESSION_FILE_ID.test(id))
19
+ return `${id}${SQLITE_SESSION_EXTENSION}`;
20
+ const encoded = Buffer.from(id, "utf16le").toString("base64url");
21
+ return `~${encoded}${SQLITE_SESSION_EXTENSION}`;
22
+ }
23
+ function sessionPath(directory, id) {
24
+ return join(directory, sessionFileName(id));
25
+ }
26
+ function storageIdentity(path, sessionId) {
27
+ return JSON.stringify([path, sessionId]);
28
+ }
29
+ function isErrorWithCode(error, code) {
30
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
31
+ }
32
+ async function removeSessionFiles(path, options) {
33
+ await rm(path, { force: options.force });
34
+ await rm(`${path}-wal`, { force: true });
35
+ await rm(`${path}-shm`, { force: true });
36
+ }
37
+ function configureWritableConnection(db) {
38
+ db.exec("PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
39
+ }
40
+ function configureReadOnlyConnection(db) {
41
+ db.exec("PRAGMA busy_timeout = 5000;");
42
+ }
43
+ function readSourceEntries(db, sessionId) {
44
+ return sql `SELECT id, parent_id, seq, type, custom_type, timestamp, payload
45
+ FROM entries WHERE session_id = ${sessionId} ORDER BY seq ASC`
46
+ .all(db)
47
+ .map(decodeEntryRow);
48
+ }
49
+ function buildForkSnapshot(source, options) {
50
+ const snapshot = createForkSnapshot({
51
+ entries: source.entries,
52
+ scalarValues: source.scalarValues,
53
+ entriesComplete: source.entriesComplete,
54
+ }, options);
55
+ const entries = [...snapshot.entries.values()].sort((left, right) => left.seq - right.seq);
56
+ return {
57
+ entries,
58
+ scalarValues: snapshot.scalarValues,
59
+ messageCount: entries.filter((entry) => entry.type === "message").length,
60
+ nextSeq: snapshot.nextSeq,
61
+ };
62
+ }
63
+ // TODO(WP08): Remove this snapshot path when SQLite forks use streaming staging.
64
+ function readForkSourceEntries(db, sessionId, scalarValues, options) {
65
+ if (options.scope === "tree")
66
+ return readSourceEntries(db, sessionId);
67
+ const sourceAddress = branchTip(options.branch);
68
+ const sourceTip = scalarValues.find((stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key);
69
+ if (sourceTip === undefined)
70
+ throw new Error(`Unknown source branch: ${options.branch}`);
71
+ return sourceTip.value === null
72
+ ? []
73
+ : scanBranchEntries(db, sessionId, { start: sourceTip.value, order: "oldestFirst" });
74
+ }
75
+ function createSqliteForkSnapshot(sourceDb, source, options) {
76
+ sourceDb.exec("BEGIN");
77
+ let committed = false;
78
+ try {
79
+ metadataFromSessionRow(source.path, readSessionRow(sourceDb, source.id), SQLITE_STORAGE_VERSION);
80
+ const scalarValues = readAllScalarValueRows(sourceDb, source.id);
81
+ const snapshot = buildForkSnapshot({
82
+ entries: readForkSourceEntries(sourceDb, source.id, scalarValues, options),
83
+ scalarValues,
84
+ entriesComplete: options.scope === "tree",
85
+ }, options);
86
+ sourceDb.exec("COMMIT");
87
+ committed = true;
88
+ return snapshot;
89
+ }
90
+ catch (error) {
91
+ if (!committed)
92
+ sourceDb.exec("ROLLBACK");
93
+ throw error;
94
+ }
95
+ }
96
+ function insertForkValue(db, sessionId, stored) {
97
+ setScalarValueRow(db, sessionId, stored.address.namespace, stored.address.key, stored.seq, stored.value);
98
+ }
99
+ function updateForkSessionStats(db, sessionId, messageCount) {
100
+ sql `UPDATE sessions SET message_count = ${messageCount} WHERE id = ${sessionId}`.run(db);
101
+ }
102
+ export class SqliteSessionRepo {
103
+ directory;
104
+ databasePath;
105
+ databaseFactory;
106
+ now;
107
+ pendingIds = new Set();
108
+ openStorages = new Map();
109
+ openSessions = new Set();
110
+ closed = false;
111
+ closePromise;
112
+ constructor(options) {
113
+ this.directory = options.directory;
114
+ this.databasePath = options.databasePath;
115
+ this.databaseFactory = options.databaseFactory;
116
+ this.now = options.now ?? Date.now;
117
+ }
118
+ async create(options, _context) {
119
+ this.assertOpen();
120
+ options ??= {};
121
+ const createdAt = this.now();
122
+ const id = options.id ?? uuidv7(createdAt);
123
+ this.reserveId(id);
124
+ const path = this.pathForSession(id);
125
+ let db;
126
+ let reservedFile = false;
127
+ let initialized = false;
128
+ let session;
129
+ try {
130
+ await mkdir(dirname(path), { recursive: true });
131
+ if (!this.usesSharedDatabase()) {
132
+ const file = await openFile(path, "wx");
133
+ await file.close();
134
+ reservedFile = true;
135
+ }
136
+ const activeDb = await this.databaseFactory.open(path);
137
+ db = activeDb;
138
+ configureWritableConnection(activeDb);
139
+ await applyInitialSchema(activeDb);
140
+ const canonicalPath = await realpath(path);
141
+ const metadata = {
142
+ id,
143
+ createdAt,
144
+ storageVersion: SQLITE_STORAGE_VERSION,
145
+ ...(options.parentSessionId === undefined ? {} : { parentSessionId: options.parentSessionId }),
146
+ path: canonicalPath,
147
+ };
148
+ activeDb.transaction(() => {
149
+ if (hasSessionRow(activeDb, id))
150
+ throw new Error(`SQLite session already exists: ${id}`);
151
+ insertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, FIRST_AVAILABLE_COMMIT_SEQ);
152
+ });
153
+ initialized = true;
154
+ session = this.openStorageBackedSession(metadata, activeDb);
155
+ return session;
156
+ }
157
+ catch (error) {
158
+ if (reservedFile && !initialized)
159
+ await removeSessionFiles(path, { force: true });
160
+ throw error;
161
+ }
162
+ finally {
163
+ if (session === undefined) {
164
+ try {
165
+ db?.close();
166
+ }
167
+ finally {
168
+ this.pendingIds.delete(id);
169
+ }
170
+ }
171
+ }
172
+ }
173
+ async open(metadata, _context) {
174
+ this.assertOpen();
175
+ this.reserveId(metadata.id);
176
+ let db;
177
+ let session;
178
+ try {
179
+ const path = await this.repositoryPathForMetadata(metadata);
180
+ const activeDb = await this.databaseFactory.openExisting(path);
181
+ db = activeDb;
182
+ configureWritableConnection(activeDb);
183
+ const stored = metadataFromSessionRow(path, readSessionRow(activeDb, metadata.id), SQLITE_STORAGE_VERSION);
184
+ session = this.openStorageBackedSession(stored, activeDb);
185
+ return session;
186
+ }
187
+ finally {
188
+ if (session === undefined) {
189
+ try {
190
+ db?.close();
191
+ }
192
+ finally {
193
+ this.pendingIds.delete(metadata.id);
194
+ }
195
+ }
196
+ }
197
+ }
198
+ async list(_options, _context) {
199
+ this.assertOpen();
200
+ let paths;
201
+ if (this.usesSharedDatabase()) {
202
+ paths = [this.databasePath];
203
+ }
204
+ else {
205
+ let names;
206
+ try {
207
+ names = await readdir(this.directory);
208
+ }
209
+ catch (error) {
210
+ if (isErrorWithCode(error, "ENOENT"))
211
+ return [];
212
+ throw error;
213
+ }
214
+ paths = names
215
+ .filter((name) => name.endsWith(SQLITE_SESSION_EXTENSION))
216
+ .map((name) => join(this.directory, name));
217
+ }
218
+ const sessions = [];
219
+ for (const path of paths) {
220
+ let db;
221
+ try {
222
+ const canonicalPath = await realpath(path);
223
+ db = await this.databaseFactory.openReadOnly(canonicalPath);
224
+ configureReadOnlyConnection(db);
225
+ for (const row of readAllSessionRows(db)) {
226
+ sessions.push(metadataFromSessionRow(canonicalPath, row, SQLITE_STORAGE_VERSION));
227
+ }
228
+ }
229
+ catch {
230
+ // Discovery is best-effort: corrupt files, incompatible versions, and
231
+ // unrelated *.sqlite files are reported when explicitly opened.
232
+ }
233
+ finally {
234
+ db?.close();
235
+ }
236
+ }
237
+ return sessions.sort((left, right) => right.createdAt - left.createdAt);
238
+ }
239
+ async delete(metadata, _context) {
240
+ this.assertOpen();
241
+ this.reserveId(metadata.id);
242
+ try {
243
+ const path = await this.repositoryPathForMetadata(metadata);
244
+ const db = await this.databaseFactory.openExisting(path);
245
+ try {
246
+ configureWritableConnection(db);
247
+ if (this.usesSharedDatabase()) {
248
+ db.transaction(() => {
249
+ metadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);
250
+ deleteSessionRows(db, metadata.id);
251
+ });
252
+ }
253
+ else {
254
+ metadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);
255
+ }
256
+ }
257
+ finally {
258
+ db.close();
259
+ }
260
+ if (!this.usesSharedDatabase())
261
+ await removeSessionFiles(path, { force: false });
262
+ }
263
+ finally {
264
+ this.pendingIds.delete(metadata.id);
265
+ }
266
+ }
267
+ async fork(source, options, context) {
268
+ this.assertOpen();
269
+ const createdAt = this.now();
270
+ const id = options.id ?? uuidv7(createdAt);
271
+ this.reserveId(id);
272
+ const sourceStorage = this.openStorages.get(storageIdentity(source.path, source.id));
273
+ const activeSourceSnapshot = sourceStorage?.snapshot(options, context);
274
+ void activeSourceSnapshot?.catch(() => undefined);
275
+ const path = this.pathForSession(id);
276
+ let db;
277
+ let reservedFile = false;
278
+ let initialized = false;
279
+ let session;
280
+ try {
281
+ await mkdir(dirname(path), { recursive: true });
282
+ if (!this.usesSharedDatabase()) {
283
+ const file = await openFile(path, "wx");
284
+ await file.close();
285
+ reservedFile = true;
286
+ }
287
+ const snapshot = activeSourceSnapshot === undefined
288
+ ? await this.createForkSnapshotFromExternalSource(source, options)
289
+ : buildForkSnapshot(await activeSourceSnapshot, options);
290
+ const activeDb = await this.databaseFactory.open(path);
291
+ db = activeDb;
292
+ configureWritableConnection(activeDb);
293
+ await applyInitialSchema(activeDb);
294
+ const canonicalPath = await realpath(path);
295
+ const metadata = {
296
+ id,
297
+ createdAt,
298
+ storageVersion: SQLITE_STORAGE_VERSION,
299
+ parentSessionId: source.id,
300
+ path: canonicalPath,
301
+ };
302
+ activeDb.transaction(() => {
303
+ if (hasSessionRow(activeDb, id))
304
+ throw new Error(`SQLite session already exists: ${id}`);
305
+ insertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, snapshot.nextSeq);
306
+ const entryWriter = new EntryRowWriter(activeDb, id);
307
+ for (const entry of snapshot.entries) {
308
+ entryWriter.insert(entry);
309
+ appendEntryToBranchIndex(activeDb, id, entry);
310
+ }
311
+ for (const stored of snapshot.scalarValues)
312
+ insertForkValue(activeDb, id, stored);
313
+ updateForkSessionStats(activeDb, id, snapshot.messageCount);
314
+ });
315
+ initialized = true;
316
+ session = this.openStorageBackedSession(metadata, activeDb);
317
+ return session;
318
+ }
319
+ catch (error) {
320
+ if (reservedFile && !initialized)
321
+ await removeSessionFiles(path, { force: true });
322
+ throw error;
323
+ }
324
+ finally {
325
+ if (session === undefined) {
326
+ try {
327
+ db?.close();
328
+ }
329
+ finally {
330
+ this.pendingIds.delete(id);
331
+ }
332
+ }
333
+ }
334
+ }
335
+ close(context) {
336
+ if (this.closePromise !== undefined)
337
+ return this.closePromise;
338
+ this.closed = true;
339
+ this.closePromise = this.closeOpenSessions(context);
340
+ return this.closePromise;
341
+ }
342
+ async createForkSnapshotFromExternalSource(source, options) {
343
+ const path = await realpath(source.path);
344
+ const sourceDb = await this.databaseFactory.openReadOnly(path);
345
+ try {
346
+ configureReadOnlyConnection(sourceDb);
347
+ return createSqliteForkSnapshot(sourceDb, { ...source, path }, options);
348
+ }
349
+ finally {
350
+ sourceDb.close();
351
+ }
352
+ }
353
+ async closeOpenSessions(context) {
354
+ const results = await Promise.allSettled([...this.openSessions].map((session) => session.close(context)));
355
+ const errors = results.flatMap((result) => (result.status === "rejected" ? [result.reason] : []));
356
+ if (errors.length === 1)
357
+ throw errors[0];
358
+ if (errors.length > 1)
359
+ throw new AggregateError(errors, "Failed to close SQLite Sessions");
360
+ }
361
+ openStorageBackedSession(metadata, db) {
362
+ const key = storageIdentity(metadata.path, metadata.id);
363
+ const storage = new SqliteStorage(db, { sessionId: metadata.id, now: this.now });
364
+ this.openStorages.set(key, storage);
365
+ const session = new StorageBackedSession(metadata, storage);
366
+ const openSession = new SqliteOpenSession(session, {
367
+ onClose: () => {
368
+ try {
369
+ db.close();
370
+ }
371
+ finally {
372
+ if (this.openStorages.get(key) === storage)
373
+ this.openStorages.delete(key);
374
+ this.openSessions.delete(openSession);
375
+ this.pendingIds.delete(metadata.id);
376
+ }
377
+ },
378
+ });
379
+ this.openSessions.add(openSession);
380
+ return openSession;
381
+ }
382
+ async repositoryPathForMetadata(metadata) {
383
+ const [expected, actual] = await Promise.all([
384
+ realpath(this.pathForSession(metadata.id)),
385
+ realpath(metadata.path),
386
+ ]);
387
+ if (expected !== actual) {
388
+ throw new Error(`SQLite session metadata path is outside this repository: ${metadata.path}`);
389
+ }
390
+ return actual;
391
+ }
392
+ reserveId(id) {
393
+ if (this.pendingIds.has(id))
394
+ throw new Error(`Session is already open: ${id}`);
395
+ this.pendingIds.add(id);
396
+ }
397
+ pathForSession(id) {
398
+ return this.databasePath ?? sessionPath(this.directory, id);
399
+ }
400
+ usesSharedDatabase() {
401
+ return this.databasePath !== undefined;
402
+ }
403
+ assertOpen() {
404
+ if (this.closed)
405
+ throw new Error("SqliteSessionRepo is closed");
406
+ }
407
+ }
408
+ //# sourceMappingURL=repo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repo.js","sourceRoot":"","sources":["../../src/sqlite/repo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,IAAI,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACpG,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAC1F,OAAO,EAAE,cAAc,EAAiB,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,EACN,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,sBAAsB,EACtB,kBAAkB,EAClB,cAAc,GAEd,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,aAAa,EAA8B,MAAM,cAAc,CAAC;AAGzE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AACxC,MAAM,CAAC,MAAM,wBAAwB,GAAG,SAAS,CAAC;AAElD,MAAM,0BAA0B,GAAG,CAAC,CAAC;AACrC,MAAM,oBAAoB,GAAG,kB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,OAAO,WAAW,CAAC;IAAA,CACnB;IAEO,KAAK,CAAC,yBAAyB,CAAC,QAA+B,EAAmB;QACzF,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YAC5C,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;YAC1C,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;SACvB,CAAC,CAAC;QACH,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,4DAA4D,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9F,CAAC;QACD,OAAO,MAAM,CAAC;IAAA,CACd;IAEO,SAAS,CAAC,EAAU,EAAQ;QACnC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,EAAE,EAAE,CAAC,CAAC;QAC/E,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAAA,CACxB;IAEO,cAAc,CAAC,EAAU,EAAU;QAC1C,OAAO,IAAI,CAAC,YAAY,IAAI,WAAW,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAAA,CAC5D;IAEO,kBAAkB,GAAY;QACrC,OAAO,IAAI,CAAC,YAAY,KAAK,SAAS,CAAC;IAAA,CACvC;IAEO,UAAU,GAAS;QAC1B,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IAAA,CAChE;CACD","sourcesContent":["import { mkdir, open as openFile, readdir, realpath, rm } from \"node:fs/promises\";\nimport { dirname, join } from \"node:path\";\nimport type { Context, Entry, ForkOptions, SessionCreateOptions, StoredValue } from \"@earendil-works/pi-agent-core\";\nimport { branchTip, createForkSnapshot, StorageBackedSession } from \"@earendil-works/pi-agent-core\";\nimport { uuidv7 } from \"@earendil-works/pi-ai\";\nimport { applyInitialSchema } from \"./migrations.ts\";\nimport { appendEntryToBranchIndex, scanBranchEntries } from \"./session/branch-entries.ts\";\nimport { decodeEntryRow, type EntryRow, EntryRowWriter } from \"./session/entries.ts\";\nimport {\n\tdeleteSessionRows,\n\thasSessionRow,\n\tinsertSessionRow,\n\tmetadataFromSessionRow,\n\treadAllSessionRows,\n\treadSessionRow,\n\ttype SqliteSessionMetadata,\n} from \"./session/session-row.ts\";\nimport { readAllScalarValueRows, setScalarValueRow } from \"./session/values.ts\";\nimport { SqliteOpenSession } from \"./session.ts\";\nimport { sql } from \"./sql.ts\";\nimport { SqliteStorage, type SqliteStorageSnapshot } from \"./storage.ts\";\nimport type { SqliteDatabase, SqliteDatabaseFactory } from \"./types.ts\";\n\nexport const SQLITE_STORAGE_VERSION = 1;\nexport const SQLITE_SESSION_EXTENSION = \".sqlite\";\n\nconst FIRST_AVAILABLE_COMMIT_SEQ = 1;\nconst SAFE_SESSION_FILE_ID = /^[A-Za-z0-9_-]+$/;\n\nexport type SqliteSessionCreateOptions = SessionCreateOptions;\n\nexport interface SqliteSessionRepoOptions {\n\tdirectory: string;\n\t/** Optional single container path. Defaults to one encoded `${id}.sqlite` file per session under directory. */\n\tdatabasePath?: string;\n\tdatabaseFactory: SqliteDatabaseFactory;\n\tnow?: () => number;\n}\n\nfunction sessionFileName(id: string): string {\n\tif (SAFE_SESSION_FILE_ID.test(id)) return `${id}${SQLITE_SESSION_EXTENSION}`;\n\tconst encoded = Buffer.from(id, \"utf16le\").toString(\"base64url\");\n\treturn `~${encoded}${SQLITE_SESSION_EXTENSION}`;\n}\n\nfunction sessionPath(directory: string, id: string): string {\n\treturn join(directory, sessionFileName(id));\n}\n\nfunction storageIdentity(path: string, sessionId: string): string {\n\treturn JSON.stringify([path, sessionId]);\n}\n\nfunction isErrorWithCode(error: unknown, code: string): boolean {\n\treturn typeof error === \"object\" && error !== null && \"code\" in error && error.code === code;\n}\n\nasync function removeSessionFiles(path: string, options: { force: boolean }): Promise<void> {\n\tawait rm(path, { force: options.force });\n\tawait rm(`${path}-wal`, { force: true });\n\tawait rm(`${path}-shm`, { force: true });\n}\n\nfunction configureWritableConnection(db: SqliteDatabase): void {\n\tdb.exec(\"PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;\");\n}\n\nfunction configureReadOnlyConnection(db: SqliteDatabase): void {\n\tdb.exec(\"PRAGMA busy_timeout = 5000;\");\n}\n\ninterface ForkSnapshot {\n\tentries: Entry[];\n\tscalarValues: StoredValue<unknown>[];\n\tmessageCount: number;\n\tnextSeq: number;\n}\n\nfunction readSourceEntries(db: SqliteDatabase, sessionId: string): Entry[] {\n\treturn sql`SELECT id, parent_id, seq, type, custom_type, timestamp, payload\n\t\tFROM entries WHERE session_id = ${sessionId} ORDER BY seq ASC`\n\t\t.all<EntryRow>(db)\n\t\t.map(decodeEntryRow);\n}\n\nfunction buildForkSnapshot(source: SqliteStorageSnapshot, options: ForkOptions): ForkSnapshot {\n\tconst snapshot = createForkSnapshot(\n\t\t{\n\t\t\tentries: source.entries,\n\t\t\tscalarValues: source.scalarValues,\n\t\t\tentriesComplete: source.entriesComplete,\n\t\t},\n\t\toptions,\n\t);\n\tconst entries = [...snapshot.entries.values()].sort((left, right) => left.seq - right.seq);\n\treturn {\n\t\tentries,\n\t\tscalarValues: snapshot.scalarValues,\n\t\tmessageCount: entries.filter((entry) => entry.type === \"message\").length,\n\t\tnextSeq: snapshot.nextSeq,\n\t};\n}\n\n// TODO(WP08): Remove this snapshot path when SQLite forks use streaming staging.\nfunction readForkSourceEntries(\n\tdb: SqliteDatabase,\n\tsessionId: string,\n\tscalarValues: readonly StoredValue<unknown>[],\n\toptions: ForkOptions,\n): Entry[] {\n\tif (options.scope === \"tree\") return readSourceEntries(db, sessionId);\n\tconst sourceAddress = branchTip(options.branch);\n\tconst sourceTip = scalarValues.find(\n\t\t(stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key,\n\t) as StoredValue<string | null> | undefined;\n\tif (sourceTip === undefined) throw new Error(`Unknown source branch: ${options.branch}`);\n\treturn sourceTip.value === null\n\t\t? []\n\t\t: scanBranchEntries(db, sessionId, { start: sourceTip.value, order: \"oldestFirst\" });\n}\n\nfunction createSqliteForkSnapshot(\n\tsourceDb: SqliteDatabase,\n\tsource: SqliteSessionMetadata,\n\toptions: ForkOptions,\n): ForkSnapshot {\n\tsourceDb.exec(\"BEGIN\");\n\tlet committed = false;\n\ttry {\n\t\tmetadataFromSessionRow(source.path, readSessionRow(sourceDb, source.id), SQLITE_STORAGE_VERSION);\n\t\tconst scalarValues = readAllScalarValueRows(sourceDb, source.id);\n\t\tconst snapshot = buildForkSnapshot(\n\t\t\t{\n\t\t\t\tentries: readForkSourceEntries(sourceDb, source.id, scalarValues, options),\n\t\t\t\tscalarValues,\n\t\t\t\tentriesComplete: options.scope === \"tree\",\n\t\t\t},\n\t\t\toptions,\n\t\t);\n\t\tsourceDb.exec(\"COMMIT\");\n\t\tcommitted = true;\n\t\treturn snapshot;\n\t} catch (error) {\n\t\tif (!committed) sourceDb.exec(\"ROLLBACK\");\n\t\tthrow error;\n\t}\n}\n\nfunction insertForkValue(db: SqliteDatabase, sessionId: string, stored: StoredValue<unknown>): void {\n\tsetScalarValueRow(db, sessionId, stored.address.namespace, stored.address.key, stored.seq, stored.value);\n}\n\nfunction updateForkSessionStats(db: SqliteDatabase, sessionId: string, messageCount: number): void {\n\tsql`UPDATE sessions SET message_count = ${messageCount} WHERE id = ${sessionId}`.run(db);\n}\n\nexport class SqliteSessionRepo {\n\tprivate readonly directory: string;\n\tprivate readonly databasePath: string | undefined;\n\tprivate readonly databaseFactory: SqliteDatabaseFactory;\n\tprivate readonly now: () => number;\n\tprivate readonly pendingIds = new Set<string>();\n\tprivate readonly openStorages = new Map<string, SqliteStorage>();\n\tprivate readonly openSessions = new Set<SqliteOpenSession>();\n\tprivate closed = false;\n\tprivate closePromise: Promise<void> | undefined;\n\n\tconstructor(options: SqliteSessionRepoOptions) {\n\t\tthis.directory = options.directory;\n\t\tthis.databasePath = options.databasePath;\n\t\tthis.databaseFactory = options.databaseFactory;\n\t\tthis.now = options.now ?? Date.now;\n\t}\n\n\tasync create(options: SqliteSessionCreateOptions | undefined, _context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\toptions ??= {};\n\t\tconst createdAt = this.now();\n\t\tconst id = options.id ?? uuidv7(createdAt);\n\t\tthis.reserveId(id);\n\t\tconst path = this.pathForSession(id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet reservedFile = false;\n\t\tlet initialized = false;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tawait mkdir(dirname(path), { recursive: true });\n\t\t\tif (!this.usesSharedDatabase()) {\n\t\t\t\tconst file = await openFile(path, \"wx\");\n\t\t\t\tawait file.close();\n\t\t\t\treservedFile = true;\n\t\t\t}\n\t\t\tconst activeDb = await this.databaseFactory.open(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tawait applyInitialSchema(activeDb);\n\t\t\tconst canonicalPath = await realpath(path);\n\t\t\tconst metadata: SqliteSessionMetadata = {\n\t\t\t\tid,\n\t\t\t\tcreatedAt,\n\t\t\t\tstorageVersion: SQLITE_STORAGE_VERSION,\n\t\t\t\t...(options.parentSessionId === undefined ? {} : { parentSessionId: options.parentSessionId }),\n\t\t\t\tpath: canonicalPath,\n\t\t\t};\n\t\t\tactiveDb.transaction(() => {\n\t\t\t\tif (hasSessionRow(activeDb, id)) throw new Error(`SQLite session already exists: ${id}`);\n\t\t\t\tinsertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, FIRST_AVAILABLE_COMMIT_SEQ);\n\t\t\t});\n\t\t\tinitialized = true;\n\t\t\tsession = this.openStorageBackedSession(metadata, activeDb);\n\t\t\treturn session;\n\t\t} catch (error) {\n\t\t\tif (reservedFile && !initialized) await removeSessionFiles(path, { force: true });\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tasync open(metadata: SqliteSessionMetadata, _context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\tthis.reserveId(metadata.id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tconst path = await this.repositoryPathForMetadata(metadata);\n\t\t\tconst activeDb = await this.databaseFactory.openExisting(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tconst stored = metadataFromSessionRow(path, readSessionRow(activeDb, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\tsession = this.openStorageBackedSession(stored, activeDb);\n\t\t\treturn session;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tasync list(_options: undefined, _context: Context): Promise<SqliteSessionMetadata[]> {\n\t\tthis.assertOpen();\n\t\tlet paths: string[];\n\t\tif (this.usesSharedDatabase()) {\n\t\t\tpaths = [this.databasePath!];\n\t\t} else {\n\t\t\tlet names: string[];\n\t\t\ttry {\n\t\t\t\tnames = await readdir(this.directory);\n\t\t\t} catch (error) {\n\t\t\t\tif (isErrorWithCode(error, \"ENOENT\")) return [];\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tpaths = names\n\t\t\t\t.filter((name) => name.endsWith(SQLITE_SESSION_EXTENSION))\n\t\t\t\t.map((name) => join(this.directory, name));\n\t\t}\n\t\tconst sessions: SqliteSessionMetadata[] = [];\n\t\tfor (const path of paths) {\n\t\t\tlet db: SqliteDatabase | undefined;\n\t\t\ttry {\n\t\t\t\tconst canonicalPath = await realpath(path);\n\t\t\t\tdb = await this.databaseFactory.openReadOnly(canonicalPath);\n\t\t\t\tconfigureReadOnlyConnection(db);\n\t\t\t\tfor (const row of readAllSessionRows(db)) {\n\t\t\t\t\tsessions.push(metadataFromSessionRow(canonicalPath, row, SQLITE_STORAGE_VERSION));\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Discovery is best-effort: corrupt files, incompatible versions, and\n\t\t\t\t// unrelated *.sqlite files are reported when explicitly opened.\n\t\t\t} finally {\n\t\t\t\tdb?.close();\n\t\t\t}\n\t\t}\n\t\treturn sessions.sort((left, right) => right.createdAt - left.createdAt);\n\t}\n\n\tasync delete(metadata: SqliteSessionMetadata, _context: Context): Promise<void> {\n\t\tthis.assertOpen();\n\t\tthis.reserveId(metadata.id);\n\t\ttry {\n\t\t\tconst path = await this.repositoryPathForMetadata(metadata);\n\t\t\tconst db = await this.databaseFactory.openExisting(path);\n\t\t\ttry {\n\t\t\t\tconfigureWritableConnection(db);\n\t\t\t\tif (this.usesSharedDatabase()) {\n\t\t\t\t\tdb.transaction(() => {\n\t\t\t\t\t\tmetadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\t\t\t\tdeleteSessionRows(db, metadata.id);\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tmetadataFromSessionRow(path, readSessionRow(db, metadata.id), SQLITE_STORAGE_VERSION);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tdb.close();\n\t\t\t}\n\t\t\tif (!this.usesSharedDatabase()) await removeSessionFiles(path, { force: false });\n\t\t} finally {\n\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t}\n\t}\n\n\tasync fork(source: SqliteSessionMetadata, options: ForkOptions, context: Context): Promise<SqliteOpenSession> {\n\t\tthis.assertOpen();\n\t\tconst createdAt = this.now();\n\t\tconst id = options.id ?? uuidv7(createdAt);\n\t\tthis.reserveId(id);\n\t\tconst sourceStorage = this.openStorages.get(storageIdentity(source.path, source.id));\n\t\tconst activeSourceSnapshot = sourceStorage?.snapshot(options, context);\n\t\tvoid activeSourceSnapshot?.catch(() => undefined);\n\t\tconst path = this.pathForSession(id);\n\t\tlet db: SqliteDatabase | undefined;\n\t\tlet reservedFile = false;\n\t\tlet initialized = false;\n\t\tlet session: SqliteOpenSession | undefined;\n\t\ttry {\n\t\t\tawait mkdir(dirname(path), { recursive: true });\n\t\t\tif (!this.usesSharedDatabase()) {\n\t\t\t\tconst file = await openFile(path, \"wx\");\n\t\t\t\tawait file.close();\n\t\t\t\treservedFile = true;\n\t\t\t}\n\n\t\t\tconst snapshot =\n\t\t\t\tactiveSourceSnapshot === undefined\n\t\t\t\t\t? await this.createForkSnapshotFromExternalSource(source, options)\n\t\t\t\t\t: buildForkSnapshot(await activeSourceSnapshot, options);\n\n\t\t\tconst activeDb = await this.databaseFactory.open(path);\n\t\t\tdb = activeDb;\n\t\t\tconfigureWritableConnection(activeDb);\n\t\t\tawait applyInitialSchema(activeDb);\n\t\t\tconst canonicalPath = await realpath(path);\n\t\t\tconst metadata: SqliteSessionMetadata = {\n\t\t\t\tid,\n\t\t\t\tcreatedAt,\n\t\t\t\tstorageVersion: SQLITE_STORAGE_VERSION,\n\t\t\t\tparentSessionId: source.id,\n\t\t\t\tpath: canonicalPath,\n\t\t\t};\n\t\t\tactiveDb.transaction(() => {\n\t\t\t\tif (hasSessionRow(activeDb, id)) throw new Error(`SQLite session already exists: ${id}`);\n\t\t\t\tinsertSessionRow(activeDb, metadata, SQLITE_STORAGE_VERSION, snapshot.nextSeq);\n\t\t\t\tconst entryWriter = new EntryRowWriter(activeDb, id);\n\t\t\t\tfor (const entry of snapshot.entries) {\n\t\t\t\t\tentryWriter.insert(entry);\n\t\t\t\t\tappendEntryToBranchIndex(activeDb, id, entry);\n\t\t\t\t}\n\t\t\t\tfor (const stored of snapshot.scalarValues) insertForkValue(activeDb, id, stored);\n\t\t\t\tupdateForkSessionStats(activeDb, id, snapshot.messageCount);\n\t\t\t});\n\t\t\tinitialized = true;\n\t\t\tsession = this.openStorageBackedSession(metadata, activeDb);\n\t\t\treturn session;\n\t\t} catch (error) {\n\t\t\tif (reservedFile && !initialized) await removeSessionFiles(path, { force: true });\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tif (session === undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tdb?.close();\n\t\t\t\t} finally {\n\t\t\t\t\tthis.pendingIds.delete(id);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tclose(context: Context): Promise<void> {\n\t\tif (this.closePromise !== undefined) return this.closePromise;\n\t\tthis.closed = true;\n\t\tthis.closePromise = this.closeOpenSessions(context);\n\t\treturn this.closePromise;\n\t}\n\n\tprivate async createForkSnapshotFromExternalSource(\n\t\tsource: SqliteSessionMetadata,\n\t\toptions: ForkOptions,\n\t): Promise<ForkSnapshot> {\n\t\tconst path = await realpath(source.path);\n\t\tconst sourceDb = await this.databaseFactory.openReadOnly(path);\n\t\ttry {\n\t\t\tconfigureReadOnlyConnection(sourceDb);\n\t\t\treturn createSqliteForkSnapshot(sourceDb, { ...source, path }, options);\n\t\t} finally {\n\t\t\tsourceDb.close();\n\t\t}\n\t}\n\n\tprivate async closeOpenSessions(context: Context): Promise<void> {\n\t\tconst results = await Promise.allSettled([...this.openSessions].map((session) => session.close(context)));\n\t\tconst errors = results.flatMap((result) => (result.status === \"rejected\" ? [result.reason] : []));\n\t\tif (errors.length === 1) throw errors[0];\n\t\tif (errors.length > 1) throw new AggregateError(errors, \"Failed to close SQLite Sessions\");\n\t}\n\n\tprivate openStorageBackedSession(metadata: SqliteSessionMetadata, db: SqliteDatabase): SqliteOpenSession {\n\t\tconst key = storageIdentity(metadata.path, metadata.id);\n\t\tconst storage = new SqliteStorage(db, { sessionId: metadata.id, now: this.now });\n\t\tthis.openStorages.set(key, storage);\n\t\tconst session = new StorageBackedSession(metadata, storage);\n\t\tconst openSession = new SqliteOpenSession(session, {\n\t\t\tonClose: () => {\n\t\t\t\ttry {\n\t\t\t\t\tdb.close();\n\t\t\t\t} finally {\n\t\t\t\t\tif (this.openStorages.get(key) === storage) this.openStorages.delete(key);\n\t\t\t\t\tthis.openSessions.delete(openSession);\n\t\t\t\t\tthis.pendingIds.delete(metadata.id);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tthis.openSessions.add(openSession);\n\t\treturn openSession;\n\t}\n\n\tprivate async repositoryPathForMetadata(metadata: SqliteSessionMetadata): Promise<string> {\n\t\tconst [expected, actual] = await Promise.all([\n\t\t\trealpath(this.pathForSession(metadata.id)),\n\t\t\trealpath(metadata.path),\n\t\t]);\n\t\tif (expected !== actual) {\n\t\t\tthrow new Error(`SQLite session metadata path is outside this repository: ${metadata.path}`);\n\t\t}\n\t\treturn actual;\n\t}\n\n\tprivate reserveId(id: string): void {\n\t\tif (this.pendingIds.has(id)) throw new Error(`Session is already open: ${id}`);\n\t\tthis.pendingIds.add(id);\n\t}\n\n\tprivate pathForSession(id: string): string {\n\t\treturn this.databasePath ?? sessionPath(this.directory, id);\n\t}\n\n\tprivate usesSharedDatabase(): boolean {\n\t\treturn this.databasePath !== undefined;\n\t}\n\n\tprivate assertOpen(): void {\n\t\tif (this.closed) throw new Error(\"SqliteSessionRepo is closed\");\n\t}\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import type { SessionSearch, SessionSearchHit } from "@earendil-works/pi-agent-core";
2
+ import type { SqliteDatabaseFactory, SqliteSessionMetadata, SqliteSessionRepositoryEnv } from "./types.ts";
3
+ export interface SqliteSessionSearchOptions {
4
+ env: Pick<SqliteSessionRepositoryEnv, "absolutePath" | "createDir">;
5
+ sqlite: SqliteDatabaseFactory;
6
+ databasePath: string;
7
+ }
8
+ export interface SqliteSessionSearchHit extends SessionSearchHit {
9
+ readonly metadata: SqliteSessionMetadata;
10
+ readonly timestamp: number;
11
+ readonly score: number;
12
+ }
13
+ export declare function createSqliteSessionSearch(options: SqliteSessionSearchOptions): SessionSearch<SqliteSessionSearchHit>;
14
+ //# sourceMappingURL=search-backend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search-backend.d.ts","sourceRoot":"","sources":["../../src/sqlite/search-backend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,aAAa,EACb,gBAAgB,EAEhB,MAAM,+BAA+B,CAAC;AAKvC,OAAO,KAAK,EAEX,qBAAqB,EACrB,qBAAqB,EACrB,0BAA0B,EAC1B,MAAM,YAAY,CAAC;AAgCpB,MAAM,WAAW,0BAA0B;IAC1C,GAAG,EAAE,IAAI,CAAC,0BAA0B,EAAE,cAAc,GAAG,WAAW,CAAC,CAAC;IACpE,MAAM,EAAE,qBAAqB,CAAC;IAC9B,YAAY,EAAE,MAAM,CAAC;CACrB;AAsCD,MAAM,WAAW,sBAAuB,SAAQ,gBAAgB;IAC/D,QAAQ,CAAC,QAAQ,EAAE,qBAAqB,CAAC;IACzC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAiGD,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,0BAA0B,GAAG,aAAa,CAAC,sBAAsB,CAAC,CAEpH","sourcesContent":["import type {\n\tFileError,\n\tResult,\n\tSessionSearch,\n\tSessionSearchHit,\n\tSessionSearchOptions,\n} from \"@earendil-works/pi-agent-core\";\nimport { SessionError } from \"@earendil-works/pi-agent-core\";\nimport { applyMigrations } from \"./migrations.ts\";\nimport { sql } from \"./sql.ts\";\nimport { decodeSessionMetadata, type SessionRow } from \"./storage/sessions.ts\";\nimport type {\n\tSqliteDatabase,\n\tSqliteDatabaseFactory,\n\tSqliteSessionMetadata,\n\tSqliteSessionRepositoryEnv,\n} from \"./types.ts\";\n\nfunction getFileSystemResultOrThrow<TValue>(result: Result<TValue, FileError>, message: string): TValue {\n\tif (!result.ok) {\n\t\tconst code = result.error.code === \"not_found\" ? \"not_found\" : \"storage\";\n\t\tthrow new SessionError(code, `${message}: ${result.error.message}`, result.error);\n\t}\n\treturn result.value;\n}\n\nfunction getParentPath(path: string): string {\n\tconst normalized = path.replace(/[\\\\/]+$/, \"\");\n\tconst lastSlash = Math.max(normalized.lastIndexOf(\"/\"), normalized.lastIndexOf(\"\\\\\"));\n\tif (lastSlash < 0) return \".\";\n\tif (lastSlash === 0) return normalized.slice(0, 1);\n\treturn normalized.slice(0, lastSlash);\n}\n\nfunction throwIfAborted(signal: AbortSignal | undefined): void {\n\tif (!signal?.aborted) return;\n\tif (signal.reason instanceof Error) throw signal.reason;\n\tconst error = new Error(\"The operation was aborted\");\n\terror.name = \"AbortError\";\n\tthrow error;\n}\n\nfunction configureSqliteDatabase(db: SqliteDatabase): void {\n\tsql`PRAGMA journal_mode=WAL`.exec(db);\n\tsql`PRAGMA synchronous=FULL`.exec(db);\n\tsql`PRAGMA busy_timeout=5000`.exec(db);\n}\n\nexport interface SqliteSessionSearchOptions {\n\tenv: Pick<SqliteSessionRepositoryEnv, \"absolutePath\" | \"createDir\">;\n\tsqlite: SqliteDatabaseFactory;\n\tdatabasePath: string;\n}\n\nfunction tableExists(db: SqliteDatabase, name: string): boolean {\n\treturn !!sql`SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = ${name} LIMIT 1`.get<{\n\t\tfound: number;\n\t}>(db);\n}\n\nfunction rebuildSearchIndex(db: SqliteDatabase): void {\n\tsql`INSERT INTO session_search_fts(session_search_fts) VALUES('rebuild')`.run(db);\n}\n\nfunction ensureSearchSchema(db: SqliteDatabase): void {\n\tconst ftsExists = tableExists(db, \"session_search_fts\");\n\tconst entriesExist = tableExists(db, \"entries\");\n\tdb.transaction(() => {\n\t\tsql`\nCREATE VIRTUAL TABLE IF NOT EXISTS session_search_fts USING fts5(\n payload,\n content = 'entries',\n content_rowid = 'rowid',\n tokenize = 'trigram remove_diacritics 1'\n);\nCREATE TRIGGER IF NOT EXISTS session_search_fts_ai AFTER INSERT ON entries BEGIN\n INSERT INTO session_search_fts(rowid, payload) VALUES (new.rowid, new.payload);\nEND;\nCREATE TRIGGER IF NOT EXISTS session_search_fts_ad AFTER DELETE ON entries BEGIN\n INSERT INTO session_search_fts(session_search_fts, rowid, payload) VALUES('delete', old.rowid, old.payload);\nEND;\nCREATE TRIGGER IF NOT EXISTS session_search_fts_au AFTER UPDATE OF payload ON entries BEGIN\n INSERT INTO session_search_fts(session_search_fts, rowid, payload) VALUES('delete', old.rowid, old.payload);\n INSERT INTO session_search_fts(rowid, payload) VALUES (new.rowid, new.payload);\nEND;\n`.exec(db);\n\t\tif (!ftsExists && entriesExist) rebuildSearchIndex(db);\n\t});\n}\n\nexport interface SqliteSessionSearchHit extends SessionSearchHit {\n\treadonly metadata: SqliteSessionMetadata;\n\treadonly timestamp: number;\n\treadonly score: number;\n}\n\n/** SQLite FTS search over a co-located canonical session database. */\nclass SqliteSessionSearch implements SessionSearch<SqliteSessionSearchHit> {\n\tprivate readonly options: SqliteSessionSearchOptions;\n\tprivate databasePath: string | undefined;\n\n\tconstructor(options: SqliteSessionSearchOptions) {\n\t\tthis.options = options;\n\t}\n\n\tprivate async getDatabasePath(): Promise<string> {\n\t\tif (!this.databasePath) {\n\t\t\tthis.databasePath = getFileSystemResultOrThrow(\n\t\t\t\tawait this.options.env.absolutePath(this.options.databasePath),\n\t\t\t\t`Failed to resolve SQLite search database ${this.options.databasePath}`,\n\t\t\t);\n\t\t}\n\t\treturn this.databasePath;\n\t}\n\n\tprivate async openDatabase(): Promise<SqliteDatabase> {\n\t\tconst path = await this.getDatabasePath();\n\t\tconst directory = getParentPath(path);\n\t\tgetFileSystemResultOrThrow(\n\t\t\tawait this.options.env.createDir(directory, { recursive: true }),\n\t\t\t`Failed to create SQLite search directory ${directory}`,\n\t\t);\n\t\tconst db = await this.options.sqlite.open(path);\n\t\ttry {\n\t\t\tconfigureSqliteDatabase(db);\n\t\t\tawait applyMigrations(db);\n\t\t\tensureSearchSchema(db);\n\t\t\treturn db;\n\t\t} catch (error) {\n\t\t\tdb.close();\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync *search(text: string, options: SessionSearchOptions = {}): AsyncIterable<SqliteSessionSearchHit> {\n\t\tconst queryText = text.trim();\n\t\tif (!queryText || (options.limit !== undefined && options.limit <= 0)) return;\n\t\tif (options.entryTypes?.length === 0) return;\n\t\tthrowIfAborted(options.signal);\n\t\tconst db = await this.openDatabase();\n\t\ttry {\n\t\t\tconst query = `\"${queryText.replaceAll('\"', '\"\"')}\"`;\n\t\t\tconst predicates = [\"session_search_fts MATCH ?\"];\n\t\t\tconst params: unknown[] = [query];\n\t\t\tif (options.entryTypes !== undefined) {\n\t\t\t\tpredicates.push(`se.type IN (${options.entryTypes.map(() => \"?\").join(\", \")})`);\n\t\t\t\tparams.push(...options.entryTypes);\n\t\t\t}\n\t\t\tconst rows = db\n\t\t\t\t.prepare(\n\t\t\t\t\t`SELECT s.id, s.created_at, s.metadata, s.cwd, s.parent_session_id,\n\t\t\t\t\t\tname_fact.seq IS NOT NULL AS has_session_name,\n\t\t\t\t\t\tname_fact.value AS session_name,\n\t\t\t\t\t\tse.id AS entry_id, se.timestamp, bm25(session_search_fts) AS score\n\t\t\t\t\tFROM session_search_fts\n\t\t\t\t\tJOIN entries AS se ON se.rowid = session_search_fts.rowid\n\t\t\t\t\tJOIN sessions AS s ON s.id = se.session_id\n\t\t\t\t\tLEFT JOIN facts AS name_fact\n\t\t\t\t\t\tON name_fact.session_id = s.id\n\t\t\t\t\t\tAND name_fact.kind = 'name'\n\t\t\t\t\t\tAND name_fact.key IS NULL\n\t\t\t\t\t\tAND name_fact.seq = (\n\t\t\t\t\t\t\tSELECT MAX(f.seq)\n\t\t\t\t\t\t\tFROM facts AS f\n\t\t\t\t\t\t\tWHERE f.session_id = s.id AND f.kind = 'name' AND f.key IS NULL\n\t\t\t\t\t\t)\n\t\t\t\t\tWHERE ${predicates.join(\" AND \")}\n\t\t\t\t\tORDER BY score\n\t\t\t\t\tLIMIT ?`,\n\t\t\t\t)\n\t\t\t\t.iterate<SessionRow & { entry_id: string; timestamp: number; score: number }>(\n\t\t\t\t\t...params,\n\t\t\t\t\toptions.limit ?? -1,\n\t\t\t\t);\n\t\t\tconst path = await this.getDatabasePath();\n\t\t\tfor (const row of rows) {\n\t\t\t\tthrowIfAborted(options.signal);\n\t\t\t\tyield {\n\t\t\t\t\tsessionId: row.id,\n\t\t\t\t\tmetadata: decodeSessionMetadata(row, path),\n\t\t\t\t\tentryId: row.entry_id,\n\t\t\t\t\ttimestamp: row.timestamp,\n\t\t\t\t\tscore: row.score,\n\t\t\t\t};\n\t\t\t}\n\t\t} finally {\n\t\t\tdb.close();\n\t\t}\n\t}\n}\n\nexport function createSqliteSessionSearch(options: SqliteSessionSearchOptions): SessionSearch<SqliteSessionSearchHit> {\n\treturn new SqliteSessionSearch(options);\n}\n"]}