2024-06-14 09:02:06 +08:00
|
|
|
use bytes::Bytes;
|
2024-06-25 01:59:38 +02:00
|
|
|
pub use client_api::entity::ai_dto::{
|
2024-08-06 07:56:13 +08:00
|
|
|
AppFlowyOfflineAI, CompletionType, CreateTextChatContext, LLMModel, LocalAIConfig, ModelInfo,
|
|
|
|
RelatedQuestion, RepeatedRelatedQuestion, StringOrMessage,
|
2024-06-25 01:59:38 +02:00
|
|
|
};
|
2024-06-03 14:27:28 +08:00
|
|
|
pub use client_api::entity::{
|
2024-08-09 07:40:24 +08:00
|
|
|
ChatAuthorType, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatMetadataContentType,
|
|
|
|
ChatMetadataData, MessageCursor, QAChatMessage, QuestionStreamValue, RepeatedChatMessage,
|
2024-06-03 14:27:28 +08:00
|
|
|
};
|
|
|
|
use client_api::error::AppResponseError;
|
|
|
|
use flowy_error::FlowyError;
|
|
|
|
use futures::stream::BoxStream;
|
|
|
|
use lib_infra::async_trait::async_trait;
|
|
|
|
use lib_infra::future::FutureResult;
|
2024-08-12 15:43:17 +08:00
|
|
|
use serde_json::Value;
|
|
|
|
use std::collections::HashMap;
|
2024-08-08 12:07:00 +08:00
|
|
|
use std::path::Path;
|
2024-06-03 14:27:28 +08:00
|
|
|
|
|
|
|
pub type ChatMessageStream = BoxStream<'static, Result<ChatMessage, AppResponseError>>;
|
2024-08-06 07:56:13 +08:00
|
|
|
pub type StreamAnswer = BoxStream<'static, Result<QuestionStreamValue, FlowyError>>;
|
2024-07-25 19:41:16 +08:00
|
|
|
pub type StreamComplete = BoxStream<'static, Result<Bytes, FlowyError>>;
|
2024-06-03 14:27:28 +08:00
|
|
|
#[async_trait]
|
|
|
|
pub trait ChatCloudService: Send + Sync + 'static {
|
|
|
|
fn create_chat(
|
|
|
|
&self,
|
|
|
|
uid: &i64,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
) -> FutureResult<(), FlowyError>;
|
|
|
|
|
2024-08-09 21:55:20 +08:00
|
|
|
async fn create_question(
|
2024-06-09 14:02:32 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
message: &str,
|
|
|
|
message_type: ChatMessageType,
|
2024-08-09 21:55:20 +08:00
|
|
|
metadata: &[ChatMessageMetadata],
|
|
|
|
) -> Result<ChatMessage, FlowyError>;
|
2024-06-09 14:02:32 +08:00
|
|
|
|
2024-08-11 20:39:25 +08:00
|
|
|
async fn create_answer(
|
2024-06-09 14:02:32 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
message: &str,
|
|
|
|
question_id: i64,
|
2024-08-06 07:56:13 +08:00
|
|
|
metadata: Option<serde_json::Value>,
|
2024-08-11 20:39:25 +08:00
|
|
|
) -> Result<ChatMessage, FlowyError>;
|
2024-06-09 14:02:32 +08:00
|
|
|
|
2024-08-06 07:56:13 +08:00
|
|
|
async fn stream_answer(
|
2024-06-09 14:02:32 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
message_id: i64,
|
|
|
|
) -> Result<StreamAnswer, FlowyError>;
|
|
|
|
|
2024-08-06 07:56:13 +08:00
|
|
|
async fn get_answer(
|
2024-06-30 17:38:39 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
question_message_id: i64,
|
|
|
|
) -> Result<ChatMessage, FlowyError>;
|
|
|
|
|
2024-08-11 20:39:25 +08:00
|
|
|
async fn get_chat_messages(
|
2024-06-03 14:27:28 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
offset: MessageCursor,
|
|
|
|
limit: u64,
|
2024-08-11 20:39:25 +08:00
|
|
|
) -> Result<RepeatedChatMessage, FlowyError>;
|
2024-06-03 14:27:28 +08:00
|
|
|
|
2024-07-25 19:41:16 +08:00
|
|
|
async fn get_related_message(
|
2024-06-03 14:27:28 +08:00
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
chat_id: &str,
|
|
|
|
message_id: i64,
|
2024-07-25 19:41:16 +08:00
|
|
|
) -> Result<RepeatedRelatedQuestion, FlowyError>;
|
2024-06-03 14:27:28 +08:00
|
|
|
|
2024-06-25 01:59:38 +02:00
|
|
|
async fn stream_complete(
|
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
|
|
|
text: &str,
|
|
|
|
complete_type: CompletionType,
|
|
|
|
) -> Result<StreamComplete, FlowyError>;
|
2024-07-15 15:23:23 +08:00
|
|
|
|
|
|
|
async fn index_file(
|
|
|
|
&self,
|
|
|
|
workspace_id: &str,
|
2024-08-08 12:07:00 +08:00
|
|
|
file_path: &Path,
|
2024-07-15 15:23:23 +08:00
|
|
|
chat_id: &str,
|
2024-08-12 15:43:17 +08:00
|
|
|
metadata: Option<HashMap<String, Value>>,
|
2024-07-15 15:23:23 +08:00
|
|
|
) -> Result<(), FlowyError>;
|
|
|
|
|
|
|
|
async fn get_local_ai_config(&self, workspace_id: &str) -> Result<LocalAIConfig, FlowyError>;
|
2024-08-06 07:56:13 +08:00
|
|
|
|
|
|
|
async fn create_chat_context(
|
|
|
|
&self,
|
|
|
|
_workspace_id: &str,
|
|
|
|
_chat_context: CreateTextChatContext,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-06-03 14:27:28 +08:00
|
|
|
}
|