@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.200

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 (391) hide show
  1. package/dist/api.js +11 -0
  2. package/dist/claude-settings.js +40 -0
  3. package/dist/claude.js +163 -0
  4. package/dist/cli.js +77 -567
  5. package/dist/control.js +74 -0
  6. package/dist/hold.js +211 -0
  7. package/dist/local.js +43 -0
  8. package/dist/mcp-config.js +25 -0
  9. package/dist/node-name.js +71 -0
  10. package/dist/onboarding.js +44 -0
  11. package/dist/provider-flags.js +9 -0
  12. package/dist/route.js +58 -0
  13. package/dist/runtime-install.js +97 -0
  14. package/dist/runtime.js +74 -0
  15. package/dist/serve.js +158 -0
  16. package/dist/service.js +243 -0
  17. package/dist/tail.js +102 -144
  18. package/dist/update.js +45 -0
  19. package/dist/version.js +97 -0
  20. package/package.json +14 -23
  21. package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
  22. package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
  23. package/runtime/marketplace/plugin/.mcp.json +1 -0
  24. package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
  25. package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
  26. package/runtime/marketplace/plugin/bin/policy.mjs +97 -0
  27. package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
  28. package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
  29. package/runtime/marketplace/plugin/orchestrator.md +24 -0
  30. package/runtime/node_modules/@metro-labs/core/package.json +47 -0
  31. package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
  32. package/runtime/node_modules/@metro-labs/core/src/events.ts +119 -0
  33. package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
  34. package/runtime/node_modules/@metro-labs/core/src/ids.ts +24 -0
  35. package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
  36. package/runtime/node_modules/@metro-labs/core/src/lines.ts +91 -0
  37. package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
  38. package/runtime/node_modules/@metro-labs/core/src/protocol.ts +227 -0
  39. package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
  40. package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
  41. package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
  42. package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
  43. package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
  44. package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
  45. package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
  46. package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
  47. package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +61 -0
  48. package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
  49. package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +89 -0
  50. package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +123 -0
  51. package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
  52. package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -0
  53. package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
  54. package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
  55. package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
  56. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +134 -0
  57. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
  58. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +119 -0
  59. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
  60. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
  61. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +91 -0
  62. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +100 -0
  63. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
  64. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +252 -0
  65. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +61 -0
  66. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
  67. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -0
  68. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
  69. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-lookups.ts +55 -0
  70. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
  71. package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
  72. package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
  73. package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
  74. package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
  75. package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
  76. package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
  77. package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +168 -0
  78. package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +35 -0
  79. package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +128 -0
  80. package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
  81. package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
  82. package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +86 -0
  83. package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
  84. package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
  85. package/runtime/node_modules/@metro-labs/daemon/src/approvals/needs.ts +2 -0
  86. package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +149 -0
  87. package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
  88. package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +221 -0
  89. package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
  90. package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
  91. package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
  92. package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
  93. package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
  94. package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +12 -0
  95. package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +370 -0
  96. package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +90 -0
  97. package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +78 -0
  98. package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
  99. package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
  100. package/runtime/node_modules/@metro-labs/daemon/src/channels/reply-id.ts +1 -0
  101. package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
  102. package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +355 -0
  103. package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
  104. package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
  105. package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +82 -0
  106. package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
  107. package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
  108. package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
  109. package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +283 -0
  110. package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
  111. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +271 -0
  112. package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
  113. package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
  114. package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +306 -0
  115. package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +176 -0
  116. package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
  117. package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
  118. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
  119. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +135 -0
  120. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +158 -0
  121. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +166 -0
  122. package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
  123. package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
  124. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
  125. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +29 -0
  126. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +348 -0
  127. package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +358 -0
  128. package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
  129. package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
  130. package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
  131. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
  132. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
  133. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
  134. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +123 -0
  135. package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +278 -0
  136. package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
  137. package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
  138. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -0
  139. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
  140. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +173 -0
  141. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +194 -0
  142. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +137 -0
  143. package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -0
  144. package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
  145. package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +237 -0
  146. package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
  147. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
  148. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
  149. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
  150. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
  151. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
  152. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +223 -0
  153. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
  154. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
  155. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +299 -0
  156. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
  157. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
  158. package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
  159. package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
  160. package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
  161. package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
  162. package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
  163. package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +15 -0
  164. package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
  165. package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +156 -0
  166. package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
  167. package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +226 -0
  168. package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
  169. package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +90 -0
  170. package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
  171. package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +137 -0
  172. package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
  173. package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
  174. package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +87 -0
  175. package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
  176. package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +83 -0
  177. package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
  178. package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
  179. package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
  180. package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
  181. package/runtime/node_modules/@metro-labs/daemon/src/mcp/read-tool.ts +87 -0
  182. package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
  183. package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +48 -0
  184. package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +202 -0
  185. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -0
  186. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
  187. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +225 -0
  188. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +333 -0
  189. package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +61 -0
  190. package/runtime/node_modules/@metro-labs/daemon/src/metro-user/api.ts +43 -0
  191. package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
  192. package/runtime/node_modules/@metro-labs/daemon/src/metro-user/move.ts +165 -0
  193. package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
  194. package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
  195. package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
  196. package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
  197. package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
  198. package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
  199. package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +365 -0
  200. package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
  201. package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +71 -0
  202. package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
  203. package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
  204. package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +39 -0
  205. package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
  206. package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
  207. package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
  208. package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
  209. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
  210. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +128 -0
  211. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +134 -0
  212. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
  213. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +225 -0
  214. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +288 -0
  215. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
  216. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
  217. package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
  218. package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +51 -0
  219. package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
  220. package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
  221. package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
  222. package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
  223. package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
  224. package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
  225. package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
  226. package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
  227. package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
  228. package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
  229. package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
  230. package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +48 -0
  231. package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
  232. package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
  233. package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
  234. package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +148 -0
  235. package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +130 -0
  236. package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +61 -0
  237. package/runtime/node_modules/@metro-labs/discord-bot/package.json +26 -0
  238. package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +117 -0
  239. package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +240 -0
  240. package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
  241. package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
  242. package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +174 -0
  243. package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
  244. package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
  245. package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +122 -0
  246. package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
  247. package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
  248. package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
  249. package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +22 -0
  250. package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
  251. package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +1 -0
  252. package/runtime/node_modules/@metro-labs/http/package.json +29 -0
  253. package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
  254. package/runtime/node_modules/@metro-labs/http/src/api-http.ts +138 -0
  255. package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
  256. package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
  257. package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
  258. package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
  259. package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
  260. package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +107 -0
  261. package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +20 -0
  262. package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
  263. package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
  264. package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
  265. package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
  266. package/runtime/node_modules/@metro-labs/outlook/src/format.ts +141 -0
  267. package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
  268. package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
  269. package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
  270. package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
  271. package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
  272. package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
  273. package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
  274. package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
  275. package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
  276. package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
  277. package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
  278. package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
  279. package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
  280. package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +256 -0
  281. package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
  282. package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
  283. package/runtime/node_modules/@metro-labs/telegram/src/format.ts +128 -0
  284. package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
  285. package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
  286. package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
  287. package/runtime/node_modules/@metro-labs/telegram/src/index.ts +7 -0
  288. package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
  289. package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
  290. package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
  291. package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
  292. package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
  293. package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
  294. package/runtime/node_modules/@metro-labs/telegram/src/station.ts +27 -0
  295. package/runtime/node_modules/@metro-labs/telegram/src/types.ts +6 -0
  296. package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
  297. package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
  298. package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +119 -0
  299. package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +246 -0
  300. package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
  301. package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +173 -0
  302. package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
  303. package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +95 -0
  304. package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
  305. package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +65 -0
  306. package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +20 -0
  307. package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
  308. package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
  309. package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +1 -0
  310. package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
  311. package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +106 -0
  312. package/runtime/node_modules/@metro-labs/threema/src/actions.ts +134 -0
  313. package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
  314. package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
  315. package/runtime/node_modules/@metro-labs/threema/src/files.ts +58 -0
  316. package/runtime/node_modules/@metro-labs/threema/src/format.ts +169 -0
  317. package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
  318. package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
  319. package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
  320. package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
  321. package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
  322. package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
  323. package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
  324. package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
  325. package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
  326. package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
  327. package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
  328. package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
  329. package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
  330. package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +213 -0
  331. package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
  332. package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
  333. package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +388 -0
  334. package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
  335. package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +83 -0
  336. package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +40 -0
  337. package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +7 -0
  338. package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
  339. package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
  340. package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
  341. package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
  342. package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +28 -0
  343. package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
  344. package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
  345. package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
  346. package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
  347. package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +24 -0
  348. package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +94 -0
  349. package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +5 -0
  350. package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +1 -0
  351. package/runtime/node_modules/@metro-labs/xmtp/package.json +34 -0
  352. package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +114 -0
  353. package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
  354. package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +101 -0
  355. package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +118 -0
  356. package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +244 -0
  357. package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
  358. package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
  359. package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
  360. package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +316 -0
  361. package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +69 -0
  362. package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
  363. package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
  364. package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +47 -0
  365. package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +122 -0
  366. package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +139 -0
  367. package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
  368. package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
  369. package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
  370. package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
  371. package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
  372. package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +17 -0
  373. package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +79 -0
  374. package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +198 -0
  375. package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +88 -0
  376. package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
  377. package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +51 -0
  378. package/runtime/runtime.json +3 -0
  379. package/runtime/server.ts +1 -0
  380. package/runtime/stations.json +37 -0
  381. package/runtime/trains/.keep +0 -0
  382. package/LICENSE +0 -21
  383. package/README.md +0 -78
  384. package/dist/channels/discord.js +0 -148
  385. package/dist/channels/telegram.js +0 -137
  386. package/dist/lib/address.js +0 -21
  387. package/dist/lib/codex-rc.js +0 -274
  388. package/dist/lib/dotenv.js +0 -31
  389. package/dist/log.js +0 -12
  390. package/dist/paths.js +0 -45
  391. package/skills/metro/SKILL.md +0 -122
@@ -0,0 +1,248 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import {
3
+ DiscordTokenError,
4
+ verifyDiscordBotToken,
5
+ } from '@metro-labs/discord-bot/verify';
6
+ import {
7
+ TelegramTokenError,
8
+ verifyTelegramBotToken,
9
+ } from '@metro-labs/telegram-bot/verify';
10
+ import {
11
+ parsePrivateKey,
12
+ ThreemaGatewayError,
13
+ verifyThreemaGateway,
14
+ } from '@metro-labs/threema/verify';
15
+ import { ApiError } from '@metro-labs/http/api-error';
16
+ import { bodyField } from '@metro-labs/http/api-http';
17
+ import { ensureStationDeps } from './runtime-deps.js';
18
+ import { publicBaseOrDefault } from '../files/attach-serve.js';
19
+ import {
20
+ discardXmtpDb,
21
+ newXmtpDbPath,
22
+ verifyXmtpKeyOutOfProcess,
23
+ XmtpAttachError,
24
+ type VerifyXmtpKey,
25
+ } from './attach-xmtp.js';
26
+
27
+ export const ATTACHABLE_STATIONS = [
28
+ 'discord-bot',
29
+ 'telegram-bot',
30
+ 'threema',
31
+ 'xmtp',
32
+ 'webhook',
33
+ ] as const;
34
+
35
+ export type AttachStation = (typeof ATTACHABLE_STATIONS)[number];
36
+
37
+ export class StationAttachError extends ApiError {}
38
+
39
+ export interface AttachInput {
40
+ station: AttachStation;
41
+ token?: unknown;
42
+ gatewayId?: unknown;
43
+ secret?: unknown;
44
+ privateKey?: unknown;
45
+ }
46
+
47
+ export interface OneTimeSecret {
48
+ label: string;
49
+ value: string;
50
+ note: string;
51
+ }
52
+
53
+ export interface PreparedAccount {
54
+ config: Record<string, unknown>;
55
+ identity: Record<string, string>;
56
+ secret?: OneTimeSecret;
57
+ discard?: () => void;
58
+ }
59
+
60
+ const TOKEN_RE = /^[A-Za-z0-9._:-]{8,256}$/;
61
+
62
+ const SECP256K1_ORDER = BigInt(
63
+ '0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
64
+ );
65
+
66
+ export function attachInputOf(station: AttachStation, body: unknown): AttachInput {
67
+ return {
68
+ station,
69
+ token: bodyField(body, 'token'),
70
+ gatewayId: bodyField(body, 'gatewayId'),
71
+ secret: bodyField(body, 'secret'),
72
+ privateKey: bodyField(body, 'privateKey'),
73
+ };
74
+ }
75
+
76
+ export function isAttachStation(raw: unknown): raw is AttachStation {
77
+ return (
78
+ typeof raw === 'string' &&
79
+ (ATTACHABLE_STATIONS as readonly string[]).includes(raw)
80
+ );
81
+ }
82
+
83
+ function requireToken(raw: unknown, label: string): string {
84
+ const token = typeof raw === 'string' ? raw.trim() : '';
85
+ if (token === '')
86
+ throw new StationAttachError(`a ${label} bot token is required`, 400);
87
+ if (!TOKEN_RE.test(token))
88
+ throw new StationAttachError(
89
+ `that does not look like a ${label} bot token`,
90
+ 400,
91
+ );
92
+ return token;
93
+ }
94
+
95
+ function rejected(err: unknown, own: boolean, fallback: string): never {
96
+ throw new StationAttachError(
97
+ own && err instanceof Error ? err.message : fallback,
98
+ 400,
99
+ );
100
+ }
101
+
102
+ async function prepareDiscord(raw: unknown): Promise<PreparedAccount> {
103
+ const token = requireToken(raw, 'Discord');
104
+ try {
105
+ const bot = await verifyDiscordBotToken(token);
106
+ if (!bot.messageContent)
107
+ throw new StationAttachError(
108
+ 'this Discord application does not have the Message Content intent enabled. Turn it on under Bot / Privileged Gateway Intents in the developer portal, then attach it again',
109
+ 400,
110
+ );
111
+ return {
112
+ config: { token },
113
+ identity: { userId: bot.userId, username: bot.username },
114
+ };
115
+ } catch (err) {
116
+ if (err instanceof StationAttachError) throw err;
117
+ return rejected(
118
+ err,
119
+ err instanceof DiscordTokenError,
120
+ 'Discord rejected that bot token',
121
+ );
122
+ }
123
+ }
124
+
125
+ async function prepareTelegram(raw: unknown): Promise<PreparedAccount> {
126
+ const token = requireToken(raw, 'Telegram');
127
+ try {
128
+ const bot = await verifyTelegramBotToken(token);
129
+ return {
130
+ config: { token },
131
+ identity: { botId: String(bot.botId), username: bot.username },
132
+ };
133
+ } catch (err) {
134
+ return rejected(
135
+ err,
136
+ err instanceof TelegramTokenError,
137
+ 'Telegram rejected that bot token',
138
+ );
139
+ }
140
+ }
141
+
142
+ export function newXmtpPrivateKey(): string {
143
+ for (;;) {
144
+ const hex = randomBytes(32).toString('hex');
145
+ const value = BigInt(`0x${hex}`);
146
+ if (value > 0n && value < SECP256K1_ORDER) return `0x${hex}`;
147
+ }
148
+ }
149
+
150
+ async function prepareXmtp(verify: VerifyXmtpKey): Promise<PreparedAccount> {
151
+ const privateKey = newXmtpPrivateKey();
152
+ const dbPath = newXmtpDbPath();
153
+ const identity = await verify(privateKey, dbPath).catch((err: unknown) =>
154
+ rejected(
155
+ err,
156
+ err instanceof XmtpAttachError,
157
+ 'Metro could not open an XMTP inbox with the key it generated, so nothing was attached',
158
+ ),
159
+ );
160
+ return {
161
+ config: { privateKey, dbPath, smart: true },
162
+ identity: { inboxId: identity.inboxId, address: identity.address },
163
+ secret: {
164
+ label: 'xmtp private key',
165
+ value: privateKey,
166
+ note: 'Metro generated this key, opened inbox ' +
167
+ `${identity.inboxId} with it, and stores it with the account. ` +
168
+ 'It is shown once, here, and is never returned by any API again.',
169
+ },
170
+ discard: () => {
171
+ discardXmtpDb(identity.dbPath);
172
+ },
173
+ };
174
+ }
175
+
176
+ export const hookUrl = (webhookId: string, secret: string): string =>
177
+ `${publicBaseOrDefault().replace(/\/+$/, '')}/api/webhooks/${webhookId}/${secret}`;
178
+
179
+ const WEBHOOK_ID_FLOOR = 10n ** 18n;
180
+
181
+ export function newWebhookId(): string {
182
+ const raw = BigInt(`0x${randomBytes(8).toString('hex')}`);
183
+ return String(WEBHOOK_ID_FLOOR + (raw % (9n * WEBHOOK_ID_FLOOR)));
184
+ }
185
+
186
+ export const threemaCallbackUrl = (callbackId: string, token: string): string =>
187
+ `${publicBaseOrDefault().replace(/\/+$/, '')}/api/threema/${callbackId}/${token}`;
188
+
189
+ function requireText(raw: unknown, label: string): string {
190
+ const text = typeof raw === 'string' ? raw.trim() : '';
191
+ if (text === '')
192
+ throw new StationAttachError(`the Threema ${label} is required`, 400);
193
+ return text;
194
+ }
195
+
196
+ async function prepareThreema(input: AttachInput): Promise<PreparedAccount> {
197
+ const gatewayId = requireText(input.gatewayId, 'Gateway ID');
198
+ const secret = requireText(input.secret, 'API secret');
199
+ const privateKey = requireText(input.privateKey, 'private key');
200
+ try {
201
+ const identity = await verifyThreemaGateway({ gatewayId, secret, privateKey });
202
+ const callbackId = newWebhookId();
203
+ const callbackToken = randomBytes(48).toString('base64url');
204
+ return {
205
+ config: {
206
+ gatewayId: identity.gatewayId,
207
+ secret,
208
+ privateKey: parsePrivateKey(privateKey) ?? privateKey,
209
+ callbackId,
210
+ callbackToken,
211
+ createdAt: new Date().toISOString(),
212
+ },
213
+ identity: {
214
+ gatewayId: identity.gatewayId,
215
+ credits: String(identity.credits),
216
+ callback: threemaCallbackUrl(callbackId, callbackToken),
217
+ },
218
+ };
219
+ } catch (err) {
220
+ if (err instanceof StationAttachError) throw err;
221
+ return rejected(
222
+ err,
223
+ err instanceof ThreemaGatewayError,
224
+ 'Threema rejected those Gateway credentials',
225
+ );
226
+ }
227
+ }
228
+
229
+ function prepareWebhook(): PreparedAccount {
230
+ const secret = randomBytes(48).toString('base64url');
231
+ const webhookId = newWebhookId();
232
+ return {
233
+ config: { secret, webhookId, createdAt: new Date().toISOString() },
234
+ identity: { endpoint: hookUrl(webhookId, secret) },
235
+ };
236
+ }
237
+
238
+ export async function prepareAccount(
239
+ input: AttachInput,
240
+ verify: VerifyXmtpKey = verifyXmtpKeyOutOfProcess,
241
+ ): Promise<PreparedAccount> {
242
+ ensureStationDeps(input.station);
243
+ if (input.station === 'discord-bot') return prepareDiscord(input.token);
244
+ if (input.station === 'telegram-bot') return prepareTelegram(input.token);
245
+ if (input.station === 'webhook') return prepareWebhook();
246
+ if (input.station === 'threema') return prepareThreema(input);
247
+ return prepareXmtp(verify);
248
+ }
@@ -0,0 +1,265 @@
1
+ import { setPolicies, type PolicyTarget, type ToolPolicy } from '../policy/policy.js';
2
+ import {
3
+ existsSync,
4
+ mkdirSync,
5
+ readFileSync,
6
+ rmSync,
7
+ writeFileSync,
8
+ } from 'node:fs';
9
+ import { homedir } from 'node:os';
10
+ import { join } from 'node:path';
11
+ import { log } from '@metro-labs/core/log';
12
+ import { writeSecure } from '@metro-labs/core/secure-fs';
13
+ import { trainsDir } from '../boot/paths.js';
14
+ import {
15
+ setAgentMap,
16
+ setAllowlistMap,
17
+ setApproversMap,
18
+ setDisabledAccounts,
19
+ type AgentMap,
20
+ type AgentNameMap,
21
+ type AllowlistMap,
22
+ } from '../agents/map.js';
23
+ import { setKeyMap } from '../agents/keys.js';
24
+ import { STATIONS, type StationName } from '@metro-labs/core/station-names';
25
+
26
+ interface StationTarget {
27
+ file: string;
28
+ fileEnv: string;
29
+ trainImport: string | null;
30
+ }
31
+
32
+ export interface LoadedAccount {
33
+ station: StationName;
34
+ id: string;
35
+ allowlist: string[] | null;
36
+ approvers?: string[];
37
+ enabled?: boolean;
38
+ policy?: ToolPolicy;
39
+ config: Record<string, unknown>;
40
+ }
41
+
42
+ export interface LoadedConnector {
43
+ id: string;
44
+ name: string;
45
+ url: string;
46
+ config: Record<string, unknown>;
47
+ }
48
+
49
+ export interface LoadedAgent {
50
+ id: string;
51
+ name: string;
52
+ accounts: LoadedAccount[];
53
+ key: string | null;
54
+ }
55
+
56
+ const METRO_DIR = join(homedir(), '.metro');
57
+
58
+ const STATION_TARGETS: Record<StationName, StationTarget> = {
59
+ xmtp: {
60
+ file: 'xmtp-accounts.json',
61
+ fileEnv: 'XMTP_ACCOUNTS_FILE',
62
+ trainImport: '@metro-labs/xmtp/train',
63
+ },
64
+ 'telegram-bot': {
65
+ file: 'telegram-bot-accounts.json',
66
+ fileEnv: 'TELEGRAM_BOT_ACCOUNTS_FILE',
67
+ trainImport: '@metro-labs/telegram-bot/train',
68
+ },
69
+ 'telegram': {
70
+ file: 'telegram-accounts.json',
71
+ fileEnv: 'TELEGRAM_ACCOUNTS_FILE',
72
+ trainImport: '@metro-labs/telegram/train',
73
+ },
74
+ 'discord-bot': {
75
+ file: 'discord-bot-accounts.json',
76
+ fileEnv: 'DISCORD_BOT_ACCOUNTS_FILE',
77
+ trainImport: '@metro-labs/discord-bot/train',
78
+ },
79
+ whatsapp: {
80
+ file: 'whatsapp-accounts.json',
81
+ fileEnv: 'WHATSAPP_ACCOUNTS_FILE',
82
+ trainImport: '@metro-labs/whatsapp/train',
83
+ },
84
+ threema: {
85
+ file: 'threema-accounts.json',
86
+ fileEnv: 'THREEMA_ACCOUNTS_FILE',
87
+ trainImport: '@metro-labs/threema/train',
88
+ },
89
+ outlook: {
90
+ file: 'outlook-accounts.json',
91
+ fileEnv: 'OUTLOOK_ACCOUNTS_FILE',
92
+ trainImport: '@metro-labs/outlook/train',
93
+ },
94
+ webhook: {
95
+ file: 'webhook-accounts.json',
96
+ fileEnv: 'WEBHOOK_ACCOUNTS_FILE',
97
+ trainImport: null,
98
+ },
99
+ };
100
+
101
+ export function accountFilePath(station: StationName): string {
102
+ const target = STATION_TARGETS[station];
103
+ return process.env[target.fileEnv] ?? join(METRO_DIR, target.file);
104
+ }
105
+
106
+ export type StationSource = () => Promise<LoadedAgent[]>;
107
+
108
+ export const MOVABLE_STATIONS = new Set<StationName>(
109
+ STATIONS.filter((s) => s !== 'webhook'),
110
+ );
111
+
112
+ function trainStubPath(station: StationName): string {
113
+ return join(trainsDir(), `${station}.ts`);
114
+ }
115
+
116
+ function currentText(path: string): string | null {
117
+ try {
118
+ return readFileSync(path, 'utf8');
119
+ } catch {
120
+ return null;
121
+ }
122
+ }
123
+
124
+ export function writeIfChanged(path: string, content: string): boolean {
125
+ if (currentText(path) === content) return false;
126
+ writeFileSync(path, content);
127
+ return true;
128
+ }
129
+
130
+ interface WrittenStations {
131
+ active: Map<StationName, number>;
132
+ changed: StationName[];
133
+ }
134
+
135
+ const channelPolicies = (list: LoadedAgent[]): [PolicyTarget, ToolPolicy][] =>
136
+ list.flatMap((agent) =>
137
+ agent.accounts.flatMap((a): [PolicyTarget, ToolPolicy][] =>
138
+ a.policy === undefined ? [] : [[{ kind: 'channel', station: a.station, account: a.id }, a.policy]],
139
+ ),
140
+ );
141
+
142
+ const approversOf = (list: LoadedAgent[]): Record<string, string[]> =>
143
+ Object.fromEntries(
144
+ list.flatMap((agent) => agent.accounts.flatMap((a) => (a.approvers === undefined ? [] : [[`${a.station}/${a.id}`, a.approvers]]))),
145
+ );
146
+
147
+ function writeStations(list: LoadedAgent[]): WrittenStations {
148
+ mkdirSync(METRO_DIR, { recursive: true });
149
+ mkdirSync(trainsDir(), { recursive: true });
150
+
151
+ const byStation = new Map<StationName, LoadedAccount[]>();
152
+ const map: AgentMap = {};
153
+ const names: AgentNameMap = {};
154
+ const allow: AllowlistMap = {};
155
+ const disabled = new Set<string>();
156
+ for (const agent of list) {
157
+ names[agent.id] = agent.name;
158
+ for (const a of agent.accounts) {
159
+ if (!(a.station in STATION_TARGETS)) {
160
+ log.error(
161
+ { station: a.station, id: a.id },
162
+ 'unknown station — this metro is older or newer than the row; skipping it',
163
+ );
164
+ continue;
165
+ }
166
+ map[`${a.station}/${a.id}`] = agent.id;
167
+ if (a.allowlist) allow[`${a.station}/${a.id}`] = a.allowlist;
168
+ if (a.enabled === false) {
169
+ disabled.add(`${a.station}/${a.id}`);
170
+ continue;
171
+ }
172
+ const cur = byStation.get(a.station);
173
+ if (cur) cur.push(a);
174
+ else byStation.set(a.station, [a]);
175
+ }
176
+ }
177
+ setAgentMap(map, names);
178
+ setAllowlistMap(allow);
179
+ setApproversMap(approversOf(list));
180
+ setDisabledAccounts(disabled);
181
+ setPolicies('channel', channelPolicies(list));
182
+
183
+ const active = new Map<StationName, number>();
184
+ const changed: StationName[] = [];
185
+ for (const [station, accts] of byStation) {
186
+ const records = accts.map((a) => ({ id: a.id, ...a.config }));
187
+ const path = accountFilePath(station);
188
+ const content = JSON.stringify(records, null, 2);
189
+ if (currentText(path) !== content) {
190
+ writeSecure(path, content);
191
+ changed.push(station);
192
+ }
193
+ const { trainImport } = STATION_TARGETS[station];
194
+ if (trainImport !== null)
195
+ writeIfChanged(trainStubPath(station), `import '${trainImport}';\n`);
196
+ active.set(station, accts.length);
197
+ }
198
+ return { active, changed };
199
+ }
200
+
201
+ const BLANKED = '[]\n';
202
+
203
+ function pruneStations(present: Map<StationName, number>): StationName[] {
204
+ const removed: StationName[] = [];
205
+ for (const station of Object.keys(STATION_TARGETS) as StationName[]) {
206
+ if (present.has(station)) continue;
207
+ const hasTrain = STATION_TARGETS[station].trainImport !== null;
208
+ const stub = trainStubPath(station);
209
+ const stubGone = !hasTrain || !existsSync(stub);
210
+ const file = currentText(accountFilePath(station));
211
+ const fileInert = file === null || file === BLANKED;
212
+ if (stubGone && fileInert) continue;
213
+ if (!fileInert) writeSecure(accountFilePath(station), BLANKED);
214
+ if (hasTrain) rmSync(stub, { force: true });
215
+ removed.push(station);
216
+ }
217
+ return removed;
218
+ }
219
+
220
+ const stationLabels = (m: Map<StationName, number>): string[] =>
221
+ [...m].map(([station, n]) => `${station}(${n})`);
222
+
223
+ function applyKeyMap(list: LoadedAgent[]): void {
224
+ setKeyMap(
225
+ list.flatMap((agent) =>
226
+ agent.key === null ? [] : [{ key: agent.key, agentId: agent.id }],
227
+ ),
228
+ );
229
+ }
230
+
231
+ async function loadAndWrite(
232
+ source: StationSource,
233
+ ): Promise<WrittenStations & { agents: number }> {
234
+ const list = await source();
235
+ applyKeyMap(list);
236
+ return { ...writeStations(list), agents: list.length };
237
+ }
238
+
239
+ export async function materializeFrom(source: StationSource): Promise<void> {
240
+ const { active, agents: found } = await loadAndWrite(source);
241
+ const removed = pruneStations(active);
242
+ log.info(
243
+ { stations: stationLabels(active), agents: found, removed },
244
+ 'materialized station accounts',
245
+ );
246
+ }
247
+
248
+ export interface ReloadedStations {
249
+ active: StationName[];
250
+ removed: StationName[];
251
+ changed: StationName[];
252
+ }
253
+
254
+ export async function reloadFrom(
255
+ source: StationSource,
256
+ ): Promise<ReloadedStations> {
257
+ const { active, changed } = await loadAndWrite(source);
258
+ const removed = pruneStations(active);
259
+ if (removed.length > 0 || changed.length > 0)
260
+ log.info(
261
+ { stations: stationLabels(active), removed, changed },
262
+ 'reloaded station accounts',
263
+ );
264
+ return { active: [...active.keys()], removed, changed };
265
+ }
@@ -0,0 +1,51 @@
1
+ import type { Station } from '@metro-labs/core/stations/types';
2
+ import { Line } from '@metro-labs/core/lines';
3
+ import { errMsg, log } from '@metro-labs/core/log';
4
+ import { xmtpStation } from '@metro-labs/xmtp';
5
+ import { telegramBotStation } from '@metro-labs/telegram-bot';
6
+ import { telegramStation } from '@metro-labs/telegram';
7
+ import { discordBotStation } from '@metro-labs/discord-bot';
8
+ import { whatsappStation } from '@metro-labs/whatsapp';
9
+ import { threemaStation } from '@metro-labs/threema';
10
+ import { outlookStation } from '@metro-labs/outlook';
11
+ import { webhookStation } from '@metro-labs/webhook';
12
+
13
+ export const STATIONS: readonly Station[] = [
14
+ xmtpStation,
15
+ telegramBotStation,
16
+ telegramStation,
17
+ discordBotStation,
18
+ whatsappStation,
19
+ threemaStation,
20
+ outlookStation,
21
+ webhookStation,
22
+ ];
23
+
24
+ export const stationByName = (name: string): Station | undefined =>
25
+ STATIONS.find((s) => s.name === name);
26
+
27
+ export const stationForLine = (line: string): Station | undefined => {
28
+ const s = Line.station(line);
29
+ return s ? stationByName(s) : undefined;
30
+ };
31
+
32
+ export const accountStationNames = (): string[] =>
33
+ STATIONS.filter((s) => s.hasAccounts).map((s) => s.name);
34
+
35
+ export const accountStationCapabilities = (): Record<string, string[]> => {
36
+ const out: Record<string, string[]> = {};
37
+ for (const s of STATIONS)
38
+ if (s.hasAccounts) out[s.name] = [...s.messageVerbs].sort();
39
+ return out;
40
+ };
41
+
42
+ export function forgetOrphans(known: readonly { station: string; id: string }[]): void {
43
+ for (const s of STATIONS) {
44
+ if (s.forgetExcept === undefined) continue;
45
+ try {
46
+ s.forgetExcept(known.filter((a) => a.station === s.name).map((a) => a.id));
47
+ } catch (err) {
48
+ log.warn({ station: s.name, err: errMsg(err) }, 'stations: could not remove the files of detached accounts');
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,82 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { log } from '@metro-labs/core/log';
5
+ import { isRecord } from '@metro-labs/core/is-record';
6
+ import type { StationName } from '@metro-labs/core/station-names';
7
+ import { knownAccounts } from '../agents/map.js';
8
+
9
+ const INSTALL_TIMEOUT_MS = 15 * 60_000;
10
+
11
+ export interface RuntimeManifest {
12
+ core: Record<string, string>;
13
+ stations: Record<string, Record<string, string>>;
14
+ }
15
+
16
+ export interface RuntimeStore {
17
+ dir: string;
18
+ manifest: RuntimeManifest;
19
+ }
20
+
21
+ const ranges = (raw: unknown): Record<string, string> =>
22
+ isRecord(raw)
23
+ ? Object.fromEntries(Object.entries(raw).filter((e): e is [string, string] => typeof e[1] === 'string'))
24
+ : {};
25
+
26
+ export function readManifest(path: string): RuntimeManifest {
27
+ const raw: unknown = JSON.parse(readFileSync(path, 'utf8'));
28
+ if (!isRecord(raw)) throw new Error(`${path} is not a runtime manifest`);
29
+ const stations = isRecord(raw.stations) ? raw.stations : {};
30
+ return {
31
+ core: ranges(raw.core),
32
+ stations: Object.fromEntries(Object.entries(stations).map(([name, deps]) => [name, ranges(deps)])),
33
+ };
34
+ }
35
+
36
+ export function runtimeStore(): RuntimeStore | null {
37
+ const dir = process.env.METRO_RUNTIME_STORE?.trim() ?? '';
38
+ const manifest = process.env.METRO_RUNTIME_MANIFEST?.trim() ?? '';
39
+ if (dir === '' || manifest === '') return null;
40
+ return { dir, manifest: readManifest(manifest) };
41
+ }
42
+
43
+ export function dependenciesFor(manifest: RuntimeManifest, stations: Iterable<string>): Record<string, string> {
44
+ const out: Record<string, string> = { ...manifest.core };
45
+ for (const station of new Set(stations)) Object.assign(out, manifest.stations[station] ?? {});
46
+ return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)));
47
+ }
48
+
49
+ const packageText = (deps: Record<string, string>): string =>
50
+ `${JSON.stringify({ name: 'metro-runtime', private: true, dependencies: deps }, null, 2)}\n`;
51
+
52
+ function current(dir: string): string | null {
53
+ try {
54
+ return readFileSync(join(dir, 'package.json'), 'utf8');
55
+ } catch {
56
+ return null;
57
+ }
58
+ }
59
+
60
+ export function installRuntime(store: RuntimeStore, stations: Iterable<string>): boolean {
61
+ const wanted = packageText(dependenciesFor(store.manifest, stations));
62
+ const installed = existsSync(join(store.dir, 'node_modules', '.metro-installed'));
63
+ if (installed && current(store.dir) === wanted) return false;
64
+ mkdirSync(store.dir, { recursive: true });
65
+ writeFileSync(join(store.dir, 'package.json'), wanted);
66
+ log.info({ dir: store.dir }, 'runtime: installing the channel SDKs this machine needs');
67
+ const run = spawnSync('bun', ['install', '--no-summary', '--no-progress'], {
68
+ cwd: store.dir,
69
+ stdio: ['ignore', 'inherit', 'inherit'],
70
+ timeout: INSTALL_TIMEOUT_MS,
71
+ });
72
+ if (run.error !== undefined || run.status !== 0)
73
+ throw new Error(`bun install failed in ${store.dir}: ${run.error?.message ?? `exit ${String(run.status)}`}`);
74
+ writeFileSync(join(store.dir, 'node_modules', '.metro-installed'), `${new Date().toISOString()}\n`);
75
+ return true;
76
+ }
77
+
78
+ export function ensureStationDeps(station: StationName): void {
79
+ const store = runtimeStore();
80
+ if (store === null) return;
81
+ installRuntime(store, [...knownAccounts().map((a) => a.station), station]);
82
+ }