Codex.Realtime.Model behaviour (Codex SDK v0.7.2)

Copy Markdown View Source

Behaviour for realtime model implementations.

This behaviour defines the interface for connecting to a realtime model and sending/receiving events. Implementations can include WebSocket-based connections to OpenAI's Realtime API or mock implementations for testing.

Implementing a Model

To implement a custom realtime model, use this behaviour and implement all callbacks:

defmodule MyCustomModel do
  @behaviour Codex.Realtime.Model

  @impl true
  def connect(config) do
    # Establish connection
    :ok
  end

  @impl true
  def add_listener(listener) do
    # Add event listener
    :ok
  end

  # ... implement other callbacks
end

Listeners

Listeners can be either a pid or a function. When an event is received, it is forwarded to all registered listeners:

  • If the listener is a pid, the event is sent as {:model_event, event}
  • If the listener is a function, it is called with the event

Summary

Callbacks

Add a listener for model events.

Close the model connection.

Establish a connection to the model.

Remove a listener for model events.

Send an event to the model.

Types

listener()

@type listener() :: pid() | (Codex.Realtime.ModelEvents.t() -> :ok)

Callbacks

add_listener(listener)

@callback add_listener(listener()) :: :ok

Add a listener for model events.

Listeners receive all events from the model connection.

close()

@callback close() :: :ok

Close the model connection.

Cleans up resources and closes any WebSocket connections.

connect(config)

@callback connect(config :: Codex.Realtime.Config.ModelConfig.t()) ::
  :ok | {:error, term()}

Establish a connection to the model.

Called when the session starts. The config contains API keys, URLs, and initial model settings.

remove_listener(listener)

@callback remove_listener(listener()) :: :ok

Remove a listener for model events.

send_event(event)

@callback send_event(event :: Codex.Realtime.ModelInputs.send_event()) ::
  :ok | {:error, term()}

Send an event to the model.

Events include user input, audio data, tool outputs, and session updates.