@sema-agent/core
Version:
Stateless, task-oriented AI agent core
90 lines (89 loc) • 5.62 kB
TypeScript
/**
* The Runner's fast-mode seat: what a session asked for and what its endpoints answered, in this process.
*
* One seat per session tree (the root's address key, `rootSessionAddressKeyOf`: its scope, id and occupant), so a child leg
* shares its parent's cooldown, as the reference's one process-wide state does. It holds what the session LEARNED
* (`SessionFastModeSeats`): a cooldown, and per ROUTE (`fastModeRouteOf`: the route a model's catalog entry names) whether the
* fast-speed parameter was sent there (the beta then stays on that route's requests to an entry that serves fast mode) and what
* that endpoint refused — the beta itself included, after which neither it nor fast speed goes there; and what the last leg
* asked for. A leg holds what it ASKS for (`TaskSpec.fastMode`, then
* `TaskStream.setFastMode`) and builds the per-call handle from both ({@link FastModeLeg.callFor}). Every reading goes through
* `fastModeReadingOf` (`brain/fast-mode.ts`) and reads the facts of the model's own route — the route its next request goes to.
*
* In-process only, as the reference keeps it: a session continued on another Runner instance or in another process starts with
* no facts — its first fast request that meets a push-back re-learns the cooldown (that request is re-sent at standard speed at
* once). Bounded: past {@link MAX_SESSIONS} the least recently used session's facts are dropped, which reads the same way.
*
* A change of what a session asks for — a leg asking otherwise than the session's last leg did, or the stream verb — is a
* toggle, read as the reference reads one: it ends a cooldown, and turning fast mode on forgets the refusals of fast speed (not
* a refused beta: the route's `beta` fact).
* Each change a leg meets is said once (`model.fast_mode_changed`); a usage-credit cause, which changes nothing, is said once
* per leg, as the reference says it once per turn.
*/
import { type FastModeCall, type FastModeCooldownReason, type FastModeReading, type FastModeRefusal } from "../../brain/fast-mode.js";
import type { Model } from "../../internal/llm.js";
import type { NoticeSeatInput } from "./notice-seat-input.js";
/** What one endpoint answered a session. */
interface RouteFacts {
/** What became of the fast-mode beta here: absent = never sent; `latched` = the fast-speed parameter was sent here, so the
* beta stays on this route's requests; `refused` = the endpoint refused the beta itself, so neither it nor fast speed goes
* here again, and every model here reads `model_not_allowed` — a toggle does not forget it (the endpoint does not know the
* header), as the reference's conversation keeps a refused beta out until its betas reset. */
beta?: "latched" | "refused";
/** The endpoint's word for every model it serves. */
refused?: Exclude<FastModeRefusal, "model_not_allowed">;
/** The models it refused the `speed` parameter for. */
readonly modelsRefused: Set<string>;
}
/** What one session tree learned about fast mode. */
interface SessionFacts {
cooldown?: {
until: number;
reason: FastModeCooldownReason;
};
readonly routes: Map<string, RouteFacts>;
/** What the session's last leg asked for. */
requested: boolean;
}
/** The facts of the sessions of one Runner, in this process, by their root's address key (`rootSessionAddressKeyOf`). */
export declare class SessionFastModeSeats {
#private;
/** The session's facts, if it has any. */
peek(key: string): SessionFacts | undefined;
/** The session's facts, created when it has none. */
hold(key: string): SessionFacts;
}
/** One leg's fast mode. */
export interface FastModeLeg {
/** What this leg asks for now. */
readonly requested: boolean;
/** The leg asked for fast mode at some point, or its session has sent fast speed: its reading rides the manifest and the result. */
readonly engaged: boolean;
/** The per-call handle — undefined when the leg asks for nothing and the session never sent the fast-speed parameter. */
callFor(): FastModeCall | undefined;
/** {@link callFor} for a MAIN-LOOP call: the same handle, and what it asks for is kept as the main call in flight's ask. */
mainCallFor(): FastModeCall | undefined;
/** The reading of `model` now (the leg's own model when omitted). */
readingOf(model?: Model): FastModeReading;
/** The reading of `model` for the main call in flight: what that call asked for when it was made (a verb landing mid-call
* changes the next call, not this one), what the session learned since. Before any main call, the leg's ask. */
inFlightReadingOf(model: Model): FastModeReading;
/** The stream verb: ask (or stop asking) for fast mode from the next request on; returns the leg model's reading. */
setRequested(on: boolean): FastModeReading;
}
/**
* The leg's fast mode over its session tree's facts. `key` is the tree root's address key (`rootSessionAddressKeyOf`, so one
* occupant's cooldown, refusals and latch never reach the next occupant of the same id); `root` says the leg IS that session's
* (only such a leg's word is a toggle of the tree — a child asks for what its parent handed down and toggles nothing); `model`
* answers the leg's model now (a degrade moves it); `requested` is the spec's word.
*/
export declare function createFastModeLeg(input: {
seats: SessionFastModeSeats;
key: string;
root: boolean;
requested: boolean;
model: () => Model;
notices: NoticeSeatInput;
now?: () => number;
}): FastModeLeg;
export {};