@stage-labs/metro 0.1.0-beta.7 → 0.1.0-beta.71
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.
- package/dist/api.js +14 -0
- package/dist/bedrock-eventstream.js +120 -0
- package/dist/bedrock-proxy.js +260 -0
- package/dist/bedrock-upstream.js +72 -0
- package/dist/bedrock.js +37 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +107 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/node-name.js +51 -0
- package/dist/plugin.js +80 -0
- package/dist/provider-flags.js +9 -0
- package/dist/runtime-install.js +84 -0
- package/dist/runtime.js +70 -0
- package/dist/serve.js +147 -0
- package/dist/service.js +224 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -25
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +302 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +128 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +204 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/bundle-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/claude-api.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/claude-files.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +307 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +70 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +330 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/control-api.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +390 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity-routes.ts +54 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-mode.ts +187 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/machine-api.ts +61 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/model-api.ts +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +109 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/relay.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/server-types.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/servers-api.ts +80 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-api.ts +71 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/terminal-ws.ts +114 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +352 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/update-api.ts +116 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/vault-api.ts +78 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/vault-types.ts +12 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +84 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +144 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-relay.ts +39 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +69 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/file-admin.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/file-source.ts +155 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/local-connectors.ts +277 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +253 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +33 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/servers.ts +83 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/stations.ts +12 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/vault.ts +109 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/bedrock.ts +252 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/codex.ts +211 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/forward.ts +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/gateway.ts +193 -0
- package/runtime/node_modules/@metro-labs/mcp/src/gateway/model-config.ts +180 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +141 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +184 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +327 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/stations.json +36 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -193
- package/dist/cache.js +0 -75
- package/dist/cli/actions.js +0 -139
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -159
- package/dist/cli/skill.js +0 -62
- package/dist/cli/util.js +0 -72
- package/dist/codex-rc.js +0 -236
- package/dist/dispatcher.js +0 -95
- package/dist/history.js +0 -84
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/paths.js +0 -68
- package/dist/stations/discord.js +0 -200
- package/dist/stations/index.js +0 -73
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -213
- package/docs/agents.md +0 -185
- package/docs/uri-scheme.md +0 -71
- package/skills/metro/SKILL.md +0 -241
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import { accounts, loadAccounts, tg, type Account } from './accounts.js';
|
|
3
|
+
import { emit } from './wire.js';
|
|
4
|
+
import {
|
|
5
|
+
emitInbound,
|
|
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/mcp/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(emit, id, env);
|
|
30
|
+
saveMediaAndEmit(emit, 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(emit, id, env);
|
|
35
|
+
}
|
|
36
|
+
if (u.message_reaction_count) {
|
|
37
|
+
const env = reactionCountEnvelope(id, u.message_reaction_count);
|
|
38
|
+
if (env) emitInbound(emit, 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
|
+
if (accounts.size === 0) {
|
|
97
|
+
process.stderr.write('telegram-bot: no accounts booted, exiting\n');
|
|
98
|
+
process.exit(2);
|
|
99
|
+
}
|
|
100
|
+
process.stderr.write(
|
|
101
|
+
`telegram-bot train ready (multi) — ${accounts.size} account(s): ${[...accounts.keys()].join(', ')}\n`,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
for (const acct of accounts.values())
|
|
105
|
+
runAccount(acct).catch((err: unknown) => {
|
|
106
|
+
process.stderr.write(
|
|
107
|
+
`telegram-bot[${acct.cfg.id}] account loop failed: ${errMsg(err)}\n`,
|
|
108
|
+
);
|
|
109
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { tg, tgForm, targetOf } from './accounts.js';
|
|
2
|
+
import { emit, mintId, respond, SELF_URI } from './wire.js';
|
|
3
|
+
import { appendFile } from '@metro-labs/mcp/stations/attachments';
|
|
4
|
+
|
|
5
|
+
export function emitOutbound(
|
|
6
|
+
accountId: string,
|
|
7
|
+
line: string,
|
|
8
|
+
messageId: string,
|
|
9
|
+
text: string,
|
|
10
|
+
replyTo?: string,
|
|
11
|
+
): void {
|
|
12
|
+
emit({
|
|
13
|
+
kind: 'outbound',
|
|
14
|
+
id: mintId(),
|
|
15
|
+
ts: new Date().toISOString(),
|
|
16
|
+
station: 'telegram-bot',
|
|
17
|
+
line,
|
|
18
|
+
from: SELF_URI,
|
|
19
|
+
to: line,
|
|
20
|
+
message_id: messageId,
|
|
21
|
+
text,
|
|
22
|
+
reply_to: replyTo,
|
|
23
|
+
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
24
|
+
account: accountId,
|
|
25
|
+
payload: { account: accountId },
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function finishSend(
|
|
30
|
+
id: string,
|
|
31
|
+
accountId: string,
|
|
32
|
+
line: string,
|
|
33
|
+
messageId: string,
|
|
34
|
+
label: string,
|
|
35
|
+
replyTo?: string,
|
|
36
|
+
extra?: Record<string, unknown>,
|
|
37
|
+
): void {
|
|
38
|
+
emitOutbound(accountId, line, messageId, label, replyTo);
|
|
39
|
+
respond(id, {
|
|
40
|
+
result: { messageId, account: accountId, ...extra },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface MediaArgs {
|
|
45
|
+
line: string;
|
|
46
|
+
path: string;
|
|
47
|
+
caption?: string;
|
|
48
|
+
replyTo?: string;
|
|
49
|
+
parseMode?: string;
|
|
50
|
+
account?: string;
|
|
51
|
+
name?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function sendMedia(
|
|
55
|
+
method: string,
|
|
56
|
+
fieldName: string,
|
|
57
|
+
args: Record<string, unknown>,
|
|
58
|
+
): Promise<{ accountId: string; message_id: number }> {
|
|
59
|
+
const {
|
|
60
|
+
line,
|
|
61
|
+
path,
|
|
62
|
+
caption,
|
|
63
|
+
replyTo,
|
|
64
|
+
parseMode,
|
|
65
|
+
account,
|
|
66
|
+
name: fileName,
|
|
67
|
+
} = args as unknown as MediaArgs;
|
|
68
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
69
|
+
const form = new FormData();
|
|
70
|
+
form.append('chat_id', String(chatId));
|
|
71
|
+
if (topicId !== undefined) form.append('message_thread_id', String(topicId));
|
|
72
|
+
if (caption) form.append('caption', caption);
|
|
73
|
+
if (parseMode) form.append('parse_mode', parseMode);
|
|
74
|
+
if (replyTo)
|
|
75
|
+
form.append(
|
|
76
|
+
'reply_parameters',
|
|
77
|
+
JSON.stringify({ message_id: Number(replyTo) }),
|
|
78
|
+
);
|
|
79
|
+
const name = fileName ?? path.split('/').pop() ?? fieldName;
|
|
80
|
+
await appendFile(form, fieldName, path, name);
|
|
81
|
+
const r = await tgForm<{ message_id: number }>(accountId, method, form);
|
|
82
|
+
return { accountId, message_id: r.message_id };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function media(
|
|
86
|
+
id: string,
|
|
87
|
+
method: string,
|
|
88
|
+
field: string,
|
|
89
|
+
label: string,
|
|
90
|
+
args: Record<string, unknown>,
|
|
91
|
+
): Promise<void> {
|
|
92
|
+
const { accountId, message_id } = await sendMedia(method, field, args);
|
|
93
|
+
const line = (args as { line: string }).line;
|
|
94
|
+
finishSend(
|
|
95
|
+
id,
|
|
96
|
+
accountId,
|
|
97
|
+
line,
|
|
98
|
+
String(message_id),
|
|
99
|
+
label,
|
|
100
|
+
args.replyTo as string | undefined,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const MEDIA_METHOD_FIELD: Record<
|
|
105
|
+
string,
|
|
106
|
+
{ method: string; field: string }
|
|
107
|
+
> = {
|
|
108
|
+
image: { method: 'sendPhoto', field: 'photo' },
|
|
109
|
+
voice: { method: 'sendVoice', field: 'voice' },
|
|
110
|
+
audio: { method: 'sendAudio', field: 'audio' },
|
|
111
|
+
video: { method: 'sendVideo', field: 'video' },
|
|
112
|
+
document: { method: 'sendDocument', field: 'document' },
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export async function sendDice(
|
|
116
|
+
id: string,
|
|
117
|
+
args: Record<string, unknown>,
|
|
118
|
+
): Promise<void> {
|
|
119
|
+
const {
|
|
120
|
+
line,
|
|
121
|
+
emoji = '\U0001F3B2',
|
|
122
|
+
account,
|
|
123
|
+
} = args as { line: string; emoji?: string; account?: string };
|
|
124
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
125
|
+
const body: Record<string, unknown> = { chat_id: chatId, emoji };
|
|
126
|
+
if (topicId !== undefined) body.message_thread_id = topicId;
|
|
127
|
+
const r = await tg<{ message_id: number; dice?: { value: number } }>(
|
|
128
|
+
accountId,
|
|
129
|
+
'sendDice',
|
|
130
|
+
body,
|
|
131
|
+
);
|
|
132
|
+
finishSend(
|
|
133
|
+
id,
|
|
134
|
+
accountId,
|
|
135
|
+
line,
|
|
136
|
+
String(r.message_id),
|
|
137
|
+
`[dice ${emoji} = ${r.dice?.value ?? '?'}]`,
|
|
138
|
+
undefined,
|
|
139
|
+
{ value: r.dice?.value },
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function sendLocation(
|
|
144
|
+
id: string,
|
|
145
|
+
args: Record<string, unknown>,
|
|
146
|
+
): Promise<void> {
|
|
147
|
+
const { line, latitude, longitude, account } = args as {
|
|
148
|
+
line: string;
|
|
149
|
+
latitude: number;
|
|
150
|
+
longitude: number;
|
|
151
|
+
account?: string;
|
|
152
|
+
};
|
|
153
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
154
|
+
const r = await tg<{ message_id: number }>(accountId, 'sendLocation', {
|
|
155
|
+
chat_id: chatId,
|
|
156
|
+
latitude,
|
|
157
|
+
longitude,
|
|
158
|
+
});
|
|
159
|
+
finishSend(
|
|
160
|
+
id,
|
|
161
|
+
accountId,
|
|
162
|
+
line,
|
|
163
|
+
String(r.message_id),
|
|
164
|
+
`[location: ${latitude}, ${longitude}]`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { MemberList, MetroMember } from '@metro-labs/mcp/stations/types';
|
|
2
|
+
|
|
3
|
+
export interface TgUser {
|
|
4
|
+
id: number;
|
|
5
|
+
username?: string;
|
|
6
|
+
first_name?: string;
|
|
7
|
+
last_name?: string;
|
|
8
|
+
is_bot?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface TgChatMember {
|
|
12
|
+
user: TgUser;
|
|
13
|
+
status: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function mapTgMember(m: TgChatMember): MetroMember {
|
|
17
|
+
const u = m.user;
|
|
18
|
+
const member: MetroMember = { id: String(u.id) };
|
|
19
|
+
if (u.username) member.name = u.username;
|
|
20
|
+
const display = [u.first_name, u.last_name].filter(Boolean).join(' ');
|
|
21
|
+
if (display) member.display_name = display;
|
|
22
|
+
member.is_admin = m.status === 'creator' || m.status === 'administrator';
|
|
23
|
+
member.is_bot = Boolean(u.is_bot);
|
|
24
|
+
return member;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const BOT_LIMIT_REASON =
|
|
28
|
+
'Telegram Bot API cannot enumerate full membership; returning administrators only.';
|
|
29
|
+
|
|
30
|
+
export function adminMemberList(
|
|
31
|
+
admins: TgChatMember[],
|
|
32
|
+
total: number | null,
|
|
33
|
+
): MemberList {
|
|
34
|
+
return {
|
|
35
|
+
members: admins.map(mapTgMember),
|
|
36
|
+
capability: {
|
|
37
|
+
supported: true,
|
|
38
|
+
complete: false,
|
|
39
|
+
reason: BOT_LIMIT_REASON,
|
|
40
|
+
...(total != null ? { total } : {}),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function inaccessibleMemberList(reason: string): MemberList {
|
|
46
|
+
return { members: [], capability: { supported: false, complete: false, reason } };
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Station, Verb } from '@metro-labs/mcp/stations/types';
|
|
2
|
+
|
|
3
|
+
export const telegramBotStation: Station = {
|
|
4
|
+
name: 'telegram-bot',
|
|
5
|
+
hasAccounts: true,
|
|
6
|
+
hasTrain: true,
|
|
7
|
+
messageVerbs: new Set<Verb>([
|
|
8
|
+
'send',
|
|
9
|
+
'reply',
|
|
10
|
+
'react',
|
|
11
|
+
'unreact',
|
|
12
|
+
'edit',
|
|
13
|
+
'delete',
|
|
14
|
+
]),
|
|
15
|
+
attachmentMode: 'canonical',
|
|
16
|
+
tools: [],
|
|
17
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface TgEntity {
|
|
2
|
+
type: string;
|
|
3
|
+
offset: number;
|
|
4
|
+
length: number;
|
|
5
|
+
user?: { id: number };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface TgMsg {
|
|
9
|
+
entities?: TgEntity[];
|
|
10
|
+
caption_entities?: TgEntity[];
|
|
11
|
+
reply_to_message?: { message_id: number; from?: { id: number; username?: string; is_bot?: boolean } };
|
|
12
|
+
message_id: number;
|
|
13
|
+
date: number;
|
|
14
|
+
chat: { id: number; type: string; title?: string; first_name?: string };
|
|
15
|
+
from?: {
|
|
16
|
+
id: number;
|
|
17
|
+
username?: string;
|
|
18
|
+
first_name?: string;
|
|
19
|
+
is_bot?: boolean;
|
|
20
|
+
};
|
|
21
|
+
text?: string;
|
|
22
|
+
caption?: string;
|
|
23
|
+
message_thread_id?: number;
|
|
24
|
+
is_topic_message?: boolean;
|
|
25
|
+
photo?: { file_id: string; file_size?: number }[];
|
|
26
|
+
document?: { file_name?: string; file_id?: string; mime_type?: string };
|
|
27
|
+
voice?: { file_id?: string; duration?: number; mime_type?: string };
|
|
28
|
+
audio?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
29
|
+
video?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
30
|
+
animation?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
31
|
+
sticker?: { file_id?: string; emoji?: string; set_name?: string };
|
|
32
|
+
location?: { latitude: number; longitude: number };
|
|
33
|
+
dice?: { emoji: string; value: number };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TgReaction {
|
|
37
|
+
chat: { id: number; type: string };
|
|
38
|
+
message_id: number;
|
|
39
|
+
user?: {
|
|
40
|
+
id: number;
|
|
41
|
+
username?: string;
|
|
42
|
+
first_name?: string;
|
|
43
|
+
is_bot?: boolean;
|
|
44
|
+
};
|
|
45
|
+
date: number;
|
|
46
|
+
old_reaction: { type: string; emoji?: string }[];
|
|
47
|
+
new_reaction: { type: string; emoji?: string }[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TgReactionCount {
|
|
51
|
+
chat: { id: number; type: string };
|
|
52
|
+
message_id: number;
|
|
53
|
+
date: number;
|
|
54
|
+
reactions: {
|
|
55
|
+
type: { type: string; emoji?: string };
|
|
56
|
+
total_count: number;
|
|
57
|
+
}[];
|
|
58
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export class TelegramTokenError extends Error {}
|
|
2
|
+
|
|
3
|
+
export interface TelegramBotIdentity {
|
|
4
|
+
botId: number;
|
|
5
|
+
username: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const API = 'https://api.telegram.org';
|
|
9
|
+
const TIMEOUT_MS = 10_000;
|
|
10
|
+
|
|
11
|
+
interface GetMe {
|
|
12
|
+
ok?: unknown;
|
|
13
|
+
result?: { id?: unknown; username?: unknown; is_bot?: unknown };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function verifyTelegramBotToken(
|
|
17
|
+
token: string,
|
|
18
|
+
): Promise<TelegramBotIdentity> {
|
|
19
|
+
let res: Response;
|
|
20
|
+
try {
|
|
21
|
+
res = await fetch(`${API}/bot${token}/getMe`, {
|
|
22
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
23
|
+
});
|
|
24
|
+
} catch {
|
|
25
|
+
throw new TelegramTokenError(
|
|
26
|
+
'could not reach the Telegram API to check that bot token',
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
const body = (await res.json().catch(() => null)) as GetMe | null;
|
|
30
|
+
const me = body?.ok === true ? body.result : undefined;
|
|
31
|
+
if (!me) throw new TelegramTokenError('Telegram rejected that bot token');
|
|
32
|
+
if (typeof me.id !== 'number' || me.is_bot !== true)
|
|
33
|
+
throw new TelegramTokenError('that token does not belong to a Telegram bot');
|
|
34
|
+
return {
|
|
35
|
+
botId: me.id,
|
|
36
|
+
username: typeof me.username === 'string' ? me.username : '',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/webhook",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/station.ts"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"test": "tsc --noEmit && bun test test/"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@metro-labs/mcp": "workspace:*"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@stage-labs/config": "0.1.0-beta.2",
|
|
18
|
+
"eslint": "^10.3.0",
|
|
19
|
+
"knip": "^6.15.0",
|
|
20
|
+
"madge": "^8.0.0",
|
|
21
|
+
"typescript": "^5"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { Line } from '@metro-labs/mcp/lines';
|
|
3
|
+
import { mintId, type MetroEvent } from '@metro-labs/mcp/events';
|
|
4
|
+
import type { Endpoint } from '@metro-labs/mcp/endpoints';
|
|
5
|
+
import type { Station, Verb } from '@metro-labs/mcp/stations/types';
|
|
6
|
+
|
|
7
|
+
export const webhookStation: Station = {
|
|
8
|
+
name: 'webhook',
|
|
9
|
+
hasAccounts: true,
|
|
10
|
+
hasTrain: false,
|
|
11
|
+
messageVerbs: new Set<Verb>(),
|
|
12
|
+
attachmentMode: 'none',
|
|
13
|
+
tools: [],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function webhookEntry(
|
|
17
|
+
endpoint: Endpoint,
|
|
18
|
+
headers: Record<string, string>,
|
|
19
|
+
body: unknown,
|
|
20
|
+
method: string,
|
|
21
|
+
url: string,
|
|
22
|
+
): MetroEvent {
|
|
23
|
+
const line = Line.webhook(endpoint.id);
|
|
24
|
+
return {
|
|
25
|
+
id: mintId(),
|
|
26
|
+
ts: new Date().toISOString(),
|
|
27
|
+
station: 'webhook',
|
|
28
|
+
line,
|
|
29
|
+
lineName: endpoint.label,
|
|
30
|
+
from: line,
|
|
31
|
+
to: line,
|
|
32
|
+
messageId:
|
|
33
|
+
headers['x-github-delivery'] ?? headers['x-request-id'] ?? randomUUID(),
|
|
34
|
+
text: `${headers['x-github-event'] ?? headers['x-intercom-topic'] ?? 'event'} ${method} ${url}`,
|
|
35
|
+
payload: { headers, body },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/whatsapp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/station.ts",
|
|
8
|
+
"./train": "./src/index.ts",
|
|
9
|
+
"./login": "./src/login.ts",
|
|
10
|
+
"./tokens": "./src/token-store.ts"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"test": "tsc --noEmit && bun test test/"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@metro-labs/mcp": "workspace:*",
|
|
18
|
+
"baileys": "7.0.0-rc14"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@stage-labs/config": "0.1.0-beta.2",
|
|
22
|
+
"eslint": "^10.3.0",
|
|
23
|
+
"knip": "^6.15.0",
|
|
24
|
+
"madge": "^8.0.0",
|
|
25
|
+
"typescript": "^5"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
makeAccountStore,
|
|
5
|
+
resolveAccountId,
|
|
6
|
+
type Die,
|
|
7
|
+
} from '@metro-labs/mcp/stations/account-store';
|
|
8
|
+
import type { WhatsAppAccount } from './types.js';
|
|
9
|
+
|
|
10
|
+
const ACCOUNTS_FILE =
|
|
11
|
+
process.env.WHATSAPP_ACCOUNTS_FILE ??
|
|
12
|
+
join(homedir(), '.metro', 'whatsapp-accounts.json');
|
|
13
|
+
|
|
14
|
+
const isJid = (s: string): boolean => s.includes('@');
|
|
15
|
+
|
|
16
|
+
function validateAccount(
|
|
17
|
+
a: WhatsAppAccount,
|
|
18
|
+
seen: Set<string>,
|
|
19
|
+
die: Die,
|
|
20
|
+
): void {
|
|
21
|
+
if (!a.id) die('account missing id');
|
|
22
|
+
if (!a.phone || typeof a.phone !== 'string')
|
|
23
|
+
die(`account '${a.id}' missing phone`);
|
|
24
|
+
if (seen.has(a.id)) die(`duplicate account id '${a.id}'`);
|
|
25
|
+
seen.add(a.id);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const { loadAccounts } = makeAccountStore<WhatsAppAccount>({
|
|
29
|
+
prefix: 'whatsapp',
|
|
30
|
+
file: ACCOUNTS_FILE,
|
|
31
|
+
validate(raw, die) {
|
|
32
|
+
const seen = new Set<string>();
|
|
33
|
+
for (const a of raw) validateAccount(a, seen, die);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const accounts = new Map<string, WhatsAppAccount>();
|
|
38
|
+
|
|
39
|
+
export function accountFor(args: { account?: string; line?: string }): string {
|
|
40
|
+
return resolveAccountId(accounts, args, (line) => targetOf(line)?.accountId);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function lineOf(accountId: string, jid: string): string {
|
|
44
|
+
return `metro://whatsapp/${accountId}/${jid}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface Target {
|
|
48
|
+
accountId: string;
|
|
49
|
+
jid: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function splitScoped(path: string[]): { accountId: string; jid?: string } {
|
|
53
|
+
const first = path[0];
|
|
54
|
+
if (path.length >= 2 && first !== undefined && !isJid(first))
|
|
55
|
+
return { accountId: first, jid: path[1] };
|
|
56
|
+
return { accountId: 'default', jid: first };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function targetOf(line: string): Target | undefined {
|
|
60
|
+
const prefix = 'metro://whatsapp/';
|
|
61
|
+
if (!line.startsWith(prefix)) return undefined;
|
|
62
|
+
const path = line.slice(prefix.length).split('/').filter(Boolean);
|
|
63
|
+
const { accountId, jid } = splitScoped(path);
|
|
64
|
+
if (jid === undefined || !isJid(jid)) return undefined;
|
|
65
|
+
return { accountId, jid };
|
|
66
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/mcp/train-error';
|
|
2
|
+
import type { BinaryNode, WASocket } from 'baileys';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_ACK_WAIT_MS = 5000;
|
|
5
|
+
|
|
6
|
+
export function ackWaitMs(raw: string | undefined): number {
|
|
7
|
+
if (raw === undefined || raw.trim() === '') return DEFAULT_ACK_WAIT_MS;
|
|
8
|
+
const ms = Number(raw);
|
|
9
|
+
return Number.isFinite(ms) && ms >= 0 ? ms : DEFAULT_ACK_WAIT_MS;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const ACK_WAIT_MS = ackWaitMs(process.env.METRO_WHATSAPP_ACK_WAIT_MS);
|
|
13
|
+
|
|
14
|
+
const RECENT_MAX = 256;
|
|
15
|
+
|
|
16
|
+
const RESTRICTION = '463';
|
|
17
|
+
|
|
18
|
+
export interface SendAck {
|
|
19
|
+
messageId: string;
|
|
20
|
+
jid: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function ackOf(node: BinaryNode): SendAck | undefined {
|
|
25
|
+
const { id, from, error } = node.attrs;
|
|
26
|
+
if (!id || !from) return undefined;
|
|
27
|
+
return { messageId: id, jid: from, ...(error ? { error } : {}) };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function rejection(ack: SendAck): TrainError | undefined {
|
|
31
|
+
if (!ack.error) return undefined;
|
|
32
|
+
if (ack.error === RESTRICTION)
|
|
33
|
+
return new TrainError(
|
|
34
|
+
'whatsapp_account_restricted',
|
|
35
|
+
`WhatsApp refused the message to ${ack.jid} with ack error 463 (MessageAccountRestriction): the send carried no trusted-contact token, so WhatsApp counted it as reaching out to a stranger and this account is under a reach-out timelock. Nothing was delivered, and it must not be retried — every retry counts as another reach-out and deepens the lock.`,
|
|
36
|
+
{ retryable: false },
|
|
37
|
+
);
|
|
38
|
+
return new TrainError(
|
|
39
|
+
'whatsapp_send_refused',
|
|
40
|
+
`WhatsApp refused the message to ${ack.jid} with ack error ${ack.error}, so it was not delivered`,
|
|
41
|
+
{ retryable: false },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AckWatch {
|
|
46
|
+
record(node: BinaryNode): SendAck | undefined;
|
|
47
|
+
wait(messageId: string, timeoutMs: number): Promise<SendAck | undefined>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function makeAckWatch(max: number = RECENT_MAX): AckWatch {
|
|
51
|
+
const recent = new Map<string, SendAck>();
|
|
52
|
+
const waiting = new Map<string, (ack: SendAck) => void>();
|
|
53
|
+
return {
|
|
54
|
+
record(node) {
|
|
55
|
+
const ack = ackOf(node);
|
|
56
|
+
if (!ack) return undefined;
|
|
57
|
+
const waiter = waiting.get(ack.messageId);
|
|
58
|
+
if (waiter) {
|
|
59
|
+
waiting.delete(ack.messageId);
|
|
60
|
+
waiter(ack);
|
|
61
|
+
return ack;
|
|
62
|
+
}
|
|
63
|
+
recent.delete(ack.messageId);
|
|
64
|
+
recent.set(ack.messageId, ack);
|
|
65
|
+
while (recent.size > max) {
|
|
66
|
+
const oldest = recent.keys().next().value;
|
|
67
|
+
if (oldest === undefined) break;
|
|
68
|
+
recent.delete(oldest);
|
|
69
|
+
}
|
|
70
|
+
return ack;
|
|
71
|
+
},
|
|
72
|
+
wait(messageId, timeoutMs) {
|
|
73
|
+
const seen = recent.get(messageId);
|
|
74
|
+
if (seen) {
|
|
75
|
+
recent.delete(messageId);
|
|
76
|
+
return Promise.resolve(seen);
|
|
77
|
+
}
|
|
78
|
+
return new Promise<SendAck | undefined>((resolve) => {
|
|
79
|
+
const timer = setTimeout(() => {
|
|
80
|
+
waiting.delete(messageId);
|
|
81
|
+
resolve(undefined);
|
|
82
|
+
}, timeoutMs);
|
|
83
|
+
waiting.set(messageId, (ack) => {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
resolve(ack);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function bindAcks(
|
|
93
|
+
sock: WASocket,
|
|
94
|
+
watch: AckWatch,
|
|
95
|
+
onError: (ack: SendAck) => void,
|
|
96
|
+
): void {
|
|
97
|
+
sock.ws.on('CB:ack,class:message', (node: BinaryNode) => {
|
|
98
|
+
const ack = watch.record(node);
|
|
99
|
+
if (ack?.error) onError(ack);
|
|
100
|
+
});
|
|
101
|
+
}
|