UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

82 lines (81 loc) • 4.7 kB
import { MAX_TIMER_DELAY_MS } from "../brain/timeout.js"; import { receiveMembers } from "./own-property-receipt.js"; import { describeRejectedValue } from "./tool-search-setting.js"; export const DEGRADE_REASONS = Object.freeze(["breaker_open", "rate_limit", "budget", "server_error", "last_resort", "network", "auth", "stream_torn", "route_refused"]); const DEGRADE_REASON_SET = new Set(DEGRADE_REASONS); export function isDegradeReason(value) { return typeof value === "string" && DEGRADE_REASON_SET.has(value); } export const MODEL_FALLBACK_GATE_WORDS = Object.freeze(DEGRADE_REASONS.filter((r) => r !== "budget" && r !== "route_refused")); const GATE_WORD_SET = new Set(MODEL_FALLBACK_GATE_WORDS); const isGateWord = (word) => typeof word === "string" && GATE_WORD_SET.has(word); export const DEFAULT_FALLBACK_GATE = Object.freeze(["rate_limit", "breaker_open", "server_error", "last_resort"]); export const ANY_HOP_FAILURE = "any"; export const GATEWAY_HOP_GATE = Object.freeze({ on: ANY_HOP_FAILURE }); export function hopGateAdmits(gate, reason) { return gate.on === ANY_HOP_FAILURE || gate.on.includes(reason); } export const MAX_MODEL_FALLBACKS = 3; export const MODEL_FALLBACK_INVALID = "config.model_fallback_invalid"; export function modelFallbackRefusal(field, message) { const e = new Error(message); e.code = MODEL_FALLBACK_INVALID; e.detail = { field }; return e; } const CHAINS = new WeakMap(); const NO_CHAIN = Object.freeze([]); export function modelFallbackChainOf(model) { return CHAINS.get(model) ?? NO_CHAIN; } export function recordedFallbackChainOf(model) { return CHAINS.get(model); } export function recordFallbackChain(model, hops) { CHAINS.set(model, Object.freeze([...hops])); } const RECEIVED_POLICIES = new WeakSet(); export const DEFAULT_MODEL_FALLBACK_POLICY = Object.freeze({ on: DEFAULT_FALLBACK_GATE }); RECEIVED_POLICIES.add(DEFAULT_MODEL_FALLBACK_POLICY); const POLICY_KEYS = ["on", "firstTokenMs"]; export function receiveModelFallbackPolicy(value, field) { if (value === undefined) return DEFAULT_MODEL_FALLBACK_POLICY; if (typeof value === "object" && value !== null && RECEIVED_POLICIES.has(value)) return value; if (typeof value !== "object" || value === null || Array.isArray(value)) { throw modelFallbackRefusal(field, `${field} is ${describeRejectedValue(value)} — it is a record of \`on\` (the reasons a hop failure moves to the next hop) and \`firstTokenMs\`, or absent for the default gate.`); } const words = { subject: field, unit: "member", moment: "when the fallback policy is received" }; const held = receiveMembers(value, words, POLICY_KEYS, (message) => modelFallbackRefusal(field, message)); let on = DEFAULT_MODEL_FALLBACK_POLICY.on; if (held.on !== undefined) { if (!Array.isArray(held.on)) throw modelFallbackRefusal(`${field}.on`, `${field}.on is ${describeRejectedValue(held.on)} — it lists reason words (${MODEL_FALLBACK_GATE_WORDS.join(", ")}).`); const listed = held.on; const seen = []; for (const word of listed) { if (!isGateWord(word)) { throw modelFallbackRefusal(`${field}.on`, `${field}.on holds ${describeRejectedValue(word)}, which is not a fallback reason — the words are ${MODEL_FALLBACK_GATE_WORDS.join(", ")} (\`budget\` is the run's near-budget switch, never a gate word).`); } if (seen.includes(word)) throw modelFallbackRefusal(`${field}.on`, `${field}.on names ${JSON.stringify(word)} twice.`); seen.push(word); } on = Object.freeze(seen); } let firstTokenMs; if (held.firstTokenMs !== undefined) { const ms = held.firstTokenMs; if (typeof ms !== "number" || !Number.isInteger(ms) || ms < 1 || ms > MAX_TIMER_DELAY_MS) { throw modelFallbackRefusal(`${field}.firstTokenMs`, `${field}.firstTokenMs must be a whole number of milliseconds from 1 to ${MAX_TIMER_DELAY_MS} (got ${typeof ms === "number" ? describeRejectedValue(ms) : `a ${ms === null ? "null" : typeof ms}`}) — omit it for no per-hop deadline.`); } if (!on.includes("network")) { throw modelFallbackRefusal(`${field}.firstTokenMs`, `${field}.firstTokenMs is set but ${field}.on does not admit "network" — the deadline abandons a hop as a network failure, so without "network" in the gate it would end the request on the hop it is meant to leave.`); } firstTokenMs = ms; } const policy = Object.freeze({ on, ...(firstTokenMs !== undefined ? { firstTokenMs } : {}) }); RECEIVED_POLICIES.add(policy); return policy; }