Skip to content

latent.chat.session

JSONL session persistence for the agent REPL.

Sessions are stored at .latent/sessions/<agent_name>/<session_id>.jsonl. Each line is a JSON object with: ts, type, content, metadata.

Classes

SessionInfo

SessionInfo(session_id: str, agent_name: str, created_at: str, message_count: int, last_user_message: str, path: Path)

Metadata for a saved session.

Functions

append_event

append_event(path: Path, event_type: str, content: str, metadata: dict | None = None) -> None

Append a single event line to a JSONL session file.

get_sessions_dir

get_sessions_dir(agent_name: str) -> Path

Return the sessions directory for an agent.

list_sessions

list_sessions(agent_name: str) -> list[SessionInfo]

List all sessions for an agent, sorted by creation time (newest first).

load_session_messages

load_session_messages(path: Path) -> list[Message]

Reconstruct message list from JSONL, respecting clear events.

Clear events cause all pre-clear messages to be dropped. Tool call/result lines are reconstructed as proper role messages.

make_session_filename

make_session_filename(agent_name: str, created_at: datetime, last_message: str, session_uuid: str) -> str

Build a session filename stem (no .jsonl extension).

new_session_id

new_session_id() -> str

Generate a new session ID.

slugify

slugify(text: str, fallback: str) -> str

Convert text to a filesystem-safe slug, max 30 chars.

Falls back to fallback if the result is empty (e.g. all non-ASCII).