Skip to content

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

Description

@AlexMikhalev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions