prompts.rs

  1use language::BufferSnapshot;
  2use std::{fmt::Write, ops::Range};
  3
  4pub fn generate_content_prompt(
  5    user_prompt: String,
  6    language_name: Option<&str>,
  7    buffer: BufferSnapshot,
  8    range: Range<usize>,
  9    _project_name: Option<String>,
 10) -> anyhow::Result<String> {
 11    let mut prompt = String::new();
 12
 13    let content_type = match language_name {
 14        None | Some("Markdown" | "Plain Text") => {
 15            writeln!(
 16                prompt,
 17                "Here's a file of text that I'm going to ask you to make an edit to."
 18            )?;
 19            "text"
 20        }
 21        Some(language_name) => {
 22            writeln!(
 23                prompt,
 24                "Here's a file of {language_name} that I'm going to ask you to make an edit to."
 25            )?;
 26            "code"
 27        }
 28    };
 29
 30    const MAX_CTX: usize = 50000;
 31    let mut is_truncated = false;
 32    if range.is_empty() {
 33        prompt.push_str("The point you'll need to insert at is marked with <insert_here></insert_here>.\n\n<document>");
 34    } else {
 35        prompt.push_str("The section you'll need to rewrite is marked with <rewrite_this></rewrite_this> tags.\n\n<document>");
 36    }
 37    // Include file content.
 38    let before_range = 0..range.start;
 39    let truncated_before = if before_range.len() > MAX_CTX {
 40        is_truncated = true;
 41        range.start - MAX_CTX..range.start
 42    } else {
 43        before_range
 44    };
 45    let mut non_rewrite_len = truncated_before.len();
 46    for chunk in buffer.text_for_range(truncated_before) {
 47        prompt.push_str(chunk);
 48    }
 49    if !range.is_empty() {
 50        prompt.push_str("<rewrite_this>\n");
 51        for chunk in buffer.text_for_range(range.clone()) {
 52            prompt.push_str(chunk);
 53        }
 54        prompt.push_str("\n<rewrite_this>");
 55    } else {
 56        prompt.push_str("<insert_here></insert_here>");
 57    }
 58    let after_range = range.end..buffer.len();
 59    let truncated_after = if after_range.len() > MAX_CTX {
 60        is_truncated = true;
 61        range.end..range.end + MAX_CTX
 62    } else {
 63        after_range
 64    };
 65    non_rewrite_len += truncated_after.len();
 66    for chunk in buffer.text_for_range(truncated_after) {
 67        prompt.push_str(chunk);
 68    }
 69
 70    write!(prompt, "</document>\n\n").unwrap();
 71
 72    if is_truncated {
 73        writeln!(prompt, "The context around the relevant section has been truncated (possibly in the middle of a line) for brevity.\n")?;
 74    }
 75
 76    if range.is_empty() {
 77        writeln!(
 78                prompt,
 79                "You can't replace {content_type}, your answer will be inserted in place of the `<insert_here></insert_here>` tags. Don't include the insert_here tags in your output.",
 80            )
 81            .unwrap();
 82        writeln!(
 83                prompt,
 84                "Generate {content_type} based on the following prompt:\n\n<prompt>\n{user_prompt}\n</prompt>",
 85            )
 86            .unwrap();
 87        writeln!(prompt, "Match the indentation in the original file in the inserted {content_type}, don't include any indentation on blank lines.\n").unwrap();
 88        prompt.push_str("Immediately start with the following format with no remarks:\n\n```\n{{INSERTED_CODE}}\n```");
 89    } else {
 90        writeln!(prompt, "Edit the section of {content_type} in <rewrite_this></rewrite_this> tags based on the following prompt:'").unwrap();
 91        writeln!(prompt, "\n<prompt>\n{user_prompt}\n</prompt>\n").unwrap();
 92        let rewrite_len = range.end - range.start;
 93        if rewrite_len < 20000 && rewrite_len * 2 < non_rewrite_len {
 94            writeln!(prompt, "And here's the section to rewrite based on that prompt again for reference:\n\n<rewrite_this>\n").unwrap();
 95            for chunk in buffer.text_for_range(range.clone()) {
 96                prompt.push_str(chunk);
 97            }
 98            writeln!(prompt, "\n</rewrite_this>\n").unwrap();
 99        }
100        writeln!(prompt, "Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding {content_type} will be preserved.\n").unwrap();
101        write!(
102            prompt,
103            "Start at the indentation level in the original file in the rewritten {content_type}. "
104        )
105        .unwrap();
106        prompt.push_str("Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.");
107        prompt.push_str("\n\nImmediately start with the following format with no remarks:\n\n```\n{{REWRITTEN_CODE}}\n```");
108    }
109
110    Ok(prompt)
111}
112
113pub fn generate_terminal_assistant_prompt(
114    user_prompt: &str,
115    shell: Option<&str>,
116    working_directory: Option<&str>,
117) -> String {
118    let mut prompt = String::new();
119    writeln!(&mut prompt, "You are an expert terminal user.").unwrap();
120    writeln!(&mut prompt, "You will be given a description of a command and you need to respond with a command that matches the description.").unwrap();
121    writeln!(&mut prompt, "Do not include markdown blocks or any other text formatting in your response, always respond with a single command that can be executed in the given shell.").unwrap();
122    if let Some(shell) = shell {
123        writeln!(&mut prompt, "Current shell is '{shell}'.").unwrap();
124    }
125    if let Some(working_directory) = working_directory {
126        writeln!(
127            &mut prompt,
128            "Current working directory is '{working_directory}'."
129        )
130        .unwrap();
131    }
132    writeln!(&mut prompt, "Here is the description of the command:").unwrap();
133    prompt.push_str(user_prompt);
134    prompt
135}