agentcompass 0.2.0: route on AgentCompass v0.6.0 (#1083)
agentcompass routes on the AgentCompass v0.6.0 engine: four capability categories, quota-economy seat ordering and data gates. With no caller profile, a request keeps the model the client asked for. External scoring stays off unless allowExternalScorer and a scorer base URL are both set. The reference ships in the package. This supersedes #791. Run at 52a98632, merged onto main, on WSL (Ubuntu 24.04) with HOME=$(mktemp -d): - npm test in packages/agentcompass: 140 pass, 0 fail - bun scripts/check.mjs agentcompass: ok - Read the diff for new I/O: src/ has no fetch outside the opt-in scorer, and no spawn, env read or disk access. - The docs' scorer example was checked against scorerOptionsOf and resolveClassifier. Not tried: a real Magpie gateway routing live traffic through it. Co-authored-by: akiiwa-games <akiiwa-games@users.noreply.github.com>
This commit is contained in:
@@ -6,8 +6,10 @@
|
||||
*
|
||||
* Capabilities in `src/middleware.js`:
|
||||
* 1. Evaluates `evaluateProviderContinuation` first (holding the current Provider/Model on
|
||||
* `within: true` tool loops, warm KV cache, or short follow-up nudges), and only executes
|
||||
* `inferTaskProfile` + `selectBestRoute` when selecting or switching a Provider/Model is needed.
|
||||
* `within: true` tool loops or warm KV cache), and only executes `selectBestRoute` when selecting or
|
||||
* switching a Provider/Model is needed. `moejs` has no network, so the task profile comes from the caller
|
||||
* (`ctx.state.profile`, `ctx.profile`, `body.profile`) or the hooks' `ctx.state.route`; without one the
|
||||
* request is not routed and goes on with the model the client asked for.
|
||||
* 2. Rewrites `body.model` (using either `<model>:<effort>` which `gateway.go:askedAt` natively
|
||||
* splits, or bare `<model>` plus protocol-specific reasoning effort fields) within the
|
||||
* configured `candidateModels` / `availableModels` / `allowedModels` (or `defaultModel`),
|
||||
@@ -18,16 +20,42 @@
|
||||
* and filter accounts, and exports `strategies["agentcompass"]` / `strategies["agentcompass-pure"]`.
|
||||
*/
|
||||
|
||||
import { selectBestRoute } from "./allocator.js";
|
||||
import {
|
||||
blockedRefusal,
|
||||
candidateOffers,
|
||||
effortGoneWarning,
|
||||
heldBundle,
|
||||
heldEffortOn,
|
||||
heldTarget,
|
||||
logRouteWarnings,
|
||||
dropPinFields,
|
||||
explicitCandidateModels,
|
||||
extractModelList,
|
||||
mergePinOptions,
|
||||
pinOptionsOf,
|
||||
pinsFixEffort,
|
||||
sessionProfile,
|
||||
selectBestRoute,
|
||||
} from "./allocator.js";
|
||||
import { canonicalModel } from "./catalog.js";
|
||||
import {
|
||||
STICK_KEEP_MS,
|
||||
bodyUpTo,
|
||||
computeSessionKey,
|
||||
defaultContinuityStore,
|
||||
evaluateProviderContinuation,
|
||||
keepsEffort,
|
||||
recentUserTurns,
|
||||
} from "./continuity.js";
|
||||
import { extractRequestFacts, inferTaskProfile } from "./profiler-core.js";
|
||||
import { orderSeatsByQuotaEconomics } from "./quota.js";
|
||||
import { extractRequestFacts, noTextProfile, resolveCallerProfile, unroutedDecision } from "./profiler-core.js";
|
||||
import {
|
||||
areCandidatesAccountBlocked,
|
||||
listQuotaAccounts,
|
||||
orderSeatsByQuotaEconomics,
|
||||
quotaAccountWarnings,
|
||||
shouldSkipQuotaAccount,
|
||||
} from "./quota.js";
|
||||
import { loadLiveQuotas } from "./quota-loader.js";
|
||||
|
||||
const ROUTE_CARRIER_KEY = "__agentcompass_route";
|
||||
|
||||
@@ -38,6 +66,28 @@ const DEFAULT_VIRTUAL_MODELS = [
|
||||
"agentcompass/system-one-0.5",
|
||||
];
|
||||
|
||||
/**
|
||||
* `needs.gui` and `profile` in a request are for the router: an upstream may refuse a field it does
|
||||
* not know. Whether one was dropped.
|
||||
*/
|
||||
function dropRouterFields(body) {
|
||||
let dropped = false;
|
||||
if (body.profile && typeof body.profile === "object") {
|
||||
delete body.profile;
|
||||
dropped = true;
|
||||
}
|
||||
const needs = body.needs;
|
||||
if (needs && typeof needs === "object") {
|
||||
const flags = ["gui"].filter((flag) => typeof needs[flag] === "boolean");
|
||||
for (const flag of flags) delete needs[flag];
|
||||
if (flags.length > 0) {
|
||||
if (Object.keys(needs).length === 0) delete body.needs;
|
||||
dropped = true;
|
||||
}
|
||||
}
|
||||
return dropPinFields(body) || dropped;
|
||||
}
|
||||
|
||||
function stripEffortSuffix(rawModel) {
|
||||
if (!rawModel || typeof rawModel !== "string") return "";
|
||||
const trimmed = rawModel.trim();
|
||||
@@ -64,38 +114,18 @@ function isVirtualModelName(model, options = {}) {
|
||||
return DEFAULT_VIRTUAL_MODELS.includes(clean);
|
||||
}
|
||||
|
||||
function extractModelList(source) {
|
||||
if (!source) return null;
|
||||
if (Array.isArray(source)) {
|
||||
const list = source
|
||||
.map((item) => {
|
||||
if (typeof item === "string") return item.trim();
|
||||
if (item && typeof item === "object") {
|
||||
return String(item.id || item.name || item.model || "").trim();
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.filter(Boolean);
|
||||
return list.length > 0 ? list : null;
|
||||
function extractAllProviderModels(options = {}) {
|
||||
const map = options?.modelsByProvider;
|
||||
if (!map || typeof map !== "object" || Array.isArray(map)) return [];
|
||||
const out = [];
|
||||
for (const val of Object.values(map)) {
|
||||
const list = extractModelList(val);
|
||||
if (list) out.push(...list);
|
||||
}
|
||||
if (typeof source === "object") {
|
||||
const keys = Object.keys(source)
|
||||
.map((k) => k.trim())
|
||||
.filter(Boolean);
|
||||
return keys.length > 0 ? keys : null;
|
||||
}
|
||||
return null;
|
||||
return out;
|
||||
}
|
||||
|
||||
function resolveCandidateModels(body, ctx = {}, options = {}) {
|
||||
const explicit =
|
||||
extractModelList(options.candidateModels) ||
|
||||
extractModelList(options.allowedModels) ||
|
||||
extractModelList(options.availableModels) ||
|
||||
extractModelList(options.models) ||
|
||||
extractModelList(ctx?.models) ||
|
||||
extractModelList(ctx?.availableModels) ||
|
||||
extractModelList(options.modelMap);
|
||||
const explicit = explicitCandidateModels(options, ctx);
|
||||
|
||||
if (explicit) {
|
||||
return { candidateModels: explicit, hasExplicitCandidates: true, canRewriteModel: true };
|
||||
@@ -105,7 +135,8 @@ function resolveCandidateModels(body, ctx = {}, options = {}) {
|
||||
return { candidateModels: null, hasExplicitCandidates: false, canRewriteModel: true };
|
||||
}
|
||||
|
||||
const rawModel = stripEffortSuffix(body?.model || ctx?.model || "");
|
||||
// A `:effort` here is the user fixing the effort: it stays on the candidate.
|
||||
const rawModel = String(body?.model || ctx?.model || "").trim();
|
||||
if (rawModel && !isVirtualModelName(rawModel, options)) {
|
||||
return {
|
||||
candidateModels: [rawModel],
|
||||
@@ -189,6 +220,342 @@ export function applyRoutedModelAndEffort(body, decision, ctx = {}) {
|
||||
return body;
|
||||
}
|
||||
|
||||
// How many earlier turns a request may be decided again from (`replayedState`): bounds the work
|
||||
// on a long conversation, and is part of the rule, so every runtime stops at the same turn.
|
||||
const MAX_REPLAY_DEPTH = 4;
|
||||
|
||||
/**
|
||||
* The turn a request belongs to, read from the history the client resent (`recentUserTurns`).
|
||||
* `stamp` is the current turn. `prior` is the turn whose decision is the session's state before
|
||||
* this request: that same turn for a step of a tool loop, the previous one for a new turn, `null`
|
||||
* on the first.
|
||||
*/
|
||||
function turnChain(body, facts) {
|
||||
const turns = recentUserTurns(body, MAX_REPLAY_DEPTH + 2);
|
||||
if (turns.length === 0) return null;
|
||||
const prior = facts.within ? turns[0] : turns[1] || null;
|
||||
return {
|
||||
stamp: turns[0].stamp,
|
||||
prior,
|
||||
accepts: [turns[0].stamp, prior?.stamp].filter(Boolean),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The session's state before this request, decided again from the history: the request that
|
||||
* began the `prior` turn, cut out of the body (`bodyUpTo`) and routed as it was then. Magpie
|
||||
* hands each request a pooled runtime and gives middleware no storage across requests
|
||||
* (`internal/middleware/middleware.go` `mw.pool`), so the runtime serving a request may never
|
||||
* have seen the session, or have seen only an earlier turn of it. The history is the one thing
|
||||
* every runtime is given, so a decision made again from it is the same on all of them.
|
||||
*/
|
||||
function replayedState(body, ctx, env, chain, stale, depth) {
|
||||
if (!chain.prior || depth >= MAX_REPLAY_DEPTH) return null;
|
||||
const earlier = routeClaimed(
|
||||
bodyUpTo(body, chain.prior.index),
|
||||
{ ...ctx, within: undefined, estimated_input_tokens: undefined },
|
||||
env,
|
||||
depth + 1
|
||||
);
|
||||
const d = earlier.decision;
|
||||
if (!d || !d.targetModel) return null;
|
||||
const tokens = Number(earlier.facts.estimatedInputTokens || 0);
|
||||
return {
|
||||
sessionKey: earlier.sessionKey,
|
||||
model: canonicalModel(d.targetModel, env.options.catalog?.aliases),
|
||||
target: d.targetModel,
|
||||
effort: d.targetEffort || "high",
|
||||
provider: "",
|
||||
workKind: d.workKind || "general_implementation",
|
||||
difficulty: d.difficulty || "routine",
|
||||
profile: d.profile || null,
|
||||
pinnedBundle: d.pins?.pinnedBundle || null,
|
||||
tokens,
|
||||
requestTokens: tokens,
|
||||
compactedTokens: Number(stale?.compactedTokens || 0),
|
||||
cached: false,
|
||||
healthy: true,
|
||||
turn: earlier.facts.turn || 1,
|
||||
anchor: chain.prior.stamp,
|
||||
replayed: true,
|
||||
// The history carries no time, so the age of a replayed turn is unknown: in `cache` mode it holds only where
|
||||
// age doesn't matter (the tool-loop lock, `within`), and a TTL-gated hold (`cache`) counts
|
||||
// as cold. Given an age of 0 instead, a fresh runtime would hold a session the runtime with the record lets go cold.
|
||||
ageKnown: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Route a request for a claimed (virtual) model: hold the session's Provider/Model where
|
||||
* `evaluateProviderContinuation` says to, otherwise profile and select. The session's state is
|
||||
* this runtime's record when that record was made in the turn this request follows (its
|
||||
* `anchor`), and is decided again from the history otherwise (`replayedState`). Returns the
|
||||
* decision and what to record; nothing is recorded here, so replaying an earlier turn leaves the
|
||||
* store as it was.
|
||||
*/
|
||||
function routeClaimed(body, ctx, env, depth = 0) {
|
||||
const { options, store, now, candidateModels, hasExplicitCandidates, canRewriteModel } = env;
|
||||
const facts = extractRequestFacts(body, ctx);
|
||||
const sessionKey = facts.sessionKey || computeSessionKey(body, ctx, facts.firstWords);
|
||||
const chain = turnChain(body, facts);
|
||||
const affinityMode = options.affinityMode || options.affinity || "cache";
|
||||
let sessionState = sessionKey ? store.get(sessionKey, now, { keepExpired: true }) : null;
|
||||
const expiredRecord = Boolean(sessionState?.updatedAt && now - sessionState.updatedAt > STICK_KEEP_MS);
|
||||
if (
|
||||
!expiredRecord &&
|
||||
affinityMode !== "off" &&
|
||||
chain?.prior &&
|
||||
(!sessionState || (sessionState.anchor && !chain.accepts.includes(sessionState.anchor)))
|
||||
) {
|
||||
sessionState = replayedState(body, ctx, env, chain, sessionState, depth);
|
||||
}
|
||||
const anchor = chain?.stamp || "";
|
||||
const pins = mergePinOptions(pinOptionsOf(options), body, body.task);
|
||||
const liveQuotas = loadLiveQuotas(options, now);
|
||||
|
||||
let cont = evaluateProviderContinuation(facts, sessionState, {
|
||||
now,
|
||||
pins,
|
||||
affinityMode,
|
||||
availableModels: candidateModels,
|
||||
unhealthyModels: options.unhealthyModels,
|
||||
quotas: liveQuotas,
|
||||
policy: options.catalog?.policy,
|
||||
store,
|
||||
coldAfterMs: options.coldAfterMs,
|
||||
cacheTtl: options.cacheTtl,
|
||||
models: options.catalog?.models,
|
||||
aliases: options.catalog?.aliases,
|
||||
});
|
||||
|
||||
if (cont.shouldHold && cont.model) {
|
||||
const keptEffort = keepsEffort(
|
||||
sessionState,
|
||||
cont.effort || sessionState?.effort || "high",
|
||||
facts.estimatedInputTokens,
|
||||
now,
|
||||
options
|
||||
);
|
||||
const target = heldTarget(cont.model, sessionState, candidateModels, options.catalog);
|
||||
// Contract R2: a hold runs what the pool offers now. Nothing at or below the held effort, or a pin that names it: route again.
|
||||
const pool = Array.isArray(candidateModels) && candidateModels.length > 0 ? candidateModels : null;
|
||||
const heldEffort = pool ? heldEffortOn(pool, target, keptEffort, options.catalog, { pins, profile: sessionProfile(sessionState) }) : keptEffort;
|
||||
if (heldEffort === null || (heldEffort !== keptEffort && pinsFixEffort(pins))) {
|
||||
cont = {
|
||||
shouldHold: false,
|
||||
why: "effort_gone",
|
||||
previousModel: cont.previousModel || cont.model,
|
||||
previousEffort: keptEffort,
|
||||
within: Boolean(facts.within),
|
||||
turn: facts.turn || 1,
|
||||
};
|
||||
} else {
|
||||
const heldAccounts = liveQuotas ? listQuotaAccounts(liveQuotas, now, options.catalog?.policy) : undefined;
|
||||
const heldWarnings = liveQuotas ? quotaAccountWarnings(liveQuotas, now, options.catalog?.policy) : [];
|
||||
const stepWarnings = heldEffort === keptEffort ? [] : [`${target}: effort ${keptEffort} is no longer offered; held at ${heldEffort}`];
|
||||
return {
|
||||
facts,
|
||||
sessionKey,
|
||||
record: {
|
||||
model: target,
|
||||
effort: heldEffort,
|
||||
provider: cont.provider || sessionState?.provider || "",
|
||||
workKind: sessionState?.workKind || "general_implementation",
|
||||
difficulty: sessionState?.difficulty || "routine",
|
||||
why: cont.why,
|
||||
held: true,
|
||||
anchor,
|
||||
pinnedBundle: pins.pinnedBundle,
|
||||
},
|
||||
decision: {
|
||||
ok: true,
|
||||
targetModel: target,
|
||||
canonicalModel: canonicalModel(cont.model, options.catalog?.aliases),
|
||||
targetEffort: heldEffort,
|
||||
modelWithEffort: `${target}:${heldEffort}`,
|
||||
bundle: pool ? heldBundle(pool, target, heldEffort, options.catalog, pins) : null,
|
||||
canRewriteModel,
|
||||
hasExplicitCandidates,
|
||||
...(heldAccounts ? { accounts: heldAccounts } : {}),
|
||||
workKind: sessionState?.workKind || "general_implementation",
|
||||
difficulty: sessionState?.difficulty || "routine",
|
||||
axisDemand: sessionState?.profile?.axis_demand || null,
|
||||
profile: sessionState?.profile || null,
|
||||
pins,
|
||||
warnings: [...stepWarnings, ...heldWarnings],
|
||||
continuation: {
|
||||
held: true,
|
||||
why: cont.why,
|
||||
sessionKey,
|
||||
anchor,
|
||||
replayed: Boolean(sessionState?.replayed),
|
||||
turn: facts.turn,
|
||||
within: facts.within,
|
||||
previousModel: cont.previousModel || cont.model,
|
||||
previousEffort: cont.previousEffort || heldEffort,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// What this turn's text looks like is no ground for putting back the row the hold just refused (contract R2).
|
||||
// A pool or a pin holds on a turn with no text too: an image alone doesn't get past either (contract R2, R3).
|
||||
const caller = resolveCallerProfile(facts.callerProfile, facts.hasVisualInput);
|
||||
const profile =
|
||||
cont.why === "effort_gone"
|
||||
? sessionProfile(sessionState)
|
||||
: caller?.profile
|
||||
? caller.profile
|
||||
: !caller && !facts.objective && (hasExplicitCandidates || Object.keys(pins).length > 0)
|
||||
? noTextProfile(facts)
|
||||
: null;
|
||||
if (!profile) {
|
||||
return { facts, sessionKey, record: null, decision: unroutedDecision(caller?.error || "no_task_profile") };
|
||||
}
|
||||
const unhealthyModels = [
|
||||
...store.getUnhealthyModels(now),
|
||||
...(Array.isArray(options.unhealthyModels) ? options.unhealthyModels : []),
|
||||
];
|
||||
if (cont.why === "provider_unhealthy" && cont.failedModel) {
|
||||
unhealthyModels.push(cont.failedModel);
|
||||
}
|
||||
const minContextTokens = cont.why === "outgrown" ? Number(facts.estimatedInputTokens || 0) : 0;
|
||||
|
||||
const decision = selectBestRoute(profile, {
|
||||
candidateModels,
|
||||
catalog: options.catalog,
|
||||
unhealthyModels,
|
||||
minContextTokens,
|
||||
quotas: liveQuotas,
|
||||
meteredOk: options.meteredOk,
|
||||
userScores: options.userScores,
|
||||
now,
|
||||
...pins,
|
||||
continuity: {
|
||||
previousModel: cont.previousModel || null,
|
||||
previousEffort: cont.previousEffort || null,
|
||||
why: cont.why,
|
||||
cacheWarm: false,
|
||||
},
|
||||
});
|
||||
if (cont.why === "effort_gone") decision.warnings = [effortGoneWarning(cont), ...decision.warnings];
|
||||
|
||||
if (decision) {
|
||||
decision.canRewriteModel = canRewriteModel;
|
||||
decision.hasExplicitCandidates = hasExplicitCandidates;
|
||||
decision.pins = pins;
|
||||
decision.minContextTokens = minContextTokens;
|
||||
decision.continuation = {
|
||||
held: false,
|
||||
why: cont.why,
|
||||
reasonDetail: cont.reasonDetail || null,
|
||||
sessionKey,
|
||||
anchor,
|
||||
replayed: Boolean(sessionState?.replayed),
|
||||
turn: facts.turn,
|
||||
within: facts.within,
|
||||
previousModel: cont.previousModel || null,
|
||||
previousEffort: cont.previousEffort || null,
|
||||
};
|
||||
}
|
||||
|
||||
if (!decision || !decision.targetModel) {
|
||||
if (decision) {
|
||||
const candidateAccountBlocked = Boolean(
|
||||
liveQuotas &&
|
||||
(areCandidatesAccountBlocked(candidateModels, liveQuotas, options, now) ||
|
||||
selectBestRoute(profile, {
|
||||
candidateModels,
|
||||
catalog: options.catalog,
|
||||
unhealthyModels,
|
||||
minContextTokens,
|
||||
quotas: undefined,
|
||||
meteredOk: options.meteredOk,
|
||||
userScores: options.userScores,
|
||||
now,
|
||||
...pins,
|
||||
}).ok)
|
||||
);
|
||||
const providerModelsAll = extractAllProviderModels(options);
|
||||
const hasDispatchableProviderModels =
|
||||
providerModelsAll.length > 0 &&
|
||||
(!liveQuotas || !areCandidatesAccountBlocked(providerModelsAll, liveQuotas, options, now));
|
||||
decision.accountBlocked = candidateAccountBlocked && !hasDispatchableProviderModels;
|
||||
}
|
||||
return { facts, sessionKey, record: null, decision };
|
||||
}
|
||||
|
||||
let finalEffort = decision.targetEffort;
|
||||
if (
|
||||
sessionState &&
|
||||
canonicalModel(decision.targetModel) === canonicalModel(sessionState.model) &&
|
||||
cont.why !== "cold" &&
|
||||
cont.why !== "compacted" &&
|
||||
cont.why !== "first" &&
|
||||
cont.why !== "worn" &&
|
||||
!pinsFixEffort(pins)
|
||||
) {
|
||||
const kept = keepsEffort(
|
||||
sessionState,
|
||||
decision.targetEffort,
|
||||
facts.estimatedInputTokens,
|
||||
now,
|
||||
options
|
||||
);
|
||||
if (!candidateModels || candidateOffers(candidateModels, decision.targetModel, kept, options.catalog)) finalEffort = kept;
|
||||
}
|
||||
decision.targetEffort = finalEffort;
|
||||
decision.modelWithEffort = `${decision.targetModel}:${finalEffort}`;
|
||||
|
||||
return {
|
||||
facts,
|
||||
sessionKey,
|
||||
decision,
|
||||
record: {
|
||||
model: decision.targetModel,
|
||||
effort: finalEffort,
|
||||
workKind: decision.workKind,
|
||||
difficulty: decision.difficulty,
|
||||
why: cont.why,
|
||||
held: false,
|
||||
anchor,
|
||||
profile: decision.profile,
|
||||
pinnedBundle: pins.pinnedBundle,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Say that a request was not routed, in `ctx.state` (for `onResponse`) and Magpie's log, and send it on: with the model
|
||||
* the client asked for, or, when that is only the router's own virtual name, the model the operator named for this.
|
||||
* Whether the body changed.
|
||||
*/
|
||||
function sendUnrouted(body, ctx, decision, options) {
|
||||
console.warn(`agentcompass: ${decision.notice} (${decision.reason})`);
|
||||
ctx.state = ctx.state || {};
|
||||
ctx.state.agentcompass = decision;
|
||||
const fallback = String(options.defaultModel || options.fallbackModel || "").trim();
|
||||
if (fallback && isVirtualModelName(body.model, options)) {
|
||||
body.model = fallback;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuse a claimed request nothing may serve, naming why, rather than send it on to some other model. A concrete model
|
||||
* under `routeAllModels` with no pin is the one case with a target of its own: routing found nothing better, it goes as asked.
|
||||
*/
|
||||
function rejectBlocked(body, ctx, blocked) {
|
||||
const options = ctx?.options || {};
|
||||
const pinned = Object.keys(mergePinOptions(pinOptionsOf(options), body, body.task)).length > 0;
|
||||
if (!pinned && !isVirtualModelName(body.model, options)) return null;
|
||||
ctx.state = ctx.state || {};
|
||||
ctx.state.routeBlocked = blocked;
|
||||
return typeof ctx?.reject === "function" ? { rejected: ctx.reject(503, `agentcompass: ${blockedRefusal(blocked)}`) } : null;
|
||||
}
|
||||
|
||||
export function onRequest(body, ctx = {}) {
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
return undefined;
|
||||
@@ -196,17 +563,19 @@ export function onRequest(body, ctx = {}) {
|
||||
|
||||
const options = ctx.options || {};
|
||||
const isClaimedModel = shouldRoute(body.model, options);
|
||||
const providerModelsAll = extractAllProviderModels(options);
|
||||
const hasModelsByProvider = providerModelsAll.length > 0;
|
||||
const stateRoute =
|
||||
isClaimedModel &&
|
||||
ctx?.state?.route &&
|
||||
typeof ctx.state.route === "object" &&
|
||||
typeof ctx.state.route.targetModel === "string" &&
|
||||
ctx.state.route.targetModel.trim()
|
||||
((typeof ctx.state.route.targetModel === "string" && ctx.state.route.targetModel.trim()) ||
|
||||
(ctx.state.route.ok === false && ctx.state.route.accountBlocked === true && !hasModelsByProvider))
|
||||
? ctx.state.route
|
||||
: null;
|
||||
|
||||
if (!isClaimedModel && !stateRoute) {
|
||||
return undefined;
|
||||
return dropRouterFields(body) ? body : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -224,20 +593,30 @@ export function onRequest(body, ctx = {}) {
|
||||
|
||||
if (!canRewriteModel && !stateRoute && isVirtualModelName(body.model, options)) {
|
||||
if (typeof ctx?.reject === "function") {
|
||||
return ctx.reject(400, "agentcompass: set candidateModels in the plugin's options");
|
||||
return ctx.reject(400, "agentcompass: a virtual model needs candidateModels or defaultModel in the plugin's options");
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const hooksBlocked = isClaimedModel && !stateRoute && ctx?.state?.routeBlocked;
|
||||
if (hooksBlocked) {
|
||||
logRouteWarnings(hooksBlocked.warnings);
|
||||
const refused = rejectBlocked(body, ctx, hooksBlocked);
|
||||
if (refused) return refused.rejected;
|
||||
return dropRouterFields(body) ? body : undefined;
|
||||
}
|
||||
|
||||
let decision = null;
|
||||
|
||||
if (stateRoute) {
|
||||
if (
|
||||
stateRoute.ok === false ||
|
||||
!hasExplicitCandidates ||
|
||||
!Array.isArray(candidateModels) ||
|
||||
candidateModels.includes(stateRoute.targetModel)
|
||||
candidateOffers(candidateModels, stateRoute.targetModel, stateRoute.targetEffort || "high", options.catalog, stateRoute.pins)
|
||||
) {
|
||||
decision = stateRoute;
|
||||
logRouteWarnings(decision.warnings);
|
||||
}
|
||||
|
||||
if (decision && decision.targetModel) {
|
||||
@@ -254,6 +633,8 @@ export function onRequest(body, ctx = {}) {
|
||||
difficulty: decision.difficulty || "routine",
|
||||
why: decision.continuation?.why || "first",
|
||||
held: Boolean(decision.continuation?.held),
|
||||
profile: decision.profile,
|
||||
pinnedBundle: decision.pins?.pinnedBundle,
|
||||
},
|
||||
facts,
|
||||
now
|
||||
@@ -263,166 +644,54 @@ export function onRequest(body, ctx = {}) {
|
||||
}
|
||||
|
||||
if (!decision && isClaimedModel) {
|
||||
// 2. Zero-latency (< 2 ms) synchronous execution inside `moejs`
|
||||
const facts = extractRequestFacts(body, ctx);
|
||||
const sessionKey = facts.sessionKey || computeSessionKey(body, ctx, facts.firstWords);
|
||||
const sessionState = sessionKey ? store.get(sessionKey, now) : null;
|
||||
|
||||
const cont = evaluateProviderContinuation(facts, sessionState, {
|
||||
now,
|
||||
affinityMode: options.affinityMode || options.affinity || "cache",
|
||||
availableModels: candidateModels,
|
||||
unhealthyModels: options.unhealthyModels,
|
||||
quotas: options.quotas,
|
||||
policy: options.catalog?.policy,
|
||||
// 2. Zero-latency (< 2 ms) synchronous execution inside `moejs`. A hooks' route this pool can't take still
|
||||
// carries the classifier's answer.
|
||||
const routed = routeClaimed(body, stateRoute?.profile ? { ...ctx, profile: stateRoute.profile } : ctx, {
|
||||
options,
|
||||
store,
|
||||
coldAfterMs: options.coldAfterMs,
|
||||
cacheTtl: options.cacheTtl,
|
||||
models: options.catalog?.models,
|
||||
aliases: options.catalog?.aliases,
|
||||
now,
|
||||
candidateModels,
|
||||
hasExplicitCandidates,
|
||||
canRewriteModel,
|
||||
});
|
||||
|
||||
if (cont.shouldHold && cont.model) {
|
||||
const heldEffort = keepsEffort(
|
||||
sessionState,
|
||||
cont.effort || sessionState?.effort || "high",
|
||||
facts.estimatedInputTokens,
|
||||
now,
|
||||
{
|
||||
...options,
|
||||
isShortFollowup: facts.isShortFollowup,
|
||||
}
|
||||
);
|
||||
if (sessionKey) {
|
||||
store.recordRequest(
|
||||
sessionKey,
|
||||
{
|
||||
model: cont.model,
|
||||
effort: heldEffort,
|
||||
provider: cont.provider || sessionState?.provider || "",
|
||||
workKind: sessionState?.workKind || "general_implementation",
|
||||
difficulty: sessionState?.difficulty || "routine",
|
||||
why: cont.why,
|
||||
held: true,
|
||||
},
|
||||
facts,
|
||||
now
|
||||
);
|
||||
}
|
||||
decision = {
|
||||
ok: true,
|
||||
targetModel: cont.model,
|
||||
canonicalModel: canonicalModel(cont.model, options.catalog?.aliases),
|
||||
targetEffort: heldEffort,
|
||||
modelWithEffort: `${cont.model}:${heldEffort}`,
|
||||
canRewriteModel,
|
||||
hasExplicitCandidates,
|
||||
workKind: sessionState?.workKind || "general_implementation",
|
||||
difficulty: sessionState?.difficulty || "routine",
|
||||
continuation: {
|
||||
held: true,
|
||||
why: cont.why,
|
||||
sessionKey,
|
||||
turn: facts.turn,
|
||||
within: facts.within,
|
||||
previousModel: cont.previousModel || cont.model,
|
||||
previousEffort: cont.previousEffort || heldEffort,
|
||||
},
|
||||
};
|
||||
} else if (facts.objective) {
|
||||
const profile = inferTaskProfile(facts);
|
||||
const unhealthyModels = [
|
||||
...store.getUnhealthyModels(now),
|
||||
...(Array.isArray(options.unhealthyModels) ? options.unhealthyModels : []),
|
||||
];
|
||||
if (cont.why === "provider_unhealthy" && cont.previousModel) {
|
||||
unhealthyModels.push(cont.previousModel);
|
||||
}
|
||||
const minContextTokens =
|
||||
cont.why === "outgrown" ? Number(facts.estimatedInputTokens || 0) : 0;
|
||||
|
||||
decision = selectBestRoute(profile, {
|
||||
objective: facts.objective,
|
||||
candidateModels,
|
||||
catalog: options.catalog,
|
||||
unhealthyModels,
|
||||
minContextTokens,
|
||||
quotas: options.quotas,
|
||||
meteredOk: options.meteredOk,
|
||||
now,
|
||||
continuity: {
|
||||
previousModel: cont.previousModel || null,
|
||||
previousEffort: cont.previousEffort || null,
|
||||
why: cont.why,
|
||||
cacheWarm: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (!decision.ok) {
|
||||
// Without the temporary unhealthy/context/quota constraints, and never unrouted: see selectBestRoute.
|
||||
decision = selectBestRoute(profile, {
|
||||
objective: facts.objective,
|
||||
candidateModels,
|
||||
catalog: options.catalog,
|
||||
now,
|
||||
fallbackToBestExcluded: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (decision && decision.targetModel) {
|
||||
let finalEffort = decision.targetEffort;
|
||||
if (
|
||||
sessionState &&
|
||||
canonicalModel(decision.targetModel) === canonicalModel(sessionState.model) &&
|
||||
cont.why !== "cold" &&
|
||||
cont.why !== "compacted" &&
|
||||
cont.why !== "first" &&
|
||||
cont.why !== "worn"
|
||||
) {
|
||||
finalEffort = keepsEffort(
|
||||
sessionState,
|
||||
decision.targetEffort,
|
||||
facts.estimatedInputTokens,
|
||||
now,
|
||||
options
|
||||
);
|
||||
}
|
||||
decision.targetEffort = finalEffort;
|
||||
decision.modelWithEffort = `${decision.targetModel}:${finalEffort}`;
|
||||
decision.canRewriteModel = canRewriteModel;
|
||||
decision.hasExplicitCandidates = hasExplicitCandidates;
|
||||
decision.continuation = {
|
||||
held: false,
|
||||
why: cont.why,
|
||||
reasonDetail: cont.reasonDetail || null,
|
||||
sessionKey,
|
||||
turn: facts.turn,
|
||||
within: facts.within,
|
||||
previousModel: cont.previousModel || null,
|
||||
previousEffort: cont.previousEffort || null,
|
||||
};
|
||||
|
||||
if (sessionKey) {
|
||||
store.recordRequest(
|
||||
sessionKey,
|
||||
{
|
||||
model: decision.targetModel,
|
||||
effort: finalEffort,
|
||||
workKind: decision.workKind,
|
||||
difficulty: decision.difficulty,
|
||||
why: cont.why,
|
||||
held: false,
|
||||
},
|
||||
facts,
|
||||
now
|
||||
);
|
||||
}
|
||||
decision = routed.decision;
|
||||
// Magpie's log is where a gateway operator sees that a pinned model is too slow for a live loop
|
||||
logRouteWarnings(decision?.warnings);
|
||||
for (const warning of decision?.warnings || []) {
|
||||
if (typeof ctx?.warn === "function") {
|
||||
ctx.warn(warning);
|
||||
}
|
||||
}
|
||||
if (routed.record && routed.sessionKey && decision?.targetModel) {
|
||||
store.recordRequest(routed.sessionKey, routed.record, routed.facts, now);
|
||||
}
|
||||
}
|
||||
|
||||
if (!decision || !decision.targetModel) {
|
||||
return undefined;
|
||||
const changed = decision?.routed === false && sendUnrouted(body, ctx, decision, options);
|
||||
if (
|
||||
decision &&
|
||||
typeof decision === "object" &&
|
||||
(decision.accountBlocked === true || hasModelsByProvider)
|
||||
) {
|
||||
ctx.state = ctx.state || {};
|
||||
ctx.state.agentcompass = decision;
|
||||
ctx.state.route = decision;
|
||||
}
|
||||
const deferToProvider =
|
||||
hasModelsByProvider &&
|
||||
!hasExplicitCandidates &&
|
||||
decision?.accountBlocked !== true &&
|
||||
Object.keys(decision?.pins || {}).length === 0;
|
||||
if (decision?.blocked && !deferToProvider) {
|
||||
const refused = rejectBlocked(body, ctx, {
|
||||
error: decision.error || "no_route",
|
||||
warnings: decision.warnings,
|
||||
refusal: decision.refusal,
|
||||
});
|
||||
if (refused) return refused.rejected;
|
||||
}
|
||||
return dropRouterFields(body) || changed ? body : undefined;
|
||||
}
|
||||
|
||||
ctx.state = ctx.state || {};
|
||||
@@ -439,14 +708,15 @@ export function onRequest(body, ctx = {}) {
|
||||
applyProtocolEffortOnly(body, decision.targetEffort, ctx);
|
||||
return body;
|
||||
}
|
||||
return undefined;
|
||||
return dropRouterFields(body) ? body : undefined;
|
||||
}
|
||||
|
||||
applyRoutedModelAndEffort(body, decision, ctx);
|
||||
return body;
|
||||
} finally {
|
||||
if (body && typeof body === "object" && !Array.isArray(body) && ROUTE_CARRIER_KEY in body) {
|
||||
delete body[ROUTE_CARRIER_KEY];
|
||||
if (body && typeof body === "object" && !Array.isArray(body)) {
|
||||
if (ROUTE_CARRIER_KEY in body) delete body[ROUTE_CARRIER_KEY];
|
||||
dropRouterFields(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -456,6 +726,7 @@ export function onRequest(body, ctx = {}) {
|
||||
* Magpie passes `ctx.provider = { id, name, account, model }`.
|
||||
* Supports optional per-provider model lists via `options.modelsByProvider[provider.id]`
|
||||
* or fallback to `ctx.provider.model` if `body.model` is still a virtual model name.
|
||||
* `ctx.reject` turns nothing away here: with nothing usable on this provider, the request goes as routed, with a warning.
|
||||
*/
|
||||
export function onProvider(body, ctx = {}) {
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
@@ -463,37 +734,74 @@ export function onProvider(body, ctx = {}) {
|
||||
}
|
||||
|
||||
const options = ctx?.options || {};
|
||||
const now = Number.isFinite(options.now) ? Number(options.now) : Date.now();
|
||||
const liveQuotas = loadLiveQuotas(options, now);
|
||||
const prov = ctx?.provider && typeof ctx.provider === "object" ? ctx.provider : null;
|
||||
const providerModels =
|
||||
extractModelList(options.modelsByProvider?.[prov?.id]) ||
|
||||
extractModelList(options.modelsByProvider?.[prov?.name]);
|
||||
|
||||
if (ctx?.state?.routeBlocked) return undefined;
|
||||
|
||||
const prevDecision = ctx?.state?.agentcompass || ctx?.state?.route;
|
||||
if (providerModels && providerModels.length > 0 && prevDecision?.profile) {
|
||||
if (
|
||||
prevDecision?.ok === false &&
|
||||
prevDecision?.accountBlocked === true &&
|
||||
(!providerModels || providerModels.length === 0)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// A held turn has no profile from this request: the session's own, else the kind and difficulty it was routed on.
|
||||
const prevProfile =
|
||||
prevDecision?.profile ||
|
||||
(prevDecision?.workKind ? sessionProfile(prevDecision) : null);
|
||||
if (providerModels && providerModels.length > 0 && prevProfile) {
|
||||
// The user unchecks an effort in the pool, and a provider's own list holds no such row (contract R2).
|
||||
const unchecked = (explicitCandidateModels(options, ctx) || []).filter((entry) => entry?.disabled && !entry.disabled_by_default);
|
||||
const providerPool = [...providerModels, ...unchecked];
|
||||
const needProviderRoute =
|
||||
isVirtualModelName(body.model, options) ||
|
||||
prevDecision.ok === false ||
|
||||
prevDecision.canRewriteModel === false ||
|
||||
prevDecision.hasExplicitCandidates === false ||
|
||||
!providerModels.includes(prevDecision.targetModel) ||
|
||||
!candidateOffers(providerPool, prevDecision.targetModel, prevDecision.targetEffort || "high", options.catalog) ||
|
||||
options.routePerProvider === true;
|
||||
|
||||
if (needProviderRoute) {
|
||||
const routed = selectBestRoute(prevDecision.profile, {
|
||||
objective: "",
|
||||
candidateModels: providerModels,
|
||||
const store = options.continuityStore || defaultContinuityStore;
|
||||
const routed = selectBestRoute(prevProfile, {
|
||||
candidateModels: providerPool,
|
||||
catalog: options.catalog,
|
||||
quotas: options.quotas,
|
||||
unhealthyModels: [
|
||||
...store.getUnhealthyModels(now),
|
||||
...(Array.isArray(options.unhealthyModels) ? options.unhealthyModels : []),
|
||||
],
|
||||
minContextTokens: prevDecision.minContextTokens || 0,
|
||||
quotas: liveQuotas,
|
||||
meteredOk: options.meteredOk,
|
||||
userScores: options.userScores,
|
||||
now,
|
||||
...mergePinOptions(pinOptionsOf(options), prevDecision.pins),
|
||||
});
|
||||
logRouteWarnings(routed.warnings);
|
||||
if (routed.ok && routed.targetModel) {
|
||||
const updated = {
|
||||
...prevDecision,
|
||||
ok: true,
|
||||
accountBlocked: false,
|
||||
targetModel: routed.targetModel,
|
||||
canonicalModel: routed.canonicalModel,
|
||||
targetEffort: routed.targetEffort || prevDecision.targetEffort || "high",
|
||||
modelWithEffort: `${routed.targetModel}:${routed.targetEffort || prevDecision.targetEffort || "high"}`,
|
||||
bundle: routed.bundle,
|
||||
profile: routed.profile,
|
||||
selectedAccount: routed.selectedAccount ?? prevDecision.selectedAccount ?? null,
|
||||
quota: routed.quota ?? prevDecision.quota ?? null,
|
||||
canRewriteModel: true,
|
||||
hasExplicitCandidates: true,
|
||||
warnings: [...(prevDecision.warnings || []), ...(routed.warnings || [])],
|
||||
...(routed.accounts ? { accounts: routed.accounts } : {}),
|
||||
};
|
||||
if (ctx.state && typeof ctx.state === "object") {
|
||||
ctx.state.agentcompass = updated;
|
||||
@@ -502,6 +810,7 @@ export function onProvider(body, ctx = {}) {
|
||||
applyRoutedModelAndEffort(body, updated, ctx);
|
||||
return body;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,11 +819,28 @@ export function onProvider(body, ctx = {}) {
|
||||
typeof prov?.model === "string" &&
|
||||
prov.model.trim()
|
||||
) {
|
||||
if (prevDecision?.ok === false && prevDecision?.accountBlocked === true) {
|
||||
return undefined;
|
||||
}
|
||||
if (liveQuotas && areCandidatesAccountBlocked([prov.model.trim()], liveQuotas, options, now)) {
|
||||
return undefined;
|
||||
}
|
||||
// The provider Magpie chose names its own model; an unrouted request takes it with no effort of ours.
|
||||
if (prevDecision?.routed === false) {
|
||||
body.model = prov.model.trim();
|
||||
return body;
|
||||
}
|
||||
const fallbackDecision = {
|
||||
...(prevDecision || {}),
|
||||
ok: true,
|
||||
accountBlocked: false,
|
||||
targetModel: prov.model.trim(),
|
||||
targetEffort: prevDecision?.targetEffort || "high",
|
||||
};
|
||||
if (prevDecision && ctx.state && typeof ctx.state === "object") {
|
||||
ctx.state.agentcompass = fallbackDecision;
|
||||
ctx.state.route = fallbackDecision;
|
||||
}
|
||||
applyRoutedModelAndEffort(body, fallbackDecision, ctx);
|
||||
return body;
|
||||
}
|
||||
@@ -525,13 +851,19 @@ export function onProvider(body, ctx = {}) {
|
||||
/**
|
||||
* Middleware `onResponse(body, ctx)` hook (`internal/middleware/run.go`):
|
||||
* Shares the pooled request runtime with `onRequest`. Updates token/cache watermarks
|
||||
* on success and marks `healthy = false` on upstream error (`status >= 400`).
|
||||
* on success and marks `healthy = false` on upstream error (`status >= 400`). An unrouted request
|
||||
* records nothing, since it must not start a session that later turns hold, and its JSON reply says why.
|
||||
*/
|
||||
export function onResponse(body, ctx = {}) {
|
||||
const options = ctx?.options || {};
|
||||
const store = options.continuityStore || defaultContinuityStore;
|
||||
const now = Number.isFinite(options.now) ? Number(options.now) : Date.now();
|
||||
const decision = ctx?.state?.agentcompass || ctx?.state?.route;
|
||||
if (decision?.routed === false) {
|
||||
return body && typeof body === "object" && !Array.isArray(body)
|
||||
? { ...body, agentcompass: { routed: false, notice: decision.notice, reason: decision.reason } }
|
||||
: undefined;
|
||||
}
|
||||
const sessionKey =
|
||||
decision?.continuation?.sessionKey ||
|
||||
ctx?.state?.sessionKey ||
|
||||
@@ -559,7 +891,9 @@ export function onResponse(body, ctx = {}) {
|
||||
export function onAccount(accountOrBody, ctx = {}) {
|
||||
const decision = ctx?.state?.agentcompass || ctx?.state?.route;
|
||||
const options = ctx?.options || {};
|
||||
if (!decision) return undefined;
|
||||
if (decision?.ok === false && decision?.accountBlocked === true) {
|
||||
return { skip: true };
|
||||
}
|
||||
|
||||
const acct =
|
||||
ctx?.account && typeof ctx.account === "object"
|
||||
@@ -577,6 +911,20 @@ export function onAccount(accountOrBody, ctx = {}) {
|
||||
|
||||
if (!acct || typeof acct !== "object") return undefined;
|
||||
|
||||
const now = Number.isFinite(options.now) ? Number(options.now) : Date.now();
|
||||
const liveQuotas = loadLiveQuotas(options, now);
|
||||
const quotaAccounts = Array.isArray(decision?.accounts)
|
||||
? decision.accounts
|
||||
: liveQuotas
|
||||
? listQuotaAccounts(liveQuotas, now, options.catalog?.policy)
|
||||
: null;
|
||||
|
||||
if (shouldSkipQuotaAccount(acct, ctx, quotaAccounts)) {
|
||||
return { skip: true };
|
||||
}
|
||||
|
||||
if (!decision) return undefined;
|
||||
|
||||
const allowedByKind = options.accountsByWorkKind?.[decision.workKind];
|
||||
if (Array.isArray(allowedByKind) && allowedByKind.length > 0) {
|
||||
const identifiers = [
|
||||
|
||||
Reference in New Issue
Block a user