@sema-agent/core
Version:
Stateless, task-oriented AI agent core
179 lines (178 loc) • 13.6 kB
TypeScript
import type { ThinkingLevel } from "../internal/harness-types.js";
import { type ModelCapability } from "./model-capability.js";
import { type OffCapabilityModel, type ReasoningEffective, type ReasoningFormat, type ReasoningWireFacts, type ResolvedReasoning } from "./reasoning.js";
/**
* THE reasoning WIRE applier — the one place that knows how a requested thinking tier is spelled on each
* wire family, and the one place that says what the spelled keys mean. The three brains (completions, Responses,
* Messages) build their requests through {@link applyReasoningToRequest}, and a host that sends its own request to one
* of those endpoints (a title generator, a side query) calls the same function with the same model entry, so the keys
* it puts on the wire are the keys the engine would have put there — nothing is re-spelled per caller.
*
* The arm that writes a request's thinking keys says, in the same step, what they say about the tier
* ({@link ReasoningCarriage.said}); the reported resolution ({@link resolveReasoning}, {@link resolutionOf}) and the
* supported-tier read ({@link reasoningEffortLevelsOf}) are readers of that answer, never a second reading of the model.
*
* What is decided elsewhere and only READ here: whether the request carries thinking at all
* ({@link reasoningRequestCarried}), the tier clamp and the `thinkingLevelMap` translation
* ({@link mintEffortWireValue}), and whether the wire spells "off" at all ({@link offSpelling}).
*/
/** The tiers a request for the answer alone can ask for: `off` where the wire spells it, `low` where it cannot. */
export type ReasoningSeat = "off" | "low";
/**
* The lowest reasoning tier a request for the answer alone can take on this model's wire — one table: does the wire
* spell off ({@link offSpelling})? Yes ⇒ `"off"`, and the output cap needs room for the answer only. No ⇒ `"low"`:
* a model whose thinking the wire cannot turn off is asked for the least it can grade, and the cap needs room for that
* thinking too — `AUTO_MODE_CLASSIFIER_LOW_REASONING_BUDGET_TOKENS` is the allowance the engine's own side requests add.
* A completions model that does not hold reasoning and declares no `compat.thinkingFormat` is on the `low` seat as
* well: its wire carries nothing either way, so the request is the gateway's default, and a default-on gateway thinks.
*
* Callers pass the seat as `level` to {@link applyReasoningToRequest}; the classifier's seat tier is this verdict.
*/
export declare function lowestReasoningSeat(model: OffCapabilityModel): ReasoningSeat;
/**
* The tier an ENGINE-CHOSEN request hands its brain for `tier` on `model` — the engine's own seat (the auto-mode classifier),
* not a requester's ask. On an entry that does not declare reasoning an enabling tier is not handed (the engine's own
* choices read an undeclared reasoning as not held, `UNKNOWN_CAPABILITY_READS`): the request is the provider's default,
* byte for byte the request a v7.68.0 brain built for that tier there. `off` (spelled by the declaration, {@link offSpelling})
* and every tier on a declared entry go as chosen. The brains TRY a tier they are handed on an undeclared entry (the
* requester's reader); the engine does not try on its own account.
*/
export declare function engineSeatTier(model: OffCapabilityModel, tier: ThinkingLevel): ThinkingLevel | undefined;
/** The model entry the applier reads. A `Model` satisfies it; `id` only names the model in a refusal. */
export interface ReasoningRequestModel extends OffCapabilityModel {
readonly id?: string;
}
/** The Messages-family knobs a host may set (the engine's own lane sets them from its construction config). */
export interface ReasoningBudgetKnobs {
/**
* Whether `body.max_tokens` is a bound the caller set. `true` (the default): the cap is never raised, and thinking
* is skipped when the cap cannot host a legal budget plus answer room. `false`: the cap is a default the lane
* raises to host one.
*/
capIsHard?: boolean;
/** The share of `max_tokens` the thinking budget takes; absent = the tier's own share. */
share?: number;
/** A fixed budget in tokens; overrides `share`. */
tokens?: number;
}
export interface ApplyReasoningInput {
/**
* The wire family of the request: `"anthropic-messages"` (Messages), one of the Open Responses apis
* (`openai-responses`, `azure-openai-responses`, `openai-chatgpt-responses`), anything else = the chat-completions
* family, whose `model.compat.thinkingFormat` picks the dialect. A host passes the model entry's own `api`.
*/
api: string;
model: ReasoningRequestModel;
/** The requested tier. `"off"` is spelled where the wire has an off spelling; `undefined` writes nothing (the provider's
* default); a value that is not one of the seven tiers is refused (`config.invalid_argument`). */
level: string | undefined;
/**
* The request body so far. Never mutated — the result carries a copy. The thinking members of the request's lane are the
* applier's to write: a body that already carries one is refused (`config.invalid_argument`, `detail.field` = the member's
* path), whatever the tier. Every other member is kept, and the applier's members are merged into the objects that hold
* them (`output_config`, `context_management`, `reasoning`, `chat_template_kwargs`).
*/
body: Readonly<Record<string, unknown>>;
/** Messages family only: a body with no numeric `max_tokens` is refused where the budget needs one. */
budget?: ReasoningBudgetKnobs;
/**
* Present only on a request one of the engine's brains builds — a request that recovers from a provider refusal of what it
* tries: the requested tier is then TRIED on a model whose entry does not declare reasoning (`capabilityTried`), unless this
* set names `"reasoning"` (a re-send after a refusal of it, an endpoint remembered to refuse it — `requestWithheld`), and a
* request that withholds it is exactly the request with this member absent. Absent (a host building its own request, a
* published table) = the declaration alone: an undeclared reasoning carries no enabling tier. The explicit off reads the
* declaration either way (`offSpelling`).
*/
withheld?: readonly ModelCapability[];
}
/**
* What one request's thinking keys say, answered by the arm that wrote them. `said`:
* · a tier — the keys name it: the explicit `off` (a disable key, or a deployment-declared off value), or an enabled tier
* carried by the lane's tier key (the effort value after the clamp; on the Messages budget form with no budget knob, the
* budget the tier's share sized; where Messages declares `effortLevels`, `output_config.effort` is the tier key and the
* budget is only a token allowance);
* · `"on"` — the keys turn thinking on and name no tier (a binary enable key, deepseek / together's enable key with no
* effort, `thinking:{type:"adaptive"}`, a budget a knob fixed, openrouter's `reasoning` object with no effort in it);
* · `"provider_default"` — no thinking key was written at all.
*/
export interface ReasoningCarriage {
readonly said: ReasoningEffective;
/** The dialect or lane the keys are spelled in: the completions dialect name, `"responses"`, or on Messages `"effort"`
* (the entry declares `effortLevels`) / `"budget"`. */
readonly format: ReasoningFormat;
}
export interface AppliedReasoning {
/** A copy of the input body with the thinking keys written (and, on Messages, `temperature` dropped when thinking is on). */
body: Record<string, unknown>;
/**
* The request headers the thinking keys need, to be folded into the request's own with
* {@link mergeReasoningHeaders}: the Messages family's `anthropic-beta` list (effort, context management,
* interleaved thinking — whichever the model entry declares). `{}` where the wire needs none.
*/
headers: Record<string, string>;
/** The effort value the request carries (completions `reasoning_effort` / openrouter `reasoning.effort`, Responses
* `reasoning.effort`, Messages `output_config.effort`), or `undefined`. */
effort: string | undefined;
/** What the written keys say about the tier ({@link ReasoningCarriage}). */
carried: ReasoningCarriage;
}
/**
* Write the thinking keys a requested tier needs onto a request body, in the dialect of the model's wire family, say which
* headers the keys need, and say what the keys mean ({@link AppliedReasoning.carried}). A pure function of (api, model entry,
* level, body): the input body is not mutated, the result carries the body to send and the headers to fold into the
* request's own.
*
* It answers the question a host that builds its own request has — "which keys does THIS model take for THIS tier?" —
* from the same table the engine's brains read, so a host never keeps a copy of a dialect. Absence is meaningful:
* an enabling tier on a model that does not hold `reasoning`, `off` where the wire has no spelling for it
* ({@link offSpelling}), a `level` that is `undefined`, and a tier the wire cannot carry write nothing, and the provider's
* default governs. Refused (`config.invalid_argument`, `detail.field` = the input): a `level` outside the seven tiers, a
* completions dialect name outside the closed set, a body that already carries one of the lane's thinking members, and on the
* Messages budget form a body with no `max_tokens` or a cap / knob that does not read as a finite number.
*
* @example
* const { body, headers } = applyReasoningToRequest({ api: model.api, model, level: "low", body: { model: model.id, max_tokens: 2304, messages } });
* mergeReasoningHeaders(requestHeaders, headers);
*/
export declare function applyReasoningToRequest(input: ApplyReasoningInput): AppliedReasoning;
/**
* How a tier resolved on one request: a pure function of the tier the request was built for, what its keys say, and the
* entry's endpoint label. `effective` is the request's `said`; `graded` = it names one of the six enabled tiers; `clamped` =
* it differs from `requested`; `dropped` = nothing was written (`provider_default`).
*/
export declare function resolutionOf(requested: ThinkingLevel, carried: ReasoningCarriage, endpoint: string): ResolvedReasoning;
/**
* Resolve a requested intensity against a model entry before any request exists: the wire applier's own answer for a request
* of that tier, read through {@link resolutionOf} — so the reported resolution and the keys a request carries cannot disagree.
* `endpoint` is the entry's `api` label. With {@link ReasoningWireFacts} the request is sized at that cap (a hard cap too small
* for a legal budget skips thinking on the Messages budget form, and the resolution says so); without, at a soft cap.
*
* What it cannot see is what only the brain holds — a budget knob from the brain's construction config (a fixed
* `thinkingBudgetTokens` turns the tier into `on`); a Runner leg's seat corrects to the answer its first request carried.
* A `requested` outside the seven tiers is refused (`config.invalid_argument`).
*/
export declare function resolveReasoning(requested: ThinkingLevel, model: ReasoningRequestModel, facts?: ReasoningWireFacts): ResolvedReasoning;
/**
* Whether a request of tier `level` on `model`'s wire sizes its thinking budget from its output cap — read off the applier
* itself (a probe request at a soft cap, `tried` as an engine turn tries an undeclared tier): true exactly when the keys it
* writes carry a token budget (the Messages budget form), the one wire where a request's output cap changes what the provider
* renders ahead of the messages. Everywhere else the cap is a decoding control and a request's thinking keys do not read it.
*/
export declare function thinkingBudgetFollowsCap(model: ReasoningRequestModel, level: ThinkingLevel | undefined, tried: boolean): boolean;
/**
* #692 C-c — the effort tiers that actually REACH THE WIRE for this model, derived from the wire applier: the enabled tiers
* `t` for which a request of tier `t` carries an effort value and says `t` (not clamped, not suppressed by a `null`
* levelmap entry, not a switch-only dialect, not a model that holds no reasoning), in rank order. `undefined` means "this
* wire clamps no tier set" (a switch-only dialect, the Messages budget form with no `effortLevels`), never "unknown": a host
* that offers a person the supported tiers reads THIS, per model, instead of a global default it cannot vouch for.
*/
export declare function reasoningEffortLevelsOf(model: ReasoningRequestModel): readonly ThinkingLevel[] | undefined;
/**
* Fold the header patch {@link applyReasoningToRequest} returned into a request's headers, in place. A beta LIST
* (`anthropic-beta`) is unioned with whatever the request already sends under that name in any letter case and in any
* number of spellings (a spelling that holds no string contributes none and is claimed all the same) — the existing
* entries first, as given, then each new entry not already there, written under the lowercase name; any other name
* replaces its case variants. Assigning the patch over the headers instead would drop a beta the request already carries.
* This is the ONE reading of the beta list: the Messages brain folds its own header layers through it too.
*/
export declare function mergeReasoningHeaders(headers: Record<string, string>, patch: Readonly<Record<string, string>>): void;