@@ -7,7 +7,6 @@ import path from "node:path";
77import { createInterface } from "node:readline" ;
88import type { ControllerEnv } from "../app/env.js" ;
99import { logger } from "../lib/logger.js" ;
10- import { resolveOpenClawEntryPath } from "../lib/openclaw-runtime-bridge.js" ;
1110
1211const MAX_CONSECUTIVE_RESTARTS = 10 ;
1312const BASE_RESTART_DELAY_MS = 3000 ;
@@ -38,6 +37,20 @@ function findWorkspaceRoot(startDir: string): string | null {
3837 return null ;
3938}
4039
40+ function resolveOpenclawEntryFromBin ( binPath : string ) : string | null {
41+ const resolvedBinPath = path . resolve ( binPath . trim ( ) ) ;
42+ if ( resolvedBinPath . endsWith ( ".mjs" ) && existsSync ( resolvedBinPath ) ) {
43+ return resolvedBinPath ;
44+ }
45+
46+ const entry = path . resolve (
47+ path . dirname ( resolvedBinPath ) ,
48+ ".." ,
49+ "node_modules/openclaw/openclaw.mjs" ,
50+ ) ;
51+ return existsSync ( entry ) ? entry : null ;
52+ }
53+
4154export interface OpenClawRuntimeEvent {
4255 event : string ;
4356 payload ?: unknown ;
@@ -123,55 +136,49 @@ export class OpenClawProcessManager {
123136 let extraEnv : Record < string , string > = { } ;
124137
125138 if ( electronExec ) {
126- const entry = resolveOpenClawEntryPath ( {
127- openclawBin : this . env . openclawBin ,
128- workspaceRoot :
139+ const openclawEntryFromBin = resolveOpenclawEntryFromBin (
140+ this . env . openclawBin ,
141+ ) ;
142+ if ( openclawEntryFromBin ) {
143+ cmd = electronExec ;
144+ args = [ openclawEntryFromBin , "gateway" , "run" ] ;
145+ extraEnv = { ELECTRON_RUN_AS_NODE : "1" } ;
146+ } else {
147+ const workspaceRoot =
129148 process . env . NEXU_WORKSPACE_ROOT ?. trim ( ) ||
130- findWorkspaceRoot ( process . cwd ( ) ) ,
131- } ) ;
132- if ( ! entry ) {
133- throw new Error (
134- "Unable to resolve OpenClaw entry point from OPENCLAW_BIN" ,
135- ) ;
149+ findWorkspaceRoot ( process . cwd ( ) ) ;
150+ const runtimeEntryPath = workspaceRoot
151+ ? path . join (
152+ workspaceRoot ,
153+ "openclaw-runtime" ,
154+ "node_modules" ,
155+ "openclaw" ,
156+ "openclaw.mjs" ,
157+ )
158+ : null ;
159+
160+ if ( runtimeEntryPath && existsSync ( runtimeEntryPath ) ) {
161+ cmd = electronExec ;
162+ args = [ runtimeEntryPath , "gateway" , "run" ] ;
163+ extraEnv = { ELECTRON_RUN_AS_NODE : "1" } ;
164+ } else {
165+ // Resolve the openclaw entry point relative to the bin script
166+ const entry = resolveOpenclawEntryFromBin ( this . env . openclawBin ) ;
167+ if ( ! entry ) {
168+ throw new Error (
169+ "Unable to resolve OpenClaw entry point from OPENCLAW_BIN" ,
170+ ) ;
171+ }
172+ cmd = electronExec ;
173+ args = [ entry , "gateway" , "run" ] ;
174+ extraEnv = { ELECTRON_RUN_AS_NODE : "1" } ;
175+ }
136176 }
137- logger . info (
138- {
139- openclawBin : this . env . openclawBin ,
140- entry,
141- electronExec,
142- } ,
143- "controller resolved openclaw entry via repo-root openclaw runtime bridge" ,
144- ) ;
145- cmd = electronExec ;
146- args = [ entry , "gateway" , "run" ] ;
147- extraEnv = { ELECTRON_RUN_AS_NODE : "1" } ;
148177 } else {
149- logger . info (
150- {
151- openclawBin : this . env . openclawBin ,
152- electronExec : null ,
153- } ,
154- "controller using direct openclaw binary path" ,
155- ) ;
156178 cmd = this . env . openclawBin ;
157179 args = [ "gateway" , "run" ] ;
158180 }
159181
160- logger . info (
161- {
162- mode : electronExec ? "electron-entry" : "direct-bin" ,
163- cmd,
164- args,
165- cwd : path . resolve ( this . env . openclawStateDir ) ,
166- openclawBin : this . env . openclawBin ,
167- openclawBinExists : existsSync ( this . env . openclawBin ) ,
168- electronExec,
169- electronExecExists : electronExec ? existsSync ( electronExec ) : null ,
170- platform : process . platform ,
171- } ,
172- "openclaw spawn contract" ,
173- ) ;
174-
175182 const child = spawn ( cmd , args , {
176183 cwd : path . resolve ( this . env . openclawStateDir ) ,
177184 env : {
@@ -232,13 +239,7 @@ export class OpenClawProcessManager {
232239
233240 child . once ( "error" , ( error ) => {
234241 logger . error (
235- {
236- error : error . message ,
237- code : "code" in error ? error . code : undefined ,
238- path : "path" in error ? error . path : undefined ,
239- syscall : "syscall" in error ? error . syscall : undefined ,
240- spawnargs : "spawnargs" in error ? error . spawnargs : undefined ,
241- } ,
242+ { error : error . message } ,
242243 "failed to spawn openclaw process" ,
243244 ) ;
244245 this . child = null ;
0 commit comments