Files
test-openclaw/extensions/whatsapp/src/setup-surface.ts
T
Marcus Castro aa76cf43f0 fix(whatsapp): stabilize auth state and reconcile local runtime after CLI login (#67815)
* WhatsApp: harden auth persistence and backup recovery

* WhatsApp: model unstable auth state across runtime and setup

* WhatsApp: recover login and monitor startup from unstable auth

* Channels: surface auth stabilizing in status and health

* Gateway protocol: add channels.start surface

* Gateway: reconcile local channel runtime after CLI login

* Channels UI: reflect recovered login start state

* Changelog: note WhatsApp auth stabilization

* Gateway: fix lint in call test
2026-04-19 14:20:46 -03:00

70 lines
2.6 KiB
TypeScript

import type { ChannelSetupWizard } from "openclaw/plugin-sdk/setup";
import {
DEFAULT_ACCOUNT_ID,
setSetupChannelEnabled,
type OpenClawConfig,
} from "openclaw/plugin-sdk/setup";
import { listWhatsAppAccountIds, resolveWhatsAppAuthDir } from "./accounts.js";
import { formatWhatsAppWebAuthStatusState, readWebAuthState } from "./auth-store.js";
import { finalizeWhatsAppSetup } from "./setup-finalize.js";
const channel = "whatsapp" as const;
type WhatsAppSetupLinkState = "linked" | "not-linked" | "unstable";
async function readWhatsAppSetupLinkState(
cfg: OpenClawConfig,
accountId: string,
): Promise<WhatsAppSetupLinkState> {
const { authDir } = resolveWhatsAppAuthDir({ cfg, accountId });
return await readWebAuthState(authDir);
}
export const whatsappSetupWizard: ChannelSetupWizard = {
channel,
status: {
configuredLabel: "linked",
unconfiguredLabel: "not linked",
configuredHint: "linked",
unconfiguredHint: "not linked",
configuredScore: 5,
unconfiguredScore: 4,
resolveConfigured: async ({ cfg, accountId }) => {
for (const resolvedAccountId of accountId ? [accountId] : listWhatsAppAccountIds(cfg)) {
if ((await readWhatsAppSetupLinkState(cfg, resolvedAccountId)) === "linked") {
return true;
}
}
return false;
},
resolveStatusLines: async ({ cfg, accountId, configured }) => {
const linkedAccountId = (
await Promise.all(
(accountId ? [accountId] : listWhatsAppAccountIds(cfg)).map(
async (resolvedAccountId) => ({
accountId: resolvedAccountId,
state: await readWhatsAppSetupLinkState(cfg, resolvedAccountId),
}),
),
)
).find((entry) => entry.state === "linked" || entry.state === "unstable");
const labelAccountId = accountId ?? linkedAccountId?.accountId;
const label = labelAccountId
? `WhatsApp (${labelAccountId === DEFAULT_ACCOUNT_ID ? "default" : labelAccountId})`
: "WhatsApp";
const stateLabel = configured
? formatWhatsAppWebAuthStatusState("linked")
: formatWhatsAppWebAuthStatusState(linkedAccountId?.state ?? "not-linked");
return [`${label}: ${stateLabel}`];
},
},
resolveShouldPromptAccountIds: ({ shouldPromptAccountIds }) => shouldPromptAccountIds,
credentials: [],
finalize: async ({ cfg, accountId, forceAllowFrom, prompter, runtime }) =>
await finalizeWhatsAppSetup({ cfg, accountId, forceAllowFrom, prompter, runtime }),
disable: (cfg) => setSetupChannelEnabled(cfg, channel, false),
onAccountRecorded: (accountId, options) => {
options?.onAccountId?.(channel, accountId);
},
};