@sema-agent/core
Version:
Stateless, task-oriented AI agent core
94 lines (93 loc) • 4.08 kB
JavaScript
export const MEDIA_TYPE_ECHO_MAX = 100;
export function mediaTypeEcho(mime) {
return typeof mime === "string" && mime.length <= MEDIA_TYPE_ECHO_MAX && /^[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}\/[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}$/.test(mime) ? mime : undefined;
}
export function mediaTypePhrase(mime) {
const echo = mediaTypeEcho(mime);
if (echo !== undefined)
return echo;
return typeof mime === "string" && mime !== "" ? `a ${[...mime].length}-character type` : "unknown type";
}
export const TOOL_RESULT_BYTES_KEEP_LIMIT = 104_857_600;
export const SAVE_TOOL_RESULT_NAME = "SaveToolResult";
export function keptBytesShown(kept) {
if (!("saveError" in kept))
return kept;
const { saveError: _storeWords, ...shown } = kept;
return shown;
}
export function noteKeptPiece(pieces, base64, kept) {
if (!pieces.has(base64))
pieces.set(base64, kept);
}
export function withKeptPieces(answer, pieces) {
if (pieces.size === 0)
return answer;
const onStack = new Set();
const done = new Map();
const walk = (value) => {
if (typeof value === "string") {
const kept = pieces.get(value);
return kept === undefined ? value : { kept: keptBytesShown(kept) };
}
if (typeof value !== "object" || value === null || onStack.has(value))
return value;
if (done.has(value))
return done.get(value);
const array = Array.isArray(value);
const proto = Object.getPrototypeOf(value);
if (!array && proto !== Object.prototype && proto !== null)
return value;
onStack.add(value);
try {
let changed = false;
const out = array ? new Array(value.length) : proto === null ? Object.create(null) : {};
for (const [key, member] of Object.entries(value)) {
const next = walk(member);
if (next !== member)
changed = true;
Object.defineProperty(out, key, { value: next, enumerable: true, writable: true, configurable: true });
}
const walked = changed ? out : value;
done.set(value, walked);
return walked;
}
finally {
onStack.delete(value);
}
};
try {
return walk(answer);
}
catch {
return answer;
}
}
export function saveToolResultReach(mounted, callable) {
if (!mounted.some((t) => t.name === SAVE_TOOL_RESULT_NAME))
return "absent";
return callable === undefined || callable.has(SAVE_TOOL_RESULT_NAME) ? "callable" : "deferred";
}
export function keptBytesFacts(bytes, mimeType) {
const echo = mediaTypeEcho(mimeType);
return { bytes: bytes.byteLength, ...(echo !== undefined ? { mimeType: echo } : {}) };
}
export const UNRETRIEVABLE_OUTPUT = Object.freeze({
gone: "This output cannot be retrieved here; any preview you saw is all that remains. If you need it, get it again only when that repeats no action (a read or a query); " +
"otherwise do not repeat the call to get it back — tell the user it can no longer be retrieved.",
notKept: "they cannot be retrieved here — if you need them, do not repeat the call to get them back; tell the user they could not be kept",
});
export function notKeptClause(kept) {
const why = kept.notKept === "no_byte_store"
? "this run keeps no binary tool results"
: kept.notKept === "over_limit"
? `${kept.bytes} bytes is over the ${kept.limit}-byte keep limit (a limit of this run's tool-result storage, not of the tool: ask for a smaller result)`
: "the tool-result store could not take them";
return `its bytes were not kept: ${why}; ${UNRETRIEVABLE_OUTPUT.notKept}`;
}
export function keepBytesWith(keeper, bytes, mimeType) {
if (keeper !== undefined)
return keeper(bytes, mimeType);
const kept = { ...keptBytesFacts(bytes, mimeType), notKept: "no_byte_store" };
return Promise.resolve({ kept, clause: notKeptClause(kept) });
}