Cyclium.Conversations.LiveHelpers (Cyclium v0.1.13)

Copy Markdown View Source

Reusable helpers for conversation LiveViews.

Provides bus event handling, message loading from episodes, and conversation lifecycle management. Consuming apps use this module to get module attributes and import the helper functions.

Usage

defmodule MyAppWeb.ConversationLive.Show do
  use MyAppWeb, :live_view
  use Cyclium.Conversations.LiveHelpers, actor_id: "my_actor"

  # @__actor_id and @__dispatch are set for you.
  # All helper functions are imported.
end

Options

Summary

Functions

Cancel the current turn (the conversation's active episode), keeping the conversation open. Returns the result of Episodes.request_cancel/2, or {:error, :no_active_episode} if nothing is running.

Dispatch a user message. Appends the user message to the message list and creates the episode.

Reconstruct chat messages from completed episodes in a conversation.

Load or create a conversation based on params and live_action. Returns {conversation, messages}.

Handle a conversation status change event. Returns updated conversation or :ignore.

Handle an episode.canceled bus event. Appends a "stopped" marker and clears the sending state. Returns {:ok, assigns_map} or :ignore.

Handle an episode.completed bus event for a conversation LiveView.

Handle an episode.failed bus event for a conversation LiveView.

Functions

cancel_current_turn(conversation_id, reason \\ "user_canceled")

Cancel the current turn (the conversation's active episode), keeping the conversation open. Returns the result of Episodes.request_cancel/2, or {:error, :no_active_episode} if nothing is running.

dispatch_message(conversation_id, message, current_messages, dispatch_mod, opts \\ [])

Dispatch a user message. Appends the user message to the message list and creates the episode.

Returns {:ok, updated_messages} or {:error, updated_messages, reason}.

load_messages_from_episodes(conversation_id)

Reconstruct chat messages from completed episodes in a conversation.

load_or_create_conversation(params, live_action, actor_id, principal)

Load or create a conversation based on params and live_action. Returns {conversation, messages}.

on_conversation_status_change(event_conv_id, current_conv_id)

Handle a conversation status change event. Returns updated conversation or :ignore.

on_episode_canceled(episode_id, conversation_id, actor_id, current_messages)

Handle an episode.canceled bus event. Appends a "stopped" marker and clears the sending state. Returns {:ok, assigns_map} or :ignore.

on_episode_completed(episode_id, conversation_id, actor_id, current_messages)

Handle an episode.completed bus event for a conversation LiveView.

Verifies the episode belongs to both the current conversation and the given actor before reacting. Passing actor_id (use @__actor_id) bakes the actor check into the helper, so it holds even if the handle_info pattern is later broadened to drop the actor_id: match. Returns {:ok, assigns_map} on a match, :ignore otherwise.

Example

def handle_info({:bus, "episode.completed", %{episode_id: eid, actor_id: @__actor_id}}, socket) do
  case on_episode_completed(eid, socket.assigns.conversation.id, @__actor_id, socket.assigns.messages) do
    {:ok, new_assigns} -> {:noreply, assign(socket, new_assigns)}
    :ignore -> {:noreply, socket}
  end
end

on_episode_failed(episode_id, conversation_id, actor_id, current_messages)

Handle an episode.failed bus event for a conversation LiveView.

Verifies the episode matches both the conversation and actor_id (see on_episode_completed/4). Returns {:ok, assigns_map} or :ignore.