UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

79 lines (78 loc) • 3.72 kB
import { describeRejectedValue } from "../tool-search-setting.js"; import { isAliasModelId, screenOpenAiCompatChatSeat } from "../memory-engine/distiller.js"; import { screenConsolidationDriverKnobs } from "../memory-engine/consolidation-driver.js"; import { memberPresent, receiptRefusal, receiveMembers } from "../own-property-receipt.js"; export function consolidationSeatRefusal(message) { const e = new Error(message); e.code = "config.memory_consolidation_driver"; return e; } export function bareChatRefusal(model) { return consolidationSeatRefusal(`memoryConsolidationDriver.chat is a bare function, so memoryConsolidationDriver.model must carry the EXPLICIT model id it serves (got ${describeRejectedValue(model)}).`); } export const explicitModelId = (model) => typeof model === "string" && model.trim() !== "" && !isAliasModelId(model); const CONSOLIDATION_DRIVER_SEAT_MEMBERS = Object.freeze(["chat", "model", "contract", "maxCycles", "mintRepairBudget"]); function endpointTripleOf(chat, field, moment) { const copy = {}; for (const member of ["baseUrl", "apiKey", "model"]) { try { copy[member] = Reflect.get(chat, member); } catch { throw receiptRefusal(`${field}.chat threw while its ${member} was read ${moment} — refused (the thrown value is not repeated)`); } } return Object.freeze(copy); } function frozenDataCopy(value, field) { let copy; try { copy = structuredClone(value); } catch { throw consolidationSeatRefusal(`${field} is not plain data (a contract is strings, numbers and lists of them) — refused rather than held by reference.`); } const freeze = (v) => { if (typeof v !== "object" || v === null || Object.isFrozen(v)) return; Object.freeze(v); for (const member of Object.values(v)) freeze(member); }; freeze(copy); return copy; } export function receiveConsolidationDriverSeat(value, field, moment) { if (value === undefined) return undefined; if (value === null || typeof value !== "object" || Array.isArray(value)) { throw consolidationSeatRefusal(`${field} must be a plain object of the seat's members (${CONSOLIDATION_DRIVER_SEAT_MEMBERS.join(", ")}) or undefined to clear it (got ${describeRejectedValue(value)}).`); } const given = receiveMembers(value, { subject: field, unit: "member", moment }, CONSOLIDATION_DRIVER_SEAT_MEMBERS, consolidationSeatRefusal); const seat = {}; for (const member of CONSOLIDATION_DRIVER_SEAT_MEMBERS) if (memberPresent(given, member)) seat[member] = given[member]; const chat = seat.chat; if (chat !== undefined && typeof chat !== "function") { if (chat === null || typeof chat !== "object" || Array.isArray(chat)) { throw consolidationSeatRefusal(`${field}.chat must be an endpoint triple { baseUrl, apiKey, model } or a chat function (got ${describeRejectedValue(chat)}).`); } seat.chat = endpointTripleOf(chat, field, moment); } if (seat.contract !== undefined) seat.contract = frozenDataCopy(seat.contract, `${field}.contract`); return Object.freeze(seat); } export function screenConsolidationDriverSeat(seat, field, plaintextOrigins) { if (seat === undefined) return; const chat = seat.chat; if (typeof chat === "function") { if (!explicitModelId(seat.model)) throw bareChatRefusal(seat.model); } else if (chat !== undefined) screenOpenAiCompatChatSeat(chat, plaintextOrigins); screenConsolidationDriverKnobs(seat, { subject: field, contract: `${field}.contract` }); }