This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
defmodule ElixirAi.Conversation do
|
||||
use ElixirAi.Data
|
||||
alias ElixirAi.Repo
|
||||
alias ElixirAi.Data.DbHelpers
|
||||
require Logger
|
||||
|
||||
defmodule Provider do
|
||||
defstruct [:name, :model_name, :api_token, :completions_url]
|
||||
@@ -33,67 +33,88 @@ defmodule ElixirAi.Conversation do
|
||||
end
|
||||
|
||||
def all_names do
|
||||
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
|
||||
"""
|
||||
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, [])
|
||||
params = %{}
|
||||
|
||||
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
|
||||
case DbHelpers.run_sql(sql, params, "conversations") do
|
||||
{:error, :db_error} ->
|
||||
[]
|
||||
|
||||
result ->
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case Zoi.parse(ConversationInfo.schema(), attrs) do
|
||||
{:ok, valid} ->
|
||||
struct(ConversationInfo, Map.put(valid, :provider, struct(Provider, valid.provider)))
|
||||
case Zoi.parse(ConversationInfo.schema(), attrs) do
|
||||
{:ok, valid} ->
|
||||
struct(
|
||||
ConversationInfo,
|
||||
Map.put(valid, :provider, struct(Provider, valid.provider))
|
||||
)
|
||||
|
||||
{:error, errors} ->
|
||||
Logger.error("Invalid conversation data: #{inspect(errors)}")
|
||||
raise ArgumentError, "Invalid conversation data: #{inspect(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
|
||||
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)
|
||||
"""
|
||||
case Ecto.UUID.dump(ai_provider_id) do
|
||||
{:ok, binary_id} ->
|
||||
sql = """
|
||||
INSERT INTO conversations (name, ai_provider_id, inserted_at, updated_at)
|
||||
VALUES ($(name), $(ai_provider_id), $(inserted_at), $(updated_at))
|
||||
"""
|
||||
|
||||
timestamp = now()
|
||||
params = [name, binary_id, timestamp, timestamp]
|
||||
timestamp = now()
|
||||
|
||||
Ecto.Adapters.SQL.query!(Repo, sql, params)
|
||||
:ok
|
||||
params = %{
|
||||
"name" => name,
|
||||
"ai_provider_id" => binary_id,
|
||||
"inserted_at" => timestamp,
|
||||
"updated_at" => timestamp
|
||||
}
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
end
|
||||
case DbHelpers.run_sql(sql, params, "conversations") do
|
||||
{:error, :db_error} ->
|
||||
{:error, :db_error}
|
||||
|
||||
_result ->
|
||||
:ok
|
||||
end
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
end
|
||||
end
|
||||
|
||||
def find_id(name) do
|
||||
broadcast_error topic: "conversations" do
|
||||
sql = "SELECT id FROM conversations WHERE name = $1 LIMIT 1"
|
||||
sql = "SELECT id FROM conversations WHERE name = $(name) LIMIT 1"
|
||||
params = %{"name" => name}
|
||||
|
||||
case Ecto.Adapters.SQL.query!(Repo, sql, [name]) do
|
||||
%{rows: []} -> {:error, :not_found}
|
||||
%{rows: [[id] | _]} -> {:ok, id}
|
||||
end
|
||||
case DbHelpers.run_sql(sql, params, "conversations") do
|
||||
{:error, :db_error} ->
|
||||
{:error, :db_error}
|
||||
|
||||
%{rows: []} ->
|
||||
{:error, :not_found}
|
||||
|
||||
%{rows: [[id] | _]} ->
|
||||
{:ok, id}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user