PhoenixKitAI.Realtime.Session (PhoenixKitAI v0.15.0)

Copy Markdown View Source

Owns one xAI realtime WebSocket connection (Xai.Realtime) on behalf of a single Playground LiveView, forwarding audio chunks and events to it as plain messages.

Runs under PhoenixKitAI.Realtime.Supervisor (a DynamicSupervisor contributed via PhoenixKitAI.children/0) with restart: :temporary — a dead session is not auto-restarted, the LiveView re-triggers "start voice" itself.

Cleanup

Xai.Realtime.connect_tts/1 is called from init/1, so the WebSockex connection links to this process, not the owning LiveView — a socket crash only takes down the session. The reverse direction (LiveView goes away) is handled by monitoring live_view_pid: LiveView's own terminate/2 isn't reliably called without trap_exit, so closing the connection is driven from here via the :DOWN message instead.

Summary

Functions

Returns a specification to start this module under a supervisor.

Close the realtime connection and stop the session.

Signal end of the current utterance.

Send text to be spoken.

Types

start_opts()

@type start_opts() :: [
  live_view_pid: pid(),
  api_key: String.t(),
  voice: String.t(),
  language: String.t(),
  codec: String.t(),
  sample_rate: pos_integer(),
  realtime_module: module()
]

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close(pid)

@spec close(pid()) :: :ok

Close the realtime connection and stop the session.

finish(pid)

@spec finish(pid()) :: :ok

Signal end of the current utterance.

send_text(pid, text)

@spec send_text(pid(), String.t()) :: :ok

Send text to be spoken.

start_link(opts)

@spec start_link(start_opts()) :: GenServer.on_start()