mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 12:10:42 +00:00
perf(matrix): narrow register-time runtime surface (#69782)
Merged via squash.
Prepared head SHA: ec32828b52
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
committed by
GitHub
parent
acb27bac3a
commit
13636c4521
@@ -1,18 +1,114 @@
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import {
|
||||
DEFAULT_ACCOUNT_ID,
|
||||
type DmPolicy,
|
||||
normalizeAccountId,
|
||||
prepareScopedSetupConfig,
|
||||
type ChannelSetupAdapter,
|
||||
type ChannelSetupWizardAdapter,
|
||||
} from "openclaw/plugin-sdk/setup";
|
||||
import { resolveDefaultMatrixAccountId, resolveMatrixAccountConfig } from "./matrix/accounts.js";
|
||||
import { resolveMatrixConfigFieldPath, updateMatrixAccountConfig } from "./matrix/config-update.js";
|
||||
import { applyMatrixSetupAccountConfig, validateMatrixSetupInput } from "./setup-config.js";
|
||||
import { resolveMatrixSetupDmAllowFrom } from "./setup-dm-policy.js";
|
||||
import type { CoreConfig } from "./types.js";
|
||||
|
||||
const channel = "matrix" as const;
|
||||
type MatrixSetupWizardModule = { matrixSetupWizard: ChannelSetupWizardAdapter };
|
||||
|
||||
function resolveMatrixSetupAccountId(params: { accountId?: string; name?: string }): string {
|
||||
return normalizeAccountId(params.accountId?.trim() || params.name?.trim() || DEFAULT_ACCOUNT_ID);
|
||||
}
|
||||
|
||||
function resolveMatrixSetupWizardAccountId(cfg: CoreConfig, accountId?: string): string {
|
||||
return normalizeAccountId(
|
||||
accountId?.trim() || resolveDefaultMatrixAccountId(cfg) || DEFAULT_ACCOUNT_ID,
|
||||
);
|
||||
}
|
||||
|
||||
function setMatrixDmPolicy(cfg: CoreConfig, policy: DmPolicy, accountId?: string): CoreConfig {
|
||||
const resolvedAccountId = resolveMatrixSetupWizardAccountId(cfg, accountId);
|
||||
const existing = resolveMatrixAccountConfig({
|
||||
cfg,
|
||||
accountId: resolvedAccountId,
|
||||
});
|
||||
const allowFrom = resolveMatrixSetupDmAllowFrom(policy, existing.dm?.allowFrom);
|
||||
return updateMatrixAccountConfig(cfg, resolvedAccountId, {
|
||||
dm: {
|
||||
...existing.dm,
|
||||
policy,
|
||||
allowFrom,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function createMatrixSetupWizardProxy(
|
||||
loadWizardModule: () => Promise<MatrixSetupWizardModule>,
|
||||
): ChannelSetupWizardAdapter {
|
||||
let wizardPromise: Promise<ChannelSetupWizardAdapter> | null = null;
|
||||
const loadWizard = () => {
|
||||
wizardPromise ??= loadWizardModule().then((module) => module.matrixSetupWizard);
|
||||
return wizardPromise;
|
||||
};
|
||||
return {
|
||||
channel,
|
||||
getStatus: async (ctx) => await (await loadWizard()).getStatus(ctx),
|
||||
configure: async (ctx) => await (await loadWizard()).configure(ctx),
|
||||
configureInteractive: async (ctx) => {
|
||||
const wizard = await loadWizard();
|
||||
return await (wizard.configureInteractive ?? wizard.configure)(ctx);
|
||||
},
|
||||
configureWhenConfigured: async (ctx) => {
|
||||
const wizard = await loadWizard();
|
||||
return await (
|
||||
wizard.configureWhenConfigured ??
|
||||
wizard.configureInteractive ??
|
||||
wizard.configure
|
||||
)(ctx);
|
||||
},
|
||||
afterConfigWritten: async (ctx) => await (await loadWizard()).afterConfigWritten?.(ctx),
|
||||
dmPolicy: {
|
||||
label: "Matrix",
|
||||
channel,
|
||||
policyKey: "channels.matrix.dm.policy",
|
||||
allowFromKey: "channels.matrix.dm.allowFrom",
|
||||
resolveConfigKeys: (cfg, accountId) => {
|
||||
const resolvedAccountId = resolveMatrixSetupWizardAccountId(cfg as CoreConfig, accountId);
|
||||
return {
|
||||
policyKey: resolveMatrixConfigFieldPath(
|
||||
cfg as CoreConfig,
|
||||
resolvedAccountId,
|
||||
"dm.policy",
|
||||
),
|
||||
allowFromKey: resolveMatrixConfigFieldPath(
|
||||
cfg as CoreConfig,
|
||||
resolvedAccountId,
|
||||
"dm.allowFrom",
|
||||
),
|
||||
};
|
||||
},
|
||||
getCurrent: (cfg, accountId) =>
|
||||
resolveMatrixAccountConfig({
|
||||
cfg: cfg as CoreConfig,
|
||||
accountId: resolveMatrixSetupWizardAccountId(cfg as CoreConfig, accountId),
|
||||
}).dm?.policy ?? "pairing",
|
||||
setPolicy: (cfg, policy, accountId) =>
|
||||
setMatrixDmPolicy(cfg as CoreConfig, policy, accountId) as OpenClawConfig,
|
||||
promptAllowFrom: async (params) => {
|
||||
const promptAllowFrom = (await loadWizard()).dmPolicy?.promptAllowFrom;
|
||||
return promptAllowFrom ? await promptAllowFrom(params) : params.cfg;
|
||||
},
|
||||
},
|
||||
disable: (cfg) => ({
|
||||
...(cfg as CoreConfig),
|
||||
channels: {
|
||||
...(cfg as CoreConfig).channels,
|
||||
matrix: { ...(cfg as CoreConfig).channels?.matrix, enabled: false },
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
export const matrixSetupAdapter: ChannelSetupAdapter = {
|
||||
resolveAccountId: ({ accountId, input }) =>
|
||||
resolveMatrixSetupAccountId({
|
||||
|
||||
Reference in New Issue
Block a user