ronantakizawa/codereview-bench huggingface dataset:
Repository: TabbyML/tabby
File: crates/tabby/src/routes/models.rs (Rust)

Code before:
use std::sync::Arc;

use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub struct ModelInfo {
    completion: Option<Vec<String>>,
    chat: Option<Vec<String>>,
}

#[utoipa::path(
    get,
    path = "/v1beta/models",
    tag = "v1beta",
    operation_id = "config",
    responses(
        (status = 200, description = "Success", body = ServerSetting, content_type = "application/json"),
    ),
    security(
        ("token" = [])
    )
)]
pub async fn models(State(state): State<Arc<tabby_common::config::Config>>) -> Json<ModelInfo> {
    let models = state.as_ref().clone().model;
    let mut http_model_configs: ModelInfo = ModelInfo {
        completion: None,
        chat: None,
    };

    if let Some(tabby_common::config::ModelConfig::Http(completion_http_config)) = models.completion
    {
        if let Some(models) = completion_http_config.supported_models {
            http_model_configs.completion = Some(models.clone());
        }
    }

    if let Some(tabby_common::config::ModelConfig::Http(chat_http_config)) = models.chat {
        if let Some(models) = chat_http_config.supported_models {
            http_model_configs.chat = Some(models.clone());
        }
    }

    Json(http_model_configs)
}


Review comment (suggestion):
```suggestion
pub async fn models(State(state): State<Arc<ModelInfo>) -> Json<ModelInfo> {
```

Code after:
use std::sync::Arc;

use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub struct ModelInfo {
    completion: Option<Vec<String>>,
    chat: Option<Vec<String>>,
}

impl From<tabby_common::config::Config> for ModelInfo {
    fn from(value: tabby_common::config::Config) -> Self {
        let models = value.model;
        let mut http_model_configs: ModelInfo = ModelInfo {
            completion: None,
            chat: None,
        };

        if let Some(tabby_common::config::ModelConfig::Http(completion_http_config)) =
            models.completion
        {
            if let Some(models) = completion_http_config.supported_models {
                http_model_configs.completion = Some(models.clone());
            }
        }

        if let Some(tabby_common::config::ModelConfig::Http(chat_http_config)) = models.chat {
            if let Some(models) = chat_http_config.supported_models {
                http_model_configs.chat = Some(models.clone());
            }
        }

        http_model_configs
    }
}

#[utoipa::path(
    get,
    path = "/v1beta/models",
    tag = "v1beta",
    operation_id = "config",
    responses(
        (status = 200, description = "Success", body = ServerSetting, content_type = "application/json"),
    ),
    security(
        ("token" = [])
    )
)]
Repository: TabbyML/tabby
File: clients/vscode/src/code-action/QuickFix.ts (TypeScript)

Code before:
      return;
    }
    if (context.diagnostics.length === 0) {
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Fix with Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Fix with Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain with Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain with Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}


Review comment (suggestion):
```suggestion
    const explainError = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
```

Code after:
      return;
    }
    if (context.diagnostics.length === 0) {
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Fix using Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Fix using Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain using Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}
Repository: TabbyML/tabby
File: clients/vscode/src/code-action/QuickFix.ts (TypeScript)

Code before:
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Fix with Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Fix with Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain with Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain with Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}


Review comment (suggestion):
```suggestion
      title: "Explain using Tabby",
```

Code after:
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Fix using Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Fix using Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain using Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}
Repository: TabbyML/tabby
File: clients/vscode/src/code-action/QuickFix.ts (TypeScript)

Code before:
    context: CodeActionContext,
    token: CancellationToken,
  ): CodeAction[] | undefined {
    if (token.isCancellationRequested) {
      return;
    }

    if (!this.contextVariables.chatEnabled) {
      return;
    }
    if (context.diagnostics.length === 0) {
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Explain using Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain using Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}


Review comment (suggestion):
```suggestion
      title: "Fix using Tabby",
```

Code after:
    context: CodeActionContext,
    token: CancellationToken,
  ): CodeAction[] | undefined {
    if (token.isCancellationRequested) {
      return;
    }

    if (!this.contextVariables.chatEnabled) {
      return;
    }
    if (context.diagnostics.length === 0) {
      return [];
    }

    const lspErrors = context.diagnostics
      .map((diagnostic, idx) => "Error " + idx + ": " + diagnostic.message)
      .join("\n");
    const quickFixCmd = `Here is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide the correct command to fix the error.`;
    getLogger("QuickFixCodeActionProvider").info("lspErrors", lspErrors);

    const quickFixEditing = new CodeAction("Fix using Tabby", CodeActionKind.QuickFix);
    quickFixEditing.command = {
      command: "tabby.chat.edit.start",
      title: "Fix using Tabby",
      arguments: [quickFixCmd],
    };

    const explainErrorCmd = `\nHere is some error information that occurred in the selection:
                        ${lspErrors}
                        Please provide an explanation for the error.`;

    const explainError = new CodeAction("Explain using Tabby", CodeActionKind.QuickFix);
    explainError.command = {
      command: "tabby.chat.explainCodeBlock",
      title: "Explain using Tabby",
      arguments: [explainErrorCmd],
    };

    return [quickFixEditing, explainError];
  }
}
Repository: TabbyML/tabby
File: crates/tabby-common/src/config.rs (Rust)

Code before:
    ///   - llama.cpp/completion: llama.cpp `/completion` API.
    /// 2. Chat API: [ChatCompletionStream](tabby_inference::ChatCompletionStream)
    ///   - openai-chat: OpenAI /v1/chat/completions API.
    /// 3. Embedding API [Embedding](tabby_inference::Embedding)
    ///   - llama.cpp/embedding: llama.cpp `/embedding` API.
    pub kind: String,

    pub api_endpoint: Option<String>,

    #[builder(default)]
    pub api_key: Option<String>,

    /// Used by OpenAI style API for model name.
    #[builder(default)]
    pub model_name: Option<String>,

    /// Used by Completion API to construct a completion model.
    #[builder(default)]
    pub prompt_template: Option<String>,

    /// Used by Completion API to construct a chat model.
    #[builder(default)]
    pub chat_template: Option<String>,

    #[builder(default)]
    pub stop_words: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct LocalModelConfig {
    pub model_id: String,

    #[serde(default = "default_parallelism")]
    pub parallelism: u8,

    #[serde(default = "default_num_gpu_layers")]
    pub num_gpu_layers: u16,

    #[serde(default)]
    pub enable_fast_attention: Option<bool>,

    #[serde(default = "default_context_size")]
    pub context_size: usize,

    #[serde(default)]
    pub stop_words: Vec<String>,
}

fn default_parallelism() -> u8 {
    1
}

Review comment (suggestion):
```suggestion
    pub additional_stop_words: Vec<String>,
```

Code after:
    ///   - llama.cpp/completion: llama.cpp `/completion` API.
    /// 2. Chat API: [ChatCompletionStream](tabby_inference::ChatCompletionStream)
    ///   - openai-chat: OpenAI /v1/chat/completions API.
    /// 3. Embedding API [Embedding](tabby_inference::Embedding)
    ///   - llama.cpp/embedding: llama.cpp `/embedding` API.
    pub kind: String,

    pub api_endpoint: Option<String>,

    #[builder(default)]
    pub api_key: Option<String>,

    /// Used by OpenAI style API for model name.
    #[builder(default)]
    pub model_name: Option<String>,

    /// Used by Completion API to construct a completion model.
    #[builder(default)]
    pub prompt_template: Option<String>,

    /// Used by Completion API to construct a chat model.
    #[builder(default)]
    pub chat_template: Option<String>,

    /// Used by Chat/Completion API allowing users to get supported models info.
    #[builder(default)]
    pub supported_models: Option<Vec<String>>,

    #[builder(default)]
    pub additional_stop_words: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct LocalModelConfig {
    pub model_id: String,

    #[serde(default = "default_parallelism")]
    pub parallelism: u8,

    #[serde(default = "default_num_gpu_layers")]
    pub num_gpu_layers: u16,

    #[serde(default)]
    pub enable_fast_attention: Option<bool>,

    #[serde(default = "default_context_size")]
    pub context_size: usize,

    #[serde(default)]
    pub additional_stop_words: Option<Vec<String>>,
}
Repository: TabbyML/tabby
File: crates/tabby-inference/src/decoding.rs (Rust)

Code before:
use dashmap::DashMap;
use tabby_common::languages::Language;
use trie_rs::{Trie, TrieBuilder};

pub struct StopConditionFactory {
    stop_trie_cache: DashMap<String, Trie<u8>>,
    model_stop_words: Vec<String>,
}

fn reverse<T>(s: T) -> String
where
    T: Into<String>,
{
    s.into().chars().rev().collect()
}

impl Default for StopConditionFactory {
    fn default() -> Self {
        Self {
            stop_trie_cache: DashMap::new(),
            model_stop_words: vec![],
        }
    }
}

type CachedTrie<'a> = dashmap::mapref::one::Ref<'a, String, Trie<u8>>;

impl StopConditionFactory {
    pub fn with_stop_words(stop_words: Vec<String>) -> Self {
        Self {
            stop_trie_cache: DashMap::new(),
            model_stop_words: stop_words,

Review comment (suggestion):
```suggestion
    stop_words_from_model_config: Vec<String>,
```

Code after:
use dashmap::DashMap;
use tabby_common::languages::Language;
use trie_rs::{Trie, TrieBuilder};

pub struct StopConditionFactory {
    stop_trie_cache: DashMap<String, Trie<u8>>,
    stop_words_from_model_config: Vec<String>,
}

fn reverse<T>(s: T) -> String
where
    T: Into<String>,
{
    s.into().chars().rev().collect()
}

impl Default for StopConditionFactory {
    fn default() -> Self {
        Self {
            stop_trie_cache: DashMap::new(),
            stop_words_from_model_config: vec![],
        }
    }
}

type CachedTrie<'a> = dashmap::mapref::one::Ref<'a, String, Trie<u8>>;

impl StopConditionFactory {
    pub fn with_stop_words(stop_words: Vec<String>) -> Self {
        Self {
            stop_trie_cache: DashMap::new(),
            stop_words_from_model_config: stop_words,
Repository: TabbyML/tabby
File: crates/tabby-inference/src/decoding.rs (Rust)

Code before:
            "
".to_owned(), // qwen 2.5 coder style
        ]);
        assert!(!trie.common_prefix_search(&text).is_empty());

        let qwen25coder = reverse("qwen25 style stop words;
");
        assert!(!trie.common_prefix_search(&qwen25coder).is_empty());
    }

    #[test]
    fn test_stop_condition_max_length() {
        let factory = StopConditionFactory::default();
        let mut cond = factory.create("", Some(&UNKNOWN_LANGUAGE));
        let (should_stop, _) = cond.should_stop("1");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("2");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("3");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("4");
        assert!(!should_stop)
    }

    #[test]
    fn test_stop_condition_additional_stop_words() {
        let factory = StopConditionFactory::with_stop_words(vec!["
".to_owned()]);
        let mut cond = factory.create("additional_stop_words", Some(&UNKNOWN_LANGUAGE));
        let (should_stop, _) = cond.should_stop("1");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("
");
        assert!(should_stop);
    }
}


Review comment (suggestion):
```suggestion
        let mut cond = factory.create("", Some(&UNKNOWN_LANGUAGE));
```

Code after:
            "
".to_owned(), // qwen 2.5 coder style
        ]);
        assert!(!trie.common_prefix_search(&text).is_empty());

        let qwen25coder = reverse("qwen25 style stop words;
");
        assert!(!trie.common_prefix_search(&qwen25coder).is_empty());
    }

    #[test]
    fn test_stop_condition_max_length() {
        let factory = StopConditionFactory::default();
        let mut cond = factory.create("", Some(&UNKNOWN_LANGUAGE));
        let (should_stop, _) = cond.should_stop("1");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("2");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("3");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("4");
        assert!(!should_stop)
    }

    #[test]
    fn test_stop_condition_additional_stop_words() {
        let factory = StopConditionFactory::with_stop_words(vec!["
".to_owned()]);
        let mut cond = factory.create("", Some(&UNKNOWN_LANGUAGE));
        let (should_stop, _) = cond.should_stop("1");
        assert!(!should_stop);
        let (should_stop, _) = cond.should_stop("
");
        assert!(should_stop);
    }
}
Repository: TabbyML/tabby
File: clients/tabby-agent/src/codeSearch/fileTracker.ts (TypeScript)

Code before:
import { Connection, Range } from "vscode-languageserver";
import { Feature } from "../feature";
import { OpenedFileParams, OpenedFileRequest, ServerCapabilities } from "../protocol";
import { getLogger } from "../logger";
import { Configurations } from "../config";

interface OpenedFile {
  uri: string;
  //order by range, the left most is the most recent one
  lastVisibleRange: Range[];
  invisible: boolean;
  isActive: boolean;
}

export class LRUList {
  private list: OpenedFile[] = [];

  constructor(private readonly maxSize: number) {}

  insert(file: OpenedFile): void {
    const existingIndex = this.list.findIndex((item) => item.uri === file.uri);
    if (existingIndex !== -1) {
      const existingFile = this.list[existingIndex];
      if (!existingFile) {
        return;
      }
      existingFile.invisible = file.invisible;
      this.list.splice(existingIndex, 1);
      this.list.unshift(existingFile);
    } else {
      this.list.unshift(file);
    }

    if (this.list.length > this.maxSize) {
      this.removeLast();
    }
  }

  removeLast(): OpenedFile | undefined {
    return this.list.pop();

Review comment (performance):
I think we can use the `LRUCache` class from an imported lib. 
You can refer to an example at https://github.com/TabbyML/tabby/blob/main/clients/tabby-agent/src/codeCompletion/cache.ts#L11

Code after:
import { Connection, Range } from "vscode-languageserver";
import { Feature } from "../feature";
import { DidChangeActiveEditorNotification, DidChangeActiveEditorParams, ServerCapabilities } from "../protocol";
import { Configurations } from "../config";
import { LRUCache } from "lru-cache";
import { isRangeEqual } from "../utils/range";

interface OpenedFile {
  uri: string;
  //order by range, the left most is the most recent one
  lastVisibleRange: Range[];
  invisible: boolean;
  isActive: boolean;
}

export class FileTracker implements Feature {
  private fileList = new LRUCache<string, OpenedFile>({
    max: this.configurations.getMergedConfig().completion.prompt.collectSnippetsFromRecentOpenedFiles.maxOpenedFiles,
  });

  constructor(private readonly configurations: Configurations) {}
  initialize(connection: Connection): ServerCapabilities | Promise<ServerCapabilities> {
    connection.onNotification(DidChangeActiveEditorNotification.type, (param: DidChangeActiveEditorParams) => {
      this.resolveChangedFile(param);
    });
    return {};
  }

  resolveChangedFile(param: DidChangeActiveEditorParams) {
    const { activeEditor, visibleEditors } = param;

    const visitedPaths = new Set<string>();

    //get all visible editors
    if (visibleEditors) {
      visibleEditors.forEach((editor) => {
        const visibleFile = this.fileList.get(editor.uri);
        if (visibleFile) {
          visibleFile.lastVisibleRange = [];
        }