Cosmo Realtime SDK
ReferenceTypescript

TypeScript types

All public types exported from cosmo-ai.

TypeScript types

All types are available as named imports from cosmo-ai. This page is the type index; the classes (RealtimeClient, RealtimeAgent, RealtimeSession) are documented on RealtimeClient, the React surface on React.


Client options

RealtimeClientOptions

FieldTypeDescription
baseUrl?stringOrigin of the Cosmo external API. Must be https:// (http:// only for localhost). Omit in a browser to use the page origin.
apiKey?stringWorkspace-scoped API key (server-side secret). Requires baseUrl. At most one of apiKey / token.
token?stringMinted end-user JWT (from mintToken). Safe on devices; can open sessions but not mint.
getAuthHeaders?() => Record<string,string> | Promise<...>Custom auth headers; a configured credential's Authorization wins.
transportFactory?() => RealtimeTransportTransport override, constructed per session start. Defaults to LiveKit.
screenInteraction?ScreenInteractionOpt into grounded screen control. See Screen interaction.

Agent configuration

TypeKindDescription
AgentConfigobjectThe inline persona: instructions?, model?, modelOptions?, voice?, tools?, interruptionSensitivity?, greeting?, audio?, skills?, hooks?.
VoiceConfigobjectHow the agent sounds: name? (provider voice id), speakingStyle? (delivery guidance appended after the persona). Accepted anywhere a plain voice-id string is.
AudioConfigobjectThe audio pipeline: output? (false runs text-only), noiseCancellation?, ambience? (AmbienceConfig: track?, gainDb? — presence enables the bed).
CatalogAgentOptionsobjectPer-run ride-alongs for client.catalogAgent(name, {...}): inputs?, tools?, voice?, hooks? (client hooks only).
SessionStartOptionsobjectPer-run options for agent.start(): resumeSessionId?, storeRecording?, publishMicrophone? (default true; false joins as a silent observer).
ModelOptionsunionGeminiModelOptions | OpenAIModelOptions | UltravoxModelOptions | PersonaplexModelOptions, discriminated on provider.
GeminiModelOptionsobjectprovider: 'gemini', temperature?, maxOutputTokens?, thinkingLevel?.
OpenAIModelOptionsobjectprovider: 'openai' — no knobs today.
UltravoxModelOptionsobjectprovider: 'cosmo_voice_ultravox', temperature?, turnEndpointDelaySeconds?.
PersonaplexModelOptionsobjectprovider: 'cosmo_voice_personaplex' — no knobs today.

Tools

TypeKindDescription
RealtimeToolunionClientToolSpec | BackgroundClientToolSpec | ServerToolSpec.
ClientToolSpecobjectkind: 'client', name, description, parameters (JSON Schema), handler?: ClientToolHandler.
BackgroundClientToolSpecobjectkind: 'client', background: true — long-running tool; the handler acks via ClientToolJob and delivers the result later.
ServerToolSpecobjectkind: 'server', name — opt-in to a server tool by dot-namespaced name.
ClientToolHandlerfunction(args) => Promise<Record<string, unknown> | null | void> — returned object is the tool result; throw to surface an error.
BackgroundClientToolHandlerfunction(args, job: ClientToolJob) => Promise<void>job.ack(note) then job.complete(...) / job.fail(...).
ClientToolJobclassHandle for a background tool call: ack, complete, fail.

Skills and hooks

TypeKindDescription
Skillobject{ name, description, body } — see Skills.
parseSkillMdfunctionParse a SKILL.md document into a Skill. Throws SkillParseError.
HookclassOne declared client hook (seam + callback + optional matcher). Build via the factories below.
sessionStart / preToolUse / postToolUse / sessionEndfactories(fn, opts?) => Hook. Tool seams accept { matcher } (glob grammar; malformed matchers throw at declaration).
HookEventNameunion'SessionStart' | 'PreToolUse' | 'PostToolUse' | 'SessionEnd'.
SessionStartContext / PreToolUseContext / PostToolUseContext / SessionEndContextobjectsPer-seam contexts passed to hook callbacks.
SessionStartResultobject{ additionalContext? } — injected into the instructions.
PreToolUseResultobject{ permission?: 'allow' | 'deny', reason?, updatedArguments? }.
SessionStartHook / PreToolUseHook / PostToolUseHook / SessionEndHookfunctionsCallback signatures per seam.
ToolOutcomeunion{ kind: 'ok', result } | { kind: 'error', message } | { kind: 'denied', reason } — what PostToolUse observes.
ServerHookaliasSilenceTimeout — declarative server-executed hook config.
ServerHookActionunionThe say / end_call action a fired server hook performed.
SilenceTimeout / Say / EndCallwire typesRe-exported wire shapes for server hooks.

See Hooks for semantics.


State

TypeValues / shape
TransportState'disconnected' | 'requesting-permission' | 'connecting' | 'connected' | 'ready' | 'reconnecting' | 'disconnecting' | 'failed'
SessionLifecycleState{ kind: 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'disconnected'; disconnectReason?; detail? } — the formal cross-SDK lifecycle. See Lifecycle.
DisconnectReason'client_ended' | 'client_closed' | 'handshake_failed' | 'server_ended' | 'transport_error'
AgentState'idle' | 'listening' | 'thinking' | 'speaking'
MicState'unknown' | 'requesting' | 'granted' | 'denied' | 'muted' | 'not-found'
OutputState'blocked' | 'playing' | 'silent''blocked' means the browser is suppressing autoplay; use <StartAudio />.
MediaState{ mic: MicState; screen: ScreenShareState; output: OutputState }
ScreenShareState{ kind: 'inactive' } | { kind: 'requesting' } | { kind: 'active'; startedAt } | { kind: 'error'; error }

Events

Payloads for client.on(...) / RealtimeEventMap:

TypeShape
TranscriptEvent{ id, turnId, role: 'user' | 'assistant', text, isFinal, append } — coalesce by (turnId, role); append says extend vs. new bubble.
ModelTextEvent{ text, isFinal } — model text channel, not a transcription.
TurnCompleteEvent{ role: 'user' | 'assistant' }
ToolCallEvent{ toolCallId, name }
ToolDispatchStartedEvent{ toolCallId, name }
ToolResultEvent{ toolCallId, ok, summary }
ReadyEvent{ sessionId, rejectedTools, maxSessionSeconds, agent: ResolvedAgentInfo | null }
ResolvedAgentInfo{ name, tools } — resolved catalog agent, informational.
ReconnectingEvent{ secondsRemaining }
UserSpeechTimeoutEvent{ sessionId, silenceMs, triggerCount, maxCount, action }
VolumeEvent{ mic, output } — 0–1 RMS, per animation frame while subscribed.
PongEvent{}
RealtimeEventMapEvent-name → payload map: transport_state, agent_state, media_state, lifecycle, transcript, model_text, tool_call, tool_dispatch_started, tool_result, session_state, volume, error, ready, session_started, reconnecting, session_ending_soon, session_ended, turn_complete, pong, user_speech_timeout.
RealtimeEventNamekeyof RealtimeEventMap
Unsubscribe() => void

Session stream items

Yielded by for await (const event of session):

TypeShape
RealtimeSessionEventWire frames verbatim, plus the two below.
SessionEndedEventItem{ type: 'session-ended', reason } — SDK-local terminal item, always last.
UnknownEvent{ type: 'unknown', rawType, payload } — forward-compat item for unrecognized frames; never terminal.

MonitorSession (from client.connectAsMonitor) exposes sessionId, getTranscript(), and subscribe().


Errors

All SDK-thrown errors extend CosmoRealtimeError.

TypeDescription
CosmoRealtimeErrorBase class — err instanceof CosmoRealtimeError catches the family.
RealtimeError{ code: RealtimeErrorCode; message } — the error event payload.
RealtimeErrorCode'mic_denied' | 'screen_denied' | 'screen_start_failed' | 'session_start_failed' | 'session_rejected' | 'auth_error' | 'not_ready' | 'transport_connect_timeout' | 'transport_disconnect' | 'unsupported_browser' | 'server_error'
RealtimeNotReadyErrorThrown by imperative methods called before the session is ready (code: 'not_ready').
RealtimeCredentialErrorUnusable credential configuration (both apiKey and token, or apiKey without baseUrl).
RealtimeMintTokenErrormintToken() failed; code: MintTokenErrorCode (server slug or no_api_key / transport_error / invalid_response).
RealtimeDialErrorsession.dial() failed; code: DialErrorCode (server slugs like phone_calls_disabled, minute_limit_exceeded, session_not_live, or client-side invalid_phone_number, transport_error).
SkillParseErrorSKILL.md parsing failed.

MintTokenErrorCode and DialErrorCode are open-ended string aliases — treat unknown codes defensively.


Auth and telephony

TypeShape
MintedToken{ jwt: string; expiresAt: Date } — from client.mintToken(externalUserId). See End-user credentials.
DialResult{ dialId: string } — from session.dial(phoneNumber). See Telephony.

Screen interaction

ScreenInteraction, ScreenCapture, ScreenElement, ScreenButton, ScreenAction — the platform capability object behind grounded screen control. See Screen interaction.


Constants and presets

ExportDescription
PROTOCOL_VERSION'1.0' — the external wire-protocol version the SDK speaks.
NaturalnessRung'warm' | 'delivery' | 'human'.
naturalness(rung)Resolves a rung to the verbatim speakingStyle instruction text.
NATURALNESS_RUNGS / NATURALNESS_INSTRUCTIONS / NATURALNESS_VERSIONThe rung list, prompt catalog, and its schema version.

React types

CosmoRealtimeProviderProps, RealtimeClientLike, RealtimeSnapshotState, RealtimeTranscriptItem, RealtimeToolCallItem — documented with the provider, hooks (useTransportState, useAgentState, useMediaState, useTranscript, useToolCalls, useRealtimeError, useMicLevel, useOutputLevel), and components (RealtimeAudio, MicToggle, BarVisualizer, StartAudio) on React.

On this page