periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
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
# File Manager Implementation

## Overview

The workflow file manager provides an interactive TUI for browsing, previewing, and managing DSL workflow files. It follows the same architectural patterns as the state browser for consistency.

**Location**: `src/tui/views/file_manager.rs`

## Features Implemented

### 1. **File Tree Navigation**
- Hierarchical directory tree with expandable/collapsible folders
- Visual tree indentation showing directory depth
- Directory expansion tracking with state persistence
- Navigate into directories with Enter key
- Navigate to parent directory with Backspace
- Icons for different file types:
  - πŸ“ Directories
  - βš™οΈ Workflow files (.yaml, .yml)
  - πŸ“„ Regular files

### 2. **Preview Pane**
- View file contents with syntax highlighting
- YAML syntax highlighting with color-coded:
  - Keys (bold, primary color)
  - Values (text color)
  - Comments (muted color)
  - List items (yellow)
- **Workflow Validation Integration**:
  - Automatic parsing of workflow files (.yaml, .yml)
  - Real-time validation using DSL validator
  - Visual validation status indicators:
    - βœ… Valid Workflow (green)
    - ❌ Validation Failed (red with error details)
    - ⚠️ Not a workflow file (yellow)
- Scrollable preview with:
  - Arrow keys for line-by-line scrolling
  - PgUp/PgDn for page scrolling
  - Visual scrollbar indicator
- Graceful error handling for unreadable files

### 3. **File Operations**

#### Quick Actions:
- **Open (o)**: Open workflow file in editor/viewer
- **Preview (p)**: View file contents with syntax highlighting
- **Delete (d)**: Remove file or directory
- **Rename (r)**: Rename file with inline input
- **Copy (c)**: Copy file to new name

#### Action Modes:
- Interactive input buffer for rename/copy operations
- Visual feedback showing current action
- Enter to confirm, Esc to cancel

### 4. **Search and Filter**
- Real-time filtering as you type
- Filter by filename or path
- Case-insensitive search
- Visual indication of filter query
- Filter persists across navigation

### 5. **Sorting Capabilities**

Seven sort modes with cycling:
- **Name ↑/↓**: Alphabetical sorting
- **Modified ↑/↓**: Sort by last modified time
- **Size ↑/↓**: Sort by file size
- **Type**: Group by file type (directories, workflows, files)

Press 's' to cycle through sort modes.

### 6. **Hidden Files**
- Toggle hidden files visibility with 'h' key
- Files starting with '.' are hidden by default
- Visual indicator showing hidden file status
- Persists across directory navigation

### 7. **Metadata Display**

For each file entry:
- File size in human-readable format (B, KB, MB, GB)
- Last modified time as relative time ("5m ago", "2h ago", "3d ago")
- File type indication through icons and colors
- Tree depth visualization

## Architecture

### State Management

```rust
pub struct FileManagerState {
    current_dir: PathBuf,              // Current directory path
    entries: Vec<FileEntry>,           // Flattened file tree
    selected_index: usize,             // Currently selected entry
    list_state: ListState,             // Ratatui list state
    view_mode: FileManagerViewMode,    // Tree or Preview
    preview_content: Option<String>,   // Cached preview
    preview_scroll: usize,             // Preview scroll position
    filter_query: String,              // Search query
    show_hidden: bool,                 // Show hidden files
    action_mode: FileActionMode,       // Current action
    input_buffer: String,              // Action input
    expanded_dirs: Vec<PathBuf>,       // Expanded directories
    sort_mode: FileSortMode,           // Current sort mode
    loaded_workflow: Option<DSLWorkflow>, // Loaded workflow (if valid)
    validation_errors: Vec<String>,    // Validation errors
}
```

### View Modes

```rust
pub enum FileManagerViewMode {
    Tree,    // File listing with tree navigation
    Preview, // File content preview with syntax highlighting
}
```

### Action Modes

```rust
pub enum FileActionMode {
    None,   // No action in progress
    Rename, // Renaming file (with input)
    Copy,   // Copying file (with input)
}
```

### File Entry

```rust
pub struct FileEntry {
    path: PathBuf,              // Full file path
    name: String,               // Display name
    is_dir: bool,               // Directory flag
    is_workflow: bool,          // Workflow file flag
    size: u64,                  // File size in bytes
    modified: SystemTime,       // Last modified time
    depth: usize,               // Tree depth level
}
```

## User Interface Layout

### Tree View
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Workflow File Manager - /path/to/workflows        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Filter: (type to search) | Sort: Name ↑ | Hidden: Off  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β–Ί πŸ“ workflows/          -        5m ago               β”‚
β”‚   β–Ί βš™οΈ workflow1.yaml    2.5 KB   2h ago               β”‚
β”‚   β–Ί βš™οΈ workflow2.yml     1.8 KB   1d ago               β”‚
β”‚ β–Ί πŸ“ templates/          -        3d ago               β”‚
β”‚ β–Ί πŸ“„ readme.txt          1.2 KB   1w ago               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Actions: o: Open | p: Preview | d: Delete      β”‚
β”‚                  r: Rename | c: Copy                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ↑/↓: Navigate | Enter: Open | Backspace: Parent        β”‚
β”‚ s: Sort | h: Toggle Hidden | /: Filter | q: Back       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Preview View (with Validation)
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Preview: /path/to/workflows/test.yaml          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              Validation                                β”‚
β”‚              βœ… Valid Workflow                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ name: Test Workflow                                    β”‚
β”‚ version: 1.0.0                                         β”‚
β”‚ description: A test workflow                           β”‚
β”‚                                                        β”‚
β”‚ agents:                                                β”‚
β”‚   researcher:                                          β”‚
β”‚     description: Research and gather information       β”‚
β”‚     tools: [WebSearch, Read]                          β”‚
β”‚                                                        β”‚
β”‚ tasks:                                                 β”‚
β”‚   research_topic:                                      β”‚
β”‚     agent: researcher                                  β”‚
β”‚     description: Research the given topic              β”‚
β”‚                                              ↑         β”‚
β”‚                                              β–ˆ         β”‚
β”‚                                              ↓         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      Esc/q: Back | ↑/↓: Scroll | PgUp/PgDn: Page       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Key Bindings

| Key | Action |
|-----|--------|
| ↑/↓ | Navigate up/down in file list |
| Enter | Open directory / Preview file |
| Backspace | Navigate to parent directory |
| o | Open selected file |
| p | Preview selected file |
| d | Delete selected file/directory |
| r | Rename selected file |
| c | Copy selected file |
| s | Cycle sort mode |
| h | Toggle hidden files |
| / | Start/modify filter |
| Esc/q | Back to previous view / Cancel action |
| PgUp/PgDn | Page up/down (in preview) |

## Implementation Details

### Workflow Loading and Validation

The file manager integrates directly with the DSL parser and validator:

```rust
/// Load and validate a workflow file
pub fn load_workflow(&mut self, path: &Path) -> Result<()> {
    self.validation_errors.clear();

    match parse_workflow_file(path) {
        Ok(workflow) => {
            // Validate the workflow
            match validate_workflow(&workflow) {
                Ok(_) => {
                    self.loaded_workflow = Some(workflow);
                }
                Err(e) => {
                    self.loaded_workflow = Some(workflow);
                    self.validation_errors.push(format!("Validation error: {}", e));
                }
            }
        }
        Err(e) => {
            self.loaded_workflow = None;
            self.validation_errors.push(format!("Parse error: {}", e));
        }
    }

    Ok(())
}
```

**Key Features**:
- Automatic workflow parsing when previewing .yaml/.yml files
- Real-time validation against DSL schema
- Stores both workflow and validation errors
- Provides query methods: `get_loaded_workflow()`, `has_validation_errors()`, `get_validation_errors()`

### Directory Loading
- Recursive directory traversal with depth tracking
- Lazy loading of subdirectories (only when expanded)
- Efficient filtering during directory read
- Error handling for permission issues

### Sorting Algorithm
- Stable sort with directories always first
- Secondary sort by selected criteria
- Preserves tree structure during sort

### Preview Rendering
- Line-by-line syntax analysis
- YAML-specific highlighting rules:
  - Comment detection (#)
  - Key-value pair parsing (:)
  - List item detection (-)
- Maintains indentation for readability
- Scrollable with visual scrollbar

### File Type Detection
```rust
fn is_workflow_file(path: &Path) -> bool {
    matches!(
        path.extension().map(|e| e.to_string_lossy().to_lowercase()),
        Some("yaml") | Some("yml")
    )
}
```

### Human-Readable Formatting

**File Sizes**:
- Bytes (< 1 KB): "500 B"
- Kilobytes (< 1 MB): "2.5 KB"
- Megabytes (< 1 GB): "1.8 MB"
- Gigabytes: "2.1 GB"

**Time Ago**:
- Seconds (< 1 min): "30s ago"
- Minutes (< 1 hour): "15m ago"
- Hours (< 1 day): "5h ago"
- Days: "3d ago"

## Testing

Comprehensive integration test suite in `tests/file_manager_integration_test.rs`:

1. **Basic Operations**: File/directory detection, counting
2. **Navigation**: Selection, movement, parent navigation
3. **Directory Expansion**: Tree expansion/collapse
4. **Filtering**: Search functionality, filter accuracy
5. **Sorting**: All sort modes, order verification
6. **Preview**: Content loading, scrolling
7. **Hidden Files**: Toggle visibility
8. **Metadata**: Icons, sizes, times
9. **Large Directories**: Performance with 100+ files
10. **Empty Directories**: Edge case handling

All tests use temporary directories for isolation.

## Integration Points

### With Other TUI Components
- Uses shared `Theme` for consistent styling
- Compatible with `Layout` system from other views
- Follows same navigation patterns as state browser
- Integrates with workflow editor for opening files

### With DSL System
- **Full Integration**: Uses `parse_workflow_file()` from DSL parser
- **Validation**: Integrates with `validate_workflow()` for real-time validation
- **Workflow Detection**: Automatically detects .yaml/.yml workflow files
- **Execution Ready**: Loaded workflows can be passed to executor
- **Editor Integration**: Can open workflows in editor with validation context

## Future Enhancements

### Planned Features
1. **File Creation**: New workflow from template
2. **Batch Operations**: Multi-select with space bar
3. **Clipboard**: Copy/paste file paths
4. **Bookmarks**: Quick access to common directories
5. **Search History**: Recent searches
6. **Validation Preview**: Show validation errors inline
7. **Git Integration**: Show file status indicators
8. **Diff View**: Compare workflow versions
9. **Tree Collapse All**: Collapse all expanded directories
10. **Keyboard Shortcuts**: Customizable keybindings

### Performance Optimizations
- Virtual scrolling for very large directories
- Incremental directory loading
- Cached file metadata
- Background file scanning

## Usage Example

```rust
use periplon_sdk::tui::views::file_manager::{
    FileManagerState, render_file_manager
};
use periplon_sdk::tui::theme::Theme;
use std::path::PathBuf;

// Initialize file manager
let mut state = FileManagerState::new(
    PathBuf::from("./workflows")
).unwrap();

// In your TUI render loop
render_file_manager(&mut frame, area, &mut state, &theme);

// Handle events
match key_event.code {
    KeyCode::Down => state.select_next(),
    KeyCode::Up => state.select_previous(),
    KeyCode::Enter => {
        if let Some(entry) = state.selected_entry() {
            if entry.is_dir {
                state.toggle_directory().unwrap();
            } else {
                state.load_preview().unwrap();
            }
        }
    }
    KeyCode::Char('p') => state.load_preview().unwrap(),
    KeyCode::Char('s') => state.next_sort_mode(),
    KeyCode::Char('h') => state.toggle_hidden().unwrap(),
    KeyCode::Esc => state.back_to_tree(),
    // ... more key handlers
}
```

## Design Patterns

### Hexagonal Architecture Compliance
- Pure view logic, no business rules
- State management separated from rendering
- File system operations abstracted
- Easy to test with temporary directories

### Ratatui Best Practices
- Stateful widgets for list management
- Efficient rendering with scroll optimization
- Proper scrollbar state management
- Theme-based styling

### Error Handling
- Result types for all I/O operations
- Graceful degradation on permission errors
- User-friendly error messages
- No panics in user-facing code

## Conclusion

The file manager provides a robust, user-friendly interface for browsing and managing DSL workflow files. It integrates seamlessly with the existing TUI architecture while providing powerful features like real-time filtering, multiple sort modes, and syntax-highlighted previews.

**Key Achievements**:
- βœ… Full file tree navigation with expansion
- βœ… Syntax-highlighted YAML preview
- βœ… Comprehensive file operations (delete, rename, copy)
- βœ… Advanced filtering and sorting
- βœ… Hidden file support
- βœ… Human-readable metadata display
- βœ… Extensive test coverage
- βœ… Consistent with existing TUI patterns