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
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tokio_util::sync::CancellationToken;
use crate::embed::ModelType;
use crate::search::SearchOptions;
/// Index subcommands
#[derive(Subcommand, Debug)]
pub enum IndexCommands {
/// Add a repository to the index (creates local or global index)
Add {
/// Path to add (defaults to current directory)
path: Option<PathBuf>,
/// Create global index instead of local
#[arg(short = 'g', long)]
global: bool,
},
/// Remove the index (local or global, auto-detected)
#[command(visible_alias = "rm")]
Remove {
/// Path to remove (defaults to current directory)
path: Option<PathBuf>,
},
/// Show index status (local or global)
List,
}
/// Fast, local semantic code search powered by Rust
#[derive(Parser, Debug)]
#[command(name = "codesearch")]
#[command(author, version = env!("CARGO_PKG_VERSION_FULL"), about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// Set log level (error, warn, info, debug, trace)
#[arg(short = 'l', long, global = true, default_value = "info")]
pub loglevel: String,
/// Suppress informational output (only show results/errors)
#[arg(short, long, global = true)]
pub quiet: bool,
/// Override default store name
#[arg(long, global = true)]
pub store: Option<String>,
/// Embedding model to use (e.g., bge-small, minilm-l6-q, jina-code)
/// Available: minilm-l6, minilm-l6-q, minilm-l12, minilm-l12-q, paraphrase-minilm,
/// bge-small, bge-small-q, bge-base, nomic-v1, nomic-v1.5, nomic-v1.5-q,
/// jina-code, e5-multilingual, mxbai-large, modernbert-large
#[arg(long, global = true)]
pub model: Option<String>,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Search the codebase using natural language
Search {
/// Search query (e.g., "where do we handle authentication?")
query: String,
/// Maximum total results to return
#[arg(short = 'm', long, default_value = "25")]
max_results: usize,
/// Maximum matches to show per file
#[arg(long, default_value = "1")]
per_file: usize,
/// Show full chunk content instead of snippets
#[arg(short, long)]
content: bool,
/// Show relevance scores
#[arg(long)]
scores: bool,
/// Show file paths only (like grep -l)
#[arg(long)]
compact: bool,
/// Force re-index changed files before searching
#[arg(short, long)]
sync: bool,
/// Output JSON for agents
#[arg(long)]
json: bool,
/// Path to search in (defaults to current directory)
#[arg(long)]
path: Option<PathBuf>,
/// Use vector-only search (disable hybrid FTS)
#[arg(long)]
vector_only: bool,
/// RRF k parameter for score fusion (default 20)
#[arg(long, default_value = "20")]
rrf_k: f32,
/// Enable neural reranking for better accuracy (uses Jina Reranker)
#[arg(long)]
rerank: bool,
/// Number of top results to rerank (default 50)
#[arg(long, default_value = "50")]
rerank_top: usize,
/// Filter results to files under this path (e.g., "src/")
#[arg(long)]
filter_path: Option<String>,
},
/// Index the repository or manage global index registry
Index {
/// Path to index (defaults to current directory), or use "list" to show status
path: Option<PathBuf>,
/// Show what would be indexed without actually indexing
#[arg(long)]
dry_run: bool,
/// Force full re-index
#[arg(short = 'f', long, alias = "full")]
force: bool,
/// Add a repository to the index (creates local or global index)
#[arg(long)]
add: bool,
/// Create global index instead of local (only with --add)
#[arg(short = 'g', long)]
global: bool,
/// Remove the index (local or global, auto-detected)
#[arg(long, visible_alias = "rm")]
remove: bool,
/// Show index status (local or global)
#[arg(long)]
list: bool,
},
/// Run a background server with live file watching
Serve {
/// Port to listen on
#[arg(short, long, default_value = "4444")]
port: u16,
/// Path to serve (defaults to current directory)
path: Option<PathBuf>,
},
/// Show statistics about the vector database
Stats {
/// Path to show stats for (defaults to current directory)
path: Option<PathBuf>,
},
/// Clear the vector database
Clear {
/// Path to clear (defaults to current directory)
path: Option<PathBuf>,
/// Skip confirmation prompt
#[arg(short = 'y', long)]
yes: bool,
},
/// Check installation health
Doctor,
/// Download embedding models
Setup {
/// Model to download (defaults to mxbai-embed-xsmall-v1)
#[arg(long)]
model: Option<String>,
},
/// Start MCP server for Claude Code integration
Mcp {
/// Path to project (defaults to current directory)
path: Option<PathBuf>,
},
/// Run multi-repo daemon with HTTP search and periodic re-indexing
Daemon {
/// Path to YAML config file
#[arg(short, long)]
config: PathBuf,
},
}
pub async fn run(cancel_token: CancellationToken) -> Result<()> {
let cli = Cli::parse();
// Parse model from CLI flag
let model_type = cli.model.as_ref().and_then(|m| ModelType::parse(m));
if cli.model.is_some() && model_type.is_none() {
eprintln!(
"Unknown model: '{}'. Available models:",
cli.model.as_ref().unwrap()
);
eprintln!(" minilm-l6, minilm-l6-q, minilm-l12, minilm-l12-q, paraphrase-minilm");
eprintln!(" bge-small, bge-small-q, bge-base, nomic-v1, nomic-v1.5, nomic-v1.5-q");
eprintln!(" jina-code, e5-multilingual, mxbai-large, modernbert-large");
std::process::exit(1);
}
// Set quiet mode if requested
if cli.quiet {
crate::output::set_quiet(true);
}
// Parse loglevel from CLI
let log_level =
crate::logger::LogLevel::parse(&cli.loglevel).unwrap_or(crate::logger::LogLevel::Info);
match cli.command {
Commands::Search {
query,
max_results,
per_file,
content,
scores,
compact,
sync,
json,
path,
vector_only,
rrf_k,
rerank,
rerank_top,
filter_path,
} => {
// Auto-enable quiet mode for JSON output
if json {
crate::output::set_quiet(true);
}
let options = SearchOptions {
max_results,
per_file: if per_file == 0 { None } else { Some(per_file) },
content_lines: if content { 3 } else { 0 },
show_scores: scores,
compact,
sync,
json,
filter_path,
model_override: model_type.map(|mt| format!("{:?}", mt)),
vector_only,
rrf_k: if rrf_k == 60.0 {
None
} else {
Some(rrf_k as usize)
},
rerank,
rerank_top: if rerank_top == 50 {
None
} else {
Some(rerank_top)
},
};
crate::search::search(&query, path, options).await
}
Commands::Index {
path,
dry_run,
force,
add,
global,
remove,
list,
} => {
// Check if path is "list", "add", or "rm"/"remove" as special cases (backward compatibility)
let path_str = path.as_ref().and_then(|p| p.to_str());
let is_list_cmd = path_str.map(|s| s == "list").unwrap_or(false);
let is_add_cmd = path_str.map(|s| s == "add").unwrap_or(false);
let is_rm_cmd = path_str
.map(|s| s == "rm" || s == "remove")
.unwrap_or(false);
if add || is_add_cmd {
// Clear path if it's "add" to avoid treating it as a directory
let effective_path = if is_add_cmd { None } else { path };
crate::index::add_to_index(effective_path, global, cancel_token.clone()).await
} else if remove || is_rm_cmd {
// Clear path if it's "rm"/"remove" to avoid treating it as a directory
let effective_path = if is_rm_cmd { None } else { path };
crate::index::remove_from_index(effective_path).await
} else if list || is_list_cmd {
crate::index::list_index_status().await
} else {
// For 'codesearch index .' or 'codesearch index <path>', just run indexing
// The index() function will handle checking for existing indexes
crate::index::index(
path,
dry_run,
force,
false,
model_type,
cancel_token.clone(),
)
.await
}
}
Commands::Stats { path } => crate::index::stats(path).await,
Commands::Serve { port, path } => {
// Discover database path and initialize logger with file output
// NOTE: For Serve, tracing is NOT initialized in main.rs — init_logger
// is the first and only call to set the global subscriber
let effective_path = path
.as_ref()
.cloned()
.unwrap_or_else(|| std::env::current_dir().unwrap());
if let Ok(Some(db_info)) =
crate::db_discovery::find_best_database(Some(&effective_path))
{
match crate::logger::init_logger(&db_info.db_path, log_level, cli.quiet) {
Err(e) => {
eprintln!("Warning: Failed to initialize file logger: {}", e);
}
_ => {
// Logger initialized successfully (either FileLogging or ConsoleOnly)
}
}
}
crate::server::serve(port, path).await
}
Commands::Clear { path, yes } => crate::index::clear(path, yes).await,
Commands::Doctor => crate::cli::doctor::run().await,
Commands::Setup { model } => crate::cli::setup::run(model).await,
Commands::Mcp { path } => {
// Discover database path and initialize logger with file output
// NOTE: For MCP, tracing is NOT initialized in main.rs — init_logger
// is the first and only call to set the global subscriber
let effective_path = path
.as_ref()
.cloned()
.unwrap_or_else(|| std::env::current_dir().unwrap());
if let Ok(Some(db_info)) =
crate::db_discovery::find_best_database(Some(&effective_path))
{
match crate::logger::init_logger(&db_info.db_path, log_level, cli.quiet) {
Err(e) => {
eprintln!("Warning: Failed to initialize file logger: {}", e);
}
_ => {
// Logger initialized successfully (either FileLogging or ConsoleOnly)
}
}
}
crate::mcp::run_mcp_server(path, cancel_token).await
}
Commands::Daemon { config } => {
// Initialize file+console logger for long-running daemon.
// init_logger appends "logs/" to the base path, so pass ~/.codesearch
// to get logs at ~/.codesearch/logs/codesearch.log
let log_base = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("/tmp"))
.join(".codesearch");
match crate::logger::init_logger(&log_base, log_level, cli.quiet) {
Err(e) => {
eprintln!("Warning: Failed to initialize file logger: {}", e);
}
_ => {}
}
let daemon_config = crate::daemon::DaemonConfig::load(&config)?;
crate::daemon::run_daemon(daemon_config, cancel_token).await
}
}
}
mod doctor;
mod setup;