terraphim-cli 1.20.3

CLI tool for semantic knowledge graph search with JSON output for automation
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
//! Service wrapper for CLI operations

use anyhow::Result;
use serde::Serialize;
use std::sync::Arc;
use terraphim_config::{Config, ConfigBuilder, ConfigId, ConfigState};
use terraphim_persistence::Persistable;
use terraphim_service::TerraphimService;
use terraphim_settings::{DeviceSettings, Error as DeviceSettingsError};
use terraphim_types::{
    CoverageSignal, Document, ExtractedEntity, GroundingMetadata, Layer, NormalizationMethod,
    NormalizedTerm, NormalizedTermValue, OntologySchema, RoleName, SchemaSignal, SearchQuery,
    Thesaurus,
};
use tokio::sync::Mutex;

#[derive(Clone)]
pub struct CliService {
    config_state: ConfigState,
    service: Arc<Mutex<TerraphimService>>,
}

impl CliService {
    /// Initialize a new CLI service.
    ///
    /// Config loading priority:
    /// 1. `config_path` (--config CLI flag) -- always loads from JSON, no persistence
    /// 2. `role_config` in settings.toml -- bootstrap-then-persistence (first run loads
    ///    JSON and saves to persistence; subsequent runs use persistence so CLI changes stick)
    /// 3. Persistence layer (SQLite)
    /// 4. Embedded defaults (hardcoded roles)
    pub async fn new(config_path: Option<String>) -> Result<Self> {
        // Initialize logging
        terraphim_service::logging::init_logging(
            terraphim_service::logging::detect_logging_config(),
        );

        log::info!("Initializing CLI service");

        // Priority 1: --config CLI flag (always loads from JSON, no persistence check)
        if let Some(ref path) = config_path {
            log::info!("Loading config from --config flag: '{}'", path);
            match Config::load_from_json_file(path) {
                Ok(config) => {
                    return Self::from_config(config).await;
                }
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "Failed to load config from '{}': {:?}",
                        path,
                        e
                    ));
                }
            }
        }

        // Load device settings, falling back to embedded defaults when running in sandboxes/tests
        let device_settings = match DeviceSettings::load_from_env_and_file(None) {
            Ok(settings) => settings,
            Err(DeviceSettingsError::IoError(err))
                if err.kind() == std::io::ErrorKind::NotFound =>
            {
                log::warn!(
                    "Device settings not found ({}); using embedded defaults",
                    err
                );
                DeviceSettings::default_embedded()
            }
            Err(err) => {
                log::error!("Failed to load device settings: {err:?}");
                return Err(err.into());
            }
        };
        log::debug!("Device settings: {:?}", device_settings);

        // Priority 2: role_config in settings.toml (bootstrap-then-persistence)
        if let Some(ref role_config_path) = device_settings.role_config {
            log::info!("Found role_config in settings.toml: '{}'", role_config_path);
            return Self::load_with_role_config(role_config_path, &device_settings).await;
        }

        // Priority 3 & 4: Persistence -> embedded defaults (existing behavior)
        log::debug!("No role_config specified, using persistence/embedded defaults");
        let mut config = match ConfigBuilder::new_with_id(ConfigId::Embedded).build() {
            Ok(mut config) => match config.load().await {
                Ok(config) => {
                    log::debug!("Loaded existing embedded configuration from persistence");
                    config
                }
                Err(_) => {
                    log::debug!("No saved config found, using default embedded");
                    return Self::new_with_embedded_defaults().await;
                }
            },
            Err(e) => {
                log::warn!("Failed to build config: {:?}, using default", e);
                return Self::new_with_embedded_defaults().await;
            }
        };

        Self::merge_project_config(&mut config);
        Self::from_config(config).await
    }

    /// Load config using bootstrap-then-persistence strategy.
    async fn load_with_role_config(
        role_config_path: &str,
        device_settings: &DeviceSettings,
    ) -> Result<Self> {
        // Try persistence first (preserves runtime changes like `config set`)
        if let Ok(mut empty_config) = ConfigBuilder::new_with_id(ConfigId::Embedded).build()
            && let Ok(persisted) = empty_config.load().await
            && !persisted.roles.is_empty()
        {
            log::info!(
                "Loaded {} role(s) from persistence (role_config bootstrap already done)",
                persisted.roles.len()
            );
            return Self::from_config(persisted).await;
        }

        // No persisted config -- bootstrap from JSON file
        log::info!(
            "No persisted config found, bootstrapping from role_config: '{}'",
            role_config_path
        );
        match Config::load_from_json_file(role_config_path) {
            Ok(mut config) => {
                // Apply default_role override from settings.toml
                if let Some(ref default_role) = device_settings.default_role {
                    let role_name = RoleName::new(default_role);
                    if config.roles.contains_key(&role_name) {
                        log::info!(
                            "Setting selected role to '{}' from settings.toml default_role",
                            default_role
                        );
                        config.selected_role = role_name.clone();
                        config.default_role = role_name;
                    } else {
                        log::warn!(
                            "default_role '{}' not found in role_config; available: {:?}",
                            default_role,
                            config
                                .roles
                                .keys()
                                .map(|k| k.to_string())
                                .collect::<Vec<_>>()
                        );
                    }
                }

                // Merge project config before saving
                Self::merge_project_config(&mut config);

                // Save to persistence so subsequent runs use persisted config
                if let Err(e) = config.save().await {
                    log::warn!("Failed to save bootstrapped config to persistence: {:?}", e);
                }

                Self::from_config(config).await
            }
            Err(e) => {
                log::error!(
                    "Failed to load role_config '{}': {:?}. Falling back to embedded defaults.",
                    role_config_path,
                    e
                );
                Self::new_with_embedded_defaults().await
            }
        }
    }

    /// Initialize service strictly from the embedded default configuration.
    async fn new_with_embedded_defaults() -> Result<Self> {
        let mut config = ConfigBuilder::new_with_id(ConfigId::Embedded)
            .build_default_embedded()
            .build()?;
        Self::merge_project_config(&mut config);
        Self::from_config(config).await
    }

    /// Merge project-level `.terraphim/` configuration into the global config.
    fn merge_project_config(config: &mut Config) {
        if let Ok(Some(path)) = terraphim_config::project::discover(None) {
            let project_config = terraphim_config::project::ProjectConfig::load_from_dir(&path)
                .unwrap_or_else(|_| {
                    let config_path = path.join("config.json");
                    if config_path.is_file() {
                        terraphim_config::project::ProjectConfig::from_file(&config_path)
                            .unwrap_or_default()
                    } else {
                        Default::default()
                    }
                });

            if !project_config.is_empty() {
                log::info!(
                    "Merging {} project role(s) from '{}'",
                    project_config.roles.len(),
                    path.display()
                );
                let builder = ConfigBuilder::from_config(
                    config.clone(),
                    DeviceSettings::new(),
                    std::path::PathBuf::new(),
                );
                *config = builder
                    .merge_with(&project_config)
                    .build()
                    .unwrap_or_else(|_| config.clone());

                // Override selected/default role if project config specifies one
                if let Ok(Some(role_name)) = project_config.resolve_role_name(None) {
                    let role_name = RoleName::new(&role_name);
                    if config.roles.contains_key(&role_name) {
                        log::info!(
                            "Setting selected/default role to '{}' from project config",
                            role_name
                        );
                        config.selected_role = role_name.clone();
                        config.default_role = role_name;
                    }
                }
            }
        }
    }

    async fn from_config(mut config: Config) -> Result<Self> {
        let config_state = ConfigState::new(&mut config).await?;
        let service = TerraphimService::new(config_state.clone());

        Ok(Self {
            config_state,
            service: Arc::new(Mutex::new(service)),
        })
    }

    /// Get the current configuration
    pub async fn get_config(&self) -> terraphim_config::Config {
        terraphim_command_runtime::get_config(&self.config_state).await
    }

    /// Get the current selected role
    pub async fn get_selected_role(&self) -> RoleName {
        terraphim_command_runtime::get_selected_role(&self.config_state).await
    }

    /// List all available roles
    pub async fn list_roles(&self) -> Vec<String> {
        let config = self.config_state.config.lock().await;
        config.roles.keys().map(|r| r.to_string()).collect()
    }

    /// List all available roles with their shortnames
    pub async fn list_roles_with_info(&self) -> Vec<(String, Option<String>)> {
        terraphim_command_runtime::list_roles_with_info(&self.config_state).await
    }

    /// Find a role by name or shortname (case-insensitive)
    pub async fn find_role_by_name_or_shortname(&self, query: &str) -> Option<RoleName> {
        terraphim_command_runtime::find_role_by_name_or_shortname(&self.config_state, query).await
    }

    /// Update the selected role
    pub async fn update_selected_role(&self, role_name: RoleName) -> Result<()> {
        let service = self.service.lock().await;
        service.update_selected_role(role_name).await?;
        Ok(())
    }

    /// Save the current configuration
    pub async fn save_config(&self) -> Result<()> {
        terraphim_command_runtime::save_config(&self.config_state).await
    }

    /// Search documents with full options including include_pinned
    pub async fn search_with_options(
        &self,
        search_term: &str,
        role: &RoleName,
        limit: Option<usize>,
        include_pinned: bool,
    ) -> Result<Vec<Document>> {
        let query = SearchQuery {
            search_term: NormalizedTermValue::from(search_term),
            search_terms: None,
            operator: None,
            skip: Some(0),
            limit,
            role: Some(role.clone()),
            layer: Layer::default(),
            include_pinned,
            min_quality: None,
        };

        let mut service = self.service.lock().await;
        Ok(service.search(&query).await?)
    }

    /// List KG entries for a role, optionally filtered to pinned entries only.
    ///
    /// Returns a list of `(node_id, term)` pairs. When `pinned_only` is true,
    /// only entries whose node ID appears in the role graph's pinned list are returned.
    pub async fn list_kg_entries(
        &self,
        role_name: &RoleName,
        pinned_only: bool,
    ) -> Result<Vec<serde_json::Value>> {
        if let Some(rolegraph_sync) = self.config_state.roles.get(role_name) {
            let rolegraph = rolegraph_sync.lock().await;
            let pinned_ids: std::collections::HashSet<u64> =
                rolegraph.get_pinned_node_ids().iter().copied().collect();

            let entries: Vec<serde_json::Value> = rolegraph
                .ac_reverse_nterm
                .iter()
                .filter(|(node_id, _)| !pinned_only || pinned_ids.contains(node_id))
                .map(|(node_id, term)| {
                    serde_json::json!({
                        "node_id": node_id,
                        "term": term.to_string(),
                        "pinned": pinned_ids.contains(node_id),
                    })
                })
                .collect();

            Ok(entries)
        } else {
            Ok(Vec::new())
        }
    }

    /// Get thesaurus for a specific role
    pub async fn get_thesaurus(&self, role_name: &RoleName) -> Result<Thesaurus> {
        let mut service = self.service.lock().await;
        Ok(service.ensure_thesaurus_loaded(role_name).await?)
    }

    /// Get the role graph top-k concepts for a specific role
    ///
    /// Returns the top-k concepts sorted by rank (number of co-occurrences) in descending order.
    pub async fn get_top_concepts(
        &self,
        role_name: &RoleName,
        top_k: usize,
    ) -> Result<Vec<String>> {
        log::info!("Getting top {} concepts for role {}", top_k, role_name);

        // Get the role graph for this role
        if let Some(rolegraph_sync) = self.config_state.roles.get(role_name) {
            let rolegraph = rolegraph_sync.lock().await;

            // Get nodes and sort by rank (descending)
            let mut nodes: Vec<_> = rolegraph.nodes_map().iter().collect();
            #[allow(clippy::unnecessary_sort_by)]
            nodes.sort_by(|a, b| b.1.rank.cmp(&a.1.rank));

            // Map node IDs to term names and collect top-k
            let top_concepts: Vec<String> = nodes
                .into_iter()
                .take(top_k)
                .filter_map(|(node_id, _node)| {
                    rolegraph
                        .ac_reverse_nterm
                        .get(node_id)
                        .map(|term| term.to_string())
                })
                .collect();

            log::debug!(
                "Found {} concepts for role {} (requested {})",
                top_concepts.len(),
                role_name,
                top_k
            );
            Ok(top_concepts)
        } else {
            log::warn!("Role graph not found for role {}", role_name);
            Ok(Vec::new())
        }
    }

    /// Find matches in text using thesaurus
    pub async fn find_matches(
        &self,
        role_name: &RoleName,
        text: &str,
    ) -> Result<Vec<terraphim_automata::Matched>> {
        // Get thesaurus for the role
        let thesaurus = self.get_thesaurus(role_name).await?;

        // Find matches
        Ok(terraphim_automata::find_matches(text, thesaurus, true)?)
    }

    /// Extract matches with grounding metadata
    pub async fn extract_with_grounding(
        &self,
        role_name: &RoleName,
        text: &str,
    ) -> Result<Vec<ExtractedEntity>> {
        let thesaurus = self.get_thesaurus(role_name).await?;
        let matches = terraphim_automata::find_matches(text, thesaurus, true)?;

        let entities: Vec<ExtractedEntity> = matches
            .iter()
            .map(|m| ExtractedEntity {
                entity_type: "term".to_string(),
                raw_value: m.term.clone(),
                normalized_value: Some(m.normalized_term.value.to_string()),
                grounding: Some(GroundingMetadata::new(
                    m.normalized_term
                        .url
                        .clone()
                        .unwrap_or_else(|| format!("kg://{}", m.normalized_term.value)),
                    m.normalized_term.value.to_string(),
                    "terraphim".to_string(),
                    1.0,
                    NormalizationMethod::Exact,
                )),
            })
            .collect();

        Ok(entities)
    }

    /// Extract entities using ontology schema, returning SchemaSignal
    pub fn extract_with_schema(&self, schema: &OntologySchema, text: &str) -> Result<SchemaSignal> {
        let thesaurus = Self::build_thesaurus_from_schema(schema);
        let matches = terraphim_automata::find_matches(text, thesaurus, true)?;

        // Build a lookup from NormalizedTermValue -> entity_type_id
        let entry_lookup: std::collections::HashMap<String, String> = schema
            .to_thesaurus_entries()
            .into_iter()
            .map(|(id, term, _)| (term.to_lowercase(), id))
            .collect();

        let entities: Vec<ExtractedEntity> = matches
            .iter()
            .map(|m| {
                let entity_type_id = entry_lookup
                    .get(&m.normalized_term.value.to_string())
                    .cloned()
                    .unwrap_or_else(|| "unknown".to_string());
                ExtractedEntity {
                    entity_type: entity_type_id.clone(),
                    raw_value: m.term.clone(),
                    normalized_value: Some(m.normalized_term.value.to_string()),
                    grounding: Some(GroundingMetadata::new(
                        schema
                            .uri_for(&entity_type_id)
                            .unwrap_or_else(|| format!("kg://{}", entity_type_id)),
                        m.normalized_term.value.to_string(),
                        schema.name.clone(),
                        1.0,
                        NormalizationMethod::Exact,
                    )),
                }
            })
            .collect();

        Ok(SchemaSignal {
            entities,
            relationships: Vec::new(),
            confidence: if matches.is_empty() { 0.0 } else { 1.0 },
        })
    }

    /// Build a Thesaurus from an OntologySchema's entity types and aliases
    fn build_thesaurus_from_schema(schema: &OntologySchema) -> Thesaurus {
        let entries = schema.to_thesaurus_entries();
        let mut thesaurus = Thesaurus::new(schema.name.clone());
        for (idx, (_id, term, url)) in entries.into_iter().enumerate() {
            let nterm_value = NormalizedTermValue::new(term);
            let mut nterm = NormalizedTerm::new(idx as u64, nterm_value.clone());
            if let Some(url) = url {
                nterm = nterm.with_url(url);
            }
            thesaurus.insert(nterm_value, nterm);
        }
        thesaurus
    }

    /// Calculate coverage of schema categories in text
    pub fn calculate_coverage(
        &self,
        schema: &OntologySchema,
        text: &str,
        threshold: f32,
    ) -> Result<CoverageResult> {
        let signal = self.extract_with_schema(schema, text)?;

        let all_categories = schema.category_ids();
        let matched_ids: std::collections::HashSet<String> = signal
            .entities
            .iter()
            .map(|e| e.entity_type.clone())
            .collect();

        let matched: Vec<String> = all_categories
            .iter()
            .filter(|id| matched_ids.contains(*id))
            .cloned()
            .collect();
        let missing: Vec<String> = all_categories
            .iter()
            .filter(|id| !matched_ids.contains(*id))
            .cloned()
            .collect();

        let coverage = CoverageSignal::compute(&all_categories, matched.len(), threshold);

        Ok(CoverageResult {
            signal: coverage,
            matched_categories: matched,
            missing_categories: missing,
            schema_name: schema.name.clone(),
        })
    }

    /// Replace matches in text with links using thesaurus
    pub async fn replace_matches(
        &self,
        role_name: &RoleName,
        text: &str,
        link_type: terraphim_automata::LinkType,
    ) -> Result<String> {
        // Get thesaurus for the role
        let thesaurus = self.get_thesaurus(role_name).await?;

        // Replace matches
        let result = terraphim_automata::replace_matches(text, thesaurus, link_type)?;
        Ok(String::from_utf8(result).unwrap_or_else(|_| text.to_string()))
    }
}

/// Result of ontology coverage analysis
#[derive(Debug, Clone, Serialize)]
pub struct CoverageResult {
    /// Coverage signal with ratio and threshold
    pub signal: CoverageSignal,
    /// Entity type IDs that were matched in the text
    pub matched_categories: Vec<String>,
    /// Entity type IDs that were NOT matched in the text
    pub missing_categories: Vec<String>,
    /// Name of the schema used
    pub schema_name: String,
}