format_prompt.rs

  1use crate::{
  2    FormatPromptArgs, PredictionProvider,
  3    example::{ActualCursor, Example, ExamplePrompt},
  4    headless::EpAppState,
  5    progress::{ExampleProgress, Step},
  6    retrieve_context::run_context_retrieval,
  7};
  8use anyhow::{Context as _, Result, anyhow};
  9use edit_prediction::{cursor_excerpt::editable_and_context_ranges_for_cursor_position, udiff};
 10use gpui::{AppContext, AsyncApp};
 11use language::{Buffer, OffsetRangeExt, Point};
 12use similar::DiffableStr;
 13use std::sync::Arc;
 14use std::{fmt::Write as _, ops::Range};
 15use zeta_prompt::ZetaFormat;
 16use zeta_prompt::format_zeta_prompt;
 17
 18pub async fn run_format_prompt(
 19    example: &mut Example,
 20    args: &FormatPromptArgs,
 21    app_state: Arc<EpAppState>,
 22    example_progress: &ExampleProgress,
 23    cx: AsyncApp,
 24) -> Result<()> {
 25    run_context_retrieval(example, app_state.clone(), example_progress, cx.clone()).await?;
 26
 27    let step_progress = example_progress.start(Step::FormatPrompt);
 28
 29    let prompt_inputs = example
 30        .prompt_inputs
 31        .as_ref()
 32        .context("prompt_inputs must be set after context retrieval")?;
 33
 34    let language = app_state
 35        .languages
 36        .load_language_for_file_path(&example.spec.cursor_path)
 37        .await
 38        .ok();
 39    let snapshot_fut = cx.update(|cx| {
 40        Buffer::build_snapshot(
 41            prompt_inputs.content.as_str().into(),
 42            language,
 43            Some(app_state.languages.clone()),
 44            cx,
 45        )
 46    });
 47    let cursor_point = Point::new(prompt_inputs.cursor_row, prompt_inputs.cursor_column);
 48    let snapshot = cx.background_spawn(snapshot_fut).await;
 49
 50    match args.provider {
 51        PredictionProvider::Teacher(_) | PredictionProvider::TeacherNonBatching(_) => {
 52            step_progress.set_substatus("formatting teacher prompt");
 53
 54            let (editable_range, context_range) = editable_and_context_ranges_for_cursor_position(
 55                cursor_point,
 56                &snapshot,
 57                edit_prediction::zeta::max_editable_tokens(ZetaFormat::default()),
 58                edit_prediction::zeta::MAX_CONTEXT_TOKENS,
 59            );
 60            let editable_range = editable_range.to_offset(&snapshot);
 61            let context_range = context_range.to_offset(&snapshot);
 62
 63            let prompt = TeacherPrompt::format_prompt(example, editable_range, context_range);
 64            example.prompt = Some(ExamplePrompt {
 65                input: prompt,
 66                expected_output: String::new(),
 67                rejected_output: None,
 68                prefill: None,
 69                provider: args.provider,
 70            });
 71        }
 72        PredictionProvider::Zeta2(version) => {
 73            step_progress.set_substatus("formatting zeta2 prompt");
 74
 75            let (editable_range, context_range) = editable_and_context_ranges_for_cursor_position(
 76                cursor_point,
 77                &snapshot,
 78                edit_prediction::zeta::max_editable_tokens(version),
 79                edit_prediction::zeta::MAX_CONTEXT_TOKENS,
 80            );
 81            let editable_range = editable_range.to_offset(&snapshot);
 82            let context_range = context_range.to_offset(&snapshot);
 83
 84            let context_start = context_range.start;
 85            let cursor_offset_in_excerpt = prompt_inputs.cursor_offset - context_start;
 86            let editable_range_in_excerpt =
 87                (editable_range.start - context_start)..(editable_range.end - context_start);
 88            let input = zeta_prompt::ZetaPromptInput {
 89                cursor_path: example.spec.cursor_path.clone(),
 90                cursor_excerpt: prompt_inputs.content[context_range].to_string().into(),
 91                editable_range_in_excerpt,
 92                cursor_offset_in_excerpt,
 93                excerpt_start_row: prompt_inputs.excerpt_start_row,
 94                events: prompt_inputs.edit_history.clone(),
 95                related_files: prompt_inputs.related_files.clone().unwrap_or_default(),
 96                excerpt_ranges: None,
 97                preferred_model: None,
 98                in_open_source_repo: example
 99                    .spec
100                    .captured_prompt_input
101                    .as_ref()
102                    .map_or(false, |input| input.in_open_source_repo),
103                can_collect_data: false,
104            };
105            let prompt = format_zeta_prompt(&input, version);
106            let prefill = zeta_prompt::get_prefill(&input, version);
107            let (expected_patch, expected_cursor_offset) = example
108                .spec
109                .expected_patches_with_cursor_positions()
110                .into_iter()
111                .next()
112                .context("expected patches is empty")?;
113            let expected_output =
114                zeta2_output_for_patch(&input, &expected_patch, expected_cursor_offset, version)?;
115            let rejected_output = example
116                .spec
117                .rejected_patch
118                .as_ref()
119                .and_then(|patch| zeta2_output_for_patch(&input, patch, None, version).ok());
120
121            example.prompt = Some(ExamplePrompt {
122                input: prompt,
123                expected_output,
124                rejected_output,
125                provider: args.provider,
126                prefill: Some(prefill),
127            });
128        }
129        _ => {
130            panic!("Cannot format prompt for {:?}", args.provider);
131        }
132    };
133    Ok(())
134}
135
136pub fn zeta2_output_for_patch(
137    input: &zeta_prompt::ZetaPromptInput,
138    patch: &str,
139    cursor_offset: Option<usize>,
140    version: ZetaFormat,
141) -> Result<String> {
142    let mut old_editable_region =
143        input.cursor_excerpt[input.editable_range_in_excerpt.clone()].to_string();
144
145    if !old_editable_region.ends_with_newline() {
146        old_editable_region.push('\n');
147    }
148
149    let (mut result, first_hunk_offset) =
150        udiff::apply_diff_to_string_with_hunk_offset(patch, &old_editable_region).with_context(
151            || {
152                format!(
153                    "Patch:\n```\n{}```\n\nEditable region:\n```\n{}```",
154                    patch, old_editable_region
155                )
156            },
157        )?;
158
159    if let Some(cursor_offset) = cursor_offset {
160        // The cursor_offset is relative to the start of the hunk's new text (context + additions).
161        // We need to add where the hunk context matched in the editable region to compute
162        // the actual cursor position in the result.
163        let hunk_start = first_hunk_offset.unwrap_or(0);
164        let offset = result.floor_char_boundary((hunk_start + cursor_offset).min(result.len()));
165        result.insert_str(offset, zeta_prompt::CURSOR_MARKER);
166    }
167
168    match version {
169        ZetaFormat::V0120GitMergeMarkers
170        | ZetaFormat::V0131GitMergeMarkersPrefix
171        | ZetaFormat::V0211SeedCoder => {
172            if !result.ends_with('\n') {
173                result.push('\n');
174            }
175            result.push_str(zeta_prompt::v0120_git_merge_markers::END_MARKER);
176        }
177        _ => (),
178    }
179
180    Ok(result)
181}
182
183pub struct TeacherPrompt;
184
185impl TeacherPrompt {
186    pub(crate) const EDITABLE_REGION_START: &str = "<|editable_region_start|>\n";
187    pub(crate) const EDITABLE_REGION_END: &str = "\n<|editable_region_end|>";
188    pub(crate) const USER_CURSOR_MARKER: &str = "<|user_cursor|>";
189    pub(crate) const NO_EDITS: &str = "NO_EDITS";
190
191    /// Truncate edit history to this number of last lines
192    const MAX_HISTORY_LINES: usize = 128;
193
194    pub fn format_prompt(
195        example: &Example,
196        editable_range: Range<usize>,
197        context_range: Range<usize>,
198    ) -> String {
199        let edit_history = Self::format_edit_history(&example.spec.edit_history);
200        let context = Self::format_context(example);
201        let cursor_excerpt = Self::format_cursor_excerpt(example, editable_range, context_range);
202
203        let prompt_template = crate::prompt_assets::get_prompt("teacher.md");
204        let prompt = prompt_template
205            .replace("{{context}}", &context)
206            .replace("{{edit_history}}", &edit_history)
207            .replace("{{cursor_excerpt}}", &cursor_excerpt);
208
209        prompt
210    }
211
212    pub fn parse(example: &Example, response: &str) -> Result<(String, Option<ActualCursor>)> {
213        // Check if the model indicated no edits are needed
214        let no_edits = (String::new(), None);
215        if let Some(last_codeblock) = extract_last_codeblock(&response) {
216            if last_codeblock.trim() == Self::NO_EDITS {
217                return Ok(no_edits);
218            }
219        }
220
221        if response.trim().ends_with(Self::NO_EDITS) {
222            return Ok(no_edits);
223        }
224
225        // Extract updated (new) editable region from the model response.
226        let new_editable_region = Self::extract_editable_region(&response)?;
227        let cursor_offset = new_editable_region.find(Self::USER_CURSOR_MARKER);
228        let mut new_editable_region = new_editable_region.replace(Self::USER_CURSOR_MARKER, "");
229        let old_editable_region = Self::extract_editable_region(
230            &example
231                .prompt
232                .as_ref()
233                .context("example prompt missing")?
234                .input,
235        )?
236        .replace(Self::USER_CURSOR_MARKER, "");
237
238        let prompt_inputs = example
239            .prompt_inputs
240            .as_ref()
241            .context("example is missing prompt inputs")?;
242
243        // Normalize leading newlines: if old starts with newline but new doesn't,
244        // prepend newline to new to preserve whitespace structure.
245        // This handles the case where the model drops the leading blank line.
246        if old_editable_region.starts_with('\n') && !new_editable_region.starts_with('\n') {
247            new_editable_region.insert(0, '\n');
248        }
249
250        let (editable_region_offset, _) = prompt_inputs
251            .content
252            .match_indices(&old_editable_region)
253            .min_by_key(|(index, _)| index.abs_diff(prompt_inputs.cursor_offset))
254            .context("editable region not found in prompt content")?;
255        let editable_region_start_line = prompt_inputs.content[..editable_region_offset]
256            .matches('\n')
257            .count();
258
259        // Use full context so cursor offset (relative to editable region start) aligns with diff content
260        let editable_region_lines = old_editable_region.lines().count() as u32;
261        let diff = language::unified_diff_with_context(
262            &old_editable_region,
263            &new_editable_region,
264            editable_region_start_line as u32,
265            editable_region_start_line as u32,
266            editable_region_lines,
267        );
268
269        let diff = indoc::formatdoc! {"
270            --- a/{path}
271            +++ b/{path}
272            {diff}",
273            path = example.spec.cursor_path.to_string_lossy(),
274            diff = diff,
275        };
276
277        let actual_cursor = cursor_offset.map(|editable_region_cursor_offset| {
278            ActualCursor::from_editable_region(
279                &example.spec.cursor_path,
280                editable_region_cursor_offset,
281                &new_editable_region,
282                &prompt_inputs.content,
283                editable_region_offset,
284                editable_region_start_line,
285            )
286        });
287
288        Ok((diff, actual_cursor))
289    }
290
291    fn format_edit_history(edit_history: &str) -> String {
292        let lines: Vec<&str> = edit_history.lines().collect();
293
294        if lines.is_empty() {
295            return "(No edit history)".to_string();
296        }
297
298        if lines.len() > Self::MAX_HISTORY_LINES {
299            let truncated = lines[lines.len() - Self::MAX_HISTORY_LINES..].join("\n");
300            format!("{truncated}\n[...truncated...]")
301        } else {
302            lines.join("\n")
303        }
304    }
305
306    pub fn format_context(example: &Example) -> String {
307        let related_files = example
308            .prompt_inputs
309            .as_ref()
310            .and_then(|pi| pi.related_files.as_ref());
311
312        let Some(related_files) = related_files else {
313            return "(No context)".to_string();
314        };
315
316        if related_files.is_empty() {
317            return "(No context)".to_string();
318        }
319
320        let mut prompt = String::new();
321        for file in related_files {
322            let path_str = file.path.to_string_lossy();
323            writeln!(&mut prompt, "`````{path_str}").ok();
324
325            let mut prev_row = 0;
326            for excerpt in &file.excerpts {
327                if excerpt.row_range.start > prev_row {
328                    prompt.push_str("\n");
329                }
330                prompt.push_str(&excerpt.text);
331                prompt.push('\n');
332                prev_row = excerpt.row_range.end;
333            }
334            if prev_row < file.max_row {
335                prompt.push_str("\n");
336            }
337            prompt.push_str("\n`````\n");
338        }
339
340        prompt
341    }
342
343    fn format_cursor_excerpt(
344        example: &Example,
345        editable_range: Range<usize>,
346        context_range: Range<usize>,
347    ) -> String {
348        let mut result = String::new();
349
350        let prompt_inputs = example.prompt_inputs.as_ref().unwrap();
351
352        let path_str = example.spec.cursor_path.to_string_lossy();
353        result.push_str(&format!("`````{path_str}\n"));
354        result.push_str(&prompt_inputs.content[context_range.start..editable_range.start]);
355        result.push_str(Self::EDITABLE_REGION_START);
356        result.push_str(&prompt_inputs.content[editable_range.start..prompt_inputs.cursor_offset]);
357        result.push_str(Self::USER_CURSOR_MARKER);
358        result.push_str(&prompt_inputs.content[prompt_inputs.cursor_offset..editable_range.end]);
359        result.push_str(Self::EDITABLE_REGION_END);
360        result.push_str(&prompt_inputs.content[editable_range.end..context_range.end]);
361        result.push_str("\n`````");
362
363        result
364    }
365
366    pub fn extract_editable_region(text: &str) -> Result<String> {
367        let start = text
368            .rfind(Self::EDITABLE_REGION_START)
369            .map_or(0, |pos| pos + Self::EDITABLE_REGION_START.len());
370        let end = text.rfind(Self::EDITABLE_REGION_END).unwrap_or(text.len());
371
372        if start >= end {
373            return Err(anyhow!("Invalid editable region markers"));
374        }
375
376        let region = &text[start..end];
377        Ok(region.strip_suffix('\n').unwrap_or(region).to_string())
378    }
379}
380
381/// Extract the cursor excerpt from an example.
382/// First tries to extract from an existing prompt, then falls back to constructing from prompt_inputs.
383pub fn extract_cursor_excerpt_from_example(example: &Example) -> Option<String> {
384    // If we have the original prompt, extract the cursor excerpt from it
385    if let Some(prompt) = &example.prompt {
386        // Find "# 3. Current File" section and extract the content
387        if let Some(start) = prompt.input.find("# 3. Current File") {
388            let content_start = prompt.input[start..].find('`').map(|i| start + i)?;
389            let backtick_count = prompt.input[content_start..]
390                .chars()
391                .take_while(|&c| c == '`')
392                .count();
393            let content_start = content_start + backtick_count;
394
395            // Find the path line and skip it
396            let newline_pos = prompt.input[content_start..].find('\n')?;
397            let text_start = content_start + newline_pos + 1;
398
399            // Find the closing backticks
400            let closing_pattern = "`".repeat(backtick_count);
401            let text_end = prompt.input[text_start..].find(&closing_pattern)?;
402            let cursor_excerpt = &prompt.input[text_start..text_start + text_end];
403
404            let path_str = example.spec.cursor_path.to_string_lossy();
405            return Some(format!("`````{path_str}\n{cursor_excerpt}`````"));
406        }
407    }
408
409    // Fallback: construct from prompt_inputs if available
410    let prompt_inputs = example.prompt_inputs.as_ref()?;
411    let content = &prompt_inputs.content;
412    let cursor_offset = prompt_inputs.cursor_offset;
413
414    // Simple fallback: just show content around cursor with markers
415    let path_str = example.spec.cursor_path.to_string_lossy();
416    let mut result = format!("`````{path_str}\n");
417    result.push_str(TeacherPrompt::EDITABLE_REGION_START);
418    result.push_str(&content[..cursor_offset]);
419    result.push_str(TeacherPrompt::USER_CURSOR_MARKER);
420    result.push_str(&content[cursor_offset..]);
421    result.push_str(TeacherPrompt::EDITABLE_REGION_END);
422    result.push_str("\n`````");
423
424    Some(result)
425}
426
427pub(crate) fn extract_last_codeblock(text: &str) -> Option<String> {
428    let lines: Vec<&str> = text.lines().collect();
429
430    // Search from the end for a closing fence (line containing only backticks, 3+)
431    let mut closing_line_idx = None;
432    let mut backtick_count = 0;
433
434    for i in (0..lines.len()).rev() {
435        let line = lines[i].trim();
436        if line.len() >= 3 && line.chars().all(|c| c == '`') {
437            closing_line_idx = Some(i);
438            backtick_count = line.len();
439            break;
440        }
441    }
442
443    let closing_idx = closing_line_idx?;
444
445    // Search backwards for matching opening fence
446    // Opening fence starts with same backtick count, possibly followed by language/metadata
447    let opening_pattern = "`".repeat(backtick_count);
448
449    for i in (0..closing_idx).rev() {
450        let line = lines[i];
451        if line.starts_with(&opening_pattern) {
452            // Ensure it's exactly the right number of backticks (not more)
453            let rest = &line[backtick_count..];
454            if rest.is_empty() || !rest.starts_with('`') {
455                // Found matching opening fence
456                // Extract content between opening and closing (exclusive)
457                if closing_idx > i + 1 {
458                    let content = lines[i + 1..closing_idx].join("\n");
459                    // Preserve trailing newline to match previous behavior
460                    return Some(format!("{}\n", content));
461                } else {
462                    // Empty block
463                    return Some(String::new());
464                }
465            }
466        }
467    }
468
469    None
470}
471
472#[cfg(test)]
473mod tests {
474    use super::*;
475
476    #[test]
477    fn test_extract_last_code_block() {
478        let text = indoc::indoc! {"
479            Some thinking
480
481            ```
482            first block
483            ```
484
485            `````path='something' lines=1:2
486            last block
487            `````
488            "};
489        let last_block = extract_last_codeblock(text).unwrap();
490        assert_eq!(last_block, "last block\n");
491    }
492
493    #[test]
494    fn test_extract_codeblock_with_nested_fences() {
495        let text = indoc::indoc! {"
496            `````
497            content with ``` inline
498            and ```python nested
499            more content
500            `````
501            "};
502        let last_block = extract_last_codeblock(text).unwrap();
503        assert_eq!(
504            last_block,
505            "content with ``` inline\nand ```python nested\nmore content\n"
506        );
507    }
508
509    #[test]
510    fn test_extract_codeblock_ignores_inline_backticks() {
511        let text = indoc::indoc! {"
512            `````
513            here is some `code` with inline backticks
514            and here```more```stuff
515            `````
516            "};
517        let last_block = extract_last_codeblock(text).unwrap();
518        assert_eq!(
519            last_block,
520            "here is some `code` with inline backticks\nand here```more```stuff\n"
521        );
522    }
523
524    #[test]
525    fn test_extract_editable_region() {
526        let text = indoc::indoc! {"
527            some lines
528            are
529            here
530            <|editable_region_start|>
531            one
532            two three
533
534            <|editable_region_end|>
535            more
536            lines here
537            "};
538        let parsed = TeacherPrompt::extract_editable_region(text).unwrap();
539        assert_eq!(
540            parsed,
541            indoc::indoc! {"
542            one
543            two three"}
544        );
545    }
546
547    #[test]
548    fn test_extract_last_codeblock_nested_bibtex() {
549        let text = indoc::indoc! {r#"
550            Looking at the edit history, I can see that a Citation section was just added.
551
552            `````
553            ## Collaborations
554            Our mission is to create a 4D generative model.
555
556            ## Citation
557
558            If you found Unique3D helpful, please cite our report:
559            ```bibtex
560            @misc{wu2024unique3d,
561                  title={Unique3D},
562            }
563            ```
564            `````
565            "#};
566        let last_block = extract_last_codeblock(text).unwrap();
567        assert_eq!(
568            last_block,
569            indoc::indoc! {r#"
570            ## Collaborations
571            Our mission is to create a 4D generative model.
572
573            ## Citation
574
575            If you found Unique3D helpful, please cite our report:
576            ```bibtex
577            @misc{wu2024unique3d,
578                  title={Unique3D},
579            }
580            ```
581            "#}
582        );
583    }
584
585    #[test]
586    fn test_extract_editable_region_no_markers() {
587        let text = indoc::indoc! {"
588            one
589            two three"};
590        let parsed = TeacherPrompt::extract_editable_region(text).unwrap();
591        assert_eq!(
592            parsed,
593            indoc::indoc! {"
594            one
595            two three"}
596        );
597    }
598
599    #[test]
600    fn test_parse_no_edits_response() {
601        let response = indoc::indoc! {"
602            The code is already complete. There is no clear next edit to make.
603
604            `````
605            NO_EDITS
606            `````
607        "};
608        let codeblock = extract_last_codeblock(response).unwrap();
609        assert_eq!(codeblock.trim(), TeacherPrompt::NO_EDITS);
610    }
611
612    #[test]
613    fn test_extract_codeblock_no_valid_block() {
614        // Text with no code blocks should return None
615        let text = "Just some plain text without any code blocks";
616        assert!(extract_last_codeblock(text).is_none());
617
618        // Unclosed code block should return None
619        let text = indoc::indoc! {"
620            ```
621            unclosed block
622        "};
623        assert!(extract_last_codeblock(text).is_none());
624
625        // Analysis text with nested markdown but no proper outer block
626        let text = indoc::indoc! {"
627            # Analysis
628            Looking at this:
629            ```
630            some code
631            ```
632            But then more analysis without wrapping block
633        "};
634        // This should find the inner block
635        let result = extract_last_codeblock(text).unwrap();
636        assert_eq!(result, "some code\n");
637    }
638
639    #[test]
640    fn test_extract_codeblock_no_trailing_newline() {
641        // Text ending without trailing newline after closing fence
642        let text = "`````\ncontent here\n`````";
643        let result = extract_last_codeblock(text).unwrap();
644        assert_eq!(result, "content here\n");
645    }
646}