@sema-agent/core
Version:
Stateless, task-oriented AI agent core
93 lines (92 loc) • 5.11 kB
JavaScript
import { ALL_BRAIN_ERROR_CODES } from "../brain/errors.js";
import { DEFAULT_TIER_ORDER, resolveModel } from "./roles.js";
import { normalizeSeatDeps, tierIndexOf } from "./runner/classifier-ladder.js";
export const ADVISOR_TOOL_NAME = "advisor";
export const ADVISOR_FORK_SENTENCE = "Never consult the advisor tool.";
export const ADVISOR_NOT_ATTACHED_REASONS = Object.freeze(["disabled", "unknown_model", "advisor_unranked", "weakest_tier", "main_unranked", "pairing", "route_refused", "excluded", "hold_unknown"]);
const NOT_ATTACHED_SENTENCE = {
disabled: null,
unknown_model: (a) => `The advisor model "${a}" is not in this deployment's model catalog, so the advisor is off.`,
advisor_unranked: (a) => `"${a}" cannot be used as an advisor: the deployment's model tiers do not say where it ranks, so the engine cannot tell whether it is stronger than the main model. Ask the operator to rank it in the deployment's model tiers.`,
weakest_tier: (a) => `"${a}" cannot be used as an advisor: it is on the weakest tier.`,
main_unranked: (_a, m) => `The advisor will not activate on the main model (${m}): the deployment's model tiers do not rank it. Subagents may still use it.`,
pairing: (a, m) => `${a} is less capable than the current main model (${m}), so the advisor will not activate. Choose a more capable advisor, or switch to a smaller main model.`,
route_refused: null,
excluded: null,
hold_unknown: () => `The advisor is off for this run: whether this conversation had switched it off could not be read.`,
};
export function assertAdvisorModelShape(ref) {
if (ref === undefined || (typeof ref === "string" ? ref !== "" : typeof ref === "object" && ref !== null && typeof ref.id === "string"))
return;
const shown = typeof ref === "string" ? "an empty string" : ref === null ? "null" : Array.isArray(ref) ? "an array" : typeof ref;
throw Object.assign(new Error(`TaskSpec.advisorModel must be a non-empty model ref or a Model (got ${shown}) — a model the engine cannot read names no advisor.`), { code: "config.advisor_model_invalid" });
}
export const ADVISOR_OUTCOMES = Object.freeze(["reviewed", "declined", "unavailable"]);
export function advisorAttachVerdict(args) {
if (args.deps.advisorTool === "disabled")
return { state: "not_attached", reason: "disabled" };
const deps = normalizeSeatDeps(args.deps);
let advisor;
try {
advisor = resolveModel(args.advisor, deps.models);
}
catch {
return { state: "not_attached", reason: "unknown_model" };
}
const advisorTier = tierIndexOf(deps, advisor);
if (advisorTier === undefined)
return { state: "not_attached", reason: "advisor_unranked" };
if (advisorTier === DEFAULT_TIER_ORDER.indexOf("lite"))
return { state: "not_attached", reason: "weakest_tier" };
const mainTier = tierIndexOf(deps, args.main);
if (mainTier === undefined)
return { state: "not_attached", reason: "main_unranked" };
if (advisorTier > mainTier)
return { state: "not_attached", reason: "pairing" };
return { state: "on", model: advisor };
}
export const ADVISOR_HOLD_CAUSES = Object.freeze({
auth: "hold",
invalid_request: "hold_unless_window",
rate_limit: "pass",
server: "pass",
network: "pass",
http: "pass",
stream_torn: "pass",
refusal: "pass",
length_empty: "pass",
});
export function advisorFailureHolds(f) {
if (f.code === undefined)
return false;
const row = ADVISOR_HOLD_CAUSES[f.code];
return row === "hold" || (row === "hold_unless_window" && f.errorKind !== "input_too_long" && f.inputTooLongRuledOut === true);
}
const ERROR_KINDS = new Set(["input_too_long", "degenerate", "length_empty", "image_unprocessable"]);
export function advisorHoldOf(entries) {
for (let i = entries.length - 1; i >= 0; i--) {
const e = entries[i];
if (e.type === "compaction")
return undefined;
if (e.type !== "message" || e.message.role !== "toolResult" || e.message.toolName !== ADVISOR_TOOL_NAME)
continue;
const d = e.message.details;
if (d === null || typeof d !== "object" || d.type !== "advisor" || d.hold !== true)
continue;
const code = ALL_BRAIN_ERROR_CODES.find((c) => c === d.code);
const errorKind = typeof d.errorKind === "string" && ERROR_KINDS.has(d.errorKind) ? d.errorKind : undefined;
return { ...(code !== undefined ? { code } : {}), ...(errorKind !== undefined ? { errorKind } : {}) };
}
return undefined;
}
export function advisorReasonTold(reason) {
return NOT_ATTACHED_SENTENCE[reason] !== null;
}
export function advisorNotAttachedNotice(args) {
const sentence = NOT_ATTACHED_SENTENCE[args.reason];
return {
code: "advisor.not_attached",
message: sentence(args.advisorModel, args.mainModel),
detail: { advisorModel: args.advisorModel, mainModel: args.mainModel, reason: args.reason, sessionId: args.sessionId, ...(args.runId !== undefined ? { runId: args.runId } : {}) },
};
}