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
use anyhow::Result;
use clap::{Parser, Subcommand};
use fastembed::TextEmbedding;
use semlith::{Semlith, default_store_dir, embed, embed::Model, filter::Filter};
use std::io::{IsTerminal, Write};
use std::path::PathBuf;
use std::time::Instant;
/// A local semantic cache for AI agents.
#[derive(Parser)]
#[command(name = "semlith", version, about, long_about = None)]
struct Cli {
/// Store directory (also settable with SEMLITH_STORE).
#[arg(long, short, global = true)]
store: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Index files and directories. Re-running only re-embeds what changed.
Index {
/// Paths to index. Defaults to the current directory.
paths: Vec<PathBuf>,
/// Embedding model, used only when creating a new store.
#[arg(long, short)]
model: Option<String>,
/// Suppress per-file output and download progress.
#[arg(long, short)]
quiet: bool,
},
/// Keep the store current: re-embed files as they are saved. Runs until
/// interrupted, and holds the store's write lock while it does.
Watch {
/// Paths to watch. Defaults to the current directory.
paths: Vec<PathBuf>,
/// Quiet period in milliseconds after the last change before
/// re-embedding, so one editor save costs one re-embed.
#[arg(long, default_value_t = semlith::watch::DEBOUNCE.as_millis() as u64)]
debounce: u64,
/// Suppress per-file output and download progress.
#[arg(long, short)]
quiet: bool,
},
/// Search the store.
Search {
query: String,
/// Number of results.
#[arg(long, short, default_value_t = 8)]
k: usize,
/// Only search files matching this glob. Repeatable; a relative
/// pattern matches anywhere in the tree.
#[arg(long, short)]
path: Vec<String>,
/// Only search files with this extension. Repeatable.
#[arg(long, short)]
ext: Vec<String>,
/// Only search files of this language. Repeatable; see `semlith languages`.
#[arg(long, short)]
lang: Vec<String>,
/// Emit JSON instead of formatted text.
#[arg(long)]
json: bool,
},
/// Show what the store contains.
Stats,
/// List the files currently indexed.
Files,
/// Remove a file from the store.
Forget { path: PathBuf },
/// Run as an MCP server over stdio, for agents to call as a tool.
Mcp,
/// List available embedding models.
Models,
/// List the language names `--lang` accepts, and their extensions.
Languages,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let dir = cli.store.clone().unwrap_or_else(default_store_dir);
match cli.command {
Command::Models => {
// The default is listed first and separately: it is not one of
// fastembed's built-ins, so it never appears in their list.
println!(
"{:<44} {:>5} dim default. IBM Granite R2 small, int8, English",
embed::GRANITE_NAME,
384
);
for info in TextEmbedding::list_supported_models() {
println!(
"{:<44} {:>5} dim {}",
info.model, info.dim, info.description
);
}
}
Command::Index {
paths,
model,
quiet,
} => {
let model = model
.map(|m| m.parse::<Model>().map_err(anyhow::Error::msg))
.transpose()?;
let mut store = Semlith::open(&dir, model)?;
store.quiet = quiet;
let roots = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths
};
let started = Instant::now();
let report = store.index_paths(&roots, |path| {
if !quiet {
eprintln!(" + {}", display(path));
}
})?;
let (files, chunks, bytes) = store.stats()?;
eprintln!(
"indexed {} files ({} chunks) in {:.1}s — {} unchanged, {} skipped, {} removed",
report.indexed,
report.chunks,
started.elapsed().as_secs_f32(),
report.unchanged,
report.skipped,
report.removed,
);
eprintln!(
"store: {files} files, {chunks} chunks, {} at {}",
semlith::human_bytes(bytes),
dir.display()
);
}
Command::Watch {
paths,
debounce,
quiet,
} => {
let roots = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths
};
let mut store = Semlith::open(&dir, None)?;
store.quiet = quiet;
// Installed before the first event: Ctrl-C is how this command
// ends, so it has to leave the store whole.
semlith::watch::stop_on_signal();
let shown: Vec<String> = roots.iter().map(|r| display(r)).collect();
semlith::watch::run(
&mut store,
&roots,
std::time::Duration::from_millis(debounce),
&semlith::watch::STOP,
|progress| {
use semlith::watch::Progress;
match progress {
// A watcher that has quietly stopped updating looks
// exactly like a corpus nobody edited, so it says what
// it is watching and what it did.
Progress::Ready {
catch_up,
files,
chunks,
} => eprintln!(
"watching {} — {files} files, {chunks} chunks \
({} indexed at startup, {} unchanged)",
shown.join(", "),
catch_up.indexed,
catch_up.unchanged,
),
Progress::File(path) if !quiet => eprintln!(" ~ {}", display(path)),
Progress::Batch(report, elapsed) if !quiet => eprintln!(
" {} re-embedded, {} removed, {} chunks in {:.1}s",
report.indexed,
report.removed,
report.chunks,
elapsed.as_secs_f32(),
),
Progress::Error(e) => eprintln!("semlith: watch error: {e}"),
_ => {}
}
},
)?;
}
Command::Search {
query,
k,
path,
ext,
lang,
json,
} => {
// Built before the store is opened, so an unknown language name
// fails immediately rather than after a model load.
let filter = Filter::new(&path, &ext, &lang)?;
let mut store = Semlith::open(&dir, None)?;
store.quiet = json;
// A glob that selects nothing is a different answer from a corpus
// that does not discuss the query, and only one of them is the
// user's typo.
let selected = (!filter.is_empty())
.then(|| store.matching_files(&filter))
.transpose()?;
if selected == Some(0) {
if json {
println!("[]");
} else {
eprintln!(
"no files match the filter (store has {} chunks)",
store.len()
);
}
return Ok(());
}
let started = Instant::now();
let hits = store.search_filtered(&query, k, &filter)?;
let elapsed = started.elapsed();
if json {
println!("{}", serde_json::to_string_pretty(&hits)?);
} else if hits.is_empty() {
eprintln!("no matches (store has {} chunks)", store.len());
} else {
let mut out = std::io::stdout().lock();
for (i, h) in hits.iter().enumerate() {
writeln!(
out,
"{}{}. {:.3} {}:{}-{}{}",
bold(),
i + 1,
h.score,
display(std::path::Path::new(&h.path)),
h.start_line,
h.end_line,
reset()
)?;
for line in h.text.lines() {
writeln!(out, " {line}")?;
}
writeln!(out)?;
}
match selected {
Some(n) => eprintln!(
"{} hits in {:?} (filter selected {n} of {} files)",
hits.len(),
elapsed,
store.stats()?.0
),
None => eprintln!("{} hits in {:?}", hits.len(), elapsed),
}
}
}
Command::Languages => {
for (name, exts) in semlith::filter::LANGUAGES {
println!(
"{name:<12} {}",
exts.iter()
.map(|e| format!(".{e}"))
.collect::<Vec<_>>()
.join(" ")
);
}
}
Command::Stats => {
let store = Semlith::open(&dir, None)?;
let (files, chunks, bytes) = store.stats()?;
println!("store {}", dir.display());
println!("model {} ({} dim)", store.model(), store.dim());
println!("files {files}");
println!("chunks {chunks}");
println!("vectors {}", store.len());
println!("indexed {}", semlith::human_bytes(bytes));
}
Command::Files => {
let store = Semlith::open(&dir, None)?;
for path in semlith::store::all_paths(store.db())? {
println!("{}", display(std::path::Path::new(&path)));
}
}
Command::Forget { path } => {
let mut store = Semlith::open(&dir, None)?;
let n = store.forget(&path)?;
eprintln!("removed {n} chunks for {}", path.display());
}
Command::Mcp => {
let mut store = Semlith::open(&dir, None)?;
// Load the model before the first tool call so an agent does not
// sit through a cold start mid-conversation.
store.quiet = true;
store.warm()?;
semlith::mcp::serve(
&mut store,
std::io::stdin().lock(),
std::io::stdout().lock(),
)?;
}
}
Ok(())
}
/// Paths are stored absolute; show them relative to the cwd when possible,
/// which is both shorter and directly usable as an editor target.
fn display(path: &std::path::Path) -> String {
let cwd = std::env::current_dir().unwrap_or_default();
path.strip_prefix(&cwd)
.unwrap_or(path)
.display()
.to_string()
}
fn bold() -> &'static str {
if std::io::stdout().is_terminal() {
"\x1b[1m"
} else {
""
}
}
fn reset() -> &'static str {
if std::io::stdout().is_terminal() {
"\x1b[0m"
} else {
""
}
}