This commit is contained in:
@@ -38,6 +38,10 @@ defmodule ElixirAi.ChatRunner do
|
||||
GenServer.call(via(name), {:tool_config, {:set_tool_choice, tool_choice}})
|
||||
end
|
||||
|
||||
def set_provider(name, provider_id) when is_binary(provider_id) do
|
||||
GenServer.call(via(name), {:tool_config, {:set_provider, provider_id}})
|
||||
end
|
||||
|
||||
def register_liveview_pid(name, liveview_pid) when is_pid(liveview_pid) do
|
||||
GenServer.call(via(name), {:session, {:register_liveview_pid, liveview_pid}})
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
defmodule ElixirAi.ChatRunner.ToolConfig do
|
||||
alias ElixirAi.{AiTools, Conversation}
|
||||
alias ElixirAi.{AiProvider, AiTools, Conversation}
|
||||
|
||||
def handle_call({:set_tool_choice, tool_choice}, _from, state) do
|
||||
Conversation.update_tool_choice(state.name, tool_choice)
|
||||
@@ -19,4 +19,13 @@ defmodule ElixirAi.ChatRunner.ToolConfig do
|
||||
liveview_tools: liveview_tools
|
||||
}}
|
||||
end
|
||||
|
||||
def handle_call({:set_provider, provider_id}, _from, state) do
|
||||
with :ok <- Conversation.update_provider(state.name, provider_id),
|
||||
{:ok, provider} <- AiProvider.find_by_id(provider_id) do
|
||||
{:reply, {:ok, provider}, %{state | provider: provider}}
|
||||
else
|
||||
error -> {:reply, error, state}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -114,6 +114,34 @@ defmodule ElixirAi.AiProvider do
|
||||
end
|
||||
end
|
||||
|
||||
def find_by_id(id) do
|
||||
case Ecto.UUID.dump(id) do
|
||||
{:ok, binary_id} ->
|
||||
sql = """
|
||||
SELECT id, name, model_name, api_token, completions_url
|
||||
FROM ai_providers
|
||||
WHERE id = $(id)
|
||||
LIMIT 1
|
||||
"""
|
||||
|
||||
params = %{"id" => binary_id}
|
||||
|
||||
case DbHelpers.run_sql(sql, params, providers_topic(), AiProviderSchema.schema()) do
|
||||
{:error, _} ->
|
||||
{:error, :db_error}
|
||||
|
||||
[] ->
|
||||
{:error, :not_found}
|
||||
|
||||
[row | _] ->
|
||||
{:ok, row |> convert_uuid_to_string() |> then(&struct(AiProviderSchema, &1))}
|
||||
end
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
end
|
||||
end
|
||||
|
||||
def delete(id) do
|
||||
sql = "DELETE FROM ai_providers WHERE id = $(id)::uuid"
|
||||
params = %{"id" => id}
|
||||
|
||||
@@ -173,6 +173,31 @@ defmodule ElixirAi.Conversation do
|
||||
end
|
||||
end
|
||||
|
||||
def update_provider(name, provider_id) when is_binary(provider_id) do
|
||||
case Ecto.UUID.dump(provider_id) do
|
||||
{:ok, binary_id} ->
|
||||
sql = """
|
||||
UPDATE conversations
|
||||
SET ai_provider_id = $(ai_provider_id), updated_at = $(updated_at)
|
||||
WHERE name = $(name)
|
||||
"""
|
||||
|
||||
params = %{
|
||||
"name" => name,
|
||||
"ai_provider_id" => binary_id,
|
||||
"updated_at" => now()
|
||||
}
|
||||
|
||||
case DbHelpers.run_sql(sql, params, "conversations") do
|
||||
{:error, :db_error} -> {:error, :db_error}
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
:error ->
|
||||
{:error, :invalid_uuid}
|
||||
end
|
||||
end
|
||||
|
||||
def find_id(name) do
|
||||
sql = "SELECT id FROM conversations WHERE name = $(name) LIMIT 1"
|
||||
params = %{"name" => name}
|
||||
|
||||
Reference in New Issue
Block a user