@sema-agent/core
Version:
Stateless, task-oriented AI agent core
163 lines (162 loc) • 7.5 kB
JavaScript
import { describe, expandOnSide, envReach, MOD_SEATS, SEAT_TIER_TABLE, } from "./events.js";
import { resolveModBudgetMs } from "./budget.js";
export const MODS_INVALID_CODE = "config.mods_invalid";
export const MODS_REGISTRATION_REFUSED_CODE = "mods.registration_refused";
export const MODS_REGISTRATION_REFUSED_DEPLOYMENT_CODE = "mods.registration_refused_deployment";
export const ENGINE_ORIGIN = Object.freeze({ plugin: "engine", tier: "core" });
export const MOD_REGISTRATION_NAME = /^[A-Za-z0-9._:-]{1,64}$/;
export function namedRegistration(registration) {
return registration === undefined || registration === ENGINE_ORIGIN.plugin ? undefined : registration;
}
export const MOD_TRACE_OUTCOMES = Object.freeze(["returned", "passed", "rejected", "caught", "expired", "kept", "skipped", "bypassed"]);
export function modsInvalid(message) {
return Object.assign(new Error(message), { code: MODS_INVALID_CODE });
}
const TIER_ORDER = ["prepend", "user", "append"];
export function screenModSeats(seats, side, report) {
const out = [];
const names = new Set();
for (const seat of MOD_SEATS) {
const list = seats[seat];
if (list === undefined)
continue;
if (!Array.isArray(list))
throw modsInvalid(`the ${seat} seat's mods must be a list of registrations, not ${describe(list)}`);
for (const item of list) {
const screened = screenOne(item, seat, side, report);
if (names.has(screened.name))
throw modsInvalid(`two registrations are named "${screened.name}" — a registration name is unique in the run`);
names.add(screened.name);
out.push(screened);
}
}
return out.sort((a, b) => TIER_ORDER.indexOf(a.tier) - TIER_ORDER.indexOf(b.tier));
}
export function seatsReachEnv(seats, side) {
const seen = { always: false, on_call: false };
let open = false;
for (const seat of MOD_SEATS) {
const list = seats[seat];
if (list === undefined)
continue;
if (!Array.isArray(list)) {
open = true;
continue;
}
for (const item of list) {
try {
if (!isRegistration(item))
throw modsInvalid("not a registration");
const uses = usesOf(item.uses, String(item.name));
for (const field of ["calls", "events"]) {
for (const p of uses[field])
for (const n of namesOf(p, String(item.name), `uses.${field}`, side)) {
const reach = envReach(n, field);
if (reach !== undefined)
seen[reach] = true;
}
}
}
catch {
open = true;
}
}
}
const answer = (yes) => (yes ? true : open ? undefined : false);
return { always: answer(seen.always), onCall: answer(seen.on_call) };
}
function isRegistration(v) {
return typeof v === "object" && v !== null && typeof Reflect.get(v, "name") === "string" && typeof Reflect.get(v, "register") === "function";
}
function screenOne(item, seat, side, report) {
if (!isRegistration(item))
throw modsInvalid(`a ${seat} mods entry is ${describe(item)}; a registration has a name and a register function`);
const name = item.name;
if (typeof name !== "string")
throw modsInvalid(`a ${seat} registration's name is ${describe(name)}; a registration name is a string`);
if (name === "")
throw modsInvalid(`a ${seat} registration has an empty name`);
if (!MOD_REGISTRATION_NAME.test(name))
throw modsInvalid(`a ${seat} registration's name (${name.length} characters) is outside the form of a registration name — 1 to 64 of letters, digits, ".", "_", ":" and "-"`);
if (name === ENGINE_ORIGIN.plugin)
throw modsInvalid(`a ${seat} registration is named "${name}", the engine's own origin word — choose another name`);
const tier = tierOf(item.tier, seat, name);
const uses = usesOf(item.uses, name);
const provenance = item.provenance;
if (provenance !== undefined)
refuseProvenance(provenance, name);
const failure = item.failure;
if (failure !== undefined && failure !== "skip")
throw modsInvalid(`${name}: failure is ${describe(failure)}; it is "skip" or absent`);
const eventsBound = new Set();
for (const p of uses.events)
for (const n of namesOf(p, name, "uses.events", side))
eventsBound.add(n);
const callsBound = new Set();
for (const c of uses.calls)
for (const n of namesOf(c, name, "uses.calls", side))
callsBound.add(n);
return {
registration: item,
name,
seat,
tier,
budgetMs: resolveModBudgetMs(item.budgetMs, report, name),
failureSkips: failure === "skip",
uses,
...(provenance !== undefined ? { provenance: provenance } : {}),
eventsBound,
callsBound,
};
}
const MOD_PROVENANCE_MAX = 256;
const NOT_ONE_LINE = /[\p{Cc}\p{Zl}\p{Zp}]/u;
function refuseProvenance(provenance, name) {
if (typeof provenance !== "string")
throw modsInvalid(`${name}: provenance is ${describe(provenance)}; it is a label (a string)`);
const length = [...provenance].length;
const form = `a provenance label is one line of 1 to ${MOD_PROVENANCE_MAX} characters, with no control character or line break`;
if (NOT_ONE_LINE.test(provenance))
throw modsInvalid(`${name}: provenance (${length} characters) holds a control character or a line break; ${form}`);
if (length === 0)
throw modsInvalid(`${name}: provenance is empty; ${form} (leave it out to give none)`);
if (length > MOD_PROVENANCE_MAX)
throw modsInvalid(`${name}: provenance is ${length} characters long; ${form}`);
}
function tierOf(declared, seat, name) {
const row = SEAT_TIER_TABLE[seat];
const allowed = row.tiers;
if (declared === undefined) {
if (row.declared === "required")
throw modsInvalid(`${name}: a ${seat} registration declares its tier (${allowed.join(", ")})`);
return allowed[0];
}
const tier = allowed.find((t) => t === declared);
if (tier === undefined)
throw modsInvalid(`${name}: a ${seat} registration's tier is ${allowed.join(" or ")}, not ${describe(declared)}`);
return tier;
}
function usesOf(uses, name) {
if (typeof uses !== "object" || uses === null)
throw modsInvalid(`${name}: uses is ${describe(uses)}; it declares { events, calls }`);
const events = stringList(Reflect.get(uses, "events"), name, "uses.events");
const calls = stringList(Reflect.get(uses, "calls"), name, "uses.calls");
return Object.freeze({ events, calls });
}
function stringList(v, name, field) {
if (!Array.isArray(v))
throw modsInvalid(`${name}: ${field} is ${describe(v)}; it is a list of event names`);
const out = [];
for (const x of v) {
if (typeof x !== "string")
throw modsInvalid(`${name}: ${field} holds ${describe(x)}; it is a list of event names`);
out.push(x);
}
return Object.freeze(out);
}
function namesOf(pattern, name, field, side) {
const expanded = expandOnSide(pattern, side);
if ("refusal" in expanded)
throw modsInvalid(`${name}: ${field}: ${expanded.refusal}`);
return expanded.names;
}