UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

34 lines (33 loc) • 1.13 kB
export const MAX_TOOL_END_OUTPUT_CHARS = 16384; export const MAX_TOOL_CARD_CHARS = MAX_TOOL_END_OUTPUT_CHARS * 4; export function toolCardChars(details) { try { return (JSON.stringify(details) ?? "").length; } catch { return Number.POSITIVE_INFINITY; } } export function cardOnFrame(card, type, members) { if (toolCardChars(card) <= MAX_TOOL_CARD_CHARS) return { structured: card }; const skeleton = { type }; try { for (const name of members) { const path = name.split("."); let value = card; for (const key of path) value = value !== null && typeof value === "object" && Object.hasOwn(value, key) ? value[key] : undefined; if (value === undefined) continue; let into = skeleton; for (const key of path.slice(0, -1)) into = (into[key] ??= {}); into[path.at(-1)] = value; } } catch { return undefined; } return toolCardChars(skeleton) <= MAX_TOOL_CARD_CHARS ? { structuredSkeleton: skeleton } : undefined; }