Skip to content

Commit

Permalink
WIP: add pr answer mock
Browse files Browse the repository at this point in the history
  • Loading branch information
zwpaper committed Nov 15, 2024
1 parent b12162c commit 96ba919
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 12 deletions.
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 struct DocSearchHit {
pub enum DocSearchDocument {
Web(DocSearchWebDocument),
Issue(DocSearchIssueDocument),
Pull(DocSearchPullRequest),
}

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

#[derive(Clone)]
pub struct DocSearchPullRequest {
pub title: String,
pub link: String,
pub body: String,
pub diff: String,
pub state: String,
}

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

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::DIFF,
);
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(),
diff: diff.into(),
state: state.into(),
})
}
}

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 DIFF: &str = "diff";
pub const STATE: &str = "state";
}
}
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 @@ impl StructuredDoc {
match &self.fields {
StructuredDocFields::Web(web) => &web.link,
StructuredDocFields::Issue(issue) => &issue.link,
StructuredDocFields::Pull(pull) => &pull.link,
}
}

pub fn kind(&self) -> &'static str {
match &self.fields {
StructuredDocFields::Web(_) => "web",
StructuredDocFields::Issue(_) => "issue",
StructuredDocFields::Pull(_) => "pull",
}
}
}
Expand Down Expand Up @@ -55,6 +58,7 @@ pub trait BuildStructuredDoc {
pub enum StructuredDocFields {
Web(web::WebDocument),
Issue(issue::IssueDocument),
Pull(pull::PullRequest),
}

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

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,
}
}

Expand All @@ -80,6 +86,7 @@ impl BuildStructuredDoc for StructuredDoc {
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,
}
}
}
Expand Down
52 changes: 52 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,52 @@
use std::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 diff: String,
pub state: String,
}

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

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::DIFF: self.diff,
fields::pull::STATE: self.state,
})
}

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.diff);
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)
}
}
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 struct ThreadMessageDAO {
pub enum ThreadMessageAttachmentDoc {
Web(ThreadMessageAttachmentWebDoc),
Issue(ThreadMessageAttachmentIssueDoc),
Pull(ThreadMessageAttachmentPullRequest),
}

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

#[derive(Serialize, Deserialize)]
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!
diff: 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 lazy_static::lazy_static;
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 @@ impl From<ThreadMessageAttachmentDoc> for thread::MessageAttachmentDoc {
closed: val.closed,
})
}
ThreadMessageAttachmentDoc::Pull(val) => {
thread::MessageAttachmentDoc::Pull(thread::MessageAttachmentPullRequest {
title: val.title,
link: val.link,
body: val.body,
diff: val.diff,
state: val.state,
})
}
}
}
}
Expand All @@ -268,6 +278,15 @@ impl From<&thread::MessageAttachmentDoc> for ThreadMessageAttachmentDoc {
closed: val.closed,
})
}
thread::MessageAttachmentDoc::Pull(val) => {
ThreadMessageAttachmentDoc::Pull(ThreadMessageAttachmentPullRequest {
title: val.title.clone(),
link: val.link.clone(),
body: val.body.clone(),
diff: val.diff.clone(),
state: val.state.clone(),
})
}
}
}
}
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 @@ impl From<CodeSearchHit> for MessageCodeSearchHit {
pub enum MessageAttachmentDoc {
Web(MessageAttachmentWebDoc),
Issue(MessageAttachmentIssueDoc),
Pull(MessageAttachmentPullRequest),
}

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

#[derive(GraphQLObject, Clone)]
pub struct MessageAttachmentPullRequest {
pub title: String,
pub link: String,
pub body: String,
pub diff: String,
pub state: String,
}

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

0 comments on commit 96ba919

Please sign in to comment.