oxi-cli 0.6.3

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
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
# oxi-cli Architecture

This document describes the internal architecture of the `oxi-cli` crate.

## Session System

The session system manages conversation history with branching support.

### Session File Format

Sessions are stored as newline-delimited JSON (JSONL):

```
~/.oxi/sessions/{session_id}.jsonl
```

Each line is a `SessionEntry`:

```rust
pub struct SessionEntry {
    pub id: Uuid,
    pub parent_id: Option<Uuid>,     // Branch parent
    pub message: AgentMessage,
    pub timestamp: i64,
    pub metadata: SessionMetadata,
}
```

### Entry Types

```rust
pub enum AgentMessage {
    User {
        content: ContentValue,
    },
    Assistant {
        content: Vec<AssistantContentBlock>,
        provider: Option<String>,
        model_id: Option<String>,
        usage: Option<Usage>,
        stop_reason: Option<StopReason>,
    },
    ToolResult {
        tool_name: String,
        content: ContentValue,
    },
}
```

### Session Tree Structure

```
Session A
├── Entry 1 (User)
├── Entry 2 (Assistant)
├── Entry 3 (User)
└── Entry 4 (Assistant)
    ├── Branch B
    │   ├── Entry 5 (User) ── parent: Entry 4
    │   └── Entry 6 (Assistant)
    └── Branch C
        ├── Entry 7 (User) ── parent: Entry 4
        └── Entry 8 (Assistant)
```

### Branching

```rust
impl Session {
    /// Fork a new session from a specific entry
    pub fn fork(&mut self, entry_id: &Uuid, new_parent_id: &Uuid) -> Result<Uuid> {
        // Create new session with entries up to and including entry_id
        let entries: Vec<SessionEntry> = self.entries()
            .take_while(|e| e.id != *entry_id)
            .cloned()
            .collect();
        
        let new_id = Uuid::new_v4();
        let new_session = Session::new(new_id, entries, Some(*new_parent_id));
        Ok(new_id)
    }
}
```

### Session Migration

Sessions support version migration:

```rust
pub const SESSION_VERSION: u32 = 2;

impl Session {
    fn migrate(&mut self) -> Result<()> {
        match self.version {
            0 => self.migrate_v0_to_v1()?,
            1 => self.migrate_v1_to_v2()?,
            SESSION_VERSION => { /* current */ }
            _ => bail!("Unknown session version"),
        }
    }
}
```

## Extension System

### Extension Lifecycle

```
load()  ──►  on_load()  ──►  running  ──►  on_unload()  ──►  unload()
         register_tools()
```

### Extension Trait

```rust
#[async_trait]
pub trait Extension: Send + Sync {
    /// Extension metadata
    fn name(&self) -> &str;
    fn version(&self) -> &str;
    
    /// Lifecycle hooks
    async fn on_load(&self, ctx: &ExtensionContext) -> Result<()>;
    async fn on_unload(&self) -> Result<()>;
    
    /// Register tools
    fn register_tools(&self, registry: &ToolRegistry);
    
    /// Permission requirements
    fn permissions(&self) -> Vec<Permission>;
}
```

### Permissions

```rust
pub enum Permission {
    FileRead,
    FileWrite,
    Network,
    ExecuteCommand,
    ReadEnvironment,
}
```

### Extension Context

```rust
pub struct ExtensionContext {
    pub settings: Arc<Settings>,
    pub session: Arc<Session>,
    pub tools: Arc<ToolRegistry>,
    pub emit: Arc<dyn Fn(ExtensionEvent) + Send + Sync>,
}
```

### Loading Extensions

```rust
impl ExtensionLoader {
    /// Load extension from path
    pub async fn load(&self, path: &Path) -> Result<Arc<dyn Extension>> {
        let lib = unsafe { Library::new(path)? };
        
        // Find and call extension factory
        let factory: libloading::Symbol<CreateExtension> = lib.get(b"create_extension")?;
        let ext = factory();
        
        // Initialize
        ext.on_load(&self.context).await?;
        
        Ok(ext)
    }
}
```

## Settings Layering

Settings are applied in layers (later overrides earlier):

```
┌─────────────────────────────────────────┐
│  5. CLI arguments (highest priority)   │
├─────────────────────────────────────────┤
│  4. Environment variables (OXI_*)       │
├─────────────────────────────────────────┤
│  3. Project config (.oxi/settings.toml)│
├─────────────────────────────────────────┤
│  2. Global config (~/.oxi/settings.toml) │
├─────────────────────────────────────────┤
│  1. Built-in defaults (lowest)         │
└─────────────────────────────────────────┘
```

### Load Order

```rust
impl Settings {
    pub fn load() -> Result<Self> {
        let mut settings = Settings::default();        // 1. Defaults
        
        if let Some(path) = Self::settings_path() {
            settings = Self::layer_file(&settings, &path)?;  // 2. Global
        }
        
        if let Some(path) = Self::find_project_settings(&cwd) {
            settings = Self::layer_file(&settings, &path)?;  // 3. Project
        }
        
        settings.apply_env();                          // 4. Environment
        
        // 5. CLI handled separately via merge_cli()
        settings
    }
}
```

### Settings Structure

```rust
pub struct Settings {
    pub version: u32,
    
    // LLM settings
    pub thinking_level: ThinkingLevel,
    pub default_model: Option<String>,
    pub default_provider: Option<String>,
    pub temperature: Option<f32>,
    pub max_tokens: Option<u32>,
    
    // Session
    pub session_dir: Option<PathBuf>,
    pub session_history_size: usize,
    
    // Resources
    pub extensions: Vec<String>,
    pub skills: Vec<String>,
    pub prompts: Vec<String>,
    pub themes: Vec<String>,
    
    // Behavior
    pub stream_responses: bool,
    pub extensions_enabled: bool,
    pub auto_compaction: bool,
    pub tool_timeout_seconds: u64,
}
```

### Environment Variables

| Variable | Setting |
|----------|---------|
| `OXI_MODEL` | `default_model` |
| `OXI_PROVIDER` | `default_provider` |
| `OXI_THINKING` | `thinking_level` |
| `OXI_THEME` | `theme` |
| `OXI_MAX_TOKENS` | `max_tokens` |
| `OXI_TEMPERATURE` | `default_temperature` |
| `OXI_SESSION_DIR` | `session_dir` |
| `OXI_STREAM` | `stream_responses` |
| `OXI_AUTO_COMPACTION` | `auto_compaction` |
| `OXI_TOOL_TIMEOUT` | `tool_timeout_seconds` |

## AgentSession

The `AgentSession` ties together the agent runtime with session persistence:

```rust
pub struct AgentSession {
    pub agent: Arc<Agent>,
    pub session: Arc<Session>,
    pub settings: Arc<Settings>,
    pub tools: Arc<ToolRegistry>,
}
```

### Session Events

```rust
impl AgentSession {
    /// Run with session persistence
    pub async fn run(&mut self, prompt: String) -> Result<String> {
        // Add user message to session
        self.session.add_entry(AgentMessage::User { content: prompt.into() });
        
        // Run agent
        let (response, events) = self.agent.run(prompt).await?;
        
        // Persist assistant response
        self.session.add_entry(AgentMessage::Assistant { ... });
        
        Ok(response.content)
    }
}
```

## CLI Arguments

```rust
pub struct CliArgs {
    pub command: Option<Commands>,
    pub provider: Option<String>,
    pub model: Option<String>,
    pub prompt: Vec<String>,
    pub interactive: bool,
    pub thinking: Option<String>,
    pub extensions: Vec<PathBuf>,
    pub mode: Option<String>,
    pub tools: Option<String>,
    pub append_system_prompt: Option<PathBuf>,
    pub print: bool,
    pub no_session: bool,
}
```

### Subcommands

```rust
pub enum Commands {
    Sessions,                                    // List sessions
    Tree { session_id: String },                 // Show session tree
    Fork { parent_id: String, entry_id: String }, // Branch session
    Delete { session_id: String },               // Delete session
    Pkg { action: PkgCommands },                 // Package management
    Config { action: ConfigCommands },           // Config management
}
```

## Package Management

### Package Sources

Packages can be loaded from:
- Local paths: `/path/to/package`
- npm packages: `npm:@scope/package-name`

### Package Installation

```rust
pub enum PkgCommands {
    Install { source: String },
    List,
    Uninstall { name: String },
    Update { name: Option<String> },
}
```

### Package Discovery

```rust
impl PackageManager {
    pub async fn install(&self, source: &str) -> Result<Package> {
        if source.starts_with("npm:") {
            self.install_npm(&source[4..]).await
        } else {
            self.install_local(Path::new(source))
        }
    }
}
```

## Slash Commands

Slash commands provide in-session shortcuts:

```rust
pub struct SlashCommand {
    pub name: String,
    pub description: String,
    pub handler: Box<dyn Fn(&str, &mut AgentSession) -> Result<()>>,
}
```

### Built-in Commands

| Command | Description |
|---------|-------------|
| `/model <model>` | Switch model |
| `/provider <provider>` | Switch provider |
| `/session` | Show current session info |
| `/clear` | Clear conversation |
| `/compact` | Trigger compaction |
| `/save <name>` | Save conversation |
| `/load <name>` | Load conversation |

## Telemetry

```rust
pub struct Telemetry {
    events: Vec<TelemetryEvent>,
    flush_interval: Duration,
}

pub enum TelemetryEvent {
    SessionStart { session_id: Uuid },
    MessageSent { tokens: usize },
    MessageReceived { tokens: usize },
    ToolUsed { tool: String, duration_ms: u64 },
    Error { error: String },
}
```

## Error Recovery

```rust
pub struct RetryConfig {
    pub max_attempts: u32,
    pub base_delay: Duration,
    pub max_delay: Duration,
    pub backoff_multiplier: f32,
}

pub enum RetryableError {
    NetworkError,
    RateLimit { retry_after: Duration },
    Timeout,
    ProviderError,
}
```

### Retry Strategy

```rust
impl RetryStrategy {
    fn should_retry(&self, error: &Error, attempt: u32) -> bool {
        match error {
            Error::Retryable(r) => attempt < self.max_attempts,
            _ => false,
        }
    }
    
    fn next_delay(&self, attempt: u32) -> Duration {
        let delay = self.base_delay * self.backoff_multiplier.powi(attempt as i32);
        delay.min(self.max_delay)
    }
}
```