more db fixes
Some checks failed
CI/CD Pipeline / build (push) Failing after 3s

This commit is contained in:
2026-03-13 13:18:03 -06:00
parent 04103dbfbd
commit 59a8ad9635
6 changed files with 131 additions and 83 deletions

View File

@@ -109,6 +109,11 @@ defmodule ElixirAi.ConversationManager do
{:reply, Map.get(conversations, name, []), state}
end
def handle_info({:db_error, reason}, state) do
Logger.error("ConversationManager received db_error: #{inspect(reason)}")
{:noreply, state}
end
def handle_info({:store_message, name, message}, %{conversations: conversations} = state) do
case Conversation.find_id(name) do
{:ok, conv_id} ->

View File

@@ -7,19 +7,23 @@ defmodule ElixirAi.Data.DbHelpers do
end
def run_sql(sql, params, topic) do
original_sql = sql
original_params = params
{sql, params} = named_params_to_positional_params(sql, params)
try do
result = Ecto.Adapters.SQL.query!(ElixirAi.Repo, sql, params)
# Transform rows to maps with column names as keys
Enum.map(result.rows, fn row ->
Enum.map(result.rows || [], fn row ->
Enum.zip(result.columns, row)
|> Enum.into(%{})
end)
rescue
exception ->
Logger.error("Database error: #{Exception.message(exception)}")
Logger.error("Failed SQL: #{original_sql}")
Logger.error("SQL params: #{inspect(original_params, pretty: true)}")
Phoenix.PubSub.broadcast(
ElixirAi.PubSub,

View File

@@ -73,7 +73,7 @@ defmodule ElixirAi.Message do
$(role),
$(content),
$(reasoning_content),
$(tool_calls),
$(tool_calls)::jsonb,
$(tool_call_id),
$(inserted_at)
)

View File

@@ -24,53 +24,62 @@ defmodule ElixirAiWeb.HomeLive do
<div>
<h1 class="text-lg font-semibold text-cyan-300 mb-8">Conversations</h1>
<ul class="mb-8 space-y-2">
<%= if @conversations == [] do %>
<li class="text-sm text-cyan-700">No conversations yet.</li>
<% end %>
<%= for name <- @conversations do %>
<li>
<.link
navigate={~p"/chat/#{name}"}
class="block px-4 py-2 rounded-lg border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors text-sm"
>
{name}
</.link>
</li>
<% end %>
</ul>
<.conversation_list conversations={@conversations} />
<form phx-submit="create" class="space-y-2">
<.input type="text" name="name" value={@new_name} label="Conversation Name" />
<select
name="ai_provider_id"
class="w-full rounded px-3 py-2 text-sm bg-cyan-950/20 border border-cyan-900/40 text-cyan-100 focus:outline-none focus:ring-1 focus:ring-cyan-700"
>
<option value="">Select AI Provider</option>
<%= for provider <- @ai_providers do %>
<option value={provider.id}>{provider.name} - {provider.model_name}</option>
<% end %>
</select>
<button
type="submit"
class="w-full px-4 py-2 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
Create
</button>
</form>
<.create_conversation_form new_name={@new_name} ai_providers={@ai_providers} />
<%= if @error do %>
<p class="mt-2 text-sm text-red-400">{@error}</p>
<% end %>
</div>
<%!-- <div>
<.live_component module={ElixirAiWeb.AiProvidersLive} id="ai-providers" />
</div> --%>
</div>
"""
end
defp conversation_list(assigns) do
~H"""
<ul class="mb-8 space-y-2">
<%= if @conversations == [] do %>
<li class="text-sm text-cyan-700">No conversations yet.</li>
<% end %>
<%= for name <- @conversations do %>
<li>
<.link
navigate={~p"/chat/#{name}"}
class="block px-4 py-2 rounded-lg border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors text-sm"
>
{name}
</.link>
</li>
<% end %>
</ul>
"""
end
defp create_conversation_form(assigns) do
~H"""
<form phx-submit="create" class="space-y-2">
<.input type="text" name="name" value={@new_name} label="Conversation Name" />
<select
name="ai_provider_id"
class="w-full rounded px-3 py-2 text-sm bg-cyan-950/20 border border-cyan-900/40 text-cyan-100 focus:outline-none focus:ring-1 focus:ring-cyan-700"
>
<%= for {provider, index} <- Enum.with_index(@ai_providers) do %>
<option value={provider.id} selected={index == 0}>
{provider.name} - {provider.model_name}
</option>
<% end %>
</select>
<button
type="submit"
class="w-full px-4 py-2 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
Create
</button>
</form>
"""
end
def handle_event("create", %{"name" => name, "ai_provider_id" => provider_id}, socket) do
name = String.trim(name)