@sema-agent/core
Version:
Stateless, task-oriented AI agent core
38 lines (37 loc) • 1.48 kB
JavaScript
import { callerText, dataKind } from "@sema-agent/primitives";
import { INLINE_TRUNCATION_MARK, inlineUntrusted } from "./untrusted-text.js";
export { callerText, dataKind };
export const CALLER_KEY_MAX = 80;
const CALLER_KEY_READ = 4096;
function quotedSpelling(text) {
const read = text.length > CALLER_KEY_READ ? text.slice(0, CALLER_KEY_READ) : text;
const folded = inlineUntrusted(read, Number.POSITIVE_INFINITY);
let body = "";
let used = 0;
let cut = read.length < text.length;
for (const character of folded) {
const piece = JSON.stringify(character).slice(1, -1);
const width = [...piece].length;
if (used + width > CALLER_KEY_MAX) {
cut = true;
break;
}
body += piece;
used += width;
}
return { quoted: `"${body}${cut ? INLINE_TRUNCATION_MARK : ""}"`, whole: !cut && folded === text };
}
const sizeOf = (text) => ` (${text.length} UTF-16 unit${text.length === 1 ? "" : "s"})`;
export function callerKey(key) {
if (typeof key === "symbol") {
const description = key.description;
if (description === undefined)
return "Symbol()";
const { quoted, whole } = quotedSpelling(description);
return `Symbol(${quoted})${whole ? "" : sizeOf(description)}`;
}
if (typeof key !== "string")
return dataKind(key);
const { quoted, whole } = quotedSpelling(key);
return `${quoted}${whole ? "" : sizeOf(key)}`;
}