An ordered message log. See spec §5.6 and §14.
Layer A — pure serializable data. A thread is the canonical container for the message history threaded through a session or chat loop; it is append-only at the API surface (helpers build new threads rather than mutating an existing one).
Summary
Functions
Append an assistant-role message with the given text content.
Append a single message to the end of the thread.
Append multiple messages to the end of the thread, preserving order.
Append a system-role message with the given text content.
Append a user-role message with the given text content.
Build a thread from an existing list of messages.
Return the last message in the thread, or nil when the thread is empty.
Return the ordered list of messages in the thread.
Build a %Thread{} from keyword opts.
Types
@type t() :: %ALLM.Thread{messages: [ALLM.Message.t()], metadata: map()}
Functions
Append an assistant-role message with the given text content.
Examples
iex> ALLM.Thread.new() |> ALLM.Thread.add_assistant("hello") |> ALLM.Thread.last_message()
%ALLM.Message{role: :assistant, content: "hello", name: nil, tool_call_id: nil, metadata: %{}}
@spec add_message(t(), ALLM.Message.t()) :: t()
Append a single message to the end of the thread.
Examples
iex> t = ALLM.Thread.new() |> ALLM.Thread.add_message(%ALLM.Message{role: :user, content: "hi"})
iex> length(t.messages)
1
@spec add_messages(t(), [ALLM.Message.t()]) :: t()
Append multiple messages to the end of the thread, preserving order.
Examples
iex> t = ALLM.Thread.new() |> ALLM.Thread.add_messages([
...> %ALLM.Message{role: :user, content: "a"},
...> %ALLM.Message{role: :assistant, content: "b"}
...> ])
iex> Enum.map(t.messages, & &1.content)
["a", "b"]
Append a system-role message with the given text content.
Examples
iex> ALLM.Thread.new() |> ALLM.Thread.add_system("be nice") |> ALLM.Thread.last_message()
%ALLM.Message{role: :system, content: "be nice", name: nil, tool_call_id: nil, metadata: %{}}
Append a user-role message with the given text content.
Examples
iex> ALLM.Thread.new() |> ALLM.Thread.add_user("hi") |> ALLM.Thread.last_message()
%ALLM.Message{role: :user, content: "hi", name: nil, tool_call_id: nil, metadata: %{}}
@spec from_messages([ALLM.Message.t()]) :: t()
Build a thread from an existing list of messages.
Examples
iex> t = ALLM.Thread.from_messages([%ALLM.Message{role: :user, content: "hi"}])
iex> length(t.messages)
1
@spec last_message(t()) :: ALLM.Message.t() | nil
Return the last message in the thread, or nil when the thread is empty.
Examples
iex> ALLM.Thread.last_message(ALLM.Thread.new())
nil
iex> ALLM.Thread.new()
...> |> ALLM.Thread.add_user("first")
...> |> ALLM.Thread.add_user("last")
...> |> ALLM.Thread.last_message()
...> |> Map.get(:content)
"last"
@spec messages(t()) :: [ALLM.Message.t()]
Return the ordered list of messages in the thread.
Examples
iex> ALLM.Thread.messages(ALLM.Thread.new())
[]
Build a %Thread{} from keyword opts.
Examples
iex> ALLM.Thread.new()
%ALLM.Thread{messages: [], metadata: %{}}
iex> ALLM.Thread.new(metadata: %{trace: "x"}).metadata
%{trace: "x"}