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
use clap::{Parser, Subcommand};
use dotenvy::dotenv;
use std::env;
use std::io::Read;
mod bm25_embedder;
mod bm25_ranker;
mod files;
mod metadata;
mod openai;
mod scan;
mod search;
mod similarity;
mod tokenizer;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Scan files matching a glob pattern
Scan {
/// The glob pattern to match files
#[arg(short, long, required = true)]
pattern: String,
/// Size of chunks to split files into (0 for no chunking)
#[arg(long, default_value = "0")]
chunk_size: usize,
/// Size of overlap between chunks (0 for no overlap)
#[arg(long, default_value = "0")]
chunk_overlap: usize,
/// Include file metadata in embeddings
#[arg(long, default_value = "false")]
embed_metadata: bool,
},
/// Suggest relevant files based on a query
SuggestFiles {
/// The query to find relevant files (optional if using stdin)
#[arg(short, long, required = false)]
prompt: Option<String>,
/// Only return results with similarity >= filter-similarity (0.0 to 1.0)
#[arg(short, long, default_value = "0.0")]
filter_similarity: f32,
/// Show detailed information including similarity scores and chunk details
#[arg(long, default_value = "false")]
verbose: bool,
/// Show debug information including BM25 rankings
#[arg(long, default_value = "false")]
debug: bool,
/// Show the actual contents of matched files/chunks
#[arg(long, default_value = "false")]
file_contents: bool,
/// Limit the number of results (0 for unlimited)
#[arg(short, long, default_value = "0")]
count: usize,
/// Scale factor for BM25 score influence (default 0.1)
#[arg(long, default_value = "0.1")]
bm25_scale: f32,
/// Scale factor for RAG score influence (default 1.0)
#[arg(long, default_value = "1.0")]
rag_scale: f32,
},
/// Expand a prompt using a system prompt
Expand {
/// System prompt for expanding the question
#[arg(short, long, required = true)]
system_prompt: String,
/// The prompt to expand (optional if using stdin)
#[arg(long, required = false)]
prompt: Option<String>,
},
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not found in environment");
let cli = Cli::parse();
match cli.command {
Commands::Scan {
pattern,
chunk_size,
chunk_overlap,
embed_metadata,
} => {
if chunk_size > 0 && chunk_overlap >= chunk_size {
eprintln!("Error: chunk-overlap must be less than chunk-size");
std::process::exit(1);
}
scan::scan_files(
&pattern,
&api_key,
chunk_size,
chunk_overlap,
embed_metadata,
)
.await?;
}
Commands::SuggestFiles {
prompt,
filter_similarity,
verbose,
debug,
file_contents,
count,
bm25_scale,
rag_scale,
} => {
if !(0.0..=1.0).contains(&filter_similarity) {
eprintln!("Error: filter-similarity must be between 0.0 and 1.0");
std::process::exit(1);
}
let prompt_text = match prompt {
None => {
// Only try to read from stdin if it's not a terminal
if atty::isnt(atty::Stream::Stdin) {
let mut buffer = String::new();
std::io::stdin().read_to_string(&mut buffer)?;
buffer.trim().to_string()
} else {
String::new()
}
}
Some(p) => p,
};
if prompt_text.is_empty() {
eprintln!("Error: No prompt given");
std::process::exit(1);
}
if let Err(e) = search::find_related_files(
&prompt_text,
&api_key,
filter_similarity,
verbose,
debug,
file_contents,
count,
bm25_scale,
rag_scale
)
.await
{
eprintln!("Error finding related files: {}", e);
}
}
Commands::Expand {
prompt,
system_prompt,
} => {
let prompt_text = match prompt {
None => {
// Only try to read from stdin if it's not a terminal
if atty::isnt(atty::Stream::Stdin) {
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer)?;
buffer
} else {
String::new()
}
}
Some(p) => p,
};
if prompt_text.is_empty() {
eprintln!("Error: No prompt given");
std::process::exit(1);
}
if !prompt_text.trim().is_empty() {
match openai::get_openai_chat_completion(&prompt_text, &system_prompt, &api_key)
.await
{
Ok(expanded) => println!("{}", expanded),
Err(e) => eprintln!("Error expanding prompt: {}", e),
}
} else {
eprintln!("Error: No prompt provided via arguments or stdin");
std::process::exit(1);
}
}
}
Ok(())
}