Overview
TheLoom.Session module is a GenServer that runs the agent loop for a coding assistant session. It handles:
- Message exchange between user and AI
- Tool execution with permission management
- Model configuration and switching
- Session persistence and resumption
- Two modes: normal and architect (planning + execution)
Session Lifecycle
Starting a Session
UseLoom.Session.Manager to start new sessions:
Unique identifier for the session. Auto-generated if not provided.
The LLM model to use, in format
provider:model-id. Supported providers: anthropic, openai, google.Root directory of the project. Defaults to current working directory.
Human-readable session title for display purposes.
List of tool modules available to the agent (e.g., FileRead, FileWrite).
If true, automatically approve all tool executions without prompting.
Finding an Existing Session
Returns the session process PID if found.
Returned when the session does not exist.
Listing Active Sessions
List of active session metadata maps containing:
id(String.t()): Session identifierpid(pid()): Process IDstatus(atom()): Current status (:idle,:thinking,:executing_tool)
Sending Messages
send_message/2
Send a user message and receive the assistant’s response.Either the session PID or session ID string.
The user message text to send.
The assistant’s text response.
Error details if the session is not found or processing failed.
This call blocks until the assistant completes its response, which may include multiple tool executions. For long-running operations, consider subscribing to session events via PubSub.
Retrieving History
get_history/1
Get the complete conversation history for a session.List of message maps with the following structure:
role(:user|:assistant|:tool|:system): Message sendercontent(String.t()): Message text contenttool_calls([map()], optional): Tool calls made by assistanttool_call_id(String.t(), optional): ID linking tool results to calls
Model Management
update_model/2
Change the LLM model for an active session.Session PID or ID.
New model identifier in format
provider:model-id.Model changes take effect immediately for the next message. Previous messages remain in history and can influence future responses regardless of model change.
Session Modes
set_mode/2
Switch between normal and architect mode.Session PID or ID.
:normal: Standard agent loop with configured model:architect: Two-phase execution with planning (strong model) and execution (fast model)
get_mode/1
Retrieve the current session mode.The active session mode.
Status and Events
get_status/1
Get the current execution status of a session.One of:
:idle: Waiting for user input:thinking: AI is generating a response:executing_tool: Running a tool
subscribe/1
Subscribe to real-time session events via Phoenix PubSub.Subscribe to session events to build reactive UIs or monitor long-running operations. All events are broadcast on the topic
"session:#{session_id}".Permission Management
respond_to_permission/3
Respond to a pending permission request for tool execution.Session ID.
Permission action:
"allow_once", "allow_always", or "deny".Additional metadata (currently unused).
Session Manager API
TheLoom.Session.Manager module provides lifecycle management functions.
start_session/1
Start a new session under the DynamicSupervisor.stop_session/1
Gracefully stop a running session.Session stopped successfully.
Session does not exist.