UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

27 lines (26 loc) • 1.05 kB
export const APPROVAL_DISPLAY_MAX_CHARS = 16_384; const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f]/g; const neutralize = (text) => text.replace(CONTROL, "␀"); export const sanitizeApprovalDisplay = (node, depth = 0) => { if (depth > 6) return undefined; if (typeof node === "string") return neutralize(node); if (node === null || typeof node !== "object") return node; if (Array.isArray(node)) return node.map((v) => sanitizeApprovalDisplay(v, depth + 1)); const out = {}; for (const [k, v] of Object.entries(node)) { out[`${sanitizeApprovalDisplay(k, depth + 1)}`] = sanitizeApprovalDisplay(v, depth + 1); } return out; }; export function approvalMessageOf(message) { if (typeof message !== "string" || message.length === 0) return undefined; const serialized = JSON.stringify(message).length; if (serialized > APPROVAL_DISPLAY_MAX_CHARS) return `approval message exceeded 16KiB (${serialized} chars serialized)`; return neutralize(message); }