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

@@ -256,18 +256,6 @@ export const bluebubblesPlugin: ChannelPlugin<ResolvedBlueBubblesAccount> = {
channelKey: "bluebubbles",
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return applyBlueBubblesConnectionConfig({
cfg: next,
accountId,
patch: {
serverUrl: input.httpUrl,
password: input.password,
webhookPath: input.webhookPath,
},
onlyDefinedFields: true,
});
}
return applyBlueBubblesConnectionConfig({
cfg: next,
accountId,

View File

@@ -1,5 +1,6 @@
import {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
buildChannelConfigSchema,
DEFAULT_ACCOUNT_ID,
deleteAccountFromConfigSection,
@@ -345,37 +346,12 @@ export const googlechatPlugin: ChannelPlugin<ResolvedGoogleChatAccount> = {
...(webhookPath ? { webhookPath } : {}),
...(webhookUrl ? { webhookUrl } : {}),
};
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
googlechat: {
...next.channels?.["googlechat"],
enabled: true,
...configPatch,
},
},
} as OpenClawConfig;
}
return {
...next,
channels: {
...next.channels,
googlechat: {
...next.channels?.["googlechat"],
enabled: true,
accounts: {
...next.channels?.["googlechat"]?.accounts,
[accountId]: {
...next.channels?.["googlechat"]?.accounts?.[accountId],
enabled: true,
...configPatch,
},
},
},
},
} as OpenClawConfig;
return applySetupAccountConfigPatch({
cfg: next,
channelKey: "googlechat",
accountId,
patch: configPatch,
});
},
},
outbound: {

View File

@@ -1,5 +1,6 @@
import {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
buildChannelConfigSchema,
DEFAULT_ACCOUNT_ID,
deleteAccountFromConfigSection,
@@ -449,43 +450,18 @@ export const mattermostPlugin: ChannelPlugin<ResolvedMattermostAccount> = {
channelKey: "mattermost",
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
mattermost: {
...next.channels?.mattermost,
enabled: true,
...(input.useEnv
? {}
: {
...(token ? { botToken: token } : {}),
...(baseUrl ? { baseUrl } : {}),
}),
},
},
};
}
return {
...next,
channels: {
...next.channels,
mattermost: {
...next.channels?.mattermost,
enabled: true,
accounts: {
...next.channels?.mattermost?.accounts,
[accountId]: {
...next.channels?.mattermost?.accounts?.[accountId],
enabled: true,
...(token ? { botToken: token } : {}),
...(baseUrl ? { baseUrl } : {}),
},
},
},
},
};
const patch = input.useEnv
? {}
: {
...(token ? { botToken: token } : {}),
...(baseUrl ? { baseUrl } : {}),
};
return applySetupAccountConfigPatch({
cfg: next,
channelKey: "mattermost",
accountId,
patch,
});
},
},
gateway: {

View File

@@ -6,6 +6,7 @@ import type {
} from "openclaw/plugin-sdk/zalo";
import {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
buildBaseAccountStatusSnapshot,
buildChannelConfigSchema,
buildTokenChannelStatusSummary,
@@ -243,47 +244,19 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
channelKey: "zalo",
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
zalo: {
...next.channels?.zalo,
enabled: true,
...(input.useEnv
? {}
: input.tokenFile
? { tokenFile: input.tokenFile }
: input.token
? { botToken: input.token }
: {}),
},
},
} as OpenClawConfig;
}
return {
...next,
channels: {
...next.channels,
zalo: {
...next.channels?.zalo,
enabled: true,
accounts: {
...next.channels?.zalo?.accounts,
[accountId]: {
...next.channels?.zalo?.accounts?.[accountId],
enabled: true,
...(input.tokenFile
? { tokenFile: input.tokenFile }
: input.token
? { botToken: input.token }
: {}),
},
},
},
},
} as OpenClawConfig;
const patch = input.useEnv
? {}
: input.tokenFile
? { tokenFile: input.tokenFile }
: input.token
? { botToken: input.token }
: {};
return applySetupAccountConfigPatch({
cfg: next,
channelKey: "zalo",
accountId,
patch,
});
},
},
pairing: {

View File

@@ -10,6 +10,7 @@ import type {
} from "openclaw/plugin-sdk/zalouser";
import {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
buildChannelSendResult,
buildBaseAccountStatusSnapshot,
buildChannelConfigSchema,
@@ -329,35 +330,12 @@ export const zalouserPlugin: ChannelPlugin<ResolvedZalouserAccount> = {
channelKey: "zalouser",
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
zalouser: {
...next.channels?.zalouser,
enabled: true,
},
},
} as OpenClawConfig;
}
return {
...next,
channels: {
...next.channels,
zalouser: {
...next.channels?.zalouser,
enabled: true,
accounts: {
...next.channels?.zalouser?.accounts,
[accountId]: {
...next.channels?.zalouser?.accounts?.[accountId],
enabled: true,
},
},
},
},
} as OpenClawConfig;
return applySetupAccountConfigPatch({
cfg: next,
channelKey: "zalouser",
accountId,
patch: {},
});
},
},
messaging: {

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";