feat: integrate cloud document search (#5523)

This commit is contained in:
Mathias Mogensen
2024-06-13 01:37:19 +02:00
committed by GitHub
parent 4f4be7eac7
commit bd5f5f8b9e
39 changed files with 539 additions and 110 deletions

View File

@ -3,6 +3,7 @@ pub(crate) use database::*;
pub(crate) use document::*;
pub(crate) use file_storage::*;
pub(crate) use folder::*;
pub(crate) use search::*;
pub(crate) use user::*;
mod chat;
@ -10,5 +11,6 @@ mod database;
mod document;
mod file_storage;
mod folder;
mod search;
mod user;
mod util;

View File

@ -0,0 +1,40 @@
use client_api::entity::search_dto::SearchDocumentResponseItem;
use flowy_error::FlowyError;
use flowy_search_pub::cloud::SearchCloudService;
use lib_infra::async_trait::async_trait;
use crate::af_cloud::AFServer;
pub(crate) struct AFCloudSearchCloudServiceImpl<T> {
pub inner: T,
}
// The limit of what the score should be for results, used to
// filter out irrelevant results.
const SCORE_LIMIT: f64 = 0.8;
const DEFAULT_PREVIEW: u32 = 80;
#[async_trait]
impl<T> SearchCloudService for AFCloudSearchCloudServiceImpl<T>
where
T: AFServer,
{
async fn document_search(
&self,
workspace_id: &str,
query: String,
) -> Result<Vec<SearchDocumentResponseItem>, FlowyError> {
let client = self.inner.try_get_client()?;
let result = client
.search_documents(workspace_id, &query, 10, DEFAULT_PREVIEW)
.await?;
// Filter out irrelevant results
let result = result
.into_iter()
.filter(|r| r.score < SCORE_LIMIT)
.collect();
Ok(result)
}
}

View File

@ -11,6 +11,7 @@ use client_api::ws::{
};
use client_api::{Client, ClientConfiguration};
use flowy_chat_pub::cloud::ChatCloudService;
use flowy_search_pub::cloud::SearchCloudService;
use flowy_storage::ObjectStorageService;
use rand::Rng;
use semver::Version;
@ -38,6 +39,8 @@ use crate::af_cloud::impls::{
use crate::AppFlowyServer;
use super::impls::AFCloudSearchCloudServiceImpl;
pub(crate) type AFCloudClient = Client;
pub struct AppFlowyCloudServer {
@ -255,6 +258,14 @@ impl AppFlowyServer for AppFlowyCloudServer {
};
Some(Arc::new(AFCloudFileStorageServiceImpl::new(client)))
}
fn search_service(&self) -> Option<Arc<dyn SearchCloudService>> {
let server = AFServerImpl {
client: self.get_client(),
};
Some(Arc::new(AFCloudSearchCloudServiceImpl { inner: server }))
}
}
/// Spawns a new asynchronous task to handle WebSocket connections based on token state.

View File

@ -1,3 +1,4 @@
use flowy_search_pub::cloud::SearchCloudService;
use flowy_storage::ObjectStorageService;
use std::sync::Arc;
@ -70,4 +71,8 @@ impl AppFlowyServer for LocalServer {
fn file_storage(&self) -> Option<Arc<dyn ObjectStorageService>> {
None
}
fn search_service(&self) -> Option<Arc<dyn SearchCloudService>> {
None
}
}

View File

@ -1,6 +1,7 @@
use client_api::ws::ConnectState;
use client_api::ws::WSConnectStateReceiver;
use client_api::ws::WebSocketChannel;
use flowy_search_pub::cloud::SearchCloudService;
use flowy_storage::ObjectStorageService;
use std::sync::Arc;
@ -100,6 +101,10 @@ pub trait AppFlowyServer: Send + Sync + 'static {
Arc::new(DefaultChatCloudServiceImpl)
}
/// Bridge for the Cloud AI Search features
///
fn search_service(&self) -> Option<Arc<dyn SearchCloudService>>;
/// Manages collaborative objects within a remote storage system. This includes operations such as
/// checking storage status, retrieving updates and snapshots, and dispatching updates. The service
/// also provides subscription capabilities for real-time updates.

View File

@ -1,3 +1,4 @@
use flowy_search_pub::cloud::SearchCloudService;
use flowy_storage::ObjectStorageService;
use std::collections::HashMap;
use std::sync::{Arc, Weak};
@ -194,4 +195,8 @@ impl AppFlowyServer for SupabaseServer {
.clone()
.map(|s| s as Arc<dyn ObjectStorageService>)
}
fn search_service(&self) -> Option<Arc<dyn SearchCloudService>> {
None
}
}