openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
183 lines (182 loc) • 4.96 kB
JavaScript
import { i as resolveEnvironmentValue } from "./process-env-DVJdcOcM.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { _ as resolveDefaultAgentId, f as resolveAgentWorkspaceDir, t as AgentSelectionRequiredError } from "./agent-scope-config-DmXgFGKP.mjs";
import { i as resolveSandboxConfigForAgent } from "./config-WbZbWrF1.mjs";
import { t as isTerminalConfigEnabled } from "./enabled-B07Lihbx.mjs";
import { r as buildRemoteCommand } from "./remote-shell-command-fpopw-g2.mjs";
import { statSync } from "node:fs";
import path from "node:path";
import os from "node:os";
//#region src/gateway/terminal/launch.ts
function resolveTerminalLaunch(e) {
const t = process.env;
const n = e.agentId?.trim();
let r;
try {
r = n ? normalizeAgentId(n) : resolveDefaultAgentId(e.config);
} catch (e) {
if (!(e instanceof AgentSelectionRequiredError)) throw e;
return {
ok: false,
block: {
kind: "owner-required",
message: e.message
}
};
}
if (n && !listAgentIds(e.config).includes(r)) return {
ok: false,
block: {
kind: "unknown-agent",
agentId: r
}
};
if (resolveSandboxConfigForAgent(e.config, r).mode === "all") return {
ok: false,
block: {
kind: "sandboxed",
agentId: r,
mode: "all"
}
};
const i = existingDirOrHome(resolveAgentWorkspaceDir(e.config, r, t), t);
const a = e.configuredShell?.trim();
const o = process.platform === "win32";
const s = a || (o ? t.ComSpec?.trim() || "cmd.exe" : t.SHELL?.trim() || "/bin/bash");
return {
ok: true,
plan: {
agentId: r,
cwd: i,
shell: s,
args: a || o ? [] : ["-l"]
}
};
}
/** Maintains fail-closed terminal admission across deferred config restarts. */
function createTerminalLaunchPolicy(e) {
let t = e;
let n = false;
let r = null;
let i = null;
const createRestrictions = () => ({
disabled: false,
blockedAgents: /* @__PURE__ */ new Map()
});
const a = createRestrictions();
const o = createRestrictions();
const committedTerminalConfig = () => i ?? t;
const resolveForConfig = (e, t, n = e) => resolveTerminalLaunch({
config: e,
agentId: t,
configuredShell: n.gateway?.terminal?.shell
});
const accumulateRestrictions = (e, n) => {
if (!isTerminalConfigEnabled(e)) n.disabled ||= isTerminalConfigEnabled(committedTerminalConfig());
for (const r of listAgentIds(t)) {
const t = resolveForConfig(e, r);
if (!t.ok) n.blockedAgents.set(r, t.block);
}
};
const clearRestrictions = (e) => {
e.disabled = false;
e.blockedAgents.clear();
};
const settlePreparedConfig = () => {
r = null;
clearRestrictions(o);
if (i) accumulateRestrictions(i, o);
};
const isEnabled = () => isTerminalConfigEnabled(committedTerminalConfig()) && !a.disabled && !o.disabled && (r === null || isTerminalConfigEnabled(r));
return {
resolve: (e) => {
if (!isEnabled()) return {
ok: false,
block: { kind: "disabled" }
};
const n = resolveForConfig(t, e, committedTerminalConfig());
if (!n.ok) return n;
const s = a.blockedAgents.get(n.plan.agentId) ?? o.blockedAgents.get(n.plan.agentId);
if (s) return {
ok: false,
block: s
};
const c = r ?? i;
if (c) {
const e = resolveForConfig(c, n.plan.agentId);
if (!e.ok) return e;
}
return n;
},
isEnabled,
prepareConfig: (e, t) => {
if (t.restartPending) {
n = true;
r = null;
accumulateRestrictions(e, a);
return;
}
r = e;
accumulateRestrictions(r, o);
},
commitConfig: () => {
if (r) {
if (n) i = r;
else t = r;
}
settlePreparedConfig();
},
acceptConfig: (e) => {
if (e.retireRejectedRestart) {
n = false;
clearRestrictions(a);
if (i) t = i;
i = null;
}
settlePreparedConfig();
}
};
}
function buildTerminalEnv(e, t = process.platform) {
const n = {};
for (const [t, r] of Object.entries(e)) if (typeof r === "string") n[t] = r;
n.TERM = n.TERM ?? "xterm-256color";
if (resolveEnvironmentValue(n, "COLORTERM", t) === void 0) n.COLORTERM = "truecolor";
n.OPENCLAW_TERMINAL = "1";
return n;
}
/** Converts a policy-approved plan into the exact local PTY spawn. */
function resolveTerminalSpawnPlan(e) {
const t = existingDirOrHome(e.cwdOverride ?? e.cwd, process.env);
const n = e.initialCommand;
let { shell: r, args: i } = e;
if (n?.length) {
if (process.platform === "win32") {
r = n[0] ?? r;
i = n.slice(1);
} else i = [
"-il",
"-c",
buildRemoteCommand(n)
];
}
return {
agentId: e.agentId,
shell: r,
args: i,
cwd: t
};
}
function existingDirOrHome(e, t) {
const n = e.trim();
const r = t.HOME?.trim() || os.homedir();
if (!n || !path.isAbsolute(n)) return r;
try {
if (statSync(n).isDirectory()) return n;
} catch {}
return r;
}
//#endregion
export { createTerminalLaunchPolicy as n, resolveTerminalSpawnPlan as r, buildTerminalEnv as t };