cozy cozy sql
Some checks failed
CI/CD Pipeline / build (push) Failing after 4s

This commit is contained in:
2026-03-13 13:00:15 -06:00
parent b5504dbdca
commit 04103dbfbd
7 changed files with 543 additions and 129 deletions

View File

@@ -20,7 +20,7 @@ defmodule ElixirAi.ConversationManager do
def init(_) do def init(_) do
Logger.info("ConversationManager initializing...") Logger.info("ConversationManager initializing...")
send(self(), :load_conversations) send(self(), :load_conversations)
{:ok, :loading_conversations} {:ok, %{conversations: :loading, subscriptions: MapSet.new()}}
end end
def create_conversation(name, ai_provider_id) do def create_conversation(name, ai_provider_id) do
@@ -39,58 +39,77 @@ defmodule ElixirAi.ConversationManager do
GenServer.call(@name, {:get_messages, name}) GenServer.call(@name, {:get_messages, name})
end end
def handle_call(message, from, :loading_conversations) do def handle_call(message, from, %{conversations: :loading} = state) do
Logger.warning( Logger.warning(
"Received call #{inspect(message)} from #{inspect(from)} while loading conversations. Retrying after delay." "Received call #{inspect(message)} from #{inspect(from)} while loading conversations. Retrying after delay."
) )
Process.send_after(self(), {:retry_call, message, from}, 100) Process.send_after(self(), {:retry_call, message, from}, 100)
{:noreply, :loading_conversations} {:noreply, state}
end end
def handle_call({:create, name, ai_provider_id}, _from, conversations) do def handle_call(
{:create, name, ai_provider_id},
_from,
%{conversations: conversations, subscriptions: subscriptions} = state
) do
if Map.has_key?(conversations, name) do if Map.has_key?(conversations, name) do
{:reply, {:error, :already_exists}, conversations} {:reply, {:error, :already_exists}, state}
else else
case Conversation.create(name, ai_provider_id) do case Conversation.create(name, ai_provider_id) do
:ok -> :ok ->
case start_and_subscribe(name) do case start_and_subscribe(name, subscriptions) do
{:ok, _pid} = ok -> {:reply, ok, Map.put(conversations, name, [])} {:ok, pid, new_subscriptions} ->
error -> {:reply, error, conversations} {:reply, {:ok, pid},
%{
state
| conversations: Map.put(conversations, name, []),
subscriptions: new_subscriptions
}}
{:error, _reason} = error ->
{:reply, error, state}
end end
{:error, _} = error -> {:error, _} = error ->
{:reply, error, conversations} {:reply, error, state}
end end
end end
end end
def handle_call({:open, name}, _from, conversations) do def handle_call(
{:open, name},
_from,
%{conversations: conversations, subscriptions: subscriptions} = state
) do
if Map.has_key?(conversations, name) do if Map.has_key?(conversations, name) do
case start_and_subscribe(name) do case start_and_subscribe(name, subscriptions) do
{:ok, _pid} = ok -> {:reply, ok, conversations} {:ok, pid, new_subscriptions} ->
error -> {:reply, error, conversations} {:reply, {:ok, pid}, %{state | subscriptions: new_subscriptions}}
{:error, _reason} = error ->
{:reply, error, state}
end end
else else
{:reply, {:error, :not_found}, conversations} {:reply, {:error, :not_found}, state}
end end
end end
def handle_call(:list, _from, conversations) do def handle_call(:list, _from, %{conversations: conversations} = state) do
keys = Map.keys(conversations) keys = Map.keys(conversations)
Logger.debug( Logger.debug(
"list_conversations returning: #{inspect(keys, limit: :infinity, printable_limit: :infinity, binaries: :as_binaries)}" "list_conversations returning: #{inspect(keys, limit: :infinity, printable_limit: :infinity, binaries: :as_binaries)}"
) )
{:reply, keys, conversations} {:reply, keys, state}
end end
def handle_call({:get_messages, name}, _from, conversations) do def handle_call({:get_messages, name}, _from, %{conversations: conversations} = state) do
{:reply, Map.get(conversations, name, []), conversations} {:reply, Map.get(conversations, name, []), state}
end end
def handle_info({:store_message, name, message}, conversations) do def handle_info({:store_message, name, message}, %{conversations: conversations} = state) do
case Conversation.find_id(name) do case Conversation.find_id(name) do
{:ok, conv_id} -> {:ok, conv_id} ->
Message.insert(conv_id, message, topic: ElixirAi.ChatRunner.message_topic(name)) Message.insert(conv_id, message, topic: ElixirAi.ChatRunner.message_topic(name))
@@ -99,17 +118,17 @@ defmodule ElixirAi.ConversationManager do
:ok :ok
end end
{:noreply, Map.update(conversations, name, [message], &(&1 ++ [message]))} {:noreply,
%{state | conversations: Map.update(conversations, name, [message], &(&1 ++ [message]))}}
end end
def handle_info(:load_conversations, _conversations) do def handle_info(:load_conversations, state) do
conversation_list = Conversation.all_names() conversation_list = Conversation.all_names()
Logger.info("Loaded #{length(conversation_list)} conversations from DB") Logger.info("Loaded #{length(conversation_list)} conversations from DB")
conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end) conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end)
Logger.info("Conversation map keys: #{inspect(Map.keys(conversations))}") Logger.info("Conversation map keys: #{inspect(Map.keys(conversations))}")
# {:ok, conversations} {:noreply, %{state | conversations: conversations}}
{:noreply, conversations}
end end
def handle_info({:retry_call, message, from}, state) do def handle_info({:retry_call, message, from}, state) do
@@ -123,7 +142,7 @@ defmodule ElixirAi.ConversationManager do
end end
end end
defp start_and_subscribe(name) do defp start_and_subscribe(name, subscriptions) do
result = result =
case Horde.DynamicSupervisor.start_child( case Horde.DynamicSupervisor.start_child(
ElixirAi.ChatRunnerSupervisor, ElixirAi.ChatRunnerSupervisor,
@@ -135,12 +154,19 @@ defmodule ElixirAi.ConversationManager do
end end
case result do case result do
{:ok, _pid} -> {:ok, pid} ->
new_subscriptions =
if MapSet.member?(subscriptions, name) do
subscriptions
else
Phoenix.PubSub.subscribe(ElixirAi.PubSub, ElixirAi.ChatRunner.message_topic(name)) Phoenix.PubSub.subscribe(ElixirAi.PubSub, ElixirAi.ChatRunner.message_topic(name))
result MapSet.put(subscriptions, name)
end
_ -> {:ok, pid, new_subscriptions}
result
error ->
error
end end
end end
end end

View File

@@ -28,44 +28,44 @@ defmodule ElixirAi.AiProvider do
sql = "SELECT id, name, model_name FROM ai_providers" sql = "SELECT id, name, model_name FROM ai_providers"
params = %{} params = %{}
case DbHelpers.run_sql(sql, params, "ai_providers") do case DbHelpers.run_sql(sql, params, "ai_providers", AiProviderSchema.partial_schema()) do
{:error, :db_error} -> {:error, _} ->
[] []
result -> rows ->
results = rows
Enum.map(result.rows, fn [id, name, model_name] -> |> Enum.map(fn row ->
attrs = %{id: id, name: name, model_name: model_name} |> convert_id_to_string() row |> convert_uuid_to_string() |> then(&struct(AiProviderSchema, &1))
case Zoi.parse(AiProviderSchema.partial_schema(), attrs) do
{:ok, valid} ->
struct(AiProviderSchema, valid)
{:error, errors} ->
Logger.error("Invalid provider data from DB: #{inspect(errors)}")
raise ArgumentError, "Invalid provider data: #{inspect(errors)}"
end
end) end)
|> tap(&Logger.debug("AiProvider.all() returning: #{inspect(&1)}"))
Logger.debug("AiProvider.all() returning: #{inspect(results)}")
results
end end
end end
# Convert binary UUID to string for frontend defp convert_uuid_to_string(%{id: id} = provider) when is_binary(id) do
defp convert_id_to_string(%{id: id} = provider) when is_binary(id) do
%{provider | id: Ecto.UUID.cast!(id)} %{provider | id: Ecto.UUID.cast!(id)}
end end
defp convert_id_to_string(provider), do: provider defp convert_uuid_to_string(provider), do: provider
def create(attrs) do def create(attrs) do
now = DateTime.truncate(DateTime.utc_now(), :second) now = DateTime.truncate(DateTime.utc_now(), :second)
sql = """ sql = """
INSERT INTO ai_providers (name, model_name, api_token, completions_url, inserted_at, updated_at) INSERT INTO ai_providers (
VALUES ($(name), $(model_name), $(api_token), $(completions_url), $(inserted_at), $(updated_at)) name,
model_name,
api_token,
completions_url,
inserted_at,
updated_at
) VALUES (
$(name),
$(model_name),
$(api_token),
$(completions_url),
$(inserted_at),
$(updated_at)
)
""" """
params = %{ params = %{
@@ -102,32 +102,10 @@ defmodule ElixirAi.AiProvider do
params = %{"name" => name} params = %{"name" => name}
case DbHelpers.run_sql(sql, params, "ai_providers") do case DbHelpers.run_sql(sql, params, "ai_providers", AiProviderSchema.schema()) do
{:error, :db_error} -> {:error, _} -> {:error, :db_error}
{:error, :db_error} [] -> {:error, :not_found}
[row | _] -> {:ok, row |> convert_uuid_to_string() |> then(&struct(AiProviderSchema, &1))}
%{rows: []} ->
{:error, :not_found}
%{rows: [[id, name, model_name, api_token, completions_url] | _]} ->
attrs =
%{
id: id,
name: name,
model_name: model_name,
api_token: api_token,
completions_url: completions_url
}
|> convert_id_to_string()
case Zoi.parse(AiProviderSchema.schema(), attrs) do
{:ok, valid} ->
{:ok, struct(AiProviderSchema, valid)}
{:error, errors} ->
Logger.error("Invalid provider data from DB: #{inspect(errors)}")
{:error, :invalid_data}
end
end end
end end
@@ -139,9 +117,9 @@ defmodule ElixirAi.AiProvider do
{:error, :db_error} -> {:error, :db_error} ->
{:error, :db_error} {:error, :db_error}
result -> rows ->
case result.rows do case rows do
[[0]] -> [%{"count" => 0}] ->
attrs = %{ attrs = %{
name: "default", name: "default",
model_name: Application.fetch_env!(:elixir_ai, :ai_model), model_name: Application.fetch_env!(:elixir_ai, :ai_model),

View File

@@ -34,40 +34,26 @@ defmodule ElixirAi.Conversation do
def all_names do def all_names do
sql = """ sql = """
SELECT c.name, p.name, p.model_name, p.api_token, p.completions_url SELECT c.name,
json_build_object(
'name', p.name,
'model_name', p.model_name,
'api_token', p.api_token,
'completions_url', p.completions_url
) as provider
FROM conversations c FROM conversations c
LEFT JOIN ai_providers p ON c.ai_provider_id = p.id LEFT JOIN ai_providers p ON c.ai_provider_id = p.id
""" """
params = %{} params = %{}
case DbHelpers.run_sql(sql, params, "conversations") do case DbHelpers.run_sql(sql, params, "conversations", ConversationInfo.schema()) do
{:error, :db_error} -> {:error, _} ->
[] []
result -> rows ->
Enum.map(result.rows, fn [name, provider_name, model_name, api_token, completions_url] -> Enum.map(rows, fn row ->
attrs = %{ struct(ConversationInfo, Map.put(row, :provider, struct(Provider, row.provider)))
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))
)
{:error, errors} ->
Logger.error("Invalid conversation data: #{inspect(errors)}")
raise ArgumentError, "Invalid conversation data: #{inspect(errors)}"
end
end) end)
end end
end end
@@ -76,8 +62,17 @@ defmodule ElixirAi.Conversation do
case Ecto.UUID.dump(ai_provider_id) do case Ecto.UUID.dump(ai_provider_id) do
{:ok, binary_id} -> {:ok, binary_id} ->
sql = """ sql = """
INSERT INTO conversations (name, ai_provider_id, inserted_at, updated_at) INSERT INTO conversations (
VALUES ($(name), $(ai_provider_id), $(inserted_at), $(updated_at)) name,
ai_provider_id,
inserted_at,
updated_at)
VALUES (
$(name),
$(ai_provider_id),
$(inserted_at),
$(updated_at)
)
""" """
timestamp = now() timestamp = now()
@@ -110,11 +105,11 @@ defmodule ElixirAi.Conversation do
{:error, :db_error} -> {:error, :db_error} ->
{:error, :db_error} {:error, :db_error}
%{rows: []} -> [] ->
{:error, :not_found} {:error, :not_found}
%{rows: [[id] | _]} -> [row | _] ->
{:ok, id} {:ok, row["id"]}
end end
end end

View File

@@ -2,11 +2,21 @@ defmodule ElixirAi.Data.DbHelpers do
require Logger require Logger
@get_named_param ~r/\$\((\w+)\)/ @get_named_param ~r/\$\((\w+)\)/
def run_sql(sql, params, topic, schema) do
run_sql(sql, params, topic) |> validate_rows(schema, topic)
end
def run_sql(sql, params, topic) do def run_sql(sql, params, topic) do
{sql, params} = named_params_to_positional_params(sql, params) {sql, params} = named_params_to_positional_params(sql, params)
try do try do
Ecto.Adapters.SQL.query!(ElixirAi.Repo, sql, params) 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.zip(result.columns, row)
|> Enum.into(%{})
end)
rescue rescue
exception -> exception ->
Logger.error("Database error: #{Exception.message(exception)}") Logger.error("Database error: #{Exception.message(exception)}")
@@ -21,6 +31,31 @@ defmodule ElixirAi.Data.DbHelpers do
end end
end end
defp validate_rows({:error, :db_error}, _schema, _topic), do: {:error, :db_error}
defp validate_rows(rows, schema, topic) do
rows
|> Enum.reduce_while({:ok, []}, fn row, {:ok, acc} ->
case Zoi.parse(schema, row, coerce: true) do
{:ok, valid} ->
{:cont, {:ok, [valid | acc]}}
{:error, errors} ->
Logger.error("Schema validation error: #{inspect(errors)}")
{:halt, {:error, :validation_error}}
end
end)
|> then(fn
{:ok, valid_rows} ->
Enum.reverse(valid_rows)
error ->
Logger.error("Validation error: #{inspect(error)}")
Phoenix.PubSub.broadcast(ElixirAi.PubSub, topic, {:sql_result_validation_error, error})
error
end)
end
def named_params_to_positional_params(query, params) do def named_params_to_positional_params(query, params) do
param_occurrences = Regex.scan(@get_named_param, query) param_occurrences = Regex.scan(@get_named_param, query)

View File

@@ -31,17 +31,9 @@ defmodule ElixirAi.Message do
{:error, :db_error} -> {:error, :db_error} ->
[] []
result -> rows ->
Enum.map(result.rows, fn row -> Enum.map(rows, fn row ->
raw = %{ decoded = decode_message(row)
role: Enum.at(row, 0),
content: Enum.at(row, 1),
reasoning_content: Enum.at(row, 2),
tool_calls: Enum.at(row, 3),
tool_call_id: Enum.at(row, 4)
}
decoded = decode_message(raw)
case Zoi.parse(MessageSchema.schema(), decoded) do case Zoi.parse(MessageSchema.schema(), decoded) do
{:ok, _valid} -> {:ok, _valid} ->
@@ -69,9 +61,22 @@ defmodule ElixirAi.Message do
when is_binary(conversation_id) and byte_size(conversation_id) == 16 do when is_binary(conversation_id) and byte_size(conversation_id) == 16 do
sql = """ sql = """
INSERT INTO messages ( INSERT INTO messages (
conversation_id, role, content, reasoning_content, conversation_id,
tool_calls, tool_call_id, inserted_at role,
) VALUES ($(conversation_id), $(role), $(content), $(reasoning_content), $(tool_calls), $(tool_call_id), $(inserted_at)) content,
reasoning_content,
tool_calls,
tool_call_id,
inserted_at
) VALUES (
$(conversation_id),
$(role),
$(content),
$(reasoning_content),
$(tool_calls),
$(tool_call_id),
$(inserted_at)
)
""" """
params = %{ params = %{
@@ -114,6 +119,7 @@ defmodule ElixirAi.Message do
defp decode_message(row) do defp decode_message(row) do
row row
|> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)
|> Map.update!(:role, &String.to_existing_atom/1) |> Map.update!(:role, &String.to_existing_atom/1)
|> Map.update(:tool_calls, nil, fn |> Map.update(:tool_calls, nil, fn
nil -> nil ->

View File

@@ -0,0 +1,217 @@
defmodule ElixirAi.AiUtils.StreamLineUtilsTest do
use ExUnit.Case
import ElixirAi.StreamChunkHelpers
alias ElixirAi.AiUtils.StreamLineUtils
setup do
test_pid = self()
{:ok, server: test_pid}
end
describe "Basic handling" do
test "handles empty string", %{server: server} do
assert :ok = StreamLineUtils.handle_stream_line(server, "")
refute_received _
end
test "handles [DONE] marker", %{server: server} do
assert :ok = StreamLineUtils.handle_stream_line(server, "data: [DONE]")
refute_received _
end
test "handles first streamed response with assistant role", %{server: server} do
line = start_response("chatcmpl-test")
StreamLineUtils.handle_stream_line(server, line)
assert_received {:start_new_ai_response, "chatcmpl-test"}
end
test "handles error response", %{server: server} do
line = error_chunk("API rate limit exceeded", "rate_limit_error")
assert :ok = StreamLineUtils.handle_stream_line(server, line)
refute_received _
end
test "handles error response from JSON", %{server: server} do
json_string = ~s({"error":{"message":"Invalid request","type":"invalid_request_error"}})
assert :ok = StreamLineUtils.handle_stream_line(server, json_string)
refute_received _
end
test "handles unmatched message structure", %{server: server} do
line = ~s(data: {"choices":[],"id":"test"})
assert :ok = StreamLineUtils.handle_stream_line(server, line)
refute_received _
end
test "handles invalid JSON gracefully", %{server: server} do
line = "data: {invalid json}"
assert :ok = StreamLineUtils.handle_stream_line(server, line)
refute_received _
end
end
describe "Reasoning content" do
test "handles single reasoning content chunk", %{server: server} do
line = reasoning_chunk("The")
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_reasoning_chunk, "chatcmpl-test", "The"}
end
test "handles multiple reasoning content chunks", %{server: server} do
lines = [
reasoning_chunk("The"),
reasoning_chunk(" user"),
reasoning_chunk(" asks")
]
for line <- lines do
StreamLineUtils.handle_stream_line(server, line)
end
assert_received {:ai_reasoning_chunk, "chatcmpl-test", "The"}
assert_received {:ai_reasoning_chunk, "chatcmpl-test", " user"}
assert_received {:ai_reasoning_chunk, "chatcmpl-test", " asks"}
end
end
describe "Text response content" do
test "handles single text content chunk", %{server: server} do
line = text_chunk("Hello")
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_text_chunk, "chatcmpl-test", "Hello"}
end
test "handles multiple text content chunks", %{server: server} do
lines = [
text_chunk("I"),
text_chunk("'m"),
text_chunk(" happy")
]
for line <- lines do
StreamLineUtils.handle_stream_line(server, line)
end
assert_received {:ai_text_chunk, "chatcmpl-test", "I"}
assert_received {:ai_text_chunk, "chatcmpl-test", "'m"}
assert_received {:ai_text_chunk, "chatcmpl-test", " happy"}
end
test "handles finish_reason stop", %{server: server} do
line = stop_chunk()
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_text_stream_finish, "chatcmpl-test"}
end
end
describe "Tool calling" do
test "handles tool call start", %{server: server} do
line = tool_call_start_chunk("get_weather", ~s({"location), 0, "call_123")
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_tool_call_start, "chatcmpl-test",
{"get_weather", ~s({"location), 0, "call_123"}}
end
test "handles tool call middle", %{server: server} do
line = tool_call_middle_chunk(~s(": "San Francisco"))
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_tool_call_middle, "chatcmpl-test", {~s(": "San Francisco"), 0}}
end
test "handles tool call end", %{server: server} do
line = tool_call_end_chunk()
StreamLineUtils.handle_stream_line(server, line)
assert_received {:ai_tool_call_end, "chatcmpl-test"}
end
test "handles complete tool call flow", %{server: server} do
# Start tool call
start_line = tool_call_start_chunk("get_weather", ~s({"location), 0, "call_123")
StreamLineUtils.handle_stream_line(server, start_line)
assert_received {:ai_tool_call_start, "chatcmpl-test",
{"get_weather", ~s({"location), 0, "call_123"}}
# Middle of tool call
middle_line = tool_call_middle_chunk(~s(": "NYC"}))
StreamLineUtils.handle_stream_line(server, middle_line)
assert_received {:ai_tool_call_middle, "chatcmpl-test", {~s(": "NYC"}), 0}}
# End tool call
end_line = tool_call_end_chunk()
StreamLineUtils.handle_stream_line(server, end_line)
assert_received {:ai_tool_call_end, "chatcmpl-test"}
end
end
describe "Integration tests" do
test "handles complete conversation flow", %{server: server} do
# Start
StreamLineUtils.handle_stream_line(server, start_response())
assert_received {:start_new_ai_response, "chatcmpl-test"}
# Reasoning chunks
StreamLineUtils.handle_stream_line(server, reasoning_chunk("Think"))
assert_received {:ai_reasoning_chunk, "chatcmpl-test", "Think"}
StreamLineUtils.handle_stream_line(server, reasoning_chunk("ing..."))
assert_received {:ai_reasoning_chunk, "chatcmpl-test", "ing..."}
# Content chunks
StreamLineUtils.handle_stream_line(server, text_chunk("Hello"))
assert_received {:ai_text_chunk, "chatcmpl-test", "Hello"}
StreamLineUtils.handle_stream_line(server, text_chunk(" world"))
assert_received {:ai_text_chunk, "chatcmpl-test", " world"}
# End
StreamLineUtils.handle_stream_line(server, stop_chunk())
assert_received {:ai_text_stream_finish, "chatcmpl-test"}
# Done marker
StreamLineUtils.handle_stream_line(server, "data: [DONE]")
refute_received _
end
test "handles conversation with tool call", %{server: server} do
# Start
StreamLineUtils.handle_stream_line(server, start_response())
assert_received {:start_new_ai_response, "chatcmpl-test"}
# Reasoning
StreamLineUtils.handle_stream_line(server, reasoning_chunk("Need to check weather"))
assert_received {:ai_reasoning_chunk, "chatcmpl-test", "Need to check weather"}
# Tool call
StreamLineUtils.handle_stream_line(
server,
tool_call_start_chunk("get_weather", ~s({"loc), 0, "call_1")
)
assert_received {:ai_tool_call_start, "chatcmpl-test",
{"get_weather", ~s({"loc), 0, "call_1"}}
StreamLineUtils.handle_stream_line(server, tool_call_middle_chunk(~s(ation"})))
assert_received {:ai_tool_call_middle, "chatcmpl-test", {~s(ation"}), 0}}
StreamLineUtils.handle_stream_line(server, tool_call_end_chunk())
assert_received {:ai_tool_call_end, "chatcmpl-test"}
# Done
StreamLineUtils.handle_stream_line(server, "data: [DONE]")
refute_received _
end
end
end

View File

@@ -0,0 +1,157 @@
defmodule ElixirAi.StreamChunkHelpers do
@moduledoc """
Helper functions for creating SSE-formatted AI streaming response chunks for testing.
"""
@doc """
Creates a response start chunk with assistant role.
"""
def start_response(id \\ "chatcmpl-test") do
chunk = %{
"choices" => [
%{
"finish_reason" => nil,
"index" => 0,
"delta" => %{"role" => "assistant", "content" => nil}
}
],
"created" => 1_773_426_536,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a reasoning content chunk.
"""
def reasoning_chunk(content, id \\ "chatcmpl-test") do
chunk = %{
"choices" => [
%{"finish_reason" => nil, "index" => 0, "delta" => %{"reasoning_content" => content}}
],
"created" => 1_773_426_536,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a text content chunk.
"""
def text_chunk(content, id \\ "chatcmpl-test") do
chunk = %{
"choices" => [%{"finish_reason" => nil, "index" => 0, "delta" => %{"content" => content}}],
"created" => 1_773_426_537,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a stop/finish chunk.
"""
def stop_chunk(id \\ "chatcmpl-test") do
chunk = %{
"choices" => [%{"finish_reason" => "stop", "index" => 0, "delta" => %{}}],
"created" => 1_773_426_537,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a tool call start chunk.
"""
def tool_call_start_chunk(
tool_name,
arguments,
index \\ 0,
tool_call_id,
id \\ "chatcmpl-test"
) do
chunk = %{
"choices" => [
%{
"finish_reason" => nil,
"index" => 0,
"delta" => %{
"tool_calls" => [
%{
"id" => tool_call_id,
"index" => index,
"type" => "function",
"function" => %{"name" => tool_name, "arguments" => arguments}
}
]
}
}
],
"created" => 1_773_426_537,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a tool call middle chunk (continuing arguments).
"""
def tool_call_middle_chunk(arguments, index \\ 0, id \\ "chatcmpl-test") do
chunk = %{
"choices" => [
%{
"finish_reason" => nil,
"index" => 0,
"delta" => %{
"tool_calls" => [
%{"index" => index, "function" => %{"arguments" => arguments}}
]
}
}
],
"created" => 1_773_426_537,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates a tool call end chunk.
"""
def tool_call_end_chunk(id \\ "chatcmpl-test") do
chunk = %{
"choices" => [%{"finish_reason" => "tool_calls", "index" => 0, "delta" => %{}}],
"created" => 1_773_426_537,
"id" => id,
"model" => "gpt-oss-120b-F16.gguf",
"object" => "chat.completion.chunk"
}
"data: #{Jason.encode!(chunk)}"
end
@doc """
Creates an error chunk.
"""
def error_chunk(message, type \\ "invalid_request_error") do
chunk = %{"error" => %{"message" => message, "type" => type}}
"data: #{Jason.encode!(chunk)}"
end
end