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
//! Command-line interface definitions for `ragcli`.
use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
/// PDF extraction backend used during indexing.
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
pub enum PdfParserArg {
Native,
Liteparse,
}
/// Retrieval mode used during querying.
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
pub enum QueryModeArg {
Naive,
Hybrid,
Agentic,
Local,
Global,
Mix,
}
/// Top-level CLI arguments.
#[derive(Parser, Debug)]
#[command(name = "ragcli", about = "RAG CLI powered by Ollama.")]
pub struct Cli {
/// Selects the store under `~/.config/ragcli` to operate on.
#[arg(long, global = true)]
pub name: Option<String>,
/// Runs one of the supported subcommands.
#[command(subcommand)]
pub command: Command,
}
/// Supported `ragcli` subcommands.
#[derive(Subcommand, Debug)]
pub enum Command {
/// Indexes a file, directory, or HTTP(S) URL into the local store.
Index {
/// Path to a file, directory, or HTTP(S) URL to index.
path: PathBuf,
/// Overrides the chunk size in characters.
#[arg(long)]
chunk_size: Option<usize>,
/// Overrides the chunk overlap in characters.
#[arg(long)]
chunk_overlap: Option<usize>,
/// Overrides the embedding model name.
#[arg(long)]
embed_model: Option<String>,
/// Selects the PDF parser used for `.pdf` inputs.
#[arg(long, value_enum)]
pdf_parser: Option<PdfParserArg>,
/// Excludes files or directories matching the provided glob.
#[arg(long = "exclude")]
exclude: Vec<String>,
/// Includes hidden files and directories during traversal.
#[arg(long, default_value_t = false)]
include_hidden: bool,
/// Re-embeds files even when their stored fingerprint is unchanged.
#[arg(long, default_value_t = false)]
force: bool,
},
/// Queries the local store and generates an answer.
Query {
/// Natural-language question to ask.
question: String,
/// Retrieval mode used for the query path.
#[arg(long, default_value = "hybrid")]
mode: QueryModeArg,
/// Number of results to retrieve before generation.
#[arg(long, default_value_t = 8)]
top_k: usize,
/// Number of candidates to overfetch before later pruning or reranking.
#[arg(long, default_value_t = 20)]
fetch_k: usize,
/// Maximum number of Ralph-style retrieval iterations.
#[arg(long, default_value_t = 2)]
max_iterations: usize,
/// Enables retrieval-oriented query rewriting.
#[arg(long, default_value_t = false)]
rewrite: bool,
/// Enables reranking after retrieval.
#[arg(long, default_value_t = false)]
rerank: bool,
/// Prints retrieved context snippets before answering.
#[arg(long, default_value_t = false)]
show_context: bool,
/// Prints the selected retrieval plan.
#[arg(long, default_value_t = false)]
show_plan: bool,
/// Prints retrieval scores for selected contexts.
#[arg(long, default_value_t = false)]
show_scores: bool,
/// Prints selected evidence sources and labels.
#[arg(long, default_value_t = false)]
show_citations: bool,
/// Prints per-iteration retrieval trace details.
#[arg(long, default_value_t = false)]
show_trace: bool,
/// Restricts retrieval to a single indexed source path.
#[arg(long)]
source: Option<String>,
/// Restricts retrieval to indexed source paths under the given prefix.
#[arg(long)]
path_prefix: Option<String>,
/// Restricts retrieval to a specific page number.
#[arg(long)]
page: Option<i32>,
/// Restricts retrieval to a specific indexed content format.
#[arg(long)]
format: Option<String>,
/// Overrides the generation model name.
#[arg(long)]
gen_model: Option<String>,
/// Maximum number of tokens to generate.
#[arg(long, default_value_t = 256)]
max_tokens: usize,
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
/// Shows or updates configuration values.
Config {
/// Configuration subcommand to run.
#[command(subcommand)]
command: ConfigCommand,
},
/// Lists indexed source paths together with per-source metadata.
#[command(visible_alias = "ls")]
Sources {
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
/// Removes a single indexed source path from the store.
Delete {
/// Indexed source path to remove.
path: String,
},
/// Removes all indexed content from the selected store.
Clear {
/// Confirms the destructive clear operation.
#[arg(long, default_value_t = false)]
yes: bool,
},
/// Removes indexed sources whose files no longer exist on disk.
Prune {
/// Applies the prune instead of printing a preview.
#[arg(long, default_value_t = false)]
apply: bool,
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
/// Shows a summary of indexed content and store usage.
Stat {
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
/// Checks store layout and runtime dependencies.
Doctor {
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
}
/// Configuration subcommands.
#[derive(Subcommand, Debug)]
pub enum ConfigCommand {
/// Prints the effective configuration for the selected store.
Show {
/// Prints machine-readable JSON instead of the default text report.
#[arg(long, default_value_t = false)]
json: bool,
},
/// Sets a config key in `~/.config/ragcli/<name>/config.toml`.
Set {
/// Configuration key such as `models.embed` or `ollama.base_url`.
key: String,
/// New value to write.
value: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_mode_and_inspection_flags_parse() {
let cli = Cli::try_parse_from([
"ragcli",
"query",
"What is this?",
"--mode",
"agentic",
"--rewrite",
"--rerank",
"--show-plan",
"--show-scores",
"--show-citations",
"--show-trace",
"--fetch-k",
"12",
"--max-iterations",
"4",
])
.unwrap();
match cli.command {
Command::Query {
question,
mode,
rewrite,
rerank,
show_plan,
show_scores,
show_citations,
show_trace,
fetch_k,
max_iterations,
..
} => {
assert_eq!(question, "What is this?");
assert_eq!(mode, QueryModeArg::Agentic);
assert!(rewrite);
assert!(rerank);
assert!(show_plan);
assert!(show_scores);
assert!(show_citations);
assert!(show_trace);
assert_eq!(fetch_k, 12);
assert_eq!(max_iterations, 4);
}
command => panic!("expected query command, got {command:?}"),
}
}
#[test]
fn test_query_mode_defaults_to_hybrid() {
let cli = Cli::try_parse_from(["ragcli", "query", "What is this?"]).unwrap();
match cli.command {
Command::Query {
mode,
fetch_k,
max_iterations,
..
} => {
assert_eq!(mode, QueryModeArg::Hybrid);
assert_eq!(fetch_k, 20);
assert_eq!(max_iterations, 2);
}
command => panic!("expected query command, got {command:?}"),
}
}
#[test]
fn test_index_force_flag_parses() {
let cli = Cli::try_parse_from(["ragcli", "index", "./docs", "--force"]).unwrap();
match cli.command {
Command::Index { path, force, .. } => {
assert_eq!(path, PathBuf::from("./docs"));
assert!(force);
}
command => panic!("expected index command, got {command:?}"),
}
}
#[test]
fn test_store_management_commands_parse() {
let sources = Cli::try_parse_from(["ragcli", "ls", "--json"]).unwrap();
match sources.command {
Command::Sources { json } => assert!(json),
command => panic!("expected sources command, got {command:?}"),
}
let delete = Cli::try_parse_from(["ragcli", "delete", "docs/a.md"]).unwrap();
match delete.command {
Command::Delete { path } => assert_eq!(path, "docs/a.md"),
command => panic!("expected delete command, got {command:?}"),
}
let clear = Cli::try_parse_from(["ragcli", "clear", "--yes"]).unwrap();
match clear.command {
Command::Clear { yes } => assert!(yes),
command => panic!("expected clear command, got {command:?}"),
}
let prune = Cli::try_parse_from(["ragcli", "prune", "--apply", "--json"]).unwrap();
match prune.command {
Command::Prune { apply, json } => {
assert!(apply);
assert!(json);
}
command => panic!("expected prune command, got {command:?}"),
}
}
#[test]
fn test_json_flags_parse_for_supported_commands() {
let stat = Cli::try_parse_from(["ragcli", "stat", "--json"]).unwrap();
match stat.command {
Command::Stat { json } => assert!(json),
command => panic!("expected stat command, got {command:?}"),
}
let doctor = Cli::try_parse_from(["ragcli", "doctor", "--json"]).unwrap();
match doctor.command {
Command::Doctor { json } => assert!(json),
command => panic!("expected doctor command, got {command:?}"),
}
let config = Cli::try_parse_from(["ragcli", "config", "show", "--json"]).unwrap();
match config.command {
Command::Config {
command: ConfigCommand::Show { json },
} => assert!(json),
command => panic!("expected config show command, got {command:?}"),
}
let query = Cli::try_parse_from(["ragcli", "query", "test", "--json"]).unwrap();
match query.command {
Command::Query { json, .. } => assert!(json),
command => panic!("expected query command, got {command:?}"),
}
}
}