@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,246 @@
1
+ import { accounts, tg, targetOf } from './accounts.js';
2
+ import { respond } from './wire.js';
3
+ import { errMsg } from '@metro-labs/core/log';
4
+ import { normalizeTelegram } from '@metro-labs/core/stations/messaging-normalize';
5
+ import {
6
+ adminMemberList,
7
+ inaccessibleMemberList,
8
+ type TgChatMember,
9
+ } from './members.js';
10
+ import {
11
+ makeStation,
12
+ type CallMsg,
13
+ type StationHandler,
14
+ } from '@metro-labs/core/stations/station-runtime';
15
+ import { mediaKindOf } from './attachments.js';
16
+ import {
17
+ emitOutbound,
18
+ finishSend,
19
+ MEDIA_METHOD_FIELD,
20
+ sendMedia,
21
+ } from './media-actions.js';
22
+ import { readProfile, setProfile } from './profile.js';
23
+
24
+ export type { CallMsg };
25
+
26
+ const meCache = new Map<string, { id: number; username: string | null }>();
27
+ async function getMe(
28
+ accountId: string,
29
+ ): Promise<{ id: number; username: string | null } | null> {
30
+ const cached = meCache.get(accountId);
31
+ if (cached) return cached;
32
+ try {
33
+ const me = await tg<{ id: number; username?: string }>(
34
+ accountId,
35
+ 'getMe',
36
+ {},
37
+ 10_000,
38
+ );
39
+ const v = { id: me.id, username: me.username ?? null };
40
+ meCache.set(accountId, v);
41
+ return v;
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ async function listAccounts(id: string): Promise<void> {
48
+ const list = await Promise.all(
49
+ [...accounts.values()].map(async (a) => {
50
+ const me = await getMe(a.cfg.id);
51
+ const username = me?.username ?? null;
52
+ return {
53
+ id: a.cfg.id,
54
+ handle: username === null ? null : `@${username}`,
55
+ url: username === null ? null : `https://t.me/${username}`,
56
+ botId: me?.id ?? null,
57
+ username,
58
+ };
59
+ }),
60
+ );
61
+ respond(id, { result: { accounts: list } });
62
+ }
63
+
64
+ interface SendArgs {
65
+ line: string;
66
+ text: string;
67
+ replyTo?: string;
68
+ parseMode?: string;
69
+ buttons?: { text: string; url: string }[][];
70
+ account?: string;
71
+ attachments?: {
72
+ kind?: string;
73
+ path?: string;
74
+ url?: string;
75
+ mime?: string;
76
+ name?: string;
77
+ }[];
78
+ }
79
+
80
+
81
+ type WireAttachment = NonNullable<SendArgs['attachments']>[number];
82
+
83
+ const attachmentSrc = (att: WireAttachment): string | undefined =>
84
+ att.path ?? att.url;
85
+
86
+ const attachmentKind = (att: WireAttachment): string =>
87
+ mediaKindOf(att.kind, att.mime, attachmentSrc(att) ?? att.name);
88
+
89
+ async function sendOneAttachment(
90
+ a: SendArgs,
91
+ att: WireAttachment,
92
+ caption: string | undefined,
93
+ ): Promise<{ accountId: string; message_id: number; kind: string }> {
94
+ const kind = attachmentKind(att);
95
+ const mf = MEDIA_METHOD_FIELD[kind];
96
+ if (!mf) throw new Error(`telegram-bot cannot send a '${kind}' attachment`);
97
+ const sent = await sendMedia(mf.method, mf.field, {
98
+ line: a.line,
99
+ path: attachmentSrc(att),
100
+ caption,
101
+ replyTo: a.replyTo,
102
+ parseMode: a.parseMode,
103
+ account: a.account,
104
+ name: att.name,
105
+ });
106
+ return { ...sent, kind };
107
+ }
108
+
109
+ const captionLabel = (caption: string | undefined, kind: string): string =>
110
+ caption !== undefined && caption !== '' ? caption : `[${kind}]`;
111
+
112
+ async function sendAttachments(id: string, a: SendArgs): Promise<void> {
113
+ const { line, text, replyTo, attachments = [] } = a;
114
+ let last: { accountId: string; message_id: number } | undefined;
115
+ const delivered: string[] = [];
116
+ for (let i = 0; i < attachments.length; i++) {
117
+ const att = attachments[i];
118
+ if (!att) continue;
119
+ const caption = i === 0 ? text : undefined;
120
+ const sent = await sendOneAttachment(a, att, caption);
121
+ last = sent;
122
+ delivered.push(sent.kind);
123
+ emitOutbound(
124
+ sent.accountId,
125
+ line,
126
+ String(sent.message_id),
127
+ captionLabel(caption, sent.kind),
128
+ replyTo,
129
+ );
130
+ }
131
+ if (!last) throw new Error('no attachments were sent');
132
+ respond(id, {
133
+ result: {
134
+ messageId: String(last.message_id),
135
+ account: last.accountId,
136
+ attachments: delivered,
137
+ },
138
+ });
139
+ }
140
+
141
+ async function send(id: string, args: Record<string, unknown>): Promise<void> {
142
+ const a = args as unknown as SendArgs;
143
+ if (a.attachments?.length) {
144
+ await sendAttachments(id, a);
145
+ return;
146
+ }
147
+ const { line, text, replyTo, parseMode, buttons, account } = a;
148
+ const { accountId, chatId, topicId } = targetOf(line, account);
149
+ const body: Record<string, unknown> = { chat_id: chatId, text };
150
+ if (topicId !== undefined) body.message_thread_id = topicId;
151
+ if (replyTo) body.reply_parameters = { message_id: Number(replyTo) };
152
+ if (parseMode) body.parse_mode = parseMode;
153
+ if (buttons) body.reply_markup = { inline_keyboard: buttons };
154
+ const sent = await tg<{ message_id: number }>(accountId, 'sendMessage', body);
155
+ finishSend(id, accountId, line, String(sent.message_id), text, replyTo);
156
+ }
157
+
158
+ async function react(id: string, args: Record<string, unknown>): Promise<void> {
159
+ const { line, messageId, emoji, account } = args as {
160
+ line: string;
161
+ messageId: string;
162
+ emoji: string;
163
+ account?: string;
164
+ };
165
+ const { accountId, chatId } = targetOf(line, account);
166
+ await tg(accountId, 'setMessageReaction', {
167
+ chat_id: chatId,
168
+ message_id: Number(messageId),
169
+ reaction: emoji ? [{ type: 'emoji', emoji }] : [],
170
+ });
171
+ respond(id, { result: { ok: true, account: accountId } });
172
+ }
173
+
174
+ async function edit(id: string, args: Record<string, unknown>): Promise<void> {
175
+ const { line, messageId, text, parseMode, account } = args as {
176
+ line: string;
177
+ messageId: string;
178
+ text: string;
179
+ parseMode?: string;
180
+ account?: string;
181
+ };
182
+ const { accountId, chatId } = targetOf(line, account);
183
+ await tg(accountId, 'editMessageText', {
184
+ chat_id: chatId,
185
+ message_id: Number(messageId),
186
+ text,
187
+ parse_mode: parseMode,
188
+ });
189
+ respond(id, { result: { ok: true, account: accountId } });
190
+ }
191
+
192
+ async function remove(id: string, args: Record<string, unknown>): Promise<void> {
193
+ const { line, messageId, account } = args as {
194
+ line: string;
195
+ messageId: string;
196
+ account?: string;
197
+ };
198
+ const { accountId, chatId } = targetOf(line, account);
199
+ await tg(accountId, 'deleteMessage', {
200
+ chat_id: chatId,
201
+ message_id: Number(messageId),
202
+ });
203
+ respond(id, { result: { ok: true, account: accountId } });
204
+ }
205
+
206
+ async function listMembers(
207
+ id: string,
208
+ args: Record<string, unknown>,
209
+ ): Promise<void> {
210
+ const { line, account } = args as { line: string; account?: string };
211
+ const { accountId, chatId } = targetOf(line, account);
212
+ let admins: TgChatMember[];
213
+ try {
214
+ admins = await tg<TgChatMember[]>(accountId, 'getChatAdministrators', {
215
+ chat_id: chatId,
216
+ });
217
+ } catch (e) {
218
+ respond(id, { result: inaccessibleMemberList(errMsg(e)) });
219
+ return;
220
+ }
221
+ let total: number | null = null;
222
+ try {
223
+ total = await tg<number>(accountId, 'getChatMemberCount', {
224
+ chat_id: chatId,
225
+ });
226
+ } catch {
227
+ total = null;
228
+ }
229
+ respond(id, { result: adminMemberList(admins, total) });
230
+ }
231
+
232
+ const HANDLERS: Record<string, StationHandler> = {
233
+ accounts: (id) => listAccounts(id),
234
+ send,
235
+ react,
236
+ edit,
237
+ delete: remove,
238
+ listMembers,
239
+ set_profile: setProfile,
240
+ profile: readProfile,
241
+ };
242
+
243
+ export const handleCall = makeStation({
244
+ handlers: HANDLERS,
245
+ normalize: normalizeTelegram,
246
+ });
@@ -0,0 +1,128 @@
1
+ import {
2
+ saveBufferToCache,
3
+ assertContentLength,
4
+ } from '@metro-labs/core/stations/attachments';
5
+ import type { SavedAttachment } from '@metro-labs/core/stations/attachments';
6
+ import { tg, accounts } from './accounts.js';
7
+ import type { TgMsg } from './types.js';
8
+
9
+ export type { SavedAttachment };
10
+
11
+ function isOggRef(mime: string | undefined, ref: string | undefined): boolean {
12
+ const ext = ref?.split('?')[0]?.split('.').pop()?.toLowerCase();
13
+ return mime === 'audio/ogg' || ext === 'ogg' || ext === 'oga';
14
+ }
15
+
16
+ function isVoiceRef(
17
+ kind: string | undefined,
18
+ mime: string | undefined,
19
+ ref: string | undefined,
20
+ ): boolean {
21
+ const isOgg = isOggRef(mime, ref);
22
+ if (kind === 'voice') return true;
23
+ if (kind === 'audio') return isOgg;
24
+ return !kind && isOgg;
25
+ }
26
+
27
+ export function mediaKindOf(
28
+ kind: string | undefined,
29
+ mime: string | undefined,
30
+ ref: string | undefined,
31
+ ): 'image' | 'voice' | 'audio' | 'video' | 'document' {
32
+ const m = mime ?? '';
33
+ if (kind === 'image' || m.startsWith('image/')) return 'image';
34
+ if (isVoiceRef(kind, mime, ref)) return 'voice';
35
+ if (kind === 'video' || m.startsWith('video/')) return 'video';
36
+ if (kind === 'audio' || m.startsWith('audio/')) return 'audio';
37
+ return 'document';
38
+ }
39
+
40
+ export interface TgMediaRef {
41
+ fileId: string;
42
+ name?: string;
43
+ mime?: string;
44
+ }
45
+
46
+ const MEDIA_EXTRACTORS: ((m: TgMsg) => TgMediaRef | null)[] = [
47
+ (m) => {
48
+ const largest = m.photo?.[m.photo.length - 1];
49
+ return largest ? { fileId: largest.file_id, mime: 'image/jpeg' } : null;
50
+ },
51
+ (m) =>
52
+ m.document?.file_id
53
+ ? {
54
+ fileId: m.document.file_id,
55
+ name: m.document.file_name,
56
+ mime: m.document.mime_type,
57
+ }
58
+ : null,
59
+ (m) =>
60
+ m.video?.file_id
61
+ ? {
62
+ fileId: m.video.file_id,
63
+ name: m.video.file_name,
64
+ mime: m.video.mime_type ?? 'video/mp4',
65
+ }
66
+ : null,
67
+ (m) =>
68
+ m.animation?.file_id
69
+ ? {
70
+ fileId: m.animation.file_id,
71
+ name: m.animation.file_name,
72
+ mime: m.animation.mime_type,
73
+ }
74
+ : null,
75
+ (m) =>
76
+ m.audio?.file_id
77
+ ? {
78
+ fileId: m.audio.file_id,
79
+ name: m.audio.file_name,
80
+ mime: m.audio.mime_type,
81
+ }
82
+ : null,
83
+ (m) =>
84
+ m.voice?.file_id
85
+ ? { fileId: m.voice.file_id, mime: m.voice.mime_type ?? 'audio/ogg' }
86
+ : null,
87
+ (m) =>
88
+ m.sticker?.file_id
89
+ ? { fileId: m.sticker.file_id, mime: 'image/webp' }
90
+ : null,
91
+ ];
92
+
93
+ export function mediaRefOf(m: TgMsg): TgMediaRef | null {
94
+ for (const extract of MEDIA_EXTRACTORS) {
95
+ const ref = extract(m);
96
+ if (ref) return ref;
97
+ }
98
+ return null;
99
+ }
100
+
101
+ export async function saveTelegramMedia(
102
+ accountId: string,
103
+ ref: TgMediaRef,
104
+ messageId: string,
105
+ index = 0,
106
+ ): Promise<SavedAttachment> {
107
+ const file = await tg<{ file_path?: string }>(accountId, 'getFile', {
108
+ file_id: ref.fileId,
109
+ });
110
+ if (!file.file_path)
111
+ throw new Error(`telegram-bot getFile returned no file_path for ${ref.fileId}`);
112
+ const acct = accounts.get(accountId);
113
+ if (!acct) throw new Error(`unknown account '${accountId}'`);
114
+ const res = await fetch(`${acct.fileApi}/${file.file_path}`, {
115
+ signal: AbortSignal.timeout(60_000),
116
+ });
117
+ if (!res.ok)
118
+ throw new Error(
119
+ `telegram-bot file download ${res.status} for ${file.file_path}`,
120
+ );
121
+ assertContentLength(res.headers.get('content-length'));
122
+ const data = new Uint8Array(await res.arrayBuffer());
123
+ const saved = await saveBufferToCache(data, messageId, index, {
124
+ mime: ref.mime,
125
+ name: ref.name ?? file.file_path,
126
+ });
127
+ return { ...saved, name: ref.name };
128
+ }
@@ -0,0 +1,173 @@
1
+ import { accounts, lineOf } from './accounts.js';
2
+ import { reportAttachment } from '@metro-labs/core/stations/train-events';
3
+ import { mintId } from './wire.js';
4
+ import { mediaRefOf, saveTelegramMedia } from './attachments.js';
5
+ import type { TgMsg, TgReaction, TgReactionCount, TgUser } from './types.js';
6
+
7
+ export type { TgMsg, TgReaction, TgReactionCount };
8
+
9
+ function lineForMsg(
10
+ accountId: string,
11
+ m: TgMsg,
12
+ ): { line: string; topicId?: number } {
13
+ const topicId = m.is_topic_message ? m.message_thread_id : undefined;
14
+ return { line: lineOf(accountId, m.chat.id, topicId), topicId };
15
+ }
16
+
17
+ function stickerTag(s: NonNullable<TgMsg['sticker']>): string {
18
+ const set = s.set_name ? ` · ${s.set_name}` : '';
19
+ return `[sticker${s.emoji ? ` ${s.emoji}` : ''}${set}]`;
20
+ }
21
+
22
+ const TAG_EXTRACTORS: ((m: TgMsg) => string | null)[] = [
23
+ (m) => (m.photo?.length ? '[image]' : null),
24
+ (m) => (m.voice ? '[voice]' : null),
25
+ (m) => (m.audio ? `[audio: ${m.audio.file_name ?? 'audio'}]` : null),
26
+ (m) => (m.video ? `[video: ${m.video.file_name ?? 'video'}]` : null),
27
+ (m) => (m.animation ? `[gif: ${m.animation.file_name ?? 'gif'}]` : null),
28
+ (m) => (m.sticker ? stickerTag(m.sticker) : null),
29
+ (m) =>
30
+ m.document && !m.animation
31
+ ? `[file: ${m.document.file_name ?? 'doc'}]`
32
+ : null,
33
+ (m) =>
34
+ m.location
35
+ ? `[location: ${m.location.latitude}, ${m.location.longitude}]`
36
+ : null,
37
+ (m) => (m.dice ? `[dice ${m.dice.emoji} = ${m.dice.value}]` : null),
38
+ ];
39
+
40
+ function projectText(m: TgMsg): string {
41
+ const tags = TAG_EXTRACTORS.map((f) => f(m)).filter(
42
+ (t): t is string => t !== null,
43
+ );
44
+ return [m.text ?? m.caption, ...tags].filter(Boolean).join(' ');
45
+ }
46
+
47
+ const botIdOf = (accountId: string): number | null => {
48
+ const token = accounts.get(accountId)?.cfg.token ?? '';
49
+ const id = Number(token.split(':')[0]);
50
+ return Number.isInteger(id) && id > 0 ? id : null;
51
+ };
52
+
53
+ function mentionsBot(m: TgMsg, botId: number | null, username: string | undefined): boolean {
54
+ const text = m.text ?? m.caption ?? '';
55
+ const handle = username === undefined ? null : `@${username.toLowerCase()}`;
56
+ return [...(m.entities ?? []), ...(m.caption_entities ?? [])].some((e) => {
57
+ if (e.type === 'text_mention') return botId !== null && e.user?.id === botId;
58
+ if (e.type !== 'mention' || handle === null) return false;
59
+ return text.slice(e.offset, e.offset + e.length).toLowerCase() === handle;
60
+ });
61
+ }
62
+
63
+ function addressing(accountId: string, m: TgMsg): Record<string, unknown> {
64
+ const botId = botIdOf(accountId);
65
+ const topicRoot = m.is_topic_message === true && m.reply_to_message?.message_id === m.message_thread_id;
66
+ const replied = topicRoot ? undefined : m.reply_to_message;
67
+ const replyTo = replied === undefined ? undefined : String(replied.message_id);
68
+ return {
69
+ ...(replyTo === undefined ? {} : { reply_to: replyTo, event: { type: 'reply', replyTo } }),
70
+ mentions_self: mentionsBot(m, botId, accounts.get(accountId)?.username),
71
+ reply_to_self: botId !== null && replied?.from?.id === botId,
72
+ };
73
+ }
74
+
75
+ export const fullName = (u: TgUser | undefined): string | undefined => {
76
+ const name = [u?.first_name, u?.last_name].filter((part) => typeof part === 'string' && part !== '').join(' ');
77
+ return name === '' ? undefined : name;
78
+ };
79
+
80
+ export function envelope(accountId: string, m: TgMsg): Record<string, unknown> {
81
+ const { line } = lineForMsg(accountId, m);
82
+ return {
83
+ kind: 'inbound',
84
+ id: mintId(),
85
+ ts: new Date(m.date * 1000).toISOString(),
86
+ station: 'telegram-bot',
87
+ line,
88
+ line_name: m.chat.title ?? m.chat.first_name ?? undefined,
89
+ from: `metro://telegram-bot/${accountId}/user/${m.from?.id ?? 'unknown'}`,
90
+ from_name: m.from?.username ? `@${m.from.username}` : m.from?.first_name,
91
+ from_display_name: fullName(m.from),
92
+ message_id: String(m.message_id),
93
+ text: projectText(m),
94
+ payload: m,
95
+ is_private: m.chat.type === 'private',
96
+ ...addressing(accountId, m),
97
+ };
98
+ }
99
+
100
+ export function reactionEnvelope(
101
+ accountId: string,
102
+ r: TgReaction,
103
+ ): Record<string, unknown> | null {
104
+ if (r.user?.is_bot) return null;
105
+ const newEmojis = r.new_reaction
106
+ .filter((x) => x.type === 'emoji')
107
+ .map((x) => x.emoji ?? '');
108
+ const oldEmojis = r.old_reaction
109
+ .filter((x) => x.type === 'emoji')
110
+ .map((x) => x.emoji ?? '');
111
+ const added = newEmojis.filter((e) => !oldEmojis.includes(e));
112
+ if (!added.length) return null;
113
+ return {
114
+ kind: 'react',
115
+ id: mintId(),
116
+ ts: new Date(r.date * 1000).toISOString(),
117
+ station: 'telegram-bot',
118
+ line: lineOf(accountId, r.chat.id),
119
+ from: `metro://telegram-bot/${accountId}/user/${r.user?.id ?? 'unknown'}`,
120
+ from_name: r.user?.username ? `@${r.user.username}` : r.user?.first_name,
121
+ from_display_name: fullName(r.user),
122
+ message_id: String(r.message_id),
123
+ emoji: added[0],
124
+ event: { type: 'react', emoji: added[0], targetId: String(r.message_id) },
125
+ is_private: r.chat.type === 'private',
126
+ payload: r,
127
+ };
128
+ }
129
+
130
+ export function reactionCountEnvelope(
131
+ accountId: string,
132
+ rc: TgReactionCount,
133
+ ): Record<string, unknown> | null {
134
+ const emojiReactions = rc.reactions.filter(
135
+ (r) => r.type.type === 'emoji' && r.type.emoji,
136
+ );
137
+ if (!emojiReactions.length) return null;
138
+ const top = emojiReactions.reduce((a, b) =>
139
+ b.total_count > a.total_count ? b : a,
140
+ );
141
+ const emoji = top.type.emoji ?? '';
142
+ if (!emoji) return null;
143
+ return {
144
+ kind: 'react',
145
+ id: mintId(),
146
+ ts: new Date(rc.date * 1000).toISOString(),
147
+ station: 'telegram-bot',
148
+ line: lineOf(accountId, rc.chat.id),
149
+ from: `metro://telegram-bot/${accountId}/user/unknown`,
150
+ message_id: String(rc.message_id),
151
+ emoji,
152
+ event: { type: 'react', emoji, targetId: String(rc.message_id) },
153
+ is_private: rc.chat.type === 'private',
154
+ payload: rc,
155
+ };
156
+ }
157
+
158
+ export function saveMediaAndEmit(
159
+ accountId: string,
160
+ m: TgMsg,
161
+ sourceEnvId: string,
162
+ ): void {
163
+ const ref = mediaRefOf(m);
164
+ if (!ref) return;
165
+ const line = lineForMsg(accountId, m).line;
166
+ reportAttachment(saveTelegramMedia(accountId, ref, String(m.message_id), 0), {
167
+ station: 'telegram-bot',
168
+ account: accountId,
169
+ line,
170
+ forId: sourceEnvId,
171
+ index: 0,
172
+ });
173
+ }
@@ -0,0 +1,103 @@
1
+ import { errMsg } from '@metro-labs/core/log';
2
+ import { accounts, loadAccounts, tg, type Account } from './accounts.js';
3
+ import { emitInbound } from '@metro-labs/core/stations/train-events';
4
+ import { announceAccounts } from '@metro-labs/core/stations/train-boot';
5
+ import {
6
+ envelope,
7
+ reactionCountEnvelope,
8
+ reactionEnvelope,
9
+ saveMediaAndEmit,
10
+ type TgMsg,
11
+ type TgReaction,
12
+ type TgReactionCount,
13
+ } from './format.js';
14
+ import { readCalls } from '@metro-labs/core/trains/protocol';
15
+ import { handleCall } from './actions.js';
16
+
17
+ readCalls('telegram-bot', handleCall);
18
+
19
+ interface Update {
20
+ update_id: number;
21
+ message?: TgMsg;
22
+ message_reaction?: TgReaction;
23
+ message_reaction_count?: TgReactionCount;
24
+ }
25
+
26
+ function handleUpdate(id: string, u: Update): void {
27
+ if (u.message && !u.message.from?.is_bot) {
28
+ const env = envelope(id, u.message);
29
+ emitInbound(id, env);
30
+ saveMediaAndEmit(id, u.message, env.id as string);
31
+ }
32
+ if (u.message_reaction) {
33
+ const env = reactionEnvelope(id, u.message_reaction);
34
+ if (env) emitInbound(id, env);
35
+ }
36
+ if (u.message_reaction_count) {
37
+ const env = reactionCountEnvelope(id, u.message_reaction_count);
38
+ if (env) emitInbound(id, env);
39
+ }
40
+ }
41
+
42
+ async function runAccount(acct: Account): Promise<void> {
43
+ const { id } = acct.cfg;
44
+ try {
45
+ await tg(id, 'deleteWebhook', { drop_pending_updates: false });
46
+ } catch (err) {
47
+ process.stderr.write(
48
+ `telegram-bot[${id}] deleteWebhook: ${errMsg(err)}\n`,
49
+ );
50
+ }
51
+ try {
52
+ const me = await tg<{ username?: string }>(id, 'getMe', {});
53
+ if (typeof me.username === 'string') acct.username = me.username;
54
+ } catch (err) {
55
+ process.stderr.write(`telegram-bot[${id}] getMe: ${errMsg(err)}\n`);
56
+ }
57
+
58
+ for (;;) {
59
+ try {
60
+ const updates = await tg<Update[]>(
61
+ id,
62
+ 'getUpdates',
63
+ {
64
+ offset: acct.offset,
65
+ timeout: 25,
66
+ allowed_updates: [
67
+ 'message',
68
+ 'message_reaction',
69
+ 'message_reaction_count',
70
+ ],
71
+ },
72
+ 60_000,
73
+ );
74
+ for (const u of updates) {
75
+ acct.offset = u.update_id + 1;
76
+ handleUpdate(id, u);
77
+ }
78
+ } catch (err) {
79
+ process.stderr.write(
80
+ `telegram-bot[${id}] poll error: ${errMsg(err)}\n`,
81
+ );
82
+ await new Promise((r) => setTimeout(r, 2_000));
83
+ }
84
+ }
85
+ }
86
+
87
+ const cfgs = loadAccounts();
88
+ for (const cfg of cfgs) {
89
+ accounts.set(cfg.id, {
90
+ cfg,
91
+ api: `https://api.telegram.org/bot${cfg.token}`,
92
+ fileApi: `https://api.telegram.org/file/bot${cfg.token}`,
93
+ offset: 0,
94
+ });
95
+ }
96
+ announceAccounts('telegram-bot', accounts.keys());
97
+
98
+ for (const acct of accounts.values())
99
+ runAccount(acct).catch((err: unknown) => {
100
+ process.stderr.write(
101
+ `telegram-bot[${acct.cfg.id}] account loop failed: ${errMsg(err)}\n`,
102
+ );
103
+ });