Skip to content

Commit

Permalink
refactor(thread): update start_line to be optional to represent entir… (
Browse files Browse the repository at this point in the history
#3614)

* refactor(thread): update start_line to be optional to represent entire file

* fix unit test

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wsxiaoys and autofix-ci[bot] authored Dec 25, 2024
1 parent 2322c11 commit a5d85d2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 20 deletions.
4 changes: 3 additions & 1 deletion crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub struct CodeSearchDocument {
pub commit: Option<String>,

pub language: String,
pub start_line: usize,

/// When start line is `None`, it represents the entire file.
pub start_line: Option<usize>,
}

#[derive(Error, Debug)]
Expand Down
6 changes: 3 additions & 3 deletions crates/tabby/src/services/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ async fn create_hit(
code::fields::CHUNK_LANGUAGE,
)
.to_owned(),
start_line: get_json_number_field(
start_line: Some(get_json_number_field(
&doc,
schema.field_chunk_attributes,
code::fields::CHUNK_START_LINE,
) as usize,
) as usize),
};
CodeSearchHit { scores, doc }
}
Expand Down Expand Up @@ -325,7 +325,7 @@ mod tests {
git_url: "".to_owned(),
commit: Some("".to_owned()),
language: "".to_owned(),
start_line: 0,
start_line: Some(0),
},
};

Expand Down
4 changes: 3 additions & 1 deletion ee/tabby-db/src/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ pub struct ThreadMessageAttachmentCode {
pub language: String,
pub filepath: String,
pub content: String,
pub start_line: usize,

/// When start line is `None`, it represents the entire file.
pub start_line: Option<usize>,
}

#[derive(Serialize, Deserialize)]
Expand Down
5 changes: 3 additions & 2 deletions ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ input EmailSettingInput {

input MessageAttachmentCodeInput {
filepath: String
startLine: Int
"When start line is `None`, it represents the entire file." startLine: Int
content: String!
}

Expand Down Expand Up @@ -502,7 +502,8 @@ type MessageAttachmentCode {
filepath: String!
language: String!
content: String!
startLine: Int!
"When start line is `None`, it represents the entire file."
startLine: Int
}

type MessageAttachmentCodeScores {
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl From<ThreadMessageAttachmentCode> for thread::MessageAttachmentCode {
filepath: value.filepath,
language: value.language,
content: value.content,
start_line: value.start_line as i32,
start_line: value.start_line.map(|x| x as i32),
}
}
}
Expand All @@ -219,7 +219,7 @@ impl From<&thread::MessageAttachmentCode> for ThreadMessageAttachmentCode {
filepath: val.filepath.clone(),
language: val.language.clone(),
content: val.content.clone(),
start_line: val.start_line as usize,
start_line: val.start_line.map(|x| x as usize),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ee/tabby-schema/src/schema/thread/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ pub struct MessageAttachmentInput {
#[derive(GraphQLInputObject, Clone)]
pub struct MessageAttachmentCodeInput {
pub filepath: Option<String>,

/// When start line is `None`, it represents the entire file.
pub start_line: Option<i32>,

pub content: String,
}

Expand Down
6 changes: 4 additions & 2 deletions ee/tabby-schema/src/schema/thread/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ pub struct MessageAttachmentCode {
pub filepath: String,
pub language: String,
pub content: String,
pub start_line: i32,

/// When start line is `None`, it represents the entire file.
pub start_line: Option<i32>,
}

impl From<CodeSearchDocument> for MessageAttachmentCode {
Expand All @@ -88,7 +90,7 @@ impl From<CodeSearchDocument> for MessageAttachmentCode {
filepath: doc.filepath,
language: doc.language,
content: doc.body,
start_line: doc.start_line as i32,
start_line: doc.start_line.map(|x| x as i32),
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions ee/tabby-webserver/src/service/answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ pub async fn merge_code_snippets(
insert_hit.scores.embedding /= num_files;
insert_hit.scores.rrf /= num_files;
insert_hit.doc.body = file_content;
insert_hit.doc.start_line = 1;

// When we use entire file content, mark start_line as None.
insert_hit.doc.start_line = None;
result.push(insert_hit);
}
} else {
Expand Down Expand Up @@ -772,7 +774,7 @@ mod tests {
filepath: "server.py".to_owned(),
language: "python".to_owned(),
content: "from flask import Flask\n\napp = Flask(__name__)\n\n@app.route('/')\ndef hello():\n return 'Hello, World!'".to_owned(),
start_line: 1,
start_line: Some(1),
}],
client_code: vec![],
};
Expand Down Expand Up @@ -806,7 +808,7 @@ mod tests {
filepath: "server.py".to_owned(),
language: "python".to_owned(),
content: "print('Hello, server!')".to_owned(),
start_line: 1,
start_line: Some(1),
}],
client_code: vec![tabby_schema::thread::MessageAttachmentClientCode {
filepath: Some("client.py".to_owned()),
Expand Down Expand Up @@ -983,7 +985,7 @@ mod tests {
filepath: "server.py".to_owned(),
language: "python".to_owned(),
content: "print('Hello, server!')".to_owned(),
start_line: 1,
start_line: Some(1),
}],
client_code: vec![tabby_schema::thread::MessageAttachmentClientCode {
filepath: Some("client.py".to_owned()),
Expand Down Expand Up @@ -1045,7 +1047,7 @@ mod tests {
filepath: "server.py".to_owned(),
language: "python".to_owned(),
content: "print('Hello, server!')".to_owned(),
start_line: 1,
start_line: Some(1),
}],
client_code: vec![tabby_schema::thread::MessageAttachmentClientCode {
filepath: Some("client.py".to_owned()),
Expand Down Expand Up @@ -1341,7 +1343,7 @@ mod tests {
git_url: "https://github.com/test/repo.git".to_string(),
commit: Some("commit".to_string()),
language: "rust".to_string(),
start_line: 1,
start_line: Some(1),
},
scores: CodeSearchScores {
bm25: 0.5,
Expand All @@ -1358,7 +1360,7 @@ mod tests {
git_url: "https://github.com/test/repo.git".to_string(),
commit: Some("commit".to_string()),
language: "rust".to_string(),
start_line: 3,
start_line: Some(3),
},
scores: CodeSearchScores {
bm25: 0.6,
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-webserver/src/service/answer/testutils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl CodeSearch for FakeCodeSearch {
doc: CodeSearchDocument {
filepath: "src/lib.rs".to_string(),
body: "fn add(a: i32, b: i32) -> i32 {\n a + b\n}".to_string(),
start_line: 1,
start_line: Some(1),
language: "rust".to_string(),
file_id: "1".to_string(),
chunk_id: "chunk1".to_string(),
Expand All @@ -160,7 +160,7 @@ impl CodeSearch for FakeCodeSearch {
doc: CodeSearchDocument {
filepath: "src/main.rs".to_string(),
body: "fn main() {\n println!(\"Hello World\");\n}".to_string(),
start_line: 1,
start_line: Some(1),
language: "rust".to_string(),
file_id: "2".to_string(),
chunk_id: "chunk2".to_string(),
Expand Down

0 comments on commit a5d85d2

Please sign in to comment.