

















@@ -227,36 +227,33 @@ function unwrapRpcPayload(raw) {
227227}
228228229229async function rpcCall(method, params, options) {
230-const { callGateway } = await loadCallGatewayModule(options.runner);
231-const payload = await callGateway({
232-config: readJson(options.env.OPENCLAW_CONFIG_PATH),
233-configPath: options.env.OPENCLAW_CONFIG_PATH,
234-url: `ws://127.0.0.1:${options.port}`,
235-token: TOKEN,
236- method,
237-params: params ?? {},
238-timeoutMs: RPC_TIMEOUT_MS,
239-requiredMethods: [method],
240-});
230+const module = await loadCallGatewayModule(options.runner);
231+const payload = module
232+ ? await module.callGateway({
233+config: readJson(options.env.OPENCLAW_CONFIG_PATH),
234+configPath: options.env.OPENCLAW_CONFIG_PATH,
235+url: `ws://127.0.0.1:${options.port}`,
236+token: TOKEN,
237+ method,
238+params: params ?? {},
239+timeoutMs: RPC_TIMEOUT_MS,
240+requiredMethods: [method],
241+})
242+ : await rpcCallViaCli(method, params, options);
241243return unwrapRpcPayload(payload);
242244}
243245244246async function loadCallGatewayModule(runner) {
245-callGatewayModulePromise ??= importCallGatewayModule(runner);
247+if (!usesBuiltOpenClawEntry(runner)) {
248+return null;
249+}
250+callGatewayModulePromise ??= importCallGatewayModule();
246251return callGatewayModulePromise;
247252}
248253249-async function importCallGatewayModule(runner) {
250-if (!usesPackagedOpenClawEntry(runner)) {
251-return import(pathToFileURL(path.join(process.cwd(), "src/gateway/call.ts")).href);
252-}
254+async function importCallGatewayModule() {
253255const distDir = path.join(process.cwd(), "dist");
254-const candidates = fs.existsSync(distDir)
255- ? fs
256-.readdirSync(distDir)
257-.filter((name) => /^call(?:\.runtime)?-[A-Za-z0-9_-]+\.js$/u.test(name))
258-.toSorted((left, right) => left.localeCompare(right))
259- : [];
256+const candidates = findDistCallGatewayModuleFiles();
260257for (const name of candidates) {
261258const module = await import(pathToFileURL(path.join(distDir, name)).href);
262259if (typeof module.callGateway === "function") {
@@ -266,10 +263,49 @@ async function importCallGatewayModule(runner) {
266263throw new Error(`unable to find callGateway export in dist (${candidates.join(", ")})`);
267264}
268265269-function usesPackagedOpenClawEntry(runner) {
270-return Boolean(
271-process.env.OPENCLAW_ENTRY && runner?.baseArgs?.[0] === process.env.OPENCLAW_ENTRY,
266+async function rpcCallViaCli(method, params, options) {
267+const { stdout } = await runOpenClaw(
268+options.runner,
269+[
270+"gateway",
271+"call",
272+method,
273+"--url",
274+`ws://127.0.0.1:${options.port}`,
275+"--token",
276+TOKEN,
277+"--timeout",
278+String(RPC_TIMEOUT_MS),
279+"--json",
280+"--params",
281+JSON.stringify(params ?? {}),
282+],
283+options.env,
284+{ timeoutMs: RPC_TIMEOUT_MS + 30000 },
272285);
286+return parseJsonOutput(stdout);
287+}
288+289+export function findDistCallGatewayModuleFiles(cwd = process.cwd()) {
290+const distDir = path.join(cwd, "dist");
291+return fs.existsSync(distDir)
292+ ? fs
293+.readdirSync(distDir)
294+.filter((name) => /^call(?:\.runtime)?-[A-Za-z0-9_-]+\.js$/u.test(name))
295+.toSorted((left, right) => left.localeCompare(right))
296+ : [];
297+}
298+299+export function usesBuiltOpenClawEntry(runner, cwd = process.cwd(), env = process.env) {
300+if (runner?.pnpm || !runner?.baseArgs?.[0]) {
301+return false;
302+}
303+const entry = runner.baseArgs[0];
304+if (env.OPENCLAW_ENTRY && entry === env.OPENCLAW_ENTRY) {
305+return true;
306+}
307+const relative = path.relative(path.resolve(cwd, "dist"), path.resolve(cwd, entry));
308+return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
273309}
274310275311async function retryRpcCall(method, params, options) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。