Upgrade openclaw to 4.9 (#804)
This commit is contained in:
committed by
GitHub
Unverified
parent
96c9f6fe5b
commit
467fcf7e92
@@ -26,6 +26,11 @@ 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';
|
||||
// Channels whose plugin schema uses additionalProperties:false, meaning
|
||||
// credential keys MUST NOT appear at the top level of `channels.<type>`.
|
||||
// All other channels get the default account mirrored to the top level
|
||||
// so their runtime/plugin can discover the credentials.
|
||||
const CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR = new Set(['dingtalk']);
|
||||
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');
|
||||
@@ -753,36 +758,50 @@ export async function saveChannelConfig(
|
||||
}
|
||||
}
|
||||
|
||||
// Write credentials into accounts.<accountId>
|
||||
const accounts = ensureChannelAccountsMap(channelSection);
|
||||
channelSection.defaultAccount =
|
||||
typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim()
|
||||
? channelSection.defaultAccount
|
||||
: resolvedAccountId;
|
||||
accounts[resolvedAccountId] = {
|
||||
...accounts[resolvedAccountId],
|
||||
...transformedConfig,
|
||||
enabled: transformedConfig.enabled ?? true,
|
||||
};
|
||||
|
||||
// Most OpenClaw channel plugins read the default account's credentials
|
||||
// from the top level of `channels.<type>` (e.g. channels.feishu.appId),
|
||||
// not from `accounts.default`. Mirror them there so plugins can discover
|
||||
// the credentials correctly.
|
||||
// This MUST run unconditionally (not just when saving the default account)
|
||||
// because migrateLegacyChannelConfigToAccounts() above strips top-level
|
||||
// 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 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)) {
|
||||
// ── Strict-schema channels (e.g. dingtalk) ──────────────────────
|
||||
// These plugins declare additionalProperties:false and do NOT
|
||||
// recognise `accounts` / `defaultAccount`. Write credentials
|
||||
// flat to the channel root and strip the multi-account keys.
|
||||
if (CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR.has(resolvedChannelType)) {
|
||||
for (const [key, value] of Object.entries(transformedConfig)) {
|
||||
channelSection[key] = value;
|
||||
}
|
||||
channelSection.enabled = transformedConfig.enabled ?? channelSection.enabled ?? true;
|
||||
// Remove keys the strict schema rejects
|
||||
delete channelSection.accounts;
|
||||
delete channelSection.defaultAccount;
|
||||
} else {
|
||||
// ── Normal channels ──────────────────────────────────────────
|
||||
// Write into accounts.<accountId> (multi-account support).
|
||||
const accounts = ensureChannelAccountsMap(channelSection);
|
||||
channelSection.defaultAccount =
|
||||
typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim()
|
||||
? channelSection.defaultAccount
|
||||
: resolvedAccountId;
|
||||
accounts[resolvedAccountId] = {
|
||||
...accounts[resolvedAccountId],
|
||||
...transformedConfig,
|
||||
enabled: transformedConfig.enabled ?? true,
|
||||
};
|
||||
|
||||
// Keep channel-level enabled explicit so callers/tests that
|
||||
// read channels.<type>.enabled still work.
|
||||
channelSection.enabled = transformedConfig.enabled ?? channelSection.enabled ?? true;
|
||||
|
||||
// Most OpenClaw channel plugins/built-ins also read the default
|
||||
// account's credentials from the top level of `channels.<type>`
|
||||
// (e.g. channels.feishu.appId). Mirror them there so the
|
||||
// runtime can discover them.
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
@@ -881,9 +900,29 @@ export async function deleteChannelAccountConfig(channelType: string, accountId:
|
||||
return;
|
||||
}
|
||||
|
||||
// Strict-schema channels have no `accounts` structure — delete means
|
||||
// removing the entire channel section.
|
||||
if (CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR.has(resolvedChannelType)) {
|
||||
delete currentConfig.channels![resolvedChannelType];
|
||||
syncBuiltinChannelsWithPluginAllowlist(currentConfig);
|
||||
await writeOpenClawConfig(currentConfig);
|
||||
logger.info('Deleted strict-schema channel config', { channelType: resolvedChannelType, accountId });
|
||||
return;
|
||||
}
|
||||
|
||||
migrateLegacyChannelConfigToAccounts(channelSection, DEFAULT_ACCOUNT_ID);
|
||||
const accounts = getChannelAccountsMap(channelSection);
|
||||
if (!accounts?.[accountId]) return;
|
||||
if (!accounts?.[accountId]) {
|
||||
// Account not found; just ensure top-level mirror is consistent
|
||||
const mirroredAccountId = typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim() ? channelSection.defaultAccount : DEFAULT_ACCOUNT_ID;
|
||||
const defaultAccountData = accounts?.[mirroredAccountId] ?? accounts?.[DEFAULT_ACCOUNT_ID];
|
||||
if (defaultAccountData) {
|
||||
for (const [key, value] of Object.entries(defaultAccountData)) {
|
||||
channelSection[key] = value;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
delete accounts[accountId];
|
||||
|
||||
@@ -905,6 +944,7 @@ export async function deleteChannelAccountConfig(channelType: string, accountId:
|
||||
}
|
||||
// Re-mirror default account credentials to top level after migration
|
||||
// stripped them (same rationale as saveChannelConfig).
|
||||
// (Strict-schema channels already returned above, so this is safe.)
|
||||
const mirroredAccountId =
|
||||
typeof channelSection.defaultAccount === 'string' && channelSection.defaultAccount.trim()
|
||||
? channelSection.defaultAccount
|
||||
@@ -1104,6 +1144,7 @@ export async function setChannelDefaultAccount(channelType: string, accountId: s
|
||||
|
||||
channelSection.defaultAccount = trimmedAccountId;
|
||||
|
||||
// Strict-schema channels don't use defaultAccount — always mirror for others
|
||||
const defaultAccountData = accounts[trimmedAccountId];
|
||||
for (const [key, value] of Object.entries(defaultAccountData)) {
|
||||
channelSection[key] = value;
|
||||
@@ -1126,8 +1167,18 @@ export async function deleteAgentChannelAccounts(agentId: string, ownedChannelAc
|
||||
const section = currentConfig.channels[channelType];
|
||||
migrateLegacyChannelConfigToAccounts(section, DEFAULT_ACCOUNT_ID);
|
||||
const accounts = getChannelAccountsMap(section);
|
||||
if (!accounts?.[accountId]) continue;
|
||||
if (ownedChannelAccounts && !ownedChannelAccounts.has(`${channelType}:${accountId}`)) {
|
||||
if (!accounts?.[accountId] || (ownedChannelAccounts && !ownedChannelAccounts.has(`${channelType}:${accountId}`))) {
|
||||
// Strict-schema channels have no accounts map; skip them.
|
||||
// For normal channels, ensure top-level mirror is consistent.
|
||||
if (!CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR.has(channelType)) {
|
||||
const mirroredAccountId = typeof section.defaultAccount === 'string' && section.defaultAccount.trim() ? section.defaultAccount : DEFAULT_ACCOUNT_ID;
|
||||
const defaultAccountData = accounts?.[mirroredAccountId] ?? accounts?.[DEFAULT_ACCOUNT_ID];
|
||||
if (defaultAccountData) {
|
||||
for (const [key, value] of Object.entries(defaultAccountData)) {
|
||||
section[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1770,30 +1770,54 @@ export async function sanitizeOpenClawConfig(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── channels default-account migration ─────────────────────────
|
||||
// Most OpenClaw channel plugins read the default account's credentials
|
||||
// from the top level of `channels.<type>` (e.g. channels.feishu.appId),
|
||||
// but ClawX historically stored them only under `channels.<type>.accounts.default`.
|
||||
// Mirror the default account credentials at the top level so plugins can
|
||||
// discover them.
|
||||
// ── channels default-account migration and cleanup ─────────────
|
||||
// Most OpenClaw channel plugins/built-ins read the default account's
|
||||
// credentials from the top level of `channels.<type>`. Mirror them
|
||||
// there so the runtime can discover them.
|
||||
//
|
||||
// Strict-schema channels (e.g. dingtalk, additionalProperties:false)
|
||||
// reject the `accounts` / `defaultAccount` keys entirely — strip them
|
||||
// so the Gateway doesn't crash on startup.
|
||||
const channelsObj = config.channels as Record<string, Record<string, unknown>> | undefined;
|
||||
const CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR = new Set(['dingtalk']);
|
||||
|
||||
if (channelsObj && typeof channelsObj === 'object') {
|
||||
for (const [channelType, section] of Object.entries(channelsObj)) {
|
||||
if (!section || typeof section !== 'object') continue;
|
||||
const accounts = section.accounts as Record<string, Record<string, unknown>> | undefined;
|
||||
const defaultAccount = accounts?.default;
|
||||
if (!defaultAccount || typeof defaultAccount !== 'object') continue;
|
||||
// Mirror each missing key from accounts.default to the top level
|
||||
let mirrored = false;
|
||||
for (const [key, value] of Object.entries(defaultAccount)) {
|
||||
if (!(key in section)) {
|
||||
section[key] = value;
|
||||
mirrored = true;
|
||||
|
||||
if (CHANNELS_EXCLUDING_TOP_LEVEL_MIRROR.has(channelType)) {
|
||||
// Strict-schema channel: strip `accounts` and `defaultAccount`.
|
||||
// Credentials should live flat at the channel root.
|
||||
if ('accounts' in section) {
|
||||
delete section['accounts'];
|
||||
modified = true;
|
||||
console.log(`[sanitize] Removed incompatible 'accounts' from channels.${channelType}`);
|
||||
}
|
||||
if ('defaultAccount' in section) {
|
||||
delete section['defaultAccount'];
|
||||
modified = true;
|
||||
console.log(`[sanitize] Removed incompatible 'defaultAccount' from channels.${channelType}`);
|
||||
}
|
||||
} else {
|
||||
// Normal channel: mirror missing keys from default account to top level.
|
||||
const accounts = section.accounts as Record<string, Record<string, unknown>> | undefined;
|
||||
const defaultAccountId =
|
||||
typeof section.defaultAccount === 'string' && section.defaultAccount.trim()
|
||||
? section.defaultAccount
|
||||
: 'default';
|
||||
const defaultAccountData = accounts?.[defaultAccountId] ?? accounts?.['default'];
|
||||
if (!defaultAccountData || typeof defaultAccountData !== 'object') continue;
|
||||
let mirrored = false;
|
||||
for (const [key, value] of Object.entries(defaultAccountData)) {
|
||||
if (!(key in section)) {
|
||||
section[key] = value;
|
||||
mirrored = true;
|
||||
}
|
||||
}
|
||||
if (mirrored) {
|
||||
modified = true;
|
||||
console.log(`[sanitize] Mirrored ${channelType} default account credentials to top-level channels.${channelType}`);
|
||||
}
|
||||
}
|
||||
if (mirrored) {
|
||||
modified = true;
|
||||
console.log(`[sanitize] Mirrored ${channelType} default account credentials to top-level channels.${channelType}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,16 +12,31 @@
|
||||
*
|
||||
* In dev mode (pnpm), the resolved path is in the pnpm virtual store where
|
||||
* self-referencing also works. The projectRequire fallback covers edge cases.
|
||||
*
|
||||
* openclaw 2026.4.5 removed the per-channel plugin-sdk subpath exports
|
||||
* (discord, telegram-surface, slack, whatsapp-shared). The functions now live
|
||||
* in the extension bundles (dist/extensions/<channel>/api.js) which pull in
|
||||
* heavy optional dependencies (grammy, @buape/carbon, @slack/web-api …).
|
||||
*
|
||||
* Since ClawX only uses the lightweight normalize / directory helpers, we load
|
||||
* these from the extension API files directly. If the optional dependency is
|
||||
* missing (common in dev without full install), we fall back to no-op stubs so
|
||||
* the app can still start — the target picker will simply be empty for that
|
||||
* channel.
|
||||
*/
|
||||
import { createRequire } from 'module';
|
||||
import { join } from 'node:path';
|
||||
import { getOpenClawDir, getOpenClawResolvedDir } from './paths';
|
||||
|
||||
const _openclawPath = getOpenClawDir();
|
||||
const _openclawResolvedPath = getOpenClawResolvedDir();
|
||||
const _openclawPath = getOpenClawDir();
|
||||
const _openclawSdkRequire = createRequire(join(_openclawResolvedPath, 'package.json'));
|
||||
const _projectSdkRequire = createRequire(join(_openclawPath, 'package.json'));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function requireOpenClawSdk(subpath: string): Record<string, unknown> {
|
||||
try {
|
||||
return _openclawSdkRequire(subpath);
|
||||
@@ -30,28 +45,138 @@ function requireOpenClawSdk(subpath: string): Record<string, unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Channel SDK dynamic imports ---
|
||||
const _discordSdk = requireOpenClawSdk('openclaw/plugin-sdk/discord') as {
|
||||
/**
|
||||
* Load an openclaw extension API module by relative path under the openclaw
|
||||
* dist directory. Falls back to no-op stubs when the optional dependency
|
||||
* tree is incomplete.
|
||||
*/
|
||||
function requireExtensionApi(relativePath: string): Record<string, unknown> | null {
|
||||
try {
|
||||
// Require relative to the openclaw dist directory.
|
||||
return _openclawSdkRequire(relativePath);
|
||||
} catch {
|
||||
try {
|
||||
return _projectSdkRequire(relativePath);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Generic no-op stubs used when channel SDK is unavailable.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const noopAsyncList = async (..._args: unknown[]): Promise<unknown[]> => [];
|
||||
const noopNormalize = (_target: string): string | undefined => undefined;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Legacy plugin-sdk subpath imports (openclaw <2026.4.5)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function tryLegacySdkImport(subpath: string): Record<string, unknown> | null {
|
||||
try {
|
||||
return requireOpenClawSdk(subpath);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Channel SDK loaders — try legacy plugin-sdk first, then extension api, then stubs
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type ChannelSdk<T> = T;
|
||||
|
||||
interface DiscordSdk {
|
||||
listDiscordDirectoryGroupsFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
listDiscordDirectoryPeersFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
normalizeDiscordMessagingTarget: (target: string) => string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
const _telegramSdk = requireOpenClawSdk('openclaw/plugin-sdk/telegram-surface') as {
|
||||
interface TelegramSdk {
|
||||
listTelegramDirectoryGroupsFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
listTelegramDirectoryPeersFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
normalizeTelegramMessagingTarget: (target: string) => string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
const _slackSdk = requireOpenClawSdk('openclaw/plugin-sdk/slack') as {
|
||||
interface SlackSdk {
|
||||
listSlackDirectoryGroupsFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
listSlackDirectoryPeersFromConfig: (...args: unknown[]) => Promise<unknown[]>;
|
||||
normalizeSlackMessagingTarget: (target: string) => string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
const _whatsappSdk = requireOpenClawSdk('openclaw/plugin-sdk/whatsapp-shared') as {
|
||||
interface WhatsappSdk {
|
||||
normalizeWhatsAppMessagingTarget: (target: string) => string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
function loadChannelSdk<T>(
|
||||
legacySubpath: string,
|
||||
extensionRelPath: string,
|
||||
fallback: T,
|
||||
keys: (keyof T)[],
|
||||
): ChannelSdk<T> {
|
||||
// 1. Try legacy plugin-sdk subpath (openclaw <4.5)
|
||||
const legacy = tryLegacySdkImport(legacySubpath);
|
||||
if (legacy && keys.every((k) => typeof legacy[k as string] === 'function')) {
|
||||
return legacy as unknown as T;
|
||||
}
|
||||
|
||||
// 2. Try extension API file (openclaw >=4.5)
|
||||
const ext = requireExtensionApi(extensionRelPath);
|
||||
if (ext && keys.every((k) => typeof ext[k as string] === 'function')) {
|
||||
return ext as unknown as T;
|
||||
}
|
||||
|
||||
// 3. Fallback to no-op stubs
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const _discordSdk = loadChannelSdk<DiscordSdk>(
|
||||
'openclaw/plugin-sdk/discord',
|
||||
'./dist/extensions/discord/api.js',
|
||||
{
|
||||
listDiscordDirectoryGroupsFromConfig: noopAsyncList,
|
||||
listDiscordDirectoryPeersFromConfig: noopAsyncList,
|
||||
normalizeDiscordMessagingTarget: noopNormalize,
|
||||
},
|
||||
['listDiscordDirectoryGroupsFromConfig', 'listDiscordDirectoryPeersFromConfig', 'normalizeDiscordMessagingTarget'],
|
||||
);
|
||||
|
||||
const _telegramSdk = loadChannelSdk<TelegramSdk>(
|
||||
'openclaw/plugin-sdk/telegram-surface',
|
||||
'./dist/extensions/telegram/api.js',
|
||||
{
|
||||
listTelegramDirectoryGroupsFromConfig: noopAsyncList,
|
||||
listTelegramDirectoryPeersFromConfig: noopAsyncList,
|
||||
normalizeTelegramMessagingTarget: noopNormalize,
|
||||
},
|
||||
['listTelegramDirectoryGroupsFromConfig', 'listTelegramDirectoryPeersFromConfig', 'normalizeTelegramMessagingTarget'],
|
||||
);
|
||||
|
||||
const _slackSdk = loadChannelSdk<SlackSdk>(
|
||||
'openclaw/plugin-sdk/slack',
|
||||
'./dist/extensions/slack/api.js',
|
||||
{
|
||||
listSlackDirectoryGroupsFromConfig: noopAsyncList,
|
||||
listSlackDirectoryPeersFromConfig: noopAsyncList,
|
||||
normalizeSlackMessagingTarget: noopNormalize,
|
||||
},
|
||||
['listSlackDirectoryGroupsFromConfig', 'listSlackDirectoryPeersFromConfig', 'normalizeSlackMessagingTarget'],
|
||||
);
|
||||
|
||||
const _whatsappSdk = loadChannelSdk<WhatsappSdk>(
|
||||
'openclaw/plugin-sdk/whatsapp-shared',
|
||||
'./dist/extensions/whatsapp/api.js',
|
||||
{
|
||||
normalizeWhatsAppMessagingTarget: noopNormalize,
|
||||
},
|
||||
['normalizeWhatsAppMessagingTarget'],
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public re-exports — identical API surface as before.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const {
|
||||
listDiscordDirectoryGroupsFromConfig,
|
||||
|
||||
Reference in New Issue
Block a user