terraphim-cli 1.0.0

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
# terraphim-cli

[![Crates.io](https://img.shields.io/crates/v/terraphim-cli.svg)](https://crates.io/crates/terraphim-cli)
[![License](https://img.shields.io/crates/l/terraphim-cli.svg)](https://github.com/terraphim/terraphim-ai/blob/main/LICENSE-Apache-2.0)

**Automation-friendly CLI for semantic knowledge graph search with JSON output.**

## Overview

`terraphim-cli` is a non-interactive command-line tool designed for scripting and automation. It provides the same semantic search capabilities as `terraphim-repl` but optimized for:

- **JSON Output**: Machine-readable output for scripts and pipelines
- **Exit Codes**: Proper exit codes (0 = success, 1 = error) for automation
- **Shell Completions**: Auto-completion for bash, zsh, and fish
- **Piping**: Works seamlessly in Unix pipelines

## Installation

### From crates.io

```bash
cargo install terraphim-cli
```

### From Source

```bash
git clone https://github.com/terraphim/terraphim-ai
cd terraphim-ai
cargo build --release -p terraphim-cli
./target/release/terraphim-cli --help
```

## Quick Start

### Basic Search

```bash
# Search with JSON output
terraphim-cli search "rust async programming"
```

**Output:**
```json
{
  "query": "rust async programming",
  "role": "Default",
  "results": [
    {
      "id": "doc1",
      "title": "Async Programming in Rust",
      "url": "https://rust-lang.github.io/async-book/",
      "rank": 0.95
    }
  ],
  "count": 1
}
```

### Pretty JSON

```bash
terraphim-cli --format json-pretty search "tokio"
```

### Pipe to jq

```bash
terraphim-cli search "rust" | jq '.results[] | .title'
```

## Commands

### search - Search Documents

```bash
terraphim-cli search <QUERY> [OPTIONS]

Options:
  --role <ROLE>      Role to use for search
  -n, --limit <N>    Maximum number of results
```

**Examples:**
```bash
# Basic search
terraphim-cli search "knowledge graph"

# With role and limit
terraphim-cli search "async" --role Engineer --limit 5

# Extract titles only
terraphim-cli search "rust" | jq -r '.results[].title'
```

---

### config - Show Configuration

```bash
terraphim-cli config
```

**Output:**
```json
{
  "selected_role": "Default",
  "roles": ["Default", "Engineer"]
}
```

---

### roles - List Available Roles

```bash
terraphim-cli roles
```

**Output:**
```json
["Default", "Engineer", "SystemOps"]
```

---

### graph - Show Top Concepts

```bash
terraphim-cli graph [OPTIONS]

Options:
  -k, --top-k <K>    Number of concepts [default: 10]
  --role <ROLE>      Role to use
```

**Example:**
```bash
terraphim-cli graph --top-k 20 --role Engineer
```

**Output:**
```json
{
  "role": "Engineer",
  "top_k": 20,
  "concepts": [
    "rust programming language",
    "async programming",
    "tokio runtime",
    ...
  ]
}
```

---

### replace - Replace Terms with Links

```bash
terraphim-cli replace <TEXT> [OPTIONS]

Options:
  --format <FORMAT>  Output format: markdown, html, wiki, plain [default: markdown]
  --role <ROLE>      Role to use
```

**Examples:**
```bash
# Markdown links (default)
terraphim-cli replace "check out rust async programming"

# HTML links
terraphim-cli replace "rust and tokio" --format html

# Wiki links
terraphim-cli replace "learn rust" --format wiki
```

**Output:**
```json
{
  "original": "check out rust async programming",
  "replaced": "check out [rust](https://rust-lang.org) [async](https://rust-lang.github.io/async-book/) programming",
  "format": "markdown"
}
```

---

### find - Find Matched Terms

```bash
terraphim-cli find <TEXT> [OPTIONS]

Options:
  --role <ROLE>  Role to use
```

**Example:**
```bash
terraphim-cli find "rust async and tokio are great"
```

**Output:**
```json
{
  "text": "rust async and tokio are great",
  "matches": [
    {
      "term": "rust",
      "position": [0, 4],
      "normalized": "rust programming language"
    },
    {
      "term": "async",
      "position": [5, 10],
      "normalized": "asynchronous programming"
    },
    {
      "term": "tokio",
      "position": [15, 20],
      "normalized": "tokio async runtime"
    }
  ],
  "count": 3
}
```

---

### thesaurus - Show Knowledge Graph Terms

```bash
terraphim-cli thesaurus [OPTIONS]

Options:
  --role <ROLE>      Role to use
  --limit <LIMIT>    Maximum terms to show [default: 50]
```

**Example:**
```bash
terraphim-cli thesaurus --role Engineer --limit 10
```

**Output:**
```json
{
  "role": "Engineer",
  "name": "engineer_thesaurus",
  "terms": [
    {
      "id": 1,
      "term": "rust",
      "normalized": "rust programming language",
      "url": "https://rust-lang.org"
    },
    ...
  ],
  "total_count": 150,
  "shown_count": 10
}
```

---

### completions - Generate Shell Completions

```bash
terraphim-cli completions <SHELL>

Shells: bash, zsh, fish, powershell
```

**Install Completions:**

**Bash:**
```bash
terraphim-cli completions bash > ~/.local/share/bash-completion/completions/terraphim-cli
```

**Zsh:**
```bash
terraphim-cli completions zsh > ~/.zfunc/_terraphim-cli
```

**Fish:**
```bash
terraphim-cli completions fish > ~/.config/fish/completions/terraphim-cli.fish
```

---

## Global Options

```bash
--format <FORMAT>   Output format: json, json-pretty, text [default: json]
--quiet             Suppress non-JSON output (errors, warnings)
--help              Print help
--version           Print version
```

## Exit Codes

- `0` - Success
- `1` - Error (invalid input, service failure, etc.)

## Scripting Examples

### Search and Extract URLs

```bash
terraphim-cli search "rust documentation" | jq -r '.results[].url'
```

### Count Results

```bash
terraphim-cli search "async" | jq '.count'
```

### Filter by Rank

```bash
terraphim-cli search "rust" | jq '.results[] | select(.rank > 0.8)'
```

### Loop Through Results

```bash
terraphim-cli search "tokio" | jq -r '.results[] | "\(.title): \(.url)"' | while read line; do
  echo "Found: $line"
done
```

### Replace Text in Files

```bash
cat input.md | while read line; do
  terraphim-cli replace "$line" --format markdown | jq -r '.replaced'
done > output.md
```

### Check if Terms Exist

```bash
if terraphim-cli find "rust tokio" | jq '.count > 0'; then
  echo "Found rust and tokio in knowledge graph"
fi
```

## CI/CD Integration

### GitHub Actions

```yaml
- name: Search Knowledge Graph
  run: |
    cargo install terraphim-cli
    terraphim-cli search "deployment" --limit 10 > results.json

- name: Validate Results
  run: |
    COUNT=$(jq '.count' results.json)
    if [ "$COUNT" -eq 0 ]; then
      echo "No results found"
      exit 1
    fi
```

### Shell Scripts

```bash
#!/bin/bash
set -e

# Search for specific terms
RESULTS=$(terraphim-cli search "api documentation" --limit 5)

# Check if we got results
if [ "$(echo $RESULTS | jq '.count')" -eq 0 ]; then
  echo "Error: No documentation found"
  exit 1
fi

# Extract URLs and fetch them
echo $RESULTS | jq -r '.results[].url' | xargs -I {} curl -s {}
```

## Differences from terraphim-repl

| Feature | terraphim-cli | terraphim-repl |
|---------|---------------|----------------|
| **Mode** | Non-interactive | Interactive |
| **Output** | JSON | Pretty tables + colored |
| **Use Case** | Automation/scripts | Human interaction |
| **Exit Codes** | Proper (0/1) | N/A |
| **Completions** | Yes (bash/zsh/fish) | Command completion in REPL |
| **Piping** | Designed for it | N/A |
| **History** | No | Yes |

Use `terraphim-cli` when:
- Writing scripts or automation
- Integrating with other tools via JSON
- CI/CD pipelines
- Batch processing
- Need machine-readable output

Use `terraphim-repl` when:
- Interactive exploration
- Learning the system
- Ad-hoc queries
- Human-readable output preferred

## Configuration

Uses the same configuration as `terraphim-repl`:
- `~/.terraphim/config.json` - Main configuration
- Supports role-based search
- Works offline with embedded defaults

## Troubleshooting

### Command Not Found

```bash
# Make sure cargo bin is in PATH
export PATH="$HOME/.cargo/bin:$PATH"
```

### JSON Parsing Errors

```bash
# Use --quiet to suppress non-JSON output
terraphim-cli --quiet search "query" | jq '.'
```

### Completions Not Working

```bash
# Bash: Check completion directory
ls ~/.local/share/bash-completion/completions/

# Zsh: Check fpath includes ~/.zfunc
echo $fpath

# Fish: Check completions directory
ls ~/.config/fish/completions/
```

## Building from Source

```bash
# Debug build
cargo build -p terraphim-cli

# Release build (optimized)
cargo build --release -p terraphim-cli

# Run tests
cargo test -p terraphim-cli

# Generate docs
cargo doc -p terraphim-cli --open
```

## Related Projects

- **[terraphim-repl]../terraphim_repl**: Interactive REPL interface
- **[terraphim_types]../terraphim_types**: Core type definitions
- **[terraphim_automata]../terraphim_automata**: Text matching engine
- **[terraphim_rolegraph]../terraphim_rolegraph**: Knowledge graph implementation

## Support

- **Discord**: https://discord.gg/VPJXB6BGuY
- **Discourse**: https://terraphim.discourse.group
- **Issues**: https://github.com/terraphim/terraphim-ai/issues

## License

Licensed under Apache-2.0. See [LICENSE](../../LICENSE-Apache-2.0) for details.

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for version history.