UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

67 lines (66 loc) • 5.84 kB
import { KEYLESS_EXIT } from "../../brain/route-adjudicator.js"; import { runWithCredentialRefusals } from "../../brain/status-sink.js"; import { deliverEngineNotice } from "../engine-notice.js"; function noticeOf(report, sessionId, runId) { switch (report.code) { case "route.credential_origin_mismatch": { const { modelId, boundOrigin, requestOrigin, boundBy } = report; const unsaid = (origin, what) => (origin !== "" ? origin : `${what} the refusal does not name`); return { code: "route.credential_origin_mismatch", message: boundBy === undefined ? `A request for model "${modelId}" was not sent: the deployment's route judgment says the model's credential belongs to ` + `${unsaid(boundOrigin, "an origin")}, and this request was going to ${unsaid(requestOrigin, "a host")}; the judgment does not say what ` + `bound the credential there. A credential does not follow its model to another host — the deployment has to give the model ` + `a credential for the host the request goes to, or point the model back at the origin its credential belongs to. ` + `Said once per run for this model and these two hosts while the judgment leaves it unsaid; a judgment that says what bound it is said on its own.` : boundBy === "generation" ? `A request for model "${modelId}" was not sent: the deployment's per-model auth answer named no origin, the run's seat was handed in ` + `before a model catalog swap, and the model's entry is not unchanged across it (the catalog the seat was handed in under and the one in force now do not both hold it, member for member), so the answer's credential was not sent to ${requestOrigin}, where it is not bound. ` + `An answer that names no origin goes out across a catalog swap only where its credential is already bound — the deployment's ` + `answer has to name the origin of the entry its key was read for, or the run has to be started again (a new run ` + `takes the seat under the catalog in force). Said once per run for this model and this host.` : boundBy === "declaration" ? `A request for model "${modelId}" was not sent: the deployment's per-model auth handed its credential out for ${boundOrigin} ` + `and this request was going to ${requestOrigin}. A declared credential goes to the origin it names or nowhere — ` + `the deployment's answer has to name the origin of the entry its key was read for, spelled as that entry's base URL. ` + `Said once per run for this model and these two hosts.` : `A request for model "${modelId}" was not sent: the model's own credential belongs to ${boundOrigin} (the host it was first sent to, ` + `the one an earlier answer named, or the one the deployment rebound it to), and this request was going to ${requestOrigin}. A credential does not follow its model to another host — ` + `the deployment has to give the model a credential for ${requestOrigin} (or point it back at ${boundOrigin}). ` + `If ${requestOrigin} takes no credential, ${KEYLESS_EXIT}. Said once per run for this model and these two hosts.`, detail: { modelId, boundOrigin, requestOrigin, ...(boundBy !== undefined ? { boundBy } : {}), sessionId, runId }, }; } case "route.credential_mismatch": { const { modelId, requestBaseUrl, configBaseUrl } = report; const pairedWith = configBaseUrl === undefined ? "a root the refusal does not name" : configBaseUrl !== "" ? configBaseUrl : "its brain's own endpoint"; const goingTo = requestBaseUrl === undefined ? "a host the refusal does not name" : requestBaseUrl !== "" ? requestBaseUrl : "a host with no root of its own"; return { code: "route.credential_mismatch", message: `A request for model "${modelId}" was not sent: the deployment's credential belongs to ${pairedWith}, and this request was going to ` + `${goingTo}. A credential does not follow a model to another host — ` + `the deployment has to give the model its own credential (a per-model key, or an auth header on the model) or serve it through a brain whose root is the model's own. ` + `Said once per run for this model and these two roots.`, detail: { modelId, ...(requestBaseUrl !== undefined ? { requestBaseUrl } : {}), ...(configBaseUrl !== undefined ? { configBaseUrl } : {}), sessionId, runId }, }; } default: { const _exhaustive = report; void _exhaustive; throw new Error("unreachable credential refusal report"); } } } export function createCredentialRefusalScope({ onNotice, sessionId, runId }) { const announced = new Set(); return (fn) => runWithCredentialRefusals((report) => { const key = JSON.stringify(report.code === "route.credential_origin_mismatch" ? [report.code, report.modelId, report.boundOrigin, report.requestOrigin, report.boundBy ?? null] : [report.code, report.modelId, report.requestBaseUrl ?? null, report.configBaseUrl ?? null]); if (announced.has(key)) return; announced.add(key); deliverEngineNotice(onNotice, noticeOf(report, sessionId, runId)); }, fn); }