refactor: centralize WhatsApp config merging

This commit is contained in:
Peter Steinberger
2026-01-08 06:27:13 +00:00
parent e09d44e63a
commit c9e07616c7
3 changed files with 67 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import {
mergeWhatsAppConfig,
setWhatsAppAllowFrom,
setWhatsAppDmPolicy,
setWhatsAppSelfChatMode,
@@ -54,4 +55,27 @@ describe("onboard-providers WhatsApp setters", () => {
expect(next.whatsapp?.selfChatMode).toBe(true);
expect(next.whatsapp?.allowFrom).toEqual(["+15555550123"]);
});
it("merges WhatsApp config without clobbering fields", () => {
const cfg: ClawdbotConfig = {
whatsapp: {
dmPolicy: "pairing",
allowFrom: ["*"],
},
};
const merged = mergeWhatsAppConfig(cfg, {
dmPolicy: "open",
allowFrom: undefined,
});
const cleared = mergeWhatsAppConfig(
cfg,
{ allowFrom: undefined },
{ unsetOnUndefined: ["allowFrom"] },
);
expect(merged.whatsapp?.dmPolicy).toBe("open");
expect(merged.whatsapp?.allowFrom).toEqual(["*"]);
expect(cleared.whatsapp?.allowFrom).toBeUndefined();
});
});

View File

@@ -1,7 +1,8 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { ClawdbotConfig } from "../config/config.js";
import type { DmPolicy, WhatsAppConfig } from "../config/types.js";
import { mergeWhatsAppConfig } from "../config/merge-config.js";
import type { DmPolicy } from "../config/types.js";
import {
listDiscordAccountIds,
resolveDefaultDiscordAccountId,
@@ -249,20 +250,7 @@ async function noteSlackTokenHelp(
);
}
export function mergeWhatsAppConfig(
cfg: ClawdbotConfig,
patch: Partial<WhatsAppConfig>,
): ClawdbotConfig {
const base = cfg.whatsapp ?? {};
return {
...cfg,
whatsapp: {
selfChatMode: base.selfChatMode,
...base,
...patch,
},
};
}
export { mergeWhatsAppConfig };
export function setWhatsAppDmPolicy(
cfg: ClawdbotConfig,
@@ -275,7 +263,11 @@ export function setWhatsAppAllowFrom(
cfg: ClawdbotConfig,
allowFrom?: string[],
): ClawdbotConfig {
return mergeWhatsAppConfig(cfg, { allowFrom });
return mergeWhatsAppConfig(
cfg,
{ allowFrom },
{ unsetOnUndefined: ["allowFrom"] },
);
}
function setMessagesResponsePrefix(

View File

@@ -0,0 +1,35 @@
import type { ClawdbotConfig } from "./config.js";
import type { WhatsAppConfig } from "./types.js";
export type MergeSectionOptions<T> = {
unsetOnUndefined?: Array<keyof T>;
};
export function mergeConfigSection<T extends Record<string, unknown>>(
base: T | undefined,
patch: Partial<T>,
options: MergeSectionOptions<T> = {},
): T {
const next: Record<string, unknown> = { ...(base ?? {}) };
for (const [key, value] of Object.entries(patch) as [keyof T, T[keyof T]][]) {
if (value === undefined) {
if (options.unsetOnUndefined?.includes(key)) {
delete next[key as string];
}
continue;
}
next[key as string] = value as unknown;
}
return next as T;
}
export function mergeWhatsAppConfig(
cfg: ClawdbotConfig,
patch: Partial<WhatsAppConfig>,
options?: MergeSectionOptions<WhatsAppConfig>,
): ClawdbotConfig {
return {
...cfg,
whatsapp: mergeConfigSection(cfg.whatsapp, patch, options),
};
}