terraphim_agent 1.16.34

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
//! Main wizard orchestration
//!
//! Provides the interactive setup wizard flow for first-time users
//! and add-role capability for extending existing configurations.

use dialoguer::{Confirm, Select, theme::ColorfulTheme};
use terraphim_config::Role;
use terraphim_types::RelevanceFunction;

use super::OnboardingError;
use super::prompts::{
    PromptResult, prompt_haystacks, prompt_knowledge_graph, prompt_llm_config,
    prompt_relevance_function, prompt_role_basics, prompt_theme,
};
use super::templates::{ConfigTemplate, TemplateRegistry};
use super::validation::validate_role;

/// Result of running the setup wizard
#[derive(Debug)]
pub enum SetupResult {
    /// User selected a quick-start template
    Template {
        /// The template that was applied
        template: ConfigTemplate,
        /// Custom path if provided
        #[allow(dead_code)]
        custom_path: Option<String>,
        /// The built role
        role: Role,
    },
    /// User created a custom role configuration
    Custom {
        /// The configured role
        role: Role,
    },
    /// User cancelled the wizard
    Cancelled,
}

/// Mode for running the setup wizard
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetupMode {
    /// First-time setup - create new configuration
    FirstRun,
    /// Add a role to existing configuration
    AddRole,
}

/// Quick start menu choices
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuickStartChoice {
    /// Terraphim Engineer with graph embeddings
    TerraphimEngineer,
    /// LLM Enforcer with bun install KG
    LlmEnforcer,
    /// Rust Developer with QueryRs
    RustEngineer,
    /// Local Notes with Ripgrep
    LocalNotes,
    /// AI Engineer with Ollama
    AiEngineer,
    /// Log Analyst with Quickwit
    LogAnalyst,
    /// Custom configuration
    Custom,
}

impl QuickStartChoice {
    /// Get the template ID for this choice
    pub fn template_id(&self) -> Option<&'static str> {
        match self {
            Self::TerraphimEngineer => Some("terraphim-engineer"),
            Self::LlmEnforcer => Some("llm-enforcer"),
            Self::RustEngineer => Some("rust-engineer"),
            Self::LocalNotes => Some("local-notes"),
            Self::AiEngineer => Some("ai-engineer"),
            Self::LogAnalyst => Some("log-analyst"),
            Self::Custom => None,
        }
    }

    /// Get the display name for this choice
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::TerraphimEngineer => {
                "Terraphim Engineer - Semantic search with knowledge graph embeddings"
            }
            Self::LlmEnforcer => "LLM Enforcer - AI agent hooks with bun install knowledge graph",
            Self::RustEngineer => "Rust Developer - Search Rust docs and crates.io via QueryRs",
            Self::LocalNotes => "Local Notes - Search markdown files in a local folder",
            Self::AiEngineer => "AI Engineer - Local Ollama LLM with knowledge graph support",
            Self::LogAnalyst => "Log Analyst - Quickwit integration for log analysis",
            Self::Custom => "Custom Configuration - Build your own role from scratch",
        }
    }

    /// Get all choices in order
    pub fn all() -> Vec<Self> {
        vec![
            Self::TerraphimEngineer,
            Self::LlmEnforcer,
            Self::RustEngineer,
            Self::LocalNotes,
            Self::AiEngineer,
            Self::LogAnalyst,
            Self::Custom,
        ]
    }
}

/// Apply a template directly without interactive wizard
///
/// # Arguments
/// * `template_id` - ID of the template to apply
/// * `custom_path` - Optional custom path override
///
/// # Returns
/// The configured Role or an error
pub fn apply_template(
    template_id: &str,
    custom_path: Option<&str>,
) -> Result<Role, OnboardingError> {
    let registry = TemplateRegistry::new();

    let template = registry
        .get(template_id)
        .ok_or_else(|| OnboardingError::TemplateNotFound(template_id.to_string()))?;

    // Check if template requires path and none provided
    if template.requires_path && custom_path.is_none() {
        return Err(OnboardingError::Validation(format!(
            "Template '{}' requires a --path argument",
            template_id
        )));
    }

    let role = template.build_role(custom_path);

    // Validate the built role
    validate_role(&role).map_err(|errors| {
        OnboardingError::Validation(
            errors
                .iter()
                .map(|e| e.to_string())
                .collect::<Vec<_>>()
                .join("; "),
        )
    })?;

    Ok(role)
}

/// Run the interactive setup wizard
///
/// # Arguments
/// * `mode` - Whether this is first-run or add-role mode
///
/// # Returns
/// SetupResult indicating what the user chose
pub async fn run_setup_wizard(mode: SetupMode) -> Result<SetupResult, OnboardingError> {
    // Check if we're running in a TTY
    #[cfg(feature = "repl-interactive")]
    {
        use std::io::IsTerminal;
        if !std::io::stdin().is_terminal() {
            return Err(OnboardingError::NotATty);
        }
    }

    let theme = ColorfulTheme::default();

    // Display welcome message
    println!();
    match mode {
        SetupMode::FirstRun => {
            println!("Welcome to Terraphim AI Setup");
            println!("-----------------------------");
            println!();
            println!("Let's configure your first role. You can add more roles later.");
        }
        SetupMode::AddRole => {
            println!("Add a New Role");
            println!("--------------");
            println!();
            println!("Configure a new role to add to your existing configuration.");
        }
    }
    println!();

    // Show quick start menu
    let choice = quick_start_menu(&theme)?;

    match choice {
        QuickStartChoice::Custom => {
            // Run full custom wizard
            match custom_wizard(&theme) {
                Ok(role) => Ok(SetupResult::Custom { role }),
                Err(OnboardingError::Cancelled) => Ok(SetupResult::Cancelled),
                Err(OnboardingError::NavigateBack) => {
                    // User went back from first step - show menu again
                    Box::pin(run_setup_wizard(mode)).await
                }
                Err(e) => Err(e),
            }
        }
        _ => {
            // Apply selected template
            let template_id = choice.template_id().unwrap();
            let registry = TemplateRegistry::new();
            let template = registry.get(template_id).unwrap().clone();

            // If template requires path, prompt for it
            let custom_path = if template.requires_path {
                Some(prompt_path_for_template(&theme, &template)?)
            } else if template.default_path.is_some() {
                // Ask if user wants to customize the default path
                let customize = Confirm::with_theme(&theme)
                    .with_prompt(format!(
                        "Default path is '{}'. Would you like to customize it?",
                        template.default_path.as_ref().unwrap()
                    ))
                    .default(false)
                    .interact()
                    .map_err(|_| OnboardingError::Cancelled)?;

                if customize {
                    Some(prompt_path_for_template(&theme, &template)?)
                } else {
                    None
                }
            } else {
                None
            };

            let role = template.build_role(custom_path.as_deref());

            // Validate the role
            validate_role(&role).map_err(|errors| {
                OnboardingError::Validation(
                    errors
                        .iter()
                        .map(|e| e.to_string())
                        .collect::<Vec<_>>()
                        .join("; "),
                )
            })?;

            Ok(SetupResult::Template {
                template,
                custom_path,
                role,
            })
        }
    }
}

/// Display the quick start menu and get user selection
fn quick_start_menu(theme: &ColorfulTheme) -> Result<QuickStartChoice, OnboardingError> {
    let choices = QuickStartChoice::all();
    let display_names: Vec<&str> = choices.iter().map(|c| c.display_name()).collect();

    println!("Select a quick-start template or create a custom configuration:");
    println!();

    let selection = Select::with_theme(theme)
        .items(&display_names)
        .default(0)
        .interact()
        .map_err(|_| OnboardingError::Cancelled)?;

    Ok(choices[selection])
}

/// Prompt user for a path when template requires it
fn prompt_path_for_template(
    theme: &ColorfulTheme,
    template: &ConfigTemplate,
) -> Result<String, OnboardingError> {
    use dialoguer::Input;

    let prompt_text = match template.id.as_str() {
        "local-notes" => "Enter the path to your notes folder",
        "llm-enforcer" => "Enter the path to your knowledge graph folder",
        _ => "Enter the path",
    };

    let default = template.default_path.clone().unwrap_or_default();

    let path: String = Input::with_theme(theme)
        .with_prompt(prompt_text)
        .default(default)
        .interact_text()
        .map_err(|_| OnboardingError::Cancelled)?;

    // Expand tilde and validate path exists
    let expanded = super::validation::expand_tilde(&path);

    if !super::validation::path_exists(&path) {
        // Path doesn't exist - ask user what to do
        println!();
        println!("Warning: Path '{}' does not exist.", expanded);

        let proceed = Confirm::with_theme(theme)
            .with_prompt("Would you like to use this path anyway?")
            .default(false)
            .interact()
            .map_err(|_| OnboardingError::Cancelled)?;

        if !proceed {
            return Err(OnboardingError::PathNotFound(expanded));
        }
    }

    Ok(path)
}

/// Run the full custom configuration wizard
fn custom_wizard(theme: &ColorfulTheme) -> Result<Role, OnboardingError> {
    println!();
    println!("Custom Role Configuration");
    println!("-------------------------");
    println!("Press Ctrl+C at any time to cancel.");
    println!();

    // Step 1: Role basics (name and shortname)
    let (name, shortname) = match prompt_role_basics()? {
        PromptResult::Value(v) => v,
        PromptResult::Back => return Err(OnboardingError::NavigateBack),
    };

    let mut role = Role::new(name);
    role.shortname = shortname;

    // Step 2: Theme selection
    role.theme = match prompt_theme()? {
        PromptResult::Value(t) => t,
        PromptResult::Back => {
            // Go back to role basics - restart wizard
            return Err(OnboardingError::NavigateBack);
        }
    };

    // Step 3: Relevance function
    let relevance = match prompt_relevance_function()? {
        PromptResult::Value(r) => r,
        PromptResult::Back => {
            // Go back - restart wizard
            return Err(OnboardingError::NavigateBack);
        }
    };
    role.relevance_function = relevance;
    // Set terraphim_it based on relevance function (TerraphimGraph requires it)
    role.terraphim_it = matches!(relevance, RelevanceFunction::TerraphimGraph);

    // Step 4: Haystacks
    role.haystacks = match prompt_haystacks()? {
        PromptResult::Value(haystacks) => haystacks,
        PromptResult::Back => {
            return Err(OnboardingError::NavigateBack);
        }
    };

    // Step 5: LLM configuration (optional)
    match prompt_llm_config()? {
        PromptResult::Value(llm_config) => {
            if let Some(provider) = llm_config.provider {
                role.llm_enabled = true;
                role.extra.insert(
                    "llm_provider".to_string(),
                    serde_json::Value::String(provider),
                );
                if let Some(model) = llm_config.model {
                    role.extra
                        .insert("ollama_model".to_string(), serde_json::Value::String(model));
                }
                if let Some(base_url) = llm_config.base_url {
                    role.extra.insert(
                        "ollama_base_url".to_string(),
                        serde_json::Value::String(base_url),
                    );
                }
                if let Some(api_key) = llm_config.api_key {
                    role.extra.insert(
                        "openrouter_api_key".to_string(),
                        serde_json::Value::String(api_key),
                    );
                }
            } else {
                role.llm_enabled = false;
            }
        }
        PromptResult::Back => {
            return Err(OnboardingError::NavigateBack);
        }
    }

    // Step 6: Knowledge graph (optional)
    role.kg = match prompt_knowledge_graph()? {
        PromptResult::Value(kg) => kg,
        PromptResult::Back => {
            return Err(OnboardingError::NavigateBack);
        }
    };

    // Validate the complete role
    validate_role(&role).map_err(|errors| {
        OnboardingError::Validation(
            errors
                .iter()
                .map(|e| e.to_string())
                .collect::<Vec<_>>()
                .join("; "),
        )
    })?;

    // Show summary and confirm
    println!();
    println!("Role Configuration Summary");
    println!("--------------------------");
    println!("Name: {}", role.name);
    if let Some(ref short) = role.shortname {
        println!("Shortname: {}", short);
    }
    println!("Theme: {}", role.theme);
    println!("Relevance: {:?}", role.relevance_function);
    println!("Haystacks: {}", role.haystacks.len());
    println!("LLM Enabled: {}", role.llm_enabled);
    println!(
        "Knowledge Graph: {}",
        if role.kg.is_some() { "Yes" } else { "No" }
    );
    println!();

    let confirm = Confirm::with_theme(theme)
        .with_prompt("Save this configuration?")
        .default(true)
        .interact()
        .map_err(|_| OnboardingError::Cancelled)?;

    if confirm {
        Ok(role)
    } else {
        Err(OnboardingError::Cancelled)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quick_start_choice_template_ids() {
        assert_eq!(
            QuickStartChoice::TerraphimEngineer.template_id(),
            Some("terraphim-engineer")
        );
        assert_eq!(
            QuickStartChoice::LlmEnforcer.template_id(),
            Some("llm-enforcer")
        );
        assert_eq!(QuickStartChoice::Custom.template_id(), None);
    }

    #[test]
    fn test_quick_start_choice_all() {
        let choices = QuickStartChoice::all();
        assert_eq!(choices.len(), 7);
        assert_eq!(choices[0], QuickStartChoice::TerraphimEngineer);
        assert_eq!(choices[1], QuickStartChoice::LlmEnforcer);
        assert_eq!(choices[6], QuickStartChoice::Custom);
    }

    #[test]
    fn test_apply_template_terraphim_engineer() {
        let role = apply_template("terraphim-engineer", None).unwrap();
        assert_eq!(role.name.to_string(), "Terraphim Engineer");
        assert!(role.kg.is_some());
    }

    #[test]
    fn test_apply_template_with_custom_path() {
        let role = apply_template("terraphim-engineer", Some("/custom/path")).unwrap();
        assert_eq!(role.haystacks[0].location, "/custom/path");
    }

    #[test]
    fn test_apply_template_not_found() {
        let result = apply_template("nonexistent", None);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            OnboardingError::TemplateNotFound(_)
        ));
    }

    #[test]
    fn test_apply_template_requires_path() {
        let result = apply_template("local-notes", None);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            OnboardingError::Validation(_)
        ));
    }

    #[test]
    fn test_apply_template_local_notes_with_path() {
        let role = apply_template("local-notes", Some("/my/notes")).unwrap();
        assert_eq!(role.name.to_string(), "Local Notes");
        assert_eq!(role.haystacks[0].location, "/my/notes");
    }
}