UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

106 lines (105 loc) • 3.97 kB
import { P as resolveIntegerOption, d as asPositiveFiniteNumber } from "./number-coercion-D6u-4YD8.mjs"; import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { n as truncateUtf8Suffix } from "./utf8-truncate-BH7Ecfuh.mjs"; import { n as encodeOwnedBuffer, t as copyOwnedBuffer } from "./owned-buffer-C1_N0F1K.mjs"; import process from "node:process"; import { isUtf8 } from "node:buffer"; import { StringDecoder } from "node:string_decoder"; //#region src/process/exec-output.ts const DEFAULT_COMMAND_OUTPUT_MAX_BYTES = 16777216; const MAX_PRESERVED_PENDING_LINE_BYTES = 8192; function createCapturedOutputBuffers() { return { chunks: [], bytes: 0, truncatedBytes: 0, preservedLines: [], decoder: new StringDecoder("utf8"), pendingLine: "" }; } function resolveMaxOutputBytes(e, t) { return resolveIntegerOption(asPositiveFiniteNumber(typeof e === "number" ? e : e?.[t]), DEFAULT_COMMAND_OUTPUT_MAX_BYTES, { min: 1 }); } function resolveOutputCapture(e, t) { return (typeof e === "string" ? e : e?.[t]) ?? "tail"; } function shouldTerminateOnOutput(e, t) { return typeof e === "boolean" ? e : e?.[t] === true; } function appendCapturedOutput(e, t, n, r) { if (r === "discard") { e.truncatedBytes += Buffer.byteLength(t); return; } const i = Buffer.isBuffer(t) ? t : encodeOwnedBuffer(t); if (r === "head") { const t = Math.max(0, n - e.bytes); if (t > 0) { const n = i.subarray(0, t); e.chunks.push(n.byteLength < i.byteLength ? copyOwnedBuffer(n) : n); e.bytes += n.byteLength; } e.truncatedBytes += Math.max(0, i.byteLength - t); return; } if (i.byteLength >= n) { e.chunks = [copyOwnedBuffer(i.subarray(i.byteLength - n))]; e.truncatedBytes += e.bytes + i.byteLength - n; e.bytes = n; return; } e.chunks.push(i); e.bytes += i.byteLength; while (e.bytes > n && e.chunks.length > 0) { const t = expectDefined(e.chunks[0], "chunks entry at 0"); const r = e.bytes - n; if (t.byteLength <= r) { e.chunks.shift(); e.bytes -= t.byteLength; e.truncatedBytes += t.byteLength; } else { e.chunks[0] = copyOwnedBuffer(t.subarray(r)); e.bytes -= r; e.truncatedBytes += r; } } } function trimTruncatedUtf8Boundary(e, t, n, r) { if (n === 0 || e.length === 0 || process.platform === "win32" && !r) return e; if (t === "tail") { let t = 0; while (t < e.length && (expectDefined(e[t], "buffer byte") & 192) === 128) t += 1; return e.subarray(t); } for (let t = 0; t <= 3 && t <= e.length; t += 1) { const n = e.length - t; if (isUtf8(e.subarray(0, n))) return e.subarray(0, n); } return e; } function finalizeCapturedOutput(e, t, n = false) { const r = Buffer.concat(e.chunks, e.bytes); const i = trimTruncatedUtf8Boundary(r, t, e.truncatedBytes, n); e.truncatedBytes += r.byteLength - i.byteLength; return i; } function appendPreservedOutputLines(e) { const { capture: t, maxPreservedOutputLines: n, preserveOutputLine: r } = e; if (!r || t.preservedLines.length >= n) return; const i = Buffer.isBuffer(e.chunk) ? t.decoder.write(e.chunk) : e.chunk; if (!i) return; const a = (t.pendingLine + i).split(/\r?\n/); t.pendingLine = truncateUtf8Suffix(a.pop() ?? "", e.maxPendingLineBytes); for (const i of a) if (t.preservedLines.length < n && r(i, e.stream)) t.preservedLines.push(i); } function flushPreservedOutputLine(e) { const { capture: t, maxPreservedOutputLines: n, preserveOutputLine: r } = e; if (!r || n <= 0) return; const i = truncateUtf8Suffix(t.pendingLine + t.decoder.end(), e.maxPendingLineBytes); t.pendingLine = ""; if (i && t.preservedLines.length < n && r(i, e.stream)) t.preservedLines.push(i); } //#endregion export { finalizeCapturedOutput as a, resolveOutputCapture as c, createCapturedOutputBuffers as i, shouldTerminateOnOutput as l, appendCapturedOutput as n, flushPreservedOutputLine as o, appendPreservedOutputLines as r, resolveMaxOutputBytes as s, MAX_PRESERVED_PENDING_LINE_BYTES as t };