@sema-agent/core
Version:
Stateless, task-oriented AI agent core
79 lines (78 loc) • 3.47 kB
JavaScript
import { mediaRemovedPlaceholder } from "../internal/llm.js";
import { claimedMediaType } from "./image-admission.js";
import { readMember } from "./thrown-value.js";
const mediaTyped = (b) => claimedMediaType(b) !== undefined;
export function isMediaBlock(block) {
return mediaTyped(block) && typeof readMember(block, "data") === "string";
}
export function mediaBlockDescriptor(block) {
return { type: block.type, ...(typeof block.mimeType === "string" && block.mimeType.length <= 255 ? { mimeType: block.mimeType } : {}), bytes: imageBytes(block), truncated: true };
}
export function omittedMediaDescriptor(blocks) {
return { type: "omitted", blocks: blocks.length, bytes: blocks.reduce((n, b) => n + imageBytes(b), 0), truncated: true };
}
export function imageBytes(b) {
return typeof b.data === "string" ? Math.floor((b.data.length * 3) / 4) : 0;
}
function blocksOf(m) {
const content = m.content;
return Array.isArray(content) ? content : undefined;
}
export function trimRequestMedia(messages, limits) {
const media = [];
const contents = messages.map(blocksOf);
for (const [mi, blocks] of contents.entries()) {
const ordinal = { image: 0, document: 0 };
for (const [bi, b] of (blocks ?? []).entries()) {
const type = claimedMediaType(b);
if (type === undefined)
continue;
const data = readMember(b, "data");
media.push({ message: mi, block: bi, type, ordinal: ++ordinal[type], data: typeof data === "string" ? data : "" });
}
}
const sizes = media.map((m) => m.data.length);
const base64Total = sizes.reduce((n, s) => n + s, 0);
const cut = new Set();
const kept = [];
const keepAfterCountCut = Math.max(0, limits.maxItems - limits.margin);
let keptChars = 0;
for (const [i, size] of sizes.entries()) {
kept.push(i);
keptChars += size;
if (kept.length > limits.maxItems) {
for (const j of kept.splice(0, kept.length - keepAfterCountCut)) {
cut.add(j);
keptChars -= sizes[j];
}
}
if (keptChars > limits.budgetBase64Chars) {
for (let k = 0; k < kept.length && keptChars > limits.budgetBase64Chars - limits.hysteresisBase64Chars;) {
const j = kept[k];
if (sizes[j] === 0) {
k++;
continue;
}
kept.splice(k, 1);
cut.add(j);
keptChars -= sizes[j];
}
}
}
if (cut.size === 0)
return { messages, removed: [], removedBase64Chars: 0, items: sizes.length, base64Chars: base64Total };
const removed = [];
const out = messages.slice();
const rewritten = new Map();
for (const [i, at] of media.entries()) {
if (!cut.has(i))
continue;
const blocks = rewritten.get(at.message) ?? contents[at.message].slice();
rewritten.set(at.message, blocks);
removed.push({ block: blocks[at.block], type: at.type, data: at.data });
blocks[at.block] = { type: "text", text: mediaRemovedPlaceholder("media_budget", at.type, at.ordinal) };
}
for (const [mi, content] of rewritten)
out[mi] = { ...messages[mi], content };
return { messages: out, removed, removedBase64Chars: removed.reduce((n, r) => n + r.data.length, 0), items: sizes.length, base64Chars: base64Total };
}