* refactor: introduce provider plugin registry * refactor: move provider CLI to plugins * docs: add provider plugin implementation notes * refactor: shift provider runtime logic into plugins * refactor: add plugin defaults and summaries * docs: update provider plugin notes * feat(commands): add /commands slash list * Auto-reply: tidy help message * Auto-reply: fix status command lint * Tests: align google shared expectations * Auto-reply: tidy help message * Auto-reply: fix status command lint * refactor: move provider routing into plugins * test: align agent routing expectations * docs: update provider plugin notes * refactor: route replies via provider plugins * docs: note route-reply plugin hooks * refactor: extend provider plugin contract * refactor: derive provider status from plugins * refactor: unify gateway provider control * refactor: use plugin metadata in auto-reply * fix: parenthesize cron target selection * refactor: derive gateway methods from plugins * refactor: generalize provider logout * refactor: route provider logout through plugins * refactor: move WhatsApp web login methods into plugin * refactor: generalize provider log prefixes * refactor: centralize default chat provider * refactor: derive provider lists from registry * refactor: move provider reload noops into plugins * refactor: resolve web login provider via alias * refactor: derive CLI provider options from plugins * refactor: derive prompt provider list from plugins * style: apply biome lint fixes * fix: resolve provider routing edge cases * docs: update provider plugin refactor notes * fix(gateway): harden agent provider routing * refactor: move provider routing into plugins * refactor: move provider CLI to plugins * refactor: derive provider lists from registry * fix: restore slash command parsing * refactor: align provider ids for schema * refactor: unify outbound target resolution * fix: keep outbound labels stable * feat: add msteams to cron surfaces * fix: clean up lint build issues * refactor: localize chat provider alias normalization * refactor: drive gateway provider lists from plugins * docs: update provider plugin notes * style: format message-provider * fix: avoid provider registry init cycles * style: sort message-provider imports * fix: relax provider alias map typing * refactor: move provider routing into plugins * refactor: add plugin pairing/config adapters * refactor: route pairing and provider removal via plugins * refactor: align auto-reply provider typing * test: stabilize telegram media mocks * docs: update provider plugin refactor notes * refactor: pluginize outbound targets * refactor: pluginize provider selection * refactor: generalize text chunk limits * docs: update provider plugin notes * refactor: generalize group session/config * fix: normalize provider id for room detection * fix: avoid provider init in system prompt * style: formatting cleanup * refactor: normalize agent delivery targets * test: update outbound delivery labels * chore: fix lint regressions * refactor: extend provider plugin adapters * refactor: move elevated/block streaming defaults to plugins * refactor: defer outbound send deps to plugins * docs: note plugin-driven streaming/elevated defaults * refactor: centralize webchat provider constant * refactor: add provider setup adapters * refactor: delegate provider add config to plugins * docs: document plugin-driven provider add * refactor: add plugin state/binding metadata * refactor: build agent provider status from plugins * docs: note plugin-driven agent bindings * refactor: centralize internal provider constant usage * fix: normalize WhatsApp targets for groups and E.164 (#631) (thanks @imfing) * refactor: centralize default chat provider * refactor: centralize WhatsApp target normalization * refactor: move provider routing into plugins * refactor: normalize agent delivery targets * chore: fix lint regressions * fix: normalize WhatsApp targets for groups and E.164 (#631) (thanks @imfing) * feat: expand provider plugin adapters * refactor: route auto-reply via provider plugins * fix: align WhatsApp target normalization * fix: normalize WhatsApp targets for groups and E.164 (#631) (thanks @imfing) * refactor: centralize WhatsApp target normalization * feat: add /config chat config updates * docs: add /config get alias * feat(commands): add /commands slash list * refactor: centralize default chat provider * style: apply biome lint fixes * chore: fix lint regressions * fix: clean up whatsapp allowlist typing * style: format config command helpers * refactor: pluginize tool threading context * refactor: normalize session announce targets * docs: note new plugin threading and announce hooks * refactor: pluginize message actions * docs: update provider plugin actions notes * fix: align provider action adapters * refactor: centralize webchat checks * style: format message provider helpers * refactor: move provider onboarding into adapters * docs: note onboarding provider adapters * feat: add msteams onboarding adapter * style: organize onboarding imports * fix: normalize msteams allowFrom types * feat: add plugin text chunk limits * refactor: use plugin chunk limit fallbacks * feat: add provider mention stripping hooks * style: organize provider plugin type imports * refactor: generalize health snapshots * refactor: update macOS health snapshot handling * docs: refresh health snapshot notes * style: format health snapshot updates * refactor: drive security warnings via plugins * docs: note provider security adapter * style: format provider security adapters * refactor: centralize provider account defaults * refactor: type gateway client identity constants * chore: regen gateway protocol swift * fix: degrade health on failed provider probe * refactor: centralize pairing approve hint * docs: add plugin CLI command references * refactor: route auth and tool sends through plugins * docs: expand provider plugin hooks * refactor: document provider docking touchpoints * refactor: normalize internal provider defaults * refactor: streamline outbound delivery wiring * refactor: make provider onboarding plugin-owned * refactor: support provider-owned agent tools * refactor: move telegram draft chunking into telegram module * refactor: infer provider tool sends via extractToolSend * fix: repair plugin onboarding imports * refactor: de-dup outbound target normalization * style: tidy plugin and agent imports * refactor: data-drive provider selection line * fix: satisfy lint after provider plugin rebase * test: deflake gateway-cli coverage * style: format gateway-cli coverage test * refactor(provider-plugins): simplify provider ids * test(pairing-cli): avoid provider-specific ternary * style(macos): swiftformat HealthStore * refactor(sandbox): derive provider tool denylist * fix(sandbox): avoid plugin init in defaults * refactor(provider-plugins): centralize provider aliases * style(test): satisfy biome * refactor(protocol): v3 providers.status maps * refactor(ui): adapt to protocol v3 * refactor(macos): adapt to protocol v3 * test: update providers.status v3 fixtures * refactor(gateway): map provider runtime snapshot * test(gateway): update reload runtime snapshot * refactor(whatsapp): normalize heartbeat provider id * docs(refactor): update provider plugin notes * style: satisfy biome after rebase * fix: describe sandboxed elevated in prompt * feat(gateway): add agent image attachments + live probe * refactor: derive CLI provider options from plugins * fix(gateway): harden agent provider routing * fix(gateway): harden agent provider routing * refactor: align provider ids for schema * fix(protocol): keep agent provider string * fix(gateway): harden agent provider routing * fix(protocol): keep agent provider string * refactor: normalize agent delivery targets * refactor: support provider-owned agent tools * refactor(config): provider-keyed elevated allowFrom * style: satisfy biome * fix(gateway): appease provider narrowing * style: satisfy biome * refactor(reply): move group intro hints into plugin * fix(reply): avoid plugin registry init cycle * refactor(providers): add lightweight provider dock * refactor(gateway): use typed client id in connect * refactor(providers): document docks and avoid init cycles * refactor(providers): make media limit helper generic * fix(providers): break plugin registry import cycles * style: satisfy biome * refactor(status-all): build providers table from plugins * refactor(gateway): delegate web login to provider plugin * refactor(provider): drop web alias * refactor(provider): lazy-load monitors * style: satisfy lint/format * style: format status-all providers table * style: swiftformat gateway discovery model * test: make reload plan plugin-driven * fix: avoid token stringification in status-all * refactor: make provider IDs explicit in status * feat: warn on signal/imessage provider runtime errors * test: cover gateway provider runtime warnings in status * fix: add runtime kind to provider status issues * test: cover health degradation on probe failure * fix: keep routeReply lightweight * style: organize routeReply imports * refactor(web): extract auth-store helpers * refactor(whatsapp): lazy login imports * refactor(outbound): route replies via plugin outbound * docs: update provider plugin notes * style: format provider status issues * fix: make sandbox scope warning wrap-safe * refactor: load outbound adapters from provider plugins * docs: update provider plugin outbound notes * style(macos): fix swiftformat lint * docs: changelog for provider plugins * fix(macos): satisfy swiftformat * fix(macos): open settings via menu action * style: format after rebase * fix(macos): open Settings via menu action --------- Co-authored-by: LK <luke@kyohere.com> Co-authored-by: Luke K (pr-0f3t) <2609441+lc0rp@users.noreply.github.com> Co-authored-by: Xin <xin@imfing.com>
476 lines
16 KiB
TypeScript
476 lines
16 KiB
TypeScript
import {
|
|
createActionGate,
|
|
readStringParam,
|
|
} from "../../agents/tools/common.js";
|
|
import { handleWhatsAppAction } from "../../agents/tools/whatsapp-actions.js";
|
|
import { chunkText } from "../../auto-reply/chunk.js";
|
|
import { shouldLogVerbose } from "../../globals.js";
|
|
import {
|
|
DEFAULT_ACCOUNT_ID,
|
|
normalizeAccountId,
|
|
} from "../../routing/session-key.js";
|
|
import { normalizeE164 } from "../../utils.js";
|
|
import {
|
|
listWhatsAppAccountIds,
|
|
type ResolvedWhatsAppAccount,
|
|
resolveDefaultWhatsAppAccountId,
|
|
resolveWhatsAppAccount,
|
|
} from "../../web/accounts.js";
|
|
import { getActiveWebListener } from "../../web/active-listener.js";
|
|
import {
|
|
getWebAuthAgeMs,
|
|
logoutWeb,
|
|
logWebSelfId,
|
|
readWebSelfId,
|
|
webAuthExists,
|
|
} from "../../web/auth-store.js";
|
|
import { sendMessageWhatsApp, sendPollWhatsApp } from "../../web/outbound.js";
|
|
import {
|
|
isWhatsAppGroupJid,
|
|
normalizeWhatsAppTarget,
|
|
} from "../../whatsapp/normalize.js";
|
|
import { getChatProviderMeta } from "../registry.js";
|
|
import { createWhatsAppLoginTool } from "./agent-tools/whatsapp-login.js";
|
|
import { resolveWhatsAppGroupRequireMention } from "./group-mentions.js";
|
|
import { formatPairingApproveHint } from "./helpers.js";
|
|
import { normalizeWhatsAppMessagingTarget } from "./normalize-target.js";
|
|
import { whatsappOnboardingAdapter } from "./onboarding/whatsapp.js";
|
|
import {
|
|
applyAccountNameToProviderSection,
|
|
migrateBaseNameToDefaultAccount,
|
|
} from "./setup-helpers.js";
|
|
import { collectWhatsAppStatusIssues } from "./status-issues/whatsapp.js";
|
|
import type { ProviderMessageActionName, ProviderPlugin } from "./types.js";
|
|
import { resolveWhatsAppHeartbeatRecipients } from "./whatsapp-heartbeat.js";
|
|
|
|
const meta = getChatProviderMeta("whatsapp");
|
|
|
|
const escapeRegExp = (value: string) =>
|
|
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
export const whatsappPlugin: ProviderPlugin<ResolvedWhatsAppAccount> = {
|
|
id: "whatsapp",
|
|
meta: {
|
|
...meta,
|
|
showConfigured: false,
|
|
quickstartAllowFrom: true,
|
|
forceAccountBinding: true,
|
|
preferSessionLookupForAnnounceTarget: true,
|
|
},
|
|
onboarding: whatsappOnboardingAdapter,
|
|
agentTools: () => [createWhatsAppLoginTool()],
|
|
pairing: {
|
|
idLabel: "whatsappSenderId",
|
|
},
|
|
capabilities: {
|
|
chatTypes: ["direct", "group"],
|
|
polls: true,
|
|
reactions: true,
|
|
media: true,
|
|
},
|
|
reload: { configPrefixes: ["web"], noopPrefixes: ["whatsapp"] },
|
|
gatewayMethods: ["web.login.start", "web.login.wait"],
|
|
config: {
|
|
listAccountIds: (cfg) => listWhatsAppAccountIds(cfg),
|
|
resolveAccount: (cfg, accountId) =>
|
|
resolveWhatsAppAccount({ cfg, accountId }),
|
|
defaultAccountId: (cfg) => resolveDefaultWhatsAppAccountId(cfg),
|
|
setAccountEnabled: ({ cfg, accountId, enabled }) => {
|
|
const accountKey = accountId || DEFAULT_ACCOUNT_ID;
|
|
const accounts = { ...cfg.whatsapp?.accounts };
|
|
const existing = accounts[accountKey] ?? {};
|
|
return {
|
|
...cfg,
|
|
whatsapp: {
|
|
...cfg.whatsapp,
|
|
accounts: {
|
|
...accounts,
|
|
[accountKey]: {
|
|
...existing,
|
|
enabled,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
},
|
|
deleteAccount: ({ cfg, accountId }) => {
|
|
const accountKey = accountId || DEFAULT_ACCOUNT_ID;
|
|
const accounts = { ...cfg.whatsapp?.accounts };
|
|
delete accounts[accountKey];
|
|
return {
|
|
...cfg,
|
|
whatsapp: {
|
|
...cfg.whatsapp,
|
|
accounts: Object.keys(accounts).length ? accounts : undefined,
|
|
},
|
|
};
|
|
},
|
|
isEnabled: (account, cfg) =>
|
|
account.enabled !== false && cfg.web?.enabled !== false,
|
|
disabledReason: () => "disabled",
|
|
isConfigured: async (account) => await webAuthExists(account.authDir),
|
|
unconfiguredReason: () => "not linked",
|
|
describeAccount: (account) => ({
|
|
accountId: account.accountId,
|
|
name: account.name,
|
|
enabled: account.enabled,
|
|
configured: Boolean(account.authDir),
|
|
dmPolicy: account.dmPolicy,
|
|
allowFrom: account.allowFrom,
|
|
}),
|
|
resolveAllowFrom: ({ cfg, accountId }) =>
|
|
resolveWhatsAppAccount({ cfg, accountId }).allowFrom ?? [],
|
|
formatAllowFrom: ({ allowFrom }) =>
|
|
allowFrom
|
|
.map((entry) => String(entry).trim())
|
|
.filter((entry): entry is string => Boolean(entry))
|
|
.map((entry) =>
|
|
entry === "*" ? entry : normalizeWhatsAppTarget(entry),
|
|
)
|
|
.filter((entry): entry is string => Boolean(entry)),
|
|
},
|
|
security: {
|
|
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
|
const resolvedAccountId =
|
|
accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
|
const useAccountPath = Boolean(
|
|
cfg.whatsapp?.accounts?.[resolvedAccountId],
|
|
);
|
|
const basePath = useAccountPath
|
|
? `whatsapp.accounts.${resolvedAccountId}.`
|
|
: "whatsapp.";
|
|
return {
|
|
policy: account.dmPolicy ?? "pairing",
|
|
allowFrom: account.allowFrom ?? [],
|
|
policyPath: `${basePath}dmPolicy`,
|
|
allowFromPath: basePath,
|
|
approveHint: formatPairingApproveHint("whatsapp"),
|
|
normalizeEntry: (raw) => normalizeE164(raw),
|
|
};
|
|
},
|
|
},
|
|
setup: {
|
|
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
|
applyAccountName: ({ cfg, accountId, name }) =>
|
|
applyAccountNameToProviderSection({
|
|
cfg,
|
|
providerKey: "whatsapp",
|
|
accountId,
|
|
name,
|
|
alwaysUseAccounts: true,
|
|
}),
|
|
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
const namedConfig = applyAccountNameToProviderSection({
|
|
cfg,
|
|
providerKey: "whatsapp",
|
|
accountId,
|
|
name: input.name,
|
|
alwaysUseAccounts: true,
|
|
});
|
|
const next = migrateBaseNameToDefaultAccount({
|
|
cfg: namedConfig,
|
|
providerKey: "whatsapp",
|
|
alwaysUseAccounts: true,
|
|
});
|
|
const entry = {
|
|
...next.whatsapp?.accounts?.[accountId],
|
|
...(input.authDir ? { authDir: input.authDir } : {}),
|
|
enabled: true,
|
|
};
|
|
return {
|
|
...next,
|
|
whatsapp: {
|
|
...next.whatsapp,
|
|
accounts: {
|
|
...next.whatsapp?.accounts,
|
|
[accountId]: entry,
|
|
},
|
|
},
|
|
};
|
|
},
|
|
},
|
|
groups: {
|
|
resolveRequireMention: resolveWhatsAppGroupRequireMention,
|
|
resolveGroupIntroHint: () =>
|
|
"WhatsApp IDs: SenderId is the participant JID; [message_id: ...] is the message id for reactions (use SenderId as participant).",
|
|
},
|
|
mentions: {
|
|
stripPatterns: ({ ctx }) => {
|
|
const selfE164 = (ctx.To ?? "").replace(/^whatsapp:/, "");
|
|
if (!selfE164) return [];
|
|
const escaped = escapeRegExp(selfE164);
|
|
return [escaped, `@${escaped}`];
|
|
},
|
|
},
|
|
commands: {
|
|
enforceOwnerForCommands: true,
|
|
skipWhenConfigEmpty: true,
|
|
},
|
|
messaging: {
|
|
normalizeTarget: normalizeWhatsAppMessagingTarget,
|
|
},
|
|
actions: {
|
|
listActions: ({ cfg }) => {
|
|
if (!cfg.whatsapp) return [];
|
|
const gate = createActionGate(cfg.whatsapp.actions);
|
|
const actions = new Set<ProviderMessageActionName>();
|
|
if (gate("reactions")) actions.add("react");
|
|
if (gate("polls")) actions.add("poll");
|
|
return Array.from(actions);
|
|
},
|
|
supportsAction: ({ action }) => action === "react",
|
|
handleAction: async ({ action, params, cfg, accountId }) => {
|
|
if (action !== "react") {
|
|
throw new Error(
|
|
`Action ${action} is not supported for provider ${meta.id}.`,
|
|
);
|
|
}
|
|
const messageId = readStringParam(params, "messageId", {
|
|
required: true,
|
|
});
|
|
const emoji = readStringParam(params, "emoji", { allowEmpty: true });
|
|
const remove =
|
|
typeof params.remove === "boolean" ? params.remove : undefined;
|
|
return await handleWhatsAppAction(
|
|
{
|
|
action: "react",
|
|
chatJid:
|
|
readStringParam(params, "chatJid") ??
|
|
readStringParam(params, "to", { required: true }),
|
|
messageId,
|
|
emoji,
|
|
remove,
|
|
participant: readStringParam(params, "participant"),
|
|
accountId: accountId ?? undefined,
|
|
fromMe:
|
|
typeof params.fromMe === "boolean" ? params.fromMe : undefined,
|
|
},
|
|
cfg,
|
|
);
|
|
},
|
|
},
|
|
outbound: {
|
|
deliveryMode: "gateway",
|
|
chunker: chunkText,
|
|
textChunkLimit: 4000,
|
|
pollMaxOptions: 12,
|
|
resolveTarget: ({ to, allowFrom, mode }) => {
|
|
const trimmed = to?.trim() ?? "";
|
|
const allowListRaw = (allowFrom ?? [])
|
|
.map((entry) => String(entry).trim())
|
|
.filter(Boolean);
|
|
const hasWildcard = allowListRaw.includes("*");
|
|
const allowList = allowListRaw
|
|
.filter((entry) => entry !== "*")
|
|
.map((entry) => normalizeWhatsAppTarget(entry))
|
|
.filter((entry): entry is string => Boolean(entry));
|
|
|
|
if (trimmed) {
|
|
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
|
if (!normalizedTo) {
|
|
if (
|
|
(mode === "implicit" || mode === "heartbeat") &&
|
|
allowList.length > 0
|
|
) {
|
|
return { ok: true, to: allowList[0] };
|
|
}
|
|
return {
|
|
ok: false,
|
|
error: new Error(
|
|
"Delivering to WhatsApp requires --to <E.164|group JID> or whatsapp.allowFrom[0]",
|
|
),
|
|
};
|
|
}
|
|
if (isWhatsAppGroupJid(normalizedTo)) {
|
|
return { ok: true, to: normalizedTo };
|
|
}
|
|
if (mode === "implicit" || mode === "heartbeat") {
|
|
if (hasWildcard || allowList.length === 0) {
|
|
return { ok: true, to: normalizedTo };
|
|
}
|
|
if (allowList.includes(normalizedTo)) {
|
|
return { ok: true, to: normalizedTo };
|
|
}
|
|
return { ok: true, to: allowList[0] };
|
|
}
|
|
return { ok: true, to: normalizedTo };
|
|
}
|
|
|
|
if (allowList.length > 0) {
|
|
return { ok: true, to: allowList[0] };
|
|
}
|
|
return {
|
|
ok: false,
|
|
error: new Error(
|
|
"Delivering to WhatsApp requires --to <E.164|group JID> or whatsapp.allowFrom[0]",
|
|
),
|
|
};
|
|
},
|
|
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
|
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
|
|
const result = await send(to, text, {
|
|
verbose: false,
|
|
accountId: accountId ?? undefined,
|
|
gifPlayback,
|
|
});
|
|
return { provider: "whatsapp", ...result };
|
|
},
|
|
sendMedia: async ({ to, text, mediaUrl, accountId, deps, gifPlayback }) => {
|
|
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
|
|
const result = await send(to, text, {
|
|
verbose: false,
|
|
mediaUrl,
|
|
accountId: accountId ?? undefined,
|
|
gifPlayback,
|
|
});
|
|
return { provider: "whatsapp", ...result };
|
|
},
|
|
sendPoll: async ({ to, poll, accountId }) =>
|
|
await sendPollWhatsApp(to, poll, {
|
|
verbose: shouldLogVerbose(),
|
|
accountId: accountId ?? undefined,
|
|
}),
|
|
},
|
|
auth: {
|
|
login: async ({ cfg, accountId, runtime, verbose }) => {
|
|
const resolvedAccountId =
|
|
accountId?.trim() || resolveDefaultWhatsAppAccountId(cfg);
|
|
const { loginWeb } = await import("../../web/login.js");
|
|
await loginWeb(Boolean(verbose), undefined, runtime, resolvedAccountId);
|
|
},
|
|
},
|
|
heartbeat: {
|
|
checkReady: async ({ cfg, accountId, deps }) => {
|
|
if (cfg.web?.enabled === false) {
|
|
return { ok: false, reason: "whatsapp-disabled" };
|
|
}
|
|
const account = resolveWhatsAppAccount({ cfg, accountId });
|
|
const authExists = await (deps?.webAuthExists ?? webAuthExists)(
|
|
account.authDir,
|
|
);
|
|
if (!authExists) {
|
|
return { ok: false, reason: "whatsapp-not-linked" };
|
|
}
|
|
const listenerActive = deps?.hasActiveWebListener
|
|
? deps.hasActiveWebListener()
|
|
: Boolean(getActiveWebListener());
|
|
if (!listenerActive) {
|
|
return { ok: false, reason: "whatsapp-not-running" };
|
|
}
|
|
return { ok: true, reason: "ok" };
|
|
},
|
|
resolveRecipients: ({ cfg, opts }) =>
|
|
resolveWhatsAppHeartbeatRecipients(cfg, opts),
|
|
},
|
|
status: {
|
|
defaultRuntime: {
|
|
accountId: DEFAULT_ACCOUNT_ID,
|
|
running: false,
|
|
connected: false,
|
|
reconnectAttempts: 0,
|
|
lastConnectedAt: null,
|
|
lastDisconnect: null,
|
|
lastMessageAt: null,
|
|
lastEventAt: null,
|
|
lastError: null,
|
|
},
|
|
collectStatusIssues: collectWhatsAppStatusIssues,
|
|
buildProviderSummary: async ({ account, snapshot }) => {
|
|
const authDir = account.authDir;
|
|
const linked =
|
|
typeof snapshot.linked === "boolean"
|
|
? snapshot.linked
|
|
: authDir
|
|
? await webAuthExists(authDir)
|
|
: false;
|
|
const authAgeMs = linked && authDir ? getWebAuthAgeMs(authDir) : null;
|
|
const self =
|
|
linked && authDir ? readWebSelfId(authDir) : { e164: null, jid: null };
|
|
return {
|
|
configured: linked,
|
|
linked,
|
|
authAgeMs,
|
|
self,
|
|
running: snapshot.running ?? false,
|
|
connected: snapshot.connected ?? false,
|
|
lastConnectedAt: snapshot.lastConnectedAt ?? null,
|
|
lastDisconnect: snapshot.lastDisconnect ?? null,
|
|
reconnectAttempts: snapshot.reconnectAttempts,
|
|
lastMessageAt: snapshot.lastMessageAt ?? null,
|
|
lastEventAt: snapshot.lastEventAt ?? null,
|
|
lastError: snapshot.lastError ?? null,
|
|
};
|
|
},
|
|
buildAccountSnapshot: async ({ account, runtime }) => {
|
|
const linked = await webAuthExists(account.authDir);
|
|
return {
|
|
accountId: account.accountId,
|
|
name: account.name,
|
|
enabled: account.enabled,
|
|
configured: true,
|
|
linked,
|
|
running: runtime?.running ?? false,
|
|
connected: runtime?.connected ?? false,
|
|
reconnectAttempts: runtime?.reconnectAttempts,
|
|
lastConnectedAt: runtime?.lastConnectedAt ?? null,
|
|
lastDisconnect: runtime?.lastDisconnect ?? null,
|
|
lastMessageAt: runtime?.lastMessageAt ?? null,
|
|
lastEventAt: runtime?.lastEventAt ?? null,
|
|
lastError: runtime?.lastError ?? null,
|
|
dmPolicy: account.dmPolicy,
|
|
allowFrom: account.allowFrom,
|
|
};
|
|
},
|
|
resolveAccountState: ({ configured }) =>
|
|
configured ? "linked" : "not linked",
|
|
logSelfId: ({ account, runtime, includeProviderPrefix }) => {
|
|
logWebSelfId(account.authDir, runtime, includeProviderPrefix);
|
|
},
|
|
},
|
|
gateway: {
|
|
startAccount: async (ctx) => {
|
|
const account = ctx.account;
|
|
const { e164, jid } = readWebSelfId(account.authDir);
|
|
const identity = e164 ? e164 : jid ? `jid ${jid}` : "unknown";
|
|
ctx.log?.info(`[${account.accountId}] starting provider (${identity})`);
|
|
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
|
const { monitorWebProvider } = await import("../web/index.js");
|
|
return monitorWebProvider(
|
|
shouldLogVerbose(),
|
|
undefined,
|
|
true,
|
|
undefined,
|
|
ctx.runtime,
|
|
ctx.abortSignal,
|
|
{
|
|
statusSink: (next) =>
|
|
ctx.setStatus({ accountId: ctx.accountId, ...next }),
|
|
accountId: account.accountId,
|
|
},
|
|
);
|
|
},
|
|
loginWithQrStart: async ({ accountId, force, timeoutMs, verbose }) =>
|
|
await (async () => {
|
|
const { startWebLoginWithQr } = await import("../../web/login-qr.js");
|
|
return await startWebLoginWithQr({
|
|
accountId,
|
|
force,
|
|
timeoutMs,
|
|
verbose,
|
|
});
|
|
})(),
|
|
loginWithQrWait: async ({ accountId, timeoutMs }) =>
|
|
await (async () => {
|
|
const { waitForWebLogin } = await import("../../web/login-qr.js");
|
|
return await waitForWebLogin({ accountId, timeoutMs });
|
|
})(),
|
|
logoutAccount: async ({ account, runtime }) => {
|
|
const cleared = await logoutWeb({
|
|
authDir: account.authDir,
|
|
isLegacyAuthDir: account.isLegacyAuthDir,
|
|
runtime,
|
|
});
|
|
return { cleared, loggedOut: cleared };
|
|
},
|
|
},
|
|
};
|