AgentClient is the client SDK for connecting to a live agent over WebSocket. It
keeps a local mirror of the agent’s persistent state — a snapshot on connect, incremental
RFC 7396 merge-patches as the agent changes state — and makes typed RPC calls to the
agent’s remote-callable methods through stub. It ships at
@telnyx/edge-runtime/client with no server dependencies: it uses the platform’s
native WebSocket (browser, or Node ≥ 22).
token / subscribe / resume switches the client into attach mode:
every (re)connect opens the session with an attach frame presenting the token, and
the server answers with the granted claims — readable via claims — and the accepted
streams ("state", "messages", "events"). With resume: true, the client
re-attaches after a drop with its last-seen messages/events cursors and the server
replays exactly what was missed — no full re-snapshot, no duplicates. Without any
attach option, the client speaks the plain protocol (snapshot + hello) and works
against servers that only speak that.
new AgentClient()
new AgentClient<Create a client and start connecting immediately. ParametersTStub,TState>(url,opts?):AgentClient<TStub,TState>
Returns
AgentClient<TStub, TState>
Throws
If no global WebSocket exists and none was injected via
opts.WebSocketCtor.
stub
The typed RPC proxy.readonlystub:TStub
agent.stub.method(...args) sends a call frame
and resolves with the agent method’s return value, or rejects with the
server’s error (e.g. method_not_found, or method_error when the
method threw).
claims
get claims(): readonly Claim[]
The claims granted to this connection by the server’s attached answer.
Empty until an attached frame arrives (always empty outside attach mode).
Returns
readonly Claim[]
onState()
onState(Subscribe to state updates; returns an unsubscribe function. Parameterslistener): () =>void
Returns
() =>
void
onMessages()
onMessages(Subscribe to conversation updates from the actor’slistener): () =>void
MessageLog. A
snapshot replaces the local log; appended adds the new messages. Returns
an unsubscribe function.
Parameters
Returns
() =>
void
onEvents()
onEvents(Subscribe to the agent’s event stream (attach mode withlistener,opts?): () =>void
"events"
subscribed). Events arrive in seq order; pass { from } to receive only
events with seq >= from. Returns an unsubscribe function.
Parameters
Returns
() =>
void
isConnected()
isConnected(): boolean
True once the first hello is received (and until closed).
Returns
boolean
close()
close(Permanently close the client (no reconnect). Outstanding calls reject. Parameterscode?,reason?):void
Returns
void
AgentClientOptions
Options for AgentClient. Setting any oftoken / subscribe / resume switches the client into
attach mode (see the AgentClient class docs); the connection and
heartbeat options apply in both modes.
Properties
pingIntervalMs?
Heartbeat: send aoptionalpingIntervalMs?:number
ping every pingIntervalMs (default 30_000).
pingTimeoutMs?
Reconnect if aoptionalpingTimeoutMs?:number
ping goes unanswered for pingTimeoutMs (default 10_000).
reconnectBackoffMs?
Base reconnect backoff in ms. Delays grow exponentially (factor 2, ±20% jitter) from this base up tooptionalreconnectBackoffMs?:number
reconnectMaxBackoffMs.
Default Value
reconnectMaxBackoffMs?
Upper bound on the reconnect backoff delay, in ms. Default ValueoptionalreconnectMaxBackoffMs?:number
resume?
Resume across reconnects: re-attach with the last-seenoptionalresume?:boolean
messages/events
cursors so the server replays exactly what was missed — no full
re-snapshot, no duplicates. The first connection carries no cursors (a
normal snapshot bootstrap is expected).
subscribe?
Streams to subscribe to. Omitted = the server’s default set.optionalsubscribe?: readonlyStream[]
token?
Opaque credential presented in theoptionaltoken?:string
attach frame; the server derives this connection’s grants from it.
WebSocketCtor?
WebSocket constructor (defaults to the platformoptionalWebSocketCtor?:AgentWebSocketCtor
globalThis.WebSocket).
Inject one for environments without a global, or for tests.
AgentEvent
One item of the agent’s event stream, as delivered toonEvents listeners.
Properties
at
Wall-clock stamp at emit time; informational (ordering is byreadonlyat:Date
seq).
payload
The event’s payload, exactly as the agent emitted it.readonlypayload:unknown
seq
Monotonic position in the event stream; also the resume cursor.readonlyseq:number
type
The event kind the agent chose when emitting (e.g.readonlytype:string
"progress").