openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 2.91 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as countStreamingFileMutationLines, t as resolveFileMutationToolName } from "./tool-mutation-names-DGg7FEBx.mjs";
import { parseStreamingJson } from "@openclaw/ai/internal/runtime";
//#region src/agents/embedded-agent-live-edit-diff.ts
const LIVE_EDIT_DIFF_MIN_INTERVAL_MS = 250;
const LIVE_EDIT_DIFF_MAX_PARTIAL_JSON_CHARS = 1048576;
const LIVE_EDIT_DIFF_MAX_TRACKED_CALLS = 64;
function readToolCallBlock(e) {
const t = e.contentIndex;
const n = e.partial;
if (typeof t !== "number" || !Number.isInteger(t) || t < 0 || !n || typeof n !== "object") return;
const r = n.content;
return asOptionalRecord(Array.isArray(r) ? r[t] : void 0);
}
function readToolCallId(e) {
const t = asOptionalRecord(e.toolCall)?.id;
if (typeof t === "string" && t) return t;
const n = readToolCallBlock(e);
return typeof n?.id === "string" && n.id ? n.id : void 0;
}
/** Update one run's bounded best-effort diff counters from a model stream event. */
function updateLiveEditDiffProgress(e, t) {
if (!t) return;
const n = t.type;
if (n === "toolcall_end") {
const n = readToolCallId(t);
if (n) e.delete(n);
return;
}
if (n !== "toolcall_delta") return;
const r = readToolCallBlock(t);
const i = typeof r?.id === "string" ? r.id : "";
const a = typeof r?.name === "string" ? r.name : "";
const o = typeof r?.partialJson === "string" ? r.partialJson : "";
return updateLiveEditDiffProgressFromInput(e, {
toolCallId: i,
name: a,
partialJsonLength: o.length,
readPartialJson: () => o
});
}
/** Share bounded counting without assembling cumulative CLI arguments before admission. */
function updateLiveEditDiffProgressFromInput(e, t) {
const { toolCallId: n, name: r, partialJsonLength: i } = t;
const a = resolveFileMutationToolName(r);
if (!n || !a || !i) return;
if (i > LIVE_EDIT_DIFF_MAX_PARTIAL_JSON_CHARS) {
e.delete(n);
return;
}
let o = e.get(n);
if (!o) {
if (e.size >= LIVE_EDIT_DIFF_MAX_TRACKED_CALLS) return;
o = {
added: 0,
removed: 0,
emittedAdded: 0,
emittedRemoved: 0,
lastCheckedAtMs: 0
};
e.set(n, o);
}
const s = Date.now();
if (o.lastCheckedAtMs > 0 && s - o.lastCheckedAtMs < LIVE_EDIT_DIFF_MIN_INTERVAL_MS) return;
o.lastCheckedAtMs = s;
const c = countStreamingFileMutationLines(a, parseStreamingJson(t.readPartialJson()));
o.added = Math.max(o.added, c.added);
o.removed = Math.max(o.removed, c.removed);
if (o.added === o.emittedAdded && o.removed === o.emittedRemoved) return;
o.emittedAdded = o.added;
o.emittedRemoved = o.removed;
return {
toolCallId: n,
name: normalizeLowercaseStringOrEmpty(r),
diff: {
added: o.added,
removed: o.removed
}
};
}
//#endregion
export { updateLiveEditDiffProgressFromInput as n, updateLiveEditDiffProgress as t };