Apply mix format
This commit is contained in:
@@ -8,7 +8,8 @@ defmodule N8nOpenaiAdapter.AgentRegistry do
|
||||
|
||||
# --- Client API ---
|
||||
|
||||
def start_link(agents) when is_map(agents), do: GenServer.start_link(__MODULE__, agents, name: __MODULE__)
|
||||
def start_link(agents) when is_map(agents),
|
||||
do: GenServer.start_link(__MODULE__, agents, name: __MODULE__)
|
||||
|
||||
@doc "Return the webhook URL for a model, or :error if unknown."
|
||||
def webhook_for(model) do
|
||||
|
||||
@@ -31,6 +31,7 @@ defmodule N8nOpenaiAdapter.Application do
|
||||
nil ->
|
||||
# single agent from old N8N_WEBHOOK_URL / MODEL_NAME env
|
||||
model = System.get_env("MODEL_NAME", "default-agent")
|
||||
|
||||
case System.get_env("N8N_WEBHOOK_URL") do
|
||||
nil -> %{}
|
||||
webhook -> %{model => webhook}
|
||||
|
||||
@@ -43,7 +43,8 @@ defmodule N8nOpenaiAdapter.Router do
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:halt, send_resp(conn, 401, Jason.encode!(%{error: %{message: "Missing Authorization header"}}))}
|
||||
{:halt,
|
||||
send_resp(conn, 401, Jason.encode!(%{error: %{message: "Missing Authorization header"}}))}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -73,8 +74,14 @@ defmodule N8nOpenaiAdapter.Router do
|
||||
|
||||
headers =
|
||||
case System.get_env("CHAT_WEBHOOK_BASIC") do
|
||||
nil -> [{"content-type", "application/json"}]
|
||||
basic -> [{"content-type", "application/json"}, {"authorization", "Basic " <> Base.encode64(basic)}]
|
||||
nil ->
|
||||
[{"content-type", "application/json"}]
|
||||
|
||||
basic ->
|
||||
[
|
||||
{"content-type", "application/json"},
|
||||
{"authorization", "Basic " <> Base.encode64(basic)}
|
||||
]
|
||||
end
|
||||
|
||||
case Req.post(webhook, json: body, headers: headers, receive_timeout: 120_000) do
|
||||
@@ -138,15 +145,20 @@ defmodule N8nOpenaiAdapter.Router do
|
||||
|
||||
true ->
|
||||
case last_user_text(messages) do
|
||||
nil -> error(conn, 400, "No user message in request")
|
||||
nil ->
|
||||
error(conn, 400, "No user message in request")
|
||||
|
||||
chat_input ->
|
||||
case AgentRegistry.webhook_for(model) do
|
||||
:error -> error(conn, 400, "Unknown model: #{model}")
|
||||
:error ->
|
||||
error(conn, 400, "Unknown model: #{model}")
|
||||
|
||||
webhook ->
|
||||
case call_n8n(webhook, session_id, chat_input) do
|
||||
{:ok, output} ->
|
||||
resp = %{
|
||||
id: "chatcmpl-#{Base.encode16(:crypto.strong_rand_bytes(12), case: :lower)}",
|
||||
id:
|
||||
"chatcmpl-#{Base.encode16(:crypto.strong_rand_bytes(12), case: :lower)}",
|
||||
object: "chat.completion",
|
||||
created: System.system_time(:second),
|
||||
model: model,
|
||||
|
||||
+14
-4
@@ -30,8 +30,13 @@ defmodule N8nOpenaiAdapter.RouterTest do
|
||||
|
||||
test "POST /v1/chat/completions with unknown model returns 400" do
|
||||
conn =
|
||||
conn(:post, "/v1/chat/completions",
|
||||
Jason.encode!(%{"model" => "nope", "messages" => [%{"role" => "user", "content" => "hi"}]})
|
||||
conn(
|
||||
:post,
|
||||
"/v1/chat/completions",
|
||||
Jason.encode!(%{
|
||||
"model" => "nope",
|
||||
"messages" => [%{"role" => "user", "content" => "hi"}]
|
||||
})
|
||||
)
|
||||
|> put_req_header("authorization", "Bearer test-key")
|
||||
|> put_req_header("content-type", "application/json")
|
||||
@@ -43,8 +48,13 @@ defmodule N8nOpenaiAdapter.RouterTest do
|
||||
|
||||
test "POST /v1/chat/completions without a user message returns 400" do
|
||||
conn =
|
||||
conn(:post, "/v1/chat/completions",
|
||||
Jason.encode!(%{"model" => "scholar-agent", "messages" => [%{"role" => "assistant", "content" => "hi"}]})
|
||||
conn(
|
||||
:post,
|
||||
"/v1/chat/completions",
|
||||
Jason.encode!(%{
|
||||
"model" => "scholar-agent",
|
||||
"messages" => [%{"role" => "assistant", "content" => "hi"}]
|
||||
})
|
||||
)
|
||||
|> put_req_header("authorization", "Bearer test-key")
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
||||
Reference in New Issue
Block a user