UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

29 lines (28 loc) • 2.01 kB
import type { Brain } from "../core/types.js"; /** * Compose several brains into one with ordered failover (primary → fallbacks). * * The happy path streams live from the first brain. A brain is only failed over to the next when it * fails **before emitting any content** (e.g. the gateway is down / connection refused, OR a * first-token timeout that fires after the stream opened but before any delta) — a clean upfront * failure. The leading `start` event is **held back** until the first real content event, so an * error between `start` and the first delta can still fail over without ever having forwarded a * (then-duplicated) `start`. Once real content (text/thinking/tool) has streamed, the outcome * (including a mid-stream error or abort) is propagated rather than restarted, so the consumer never * sees duplicate `start`/text events. The last brain's result is always final. * * The non-streaming face walks the same brains the same way, each through ITS non-streaming face (`completeOf`): a message that * carries content is the answer, whatever its outcome. * * Every gateway moved off is listed on the answer (the model lane's left-behind list: `leftEntriesOf` / `carryLeftBehind`), with the * usage its own message reported — what a failed attempt cost is billed, at the request's model. * * Each leg is asked before it is sent ({@link legRefusal}), on both faces and on the pass-through: a leg whose own judgment does * not go ahead (it refuses, throws, or answers no verdict) is not called — never handed the request or its per-call credential. * That is a clean failure of that leg before anything went out: the walk moves on; the last leg's (or the pass-through's) refusal * is the answer; a leg not sent is not among the requests left behind. * * Each underlying brain keeps its own retry policy (e.g. `createOpenAIBrain({ maxRetries })`); * failover is the cross-gateway layer on top. */ export declare function createFailoverBrain(brains: Brain[]): Brain;