Files
openclaw/extensions/matrix/src/plugin-entry.runtime.js
Gustavo Madeira Santana a204f790ce fix(matrix): package verification bootstrap runtime (#59249)
Merged via squash.

Prepared head SHA: df5891b663
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
2026-04-01 17:52:51 -04:00

160 lines
4.9 KiB
JavaScript

// Thin ESM wrapper so native dynamic import() resolves in source-checkout mode
// while packaged dist builds resolve a distinct runtime entry that cannot loop
// back into this wrapper through the stable root runtime alias.
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";
const require = createRequire(import.meta.url);
const { createJiti } = require("jiti");
const PLUGIN_ID = "matrix";
const OPENCLAW_PLUGIN_SDK_PREFIX = ["openclaw", "plugin-sdk"].join("/");
const PLUGIN_SDK_EXPORT_PREFIX = "./plugin-sdk/";
const PLUGIN_SDK_SOURCE_EXTENSIONS = [".ts", ".mts", ".js", ".mjs", ".cts", ".cjs"];
const PLUGIN_ENTRY_RUNTIME_BASENAME = "plugin-entry.handlers.runtime";
const JITI_EXTENSIONS = [
".ts",
".tsx",
".mts",
".cts",
".mtsx",
".ctsx",
".js",
".mjs",
".cjs",
".json",
];
function readPackageJson(packageRoot) {
try {
return JSON.parse(fs.readFileSync(path.join(packageRoot, "package.json"), "utf8"));
} catch {
return null;
}
}
function findOpenClawPackageRoot(startDir) {
let cursor = path.resolve(startDir);
for (let i = 0; i < 12; i += 1) {
const pkg = readPackageJson(cursor);
if (pkg?.name === "openclaw" && pkg.exports?.["./plugin-sdk"]) {
return { packageRoot: cursor, packageJson: pkg };
}
const parent = path.dirname(cursor);
if (parent === cursor) {
break;
}
cursor = parent;
}
return null;
}
function resolveExistingFile(basePath, extensions) {
for (const ext of extensions) {
const candidate = `${basePath}${ext}`;
if (fs.existsSync(candidate)) {
return candidate;
}
}
return null;
}
function buildPluginSdkAliasMap(moduleUrl) {
const location = findOpenClawPackageRoot(path.dirname(fileURLToPath(moduleUrl)));
if (!location) {
return {};
}
const { packageRoot, packageJson } = location;
const sourcePluginSdkDir = path.join(packageRoot, "src", "plugin-sdk");
const distPluginSdkDir = path.join(packageRoot, "dist", "plugin-sdk");
const aliasMap = {};
const rootAlias =
resolveExistingFile(path.join(sourcePluginSdkDir, "root-alias"), [".cjs"]) ??
resolveExistingFile(path.join(distPluginSdkDir, "root-alias"), [".cjs"]);
if (rootAlias) {
aliasMap[OPENCLAW_PLUGIN_SDK_PREFIX] = rootAlias;
}
for (const exportKey of Object.keys(packageJson.exports ?? {})) {
if (!exportKey.startsWith(PLUGIN_SDK_EXPORT_PREFIX)) {
continue;
}
const subpath = exportKey.slice(PLUGIN_SDK_EXPORT_PREFIX.length);
if (!subpath) {
continue;
}
const resolvedPath =
resolveExistingFile(path.join(sourcePluginSdkDir, subpath), PLUGIN_SDK_SOURCE_EXTENSIONS) ??
resolveExistingFile(path.join(distPluginSdkDir, subpath), [".js"]);
if (resolvedPath) {
aliasMap[`${OPENCLAW_PLUGIN_SDK_PREFIX}/${subpath}`] = resolvedPath;
}
}
const extensionApi =
resolveExistingFile(path.join(packageRoot, "src", "extensionAPI"), [".ts", ".js"]) ??
resolveExistingFile(path.join(packageRoot, "dist", "extensionAPI"), [".js"]);
if (extensionApi) {
aliasMap["openclaw/extension-api"] = extensionApi;
}
return aliasMap;
}
function resolveBundledPluginRuntimeModulePath(moduleUrl, params) {
const modulePath = fileURLToPath(moduleUrl);
const moduleDir = path.dirname(modulePath);
const localCandidates = [
path.join(moduleDir, "..", params.runtimeBasename),
path.join(moduleDir, "extensions", params.pluginId, params.runtimeBasename),
];
for (const candidate of localCandidates) {
const resolved = resolveExistingFile(candidate, PLUGIN_SDK_SOURCE_EXTENSIONS);
if (resolved) {
return resolved;
}
}
const location = findOpenClawPackageRoot(moduleDir);
if (location) {
const { packageRoot } = location;
const packageCandidates = [
path.join(packageRoot, "extensions", params.pluginId, params.runtimeBasename),
path.join(packageRoot, "dist", "extensions", params.pluginId, params.runtimeBasename),
];
for (const candidate of packageCandidates) {
const resolved = resolveExistingFile(candidate, PLUGIN_SDK_SOURCE_EXTENSIONS);
if (resolved) {
return resolved;
}
}
}
throw new Error(
`Cannot resolve ${params.pluginId} plugin runtime module ${params.runtimeBasename} from ${modulePath}`,
);
}
const jiti = createJiti(import.meta.url, {
alias: buildPluginSdkAliasMap(import.meta.url),
interopDefault: true,
tryNative: false,
extensions: JITI_EXTENSIONS,
});
const mod = jiti(
resolveBundledPluginRuntimeModulePath(import.meta.url, {
pluginId: PLUGIN_ID,
runtimeBasename: PLUGIN_ENTRY_RUNTIME_BASENAME,
}),
);
export const ensureMatrixCryptoRuntime = mod.ensureMatrixCryptoRuntime;
export const handleVerifyRecoveryKey = mod.handleVerifyRecoveryKey;
export const handleVerificationBootstrap = mod.handleVerificationBootstrap;
export const handleVerificationStatus = mod.handleVerificationStatus;