@sema-agent/core
Version:
Stateless, task-oriented AI agent core
95 lines (94 loc) • 6.87 kB
TypeScript
import { type RoutePairingConfig } from "../internal/llm.js";
import type { Brain } from "../core/types.js";
import { type StreamEngineConfig } from "./stream-engine.js";
export interface AnthropicBrainConfig extends StreamEngineConfig, Pick<RoutePairingConfig, "requireCredential"> {
/** Base URL of the Anthropic(-compatible) API. Default {@link ANTHROPIC_DEFAULT_BASE_URL}. Overridden by model.baseUrl. */
baseUrl?: string;
/** API key (`x-api-key`; a subscription token, head `sk-ant-oat`, rides as `Authorization: Bearer` while no header bag fills that seat). Overridden by per-call options.apiKey / getApiKeyAndHeaders. */
apiKey?: string;
/** `anthropic-version` header. Default `2023-06-01`. */
version?: string;
/** Extra headers merged into every request (e.g. `anthropic-beta`). */
headers?: Record<string, string>;
/** Custom fetch (tests / proxies); absent ⇒ the engine's default transport — see `resolveFetch`. */
fetchImpl?: typeof fetch;
/** `max_tokens` when the call/model doesn't specify one (Anthropic requires it). Default 4096. */
defaultMaxTokens?: number;
/**
* Insert `cache_control` prompt-cache breakpoints on the last system block and the last tool, so the (stable) system +
* tools prefix is cached across requests. Default true. Their lifetime is the request's retention word
* (`StreamOptions.cacheRetention`: `short` / absent ⇒ `{type:"ephemeral"}`, `long` ⇒ `{type:"ephemeral", ttl:"1h"}`); `false`
* here sends none and moves no lifetime, and a request whose word is `none` sends none whatever this says.
*/
cacheBreakpoints?: boolean;
/**
* Detect **degenerate repetition** (the model looping on the same char/phrase forever) mid-stream
* and cut it off — the turn ends `error` ("degenerate repetition"). Default true.
*/
detectRepetition?: boolean;
/**
* Extended-thinking budget as a fraction of `max_tokens` when reasoning is on. Default 0.5.
* The budget is always clamped to `[1024, max_tokens - 1]` (Anthropic requires it strictly below
* `max_tokens`, ≥ 1024). Lower it to leave more room for the answer with verbose reasoning models.
*/
thinkingBudgetShare?: number;
/**
* Fixed extended-thinking budget in tokens (overrides `thinkingBudgetShare`). Still clamped to
* `[1024, max_tokens - 1]`. Use when you want a constant reasoning budget regardless of max_tokens.
*/
thinkingBudgetTokens?: number;
}
/** This lane's own default endpoint: where a request goes when neither the entry nor the config names a root,
* and so the root a key on the config is paired with when the config declares none. Exported so that every place that
* names the Anthropic API's own root — the {@link fable5Model} preset, a host's disclosure of where a default request
* lands — reads this one spelling. */
export declare const ANTHROPIC_DEFAULT_BASE_URL = "https://api.anthropic.com";
/** What {@link anthropicCredentialHeadersOf} answers for an Anthropic request: which header carries the API key (when one is supplied) and which `anthropic-beta` token the outgoing credential asks for (when any). */
export interface AnthropicCredentialHeaders {
readonly key?: {
readonly header: "authorization" | "x-api-key";
readonly value: string;
};
readonly beta?: string;
}
/**
* The one pure judge on the Anthropic lane that answers with the same two rules the adapter applies to its own request
* (`anthropicKeyHeaderOf` and `credentialBetaOf`, over `BEARER_CREDENTIAL_BETAS`) — exported for a host that builds its own
* direct request to the same provider — over the same pair the adapter reads: the key and the header bag are first read as
* the route law reads them (`keyCarriedBy`, `dropBlankCredentialHeaders`: those two functions are the law's, read here and
* not copied).
*
* Two normalizations, then two rules (the head is written into the value by the provider that minted it; no header name,
* source or host word is read beside it):
* - **Blank is nothing.** A key that is blank (the empty string or white space only; `keyCarriedBy`) is no key, and a credential
* header whose value is blank (`Authorization: ""`; `dropBlankCredentialHeaders`) is dropped from the copy of the bag
* before anything reads it — so it holds no seat and asks for no beta.
* - **Key header (`key`).** Present when `apiKey` is a string that is not blank (`keyCarriedBy`): `authorization` (as
* `Bearer <apiKey>`) for a key whose head is a row of `BEARER_CREDENTIAL_BETAS` when that seat is empty on the headers; else
* `x-api-key` (value `<apiKey>`). A credential a header bag put in `Authorization` is the host's and stays as it is (the
* documented dual-auth shape).
* - **Credential beta (`beta`).** Read on the headers as they would go out (a null-prototype shallow copy of the given headers
* without its blank credential headers, with `key` written into its header, replacing any spelling of that header name):
* exactly one header whose name folds to `authorization`, its value read as the transport sends it (`httpTrimmed`), its
* scheme `Bearer` in any letter case, its credentials part opening with a row's head, letter case as written. Two spellings
* of that name (comma-joined on the wire) or another scheme ask for nothing.
*
* For plain data inputs (own enumerable data properties, header values that are strings) it performs no I/O, reads no
* environment, does not mutate its input and does not throw. A header value that is not a string is outside that: it is copied
* as it is and the blank-credential reading does not judge it. A header bag with an own `__proto__` member that holds an object
* is outside the agreement with the adapter too: the adapter's merge (`mergeHeaders`) assigns it through the prototype setter,
* this judge copies it as a plain member. What it does NOT answer: it does not send, does not validate the key, and does not
* read `Proxy-Authorization`. `key.value` contains the credential itself — do not log the result.
*/
export declare function anthropicCredentialHeadersOf(input: {
apiKey?: string;
headers?: Readonly<Record<string, string>>;
}): AnthropicCredentialHeaders;
/**
* Create a Brain for the Anthropic Messages API (`/v1/messages`) with **prompt caching** and
* **extended-thinking replay**. Translates the unified Context into an Anthropic request and the
* streamed response back into the unified AssistantMessageEvent protocol. The connect/retry/timeout/
* read-loop machinery lives in the shared {@link runStreamingBrain} engine (design/32); this adapter
* owns the Anthropic request shaping + typed content-block SSE parsing + finalization.
*/
export declare function createAnthropicBrain(given?: AnthropicBrainConfig): Brain;