Deepgram.Agent.WebSocket (Deepgram v0.1.0)

View Source

WebSocket client for AI voice agent interactions.

This module provides a WebSocket client that can connect to Deepgram's AI voice agent service for real-time conversational AI interactions.

Summary

Functions

Builds WebSocket headers for authentication.

Builds the WebSocket URL for agent connection.

Closes the agent session.

Injects a message into the agent conversation.

Sends a keepalive message to maintain the connection.

Responds to a function call from the agent.

Sends audio data to the agent.

Sends a text message to the agent.

Starts a WebSocket connection for AI voice agent interactions.

Updates the agent's configuration.

Types

state()

@type state() :: %{
  client: Deepgram.Client.t(),
  settings: Deepgram.Types.Agent.settings_options(),
  callback_pid: pid() | nil,
  connected: boolean(),
  keepalive_ref: reference() | nil
}

Functions

build_websocket_headers(client)

Builds WebSocket headers for authentication.

build_websocket_url(client)

Builds the WebSocket URL for agent connection.

close(agent)

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

Closes the agent session.

inject_message(agent, message)

@spec inject_message(pid(), Deepgram.Types.Agent.inject_message_options()) :: :ok

Injects a message into the agent conversation.

keepalive(agent)

@spec keepalive(pid()) :: :ok

Sends a keepalive message to maintain the connection.

respond_to_function_call(agent, function_call_id, result)

@spec respond_to_function_call(pid(), String.t(), any()) :: :ok

Responds to a function call from the agent.

send_audio(agent, audio_data)

@spec send_audio(pid(), binary()) :: :ok

Sends audio data to the agent.

send_text(agent, text)

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

Sends a text message to the agent.

start_link(client, settings, callback_pid \\ nil)

@spec start_link(
  Deepgram.Client.t(),
  Deepgram.Types.Agent.settings_options(),
  pid() | nil
) ::
  {:ok, pid()} | {:error, any()}

Starts a WebSocket connection for AI voice agent interactions.

Parameters

  • client - A Deepgram.Client struct
  • settings - Agent configuration settings
  • callback_pid - Optional PID to receive messages (defaults to caller)

Examples

iex> client = Deepgram.new(api_key: "your-api-key")
iex> settings = %{agent: %{...}, ...}
iex> {:ok, agent} = Deepgram.Agent.WebSocket.start_link(client, settings)
{:ok, #PID<...>}

update_settings(agent, settings)

@spec update_settings(pid(), Deepgram.Types.Agent.settings_options()) :: :ok

Updates the agent's configuration.