1mod evaluate;
2mod example;
3mod headless;
4mod paths;
5mod predict;
6mod source_location;
7mod syntax_retrieval_stats;
8mod util;
9
10use crate::evaluate::{EvaluateArguments, run_evaluate};
11use crate::example::{ExampleFormat, NamedExample};
12use crate::predict::{PredictArguments, run_zeta2_predict};
13use crate::syntax_retrieval_stats::retrieval_stats;
14use ::util::paths::PathStyle;
15use anyhow::{Result, anyhow};
16use clap::{Args, Parser, Subcommand};
17use cloud_llm_client::predict_edits_v3;
18use edit_prediction_context::{
19 EditPredictionContextOptions, EditPredictionExcerptOptions, EditPredictionScoreOptions,
20};
21use gpui::{Application, AsyncApp, Entity, prelude::*};
22use language::{Bias, Buffer, BufferSnapshot, Point};
23use project::{Project, Worktree};
24use reqwest_client::ReqwestClient;
25use serde_json::json;
26use std::io::{self};
27use std::time::Duration;
28use std::{collections::HashSet, path::PathBuf, str::FromStr, sync::Arc};
29use zeta2::ContextMode;
30
31use crate::headless::ZetaCliAppState;
32use crate::source_location::SourceLocation;
33use crate::util::{open_buffer, open_buffer_with_language_server};
34
35#[derive(Parser, Debug)]
36#[command(name = "zeta")]
37struct ZetaCliArgs {
38 #[arg(long, default_value_t = false)]
39 printenv: bool,
40 #[command(subcommand)]
41 command: Option<Command>,
42}
43
44#[derive(Subcommand, Debug)]
45enum Command {
46 Zeta1 {
47 #[command(subcommand)]
48 command: Zeta1Command,
49 },
50 Zeta2 {
51 #[command(subcommand)]
52 command: Zeta2Command,
53 },
54 ConvertExample {
55 path: PathBuf,
56 #[arg(long, value_enum, default_value_t = ExampleFormat::Md)]
57 output_format: ExampleFormat,
58 },
59 Clean,
60}
61
62#[derive(Subcommand, Debug)]
63enum Zeta1Command {
64 Context {
65 #[clap(flatten)]
66 context_args: ContextArgs,
67 },
68}
69
70#[derive(Subcommand, Debug)]
71enum Zeta2Command {
72 Syntax {
73 #[clap(flatten)]
74 args: Zeta2Args,
75 #[clap(flatten)]
76 syntax_args: Zeta2SyntaxArgs,
77 #[command(subcommand)]
78 command: Zeta2SyntaxCommand,
79 },
80 Predict(PredictArguments),
81 Eval(EvaluateArguments),
82}
83
84#[derive(Subcommand, Debug)]
85enum Zeta2SyntaxCommand {
86 Context {
87 #[clap(flatten)]
88 context_args: ContextArgs,
89 },
90 Stats {
91 #[arg(long)]
92 worktree: PathBuf,
93 #[arg(long)]
94 extension: Option<String>,
95 #[arg(long)]
96 limit: Option<usize>,
97 #[arg(long)]
98 skip: Option<usize>,
99 },
100}
101
102#[derive(Debug, Args)]
103#[group(requires = "worktree")]
104struct ContextArgs {
105 #[arg(long)]
106 worktree: PathBuf,
107 #[arg(long)]
108 cursor: SourceLocation,
109 #[arg(long)]
110 use_language_server: bool,
111 #[arg(long)]
112 edit_history: Option<FileOrStdin>,
113}
114
115#[derive(Debug, Args)]
116struct Zeta2Args {
117 #[arg(long, default_value_t = 8192)]
118 max_prompt_bytes: usize,
119 #[arg(long, default_value_t = 2048)]
120 max_excerpt_bytes: usize,
121 #[arg(long, default_value_t = 1024)]
122 min_excerpt_bytes: usize,
123 #[arg(long, default_value_t = 0.66)]
124 target_before_cursor_over_total_bytes: f32,
125 #[arg(long, default_value_t = 1024)]
126 max_diagnostic_bytes: usize,
127 #[arg(long, value_enum, default_value_t = PromptFormat::default())]
128 prompt_format: PromptFormat,
129 #[arg(long, value_enum, default_value_t = Default::default())]
130 output_format: OutputFormat,
131 #[arg(long, default_value_t = 42)]
132 file_indexing_parallelism: usize,
133}
134
135#[derive(Debug, Args)]
136struct Zeta2SyntaxArgs {
137 #[arg(long, default_value_t = false)]
138 disable_imports_gathering: bool,
139 #[arg(long, default_value_t = u8::MAX)]
140 max_retrieved_definitions: u8,
141}
142
143fn syntax_args_to_options(
144 zeta2_args: &Zeta2Args,
145 syntax_args: &Zeta2SyntaxArgs,
146 omit_excerpt_overlaps: bool,
147) -> zeta2::ZetaOptions {
148 zeta2::ZetaOptions {
149 context: ContextMode::Syntax(EditPredictionContextOptions {
150 max_retrieved_declarations: syntax_args.max_retrieved_definitions,
151 use_imports: !syntax_args.disable_imports_gathering,
152 excerpt: EditPredictionExcerptOptions {
153 max_bytes: zeta2_args.max_excerpt_bytes,
154 min_bytes: zeta2_args.min_excerpt_bytes,
155 target_before_cursor_over_total_bytes: zeta2_args
156 .target_before_cursor_over_total_bytes,
157 },
158 score: EditPredictionScoreOptions {
159 omit_excerpt_overlaps,
160 },
161 }),
162 max_diagnostic_bytes: zeta2_args.max_diagnostic_bytes,
163 max_prompt_bytes: zeta2_args.max_prompt_bytes,
164 prompt_format: zeta2_args.prompt_format.into(),
165 file_indexing_parallelism: zeta2_args.file_indexing_parallelism,
166 buffer_change_grouping_interval: Duration::ZERO,
167 }
168}
169
170#[derive(clap::ValueEnum, Default, Debug, Clone, Copy)]
171enum PromptFormat {
172 MarkedExcerpt,
173 LabeledSections,
174 OnlySnippets,
175 #[default]
176 NumberedLines,
177 OldTextNewText,
178}
179
180impl Into<predict_edits_v3::PromptFormat> for PromptFormat {
181 fn into(self) -> predict_edits_v3::PromptFormat {
182 match self {
183 Self::MarkedExcerpt => predict_edits_v3::PromptFormat::MarkedExcerpt,
184 Self::LabeledSections => predict_edits_v3::PromptFormat::LabeledSections,
185 Self::OnlySnippets => predict_edits_v3::PromptFormat::OnlySnippets,
186 Self::NumberedLines => predict_edits_v3::PromptFormat::NumLinesUniDiff,
187 Self::OldTextNewText => predict_edits_v3::PromptFormat::OldTextNewText,
188 }
189 }
190}
191
192#[derive(clap::ValueEnum, Default, Debug, Clone)]
193enum OutputFormat {
194 #[default]
195 Prompt,
196 Request,
197 Full,
198}
199
200#[derive(Debug, Clone)]
201enum FileOrStdin {
202 File(PathBuf),
203 Stdin,
204}
205
206impl FileOrStdin {
207 async fn read_to_string(&self) -> Result<String, std::io::Error> {
208 match self {
209 FileOrStdin::File(path) => smol::fs::read_to_string(path).await,
210 FileOrStdin::Stdin => smol::unblock(|| std::io::read_to_string(std::io::stdin())).await,
211 }
212 }
213}
214
215impl FromStr for FileOrStdin {
216 type Err = <PathBuf as FromStr>::Err;
217
218 fn from_str(s: &str) -> Result<Self, Self::Err> {
219 match s {
220 "-" => Ok(Self::Stdin),
221 _ => Ok(Self::File(PathBuf::from_str(s)?)),
222 }
223 }
224}
225
226struct LoadedContext {
227 full_path_str: String,
228 snapshot: BufferSnapshot,
229 clipped_cursor: Point,
230 worktree: Entity<Worktree>,
231 project: Entity<Project>,
232 buffer: Entity<Buffer>,
233}
234
235async fn load_context(
236 args: &ContextArgs,
237 app_state: &Arc<ZetaCliAppState>,
238 cx: &mut AsyncApp,
239) -> Result<LoadedContext> {
240 let ContextArgs {
241 worktree: worktree_path,
242 cursor,
243 use_language_server,
244 ..
245 } = args;
246
247 let worktree_path = worktree_path.canonicalize()?;
248
249 let project = cx.update(|cx| {
250 Project::local(
251 app_state.client.clone(),
252 app_state.node_runtime.clone(),
253 app_state.user_store.clone(),
254 app_state.languages.clone(),
255 app_state.fs.clone(),
256 None,
257 cx,
258 )
259 })?;
260
261 let worktree = project
262 .update(cx, |project, cx| {
263 project.create_worktree(&worktree_path, true, cx)
264 })?
265 .await?;
266
267 let mut ready_languages = HashSet::default();
268 let (_lsp_open_handle, buffer) = if *use_language_server {
269 let (lsp_open_handle, _, buffer) = open_buffer_with_language_server(
270 project.clone(),
271 worktree.clone(),
272 cursor.path.clone(),
273 &mut ready_languages,
274 cx,
275 )
276 .await?;
277 (Some(lsp_open_handle), buffer)
278 } else {
279 let buffer =
280 open_buffer(project.clone(), worktree.clone(), cursor.path.clone(), cx).await?;
281 (None, buffer)
282 };
283
284 let full_path_str = worktree
285 .read_with(cx, |worktree, _| worktree.root_name().join(&cursor.path))?
286 .display(PathStyle::local())
287 .to_string();
288
289 let snapshot = cx.update(|cx| buffer.read(cx).snapshot())?;
290 let clipped_cursor = snapshot.clip_point(cursor.point, Bias::Left);
291 if clipped_cursor != cursor.point {
292 let max_row = snapshot.max_point().row;
293 if cursor.point.row < max_row {
294 return Err(anyhow!(
295 "Cursor position {:?} is out of bounds (line length is {})",
296 cursor.point,
297 snapshot.line_len(cursor.point.row)
298 ));
299 } else {
300 return Err(anyhow!(
301 "Cursor position {:?} is out of bounds (max row is {})",
302 cursor.point,
303 max_row
304 ));
305 }
306 }
307
308 Ok(LoadedContext {
309 full_path_str,
310 snapshot,
311 clipped_cursor,
312 worktree,
313 project,
314 buffer,
315 })
316}
317
318async fn zeta2_syntax_context(
319 zeta2_args: Zeta2Args,
320 syntax_args: Zeta2SyntaxArgs,
321 args: ContextArgs,
322 app_state: &Arc<ZetaCliAppState>,
323 cx: &mut AsyncApp,
324) -> Result<String> {
325 let LoadedContext {
326 worktree,
327 project,
328 buffer,
329 clipped_cursor,
330 ..
331 } = load_context(&args, app_state, cx).await?;
332
333 // wait for worktree scan before starting zeta2 so that wait_for_initial_indexing waits for
334 // the whole worktree.
335 worktree
336 .read_with(cx, |worktree, _cx| {
337 worktree.as_local().unwrap().scan_complete()
338 })?
339 .await;
340 let output = cx
341 .update(|cx| {
342 let zeta = cx.new(|cx| {
343 zeta2::Zeta::new(app_state.client.clone(), app_state.user_store.clone(), cx)
344 });
345 let indexing_done_task = zeta.update(cx, |zeta, cx| {
346 zeta.set_options(syntax_args_to_options(&zeta2_args, &syntax_args, true));
347 zeta.register_buffer(&buffer, &project, cx);
348 zeta.wait_for_initial_indexing(&project, cx)
349 });
350 cx.spawn(async move |cx| {
351 indexing_done_task.await?;
352 let request = zeta
353 .update(cx, |zeta, cx| {
354 let cursor = buffer.read(cx).snapshot().anchor_before(clipped_cursor);
355 zeta.cloud_request_for_zeta_cli(&project, &buffer, cursor, cx)
356 })?
357 .await?;
358
359 let (prompt_string, section_labels) = cloud_zeta2_prompt::build_prompt(&request)?;
360
361 match zeta2_args.output_format {
362 OutputFormat::Prompt => anyhow::Ok(prompt_string),
363 OutputFormat::Request => anyhow::Ok(serde_json::to_string_pretty(&request)?),
364 OutputFormat::Full => anyhow::Ok(serde_json::to_string_pretty(&json!({
365 "request": request,
366 "prompt": prompt_string,
367 "section_labels": section_labels,
368 }))?),
369 }
370 })
371 })?
372 .await?;
373
374 Ok(output)
375}
376
377async fn zeta1_context(
378 args: ContextArgs,
379 app_state: &Arc<ZetaCliAppState>,
380 cx: &mut AsyncApp,
381) -> Result<zeta::GatherContextOutput> {
382 let LoadedContext {
383 full_path_str,
384 snapshot,
385 clipped_cursor,
386 ..
387 } = load_context(&args, app_state, cx).await?;
388
389 let events = match args.edit_history {
390 Some(events) => events.read_to_string().await?,
391 None => String::new(),
392 };
393
394 let prompt_for_events = move || (events, 0);
395 cx.update(|cx| {
396 zeta::gather_context(
397 full_path_str,
398 &snapshot,
399 clipped_cursor,
400 prompt_for_events,
401 cx,
402 )
403 })?
404 .await
405}
406
407fn main() {
408 zlog::init();
409 zlog::init_output_stderr();
410 let args = ZetaCliArgs::parse();
411 let http_client = Arc::new(ReqwestClient::new());
412 let app = Application::headless().with_http_client(http_client);
413
414 app.run(move |cx| {
415 let app_state = Arc::new(headless::init(cx));
416 cx.spawn(async move |cx| {
417 match args.command {
418 None => {
419 if args.printenv {
420 ::util::shell_env::print_env();
421 return;
422 } else {
423 panic!("Expected a command");
424 }
425 }
426 Some(Command::Zeta1 {
427 command: Zeta1Command::Context { context_args },
428 }) => {
429 let context = zeta1_context(context_args, &app_state, cx).await.unwrap();
430 let result = serde_json::to_string_pretty(&context.body).unwrap();
431 println!("{}", result);
432 }
433 Some(Command::Zeta2 { command }) => match command {
434 Zeta2Command::Predict(arguments) => {
435 run_zeta2_predict(arguments, &app_state, cx).await;
436 }
437 Zeta2Command::Eval(arguments) => {
438 run_evaluate(arguments, &app_state, cx).await;
439 }
440 Zeta2Command::Syntax {
441 args,
442 syntax_args,
443 command,
444 } => {
445 let result = match command {
446 Zeta2SyntaxCommand::Context { context_args } => {
447 zeta2_syntax_context(
448 args,
449 syntax_args,
450 context_args,
451 &app_state,
452 cx,
453 )
454 .await
455 }
456 Zeta2SyntaxCommand::Stats {
457 worktree,
458 extension,
459 limit,
460 skip,
461 } => {
462 retrieval_stats(
463 worktree,
464 app_state,
465 extension,
466 limit,
467 skip,
468 syntax_args_to_options(&args, &syntax_args, false),
469 cx,
470 )
471 .await
472 }
473 };
474 println!("{}", result.unwrap());
475 }
476 },
477 Some(Command::ConvertExample {
478 path,
479 output_format,
480 }) => {
481 let example = NamedExample::load(path).unwrap();
482 example.write(output_format, io::stdout()).unwrap();
483 }
484 Some(Command::Clean) => {
485 std::fs::remove_dir_all(&*crate::paths::TARGET_ZETA_DIR).unwrap()
486 }
487 };
488
489 let _ = cx.update(|cx| cx.quit());
490 })
491 .detach();
492 });
493}