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

feat(context): add support for Pull Request in GitHub Context #3429

Merged
merged 12 commits into from
Nov 19, 2024
51 changes: 51 additions & 0 deletions crates/tabby-common/src/api/structured_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
pub enum DocSearchDocument {
Web(DocSearchWebDocument),
Issue(DocSearchIssueDocument),
Pull(DocSearchPullRequest),
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -65,6 +66,15 @@
pub closed: bool,
}

#[derive(Clone)]
pub struct DocSearchPullRequest {
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
pub title: String,
pub link: String,
pub body: String,
pub patch: String,
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
pub state: String,
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
}

pub trait FromTantivyDocument {
fn from_tantivy_document(doc: &TantivyDocument, chunk: &TantivyDocument) -> Option<Self>
where
Expand All @@ -82,6 +92,9 @@
}
"issue" => DocSearchIssueDocument::from_tantivy_document(doc, chunk)
.map(DocSearchDocument::Issue),
"pull" => {
DocSearchPullRequest::from_tantivy_document(doc, chunk).map(DocSearchDocument::Pull)

Check warning on line 96 in crates/tabby-common/src/api/structured_doc.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-common/src/api/structured_doc.rs#L95-L96

Added lines #L95 - L96 were not covered by tests
}
_ => None,
}
}
Expand Down Expand Up @@ -146,6 +159,44 @@
}
}

impl FromTantivyDocument for DocSearchPullRequest {
fn from_tantivy_document(doc: &TantivyDocument, _: &TantivyDocument) -> Option<Self> {
let schema = IndexSchema::instance();
let title = get_json_text_field(
doc,
schema.field_attributes,
structured_doc::fields::pull::TITLE,
);
let link = get_json_text_field(
doc,
schema.field_attributes,
structured_doc::fields::pull::LINK,
);
let body = get_json_text_field(
doc,
schema.field_attributes,
structured_doc::fields::pull::BODY,
);
let diff = get_json_text_field(
doc,
schema.field_attributes,
structured_doc::fields::pull::PATCH,
);
let state = get_json_text_field(
doc,
schema.field_attributes,
structured_doc::fields::pull::STATE,
);
Some(Self {
title: title.into(),
link: link.into(),
body: body.into(),
patch: diff.into(),
state: state.into(),
})
}

Check warning on line 197 in crates/tabby-common/src/api/structured_doc.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-common/src/api/structured_doc.rs#L163-L197

Added lines #L163 - L197 were not covered by tests
}

fn get_json_field<'a>(
doc: &'a TantivyDocument,
field: schema::Field,
Expand Down
8 changes: 8 additions & 0 deletions crates/tabby-common/src/index/structured_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ pub mod fields {
pub const BODY: &str = "body";
pub const CLOSED: &str = "closed";
}

pub mod pull {
pub const TITLE: &str = "title";
pub const LINK: &str = "link";
pub const BODY: &str = "body";
pub const PATCH: &str = "patch";
pub const STATE: &str = "state";
}
}
2 changes: 1 addition & 1 deletion crates/tabby-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod public {
code::CodeIndexer,
structured_doc::public::{
StructuredDoc, StructuredDocFields, StructuredDocIndexer, StructuredDocIssueFields,
StructuredDocWebFields,
StructuredDocPullRequestFields, StructuredDocPullRequestState, StructuredDocWebFields,
},
};

Expand Down
7 changes: 6 additions & 1 deletion crates/tabby-index/src/structured_doc/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ use tabby_common::index::corpus;
use tabby_inference::Embedding;

pub use super::types::{
issue::IssueDocument as StructuredDocIssueFields, web::WebDocument as StructuredDocWebFields,
issue::IssueDocument as StructuredDocIssueFields,
pull::{
PullRequest as StructuredDocPullRequestFields,
PullRequestState as StructuredDocPullRequestState,
},
web::WebDocument as StructuredDocWebFields,
StructuredDoc, StructuredDocFields,
};
use super::{create_structured_doc_builder, types::BuildStructuredDoc};
Expand Down
7 changes: 7 additions & 0 deletions crates/tabby-index/src/structured_doc/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod issue;
pub mod pull;
pub mod web;

use std::sync::Arc;
Expand All @@ -21,13 +22,15 @@
match &self.fields {
StructuredDocFields::Web(web) => &web.link,
StructuredDocFields::Issue(issue) => &issue.link,
StructuredDocFields::Pull(pull) => &pull.link,

Check warning on line 25 in crates/tabby-index/src/structured_doc/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types.rs#L25

Added line #L25 was not covered by tests
}
}

pub fn kind(&self) -> &'static str {
match &self.fields {
StructuredDocFields::Web(_) => "web",
StructuredDocFields::Issue(_) => "issue",
StructuredDocFields::Pull(_) => "pull",

Check warning on line 33 in crates/tabby-index/src/structured_doc/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types.rs#L33

Added line #L33 was not covered by tests
}
}
}
Expand Down Expand Up @@ -55,6 +58,7 @@
pub enum StructuredDocFields {
Web(web::WebDocument),
Issue(issue::IssueDocument),
Pull(pull::PullRequest),
}

#[async_trait]
Expand All @@ -63,13 +67,15 @@
match &self.fields {
StructuredDocFields::Web(doc) => doc.should_skip(),
StructuredDocFields::Issue(doc) => doc.should_skip(),
StructuredDocFields::Pull(doc) => doc.should_skip(),

Check warning on line 70 in crates/tabby-index/src/structured_doc/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types.rs#L70

Added line #L70 was not covered by tests
}
}

async fn build_attributes(&self) -> serde_json::Value {
match &self.fields {
StructuredDocFields::Web(doc) => doc.build_attributes().await,
StructuredDocFields::Issue(doc) => doc.build_attributes().await,
StructuredDocFields::Pull(doc) => doc.build_attributes().await,

Check warning on line 78 in crates/tabby-index/src/structured_doc/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types.rs#L78

Added line #L78 was not covered by tests
}
}

Expand All @@ -80,6 +86,7 @@
match &self.fields {
StructuredDocFields::Web(doc) => doc.build_chunk_attributes(embedding).await,
StructuredDocFields::Issue(doc) => doc.build_chunk_attributes(embedding).await,
StructuredDocFields::Pull(doc) => doc.build_chunk_attributes(embedding).await,

Check warning on line 89 in crates/tabby-index/src/structured_doc/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types.rs#L89

Added line #L89 was not covered by tests
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions crates/tabby-index/src/structured_doc/types/pull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{fmt, sync::Arc};

use async_stream::stream;
use async_trait::async_trait;
use futures::stream::BoxStream;
use serde_json::json;
use tabby_common::index::structured_doc::fields;
use tabby_inference::Embedding;
use tokio::task::JoinHandle;

use super::{build_tokens, BuildStructuredDoc};

pub struct PullRequest {
pub link: String,
pub title: String,
pub body: String,
pub patch: String,
pub state: String,
}

pub enum PullRequestState {
Open,
Closed,
Merged,
}

// Implement Display trait
impl fmt::Display for PullRequestState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PullRequestState::Open => write!(f, "Open"),
PullRequestState::Closed => write!(f, "Closed"),
PullRequestState::Merged => write!(f, "Merged"),

Check warning on line 33 in crates/tabby-index/src/structured_doc/types/pull.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types/pull.rs#L29-L33

Added lines #L29 - L33 were not covered by tests
}
}

Check warning on line 35 in crates/tabby-index/src/structured_doc/types/pull.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types/pull.rs#L35

Added line #L35 was not covered by tests
}

#[async_trait]
impl BuildStructuredDoc for PullRequest {
fn should_skip(&self) -> bool {
false
}

Check warning on line 42 in crates/tabby-index/src/structured_doc/types/pull.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types/pull.rs#L40-L42

Added lines #L40 - L42 were not covered by tests

async fn build_attributes(&self) -> serde_json::Value {
json!({
fields::pull::LINK: self.link,
fields::pull::TITLE: self.title,
fields::pull::BODY: self.body,
fields::pull::PATCH: self.patch,
fields::pull::STATE: self.state,
})
}

Check warning on line 52 in crates/tabby-index/src/structured_doc/types/pull.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types/pull.rs#L44-L52

Added lines #L44 - L52 were not covered by tests

async fn build_chunk_attributes(
&self,
embedding: Arc<dyn Embedding>,
) -> BoxStream<JoinHandle<(Vec<String>, serde_json::Value)>> {
let text = format!("{}\n\n{}\n\n{}", self.title, self.body, self.patch);
let s = stream! {
yield tokio::spawn(async move {
let tokens = build_tokens(embedding, &text).await;
let chunk_attributes = json!({});
(tokens, chunk_attributes)
})
};

Box::pin(s)
}

Check warning on line 68 in crates/tabby-index/src/structured_doc/types/pull.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-index/src/structured_doc/types/pull.rs#L57-L68

Added lines #L57 - L68 were not covered by tests
}
4 changes: 2 additions & 2 deletions ee/tabby-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub use server_setting::ServerSettingDAO;
use sqlx::{query, query_scalar, sqlite::SqliteQueryResult, Pool, Sqlite, SqlitePool};
pub use threads::{
ThreadDAO, ThreadMessageAttachmentClientCode, ThreadMessageAttachmentCode,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc, ThreadMessageAttachmentWebDoc,
ThreadMessageDAO,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc,
ThreadMessageAttachmentPullRequest, ThreadMessageAttachmentWebDoc, ThreadMessageDAO,
};
use tokio::sync::Mutex;
use user_completions::UserCompletionDailyStatsDAO;
Expand Down
10 changes: 10 additions & 0 deletions ee/tabby-db/src/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
pub enum ThreadMessageAttachmentDoc {
Web(ThreadMessageAttachmentWebDoc),
Issue(ThreadMessageAttachmentIssueDoc),
Pull(ThreadMessageAttachmentPullRequest),
}

#[derive(Serialize, Deserialize)]
Expand All @@ -53,6 +54,15 @@
pub closed: bool,
}

#[derive(Serialize, Deserialize)]

Check warning on line 57 in ee/tabby-db/src/threads.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/threads.rs#L57

Added line #L57 was not covered by tests
pub struct ThreadMessageAttachmentPullRequest {
pub title: String,
pub link: String,
pub body: String,
pub diff: String,
pub state: String,
}

#[derive(Serialize, Deserialize)]
pub struct ThreadMessageAttachmentCode {
pub git_url: String,
Expand Down
10 changes: 9 additions & 1 deletion ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ type MessageAttachmentIssueDoc {
closed: Boolean!
}

type MessageAttachmentPullRequest {
title: String!
link: String!
body: String!
patch: String!
state: String!
}

type MessageAttachmentWebDoc {
title: String!
link: String!
Expand Down Expand Up @@ -900,7 +908,7 @@ type WebContextSource implements ContextSourceId & ContextSource {
sourceName: String!
}

union MessageAttachmentDoc = MessageAttachmentWebDoc | MessageAttachmentIssueDoc
union MessageAttachmentDoc = MessageAttachmentWebDoc | MessageAttachmentIssueDoc | MessageAttachmentPullRequest

"""
Schema of thread run stream.
Expand Down
23 changes: 21 additions & 2 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
use tabby_db::{
EmailSettingDAO, IntegrationDAO, InvitationDAO, JobRunDAO, OAuthCredentialDAO,
ServerSettingDAO, ThreadDAO, ThreadMessageAttachmentClientCode, ThreadMessageAttachmentCode,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc, ThreadMessageAttachmentWebDoc,
ThreadMessageDAO, UserEventDAO,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc,
ThreadMessageAttachmentPullRequest, ThreadMessageAttachmentWebDoc, ThreadMessageDAO,
UserEventDAO,
};

use crate::{
Expand Down Expand Up @@ -246,6 +247,15 @@
closed: val.closed,
})
}
ThreadMessageAttachmentDoc::Pull(val) => {
thread::MessageAttachmentDoc::Pull(thread::MessageAttachmentPullRequest {
title: val.title,
link: val.link,
body: val.body,
patch: val.diff,
state: val.state,
})

Check warning on line 257 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L250-L257

Added lines #L250 - L257 were not covered by tests
}
}
}
}
Expand All @@ -268,6 +278,15 @@
closed: val.closed,
})
}
thread::MessageAttachmentDoc::Pull(val) => {
ThreadMessageAttachmentDoc::Pull(ThreadMessageAttachmentPullRequest {
title: val.title.clone(),
link: val.link.clone(),
body: val.body.clone(),
diff: val.patch.clone(),
state: val.state.clone(),
})

Check warning on line 288 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L281-L288

Added lines #L281 - L288 were not covered by tests
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions ee/tabby-schema/src/schema/thread/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
pub enum MessageAttachmentDoc {
Web(MessageAttachmentWebDoc),
Issue(MessageAttachmentIssueDoc),
Pull(MessageAttachmentPullRequest),
}

#[derive(GraphQLObject, Clone)]
Expand All @@ -142,6 +143,15 @@
pub closed: bool,
}

#[derive(GraphQLObject, Clone)]

Check warning on line 146 in ee/tabby-schema/src/schema/thread/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/thread/types.rs#L146

Added line #L146 was not covered by tests
pub struct MessageAttachmentPullRequest {
pub title: String,
pub link: String,
pub body: String,
pub patch: String,
pub state: String,
}

impl From<DocSearchDocument> for MessageAttachmentDoc {
fn from(doc: DocSearchDocument) -> Self {
match doc {
Expand All @@ -158,6 +168,15 @@
closed: issue.closed,
})
}
DocSearchDocument::Pull(pull) => {
MessageAttachmentDoc::Pull(MessageAttachmentPullRequest {
title: pull.title,
link: pull.link,
body: pull.body,
patch: pull.patch,
state: pull.state,
})
}
}
}
}
Expand Down
Loading
Loading