@sema-agent/core
Version:
Stateless, task-oriented AI agent core
112 lines (111 loc) • 5.03 kB
JavaScript
export const MODEL_CAPABILITIES = Object.freeze(["image", "reasoning", "fast_mode"]);
export const CAPABILITY_DECLARATIONS = Object.freeze(["declared_on", "declared_off", "unknown"]);
export const UNKNOWN_CAPABILITY_READS = Object.freeze({
image: Object.freeze({ engine: "held" }),
reasoning: Object.freeze({ engine: "not_held", requested: "tried" }),
fast_mode: Object.freeze({ requested: "not_tried" }),
});
const BOOLEAN_DECLARATIONS = Object.freeze({ reasoning: "reasoning", fast_mode: "fastMode" });
function typeWordOf(value) {
return value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
}
function nameOf(model) {
return typeof model.id === "string" ? `"${model.id}"` : "a model with no id";
}
export function capabilityOf(model, cap) {
if (cap !== "image") {
const member = BOOLEAN_DECLARATIONS[cap];
const declared = model[member];
if (declared === undefined)
return "unknown";
if (typeof declared !== "boolean") {
throw new TypeError(`Model.${member} of ${nameOf(model)} must be true, false or absent; got ${typeWordOf(declared)}`);
}
return declared ? "declared_on" : "declared_off";
}
const input = model.input;
if (input === undefined)
return "unknown";
if (!Array.isArray(input) || !input.every((m) => typeof m === "string")) {
throw new TypeError(`Model.input of ${nameOf(model)} must be a list of input kinds or absent; got ${Array.isArray(input) ? "a list holding a non-string" : typeWordOf(input)}`);
}
return input.includes("image") ? "declared_on" : "declared_off";
}
export function capabilityHeld(model, cap) {
const declared = capabilityOf(model, cap);
return declared === "declared_on" || (declared === "unknown" && UNKNOWN_CAPABILITY_READS[cap].engine === "held");
}
export function capabilityTried(model, cap, withheld = NONE_WITHHELD) {
const declared = capabilityOf(model, cap);
return declared === "declared_on" || (declared === "unknown" && UNKNOWN_CAPABILITY_READS[cap].requested === "tried" && !withheld.includes(cap));
}
const TRIED_CAPABILITIES = Object.freeze(MODEL_CAPABILITIES.filter((cap) => {
const cells = UNKNOWN_CAPABILITY_READS[cap];
return cells.requested === "tried";
}));
const NONE_WITHHELD = Object.freeze([]);
const CAPABILITY_LATCH_MAX = 256;
const CAPABILITY_CURES_TO_REMEMBER = 2;
const CAPABILITY_LATCH_TABLE = Symbol.for("sema.core.modelCapabilityLatch");
function capabilityLatchTable() {
const held = Reflect.get(globalThis, CAPABILITY_LATCH_TABLE);
if (held instanceof Map)
return held;
const table = new Map();
Reflect.set(globalThis, CAPABILITY_LATCH_TABLE, table);
return table;
}
function touchLatch(table, key, entry) {
table.delete(key);
table.set(key, entry);
while (table.size > CAPABILITY_LATCH_MAX) {
const oldest = table.keys().next().value;
if (oldest === undefined)
break;
table.delete(oldest);
}
}
function capabilityLatchKey(model, url, speed, cap) {
if (!URL.canParse(url))
return undefined;
const u = new URL(url);
return JSON.stringify([model.id, model.api, `${u.origin}${u.pathname}`, speed, cap]);
}
export function noteCapabilityCured(model, url, speed, cap, memory) {
const key = capabilityLatchKey(model, url, speed, cap);
if (key === undefined)
return false;
const table = capabilityLatchTable();
const cures = Math.min((table.get(key)?.cures ?? 0) + 1, CAPABILITY_CURES_TO_REMEMBER);
touchLatch(table, key, Object.freeze({ cures, why: memory.why }));
return cures >= CAPABILITY_CURES_TO_REMEMBER;
}
export function noteCapabilityBrokeOff(model, url, speed, cap) {
const key = capabilityLatchKey(model, url, speed, cap);
if (key !== undefined)
touchLatch(capabilityLatchTable(), key, Object.freeze({ cures: CAPABILITY_CURES_TO_REMEMBER, why: "broke_off" }));
}
export function noteCapabilityTaken(model, url, speed, cap) {
const key = capabilityLatchKey(model, url, speed, cap);
if (key !== undefined)
capabilityLatchTable().delete(key);
}
export function rememberedCapabilityWithhold(model, url, speed, cap) {
const key = capabilityLatchKey(model, url, speed, cap);
if (key === undefined)
return undefined;
const table = capabilityLatchTable();
const entry = table.get(key);
if (entry === undefined)
return undefined;
touchLatch(table, key, entry);
return entry.cures < CAPABILITY_CURES_TO_REMEMBER ? undefined : Object.freeze({ why: entry.why });
}
export function __resetCapabilityMemory() {
capabilityLatchTable().clear();
}
export function requestWithheld(model, url, speed, decided, tries) {
if (tries !== true)
return undefined;
return TRIED_CAPABILITIES.filter((cap) => capabilityOf(model, cap) === "unknown" && (decided !== undefined ? decided.includes(cap) : rememberedCapabilityWithhold(model, url, speed, cap) !== undefined));
}