mix sagents.gen.live_helpers (Sagents v0.8.0-rc.4)

Copy Markdown

Generates AgentLiveHelpers module for Phoenix LiveView integration with Sagents.

This module provides reusable patterns for agent event handling, state management, and UI updates in Phoenix LiveView applications. All functions take a socket and return an updated socket, following the LiveView pattern.

Examples

mix sagents.gen.live_helpers MyAppWeb.AgentLiveHelpers \
  --context MyApp.Conversations

Generated files

  • Helper module: Reusable handlers for agent events (status, messages, tools, lifecycle) and state management helpers (init, load, reset)

Options

  • --context - Your conversations context module (required, e.g., MyApp.Conversations)

Integration

After generation, use the helpers in your LiveView:

# In mount/3
def mount(_params, _session, socket) do
  {:ok, socket |> AgentLiveHelpers.init_agent_state() |> assign(...)}
end

# In handle_params/3 -- pass scope (required) and user_id (for presence)
def handle_params(%{"conversation_id" => id}, _uri, socket) do
  current_scope = socket.assigns.current_scope

  case AgentLiveHelpers.load_conversation(
         socket,
         id,
         scope: current_scope,
         user_id: current_scope.user.id
       ) do
    {:ok, socket} -> {:noreply, socket}
    {:error, socket} -> {:noreply, push_navigate(socket, to: ~p"/chat")}
  end
end

# In handle_info/2
def handle_info({:agent, {:status_changed, :running, nil}}, socket) do
  {:noreply, AgentLiveHelpers.handle_status_running(socket)}
end

See the generated file for complete documentation and usage examples.

Reference Implementation

See agents_demo/test/agents_demo_web/live/agent_live_helpers_test.exs for a comprehensive test suite that you can adapt for your application.