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