Repository: TabbyML/tabby
File: ee/tabby-webserver/src/service/answer.rs (Rust)

Code before:
        .enumerate()
        .map(|(i, snippet)| format!("[[citation:{}]]\n{}", i + 1, *snippet))
        .collect();

    let context = citations.join("\n\n");

    format!(
        r#"You are given a user question, and please write clean, concise and accurate answer to the question. You will be given a set of related contexts to the question, each starting with a reference number like [[citation:x]], where x is a number. Please use the context and cite the context at the end of each sentence if applicable.

Your answer must be correct, accurate and written by an expert using an unbiased and professional tone. Please limit to 1024 tokens. Do not give any information that is not related to the question, and do not repeat. Say "information is missing on" followed by the related topic, if the given context do not provide sufficient information.

Please cite the contexts with the reference numbers, in the format [[citation:x]]. If a sentence comes from multiple contexts, please list all applicable citations, like [[citation:3]][[citation:5]]. Other than code and specific names and citations, your answer must be written in the same language as the question.

Here are the set of contexts:

{context}

Remember, don't blindly repeat the contexts verbatim. When possible, give code snippet to demonstrate the answer. And here is the user question:

{user_input}
"#
    )
}

/// Merges code snippets from search hits, potentially combining multiple hits from the same file.
/// If multiple hits are found in the same file, the function will attempt to merge them into a single hit.
pub async fn merge_code_snippets(
    repository: Arc<dyn RepositoryService>,
    hits: Vec<CodeSearchHit>,
    policy: &AccessPolicy,
    source_id: &str,
) -> Vec<CodeSearchHit> {
    // group hits by filepath
    let mut file_hits: HashMap<String, Vec<CodeSearchHit>> = HashMap::new();
    for hit in hits.into_iter() {
        file_hits
            .entry(hit.doc.filepath.clone())
            .or_default()
            .push(hit);
    }

    let mut result = Vec::with_capacity(file_hits.len());

    for (filepath, file_hits) in file_hits {
        if file_hits.len() > 1 {
            // multiple hits in the same file, attempt to merge
            let repo = match repository
                .resolve_repository_by_source_id(policy, source_id)
                .await
            {
                Ok(repo) => repo,

Review comment (suggestion):
```suggestion
/// Combine code snippets from search results rather than utilizing multiple hits: Presently, there is only one rule: if the number of lines of code (LoC) is less than 200, and there are multiple hits (number of hits > 1), include the entire file.
```

Code after:
                }),
        )
        .chain(assistant_attachment.code.iter().map(|snippet| {
            format!(
                "```{} title=\"{}\"\n{}\n```",
                snippet.language, snippet.filepath, snippet.content
            )
        }))
        .collect();

    let citations: Vec<String> = snippets
        .iter()
        .enumerate()
        .map(|(i, snippet)| format!("[[citation:{}]]\n{}", i + 1, *snippet))
        .collect();

    let context = citations.join("\n\n");

    format!(
        r#"You are given a user question, and please write clean, concise and accurate answer to the question. You will be given a set of related contexts to the question, each starting with a reference number like [[citation:x]], where x is a number. Please use the context and cite the context at the end of each sentence if applicable.

Your answer must be correct, accurate and written by an expert using an unbiased and professional tone. Please limit to 1024 tokens. Do not give any information that is not related to the question, and do not repeat. Say "information is missing on" followed by the related topic, if the given context do not provide sufficient information.

Please cite the contexts with the reference numbers, in the format [[citation:x]]. If a sentence comes from multiple contexts, please list all applicable citations, like [[citation:3]][[citation:5]]. Other than code and specific names and citations, your answer must be written in the same language as the question.

Here are the set of contexts:

{context}

Remember, don't blindly repeat the contexts verbatim. When possible, give code snippet to demonstrate the answer. And here is the user question:

{user_input}
"#
    )
}

/// Combine code snippets from search results rather than utilizing multiple hits: Presently, there is only one rule: if the number of lines of code (LoC) is less than 200, and there are multiple hits (number of hits > 1), include the entire file.
pub async fn merge_code_snippets(
    repository: Option<Repository>,
    hits: Vec<CodeSearchHit>,
) -> Vec<CodeSearchHit> {
    let Some(repository) = repository else {
        return hits;
    };

    // group hits by filepath
    let mut file_hits: HashMap<String, Vec<CodeSearchHit>> = HashMap::new();
    for hit in hits.clone().into_iter() {
        let key = format!("{}-{}", repository.source_id, hit.doc.filepath);
        file_hits.entry(key).or_default().push(hit);
    }