mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 20:50:22 +00:00
refactor(setup): support account-scoped default patches
This commit is contained in:
@@ -130,4 +130,30 @@ describe("createPatchedAccountSetupAdapter", () => {
|
||||
});
|
||||
expect(next.channels?.zalo).not.toHaveProperty("name");
|
||||
});
|
||||
|
||||
it("can store the default account in accounts.default", () => {
|
||||
const adapter = createPatchedAccountSetupAdapter({
|
||||
channelKey: "whatsapp",
|
||||
alwaysUseAccounts: true,
|
||||
buildPatch: (input) => ({ authDir: input.authDir }),
|
||||
});
|
||||
|
||||
const next = adapter.applyAccountConfig({
|
||||
cfg: asConfig({ channels: { whatsapp: {} } }),
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
input: { name: "Phone", authDir: "/tmp/auth" },
|
||||
});
|
||||
|
||||
expect(next.channels?.whatsapp).toMatchObject({
|
||||
accounts: {
|
||||
default: {
|
||||
enabled: true,
|
||||
name: "Phone",
|
||||
authDir: "/tmp/auth",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(next.channels?.whatsapp).not.toHaveProperty("enabled");
|
||||
expect(next.channels?.whatsapp).not.toHaveProperty("authDir");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -139,6 +139,8 @@ export function applySetupAccountConfigPatch(params: {
|
||||
export function createPatchedAccountSetupAdapter(params: {
|
||||
channelKey: string;
|
||||
alwaysUseAccounts?: boolean;
|
||||
ensureChannelEnabled?: boolean;
|
||||
ensureAccountEnabled?: boolean;
|
||||
validateInput?: ChannelSetupAdapter["validateInput"];
|
||||
buildPatch: (input: ChannelSetupInput) => Record<string, unknown>;
|
||||
}): ChannelSetupAdapter {
|
||||
@@ -169,11 +171,16 @@ export function createPatchedAccountSetupAdapter(params: {
|
||||
alwaysUseAccounts: params.alwaysUseAccounts,
|
||||
})
|
||||
: namedConfig;
|
||||
return applySetupAccountConfigPatch({
|
||||
const patch = params.buildPatch(input);
|
||||
return patchScopedAccountConfig({
|
||||
cfg: next,
|
||||
channelKey: params.channelKey,
|
||||
accountId,
|
||||
patch: params.buildPatch(input),
|
||||
patch,
|
||||
accountPatch: patch,
|
||||
ensureChannelEnabled: params.ensureChannelEnabled ?? !params.alwaysUseAccounts,
|
||||
ensureAccountEnabled: params.ensureAccountEnabled ?? true,
|
||||
scopeDefaultToAccounts: params.alwaysUseAccounts,
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -187,6 +194,7 @@ export function patchScopedAccountConfig(params: {
|
||||
accountPatch?: Record<string, unknown>;
|
||||
ensureChannelEnabled?: boolean;
|
||||
ensureAccountEnabled?: boolean;
|
||||
scopeDefaultToAccounts?: boolean;
|
||||
}): OpenClawConfig {
|
||||
const accountId = normalizeAccountId(params.accountId);
|
||||
const channels = params.cfg.channels as Record<string, unknown> | undefined;
|
||||
@@ -201,7 +209,7 @@ export function patchScopedAccountConfig(params: {
|
||||
const ensureAccountEnabled = params.ensureAccountEnabled ?? ensureChannelEnabled;
|
||||
const patch = params.patch;
|
||||
const accountPatch = params.accountPatch ?? patch;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
if (accountId === DEFAULT_ACCOUNT_ID && !params.scopeDefaultToAccounts) {
|
||||
return {
|
||||
...params.cfg,
|
||||
channels: {
|
||||
|
||||
Reference in New Issue
Block a user