openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
63 lines (62 loc) • 3.33 kB
JavaScript
import { s as formatErrorMessage } from "./error-coercion-hDrHOSGy.mjs";
import { n as sliceUtf16Safe, r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { i as stripAnsi, n as sanitizeForLog } from "./ansi-CZa2t0Hf.mjs";
import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs";
//#region src/process/command-error.ts
const COMMAND_ERROR_NAME = "CommandExecutionError";
const COMMAND_ERROR_HEADER_RE = /^(?:Error:\s*)?.+ failed \((?:timed out after [\d.]+ seconds|timed out waiting for output|output limit exceeded|exit code -?\d+|terminated|signal SIG[A-Z0-9]+)(?:; signal SIG[A-Z0-9]+)?\):?$/u;
function formatCommandErrorForUser(e) {
const t = e.trim().split(/\r?\n/u);
const n = t[0] ?? "";
if (!n.startsWith(`${COMMAND_ERROR_NAME}: `) && !COMMAND_ERROR_HEADER_RE.test(n)) return;
const r = [n, t.length > 1 ? t.at(-1)?.trim() : void 0].filter(Boolean).join(" ").replace(/\s+/gu, " ").trim();
return r.length > 500 ? `${truncateUtf16Safe(r, 497)}...` : r;
}
function formatCommandOutput(e, t = 800) {
const n = stripAnsi(e.toString()).replace(/(^|[^\r])\r+(?=\n|$)/g, "$1").replace(/(^|\n)[^\n]*\r/g, "$1").trim().split("\n");
const r = n.slice(-12).map(sanitizeTerminalText).join("\n");
return `${n.length > 12 || r.length > t ? "…\n" : ""}${sliceUtf16Safe(r, Math.max(0, r.length - t))}`;
}
/** Use an operation label, never argv that may contain credentials. */
function formatCommandResult(e, t) {
const n = truncateUtf16Safe(sanitizeForLog(e.replace(/[\r\n]+/g, " ")), 256);
const r = t.outputLimitExceeded ? "output-limit" : t.termination;
const i = t.signal ? `, signal=${t.signal}` : "";
const a = t.killed ? ", killed=true" : "";
return [`${n} ${t.code === 0 ? "exited" : "failed"} (code=${t.code}, termination=${r}${i}${a})`, ...["stderr", "stdout"].flatMap((e) => {
const n = formatCommandOutput(t[e]);
return n ? [`${e}: ${n}`] : [];
})].join("\n");
}
function createCommandError(e, t, n) {
const r = formatCommandOutput(t.stderr, 2e3);
const i = formatCommandOutput(t.stdout, 2e3);
let a = r || i;
if (r && i) {
const e = 1983;
const t = Math.min(r.length, Math.max(Math.ceil(e / 2), e - i.length));
const tail = (e, t) => e.length <= t ? e : `…\n${sliceUtf16Safe(e, 2 - t)}`;
a = `stderr: ${tail(r, t)}\nstdout: ${tail(i, e - t)}`;
}
if (t.error) {
const e = formatCommandOutput(formatErrorMessage(t.error, { includeCode: true }), 2e3);
a = [a, e].filter(Boolean).join("\n");
}
const o = t.signal ? `signal ${t.signal}` : "";
const s = "outputLimitExceeded" in t && t.outputLimitExceeded ? "output limit exceeded" : "";
const c = s || (!o && t.code !== null ? `exit code ${t.code}` : "");
const l = [{
timeout: `timed out after ${n.timeoutMs / 1e3} seconds`,
"no-output-timeout": "timed out waiting for output",
"output-limit": "output limit exceeded",
exit: c,
error: c,
signal: s || (!o ? "terminated" : "")
}[t.termination], o].filter(Boolean).join("; ");
const u = truncateUtf16Safe(stripAnsi(e).replace(/[\r\n]+/g, " "), 256);
const d = new Error(`${u} failed${l ? ` (${l})` : ""}${a ? `:\n${a}` : ""}`, t.error ? { cause: t.error } : void 0);
d.name = COMMAND_ERROR_NAME;
return d;
}
//#endregion
export { formatCommandResult as i, formatCommandErrorForUser as n, formatCommandOutput as r, createCommandError as t };