Rag.Agent.Session (rag v0.3.4)
View SourceSession management for agent conversations.
A session maintains the conversation history, context, and state for an agent interaction. It handles:
- Message history (user, assistant, tool results)
- Context storage (persistent data across turns)
- Token estimation for context window management
Usage
session = Session.new()
|> Session.add_message(:user, "Hello!")
|> Session.add_message(:assistant, "Hi there!")
|> Session.set_context(:repo, "my_app")
# Get messages for LLM
messages = Session.to_llm_messages(session)
Summary
Functions
Adds a message to the session history.
Adds a tool result to the session history.
Clears all messages but keeps context and metadata.
Returns the session context.
Gets a context value.
Returns the last n messages.
Merges values into the context.
Returns the number of messages in the session.
Returns all messages in the session.
Creates a new session.
Sets a context value.
Formats messages for LLM API consumption.
Estimates the token count for the session.
Types
@type role() :: :user | :assistant | :system | :tool
Functions
Adds a message to the session history.
Parameters
session- The session structrole- Message role (:user, :assistant, :system)content- Message content
Examples
iex> session |> Session.add_message(:user, "Hello!")
%Session{messages: [%{role: :user, content: "Hello!", ...}]}
Adds a tool result to the session history.
Parameters
session- The session structtool_name- Name of the tool that was calledresult- Tool result as {:ok, content} or {:error, reason}
Clears all messages but keeps context and metadata.
Returns the session context.
Gets a context value.
Examples
iex> Session.get_context(session, :key)
"value"
iex> Session.get_context(session, :missing, "default")
"default"
@spec last_messages(t(), non_neg_integer()) :: [message()]
Returns the last n messages.
Examples
iex> Session.last_messages(session, 5)
[%{role: :user, ...}, %{role: :assistant, ...}]
Merges values into the context.
Examples
iex> session |> Session.merge_context(%{a: 1, b: 2})
%Session{context: %{a: 1, b: 2}}
@spec message_count(t()) :: non_neg_integer()
Returns the number of messages in the session.
Returns all messages in the session.
Creates a new session.
Options
:id- Custom session ID (generates UUID if not provided):metadata- Initial metadata map
Examples
iex> Session.new()
%Session{id: "...", messages: [], context: %{}}
iex> Session.new(id: "my-session", metadata: %{user: "alice"})
%Session{id: "my-session", metadata: %{user: "alice"}}
Sets a context value.
Examples
iex> session |> Session.set_context(:key, "value")
%Session{context: %{key: "value"}}
Formats messages for LLM API consumption.
Returns a list of simplified message maps suitable for sending to LLM providers.
Examples
iex> Session.to_llm_messages(session)
[%{role: :user, content: "Hello"}, %{role: :assistant, content: "Hi!"}]
@spec token_estimate(t()) :: non_neg_integer()
Estimates the token count for the session.
Uses a rough approximation based on character count.