@sema-agent/core
Version:
Stateless, task-oriented AI agent core
61 lines (60 loc) • 3.51 kB
JavaScript
import { bindingShown, receiveModelCatalog } from "../roles.js";
import { assertAutoConsolidationWirable } from "./memory-consolidation-driver.js";
import { receiveRoleMapPriced, resolveRunModel } from "./seat-model-doors.js";
export const ROLE_RESOLUTION_TABLE = Object.freeze([
Object.freeze({ role: "consolidate", when: "deployment", hop: "src/core/runner/memory-consolidation-driver.ts :: assertAutoConsolidationWirable", absence: "throws" }),
Object.freeze({ role: "default", when: "per-task", hop: "src/core/runner/seat-model-doors.ts :: resolveRunModel", absence: "throws" }),
]);
const HOPS = {
consolidate: (_task, deps) => assertAutoConsolidationWirable({ ...deps, memoryConsolidation: {}, autoRunOnRecommendation: true }),
default: (task, deps) => void resolveRunModel(task, deps),
};
const WHEN_WORDS = ["per-task", "deployment"];
function argumentRefusal(message) {
const e = new Error(message);
e.code = "config.invalid_argument";
return e;
}
function refusalCode(thrown) {
const code = thrown !== null && typeof thrown === "object" ? thrown.code : undefined;
return typeof code === "string" ? code : undefined;
}
function kindOf(value) {
return value === null ? "null" : Array.isArray(value) ? "a list" : typeof value;
}
export function modelFaceResolvable(face, options = {}) {
if (typeof face !== "object" || face === null || Array.isArray(face))
throw argumentRefusal(`modelFaceResolvable: the face must be an object { models?, tiers?, roles?, pricing? } (got ${kindOf(face)}).`);
if (typeof options !== "object" || options === null)
throw argumentRefusal(`modelFaceResolvable: the options must be an object { spec?, when? } when present (got ${kindOf(options)}).`);
const when = options.when ?? ["per-task"];
if (!Array.isArray(when))
throw argumentRefusal(`modelFaceResolvable: options.when must be a list of row groups (${WHEN_WORDS.join(" / ")}) when present (got ${kindOf(when)}).`);
for (const word of when) {
if (!WHEN_WORDS.includes(word))
throw argumentRefusal(`modelFaceResolvable: options.when holds ${bindingShown(word)}, which is no row group of the role resolution table (${WHEN_WORDS.join(" / ")}).`);
}
const given = options.spec ?? {};
if (typeof given !== "object" || given === null || Array.isArray(given))
throw argumentRefusal(`modelFaceResolvable: options.spec must be a task { model?, modelRole?, roles? } when present (got ${kindOf(given)}).`);
const { models: heldModels, tiers, roles: deploymentRoles, pricing } = face;
const models = heldModels !== undefined || tiers !== undefined ? receiveModelCatalog(heldModels, tiers, "RunnerDeps", pricing).models : undefined;
const { model, modelRole, roles: taskRoles } = given;
let read;
const received = () => (read ??= { deps: { models, roles: receiveRoleMapPriced(deploymentRoles, "RunnerDeps.roles", pricing), pricing }, task: { model, modelRole, roles: receiveRoleMapPriced(taskRoles, "TaskSpec.roles", pricing) } });
for (const row of ROLE_RESOLUTION_TABLE) {
if (!when.includes(row.when))
continue;
try {
const { deps, task } = received();
HOPS[row.role](task, deps);
}
catch (thrown) {
const code = refusalCode(thrown);
if (code === undefined)
throw thrown;
return { ok: false, role: row.role, code };
}
}
return { ok: true };
}