Files
bDS2/lib/bds/ai/chat_message.ex
2026-05-01 17:49:50 +02:00

44 lines
1.1 KiB
Elixir

defmodule BDS.AI.ChatMessage do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
@foreign_key_type :string
@roles [:system, :user, :assistant, :tool]
schema "chat_messages" do
belongs_to :conversation, BDS.AI.ChatConversation, references: :id, type: :string
field :role, Ecto.Enum, values: @roles
field :content, :string
field :tool_call_id, :string
field :tool_calls, :string
field :token_usage_input, :integer
field :token_usage_output, :integer
field :cache_read_tokens, :integer
field :cache_write_tokens, :integer
field :created_at, :integer
end
def changeset(message, attrs) do
message
|> cast(
attrs,
[
:conversation_id,
:role,
:content,
:tool_call_id,
:tool_calls,
:token_usage_input,
:token_usage_output,
:cache_read_tokens,
:cache_write_tokens,
:created_at
], empty_values: [nil])
|> validate_required([:conversation_id, :role, :created_at])
|> assoc_constraint(:conversation)
end
end