





















@@ -2,7 +2,6 @@ import { spawn } from "node:child_process";
22import fs from "node:fs";
33import { createRequire } from "node:module";
44import path from "node:path";
5-import { fileURLToPath } from "node:url";
65import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
76import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime";
87@@ -26,12 +25,12 @@ type MatrixCryptoRuntimeDeps = {
2625log?: (message: string) => void;
2726};
282729-function resolveMissingMatrixPackages(): string[] {
28+function resolveMissingMatrixPackages(resolveFn?: (id: string) => string): string[] {
3029try {
31-const req = createRequire(import.meta.url);
30+const resolve = resolveFn ?? defaultResolveFn;
3231return REQUIRED_MATRIX_PACKAGES.filter((pkg) => {
3332try {
34-req.resolve(pkg);
33+resolve(pkg);
3534return false;
3635} catch {
3736return true;
@@ -46,9 +45,12 @@ export function isMatrixSdkAvailable(): boolean {
4645return resolveMissingMatrixPackages().length === 0;
4746}
484749-function resolvePluginRoot(): string {
50-const currentDir = path.dirname(fileURLToPath(import.meta.url));
51-return path.resolve(currentDir, "..", "..");
48+function buildMatrixDepsMissingMessage(missing: string[]): string {
49+const packages = missing.length > 0 ? missing.join(", ") : REQUIRED_MATRIX_PACKAGES.join(", ");
50+return [
51+`Matrix plugin dependencies are missing: ${packages}.`,
52+"Repair this plugin with `openclaw plugins update matrix` or run `openclaw doctor --fix`.",
53+].join(" ");
5254}
53555456type CommandResult = {
@@ -299,47 +301,14 @@ async function ensureMatrixCryptoRuntimeOnce(params: MatrixCryptoRuntimeDeps): P
299301requireFn("@matrix-org/matrix-sdk-crypto-nodejs");
300302}
301303302-export async function ensureMatrixSdkInstalled(params: {
303-runtime: RuntimeEnv;
304+export async function ensureMatrixSdkInstalled(params?: {
305+runtime?: RuntimeEnv;
304306confirm?: (message: string) => Promise<boolean>;
307+resolveFn?: (id: string) => string;
305308}): Promise<void> {
306-if (isMatrixSdkAvailable()) {
309+const missing = resolveMissingMatrixPackages(params?.resolveFn);
310+if (missing.length === 0) {
307311return;
308312}
309-const confirm = params.confirm;
310-if (confirm) {
311-const ok = await confirm(
312-"Matrix requires matrix-js-sdk, @matrix-org/matrix-sdk-crypto-nodejs, and @matrix-org/matrix-sdk-crypto-wasm. Install now?",
313-);
314-if (!ok) {
315-throw new Error(
316-"Matrix requires matrix-js-sdk, @matrix-org/matrix-sdk-crypto-nodejs, and @matrix-org/matrix-sdk-crypto-wasm (install dependencies first).",
317-);
318-}
319-}
320-321-const root = resolvePluginRoot();
322-const command = fs.existsSync(path.join(root, "pnpm-lock.yaml"))
323- ? ["pnpm", "install"]
324- : ["npm", "install", "--omit=dev", "--silent"];
325-params.runtime.log?.(`matrix: installing dependencies via ${command[0]} (${root})…`);
326-const result = await runFixedCommandWithTimeout({
327-argv: command,
328-cwd: root,
329-timeoutMs: 300_000,
330-env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
331-});
332-if (result.code !== 0) {
333-throw new Error(
334-result.stderr.trim() || result.stdout.trim() || "Matrix dependency install failed.",
335-);
336-}
337-if (!isMatrixSdkAvailable()) {
338-const missing = resolveMissingMatrixPackages();
339-throw new Error(
340-missing.length > 0
341- ? `Matrix dependency install completed but required packages are still missing: ${missing.join(", ")}`
342- : "Matrix dependency install completed but Matrix dependencies are still missing.",
343-);
344-}
313+throw new Error(buildMatrixDepsMissingMessage(missing));
345314}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。