# AI-Session API Guide
## Overview
This guide provides comprehensive documentation for the `ai-session` crate API, including usage examples, best practices, and advanced features. The ai-session library revolutionizes terminal session management with AI-optimized features that provide:
- **93% API cost reduction** through intelligent context compression
- **Native PTY implementation** for cross-platform compatibility
- **Multi-agent coordination** via message bus architecture
- **Advanced observability** for decision tracking and performance analysis
- **Security and isolation** with capability-based access control
## Table of Contents
1. [Quick Start Examples](#quick-start-examples)
2. [Core API](#core-api)
3. [Context Management](#context-management)
4. [Multi-Agent Coordination](#multi-agent-coordination)
5. [Output Processing](#output-processing)
6. [Security & Access Control](#security--access-control)
7. [Observability](#observability)
8. [Persistence](#persistence)
9. [Integration](#integration)
10. [HTTP Server API](#http-server-api)
11. [Performance Optimizations](#performance-optimizations)
12. [Real-World Examples](#real-world-examples)
## Quick Start Examples
### Simple Terminal Session
```rust
use ai_session::{SessionManager, SessionConfig};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let manager = SessionManager::new();
let session = manager.create_session().await?;
session.start().await?;
session.send_input("echo 'Hello AI Session!'\n").await?;
// Wait a moment for command execution
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
let output = session.read_output().await?;
println!("Output: {}", String::from_utf8_lossy(&output));
session.stop().await?;
Ok(())
}
```
### AI-Enhanced Development Session
```rust
use ai_session::{SessionManager, SessionConfig, ContextConfig};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let manager = SessionManager::new();
// Configure for AI features
let mut config = SessionConfig::default();
config.enable_ai_features = true;
config.agent_role = Some("rust-developer".to_string());
config.context_config = ContextConfig {
max_tokens: 8192,
compression_threshold: 0.8,
};
let session = manager.create_session_with_config(config).await?;
session.start().await?;
// Development workflow
let commands = vec![
"cargo new my-project --bin",
"cd my-project",
"cargo add tokio --features full",
"cargo check",
];
for cmd in commands {
session.send_input(&format!("{}\n", cmd)).await?;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let output = session.read_output().await?;
println!("Command: {}", cmd);
println!("Output: {}\n", String::from_utf8_lossy(&output));
}
session.stop().await?;
Ok(())
}
```
## Core API
### SessionManager
The `SessionManager` is the main entry point for creating and managing sessions.
```rust
use ai_session::{SessionManager, SessionConfig};
// Create a new session manager
let manager = SessionManager::new();
// Create a basic session
let session = manager.create_session().await?;
// Create a session with custom configuration
let mut config = SessionConfig::default();
config.enable_ai_features = true;
config.context_config.max_tokens = 8192;
let session = manager.create_session_with_config(config).await?;
```
### AISession
The `AISession` represents an individual terminal session with AI capabilities.
```rust
// Start the session
session.start().await?;
// Send input to the session
session.send_input("echo 'Hello World'\n").await?;
// Read output from the session
let output = session.read_output().await?;
let output_str = String::from_utf8_lossy(&output);
// Get session status
let status = session.status().await;
// Set metadata
session.set_metadata("user_id".to_string(), serde_json::json!("123")).await?;
// Get metadata
let user_id = session.get_metadata("user_id").await;
// Stop the session
session.stop().await?;
```
### Session Configuration
```rust
use ai_session::{SessionConfig, ContextConfig};
use std::collections::HashMap;
let mut config = SessionConfig::default();
// Basic configuration
config.name = Some("my-session".to_string());
config.working_directory = "/path/to/workdir".into();
config.shell = Some("/bin/zsh".to_string());
// Environment variables
config.environment.insert("NODE_ENV".to_string(), "development".to_string());
// PTY configuration
config.pty_size = (24, 80); // rows, cols
config.output_buffer_size = 2 * 1024 * 1024; // 2MB
// AI features
config.enable_ai_features = true;
config.agent_role = Some("frontend-developer".to_string());
// Context configuration
config.context_config = ContextConfig {
max_tokens: 4096,
compression_threshold: 0.8,
};
// Output processing
config.compress_output = true;
config.parse_output = true;
```
## Context Management
### SessionContext
Manages AI conversation history and context for efficient token usage.
```rust
use ai_session::context::{SessionContext, Message, MessageRole};
// Get the session context
let context = session.get_ai_context().await?;
// Add a message to the context
let message = Message {
role: MessageRole::User,
content: "What files are in the current directory?".to_string(),
timestamp: chrono::Utc::now(),
token_count: 12, // Estimated token count
};
context.add_message(message).await?;
// Get messages within a token limit
let messages = context.get_messages_within_limit(2048);
// Get context summary
let summary = context.get_summary().await;
println!("Total messages: {}", summary.total_messages);
println!("Total tokens: {}", summary.total_tokens);
println!("Compression ratio: {:.2}", summary.compression_ratio);
```
### Message Roles
```rust
use ai_session::context::MessageRole;
// Different message types
MessageRole::User; // Human input
MessageRole::Assistant; // AI response
MessageRole::System; // System messages
MessageRole::Tool; // Tool/command output
```
## Multi-Agent Coordination
### MultiAgentSession
Coordinates multiple AI agents working together.
```rust
use ai_session::coordination::{MultiAgentSession, AgentId, MessageType, BroadcastMessage};
use std::sync::Arc;
let coordinator = Arc::new(MultiAgentSession::new());
// Register agents
let frontend_id = AgentId::new();
let backend_id = AgentId::new();
coordinator.register_agent(frontend_id.clone(), frontend_session)?;
coordinator.register_agent(backend_id.clone(), backend_session)?;
// Send direct message between agents
let message = Message {
from: frontend_id.clone(),
message_type: MessageType::DataShare,
payload: serde_json::json!({
"component_structure": {
"Header": "React component",
"Footer": "React component"
}
}),
timestamp: chrono::Utc::now(),
};
coordinator.send_message(frontend_id.clone(), backend_id.clone(), message).await?;
// Broadcast to all agents
let broadcast = BroadcastMessage {
id: uuid::Uuid::new_v4(),
from: frontend_id.clone(),
content: "Build completed successfully".to_string(),
priority: MessagePriority::Normal,
timestamp: chrono::Utc::now(),
};
coordinator.broadcast(frontend_id.clone(), broadcast).await?;
```
### Task Distribution
```rust
use ai_session::coordination::{TaskDistributor, Task, TaskId, TaskPriority};
let distributor = TaskDistributor::new();
// Register agent capabilities
distributor.register_capabilities(
frontend_id.clone(),
vec!["react".to_string(), "typescript".to_string(), "css".to_string()]
);
distributor.register_capabilities(
backend_id.clone(),
vec!["rust".to_string(), "database".to_string(), "api".to_string()]
);
// Submit a task
let task = Task {
id: TaskId::new(),
description: "Create user authentication API".to_string(),
requirements: vec!["rust".to_string(), "api".to_string()],
priority: TaskPriority::High,
estimated_effort: Duration::from_hours(4),
metadata: serde_json::json!({}),
};
distributor.submit_task(task).await?;
// Distribute tasks to appropriate agents
let assignments = distributor.distribute_tasks().await?;
```
## Output Processing
### OutputParser
Intelligently parses command output into structured formats.
```rust
use ai_session::output::{OutputParser, ParsedOutput, BuildStatus};
let parser = OutputParser::new();
// Parse build output
let build_output = "BUILD SUCCESSFUL\nartifacts: target/release/myapp";
let parsed = parser.parse(build_output)?;
match parsed {
ParsedOutput::BuildOutput { status, artifacts } => {
match status {
BuildStatus::Success => println!("Build succeeded!"),
BuildStatus::Failed(reason) => println!("Build failed: {}", reason),
}
println!("Artifacts: {:?}", artifacts);
},
_ => println!("Other output type"),
}
// Parse test output
let test_output = "running 5 tests\ntest test_one ... ok\ntest test_two ... FAILED";
let parsed = parser.parse(test_output)?;
match parsed {
ParsedOutput::TestResults { passed, failed, details } => {
println!("Tests: {} passed, {} failed", passed, failed);
},
_ => {},
}
```
### OutputManager
Processes and caches output for efficient retrieval.
```rust
use ai_session::output::OutputManager;
let mut manager = OutputManager::new();
// Process raw output
let processed = manager.process_output("echo 'hello world'\nhello world")?;
// The manager automatically categorizes and stores the output
println!("Output type: {:?}", processed.output_type);
println!("Patterns found: {:?}", processed.patterns);
```
## Persistence
### PersistenceManager
Handles session state storage and recovery.
```rust
use ai_session::persistence::{PersistenceManager, SessionState, SessionMetadata};
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let manager = PersistenceManager::new(temp_dir.path().to_path_buf());
// Create session state
let session_state = SessionState {
session_id: session.id.clone(),
config: session.config.clone(),
status: session.status().await,
context: session.get_ai_context().await?,
command_history: vec![],
metadata: SessionMetadata::default(),
};
// Save session
manager.save_session(&session.id, &session_state).await?;
// Load session
let loaded_state = manager.load_session(&session.id).await?;
// List all sessions
let session_ids = manager.list_sessions().await?;
// Delete session
manager.delete_session(&session.id).await?;
```
### Snapshots
```rust
use ai_session::persistence::SnapshotManager;
let snapshot_manager = SnapshotManager::new(temp_dir.path().to_path_buf());
// Create a snapshot
let snapshot_id = snapshot_manager.create_snapshot(
&session.id,
&session_state,
Some("Before major refactoring".to_string())
).await?;
// List snapshots
let snapshots = snapshot_manager.list_snapshots(&session.id).await?;
// Restore from snapshot
let restored_state = snapshot_manager.restore_snapshot(&session.id, &snapshot_id).await?;
```
## Integration
### tmux Compatibility
```rust
use ai_session::integration::{TmuxCompatLayer, MigrationHelper};
let tmux = TmuxCompatLayer::new();
// Create tmux session
let tmux_name = tmux.create_tmux_session(&session.id, &config).await?;
// Send commands
tmux.send_command(&tmux_name, "ls -la").await?;
// Capture output
let output = tmux.capture_output(&tmux_name, Some(100)).await?;
// Migration helper
let migration = MigrationHelper::new();
let result = migration.migrate_tmux_session(&tmux_name).await?;
```
### VS Code Integration
```rust
use ai_session::integration::{VSCodeIntegration, ExternalIntegration};
let mut vscode = VSCodeIntegration::new(3000); // Port 3000
vscode.initialize().await?;
// The integration will automatically notify VS Code of session events
vscode.on_session_created(&session.id).await?;
vscode.on_session_terminated(&session.id).await?;
// Export session data for VS Code
let data = vscode.export_session_data(&session.id).await?;
```
## Best Practices
### Error Handling
```rust
use ai_session::core::SessionError;
match session.send_input("invalid_command\n").await {
Ok(_) => println!("Command sent successfully"),
Err(e) => match e.downcast_ref::<SessionError>() {
Some(SessionError::NotFound(id)) => {
println!("Session {} not found", id);
},
Some(SessionError::PtyError(msg)) => {
println!("PTY error: {}", msg);
},
_ => println!("Other error: {}", e),
}
}
```
### Resource Management
```rust
// Always clean up sessions
{
let session = manager.create_session().await?;
session.start().await?;
// Use the session...
// Ensure cleanup happens
session.stop().await?;
}
// Or use RAII pattern with Drop
impl Drop for MySessionWrapper {
fn drop(&mut self) {
if let Some(session) = &self.session {
// Cleanup in background
tokio::spawn(async move {
let _ = session.stop().await;
});
}
}
}
```
### Performance Optimization
```rust
// Configure appropriate token limits
config.context_config.max_tokens = 4096; // Adjust based on needs
// Enable compression for large outputs
config.compress_output = true;
// Use session pooling for high-throughput scenarios
let pool = SessionPool::new(10); // Pool of 10 sessions
let session = pool.acquire().await?;
// ... use session ...
pool.release(session).await?;
```
## Advanced Usage
### Custom Output Parsers
```rust
use ai_session::output::{OutputParser, ParsedOutput};
impl OutputParser {
pub fn parse_custom(&self, output: &str) -> Result<ParsedOutput> {
// Custom parsing logic
if output.contains("CUSTOM_SUCCESS") {
Ok(ParsedOutput::Custom {
data: serde_json::json!({"status": "success"}),
})
} else {
self.parse(output) // Fall back to default parsing
}
}
}
```
## HTTP Server API
The ai-session HTTP server provides a REST API for external command execution and session management.
### Starting the Server
```bash
# Start the server on default port 3000
ai-session-server
# Custom port and host
ai-session-server --port 8080 --host 0.0.0.0
```
### API Endpoints
#### Session Management
**Create Session**
```http
POST /sessions
Content-Type: application/json
{
"name": "dev-session",
"enable_ai_features": true,
"working_directory": "/path/to/project",
"shell": "/bin/bash"
}
```
**List Sessions**
```http
GET /sessions
```
**Execute Command**
```http
POST /sessions/{name}/execute
Content-Type: application/json
{
"command": "cargo build --release",
"timeout_ms": 30000
}
```
**Get Session Output**
```http
GET /sessions/{name}/output
```
### Client Example
```rust
use reqwest::Client;
use serde_json::json;
async fn http_client_example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let base_url = "http://localhost:3000";
// Create session
let response = client
.post(&format!("{}/sessions", base_url))
.json(&json!({
"name": "test-session",
"enable_ai_features": true
}))
.send()
.await?;
println!("Session created: {}", response.status());
// Execute command
let result = client
.post(&format!("{}/sessions/test-session/execute", base_url))
.json(&json!({
"command": "echo 'Hello from HTTP API'"
}))
.send()
.await?
.json::<serde_json::Value>()
.await?;
println!("Command result: {}", result);
Ok(())
}
```
## Performance Optimizations
### Session Pooling
```rust
use ai_session::{SessionManager, SessionConfig};
use std::sync::Arc;
use tokio::sync::Semaphore;
pub struct SessionPool {
manager: SessionManager,
config: SessionConfig,
semaphore: Arc<Semaphore>,
}
impl SessionPool {
pub fn new(max_sessions: usize, config: SessionConfig) -> Self {
Self {
manager: SessionManager::new(),
config,
semaphore: Arc::new(Semaphore::new(max_sessions)),
}
}
pub async fn execute_with_session<F, R>(&self, task: F) -> anyhow::Result<R>
where
F: FnOnce(Arc<ai_session::AISession>) -> futures::future::BoxFuture<'static, anyhow::Result<R>>,
R: Send + 'static,
{
let _permit = self.semaphore.acquire().await?;
let session = self.manager.create_session_with_config(self.config.clone()).await?;
session.start().await?;
let result = task(session.clone()).await?;
session.stop().await?;
Ok(result)
}
}
```
### Context Compression
```rust
use ai_session::context::{SessionContext, CompressionStrategy};
async fn optimize_context() -> anyhow::Result<()> {
let mut context = SessionContext::new(session_id);
// Configure aggressive compression
context.config.compression_threshold = 0.6;
context.config.compression_strategy = CompressionStrategy::Aggressive;
// Auto-compress when approaching limit
if context.get_total_tokens() > (context.config.max_tokens as f64 * 0.8) as usize {
context.compress_context().await;
println!("Context compressed to {} tokens", context.get_total_tokens());
}
Ok(())
}
```
### Batch Operations
```rust
async fn batch_commands() -> anyhow::Result<()> {
let session = create_session().await?;
// Execute commands in batch for efficiency
let commands = vec![
"cargo fmt",
"cargo clippy",
"cargo test",
"cargo build --release"
];
let batch_command = commands.join(" && ");
session.send_input(&format!("{}\n", batch_command)).await?;
// Monitor progress
loop {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let output = session.read_output().await?;
let output_str = String::from_utf8_lossy(&output);
if output_str.contains("release [optimized]") {
println!("Batch build completed successfully");
break;
}
}
Ok(())
}
```
## Real-World Examples
### CI/CD Pipeline Integration
```rust
use ai_session::{SessionManager, SessionConfig};
async fn ci_pipeline() -> anyhow::Result<()> {
let manager = SessionManager::new();
let mut config = SessionConfig::default();
config.name = Some("ci-runner".to_string());
config.timeout = Some(std::time::Duration::from_secs(1800)); // 30 min timeout
let session = manager.create_session_with_config(config).await?;
session.start().await?;
let pipeline_steps = vec![
("Clone", "git clone https://github.com/user/repo.git ."),
("Install", "cargo build"),
("Test", "cargo test --all"),
("Lint", "cargo clippy -- -D warnings"),
("Format Check", "cargo fmt -- --check"),
("Security Audit", "cargo audit"),
];
for (step_name, command) in pipeline_steps {
println!("Running: {}", step_name);
session.send_input(&format!("{}\n", command)).await?;
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
let output = session.read_output().await?;
let output_str = String::from_utf8_lossy(&output);
if output_str.contains("error") || output_str.contains("failed") {
eprintln!("❌ {} failed: {}", step_name, output_str);
break;
} else {
println!("✅ {} completed", step_name);
}
}
session.stop().await?;
Ok(())
}
```
### Development Environment Setup
```rust
async fn setup_dev_environment() -> anyhow::Result<()> {
let manager = SessionManager::new();
let mut config = SessionConfig::default();
config.enable_ai_features = true;
config.agent_role = Some("setup-assistant".to_string());
let session = manager.create_session_with_config(config).await?;
session.start().await?;
let setup_commands = vec![
// Rust toolchain
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y",
"source ~/.cargo/env",
"rustup component add clippy rustfmt rust-analyzer",
// Project setup
"cargo new awesome-project --bin",
"cd awesome-project",
"cargo add tokio --features full",
"cargo add serde --features derive",
"cargo add anyhow",
// Initial build
"cargo check",
];
for cmd in setup_commands {
println!("Executing: {}", cmd);
session.send_input(&format!("{}\n", cmd)).await?;
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
let output = session.read_output().await?;
println!("Output: {}\n", String::from_utf8_lossy(&output));
}
session.stop().await?;
Ok(())
}
```
### Multi-Language Project Coordination
```rust
use ai_session::coordination::{MultiAgentSession, AgentId, Message, MessageType};
async fn polyglot_development() -> anyhow::Result<()> {
let coordinator = Arc::new(MultiAgentSession::new());
let manager = SessionManager::new();
// Rust backend agent
let rust_agent = {
let mut config = SessionConfig::default();
config.agent_role = Some("rust-backend".to_string());
config.working_directory = "/project/backend".into();
let session = manager.create_session_with_config(config).await?;
let agent_id = AgentId::new();
coordinator.register_agent(agent_id.clone(), session)?;
agent_id
};
// TypeScript frontend agent
let ts_agent = {
let mut config = SessionConfig::default();
config.agent_role = Some("typescript-frontend".to_string());
config.working_directory = "/project/frontend".into();
let session = manager.create_session_with_config(config).await?;
let agent_id = AgentId::new();
coordinator.register_agent(agent_id.clone(), session)?;
agent_id
};
// Start coordination
coordinator.start_all_agents().await?;
// Backend notifies frontend of API changes
let api_update = Message {
from: rust_agent.clone(),
message_type: MessageType::DataShare,
payload: serde_json::json!({
"api_version": "2.0",
"new_endpoints": ["/api/users", "/api/auth"],
"breaking_changes": []
}),
priority: ai_session::coordination::MessagePriority::High,
timestamp: chrono::Utc::now(),
};
coordinator.send_message(rust_agent, ts_agent, api_update).await?;
Ok(())
}
```
## Best Practices Summary
1. **Resource Management**: Always clean up sessions explicitly or use RAII patterns
2. **Performance**: Use session pooling for high-throughput scenarios
3. **Context Optimization**: Set appropriate compression thresholds and token limits
4. **Error Handling**: Handle specific error types for better user experience
5. **Monitoring**: Track token usage and performance metrics
6. **Security**: Use capability-based access control and rate limiting
7. **Integration**: Leverage the HTTP API for language-agnostic integration
8. **Testing**: Test with realistic data sizes and network conditions
This concludes the comprehensive API guide for the `ai-session` crate. For more examples and advanced usage patterns, refer to the examples directory and integration tests.