UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

60 lines (59 loc) • 2.55 kB
import { receiveMembers } from "./own-property-receipt.js"; import { describeRejectedValue } from "./tool-search-setting.js"; const AGE_KNOBS = ["maxAgeMs", "staleRunningMaxAgeMs"]; const COUNT_KNOBS = ["keep", "maxEntries"]; export function refuseRetentionPolicy(label, knob, value, requirement) { const e = new Error(`${label}: ${knob} must be ${requirement} (got ${describeRejectedValue(value)}) — a retention bound that cannot be evaluated silently decides what to delete instead of bounding it`); e.code = "config.retention_policy_invalid"; throw e; } function invalidPolicy(label, knob, value, requirement) { refuseRetentionPolicy(label, knob, value, requirement); } export function ageCutoff(label, now, opts) { const { maxAgeMs } = receiveRetentionPolicy(label, opts, ["maxAgeMs"]); if (typeof now !== "number" || !Number.isFinite(now)) refuseRetentionPolicy(label, "now", now, "a finite number of milliseconds"); return maxAgeMs === undefined ? undefined : now - maxAgeMs; } export function retentionKnobRefusal(message) { const e = new Error(message); e.code = "config.retention_policy_invalid"; return e; } export function receiveRetentionPolicy(label, opts, accepts, beside = []) { if (opts === undefined) return Object.freeze({}); if (typeof opts !== "object" || opts === null) refuseRetentionPolicy(label, "opts", opts, "an object of retention knobs or absent"); const got = receiveMembers(opts, { subject: `${label} policy`, unit: "retention knob", moment: "before anything is judged or deleted" }, [...accepts, ...beside], retentionKnobRefusal); const held = {}; for (const knob of accepts) { const v = got[knob]; if (v !== undefined) held[knob] = v; } assertRetentionPolicy(label, held); for (const name of beside) { const v = got[name]; if (v !== undefined) held[name] = v; } return Object.freeze(held); } export function assertRetentionPolicy(label, opts) { if (opts === undefined) return; for (const knob of AGE_KNOBS) { const v = opts[knob]; if (v !== undefined && (!Number.isFinite(v) || v < 0)) { invalidPolicy(label, knob, v, "a finite, non-negative number of milliseconds"); } } for (const knob of COUNT_KNOBS) { const v = opts[knob]; if (v !== undefined && (!Number.isInteger(v) || v < 0)) { invalidPolicy(label, knob, v, "a whole number of rows, 0 or more"); } } }