refactor: share setup account config patch helper

This commit is contained in:
Peter Steinberger
2026-03-07 19:53:31 +00:00
parent 2ee8b807f8
commit b0ac284dae
11 changed files with 176 additions and 150 deletions

View File

@@ -0,0 +1,81 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import { DEFAULT_ACCOUNT_ID } from "../../routing/session-key.js";
import { applySetupAccountConfigPatch } from "./setup-helpers.js";
function asConfig(value: unknown): OpenClawConfig {
return value as OpenClawConfig;
}
describe("applySetupAccountConfigPatch", () => {
it("patches top-level config for default account and enables channel", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
webhookPath: "/old",
enabled: false,
},
},
}),
channelKey: "zalo",
accountId: DEFAULT_ACCOUNT_ID,
patch: { webhookPath: "/new", botToken: "tok" },
});
expect(next.channels?.zalo).toMatchObject({
enabled: true,
webhookPath: "/new",
botToken: "tok",
});
});
it("patches named account config and enables both channel and account", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
enabled: false,
accounts: {
work: { botToken: "old", enabled: false },
},
},
},
}),
channelKey: "zalo",
accountId: "work",
patch: { botToken: "new" },
});
expect(next.channels?.zalo).toMatchObject({
enabled: true,
accounts: {
work: { enabled: true, botToken: "new" },
},
});
});
it("normalizes account id and preserves other accounts", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
accounts: {
personal: { botToken: "personal-token" },
},
},
},
}),
channelKey: "zalo",
accountId: "Work Team",
patch: { botToken: "work-token" },
});
expect(next.channels?.zalo).toMatchObject({
accounts: {
personal: { botToken: "personal-token" },
"work-team": { enabled: true, botToken: "work-token" },
},
});
});
});

View File

@@ -120,6 +120,56 @@ export function migrateBaseNameToDefaultAccount(params: {
} as OpenClawConfig;
}
export function applySetupAccountConfigPatch(params: {
cfg: OpenClawConfig;
channelKey: string;
accountId: string;
patch: Record<string, unknown>;
}): OpenClawConfig {
const accountId = normalizeAccountId(params.accountId);
const channels = params.cfg.channels as Record<string, unknown> | undefined;
const channelConfig = channels?.[params.channelKey];
const base =
typeof channelConfig === "object" && channelConfig
? (channelConfig as Record<string, unknown> & {
accounts?: Record<string, Record<string, unknown>>;
})
: undefined;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channelKey]: {
...base,
enabled: true,
...params.patch,
},
},
} as OpenClawConfig;
}
const accounts = base?.accounts ?? {};
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channelKey]: {
...base,
enabled: true,
accounts: {
...accounts,
[accountId]: {
...accounts[accountId],
enabled: true,
...params.patch,
},
},
},
},
} as OpenClawConfig;
}
type ChannelSectionRecord = Record<string, unknown> & {
accounts?: Record<string, Record<string, unknown>>;
};

View File

@@ -30,6 +30,7 @@ export {
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
migrateBaseNameToDefaultAccount,
} from "../channels/plugins/setup-helpers.js";
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";

View File

@@ -35,6 +35,7 @@ export {
} from "../channels/plugins/onboarding/helpers.js";
export {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
migrateBaseNameToDefaultAccount,
} from "../channels/plugins/setup-helpers.js";
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";

View File

@@ -23,6 +23,7 @@ export {
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
migrateBaseNameToDefaultAccount,
} from "../channels/plugins/setup-helpers.js";
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";

View File

@@ -23,6 +23,7 @@ export {
} from "../channels/plugins/onboarding/helpers.js";
export {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
migrateBaseNameToDefaultAccount,
} from "../channels/plugins/setup-helpers.js";
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";