refactor(matrix): keep runtime wrapper native-only

This commit is contained in:
Peter Steinberger
2026-05-01 23:36:00 +01:00
parent 45dee50c28
commit 4fce56294d
7 changed files with 17 additions and 304 deletions

View File

@@ -2,33 +2,12 @@
// 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, pathToFileURL } from "node:url";
const require = createRequire(import.meta.url);
const PLUGIN_ID = "matrix";
const OPENCLAW_PLUGIN_SDK_PACKAGE_NAMES = [
["openclaw", "plugin-sdk"].join("/"),
["@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 NATIVE_RUNTIME_EXTENSIONS = [".js", ".mjs", ".cjs"];
const JITI_EXTENSIONS = [
".ts",
".tsx",
".mts",
".cts",
".mtsx",
".ctsx",
".js",
".mjs",
".cjs",
".json",
];
function readPackageJson(packageRoot) {
try {
@@ -84,55 +63,6 @@ function resolveExistingFile(basePath, extensions) {
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) {
for (const packageName of OPENCLAW_PLUGIN_SDK_PACKAGE_NAMES) {
aliasMap[packageName] = rootAlias;
}
}
for (const exportKey of Object.keys(packageJson.exports ?? {}).toSorted()) {
if (!exportKey.startsWith(PLUGIN_SDK_EXPORT_PREFIX)) {
continue;
}
const subpath = exportKey.slice(PLUGIN_SDK_EXPORT_PREFIX.length);
if (!/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(subpath)) {
continue;
}
const resolvedPath =
resolveExistingFile(path.join(sourcePluginSdkDir, subpath), PLUGIN_SDK_SOURCE_EXTENSIONS) ??
resolveExistingFile(path.join(distPluginSdkDir, subpath), [".js"]);
if (resolvedPath) {
for (const packageName of OPENCLAW_PLUGIN_SDK_PACKAGE_NAMES) {
aliasMap[`${packageName}/${subpath}`] = resolvedPath;
}
}
}
const extensionApi =
resolveExistingFile(
path.join(packageRoot, "src", "extensionAPI"),
PLUGIN_SDK_SOURCE_EXTENSIONS,
) ?? 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);
@@ -142,7 +72,7 @@ function resolveBundledPluginRuntimeModulePath(moduleUrl, params) {
];
for (const candidate of localCandidates) {
const resolved = resolveExistingFile(candidate, PLUGIN_SDK_SOURCE_EXTENSIONS);
const resolved = resolveExistingFile(candidate, NATIVE_RUNTIME_EXTENSIONS);
if (resolved) {
return resolved;
}
@@ -157,7 +87,7 @@ function resolveBundledPluginRuntimeModulePath(moduleUrl, params) {
];
for (const candidate of packageCandidates) {
const resolved = resolveExistingFile(candidate, PLUGIN_SDK_SOURCE_EXTENSIONS);
const resolved = resolveExistingFile(candidate, NATIVE_RUNTIME_EXTENSIONS);
if (resolved) {
return resolved;
}
@@ -170,17 +100,7 @@ function resolveBundledPluginRuntimeModulePath(moduleUrl, params) {
}
async function loadRuntimeModule(modulePath) {
if (NATIVE_RUNTIME_EXTENSIONS.includes(path.extname(modulePath))) {
return import(pathToFileURL(modulePath).href);
}
const { createJiti } = require("jiti");
const jiti = createJiti(import.meta.url, {
alias: buildPluginSdkAliasMap(import.meta.url),
interopDefault: true,
tryNative: false,
extensions: JITI_EXTENSIONS,
});
return jiti(modulePath);
return import(pathToFileURL(modulePath).href);
}
const mod = await loadRuntimeModule(