@sema-agent/core
Version:
Stateless, task-oriented AI agent core
66 lines (65 loc) • 3.36 kB
JavaScript
import { fastModeUnavailableFor } from "../brain/fast-mode.js";
import { isModelSnapshot } from "./model-receipt.js";
import { fastTierFaultOf } from "./pricing.js";
import { refusalAbout } from "./config-refusal-input.js";
export const FAST_TIER_UNPRICED = "fast-tier-unpriced";
export function fastTierNoticeTo(onError, sessionId) {
return (notice) => onError?.(notice, { phase: "config", sessionId, classification: FAST_TIER_UNPRICED });
}
function refusal(where, message) {
const e = new Error(message);
e.code = "config.invalid_argument";
e.detail = { field: where };
return e;
}
function fastTierVerdictOf(model, pricing, seat) {
let servesFast;
try {
servesFast = fastModeUnavailableFor(model) === undefined;
}
catch (e) {
if (!(e instanceof TypeError))
throw e;
return { refusal: refusal(`${seat}.fastMode`, `${seat}: ${e.message} — fast mode is declared \`true\`, \`false\`, or left out.`) };
}
const fault = fastTierFaultOf(model, pricing, servesFast);
if (fault === undefined)
return undefined;
const where = fault.where.startsWith("cost.") ? `${seat}.${fault.where}` : fault.where;
const name = `${seat} (model ${JSON.stringify(model.id)})`;
if (fault.missing) {
return {
notice: new Error(`${name} serves fast mode (\`fastMode: true\`) and its price table declares no fast tier (${where}): a call served at fast speed has no price — its cost reads absent, ` +
`a money ceiling in force stops the run at the end of that turn, and a usage window's money lane refuses it. To price it, declare the fast tier's rates beside the standard ones ` +
`(${where}: { input, output, cacheRead, cacheWrite, cacheWriteLong? }, USD per 1M tokens); or declare fast mode off for this model.`),
about: `${FAST_TIER_UNPRICED} ${JSON.stringify([model.id, fault.where])}`,
};
}
const bad = refusal(where, `${name} declares a fast tier the table judge refuses at ${where} — every fast-tier rate must be a finite, non-negative number of USD per 1M tokens (input and output present). A tier like this prices fast calls at zero (or below) while reading as priced.`);
return { refusal: fault.from === "deployment" ? refusalAbout("deps", bad) : bad };
}
export function assertFastTierPriced(model, pricing, seat, tell) {
const v = fastTierVerdictOf(model, pricing, seat);
if (v === undefined)
return;
if ("refusal" in v)
throw v.refusal;
tell?.(v.notice, v.about);
}
export function assertFastTiersPriced(catalog, pricing, field, tell) {
if (catalog === undefined)
return;
for (const key of Object.keys(catalog)) {
const entry = catalog[key];
if (entry !== undefined && entry !== null)
assertFastTierPriced(entry, pricing, `${field}.models.${key}`, tell);
}
}
export function assertRoleMapFastTiersPriced(roles, pricing, field, tell) {
for (const [key, value] of Object.entries(roles ?? {})) {
if (isModelSnapshot(value))
assertFastTierPriced(value, pricing, `${field}.${key}`, tell);
else if (typeof value === "object" && value !== null && isModelSnapshot(value.model))
assertFastTierPriced(value.model, pricing, `${field}.${key}.model`, tell);
}
}