terraphim_agent 1.16.30

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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
use anyhow::Result;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use terraphim_types::{Document, SearchQuery};

#[derive(Clone, Debug)]
pub struct ApiClient {
    http: Client,
    base: String,
}

impl ApiClient {
    pub fn new(base_url: impl Into<String>) -> Self {
        // Use longer timeout for CI/CD environments where search may be slow
        let timeout_secs: u64 = match std::env::var("TERRAPHIM_CLIENT_TIMEOUT") {
            Ok(v) => v.parse().unwrap_or(60),
            Err(_) => 30,
        };
        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(timeout_secs))
            .user_agent("Terraphim-TUI/1.0")
            .build()
            .unwrap_or_else(|_| Client::new());

        Self {
            http: client,
            base: base_url.into(),
        }
    }

    #[allow(dead_code)]
    pub async fn health(&self) -> Result<()> {
        let url = format!("{}/health", self.base);
        let res = self.http.get(url).send().await?;
        if res.status().is_success() {
            Ok(())
        } else {
            anyhow::bail!("health check failed")
        }
    }

    pub async fn get_config(&self) -> Result<ConfigResponse> {
        let url = format!("{}/config", self.base);
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<ConfigResponse>().await?;
        Ok(body)
    }

    /// Resolve a role string (name or shortname) to a RoleName using server config.
    /// Falls back to RoleName::new if no match found (server will validate).
    pub async fn resolve_role(&self, role: &str) -> Result<terraphim_types::RoleName> {
        use terraphim_types::RoleName;
        let config_res = self.get_config().await?;
        let role_lower = role.to_lowercase();
        let selected = &config_res.config.selected_role;
        if selected.to_string().to_lowercase() == role_lower {
            return Ok(selected.clone());
        }
        for (name, role_cfg) in &config_res.config.roles {
            if name.to_string().to_lowercase() == role_lower {
                return Ok(name.clone());
            }
            if let Some(ref sn) = role_cfg.shortname {
                if sn.to_lowercase() == role_lower {
                    return Ok(name.clone());
                }
            }
        }
        Ok(RoleName::new(role))
    }

    pub async fn update_selected_role(&self, role: &str) -> Result<ConfigResponse> {
        let url = format!("{}/config/selected_role", self.base);
        #[derive(Serialize)]
        struct Payload<'a> {
            selected_role: &'a str,
        }
        let res = self
            .http
            .post(url)
            .json(&Payload {
                selected_role: role,
            })
            .send()
            .await?;
        let body = res.error_for_status()?.json::<ConfigResponse>().await?;
        Ok(body)
    }

    pub async fn post_config(&self, cfg: &terraphim_config::Config) -> Result<ConfigResponse> {
        let url = format!("{}/config", self.base);
        let res = self.http.post(url).json(cfg).send().await?;
        let body = res.error_for_status()?.json::<ConfigResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_rolegraph_edges(&self, role: Option<&str>) -> Result<RoleGraphResponseDto> {
        self.rolegraph(role).await
    }

    pub async fn search(&self, query: &SearchQuery) -> Result<SearchResponse> {
        let url = format!("{}/documents/search", self.base);
        let res = self.http.post(url).json(query).send().await?;
        let body = res.error_for_status()?.json::<SearchResponse>().await?;
        Ok(body)
    }

    pub async fn rolegraph(&self, role: Option<&str>) -> Result<RoleGraphResponseDto> {
        let url = match role {
            Some(r) if !r.is_empty() => {
                format!("{}/rolegraph?role={}", self.base, urlencoding::encode(r))
            }
            _ => format!("{}/rolegraph", self.base),
        };
        let res = self.http.get(url).send().await?;
        let body = res
            .error_for_status()?
            .json::<RoleGraphResponseDto>()
            .await?;
        Ok(body)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SearchResponse {
    pub status: String,
    pub results: Vec<Document>,
    pub total: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ConfigResponse {
    pub status: String,
    pub config: terraphim_config::Config,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GraphNodeDto {
    pub id: u64,
    pub label: String,
    pub rank: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GraphEdgeDto {
    pub source: u64,
    pub target: u64,
    pub rank: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RoleGraphResponseDto {
    pub status: String,
    pub nodes: Vec<GraphNodeDto>,
    pub edges: Vec<GraphEdgeDto>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChatMessage {
    pub role: String,
    pub content: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChatRequest {
    pub role: String,
    pub messages: Vec<ChatMessage>,
    pub model: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChatResponse {
    pub status: String,
    pub message: Option<String>,
    pub model_used: Option<String>,
    pub error: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SummarizeRequest {
    pub document: Document,
    pub role: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SummarizeResponse {
    pub status: String,
    pub summary: Option<String>,
    pub error: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ThesaurusEntry {
    pub id: String,
    pub nterm: String,
    pub url: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ThesaurusResponse {
    pub status: String,
    pub terms: Vec<ThesaurusEntry>,
    pub total: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AutocompleteSuggestion {
    pub text: String,
    pub score: f64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AutocompleteResponse {
    pub status: String,
    pub suggestions: Vec<AutocompleteSuggestion>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AsyncSummarizeResponse {
    pub status: String,
    pub task_id: String,
    pub message: Option<String>,
    pub error: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TaskStatusResponse {
    pub status: String,
    pub task_id: String,
    pub state: String, // "pending", "processing", "completed", "failed", "cancelled"
    pub progress: Option<f64>,
    pub result: Option<String>,
    pub error: Option<String>,
    pub created_at: Option<String>,
    pub updated_at: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QueueStatsResponse {
    pub status: String,
    pub pending_tasks: usize,
    pub processing_tasks: usize,
    pub completed_tasks: usize,
    pub failed_tasks: usize,
    pub total_tasks: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BatchSummarizeRequest {
    pub documents: Vec<Document>,
    pub role: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BatchSummarizeResponse {
    pub status: String,
    pub task_ids: Vec<String>,
    pub message: Option<String>,
    pub error: Option<String>,
}

// VM Management Types

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmWithIp {
    pub vm_id: String,
    pub ip_address: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmPoolListResponse {
    pub vms: Vec<VmWithIp>,
    pub stats: VmPoolStatsResponse,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmPoolStatsResponse {
    pub total_ips: usize,
    pub allocated_ips: usize,
    pub available_ips: usize,
    pub utilization_percent: u8,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmStatusResponse {
    pub vm_id: String,
    pub status: String,
    pub ip_address: String,
    pub created_at: String,
    pub updated_at: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmExecuteRequest {
    pub code: String,
    pub language: String,
    pub agent_id: String,
    pub vm_id: Option<String>,
    pub timeout_ms: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmExecuteResponse {
    pub execution_id: String,
    pub vm_id: String,
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
    pub duration_ms: u64,
    pub started_at: String,
    pub completed_at: String,
    pub error: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmTask {
    pub id: String,
    pub vm_id: String,
    pub status: String,
    pub created_at: String,
    pub updated_at: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmTasksResponse {
    pub tasks: Vec<VmTask>,
    pub vm_id: String,
    pub total: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmAllocateRequest {
    pub vm_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmAllocateResponse {
    pub vm_id: String,
    pub ip_address: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmMetricsResponse {
    pub vm_id: String,
    pub status: String,
    pub cpu_usage_percent: f64,
    pub memory_usage_mb: u32,
    pub disk_usage_percent: f64,
    pub network_io_mbps: f64,
    pub uptime_seconds: u64,
    pub process_count: u32,
    pub updated_at: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmAgentRequest {
    pub agent_id: String,
    pub task: String,
    pub vm_id: Option<String>,
    pub timeout_ms: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(dead_code)]
pub struct VmAgentResponse {
    pub task_id: String,
    pub agent_id: String,
    pub vm_id: Option<String>,
    pub status: String,
    pub result: String,
    pub duration_ms: u64,
    pub started_at: String,
    pub completed_at: String,
    pub snapshot_id: Option<String>,
    pub error: Option<String>,
}

impl ApiClient {
    pub async fn chat(
        &self,
        role: &str,
        user_message: &str,
        model: Option<&str>,
    ) -> Result<ChatResponse> {
        let url = format!("{}/chat", self.base);
        let req = ChatRequest {
            role: role.to_string(),
            messages: vec![ChatMessage {
                role: "user".into(),
                content: user_message.into(),
            }],
            model: model.map(|s| s.to_string()),
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res.error_for_status()?.json::<ChatResponse>().await?;
        Ok(body)
    }

    pub async fn summarize_document(
        &self,
        document: &Document,
        role: Option<&str>,
    ) -> Result<SummarizeResponse> {
        let url = format!("{}/documents/summarize", self.base);
        let req = SummarizeRequest {
            document: document.clone(),
            role: role.map(|r| r.to_string()),
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res.error_for_status()?.json::<SummarizeResponse>().await?;
        Ok(body)
    }

    pub async fn get_thesaurus(&self, role_name: &str) -> Result<ThesaurusResponse> {
        let url = format!("{}/thesaurus/{}", self.base, urlencoding::encode(role_name));
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<ThesaurusResponse>().await?;
        Ok(body)
    }

    pub async fn get_autocomplete(
        &self,
        role_name: &str,
        query: &str,
    ) -> Result<AutocompleteResponse> {
        let url = format!(
            "{}/autocomplete/{}/{}",
            self.base,
            urlencoding::encode(role_name),
            urlencoding::encode(query)
        );
        let res = self.http.get(url).send().await?;
        let body = res
            .error_for_status()?
            .json::<AutocompleteResponse>()
            .await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn async_summarize_document(
        &self,
        document: &Document,
        role: Option<&str>,
    ) -> Result<AsyncSummarizeResponse> {
        let url = format!("{}/documents/async_summarize", self.base);
        let req = SummarizeRequest {
            document: document.clone(),
            role: role.map(|r| r.to_string()),
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res
            .error_for_status()?
            .json::<AsyncSummarizeResponse>()
            .await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_task_status(&self, task_id: &str) -> Result<TaskStatusResponse> {
        let url = format!(
            "{}/summarization/task/{}/status",
            self.base,
            urlencoding::encode(task_id)
        );
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<TaskStatusResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn cancel_task(&self, task_id: &str) -> Result<TaskStatusResponse> {
        let url = format!(
            "{}/summarization/task/{}/cancel",
            self.base,
            urlencoding::encode(task_id)
        );
        let res = self.http.post(url).send().await?;
        let body = res.error_for_status()?.json::<TaskStatusResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_queue_stats(&self) -> Result<QueueStatsResponse> {
        let url = format!("{}/summarization/queue/stats", self.base);
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<QueueStatsResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn batch_summarize_documents(
        &self,
        documents: &[Document],
        role: Option<&str>,
    ) -> Result<BatchSummarizeResponse> {
        let url = format!("{}/summarization/batch", self.base);
        let req = BatchSummarizeRequest {
            documents: documents.to_vec(),
            role: role.map(|r| r.to_string()),
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res
            .error_for_status()?
            .json::<BatchSummarizeResponse>()
            .await?;
        Ok(body)
    }

    // VM Management APIs

    #[allow(dead_code)]
    pub async fn list_vms(&self) -> Result<VmPoolListResponse> {
        let url = format!("{}/api/vm-pool", self.base);
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<VmPoolListResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_vm_pool_stats(&self) -> Result<VmPoolStatsResponse> {
        let url = format!("{}/api/vm-pool/stats", self.base);
        let res = self.http.get(url).send().await?;
        let body = res
            .error_for_status()?
            .json::<VmPoolStatsResponse>()
            .await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_vm_status(&self, vm_id: &str) -> Result<VmStatusResponse> {
        let url = format!("{}/api/vms/{}", self.base, urlencoding::encode(vm_id));
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<VmStatusResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn execute_vm_code(
        &self,
        code: &str,
        language: &str,
        vm_id: Option<&str>,
    ) -> Result<VmExecuteResponse> {
        let url = format!("{}/api/llm/execute", self.base);
        let req = VmExecuteRequest {
            code: code.to_string(),
            language: language.to_string(),
            agent_id: "tui-user".to_string(),
            vm_id: vm_id.map(|s| s.to_string()),
            timeout_ms: Some(30000), // 30 second default timeout
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res.error_for_status()?.json::<VmExecuteResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn list_vm_tasks(&self, vm_id: &str) -> Result<VmTasksResponse> {
        let url = format!("{}/api/vms/{}/tasks", self.base, urlencoding::encode(vm_id));
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<VmTasksResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn allocate_vm_ip(&self, vm_id: &str) -> Result<VmAllocateResponse> {
        let url = format!("{}/api/vm-pool/allocate", self.base);
        let req = VmAllocateRequest {
            vm_id: vm_id.to_string(),
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res.error_for_status()?.json::<VmAllocateResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn release_vm_ip(&self, vm_id: &str) -> Result<()> {
        let url = format!(
            "{}/api/vm-pool/release/{}",
            self.base,
            urlencoding::encode(vm_id)
        );
        let res = self.http.post(url).send().await?;
        res.error_for_status()?;
        Ok(())
    }

    #[allow(dead_code)]
    pub async fn get_vm_metrics(&self, vm_id: &str) -> Result<VmMetricsResponse> {
        let url = format!(
            "{}/api/vms/{}/metrics",
            self.base,
            urlencoding::encode(vm_id)
        );
        let res = self.http.get(url).send().await?;
        let body = res.error_for_status()?.json::<VmMetricsResponse>().await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn get_all_vm_metrics(&self) -> Result<Vec<VmMetricsResponse>> {
        let url = format!("{}/api/vms/metrics", self.base);
        let res = self.http.get(url).send().await?;
        let body = res
            .error_for_status()?
            .json::<Vec<VmMetricsResponse>>()
            .await?;
        Ok(body)
    }

    #[allow(dead_code)]
    pub async fn execute_agent_task(
        &self,
        agent_id: &str,
        task: &str,
        vm_id: Option<&str>,
    ) -> Result<VmAgentResponse> {
        let url = format!("{}/api/vm/agent/execute", self.base);
        let req = VmAgentRequest {
            agent_id: agent_id.to_string(),
            task: task.to_string(),
            vm_id: vm_id.map(|s| s.to_string()),
            timeout_ms: Some(60000), // 60 second default timeout for agent tasks
        };
        let res = self.http.post(url).json(&req).send().await?;
        let body = res.error_for_status()?.json::<VmAgentResponse>().await?;
        Ok(body)
    }
}