appam 0.1.1

High-throughput, traceable, reliable Rust agent framework for long-horizon AI sessions and easy extensibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! Durable session persistence backed by SQLite.
//!
//! [`SessionHistory`] stores completed or in-progress sessions so callers can
//! resume conversations, inspect prior runs, or build operational tooling on
//! top of Appam's session data. The API is runtime agnostic and works equally
//! well from CLIs, libraries, and services.

use std::path::Path;

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use sqlx::Row;
use tracing::{debug, info};

use crate::config::HistoryConfig;
use crate::llm::ChatMessage;

use super::Session;

/// Summary of a session for listing operations.
///
/// Provides essential metadata without loading full message history.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSummary {
    /// Unique session identifier
    pub id: String,
    /// Agent name
    pub agent_name: String,
    /// Model used
    pub model: String,
    /// Number of messages in session
    pub message_count: usize,
    /// Number of turns (user-assistant exchanges)
    pub turn_count: i32,
    /// Number of tool calls made
    pub tool_call_count: i32,
    /// Session creation time
    pub started_at: DateTime<Utc>,
    /// Last activity time
    pub updated_at: DateTime<Utc>,
    /// Session end time (if completed)
    pub ended_at: Option<DateTime<Utc>>,
}

/// SQLite-backed history manager for agent sessions.
///
/// When history is disabled in [`HistoryConfig`], the public API remains usable
/// but degrades to a no-op surface that returns empty results. This lets higher
/// level code keep a single control flow regardless of whether persistence is
/// enabled.
///
/// # Examples
///
/// ```no_run
/// use appam::agent::history::SessionHistory;
/// use appam::config::HistoryConfig;
///
/// # async fn example() -> anyhow::Result<()> {
/// let mut config = HistoryConfig::default();
/// config.enabled = true;
/// config.db_path = "data/sessions.db".into();
///
/// let history = SessionHistory::new(config).await?;
///
/// // List all sessions
/// let sessions = history.list_sessions().await?;
/// for summary in sessions {
///     println!("Session {}: {} messages", summary.id, summary.message_count);
/// }
/// # Ok(())
/// # }
/// ```
pub struct SessionHistory {
    store: Option<SessionStore>,
    config: HistoryConfig,
}

impl SessionHistory {
    /// Create a new session history manager.
    ///
    /// If history is disabled in config, the store is initialized as `None`
    /// and all operations become no-ops.
    ///
    /// # Errors
    ///
    /// Returns an error if history is enabled but the database cannot be
    /// initialized.
    pub async fn new(config: HistoryConfig) -> Result<Self> {
        let store = if config.enabled {
            Some(SessionStore::new(&config.db_path).await?)
        } else {
            None
        };

        Ok(Self { store, config })
    }

    /// Check if history is enabled.
    pub fn is_enabled(&self) -> bool {
        self.store.is_some()
    }

    /// Create a new session entry.
    ///
    /// If history is disabled, this is a no-op.
    ///
    /// # Errors
    ///
    /// Returns an error if the session cannot be written to the database.
    pub async fn create_session(&self, session: &Session) -> Result<()> {
        if let Some(store) = &self.store {
            store.create_session(session).await?;
            debug!(session_id = %session.session_id, "Session created in history");
        }
        Ok(())
    }

    /// Load a session by ID.
    ///
    /// Returns `None` if history is disabled or the session doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub async fn load_session(&self, session_id: &str) -> Result<Option<Session>> {
        if let Some(store) = &self.store {
            store.get_session(session_id).await
        } else {
            Ok(None)
        }
    }

    /// Save or update a session.
    ///
    /// If history is disabled, this is a no-op.
    ///
    /// # Errors
    ///
    /// Returns an error if the session cannot be written to the database.
    pub async fn save_session(&self, session: &Session) -> Result<()> {
        if let Some(store) = &self.store {
            store.upsert_session(session).await?;
            debug!(session_id = %session.session_id, "Session saved to history");
        }
        Ok(())
    }

    /// Append messages to an existing session.
    ///
    /// This is more efficient than loading, modifying, and saving the entire
    /// session when you only need to add messages.
    ///
    /// # Errors
    ///
    /// Returns an error if the session doesn't exist or cannot be updated.
    pub async fn append_messages(&self, session_id: &str, messages: &[ChatMessage]) -> Result<()> {
        if let Some(store) = &self.store {
            store.append_messages(session_id, messages).await?;
        }
        Ok(())
    }

    /// List all sessions.
    ///
    /// Returns session summaries ordered by most recent activity.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub async fn list_sessions(&self) -> Result<Vec<SessionSummary>> {
        if let Some(store) = &self.store {
            store.list_sessions().await
        } else {
            Ok(Vec::new())
        }
    }

    /// List sessions for a specific agent.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub async fn list_agent_sessions(&self, agent_name: &str) -> Result<Vec<SessionSummary>> {
        if let Some(store) = &self.store {
            store.list_agent_sessions(agent_name).await
        } else {
            Ok(Vec::new())
        }
    }

    /// Delete a session by ID.
    ///
    /// Returns `true` if a session was deleted, `false` if it didn't exist or
    /// history is disabled.
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    pub async fn delete_session(&self, session_id: &str) -> Result<bool> {
        if let Some(store) = &self.store {
            store.delete_session(session_id).await
        } else {
            Ok(false)
        }
    }

    /// Get the number of stored sessions.
    pub async fn session_count(&self) -> Result<usize> {
        if let Some(store) = &self.store {
            store.session_count().await
        } else {
            Ok(0)
        }
    }

    /// Clean up old sessions beyond the configured maximum.
    ///
    /// If `max_sessions` is configured, deletes oldest sessions to stay within
    /// the limit. Returns the number of sessions deleted.
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    pub async fn enforce_max_sessions(&self) -> Result<usize> {
        if let Some(store) = &self.store {
            if let Some(max) = self.config.max_sessions {
                return store.enforce_max_sessions(max).await;
            }
        }
        Ok(0)
    }
}

/// Internal SQLite session store.
///
/// This is similar to `src/web/session.rs` but enhanced for general use with
/// additional metadata fields.
struct SessionStore {
    pool: SqlitePool,
}

impl SessionStore {
    /// Initialize a new session store with database at the given path.
    async fn new<P: AsRef<Path>>(db_path: P) -> Result<Self> {
        let db_path = db_path.as_ref();

        // Create parent directory if needed
        if let Some(parent) = db_path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        info!(path = %db_path.display(), "Initializing session history database");

        // Connect to database
        let options = SqliteConnectOptions::new()
            .filename(db_path)
            .create_if_missing(true);

        let pool = SqlitePoolOptions::new()
            .max_connections(5)
            .connect_with(options)
            .await
            .context("Failed to connect to session history database")?;

        // Initialize schema
        Self::init_schema(&pool).await?;

        info!("Session history database initialized");

        Ok(Self { pool })
    }

    /// Initialize database schema with enhanced metadata.
    async fn init_schema(pool: &SqlitePool) -> Result<()> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS session_history (
                id TEXT PRIMARY KEY,
                agent_name TEXT NOT NULL,
                model TEXT NOT NULL,
                messages TEXT NOT NULL,
                metadata TEXT,
                started_at TEXT NOT NULL,
                updated_at TEXT NOT NULL,
                ended_at TEXT,
                turn_count INTEGER DEFAULT 0,
                tool_call_count INTEGER DEFAULT 0
            )
            "#,
        )
        .execute(pool)
        .await?;

        // Create indexes for common queries
        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_history_updated ON session_history(updated_at DESC)",
        )
        .execute(pool)
        .await?;

        sqlx::query("CREATE INDEX IF NOT EXISTS idx_history_agent ON session_history(agent_name)")
            .execute(pool)
            .await?;

        sqlx::query(
            "CREATE INDEX IF NOT EXISTS idx_history_started ON session_history(started_at DESC)",
        )
        .execute(pool)
        .await?;

        // Migration: Add usage column for token/cost tracking
        let _ = sqlx::query(
            r#"
            ALTER TABLE session_history
            ADD COLUMN usage TEXT
            "#,
        )
        .execute(pool)
        .await;
        // Ignore error if column already exists

        Ok(())
    }

    /// Create a new session in the database.
    async fn create_session(&self, session: &Session) -> Result<()> {
        let messages_json = serde_json::to_string(&session.messages)?;
        let usage_json = session
            .usage
            .as_ref()
            .and_then(|u| serde_json::to_string(u).ok());
        let (turn_count, tool_call_count) = Self::compute_counts(&session.messages);

        sqlx::query(
            r#"
            INSERT INTO session_history (
                id, agent_name, model, messages, metadata,
                started_at, updated_at, ended_at, turn_count, tool_call_count, usage
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            "#,
        )
        .bind(&session.session_id)
        .bind(&session.agent_name)
        .bind(&session.model)
        .bind(&messages_json)
        .bind(Option::<String>::None) // metadata placeholder
        .bind(session.started_at.map(|t| t.to_rfc3339()))
        .bind(
            session
                .ended_at
                .map(|t| t.to_rfc3339())
                .unwrap_or_else(|| Utc::now().to_rfc3339()),
        )
        .bind(session.ended_at.map(|t| t.to_rfc3339()))
        .bind(turn_count)
        .bind(tool_call_count)
        .bind(usage_json)
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Update an existing session or insert if it doesn't exist.
    async fn upsert_session(&self, session: &Session) -> Result<()> {
        let messages_json = serde_json::to_string(&session.messages)?;
        let usage_json = session
            .usage
            .as_ref()
            .and_then(|u| serde_json::to_string(u).ok());
        let (turn_count, tool_call_count) = Self::compute_counts(&session.messages);

        // Try update first
        let result = sqlx::query(
            r#"
            UPDATE session_history
            SET messages = ?, updated_at = ?, ended_at = ?, turn_count = ?, tool_call_count = ?, usage = ?
            WHERE id = ?
            "#,
        )
        .bind(&messages_json)
        .bind(
            session
                .ended_at
                .map(|t| t.to_rfc3339())
                .unwrap_or_else(|| Utc::now().to_rfc3339()),
        )
        .bind(session.ended_at.map(|t| t.to_rfc3339()))
        .bind(turn_count)
        .bind(tool_call_count)
        .bind(usage_json)
        .bind(&session.session_id)
        .execute(&self.pool)
        .await?;

        // If no rows affected, insert
        if result.rows_affected() == 0 {
            self.create_session(session).await?;
        }

        Ok(())
    }

    /// Get a session by ID.
    async fn get_session(&self, session_id: &str) -> Result<Option<Session>> {
        let row = sqlx::query(
            r#"
            SELECT id, agent_name, model, messages, started_at, updated_at, ended_at, usage
            FROM session_history
            WHERE id = ?
            "#,
        )
        .bind(session_id)
        .fetch_optional(&self.pool)
        .await?;

        match row {
            Some(row) => {
                let messages_json: String = row.try_get("messages")?;
                let messages: Vec<ChatMessage> = serde_json::from_str(&messages_json)?;

                let started_at_str: Option<String> = row.try_get("started_at")?;
                let ended_at_str: Option<String> = row.try_get("ended_at")?;

                // Deserialize usage if present
                let usage_json: Option<String> = row.try_get("usage").ok().flatten();
                let usage = usage_json.and_then(|json| serde_json::from_str(&json).ok());

                Ok(Some(Session {
                    session_id: row.try_get("id")?,
                    agent_name: row.try_get("agent_name")?,
                    model: row.try_get("model")?,
                    messages,
                    started_at: started_at_str
                        .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                        .map(|dt| dt.into()),
                    ended_at: ended_at_str
                        .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                        .map(|dt| dt.into()),
                    usage,
                }))
            }
            None => Ok(None),
        }
    }

    /// Append messages to an existing session.
    async fn append_messages(&self, session_id: &str, new_messages: &[ChatMessage]) -> Result<()> {
        // Load existing session
        let mut session = self
            .get_session(session_id)
            .await?
            .ok_or_else(|| anyhow::anyhow!("Session not found: {}", session_id))?;

        // Append new messages
        session.messages.extend_from_slice(new_messages);

        // Update in database
        self.upsert_session(&session).await
    }

    /// Delete a session by ID.
    async fn delete_session(&self, session_id: &str) -> Result<bool> {
        let result = sqlx::query("DELETE FROM session_history WHERE id = ?")
            .bind(session_id)
            .execute(&self.pool)
            .await?;

        Ok(result.rows_affected() > 0)
    }

    /// List all sessions.
    async fn list_sessions(&self) -> Result<Vec<SessionSummary>> {
        let rows = sqlx::query(
            r#"
            SELECT id, agent_name, model, messages, started_at, updated_at, ended_at,
                   turn_count, tool_call_count
            FROM session_history
            ORDER BY updated_at DESC
            "#,
        )
        .fetch_all(&self.pool)
        .await?;

        Self::rows_to_summaries(rows)
    }

    /// List sessions for a specific agent.
    async fn list_agent_sessions(&self, agent_name: &str) -> Result<Vec<SessionSummary>> {
        let rows = sqlx::query(
            r#"
            SELECT id, agent_name, model, messages, started_at, updated_at, ended_at,
                   turn_count, tool_call_count
            FROM session_history
            WHERE agent_name = ?
            ORDER BY updated_at DESC
            "#,
        )
        .bind(agent_name)
        .fetch_all(&self.pool)
        .await?;

        Self::rows_to_summaries(rows)
    }

    /// Get session count.
    async fn session_count(&self) -> Result<usize> {
        let row = sqlx::query("SELECT COUNT(*) as count FROM session_history")
            .fetch_one(&self.pool)
            .await?;

        let count: i64 = row.try_get("count")?;
        Ok(count as usize)
    }

    /// Enforce maximum session limit by deleting oldest sessions.
    async fn enforce_max_sessions(&self, max_sessions: usize) -> Result<usize> {
        // Get current count
        let count = self.session_count().await?;

        if count <= max_sessions {
            return Ok(0);
        }

        let to_delete = count - max_sessions;

        // Delete oldest sessions
        let result = sqlx::query(
            r#"
            DELETE FROM session_history
            WHERE id IN (
                SELECT id FROM session_history
                ORDER BY updated_at ASC
                LIMIT ?
            )
            "#,
        )
        .bind(to_delete as i64)
        .execute(&self.pool)
        .await?;

        let deleted = result.rows_affected() as usize;
        info!(
            deleted = deleted,
            max = max_sessions,
            "Enforced session limit"
        );

        Ok(deleted)
    }

    /// Convert database rows to session summaries.
    fn rows_to_summaries(rows: Vec<sqlx::sqlite::SqliteRow>) -> Result<Vec<SessionSummary>> {
        let mut summaries = Vec::new();

        for row in rows {
            let messages_json: String = row.try_get("messages")?;
            let messages: Vec<ChatMessage> = serde_json::from_str(&messages_json)?;

            let started_at_str: Option<String> = row.try_get("started_at")?;
            let updated_at_str: String = row.try_get("updated_at")?;
            let ended_at_str: Option<String> = row.try_get("ended_at")?;

            summaries.push(SessionSummary {
                id: row.try_get("id")?,
                agent_name: row.try_get("agent_name")?,
                model: row.try_get("model")?,
                message_count: messages.len(),
                turn_count: row.try_get("turn_count")?,
                tool_call_count: row.try_get("tool_call_count")?,
                started_at: started_at_str
                    .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                    .map(|dt| dt.into())
                    .unwrap_or_else(Utc::now),
                updated_at: DateTime::parse_from_rfc3339(&updated_at_str)?.into(),
                ended_at: ended_at_str
                    .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
                    .map(|dt| dt.into()),
            });
        }

        Ok(summaries)
    }

    /// Compute turn and tool call counts from messages.
    fn compute_counts(messages: &[ChatMessage]) -> (i32, i32) {
        let mut turn_count = 0;
        let mut tool_call_count = 0;

        for msg in messages {
            if msg.role == crate::llm::Role::Assistant && msg.content.is_some() {
                turn_count += 1;
            }
            if let Some(tool_calls) = &msg.tool_calls {
                tool_call_count += tool_calls.len() as i32;
            }
        }

        (turn_count, tool_call_count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::Role;
    use tempfile::NamedTempFile;

    #[tokio::test]
    async fn test_history_disabled() {
        let config = HistoryConfig::default(); // disabled by default
        let history = SessionHistory::new(config).await.unwrap();

        assert!(!history.is_enabled());
        assert_eq!(history.session_count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_history_enabled() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = HistoryConfig {
            enabled: true,
            db_path: temp_file.path().to_owned(),
            ..HistoryConfig::default()
        };

        let history = SessionHistory::new(config).await.unwrap();
        assert!(history.is_enabled());
    }

    #[tokio::test]
    async fn test_create_and_load_session() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = HistoryConfig {
            enabled: true,
            db_path: temp_file.path().to_owned(),
            ..HistoryConfig::default()
        };

        let history = SessionHistory::new(config).await.unwrap();

        let session = Session {
            session_id: "test-123".to_string(),
            agent_name: "test-agent".to_string(),
            model: "gpt-5".to_string(),
            messages: vec![ChatMessage {
                role: Role::User,
                name: None,
                tool_call_id: None,
                content: Some("Hello".to_string()),
                tool_calls: None,
                reasoning: None,
                raw_content_blocks: None,
                tool_metadata: None,
                timestamp: Some(Utc::now()),
                id: None,
                provider_response_id: None,
                status: None,
            }],
            started_at: Some(Utc::now()),
            ended_at: None,
            usage: None,
        };

        history.create_session(&session).await.unwrap();

        let loaded = history.load_session("test-123").await.unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().session_id, "test-123");
    }

    #[tokio::test]
    async fn test_list_sessions() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = HistoryConfig {
            enabled: true,
            db_path: temp_file.path().to_owned(),
            ..HistoryConfig::default()
        };

        let history = SessionHistory::new(config).await.unwrap();

        // Create multiple sessions
        for i in 0..3 {
            let session = Session {
                session_id: format!("test-{}", i),
                agent_name: "test-agent".to_string(),
                model: "gpt-5".to_string(),
                messages: vec![],
                started_at: Some(Utc::now()),
                ended_at: None,
                usage: None,
            };
            history.create_session(&session).await.unwrap();
        }

        let sessions = history.list_sessions().await.unwrap();
        assert_eq!(sessions.len(), 3);
    }
}