@sema-agent/core
Version:
Stateless, task-oriented AI agent core
161 lines (160 loc) • 12.4 kB
TypeScript
import type { Model } from "../internal/llm.js";
/** OpenAI request-body keys core owns; `extraBody` cannot override these. */
export declare const OPENAI_RESERVED: ReadonlySet<string>;
/** Anthropic request-body keys core owns; `extraBody` cannot override these. (Note the asymmetry vs
* OpenAI — e.g. `stop`/`stop_sequences` is NOT reserved here because the Anthropic brain never sets it.) */
export declare const ANTHROPIC_RESERVED: ReadonlySet<string>;
/**
* Open Responses request-body keys core owns; `extraBody` cannot override these (design/163).
*
* The last three are NOT keys the adapter sets — they are keys it must be IMPOSSIBLE to set. The
* Open Responses lane is stateless by contract (this engine is the transcript authority), and
* `extraBody` is the only other path to the request body, so `store` / `previous_response_id` /
* `conversation` are reserved purely to close it. A deployment that sets one gets a stripped key on the
* wire and the drop announced, never a silently server-linked turn.
*/
export declare const RESPONSES_RESERVED: ReadonlySet<string>;
/** What {@link judgeExtraBody} refuses: the code, the one sentence, and (on `config.invalid_argument`) the member it names.
* Not an engine notice (a refusal is thrown, never delivered), so it carries none of a notice's members. */
export interface ExtraBodyRefusal {
readonly code: "config.invalid_argument" | "config.extra_body_output_cap";
readonly sentence: string;
readonly field?: "Model.extraBody";
}
/**
* {@link judgeExtraBody}'s answer: the refusal, or — admitted — the model's keys as ONE copy (`undefined` when the model
* carries no `extraBody` at all). The copy is what was judged, and it is what the merge walks: nothing is read from the
* object a second time.
*/
export type ExtraBodyVerdict = {
readonly refused: ExtraBodyRefusal;
readonly entries?: undefined;
} | {
readonly refused?: undefined;
readonly entries: ReadonlyArray<readonly [string, unknown]> | undefined;
};
/**
* THE admission law for a model's `extraBody`, asked at both seats that can hand a request a model spec: the prepare step
* (the task's own model, before the session is acquired — a refused request never touches the session) and the request
* build ({@link applyExtraBody} — a model spec a host brain swapped in for one request, the compaction model, any other
* call of the leg). One judge, one sentence per refusal, whichever seat asks. Absent is admitted; otherwise the value must
* be a plain object of request-body keys, and it must not carry `max_completion_tokens`.
*
* The keys are read ONCE (`Object.entries`, one walk of the object): the refusal is decided on that copy and an admitted
* verdict hands the same copy to the merge, so an object that answers differently the second time it is asked (a getter, a
* Proxy trap) cannot pass one answer and send another.
*
* - A value that is not a plain object is refused as `config.invalid_argument` (`detail.field` = `Model.extraBody`). It is
* never read as keys: a string or an array would spread into the body as numbered keys, a number or `false` would be
* ignored without a word.
* - `max_completion_tokens` is refused as `config.extra_body_output_cap` on every wire: it is a spelling of the output cap, and
* the engine owns the output cap (it writes the per-request cap itself), so a cap is never taken from `extraBody` and the key
* is refused rather than stripped-and-announced like the keys in a wire's reserved set. On the OpenAI wire the spelling is
* also one the endpoint reads and the overflow retry lowers (stripping it would silently un-cap a deployment that used it as
* its only bound; passing it on would let a constant fight the retry); the other two wires' endpoints do not read it, and the
* refusal was never wire-dependent at the task's own model, so it is not made so here. (The sibling spelling `max_tokens` IS
* reserved: the adapter always writes it, so a stray copy never survives.)
*
* The sentence names the kind of a refused value and reads none of its members: it never echoes the value (an `extraBody`
* can hold a credential-bearing template).
*/
export declare function judgeExtraBody(extraBody: unknown, modelId: string): ExtraBodyVerdict;
/**
* Merge a model's `extraBody` into a brain's already-built request `body`, dropping any key the brain owns so core
* always wins. `body` is a NEW object (passthrough keys first, then core keys) when `extraBody` is set; the
* exact `body` reference (no allocation, byte-identical serialization) when it isn't — so an existing
* deployment with no `extraBody` is unaffected.
*
* The request seat of {@link judgeExtraBody}: an `extraBody` it refuses ends the build with a `BrainError`
* (`invalid_request`, the sentence led by the refusal's code — the shape a pre-flight route refusal has) before anything is
* merged or sent. The three wire adapters reach this function and nothing else; none of them judges the value itself.
*
* `dropped` is the keys of `extraBody` that were stripped, in `extraBody`'s insertion order, names only (a
* value never leaves this function except inside `body`). It is a property of the same walk that strips, so
* what is said and what is dropped cannot disagree; empty when nothing was stripped.
*/
export declare function applyExtraBody(body: Record<string, unknown>, model: Pick<Model, "id" | "extraBody">, reserved: ReadonlySet<string>): {
body: Record<string, unknown>;
dropped: readonly string[];
};
/**
* #343 — the shared USER-HEADER merge layer (`model.headers` → construction `config.headers` →
* per-call `options.headers`, later bag wins), CASE-FOLD deduplicated: HTTP header field names are
* case-insensitive (RFC 9110), but a plain-object spread keys by exact spelling, so an `X-Tenant`
* in one bag and an `x-tenant` in another both reach the wire, where fetch's Headers folds them
* into one comma-joined value ("a, b") — neither writer's value, the later layer's documented
* override silently defeated. A later bag's entry replaces an earlier case-variant instead; the
* WINNER'S spelling and value survive (a single-spelling config is byte-identical on the wire).
*
* EXEMPT: the credential headers ({@link isCredentialHeaderName}, the engine's one reading of which
* header carries a credential: `authorization`, `x-api-key`, `api-key`, `cookie`, …, any case) pass
* through with plain spread semantics (same-spelling override only, no case-fold dedup), so that the
* credential enforcement (`applyRouteCredentialHeaders`, route-adjudicator.ts) stays the ONE authority
* over credential spelling and this layer never becomes a second, subtly different one.
*
* The exemption is NOT what keeps a header-only `ANTHROPIC_AUTH_TOKEN` alive: dedup keeps the
* winner's spelling, a lone `Authorization` has nothing to dedup against, and the credential
* enforcement already deletes every spelling present when a per-model credential wins. What the
* exemption really costs is measured, not reasoned:
* `mergeHeaders({Authorization:A},{authorization:B})` keeps BOTH, and the platform `Headers` fold
* sends `authorization: A, B`.
*
* STATED RESIDUAL (deliberately not fixed here), NARROWED by the credential enforcement: where a
* PER-MODEL credential wins (an options- or entry-bag carrier), every carrier is stripped and only
* the winning bag's re-asserted, so a cross-layer double spelling now folds to the winner. What
* remains is the DEPLOYMENT-CONFIG arm — its bytes ride untouched by design — so a config bag that
* itself spells the SAME auth carrier two ways still ships both, comma-folded. Held, not denied, on
* severity: no server accepts a comma-joined credential, so that failure is a LOUD 401 attributable
* to the misconfiguration, while the non-auth case this function exists for produces a silently
* WRONG value (`X-Tenant: a, b`). Tightening it changes which credential reaches the wire, so it
* belongs in a window that discloses an auth-face behavior change.
*/
/**
* #343 — assign a STRUCTURAL locked header under its canonical lowercase name, deleting every
* case-variant spelling first. The brains hard-lock `content-type` / `anthropic-version` AFTER the
* user-bag merge precisely so they "can NEVER be overridden" (council design/40) — and without the
* delete a user bag carrying `Content-Type: text/plain` survives BESIDE the lowercase lock, the
* platform `Headers` fold turning the pair into `text/plain, application/json` on the wire: a lock
* that decides nothing. WHICH credential headers ride is the jurisdiction of `applyRouteCredentialHeaders`
* (route-adjudicator.ts); the key header a brain writes after it (`authorization` / `x-api-key`, from the
* apiKey knob) is a lock like any other — one value per name, whatever spelling a bag carried.
*/
export declare function lockHeader(headers: Record<string, string>, lowerName: string, value: string): void;
/**
* #343 (review r4) — case-fold READ-AND-CLAIM for an AUGMENTABLE structural header (`anthropic-beta`):
* returns the value under EVERY spelling the bag carried, in key order, and deletes them all, so the
* caller's canonical lowercase write REPLACES them instead of duplicating beside them (the
* read-modify-write used to key the read by the exact lowercase name and miss `Anthropic-Beta`,
* losing the user's betas from the merge AND double-sending the header). A spelling is claimed whatever
* it holds — its value is returned as it is, so a spelling holding `undefined` does not end the claim.
*/
export declare function takeHeaderSpellings(headers: Record<string, string>, lowerName: string): unknown[];
/** The entries of a comma-list header value (`" a , b,,c"` → `a`, `b`, `c`); a value that is not a string carries none. */
export declare function headerListEntries(value: unknown): string[];
/** A caller's header bag read ONCE, into a plain copy of its own enumerable entries — what a request build hands both
* the route law (which credential rides, and its identity) and the merge (what goes on the wire), so a bag whose getters
* or keys answer differently on a second read cannot be judged as one bag and sent as another. */
export declare function headerBagOnce(bag: Readonly<Record<string, string>> | undefined): Record<string, string> | undefined;
export declare function mergeHeaders(...bags: Array<Record<string, string> | undefined>): Record<string, string>;
/** The values a request is DECLARED with ({@link import("./stream-engine.js").SSERequest.declared}): every value of the header bags it was
* built from — the route's credential headers included, as the merge after `applyRouteCredentialHeaders` holds them — the values the
* deployment or the call stated outside a bag (a configured protocol version, each entry of a list header the brain re-folds), and the
* key when it names a secret ({@link namesASecret}: a placeholder key is left in the words as it is left to every child). The values the
* brain writes itself (a structural lock, the default version, the beta entries it chose) are not among them. */
export declare function declaredRequestValues(bags: ReadonlyArray<Readonly<Record<string, string>> | undefined>, stated: ReadonlyArray<string | undefined>, key: string | undefined): string[];
/**
* The output-cap key(s) each lane's wire form uses. A lane's set is exactly the keys THAT lane's
* endpoint reads — a stray cap key belonging to another wire form is inert there and must not be
* mistaken for a bound this request carries.
*/
export declare const OUTPUT_CAP_KEYS: Readonly<Record<"openai" | "anthropic" | "responses", readonly string[]>>;
/**
* The per-request output cap a FINAL (post-`extraBody`) request body actually carries — the SMALLEST
* of the lane's cap keys that is present and usable, or `undefined` when none is.
*
* Read off the merged body rather than off the knob the adapter set, because those are not the same
* thing: what is on the wire is the only cap the provider enforces, and the adapter's own number can differ
* from it (the Anthropic lane raises `max_tokens` to fit a thinking budget). Smallest wins so a consumer
* reasoning about the bound stays conservative.
*/
export declare function effectiveOutputCap(body: Record<string, unknown>, keys: readonly string[]): number | undefined;