refactor(setup): share patched account adapters

This commit is contained in:
Peter Steinberger
2026-03-17 03:43:13 +00:00
parent 9c48321176
commit 7fc134d74e
4 changed files with 119 additions and 86 deletions

View File

@@ -1,7 +1,7 @@
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";
import { applySetupAccountConfigPatch, createPatchedAccountSetupAdapter } from "./setup-helpers.js";
function asConfig(value: unknown): OpenClawConfig {
return value as OpenClawConfig;
@@ -79,3 +79,55 @@ describe("applySetupAccountConfigPatch", () => {
});
});
});
describe("createPatchedAccountSetupAdapter", () => {
it("stores default-account patch at channel root", () => {
const adapter = createPatchedAccountSetupAdapter({
channelKey: "zalo",
buildPatch: (input) => ({ botToken: input.token }),
});
const next = adapter.applyAccountConfig({
cfg: asConfig({ channels: { zalo: { enabled: false } } }),
accountId: DEFAULT_ACCOUNT_ID,
input: { name: "Personal", token: "tok" },
});
expect(next.channels?.zalo).toMatchObject({
enabled: true,
name: "Personal",
botToken: "tok",
});
});
it("migrates base name into the default account before patching a named account", () => {
const adapter = createPatchedAccountSetupAdapter({
channelKey: "zalo",
buildPatch: (input) => ({ botToken: input.token }),
});
const next = adapter.applyAccountConfig({
cfg: asConfig({
channels: {
zalo: {
name: "Personal",
accounts: {
work: { botToken: "old" },
},
},
},
}),
accountId: "Work Team",
input: { name: "Work", token: "new" },
});
expect(next.channels?.zalo).toMatchObject({
accounts: {
default: { name: "Personal" },
work: { botToken: "old" },
"work-team": { enabled: true, name: "Work", botToken: "new" },
},
});
expect(next.channels?.zalo).not.toHaveProperty("name");
});
});

View File

@@ -1,5 +1,7 @@
import type { OpenClawConfig } from "../../config/config.js";
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
import type { ChannelSetupAdapter } from "./types.adapters.js";
import type { ChannelSetupInput } from "./types.core.js";
type ChannelSectionBase = {
name?: string;
@@ -134,6 +136,49 @@ export function applySetupAccountConfigPatch(params: {
});
}
export function createPatchedAccountSetupAdapter(params: {
channelKey: string;
alwaysUseAccounts?: boolean;
validateInput?: ChannelSetupAdapter["validateInput"];
buildPatch: (input: ChannelSetupInput) => Record<string, unknown>;
}): ChannelSetupAdapter {
return {
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
applyAccountName: ({ cfg, accountId, name }) =>
applyAccountNameToChannelSection({
cfg,
channelKey: params.channelKey,
accountId,
name,
alwaysUseAccounts: params.alwaysUseAccounts,
}),
validateInput: params.validateInput,
applyAccountConfig: ({ cfg, accountId, input }) => {
const namedConfig = applyAccountNameToChannelSection({
cfg,
channelKey: params.channelKey,
accountId,
name: input.name,
alwaysUseAccounts: params.alwaysUseAccounts,
});
const next =
accountId !== DEFAULT_ACCOUNT_ID
? migrateBaseNameToDefaultAccount({
cfg: namedConfig,
channelKey: params.channelKey,
alwaysUseAccounts: params.alwaysUseAccounts,
})
: namedConfig;
return applySetupAccountConfigPatch({
cfg: next,
channelKey: params.channelKey,
accountId,
patch: params.buildPatch(input),
});
},
};
}
export function patchScopedAccountConfig(params: {
cfg: OpenClawConfig;
channelKey: string;