Skip to content

Commit 7ab0746

Browse files
committed
refactor(setup): share allowlist wizard proxies
1 parent 5ce2ed3 commit 7ab0746

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

extensions/discord/src/setup-core.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
2+
import { createAllowlistSetupWizardProxy } from "../../../src/channels/plugins/setup-wizard-proxy.js";
13
import type { DiscordGuildEntry } from "openclaw/plugin-sdk/config-runtime";
24
import {
35
applyAccountNameToChannelSection,
@@ -347,3 +349,13 @@ export function createDiscordSetupWizardProxy(
347349
disable: (cfg: OpenClawConfig) => setSetupChannelEnabled(cfg, channel, false),
348350
} satisfies ChannelSetupWizard;
349351
}
352+
export function createDiscordSetupWizardProxy(
353+
loadWizard: () => Promise<{ discordSetupWizard: ChannelSetupWizard }>,
354+
) {
355+
return createAllowlistSetupWizardProxy({
356+
loadWizard: async () => (await loadWizard()).discordSetupWizard,
357+
createBase: createDiscordSetupWizardBase,
358+
fallbackResolvedGroupAllowlist: (entries) =>
359+
entries.map((input) => ({ input, resolved: false })),
360+
});
361+
}

extensions/slack/src/setup-core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
2+
import { createAllowlistSetupWizardProxy } from "../../../src/channels/plugins/setup-wizard-proxy.js";
13
import {
24
applyAccountNameToChannelSection,
35
DEFAULT_ACCOUNT_ID,
@@ -390,3 +392,12 @@ export function createSlackSetupWizardProxy(
390392
disable: (cfg: OpenClawConfig) => setSetupChannelEnabled(cfg, channel, false),
391393
} satisfies ChannelSetupWizard;
392394
}
395+
export function createSlackSetupWizardProxy(
396+
loadWizard: () => Promise<{ slackSetupWizard: ChannelSetupWizard }>,
397+
) {
398+
return createAllowlistSetupWizardProxy({
399+
loadWizard: async () => (await loadWizard()).slackSetupWizard,
400+
createBase: createSlackSetupWizardBase,
401+
fallbackResolvedGroupAllowlist: (entries) => entries,
402+
});
403+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { OpenClawConfig } from "../../config/config.js";
2+
import type { ChannelSetupDmPolicy } from "./setup-wizard-types.js";
3+
import type { ChannelSetupWizard } from "./setup-wizard.js";
4+
5+
type PromptAllowFromParams = Parameters<NonNullable<ChannelSetupDmPolicy["promptAllowFrom"]>>[0];
6+
type ResolveAllowFromEntriesParams = Parameters<
7+
NonNullable<ChannelSetupWizard["allowFrom"]>["resolveEntries"]
8+
>[0];
9+
type ResolveAllowFromEntriesResult = Awaited<
10+
ReturnType<NonNullable<ChannelSetupWizard["allowFrom"]>["resolveEntries"]>
11+
>;
12+
type ResolveGroupAllowlistParams = Parameters<
13+
NonNullable<NonNullable<ChannelSetupWizard["groupAccess"]>["resolveAllowlist"]>
14+
>[0];
15+
16+
export function createAllowlistSetupWizardProxy<TGroupResolved>(params: {
17+
loadWizard: () => Promise<ChannelSetupWizard>;
18+
createBase: (handlers: {
19+
promptAllowFrom: (params: PromptAllowFromParams) => Promise<OpenClawConfig>;
20+
resolveAllowFromEntries: (
21+
params: ResolveAllowFromEntriesParams,
22+
) => Promise<ResolveAllowFromEntriesResult>;
23+
resolveGroupAllowlist: (params: ResolveGroupAllowlistParams) => Promise<TGroupResolved>;
24+
}) => ChannelSetupWizard;
25+
fallbackResolvedGroupAllowlist: (entries: string[]) => TGroupResolved;
26+
}) {
27+
return params.createBase({
28+
promptAllowFrom: async ({ cfg, prompter, accountId }) => {
29+
const wizard = await params.loadWizard();
30+
if (!wizard.dmPolicy?.promptAllowFrom) {
31+
return cfg;
32+
}
33+
return await wizard.dmPolicy.promptAllowFrom({ cfg, prompter, accountId });
34+
},
35+
resolveAllowFromEntries: async ({ cfg, accountId, credentialValues, entries }) => {
36+
const wizard = await params.loadWizard();
37+
if (!wizard.allowFrom) {
38+
return entries.map((input) => ({ input, resolved: false, id: null }));
39+
}
40+
return await wizard.allowFrom.resolveEntries({
41+
cfg,
42+
accountId,
43+
credentialValues,
44+
entries,
45+
});
46+
},
47+
resolveGroupAllowlist: async ({ cfg, accountId, credentialValues, entries, prompter }) => {
48+
const wizard = await params.loadWizard();
49+
if (!wizard.groupAccess?.resolveAllowlist) {
50+
return params.fallbackResolvedGroupAllowlist(entries);
51+
}
52+
return (await wizard.groupAccess.resolveAllowlist({
53+
cfg,
54+
accountId,
55+
credentialValues,
56+
entries,
57+
prompter,
58+
})) as TGroupResolved;
59+
},
60+
});
61+
}

0 commit comments

Comments
 (0)