refactor: share scoped account config patching

This commit is contained in:
Peter Steinberger
2026-03-10 20:22:06 +00:00
parent b517dc089a
commit 00170f8e1a
14 changed files with 118 additions and 253 deletions

View File

@@ -9,7 +9,10 @@ import { promptAccountId as promptAccountIdSdk } from "../../../plugin-sdk/onboa
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../../routing/session-key.js";
import type { WizardPrompter } from "../../../wizard/prompts.js";
import type { PromptAccountId, PromptAccountIdParams } from "../onboarding-types.js";
import { moveSingleAccountChannelSectionToDefaultAccount } from "../setup-helpers.js";
import {
moveSingleAccountChannelSectionToDefaultAccount,
patchScopedAccountConfig,
} from "../setup-helpers.js";
export const promptAccountId: PromptAccountId = async (params: PromptAccountIdParams) => {
return await promptAccountIdSdk(params);
@@ -364,50 +367,14 @@ function patchConfigForScopedAccount(params: {
cfg,
channelKey: channel,
});
const channelConfig =
(seededCfg.channels?.[channel] as Record<string, unknown> | undefined) ?? {};
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...seededCfg,
channels: {
...seededCfg.channels,
[channel]: {
...channelConfig,
...(ensureEnabled ? { enabled: true } : {}),
...patch,
},
},
};
}
const accounts =
(channelConfig.accounts as Record<string, Record<string, unknown>> | undefined) ?? {};
const existingAccount = accounts[accountId] ?? {};
return {
...seededCfg,
channels: {
...seededCfg.channels,
[channel]: {
...channelConfig,
...(ensureEnabled ? { enabled: true } : {}),
accounts: {
...accounts,
[accountId]: {
...existingAccount,
...(ensureEnabled
? {
enabled:
typeof existingAccount.enabled === "boolean" ? existingAccount.enabled : true,
}
: {}),
...patch,
},
},
},
},
};
return patchScopedAccountConfig({
cfg: seededCfg,
channelKey: channel,
accountId,
patch,
ensureChannelEnabled: ensureEnabled,
ensureAccountEnabled: ensureEnabled,
});
}
export function patchChannelConfigForAccount(params: {

View File

@@ -125,6 +125,23 @@ export function applySetupAccountConfigPatch(params: {
channelKey: string;
accountId: string;
patch: Record<string, unknown>;
}): OpenClawConfig {
return patchScopedAccountConfig({
cfg: params.cfg,
channelKey: params.channelKey,
accountId: params.accountId,
patch: params.patch,
});
}
export function patchScopedAccountConfig(params: {
cfg: OpenClawConfig;
channelKey: string;
accountId: string;
patch: Record<string, unknown>;
accountPatch?: Record<string, unknown>;
ensureChannelEnabled?: boolean;
ensureAccountEnabled?: boolean;
}): OpenClawConfig {
const accountId = normalizeAccountId(params.accountId);
const channels = params.cfg.channels as Record<string, unknown> | undefined;
@@ -135,6 +152,10 @@ export function applySetupAccountConfigPatch(params: {
accounts?: Record<string, Record<string, unknown>>;
})
: undefined;
const ensureChannelEnabled = params.ensureChannelEnabled ?? true;
const ensureAccountEnabled = params.ensureAccountEnabled ?? ensureChannelEnabled;
const patch = params.patch;
const accountPatch = params.accountPatch ?? patch;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...params.cfg,
@@ -142,27 +163,33 @@ export function applySetupAccountConfigPatch(params: {
...params.cfg.channels,
[params.channelKey]: {
...base,
enabled: true,
...params.patch,
...(ensureChannelEnabled ? { enabled: true } : {}),
...patch,
},
},
} as OpenClawConfig;
}
const accounts = base?.accounts ?? {};
const existingAccount = accounts[accountId] ?? {};
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channelKey]: {
...base,
enabled: true,
...(ensureChannelEnabled ? { enabled: true } : {}),
accounts: {
...accounts,
[accountId]: {
...accounts[accountId],
enabled: true,
...params.patch,
...existingAccount,
...(ensureAccountEnabled
? {
enabled:
typeof existingAccount.enabled === "boolean" ? existingAccount.enabled : true,
}
: {}),
...accountPatch,
},
},
},