mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 00:20:22 +00:00
refactor: share account config merge helper
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { createAccountListHelpers } from "./account-helpers.js";
|
||||
import { createAccountListHelpers, mergeAccountConfig } from "./account-helpers.js";
|
||||
|
||||
const { listConfiguredAccountIds, listAccountIds, resolveDefaultAccountId } =
|
||||
createAccountListHelpers("testchannel");
|
||||
@@ -109,3 +109,50 @@ describe("createAccountListHelpers", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("mergeAccountConfig", () => {
|
||||
it("drops accounts from the base config before merging", () => {
|
||||
const merged = mergeAccountConfig<{
|
||||
enabled?: boolean;
|
||||
name?: string;
|
||||
accounts?: Record<string, { name?: string }>;
|
||||
}>({
|
||||
channelConfig: {
|
||||
enabled: true,
|
||||
accounts: {
|
||||
work: { name: "Work" },
|
||||
},
|
||||
},
|
||||
accountConfig: {
|
||||
name: "Work",
|
||||
},
|
||||
});
|
||||
|
||||
expect(merged).toEqual({
|
||||
enabled: true,
|
||||
name: "Work",
|
||||
});
|
||||
});
|
||||
|
||||
it("drops caller-specified keys from the base config before merging", () => {
|
||||
const merged = mergeAccountConfig<{
|
||||
enabled?: boolean;
|
||||
defaultAccount?: string;
|
||||
name?: string;
|
||||
}>({
|
||||
channelConfig: {
|
||||
enabled: true,
|
||||
defaultAccount: "work",
|
||||
},
|
||||
accountConfig: {
|
||||
name: "Work",
|
||||
},
|
||||
omitKeys: ["defaultAccount"],
|
||||
});
|
||||
|
||||
expect(merged).toEqual({
|
||||
enabled: true,
|
||||
name: "Work",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -60,3 +60,20 @@ export function createAccountListHelpers(
|
||||
|
||||
return { listConfiguredAccountIds, listAccountIds, resolveDefaultAccountId };
|
||||
}
|
||||
|
||||
export function mergeAccountConfig<TConfig extends Record<string, unknown>>(params: {
|
||||
channelConfig: TConfig | undefined;
|
||||
accountConfig: Partial<TConfig> | undefined;
|
||||
omitKeys?: string[];
|
||||
}): TConfig {
|
||||
const omitKeys = new Set(["accounts", ...(params.omitKeys ?? [])]);
|
||||
const base = Object.fromEntries(
|
||||
Object.entries((params.channelConfig ?? {}) as Record<string, unknown>).filter(
|
||||
([key]) => !omitKeys.has(key),
|
||||
),
|
||||
) as TConfig;
|
||||
return {
|
||||
...base,
|
||||
...params.accountConfig,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";
|
||||
export {
|
||||
createAccountListHelpers,
|
||||
mergeAccountConfig,
|
||||
} from "../channels/plugins/account-helpers.js";
|
||||
export { createAccountActionGate } from "../channels/plugins/account-action-gate.js";
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
export type { OpenClawConfig } from "../config/config.js";
|
||||
|
||||
export { createAccountActionGate } from "../channels/plugins/account-action-gate.js";
|
||||
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";
|
||||
export {
|
||||
createAccountListHelpers,
|
||||
mergeAccountConfig,
|
||||
} from "../channels/plugins/account-helpers.js";
|
||||
export { normalizeChatType } from "../channels/chat-type.js";
|
||||
export { resolveAccountEntry } from "../routing/account-lookup.js";
|
||||
export {
|
||||
|
||||
Reference in New Issue
Block a user