cozy cozy sql
Some checks failed
CI/CD Pipeline / build (push) Failing after 4s

This commit is contained in:
2026-03-13 13:00:15 -06:00
parent b5504dbdca
commit 04103dbfbd
7 changed files with 543 additions and 129 deletions

View File

@@ -20,7 +20,7 @@ defmodule ElixirAi.ConversationManager do
def init(_) do
Logger.info("ConversationManager initializing...")
send(self(), :load_conversations)
{:ok, :loading_conversations}
{:ok, %{conversations: :loading, subscriptions: MapSet.new()}}
end
def create_conversation(name, ai_provider_id) do
@@ -39,58 +39,77 @@ defmodule ElixirAi.ConversationManager do
GenServer.call(@name, {:get_messages, name})
end
def handle_call(message, from, :loading_conversations) do
def handle_call(message, from, %{conversations: :loading} = state) do
Logger.warning(
"Received call #{inspect(message)} from #{inspect(from)} while loading conversations. Retrying after delay."
)
Process.send_after(self(), {:retry_call, message, from}, 100)
{:noreply, :loading_conversations}
{:noreply, state}
end
def handle_call({:create, name, ai_provider_id}, _from, conversations) do
def handle_call(
{:create, name, ai_provider_id},
_from,
%{conversations: conversations, subscriptions: subscriptions} = state
) do
if Map.has_key?(conversations, name) do
{:reply, {:error, :already_exists}, conversations}
{:reply, {:error, :already_exists}, state}
else
case Conversation.create(name, ai_provider_id) do
:ok ->
case start_and_subscribe(name) do
{:ok, _pid} = ok -> {:reply, ok, Map.put(conversations, name, [])}
error -> {:reply, error, conversations}
case start_and_subscribe(name, subscriptions) do
{:ok, pid, new_subscriptions} ->
{:reply, {:ok, pid},
%{
state
| conversations: Map.put(conversations, name, []),
subscriptions: new_subscriptions
}}
{:error, _reason} = error ->
{:reply, error, state}
end
{:error, _} = error ->
{:reply, error, conversations}
{:reply, error, state}
end
end
end
def handle_call({:open, name}, _from, conversations) do
def handle_call(
{:open, name},
_from,
%{conversations: conversations, subscriptions: subscriptions} = state
) do
if Map.has_key?(conversations, name) do
case start_and_subscribe(name) do
{:ok, _pid} = ok -> {:reply, ok, conversations}
error -> {:reply, error, conversations}
case start_and_subscribe(name, subscriptions) do
{:ok, pid, new_subscriptions} ->
{:reply, {:ok, pid}, %{state | subscriptions: new_subscriptions}}
{:error, _reason} = error ->
{:reply, error, state}
end
else
{:reply, {:error, :not_found}, conversations}
{:reply, {:error, :not_found}, state}
end
end
def handle_call(:list, _from, conversations) do
def handle_call(:list, _from, %{conversations: conversations} = state) do
keys = Map.keys(conversations)
Logger.debug(
"list_conversations returning: #{inspect(keys, limit: :infinity, printable_limit: :infinity, binaries: :as_binaries)}"
)
{:reply, keys, conversations}
{:reply, keys, state}
end
def handle_call({:get_messages, name}, _from, conversations) do
{:reply, Map.get(conversations, name, []), conversations}
def handle_call({:get_messages, name}, _from, %{conversations: conversations} = state) do
{:reply, Map.get(conversations, name, []), state}
end
def handle_info({:store_message, name, message}, conversations) do
def handle_info({:store_message, name, message}, %{conversations: conversations} = state) do
case Conversation.find_id(name) do
{:ok, conv_id} ->
Message.insert(conv_id, message, topic: ElixirAi.ChatRunner.message_topic(name))
@@ -99,17 +118,17 @@ defmodule ElixirAi.ConversationManager do
:ok
end
{:noreply, Map.update(conversations, name, [message], &(&1 ++ [message]))}
{:noreply,
%{state | conversations: Map.update(conversations, name, [message], &(&1 ++ [message]))}}
end
def handle_info(:load_conversations, _conversations) do
def handle_info(:load_conversations, state) do
conversation_list = Conversation.all_names()
Logger.info("Loaded #{length(conversation_list)} conversations from DB")
conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end)
Logger.info("Conversation map keys: #{inspect(Map.keys(conversations))}")
# {:ok, conversations}
{:noreply, conversations}
{:noreply, %{state | conversations: conversations}}
end
def handle_info({:retry_call, message, from}, state) do
@@ -123,7 +142,7 @@ defmodule ElixirAi.ConversationManager do
end
end
defp start_and_subscribe(name) do
defp start_and_subscribe(name, subscriptions) do
result =
case Horde.DynamicSupervisor.start_child(
ElixirAi.ChatRunnerSupervisor,
@@ -135,12 +154,19 @@ defmodule ElixirAi.ConversationManager do
end
case result do
{:ok, _pid} ->
Phoenix.PubSub.subscribe(ElixirAi.PubSub, ElixirAi.ChatRunner.message_topic(name))
result
{:ok, pid} ->
new_subscriptions =
if MapSet.member?(subscriptions, name) do
subscriptions
else
Phoenix.PubSub.subscribe(ElixirAi.PubSub, ElixirAi.ChatRunner.message_topic(name))
MapSet.put(subscriptions, name)
end
_ ->
result
{:ok, pid, new_subscriptions}
error ->
error
end
end
end