import type { EventWrite } from './events.js'; import type { ScenarioDecision, ScenarioFaultResult } from './scenario.js'; import type { WebSocketUpgrade } from './serve-http.js'; /** A socket the vendor serves: its path (and host, when the vendor serves it on a host of its own), as the manifest * declares it. */ /** A vendor's socket: its id, path and host; `protocols` are the subprotocols its server selects from those a client * offers in `Sec-WebSocket-Protocol` (OpenAI Realtime's `realtime`), the first offered that it lists. */ export type SocketDecl = { id: string; path: string; host?: string; note: string; source?: string; protocols?: ReadonlyArray; }; /** One open connection, as its engine sees it: its own state, and the frames it sends (an object is sent as JSON). */ export type SocketSession = { readonly id: number; readonly socket: string; state: Record; send(frame: unknown): void; close(code?: number, reason?: string): void; /** A reply this session will send, by its correlation key: settles when the engine calls `fulfil(key, value)`, and * rejects when `ms` pass or the session closes (a request answered by a session: the tunnel's relay). */ expect(key: string, ms: number): Promise; /** Settles what `expect(key)` awaits; false when nothing awaits it (a late or unknown reply). */ fulfil(key: string, value: unknown): boolean; /** The World's scenario's decision for a turn this session asks (a model vendor's turn spoken over a socket: a voice * agent's reply), the turn given as the pack's scenario adapter reads a request; undefined when the pack has no * scenario or the World carries none. A fault decision is the engine's to answer in its own frames. */ decide(asked: unknown): Promise; /** The session resumes after the last sequence number it saw: every later frame the vendor's declared events sent the * endpoint it is bound to (`state[]`) is sent again, in order (event-delivery.ts). * Answers how many were sent. */ resume(after: number): Promise; }; /** What the World's scenario serves a turn (scenario-serve.ts): its decision, the miss, or a fault's answer. */ export type ScenarioServed = ScenarioDecision | { kind: 'fault'; result: ScenarioFaultResult; ruleId: string; }; /** A socket's protocol, over the handler contract's context (a read of the World as the request that opened it). */ export type SocketEngine = { open?(session: SocketSession, ctx: C): void | Promise; message(session: SocketSession, data: string, ctx: C): void | Promise; /** A write of the World: the frames it sends this session, if any (the engine decides who is told what). */ write?(session: SocketSession, write: EventWrite, ctx: C): void | Promise; /** The session ended (the client hung up, or the engine closed it): the World is read and written as at a frame, so * what the end settles is written (a call's final status). */ close?(session: SocketSession, ctx: C): void | Promise; }; /** The open sessions of one socket of the vendor in a World (a handler reaches them as `ctx.sockets(id)`). */ export declare function openSessions(service: string, root: string | undefined, socket: string): SocketSession[]; /** What a session awaits that the World's clock has run out (a life's `wait` past a relay's timeout), rejected as the * machine's clock would reject it; called as the vendor serves a request ("A request answered by a session"). */ export declare function expireSocketWaits(service: string, root: string | undefined): void; /** The open sessions of one socket of the vendor in the World whose state lives at `root` (resolved, as a delivery's * root is), of every tenant: a delivery binds them by the endpoint's id. */ export declare function sessionsAt(service: string, root: string, socket: string): SocketSession[]; /** Offer a write to every open session of the vendor, each in its turn after what it is already doing. Queued, never * awaited: the write may be one a session's own frame made, which that session finishes before it is told. */ export declare function socketWrite(service: string, root: string | undefined, write: EventWrite): void; /** The upgrade the serve seam takes: a request to a declared socket's path (and host) opens a session of its engine. */ export declare function socketUpgrade(service: string, root: string | undefined, decls: ReadonlyArray, engines: Record>, context: (request: Request, decl: SocketDecl, params: Record) => Promise, hostOf: (request: Request) => string, pathOf: (request: Request) => string, decide: (asked: unknown, context: () => Promise) => Promise): WebSocketUpgrade;