Skip to content

Commit 09e2a1a

Browse files
committed
refactor(channels): unify setup ownership across bundled channels
1 parent 7e78de7 commit 09e2a1a

8 files changed

Lines changed: 173 additions & 401 deletions

File tree

‎extensions/matrix/src/channel.setup.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const matrixSetupWizard = createMatrixSetupWizardProxy(async () => ({
1010
matrixSetupWizard: (await import("./setup-surface.js")).matrixSetupWizard,
1111
}));
1212

13-
export const matrixSetupPlugin: ChannelPlugin<ResolvedMatrixAccount> = {
13+
export const matrixPluginBase = {
1414
id: "matrix",
1515
meta: {
1616
id: "matrix",
@@ -44,6 +44,13 @@ export const matrixSetupPlugin: ChannelPlugin<ResolvedMatrixAccount> = {
4444
baseUrl: account.homeserver,
4545
},
4646
}),
47+
},
48+
} satisfies ChannelPlugin<ResolvedMatrixAccount>;
49+
50+
export const matrixSetupPlugin: ChannelPlugin<ResolvedMatrixAccount> = {
51+
...matrixPluginBase,
52+
config: {
53+
...matrixPluginBase.config,
4754
hasConfiguredState: ({ cfg }) => resolveMatrixAccount({ cfg }).configured,
4855
},
4956
};

‎extensions/matrix/src/channel.ts‎

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Matrix plugin module implements channel behavior.
2-
import { describeAccountSnapshot } from "openclaw/plugin-sdk/account-helpers";
32
import {
43
adaptScopedAccountAccessor,
54
createScopedDmSecurityResolver,
@@ -45,8 +44,8 @@ import {
4544
import { matrixMessageActions } from "./actions.js";
4645
import { matrixApprovalCapability } from "./approval-native.js";
4746
import { createMatrixPairingText, createMatrixProbeAccount } from "./channel-account-paths.js";
48-
import { DEFAULT_ACCOUNT_ID, matrixConfigAdapter } from "./config-adapter.js";
49-
import { MatrixChannelConfigSchema } from "./config-schema.js";
47+
import { matrixPluginBase } from "./channel.setup.js";
48+
import { DEFAULT_ACCOUNT_ID } from "./config-adapter.js";
5049
import {
5150
legacyConfigRules as MATRIX_LEGACY_CONFIG_RULES,
5251
normalizeCompatibilityConfig as normalizeMatrixCompatibilityConfig,
@@ -75,12 +74,6 @@ import {
7574
import { matrixResolverAdapter } from "./resolver.js";
7675
import { collectRuntimeConfigAssignments, secretTargetRegistryEntries } from "./secret-contract.js";
7776
import { resolveMatrixOutboundSessionRoute } from "./session-route.js";
78-
import {
79-
namedAccountPromotionKeys,
80-
resolveSingleAccountPromotionTarget,
81-
singleAccountKeysToMove,
82-
} from "./setup-contract.js";
83-
import { createMatrixSetupWizardProxy, matrixSetupContract } from "./setup-core.js";
8477
import {
8578
defaultTopLevelPlacement,
8679
resolveMatrixInboundConversation,
@@ -89,29 +82,13 @@ import type { CoreConfig } from "./types.js";
8982
// Mutex for serializing account startup (workaround for concurrent dynamic import race condition)
9083
let matrixStartupLock: Promise<void> = Promise.resolve();
9184

92-
const loadMatrixSetupWizard = createLazyRuntimeNamedExport(
93-
() => import("./setup-surface.js"),
94-
"matrixSetupWizard",
95-
);
9685
const loadMatrixChannelRuntime = createLazyRuntimeNamedExport(
9786
() => import("./channel.runtime.js"),
9887
"matrixChannelRuntime",
9988
);
10089

10190
const loadMatrixDoctorModule = createLazyRuntimeModule(() => import("./doctor.js"));
10291

103-
const meta = {
104-
id: "matrix",
105-
label: "Matrix",
106-
selectionLabel: "Matrix (plugin)",
107-
docsPath: "/channels/matrix",
108-
docsLabel: "matrix",
109-
blurb: "open protocol; configure a homeserver + access token.",
110-
order: 70,
111-
markdownCapable: true,
112-
quickstartAllowFrom: true,
113-
};
114-
11592
function buildMatrixTrafficStatusSummary(
11693
snapshot?: {
11794
lastInboundAt?: number | null;
@@ -439,37 +416,16 @@ const matrixMessageAdapter = createChannelMessageAdapterFromOutbound({
439416
export const matrixPlugin: ChannelPlugin<ResolvedMatrixAccount, MatrixProbe> =
440417
createChatChannelPlugin<ResolvedMatrixAccount, MatrixProbe>({
441418
base: {
442-
id: "matrix",
443-
meta,
444-
setupWizard: createMatrixSetupWizardProxy(async () => ({
445-
matrixSetupWizard: await loadMatrixSetupWizard(),
446-
})),
419+
...matrixPluginBase,
420+
meta: { ...matrixPluginBase.meta, markdownCapable: true },
447421
capabilities: {
448-
chatTypes: ["direct", "group", "thread"],
449-
polls: true,
450-
reactions: true,
451-
threads: true,
452-
media: true,
422+
...matrixPluginBase.capabilities,
453423
tts: {
454424
voice: {
455425
synthesisTarget: "voice-note",
456426
},
457427
},
458428
},
459-
reload: { configPrefixes: ["channels.matrix"] },
460-
configSchema: MatrixChannelConfigSchema,
461-
config: {
462-
...matrixConfigAdapter,
463-
isConfigured: (account) => account.configured,
464-
describeAccount: (account) =>
465-
describeAccountSnapshot({
466-
account,
467-
configured: account.configured,
468-
extra: {
469-
baseUrl: account.homeserver,
470-
},
471-
}),
472-
},
473429
approvalCapability: matrixApprovalCapability,
474430
groups: {
475431
resolveRequireMention: resolveMatrixGroupRequireMention,
@@ -540,12 +496,6 @@ export const matrixPlugin: ChannelPlugin<ResolvedMatrixAccount, MatrixProbe> =
540496
secretTargetRegistryEntries,
541497
collectRuntimeConfigAssignments,
542498
},
543-
setupContract: {
544-
...matrixSetupContract,
545-
singleAccountKeysToMove,
546-
namedAccountPromotionKeys,
547-
resolveSingleAccountPromotionTarget,
548-
},
549499
bindings: {
550500
compileConfiguredBinding: ({ conversationId }) =>
551501
normalizeMatrixAcpConversationId(conversationId),

‎extensions/nostr/src/channel.setup.ts‎

Lines changed: 19 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,28 @@ import { describeAccountSnapshot } from "openclaw/plugin-sdk/account-helpers";
33
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
44
import {
55
createDelegatedSetupWizardProxy,
6-
createStandardChannelSetupStatus,
76
DEFAULT_ACCOUNT_ID,
8-
createSetupTranslator,
97
} from "openclaw/plugin-sdk/setup-runtime";
108
import { buildChannelConfigSchema, type ChannelPlugin } from "./channel-api.js";
119
import { NostrConfigSchema } from "./config-schema.js";
1210
import { DEFAULT_RELAYS } from "./default-relays.js";
13-
import { createNostrSetupAdapter, createNostrSetupContract } from "./setup-adapter.js";
14-
15-
const t = createSetupTranslator();
11+
import {
12+
createNostrSetupAdapter,
13+
createNostrSetupContract,
14+
createNostrSetupStatus,
15+
} from "./setup-adapter.js";
16+
import type { ResolvedNostrAccount } from "./types.js";
1617

1718
const channel = "nostr" as const;
1819

19-
type NostrAccountConfig = {
20-
enabled?: boolean;
21-
name?: string;
22-
defaultAccount?: string;
23-
privateKey?: unknown;
24-
relays?: string[];
25-
dmPolicy?: "pairing" | "allowlist" | "open" | "disabled";
26-
allowFrom?: Array<string | number>;
27-
profile?: unknown;
28-
};
29-
30-
type ResolvedNostrSetupAccount = {
31-
accountId: string;
32-
name?: string;
33-
enabled: boolean;
34-
configured: boolean;
35-
privateKey: string;
36-
publicKey: string;
37-
relays: string[];
38-
profile?: unknown;
39-
config: NostrAccountConfig;
40-
};
20+
type NostrAccountConfig = ResolvedNostrAccount["config"];
4121

4222
function getNostrConfig(cfg: OpenClawConfig): NostrAccountConfig | undefined {
4323
return (cfg.channels as Record<string, unknown> | undefined)?.nostr as
4424
| NostrAccountConfig
4525
| undefined;
4626
}
4727

48-
function listSetupNostrAccountIds(cfg: OpenClawConfig): string[] {
49-
const nostrCfg = getNostrConfig(cfg);
50-
const privateKey = typeof nostrCfg?.privateKey === "string" ? nostrCfg.privateKey.trim() : "";
51-
if (!privateKey) {
52-
return [];
53-
}
54-
return [resolveDefaultSetupNostrAccountId(cfg)];
55-
}
56-
5728
function resolveDefaultSetupNostrAccountId(cfg: OpenClawConfig): string {
5829
const configured = getNostrConfig(cfg)?.defaultAccount;
5930
return typeof configured === "string" && configured.trim()
@@ -64,7 +35,7 @@ function resolveDefaultSetupNostrAccountId(cfg: OpenClawConfig): string {
6435
function resolveSetupNostrAccount(params: {
6536
cfg: OpenClawConfig;
6637
accountId?: string | null;
67-
}): ResolvedNostrSetupAccount {
38+
}): ResolvedNostrAccount {
6839
const nostrCfg = getNostrConfig(params.cfg);
6940
const accountId = params.accountId?.trim() || resolveDefaultSetupNostrAccountId(params.cfg);
7041
const privateKey = typeof nostrCfg?.privateKey === "string" ? nostrCfg.privateKey.trim() : "";
@@ -90,47 +61,16 @@ function resolveSetupNostrAccount(params: {
9061
};
9162
}
9263

93-
function looksLikeNostrPrivateKey(privateKey: string): boolean {
94-
return (
95-
privateKey.startsWith("nsec1") ||
96-
privateKey.startsWith("NSEC1") ||
97-
/^[0-9a-fA-F]{64}$/.test(privateKey)
98-
);
99-
}
100-
101-
const nostrSetupAdapter = createNostrSetupAdapter({
102-
resolveAccountId: (cfg, accountId) => accountId?.trim() || resolveDefaultSetupNostrAccountId(cfg),
103-
validatePrivateKey: looksLikeNostrPrivateKey,
104-
});
105-
const nostrSetupContract = createNostrSetupContract(nostrSetupAdapter);
106-
10764
const nostrSetupWizard = createDelegatedSetupWizardProxy({
10865
channel,
10966
loadWizard: async () => (await import("./setup-surface.js")).nostrSetupWizard,
110-
status: {
111-
...createStandardChannelSetupStatus({
112-
channelLabel: "Nostr",
113-
configuredLabel: t("wizard.channels.statusConfigured"),
114-
unconfiguredLabel: t("wizard.channels.statusNeedsPrivateKey"),
115-
configuredHint: t("wizard.channels.statusConfigured"),
116-
unconfiguredHint: t("wizard.channels.statusNeedsPrivateKey"),
117-
configuredScore: 1,
118-
unconfiguredScore: 0,
119-
includeStatusLine: true,
120-
resolveConfigured: ({ cfg, accountId }) =>
121-
resolveSetupNostrAccount({ cfg, accountId }).configured,
122-
resolveExtraStatusLines: ({ cfg }) => {
123-
const account = resolveSetupNostrAccount({ cfg });
124-
return [`Relays: ${account.relays.length || DEFAULT_RELAYS.length}`];
125-
},
126-
}),
127-
},
67+
status: createNostrSetupStatus(resolveSetupNostrAccount),
12868
resolveShouldPromptAccountIds: () => false,
12969
delegatePrepare: true,
13070
delegateFinalize: true,
13171
});
13272

133-
export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrSetupAccount> = {
73+
export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrAccount> = {
13474
id: channel,
13575
meta: {
13676
id: channel,
@@ -147,10 +87,17 @@ export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrSetupAccount> = {
14787
},
14888
reload: { configPrefixes: ["channels.nostr"] },
14989
configSchema: buildChannelConfigSchema(NostrConfigSchema),
150-
setupContract: nostrSetupContract,
90+
setupContract: createNostrSetupContract(
91+
createNostrSetupAdapter({
92+
resolveAccountId: (cfg, accountId) =>
93+
accountId?.trim() || resolveDefaultSetupNostrAccountId(cfg),
94+
validatePrivateKey: (privateKey) => /^(?:nsec1|NSEC1)|^[0-9a-fA-F]{64}$/u.test(privateKey),
95+
}),
96+
),
15197
setupWizard: nostrSetupWizard,
15298
config: {
153-
listAccountIds: listSetupNostrAccountIds,
99+
listAccountIds: (cfg) =>
100+
resolveSetupNostrAccount({ cfg }).configured ? [resolveDefaultSetupNostrAccountId(cfg)] : [],
154101
resolveAccount: (cfg, accountId) => resolveSetupNostrAccount({ cfg, accountId }),
155102
defaultAccountId: resolveDefaultSetupNostrAccountId,
156103
isConfigured: (account) => account.configured,

0 commit comments

Comments
 (0)