limit-cli 0.0.46

AI-powered terminal coding assistant with TUI. Multi-provider LLM support, session persistence, and built-in tools.
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use limit_cli::{AgentBridge, TuiBridge, TuiState};
use limit_llm::{
    BrowserConfigSection, CacheSettings, CompactionSettings, Config as LlmConfig, ProviderConfig,
};
use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use tokio::sync::mpsc;

#[test]
fn test_tui_integration_full_conversation() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-integration-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    assert!(agent_bridge.is_ready());

    let tools = agent_bridge.get_tool_definitions();
    assert!(!tools.is_empty());
    assert!(tools.iter().any(|t| t.function.name == "file_read"));
}

#[test]
fn test_tui_bridge_event_ordering() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (tx, rx) = mpsc::unbounded_channel();

    let mut tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Simulate event sequence: Thinking -> ToolStart -> ToolComplete -> Done
    let events = vec![
        limit_cli::AgentEvent::Thinking {
            operation_id: tui_bridge.operation_id(),
        },
        limit_cli::AgentEvent::ToolStart {
            operation_id: tui_bridge.operation_id(),
            name: "file_read".to_string(),
            args: serde_json::json!({"path": "/tmp/test.txt"}),
        },
        limit_cli::AgentEvent::ToolComplete {
            operation_id: tui_bridge.operation_id(),
            name: "file_read".to_string(),
            result: "Hello, World!".to_string(),
        },
        limit_cli::AgentEvent::Done {
            operation_id: tui_bridge.operation_id(),
        },
    ];

    for event in events {
        tx.send(event).unwrap();
        tui_bridge.process_events().unwrap();
        thread::sleep(Duration::from_millis(10));
    }

    // Verify final state is Idle
    assert_eq!(tui_bridge.state(), TuiState::Idle);

    // Verify chat has system messages
    assert!(tui_bridge.chat_view().lock().unwrap().message_count() > 0);
}

#[test]
fn test_tui_bridge_tool_execution_display() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (tx, rx) = mpsc::unbounded_channel();

    let mut tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // TuiBridge::new() adds 2 system messages (welcome + model info)
    let initial_count = tui_bridge.chat_view().lock().unwrap().message_count();
    assert!(
        initial_count >= 2,
        "Should have at least 2 system messages, got {}",
        initial_count
    );

    // Add user message
    tui_bridge.add_user_message("Read the file /tmp/test.txt".to_string());
    assert_eq!(
        tui_bridge.chat_view().lock().unwrap().message_count(),
        initial_count + 1,
        "Should have one more message after adding user message"
    );

    // Send tool events
    tx.send(limit_cli::AgentEvent::ToolStart {
        operation_id: tui_bridge.operation_id(),
        name: "file_read".to_string(),
        args: serde_json::json!({"path": "/tmp/test.txt"}),
    })
    .unwrap();
    tui_bridge.process_events().unwrap();

    // Tool activities go to activity feed, state remains Idle (or Thinking if set)
    // Activity feed should have an in-progress activity
    assert!(tui_bridge.activity_feed().lock().unwrap().has_in_progress());

    // Send tool complete event
    tx.send(limit_cli::AgentEvent::ToolComplete {
        operation_id: tui_bridge.operation_id(),
        name: "file_read".to_string(),
        result: "File content here".to_string(),
    })
    .unwrap();
    tui_bridge.process_events().unwrap();

    // Activity should be marked complete (no longer in-progress)

    // State should be Idle
    assert_eq!(tui_bridge.state(), TuiState::Idle);

    // Chat should have more messages
    assert!(tui_bridge.chat_view().lock().unwrap().message_count() > 1);
}

#[test]
fn test_tui_bridge_error_handling() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (tx, rx) = mpsc::unbounded_channel();

    let mut tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Send error event
    tx.send(limit_cli::AgentEvent::Error {
        operation_id: tui_bridge.operation_id(),
        message: "Tool execution failed".to_string(),
    })
    .unwrap();
    tui_bridge.process_events().unwrap();

    // State should be Idle (errors reset state so user can continue)
    assert_eq!(tui_bridge.state(), TuiState::Idle);

    // Chat should have error message
}

#[test]
fn test_tui_bridge_spinner_animation() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Send thinking event
    tx.send(limit_cli::AgentEvent::Thinking {
        operation_id: tui_bridge.operation_id(),
    })
    .unwrap();

    let mut tui_bridge_mut = tui_bridge;
    tui_bridge_mut.process_events().unwrap();

    // State should be Thinking
    assert!(matches!(tui_bridge_mut.state(), TuiState::Thinking));

    // Tick spinner multiple times and verify it changes
    let frames: Vec<String> = (0..5)
        .map(|_| {
            let frame = tui_bridge_mut
                .spinner()
                .lock()
                .unwrap()
                .current_frame()
                .to_string();
            tui_bridge_mut.tick_spinner();
            frame
        })
        .collect();

    // All frames should be different (at least some)
    let unique_frames: std::collections::HashSet<&String> = frames.iter().collect();
    assert!(unique_frames.len() > 1);
}

#[test]
fn test_tui_bridge_content_streaming() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (tx, rx) = mpsc::unbounded_channel();

    let mut tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Send multiple content chunks
    let chunks = ["Hello", " ", "World", "!"];
    for chunk in chunks.iter() {
        tx.send(limit_cli::AgentEvent::ContentChunk {
            operation_id: tui_bridge.operation_id(),
            chunk: chunk.to_string(),
        })
        .unwrap();
        tui_bridge.process_events().unwrap();
    }

    // Chat should have initial system messages from TuiBridge::new()
    // ContentChunk accumulates content into a single assistant message
    // Expected: 2 system messages (welcome + model) + 1 assistant message = 3 total
    let chat = tui_bridge.chat_view().lock().unwrap();
    let count = chat.message_count();
    assert!(
        count >= 2,
        "Should have at least 2 system messages, got {}",
        count
    );
    assert!(
        count >= 3,
        "Should have at least 3 messages (2 system + 1 assistant), got {}",
        count
    );
}

#[test]
fn test_tui_bridge_is_ready() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (_tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // The underlying agent bridge should be ready
    assert!(tui_bridge.agent_bridge().is_ready());
}

#[test]
fn test_tui_bridge_get_tool_definitions() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (_tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Get tool definitions through the agent bridge
    let tools = tui_bridge.agent_bridge().get_tool_definitions();
    assert!(!tools.is_empty());
}

#[test]
fn test_tui_bridge_tool_schema() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("test-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (_tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Verify file_read tool schema
    let tools = tui_bridge.agent_bridge().get_tool_definitions();
    let file_read = tools
        .iter()
        .find(|t| t.function.name == "file_read")
        .unwrap();

    assert_eq!(file_read.function.name, "file_read");
    assert!(file_read.function.description.contains("Read"));
    assert!(file_read.function.parameters["properties"]["path"]["type"] == "string");
}

#[test]
fn test_tui_bridge_with_good_config() {
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("good-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 8192,
            timeout: 120,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (_tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Verify the bridge was created successfully
    assert_eq!(tui_bridge.state(), TuiState::Idle);
    assert!(tui_bridge.agent_bridge().is_ready());
    assert_eq!(
        tui_bridge.agent_bridge().model(),
        "claude-3-5-sonnet-20241022"
    );
    assert_eq!(tui_bridge.agent_bridge().max_tokens(), 8192);
    assert_eq!(tui_bridge.agent_bridge().timeout(), 120);
}

#[test]
fn test_tui_bridge_from_string_config() {
    // This test verifies that the agent bridge can be created from a config
    // The actual config loading happens in the REPL, so we just verify the bridge works
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: Some("string-config-key".to_string()),
            model: "claude-3-5-sonnet-20241022".to_string(),
            base_url: None,
            max_tokens: 4096,
            timeout: 60,
            max_iterations: 100,
            thinking_enabled: false,
            clear_thinking: true,
        },
    );
    let config = LlmConfig {
        provider: "anthropic".to_string(),
        providers,
        browser: BrowserConfigSection::default(),
        compaction: CompactionSettings::default(),
        cache: CacheSettings::default(),
    };

    let agent_bridge = AgentBridge::new(config).unwrap();
    let (_tx, rx) = mpsc::unbounded_channel();

    let tui_bridge = TuiBridge::new(agent_bridge, rx).unwrap();

    // Verify the bridge was created and is ready
    assert!(tui_bridge.agent_bridge().is_ready());
}

#[test]
fn test_file_autocomplete_integration() {
    // Test that file autocomplete can be triggered and returns results
    use limit_cli::file_finder::FileFinder;

    // Create a file finder for current directory
    let working_dir = std::env::current_dir().unwrap();
    let mut finder = FileFinder::new(working_dir);

    // Scan files (clone to avoid borrow issues)
    let files = finder.scan_files().clone();
    assert!(!files.is_empty(), "Should find files in current directory");

    // Filter by "Cargo"
    let matches = finder.filter_files(&files, "Cargo");
    assert!(!matches.is_empty(), "Should find Cargo files");

    // Verify Cargo.toml is in results
    assert!(
        matches
            .iter()
            .any(|m| m.path.to_string_lossy() == "Cargo.toml"),
        "Should find Cargo.toml"
    );

    // Verify fuzzy matching works
    let fuzzy_matches = finder.filter_files(&files, "Crgo");
    // Fuzzy matching should still find Cargo.toml even with typo
    // Note: frizbee may or may not match depending on fuzziness threshold
    // So we just verify the function runs without error
    assert!(fuzzy_matches.len() <= 20, "Should limit to 20 results");
}