Skip to main content

codetether_agent/session/
mod.rs

1//! # Session Management
2//!
3//! A [`Session`] tracks the conversation history and execution state for a
4//! single agent interaction. It is the primary abstraction used by the CLI,
5//! TUI, HTTP server, and A2A worker to drive the agentic loop: send a user
6//! message, let the model call tools until it converges, and return the
7//! final answer.
8//!
9//! ## Quick Start
10//!
11//! ```rust,no_run
12//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
13//! use codetether_agent::session::Session;
14//!
15//! let mut session = Session::new().await.unwrap();
16//! let result = session.prompt("List the files in the current directory").await.unwrap();
17//! println!("Assistant: {}", result.text);
18//! println!("Session ID: {}", result.session_id);
19//! # });
20//! ```
21//!
22//! ## Architecture
23//!
24//! Sessions are stored as JSON files in the platform data directory
25//! (`~/.local/share/codetether/sessions` on Linux). Each session has a UUID,
26//! an ordered list of [`Message`](crate::provider::Message)s, a record of
27//! all [`ToolUse`](crate::agent::ToolUse) events, aggregated token
28//! [`Usage`](crate::provider::Usage), and a [`SessionMetadata`] block.
29//!
30//! ## Module Layout
31//!
32//! The [`Session`] type is implemented across several single-responsibility
33//! submodules and exposed here as a single facade:
34//!
35//! - [`types`] — [`Session`], [`SessionMetadata`], [`ImageAttachment`].
36//! - [`events`] — [`SessionResult`], [`SessionEvent`].
37//! - [`lifecycle`] — constructor, agent/provenance, message append.
38//! - [`persistence`] — save / load / delete / directory lookup.
39//! - [`title`] — title generation and context-change hook.
40//! - [`prompt_api`] — public [`prompt`](Session::prompt) entry points.
41//! - [`helper`] — the agentic loop implementation (non-public details).
42
43mod bus;
44mod event_compaction;
45mod event_rlm;
46mod event_token;
47mod events;
48mod lifecycle;
49mod persistence;
50mod prompt_api;
51mod title;
52mod types;
53
54pub mod codex_import;
55pub mod helper;
56mod listing;
57mod listing_all;
58
59pub use self::bus::{DurableSink, NoopSink, SessionBus};
60pub use self::codex_import::{
61    import_codex_session_by_id, import_codex_sessions_for_directory, load_or_import_session,
62};
63pub use self::event_compaction::{
64    CompactionFailure, CompactionOutcome, CompactionStart, ContextTruncation, FallbackStrategy,
65};
66pub use self::event_rlm::{RlmCompletion, RlmOutcome, RlmProgressEvent, RlmSubcallFallback};
67pub use self::event_token::{TokenDelta, TokenEstimate, TokenSource};
68pub use self::events::{SessionEvent, SessionResult};
69pub use self::listing::{SessionSummary, list_sessions};
70pub use self::listing_all::list_all_sessions_for_directory;
71pub use self::types::{DEFAULT_MAX_STEPS, ImageAttachment, Session, SessionMetadata};
72
73#[cfg(test)]
74mod tests;