openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
143 lines (142 loc) • 5.15 kB
JavaScript
import { m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs";
import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs";
import { t as mergeDeep } from "./deep-merge-Wuxftp-p.mjs";
import { t as readByteStreamWithLimit } from "./read-byte-stream-with-limit-BjmaH2Xs.mjs";
import { createReadStream, existsSync } from "node:fs";
import path from "node:path";
import { TextDecoder } from "node:util";
//#region src/commands/agent-exec-input.ts
const AGENT_EXEC_MESSAGE_MAX_BYTES = 4194304;
const UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
/** Resolve the one allowed prompt source for `agent exec`. */
async function resolveAgentExecPrompt(e, t) {
const n = readNonBlankString(t);
if (e !== void 0 && n) throw new Error("Use either the prompt argument or --message-file, not both.");
if (t !== void 0 && !n) throw new Error("--message-file must not be empty.");
if (n) {
const e = n === "-" ? process.stdin : createReadStream(n);
try {
const t = n === "-" ? "stdin" : `Message file ${n}`;
const r = await readByteStreamWithLimit(e, {
maxBytes: AGENT_EXEC_MESSAGE_MAX_BYTES,
onOverflow: () => /* @__PURE__ */ new Error(`${t} exceeds ${String(AGENT_EXEC_MESSAGE_MAX_BYTES)} bytes`)
});
let i;
try {
i = UTF8_DECODER.decode(r).replace(/^\uFEFF/, "");
} catch {
throw new Error(`${t} must be valid UTF-8`);
}
if (!i.trim()) throw new Error(`${t} is empty`);
return i;
} catch (e) {
if (n === "-" || !(e instanceof Error) || !("code" in e)) throw e;
if (e.code === "ENOENT") throw new Error(`Message file not found: ${n}`, { cause: e });
throw e;
}
}
if (!e?.trim()) throw new Error("Missing prompt. Pass text or use --message-file <path>.");
return e;
}
/**
* Coding one-shot defaults. These merge *under* the resolved config so an
* operator who configured a tool profile, shell env, or sandbox keeps it;
* notably exec must never downgrade a configured sandbox to `off`.
*/
function buildExecConfigDefaults() {
return {
env: { shellEnv: { enabled: false } },
agents: { defaults: { sandbox: { mode: "off" } } },
tools: {
profile: "coding",
fs: { workspaceOnly: true },
exec: { mode: "full" }
}
};
}
/**
* Config is a credential source (provider keys, headers, env and shell imports).
* Environment-only execution therefore skips it entirely; ordinary exec inherits it.
*/
async function resolveExecBaseConfig(e) {
if (e.configOverride?.length && e.authEnvOnly === true) throw new Error("--config-override cannot be combined with --auth-env-only.");
if (e.config && (e.isolated || e.authEnvOnly === true)) {
const t = e.isolated ? "--isolated" : "--auth-env-only";
throw new Error(`--config cannot be combined with ${t}.`);
}
if (e.isolated || e.authEnvOnly === true) {
const { applyImplicitAgentRosterDefaults: e } = await import("./implicit-agent-roster-B-t60W6b.mjs");
const { coerceConfig: t } = await import("./io.read-helpers-CZfTtz4H.mjs");
return t(e({}));
}
const { createConfigIO: t, captureRuntimeConfigAsyncReader: n } = await import("./io-DeTUDO0h.mjs");
if (!e.config) return await n({ observe: false })();
const r = t({
configPath: path.resolve(e.config),
observe: false
});
if (!existsSync(r.configPath)) throw new Error(`--config file not found: ${r.configPath}`);
return await r.loadConfigAsync();
}
/** Overlay the selected config before resolving the run's agent and credential owner. */
async function applyExecConfigOverrides(e, t = []) {
if (t.length === 0) return e;
const n = t.map((e) => {
let t;
try {
t = JSON.parse(e);
} catch {
throw new Error("--config-override must contain valid JSON.");
}
if (!isPlainObject(t)) throw new Error("--config-override must be a JSON object.");
return t;
}).reduce((e, t) => mergeDeep(e, t), e);
const { validateConfigObjectWithPlugins: r } = await import("./validation-CK9-S99j.mjs");
const i = r(n);
if (!i.ok) throw new Error(`Invalid --config-override: ${i.issues.map((e) => `${e.path}: ${e.message}`).join("; ")}`);
return i.config;
}
function buildExecRunConfig(e) {
const t = e.opts ?? {};
const { base: n, cwd: r } = e;
const { session: i, ...a } = n;
const { store: o, ...s } = i ?? {};
const c = i ? {
...a,
session: s
} : n;
const l = c.agents?.entries;
const u = l ? {
...c,
agents: {
...c.agents,
entries: Object.fromEntries(Object.entries(l).map(([e, t]) => {
const { agentDir: n, runtime: i, ...a } = t;
if (i?.type !== "acp" || i.acp?.cwd === void 0) return [e, {
...a,
workspace: r,
...i ? { runtime: i } : {}
}];
const { cwd: o, ...s } = i.acp;
return [e, {
...a,
workspace: r,
runtime: {
...i,
acp: s
}
}];
}))
}
} : c;
return mergeDeep(mergeDeep(buildExecConfigDefaults(), u), {
agents: { defaults: {
workspace: e.cwd,
skipBootstrap: true,
...t.localModelLean ? { experimental: { localModelLean: true } } : {}
} },
skills: { load: { watch: false } }
});
}
//#endregion
export { resolveExecBaseConfig as i, buildExecRunConfig as n, resolveAgentExecPrompt as r, applyExecConfigOverrides as t };