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

This commit is contained in:
2026-03-12 13:20:35 -06:00
parent abe27b82d1
commit 399eb9f93f
10 changed files with 203 additions and 55 deletions

View File

@@ -17,3 +17,12 @@ video ideas:
real time blog: <https://seanmoriarity.com/2024/02/25/implementing-natural-conversational-agents-with-elixir/> real time blog: <https://seanmoriarity.com/2024/02/25/implementing-natural-conversational-agents-with-elixir/>
elixir clustering examples and architecture: <https://oneuptime.com/blog/post/2026-01-26-elixir-distributed-systems/view>
process groups for distributing workers across cluster: <https://memo.d.foundation/topics/elixir/pg-in-elixir>

View File

@@ -1,6 +1,7 @@
defmodule ElixirAi.ConversationManager do defmodule ElixirAi.ConversationManager do
use GenServer use GenServer
alias ElixirAi.{Conversation, Message} alias ElixirAi.{Conversation, Message}
require Logger
@name {:via, Horde.Registry, {ElixirAi.ChatRegistry, __MODULE__}} @name {:via, Horde.Registry, {ElixirAi.ChatRegistry, __MODULE__}}
@@ -17,8 +18,19 @@ defmodule ElixirAi.ConversationManager do
end end
def init(_) do def init(_) do
Logger.info("ConversationManager initializing...")
conversation_list = Conversation.all_names() conversation_list = Conversation.all_names()
Logger.info("Loaded #{length(conversation_list)} conversations from DB")
# Log each conversation and check for UTF-8 issues
Enum.each(conversation_list, fn conv ->
Logger.info(
"Conversation: #{inspect(conv, limit: :infinity, printable_limit: :infinity, binaries: :as_binaries)}"
)
end)
conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end) conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end)
Logger.info("Conversation map keys: #{inspect(Map.keys(conversations))}")
{:ok, conversations} {:ok, conversations}
end end
@@ -67,7 +79,13 @@ defmodule ElixirAi.ConversationManager do
end end
def handle_call(:list, _from, conversations) do def handle_call(:list, _from, conversations) do
{:reply, Map.keys(conversations), conversations} keys = Map.keys(conversations)
Logger.debug(
"list_conversations returning: #{inspect(keys, limit: :infinity, printable_limit: :infinity, binaries: :as_binaries)}"
)
{:reply, keys, conversations}
end end
def handle_call({:get_messages, name}, _from, conversations) do def handle_call({:get_messages, name}, _from, conversations) do

View File

@@ -1,21 +1,34 @@
defmodule ElixirAi.AiProvider do defmodule ElixirAi.AiProvider do
import Ecto.Query import Ecto.Query
alias ElixirAi.Repo alias ElixirAi.Repo
alias ElixirAi.Data.AiProviderSchema
require Logger
def all do def all do
results =
Repo.all( Repo.all(
from(p in "ai_providers", from(p in AiProviderSchema,
select: %{ select: %{
id: p.id, id: p.id,
name: p.name, name: p.name,
model_name: p.model_name, model_name: p.model_name
api_token: p.api_token,
completions_url: p.completions_url
} }
) )
) )
|> Enum.map(&convert_id_to_string/1)
Logger.debug("AiProvider.all() returning: #{inspect(results)}")
results
end end
# Convert binary UUID to string for frontend
defp convert_id_to_string(%{id: id} = provider) when is_binary(id) do
%{provider | id: Ecto.UUID.cast!(id)}
end
defp convert_id_to_string(provider), do: provider
def create(attrs) do def create(attrs) do
now = DateTime.truncate(DateTime.utc_now(), :second) now = DateTime.truncate(DateTime.utc_now(), :second)
@@ -62,7 +75,7 @@ defmodule ElixirAi.AiProvider do
) )
) do ) do
nil -> {:error, :not_found} nil -> {:error, :not_found}
provider -> {:ok, provider} provider -> {:ok, convert_id_to_string(provider)}
end end
end end
@@ -70,14 +83,10 @@ defmodule ElixirAi.AiProvider do
case Repo.aggregate(from(p in "ai_providers"), :count) do case Repo.aggregate(from(p in "ai_providers"), :count) do
0 -> 0 ->
attrs = %{ attrs = %{
name: System.get_env("DEFAULT_PROVIDER_NAME", "default_provider"), name: "default",
model_name: System.get_env("DEFAULT_MODEL_NAME", "gpt-4"), model_name: Application.fetch_env!(:elixir_ai, :ai_model),
api_token: System.get_env("DEFAULT_API_TOKEN", ""), api_token: Application.fetch_env!(:elixir_ai, :ai_token),
completions_url: completions_url: Application.fetch_env!(:elixir_ai, :ai_endpoint)
System.get_env(
"DEFAULT_COMPLETIONS_URL",
"https://api.openai.com/v1/chat/completions"
)
} }
create(attrs) create(attrs)

View File

@@ -1,11 +1,52 @@
defmodule ElixirAi.Conversation do defmodule ElixirAi.Conversation do
import Ecto.Query import Ecto.Query
alias ElixirAi.Repo alias ElixirAi.Repo
alias ElixirAi.Data.ConversationSchema
alias ElixirAi.Data.AiProviderSchema
require Logger
defmodule Provider do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field(:name, :string)
field(:model_name, :string)
field(:api_token, :string)
field(:completions_url, :string)
end
def changeset(provider, attrs) do
provider
|> cast(attrs, [:name, :model_name, :api_token, :completions_url])
|> validate_required([:name, :model_name, :api_token, :completions_url])
end
end
defmodule ConversationInfo do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field(:name, :string)
embeds_one(:provider, Provider)
end
def changeset(conversation, attrs) do
conversation
|> cast(attrs, [:name])
|> validate_required([:name])
|> cast_embed(:provider, with: &Provider.changeset/2, required: true)
end
end
def all_names do def all_names do
results =
Repo.all( Repo.all(
from(c in "conversations", from(c in ConversationSchema,
left_join: p in "ai_providers", left_join: p in AiProviderSchema,
on: c.ai_provider_id == p.id, on: c.ai_provider_id == p.id,
select: %{ select: %{
name: c.name, name: c.name,
@@ -18,15 +59,34 @@ defmodule ElixirAi.Conversation do
} }
) )
) )
Enum.map(results, fn attrs ->
changeset = ConversationInfo.changeset(%ConversationInfo{}, attrs)
if changeset.valid? do
Ecto.Changeset.apply_changes(changeset)
else
Logger.error("Invalid conversation data: #{inspect(changeset.errors)}")
raise ArgumentError, "Invalid conversation data: #{inspect(changeset.errors)}"
end
end)
end end
def create(name, ai_provider_id) do def create(name, ai_provider_id) when is_binary(ai_provider_id) do
case Repo.insert_all("conversations", [ # Convert string UUID from frontend to binary UUID for database
[name: name, ai_provider_id: ai_provider_id, inserted_at: now(), updated_at: now()] case Ecto.UUID.dump(ai_provider_id) do
]) do {:ok, binary_id} ->
Repo.insert_all("conversations", [
[name: name, ai_provider_id: binary_id, inserted_at: now(), updated_at: now()]
])
|> case do
{1, _} -> :ok {1, _} -> :ok
_ -> {:error, :db_error} _ -> {:error, :db_error}
end end
:error ->
{:error, :invalid_uuid}
end
rescue rescue
e in Ecto.ConstraintError -> e in Ecto.ConstraintError ->
if e.constraint == "conversations_name_index", if e.constraint == "conversations_name_index",
@@ -35,7 +95,7 @@ defmodule ElixirAi.Conversation do
end end
def find_id(name) do def find_id(name) do
case Repo.one(from(c in "conversations", where: c.name == ^name, select: c.id)) do case Repo.one(from(c in ConversationSchema, where: c.name == ^name, select: c.id)) do
nil -> {:error, :not_found} nil -> {:error, :not_found}
id -> {:ok, id} id -> {:ok, id}
end end

View File

@@ -1,10 +1,11 @@
defmodule ElixirAi.Message do defmodule ElixirAi.Message do
import Ecto.Query import Ecto.Query
alias ElixirAi.Repo alias ElixirAi.Repo
alias ElixirAi.Data.MessageSchema
def load_for_conversation(conversation_id) do def load_for_conversation(conversation_id) do
Repo.all( Repo.all(
from m in "messages", from m in MessageSchema,
where: m.conversation_id == ^conversation_id, where: m.conversation_id == ^conversation_id,
order_by: m.id, order_by: m.id,
select: %{ select: %{

View File

@@ -0,0 +1,15 @@
defmodule ElixirAi.Data.AiProviderSchema do
use Ecto.Schema
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "ai_providers" do
field(:name, :string)
field(:model_name, :string)
field(:api_token, :string)
field(:completions_url, :string)
timestamps(type: :utc_datetime)
end
end

View File

@@ -0,0 +1,13 @@
defmodule ElixirAi.Data.ConversationSchema do
use Ecto.Schema
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "conversations" do
field(:name, :string)
belongs_to(:ai_provider, ElixirAi.Data.AiProviderSchema, type: :binary_id)
timestamps(type: :utc_datetime)
end
end

View File

@@ -0,0 +1,16 @@
defmodule ElixirAi.Data.MessageSchema do
use Ecto.Schema
@primary_key {:id, :id, autogenerate: true}
schema "messages" do
belongs_to(:conversation, ElixirAi.Data.ConversationSchema, type: :binary_id)
field(:role, :string)
field(:content, :string)
field(:reasoning_content, :string)
field(:tool_calls, :map)
field(:tool_call_id, :string)
timestamps(inserted_at: :inserted_at, updated_at: false, type: :utc_datetime)
end
end

View File

@@ -79,12 +79,6 @@ defmodule ElixirAiWeb.AiProvidersLive do
<div class="flex-1"> <div class="flex-1">
<h3 class="text-sm font-medium text-cyan-300">{provider.name}</h3> <h3 class="text-sm font-medium text-cyan-300">{provider.name}</h3>
<p class="text-xs text-cyan-500 mt-1">Model: {provider.model_name}</p> <p class="text-xs text-cyan-500 mt-1">Model: {provider.model_name}</p>
<p class="text-xs text-cyan-600 mt-1 truncate">
Endpoint: {provider.completions_url}
</p>
<p class="text-xs text-cyan-600 mt-1">
Token: {String.slice(provider.api_token || "", 0..7)}***
</p>
</div> </div>
</div> </div>
</li> </li>

View File

@@ -2,16 +2,29 @@ defmodule ElixirAiWeb.HomeLive do
use ElixirAiWeb, :live_view use ElixirAiWeb, :live_view
import ElixirAiWeb.FormComponents import ElixirAiWeb.FormComponents
alias ElixirAi.{ConversationManager, AiProvider} alias ElixirAi.{ConversationManager, AiProvider}
require Logger
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
if connected?(socket) do if connected?(socket) do
Phoenix.PubSub.subscribe(ElixirAi.PubSub, "ai_providers") Phoenix.PubSub.subscribe(ElixirAi.PubSub, "ai_providers")
end end
conversations = ConversationManager.list_conversations()
Logger.debug(
"Conversations: #{inspect(conversations, limit: :infinity, printable_limit: :infinity)}"
)
ai_providers = AiProvider.all()
Logger.debug(
"AI Providers: #{inspect(ai_providers, limit: :infinity, printable_limit: :infinity)}"
)
{:ok, {:ok,
socket socket
|> assign(conversations: ConversationManager.list_conversations()) |> assign(conversations: conversations)
|> assign(ai_providers: AiProvider.all()) |> assign(ai_providers: ai_providers)
|> assign(new_name: "") |> assign(new_name: "")
|> assign(error: nil)} |> assign(error: nil)}
end end
@@ -62,9 +75,9 @@ defmodule ElixirAiWeb.HomeLive do
<% end %> <% end %>
</div> </div>
<div> <%!-- <div>
<.live_component module={ElixirAiWeb.AiProvidersLive} id="ai-providers" /> <.live_component module={ElixirAiWeb.AiProvidersLive} id="ai-providers" />
</div> </div> --%>
</div> </div>
""" """
end end