openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
156 lines (155 loc) • 5.02 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as mergeProcessEnv } from "./process-env-DVJdcOcM.mjs";
import { a as resolveExecutableFromPathEnv } from "./executable-path-D8oWZRS5.mjs";
import { o as resolveExecutableFromUserShellPath } from "./shell-env-Dbjy7T84.mjs";
import { r as spawnTerminalPty } from "./terminal-pty-CnUDkuzi.mjs";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
//#region src/node-host/pty-command.ts
function resolvePtyCwd(e, t = false) {
if (e && path.isAbsolute(e)) try {
if (fs.statSync(e).isDirectory()) return e;
} catch {}
if (t) throw new Error("INVALID_REQUEST: cwd must be an existing absolute directory on this node");
return os.homedir();
}
function isPtyDimension(e) {
return typeof e === "number" && Number.isInteger(e) && e >= 1 && e <= 2e3;
}
function decodePtyInput(e) {
try {
const t = JSON.parse(e);
if (!isRecord(t)) return null;
const n = t;
if (n.kind === "data" && typeof n.data === "string") return {
kind: "data",
data: n.data
};
if (n.kind === "resize" && isPtyDimension(n.cols) && isPtyDimension(n.rows)) return {
kind: "resize",
cols: n.cols,
rows: n.rows
};
return null;
} catch {
return null;
}
}
function decodePtyParams(e, t) {
let n;
try {
n = JSON.parse(e ?? "");
} catch {
throw new Error(`INVALID_REQUEST: terminal ${t} params must be valid JSON`);
}
if (!isRecord(n)) throw new Error(`INVALID_REQUEST: terminal ${t} params must be an object`);
const r = /* @__PURE__ */ new Set([
t === "start" ? "initialMessage" : "threadId",
"cwd",
"cols",
"rows"
]);
const i = Object.keys(n).find((e) => !r.has(e));
if (i) throw new Error(`INVALID_REQUEST: unknown terminal ${t} parameter: ${i}`);
const dimension = (e, t) => {
if (!isPtyDimension(e)) throw new Error(`INVALID_REQUEST: ${t} must be an integer from 1 to 2000`);
return e;
};
if (n.cwd !== void 0 && (typeof n.cwd !== "string" || Buffer.byteLength(n.cwd, "utf8") > 4096)) throw new Error("INVALID_REQUEST: cwd must be a bounded string");
return {
record: n,
cwd: typeof n.cwd === "string" && n.cwd ? n.cwd : void 0,
cols: dimension(n.cols, "cols"),
rows: dimension(n.rows, "rows")
};
}
function decodeNodePtyResumeParams(e, t) {
const { record: n, ...r } = decodePtyParams(e, "resume");
return {
threadId: t(n.threadId),
...r
};
}
function decodeNodePtyStartParams(e) {
const { record: t, cwd: n, ...r } = decodePtyParams(e, "start");
if (t.initialMessage !== void 0 && (typeof t.initialMessage !== "string" || t.initialMessage.length > 16384)) throw new Error("INVALID_REQUEST: initialMessage must be a string of at most 16384 characters");
return {
cwd: resolvePtyCwd(n, true),
...r,
...typeof t.initialMessage === "string" ? { initialMessage: t.initialMessage } : {}
};
}
/** Runs one allowlisted plugin-owned command in an interactive node PTY. */
async function runNodePtyCommand(e, t, n = spawnTerminalPty) {
if (t.signal.aborted) return { exitCode: 130 };
const r = mergeProcessEnv([
process.env,
e.env,
e.pathEnv ? { PATH: e.pathEnv } : void 0,
{ OPENCLAW_TERMINAL: "1" }
]);
e.assertCurrent?.();
const i = await n({
file: e.file,
args: e.args,
cwd: resolvePtyCwd(e.cwd, e.requiredCwd),
env: r,
cols: e.cols,
rows: e.rows
}, {
abortSignal: t.signal,
assertCurrent: e.assertCurrent
});
let a = Promise.resolve();
let o = false;
const kill = () => i.kill();
t.signal.addEventListener("abort", kill, { once: true });
if (t.signal.aborted) kill();
t.onInput((e) => {
if (o || t.signal.aborted) return;
const n = decodePtyInput(e);
try {
if (n?.kind === "data") i.write(n.data);
else if (n?.kind === "resize") i.resize(n.cols, n.rows);
} catch {}
});
i.onData((e) => {
if (o) return;
i.pause();
a = a.then(() => t.emitChunk(e)).finally(() => i.resume());
});
return await new Promise((e) => {
i.onExit((n) => {
if (o) return;
o = true;
t.signal.removeEventListener("abort", kill);
a.finally(() => e({
exitCode: n.exitCode,
...n.signal ? { signal: n.signal } : {}
}));
});
});
}
//#endregion
//#region src/plugin-sdk/node-host.ts
/** Resolve a node-host executable using the selected PATH source policy. */
function resolveNodeHostExecutable(e, t) {
const n = t.env ?? process.env;
const resolve = (r) => {
if (t.strategy === "direct") {
const i = resolveExecutableFromPathEnv(e, t.pathEnv ?? n.PATH ?? n.Path ?? "", n, { includeExtensionless: r });
return i ? { executable: i } : void 0;
}
return resolveExecutableFromUserShellPath(e, {
env: n,
pathEnv: t.pathEnv,
includeExtensionless: r,
strategy: t.strategy
});
};
if (t.includeExtensionless !== void 0 || process.platform !== "win32") return resolve(t.includeExtensionless ?? true);
return resolve(false) ?? resolve(true);
}
//#endregion
export { runNodePtyCommand as i, decodeNodePtyResumeParams as n, decodeNodePtyStartParams as r, resolveNodeHostExecutable as t };