Files

File handling and session persistence in PraisonAI Rust SDK

File handling enables agents to persist sessions and search file stores.

Diagram

File Operations

🤖 Agent

💾 Session Store

📁 File System

🔍 File Search

Quick Start

  1. Persist Agent Sessions
    rust
    use praisonai::{Agent, FileSessionStore};
    
    // Create session store (uses default ~/.praisonai/sessions/)
    let store = FileSessionStore::new();
    
    // Create agent with session persistence
    let agent = Agent::new()
        .name("Assistant")
        .instructions("You are a helpful assistant")
        .session_store(store)
        .build()?;
    
    // Sessions automatically persist across restarts
    agent.start("Remember my name is Alice").await?;
  2. Custom Session Directory
    rust
    use praisonai::FileSessionStore;
    
    // Use custom directory for sessions
    let store = FileSessionStore::with_dir("/path/to/sessions")
        .max_messages(100);  // Limit stored messages

User Interaction Flow

Diagram
FileSystemFileSessionStoreAgentUserFileSystemFileSessionStoreAgentUserLater session…“Remember my preference”save(session_id, data)Write JSON fileOK“What’s my preference?”load(session_id)Read JSON filePrevious context“Your preference is…”

FileSessionStore

Persists session data to JSON files on disk.

rust
pub struct FileSessionStore {
    session_dir: PathBuf,
    max_messages: usize,
}

Configuration Options

OptionTypeDefaultDescription
session_dirPathBuf~/.praisonai/sessions/Directory for session files
max_messagesusize100Maximum messages per session

Methods

MethodSignatureDescription
new()fn new() -> SelfCreate with default directory
with_dir(dir)`fn with_dir(impl Into

) -> Self | Create with custom directory | | max_messages(n) | fn max_messages(self, usize) -> Self` | Set message limit |

Example

rust
use praisonai::FileSessionStore;

// Default location
let store = FileSessionStore::new();

// Custom location with message limit
let store = FileSessionStore::with_dir("./my_sessions")
    .max_messages(50);

FileSearchCall

Search across file stores (used with Gemini deep research).

rust
pub struct FileSearchCall {
    pub store_names: Vec<String>,
}

Configuration Options

OptionTypeDefaultDescription
store_names`Vec

| []` | Names of stores to search |

Example

rust
use praisonai::parity::FileSearchCall;

// Search specific stores
let search = FileSearchCall::new(vec![
    "documents".to_string(),
    "knowledge_base".to_string(),
]);

println!("Searching {} stores", search.store_names.len());

Best Practices

Use default paths for simplicity

FileSessionStore::new() uses ~/.praisonai/sessions/ which works across platforms.

Set message limits for long-running agents

Use .max_messages(n) to prevent unbounded memory growth in persistent agents.

Handle file I/O errors gracefully

Session store operations return Result types – always handle potential errors.


Related