Skip to content

Commit a5683f3

Browse files
mgsloanosyvokon
andauthored
zeta_cli: Add --output-format both and --prompt-format only-snippets (zed-industries#38920)
These are options are probably temporary, added for use in some experimental code Release Notes: - N/A Co-authored-by: Oleksiy <oleksiy@zed.dev>
1 parent 67984d5 commit a5683f3

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

crates/cloud_llm_client/src/predict_edits_v3.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub enum PromptFormat {
4848
#[default]
4949
MarkedExcerpt,
5050
LabeledSections,
51+
/// Prompt format intended for use via zeta_cli
52+
OnlySnippets,
5153
}
5254

5355
impl PromptFormat {
@@ -61,6 +63,7 @@ impl std::fmt::Display for PromptFormat {
6163
match self {
6264
PromptFormat::MarkedExcerpt => write!(f, "Marked Excerpt"),
6365
PromptFormat::LabeledSections => write!(f, "Labeled Sections"),
66+
PromptFormat::OnlySnippets => write!(f, "Only Snippets"),
6467
}
6568
}
6669
}

crates/cloud_zeta2_prompt/src/cloud_zeta2_prompt.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub fn system_prompt(format: PromptFormat) -> &'static str {
5454
match format {
5555
PromptFormat::MarkedExcerpt => MARKED_EXCERPT_SYSTEM_PROMPT,
5656
PromptFormat::LabeledSections => LABELED_SECTIONS_SYSTEM_PROMPT,
57+
// only intended for use via zeta_cli
58+
PromptFormat::OnlySnippets => "",
5759
}
5860
}
5961

@@ -343,6 +345,7 @@ impl<'a> PlannedPrompt<'a> {
343345
self.request.excerpt_range.start + self.request.cursor_offset,
344346
CURSOR_MARKER,
345347
)],
348+
PromptFormat::OnlySnippets => vec![],
346349
};
347350

348351
let mut prompt = String::new();
@@ -432,12 +435,13 @@ impl<'a> PlannedPrompt<'a> {
432435
}
433436

434437
writeln!(output, "```{}", file_path.display()).ok();
438+
let mut skipped_last_snippet = false;
435439
for (snippet, range) in disjoint_snippets {
436440
let section_index = section_ranges.len();
437441

438442
match self.request.prompt_format {
439-
PromptFormat::MarkedExcerpt => {
440-
if range.start > 0 {
443+
PromptFormat::MarkedExcerpt | PromptFormat::OnlySnippets => {
444+
if range.start > 0 && !skipped_last_snippet {
441445
output.push_str("…\n");
442446
}
443447
}
@@ -454,25 +458,38 @@ impl<'a> PlannedPrompt<'a> {
454458
}
455459

456460
if is_excerpt_file {
457-
excerpt_index = Some(section_index);
458-
let mut last_offset = range.start;
459-
let mut i = 0;
460-
while i < excerpt_file_insertions.len() {
461-
let (offset, insertion) = &excerpt_file_insertions[i];
462-
let found = *offset >= range.start && *offset <= range.end;
463-
if found {
464-
output.push_str(
465-
&snippet.text[last_offset - range.start..offset - range.start],
466-
);
467-
output.push_str(insertion);
468-
last_offset = *offset;
469-
excerpt_file_insertions.remove(i);
470-
continue;
461+
if self.request.prompt_format == PromptFormat::OnlySnippets {
462+
if range.start >= self.request.excerpt_range.start
463+
&& range.end <= self.request.excerpt_range.end
464+
{
465+
skipped_last_snippet = true;
466+
} else {
467+
skipped_last_snippet = false;
468+
output.push_str(snippet.text);
471469
}
472-
i += 1;
470+
} else {
471+
let mut last_offset = range.start;
472+
let mut i = 0;
473+
while i < excerpt_file_insertions.len() {
474+
let (offset, insertion) = &excerpt_file_insertions[i];
475+
let found = *offset >= range.start && *offset <= range.end;
476+
if found {
477+
excerpt_index = Some(section_index);
478+
output.push_str(
479+
&snippet.text[last_offset - range.start..offset - range.start],
480+
);
481+
output.push_str(insertion);
482+
last_offset = *offset;
483+
excerpt_file_insertions.remove(i);
484+
continue;
485+
}
486+
i += 1;
487+
}
488+
skipped_last_snippet = false;
489+
output.push_str(&snippet.text[last_offset - range.start..]);
473490
}
474-
output.push_str(&snippet.text[last_offset - range.start..]);
475491
} else {
492+
skipped_last_snippet = false;
476493
output.push_str(snippet.text);
477494
}
478495

@@ -483,7 +500,11 @@ impl<'a> PlannedPrompt<'a> {
483500
}
484501

485502
Ok(SectionLabels {
486-
excerpt_index: excerpt_index.context("bug: no snippet found for excerpt")?,
503+
// TODO: Clean this up
504+
excerpt_index: match self.request.prompt_format {
505+
PromptFormat::OnlySnippets => 0,
506+
_ => excerpt_index.context("bug: no snippet found for excerpt")?,
507+
},
487508
section_ranges,
488509
})
489510
}

crates/zeta_cli/src/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use language_model::LlmApiToken;
1515
use project::{Project, ProjectPath, Worktree};
1616
use release_channel::AppVersion;
1717
use reqwest_client::ReqwestClient;
18+
use serde_json::json;
1819
use std::path::{Path, PathBuf};
1920
use std::process::exit;
2021
use std::str::FromStr;
@@ -86,13 +87,15 @@ enum PromptFormat {
8687
#[default]
8788
MarkedExcerpt,
8889
LabeledSections,
90+
OnlySnippets,
8991
}
9092

9193
impl Into<predict_edits_v3::PromptFormat> for PromptFormat {
9294
fn into(self) -> predict_edits_v3::PromptFormat {
9395
match self {
9496
Self::MarkedExcerpt => predict_edits_v3::PromptFormat::MarkedExcerpt,
9597
Self::LabeledSections => predict_edits_v3::PromptFormat::LabeledSections,
98+
Self::OnlySnippets => predict_edits_v3::PromptFormat::OnlySnippets,
9699
}
97100
}
98101
}
@@ -102,6 +105,7 @@ enum OutputFormat {
102105
#[default]
103106
Prompt,
104107
Request,
108+
Both,
105109
}
106110

107111
#[derive(Debug, Clone)]
@@ -269,16 +273,18 @@ async fn get_context(
269273
zeta.cloud_request_for_zeta_cli(&project, &buffer, cursor, cx)
270274
})?
271275
.await?;
276+
277+
let planned_prompt = cloud_zeta2_prompt::PlannedPrompt::populate(&request)?;
278+
let prompt_string = planned_prompt.to_prompt_string()?.0;
272279
match zeta2_args.output_format {
273-
OutputFormat::Prompt => {
274-
let planned_prompt =
275-
cloud_zeta2_prompt::PlannedPrompt::populate(&request)?;
276-
// TODO: Output the section label ranges
277-
anyhow::Ok(planned_prompt.to_prompt_string()?.0)
278-
}
280+
OutputFormat::Prompt => anyhow::Ok(prompt_string),
279281
OutputFormat::Request => {
280282
anyhow::Ok(serde_json::to_string_pretty(&request)?)
281283
}
284+
OutputFormat::Both => anyhow::Ok(serde_json::to_string_pretty(&json!({
285+
"request": request,
286+
"prompt": prompt_string,
287+
}))?),
282288
}
283289
})
284290
})?

0 commit comments

Comments
 (0)