@sema-agent/core
Version:
Stateless, task-oriented AI agent core
154 lines (153 loc) • 6.74 kB
JavaScript
import { capabilityHeld, capabilityTried } from "./model-capability.js";
import { describeRejectedValue } from "../core/tool-search-setting.js";
const RANK = {
off: 0,
minimal: 1,
low: 2,
medium: 3,
high: 4,
xhigh: 5,
max: 6,
};
export const THINKING_LEVELS = Object.freeze(Object.keys(RANK));
export function isThinkingLevel(v) {
return typeof v === "string" && Object.prototype.hasOwnProperty.call(RANK, v);
}
export function reasoningRequestCarried(model, reasoning, withheld) {
const sends = withheld === undefined ? capabilityHeld(model, "reasoning") : capabilityTried(model, "reasoning", withheld);
return sends && isThinkingLevel(reasoning) && reasoning !== "off";
}
export function judgeThinkingLevel(value, seat) {
if (value === undefined)
return undefined;
if (isThinkingLevel(value))
return value;
const e = new Error(`${seat} must be one of ${THINKING_LEVELS.map((l) => `"${l}"`).join(", ")} when present (got ${describeRejectedValue(value)}) — omit it for no tier ("off" spells thinking off); a value outside the seven tiers is refused, never read as the provider's own default`);
e.code = "config.invalid_argument";
e.detail = { field: seat };
throw e;
}
export function rankOf(level) {
return RANK[level];
}
export const DEFAULT_EFFORT_LEVELS = ["minimal", "low", "medium", "high"];
const COMPLETIONS_OFF_SPELLING = {
openai: "declared",
openrouter: "always",
deepseek: "always",
together: "always",
zai: "always",
qwen: "always",
"qwen-chat-template": "always",
};
export const COMPLETIONS_THINKING_FORMATS = Object.freeze(Object.keys(COMPLETIONS_OFF_SPELLING));
export function isCompletionsThinkingFormat(v) {
return typeof v === "string" && Object.prototype.hasOwnProperty.call(COMPLETIONS_OFF_SPELLING, v);
}
export function declaredCompletionsFormat(compat) {
const declared = compat.thinkingFormat;
if (declared === undefined || declared === null)
return undefined;
if (isCompletionsThinkingFormat(declared))
return declared;
const e = new Error(`compat.thinkingFormat must be one of ${COMPLETIONS_THINKING_FORMATS.map((n) => `"${n}"`).join(", ")} when present (got ${describeRejectedValue(declared)}) — omit it for "openai"; a name outside the set is refused, never read as a model with no dialect`);
e.code = "config.invalid_argument";
e.detail = { field: "compat.thinkingFormat" };
throw e;
}
export function completionsFormatOf(compat) {
return declaredCompletionsFormat(compat) ?? "openai";
}
export const RESPONSES_APIS = new Set(["openai-responses", "azure-openai-responses", "openai-chatgpt-responses"]);
export const REASONING_EFFECTIVE_WORDS = Object.freeze(["off", "minimal", "low", "medium", "high", "xhigh", "max", "on", "provider_default"]);
const _everyThinkingLevelIsListed = true;
void _everyThinkingLevelIsListed;
export const MIN_THINKING_TOKENS = 1024;
export function budgetCapSkipsThinking(outputCapTokens, hardCap) {
return hardCap && outputCapTokens < MIN_THINKING_TOKENS * 2;
}
export function declaredOffSpelling(model) {
const compat = (model.compat ?? {});
if (compat.supportsReasoningEffort === false)
return undefined;
const mapped = model.thinkingLevelMap?.off;
return typeof mapped === "string" && mapped !== "" ? mapped : undefined;
}
const OFF_SPELLED = Object.freeze({ spelled: true });
const offMissing = (missing) => Object.freeze({ spelled: false, missing });
function effortWireOff(model) {
if (declaredOffSpelling(model) !== undefined)
return OFF_SPELLED;
return (model.compat ?? {}).supportsReasoningEffort === false ? offMissing("none") : offMissing("thinkingLevelMap.off");
}
export function offSpelling(model, api = model.api) {
const compat = (model.compat ?? {});
const held = capabilityHeld(model, "reasoning");
if (api === "anthropic-messages")
return held && compat.thinkingMode !== "adaptive" ? OFF_SPELLED : offMissing("none");
if (api !== undefined && RESPONSES_APIS.has(api))
return held ? effortWireOff(model) : offMissing("none");
const declared = declaredCompletionsFormat(compat);
if (declared === undefined && !held)
return offMissing("compat.thinkingFormat");
return COMPLETIONS_OFF_SPELLING[declared ?? "openai"] === "declared" ? effortWireOff(model) : OFF_SPELLED;
}
export function thinkingOffSpelled(model) {
return offSpelling(model).spelled;
}
export function declaredEffortLevels(v) {
return Array.isArray(v) && v.length > 0 ? v : undefined;
}
export function mintEffortWireValue(requested, model, allowed) {
const resolution = resolveEffort(requested, allowed ?? DEFAULT_EFFORT_LEVELS);
let wireValue = resolution.effective;
const mapped = model.thinkingLevelMap?.[resolution.effective];
if (mapped === null)
wireValue = undefined;
else if (mapped !== undefined)
wireValue = mapped;
return { resolution, wireValue };
}
export function sanitizeEffortLevels(allowed) {
const declared = Array.isArray(allowed) ? allowed.filter((lvl) => isThinkingLevel(lvl) && lvl !== "off") : [];
return declared.length > 0 ? declared : DEFAULT_EFFORT_LEVELS;
}
export function resolveEffort(requested, allowed = DEFAULT_EFFORT_LEVELS) {
allowed = sanitizeEffortLevels(allowed);
if (requested === "off" || allowed.includes(requested)) {
return { requested, effective: requested, graded: true, clamped: false };
}
const reqRank = RANK[requested];
let below;
let lowest;
for (const lvl of allowed) {
if (lowest === undefined || RANK[lvl] < RANK[lowest])
lowest = lvl;
if (RANK[lvl] <= reqRank && (below === undefined || RANK[lvl] > RANK[below]))
below = lvl;
}
const effective = below ?? lowest ?? requested;
return { requested, effective, graded: true, clamped: effective !== requested };
}
export const REASONING_BUDGET_SHARE = {
off: 0,
minimal: 0.1,
low: 0.25,
medium: 0.4,
high: 0.6,
xhigh: 0.75,
max: 0.9,
};
export function reasoningBudgetShare(level) {
return REASONING_BUDGET_SHARE[level];
}
const REASONING_PROFILE = {
off: { thinking: "off", awarenessGuidance: false, goalSuggested: false },
fast: { thinking: "low", awarenessGuidance: false, goalSuggested: false },
standard: { thinking: "medium", awarenessGuidance: false, goalSuggested: false },
deep: { thinking: "high", awarenessGuidance: false, goalSuggested: false },
ultra: { thinking: "max", awarenessGuidance: true, goalSuggested: false },
};
export function resolveReasoningProfile(tier) {
return { ...REASONING_PROFILE[tier] };
}