ai-session 0.5.0

AI-optimized terminal session management library
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
//! AI-Session HTTP Server - provides REST API for external command execution via curl

use ai_session::{SessionConfig, SessionManager};
use anyhow::Result;
use axum::{
    Router,
    extract::{Path, State},
    http::StatusCode,
    response::Json,
    routing::{delete, get, post},
};
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_http::cors::CorsLayer;

/// Command line arguments
#[derive(Parser)]
#[command(name = "ai-session-server")]
#[command(about = "AI-Session HTTP server for external command execution")]
struct Args {
    /// Port to listen on
    #[arg(short, long, default_value = "3000")]
    port: u16,

    /// Host to bind to
    #[arg(long, default_value = "127.0.0.1")]
    host: String,
}

/// Server state shared across requests
#[derive(Clone)]
struct AppState {
    manager: Arc<SessionManager>,
    sessions: Arc<RwLock<HashMap<String, String>>>, // name -> session_id mapping
}

/// Request to create a new session
#[derive(Deserialize)]
struct CreateSessionRequest {
    name: String,
    #[serde(default)]
    enable_ai_features: bool,
    #[serde(default)]
    working_directory: Option<String>,
    #[serde(default)]
    shell: Option<String>,
}

/// Request to execute a command
#[derive(Deserialize)]
struct ExecuteCommandRequest {
    command: String,
    #[serde(default = "default_timeout")]
    timeout_ms: u64,
}

fn default_timeout() -> u64 {
    5000
}

/// Response for session creation
#[derive(Serialize)]
struct SessionResponse {
    id: String,
    name: String,
    status: String,
    created_at: String,
}

/// Response for command execution
#[derive(Serialize)]
struct CommandResponse {
    success: bool,
    output: String,
    error: Option<String>,
    execution_time_ms: u64,
}

/// Response for session listing
#[derive(Serialize)]
struct SessionListResponse {
    sessions: Vec<SessionSummary>,
    total: usize,
}

#[derive(Serialize)]
struct SessionSummary {
    id: String,
    name: String,
    status: String,
    created_at: String,
    last_activity: String,
}

/// Error response
#[derive(Serialize)]
struct ErrorResponse {
    error: String,
    code: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    // Parse command line arguments
    let args = Args::parse();

    // Initialize tracing
    tracing_subscriber::fmt::init();

    println!("🚀 Starting AI-Session HTTP Server...");

    // Create application state
    let manager = Arc::new(SessionManager::new());
    let sessions = Arc::new(RwLock::new(HashMap::new()));

    let state = AppState { manager, sessions };

    // Build application routes
    let app = Router::new()
        .route("/", get(health_check))
        .route("/health", get(health_check))
        .route("/sessions", get(list_sessions))
        .route("/sessions", post(create_session))
        .route("/sessions/:name", get(get_session))
        .route("/sessions/:name", delete(delete_session))
        .route("/sessions/:name/execute", post(execute_command))
        .route("/sessions/:name/status", get(get_session_status))
        .route("/sessions/:name/output", get(get_session_output))
        .with_state(state)
        .layer(CorsLayer::permissive());

    // Start server
    let bind_addr = format!("{}:{}", args.host, args.port);
    let listener = tokio::net::TcpListener::bind(&bind_addr).await?;
    println!("✓ Server listening on http://{}", bind_addr);
    println!("\n📖 API Endpoints:");
    println!("  GET    /health                     - Health check");
    println!("  GET    /sessions                   - List all sessions");
    println!("  POST   /sessions                   - Create new session");
    println!("  GET    /sessions/:name             - Get session details");
    println!("  DELETE /sessions/:name             - Delete session");
    println!("  POST   /sessions/:name/execute     - Execute command");
    println!("  GET    /sessions/:name/status      - Get session status");
    println!("  GET    /sessions/:name/output      - Get session output");

    println!("\n🔧 Example curl commands:");
    println!("  # Create session:");
    println!(
        "  curl -X POST http://{}:{}/sessions \\",
        args.host, args.port
    );
    println!("       -H 'Content-Type: application/json' \\");
    println!("       -d '{{\"name\": \"dev\", \"enable_ai_features\": true}}'");
    println!();
    println!("  # Execute command:");
    println!(
        "  curl -X POST http://{}:{}/sessions/dev/execute \\",
        args.host, args.port
    );
    println!("       -H 'Content-Type: application/json' \\");
    println!("       -d '{{\"command\": \"echo Hello World\"}}'");
    println!();
    println!("  # Get session output:");
    println!(
        "  curl http://{}:{}/sessions/dev/output",
        args.host, args.port
    );

    axum::serve(listener, app).await?;

    Ok(())
}

/// Health check endpoint
async fn health_check() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "status": "healthy",
        "service": "ai-session-server",
        "version": env!("CARGO_PKG_VERSION"),
        "timestamp": chrono::Utc::now().to_rfc3339()
    }))
}

/// List all sessions
async fn list_sessions(
    State(state): State<AppState>,
) -> Result<Json<SessionListResponse>, StatusCode> {
    let sessions_map = state.sessions.read().await;
    let session_list = state.manager.list_session_refs();

    let mut sessions = Vec::new();
    for session in session_list {
        // Find the name for this session
        let name = sessions_map
            .iter()
            .find(|(_, id)| **id == session.id.to_string())
            .map(|(name, _)| name.clone())
            .unwrap_or_else(|| session.id.to_string());

        sessions.push(SessionSummary {
            id: session.id.to_string(),
            name,
            status: format!("{:?}", session.status().await),
            created_at: session.created_at.to_rfc3339(),
            last_activity: session.last_activity.read().await.to_rfc3339(),
        });
    }

    Ok(Json(SessionListResponse {
        total: sessions.len(),
        sessions,
    }))
}

/// Create a new session
async fn create_session(
    State(state): State<AppState>,
    Json(req): Json<CreateSessionRequest>,
) -> Result<Json<SessionResponse>, (StatusCode, Json<ErrorResponse>)> {
    // Check if session name already exists
    {
        let sessions = state.sessions.read().await;
        if sessions.contains_key(&req.name) {
            return Err((
                StatusCode::CONFLICT,
                Json(ErrorResponse {
                    error: format!("Session '{}' already exists", req.name),
                    code: "SESSION_EXISTS".to_string(),
                }),
            ));
        }
    }

    // Create session configuration
    let mut config = SessionConfig::default();
    config.enable_ai_features = req.enable_ai_features;

    if let Some(wd) = req.working_directory {
        config.working_directory = std::path::PathBuf::from(wd);
    }

    if let Some(shell) = req.shell {
        config.shell = Some(shell);
    }

    // Create the session
    let session = state
        .manager
        .create_session_with_config(config)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: format!("Failed to create session: {}", e),
                    code: "SESSION_CREATION_FAILED".to_string(),
                }),
            )
        })?;

    // Start the session
    session.start().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("Failed to start session: {}", e),
                code: "SESSION_START_FAILED".to_string(),
            }),
        )
    })?;

    // Store the name mapping
    {
        let mut sessions = state.sessions.write().await;
        sessions.insert(req.name.clone(), session.id.to_string());
    }

    Ok(Json(SessionResponse {
        id: session.id.to_string(),
        name: req.name,
        status: format!("{:?}", session.status().await),
        created_at: session.created_at.to_rfc3339(),
    }))
}

/// Get session details
async fn get_session(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<Json<SessionResponse>, (StatusCode, Json<ErrorResponse>)> {
    let session = get_session_by_name(&state, &name).await?;

    Ok(Json(SessionResponse {
        id: session.id.to_string(),
        name,
        status: format!("{:?}", session.status().await),
        created_at: session.created_at.to_rfc3339(),
    }))
}

/// Delete a session
async fn delete_session(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let session = get_session_by_name(&state, &name).await?;

    // Stop the session
    session.stop().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("Failed to stop session: {}", e),
                code: "SESSION_STOP_FAILED".to_string(),
            }),
        )
    })?;

    // Remove from manager
    state
        .manager
        .remove_session(&session.id)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: format!("Failed to remove session: {}", e),
                    code: "SESSION_REMOVAL_FAILED".to_string(),
                }),
            )
        })?;

    // Remove from name mapping
    {
        let mut sessions = state.sessions.write().await;
        sessions.remove(&name);
    }

    Ok(Json(serde_json::json!({
        "message": format!("Session '{}' deleted successfully", name),
        "timestamp": chrono::Utc::now().to_rfc3339()
    })))
}

/// Execute a command in a session
async fn execute_command(
    State(state): State<AppState>,
    Path(name): Path<String>,
    Json(req): Json<ExecuteCommandRequest>,
) -> Result<Json<CommandResponse>, (StatusCode, Json<ErrorResponse>)> {
    let session = get_session_by_name(&state, &name).await?;

    let start_time = std::time::Instant::now();

    // Send command to session
    session
        .send_input(&format!("{}\n", req.command))
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: format!("Failed to send command: {}", e),
                    code: "COMMAND_SEND_FAILED".to_string(),
                }),
            )
        })?;

    // Wait a bit for command execution
    tokio::time::sleep(tokio::time::Duration::from_millis(300)).await;

    // Read output
    let output = session.read_output().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("Failed to read output: {}", e),
                code: "OUTPUT_READ_FAILED".to_string(),
            }),
        )
    })?;

    let execution_time = start_time.elapsed().as_millis() as u64;
    let output_str = String::from_utf8_lossy(&output).to_string();

    Ok(Json(CommandResponse {
        success: true,
        output: clean_terminal_output(&output_str),
        error: None,
        execution_time_ms: execution_time,
    }))
}

/// Get session status
async fn get_session_status(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let session = get_session_by_name(&state, &name).await?;

    Ok(Json(serde_json::json!({
        "id": session.id,
        "name": name,
        "status": format!("{:?}", session.status().await),
        "created_at": session.created_at.to_rfc3339(),
        "last_activity": session.last_activity.read().await.to_rfc3339(),
        "config": {
            "enable_ai_features": session.config.enable_ai_features,
            "working_directory": session.config.working_directory.display().to_string(),
            "pty_size": session.config.pty_size
        }
    })))
}

/// Get session output (read latest output buffer)
async fn get_session_output(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let session = get_session_by_name(&state, &name).await?;

    let output = session.read_output().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("Failed to read output: {}", e),
                code: "OUTPUT_READ_FAILED".to_string(),
            }),
        )
    })?;

    let output_str = String::from_utf8_lossy(&output).to_string();

    Ok(Json(serde_json::json!({
        "session_name": name,
        "output": clean_terminal_output(&output_str),
        "raw_output": output_str,
        "timestamp": chrono::Utc::now().to_rfc3339(),
        "size_bytes": output.len()
    })))
}

/// Helper function to get session by name
async fn get_session_by_name(
    state: &AppState,
    name: &str,
) -> Result<std::sync::Arc<ai_session::AISession>, (StatusCode, Json<ErrorResponse>)> {
    // Get session ID from name mapping
    let session_id = {
        let sessions = state.sessions.read().await;
        sessions.get(name).cloned()
    };

    let session_id = session_id.ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: format!("Session '{}' not found", name),
                code: "SESSION_NOT_FOUND".to_string(),
            }),
        )
    })?;

    // Parse session ID and get session from manager
    let session_id = ai_session::SessionId::parse_str(&session_id).map_err(|_| {
        (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("Invalid session ID format: {}", session_id),
                code: "INVALID_SESSION_ID".to_string(),
            }),
        )
    })?;

    state.manager.get_session(&session_id).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: format!("Session '{}' not found in manager", name),
                code: "SESSION_NOT_FOUND".to_string(),
            }),
        )
    })
}

/// Clean terminal escape sequences and control characters for display
fn clean_terminal_output(output: &str) -> String {
    // Remove ANSI escape sequences and control characters
    let ansi_escape = regex::Regex::new(r"\x1b\[[0-9;]*[mK]").unwrap();
    let control_chars = regex::Regex::new(r"[\x00-\x1f\x7f]").unwrap();

    let cleaned = ansi_escape.replace_all(output, "");
    let cleaned = control_chars.replace_all(&cleaned, "");

    // Remove empty lines and excessive whitespace
    cleaned
        .lines()
        .filter(|line| !line.trim().is_empty())
        .map(|line| line.trim())
        .collect::<Vec<_>>()
        .join("\n")
}