@sema-agent/core
Version:
Stateless, task-oriented AI agent core
30 lines (29 loc) • 2.1 kB
JavaScript
import { types } from "node:util";
import { describeFault, isPlainRecord, receive } from "./mods/kernel/receive.js";
import { MODS_VALUE_DEPTH } from "./mods/kernel/site-rules.js";
export function receiptRefusal(message) {
const e = new Error(message);
e.code = "config.invalid_argument";
return e;
}
export function refuseProxy(given, words) {
if (types.isProxy(given))
throw receiptRefusal(`${words.subject} must be a plain object, not a Proxy: its ${words.unit}s are read once off its own descriptors, and a Proxy would run its traps in that read — refused rather than wired through it`);
}
export const CONFIG_RECEIPT_MODE = Object.freeze({ accessors: "call-once", depth: 1, others: "as-is", isProxy: (v) => types.isProxy(v) });
export const DECLARATION_RECEIPT_MODE = Object.freeze({ accessors: "call-once", depth: MODS_VALUE_DEPTH + 1, others: "refuse", freeze: true, isProxy: (v) => types.isProxy(v) });
export function receiveOwnProperties(given, words) {
refuseProxy(given, words);
if (!isPlainRecord(given)) {
throw receiptRefusal(`${words.subject} must be a plain object (its prototype is neither Object.prototype nor null): every ${words.unit} is read once as an own property, and a ${words.unit} its prototype would supply is not read — refused rather than wired without it`);
}
const received = receive(given, CONFIG_RECEIPT_MODE);
if (received.ok)
return received.value;
const name = received.fault.path[0] ?? "";
if (received.fault.kind === "getter-threw")
throw receiptRefusal(`${words.subject}.${name} threw while it was read ${words.moment} — a ${words.unit} is read once, here; refused rather than wired without it`);
if (received.fault.kind === "prototype-moved")
throw receiptRefusal(`${words.subject}'s prototype changed while its ${words.unit}s were read ${words.moment} — refused rather than wired from a moving object`);
throw receiptRefusal(`${words.subject} could not be read ${words.moment}: ${describeFault(received.fault)} — refused rather than wired without it`);
}