refactor: unify onboarding dm/group policy scaffolding

This commit is contained in:
Peter Steinberger
2026-03-07 21:38:27 +00:00
parent fecca6fd8d
commit 6b1c82c4f1
20 changed files with 239 additions and 169 deletions

View File

@@ -7,11 +7,11 @@ import type {
} from "openclaw/plugin-sdk/bluebubbles";
import {
DEFAULT_ACCOUNT_ID,
addWildcardAllowFrom,
formatDocsLink,
mergeAllowFromEntries,
normalizeAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "openclaw/plugin-sdk/bluebubbles";
import {
listBlueBubblesAccountIds,
@@ -26,19 +26,11 @@ import { normalizeBlueBubblesServerUrl } from "./types.js";
const channel = "bluebubbles" as const;
function setBlueBubblesDmPolicy(cfg: OpenClawConfig, dmPolicy: DmPolicy): OpenClawConfig {
const allowFrom =
dmPolicy === "open" ? addWildcardAllowFrom(cfg.channels?.bluebubbles?.allowFrom) : undefined;
return {
...cfg,
channels: {
...cfg.channels,
bluebubbles: {
...cfg.channels?.bluebubbles,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
};
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "bluebubbles",
dmPolicy,
});
}
function setBlueBubblesAllowFrom(

View File

@@ -7,11 +7,13 @@ import type {
WizardPrompter,
} from "openclaw/plugin-sdk/feishu";
import {
addWildcardAllowFrom,
DEFAULT_ACCOUNT_ID,
formatDocsLink,
hasConfiguredSecretInput,
promptSingleChannelSecretInput,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
} from "openclaw/plugin-sdk/feishu";
import { resolveFeishuCredentials } from "./accounts.js";
import { probeFeishu } from "./probe.js";
@@ -28,34 +30,19 @@ function normalizeString(value: unknown): string | undefined {
}
function setFeishuDmPolicy(cfg: ClawdbotConfig, dmPolicy: DmPolicy): ClawdbotConfig {
const allowFrom =
dmPolicy === "open"
? addWildcardAllowFrom(cfg.channels?.feishu?.allowFrom)?.map((entry) => String(entry))
: undefined;
return {
...cfg,
channels: {
...cfg.channels,
feishu: {
...cfg.channels?.feishu,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
};
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "feishu",
dmPolicy,
}) as ClawdbotConfig;
}
function setFeishuAllowFrom(cfg: ClawdbotConfig, allowFrom: string[]): ClawdbotConfig {
return {
...cfg,
channels: {
...cfg.channels,
feishu: {
...cfg.channels?.feishu,
allowFrom,
},
},
};
return setTopLevelChannelAllowFrom({
cfg,
channel: "feishu",
allowFrom,
}) as ClawdbotConfig;
}
function parseAllowFromInput(raw: string): string[] {
@@ -137,17 +124,12 @@ function setFeishuGroupPolicy(
cfg: ClawdbotConfig,
groupPolicy: "open" | "allowlist" | "disabled",
): ClawdbotConfig {
return {
...cfg,
channels: {
...cfg.channels,
feishu: {
...cfg.channels?.feishu,
enabled: true,
groupPolicy,
},
},
};
return setTopLevelChannelGroupPolicy({
cfg,
channel: "feishu",
groupPolicy,
enabled: true,
}) as ClawdbotConfig;
}
function setFeishuGroupAllowFrom(cfg: ClawdbotConfig, groupAllowFrom: string[]): ClawdbotConfig {

View File

@@ -1,9 +1,10 @@
import {
addWildcardAllowFrom,
DEFAULT_ACCOUNT_ID,
formatDocsLink,
promptChannelAccessConfig,
resolveAccountIdForConfigure,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
type ChannelOnboardingAdapter,
type ChannelOnboardingDmPolicy,
type DmPolicy,
@@ -90,32 +91,19 @@ function updateIrcAccountConfig(
}
function setIrcDmPolicy(cfg: CoreConfig, dmPolicy: DmPolicy): CoreConfig {
const allowFrom =
dmPolicy === "open" ? addWildcardAllowFrom(cfg.channels?.irc?.allowFrom) : undefined;
return {
...cfg,
channels: {
...cfg.channels,
irc: {
...cfg.channels?.irc,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
};
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "irc",
dmPolicy,
}) as CoreConfig;
}
function setIrcAllowFrom(cfg: CoreConfig, allowFrom: string[]): CoreConfig {
return {
...cfg,
channels: {
...cfg.channels,
irc: {
...cfg.channels?.irc,
allowFrom,
},
},
};
return setTopLevelChannelAllowFrom({
cfg,
channel: "irc",
allowFrom,
}) as CoreConfig;
}
function setIrcNickServ(

View File

@@ -7,6 +7,7 @@ import {
mergeAllowFromEntries,
promptSingleChannelSecretInput,
promptChannelAccessConfig,
setTopLevelChannelGroupPolicy,
type SecretInput,
type ChannelOnboardingAdapter,
type ChannelOnboardingDmPolicy,
@@ -143,17 +144,12 @@ async function promptMatrixAllowFrom(params: {
}
function setMatrixGroupPolicy(cfg: CoreConfig, groupPolicy: "open" | "allowlist" | "disabled") {
return {
...cfg,
channels: {
...cfg.channels,
matrix: {
...cfg.channels?.matrix,
enabled: true,
groupPolicy,
},
},
};
return setTopLevelChannelGroupPolicy({
cfg,
channel: "matrix",
groupPolicy,
enabled: true,
}) as CoreConfig;
}
function setMatrixGroupRooms(cfg: CoreConfig, roomKeys: string[]) {

View File

@@ -7,11 +7,13 @@ import type {
MSTeamsTeamConfig,
} from "openclaw/plugin-sdk/msteams";
import {
addWildcardAllowFrom,
DEFAULT_ACCOUNT_ID,
formatDocsLink,
mergeAllowFromEntries,
promptChannelAccessConfig,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
} from "openclaw/plugin-sdk/msteams";
import {
parseMSTeamsTeamEntry,
@@ -24,34 +26,19 @@ import { hasConfiguredMSTeamsCredentials, resolveMSTeamsCredentials } from "./to
const channel = "msteams" as const;
function setMSTeamsDmPolicy(cfg: OpenClawConfig, dmPolicy: DmPolicy) {
const allowFrom =
dmPolicy === "open"
? addWildcardAllowFrom(cfg.channels?.msteams?.allowFrom)?.map((entry) => String(entry))
: undefined;
return {
...cfg,
channels: {
...cfg.channels,
msteams: {
...cfg.channels?.msteams,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
};
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "msteams",
dmPolicy,
});
}
function setMSTeamsAllowFrom(cfg: OpenClawConfig, allowFrom: string[]): OpenClawConfig {
return {
...cfg,
channels: {
...cfg.channels,
msteams: {
...cfg.channels?.msteams,
allowFrom,
},
},
};
return setTopLevelChannelAllowFrom({
cfg,
channel: "msteams",
allowFrom,
});
}
function parseAllowFromInput(raw: string): string[] {
@@ -171,17 +158,12 @@ function setMSTeamsGroupPolicy(
cfg: OpenClawConfig,
groupPolicy: "open" | "allowlist" | "disabled",
): OpenClawConfig {
return {
...cfg,
channels: {
...cfg.channels,
msteams: {
...cfg.channels?.msteams,
enabled: true,
groupPolicy,
},
},
};
return setTopLevelChannelGroupPolicy({
cfg,
channel: "msteams",
groupPolicy,
enabled: true,
});
}
function setMSTeamsTeamsAllowlist(

View File

@@ -1,5 +1,4 @@
import {
addWildcardAllowFrom,
formatDocsLink,
hasConfiguredSecretInput,
mergeAllowFromEntries,
@@ -7,6 +6,7 @@ import {
resolveAccountIdForConfigure,
DEFAULT_ACCOUNT_ID,
normalizeAccountId,
setTopLevelChannelDmPolicyWithAllowFrom,
type SecretInput,
type ChannelOnboardingAdapter,
type ChannelOnboardingDmPolicy,
@@ -23,24 +23,13 @@ import type { CoreConfig, DmPolicy } from "./types.js";
const channel = "nextcloud-talk" as const;
function setNextcloudTalkDmPolicy(cfg: CoreConfig, dmPolicy: DmPolicy): CoreConfig {
const existingConfig = cfg.channels?.["nextcloud-talk"];
const existingAllowFrom: string[] = (existingConfig?.allowFrom ?? []).map((x) => String(x));
const allowFrom: string[] =
dmPolicy === "open" ? (addWildcardAllowFrom(existingAllowFrom) as string[]) : existingAllowFrom;
const newNextcloudTalkConfig = {
...existingConfig,
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "nextcloud-talk",
dmPolicy,
allowFrom,
};
return {
...cfg,
channels: {
...cfg.channels,
"nextcloud-talk": newNextcloudTalkConfig,
},
} as CoreConfig;
getAllowFrom: (inputCfg) =>
(inputCfg.channels?.["nextcloud-talk"]?.allowFrom ?? []).map((entry) => String(entry)),
}) as CoreConfig;
}
function setNextcloudTalkAccountConfig(

View File

@@ -6,13 +6,13 @@ import type {
WizardPrompter,
} from "openclaw/plugin-sdk/zalo";
import {
addWildcardAllowFrom,
DEFAULT_ACCOUNT_ID,
hasConfiguredSecretInput,
mergeAllowFromEntries,
normalizeAccountId,
promptSingleChannelSecretInput,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "openclaw/plugin-sdk/zalo";
import { listZaloAccountIds, resolveDefaultZaloAccountId, resolveZaloAccount } from "./accounts.js";
@@ -24,19 +24,11 @@ function setZaloDmPolicy(
cfg: OpenClawConfig,
dmPolicy: "pairing" | "allowlist" | "open" | "disabled",
) {
const allowFrom =
dmPolicy === "open" ? addWildcardAllowFrom(cfg.channels?.zalo?.allowFrom) : undefined;
return {
...cfg,
channels: {
...cfg.channels,
zalo: {
...cfg.channels?.zalo,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
} as OpenClawConfig;
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "zalo",
dmPolicy,
}) as OpenClawConfig;
}
function setZaloUpdateMode(

View File

@@ -5,13 +5,13 @@ import type {
WizardPrompter,
} from "openclaw/plugin-sdk/zalouser";
import {
addWildcardAllowFrom,
DEFAULT_ACCOUNT_ID,
formatResolvedUnresolvedNote,
mergeAllowFromEntries,
normalizeAccountId,
promptChannelAccessConfig,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "openclaw/plugin-sdk/zalouser";
import {
listZalouserAccountIds,
@@ -73,19 +73,11 @@ function setZalouserDmPolicy(
cfg: OpenClawConfig,
dmPolicy: "pairing" | "allowlist" | "open" | "disabled",
): OpenClawConfig {
const allowFrom =
dmPolicy === "open" ? addWildcardAllowFrom(cfg.channels?.zalouser?.allowFrom) : undefined;
return {
...cfg,
channels: {
...cfg.channels,
zalouser: {
...cfg.channels?.zalouser,
dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
} as OpenClawConfig;
return setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "zalouser",
dmPolicy,
}) as OpenClawConfig;
}
async function noteZalouserHelp(prompter: WizardPrompter): Promise<void> {

View File

@@ -27,6 +27,9 @@ import {
setAccountAllowFromForChannel,
setAccountGroupPolicyForChannel,
setChannelDmPolicyWithAllowFrom,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
setLegacyChannelAllowFrom,
setLegacyChannelDmPolicyWithAllowFrom,
setOnboardingChannelEnabled,
@@ -913,6 +916,73 @@ describe("setChannelDmPolicyWithAllowFrom", () => {
});
});
describe("setTopLevelChannelDmPolicyWithAllowFrom", () => {
it("adds wildcard allowFrom for open policy", () => {
const cfg: OpenClawConfig = {
channels: {
zalo: {
dmPolicy: "pairing",
allowFrom: ["12345"],
},
},
};
const next = setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "zalo",
dmPolicy: "open",
});
expect(next.channels?.zalo?.dmPolicy).toBe("open");
expect(next.channels?.zalo?.allowFrom).toEqual(["12345", "*"]);
});
it("supports custom allowFrom lookup callback", () => {
const cfg: OpenClawConfig = {
channels: {
"nextcloud-talk": {
dmPolicy: "pairing",
allowFrom: ["alice"],
},
},
};
const next = setTopLevelChannelDmPolicyWithAllowFrom({
cfg,
channel: "nextcloud-talk",
dmPolicy: "open",
getAllowFrom: (inputCfg) =>
(inputCfg.channels?.["nextcloud-talk"]?.allowFrom ?? []).map((entry) => String(entry)),
});
expect(next.channels?.["nextcloud-talk"]?.allowFrom).toEqual(["alice", "*"]);
});
});
describe("setTopLevelChannelAllowFrom", () => {
it("writes allowFrom and can force enabled state", () => {
const next = setTopLevelChannelAllowFrom({
cfg: {},
channel: "msteams",
allowFrom: ["user-1"],
enabled: true,
});
expect(next.channels?.msteams?.allowFrom).toEqual(["user-1"]);
expect(next.channels?.msteams?.enabled).toBe(true);
});
});
describe("setTopLevelChannelGroupPolicy", () => {
it("writes groupPolicy and can force enabled state", () => {
const next = setTopLevelChannelGroupPolicy({
cfg: {},
channel: "feishu",
groupPolicy: "allowlist",
enabled: true,
});
expect(next.channels?.feishu?.groupPolicy).toBe("allowlist");
expect(next.channels?.feishu?.enabled).toBe(true);
});
});
describe("splitOnboardingEntries", () => {
it("splits comma/newline/semicolon input and trims blanks", () => {
expect(splitOnboardingEntries(" alice, bob \ncarol; ;\n")).toEqual(["alice", "bob", "carol"]);

View File

@@ -161,6 +161,75 @@ export function setAccountAllowFromForChannel(params: {
});
}
export function setTopLevelChannelAllowFrom(params: {
cfg: OpenClawConfig;
channel: string;
allowFrom: string[];
enabled?: boolean;
}): OpenClawConfig {
const channelConfig =
(params.cfg.channels?.[params.channel] as Record<string, unknown> | undefined) ?? {};
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channel]: {
...channelConfig,
...(params.enabled ? { enabled: true } : {}),
allowFrom: params.allowFrom,
},
},
};
}
export function setTopLevelChannelDmPolicyWithAllowFrom(params: {
cfg: OpenClawConfig;
channel: string;
dmPolicy: DmPolicy;
getAllowFrom?: (cfg: OpenClawConfig) => Array<string | number> | undefined;
}): OpenClawConfig {
const channelConfig =
(params.cfg.channels?.[params.channel] as Record<string, unknown> | undefined) ?? {};
const existingAllowFrom =
params.getAllowFrom?.(params.cfg) ??
(channelConfig.allowFrom as Array<string | number> | undefined) ??
undefined;
const allowFrom =
params.dmPolicy === "open" ? addWildcardAllowFrom(existingAllowFrom) : undefined;
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channel]: {
...channelConfig,
dmPolicy: params.dmPolicy,
...(allowFrom ? { allowFrom } : {}),
},
},
};
}
export function setTopLevelChannelGroupPolicy(params: {
cfg: OpenClawConfig;
channel: string;
groupPolicy: GroupPolicy;
enabled?: boolean;
}): OpenClawConfig {
const channelConfig =
(params.cfg.channels?.[params.channel] as Record<string, unknown> | undefined) ?? {};
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.channel]: {
...channelConfig,
...(params.enabled ? { enabled: true } : {}),
groupPolicy: params.groupPolicy,
},
},
};
}
export function setChannelDmPolicyWithAllowFrom(params: {
cfg: OpenClawConfig;
channel: "imessage" | "signal" | "telegram";

View File

@@ -40,6 +40,7 @@ export {
mergeAllowFromEntries,
promptAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {

View File

@@ -18,6 +18,9 @@ export type {
export {
addWildcardAllowFrom,
promptSingleChannelSecretInput,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export type {

View File

@@ -32,6 +32,7 @@ export {
mergeAllowFromEntries,
promptAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {

View File

@@ -533,6 +533,10 @@ export {
addWildcardAllowFrom,
mergeAllowFromEntries,
promptAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
} from "../channels/plugins/onboarding/helpers.js";
export { promptChannelAccessConfig } from "../channels/plugins/onboarding/channel-access.js";

View File

@@ -19,6 +19,8 @@ export {
addWildcardAllowFrom,
promptAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export type { BaseProbeResult } from "../channels/plugins/types.js";

View File

@@ -36,6 +36,7 @@ export {
addWildcardAllowFrom,
mergeAllowFromEntries,
promptSingleChannelSecretInput,
setTopLevelChannelGroupPolicy,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export { applyAccountNameToChannelSection } from "../channels/plugins/setup-helpers.js";

View File

@@ -37,6 +37,9 @@ export { promptChannelAccessConfig } from "../channels/plugins/onboarding/channe
export {
addWildcardAllowFrom,
mergeAllowFromEntries,
setTopLevelChannelAllowFrom,
setTopLevelChannelDmPolicyWithAllowFrom,
setTopLevelChannelGroupPolicy,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export type {

View File

@@ -27,6 +27,7 @@ export {
promptAccountId,
promptSingleChannelSecretInput,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export { applyAccountNameToChannelSection } from "../channels/plugins/setup-helpers.js";
export { createAccountListHelpers } from "../channels/plugins/account-helpers.js";

View File

@@ -21,6 +21,7 @@ export {
promptAccountId,
promptSingleChannelSecretInput,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {

View File

@@ -21,6 +21,7 @@ export {
mergeAllowFromEntries,
promptAccountId,
resolveAccountIdForConfigure,
setTopLevelChannelDmPolicyWithAllowFrom,
} from "../channels/plugins/onboarding/helpers.js";
export {
applyAccountNameToChannelSection,