ronantakizawa/codereview-bench huggingface dataset:
Repository: TabbyML/tabby
File: crates/tabby-common/src/api/structured_doc.rs (Rust)

Code before:

#[async_trait]
pub trait DocSearch: Send + Sync {
    /// Search docs from underlying index.
    ///
    /// * `source_ids`: Filter documents by source IDs, when empty, search all sources.
    async fn search(
        &self,
        source_ids: &[String],
        q: &str,
        limit: usize,
    ) -> Result<DocSearchResponse, DocSearchError>;
}

#[derive(Clone)]
pub struct DocSearchWebDocument {
    pub title: String,
    pub link: String,
    pub snippet: String,
}

#[derive(Clone)]
pub struct DocSearchIssueDocument {
    pub title: String,
    pub link: String,
    pub author_email: String,
    pub body: String,
    pub closed: bool,
}

#[derive(Clone)]
pub struct DocSearchPullDocument {
    pub title: String,
    pub link: String,
    pub author_email: String,
    pub body: String,
    pub diff: String,
    pub merged: bool,
}

pub trait FromTantivyDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self>
    where
        Self: Sized;
}

impl FromTantivyDocument for DocSearchDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self> {
        let schema = IndexSchema::instance();
        let kind = get_json_text_field(doc, schema.field_attributes, structured_doc::fields::KIND);


Review comment (security):
```suggestion
    pub author_email: Option<String>,
```

Code after:

#[async_trait]
pub trait DocSearch: Send + Sync {
    /// Search docs from underlying index.
    ///
    /// * `source_ids`: Filter documents by source IDs, when empty, search all sources.
    async fn search(
        &self,
        source_ids: &[String],
        q: &str,
        limit: usize,
    ) -> Result<DocSearchResponse, DocSearchError>;
}

#[derive(Clone)]
pub struct DocSearchWebDocument {
    pub title: String,
    pub link: String,
    pub snippet: String,
}

#[derive(Clone)]
pub struct DocSearchIssueDocument {
    pub title: String,
    pub link: String,
    pub author_email: Option<String>,
    pub body: String,
    pub closed: bool,
}

#[derive(Clone)]
pub struct DocSearchPullDocument {
    pub title: String,
    pub link: String,
    pub author_email: Option<String>,
    pub body: String,
    pub diff: String,
    pub merged: bool,
}

pub trait FromTantivyDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self>
    where
        Self: Sized;
}

impl FromTantivyDocument for DocSearchDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self> {
        let schema = IndexSchema::instance();
        let kind = get_json_text_field(doc, schema.field_attributes, structured_doc::fields::KIND);
Repository: TabbyML/tabby
File: crates/tabby-common/src/api/structured_doc.rs (Rust)

Code before:
        q: &str,
        limit: usize,
    ) -> Result<DocSearchResponse, DocSearchError>;
}

#[derive(Clone)]
pub struct DocSearchWebDocument {
    pub title: String,
    pub link: String,
    pub snippet: String,
}

#[derive(Clone)]
pub struct DocSearchIssueDocument {
    pub title: String,
    pub link: String,
    pub author_email: String,
    pub body: String,
    pub closed: bool,
}

#[derive(Clone)]
pub struct DocSearchPullDocument {
    pub title: String,
    pub link: String,
    pub author_email: String,
    pub body: String,
    pub diff: String,
    pub merged: bool,
}

pub trait FromTantivyDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self>
    where
        Self: Sized;
}

impl FromTantivyDocument for DocSearchDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self> {
        let schema = IndexSchema::instance();
        let kind = get_json_text_field(doc, schema.field_attributes, structured_doc::fields::KIND);

        match kind {
            "web" => {
                DocSearchWebDocument::from_tantivy_document(doc, chunk).map(DocSearchDocument::Web)
            }
            "issue" => DocSearchIssueDocument::from_tantivy_document(doc, chunk)
                .map(DocSearchDocument::Issue),
            "pull" => DocSearchPullDocument::from_tantivy_document(doc, chunk)
                .map(DocSearchDocument::Pull),
            _ => None,

Review comment (security):
```suggestion
    pub author_email: Option<String>,
```

Code after:
        q: &str,
        limit: usize,
    ) -> Result<DocSearchResponse, DocSearchError>;
}

#[derive(Clone)]
pub struct DocSearchWebDocument {
    pub title: String,
    pub link: String,
    pub snippet: String,
}

#[derive(Clone)]
pub struct DocSearchIssueDocument {
    pub title: String,
    pub link: String,
    pub author_email: Option<String>,
    pub body: String,
    pub closed: bool,
}

#[derive(Clone)]
pub struct DocSearchPullDocument {
    pub title: String,
    pub link: String,
    pub author_email: Option<String>,
    pub body: String,
    pub diff: String,
    pub merged: bool,
}

pub trait FromTantivyDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self>
    where
        Self: Sized;
}

impl FromTantivyDocument for DocSearchDocument {
    fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self> {
        let schema = IndexSchema::instance();
        let kind = get_json_text_field(doc, schema.field_attributes, structured_doc::fields::KIND);

        match kind {
            "web" => {
                DocSearchWebDocument::from_tantivy_document(doc, chunk).map(DocSearchDocument::Web)
            }
            "issue" => DocSearchIssueDocument::from_tantivy_document(doc, chunk)
                .map(DocSearchDocument::Issue),
            "pull" => DocSearchPullDocument::from_tantivy_document(doc, chunk)
                .map(DocSearchDocument::Pull),
            _ => None,
Repository: TabbyML/tabby
File: ee/tabby-db/src/notifications.rs (Rust)

Code before:
pub struct NotificationDAO {
    pub id: i64,

    pub recipient: String,
    pub content: String,
    pub read: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl DbConn {
    pub async fn create_notification(&self, recipient: &str, content: &str) -> Result<i64> {
        let res = query!(
            "INSERT INTO notifications (recipient, content) VALUES (?, ?)",
            recipient,
            content
        )
        .execute(&self.pool)
        .await?;

        Ok(res.last_insert_rowid())
    }

    pub async fn mark_notification_read(&self, id: i64, user_id: i64) -> Result<()> {
        query!(
            "INSERT INTO read_notifications (notification_id, user_id)
            SELECT ?, ?
            WHERE NOT EXISTS (
                SELECT 1 FROM read_notifications WHERE notification_id = ? AND user_id = ?
            )",
            id,
            user_id,
            id,
            user_id
        )
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    pub async fn mark_all_notifications_read_by_user(&self, user_id: i64) -> Result<()> {
        let user = self
            .get_user(user_id)
            .await?
            .context("User doesn't exist")?;
        let recipient_clause = if user.is_admin {
            format!(
                "recipient = '{}' OR recipient = '{}'",
                NOTIFICATION_RECIPIENT_ALL_USER, NOTIFICATION_RECIPIENT_ADMIN
            )

Review comment (suggestion):
you might use `ON CONFLICT` query to handle this situation:

https://www.sqlite.org/lang_upsert.html

Code after:
pub struct NotificationDAO {
    pub id: i64,

    pub recipient: String,
    pub content: String,
    pub read: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl DbConn {
    pub async fn create_notification(&self, recipient: &str, content: &str) -> Result<i64> {
        let res = query!(
            "INSERT INTO notifications (recipient, content) VALUES (?, ?)",
            recipient,
            content
        )
        .execute(&self.pool)
        .await?;

        Ok(res.last_insert_rowid())
    }

    pub async fn mark_notification_read(&self, id: i64, user_id: i64) -> Result<()> {
        query!(
            "INSERT INTO read_notifications (notification_id, user_id)
             VALUES (?, ?)
             ON CONFLICT (notification_id, user_id)
             DO NOTHING",
            id,
            user_id,
        )
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    pub async fn mark_all_notifications_read_by_user(&self, user_id: i64) -> Result<()> {
        let user = self
            .get_user(user_id)
            .await?
            .context("User doesn't exist")?;
        let recipient_clause = if user.is_admin {
            format!(
                "recipient = '{}' OR recipient = '{}'",
                NOTIFICATION_RECIPIENT_ALL_USER, NOTIFICATION_RECIPIENT_ADMIN
            )
        } else {
            format!("recipient = '{}'", NOTIFICATION_RECIPIENT_ALL_USER)
        };
Repository: TabbyML/tabby
File: crates/tabby-index/src/code/mod.rs (Rust)

Code before:
                    code::fields::CHUNK_BODY: body,
                    code::fields::CHUNK_START_LINE: start_line,
                });

                let embedding = embedding.clone();
                let rewritten_body = format!("```{}\n{}\n```", source_code.filepath, body);
                yield tokio::spawn(async move {
                    match build_binarize_embedding_tokens(embedding.clone(), &rewritten_body).await {
                        Ok(tokens) => Ok((tokens, attributes)),
                        Err(err) => Err(err),
                    }
                });
            }
        };

        Box::pin(s)
    }
}

async fn build_binarize_embedding_tokens(
    embedding: Arc<dyn Embedding>,
    body: &str,
) -> Result<Vec<String>> {
    let embedding = match embedding
        .embed(body)
        .instrument(info_span!("embed", corpus = corpus::CODE))
        .await
    {
        Ok(x) => x,
        Err(err) => {
            bail!("Failed to embed chunk text: {}", err);
        }
    };

    let mut tokens = code::tokenize_code(body);
    for token in tabby_common::index::binarize_embedding(embedding.iter()) {
        tokens.push(token);
    }

    Ok(tokens)
}

pub fn create_code_builder(embedding: Option<Arc<dyn Embedding>>) -> TantivyDocBuilder<SourceCode> {
    let builder = CodeBuilder::new(embedding);
    TantivyDocBuilder::new(corpus::CODE, builder)
}


Review comment (suggestion):
```suggestion
        .instrument(info_span!("index_compute_embedding", corpus = corpus::CODE))
```

Code after:
                    code::fields::CHUNK_BODY: body,
                    code::fields::CHUNK_START_LINE: start_line,
                });

                let embedding = embedding.clone();
                let rewritten_body = format!("```{}\n{}\n```", source_code.filepath, body);
                yield tokio::spawn(async move {
                    match build_binarize_embedding_tokens(embedding.clone(), &rewritten_body).await {
                        Ok(tokens) => Ok((tokens, attributes)),
                        Err(err) => Err(err),
                    }
                });
            }
        };

        Box::pin(s)
    }
}

async fn build_binarize_embedding_tokens(
    embedding: Arc<dyn Embedding>,
    body: &str,
) -> Result<Vec<String>> {
    let embedding = match embedding
        .embed(body)
        .instrument(info_span!("index_compute_embedding", corpus = corpus::CODE))
        .await
    {
        Ok(x) => x,
        Err(err) => {
            bail!("Failed to embed chunk text: {}", err);
        }
    };

    let mut tokens = code::tokenize_code(body);
    for token in tabby_common::index::binarize_embedding(embedding.iter()) {
        tokens.push(token);
    }

    Ok(tokens)
}

pub fn create_code_builder(embedding: Option<Arc<dyn Embedding>>) -> TantivyDocBuilder<SourceCode> {
    let builder = CodeBuilder::new(embedding);
    TantivyDocBuilder::new(corpus::CODE, builder)
}
Repository: TabbyML/tabby
File: crates/http-api-bindings/src/rate_limit.rs (Rust)

Code before:
    RateLimiter::builder()
        .initial(rps)
        .interval(Duration::from_secs(1))
        .refill(rps)
        .build()
}

pub struct RateLimitedEmbedding {
    embedding: Box<dyn Embedding>,
    rate_limiter: RateLimiter,
}

pub fn new_embedding(embedding: Box<dyn Embedding>, request_per_minute: u64) -> impl Embedding {
    RateLimitedEmbedding {
        embedding,
        rate_limiter: new_rate_limiter(request_per_minute),
    }
}

#[async_trait]
impl Embedding for RateLimitedEmbedding {
    async fn embed(&self, prompt: &str) -> anyhow::Result<Vec<f32>> {
        self.rate_limiter.acquire(1).await;
        self.embedding
            .embed(prompt)
            .instrument(info_span!("rate_limit_embed"))
            .await
    }
}

pub struct RateLimitedCompletion {
    completion: Box<dyn CompletionStream>,
    rate_limiter: RateLimiter,
}

pub fn new_completion(
    completion: Box<dyn CompletionStream>,
    request_per_minute: u64,
) -> impl CompletionStream {
    RateLimitedCompletion {
        completion,
        rate_limiter: new_rate_limiter(request_per_minute),
    }
}

#[async_trait]
impl CompletionStream for RateLimitedCompletion {
    async fn generate(&self, prompt: &str, options: CompletionOptions) -> BoxStream<String> {
        self.rate_limiter.acquire(1).await;
        self.completion.generate(prompt, options).await
    }

Review comment (suggestion):
```suggestion
            .instrument(info_span!("rate_limited_compute_embedding"))
```

Code after:
    RateLimiter::builder()
        .initial(rps)
        .interval(Duration::from_secs(1))
        .refill(rps)
        .build()
}

pub struct RateLimitedEmbedding {
    embedding: Box<dyn Embedding>,
    rate_limiter: RateLimiter,
}

pub fn new_embedding(embedding: Box<dyn Embedding>, request_per_minute: u64) -> impl Embedding {
    RateLimitedEmbedding {
        embedding,
        rate_limiter: new_rate_limiter(request_per_minute),
    }
}

#[async_trait]
impl Embedding for RateLimitedEmbedding {
    async fn embed(&self, prompt: &str) -> anyhow::Result<Vec<f32>> {
        self.rate_limiter.acquire(1).await;
        self.embedding
            .embed(prompt)
            .instrument(info_span!("rate_limited_compute_embedding"))
            .await
    }
}

pub struct RateLimitedCompletion {
    completion: Box<dyn CompletionStream>,
    rate_limiter: RateLimiter,
}

pub fn new_completion(
    completion: Box<dyn CompletionStream>,
    request_per_minute: u64,
) -> impl CompletionStream {
    RateLimitedCompletion {
        completion,
        rate_limiter: new_rate_limiter(request_per_minute),
    }
}

#[async_trait]
impl CompletionStream for RateLimitedCompletion {
    async fn generate(&self, prompt: &str, options: CompletionOptions) -> BoxStream<String> {
        self.rate_limiter.acquire(1).await;
        self.completion.generate(prompt, options).await
    }
Repository: TabbyML/tabby
File: clients/tabby-agent/src/status.ts (TypeScript)

Code before:
      case "connecting":
        statusInfo.tooltip = "Tabby: Connecting to Server...";
        break;
      case "unauthorized":
        statusInfo.tooltip = "Tabby: Authorization Required";
        break;
      case "disconnected":
        statusInfo.tooltip = "Tabby: Connect to Server Failed";
        break;
      case "ready":
        statusInfo.tooltip = "Tabby: Code Completion Enabled";
        break;
      case "readyForAutoTrigger":
        statusInfo.tooltip = "Tabby: Automatic Code Completion Enabled";
        break;
      case "readyForManualTrigger":
        statusInfo.tooltip = "Tabby: Manual Code Completion Enabled";
        break;
      case "fetching":
        statusInfo.tooltip = "Tabby: Generating Completions...";
        break;
      case "completionResponseSlow":
        statusInfo.tooltip = "Tabby: Slow Completion Response Detected";
        break;
      case "rateLimited":
        statusInfo.tooltip = "Tabby: Too many request";
        break;
      default:
        break;
    }
  }
}


Review comment (suggestion):
```suggestion
        statusInfo.tooltip = "Tabby: Too Many Requests";
```

Code after:

  private fillToolTip(statusInfo: StatusInfo) {
    switch (statusInfo.status) {
      case "connecting":
        statusInfo.tooltip = "Tabby: Connecting to Server...";
        break;
      case "unauthorized":
        statusInfo.tooltip = "Tabby: Authorization Required";
        break;
      case "disconnected":
        statusInfo.tooltip = "Tabby: Connect to Server Failed";
        break;
      case "ready":
        statusInfo.tooltip = "Tabby: Code Completion Enabled";
        break;
      case "readyForAutoTrigger":
        statusInfo.tooltip = "Tabby: Automatic Code Completion Enabled";
        break;
      case "readyForManualTrigger":
        statusInfo.tooltip = "Tabby: Manual Code Completion Enabled";
        break;
      case "fetching":
        statusInfo.tooltip = "Tabby: Generating Completions...";
        break;
      case "completionResponseSlow":
        statusInfo.tooltip = "Tabby: Slow Completion Response Detected";
        break;
      case "rateLimitExceeded":
        statusInfo.tooltip = "Tabby: Too Many Requests";
        break;
      default:
        break;
    }
  }
}
Repository: TabbyML/tabby
File: clients/vscode/src/commands/commandPalette.ts (TypeScript)

Code before:
                  .showWarningMessage(
                    "Completion requests appear to take too much time.",
                    {
                      modal: true,
                      detail: currentStatusInfo.helpMessage,
                    },
                    "Online Help...",
                    "Don't Show Again",
                  )
                  .then((selection) => {
                    switch (selection) {
                      case "Online Help...":
                        commands.executeCommand("tabby.openOnlineHelp");
                        break;
                      case "Don't Show Again":
                        commands.executeCommand("tabby.status.addIgnoredIssues", "completionResponseSlow");
                        break;
                    }
                  });
              },
            };
          }
          case "rateLimited": {
            return {
              label: `${STATUS_PREFIX}Too Many Request`,
              description: "Request limit exceeded, please try again later.",
              command: "tabby.outputPanel.focus",
            };
          }
          default: {
            return {
              label: `${STATUS_PREFIX}Unknown Status`,
              description: "Please check the logs for more information.",
              command: "tabby.outputPanel.focus",
            };
          }
        }
      }
    }
  }
}


Review comment (suggestion):
```suggestion
              label: `${STATUS_PREFIX}Too Many Requests`,
              description: "Request limit exceeded",
```

Code after:
                  .showWarningMessage(
                    "Completion requests appear to take too much time.",
                    {
                      modal: true,
                      detail: currentStatusInfo.helpMessage,
                    },
                    "Online Help...",
                    "Don't Show Again",
                  )
                  .then((selection) => {
                    switch (selection) {
                      case "Online Help...":
                        commands.executeCommand("tabby.openOnlineHelp");
                        break;
                      case "Don't Show Again":
                        commands.executeCommand("tabby.status.addIgnoredIssues", "completionResponseSlow");
                        break;
                    }
                  });
              },
            };
          }
          case "rateLimitExceeded": {
            return {
              label: `${STATUS_PREFIX}Too Many Requests`,
              description: "Request limit exceeded",
              command: "tabby.outputPanel.focus",
            };
          }
          default: {
            return {
              label: `${STATUS_PREFIX}Unknown Status`,
              description: "Please check the logs for more information.",
              command: "tabby.outputPanel.focus",
            };
          }
        }
      }
    }
  }
}
Repository: TabbyML/tabby
File: ee/tabby-db/src/user_events.rs (Rust)

Code before:
    ) -> Result<Vec<UserEventDAO>> {
        let users = users
            .iter()
            .map(|u| u.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let no_selected_users = users.is_empty();
        let condition = Some(format!(
            "created_at >= '{start}' AND created_at < '{end}' AND ({no_selected_users} OR user_id IN ({users}))"
        ));
        let events = query_paged_as!(
            UserEventDAO,
            "user_events",
            ["id", "user_id", "kind", "created_at" as "created_at!: DateTime<Utc>", "payload"],
            limit,
            skip_id,
            backwards,
            condition
        )
        .fetch_all(&self.pool)
        .await?;

        Ok(events)
    }

    pub async fn delete_user_events_before(&self, before: DateTime<Utc>) -> Result<usize> {
        let before = before.as_sqlite_datetime();
        let num_deleted = query!("delete FROM user_events WHERE created_at < ?", before,)
            .execute(&self.pool)
            .await?
            .rows_affected();

        Ok(num_deleted as usize)
    }
}


Review comment (suggestion):
```suggestion
    pub async fn delete_user_events_before_three_months(&self) -> Result<usize> {
```

Code after:
    ) -> Result<Vec<UserEventDAO>> {
        let users = users
            .iter()
            .map(|u| u.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let no_selected_users = users.is_empty();
        let condition = Some(format!(
            "created_at >= '{start}' AND created_at < '{end}' AND ({no_selected_users} OR user_id IN ({users}))"
        ));
        let events = query_paged_as!(
            UserEventDAO,
            "user_events",
            ["id", "user_id", "kind", "created_at" as "created_at!: DateTime<Utc>", "payload"],
            limit,
            skip_id,
            backwards,
            condition
        )
        .fetch_all(&self.pool)
        .await?;

        Ok(events)
    }

    pub async fn delete_user_events_before_three_months(
        &self,
        now: DateTime<Utc>,
    ) -> Result<usize> {
        if let Some(three_months_ago) = now.checked_sub_months(Months::new(3)) {
            let three_months_ago = three_months_ago.as_sqlite_datetime();
            let num_deleted = query!(
                "delete FROM user_events WHERE created_at < ?",
                three_months_ago,
            )
            .execute(&self.pool)
            .await?
            .rows_affected();

            Ok(num_deleted as usize)
        } else {
            Ok(0)
        }
    }
}