terraphim / terraphim/terraphim-ai

Fix TUI/REPL offline mode to use TuiService instead of mock data

Open
#261 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug enhancement
Dominant language
Rust
Stars
62
Forks
5
Avg merge
2h 27m
Merged PRs (30d)
1

Description

Problem

The TUI and REPL offline modes are currently broken or showing mock data instead of performing real searches:

Issues
  1. Interactive TUI Offline Mode (main.rs:202-206)

    • Has TODO comment
    • Falls back to server mode (requires running server)
    • Should use TuiService for true standalone operation
  2. REPL /search in Offline Mode (handler.rs:526-617)

    • Shows hardcoded mock results
    • Displays fake data like "Introduction to Terraphim"
    • Doesn't actually search using TuiService
  3. REPL Autocomplete in Offline Mode (handler.rs:995+)

    • Not implemented for offline mode
    • Needs TuiService.autocomplete() integration
  4. Role/Config Commands Offline

    • REPL offline mode doesn't properly handle role switching
    • Config changes don't work without server
Current Behavior
# User expects offline mode to work
$ terraphim-tui repl
terraphim> /search "async"
📱 Offline mode - showing mock results  # ← WRONG!
Introduction to Terraphim              # ← FAKE DATA
Expected Behavior
$ terraphim-tui repl
terraphim> /search "async"
🔍 Searching for: 'async'
✅ Found 5 result(s)
- async programming patterns
- tokio runtime guide  
- (real search results from TuiService)

Root Cause

REPL Handler doesn't use TuiService - only has Option<ApiClient>:

pub struct ReplHandler {
    api_client: Option<ApiClient>,  // ← Only this
    current_role: String,
    command_registry: Option<CommandRegistry>,
}

Solution

1. Add TuiService to ReplHandler
pub struct ReplHandler {
    api_client: Option<ApiClient>,
    tui_service: Option<TuiService>,  // ← ADD THIS
    current_role: String,
    command_registry: Option<CommandRegistry>,
}
2. Initialize TuiService in Offline Mode
pub async fn run_repl_offline_mode() -> Result<()> {
    let mut handler = ReplHandler::new_offline();
    handler.initialize_offline_service().await?;  // ← Initialize TuiService
    handler.run().await
}

impl ReplHandler {
    pub async fn initialize_offline_service(&mut self) -> Result<()> {
        self.tui_service = Some(TuiService::new().await?);
        
        // Get actual role from service
        if let Some(service) = &self.tui_service {
            self.current_role = service.get_selected_role().await.to_string();
        }
        
        Ok(())
    }
}
3. Fix handle_search for Offline

Replace mock data section (line 526-617) with:

if self.api_client.is_none() {
    // Offline mode - use TuiService
    if let Some(service) = &self.tui_service {
        let role_name = role.as_ref()
            .map(|r| RoleName::new(r))
            .unwrap_or_else(|| RoleName::new(&self.current_role));
        
        let results = service.search_with_role(&query, &role_name, limit).await?;
        
        // Display REAL results (not mock)
        // ... format and display table
    } else {
        println!("⚠️  Offline service not initialized. Run with --server flag.");
    }
}
4. Fix handle_autocomplete
if self.api_client.is_none() {
    // Offline mode - use TuiService
    if let Some(service) = &self.tui_service {
        let role_name = RoleName::new(&self.current_role);
        let results = service.autocomplete(&role_name, &query, limit).await?;
        
        // Display autocomplete results
        for (i, result) in results.iter().enumerate() {
            println!("  {}. {}", i + 1, result.text);
        }
    }
}
5. Fix Interactive TUI Offline Mode

Update main.rs:202-206:

fn run_tui_offline_mode(transparent: bool) -> Result<()> {
    let rt = Runtime::new()?;
    let service = rt.block_on(async { TuiService::new().await })?;
    run_tui_with_service(transparent, service, rt)
}

Create new ui_loop_offline() that uses TuiService instead of ApiClient.

Implementation Checklist

  • Add tui_service: Option<TuiService> to ReplHandler struct
  • Add initialize_offline_service() method
  • Update run_repl_offline_mode() to initialize TuiService
  • Fix handle_search() offline branch to use TuiService
  • Fix handle_autocomplete() offline branch to use TuiService
  • Fix handle_role() offline branch
  • Fix handle_config() offline branch
  • Fix run_tui_offline_mode() in main.rs
  • Create ui_loop_offline() or refactor to accept either ApiClient or TuiService
  • Add tests for offline mode
  • Update docs/tui-usage.md with offline mode instructions

Affected Files

  • crates/terraphim_tui/src/repl/handler.rs - Main REPL logic
  • crates/terraphim_tui/src/main.rs - Interactive TUI offline mode
  • docs/tui-usage.md - Documentation
  • Tests: Add offline mode integration tests

Benefits

✅ True offline operation without requiring server
✅ Real search results from local haystacks
✅ Real autocomplete from thesaurus
✅ Consistent behavior between offline/online modes
✅ Better user experience for local-only usage

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with crates/terraphim_tui/src/repl/handler.rs, especially handle_search, handle_autocomplete, role/config handling, and run_repl_offline_mode; then inspect main.rs:202-206 and the existing TUI flow. Add offline-mode integration tests and update docs/tui-usage.md; done means REPL and interactive TUI use TuiService for real search and autocomplete without a server, with role and config commands working offline.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.