Update server.js
Browse files
server.js
CHANGED
|
@@ -145,40 +145,47 @@ const env = {
|
|
| 145 |
6. EXECUTION + JSON PARSE
|
| 146 |
================================ */
|
| 147 |
|
|
|
|
|
|
|
| 148 |
function runOpenClaw(env, payload) {
|
| 149 |
return new Promise((resolve, reject) => {
|
| 150 |
const proc = spawn(process.execPath, ["src/index.js"], {
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
});
|
| 154 |
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
}, 120000); // 2 minutes
|
| 163 |
|
| 164 |
-
proc.
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
proc.on("close", code => {
|
| 168 |
-
clearTimeout(timeout);
|
| 169 |
-
|
| 170 |
if (code !== 0) {
|
| 171 |
-
return reject(
|
|
|
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
try {
|
| 175 |
-
const json = JSON.parse(
|
| 176 |
resolve(json);
|
| 177 |
-
} catch {
|
| 178 |
-
reject(new Error("Invalid JSON
|
| 179 |
}
|
| 180 |
});
|
| 181 |
|
|
|
|
| 182 |
proc.stdin.write(JSON.stringify(payload));
|
| 183 |
proc.stdin.end();
|
| 184 |
});
|
|
|
|
| 145 |
6. EXECUTION + JSON PARSE
|
| 146 |
================================ */
|
| 147 |
|
| 148 |
+
import { spawn } from "child_process";
|
| 149 |
+
|
| 150 |
function runOpenClaw(env, payload) {
|
| 151 |
return new Promise((resolve, reject) => {
|
| 152 |
const proc = spawn(process.execPath, ["src/index.js"], {
|
| 153 |
+
cwd: "/app",
|
| 154 |
+
env
|
| 155 |
+
});
|
| 156 |
|
| 157 |
+
let stdout = "";
|
| 158 |
+
let stderr = "";
|
| 159 |
|
| 160 |
+
// CRITICAL: handle spawn errors
|
| 161 |
+
proc.on("error", err => {
|
| 162 |
+
reject(err);
|
| 163 |
+
});
|
| 164 |
|
| 165 |
+
proc.stdout.on("data", data => {
|
| 166 |
+
stdout += data.toString();
|
| 167 |
+
});
|
|
|
|
| 168 |
|
| 169 |
+
proc.stderr.on("data", data => {
|
| 170 |
+
stderr += data.toString();
|
| 171 |
+
});
|
| 172 |
|
| 173 |
proc.on("close", code => {
|
|
|
|
|
|
|
| 174 |
if (code !== 0) {
|
| 175 |
+
return reject(
|
| 176 |
+
new Error(stderr || `Agent exited with code ${code}`)
|
| 177 |
+
);
|
| 178 |
}
|
| 179 |
|
| 180 |
try {
|
| 181 |
+
const json = JSON.parse(stdout);
|
| 182 |
resolve(json);
|
| 183 |
+
} catch (err) {
|
| 184 |
+
reject(new Error("Invalid JSON from OpenClaw agent"));
|
| 185 |
}
|
| 186 |
});
|
| 187 |
|
| 188 |
+
// SEND INPUT TO AGENT
|
| 189 |
proc.stdin.write(JSON.stringify(payload));
|
| 190 |
proc.stdin.end();
|
| 191 |
});
|