mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
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
//! Session management for conversation persistence
//!
//! Supports multiple storage backends through a trait abstraction

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::fs;
use tokio::sync::RwLock;

use mofa_kernel::agent::error::{AgentError, AgentResult};

// ============================================================================
// Session 数据类型
// ============================================================================

/// Session message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionMessage {
    pub role: String,
    pub content: String,
    #[serde(default = "Utc::now")]
    pub timestamp: DateTime<Utc>,
}

impl SessionMessage {
    /// Create a new session message
    pub fn new(role: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            role: role.into(),
            content: content.into(),
            timestamp: Utc::now(),
        }
    }
}

/// Conversation session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub key: String,
    pub messages: Vec<SessionMessage>,
    #[serde(default = "Utc::now")]
    pub created_at: DateTime<Utc>,
    #[serde(default = "Utc::now")]
    pub updated_at: DateTime<Utc>,
    #[serde(default)]
    pub metadata: HashMap<String, Value>,
}

impl Session {
    /// Create a new session
    pub fn new(key: impl Into<String>) -> Self {
        let key = key.into();
        Self {
            key,
            messages: Vec::new(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            metadata: HashMap::new(),
        }
    }

    /// Add a message to the session
    pub fn add_message(&mut self, role: impl Into<String>, content: impl Into<String>) {
        let msg = SessionMessage::new(role, content);
        self.messages.push(msg);
        self.updated_at = Utc::now();
    }

    /// Get message history (limited to recent messages)
    pub fn get_history(&self, max_messages: usize) -> Vec<SessionMessage> {
        if self.messages.len() > max_messages {
            self.messages[self.messages.len() - max_messages..].to_vec()
        } else {
            self.messages.clone()
        }
    }

    /// Clear all messages
    pub fn clear(&mut self) {
        self.messages.clear();
        self.updated_at = Utc::now();
    }

    /// Get the number of messages
    pub fn len(&self) -> usize {
        self.messages.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }
}

// ============================================================================
// SessionStorage trait - 多种后端支持
// ============================================================================

/// Session storage backend trait
///
/// This trait provides an abstraction for session storage that can be
/// implemented by different backends (file-based, in-memory, database, etc.).
///
/// Note: A kernel-level storage abstraction exists in mofa_kernel::storage,
/// but this trait is foundation-specific as it works with the concrete Session type.
#[async_trait]
pub trait SessionStorage: Send + Sync {
    /// Load a session by key
    async fn load(&self, key: &str) -> AgentResult<Option<Session>>;

    /// Save a session
    async fn save(&self, session: &Session) -> AgentResult<()>;

    /// Delete a session
    async fn delete(&self, key: &str) -> AgentResult<bool>;

    /// List all session keys
    async fn list(&self) -> AgentResult<Vec<String>>;
}

// ============================================================================
// JSONL 文件存储实现
// ============================================================================

/// JSONL file-based session storage
pub struct JsonlSessionStorage {
    sessions_dir: PathBuf,
}

impl JsonlSessionStorage {
    /// Create a new JSONL session storage
    pub async fn new(workspace: impl AsRef<Path>) -> AgentResult<Self> {
        let sessions_dir = workspace.as_ref().join("sessions");
        fs::create_dir_all(&sessions_dir).await.map_err(|e| {
            AgentError::IoError(format!("Failed to create sessions directory: {}", e))
        })?;

        Ok(Self { sessions_dir })
    }

    /// Get the file path for a session
    fn session_file(&self, key: &str) -> PathBuf {
        let safe_key = key.replace(
            |c: char| !c.is_alphanumeric() && c != '-' && c != ':' && c != '_',
            "_",
        );
        self.sessions_dir.join(format!("{}.jsonl", safe_key))
    }

    /// Load a session from file
    async fn load_session(&self, key: &str) -> AgentResult<Option<Session>> {
        let session_file = self.session_file(key);
        if !session_file.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(&session_file)
            .await
            .map_err(|e| AgentError::IoError(format!("Failed to read session file: {}", e)))?;

        let mut lines = content.lines();
        let header = lines
            .next()
            .ok_or_else(|| AgentError::SerializationError("Empty session file".to_string()))?;

        // Parse header for metadata
        let header_data: Value = serde_json::from_str(header).map_err(|e| {
            AgentError::SerializationError(format!("Failed to parse session header: {}", e))
        })?;

        let key = header_data
            .get("key")
            .and_then(|v| v.as_str())
            .unwrap_or(key)
            .to_string();

        let created_at = header_data
            .get("created_at")
            .and_then(|v| v.as_str())
            .and_then(|s| DateTime::parse_from_rfc3339(s).ok())
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(Utc::now);

        let metadata = header_data
            .get("metadata")
            .and_then(|v| serde_json::from_value::<HashMap<String, Value>>(v.clone()).ok())
            .unwrap_or_default();

        let mut messages = Vec::new();
        for line in lines {
            if let Ok(msg) = serde_json::from_str::<SessionMessage>(line) {
                messages.push(msg);
            }
        }

        Ok(Some(Session {
            key,
            messages,
            created_at,
            updated_at: Utc::now(),
            metadata,
        }))
    }
}

#[async_trait]
impl SessionStorage for JsonlSessionStorage {
    async fn load(&self, key: &str) -> AgentResult<Option<Session>> {
        self.load_session(key).await
    }

    async fn save(&self, session: &Session) -> AgentResult<()> {
        let session_file = self.session_file(&session.key);

        // Ensure directory exists
        if let Some(parent) = session_file.parent() {
            fs::create_dir_all(parent).await.map_err(|e| {
                AgentError::IoError(format!("Failed to create sessions directory: {}", e))
            })?;
        }

        let mut lines = vec![
            serde_json::to_string(&serde_json::json!({
                "key": session.key,
                "created_at": session.created_at.to_rfc3339(),
                "updated_at": session.updated_at.to_rfc3339(),
                "metadata": session.metadata,
            }))
            .map_err(|e| {
                AgentError::SerializationError(format!("Failed to serialize session: {}", e))
            })?,
        ];

        for msg in &session.messages {
            lines.push(serde_json::to_string(msg).map_err(|e| {
                AgentError::SerializationError(format!("Failed to serialize message: {}", e))
            })?);
        }

        fs::write(&session_file, lines.join("\n"))
            .await
            .map_err(|e| AgentError::IoError(format!("Failed to write session file: {}", e)))?;

        Ok(())
    }

    async fn delete(&self, key: &str) -> AgentResult<bool> {
        let session_file = self.session_file(key);
        if session_file.exists() {
            fs::remove_file(&session_file).await.map_err(|e| {
                AgentError::IoError(format!("Failed to remove session file: {}", e))
            })?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn list(&self) -> AgentResult<Vec<String>> {
        let mut entries = fs::read_dir(&self.sessions_dir).await.map_err(|e| {
            AgentError::IoError(format!("Failed to read sessions directory: {}", e))
        })?;

        let mut keys = Vec::new();
        while let Some(entry) = entries
            .next_entry()
            .await
            .map_err(|e| AgentError::IoError(format!("Failed to read entry: {}", e)))?
        {
            if let Some(name) = entry.path().file_stem().and_then(|s| s.to_str()) {
                // Convert back the safe filename to original key
                let key = name.replace('_', ":");
                keys.push(key);
            }
        }

        Ok(keys)
    }
}

// ============================================================================
// 内存存储实现(用于测试)
// ============================================================================

/// In-memory session storage (for testing)
pub struct MemorySessionStorage {
    sessions: RwLock<HashMap<String, Session>>,
}

impl MemorySessionStorage {
    pub fn new() -> Self {
        Self {
            sessions: RwLock::new(HashMap::new()),
        }
    }
}

impl Default for MemorySessionStorage {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl SessionStorage for MemorySessionStorage {
    async fn load(&self, key: &str) -> AgentResult<Option<Session>> {
        let sessions = self.sessions.read().await;
        Ok(sessions.get(key).cloned())
    }

    async fn save(&self, session: &Session) -> AgentResult<()> {
        let mut sessions = self.sessions.write().await;
        sessions.insert(session.key.clone(), session.clone());
        Ok(())
    }

    async fn delete(&self, key: &str) -> AgentResult<bool> {
        let mut sessions = self.sessions.write().await;
        Ok(sessions.remove(key).is_some())
    }

    async fn list(&self) -> AgentResult<Vec<String>> {
        let sessions = self.sessions.read().await;
        Ok(sessions.keys().cloned().collect())
    }
}

// ============================================================================
// SessionManager - 统一会话管理接口
// ============================================================================

/// Session manager with pluggable storage backend
pub struct SessionManager {
    storage: Box<dyn SessionStorage>,
    cache: RwLock<HashMap<String, Session>>,
}

impl SessionManager {
    /// Create with JSONL file storage
    pub async fn with_jsonl(workspace: impl AsRef<Path>) -> AgentResult<Self> {
        let storage = JsonlSessionStorage::new(workspace).await?;
        Ok(Self {
            storage: Box::new(storage),
            cache: RwLock::new(HashMap::new()),
        })
    }

    /// Create with custom storage backend
    pub fn with_storage(storage: Box<dyn SessionStorage>) -> Self {
        Self {
            storage,
            cache: RwLock::new(HashMap::new()),
        }
    }

    /// Get or create a session
    pub async fn get_or_create(&self, key: &str) -> Session {
        // Try cache first
        {
            let cache = self.cache.read().await;
            if let Some(session) = cache.get(key) {
                return session.clone();
            }
        }

        // Try storage
        if let Ok(Some(session)) = self.storage.load(key).await {
            let mut cache = self.cache.write().await;
            cache.insert(key.to_string(), session.clone());
            return session;
        }

        // Create new session
        let session = Session::new(key);
        let mut cache = self.cache.write().await;
        cache.insert(key.to_string(), session.clone());
        session
    }

    /// Save a session
    pub async fn save(&self, session: &Session) -> AgentResult<()> {
        self.storage.save(session).await?;
        let mut cache = self.cache.write().await;
        cache.insert(session.key.clone(), session.clone());
        Ok(())
    }

    /// Delete a session
    pub async fn delete(&self, key: &str) -> AgentResult<bool> {
        let result = self.storage.delete(key).await?;
        let mut cache = self.cache.write().await;
        cache.remove(key);
        Ok(result)
    }

    /// List all session keys
    pub async fn list(&self) -> AgentResult<Vec<String>> {
        self.storage.list().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_session_creation() {
        let session = Session::new("test:key");
        assert_eq!(session.key, "test:key");
        assert!(session.is_empty());
    }

    #[tokio::test]
    async fn test_session_add_message() {
        let mut session = Session::new("test:key");
        session.add_message("user", "Hello");
        session.add_message("assistant", "Hi there!");

        assert_eq!(session.len(), 2);
        let history = session.get_history(10);
        assert_eq!(history.len(), 2);
        assert_eq!(history[0].role, "user");
    }

    #[tokio::test]
    async fn test_memory_storage() {
        let storage = MemorySessionStorage::new();
        let session = Session::new("test:memory");

        storage.save(&session).await.unwrap();
        let loaded = storage.load("test:memory").await.unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().key, "test:memory");
    }

    #[tokio::test]
    async fn test_jsonl_storage() {
        let temp_dir = TempDir::new().unwrap();
        let storage = JsonlSessionStorage::new(temp_dir.path()).await.unwrap();

        let mut session = Session::new("test:jsonl");
        session.add_message("user", "Hello");

        storage.save(&session).await.unwrap();

        let loaded = storage.load("test:jsonl").await.unwrap();
        assert!(loaded.is_some());
        let loaded_session = loaded.unwrap();
        assert_eq!(loaded_session.key, "test:jsonl");
        assert_eq!(loaded_session.len(), 1);
    }

    #[tokio::test]
    async fn test_session_manager() {
        let temp_dir = TempDir::new().unwrap();
        let manager = SessionManager::with_jsonl(temp_dir.path()).await.unwrap();

        let session = manager.get_or_create("test:manager").await;
        assert_eq!(session.key, "test:manager");

        manager.save(&session).await.unwrap();

        // Reload
        let loaded = manager.get_or_create("test:manager").await;
        assert_eq!(loaded.key, "test:manager");
    }
}