refactor(setup): reuse patched adapters in discord and signal

This commit is contained in:
Peter Steinberger
2026-03-17 03:46:56 +00:00
parent 4fd75e5fc8
commit 387d9fa7c4
2 changed files with 10 additions and 119 deletions

View File

@@ -1,9 +1,7 @@
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
import type { DiscordGuildEntry } from "../../../src/config/types.discord.js";
import {
applyAccountNameToChannelSection,
DEFAULT_ACCOUNT_ID,
migrateBaseNameToDefaultAccount,
normalizeAccountId,
noteChannelLookupFailure,
noteChannelLookupSummary,
parseMentionOrPrefixedId,
@@ -72,15 +70,8 @@ export function parseDiscordAllowFromId(value: string): string | null {
});
}
export const discordSetupAdapter: ChannelSetupAdapter = {
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
applyAccountName: ({ cfg, accountId, name }) =>
applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name,
}),
export const discordSetupAdapter: ChannelSetupAdapter = createPatchedAccountSetupAdapter({
channelKey: channel,
validateInput: ({ accountId, input }) => {
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
return "DISCORD_BOT_TOKEN can only be used for the default account.";
@@ -90,53 +81,8 @@ export const discordSetupAdapter: ChannelSetupAdapter = {
}
return null;
},
applyAccountConfig: ({ cfg, accountId, input }) => {
const namedConfig = applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name: input.name,
});
const next =
accountId !== DEFAULT_ACCOUNT_ID
? migrateBaseNameToDefaultAccount({
cfg: namedConfig,
channelKey: channel,
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
discord: {
...next.channels?.discord,
enabled: true,
...(input.useEnv ? {} : input.token ? { token: input.token } : {}),
},
},
};
}
return {
...next,
channels: {
...next.channels,
discord: {
...next.channels?.discord,
enabled: true,
accounts: {
...next.channels?.discord?.accounts,
[accountId]: {
...next.channels?.discord?.accounts?.[accountId],
enabled: true,
...(input.token ? { token: input.token } : {}),
},
},
},
},
};
},
};
buildPatch: (input) => (input.useEnv ? {} : input.token ? { token: input.token } : {}),
});
type DiscordAllowFromResolverParams = {
cfg: OpenClawConfig;

View File

@@ -1,8 +1,5 @@
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
import {
applyAccountNameToChannelSection,
DEFAULT_ACCOUNT_ID,
migrateBaseNameToDefaultAccount,
normalizeAccountId,
normalizeE164,
parseSetupEntriesAllowingWildcard,
promptParsedAllowFromForScopedChannel,
@@ -114,15 +111,8 @@ export async function promptSignalAllowFrom(params: {
});
}
export const signalSetupAdapter: ChannelSetupAdapter = {
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
applyAccountName: ({ cfg, accountId, name }) =>
applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name,
}),
export const signalSetupAdapter: ChannelSetupAdapter = createPatchedAccountSetupAdapter({
channelKey: channel,
validateInput: ({ input }) => {
if (
!input.signalNumber &&
@@ -135,53 +125,8 @@ export const signalSetupAdapter: ChannelSetupAdapter = {
}
return null;
},
applyAccountConfig: ({ cfg, accountId, input }) => {
const namedConfig = applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name: input.name,
});
const next =
accountId !== DEFAULT_ACCOUNT_ID
? migrateBaseNameToDefaultAccount({
cfg: namedConfig,
channelKey: channel,
})
: namedConfig;
if (accountId === DEFAULT_ACCOUNT_ID) {
return {
...next,
channels: {
...next.channels,
signal: {
...next.channels?.signal,
enabled: true,
...buildSignalSetupPatch(input),
},
},
};
}
return {
...next,
channels: {
...next.channels,
signal: {
...next.channels?.signal,
enabled: true,
accounts: {
...next.channels?.signal?.accounts,
[accountId]: {
...next.channels?.signal?.accounts?.[accountId],
enabled: true,
...buildSignalSetupPatch(input),
},
},
},
},
};
},
};
buildPatch: (input) => buildSignalSetupPatch(input),
});
type SignalSetupWizardHandlers = {
resolveStatusLines: NonNullable<ChannelSetupWizard["status"]>["resolveStatusLines"];