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
//! Intelligent output management for AI sessions

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Output manager for intelligent processing
pub struct OutputManager {
    /// Output parser
    pub parser: OutputParser,
    /// Semantic compressor
    pub compressor: SemanticCompressor,
    /// Output cache
    _cache: HashMap<String, ParsedOutput>,
}

impl OutputManager {
    /// Create a new output manager
    pub fn new() -> Self {
        Self {
            parser: OutputParser::new(),
            compressor: SemanticCompressor::new(),
            _cache: HashMap::new(),
        }
    }

    /// Process raw output
    pub fn process_output(&mut self, raw_output: &str) -> Result<ProcessedOutput> {
        // Parse the output
        let parsed = self.parser.parse(raw_output)?;

        // Extract highlights
        let highlights = self.extract_highlights(&parsed);

        // Compress if needed
        let compressed = if raw_output.len() > 1024 {
            Some(self.compressor.compress(raw_output)?)
        } else {
            None
        };

        Ok(ProcessedOutput {
            raw: raw_output.to_string(),
            parsed: parsed.clone(),
            highlights,
            compressed,
            timestamp: chrono::Utc::now(),
        })
    }

    /// Extract highlights from parsed output
    fn extract_highlights(&self, parsed: &ParsedOutput) -> Vec<Highlight> {
        let mut highlights = Vec::new();

        match parsed {
            ParsedOutput::CodeExecution { result: _, metrics } => {
                if metrics.execution_time > std::time::Duration::from_secs(5) {
                    highlights.push(Highlight {
                        category: HighlightCategory::Performance,
                        message: format!("Slow execution: {:?}", metrics.execution_time),
                        severity: Severity::Warning,
                    });
                }
            }
            ParsedOutput::BuildOutput { status, .. } => match status {
                BuildStatus::Failed(error) => {
                    highlights.push(Highlight {
                        category: HighlightCategory::Error,
                        message: error.clone(),
                        severity: Severity::Error,
                    });
                }
                BuildStatus::Warning(warning) => {
                    highlights.push(Highlight {
                        category: HighlightCategory::Warning,
                        message: warning.clone(),
                        severity: Severity::Warning,
                    });
                }
                _ => {}
            },
            ParsedOutput::TestResults { failed, .. } => {
                if *failed > 0 {
                    highlights.push(Highlight {
                        category: HighlightCategory::TestFailure,
                        message: format!("{} tests failed", failed),
                        severity: Severity::Error,
                    });
                }
            }
            ParsedOutput::StructuredLog { level, message, .. } => {
                if matches!(level, LogLevel::Error | LogLevel::Warning) {
                    highlights.push(Highlight {
                        category: HighlightCategory::Log,
                        message: message.clone(),
                        severity: match level {
                            LogLevel::Error => Severity::Error,
                            LogLevel::Warning => Severity::Warning,
                            _ => Severity::Info,
                        },
                    });
                }
            }
            _ => {}
        }

        highlights
    }
}

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

/// Output parser
pub struct OutputParser {
    /// Pattern matchers
    patterns: HashMap<String, regex::Regex>,
}

impl OutputParser {
    /// Create a new parser
    pub fn new() -> Self {
        let mut patterns = HashMap::new();

        // Add common patterns
        patterns.insert(
            "error".to_string(),
            regex::Regex::new(r"(?i)(error|exception|failure)").unwrap(),
        );
        patterns.insert(
            "warning".to_string(),
            regex::Regex::new(r"(?i)(warning|warn)").unwrap(),
        );
        patterns.insert(
            "success".to_string(),
            regex::Regex::new(r"(?i)(success|passed|completed)").unwrap(),
        );

        Self { patterns }
    }

    /// Parse output into structured format
    pub fn parse(&self, output: &str) -> Result<ParsedOutput> {
        // Simple heuristic-based parsing
        // In a real implementation, this would be much more sophisticated

        if output.contains("BUILD SUCCESSFUL") || output.contains("Build succeeded") {
            Ok(ParsedOutput::BuildOutput {
                status: BuildStatus::Success,
                artifacts: Vec::new(),
            })
        } else if output.contains("BUILD FAILED") || output.contains("Build failed") {
            Ok(ParsedOutput::BuildOutput {
                status: BuildStatus::Failed("Build failed".to_string()),
                artifacts: Vec::new(),
            })
        } else if output.contains("tests passed") || output.contains("All tests passed") {
            Ok(ParsedOutput::TestResults {
                passed: 1, // Placeholder
                failed: 0,
                details: TestDetails::default(),
            })
        } else if self.patterns["error"].is_match(output) {
            Ok(ParsedOutput::StructuredLog {
                level: LogLevel::Error,
                message: output.to_string(),
                context: LogContext::default(),
            })
        } else {
            Ok(ParsedOutput::PlainText(output.to_string()))
        }
    }
}

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

/// Semantic compressor
pub struct SemanticCompressor {
    /// Compression level (0.0 - 1.0)
    _compression_level: f32,
}

impl SemanticCompressor {
    /// Create a new compressor
    pub fn new() -> Self {
        Self {
            _compression_level: 0.5,
        }
    }

    /// Compress output semantically
    pub fn compress(&self, output: &str) -> Result<CompressedOutput> {
        // Simple implementation: just truncate for now
        // In a real implementation, this would use NLP techniques
        let compressed = if output.len() > 500 {
            format!("{}... (truncated)", &output[..500])
        } else {
            output.to_string()
        };

        let compressed_len = compressed.len();
        let original_len = output.len();

        Ok(CompressedOutput {
            original_size: original_len,
            compressed_size: compressed_len,
            content: compressed,
            compression_ratio: compressed_len as f32 / original_len as f32,
        })
    }
}

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

/// Parsed output types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ParsedOutput {
    /// Plain text output
    PlainText(String),

    /// Code execution result
    CodeExecution {
        result: String,
        metrics: ExecutionMetrics,
    },

    /// Build output
    BuildOutput {
        status: BuildStatus,
        artifacts: Vec<Artifact>,
    },

    /// Test results
    TestResults {
        passed: usize,
        failed: usize,
        details: TestDetails,
    },

    /// Structured log
    StructuredLog {
        level: LogLevel,
        message: String,
        context: LogContext,
    },
}

/// Execution metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionMetrics {
    /// Execution time
    pub execution_time: std::time::Duration,
    /// Memory usage
    pub memory_usage: Option<usize>,
    /// CPU usage
    pub cpu_usage: Option<f32>,
}

/// Build status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BuildStatus {
    Success,
    Failed(String),
    Warning(String),
    InProgress,
}

/// Build artifact
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Artifact {
    /// Artifact name
    pub name: String,
    /// Artifact path
    pub path: String,
    /// Size in bytes
    pub size: usize,
}

/// Test details
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TestDetails {
    /// Test suite name
    pub suite: Option<String>,
    /// Duration
    pub duration: Option<std::time::Duration>,
    /// Failed test names
    pub failed_tests: Vec<String>,
}

/// Log level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warning,
    Error,
}

/// Log context
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LogContext {
    /// Source file
    pub file: Option<String>,
    /// Line number
    pub line: Option<usize>,
    /// Additional fields
    pub fields: HashMap<String, serde_json::Value>,
}

/// Processed output
#[derive(Debug, Clone)]
pub struct ProcessedOutput {
    /// Raw output
    pub raw: String,
    /// Parsed output
    pub parsed: ParsedOutput,
    /// Highlights
    pub highlights: Vec<Highlight>,
    /// Compressed version
    pub compressed: Option<CompressedOutput>,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Output highlight
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Highlight {
    /// Category
    pub category: HighlightCategory,
    /// Message
    pub message: String,
    /// Severity
    pub severity: Severity,
}

/// Highlight category
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HighlightCategory {
    Error,
    Warning,
    Performance,
    TestFailure,
    Log,
    Success,
}

/// Severity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Severity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Compressed output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressedOutput {
    /// Original size
    pub original_size: usize,
    /// Compressed size
    pub compressed_size: usize,
    /// Compressed content
    pub content: String,
    /// Compression ratio
    pub compression_ratio: f32,
}

// Add regex dependency to Cargo.toml for this module
use once_cell::sync::Lazy;
static _REGEX_DEPENDENCY: Lazy<()> = Lazy::new(|| {
    // This is just a marker to remind about adding regex = "1.10" to Cargo.toml
});

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

    #[test]
    fn test_output_parser() {
        let parser = OutputParser::new();

        let output = "BUILD SUCCESSFUL";
        let parsed = parser.parse(output).unwrap();

        match parsed {
            ParsedOutput::BuildOutput { status, .. } => {
                assert!(matches!(status, BuildStatus::Success));
            }
            _ => panic!("Expected BuildOutput"),
        }
    }

    #[test]
    fn test_output_manager() {
        let mut manager = OutputManager::new();

        let output = "Error: Something went wrong";
        let processed = manager.process_output(output).unwrap();

        assert!(!processed.highlights.is_empty());
        assert_eq!(processed.highlights[0].severity, Severity::Error);
    }
}