@sema-agent/core
Version:
Stateless, task-oriented AI agent core
79 lines (78 loc) • 4.51 kB
JavaScript
import { types } from "node:util";
import { closestName } from "./closest-name.js";
import { callerKey } from "./caller-text.js";
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;
}
function walkRefusal(message, member) {
const e = receiptRefusal(message);
if (member !== undefined)
e.member = member;
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 optionKeys() {
return ((table) => Object.freeze(Object.keys(table)));
}
export function receiveOptions(given, owner, keys) {
return given === undefined ? Object.freeze({}) : receiveMembers(given, { subject: `${owner} options`, unit: "option", moment: `at new ${owner}` }, keys);
}
export function receiveOwnProperties(given, words) {
admitPlainRecord(given, words);
return readCaptured(given, words);
}
export function strayMembersOf(given, words, keys) {
const names = Reflect.ownKeys(given).filter((k) => typeof k === "symbol" || !keys.includes(k));
return names.length === 0 ? undefined : Object.freeze({ names: Object.freeze(names), sentence: strayMembersSentence(words, names, keys) });
}
export function memberPresent(record, name) {
return Reflect.get(record, name) !== undefined;
}
export function refuseStrayMembers(given, words, keys, refusal = receiptRefusal) {
admitPlainRecord(given, words);
const stray = strayMembersOf(given, words, keys);
if (stray !== undefined)
throw refusal(stray.sentence, stray.names);
}
export function receiveMembers(given, words, keys, refusal = receiptRefusal) {
refuseStrayMembers(given, words, keys, refusal);
return Object.freeze(readCaptured(given, words));
}
function admitPlainRecord(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`);
}
}
function readCaptured(given, words) {
const received = receive(given, CONFIG_RECEIPT_MODE);
if (received.ok)
return received.value;
const member = received.fault.path[0];
const name = member ?? "";
if (received.fault.kind === "getter-threw")
throw walkRefusal(`${words.subject}.${name} threw while it was read ${words.moment} — a ${words.unit} is read once, here; refused rather than wired without it`, member);
if (received.fault.kind === "prototype-moved")
throw walkRefusal(`${words.subject}'s prototype changed while its ${words.unit}s were read ${words.moment} — refused rather than wired from a moving object`, member);
throw walkRefusal(`${words.subject} could not be read ${words.moment}: ${describeFault(received.fault)} — refused rather than wired without it`, member);
}
function strayMembersSentence(words, stray, keys) {
const named = stray.map((k) => {
const near = typeof k === "symbol" ? undefined : closestName(k, keys);
return near === undefined ? callerKey(k) : `${callerKey(k)} (did you mean ${JSON.stringify(near)}?)`;
});
const one = stray.length === 1;
const unit = words.unit;
const what = one ? `is not ${/^[aeiou]/i.test(unit) ? "an" : "a"} ${unit}` : `are not ${unit}s`;
const takes = keys.length === 0 ? `it takes no ${unit}s` : `the ${unit}s it takes are ${keys.map((k) => JSON.stringify(k)).join(", ")}`;
return `${words.subject}: ${named.join(", ")} ${what} it takes — ${takes}; refused ${words.moment} rather than read as absent (a misspelled ${unit} would leave its default in force and nothing would say so)`;
}