UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

74 lines (73 loc) • 4.5 kB
import { deliverEngineNotice, endNoticeFact } from "../engine-notice.js"; const walkCodeOf = (lane) => (lane === "main" ? "model.fallback" : "model.fallback_side_call"); const answeredWith = (reply) => reply?.stopReason !== undefined && ["stop", "length", "toolUse"].includes(reply.stopReason) && reply.partialFinalized !== true; function windowOf(model) { const w = model?.contextWindow; return typeof w === "number" && Number.isFinite(w) && w > 0 ? w : undefined; } const refusedHopOf = (s) => (s.code !== undefined ? `${s.model} (${s.code})` : s.model); const refusedListOf = (from, skipped) => skipped.filter((s) => s.model !== from).map(refusedHopOf).join(", "); function leftBecause(from, reason, skipped) { if (reason !== "route_refused") return `${from} could not serve this request (${reason})`; const own = skipped.find((s) => s.model === from); const said = [own?.code, own?.fixHint].filter((w) => w !== undefined).join(": "); return `${from} was not sent — its route was refused before sending${said !== "" ? ` (${said})` : ""}`; } export function createModelFallbackSeat(onNotice, told, ids, alert) { const alerted = new Set(); const attribution = { sessionId: ids.sessionId, ...(ids.runId !== undefined ? { runId: ids.runId } : {}) }; return { observe(lane, requested, reading, reply) { const fromWindow = windowOf(requested); const d = reading.degraded; if (d === undefined) { const u = reading.unsent; if (u === undefined) { if (answeredWith(reply)) endNoticeFact(told, walkCodeOf(lane), { from: requested.id, lane }); return; } const message = `${leftBecause(u.from, u.reason, u.skipped)}; none of its fallbacks was sent — its route was refused before sending: ${refusedListOf(u.from, u.skipped)}; the request ended with ${u.from}'s own failure.`; const detail = { from: u.from, reason: u.reason, outcome: "exhausted", chain: [], skipped: u.skipped.map((s) => ({ ...s })), lane, ...(fromWindow !== undefined ? { fromWindow } : {}), ...attribution }; if (lane === "main") deliverEngineNotice(onNotice, { code: "model.fallback", message, detail }, undefined, told); else deliverEngineNotice(onNotice, { code: "model.fallback_side_call", message, detail }, undefined, told); const walk = `${u.from}\u0000${u.reason}`; if (lane === "main" && !alerted.has(walk)) { alerted.add(walk); alert?.(message); } return; } const outcome = d.outcome ?? "served"; const to = outcome === "served" ? reading.served : undefined; const toWindow = windowOf(to); const smaller = fromWindow !== undefined && toWindow !== undefined && toWindow < fromWindow ? ` ${d.to} has a smaller context window (${toWindow} tokens; ${d.from} has ${fromWindow}).` : ""; const skipped = d.skipped ?? []; const refusedList = refusedListOf(d.from, skipped); const refused = refusedList !== "" ? ` Not tried — its route was refused before sending: ${refusedList}.` : ""; const head = leftBecause(d.from, d.reason, skipped); const message = outcome === "served" ? `${head}; its fallback ${d.to} answered instead.${smaller}${refused}` : `${head}, and none of its fallbacks could (tried ${(d.chain ?? [d.to]).join(", ")}); the request ended with ${d.from}'s own failure.${refused}`; const detail = { from: d.from, to: d.to, reason: d.reason, outcome, chain: [...(d.chain ?? [])], ...(skipped.length > 0 ? { skipped: skipped.map((s) => ({ ...s })) } : {}), lane, ...(fromWindow !== undefined ? { fromWindow } : {}), ...(toWindow !== undefined ? { toWindow } : {}), ...attribution, }; if (lane === "main") deliverEngineNotice(onNotice, { code: "model.fallback", message, detail }, undefined, told); else deliverEngineNotice(onNotice, { code: "model.fallback_side_call", message, detail }, undefined, told); }, }; }