@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.21
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 +103 -0
- package/dist/cli.js +134 -565
- package/dist/control.js +102 -0
- package/dist/prompt.js +10 -0
- package/dist/runtime.js +94 -0
- package/dist/store.js +70 -0
- package/dist/update.js +33 -0
- package/dist/version.js +97 -0
- package/package.json +34 -22
- package/runtime/node_modules/@metro-labs/discord/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/discord/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord/src/format.ts +210 -0
- package/runtime/node_modules/@metro-labs/discord/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +66 -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 +367 -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 +335 -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 +129 -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 +124 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +278 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +322 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +20 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +202 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +338 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +31 -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 +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +333 -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 +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +164 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +208 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +357 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +79 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +162 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +58 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +134 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +214 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +224 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +125 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +41 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +218 -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 +323 -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 +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +71 -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 +37 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +147 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +364 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +300 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +131 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +145 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +203 -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 +192 -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 +73 -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 +211 -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 +212 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +397 -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 +240 -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 +80 -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 +182 -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 +25 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +172 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +48 -0
- package/runtime/node_modules/@metro-labs/telegram/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-user/package.json +28 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +29 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +67 -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 +318 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +132 -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 +141 -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 +113 -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 +333 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +78 -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 +39 -0
- package/runtime/runtime.json +3 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/dist/tail.js +0 -161
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bufferedSince,
|
|
3
|
+
currentBusSeq,
|
|
4
|
+
subscribeEvents,
|
|
5
|
+
type MetroEvent,
|
|
6
|
+
} from '../daemon/events.js';
|
|
7
|
+
import type { InboundRelay } from './inbound.js';
|
|
8
|
+
|
|
9
|
+
export interface ReplayLedger {
|
|
10
|
+
startAt: number;
|
|
11
|
+
delivered: Set<number>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const newReplayLedger = (startAt = -1): ReplayLedger => ({
|
|
15
|
+
startAt,
|
|
16
|
+
delivered: new Set<number>(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
interface ChannelRelayDeps {
|
|
20
|
+
relay: InboundRelay;
|
|
21
|
+
log: (...a: unknown[]) => void;
|
|
22
|
+
inScope: (line: string) => boolean;
|
|
23
|
+
ledger?: ReplayLedger;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const PENDING_MAX = 2000;
|
|
27
|
+
|
|
28
|
+
export class ChannelRelay {
|
|
29
|
+
private readonly deps: ChannelRelayDeps;
|
|
30
|
+
private readonly ledger: ReplayLedger;
|
|
31
|
+
private chain: Promise<void> = Promise.resolve();
|
|
32
|
+
|
|
33
|
+
constructor(deps: ChannelRelayDeps) {
|
|
34
|
+
this.deps = deps;
|
|
35
|
+
this.ledger = deps.ledger ?? newReplayLedger();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
start(): () => void {
|
|
39
|
+
if (this.ledger.startAt < 0) this.ledger.startAt = currentBusSeq();
|
|
40
|
+
return subscribeEvents((event, busSeq) => {
|
|
41
|
+
this.enqueue(event, busSeq, false);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
replayMissed(): void {
|
|
46
|
+
const missed = bufferedSince(this.ledger.startAt).filter(
|
|
47
|
+
(b) => !this.ledger.delivered.has(b.busSeq),
|
|
48
|
+
);
|
|
49
|
+
const first = missed.at(0);
|
|
50
|
+
const last = missed.at(-1);
|
|
51
|
+
if (!first || !last) return;
|
|
52
|
+
this.deps.log(
|
|
53
|
+
'relay: replay',
|
|
54
|
+
`(${missed.length} events)`,
|
|
55
|
+
'range',
|
|
56
|
+
`${first.busSeq}..${last.busSeq}`,
|
|
57
|
+
);
|
|
58
|
+
for (const b of missed) this.enqueue(b.event, b.busSeq, true);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private enqueue(event: MetroEvent, busSeq: number, replay: boolean): void {
|
|
62
|
+
const { delivered } = this.ledger;
|
|
63
|
+
if (delivered.has(busSeq)) return;
|
|
64
|
+
delivered.add(busSeq);
|
|
65
|
+
if (delivered.size > PENDING_MAX) {
|
|
66
|
+
const cutoff = busSeq - PENDING_MAX;
|
|
67
|
+
for (const s of delivered) if (s <= cutoff) delivered.delete(s);
|
|
68
|
+
}
|
|
69
|
+
this.chain = this.chain.then(() =>
|
|
70
|
+
this.deliver(event, busSeq, replay).catch((err: unknown) => {
|
|
71
|
+
this.deps.log(
|
|
72
|
+
'relay: delivery step failed; chain kept alive',
|
|
73
|
+
'busSeq',
|
|
74
|
+
busSeq,
|
|
75
|
+
'line',
|
|
76
|
+
event.line,
|
|
77
|
+
err,
|
|
78
|
+
);
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private withhold(event: MetroEvent, busSeq: number): void {
|
|
84
|
+
this.ledger.delivered.delete(busSeq);
|
|
85
|
+
this.deps.log(
|
|
86
|
+
'relay: withheld (line outside the channel session scope)',
|
|
87
|
+
'busSeq',
|
|
88
|
+
busSeq,
|
|
89
|
+
'line',
|
|
90
|
+
event.line,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private async deliver(
|
|
95
|
+
event: MetroEvent,
|
|
96
|
+
busSeq: number,
|
|
97
|
+
replay: boolean,
|
|
98
|
+
): Promise<void> {
|
|
99
|
+
if (!this.deps.inScope(event.line)) {
|
|
100
|
+
this.withhold(event, busSeq);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.deps.log(
|
|
104
|
+
'relay: notify',
|
|
105
|
+
'busSeq',
|
|
106
|
+
busSeq,
|
|
107
|
+
'id',
|
|
108
|
+
event.id,
|
|
109
|
+
'replay',
|
|
110
|
+
replay,
|
|
111
|
+
);
|
|
112
|
+
try {
|
|
113
|
+
await this.deps.relay.handleEvent(
|
|
114
|
+
event as unknown as Record<string, unknown>,
|
|
115
|
+
replay,
|
|
116
|
+
);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
this.ledger.delivered.delete(busSeq);
|
|
119
|
+
this.deps.log(
|
|
120
|
+
'channel delivery failed; bounded replay on reconnect',
|
|
121
|
+
'busSeq',
|
|
122
|
+
busSeq,
|
|
123
|
+
'line',
|
|
124
|
+
event.line,
|
|
125
|
+
err,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const MAX_BODY_CHARS = 8 * 1024;
|
|
2
|
+
|
|
3
|
+
const HEADERS_KEPT = [
|
|
4
|
+
'x-github-event',
|
|
5
|
+
'x-github-delivery',
|
|
6
|
+
'x-intercom-topic',
|
|
7
|
+
'x-request-id',
|
|
8
|
+
'user-agent',
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
interface WebhookPayload {
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function renderBody(body: unknown): string {
|
|
17
|
+
if (body === undefined || body === null) return '';
|
|
18
|
+
const text =
|
|
19
|
+
typeof body === 'string' ? body : JSON.stringify(body, null, 2);
|
|
20
|
+
if (text.length <= MAX_BODY_CHARS) return text;
|
|
21
|
+
return (
|
|
22
|
+
text.slice(0, MAX_BODY_CHARS) +
|
|
23
|
+
`\n[truncated: ${String(text.length - MAX_BODY_CHARS)} more characters]`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function renderHeaders(headers: Record<string, string> | undefined): string {
|
|
28
|
+
if (!headers) return '';
|
|
29
|
+
const kept = HEADERS_KEPT.flatMap((k) =>
|
|
30
|
+
headers[k] === undefined ? [] : [`${k}: ${headers[k]}`],
|
|
31
|
+
);
|
|
32
|
+
return kept.length === 0 ? '' : kept.join('\n') + '\n';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function buildWebhookNote(
|
|
36
|
+
summary: string,
|
|
37
|
+
lineName: string | undefined,
|
|
38
|
+
payload: unknown,
|
|
39
|
+
): string {
|
|
40
|
+
const p = (
|
|
41
|
+
payload !== null && typeof payload === 'object' ? payload : {}
|
|
42
|
+
) as WebhookPayload;
|
|
43
|
+
const label = lineName ? `${lineName}: ` : '';
|
|
44
|
+
const body = renderBody(p.body);
|
|
45
|
+
return (
|
|
46
|
+
`[webhook received] ${label}${summary}\n` +
|
|
47
|
+
renderHeaders(p.headers) +
|
|
48
|
+
(body ? `\n${body}\n` : '') +
|
|
49
|
+
'Inbound only: this line takes no send, reply or react.'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from './log.js';
|
|
3
|
+
import { ApiError } from './api-error.js';
|
|
4
|
+
import {
|
|
5
|
+
apiFailure,
|
|
6
|
+
bodyField,
|
|
7
|
+
readJsonBody,
|
|
8
|
+
sendJson,
|
|
9
|
+
type ApiSession,
|
|
10
|
+
} from './api-http.js';
|
|
11
|
+
import {
|
|
12
|
+
isStationName,
|
|
13
|
+
parseAccountId,
|
|
14
|
+
type AccountRef,
|
|
15
|
+
} from '../db/account-attach.js';
|
|
16
|
+
import type { StationName } from '../db/schema.js';
|
|
17
|
+
import {
|
|
18
|
+
ATTACHABLE_STATIONS,
|
|
19
|
+
isAttachStation,
|
|
20
|
+
type AttachInput,
|
|
21
|
+
type OneTimeSecret,
|
|
22
|
+
type PreparedAccount,
|
|
23
|
+
} from '../stations/attach.js';
|
|
24
|
+
import {
|
|
25
|
+
INTERACTIVE_STATIONS,
|
|
26
|
+
isInteractiveStation,
|
|
27
|
+
type InteractiveStation,
|
|
28
|
+
} from '../stations/attach-interactive.js';
|
|
29
|
+
import {
|
|
30
|
+
ATTACH_ID_RE,
|
|
31
|
+
type AttachOwner,
|
|
32
|
+
type AttachView,
|
|
33
|
+
} from './attach-session.js';
|
|
34
|
+
|
|
35
|
+
export interface AttachSessionApi {
|
|
36
|
+
start: (
|
|
37
|
+
owner: AttachOwner,
|
|
38
|
+
station: InteractiveStation,
|
|
39
|
+
input: Record<string, unknown>,
|
|
40
|
+
) => Promise<AttachView>;
|
|
41
|
+
view: (owner: AttachOwner, attachId: string) => AttachView;
|
|
42
|
+
submit: (
|
|
43
|
+
owner: AttachOwner,
|
|
44
|
+
attachId: string,
|
|
45
|
+
input: { code?: unknown; password?: unknown },
|
|
46
|
+
) => Promise<AttachView>;
|
|
47
|
+
cancel: (owner: AttachOwner, attachId: string) => Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface AccountApiDeps {
|
|
51
|
+
attachSessions: AttachSessionApi;
|
|
52
|
+
prepareAccount: (input: AttachInput) => Promise<PreparedAccount>;
|
|
53
|
+
attachAccount: (
|
|
54
|
+
email: string,
|
|
55
|
+
agentId: string,
|
|
56
|
+
station: StationName,
|
|
57
|
+
config: Record<string, unknown>,
|
|
58
|
+
) => Promise<AccountRef>;
|
|
59
|
+
detachAccount: (
|
|
60
|
+
email: string,
|
|
61
|
+
agentId: string,
|
|
62
|
+
station: StationName,
|
|
63
|
+
accountId: string,
|
|
64
|
+
) => Promise<AccountRef>;
|
|
65
|
+
syncStations: (station: StationName) => Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type AccountRoute =
|
|
69
|
+
| { kind: 'start' }
|
|
70
|
+
| { kind: 'session'; attachId: string }
|
|
71
|
+
| { kind: 'step'; attachId: string }
|
|
72
|
+
| { kind: 'account'; station: StationName; accountId: string };
|
|
73
|
+
|
|
74
|
+
export const ATTACHABLE: string[] = [
|
|
75
|
+
...ATTACHABLE_STATIONS,
|
|
76
|
+
...INTERACTIVE_STATIONS,
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
const ROUTE_METHODS: Record<AccountRoute['kind'], string[]> = {
|
|
80
|
+
start: ['POST'],
|
|
81
|
+
session: ['GET', 'DELETE'],
|
|
82
|
+
step: ['POST'],
|
|
83
|
+
account: ['DELETE'],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function twoSegmentRoute(head: string, tail: string): AccountRoute | null {
|
|
87
|
+
if (ATTACH_ID_RE.test(head))
|
|
88
|
+
return tail === 'step' ? { kind: 'step', attachId: head } : null;
|
|
89
|
+
if (!isStationName(head)) return null;
|
|
90
|
+
const accountId = parseAccountId(tail);
|
|
91
|
+
return accountId === null ? null : { kind: 'account', station: head, accountId };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function accountRoute(rest: string[]): AccountRoute | null {
|
|
95
|
+
const [head, tail] = rest;
|
|
96
|
+
if (head === undefined) return null;
|
|
97
|
+
if (rest.length === 1)
|
|
98
|
+
return head === 'start'
|
|
99
|
+
? { kind: 'start' }
|
|
100
|
+
: ATTACH_ID_RE.test(head)
|
|
101
|
+
? { kind: 'session', attachId: head }
|
|
102
|
+
: null;
|
|
103
|
+
if (rest.length !== 2 || tail === undefined) return null;
|
|
104
|
+
return twoSegmentRoute(head, tail);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function accountRouteAllows(
|
|
108
|
+
route: AccountRoute,
|
|
109
|
+
method: string | undefined,
|
|
110
|
+
): boolean {
|
|
111
|
+
return ROUTE_METHODS[route.kind].includes(method ?? '');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function activate(
|
|
115
|
+
deps: AccountApiDeps,
|
|
116
|
+
station: StationName,
|
|
117
|
+
): Promise<boolean> {
|
|
118
|
+
try {
|
|
119
|
+
await deps.syncStations(station);
|
|
120
|
+
return true;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
log.warn(
|
|
123
|
+
{ station, err: errMsg(err) },
|
|
124
|
+
'account-api: station reload failed, the change lands at the next boot',
|
|
125
|
+
);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
interface AttachPayload {
|
|
131
|
+
status: 'done';
|
|
132
|
+
agentId: string;
|
|
133
|
+
station: string;
|
|
134
|
+
accountId: string;
|
|
135
|
+
identity: Record<string, string>;
|
|
136
|
+
activated: boolean;
|
|
137
|
+
secret?: OneTimeSecret;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function storeAccount(
|
|
141
|
+
deps: AccountApiDeps,
|
|
142
|
+
session: ApiSession,
|
|
143
|
+
agentId: string,
|
|
144
|
+
station: StationName,
|
|
145
|
+
prepared: PreparedAccount,
|
|
146
|
+
): Promise<AccountRef> {
|
|
147
|
+
try {
|
|
148
|
+
return await deps.attachAccount(
|
|
149
|
+
session.email,
|
|
150
|
+
agentId,
|
|
151
|
+
station,
|
|
152
|
+
prepared.config,
|
|
153
|
+
);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
prepared.discard?.();
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function ownerOf(session: ApiSession, agentId: string): AttachOwner {
|
|
161
|
+
return { email: session.email, agentId };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function asInput(body: unknown): Record<string, unknown> {
|
|
165
|
+
const out: Record<string, unknown> = {};
|
|
166
|
+
for (const key of ['apiId', 'apiHash', 'phone'])
|
|
167
|
+
out[key] = bodyField(body, key);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function handleStart(
|
|
172
|
+
req: IncomingMessage,
|
|
173
|
+
res: ServerResponse,
|
|
174
|
+
deps: AccountApiDeps,
|
|
175
|
+
session: ApiSession,
|
|
176
|
+
agentId: string,
|
|
177
|
+
): Promise<void> {
|
|
178
|
+
const body = await readJsonBody(req);
|
|
179
|
+
const station = bodyField(body, 'station');
|
|
180
|
+
if (isInteractiveStation(station)) {
|
|
181
|
+
const view = await deps.attachSessions.start(
|
|
182
|
+
ownerOf(session, agentId),
|
|
183
|
+
station,
|
|
184
|
+
asInput(body),
|
|
185
|
+
);
|
|
186
|
+
sendJson(req, res, 201, view);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (!isAttachStation(station))
|
|
190
|
+
throw new ApiError(`station must be one of ${ATTACHABLE.join(', ')}`, 400);
|
|
191
|
+
const prepared = await deps.prepareAccount({
|
|
192
|
+
station,
|
|
193
|
+
token: bodyField(body, 'token'),
|
|
194
|
+
});
|
|
195
|
+
const ref = await storeAccount(deps, session, agentId, station, prepared);
|
|
196
|
+
log.info(
|
|
197
|
+
{ agentId: ref.agentId, station, account: ref.accountId },
|
|
198
|
+
'account-api: attached a station account',
|
|
199
|
+
);
|
|
200
|
+
const payload: AttachPayload = {
|
|
201
|
+
status: 'done',
|
|
202
|
+
agentId: ref.agentId,
|
|
203
|
+
station,
|
|
204
|
+
accountId: ref.accountId,
|
|
205
|
+
identity: prepared.identity,
|
|
206
|
+
activated: await activate(deps, station),
|
|
207
|
+
};
|
|
208
|
+
sendJson(
|
|
209
|
+
req,
|
|
210
|
+
res,
|
|
211
|
+
201,
|
|
212
|
+
prepared.secret === undefined
|
|
213
|
+
? payload
|
|
214
|
+
: { ...payload, secret: prepared.secret },
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function handleDetach(
|
|
219
|
+
req: IncomingMessage,
|
|
220
|
+
res: ServerResponse,
|
|
221
|
+
deps: AccountApiDeps,
|
|
222
|
+
session: ApiSession,
|
|
223
|
+
agentId: string,
|
|
224
|
+
target: { station: StationName; accountId: string },
|
|
225
|
+
): Promise<void> {
|
|
226
|
+
const ref = await deps.detachAccount(
|
|
227
|
+
session.email,
|
|
228
|
+
agentId,
|
|
229
|
+
target.station,
|
|
230
|
+
target.accountId,
|
|
231
|
+
);
|
|
232
|
+
log.info(
|
|
233
|
+
{ agentId: ref.agentId, station: ref.station, account: ref.accountId },
|
|
234
|
+
'account-api: detached a station account',
|
|
235
|
+
);
|
|
236
|
+
sendJson(req, res, 200, {
|
|
237
|
+
agentId: ref.agentId,
|
|
238
|
+
station: ref.station,
|
|
239
|
+
accountId: ref.accountId,
|
|
240
|
+
detached: true,
|
|
241
|
+
activated: await activate(deps, ref.station),
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function handleSession(
|
|
246
|
+
req: IncomingMessage,
|
|
247
|
+
res: ServerResponse,
|
|
248
|
+
deps: AccountApiDeps,
|
|
249
|
+
owner: AttachOwner,
|
|
250
|
+
attachId: string,
|
|
251
|
+
): Promise<void> {
|
|
252
|
+
if (req.method === 'DELETE') {
|
|
253
|
+
await deps.attachSessions.cancel(owner, attachId);
|
|
254
|
+
sendJson(req, res, 200, { attachId, cancelled: true });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
sendJson(req, res, 200, deps.attachSessions.view(owner, attachId));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function handleStep(
|
|
261
|
+
req: IncomingMessage,
|
|
262
|
+
res: ServerResponse,
|
|
263
|
+
deps: AccountApiDeps,
|
|
264
|
+
owner: AttachOwner,
|
|
265
|
+
attachId: string,
|
|
266
|
+
): Promise<void> {
|
|
267
|
+
const body = await readJsonBody(req);
|
|
268
|
+
const view = await deps.attachSessions.submit(owner, attachId, {
|
|
269
|
+
code: bodyField(body, 'code'),
|
|
270
|
+
password: bodyField(body, 'password'),
|
|
271
|
+
});
|
|
272
|
+
sendJson(req, res, 200, view);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function dispatchRoute(
|
|
276
|
+
req: IncomingMessage,
|
|
277
|
+
res: ServerResponse,
|
|
278
|
+
deps: AccountApiDeps,
|
|
279
|
+
session: ApiSession,
|
|
280
|
+
agentId: string,
|
|
281
|
+
route: AccountRoute,
|
|
282
|
+
): Promise<void> {
|
|
283
|
+
if (route.kind === 'start')
|
|
284
|
+
return handleStart(req, res, deps, session, agentId);
|
|
285
|
+
if (route.kind === 'session')
|
|
286
|
+
return handleSession(
|
|
287
|
+
req,
|
|
288
|
+
res,
|
|
289
|
+
deps,
|
|
290
|
+
ownerOf(session, agentId),
|
|
291
|
+
route.attachId,
|
|
292
|
+
);
|
|
293
|
+
if (route.kind === 'step')
|
|
294
|
+
return handleStep(req, res, deps, ownerOf(session, agentId), route.attachId);
|
|
295
|
+
return handleDetach(req, res, deps, session, agentId, route);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export async function handleAccountRoute(
|
|
299
|
+
req: IncomingMessage,
|
|
300
|
+
res: ServerResponse,
|
|
301
|
+
deps: AccountApiDeps,
|
|
302
|
+
session: ApiSession,
|
|
303
|
+
agentId: string,
|
|
304
|
+
route: AccountRoute,
|
|
305
|
+
): Promise<void> {
|
|
306
|
+
try {
|
|
307
|
+
await dispatchRoute(req, res, deps, session, agentId, route);
|
|
308
|
+
} catch (err) {
|
|
309
|
+
apiFailure(req, res, err);
|
|
310
|
+
}
|
|
311
|
+
}
|