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(document): support crawl preset document #2907

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions ee/tabby-db/src/web_documents.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use sqlx::{prelude::FromRow, query};
use tabby_db_macros::query_paged_as;
Expand All @@ -22,9 +22,9 @@ impl DbConn {
limit: Option<usize>,
skip_id: Option<i32>,
backwards: bool,
is_preset: Option<bool>,
is_preset: bool,
) -> Result<Vec<WebDocumentDAO>> {
let condition = is_preset.map(|is_preset| format!("is_preset={}", is_preset));
let condition = Some(format!("is_preset={}", is_preset));

let urls = query_paged_as!(
WebDocumentDAO,
Expand Down Expand Up @@ -58,6 +58,16 @@ impl DbConn {
Ok(res.last_insert_rowid())
}

pub async fn deactivate_preset_web_document(&self, name: String) -> Result<()> {
let res = query!("DELETE FROM web_documents WHERE name = ?;", name)
.execute(&self.pool)
.await?;
if res.rows_affected() != 1 {
return Err(anyhow!("No preset web document to deactivate"));
}
Ok(())
}

pub async fn delete_web_document(&self, id: i64) -> Result<()> {
query!("DELETE FROM web_documents WHERE id = ?;", id)
.execute(&self.pool)
Expand Down
9 changes: 5 additions & 4 deletions ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type CustomWebDocument {
name: String!
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
jobInfo: JobInfo!
}

Expand Down Expand Up @@ -505,7 +506,7 @@ type Mutation {
deleteWebCrawlerUrl(id: ID!): Boolean!
createCustomDocument(input: CreateCustomDocumentInput!): ID!
deleteCustomDocument(id: ID!): Boolean!
setPresetDocumentActive(input: SetPresetDocumentActiveInput!): ID!
setPresetDocumentActive(input: SetPresetDocumentActiveInput!): Boolean!
"Delete pair of user message and bot response in a thread."
deleteThreadMessagePair(threadId: ID!, userMessageId: ID!, assistantMessageId: ID!): Boolean!
}
Expand Down Expand Up @@ -539,11 +540,11 @@ type PresetDocumentEdge {
}

type PresetWebDocument {
name: String!
id: ID!
active: Boolean!
name: String!
"`updated_at` is only filled when the preset is active."
updatedAt: DateTime
"`job_info` is only filled when the preset is active."
jobInfo: JobInfo
}

Expand Down Expand Up @@ -619,7 +620,7 @@ type Query {
"""
threadMessages(threadId: ID!, after: String, before: String, first: Int, last: Int): MessageConnection!
customWebDocuments(after: String, before: String, first: Int, last: Int): CustomDocumentConnection!
presetWebDocuments(after: String, before: String, first: Int, last: Int, active: Boolean!): PresetDocumentConnection!
presetWebDocuments(after: String, before: String, first: Int, last: Int, isActive: Boolean!): PresetDocumentConnection!
}

type RefreshTokenResponse {
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-schema/src/schema/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ lazy_static! {
pub static ref USERNAME_REGEX: Regex =
Regex::new(r"^[^0-9±!@£$%^&*_+§¡€#¢¶•ªº«\\/<>?:;|=.,]{2,20}$").unwrap();
pub static ref WEB_DOCUMENT_NAME_REGEX: Regex =
Regex::new(r"^[A-Za-z][A-Za-z0-9\ ]*$").unwrap();
Regex::new(r"^[A-Za-z][A-Za-z0-9]*(\ [A-Za-z0-9]+)*$").unwrap();
}

#[cfg(test)]
Expand Down Expand Up @@ -53,6 +53,7 @@ mod tests {
(" abc 123", false),
("abc123*", false),
("abc123_", false),
("abc 123", false), // two space
];

for (name, expected) in test_cases {
Expand Down
11 changes: 5 additions & 6 deletions ee/tabby-schema/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ impl Query {
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
active: bool,
is_active: bool,
) -> Result<Connection<PresetWebDocument>> {
query_async(
after,
Expand All @@ -606,7 +606,7 @@ impl Query {
|after, before, first, last| async move {
ctx.locator
.web_documents()
.list_preset_web_documents(after, before, first, last, active)
.list_preset_web_documents(after, before, first, last, is_active)
.await
},
)
Expand Down Expand Up @@ -986,14 +986,13 @@ impl Mutation {
async fn set_preset_document_active(
ctx: &Context,
input: SetPresetDocumentActiveInput,
) -> Result<ID> {
) -> Result<bool> {
input.validate()?;
let id = ctx
.locator
ctx.locator
.web_documents()
.set_preset_web_documents_active(input.name, input.active)
.await?;
Ok(id)
Ok(true)
}

/// Delete pair of user message and bot response in a thread.
Expand Down
12 changes: 7 additions & 5 deletions ee/tabby-schema/src/schema/web_documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ pub struct CustomWebDocument {
pub name: String,
pub id: ID,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub job_info: JobInfo,
}

#[derive(GraphQLObject)]
#[graphql(context = Context)]
pub struct PresetWebDocument {
pub name: String,
xxs-wallace marked this conversation as resolved.
Show resolved Hide resolved
pub id: ID,
pub active: bool,

pub name: String,
/// `updated_at` is only filled when the preset is active.
pub updated_at: Option<DateTime<Utc>>,
/// `job_info` is only filled when the preset is active.
pub job_info: Option<JobInfo>,
}

Expand Down Expand Up @@ -79,7 +81,7 @@ impl NodeType for PresetWebDocument {
type Cursor = String;

fn cursor(&self) -> Self::Cursor {
self.name.clone()
self.id.to_string()
}

fn connection_type_name() -> &'static str {
Expand Down Expand Up @@ -109,7 +111,7 @@ pub trait WebDocumentService: Send + Sync {
before: Option<String>,
first: Option<usize>,
last: Option<usize>,
active: bool,
is_active: bool,
) -> Result<Vec<PresetWebDocument>>;
async fn set_preset_web_documents_active(&self, name: String, active: bool) -> Result<ID>;
async fn set_preset_web_documents_active(&self, name: String, active: bool) -> Result<()>;
}
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod event_logger;
pub mod integration;
pub mod job;
mod license;
mod preset_web_documents_data;
pub mod repository;
mod setting;
mod thread;
Expand Down
Loading