@stage-labs/metro 0.1.0-beta.315 → 0.1.0-beta.317

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 (39) hide show
  1. package/package.json +1 -1
  2. package/runtime/node_modules/@metro-labs/core/package.json +2 -0
  3. package/runtime/node_modules/@metro-labs/core/src/effort.ts +21 -0
  4. package/runtime/node_modules/@metro-labs/core/src/stations/account-status.ts +26 -0
  5. package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +2 -0
  6. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +3 -1
  7. package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +23 -2
  8. package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +3 -0
  9. package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +17 -0
  10. package/runtime/node_modules/@metro-labs/daemon/src/gateway/delivered.ts +16 -3
  11. package/runtime/node_modules/@metro-labs/daemon/src/gateway/effective.ts +1 -1
  12. package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +4 -4
  13. package/runtime/node_modules/@metro-labs/daemon/src/gateway/fallback.ts +10 -8
  14. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +21 -8
  15. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +11 -1
  16. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +22 -0
  17. package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage-refresh.ts +1 -1
  18. package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +30 -3
  19. package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +9 -3
  20. package/runtime/node_modules/@metro-labs/daemon/src/routes/pair-again.ts +23 -0
  21. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +1 -1
  22. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +9 -2
  23. package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +6 -0
  24. package/runtime/node_modules/@metro-labs/http/src/connector-oauth.ts +13 -4
  25. package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +3 -2
  26. package/runtime/node_modules/@metro-labs/whatsapp/src/client-types.ts +2 -0
  27. package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +35 -40
  28. package/runtime/node_modules/@metro-labs/whatsapp/src/connection.ts +139 -0
  29. package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +36 -3
  30. package/runtime/node_modules/@metro-labs/whatsapp/src/messages.ts +24 -3
  31. package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +46 -15
  32. package/runtime/node_modules/@metro-labs/whatsapp/src/profile.ts +158 -0
  33. package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +2 -0
  34. package/runtime/runtime.json +1 -1
  35. package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +2 -0
  36. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/effort.ts +21 -0
  37. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/account-status.ts +26 -0
  38. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/types.ts +2 -0
  39. package/runtime/sdk-runner/runner.json +1 -1
@@ -19,6 +19,7 @@ export type StartAttach = (
19
19
  input: Record<string, unknown>,
20
20
  hooks: DriverHooks,
21
21
  owner: AttachOwner,
22
+ repair?: string,
22
23
  ) => Promise<StartedAttach>;
23
24
 
24
25
  export const ATTACH_ID_RE = /^as_[A-Za-z0-9_-]{22}$/;
@@ -60,6 +61,7 @@ type CompleteAttach = (
60
61
  station: StationName,
61
62
  config: Record<string, unknown>,
62
63
  active: () => boolean,
64
+ repair?: string,
63
65
  ) => Promise<{ accountId: string; activated: boolean }>;
64
66
 
65
67
  type AuthorizeAttach = (owner: AttachOwner) => Promise<void>;
@@ -72,6 +74,7 @@ export interface AttachSessionDeps {
72
74
 
73
75
  interface Session {
74
76
  owner: AttachOwner;
77
+ repair?: string;
75
78
  driver: AttachDriver | null;
76
79
  view: AttachView;
77
80
  finishing?: boolean;
@@ -177,6 +180,7 @@ export class AttachSessions {
177
180
  session.committing = true;
178
181
  return true;
179
182
  },
183
+ session.repair,
180
184
  );
181
185
  this.settle(session, {
182
186
  status: 'done',
@@ -187,12 +191,12 @@ export class AttachSessions {
187
191
  });
188
192
  log.info(
189
193
  { agentId: session.owner.agentId, station, account: landed.accountId },
190
- 'attach-session: signed in and attached a station account',
194
+ session.repair === undefined ? 'attach-session: signed in and attached a station account' : 'attach-session: paired a station account again',
191
195
  );
192
196
  } catch (err) {
193
197
  const reason =
194
198
  err instanceof ApiError ? err.message : 'could not store the account';
195
- log.warn({ station, err: errMsg(err) }, 'attach-session: store failed');
199
+ log.warn({ station, ...(err instanceof ApiError ? { status: err.status } : { err: errMsg(err).replace(/\d{6,}/g, '<number>') }) }, 'attach-session: store failed');
196
200
  this.settle(session, { status: 'failed', prompt: '', error: reason });
197
201
  }
198
202
  }
@@ -230,6 +234,7 @@ export class AttachSessions {
230
234
  owner: AttachOwner,
231
235
  station: InteractiveStation,
232
236
  input: Record<string, unknown>,
237
+ repair?: string,
233
238
  ): Promise<AttachView> {
234
239
  await this.deps.authorize(owner);
235
240
  this.admit(owner);
@@ -237,6 +242,7 @@ export class AttachSessions {
237
242
  const attachId = newAttachId();
238
243
  const session: Session = {
239
244
  owner,
245
+ ...(repair === undefined ? {} : { repair }),
240
246
  driver: null,
241
247
  view: blankView(attachId, station),
242
248
  };
@@ -248,6 +254,7 @@ export class AttachSessions {
248
254
  input,
249
255
  this.hooksFor(attachId, station),
250
256
  owner,
257
+ repair,
251
258
  );
252
259
  if (this.sessions.get(attachId) !== session || session.view.expiresAt <= Date.now()) {
253
260
  await started.driver.cancel();
@@ -41,6 +41,12 @@ export const accountStationCapabilities = (): Record<string, string[]> => {
41
41
  return out;
42
42
  };
43
43
 
44
+ export const statusStations = (): string[] =>
45
+ STATIONS.filter((s) => s.reportsStatus === true).map((s) => s.name);
46
+
47
+ export const pairAgainStations = (): string[] =>
48
+ STATIONS.filter((s) => s.pairAgain !== undefined).map((s) => s.name);
49
+
44
50
  export function forgetOrphans(known: readonly { station: string; id: string }[]): void {
45
51
  for (const s of STATIONS) {
46
52
  if (s.forgetExcept === undefined) continue;
@@ -11,6 +11,8 @@ const CLIENT_NAME = 'Metro';
11
11
 
12
12
  export class OAuthInvalidGrant extends ConnectorVerifyError {}
13
13
 
14
+ const SIGN_IN_AGAIN = new Set(['invalid_grant', 'invalid_client', 'unauthorized_client']);
15
+
14
16
  export interface OAuthClient {
15
17
  clientId: string;
16
18
  clientSecret?: string;
@@ -143,10 +145,17 @@ async function postToken(
143
145
  } catch (err) {
144
146
  throw refused(`Metro could not reach that server to finish signing in: ${whyUnreachable(err)}`);
145
147
  }
146
- const body: unknown = await res.json().catch(() => null);
147
- if (form.grant_type === 'refresh_token' && res.status === 400 && isRecord(body) && body.error === 'invalid_grant') throw new OAuthInvalidGrant('That server requires a new sign-in.', 400);
148
- if (!res.ok || !isRecord(body))
149
- throw refused('That server refused to complete the sign-in.');
148
+ return tokenAnswer(form.grant_type === 'refresh_token', res.status, res.ok, await res.json().catch(() => null));
149
+ }
150
+
151
+ const errorCode = (body: unknown): string | null => (isRecord(body) && typeof body.error === 'string' && /^[a-z_]{1,64}$/.test(body.error) ? body.error : null);
152
+
153
+ const grantGone = (refreshing: boolean, status: number, code: string | null): boolean => refreshing && (status === 400 || status === 401) && code !== null && SIGN_IN_AGAIN.has(code);
154
+
155
+ function tokenAnswer(refreshing: boolean, status: number, ok: boolean, body: unknown): OAuthTokens {
156
+ const code = errorCode(body);
157
+ if (grantGone(refreshing, status, code)) throw new OAuthInvalidGrant('That server requires a new sign-in.', 400);
158
+ if (!ok || !isRecord(body)) throw refused(`That server refused to complete the sign-in (${String(status)}${code === null ? '' : ` ${code}`}).`);
150
159
  return tokensFrom(body);
151
160
  }
152
161
 
@@ -196,12 +196,13 @@ function makeDelete(clientFor: ClientFor): StationHandler {
196
196
  function makeAccounts(clientFor: ClientFor): StationHandler {
197
197
  return (id) => {
198
198
  const list = [...accounts.values()].map((a) => {
199
- const phone = clientFor(a.id).self();
199
+ const client = clientFor(a.id);
200
+ const phone = client.self();
200
201
  return {
201
202
  id: a.id,
202
203
  handle: phone === null ? null : `+${phone}`,
203
204
  url: phone === null ? null : `https://wa.me/${phone}`,
204
- connected: phone !== null,
205
+ status: client.status(),
205
206
  };
206
207
  });
207
208
  respond(id, { result: { accounts: list } });
@@ -4,6 +4,7 @@ import type { MemberList } from '@metro-labs/core/stations/types';
4
4
  import type { ChannelList } from '@metro-labs/core/stations/channel-directory';
5
5
  import type { ProfileChange } from '@metro-labs/core/stations/profile';
6
6
  import type { SenderProfile } from '@metro-labs/core/stations/sender-profile';
7
+ import type { AccountStatus } from '@metro-labs/core/stations/account-status';
7
8
  import type { WhatsAppAccount } from './types.js';
8
9
  import type { InboundMessage, ReactionInput } from './format.js';
9
10
  import type { SenderFound } from './resolve.js';
@@ -25,6 +26,7 @@ export interface WAMedia {
25
26
  export interface WAClient {
26
27
  account: WhatsAppAccount;
27
28
  self(): string | null;
29
+ status(): AccountStatus;
28
30
  start(handlers: InboundHandlers): Promise<void>;
29
31
  read(jid: string, options?: HistoryOptions): Promise<HistoryPage>;
30
32
  listMembers(jid: string, limit?: number): Promise<MemberList>;
@@ -1,7 +1,6 @@
1
1
  import makeWASocket, {
2
2
  jidNormalizedUser,
3
3
  Browsers,
4
- DisconnectReason,
5
4
  fetchLatestWaWebVersion,
6
5
  type proto,
7
6
  type WAMessage,
@@ -13,7 +12,7 @@ import { errMsg } from '@metro-labs/core/log';
13
12
  import type { WhatsAppAccount } from './types.js';
14
13
  import type { InboundHandlers, WAClient, WAMedia, ClientRuntime } from './client-types.js';
15
14
  export type { InboundHandlers, WAClient, WAMedia } from './client-types.js';
16
- import { makeProfileCache, nonEmpty, type SenderProfile } from '@metro-labs/core/stations/sender-profile';
15
+ import { makeProfileCache, type SenderProfile } from '@metro-labs/core/stations/sender-profile';
17
16
  import type { SelfRef } from './parse.js';
18
17
  import type { History } from './history.js';
19
18
  import { createClientHistory } from './client-history.js';
@@ -22,11 +21,13 @@ import { bindMessages } from './messages.js';
22
21
  import { listMembers } from './members.js';
23
22
  import { SendExpiry } from './send-expiry.js';
24
23
  import { baileysLogger } from './logger.js';
25
- import { makeNameBook, nameFiles, phoneOf, type NameBook } from './names.js';
24
+ import { makeNameBook, nameFiles, type NameBook } from './names.js';
25
+ import { limiter, PictureMemory, PROFILE_READS, readPeer, withName, type PeerProfile } from './profile.js';
26
26
  import { useAccountAuthState } from './auth-state.js';
27
27
  import { knownKey, makeKeyCache, targetKey, type KeyCache } from './keys.js';
28
28
  import { makeOutbox, type Outbox } from './outbox.js';
29
29
  import { deliveryNotes, describeNote } from './delivery.js';
30
+ import { closeOf, closeReason, ConnectionWatch, failureOf, type Closed } from './connection.js';
30
31
  import {
31
32
  ACK_WAIT_MS,
32
33
  bindAcks,
@@ -51,6 +52,7 @@ interface State {
51
52
  acks: AckWatch;
52
53
  names: NameBook;
53
54
  channels: WhatsAppChannels;
55
+ watch: ConnectionWatch;
54
56
  }
55
57
  type SendOpts = Parameters<WASocket['sendMessage']>[2];
56
58
  type SendContent = Parameters<WASocket['sendMessage']>[1];
@@ -107,19 +109,10 @@ function servedFromOutbox(
107
109
  return message;
108
110
  }
109
111
 
110
- const TERMINAL: Record<number, string> = {
111
- [DisconnectReason.loggedOut]: ' — re-pair required',
112
- [DisconnectReason.connectionReplaced]:
113
- ' — another metro took this device; not reconnecting (two sockets on one ' +
114
- 'identity evict each other and risk a 463 timelock)',
115
- };
116
-
117
- export function terminalReason(code: number | undefined): string | undefined {
118
- return code === undefined ? undefined : TERMINAL[code];
119
- }
120
-
121
- function onClose(st: State, code: number | undefined): void {
122
- const terminal = terminalReason(code);
112
+ function onClose(st: State, closed: Closed): void {
113
+ const { code } = closed;
114
+ const terminal = closeReason(closed);
115
+ if (!st.closed) st.watch.closed(closed);
123
116
  if (st.closed || terminal !== undefined) {
124
117
  process.stderr.write(
125
118
  `whatsapp[${st.account.id}] disconnected (code=${code ?? '?'})${terminal ?? ''}\n`,
@@ -132,6 +125,7 @@ function onClose(st: State, code: number | undefined): void {
132
125
  process.stderr.write(
133
126
  `whatsapp[${st.account.id}] reconnect failed: ${errMsg(e)}\n`,
134
127
  );
128
+ st.watch.failed(failureOf(e, 'reconnect'));
135
129
  });
136
130
  }
137
131
 
@@ -140,14 +134,15 @@ function bindConnection(st: State, sock: WASocket): void {
140
134
  const { connection, lastDisconnect } = update;
141
135
  if (connection === 'open') {
142
136
  process.stderr.write(`whatsapp[${st.account.id}] connected\n`);
137
+ if (st.sock === sock) st.watch.opened();
143
138
  st.openResolve?.();
144
139
  return;
145
140
  }
146
141
  if (connection !== 'close') return;
147
- const code = (
148
- lastDisconnect?.error as { output?: { statusCode?: number } } | undefined
149
- )?.output?.statusCode;
150
- onClose(st, code);
142
+ onClose(st, closeOf(lastDisconnect?.error));
143
+ });
144
+ sock.ws.on('frame', () => {
145
+ if (st.sock === sock) st.watch.heard();
151
146
  });
152
147
  }
153
148
 
@@ -254,24 +249,24 @@ function mediaContent(m: WAMedia): SendContent {
254
249
  };
255
250
  }
256
251
 
257
- async function readProfile(st: State, jid: string): Promise<SenderProfile> {
258
- const sock = await ready(st);
259
- const [statuses, avatar] = await Promise.all([sock.fetchStatus(jid).catch(() => undefined), sock.profilePictureUrl(jid, 'image').catch(() => undefined)]);
260
- const about = nonEmpty((statuses?.[0]?.status as { status?: unknown } | undefined)?.status);
261
- const picture = nonEmpty(avatar);
262
- const phone = phoneOf(jid.endsWith('@lid') ? await sock.signalRepository.lidMapping.getPNForLID(jid).catch(() => null) : jid);
263
- const name = st.names.get(jid);
264
- return {
265
- id: jid,
266
- ...(name === undefined ? {} : { display_name: name }),
267
- ...(phone === undefined ? {} : { name: phone, address: phone }),
268
- ...(about === undefined ? {} : { about }),
269
- ...(picture === undefined ? {} : { avatar: picture }),
252
+ function profileReader(st: State): (jid: string) => Promise<SenderProfile | null> {
253
+ const pictures = new PictureMemory();
254
+ const reads = limiter(PROFILE_READS);
255
+ const senders = makeProfileCache<PeerProfile>(async (jid) => {
256
+ const sock = await ready(st);
257
+ return reads(() => readPeer(sock, jid, pictures));
258
+ }, {
259
+ onError: (jid, err) => process.stderr.write(`whatsapp[${st.account.id}] could not read the profile of ${jid}: ${errMsg(err)}\n`),
260
+ });
261
+ return async (jid) => {
262
+ const peer = await senders.get(jid);
263
+ return peer === null ? null : withName(peer, st.names);
270
264
  };
271
265
  }
272
266
 
273
267
  async function disconnect(st: State): Promise<void> {
274
268
  st.closed = true;
269
+ st.watch.stop();
275
270
  st.openResolve?.();
276
271
  try {
277
272
  st.history.close();
@@ -295,6 +290,7 @@ function initialState(account: WhatsAppAccount, runtime: ClientRuntime): State {
295
290
  acks: makeAckWatch(),
296
291
  names: makeNameBook(nameFiles.path(account.id)),
297
292
  channels: new WhatsAppChannels(account.id),
293
+ watch: new ConnectionWatch(account.id, () => st.sock?.ws.isOpen !== false),
298
294
  };
299
295
  resetGate(st);
300
296
  return st;
@@ -305,9 +301,7 @@ export function createClient(
305
301
  runtime: ClientRuntime = { makeSocket: makeWASocket, fetchVersion: fetchLatestWaWebVersion },
306
302
  ): WAClient {
307
303
  const st = initialState(account, runtime);
308
- const senders = makeProfileCache<SenderProfile>((jid) => readProfile(st, jid), {
309
- onError: (jid, err) => process.stderr.write(`whatsapp[${st.account.id}] could not read the profile of ${jid}: ${errMsg(err)}\n`),
310
- });
304
+ const senderProfile = profileReader(st);
311
305
  return {
312
306
  account,
313
307
  self() {
@@ -317,14 +311,15 @@ export function createClient(
317
311
  },
318
312
  async start(handlers) {
319
313
  st.handlers = handlers;
314
+ st.watch.watch();
320
315
  try {
321
316
  await connect(st);
322
317
  } catch (e) {
323
- process.stderr.write(
324
- `whatsapp[${account.id}] connect failed: ${errMsg(e)}\n`,
325
- );
318
+ process.stderr.write(`whatsapp[${account.id}] connect failed: ${errMsg(e)}\n`);
319
+ st.watch.failed(failureOf(e, 'connect'));
326
320
  }
327
321
  },
322
+ status: () => st.watch.read(),
328
323
  read(jid, options) {
329
324
  const page = st.history.read(jid, options);
330
325
  for (const message of page.messages) st.keys.remember(message.key);
@@ -394,7 +389,7 @@ export function createClient(
394
389
  await sock.updateProfilePicture(me, { url: change.avatar.path });
395
390
  }
396
391
  },
397
- senderProfile: (jid) => senders.get(jid),
392
+ senderProfile,
398
393
  disconnect: () => st.closing ??= disconnect(st),
399
394
  };
400
395
  }
@@ -0,0 +1,139 @@
1
+ import { DisconnectReason } from 'baileys';
2
+ import { isRecord } from '@metro-labs/core/is-record';
3
+ import { errMsg } from '@metro-labs/core/log';
4
+ import { TrainError } from '@metro-labs/core/train-error';
5
+ import { accountStatus, type AccountStatus } from '@metro-labs/core/stations/account-status';
6
+
7
+ export const RECONNECT_GRACE_MS = 3 * 60_000;
8
+ export const SILENT_MS = 90_000;
9
+ const CHECK_MS = 30_000;
10
+
11
+ const TERMINAL: Record<number, string> = {
12
+ [DisconnectReason.loggedOut]: ' — re-pair required',
13
+ [DisconnectReason.connectionReplaced]:
14
+ ' — another metro took this device; not reconnecting (two sockets on one ' +
15
+ 'identity evict each other and risk a 463 timelock)',
16
+ };
17
+
18
+ export function terminalReason(code: number | undefined): string | undefined {
19
+ return code === undefined ? undefined : TERMINAL[code];
20
+ }
21
+
22
+ export const closeReason = (closed: Closed): string | undefined =>
23
+ terminalReason(closed.removed ? DisconnectReason.loggedOut : closed.code);
24
+
25
+ export interface Closed {
26
+ code: number | undefined;
27
+ message: string;
28
+ removed: boolean;
29
+ }
30
+
31
+ const recordOf = (value: unknown): Record<string, unknown> => (isRecord(value) ? value : {});
32
+
33
+ export function closeOf(error: unknown): Closed {
34
+ const boom = recordOf(error);
35
+ const output = recordOf(boom.output);
36
+ const data = recordOf(boom.data);
37
+ return {
38
+ code: typeof output.statusCode === 'number' ? output.statusCode : undefined,
39
+ message: error instanceof Error ? error.message : '',
40
+ removed: data.tag === 'conflict' && recordOf(data.attrs).type === 'device_removed',
41
+ };
42
+ }
43
+
44
+ export function statusAfterClose(closed: Closed, now: number): AccountStatus {
45
+ if (closed.removed) return accountStatus('logged_out', 'WhatsApp removed this linked device.', now);
46
+ if (closed.code === DisconnectReason.loggedOut) return accountStatus('logged_out', 'WhatsApp logged this device out.', now);
47
+ if (closed.code === DisconnectReason.connectionReplaced)
48
+ return accountStatus('disconnected', 'Another Metro took over this WhatsApp device, so this one stopped.', now);
49
+ const code = `code ${closed.code === undefined ? '?' : String(closed.code)}`;
50
+ return accountStatus('connecting', `The connection dropped (${closed.message === '' ? code : `${closed.message}, ${code}`}).`, now);
51
+ }
52
+
53
+ export function failureOf(e: unknown, step: 'connect' | 'reconnect'): AccountStatus {
54
+ if (e instanceof TrainError && e.code === 'whatsapp_auth') return accountStatus('logged_out', 'No WhatsApp link is stored for this channel.');
55
+ return accountStatus('disconnected', `Metro could not ${step} to WhatsApp: ${errMsg(e)}`);
56
+ }
57
+
58
+ export interface Liveness {
59
+ open: boolean;
60
+ socketOpen: boolean;
61
+ lastFrameAt: number;
62
+ }
63
+
64
+ function openStatus(current: AccountStatus, live: Liveness, now: number): AccountStatus {
65
+ const trouble = !live.socketOpen ? 'The WhatsApp connection is not open.' : now - live.lastFrameAt > SILENT_MS ? 'WhatsApp stopped answering.' : null;
66
+ if (trouble === null) return current.state === 'connected' ? current : accountStatus('connected', undefined, now);
67
+ return current.state === 'connected' ? accountStatus('connecting', trouble, now) : current;
68
+ }
69
+
70
+ export function liveStatus(current: AccountStatus, live: Liveness, now: number): AccountStatus {
71
+ if (current.state === 'logged_out') return current;
72
+ const next = live.open ? openStatus(current, live, now) : current;
73
+ if (next.state !== 'connecting' || now - next.since < RECONNECT_GRACE_MS) return next;
74
+ return accountStatus('disconnected', `${next.reason ?? 'Metro could not reach WhatsApp.'} It has not come back for 3 minutes.`, next.since + RECONNECT_GRACE_MS);
75
+ }
76
+
77
+ export class ConnectionWatch {
78
+ private status: AccountStatus;
79
+ private open = false;
80
+ private lastFrameAt = 0;
81
+ private timer: ReturnType<typeof setInterval> | null = null;
82
+
83
+ constructor(
84
+ private readonly accountId: string,
85
+ private readonly socketOpen: () => boolean,
86
+ now = Date.now(),
87
+ ) {
88
+ this.status = accountStatus('connecting', undefined, now);
89
+ }
90
+
91
+ opened(now = Date.now()): void {
92
+ this.open = true;
93
+ this.lastFrameAt = now;
94
+ this.enter(accountStatus('connected', undefined, now));
95
+ }
96
+
97
+ heard(now = Date.now()): void {
98
+ this.lastFrameAt = now;
99
+ }
100
+
101
+ closed(closed: Closed, now = Date.now()): void {
102
+ this.open = false;
103
+ const next = statusAfterClose(closed, now);
104
+ if (next.state !== 'connecting') this.enter(next);
105
+ else if (this.status.state === 'connecting') this.enter(accountStatus('connecting', next.reason, this.status.since));
106
+ else if (this.status.state !== 'disconnected') this.enter(next);
107
+ }
108
+
109
+ failed(status: AccountStatus): void {
110
+ this.open = false;
111
+ this.enter(status);
112
+ }
113
+
114
+ read(now = Date.now()): AccountStatus {
115
+ const next = liveStatus(this.status, { open: this.open, socketOpen: this.socketOpen(), lastFrameAt: this.lastFrameAt }, now);
116
+ if (next !== this.status) this.enter(next);
117
+ return this.status;
118
+ }
119
+
120
+ watch(): void {
121
+ if (this.timer !== null) return;
122
+ this.timer = setInterval(() => {
123
+ this.read();
124
+ }, CHECK_MS);
125
+ this.timer.unref();
126
+ }
127
+
128
+ stop(): void {
129
+ if (this.timer !== null) clearInterval(this.timer);
130
+ this.timer = null;
131
+ }
132
+
133
+ private enter(next: AccountStatus): void {
134
+ const before = this.status.state;
135
+ this.status = next;
136
+ if (before === next.state) return;
137
+ process.stderr.write(`whatsapp[${this.accountId}] state ${before} -> ${next.state}${next.reason === undefined ? '' : `: ${next.reason}`}\n`);
138
+ }
139
+ }
@@ -6,6 +6,7 @@ import makeWASocket, {
6
6
  } from 'baileys';
7
7
  import { errMsg, log } from '@metro-labs/core/log';
8
8
  import { inMemoryAuthState } from './auth-state.js';
9
+ import type { ClientRuntime } from './client-types.js';
9
10
 
10
11
  export class WhatsappLoginError extends Error {}
11
12
 
@@ -32,6 +33,13 @@ export function normalizePhone(raw: unknown): string {
32
33
  return phone;
33
34
  }
34
35
 
36
+ export function assertSamePhone(phone: string, expect: string): void {
37
+ if (expect !== '' && phone !== '' && phone !== expect)
38
+ throw new WhatsappLoginError(
39
+ `This channel is +${expect}. Pair it again with that number, or add a new channel for +${phone}.`,
40
+ );
41
+ }
42
+
35
43
  function closeReason(err: unknown): number | undefined {
36
44
  return (err as { output?: { statusCode?: number } } | undefined)?.output
37
45
  ?.statusCode;
@@ -47,14 +55,18 @@ export class WhatsappLogin {
47
55
  constructor(
48
56
  private phone: string,
49
57
  private events: WhatsappLoginEvents,
50
- ) {}
58
+ private expect = '',
59
+ private runtime: ClientRuntime = { makeSocket: makeWASocket, fetchVersion: fetchLatestWaWebVersion },
60
+ ) {
61
+ assertSamePhone(phone, expect);
62
+ }
51
63
 
52
64
  get mode(): 'pairing-code' | 'qr' {
53
65
  return this.phone === '' ? 'qr' : 'pairing-code';
54
66
  }
55
67
 
56
68
  async start(): Promise<void> {
57
- const { version, error } = await fetchLatestWaWebVersion({});
69
+ const { version, error } = await this.runtime.fetchVersion({});
58
70
  if (error)
59
71
  throw new WhatsappLoginError(
60
72
  'could not reach WhatsApp to start the pairing',
@@ -64,7 +76,7 @@ export class WhatsappLogin {
64
76
 
65
77
  private open(version: [number, number, number]): void {
66
78
  if (this.settled) return;
67
- const sock = makeWASocket({
79
+ const sock = this.runtime.makeSocket({
68
80
  version,
69
81
  auth: this.auth.state,
70
82
  browser: Browsers.macOS('Safari'),
@@ -131,6 +143,10 @@ export class WhatsappLogin {
131
143
  );
132
144
  return;
133
145
  }
146
+ if (this.expect !== '' && phone !== this.expect) {
147
+ this.refuseOther(phone);
148
+ return;
149
+ }
134
150
  const credentials = this.auth.serialize();
135
151
  this.settled = true;
136
152
  this.closeSocket()
@@ -145,6 +161,23 @@ export class WhatsappLogin {
145
161
  });
146
162
  }
147
163
 
164
+ private refuseOther(phone: string): void {
165
+ this.settled = true;
166
+ const sock = this.sock;
167
+ const message = `You linked +${phone}, but this channel is +${this.expect}. Nothing was changed. Scan the code with the phone of +${this.expect}.`;
168
+ (sock === null ? Promise.resolve() : sock.logout('Metro: another phone than the channel'))
169
+ .catch((err: unknown) => {
170
+ log.warn({ err: errMsg(err) }, 'whatsapp login: could not unlink the other phone');
171
+ })
172
+ .then(() => this.closeSocket())
173
+ .then(() => {
174
+ this.events.onFailed(message);
175
+ })
176
+ .catch((err: unknown) => {
177
+ log.warn({ err: errMsg(err) }, 'whatsapp login: failure report failed');
178
+ });
179
+ }
180
+
148
181
  private fail(message: string): void {
149
182
  if (this.settled) return;
150
183
  this.settled = true;
@@ -1,4 +1,4 @@
1
- import { jidNormalizedUser, type Contact, type WASocket } from 'baileys';
1
+ import { jidNormalizedUser, type Contact, type WAMessage, type WASocket } from 'baileys';
2
2
  import { bindAppState, noteHistoryContact } from './app-state.js';
3
3
  import type { InboundHandlers } from './client-types.js';
4
4
  import type { History } from './history.js';
@@ -16,6 +16,20 @@ interface MessageHooks {
16
16
  self(): SelfRef;
17
17
  }
18
18
 
19
+ const person = (jid: string | null | undefined): string | undefined =>
20
+ jid && (jid.endsWith('@s.whatsapp.net') || jid.endsWith('@lid')) ? jidNormalizedUser(jid) : undefined;
21
+
22
+ const pair = (a: string, b: string): [string, string] => (a.endsWith('@lid') ? [b, a] : [a, b]);
23
+
24
+ function noteSender(names: NameBook, m: WAMessage): void {
25
+ if (m.key.fromMe === true) return;
26
+ const name = m.pushName ?? m.verifiedBizName;
27
+ const sender = person(m.key.participant ?? m.key.remoteJid);
28
+ const alt = person(m.key.participantAlt ?? m.key.remoteJidAlt);
29
+ for (const jid of [sender, alt]) names.note(jid, name);
30
+ if (sender !== undefined && alt !== undefined) names.alias(...pair(sender, alt));
31
+ }
32
+
19
33
  export function bindMessages(sock: Pick<WASocket, 'ev' | 'user'>, hooks: MessageHooks): void {
20
34
  const selfJid = (): string | undefined => sock.user?.id ? jidNormalizedUser(sock.user.id) : undefined;
21
35
  const noteContacts = (contacts: Partial<Contact>[]): void => {
@@ -37,18 +51,25 @@ export function bindMessages(sock: Pick<WASocket, 'ev' | 'user'>, hooks: Message
37
51
  const inbound = toInbound(hooks.accountId, m, hooks.self());
38
52
  if (!inbound) continue;
39
53
  hooks.names.note(inbound.senderJid, inbound.pushName);
54
+ noteSender(hooks.names, m);
40
55
  handlers.onMessage(inbound, m);
41
56
  }
42
57
  });
58
+ sock.ev.on('lid-mapping.update', ({ pn, lid }) => {
59
+ if (hooks.current()) hooks.names.alias(pn, lid);
60
+ });
43
61
  sock.ev.on('contacts.upsert', noteContacts);
44
62
  sock.ev.on('contacts.update', noteContacts);
45
63
  sock.ev.on('messaging-history.set', ({ contacts, messages, lidPnMappings }) => {
46
64
  if (!hooks.current()) return;
47
65
  noteContacts(contacts);
48
- for (const { pn, lid } of lidPnMappings ?? []) hooks.history.alias(pn, lid);
66
+ for (const { pn, lid } of lidPnMappings ?? []) {
67
+ hooks.history.alias(pn, lid);
68
+ hooks.names.alias(pn, lid);
69
+ }
49
70
  for (const m of messages) {
50
71
  hooks.keys.remember(m.key);
51
- if (m.key.fromMe !== true) hooks.names.note(m.key.participant ?? m.key.remoteJid, m.pushName);
72
+ noteSender(hooks.names, m);
52
73
  }
53
74
  hooks.history.ingest(messages, selfJid());
54
75
  });