Persist chat history

This commit is contained in:
Claudio Ortolina
2026-03-17 13:50:01 +00:00
parent bcef30eb52
commit 8fd8dde73d
12 changed files with 717 additions and 121 deletions
+31
View File
@@ -0,0 +1,31 @@
defmodule MusicLibrary.Chats.Chat do
use Ecto.Schema
import Ecto.Changeset
alias MusicLibrary.Chats.Message
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "chats" do
field :entity, Ecto.Enum, values: [:record, :artist]
field :musicbrainz_id, Ecto.UUID
field :topic, :string
field :message_count, :integer, virtual: true, default: 0
has_many :messages, Message, preload_order: [asc: :position]
timestamps(type: :utc_datetime)
end
@type t :: %__MODULE__{}
@spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
def changeset(chat, attrs) do
chat
|> cast(attrs, [:entity, :musicbrainz_id, :topic])
|> validate_required([:entity, :musicbrainz_id])
|> validate_length(:topic, max: 200)
end
end
+29
View File
@@ -0,0 +1,29 @@
defmodule MusicLibrary.Chats.Message do
use Ecto.Schema
import Ecto.Changeset
alias MusicLibrary.Chats.Chat
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "chat_messages" do
field :role, :string
field :content, :string
field :position, :integer
belongs_to :chat, Chat
timestamps(type: :utc_datetime)
end
@type t :: %__MODULE__{}
@spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
def changeset(message, attrs) do
message
|> cast(attrs, [:role, :content, :position])
|> validate_required([:role, :content, :position])
|> validate_inclusion(:role, ["user", "assistant"])
end
end