From 9deb90a6ffaecffaf4bbdeb40fd1e711687ccfd1 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Wed, 18 Mar 2026 02:35:30 +0000 Subject: [PATCH] Matrix: lazy-load plugin entry runtime --- extensions/matrix/index.ts | 116 +++++------------- extensions/matrix/src/plugin-entry.runtime.ts | 67 ++++++++++ 2 files changed, 99 insertions(+), 84 deletions(-) create mode 100644 extensions/matrix/src/plugin-entry.runtime.ts diff --git a/extensions/matrix/index.ts b/extensions/matrix/index.ts index 675a9d31284..6fecfa5ffa3 100644 --- a/extensions/matrix/index.ts +++ b/extensions/matrix/index.ts @@ -1,92 +1,44 @@ -import type { GatewayRequestHandlerOptions, OpenClawPluginApi } from "openclaw/plugin-sdk/matrix"; -import { emptyPluginConfigSchema } from "openclaw/plugin-sdk/matrix"; +import { defineChannelPluginEntry } from "openclaw/plugin-sdk/core"; import { matrixPlugin } from "./src/channel.js"; import { registerMatrixCli } from "./src/cli.js"; -import { - bootstrapMatrixVerification, - getMatrixVerificationStatus, - verifyMatrixRecoveryKey, -} from "./src/matrix/actions/verification.js"; -import { ensureMatrixCryptoRuntime } from "./src/matrix/deps.js"; import { setMatrixRuntime } from "./src/runtime.js"; -const plugin = { +export { matrixPlugin } from "./src/channel.js"; +export { setMatrixRuntime } from "./src/runtime.js"; + +export default defineChannelPluginEntry({ id: "matrix", name: "Matrix", description: "Matrix channel plugin (matrix-js-sdk)", - configSchema: emptyPluginConfigSchema(), - register(api: OpenClawPluginApi) { - setMatrixRuntime(api.runtime); - void ensureMatrixCryptoRuntime({ log: api.logger.info }).catch((err: unknown) => { - const message = err instanceof Error ? err.message : String(err); - api.logger.warn?.(`matrix: crypto runtime bootstrap failed: ${message}`); + plugin: matrixPlugin, + setRuntime: setMatrixRuntime, + registerFull(api) { + void import("./src/plugin-entry.runtime.js") + .then(({ ensureMatrixCryptoRuntime }) => + ensureMatrixCryptoRuntime({ log: api.logger.info }).catch((err: unknown) => { + const message = err instanceof Error ? err.message : String(err); + api.logger.warn?.(`matrix: crypto runtime bootstrap failed: ${message}`); + }), + ) + .catch((err: unknown) => { + const message = err instanceof Error ? err.message : String(err); + api.logger.warn?.(`matrix: failed loading crypto bootstrap runtime: ${message}`); + }); + + api.registerGatewayMethod("matrix.verify.recoveryKey", async (ctx) => { + const { handleVerifyRecoveryKey } = await import("./src/plugin-entry.runtime.js"); + await handleVerifyRecoveryKey(ctx); }); - api.registerChannel({ plugin: matrixPlugin }); - const sendError = (respond: (ok: boolean, payload?: unknown) => void, err: unknown) => { - respond(false, { error: err instanceof Error ? err.message : String(err) }); - }; + api.registerGatewayMethod("matrix.verify.bootstrap", async (ctx) => { + const { handleVerificationBootstrap } = await import("./src/plugin-entry.runtime.js"); + await handleVerificationBootstrap(ctx); + }); - api.registerGatewayMethod( - "matrix.verify.recoveryKey", - async ({ params, respond }: GatewayRequestHandlerOptions) => { - try { - const key = typeof params?.key === "string" ? params.key : ""; - if (!key.trim()) { - respond(false, { error: "key required" }); - return; - } - const accountId = - typeof params?.accountId === "string" - ? params.accountId.trim() || undefined - : undefined; - const result = await verifyMatrixRecoveryKey(key, { accountId }); - respond(result.success, result); - } catch (err) { - sendError(respond, err); - } - }, - ); - - api.registerGatewayMethod( - "matrix.verify.bootstrap", - async ({ params, respond }: GatewayRequestHandlerOptions) => { - try { - const accountId = - typeof params?.accountId === "string" - ? params.accountId.trim() || undefined - : undefined; - const recoveryKey = - typeof params?.recoveryKey === "string" ? params.recoveryKey : undefined; - const forceResetCrossSigning = params?.forceResetCrossSigning === true; - const result = await bootstrapMatrixVerification({ - accountId, - recoveryKey, - forceResetCrossSigning, - }); - respond(result.success, result); - } catch (err) { - sendError(respond, err); - } - }, - ); - - api.registerGatewayMethod( - "matrix.verify.status", - async ({ params, respond }: GatewayRequestHandlerOptions) => { - try { - const accountId = - typeof params?.accountId === "string" - ? params.accountId.trim() || undefined - : undefined; - const includeRecoveryKey = params?.includeRecoveryKey === true; - const status = await getMatrixVerificationStatus({ accountId, includeRecoveryKey }); - respond(true, status); - } catch (err) { - sendError(respond, err); - } - }, - ); + api.registerGatewayMethod("matrix.verify.status", async (ctx) => { + const { handleVerificationStatus } = await import("./src/plugin-entry.runtime.js"); + await handleVerificationStatus(ctx); + }); api.registerCli( ({ program }) => { @@ -95,8 +47,4 @@ const plugin = { { commands: ["matrix"] }, ); }, -}; - -export { matrixPlugin } from "./src/channel.js"; -export { setMatrixRuntime } from "./src/runtime.js"; -export default plugin; +}); diff --git a/extensions/matrix/src/plugin-entry.runtime.ts b/extensions/matrix/src/plugin-entry.runtime.ts new file mode 100644 index 00000000000..f5260242a72 --- /dev/null +++ b/extensions/matrix/src/plugin-entry.runtime.ts @@ -0,0 +1,67 @@ +import type { GatewayRequestHandlerOptions } from "openclaw/plugin-sdk/core"; +import { + bootstrapMatrixVerification, + getMatrixVerificationStatus, + verifyMatrixRecoveryKey, +} from "./matrix/actions/verification.js"; +import { ensureMatrixCryptoRuntime } from "./matrix/deps.js"; + +function sendError(respond: (ok: boolean, payload?: unknown) => void, err: unknown) { + respond(false, { error: err instanceof Error ? err.message : String(err) }); +} + +export { ensureMatrixCryptoRuntime }; + +export async function handleVerifyRecoveryKey({ + params, + respond, +}: GatewayRequestHandlerOptions): Promise { + try { + const key = typeof params?.key === "string" ? params.key : ""; + if (!key.trim()) { + respond(false, { error: "key required" }); + return; + } + const accountId = + typeof params?.accountId === "string" ? params.accountId.trim() || undefined : undefined; + const result = await verifyMatrixRecoveryKey(key, { accountId }); + respond(result.success, result); + } catch (err) { + sendError(respond, err); + } +} + +export async function handleVerificationBootstrap({ + params, + respond, +}: GatewayRequestHandlerOptions): Promise { + try { + const accountId = + typeof params?.accountId === "string" ? params.accountId.trim() || undefined : undefined; + const recoveryKey = typeof params?.recoveryKey === "string" ? params.recoveryKey : undefined; + const forceResetCrossSigning = params?.forceResetCrossSigning === true; + const result = await bootstrapMatrixVerification({ + accountId, + recoveryKey, + forceResetCrossSigning, + }); + respond(result.success, result); + } catch (err) { + sendError(respond, err); + } +} + +export async function handleVerificationStatus({ + params, + respond, +}: GatewayRequestHandlerOptions): Promise { + try { + const accountId = + typeof params?.accountId === "string" ? params.accountId.trim() || undefined : undefined; + const includeRecoveryKey = params?.includeRecoveryKey === true; + const status = await getMatrixVerificationStatus({ accountId, includeRecoveryKey }); + respond(true, status); + } catch (err) { + sendError(respond, err); + } +}