mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-20 06:20:55 +00:00
Matrix: lazy-load plugin entry runtime
This commit is contained in:
@@ -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;
|
||||
});
|
||||
|
||||
67
extensions/matrix/src/plugin-entry.runtime.ts
Normal file
67
extensions/matrix/src/plugin-entry.runtime.ts
Normal file
@@ -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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user