feat(channel): support weichat channel (#620)
This commit is contained in:
@@ -5,6 +5,7 @@ import { deleteAgentChannelAccounts, listConfiguredChannels, readOpenClawConfig,
|
||||
import { withConfigLock } from './config-mutex';
|
||||
import { expandPath, getOpenClawConfigDir } from './paths';
|
||||
import * as logger from './logger';
|
||||
import { toUiChannelType } from './channel-alias';
|
||||
|
||||
const MAIN_AGENT_ID = 'main';
|
||||
const MAIN_AGENT_NAME = 'Main Agent';
|
||||
@@ -493,14 +494,16 @@ async function buildSnapshotFromConfig(config: AgentConfigDocument): Promise<Age
|
||||
workspace: entry.workspace || (entry.id === MAIN_AGENT_ID ? getDefaultWorkspacePath(config) : `~/.openclaw/workspace-${entry.id}`),
|
||||
agentDir: entry.agentDir || getDefaultAgentDirPath(entry.id),
|
||||
mainSessionKey: buildAgentMainSessionKey(config, entry.id),
|
||||
channelTypes: configuredChannels.filter((ct) => ownedChannels.has(ct)),
|
||||
channelTypes: configuredChannels
|
||||
.filter((ct) => ownedChannels.has(ct))
|
||||
.map((channelType) => toUiChannelType(channelType)),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
agents,
|
||||
defaultAgentId,
|
||||
configuredChannelTypes: configuredChannels,
|
||||
configuredChannelTypes: configuredChannels.map((channelType) => toUiChannelType(channelType)),
|
||||
channelOwners,
|
||||
channelAccountOwners,
|
||||
};
|
||||
|
||||
46
electron/utils/channel-alias.ts
Normal file
46
electron/utils/channel-alias.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
const BLOCKED_OBJECT_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
|
||||
const VALID_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
|
||||
const INVALID_CHARS_RE = /[^a-z0-9_-]+/g;
|
||||
const LEADING_DASH_RE = /^-+/;
|
||||
const TRAILING_DASH_RE = /-+$/;
|
||||
|
||||
export const UI_WECHAT_CHANNEL_TYPE = 'wechat';
|
||||
export const OPENCLAW_WECHAT_CHANNEL_TYPE = 'openclaw-weixin';
|
||||
|
||||
export type QrChannelEvent = 'qr' | 'success' | 'error';
|
||||
|
||||
export function toOpenClawChannelType(channelType: string): string {
|
||||
return channelType === UI_WECHAT_CHANNEL_TYPE ? OPENCLAW_WECHAT_CHANNEL_TYPE : channelType;
|
||||
}
|
||||
|
||||
export function toUiChannelType(channelType: string): string {
|
||||
return channelType === OPENCLAW_WECHAT_CHANNEL_TYPE ? UI_WECHAT_CHANNEL_TYPE : channelType;
|
||||
}
|
||||
|
||||
export function isWechatChannelType(channelType: string | null | undefined): boolean {
|
||||
return channelType === UI_WECHAT_CHANNEL_TYPE || channelType === OPENCLAW_WECHAT_CHANNEL_TYPE;
|
||||
}
|
||||
|
||||
export function buildQrChannelEventName(channelType: string, event: QrChannelEvent): string {
|
||||
return `channel:${toUiChannelType(channelType)}-${event}`;
|
||||
}
|
||||
|
||||
function canonicalizeAccountId(value: string): string {
|
||||
if (VALID_ID_RE.test(value)) return value.toLowerCase();
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(INVALID_CHARS_RE, '-')
|
||||
.replace(LEADING_DASH_RE, '')
|
||||
.replace(TRAILING_DASH_RE, '')
|
||||
.slice(0, 64);
|
||||
}
|
||||
|
||||
export function normalizeOpenClawAccountId(value: string | null | undefined, fallback = 'default'): string {
|
||||
const trimmed = (value ?? '').trim();
|
||||
if (!trimmed) return fallback;
|
||||
const normalized = canonicalizeAccountId(trimmed);
|
||||
if (!normalized || BLOCKED_OBJECT_KEYS.has(normalized)) {
|
||||
return fallback;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
@@ -12,13 +12,25 @@ import { getOpenClawResolvedDir } from './paths';
|
||||
import * as logger from './logger';
|
||||
import { proxyAwareFetch } from './proxy-fetch';
|
||||
import { withConfigLock } from './config-mutex';
|
||||
import {
|
||||
OPENCLAW_WECHAT_CHANNEL_TYPE,
|
||||
isWechatChannelType,
|
||||
normalizeOpenClawAccountId,
|
||||
toOpenClawChannelType,
|
||||
} from './channel-alias';
|
||||
|
||||
const OPENCLAW_DIR = join(homedir(), '.openclaw');
|
||||
const CONFIG_FILE = join(OPENCLAW_DIR, 'openclaw.json');
|
||||
const WECOM_PLUGIN_ID = 'wecom';
|
||||
const WECHAT_PLUGIN_ID = OPENCLAW_WECHAT_CHANNEL_TYPE;
|
||||
const FEISHU_PLUGIN_ID_CANDIDATES = ['openclaw-lark', 'feishu-openclaw-plugin'] as const;
|
||||
const DEFAULT_ACCOUNT_ID = 'default';
|
||||
const CHANNEL_TOP_LEVEL_KEYS_TO_KEEP = new Set(['accounts', 'defaultAccount', 'enabled']);
|
||||
const WECHAT_STATE_DIR = join(OPENCLAW_DIR, WECHAT_PLUGIN_ID);
|
||||
const WECHAT_ACCOUNT_INDEX_FILE = join(WECHAT_STATE_DIR, 'accounts.json');
|
||||
const WECHAT_ACCOUNTS_DIR = join(WECHAT_STATE_DIR, 'accounts');
|
||||
const LEGACY_WECHAT_CREDENTIALS_DIR = join(OPENCLAW_DIR, 'credentials', WECHAT_PLUGIN_ID);
|
||||
const LEGACY_WECHAT_SYNC_DIR = join(OPENCLAW_DIR, 'agents', 'default', 'sessions', '.openclaw-weixin-sync');
|
||||
|
||||
// Channels that are managed as plugins (config goes under plugins.entries, not channels)
|
||||
const PLUGIN_CHANNELS = ['whatsapp'];
|
||||
@@ -70,6 +82,117 @@ async function resolveFeishuPluginId(): Promise<string> {
|
||||
return FEISHU_PLUGIN_ID_CANDIDATES[0];
|
||||
}
|
||||
|
||||
function resolveStoredChannelType(channelType: string): string {
|
||||
return toOpenClawChannelType(channelType);
|
||||
}
|
||||
|
||||
function deriveLegacyWeChatRawAccountId(normalizedId: string): string | undefined {
|
||||
if (normalizedId.endsWith('-im-bot')) {
|
||||
return `${normalizedId.slice(0, -7)}@im.bot`;
|
||||
}
|
||||
if (normalizedId.endsWith('-im-wechat')) {
|
||||
return `${normalizedId.slice(0, -10)}@im.wechat`;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function readWeChatAccountIndex(): Promise<string[]> {
|
||||
try {
|
||||
const raw = await readFile(WECHAT_ACCOUNT_INDEX_FILE, 'utf-8');
|
||||
const parsed = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter((entry): entry is string => typeof entry === 'string' && entry.trim().length > 0);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function writeWeChatAccountIndex(accountIds: string[]): Promise<void> {
|
||||
await mkdir(WECHAT_STATE_DIR, { recursive: true });
|
||||
await writeFile(WECHAT_ACCOUNT_INDEX_FILE, JSON.stringify(accountIds, null, 2), 'utf-8');
|
||||
}
|
||||
|
||||
async function deleteWeChatAccountState(accountId: string): Promise<void> {
|
||||
const normalizedAccountId = normalizeOpenClawAccountId(accountId);
|
||||
const legacyRawAccountId = deriveLegacyWeChatRawAccountId(normalizedAccountId);
|
||||
const candidateIds = new Set<string>([normalizedAccountId]);
|
||||
if (legacyRawAccountId) {
|
||||
candidateIds.add(legacyRawAccountId);
|
||||
}
|
||||
if (accountId.trim()) {
|
||||
candidateIds.add(accountId.trim());
|
||||
}
|
||||
|
||||
for (const candidateId of candidateIds) {
|
||||
await rm(join(WECHAT_ACCOUNTS_DIR, `${candidateId}.json`), { force: true });
|
||||
}
|
||||
|
||||
const existingAccountIds = await readWeChatAccountIndex();
|
||||
const nextAccountIds = existingAccountIds.filter((entry) => !candidateIds.has(entry));
|
||||
if (nextAccountIds.length !== existingAccountIds.length) {
|
||||
if (nextAccountIds.length === 0) {
|
||||
await rm(WECHAT_ACCOUNT_INDEX_FILE, { force: true });
|
||||
} else {
|
||||
await writeWeChatAccountIndex(nextAccountIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteWeChatState(): Promise<void> {
|
||||
await rm(WECHAT_STATE_DIR, { recursive: true, force: true });
|
||||
await rm(LEGACY_WECHAT_CREDENTIALS_DIR, { recursive: true, force: true });
|
||||
await rm(LEGACY_WECHAT_SYNC_DIR, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
function removePluginRegistration(currentConfig: OpenClawConfig, pluginId: string): boolean {
|
||||
if (!currentConfig.plugins) return false;
|
||||
let modified = false;
|
||||
|
||||
if (Array.isArray(currentConfig.plugins.allow)) {
|
||||
const nextAllow = currentConfig.plugins.allow.filter((entry) => entry !== pluginId);
|
||||
if (nextAllow.length !== currentConfig.plugins.allow.length) {
|
||||
currentConfig.plugins.allow = nextAllow;
|
||||
modified = true;
|
||||
}
|
||||
if (nextAllow.length === 0) {
|
||||
delete currentConfig.plugins.allow;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentConfig.plugins.entries && currentConfig.plugins.entries[pluginId]) {
|
||||
delete currentConfig.plugins.entries[pluginId];
|
||||
modified = true;
|
||||
if (Object.keys(currentConfig.plugins.entries).length === 0) {
|
||||
delete currentConfig.plugins.entries;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
currentConfig.plugins.enabled !== undefined
|
||||
&& !currentConfig.plugins.allow?.length
|
||||
&& !currentConfig.plugins.entries
|
||||
) {
|
||||
delete currentConfig.plugins.enabled;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (Object.keys(currentConfig.plugins).length === 0) {
|
||||
delete currentConfig.plugins;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
function channelHasConfiguredAccounts(channelSection: ChannelConfigData | undefined): boolean {
|
||||
if (!channelSection || typeof channelSection !== 'object') return false;
|
||||
const accounts = channelSection.accounts as Record<string, ChannelConfigData> | undefined;
|
||||
if (accounts && typeof accounts === 'object') {
|
||||
return Object.keys(accounts).length > 0;
|
||||
}
|
||||
return Object.keys(channelSection).some((key) => !CHANNEL_TOP_LEVEL_KEYS_TO_KEEP.has(key));
|
||||
}
|
||||
|
||||
// ── Types ────────────────────────────────────────────────────────
|
||||
|
||||
export interface ChannelConfigData {
|
||||
@@ -239,6 +362,35 @@ async function ensurePluginAllowlist(currentConfig: OpenClawConfig, channelType:
|
||||
currentConfig.plugins.allow = [...allow, 'qqbot'];
|
||||
}
|
||||
}
|
||||
|
||||
if (channelType === WECHAT_PLUGIN_ID) {
|
||||
if (!currentConfig.plugins) {
|
||||
currentConfig.plugins = {
|
||||
allow: [WECHAT_PLUGIN_ID],
|
||||
enabled: true,
|
||||
entries: {
|
||||
[WECHAT_PLUGIN_ID]: { enabled: true },
|
||||
},
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
currentConfig.plugins.enabled = true;
|
||||
const allow = Array.isArray(currentConfig.plugins.allow)
|
||||
? currentConfig.plugins.allow as string[]
|
||||
: [];
|
||||
if (!allow.includes(WECHAT_PLUGIN_ID)) {
|
||||
currentConfig.plugins.allow = [...allow, WECHAT_PLUGIN_ID];
|
||||
}
|
||||
|
||||
if (!currentConfig.plugins.entries) {
|
||||
currentConfig.plugins.entries = {};
|
||||
}
|
||||
if (!currentConfig.plugins.entries[WECHAT_PLUGIN_ID]) {
|
||||
currentConfig.plugins.entries[WECHAT_PLUGIN_ID] = {};
|
||||
}
|
||||
currentConfig.plugins.entries[WECHAT_PLUGIN_ID].enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
function transformChannelConfig(
|
||||
@@ -426,55 +578,56 @@ export async function saveChannelConfig(
|
||||
accountId?: string,
|
||||
): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
const resolvedAccountId = accountId || DEFAULT_ACCOUNT_ID;
|
||||
|
||||
await ensurePluginAllowlist(currentConfig, channelType);
|
||||
await ensurePluginAllowlist(currentConfig, resolvedChannelType);
|
||||
|
||||
// Plugin-based channels (e.g. WhatsApp) go under plugins.entries, not channels
|
||||
if (PLUGIN_CHANNELS.includes(channelType)) {
|
||||
if (PLUGIN_CHANNELS.includes(resolvedChannelType)) {
|
||||
if (!currentConfig.plugins) {
|
||||
currentConfig.plugins = {};
|
||||
}
|
||||
if (!currentConfig.plugins.entries) {
|
||||
currentConfig.plugins.entries = {};
|
||||
}
|
||||
currentConfig.plugins.entries[channelType] = {
|
||||
...currentConfig.plugins.entries[channelType],
|
||||
currentConfig.plugins.entries[resolvedChannelType] = {
|
||||
...currentConfig.plugins.entries[resolvedChannelType],
|
||||
enabled: config.enabled ?? true,
|
||||
};
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
logger.info('Plugin channel config saved', {
|
||||
channelType,
|
||||
channelType: resolvedChannelType,
|
||||
configFile: CONFIG_FILE,
|
||||
path: `plugins.entries.${channelType}`,
|
||||
path: `plugins.entries.${resolvedChannelType}`,
|
||||
});
|
||||
console.log(`Saved plugin channel config for ${channelType}`);
|
||||
console.log(`Saved plugin channel config for ${resolvedChannelType}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentConfig.channels) {
|
||||
currentConfig.channels = {};
|
||||
}
|
||||
if (!currentConfig.channels[channelType]) {
|
||||
currentConfig.channels[channelType] = {};
|
||||
if (!currentConfig.channels[resolvedChannelType]) {
|
||||
currentConfig.channels[resolvedChannelType] = {};
|
||||
}
|
||||
|
||||
const channelSection = currentConfig.channels[channelType];
|
||||
const channelSection = currentConfig.channels[resolvedChannelType];
|
||||
migrateLegacyChannelConfigToAccounts(channelSection, DEFAULT_ACCOUNT_ID);
|
||||
|
||||
// Guard: reject if this bot/app credential is already used by another account.
|
||||
assertNoDuplicateCredential(channelType, config, channelSection, resolvedAccountId);
|
||||
assertNoDuplicateCredential(resolvedChannelType, config, channelSection, resolvedAccountId);
|
||||
|
||||
const existingAccountConfig = resolveAccountConfig(channelSection, resolvedAccountId);
|
||||
const transformedConfig = transformChannelConfig(channelType, config, existingAccountConfig);
|
||||
const uniqueKey = CHANNEL_UNIQUE_CREDENTIAL_KEY[channelType];
|
||||
const transformedConfig = transformChannelConfig(resolvedChannelType, config, existingAccountConfig);
|
||||
const uniqueKey = CHANNEL_UNIQUE_CREDENTIAL_KEY[resolvedChannelType];
|
||||
if (uniqueKey && typeof transformedConfig[uniqueKey] === 'string') {
|
||||
const rawCredentialValue = transformedConfig[uniqueKey] as string;
|
||||
const normalizedCredentialValue = normalizeCredentialValue(rawCredentialValue);
|
||||
if (normalizedCredentialValue !== rawCredentialValue) {
|
||||
logger.warn('Normalizing channel credential value before save', {
|
||||
channelType,
|
||||
channelType: resolvedChannelType,
|
||||
accountId: resolvedAccountId,
|
||||
key: uniqueKey,
|
||||
});
|
||||
@@ -490,7 +643,7 @@ export async function saveChannelConfig(
|
||||
channelSection.defaultAccount =
|
||||
typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim()
|
||||
? channelSection.defaultAccount
|
||||
: DEFAULT_ACCOUNT_ID;
|
||||
: resolvedAccountId;
|
||||
accounts[resolvedAccountId] = {
|
||||
...accounts[resolvedAccountId],
|
||||
...transformedConfig,
|
||||
@@ -506,7 +659,11 @@ export async function saveChannelConfig(
|
||||
// credential keys on every invocation. Without this, saving a non-default
|
||||
// account (e.g. a sub-agent's Feishu bot) leaves the top-level credentials
|
||||
// missing, breaking plugins that only read from the top level.
|
||||
const defaultAccountData = accounts[DEFAULT_ACCOUNT_ID];
|
||||
const mirroredAccountId =
|
||||
typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim()
|
||||
? channelSection.defaultAccount
|
||||
: resolvedAccountId;
|
||||
const defaultAccountData = accounts[mirroredAccountId] ?? accounts[resolvedAccountId] ?? accounts[DEFAULT_ACCOUNT_ID];
|
||||
if (defaultAccountData) {
|
||||
for (const [key, value] of Object.entries(defaultAccountData)) {
|
||||
channelSection[key] = value;
|
||||
@@ -515,19 +672,20 @@ export async function saveChannelConfig(
|
||||
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
logger.info('Channel config saved', {
|
||||
channelType,
|
||||
channelType: resolvedChannelType,
|
||||
accountId: resolvedAccountId,
|
||||
configFile: CONFIG_FILE,
|
||||
rawKeys: Object.keys(config),
|
||||
transformedKeys: Object.keys(transformedConfig),
|
||||
});
|
||||
console.log(`Saved channel config for ${channelType} account ${resolvedAccountId}`);
|
||||
console.log(`Saved channel config for ${resolvedChannelType} account ${resolvedAccountId}`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getChannelConfig(channelType: string, accountId?: string): Promise<ChannelConfigData | undefined> {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const config = await readOpenClawConfig();
|
||||
const channelSection = config.channels?.[channelType];
|
||||
const channelSection = config.channels?.[resolvedChannelType];
|
||||
if (!channelSection) return undefined;
|
||||
|
||||
const resolvedAccountId = accountId || DEFAULT_ACCOUNT_ID;
|
||||
@@ -596,9 +754,17 @@ export async function getChannelFormValues(channelType: string, accountId?: stri
|
||||
|
||||
export async function deleteChannelAccountConfig(channelType: string, accountId: string): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
const channelSection = currentConfig.channels?.[channelType];
|
||||
if (!channelSection) return;
|
||||
const channelSection = currentConfig.channels?.[resolvedChannelType];
|
||||
if (!channelSection) {
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
await deleteWeChatAccountState(accountId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
migrateLegacyChannelConfigToAccounts(channelSection, DEFAULT_ACCOUNT_ID);
|
||||
const accounts = channelSection.accounts as Record<string, ChannelConfigData> | undefined;
|
||||
@@ -607,7 +773,10 @@ export async function deleteChannelAccountConfig(channelType: string, accountId:
|
||||
delete accounts[accountId];
|
||||
|
||||
if (Object.keys(accounts).length === 0) {
|
||||
delete currentConfig.channels![channelType];
|
||||
delete currentConfig.channels![resolvedChannelType];
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
}
|
||||
} else {
|
||||
if (channelSection.defaultAccount === accountId) {
|
||||
const nextDefaultAccountId = Object.keys(accounts).sort((a, b) => {
|
||||
@@ -634,22 +803,32 @@ export async function deleteChannelAccountConfig(channelType: string, accountId:
|
||||
}
|
||||
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
logger.info('Deleted channel account config', { channelType, accountId });
|
||||
console.log(`Deleted channel account config for ${channelType}/${accountId}`);
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
await deleteWeChatAccountState(accountId);
|
||||
}
|
||||
logger.info('Deleted channel account config', { channelType: resolvedChannelType, accountId });
|
||||
console.log(`Deleted channel account config for ${resolvedChannelType}/${accountId}`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteChannelConfig(channelType: string): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
|
||||
if (currentConfig.channels?.[channelType]) {
|
||||
delete currentConfig.channels[channelType];
|
||||
if (currentConfig.channels?.[resolvedChannelType]) {
|
||||
delete currentConfig.channels[resolvedChannelType];
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
}
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
console.log(`Deleted channel config for ${channelType}`);
|
||||
} else if (PLUGIN_CHANNELS.includes(channelType)) {
|
||||
if (currentConfig.plugins?.entries?.[channelType]) {
|
||||
delete currentConfig.plugins.entries[channelType];
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
await deleteWeChatState();
|
||||
}
|
||||
console.log(`Deleted channel config for ${resolvedChannelType}`);
|
||||
} else if (PLUGIN_CHANNELS.includes(resolvedChannelType)) {
|
||||
if (currentConfig.plugins?.entries?.[resolvedChannelType]) {
|
||||
delete currentConfig.plugins.entries[resolvedChannelType];
|
||||
if (Object.keys(currentConfig.plugins.entries).length === 0) {
|
||||
delete currentConfig.plugins.entries;
|
||||
}
|
||||
@@ -657,11 +836,15 @@ export async function deleteChannelConfig(channelType: string): Promise<void> {
|
||||
delete currentConfig.plugins;
|
||||
}
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
console.log(`Deleted plugin channel config for ${channelType}`);
|
||||
console.log(`Deleted plugin channel config for ${resolvedChannelType}`);
|
||||
}
|
||||
} else if (isWechatChannelType(resolvedChannelType)) {
|
||||
removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
await deleteWeChatState();
|
||||
}
|
||||
|
||||
if (channelType === 'whatsapp') {
|
||||
if (resolvedChannelType === 'whatsapp') {
|
||||
try {
|
||||
const whatsappDir = join(homedir(), '.openclaw', 'credentials', 'whatsapp');
|
||||
if (await fileExists(whatsappDir)) {
|
||||
@@ -742,9 +925,16 @@ export async function listConfiguredChannelAccounts(): Promise<Record<string, Co
|
||||
? Object.keys(section.accounts).filter(Boolean)
|
||||
: [];
|
||||
|
||||
const defaultAccountId = typeof section.defaultAccount === 'string' && section.defaultAccount.trim()
|
||||
let defaultAccountId = typeof section.defaultAccount === 'string' && section.defaultAccount.trim()
|
||||
? section.defaultAccount
|
||||
: DEFAULT_ACCOUNT_ID;
|
||||
if (accountIds.length > 0 && !accountIds.includes(defaultAccountId)) {
|
||||
defaultAccountId = accountIds.sort((a, b) => {
|
||||
if (a === DEFAULT_ACCOUNT_ID) return -1;
|
||||
if (b === DEFAULT_ACCOUNT_ID) return 1;
|
||||
return a.localeCompare(b);
|
||||
})[0];
|
||||
}
|
||||
|
||||
if (accountIds.length === 0) {
|
||||
const hasAnyPayload = Object.keys(section).some((key) => !CHANNEL_TOP_LEVEL_KEYS_TO_KEEP.has(key));
|
||||
@@ -771,21 +961,22 @@ export async function listConfiguredChannelAccounts(): Promise<Record<string, Co
|
||||
|
||||
export async function setChannelDefaultAccount(channelType: string, accountId: string): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const trimmedAccountId = accountId.trim();
|
||||
if (!trimmedAccountId) {
|
||||
throw new Error('accountId is required');
|
||||
}
|
||||
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
const channelSection = currentConfig.channels?.[channelType];
|
||||
const channelSection = currentConfig.channels?.[resolvedChannelType];
|
||||
if (!channelSection) {
|
||||
throw new Error(`Channel "${channelType}" is not configured`);
|
||||
throw new Error(`Channel "${resolvedChannelType}" is not configured`);
|
||||
}
|
||||
|
||||
migrateLegacyChannelConfigToAccounts(channelSection, DEFAULT_ACCOUNT_ID);
|
||||
const accounts = channelSection.accounts as Record<string, ChannelConfigData> | undefined;
|
||||
if (!accounts || !accounts[trimmedAccountId]) {
|
||||
throw new Error(`Account "${trimmedAccountId}" is not configured for channel "${channelType}"`);
|
||||
throw new Error(`Account "${trimmedAccountId}" is not configured for channel "${resolvedChannelType}"`);
|
||||
}
|
||||
|
||||
channelSection.defaultAccount = trimmedAccountId;
|
||||
@@ -796,7 +987,7 @@ export async function setChannelDefaultAccount(channelType: string, accountId: s
|
||||
}
|
||||
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
logger.info('Set channel default account', { channelType, accountId: trimmedAccountId });
|
||||
logger.info('Set channel default account', { channelType: resolvedChannelType, accountId: trimmedAccountId });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -856,23 +1047,55 @@ export async function deleteAgentChannelAccounts(agentId: string, ownedChannelAc
|
||||
|
||||
export async function setChannelEnabled(channelType: string, enabled: boolean): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
|
||||
if (PLUGIN_CHANNELS.includes(channelType)) {
|
||||
if (isWechatChannelType(resolvedChannelType)) {
|
||||
if (enabled) {
|
||||
await ensurePluginAllowlist(currentConfig, WECHAT_PLUGIN_ID);
|
||||
} else {
|
||||
removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
}
|
||||
}
|
||||
|
||||
if (PLUGIN_CHANNELS.includes(resolvedChannelType)) {
|
||||
if (!currentConfig.plugins) currentConfig.plugins = {};
|
||||
if (!currentConfig.plugins.entries) currentConfig.plugins.entries = {};
|
||||
if (!currentConfig.plugins.entries[channelType]) currentConfig.plugins.entries[channelType] = {};
|
||||
currentConfig.plugins.entries[channelType].enabled = enabled;
|
||||
if (!currentConfig.plugins.entries[resolvedChannelType]) currentConfig.plugins.entries[resolvedChannelType] = {};
|
||||
currentConfig.plugins.entries[resolvedChannelType].enabled = enabled;
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
console.log(`Set plugin channel ${channelType} enabled: ${enabled}`);
|
||||
console.log(`Set plugin channel ${resolvedChannelType} enabled: ${enabled}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentConfig.channels) currentConfig.channels = {};
|
||||
if (!currentConfig.channels[channelType]) currentConfig.channels[channelType] = {};
|
||||
currentConfig.channels[channelType].enabled = enabled;
|
||||
if (!currentConfig.channels[resolvedChannelType]) currentConfig.channels[resolvedChannelType] = {};
|
||||
currentConfig.channels[resolvedChannelType].enabled = enabled;
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
console.log(`Set channel ${channelType} enabled: ${enabled}`);
|
||||
console.log(`Set channel ${resolvedChannelType} enabled: ${enabled}`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function cleanupDanglingWeChatPluginState(): Promise<{ cleanedDanglingState: boolean }> {
|
||||
return withConfigLock(async () => {
|
||||
const currentConfig = await readOpenClawConfig();
|
||||
const channelSection = currentConfig.channels?.[WECHAT_PLUGIN_ID];
|
||||
const hasConfiguredWeChatAccounts = channelHasConfiguredAccounts(channelSection);
|
||||
const hadPluginRegistration = Boolean(
|
||||
currentConfig.plugins?.entries?.[WECHAT_PLUGIN_ID]
|
||||
|| currentConfig.plugins?.allow?.includes(WECHAT_PLUGIN_ID),
|
||||
);
|
||||
|
||||
if (hasConfiguredWeChatAccounts) {
|
||||
return { cleanedDanglingState: false };
|
||||
}
|
||||
|
||||
const modified = removePluginRegistration(currentConfig, WECHAT_PLUGIN_ID);
|
||||
if (modified) {
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
}
|
||||
await deleteWeChatState();
|
||||
return { cleanedDanglingState: hadPluginRegistration || modified };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -954,7 +1177,7 @@ export async function validateChannelCredentials(
|
||||
channelType: string,
|
||||
config: Record<string, string>
|
||||
): Promise<CredentialValidationResult> {
|
||||
switch (channelType) {
|
||||
switch (resolveStoredChannelType(channelType)) {
|
||||
case 'discord':
|
||||
return validateDiscordCredentials(config);
|
||||
case 'telegram':
|
||||
@@ -1072,6 +1295,7 @@ async function validateTelegramCredentials(
|
||||
|
||||
export async function validateChannelConfig(channelType: string): Promise<ValidationResult> {
|
||||
const { exec } = await import('child_process');
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
|
||||
const result: ValidationResult = { valid: true, errors: [], warnings: [] };
|
||||
|
||||
@@ -1104,7 +1328,7 @@ export async function validateChannelConfig(channelType: string): Promise<Valida
|
||||
|
||||
const output = await runDoctor(`node openclaw.mjs doctor 2>&1`);
|
||||
|
||||
const parsedDoctor = parseDoctorValidationOutput(channelType, output);
|
||||
const parsedDoctor = parseDoctorValidationOutput(resolvedChannelType, output);
|
||||
result.errors.push(...parsedDoctor.errors);
|
||||
result.warnings.push(...parsedDoctor.warnings);
|
||||
if (parsedDoctor.errors.length > 0) {
|
||||
@@ -1112,27 +1336,27 @@ export async function validateChannelConfig(channelType: string): Promise<Valida
|
||||
}
|
||||
if (parsedDoctor.undetermined) {
|
||||
logger.warn('Doctor output parsing fell back to local channel checks', {
|
||||
channelType,
|
||||
channelType: resolvedChannelType,
|
||||
hint: DOCTOR_PARSER_FALLBACK_HINT,
|
||||
});
|
||||
}
|
||||
|
||||
const config = await readOpenClawConfig();
|
||||
const savedChannelConfig = await getChannelConfig(channelType, DEFAULT_ACCOUNT_ID);
|
||||
if (!config.channels?.[channelType] || !savedChannelConfig) {
|
||||
result.errors.push(`Channel ${channelType} is not configured`);
|
||||
const savedChannelConfig = await getChannelConfig(resolvedChannelType, DEFAULT_ACCOUNT_ID);
|
||||
if (!config.channels?.[resolvedChannelType] || !savedChannelConfig) {
|
||||
result.errors.push(`Channel ${resolvedChannelType} is not configured`);
|
||||
result.valid = false;
|
||||
} else if (config.channels[channelType].enabled === false) {
|
||||
result.warnings.push(`Channel ${channelType} is disabled`);
|
||||
} else if (config.channels[resolvedChannelType].enabled === false) {
|
||||
result.warnings.push(`Channel ${resolvedChannelType} is disabled`);
|
||||
}
|
||||
|
||||
if (channelType === 'discord') {
|
||||
if (resolvedChannelType === 'discord') {
|
||||
const discordConfig = savedChannelConfig;
|
||||
if (!discordConfig?.token) {
|
||||
result.errors.push('Discord: Bot token is required');
|
||||
result.valid = false;
|
||||
}
|
||||
} else if (channelType === 'telegram') {
|
||||
} else if (resolvedChannelType === 'telegram') {
|
||||
const telegramConfig = savedChannelConfig;
|
||||
if (!telegramConfig?.botToken) {
|
||||
result.errors.push('Telegram: Bot token is required');
|
||||
@@ -1161,10 +1385,10 @@ export async function validateChannelConfig(channelType: string): Promise<Valida
|
||||
} else {
|
||||
console.warn('Doctor command failed:', errorMessage);
|
||||
const config = await readOpenClawConfig();
|
||||
if (config.channels?.[channelType]) {
|
||||
if (config.channels?.[resolvedChannelType]) {
|
||||
result.valid = true;
|
||||
} else {
|
||||
result.errors.push(`Channel ${channelType} is not configured`);
|
||||
result.errors.push(`Channel ${resolvedChannelType} is not configured`);
|
||||
result.valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
101
electron/utils/channel-status.ts
Normal file
101
electron/utils/channel-status.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
export type ChannelConnectionStatus = 'connected' | 'connecting' | 'disconnected' | 'error';
|
||||
|
||||
export interface ChannelRuntimeAccountSnapshot {
|
||||
connected?: boolean;
|
||||
linked?: boolean;
|
||||
running?: boolean;
|
||||
lastError?: string | null;
|
||||
lastConnectedAt?: number | null;
|
||||
lastInboundAt?: number | null;
|
||||
lastOutboundAt?: number | null;
|
||||
lastProbeAt?: number | null;
|
||||
probe?: {
|
||||
ok?: boolean | null;
|
||||
} | null;
|
||||
}
|
||||
|
||||
export interface ChannelRuntimeSummarySnapshot {
|
||||
error?: string | null;
|
||||
lastError?: string | null;
|
||||
}
|
||||
|
||||
const RECENT_ACTIVITY_MS = 10 * 60 * 1000;
|
||||
|
||||
function hasNonEmptyError(value: string | null | undefined): boolean {
|
||||
return typeof value === 'string' && value.trim().length > 0;
|
||||
}
|
||||
|
||||
export function hasRecentChannelActivity(
|
||||
account: Pick<ChannelRuntimeAccountSnapshot, 'lastConnectedAt' | 'lastInboundAt' | 'lastOutboundAt'>,
|
||||
now = Date.now(),
|
||||
recentMs = RECENT_ACTIVITY_MS,
|
||||
): boolean {
|
||||
return (
|
||||
(typeof account.lastInboundAt === 'number' && now - account.lastInboundAt < recentMs) ||
|
||||
(typeof account.lastOutboundAt === 'number' && now - account.lastOutboundAt < recentMs) ||
|
||||
(typeof account.lastConnectedAt === 'number' && now - account.lastConnectedAt < recentMs)
|
||||
);
|
||||
}
|
||||
|
||||
export function hasSuccessfulChannelProbe(
|
||||
account: Pick<ChannelRuntimeAccountSnapshot, 'probe'>,
|
||||
): boolean {
|
||||
return account.probe?.ok === true;
|
||||
}
|
||||
|
||||
export function hasChannelRuntimeError(
|
||||
account: Pick<ChannelRuntimeAccountSnapshot, 'lastError'>,
|
||||
): boolean {
|
||||
return hasNonEmptyError(account.lastError);
|
||||
}
|
||||
|
||||
export function hasSummaryRuntimeError(
|
||||
summary: ChannelRuntimeSummarySnapshot | undefined,
|
||||
): boolean {
|
||||
if (!summary) return false;
|
||||
return hasNonEmptyError(summary.error) || hasNonEmptyError(summary.lastError);
|
||||
}
|
||||
|
||||
export function isChannelRuntimeConnected(
|
||||
account: ChannelRuntimeAccountSnapshot,
|
||||
): boolean {
|
||||
if (account.connected === true || account.linked === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hasRecentChannelActivity(account) || hasSuccessfulChannelProbe(account)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// OpenClaw integrations such as Feishu/WeCom may stay "running" without ever
|
||||
// setting a durable connected=true flag. Treat healthy running as connected.
|
||||
return account.running === true && !hasChannelRuntimeError(account);
|
||||
}
|
||||
|
||||
export function computeChannelRuntimeStatus(
|
||||
account: ChannelRuntimeAccountSnapshot,
|
||||
): ChannelConnectionStatus {
|
||||
if (isChannelRuntimeConnected(account)) return 'connected';
|
||||
if (hasChannelRuntimeError(account)) return 'error';
|
||||
if (account.running === true) return 'connecting';
|
||||
return 'disconnected';
|
||||
}
|
||||
|
||||
export function pickChannelRuntimeStatus(
|
||||
accounts: ChannelRuntimeAccountSnapshot[],
|
||||
summary?: ChannelRuntimeSummarySnapshot,
|
||||
): ChannelConnectionStatus {
|
||||
if (accounts.some((account) => isChannelRuntimeConnected(account))) {
|
||||
return 'connected';
|
||||
}
|
||||
|
||||
if (accounts.some((account) => hasChannelRuntimeError(account)) || hasSummaryRuntimeError(summary)) {
|
||||
return 'error';
|
||||
}
|
||||
|
||||
if (accounts.some((account) => account.running === true)) {
|
||||
return 'connecting';
|
||||
}
|
||||
|
||||
return 'disconnected';
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
* Shared OpenClaw Plugin Install Utilities
|
||||
*
|
||||
* Provides version-aware install/upgrade logic for bundled OpenClaw plugins
|
||||
* (DingTalk, WeCom, QQBot, Feishu). Used both at app startup (to auto-upgrade
|
||||
* (DingTalk, WeCom, QQBot, Feishu, WeChat). Used both at app startup (to auto-upgrade
|
||||
* stale plugins) and when a user configures a channel.
|
||||
*/
|
||||
import { app } from 'electron';
|
||||
@@ -171,6 +171,7 @@ const PLUGIN_NPM_NAMES: Record<string, string> = {
|
||||
wecom: '@wecom/wecom-openclaw-plugin',
|
||||
'feishu-openclaw-plugin': '@larksuite/openclaw-lark',
|
||||
qqbot: '@sliverp/qqbot',
|
||||
'openclaw-weixin': '@tencent-weixin/openclaw-weixin',
|
||||
};
|
||||
|
||||
// ── Version helper ───────────────────────────────────────────────────────────
|
||||
@@ -450,6 +451,10 @@ export function ensureQQBotPluginInstalled(): { installed: boolean; warning?: st
|
||||
return ensurePluginInstalled('qqbot', buildCandidateSources('qqbot'), 'QQ Bot');
|
||||
}
|
||||
|
||||
export function ensureWeChatPluginInstalled(): { installed: boolean; warning?: string } {
|
||||
return ensurePluginInstalled('openclaw-weixin', buildCandidateSources('openclaw-weixin'), 'WeChat');
|
||||
}
|
||||
|
||||
// ── Bulk startup installer ───────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -460,6 +465,7 @@ const ALL_BUNDLED_PLUGINS = [
|
||||
{ fn: ensureWeComPluginInstalled, label: 'WeCom' },
|
||||
{ fn: ensureQQBotPluginInstalled, label: 'QQ Bot' },
|
||||
{ fn: ensureFeishuPluginInstalled, label: 'Feishu' },
|
||||
{ fn: ensureWeChatPluginInstalled, label: 'WeChat' },
|
||||
] as const;
|
||||
|
||||
/**
|
||||
|
||||
457
electron/utils/wechat-login.ts
Normal file
457
electron/utils/wechat-login.ts
Normal file
@@ -0,0 +1,457 @@
|
||||
import { createRequire } from 'node:module';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { deflateSync } from 'node:zlib';
|
||||
import { normalizeOpenClawAccountId } from './channel-alias';
|
||||
import { getOpenClawResolvedDir } from './paths';
|
||||
|
||||
export const DEFAULT_WECHAT_BASE_URL = 'https://ilinkai.weixin.qq.com';
|
||||
const DEFAULT_ILINK_BOT_TYPE = '3';
|
||||
const ACTIVE_LOGIN_TTL_MS = 5 * 60_000;
|
||||
const QR_POLL_TIMEOUT_MS = 35_000;
|
||||
const MAX_QR_REFRESH_COUNT = 3;
|
||||
const OPENCLAW_DIR = join(homedir(), '.openclaw');
|
||||
const WECHAT_STATE_DIR = join(OPENCLAW_DIR, 'openclaw-weixin');
|
||||
const WECHAT_ACCOUNT_INDEX_FILE = join(WECHAT_STATE_DIR, 'accounts.json');
|
||||
const WECHAT_ACCOUNTS_DIR = join(WECHAT_STATE_DIR, 'accounts');
|
||||
const require = createRequire(import.meta.url);
|
||||
type QrRenderDeps = {
|
||||
QRCode: typeof import('qrcode-terminal/vendor/QRCode/index.js');
|
||||
QRErrorCorrectLevel: typeof import('qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel.js');
|
||||
};
|
||||
|
||||
let qrRenderDeps: QrRenderDeps | null = null;
|
||||
|
||||
function getQrRenderDeps(): QrRenderDeps {
|
||||
if (qrRenderDeps) {
|
||||
return qrRenderDeps;
|
||||
}
|
||||
|
||||
const openclawRequire = createRequire(join(getOpenClawResolvedDir(), 'package.json'));
|
||||
const qrcodeTerminalPath = dirname(openclawRequire.resolve('qrcode-terminal/package.json'));
|
||||
qrRenderDeps = {
|
||||
QRCode: require(join(qrcodeTerminalPath, 'vendor', 'QRCode', 'index.js')),
|
||||
QRErrorCorrectLevel: require(join(qrcodeTerminalPath, 'vendor', 'QRCode', 'QRErrorCorrectLevel.js')),
|
||||
};
|
||||
return qrRenderDeps;
|
||||
}
|
||||
|
||||
type ActiveLogin = {
|
||||
sessionKey: string;
|
||||
qrcode: string;
|
||||
qrcodeUrl: string;
|
||||
startedAt: number;
|
||||
apiBaseUrl: string;
|
||||
};
|
||||
|
||||
type QrCodeResponse = {
|
||||
qrcode: string;
|
||||
qrcode_img_content: string;
|
||||
};
|
||||
|
||||
type QrStatusResponse = {
|
||||
status: 'wait' | 'scaned' | 'confirmed' | 'expired';
|
||||
bot_token?: string;
|
||||
ilink_bot_id?: string;
|
||||
baseurl?: string;
|
||||
ilink_user_id?: string;
|
||||
};
|
||||
|
||||
export type WeChatLoginStartResult = {
|
||||
sessionKey: string;
|
||||
qrcodeUrl?: string;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export type WeChatLoginWaitResult = {
|
||||
connected: boolean;
|
||||
message: string;
|
||||
botToken?: string;
|
||||
accountId?: string;
|
||||
baseUrl?: string;
|
||||
userId?: string;
|
||||
};
|
||||
|
||||
const activeLogins = new Map<string, ActiveLogin>();
|
||||
|
||||
function createQrMatrix(input: string) {
|
||||
const { QRCode, QRErrorCorrectLevel } = getQrRenderDeps();
|
||||
const qr = new QRCode(-1, QRErrorCorrectLevel.L);
|
||||
qr.addData(input);
|
||||
qr.make();
|
||||
return qr;
|
||||
}
|
||||
|
||||
function fillPixel(
|
||||
buf: Buffer,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
r: number,
|
||||
g: number,
|
||||
b: number,
|
||||
a = 255,
|
||||
) {
|
||||
const idx = (y * width + x) * 4;
|
||||
buf[idx] = r;
|
||||
buf[idx + 1] = g;
|
||||
buf[idx + 2] = b;
|
||||
buf[idx + 3] = a;
|
||||
}
|
||||
|
||||
function crcTable() {
|
||||
const table = new Uint32Array(256);
|
||||
for (let i = 0; i < 256; i += 1) {
|
||||
let c = i;
|
||||
for (let k = 0; k < 8; k += 1) {
|
||||
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
||||
}
|
||||
table[i] = c >>> 0;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
const CRC_TABLE = crcTable();
|
||||
|
||||
function crc32(buf: Buffer) {
|
||||
let crc = 0xffffffff;
|
||||
for (let i = 0; i < buf.length; i += 1) {
|
||||
crc = CRC_TABLE[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
|
||||
}
|
||||
return (crc ^ 0xffffffff) >>> 0;
|
||||
}
|
||||
|
||||
function pngChunk(type: string, data: Buffer) {
|
||||
const typeBuf = Buffer.from(type, 'ascii');
|
||||
const len = Buffer.alloc(4);
|
||||
len.writeUInt32BE(data.length, 0);
|
||||
const crc = crc32(Buffer.concat([typeBuf, data]));
|
||||
const crcBuf = Buffer.alloc(4);
|
||||
crcBuf.writeUInt32BE(crc, 0);
|
||||
return Buffer.concat([len, typeBuf, data, crcBuf]);
|
||||
}
|
||||
|
||||
function encodePngRgba(buffer: Buffer, width: number, height: number) {
|
||||
const stride = width * 4;
|
||||
const raw = Buffer.alloc((stride + 1) * height);
|
||||
for (let row = 0; row < height; row += 1) {
|
||||
const rawOffset = row * (stride + 1);
|
||||
raw[rawOffset] = 0;
|
||||
buffer.copy(raw, rawOffset + 1, row * stride, row * stride + stride);
|
||||
}
|
||||
const compressed = deflateSync(raw);
|
||||
const signature = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
const ihdr = Buffer.alloc(13);
|
||||
ihdr.writeUInt32BE(width, 0);
|
||||
ihdr.writeUInt32BE(height, 4);
|
||||
ihdr[8] = 8;
|
||||
ihdr[9] = 6;
|
||||
ihdr[10] = 0;
|
||||
ihdr[11] = 0;
|
||||
ihdr[12] = 0;
|
||||
|
||||
return Buffer.concat([
|
||||
signature,
|
||||
pngChunk('IHDR', ihdr),
|
||||
pngChunk('IDAT', compressed),
|
||||
pngChunk('IEND', Buffer.alloc(0)),
|
||||
]);
|
||||
}
|
||||
|
||||
async function renderQrPngDataUrl(
|
||||
input: string,
|
||||
opts: { scale?: number; marginModules?: number } = {},
|
||||
): Promise<string> {
|
||||
const { scale = 6, marginModules = 4 } = opts;
|
||||
const qr = createQrMatrix(input);
|
||||
const modules = qr.getModuleCount();
|
||||
const size = (modules + marginModules * 2) * scale;
|
||||
const buf = Buffer.alloc(size * size * 4, 255);
|
||||
|
||||
for (let row = 0; row < modules; row += 1) {
|
||||
for (let col = 0; col < modules; col += 1) {
|
||||
if (!qr.isDark(row, col)) continue;
|
||||
const startX = (col + marginModules) * scale;
|
||||
const startY = (row + marginModules) * scale;
|
||||
for (let y = 0; y < scale; y += 1) {
|
||||
const pixelY = startY + y;
|
||||
for (let x = 0; x < scale; x += 1) {
|
||||
const pixelX = startX + x;
|
||||
fillPixel(buf, pixelX, pixelY, size, 0, 0, 0, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const png = encodePngRgba(buf, size, size);
|
||||
return `data:image/png;base64,${png.toString('base64')}`;
|
||||
}
|
||||
|
||||
function isLoginFresh(login: ActiveLogin): boolean {
|
||||
return Date.now() - login.startedAt < ACTIVE_LOGIN_TTL_MS;
|
||||
}
|
||||
|
||||
function resolveConfigPath(): string {
|
||||
const envPath = process.env.OPENCLAW_CONFIG?.trim();
|
||||
if (envPath) return envPath;
|
||||
return join(OPENCLAW_DIR, 'openclaw.json');
|
||||
}
|
||||
|
||||
function loadWeChatRouteTag(accountId?: string): string | undefined {
|
||||
try {
|
||||
const configPath = resolveConfigPath();
|
||||
if (!existsSync(configPath)) return undefined;
|
||||
const raw = readFileSync(configPath, 'utf-8');
|
||||
const parsed = JSON.parse(raw) as {
|
||||
channels?: Record<string, {
|
||||
routeTag?: string | number;
|
||||
accounts?: Record<string, { routeTag?: string | number }>;
|
||||
}>;
|
||||
};
|
||||
const section = parsed.channels?.['openclaw-weixin'];
|
||||
if (!section) return undefined;
|
||||
if (accountId) {
|
||||
const normalizedAccountId = normalizeOpenClawAccountId(accountId);
|
||||
const scopedRouteTag = section.accounts?.[normalizedAccountId]?.routeTag;
|
||||
if (typeof scopedRouteTag === 'number') return String(scopedRouteTag);
|
||||
if (typeof scopedRouteTag === 'string' && scopedRouteTag.trim()) return scopedRouteTag.trim();
|
||||
}
|
||||
if (typeof section.routeTag === 'number') return String(section.routeTag);
|
||||
if (typeof section.routeTag === 'string' && section.routeTag.trim()) return section.routeTag.trim();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function fetchWeChatQrCode(apiBaseUrl: string, accountId?: string, botType = DEFAULT_ILINK_BOT_TYPE): Promise<QrCodeResponse> {
|
||||
const base = apiBaseUrl.endsWith('/') ? apiBaseUrl : `${apiBaseUrl}/`;
|
||||
const url = new URL(`ilink/bot/get_bot_qrcode?bot_type=${encodeURIComponent(botType)}`, base);
|
||||
const headers: Record<string, string> = {};
|
||||
const routeTag = loadWeChatRouteTag(accountId);
|
||||
if (routeTag) {
|
||||
headers.SKRouteTag = routeTag;
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), { headers });
|
||||
if (!response.ok) {
|
||||
const body = await response.text().catch(() => '(unreadable)');
|
||||
throw new Error(`Failed to fetch QR code: ${response.status} ${response.statusText} ${body}`);
|
||||
}
|
||||
return await response.json() as QrCodeResponse;
|
||||
}
|
||||
|
||||
async function pollWeChatQrStatus(apiBaseUrl: string, qrcode: string, accountId?: string): Promise<QrStatusResponse> {
|
||||
const base = apiBaseUrl.endsWith('/') ? apiBaseUrl : `${apiBaseUrl}/`;
|
||||
const url = new URL(`ilink/bot/get_qrcode_status?qrcode=${encodeURIComponent(qrcode)}`, base);
|
||||
const headers: Record<string, string> = {
|
||||
'iLink-App-ClientVersion': '1',
|
||||
};
|
||||
const routeTag = loadWeChatRouteTag(accountId);
|
||||
if (routeTag) {
|
||||
headers.SKRouteTag = routeTag;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), QR_POLL_TIMEOUT_MS);
|
||||
try {
|
||||
const response = await fetch(url.toString(), { headers, signal: controller.signal });
|
||||
clearTimeout(timer);
|
||||
const rawText = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to poll QR status: ${response.status} ${response.statusText} ${rawText}`);
|
||||
}
|
||||
return JSON.parse(rawText) as QrStatusResponse;
|
||||
} catch (error) {
|
||||
clearTimeout(timer);
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
return { status: 'wait' };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function readAccountIndex(): Promise<string[]> {
|
||||
try {
|
||||
const raw = await readFile(WECHAT_ACCOUNT_INDEX_FILE, 'utf-8');
|
||||
const parsed = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter((entry): entry is string => typeof entry === 'string' && entry.trim().length > 0);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function writeAccountIndex(accountIds: string[]): Promise<void> {
|
||||
await mkdir(WECHAT_STATE_DIR, { recursive: true });
|
||||
await writeFile(WECHAT_ACCOUNT_INDEX_FILE, JSON.stringify(accountIds, null, 2), 'utf-8');
|
||||
}
|
||||
|
||||
export async function saveWeChatAccountState(rawAccountId: string, payload: {
|
||||
token: string;
|
||||
baseUrl?: string;
|
||||
userId?: string;
|
||||
}): Promise<string> {
|
||||
const accountId = normalizeOpenClawAccountId(rawAccountId);
|
||||
await mkdir(WECHAT_ACCOUNTS_DIR, { recursive: true });
|
||||
|
||||
const filePath = join(WECHAT_ACCOUNTS_DIR, `${accountId}.json`);
|
||||
const data = {
|
||||
token: payload.token.trim(),
|
||||
savedAt: new Date().toISOString(),
|
||||
...(payload.baseUrl?.trim() ? { baseUrl: payload.baseUrl.trim() } : {}),
|
||||
...(payload.userId?.trim() ? { userId: payload.userId.trim() } : {}),
|
||||
};
|
||||
await writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
||||
try {
|
||||
await chmod(filePath, 0o600);
|
||||
} catch {
|
||||
// best effort only
|
||||
}
|
||||
|
||||
const existingAccountIds = await readAccountIndex();
|
||||
if (!existingAccountIds.includes(accountId)) {
|
||||
await writeAccountIndex([...existingAccountIds, accountId]);
|
||||
}
|
||||
|
||||
return accountId;
|
||||
}
|
||||
|
||||
export async function startWeChatLoginSession(options: {
|
||||
sessionKey?: string;
|
||||
accountId?: string;
|
||||
apiBaseUrl?: string;
|
||||
force?: boolean;
|
||||
}): Promise<WeChatLoginStartResult> {
|
||||
const sessionKey = options.sessionKey?.trim() || randomUUID();
|
||||
const apiBaseUrl = options.apiBaseUrl?.trim() || DEFAULT_WECHAT_BASE_URL;
|
||||
const existing = activeLogins.get(sessionKey);
|
||||
|
||||
if (!options.force && existing && isLoginFresh(existing) && existing.qrcodeUrl) {
|
||||
return {
|
||||
sessionKey,
|
||||
qrcodeUrl: existing.qrcodeUrl,
|
||||
message: 'QR code is ready. Scan it with WeChat.',
|
||||
};
|
||||
}
|
||||
|
||||
const qrResponse = await fetchWeChatQrCode(apiBaseUrl, options.accountId);
|
||||
const qrDataUrl = await renderQrPngDataUrl(qrResponse.qrcode_img_content);
|
||||
activeLogins.set(sessionKey, {
|
||||
sessionKey,
|
||||
qrcode: qrResponse.qrcode,
|
||||
qrcodeUrl: qrDataUrl,
|
||||
startedAt: Date.now(),
|
||||
apiBaseUrl,
|
||||
});
|
||||
|
||||
return {
|
||||
sessionKey,
|
||||
qrcodeUrl: qrDataUrl,
|
||||
message: 'Scan the QR code with WeChat to complete login.',
|
||||
};
|
||||
}
|
||||
|
||||
export async function waitForWeChatLoginSession(options: {
|
||||
sessionKey: string;
|
||||
timeoutMs?: number;
|
||||
accountId?: string;
|
||||
onQrRefresh?: (payload: { qrcodeUrl: string }) => void | Promise<void>;
|
||||
}): Promise<WeChatLoginWaitResult> {
|
||||
const login = activeLogins.get(options.sessionKey);
|
||||
if (!login) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'No active WeChat login session. Generate a new QR code and try again.',
|
||||
};
|
||||
}
|
||||
|
||||
if (!isLoginFresh(login)) {
|
||||
activeLogins.delete(options.sessionKey);
|
||||
return {
|
||||
connected: false,
|
||||
message: 'The QR code has expired. Generate a new QR code and try again.',
|
||||
};
|
||||
}
|
||||
|
||||
const timeoutMs = Math.max(options.timeoutMs ?? 480_000, 1000);
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
let qrRefreshCount = 1;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
const current = activeLogins.get(options.sessionKey);
|
||||
if (!current) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'The WeChat login session was cancelled.',
|
||||
};
|
||||
}
|
||||
|
||||
const statusResponse = await pollWeChatQrStatus(current.apiBaseUrl, current.qrcode, options.accountId);
|
||||
switch (statusResponse.status) {
|
||||
case 'wait':
|
||||
case 'scaned':
|
||||
break;
|
||||
case 'expired': {
|
||||
qrRefreshCount += 1;
|
||||
if (qrRefreshCount > MAX_QR_REFRESH_COUNT) {
|
||||
activeLogins.delete(options.sessionKey);
|
||||
return {
|
||||
connected: false,
|
||||
message: 'The QR code expired too many times. Generate a new QR code and try again.',
|
||||
};
|
||||
}
|
||||
const refreshedQr = await fetchWeChatQrCode(current.apiBaseUrl, options.accountId);
|
||||
const refreshedQrDataUrl = await renderQrPngDataUrl(refreshedQr.qrcode_img_content);
|
||||
activeLogins.set(options.sessionKey, {
|
||||
...current,
|
||||
qrcode: refreshedQr.qrcode,
|
||||
qrcodeUrl: refreshedQrDataUrl,
|
||||
startedAt: Date.now(),
|
||||
});
|
||||
await options.onQrRefresh?.({ qrcodeUrl: refreshedQrDataUrl });
|
||||
break;
|
||||
}
|
||||
case 'confirmed':
|
||||
activeLogins.delete(options.sessionKey);
|
||||
if (!statusResponse.ilink_bot_id || !statusResponse.bot_token) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'WeChat login succeeded but the server did not return the required account credentials.',
|
||||
};
|
||||
}
|
||||
return {
|
||||
connected: true,
|
||||
botToken: statusResponse.bot_token,
|
||||
accountId: statusResponse.ilink_bot_id,
|
||||
baseUrl: statusResponse.baseurl,
|
||||
userId: statusResponse.ilink_user_id,
|
||||
message: 'WeChat connected successfully.',
|
||||
};
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
|
||||
activeLogins.delete(options.sessionKey);
|
||||
return {
|
||||
connected: false,
|
||||
message: 'Timed out waiting for WeChat QR confirmation.',
|
||||
};
|
||||
}
|
||||
|
||||
export async function cancelWeChatLoginSession(sessionKey?: string): Promise<void> {
|
||||
if (!sessionKey) {
|
||||
activeLogins.clear();
|
||||
return;
|
||||
}
|
||||
activeLogins.delete(sessionKey);
|
||||
}
|
||||
|
||||
export async function clearWeChatLoginState(): Promise<void> {
|
||||
activeLogins.clear();
|
||||
await rm(WECHAT_STATE_DIR, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user