fix(matrix): align wrapper scoped sdk aliases

This commit is contained in:
Vincent Koc
2026-04-14 17:41:32 +01:00
parent a80ecb9937
commit 074efc94dc
2 changed files with 100 additions and 3 deletions

View File

@@ -10,7 +10,10 @@ const require = createRequire(import.meta.url);
const { createJiti } = require("jiti");
const PLUGIN_ID = "matrix";
const OPENCLAW_PLUGIN_SDK_PREFIX = ["openclaw", "plugin-sdk"].join("/");
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";
@@ -75,7 +78,9 @@ function buildPluginSdkAliasMap(moduleUrl) {
resolveExistingFile(path.join(sourcePluginSdkDir, "root-alias"), [".cjs"]) ??
resolveExistingFile(path.join(distPluginSdkDir, "root-alias"), [".cjs"]);
if (rootAlias) {
aliasMap[OPENCLAW_PLUGIN_SDK_PREFIX] = rootAlias;
for (const packageName of OPENCLAW_PLUGIN_SDK_PACKAGE_NAMES) {
aliasMap[packageName] = rootAlias;
}
}
for (const exportKey of Object.keys(packageJson.exports ?? {})) {
@@ -90,7 +95,9 @@ function buildPluginSdkAliasMap(moduleUrl) {
resolveExistingFile(path.join(sourcePluginSdkDir, subpath), PLUGIN_SDK_SOURCE_EXTENSIONS) ??
resolveExistingFile(path.join(distPluginSdkDir, subpath), [".js"]);
if (resolvedPath) {
aliasMap[`${OPENCLAW_PLUGIN_SDK_PREFIX}/${subpath}`] = resolvedPath;
for (const packageName of OPENCLAW_PLUGIN_SDK_PACKAGE_NAMES) {
aliasMap[`${packageName}/${subpath}`] = resolvedPath;
}
}
}

View File

@@ -9,6 +9,9 @@ const tempDirs: string[] = [];
const REPO_ROOT = process.cwd();
const require = createRequire(import.meta.url);
const JITI_ENTRY_PATH = require.resolve("jiti");
const matrixWrapperGlobal = globalThis as typeof globalThis & {
__openclawMatrixWrapperJitiOptions?: unknown;
};
const PACKAGED_RUNTIME_STUB = [
"export async function ensureMatrixCryptoRuntime() {}",
"export async function handleVerifyRecoveryKey() {}",
@@ -37,6 +40,27 @@ function writeJitiFixture(fixtureRoot: string) {
);
}
function writeCapturingJitiFixture(fixtureRoot: string) {
writeFixtureFile(
fixtureRoot,
"node_modules/jiti/index.js",
[
"exports.createJiti = function createJiti(_filename, options) {",
" globalThis.__openclawMatrixWrapperJitiOptions = options;",
" return function jiti() {",
" return {",
" ensureMatrixCryptoRuntime: async function ensureMatrixCryptoRuntime() {},",
" handleVerifyRecoveryKey: async function handleVerifyRecoveryKey() {},",
" handleVerificationBootstrap: async function handleVerificationBootstrap() {},",
" handleVerificationStatus: async function handleVerificationStatus() {},",
" };",
" };",
"};",
"",
].join("\n"),
);
}
function writeOpenClawPackageFixture(fixtureRoot: string) {
writeFixtureFile(
fixtureRoot,
@@ -56,6 +80,30 @@ function writeOpenClawPackageFixture(fixtureRoot: string) {
writeFixtureFile(fixtureRoot, "dist/plugin-sdk/index.js", "export {};\n");
}
function writeOpenClawAliasFixture(fixtureRoot: string) {
writeFixtureFile(
fixtureRoot,
"package.json",
JSON.stringify(
{
name: "openclaw",
type: "module",
exports: {
"./plugin-sdk": "./dist/plugin-sdk/index.js",
"./plugin-sdk/group-access": "./dist/plugin-sdk/group-access.js",
},
},
null,
2,
) + "\n",
);
writeFixtureFile(fixtureRoot, "src/plugin-sdk/root-alias.cjs", "module.exports = {};\n");
writeFixtureFile(fixtureRoot, "src/plugin-sdk/group-access.ts", "export {};\n");
writeFixtureFile(fixtureRoot, "dist/plugin-sdk/index.js", "export {};\n");
writeFixtureFile(fixtureRoot, "dist/plugin-sdk/root-alias.cjs", "module.exports = {};\n");
writeFixtureFile(fixtureRoot, "dist/plugin-sdk/group-access.js", "export {};\n");
}
afterEach(() => {
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
@@ -124,3 +172,45 @@ it("loads the packaged runtime wrapper without recursing through the stable root
handleVerificationStatus: expect.any(Function),
});
}, 240_000);
it("builds scoped and unscoped plugin-sdk aliases for the wrapper jiti loader", async () => {
const fixtureRoot = makeFixtureRoot(".tmp-matrix-runtime-aliases-");
const wrapperSource = fs.readFileSync(
path.join(REPO_ROOT, "extensions", "matrix", "src", "plugin-entry.runtime.js"),
"utf8",
);
delete matrixWrapperGlobal.__openclawMatrixWrapperJitiOptions;
writeOpenClawAliasFixture(fixtureRoot);
writeCapturingJitiFixture(fixtureRoot);
writeFixtureFile(fixtureRoot, "extensions/matrix/src/plugin-entry.runtime.js", wrapperSource);
writeFixtureFile(
fixtureRoot,
"extensions/matrix/plugin-entry.handlers.runtime.js",
PACKAGED_RUNTIME_STUB,
);
const wrapperUrl = pathToFileURL(
path.join(fixtureRoot, "extensions", "matrix", "src", "plugin-entry.runtime.js"),
);
await import(`${wrapperUrl.href}?t=${Date.now()}`);
expect(matrixWrapperGlobal.__openclawMatrixWrapperJitiOptions).toMatchObject({
alias: {
"openclaw/plugin-sdk": path.join(fixtureRoot, "src", "plugin-sdk", "root-alias.cjs"),
"@openclaw/plugin-sdk": path.join(fixtureRoot, "src", "plugin-sdk", "root-alias.cjs"),
"openclaw/plugin-sdk/group-access": path.join(
fixtureRoot,
"src",
"plugin-sdk",
"group-access.ts",
),
"@openclaw/plugin-sdk/group-access": path.join(
fixtureRoot,
"src",
"plugin-sdk",
"group-access.ts",
),
},
});
}, 240_000);