@stage-labs/metro 0.1.0-beta.3 → 0.1.0-beta.31

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 (235) hide show
  1. package/dist/api.js +131 -0
  2. package/dist/bedrock-eventstream.js +120 -0
  3. package/dist/bedrock-proxy.js +260 -0
  4. package/dist/bedrock-upstream.js +72 -0
  5. package/dist/bedrock.js +106 -0
  6. package/dist/claude.js +15 -0
  7. package/dist/cli.js +164 -253
  8. package/dist/control.js +158 -0
  9. package/dist/plugin.js +82 -0
  10. package/dist/prompt.js +61 -0
  11. package/dist/runtime.js +94 -0
  12. package/dist/store.js +93 -0
  13. package/dist/tail.js +120 -0
  14. package/dist/update.js +33 -0
  15. package/dist/version.js +97 -0
  16. package/package.json +34 -21
  17. package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
  18. package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +119 -0
  19. package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
  20. package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
  21. package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
  22. package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +210 -0
  23. package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
  24. package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
  25. package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
  26. package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
  27. package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
  28. package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
  29. package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
  30. package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
  31. package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
  32. package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
  33. package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
  34. package/runtime/node_modules/@metro-labs/mcp/package.json +66 -0
  35. package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
  36. package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +367 -0
  37. package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
  38. package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
  39. package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
  40. package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
  41. package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
  42. package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +332 -0
  43. package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-connector-api.ts +150 -0
  44. package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-pair.ts +20 -0
  45. package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
  46. package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +152 -0
  47. package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
  48. package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
  49. package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +129 -0
  50. package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +278 -0
  51. package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +330 -0
  52. package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +125 -0
  53. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +313 -0
  54. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +21 -0
  55. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
  56. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
  57. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +100 -0
  58. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +57 -0
  59. package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +333 -0
  60. package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
  61. package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
  62. package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +117 -0
  63. package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +164 -0
  64. package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +208 -0
  65. package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +359 -0
  66. package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +79 -0
  67. package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
  68. package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +162 -0
  69. package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +120 -0
  70. package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
  71. package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +58 -0
  72. package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +134 -0
  73. package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +214 -0
  74. package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +224 -0
  75. package/runtime/node_modules/@metro-labs/mcp/src/daemon/relay.ts +303 -0
  76. package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +139 -0
  77. package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +105 -0
  78. package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
  79. package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +27 -0
  80. package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +48 -0
  81. package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +218 -0
  82. package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
  83. package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +323 -0
  84. package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
  85. package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
  86. package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +169 -0
  87. package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +295 -0
  88. package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
  89. package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +91 -0
  90. package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +215 -0
  91. package/runtime/node_modules/@metro-labs/mcp/src/db/agent-connectors.ts +143 -0
  92. package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +71 -0
  93. package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
  94. package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +37 -0
  95. package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +147 -0
  96. package/runtime/node_modules/@metro-labs/mcp/src/db/connector-relay.ts +126 -0
  97. package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +308 -0
  98. package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +19 -0
  99. package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
  100. package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +382 -0
  101. package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +300 -0
  102. package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +24 -0
  103. package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +105 -0
  104. package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +117 -0
  105. package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +45 -0
  106. package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +62 -0
  107. package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +18 -0
  108. package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +145 -0
  109. package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +203 -0
  110. package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
  111. package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
  112. package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
  113. package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
  114. package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
  115. package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +192 -0
  116. package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
  117. package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
  118. package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
  119. package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +73 -0
  120. package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
  121. package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
  122. package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +211 -0
  123. package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
  124. package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +212 -0
  125. package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +397 -0
  126. package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
  127. package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +240 -0
  128. package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
  129. package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +80 -0
  130. package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
  131. package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
  132. package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
  133. package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
  134. package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +182 -0
  135. package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
  136. package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
  137. package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
  138. package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
  139. package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
  140. package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
  141. package/runtime/node_modules/@metro-labs/telegram/package.json +28 -0
  142. package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +81 -0
  143. package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
  144. package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
  145. package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
  146. package/runtime/node_modules/@metro-labs/telegram/src/format.ts +141 -0
  147. package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
  148. package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
  149. package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
  150. package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
  151. package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
  152. package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
  153. package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
  154. package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
  155. package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
  156. package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
  157. package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
  158. package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
  159. package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
  160. package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
  161. package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
  162. package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
  163. package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +172 -0
  164. package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
  165. package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
  166. package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
  167. package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
  168. package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +48 -0
  169. package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
  170. package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
  171. package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
  172. package/runtime/node_modules/@metro-labs/webhook/src/station.ts +40 -0
  173. package/runtime/node_modules/@metro-labs/whatsapp/package.json +29 -0
  174. package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +67 -0
  175. package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
  176. package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
  177. package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
  178. package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
  179. package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +318 -0
  180. package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
  181. package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +132 -0
  182. package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
  183. package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
  184. package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
  185. package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
  186. package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
  187. package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
  188. package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
  189. package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
  190. package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +141 -0
  191. package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
  192. package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
  193. package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
  194. package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
  195. package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
  196. package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +113 -0
  197. package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
  198. package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
  199. package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
  200. package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
  201. package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
  202. package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
  203. package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
  204. package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
  205. package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
  206. package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +333 -0
  207. package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +78 -0
  208. package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
  209. package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
  210. package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
  211. package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
  212. package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
  213. package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
  214. package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
  215. package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
  216. package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
  217. package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
  218. package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
  219. package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
  220. package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +39 -0
  221. package/runtime/runtime.json +3 -0
  222. package/runtime/trains/.keep +0 -0
  223. package/LICENSE +0 -21
  224. package/README.md +0 -114
  225. package/dist/agents/claude.js +0 -229
  226. package/dist/agents/codex.js +0 -282
  227. package/dist/agents/types.js +0 -3
  228. package/dist/channels/discord.js +0 -142
  229. package/dist/channels/telegram.js +0 -209
  230. package/dist/lib/dotenv.js +0 -31
  231. package/dist/lib/scope-cache.js +0 -85
  232. package/dist/lib/streaming.js +0 -207
  233. package/dist/log.js +0 -12
  234. package/dist/orchestrator.js +0 -399
  235. package/dist/paths.js +0 -33
@@ -0,0 +1,239 @@
1
+ import { IdentifierKind } from '@xmtp/node-sdk';
2
+ import { convOf, type Account } from './accounts.js';
3
+ import { inboxEthCache, cacheInboxEth } from './wire.js';
4
+ import { TrainError } from '@metro-labs/mcp/train-error';
5
+ import type { MemberList, MetroMember } from '@metro-labs/mcp/stations/types';
6
+ import { readAppData, type GroupLike } from './labels.js';
7
+
8
+ export { parseMemberArgs, resolveMembers } from './member-args.js';
9
+
10
+ type Conv = Awaited<ReturnType<typeof convOf>>['conv'] & object;
11
+ export interface EthId {
12
+ identifier: string;
13
+ identifierKind: IdentifierKind;
14
+ }
15
+
16
+ export function ethIdentifiers(addrs: string[]): EthId[] {
17
+ return addrs.map((a) => ({
18
+ identifier: a,
19
+ identifierKind: IdentifierKind.Ethereum,
20
+ }));
21
+ }
22
+
23
+ export async function createGroupWithMembers(
24
+ acct: Account,
25
+ addrs: string[],
26
+ inboxes: string[],
27
+ opts: { groupName: string; groupDescription?: string; appData?: string },
28
+ ): Promise<GroupLike> {
29
+ if (!addrs.length)
30
+ return acct.client.conversations.createGroup(inboxes, opts);
31
+ const created = await acct.client.conversations.createGroupWithIdentifiers(
32
+ ethIdentifiers(addrs),
33
+ opts,
34
+ );
35
+ if (inboxes.length)
36
+ await (
37
+ created as unknown as { addMembers: (ids: string[]) => Promise<unknown> }
38
+ ).addMembers(inboxes);
39
+ return created;
40
+ }
41
+
42
+ async function resolveAddresses(
43
+ acct: Account,
44
+ inboxIds: string[],
45
+ ): Promise<Record<string, string>> {
46
+ const addresses: Record<string, string> = {};
47
+ const missing = inboxIds.filter((iid) => {
48
+ const cached = inboxEthCache.get(iid);
49
+ if (cached) {
50
+ addresses[iid] = cached;
51
+ return false;
52
+ }
53
+ return true;
54
+ });
55
+ if (!missing.length) return addresses;
56
+ try {
57
+ const states = await acct.client.preferences.fetchInboxStates(missing);
58
+ for (let i = 0; i < missing.length; i++) {
59
+ const key = missing[i];
60
+ if (key === undefined) continue;
61
+ const eth = states[i]?.identifiers.find(
62
+ (it: { identifierKind: IdentifierKind }) =>
63
+ it.identifierKind === IdentifierKind.Ethereum,
64
+ );
65
+ if (eth?.identifier) {
66
+ addresses[key] = eth.identifier;
67
+ cacheInboxEth(key, eth.identifier);
68
+ }
69
+ }
70
+ } catch {
71
+ return addresses;
72
+ }
73
+ return addresses;
74
+ }
75
+
76
+ export async function buildGroupInfo(
77
+ line: string,
78
+ acct: Account,
79
+ conv: Conv,
80
+ ): Promise<Record<string, unknown>> {
81
+ const inboxIds = (await conv.members()).map((m) => m.inboxId);
82
+ const addresses = await resolveAddresses(acct, inboxIds);
83
+ const isDm =
84
+ typeof (conv as unknown as { peerInboxId?: unknown }).peerInboxId ===
85
+ 'function';
86
+ const gn = (conv as unknown as { name?: string | (() => Promise<string>) })
87
+ .name;
88
+ const resolvedName = typeof gn === 'function' ? await gn() : (gn ?? '');
89
+ const { labels, github, preview } = readAppData(
90
+ (conv as unknown as GroupLike).appData,
91
+ );
92
+ return {
93
+ line,
94
+ id: conv.id,
95
+ account: acct.cfg.id,
96
+ version: isDm ? 'dm' : 'group',
97
+ name: resolvedName ?? '',
98
+ memberCount: inboxIds.length,
99
+ labels,
100
+ github,
101
+ preview,
102
+ members: inboxIds.map((iid) => ({
103
+ inboxId: iid,
104
+ address: addresses[iid] ?? null,
105
+ })),
106
+ };
107
+ }
108
+
109
+ interface RawMember {
110
+ inboxId: string;
111
+ permissionLevel?: number;
112
+ }
113
+
114
+ function toMetroMember(
115
+ m: RawMember,
116
+ addresses: Record<string, string>,
117
+ ): MetroMember {
118
+ const member: MetroMember = { id: m.inboxId };
119
+ const address = addresses[m.inboxId];
120
+ if (address) member.address = address;
121
+ if (typeof m.permissionLevel === 'number')
122
+ member.is_admin = m.permissionLevel >= 1;
123
+ return member;
124
+ }
125
+
126
+ export async function buildMemberList(
127
+ acct: Account,
128
+ conv: Conv,
129
+ ): Promise<MemberList> {
130
+ const raw = (await conv.members()) as RawMember[];
131
+ const addresses = await resolveAddresses(
132
+ acct,
133
+ raw.map((m) => m.inboxId),
134
+ );
135
+ return {
136
+ members: raw.map((m) => toMetroMember(m, addresses)),
137
+ capability: { supported: true, complete: true, total: raw.length },
138
+ };
139
+ }
140
+
141
+ interface MemberGroup {
142
+ sync?: () => Promise<unknown>;
143
+ addMembers?: (ids: string[]) => Promise<unknown>;
144
+ addMembersByIdentifiers?: (ids: EthId[]) => Promise<unknown>;
145
+ removeMembers?: (ids: string[]) => Promise<unknown>;
146
+ removeMembersByIdentifiers?: (ids: EthId[]) => Promise<unknown>;
147
+ }
148
+
149
+ async function applyByIdentifiers(
150
+ group: MemberGroup,
151
+ byIdent: ((ids: EthId[]) => Promise<unknown>) | undefined,
152
+ addrs: string[],
153
+ verb: string,
154
+ ): Promise<void> {
155
+ if (!addrs.length) return;
156
+ if (typeof byIdent !== 'function')
157
+ throw new TrainError(
158
+ 'INVALID_ARGS',
159
+ `group does not support ${verb}ByIdentifiers; pass inboxIds instead`,
160
+ );
161
+ await byIdent.call(group, ethIdentifiers(addrs));
162
+ }
163
+
164
+ async function applyByInboxId(
165
+ group: MemberGroup,
166
+ byId: ((ids: string[]) => Promise<unknown>) | undefined,
167
+ inboxes: string[],
168
+ verb: string,
169
+ ): Promise<void> {
170
+ if (!inboxes.length) return;
171
+ if (typeof byId !== 'function')
172
+ throw new TrainError(
173
+ 'INVALID_ARGS',
174
+ `group does not support ${verb} by inboxId`,
175
+ );
176
+ await byId.call(group, inboxes);
177
+ }
178
+
179
+ export async function applyMemberOp(
180
+ conv: Conv,
181
+ addrs: string[],
182
+ inboxes: string[],
183
+ mode: 'add' | 'remove',
184
+ ): Promise<void> {
185
+ const group = conv as unknown as MemberGroup;
186
+ const byId = mode === 'add' ? group.addMembers : group.removeMembers;
187
+ const byIdent =
188
+ mode === 'add'
189
+ ? group.addMembersByIdentifiers
190
+ : group.removeMembersByIdentifiers;
191
+ const verb = mode === 'add' ? 'addMembers' : 'removeMembers';
192
+ if (typeof byId !== 'function' && typeof byIdent !== 'function')
193
+ throw new TrainError(
194
+ 'INVALID_ARGS',
195
+ `${verb} target is not a group (no ${verb})`,
196
+ );
197
+ await group.sync?.().catch(() => undefined);
198
+ await applyByIdentifiers(group, byIdent, addrs, verb);
199
+ await applyByInboxId(group, byId, inboxes, verb);
200
+ }
201
+
202
+ const NAME_CACHE_MAX = 5000;
203
+ const convNameCache = new Map<string, string>();
204
+
205
+ function remember(convId: string, name: string): void {
206
+ convNameCache.delete(convId);
207
+ convNameCache.set(convId, name);
208
+ if (convNameCache.size > NAME_CACHE_MAX) {
209
+ const oldest = convNameCache.keys().next().value;
210
+ if (oldest !== undefined) convNameCache.delete(oldest);
211
+ }
212
+ }
213
+
214
+ async function readName(conv: unknown): Promise<string> {
215
+ try {
216
+ if (typeof (conv as { peerInboxId?: unknown }).peerInboxId === 'function')
217
+ return '';
218
+ const n = (conv as { name?: string | (() => Promise<string>) }).name;
219
+ const resolved = typeof n === 'function' ? await n() : n;
220
+ return typeof resolved === 'string' ? resolved : '';
221
+ } catch {
222
+ return '';
223
+ }
224
+ }
225
+
226
+ export async function groupNameFor(
227
+ convId: string,
228
+ conv: unknown,
229
+ ): Promise<string> {
230
+ const cached = convNameCache.get(convId);
231
+ if (cached !== undefined) return cached;
232
+ const name = await readName(conv);
233
+ remember(convId, name);
234
+ return name;
235
+ }
236
+
237
+ export function warmGroupName(convId: string, name: string | undefined): void {
238
+ if (typeof name === 'string' && name) remember(convId, name);
239
+ }
@@ -0,0 +1,55 @@
1
+ import { accounts } from './accounts.js';
2
+ import { emit, mintId, SELF_URI } from './wire.js';
3
+
4
+ export function emitInbound(
5
+ accountId: string,
6
+ e: Record<string, unknown>,
7
+ ): void {
8
+ const acct = accounts.get(accountId);
9
+ const owner = acct?.cfg.owner;
10
+ const payload = {
11
+ ...(e.payload as Record<string, unknown> | undefined),
12
+ account: accountId,
13
+ };
14
+ emit({
15
+ kind: 'inbound',
16
+ ...e,
17
+ ...(owner ? { to: owner } : {}),
18
+ account: accountId,
19
+ payload,
20
+ });
21
+ }
22
+
23
+ export function emitAttachmentSaved(
24
+ accountId: string,
25
+ line: string,
26
+ sourceMsgId: string,
27
+ index: number,
28
+ save: Promise<{ path: string; mime?: string; name?: string }>,
29
+ ): void {
30
+ void save
31
+ .then((saved) => {
32
+ emitInbound(accountId, {
33
+ id: mintId(),
34
+ ts: new Date().toISOString(),
35
+ station: 'xmtp',
36
+ line,
37
+ from: SELF_URI,
38
+ text: `📎 saved: ${saved.path}`,
39
+ payload: {
40
+ contentType: 'attachmentSaved',
41
+ attachmentFor: sourceMsgId,
42
+ index,
43
+ attachmentPath: saved.path,
44
+ localPath: saved.path,
45
+ mime: saved.mime,
46
+ name: saved.name,
47
+ },
48
+ });
49
+ })
50
+ .catch((err: unknown) =>
51
+ process.stderr.write(
52
+ `xmtp attachment save failed: ${err instanceof Error ? err.message : String(err)}\n`,
53
+ ),
54
+ );
55
+ }
@@ -0,0 +1,333 @@
1
+ import type { Reaction } from '@xmtp/content-type-reaction';
2
+ import type { Reply } from '@xmtp/content-type-reply';
3
+ import { errMsg } from '@metro-labs/mcp/log';
4
+ import { transcribeAndEmit } from './transcribe.js';
5
+ import {
6
+ saveInlineAttachment,
7
+ saveRemoteAttachment,
8
+ type RemoteEntry,
9
+ } from './attachments.js';
10
+ import { emitInbound, emitAttachmentSaved } from './emit-core.js';
11
+
12
+ export interface EnvelopeCtx {
13
+ accountId: string;
14
+ msgId: string;
15
+ line: string;
16
+ baseId: string;
17
+ }
18
+
19
+ interface RemoteAtt {
20
+ url: string;
21
+ filename?: string;
22
+ contentDigest?: string;
23
+ nonce?: Uint8Array;
24
+ salt?: Uint8Array;
25
+ secret?: Uint8Array;
26
+ scheme?: string;
27
+ contentLength?: number;
28
+ }
29
+
30
+ interface AttView {
31
+ kind: string;
32
+ url: string;
33
+ name?: string;
34
+ size?: number;
35
+ remote: {
36
+ contentDigest?: string;
37
+ nonce?: string;
38
+ salt?: string;
39
+ secret?: string;
40
+ scheme?: string;
41
+ };
42
+ }
43
+
44
+ const b64 = (u?: Uint8Array): string | undefined =>
45
+ u ? Buffer.from(u).toString('base64') : undefined;
46
+
47
+ function remoteAtt(r: RemoteAtt, kind: string): AttView {
48
+ return {
49
+ kind,
50
+ url: r.url,
51
+ name: r.filename,
52
+ size: r.contentLength,
53
+ remote: {
54
+ contentDigest: r.contentDigest,
55
+ nonce: b64(r.nonce),
56
+ salt: b64(r.salt),
57
+ secret: b64(r.secret),
58
+ scheme: r.scheme,
59
+ },
60
+ };
61
+ }
62
+
63
+ const IMG_RE = /\.(png|jpg|jpeg|gif|webp|heic)(\?|$)/i;
64
+ const VID_RE = /\.(mp4|mov|webm|m4v)(\?|$)/i;
65
+
66
+ function attKindFor(r: RemoteAtt): string {
67
+ const isImg = IMG_RE.test(r.url) || IMG_RE.test(r.filename ?? '');
68
+ if (isImg) return 'image';
69
+ const isVid = VID_RE.test(r.url) || VID_RE.test(r.filename ?? '');
70
+ return isVid ? 'video' : 'file';
71
+ }
72
+
73
+ function singleAttText(one: AttView): string {
74
+ if (one.kind === 'video') return `🎥 ${one.name ?? 'video'}`;
75
+ return `[${one.kind}: ${one.name ?? one.url}]`;
76
+ }
77
+
78
+ function multiAttText(attachments: AttView[]): string {
79
+ const total = attachments.length;
80
+ const imgCount = attachments.filter((a) => a.kind === 'image').length;
81
+ const vidCount = attachments.filter((a) => a.kind === 'video').length;
82
+ if (imgCount === total && imgCount > 1) return `📷 ${imgCount} photos`;
83
+ if (vidCount === total && vidCount > 1) return `🎥 ${vidCount} videos`;
84
+ const one = attachments[0];
85
+ if (total === 1 && one) return singleAttText(one);
86
+ return `📎 ${total} attachments`;
87
+ }
88
+
89
+ function multiRemotePayload(
90
+ base: Record<string, unknown>,
91
+ typeId: string,
92
+ c: object,
93
+ ): Record<string, unknown> {
94
+ const m = c as { attachments?: RemoteAtt[] };
95
+ const list = Array.isArray(m.attachments) ? m.attachments : [];
96
+ const attachments = list.map((r) => remoteAtt(r, attKindFor(r)));
97
+ return {
98
+ ...base,
99
+ text: multiAttText(attachments),
100
+ payload: { contentType: typeId, attachments },
101
+ };
102
+ }
103
+
104
+ function reactionPayload(
105
+ base: Record<string, unknown>,
106
+ r: Reaction,
107
+ ): Record<string, unknown> {
108
+ const schemaStr = ((): string | undefined => {
109
+ const s = (r as { schema?: unknown }).schema;
110
+ if (typeof s === 'string') return s.toLowerCase();
111
+ if (s === 3) return 'custom';
112
+ if (s === 2) return 'shortcode';
113
+ if (s === 1) return 'unicode';
114
+ return undefined;
115
+ })();
116
+ const actionStr = ((): string | undefined => {
117
+ const a = (r as { action?: unknown }).action;
118
+ if (typeof a === 'string') return a.toLowerCase();
119
+ if (a === 2) return 'removed';
120
+ if (a === 1) return 'added';
121
+ return undefined;
122
+ })();
123
+ const removed = actionStr === 'removed';
124
+ return {
125
+ ...base,
126
+ text: `[react ${r.content ?? ''}${removed ? ' (removed)' : ''}]`,
127
+ event: { type: 'react', emoji: r.content, targetId: r.reference },
128
+ payload: {
129
+ contentType: 'reaction',
130
+ reactTo: r.reference,
131
+ emoji: r.content,
132
+ content: r.content,
133
+ schema: schemaStr,
134
+ action: actionStr,
135
+ removed,
136
+ optionIndex: schemaStr === 'custom' ? Number(r.content) : undefined,
137
+ },
138
+ };
139
+ }
140
+
141
+ function replyPayload(
142
+ base: Record<string, unknown>,
143
+ typeId: string,
144
+ c: Reply,
145
+ ): Record<string, unknown> {
146
+ return {
147
+ ...base,
148
+ text:
149
+ typeof c.content === 'string'
150
+ ? c.content
151
+ : `[reply with ${c.contentType?.typeId ?? 'unknown'}]`,
152
+ event: { type: 'reply', replyTo: c.reference },
153
+ payload: {
154
+ contentType: typeId,
155
+ replyTo: c.reference,
156
+ replyContentType: c.contentType?.typeId,
157
+ },
158
+ };
159
+ }
160
+
161
+ function attachmentKind(mime: string | undefined): string {
162
+ if (mime?.startsWith('image/')) return 'image';
163
+ if (mime?.startsWith('audio/')) return 'audio';
164
+ if (mime?.startsWith('video/')) return 'video';
165
+ return 'file';
166
+ }
167
+
168
+ function inlineAttachmentPayload(
169
+ base: Record<string, unknown>,
170
+ typeId: string,
171
+ c: { filename?: string; mimeType: string; content: Uint8Array },
172
+ ctx: EnvelopeCtx,
173
+ ): Record<string, unknown> {
174
+ const kind = attachmentKind(c.mimeType);
175
+ const dataB64 = Buffer.from(c.content).toString('base64');
176
+ const out = {
177
+ ...base,
178
+ text: `[${kind}: ${c.filename ?? 'attachment'}]`,
179
+ payload: {
180
+ contentType: typeId,
181
+ attachments: [{ kind, mime: c.mimeType, name: c.filename, dataB64 }],
182
+ },
183
+ };
184
+ if (kind === 'audio')
185
+ transcribeAndEmit(
186
+ c.content,
187
+ ctx.line,
188
+ ctx.accountId,
189
+ ctx.baseId,
190
+ emitInbound,
191
+ ).catch((err: unknown) => {
192
+ process.stderr.write(
193
+ `xmtp[${ctx.accountId}] voice transcription failed: ${errMsg(err)}\n`,
194
+ );
195
+ });
196
+ emitAttachmentSaved(
197
+ ctx.accountId,
198
+ ctx.line,
199
+ ctx.baseId,
200
+ 0,
201
+ saveInlineAttachment(c, ctx.msgId, 0),
202
+ );
203
+ return out;
204
+ }
205
+
206
+ function remoteStaticPayload(
207
+ base: Record<string, unknown>,
208
+ typeId: string,
209
+ c: RemoteAtt,
210
+ ctx: EnvelopeCtx,
211
+ ): Record<string, unknown> {
212
+ const kind = IMG_RE.test(c.url) ? 'image' : 'file';
213
+ emitAttachmentSaved(
214
+ ctx.accountId,
215
+ ctx.line,
216
+ ctx.baseId,
217
+ 0,
218
+ saveRemoteAttachment(c, ctx.msgId, 0),
219
+ );
220
+ return {
221
+ ...base,
222
+ text: `[${kind}: ${c.filename ?? c.url}]`,
223
+ payload: { contentType: typeId, attachments: [remoteAtt(c, kind)] },
224
+ };
225
+ }
226
+
227
+ function multiRemoteEnvelope(
228
+ base: Record<string, unknown>,
229
+ typeId: string,
230
+ c: object,
231
+ ctx: EnvelopeCtx,
232
+ ): Record<string, unknown> {
233
+ const m = c as { attachments?: RemoteEntry[] };
234
+ (Array.isArray(m.attachments) ? m.attachments : []).forEach((r, i) => {
235
+ emitAttachmentSaved(
236
+ ctx.accountId,
237
+ ctx.line,
238
+ ctx.baseId,
239
+ i,
240
+ saveRemoteAttachment(r, ctx.msgId, i),
241
+ );
242
+ });
243
+ return multiRemotePayload(base, typeId, c);
244
+ }
245
+
246
+ function pollPayload(
247
+ base: Record<string, unknown>,
248
+ typeId: string,
249
+ c: object,
250
+ ): Record<string, unknown> {
251
+ const p = c as { question?: string };
252
+ return {
253
+ ...base,
254
+ text: `Poll: ${p.question ?? ''}`,
255
+ payload: { contentType: typeId, poll: c },
256
+ };
257
+ }
258
+
259
+ function labeledPayload(
260
+ base: Record<string, unknown>,
261
+ typeId: string,
262
+ text: string,
263
+ key: string,
264
+ c: unknown,
265
+ ): Record<string, unknown> {
266
+ return { ...base, text, payload: { contentType: typeId, [key]: c } };
267
+ }
268
+
269
+ function attachmentEnvelope(
270
+ base: Record<string, unknown>,
271
+ typeId: string,
272
+ c: object,
273
+ ctx: EnvelopeCtx,
274
+ ): Record<string, unknown> | undefined {
275
+ if (typeId === 'attachment')
276
+ return inlineAttachmentPayload(
277
+ base,
278
+ typeId,
279
+ c as { filename?: string; mimeType: string; content: Uint8Array },
280
+ ctx,
281
+ );
282
+ if (typeId === 'remoteStaticAttachment')
283
+ return remoteStaticPayload(base, typeId, c as RemoteAtt, ctx);
284
+ if (
285
+ typeId === 'multiRemoteStaticAttachment' ||
286
+ typeId === 'multiRemoteAttachment'
287
+ )
288
+ return multiRemoteEnvelope(base, typeId, c, ctx);
289
+ return undefined;
290
+ }
291
+
292
+ function refEnvelope(
293
+ base: Record<string, unknown>,
294
+ typeId: string | undefined,
295
+ c: object,
296
+ ): Record<string, unknown> | undefined {
297
+ if (typeId === 'walletSendCalls')
298
+ return labeledPayload(
299
+ base,
300
+ typeId,
301
+ 'Payment request',
302
+ 'walletSendCalls',
303
+ c,
304
+ );
305
+ if (typeId === 'transactionReference')
306
+ return labeledPayload(
307
+ base,
308
+ typeId,
309
+ 'Transaction',
310
+ 'transactionReference',
311
+ c,
312
+ );
313
+ if (typeId === 'signatureRequest')
314
+ return labeledPayload(base, typeId, 'Signature request', 'signatureRequest', c);
315
+ if (typeId === 'signatureReference')
316
+ return labeledPayload(base, typeId, 'Signature', 'signatureReference', c);
317
+ return undefined;
318
+ }
319
+
320
+ export function typedEnvelope(
321
+ base: Record<string, unknown>,
322
+ typeId: string | undefined,
323
+ c: object,
324
+ ctx: EnvelopeCtx,
325
+ ): Record<string, unknown> | undefined {
326
+ if (typeId === 'reaction') return reactionPayload(base, c as Reaction);
327
+ if (typeId === 'reply') return replyPayload(base, typeId, c as Reply);
328
+ if (typeId === 'poll') return pollPayload(base, typeId, c);
329
+ return (
330
+ attachmentEnvelope(base, typeId ?? '', c, ctx) ??
331
+ refEnvelope(base, typeId, c)
332
+ );
333
+ }
@@ -0,0 +1,78 @@
1
+ import type { Conversation, DecodedMessage } from '@xmtp/node-sdk';
2
+ import { lineOf, parseLine } from './accounts.js';
3
+ import { emit, mintId, rememberUid, SELF_URI } from './wire.js';
4
+ import { fcmPushToAll } from './push.js';
5
+ import { emitInbound, emitAttachmentSaved } from './emit-core.js';
6
+ import { typedEnvelope, type EnvelopeCtx } from './emit-payloads.js';
7
+ import type { StructuredEvent } from '@metro-labs/mcp/events';
8
+
9
+ export { emitInbound, emitAttachmentSaved };
10
+
11
+ export function envelope(
12
+ accountId: string,
13
+ msg: DecodedMessage,
14
+ conv: Conversation,
15
+ ): Record<string, unknown> {
16
+ const typeId = msg.contentType?.typeId;
17
+ const c = msg.content;
18
+ const line = lineOf(accountId, conv.id);
19
+ const base = {
20
+ id: mintId(),
21
+ ts: msg.sentAt.toISOString(),
22
+ station: 'xmtp',
23
+ line,
24
+ from: `metro://xmtp/${accountId}/user/${msg.senderInboxId}`,
25
+ message_id: msg.id,
26
+ };
27
+ rememberUid(base.id, msg.id);
28
+ if (typeof c === 'string')
29
+ return { ...base, text: c, payload: { contentType: typeId } };
30
+ if (c && typeof c === 'object') {
31
+ const ctx: EnvelopeCtx = {
32
+ accountId,
33
+ msgId: msg.id,
34
+ line,
35
+ baseId: base.id,
36
+ };
37
+ const out = typedEnvelope(base, typeId, c, ctx);
38
+ if (out) return out;
39
+ }
40
+ return {
41
+ ...base,
42
+ text: `[${typeId ?? 'unknown'} payload]`,
43
+ payload: { contentType: typeId },
44
+ };
45
+ }
46
+
47
+ export function emitOutbound(
48
+ accountId: string,
49
+ line: string,
50
+ messageId: string,
51
+ text: string,
52
+ event?: StructuredEvent,
53
+ ): void {
54
+ const uid = mintId();
55
+ rememberUid(uid, messageId);
56
+ emit({
57
+ kind: 'outbound',
58
+ id: uid,
59
+ ts: new Date().toISOString(),
60
+ station: 'xmtp',
61
+ line,
62
+ from: SELF_URI,
63
+ to: line,
64
+ message_id: messageId,
65
+ text,
66
+ account: accountId,
67
+ ...(event ? { event } : {}),
68
+ payload: { account: accountId },
69
+ });
70
+ void (async (): Promise<void> => {
71
+ const data: Record<string, string> = { line, messageId };
72
+ {
73
+ const p = parseLine(line);
74
+ if (p) data.convId = p.convId.toLowerCase();
75
+ }
76
+ await fcmPushToAll(accountId, data);
77
+ })().catch(() => undefined);
78
+ }