dal-code 1.0.8 → 1.0.10

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 (452) hide show
  1. package/CHANGELOG.md +39 -53
  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/package.json +1 -1
  440. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  441. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  442. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  443. package/examples/extensions/gondolin/package-lock.json +2 -2
  444. package/examples/extensions/gondolin/package.json +1 -1
  445. package/examples/extensions/official/package.json +1 -1
  446. package/examples/extensions/sandbox/package-lock.json +2 -2
  447. package/examples/extensions/sandbox/package.json +1 -1
  448. package/examples/extensions/with-deps/package-lock.json +2 -2
  449. package/examples/extensions/with-deps/package.json +1 -1
  450. package/examples/plugins/pi-example-plugin/package.json +1 -1
  451. package/npm-shrinkwrap.json +3 -14
  452. package/package.json +2 -2
@@ -0,0 +1,40 @@
1
+ import type { JsonValue, ServiceCall, ServiceProviderUpdate } from "@earendil-works/chord";
2
+ import { type Context, type SessionMetadata } from "@earendil-works/pi-agent-core";
3
+ import type { RpcTarget, SessionTarget } from "@earendil-works/pi-protocol";
4
+ import type { ServerHost } from "./types.ts";
5
+ interface SessionRouterOptions<TMetadata extends SessionMetadata> {
6
+ host: ServerHost<TMetadata>;
7
+ serverId: string;
8
+ isClosing: () => boolean;
9
+ publishAttachment(client: object, attachment: SessionTarget | undefined, context: Context): Promise<void>;
10
+ reportError: (error: unknown) => void;
11
+ }
12
+ export declare class SessionRouter<TMetadata extends SessionMetadata = SessionMetadata> {
13
+ private readonly options;
14
+ private readonly hostedSessions;
15
+ private readonly openingSessions;
16
+ private readonly attachmentsByClient;
17
+ private readonly disconnectedClients;
18
+ private readonly clientOperations;
19
+ private closePromise?;
20
+ constructor(options: SessionRouterOptions<TMetadata>);
21
+ executeServiceCall(call: ServiceCall, target: RpcTarget, client: object, publish: (subscriptionId: string, update: ServiceProviderUpdate, context: Context) => Promise<void>, context: Context): Promise<JsonValue | undefined>;
22
+ attachClient(client: object, sessionId: string, context: Context): Promise<void>;
23
+ detachClient(client: object, context: Context): Promise<void>;
24
+ removeSession(sessionId: string, context: Context): Promise<void>;
25
+ disconnect(client: object, context: Context): Promise<void>;
26
+ close(context: Context): Promise<void>;
27
+ private closeInternal;
28
+ private runForClient;
29
+ private attachClientNow;
30
+ private startServiceCall;
31
+ private trackOperation;
32
+ private requireAttachment;
33
+ private releaseAttachment;
34
+ private clearAttachment;
35
+ private acquire;
36
+ private open;
37
+ private invalidate;
38
+ }
39
+ export {};
40
+ //# sourceMappingURL=session-router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-router.d.ts","sourceRoot":"","sources":["../src/session-router.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAsB,KAAK,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACvG,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5E,OAAO,KAAK,EAAgD,UAAU,EAAE,MAAM,YAAY,CAAC;AAoB3F,UAAU,oBAAoB,CAAC,SAAS,SAAS,eAAe;IAC/D,IAAI,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,GAAG,SAAS,EA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,CAAC,CAUhE;IAED,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAGrC;YAEa,aAAa;IAsC3B,OAAO,CAAC,YAAY;YAcN,eAAe;YAuCf,gBAAgB;IAiB9B,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,iBAAiB;YAoBX,eAAe;YAQf,OAAO;YAcP,IAAI;IA0BlB,OAAO,CAAC,UAAU;CAUlB","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { JsonValue, ServiceCall, ServiceProviderUpdate } from \"@earendil-works/chord\";\nimport { BACKGROUND_CONTEXT, type Context, type SessionMetadata } from \"@earendil-works/pi-agent-core\";\nimport type { RpcTarget, SessionTarget } from \"@earendil-works/pi-protocol\";\nimport { ServerDrainingError, SessionNotAttachedError } from \"./errors.ts\";\nimport type { RoutedSessionAttachment, RoutedSessionHandle, ServerHost } from \"./types.ts\";\n\nclass SessionCleanupError extends AggregateError {}\n\ninterface ClientAttachment {\n\treadonly id: string;\n\treadonly client: object;\n\treadonly session: HostedSession;\n\treadonly operations: Set<Promise<unknown>>;\n\tacquiring?: Promise<RoutedSessionAttachment>;\n\tlease?: RoutedSessionAttachment;\n\treleasing?: Promise<void>;\n}\n\ninterface HostedSession {\n\treadonly id: string;\n\treadonly handle: RoutedSessionHandle;\n\treadonly attachments: Set<ClientAttachment>;\n}\n\ninterface SessionRouterOptions<TMetadata extends SessionMetadata> {\n\thost: ServerHost<TMetadata>;\n\tserverId: string;\n\tisClosing: () => boolean;\n\tpublishAttachment(client: object, attachment: SessionTarget | undefined, context: Context): Promise<void>;\n\treportError: (error: unknown) => void;\n}\n\nexport class SessionRouter<TMetadata extends SessionMetadata = SessionMetadata> {\n\tprivate readonly options: SessionRouterOptions<TMetadata>;\n\tprivate readonly hostedSessions = new Map<string, HostedSession>();\n\tprivate readonly openingSessions = new Map<string, Promise<HostedSession>>();\n\tprivate readonly attachmentsByClient = new Map<object, ClientAttachment>();\n\tprivate readonly disconnectedClients = new Set<object>();\n\tprivate readonly clientOperations = new Map<object, Promise<void>>();\n\tprivate closePromise?: Promise<void>;\n\n\tconstructor(options: SessionRouterOptions<TMetadata>) {\n\t\tthis.options = options;\n\t}\n\n\tasync executeServiceCall(\n\t\tcall: ServiceCall,\n\t\ttarget: RpcTarget,\n\t\tclient: object,\n\t\tpublish: (subscriptionId: string, update: ServiceProviderUpdate, context: Context) => Promise<void>,\n\t\tcontext: Context,\n\t): Promise<JsonValue | undefined> {\n\t\tconst admitted = await this.runForClient(client, () =>\n\t\t\tthis.startServiceCall(client, target, call, publish, context),\n\t\t);\n\t\treturn admitted.result;\n\t}\n\n\tattachClient(client: object, sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing()) return Promise.reject(new ServerDrainingError());\n\t\treturn this.runForClient(client, () => this.attachClientNow(client, sessionId, context));\n\t}\n\n\tdetachClient(client: object, context: Context): Promise<void> {\n\t\treturn this.runForClient(client, async () => {\n\t\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\t\tif (attachment) await this.releaseAttachment(attachment, context);\n\t\t});\n\t}\n\n\tasync removeSession(sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing()) throw new ServerDrainingError();\n\t\tconst hosted = this.hostedSessions.get(sessionId);\n\t\tif (hosted === undefined) return;\n\t\tconst errors: unknown[] = [];\n\t\tconst releases = await Promise.allSettled(\n\t\t\t[...hosted.attachments].map((attachment) => this.releaseAttachment(attachment, context)),\n\t\t);\n\t\tfor (const result of releases) if (result.status === \"rejected\") errors.push(result.reason);\n\t\ttry {\n\t\t\tawait hosted.handle.close(context);\n\t\t} catch (error) {\n\t\t\terrors.push(error);\n\t\t}\n\t\tif (this.hostedSessions.get(sessionId) === hosted) this.hostedSessions.delete(sessionId);\n\t\tif (errors.length === 1) throw errors[0];\n\t\tif (errors.length > 1) throw new AggregateError(errors, `Failed to close Session ${sessionId}`);\n\t}\n\n\tasync disconnect(client: object, context: Context): Promise<void> {\n\t\tthis.disconnectedClients.add(client);\n\t\ttry {\n\t\t\tawait this.runForClient(client, async () => {\n\t\t\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\t\t\tif (attachment) await this.releaseAttachment(attachment, context, false);\n\t\t\t});\n\t\t} finally {\n\t\t\tthis.disconnectedClients.delete(client);\n\t\t}\n\t}\n\n\tclose(context: Context): Promise<void> {\n\t\tthis.closePromise ??= this.closeInternal(context);\n\t\treturn this.closePromise;\n\t}\n\n\tprivate async closeInternal(context: Context): Promise<void> {\n\t\tconst operationPromises = [...this.clientOperations.values()];\n\t\tconst openingPromises = [...this.openingSessions.values()];\n\t\tconst [operationResults, openingResults] = await Promise.all([\n\t\t\tPromise.allSettled(operationPromises),\n\t\t\tPromise.allSettled(openingPromises),\n\t\t]);\n\t\tconst closeErrors: unknown[] = [];\n\t\tfor (const result of [...operationResults, ...openingResults]) {\n\t\t\tif (result.status !== \"rejected\") continue;\n\t\t\tthis.options.reportError(result.reason);\n\t\t\tif (result.reason instanceof SessionCleanupError) closeErrors.push(result.reason);\n\t\t}\n\t\tconst attachmentResults = await Promise.allSettled(\n\t\t\t[...this.hostedSessions.values()].flatMap((session) =>\n\t\t\t\t[...session.attachments].map((attachment) => this.releaseAttachment(attachment, context)),\n\t\t\t),\n\t\t);\n\t\tfor (const result of attachmentResults) {\n\t\t\tif (result.status === \"rejected\") closeErrors.push(result.reason);\n\t\t}\n\t\tconst hosted = [...this.hostedSessions.values()];\n\t\tconst closeResults = await Promise.allSettled(hosted.map(({ handle }) => handle.close(context)));\n\t\tfor (let index = 0; index < closeResults.length; index++) {\n\t\t\tconst result = closeResults[index]!;\n\t\t\tconst session = hosted[index]!;\n\t\t\tif (result.status === \"fulfilled\") {\n\t\t\t\tif (this.hostedSessions.get(session.id) === session) this.hostedSessions.delete(session.id);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.options.reportError(result.reason);\n\t\t\tcloseErrors.push(result.reason);\n\t\t}\n\t\tthis.attachmentsByClient.clear();\n\t\tthis.clientOperations.clear();\n\t\tif (closeErrors.length > 0) throw new AggregateError(closeErrors, \"Failed to close routed Sessions\");\n\t}\n\n\tprivate runForClient<T>(client: object, operation: () => Promise<T>): Promise<T> {\n\t\tconst previous = this.clientOperations.get(client) ?? Promise.resolve();\n\t\tconst result = previous.catch(() => {}).then(operation);\n\t\tconst tail = result.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t);\n\t\tthis.clientOperations.set(client, tail);\n\t\tvoid tail.finally(() => {\n\t\t\tif (this.clientOperations.get(client) === tail) this.clientOperations.delete(client);\n\t\t});\n\t\treturn result;\n\t}\n\n\tprivate async attachClientNow(client: object, sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tconst current = this.attachmentsByClient.get(client);\n\t\tif (current?.session.id === sessionId) return;\n\t\tconst hosted = await this.acquire(sessionId, context);\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tif (current) await this.releaseAttachment(current, context, false);\n\t\tconst attachment: ClientAttachment = {\n\t\t\tid: randomUUID(),\n\t\t\tclient,\n\t\t\tsession: hosted,\n\t\t\toperations: new Set(),\n\t\t};\n\t\thosted.attachments.add(attachment);\n\t\ttry {\n\t\t\tconst acquiring = Promise.resolve(hosted.handle.attachClient(context));\n\t\t\tattachment.acquiring = acquiring;\n\t\t\tattachment.lease = await acquiring;\n\t\t} catch (error) {\n\t\t\thosted.attachments.delete(attachment);\n\t\t\tthrow error;\n\t\t}\n\t\tif (\n\t\t\tthis.hostedSessions.get(hosted.id) !== hosted ||\n\t\t\t!hosted.attachments.has(attachment) ||\n\t\t\tthis.disconnectedClients.has(client) ||\n\t\t\tthis.options.isClosing()\n\t\t) {\n\t\t\tawait this.releaseAttachment(attachment, context);\n\t\t\tthrow new ServerDrainingError();\n\t\t}\n\t\tthis.attachmentsByClient.set(client, attachment);\n\t\tawait this.options.publishAttachment(\n\t\t\tclient,\n\t\t\t{ serverId: this.options.serverId, sessionId, attachmentId: attachment.id },\n\t\t\tcontext,\n\t\t);\n\t}\n\n\tprivate async startServiceCall(\n\t\tclient: object,\n\t\ttarget: RpcTarget,\n\t\tcall: ServiceCall,\n\t\tpublish: (subscriptionId: string, update: ServiceProviderUpdate, context: Context) => Promise<void>,\n\t\tcontext: Context,\n\t): Promise<{ result: Promise<JsonValue | undefined> }> {\n\t\tconst attachment = this.requireAttachment(client, target);\n\t\tconst result = attachment.lease!.invokeService(\n\t\t\tcall,\n\t\t\t(subscriptionId, update, updateContext) => publish(subscriptionId, update, updateContext),\n\t\t\tcontext,\n\t\t);\n\t\tthis.trackOperation(attachment, result);\n\t\treturn { result };\n\t}\n\n\tprivate trackOperation(attachment: ClientAttachment, result: Promise<unknown>): void {\n\t\tattachment.operations.add(result);\n\t\tconst remove = (): void => {\n\t\t\tattachment.operations.delete(result);\n\t\t};\n\t\tvoid result.then(remove, remove);\n\t}\n\n\tprivate requireAttachment(client: object, target: RpcTarget): ClientAttachment {\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tif (!(\"sessionId\" in target)) throw new SessionNotAttachedError();\n\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\tif (!attachment || attachment.session.id !== target.sessionId || attachment.id !== target.attachmentId) {\n\t\t\tthrow new SessionNotAttachedError();\n\t\t}\n\t\treturn attachment;\n\t}\n\n\tprivate releaseAttachment(attachment: ClientAttachment, context: Context, publish = true): Promise<void> {\n\t\tattachment.releasing ??= (async () => {\n\t\t\tconst errors: unknown[] = [];\n\t\t\ttry {\n\t\t\t\tawait Promise.allSettled(attachment.operations);\n\t\t\t\ttry {\n\t\t\t\t\tconst lease = attachment.lease ?? (await attachment.acquiring);\n\t\t\t\t\tif (lease) await lease.release(context);\n\t\t\t\t} catch (error) {\n\t\t\t\t\terrors.push(error);\n\t\t\t\t}\n\t\t\t\tif (errors.length === 1) throw errors[0];\n\t\t\t\tif (errors.length > 1) throw new AggregateError(errors, \"Failed to release Session attachment\");\n\t\t\t} finally {\n\t\t\t\tawait this.clearAttachment(attachment, context, publish);\n\t\t\t}\n\t\t})();\n\t\treturn attachment.releasing;\n\t}\n\n\tprivate async clearAttachment(attachment: ClientAttachment, context: Context, publish: boolean): Promise<void> {\n\t\tattachment.session.attachments.delete(attachment);\n\t\tif (this.attachmentsByClient.get(attachment.client) === attachment) {\n\t\t\tthis.attachmentsByClient.delete(attachment.client);\n\t\t\tif (publish) await this.options.publishAttachment(attachment.client, undefined, context);\n\t\t}\n\t}\n\n\tprivate async acquire(sessionId: string, context: Context): Promise<HostedSession> {\n\t\tconst existing = this.hostedSessions.get(sessionId);\n\t\tif (existing) return existing;\n\t\tconst opening = this.openingSessions.get(sessionId);\n\t\tif (opening) return opening;\n\t\tconst pending = this.open(sessionId, context);\n\t\tthis.openingSessions.set(sessionId, pending);\n\t\ttry {\n\t\t\treturn await pending;\n\t\t} finally {\n\t\t\tif (this.openingSessions.get(sessionId) === pending) this.openingSessions.delete(sessionId);\n\t\t}\n\t}\n\n\tprivate async open(sessionId: string, context: Context): Promise<HostedSession> {\n\t\tconst metadata = await this.options.host.resolveSession(sessionId, context);\n\t\tconst handle = await this.options.host.openSession(metadata, context);\n\t\tif (this.options.isClosing()) {\n\t\t\ttry {\n\t\t\t\tawait handle.close(context);\n\t\t\t} catch (error) {\n\t\t\t\tthis.options.reportError(error);\n\t\t\t\tthrow new SessionCleanupError(\n\t\t\t\t\t[new ServerDrainingError(), error],\n\t\t\t\t\t\"Failed to close routed Session acquired while draining\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow new ServerDrainingError();\n\t\t}\n\t\tconst hosted: HostedSession = { id: metadata.id, handle, attachments: new Set() };\n\t\tthis.hostedSessions.set(hosted.id, hosted);\n\t\tif (handle.terminated) {\n\t\t\tvoid handle.terminated.then(\n\t\t\t\t(error) => this.invalidate(hosted, error),\n\t\t\t\t(error: unknown) => this.invalidate(hosted, error instanceof Error ? error : new Error(String(error))),\n\t\t\t);\n\t\t}\n\t\treturn hosted;\n\t}\n\n\tprivate invalidate(hosted: HostedSession, error: Error | undefined): void {\n\t\tif (this.hostedSessions.get(hosted.id) !== hosted) return;\n\t\tthis.hostedSessions.delete(hosted.id);\n\t\tfor (const attachment of hosted.attachments) {\n\t\t\tvoid this.releaseAttachment(attachment, BACKGROUND_CONTEXT).catch((releaseError: unknown) =>\n\t\t\t\tthis.options.reportError(releaseError),\n\t\t\t);\n\t\t}\n\t\tif (error) this.options.reportError(error);\n\t}\n}\n"]}
@@ -0,0 +1,264 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { BACKGROUND_CONTEXT } from "@earendil-works/pi-agent-core";
3
+ import { ServerDrainingError, SessionNotAttachedError } from "./errors.js";
4
+ class SessionCleanupError extends AggregateError {
5
+ }
6
+ export class SessionRouter {
7
+ options;
8
+ hostedSessions = new Map();
9
+ openingSessions = new Map();
10
+ attachmentsByClient = new Map();
11
+ disconnectedClients = new Set();
12
+ clientOperations = new Map();
13
+ closePromise;
14
+ constructor(options) {
15
+ this.options = options;
16
+ }
17
+ async executeServiceCall(call, target, client, publish, context) {
18
+ const admitted = await this.runForClient(client, () => this.startServiceCall(client, target, call, publish, context));
19
+ return admitted.result;
20
+ }
21
+ attachClient(client, sessionId, context) {
22
+ if (this.options.isClosing())
23
+ return Promise.reject(new ServerDrainingError());
24
+ return this.runForClient(client, () => this.attachClientNow(client, sessionId, context));
25
+ }
26
+ detachClient(client, context) {
27
+ return this.runForClient(client, async () => {
28
+ const attachment = this.attachmentsByClient.get(client);
29
+ if (attachment)
30
+ await this.releaseAttachment(attachment, context);
31
+ });
32
+ }
33
+ async removeSession(sessionId, context) {
34
+ if (this.options.isClosing())
35
+ throw new ServerDrainingError();
36
+ const hosted = this.hostedSessions.get(sessionId);
37
+ if (hosted === undefined)
38
+ return;
39
+ const errors = [];
40
+ const releases = await Promise.allSettled([...hosted.attachments].map((attachment) => this.releaseAttachment(attachment, context)));
41
+ for (const result of releases)
42
+ if (result.status === "rejected")
43
+ errors.push(result.reason);
44
+ try {
45
+ await hosted.handle.close(context);
46
+ }
47
+ catch (error) {
48
+ errors.push(error);
49
+ }
50
+ if (this.hostedSessions.get(sessionId) === hosted)
51
+ this.hostedSessions.delete(sessionId);
52
+ if (errors.length === 1)
53
+ throw errors[0];
54
+ if (errors.length > 1)
55
+ throw new AggregateError(errors, `Failed to close Session ${sessionId}`);
56
+ }
57
+ async disconnect(client, context) {
58
+ this.disconnectedClients.add(client);
59
+ try {
60
+ await this.runForClient(client, async () => {
61
+ const attachment = this.attachmentsByClient.get(client);
62
+ if (attachment)
63
+ await this.releaseAttachment(attachment, context, false);
64
+ });
65
+ }
66
+ finally {
67
+ this.disconnectedClients.delete(client);
68
+ }
69
+ }
70
+ close(context) {
71
+ this.closePromise ??= this.closeInternal(context);
72
+ return this.closePromise;
73
+ }
74
+ async closeInternal(context) {
75
+ const operationPromises = [...this.clientOperations.values()];
76
+ const openingPromises = [...this.openingSessions.values()];
77
+ const [operationResults, openingResults] = await Promise.all([
78
+ Promise.allSettled(operationPromises),
79
+ Promise.allSettled(openingPromises),
80
+ ]);
81
+ const closeErrors = [];
82
+ for (const result of [...operationResults, ...openingResults]) {
83
+ if (result.status !== "rejected")
84
+ continue;
85
+ this.options.reportError(result.reason);
86
+ if (result.reason instanceof SessionCleanupError)
87
+ closeErrors.push(result.reason);
88
+ }
89
+ const attachmentResults = await Promise.allSettled([...this.hostedSessions.values()].flatMap((session) => [...session.attachments].map((attachment) => this.releaseAttachment(attachment, context))));
90
+ for (const result of attachmentResults) {
91
+ if (result.status === "rejected")
92
+ closeErrors.push(result.reason);
93
+ }
94
+ const hosted = [...this.hostedSessions.values()];
95
+ const closeResults = await Promise.allSettled(hosted.map(({ handle }) => handle.close(context)));
96
+ for (let index = 0; index < closeResults.length; index++) {
97
+ const result = closeResults[index];
98
+ const session = hosted[index];
99
+ if (result.status === "fulfilled") {
100
+ if (this.hostedSessions.get(session.id) === session)
101
+ this.hostedSessions.delete(session.id);
102
+ continue;
103
+ }
104
+ this.options.reportError(result.reason);
105
+ closeErrors.push(result.reason);
106
+ }
107
+ this.attachmentsByClient.clear();
108
+ this.clientOperations.clear();
109
+ if (closeErrors.length > 0)
110
+ throw new AggregateError(closeErrors, "Failed to close routed Sessions");
111
+ }
112
+ runForClient(client, operation) {
113
+ const previous = this.clientOperations.get(client) ?? Promise.resolve();
114
+ const result = previous.catch(() => { }).then(operation);
115
+ const tail = result.then(() => undefined, () => undefined);
116
+ this.clientOperations.set(client, tail);
117
+ void tail.finally(() => {
118
+ if (this.clientOperations.get(client) === tail)
119
+ this.clientOperations.delete(client);
120
+ });
121
+ return result;
122
+ }
123
+ async attachClientNow(client, sessionId, context) {
124
+ if (this.options.isClosing() || this.disconnectedClients.has(client))
125
+ throw new ServerDrainingError();
126
+ const current = this.attachmentsByClient.get(client);
127
+ if (current?.session.id === sessionId)
128
+ return;
129
+ const hosted = await this.acquire(sessionId, context);
130
+ if (this.options.isClosing() || this.disconnectedClients.has(client))
131
+ throw new ServerDrainingError();
132
+ if (current)
133
+ await this.releaseAttachment(current, context, false);
134
+ const attachment = {
135
+ id: randomUUID(),
136
+ client,
137
+ session: hosted,
138
+ operations: new Set(),
139
+ };
140
+ hosted.attachments.add(attachment);
141
+ try {
142
+ const acquiring = Promise.resolve(hosted.handle.attachClient(context));
143
+ attachment.acquiring = acquiring;
144
+ attachment.lease = await acquiring;
145
+ }
146
+ catch (error) {
147
+ hosted.attachments.delete(attachment);
148
+ throw error;
149
+ }
150
+ if (this.hostedSessions.get(hosted.id) !== hosted ||
151
+ !hosted.attachments.has(attachment) ||
152
+ this.disconnectedClients.has(client) ||
153
+ this.options.isClosing()) {
154
+ await this.releaseAttachment(attachment, context);
155
+ throw new ServerDrainingError();
156
+ }
157
+ this.attachmentsByClient.set(client, attachment);
158
+ await this.options.publishAttachment(client, { serverId: this.options.serverId, sessionId, attachmentId: attachment.id }, context);
159
+ }
160
+ async startServiceCall(client, target, call, publish, context) {
161
+ const attachment = this.requireAttachment(client, target);
162
+ const result = attachment.lease.invokeService(call, (subscriptionId, update, updateContext) => publish(subscriptionId, update, updateContext), context);
163
+ this.trackOperation(attachment, result);
164
+ return { result };
165
+ }
166
+ trackOperation(attachment, result) {
167
+ attachment.operations.add(result);
168
+ const remove = () => {
169
+ attachment.operations.delete(result);
170
+ };
171
+ void result.then(remove, remove);
172
+ }
173
+ requireAttachment(client, target) {
174
+ if (this.options.isClosing() || this.disconnectedClients.has(client))
175
+ throw new ServerDrainingError();
176
+ if (!("sessionId" in target))
177
+ throw new SessionNotAttachedError();
178
+ const attachment = this.attachmentsByClient.get(client);
179
+ if (!attachment || attachment.session.id !== target.sessionId || attachment.id !== target.attachmentId) {
180
+ throw new SessionNotAttachedError();
181
+ }
182
+ return attachment;
183
+ }
184
+ releaseAttachment(attachment, context, publish = true) {
185
+ attachment.releasing ??= (async () => {
186
+ const errors = [];
187
+ try {
188
+ await Promise.allSettled(attachment.operations);
189
+ try {
190
+ const lease = attachment.lease ?? (await attachment.acquiring);
191
+ if (lease)
192
+ await lease.release(context);
193
+ }
194
+ catch (error) {
195
+ errors.push(error);
196
+ }
197
+ if (errors.length === 1)
198
+ throw errors[0];
199
+ if (errors.length > 1)
200
+ throw new AggregateError(errors, "Failed to release Session attachment");
201
+ }
202
+ finally {
203
+ await this.clearAttachment(attachment, context, publish);
204
+ }
205
+ })();
206
+ return attachment.releasing;
207
+ }
208
+ async clearAttachment(attachment, context, publish) {
209
+ attachment.session.attachments.delete(attachment);
210
+ if (this.attachmentsByClient.get(attachment.client) === attachment) {
211
+ this.attachmentsByClient.delete(attachment.client);
212
+ if (publish)
213
+ await this.options.publishAttachment(attachment.client, undefined, context);
214
+ }
215
+ }
216
+ async acquire(sessionId, context) {
217
+ const existing = this.hostedSessions.get(sessionId);
218
+ if (existing)
219
+ return existing;
220
+ const opening = this.openingSessions.get(sessionId);
221
+ if (opening)
222
+ return opening;
223
+ const pending = this.open(sessionId, context);
224
+ this.openingSessions.set(sessionId, pending);
225
+ try {
226
+ return await pending;
227
+ }
228
+ finally {
229
+ if (this.openingSessions.get(sessionId) === pending)
230
+ this.openingSessions.delete(sessionId);
231
+ }
232
+ }
233
+ async open(sessionId, context) {
234
+ const metadata = await this.options.host.resolveSession(sessionId, context);
235
+ const handle = await this.options.host.openSession(metadata, context);
236
+ if (this.options.isClosing()) {
237
+ try {
238
+ await handle.close(context);
239
+ }
240
+ catch (error) {
241
+ this.options.reportError(error);
242
+ throw new SessionCleanupError([new ServerDrainingError(), error], "Failed to close routed Session acquired while draining");
243
+ }
244
+ throw new ServerDrainingError();
245
+ }
246
+ const hosted = { id: metadata.id, handle, attachments: new Set() };
247
+ this.hostedSessions.set(hosted.id, hosted);
248
+ if (handle.terminated) {
249
+ void handle.terminated.then((error) => this.invalidate(hosted, error), (error) => this.invalidate(hosted, error instanceof Error ? error : new Error(String(error))));
250
+ }
251
+ return hosted;
252
+ }
253
+ invalidate(hosted, error) {
254
+ if (this.hostedSessions.get(hosted.id) !== hosted)
255
+ return;
256
+ this.hostedSessions.delete(hosted.id);
257
+ for (const attachment of hosted.attachments) {
258
+ void this.releaseAttachment(attachment, BACKGROUND_CONTEXT).catch((releaseError) => this.options.reportError(releaseError));
259
+ }
260
+ if (error)
261
+ this.options.reportError(error);
262
+ }
263
+ }
264
+ //# sourceMappingURL=session-router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-router.js","sourceRoot":"","sources":["../src/session-router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,kBAAkB,EAAsC,MAAM,+BAA+B,CAAC;AAEvG,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAG3E,MAAM,mBAAoB,SAAQ,cAAc;CAAG;AA0BnD,MAAM,OAAO,aAAa;IACR,OAAO,CAAkC;IACz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AI,GAAG,EAAE,EAAE,CAAC;QAClF,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QAC3C,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACvB,KAAK,MAAM,CAAC,UAAU,CAAC,IAAI,CAC1B,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,EACzC,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CACtG,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAAA,CACd;IAEO,UAAU,CAAC,MAAqB,EAAE,KAAwB,EAAQ;QACzE,IAAI,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,MAAM;YAAE,OAAO;QAC1D,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACtC,KAAK,MAAM,UAAU,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;YAC7C,KAAK,IAAI,CAAC,iBAAiB,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC,KAAK,CAAC,CAAC,YAAqB,EAAE,EAAE,CAC3F,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,YAAY,CAAC,CACtC,CAAC;QACH,CAAC;QACD,IAAI,KAAK;YAAE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAAA,CAC3C;CACD","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { JsonValue, ServiceCall, ServiceProviderUpdate } from \"@earendil-works/chord\";\nimport { BACKGROUND_CONTEXT, type Context, type SessionMetadata } from \"@earendil-works/pi-agent-core\";\nimport type { RpcTarget, SessionTarget } from \"@earendil-works/pi-protocol\";\nimport { ServerDrainingError, SessionNotAttachedError } from \"./errors.ts\";\nimport type { RoutedSessionAttachment, RoutedSessionHandle, ServerHost } from \"./types.ts\";\n\nclass SessionCleanupError extends AggregateError {}\n\ninterface ClientAttachment {\n\treadonly id: string;\n\treadonly client: object;\n\treadonly session: HostedSession;\n\treadonly operations: Set<Promise<unknown>>;\n\tacquiring?: Promise<RoutedSessionAttachment>;\n\tlease?: RoutedSessionAttachment;\n\treleasing?: Promise<void>;\n}\n\ninterface HostedSession {\n\treadonly id: string;\n\treadonly handle: RoutedSessionHandle;\n\treadonly attachments: Set<ClientAttachment>;\n}\n\ninterface SessionRouterOptions<TMetadata extends SessionMetadata> {\n\thost: ServerHost<TMetadata>;\n\tserverId: string;\n\tisClosing: () => boolean;\n\tpublishAttachment(client: object, attachment: SessionTarget | undefined, context: Context): Promise<void>;\n\treportError: (error: unknown) => void;\n}\n\nexport class SessionRouter<TMetadata extends SessionMetadata = SessionMetadata> {\n\tprivate readonly options: SessionRouterOptions<TMetadata>;\n\tprivate readonly hostedSessions = new Map<string, HostedSession>();\n\tprivate readonly openingSessions = new Map<string, Promise<HostedSession>>();\n\tprivate readonly attachmentsByClient = new Map<object, ClientAttachment>();\n\tprivate readonly disconnectedClients = new Set<object>();\n\tprivate readonly clientOperations = new Map<object, Promise<void>>();\n\tprivate closePromise?: Promise<void>;\n\n\tconstructor(options: SessionRouterOptions<TMetadata>) {\n\t\tthis.options = options;\n\t}\n\n\tasync executeServiceCall(\n\t\tcall: ServiceCall,\n\t\ttarget: RpcTarget,\n\t\tclient: object,\n\t\tpublish: (subscriptionId: string, update: ServiceProviderUpdate, context: Context) => Promise<void>,\n\t\tcontext: Context,\n\t): Promise<JsonValue | undefined> {\n\t\tconst admitted = await this.runForClient(client, () =>\n\t\t\tthis.startServiceCall(client, target, call, publish, context),\n\t\t);\n\t\treturn admitted.result;\n\t}\n\n\tattachClient(client: object, sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing()) return Promise.reject(new ServerDrainingError());\n\t\treturn this.runForClient(client, () => this.attachClientNow(client, sessionId, context));\n\t}\n\n\tdetachClient(client: object, context: Context): Promise<void> {\n\t\treturn this.runForClient(client, async () => {\n\t\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\t\tif (attachment) await this.releaseAttachment(attachment, context);\n\t\t});\n\t}\n\n\tasync removeSession(sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing()) throw new ServerDrainingError();\n\t\tconst hosted = this.hostedSessions.get(sessionId);\n\t\tif (hosted === undefined) return;\n\t\tconst errors: unknown[] = [];\n\t\tconst releases = await Promise.allSettled(\n\t\t\t[...hosted.attachments].map((attachment) => this.releaseAttachment(attachment, context)),\n\t\t);\n\t\tfor (const result of releases) if (result.status === \"rejected\") errors.push(result.reason);\n\t\ttry {\n\t\t\tawait hosted.handle.close(context);\n\t\t} catch (error) {\n\t\t\terrors.push(error);\n\t\t}\n\t\tif (this.hostedSessions.get(sessionId) === hosted) this.hostedSessions.delete(sessionId);\n\t\tif (errors.length === 1) throw errors[0];\n\t\tif (errors.length > 1) throw new AggregateError(errors, `Failed to close Session ${sessionId}`);\n\t}\n\n\tasync disconnect(client: object, context: Context): Promise<void> {\n\t\tthis.disconnectedClients.add(client);\n\t\ttry {\n\t\t\tawait this.runForClient(client, async () => {\n\t\t\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\t\t\tif (attachment) await this.releaseAttachment(attachment, context, false);\n\t\t\t});\n\t\t} finally {\n\t\t\tthis.disconnectedClients.delete(client);\n\t\t}\n\t}\n\n\tclose(context: Context): Promise<void> {\n\t\tthis.closePromise ??= this.closeInternal(context);\n\t\treturn this.closePromise;\n\t}\n\n\tprivate async closeInternal(context: Context): Promise<void> {\n\t\tconst operationPromises = [...this.clientOperations.values()];\n\t\tconst openingPromises = [...this.openingSessions.values()];\n\t\tconst [operationResults, openingResults] = await Promise.all([\n\t\t\tPromise.allSettled(operationPromises),\n\t\t\tPromise.allSettled(openingPromises),\n\t\t]);\n\t\tconst closeErrors: unknown[] = [];\n\t\tfor (const result of [...operationResults, ...openingResults]) {\n\t\t\tif (result.status !== \"rejected\") continue;\n\t\t\tthis.options.reportError(result.reason);\n\t\t\tif (result.reason instanceof SessionCleanupError) closeErrors.push(result.reason);\n\t\t}\n\t\tconst attachmentResults = await Promise.allSettled(\n\t\t\t[...this.hostedSessions.values()].flatMap((session) =>\n\t\t\t\t[...session.attachments].map((attachment) => this.releaseAttachment(attachment, context)),\n\t\t\t),\n\t\t);\n\t\tfor (const result of attachmentResults) {\n\t\t\tif (result.status === \"rejected\") closeErrors.push(result.reason);\n\t\t}\n\t\tconst hosted = [...this.hostedSessions.values()];\n\t\tconst closeResults = await Promise.allSettled(hosted.map(({ handle }) => handle.close(context)));\n\t\tfor (let index = 0; index < closeResults.length; index++) {\n\t\t\tconst result = closeResults[index]!;\n\t\t\tconst session = hosted[index]!;\n\t\t\tif (result.status === \"fulfilled\") {\n\t\t\t\tif (this.hostedSessions.get(session.id) === session) this.hostedSessions.delete(session.id);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.options.reportError(result.reason);\n\t\t\tcloseErrors.push(result.reason);\n\t\t}\n\t\tthis.attachmentsByClient.clear();\n\t\tthis.clientOperations.clear();\n\t\tif (closeErrors.length > 0) throw new AggregateError(closeErrors, \"Failed to close routed Sessions\");\n\t}\n\n\tprivate runForClient<T>(client: object, operation: () => Promise<T>): Promise<T> {\n\t\tconst previous = this.clientOperations.get(client) ?? Promise.resolve();\n\t\tconst result = previous.catch(() => {}).then(operation);\n\t\tconst tail = result.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t);\n\t\tthis.clientOperations.set(client, tail);\n\t\tvoid tail.finally(() => {\n\t\t\tif (this.clientOperations.get(client) === tail) this.clientOperations.delete(client);\n\t\t});\n\t\treturn result;\n\t}\n\n\tprivate async attachClientNow(client: object, sessionId: string, context: Context): Promise<void> {\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tconst current = this.attachmentsByClient.get(client);\n\t\tif (current?.session.id === sessionId) return;\n\t\tconst hosted = await this.acquire(sessionId, context);\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tif (current) await this.releaseAttachment(current, context, false);\n\t\tconst attachment: ClientAttachment = {\n\t\t\tid: randomUUID(),\n\t\t\tclient,\n\t\t\tsession: hosted,\n\t\t\toperations: new Set(),\n\t\t};\n\t\thosted.attachments.add(attachment);\n\t\ttry {\n\t\t\tconst acquiring = Promise.resolve(hosted.handle.attachClient(context));\n\t\t\tattachment.acquiring = acquiring;\n\t\t\tattachment.lease = await acquiring;\n\t\t} catch (error) {\n\t\t\thosted.attachments.delete(attachment);\n\t\t\tthrow error;\n\t\t}\n\t\tif (\n\t\t\tthis.hostedSessions.get(hosted.id) !== hosted ||\n\t\t\t!hosted.attachments.has(attachment) ||\n\t\t\tthis.disconnectedClients.has(client) ||\n\t\t\tthis.options.isClosing()\n\t\t) {\n\t\t\tawait this.releaseAttachment(attachment, context);\n\t\t\tthrow new ServerDrainingError();\n\t\t}\n\t\tthis.attachmentsByClient.set(client, attachment);\n\t\tawait this.options.publishAttachment(\n\t\t\tclient,\n\t\t\t{ serverId: this.options.serverId, sessionId, attachmentId: attachment.id },\n\t\t\tcontext,\n\t\t);\n\t}\n\n\tprivate async startServiceCall(\n\t\tclient: object,\n\t\ttarget: RpcTarget,\n\t\tcall: ServiceCall,\n\t\tpublish: (subscriptionId: string, update: ServiceProviderUpdate, context: Context) => Promise<void>,\n\t\tcontext: Context,\n\t): Promise<{ result: Promise<JsonValue | undefined> }> {\n\t\tconst attachment = this.requireAttachment(client, target);\n\t\tconst result = attachment.lease!.invokeService(\n\t\t\tcall,\n\t\t\t(subscriptionId, update, updateContext) => publish(subscriptionId, update, updateContext),\n\t\t\tcontext,\n\t\t);\n\t\tthis.trackOperation(attachment, result);\n\t\treturn { result };\n\t}\n\n\tprivate trackOperation(attachment: ClientAttachment, result: Promise<unknown>): void {\n\t\tattachment.operations.add(result);\n\t\tconst remove = (): void => {\n\t\t\tattachment.operations.delete(result);\n\t\t};\n\t\tvoid result.then(remove, remove);\n\t}\n\n\tprivate requireAttachment(client: object, target: RpcTarget): ClientAttachment {\n\t\tif (this.options.isClosing() || this.disconnectedClients.has(client)) throw new ServerDrainingError();\n\t\tif (!(\"sessionId\" in target)) throw new SessionNotAttachedError();\n\t\tconst attachment = this.attachmentsByClient.get(client);\n\t\tif (!attachment || attachment.session.id !== target.sessionId || attachment.id !== target.attachmentId) {\n\t\t\tthrow new SessionNotAttachedError();\n\t\t}\n\t\treturn attachment;\n\t}\n\n\tprivate releaseAttachment(attachment: ClientAttachment, context: Context, publish = true): Promise<void> {\n\t\tattachment.releasing ??= (async () => {\n\t\t\tconst errors: unknown[] = [];\n\t\t\ttry {\n\t\t\t\tawait Promise.allSettled(attachment.operations);\n\t\t\t\ttry {\n\t\t\t\t\tconst lease = attachment.lease ?? (await attachment.acquiring);\n\t\t\t\t\tif (lease) await lease.release(context);\n\t\t\t\t} catch (error) {\n\t\t\t\t\terrors.push(error);\n\t\t\t\t}\n\t\t\t\tif (errors.length === 1) throw errors[0];\n\t\t\t\tif (errors.length > 1) throw new AggregateError(errors, \"Failed to release Session attachment\");\n\t\t\t} finally {\n\t\t\t\tawait this.clearAttachment(attachment, context, publish);\n\t\t\t}\n\t\t})();\n\t\treturn attachment.releasing;\n\t}\n\n\tprivate async clearAttachment(attachment: ClientAttachment, context: Context, publish: boolean): Promise<void> {\n\t\tattachment.session.attachments.delete(attachment);\n\t\tif (this.attachmentsByClient.get(attachment.client) === attachment) {\n\t\t\tthis.attachmentsByClient.delete(attachment.client);\n\t\t\tif (publish) await this.options.publishAttachment(attachment.client, undefined, context);\n\t\t}\n\t}\n\n\tprivate async acquire(sessionId: string, context: Context): Promise<HostedSession> {\n\t\tconst existing = this.hostedSessions.get(sessionId);\n\t\tif (existing) return existing;\n\t\tconst opening = this.openingSessions.get(sessionId);\n\t\tif (opening) return opening;\n\t\tconst pending = this.open(sessionId, context);\n\t\tthis.openingSessions.set(sessionId, pending);\n\t\ttry {\n\t\t\treturn await pending;\n\t\t} finally {\n\t\t\tif (this.openingSessions.get(sessionId) === pending) this.openingSessions.delete(sessionId);\n\t\t}\n\t}\n\n\tprivate async open(sessionId: string, context: Context): Promise<HostedSession> {\n\t\tconst metadata = await this.options.host.resolveSession(sessionId, context);\n\t\tconst handle = await this.options.host.openSession(metadata, context);\n\t\tif (this.options.isClosing()) {\n\t\t\ttry {\n\t\t\t\tawait handle.close(context);\n\t\t\t} catch (error) {\n\t\t\t\tthis.options.reportError(error);\n\t\t\t\tthrow new SessionCleanupError(\n\t\t\t\t\t[new ServerDrainingError(), error],\n\t\t\t\t\t\"Failed to close routed Session acquired while draining\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow new ServerDrainingError();\n\t\t}\n\t\tconst hosted: HostedSession = { id: metadata.id, handle, attachments: new Set() };\n\t\tthis.hostedSessions.set(hosted.id, hosted);\n\t\tif (handle.terminated) {\n\t\t\tvoid handle.terminated.then(\n\t\t\t\t(error) => this.invalidate(hosted, error),\n\t\t\t\t(error: unknown) => this.invalidate(hosted, error instanceof Error ? error : new Error(String(error))),\n\t\t\t);\n\t\t}\n\t\treturn hosted;\n\t}\n\n\tprivate invalidate(hosted: HostedSession, error: Error | undefined): void {\n\t\tif (this.hostedSessions.get(hosted.id) !== hosted) return;\n\t\tthis.hostedSessions.delete(hosted.id);\n\t\tfor (const attachment of hosted.attachments) {\n\t\t\tvoid this.releaseAttachment(attachment, BACKGROUND_CONTEXT).catch((releaseError: unknown) =>\n\t\t\t\tthis.options.reportError(releaseError),\n\t\t\t);\n\t\t}\n\t\tif (error) this.options.reportError(error);\n\t}\n}\n"]}