Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should trim bullet even when spaces in bullet chars #3937

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions ee/tabby-webserver/src/service/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tabby_schema::{
use super::graphql_pagination_to_filter;
use crate::service::utils::{
convert_messages_to_chat_completion_request, convert_user_message_to_chat_completion_request,
prompt::{request_llm_stream, request_llm_with_message, transform_line_items},
prompt::{request_llm_stream, request_llm_with_message, transform_line_items, trim_title},
};

struct PageServiceImpl {
Expand Down Expand Up @@ -367,7 +367,7 @@ impl PageServiceImpl {
.await?;

let title = request_llm_with_message(self.chat.clone(), messages).await?;
let title = trim_title(title.as_ref());
let title = trim_title(title.as_str());

self.db
.update_page_title(page_id.as_rowid()?, title)
Expand All @@ -376,12 +376,6 @@ impl PageServiceImpl {
}
}

fn trim_title(title: &str) -> &str {
// take first line.
let title = title.lines().next().unwrap_or(title);
title.trim_matches(&['"', '#', ' ', '-', '*'][..]).trim()
}

async fn build_chat_messages(
context: Arc<dyn ContextService>,
policy: &AccessPolicy,
Expand Down Expand Up @@ -447,7 +441,7 @@ pub async fn generate_page_sections(
let titles = request_llm_with_message(chat.clone(), messages).await?;
Ok(transform_line_items(&titles)
.into_iter()
.map(|x| trim_title(&x).to_owned())
.map(|x| trim_title(x.as_str()).to_owned())
.collect())
}

Expand Down
51 changes: 41 additions & 10 deletions ee/tabby-webserver/src/service/utils/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,32 @@ pub fn transform_line_items(content: &str) -> Vec<String> {
.lines()
.map(trim_bullet)
.filter(|x| !x.is_empty())
.map(|x| x.to_owned())
.collect()
}

/// Trims leading and trailing bullet-like characters or digits from the provided string and returns the trimmed result.
pub fn trim_bullet(s: &str) -> String {
let is_bullet = |c: char| c == '-' || c == '*' || c == '.' || c.is_numeric();
s.trim()
.trim_start_matches(is_bullet)
.trim_end_matches(is_bullet)
.trim()
.to_owned()
fn trim_bullet(s: &str) -> &str {
let s = s.trim();
if s.starts_with('-') || s.starts_with('+') || s.starts_with('*') {
return s.trim_matches(&['-', '+', '*', ' '][..]);
}

// Check for numbered list
if let Some(pos) = s.find(". ") {
let prefix = &s[..pos];
if prefix.chars().all(|c| c.is_numeric()) {
return trim_bullet(s[pos + 2..].trim_start());
}
}

s
}

pub fn trim_title(title: &str) -> &str {
// take first line.
let title = title.lines().next().unwrap_or(title).trim();
trim_bullet(title.trim_matches(&['"', '#', ' ', '-', '*'][..]))
}

/// Checks if the `check` string is contained within `content` in a case-insensitive manner.
Expand All @@ -130,10 +145,12 @@ mod tests {
assert_eq!(trim_bullet("- Hello"), "Hello");
assert_eq!(trim_bullet("* World"), "World");
assert_eq!(trim_bullet("1. Test"), "Test");
assert_eq!(trim_bullet(".Dot"), "Dot");

assert_eq!(trim_bullet("- Hello -"), "Hello");
assert_eq!(trim_bullet("1. Test 1"), "Test");
assert_eq!(trim_bullet("1. Test 1"), "Test 1");
assert_eq!(trim_bullet("12. Test 1"), "Test 1");
assert_eq!(trim_bullet("1. Test 1."), "Test 1.");
assert_eq!(trim_bullet("1 Test "), "1 Test");

assert_eq!(trim_bullet("--** Mixed"), "Mixed");

Expand All @@ -145,6 +162,20 @@ mod tests {

assert_eq!(trim_bullet("Hello World"), "Hello World");

assert_eq!(trim_bullet("1. *Bold* and -italic-"), "*Bold* and -italic");
assert_eq!(trim_bullet("1. *Bold* and -italic-"), "Bold* and -italic");
assert_eq!(trim_bullet("1. **Bold* and -italic"), "Bold* and -italic");
}

#[test]
fn test_trim_title() {
assert_eq!(trim_title("# - Hello"), "Hello");
assert_eq!(trim_title("# * World"), "World");
assert_eq!(trim_title("# 1. Test"), "Test");

assert_eq!(trim_title("## - Hello -"), "Hello");
assert_eq!(trim_title(" # 1. Test 1"), "Test 1");
assert_eq!(trim_title("12. Test 1"), "Test 1");
assert_eq!(trim_title(r#""1. Test 1.""#), "Test 1.");
assert_eq!(trim_title("## **Test "), "Test");
}
}
Loading