Skip to content

Commit

Permalink
:wqerge branch 'main' into stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
Horusiath committed Dec 19, 2024
2 parents c1d94e3 + ea131f0 commit 42287e1
Show file tree
Hide file tree
Showing 32 changed files with 1,703 additions and 577 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ tokio-stream.workspace = true
tokio-util = { version = "0.7.10", features = ["io"] }
futures-util = { workspace = true, features = ["std", "io"] }
once_cell = "1.19.0"
chrono = { version = "0.4.37", features = [
"serde",
"clock",
], default-features = false }
chrono.workspace = true
derive_more = { version = "0.99" }
secrecy.workspace = true
rand = { version = "0.8", features = ["std_rng"] }
Expand Down Expand Up @@ -287,6 +284,10 @@ pin-project = "1.1.5"
arc-swap = { version = "1.7" }
validator = "0.19"
zstd = { version = "0.13.2", features = [] }
chrono = { version = "0.4.39", features = [
"serde",
"clock",
], default-features = false }

# collaboration
yrs = { version = "0.21.3", features = ["sync"] }
Expand Down Expand Up @@ -316,13 +317,13 @@ lto = false
[patch.crates-io]
# It's diffcult to resovle different version with the same crate used in AppFlowy Frontend and the Client-API crate.
# So using patch to workaround this issue.
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab-importer = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "33cca67554c29b1a3821df6faf5423cff2cd5db3" }
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }
collab-importer = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2443178e4249354c094867875f300cc924cbe0e2" }

[features]
history = []
Expand Down
33 changes: 27 additions & 6 deletions libs/app-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod gotrue;

#[cfg(feature = "gotrue_error")]
use crate::gotrue::GoTrueError;
use std::error::Error;
use std::string::FromUtf8Error;

#[cfg(feature = "appflowy_ai_error")]
Expand Down Expand Up @@ -277,13 +278,32 @@ impl From<reqwest::Error> for AppError {
return AppError::RequestTimeout(error.to_string());
}

if error.is_request() {
return if error.status() == Some(StatusCode::PAYLOAD_TOO_LARGE) {
AppError::PayloadTooLarge(error.to_string())
} else {
AppError::InvalidRequest(error.to_string())
};
if let Some(cause) = error.source() {
if cause
.to_string()
.contains("connection closed before message completed")
{
return AppError::ServiceTemporaryUnavailable(error.to_string());
}
}

// Handle request-related errors
if let Some(status_code) = error.status() {
if error.is_request() {
match status_code {
StatusCode::PAYLOAD_TOO_LARGE => {
return AppError::PayloadTooLarge(error.to_string());
},
status_code if status_code.is_server_error() => {
return AppError::ServiceTemporaryUnavailable(error.to_string());
},
_ => {
return AppError::InvalidRequest(error.to_string());
},
}
}
}

AppError::Unhandled(error.to_string())
}
}
Expand Down Expand Up @@ -447,6 +467,7 @@ impl From<AIError> for AppError {
AIError::PayloadTooLarge(err) => AppError::PayloadTooLarge(err),
AIError::InvalidRequest(err) => AppError::InvalidRequest(err),
AIError::SerdeError(err) => AppError::SerdeError(err),
AIError::ServiceUnavailable(err) => AppError::AIServiceUnavailable(err),
}
}
}
30 changes: 24 additions & 6 deletions libs/appflowy-ai-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ where
resp: reqwest::Response,
) -> Result<impl Stream<Item = Result<Bytes, AIError>>, AIError> {
let status_code = resp.status();
if status_code.is_server_error() {
let body = resp.text().await?;
return Err(AIError::ServiceUnavailable(body));
}

if !status_code.is_success() {
let body = resp.text().await?;
return Err(AIError::InvalidRequest(body));
Expand All @@ -385,16 +390,29 @@ where
}
impl From<reqwest::Error> for AIError {
fn from(error: reqwest::Error) -> Self {
if error.is_connect() {
return AIError::ServiceUnavailable(error.to_string());
}

if error.is_timeout() {
return AIError::RequestTimeout(error.to_string());
}

if error.is_request() {
return if error.status() == Some(StatusCode::PAYLOAD_TOO_LARGE) {
AIError::PayloadTooLarge(error.to_string())
} else {
AIError::InvalidRequest(format!("{:?}", error))
};
// Handle request-related errors
if let Some(status_code) = error.status() {
if error.is_request() {
match status_code {
StatusCode::PAYLOAD_TOO_LARGE => {
return AIError::PayloadTooLarge(error.to_string());
},
status_code if status_code.is_server_error() => {
return AIError::ServiceUnavailable(error.to_string());
},
_ => {
return AIError::InvalidRequest(format!("{:?}", error));
},
}
}
}
AIError::Internal(error.into())
}
Expand Down
3 changes: 3 additions & 0 deletions libs/appflowy-ai-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ pub enum AIError {

#[error(transparent)]
SerdeError(#[from] serde_json::Error),

#[error("Service unavailable:{0}")]
ServiceUnavailable(String),
}
35 changes: 34 additions & 1 deletion libs/client-api/src/http_chat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::http::log_request_id;
use crate::Client;

use app_error::AppError;
use client_api_entity::chat_dto::{
ChatMessage, CreateAnswerMessageParams, CreateChatMessageParams, CreateChatParams, MessageCursor,
RepeatedChatMessage, UpdateChatMessageContentParams,
Expand Down Expand Up @@ -154,7 +155,17 @@ impl Client {
.await?
.timeout(Duration::from_secs(30))
.send()
.await?;
.await
.map_err(|err| {
let app_err = AppError::from(err);
if matches!(app_err, AppError::ServiceTemporaryUnavailable(_)) {
AppError::AIServiceUnavailable(
"AI service temporarily unavailable, please try again later".to_string(),
)
} else {
app_err
}
})?;
log_request_id(&resp);
let stream = AppResponse::<serde_json::Value>::json_response_stream(resp).await?;
Ok(QuestionStream::new(stream))
Expand Down Expand Up @@ -262,6 +273,28 @@ impl Client {
.into_data()
}

pub async fn get_question_message_from_answer_id(
&self,
workspace_id: &str,
chat_id: &str,
answer_message_id: i64,
) -> Result<Option<ChatMessage>, AppResponseError> {
let url = format!(
"{}/api/chat/{workspace_id}/{chat_id}/message/find_question",
self.base_url
);

let resp = self
.http_client_with_auth(Method::GET, &url)
.await?
.query(&[("answer_message_id", answer_message_id)])
.send()
.await?;
AppResponse::<Option<ChatMessage>>::from_response(resp)
.await?
.into_data()
}

pub async fn calculate_similarity(
&self,
params: CalculateSimilarityParams,
Expand Down
Loading

0 comments on commit 42287e1

Please sign in to comment.