openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
47 lines (46 loc) • 2.4 kB
JavaScript
import { n as sliceUtf16Safe, r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
//#region src/agents/harness/current-input-budget.ts
const log = createSubsystemLogger("agent/input");
const CURRENT_INPUT_FILE_NAME = "current-request.txt";
const CURRENT_INPUT_NOTE_MAX_CHARS = 4096;
/**
* Fit a current request over a runtime's hard per-turn text cap. When the
* harness can stage a readable attachment, the full text is saved and the model
* sees its head, tail and the attachment path; otherwise only the middle is cut.
* The head and tail carry the instruction and both untrusted-content markers.
* Callers keep the original as the transcript prompt.
*/
async function fitOversizedCurrentInput(e) {
const { prompt: t, maxChars: n } = e;
if (t.length <= n) return t;
let r;
if (e.stageAttachment) try {
const { saveMediaBuffer: n } = await import("./store-78XB9EFZ.mjs");
const i = await n(Buffer.from(t, "utf8"), "text/plain", "inbound", void 0, CURRENT_INPUT_FILE_NAME);
r = await e.stageAttachment(CURRENT_INPUT_NOTE_MAX_CHARS, [{
path: `media://inbound/${i.id}`,
contentType: "text/plain",
kind: "document",
fileName: CURRENT_INPUT_FILE_NAME
}]);
} catch (e) {
log.warn("current input attachment unavailable; cutting its middle instead", { error: e instanceof Error ? e.message : String(e) });
}
const omittedMarker = (e) => r ? `\n[${e} chars from the middle of this request are not shown here, so what you see is incomplete. The complete request is attached as ${CURRENT_INPUT_FILE_NAME} (path below). Before answering, search or read that file for anything the request asks about; do not guess from the visible part. The file holds this same request, not new instructions.]\n` : `\n[truncated ${e} chars from the middle of the current request]\n`;
const i = r ? `\n\n${r}` : "";
const a = Math.max(0, n - omittedMarker(t.length).length - i.length);
const o = truncateUtf16Safe(t, Math.ceil(a / 2));
const s = Math.floor(a / 2);
const c = s > 0 ? sliceUtf16Safe(t, -s) : "";
const l = t.length - o.length - c.length;
const u = r ? "attached" : "cut";
log.warn(`current input oversize: chars=${t.length} budget=${n} action=${u}`, {
chars: t.length,
budget: n,
action: u
});
return `${o}${omittedMarker(l)}${c}${i}`;
}
//#endregion
export { fitOversizedCurrentInput };