@sema-agent/core
Version:
Stateless, task-oriented AI agent core
61 lines (60 loc) • 4.54 kB
JavaScript
import { runWithCapabilityWithheld } from "../../brain/status-sink.js";
import { deliverEngineNotice } from "../engine-notice.js";
import { announceDigest, tellOncePerSession } from "./announce-once-ledger.js";
const CAPABILITY_DOWNGRADED = "model.capability_downgraded";
function fastModeOpening(cause, model) {
switch (cause) {
case "not_first_party":
return `Fast mode is only available when using the Anthropic API directly: requests to model ${model} go at standard speed (its catalog entry is not on the Anthropic Messages wire, the only one with a fast-speed parameter).`;
case "model_not_allowed":
return `Model ${model} is not in this deployment's allowed fast-mode models: its catalog entry declares fast mode off, so its requests go at standard speed.`;
case "unknown":
return `Fast mode is currently unavailable for model ${model}: its catalog entry does not declare fast mode, and fast speed is not asked of an entry that does not, so its requests go at standard speed.`;
default: {
const _exhaustive = cause;
return _exhaustive;
}
}
}
function thinkingSentence(report) {
const fastOnly = report.speed === "fast" ? ` on fast-speed requests (requests at standard speed are judged apart)` : "";
const restart = `this process now sends that endpoint no thinking parameters for this model${fastOnly} until the process restarts.`;
const what = report.why === "broke_off"
? `a request to its endpoint that carried the thinking parameters failed after it had begun answering, so ${restart} Requests without them are answered at the provider's own default.`
: `its endpoint ${report.why === "named" ? "refused the thinking parameters" : "refused a request that carried the thinking parameters"}, then answered the same request without them, at the provider's own default. ${report.remembered
? `It did so twice in a row, so ${restart}`
: `The next request tries them again; after two such refusals in a row, this process stops sending them to that endpoint until it restarts.`}`;
const adaptive = report.budgetForm ? `, and declaring the adaptive thinking form in the catalog entry sends it if that is the only form the endpoint takes` : "";
return (`Thinking tier "${report.requested}" ${report.why === "broke_off" ? "is no longer applied" : "was not applied"} on model "${report.modelId}": the model's catalog entry does not declare reasoning, and ${what} ` +
`The catalog entry belongs to the deployment, not to this conversation: declaring there that the model does not reason skips the attempt with them, and declaring that it does makes such a failure the call's instead${adaptive}. ` +
`Said once per conversation for this model.`);
}
function sentenceOf(report) {
switch (report.capability) {
case "image": {
const why = report.cause === "declared_off"
? `its catalog entry declares no image input`
: `its catalog entry does not declare image input, and images are not sent to an entry that does not`;
return (`Images in this conversation are not being sent to model "${report.modelId}": ${why}, so each image reaches the model as a one-line placeholder. ` +
`If the model reads images, declare image input for it in its catalog entry; otherwise switch to a model that reads images. Said once per conversation for this model.`);
}
case "fast_mode":
return `${fastModeOpening(report.cause, `"${report.modelId}"`)} If this model's endpoint serves fast mode, declare fast mode on in its catalog entry; otherwise switch to a model that does. Said once per conversation for this model.`;
case "reasoning":
return thinkingSentence(report);
default: {
const _exhaustive = report;
return _exhaustive;
}
}
}
export function createCapabilityWithheldScope(ids, announced) {
const announce = (report) => {
tellOncePerSession(announced, announceDigest(CAPABILITY_DOWNGRADED, `${report.modelId}\n${report.capability}`), () => deliverEngineNotice(ids.onNotice, {
code: CAPABILITY_DOWNGRADED,
message: sentenceOf(report),
detail: { model: report.modelId, capability: report.capability, status: report.cause, sessionId: ids.sessionId, runId: ids.runId },
}));
};
return (fn) => runWithCapabilityWithheld(announce, fn);
}