This commit is contained in:
@@ -1,103 +1,99 @@
|
||||
defmodule ElixirAi.Conversation do
|
||||
import Ecto.Query
|
||||
use ElixirAi.Data
|
||||
alias ElixirAi.Repo
|
||||
alias ElixirAi.Data.ConversationSchema
|
||||
alias ElixirAi.Data.AiProviderSchema
|
||||
require Logger
|
||||
|
||||
defmodule Provider do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
defstruct [:name, :model_name, :api_token, :completions_url]
|
||||
|
||||
@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])
|
||||
def schema do
|
||||
Zoi.object(%{
|
||||
name: Zoi.string(),
|
||||
model_name: Zoi.string(),
|
||||
api_token: Zoi.string(),
|
||||
completions_url: Zoi.string()
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
defmodule ConversationInfo do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
defstruct [:name, :provider]
|
||||
|
||||
@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)
|
||||
def schema do
|
||||
Zoi.object(%{
|
||||
name: Zoi.string(),
|
||||
provider:
|
||||
Zoi.object(%{
|
||||
name: Zoi.string(),
|
||||
model_name: Zoi.string(),
|
||||
api_token: Zoi.string(),
|
||||
completions_url: Zoi.string()
|
||||
})
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def all_names do
|
||||
results =
|
||||
Repo.all(
|
||||
from(c in ConversationSchema,
|
||||
left_join: p in AiProviderSchema,
|
||||
on: c.ai_provider_id == p.id,
|
||||
select: %{
|
||||
name: c.name,
|
||||
provider: %{
|
||||
name: p.name,
|
||||
model_name: p.model_name,
|
||||
api_token: p.api_token,
|
||||
completions_url: p.completions_url
|
||||
}
|
||||
broadcast_error topic: "conversations" do
|
||||
sql = """
|
||||
SELECT c.name, p.name, p.model_name, p.api_token, p.completions_url
|
||||
FROM conversations c
|
||||
LEFT JOIN ai_providers p ON c.ai_provider_id = p.id
|
||||
"""
|
||||
|
||||
result = Ecto.Adapters.SQL.query!(Repo, sql, [])
|
||||
|
||||
Enum.map(result.rows, fn [name, provider_name, model_name, api_token, completions_url] ->
|
||||
attrs = %{
|
||||
name: name,
|
||||
provider: %{
|
||||
name: provider_name,
|
||||
model_name: model_name,
|
||||
api_token: api_token,
|
||||
completions_url: completions_url
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Enum.map(results, fn attrs ->
|
||||
changeset = ConversationInfo.changeset(%ConversationInfo{}, attrs)
|
||||
case Zoi.parse(ConversationInfo.schema(), attrs) do
|
||||
{:ok, valid} ->
|
||||
struct(ConversationInfo, Map.put(valid, :provider, struct(Provider, valid.provider)))
|
||||
|
||||
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)
|
||||
{:error, errors} ->
|
||||
Logger.error("Invalid conversation data: #{inspect(errors)}")
|
||||
raise ArgumentError, "Invalid conversation data: #{inspect(errors)}"
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
def create(name, ai_provider_id) when is_binary(ai_provider_id) do
|
||||
# Convert string UUID from frontend to binary UUID for database
|
||||
case Ecto.UUID.dump(ai_provider_id) 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
|
||||
_ -> {:error, :db_error}
|
||||
end
|
||||
broadcast_error topic: "conversations" do
|
||||
case Ecto.UUID.dump(ai_provider_id) do
|
||||
{:ok, binary_id} ->
|
||||
sql = """
|
||||
INSERT INTO conversations (name, ai_provider_id, inserted_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
"""
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
timestamp = now()
|
||||
params = [name, binary_id, timestamp, timestamp]
|
||||
|
||||
Ecto.Adapters.SQL.query!(Repo, sql, params)
|
||||
:ok
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
end
|
||||
end
|
||||
rescue
|
||||
e in Ecto.ConstraintError ->
|
||||
if e.constraint == "conversations_name_index",
|
||||
do: {:error, :already_exists},
|
||||
else: {:error, :db_error}
|
||||
end
|
||||
|
||||
def find_id(name) do
|
||||
case Repo.one(from(c in ConversationSchema, where: c.name == ^name, select: c.id)) do
|
||||
nil -> {:error, :not_found}
|
||||
id -> {:ok, id}
|
||||
broadcast_error topic: "conversations" do
|
||||
sql = "SELECT id FROM conversations WHERE name = $1 LIMIT 1"
|
||||
|
||||
case Ecto.Adapters.SQL.query!(Repo, sql, [name]) do
|
||||
%{rows: []} -> {:error, :not_found}
|
||||
%{rows: [[id] | _]} -> {:ok, id}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user