diff --git a/deploy.env b/deploy.env index 0377709b..7694d1eb 100644 --- a/deploy.env +++ b/deploy.env @@ -113,6 +113,7 @@ CLOUDFLARE_TUNNEL_TOKEN= # AppFlowy AI APPFLOWY_AI_OPENAI_API_KEY= APPFLOWY_AI_SERVER_PORT=5001 +APPFLOWY_AI_SERVER_HOST=ai APPFLOWY_AI_DATABASE_URL=postgresql+psycopg://postgres:password@postgres:5432/postgres # AppFlowy History diff --git a/dev.env b/dev.env index 367d8b72..0520b40c 100644 --- a/dev.env +++ b/dev.env @@ -104,6 +104,7 @@ CLOUDFLARE_TUNNEL_TOKEN= # AppFlowy AI APPFLOWY_AI_OPENAI_API_KEY= APPFLOWY_AI_SERVER_PORT=5001 +APPFLOWY_AI_SERVER_HOST=localhost APPFLOWY_AI_DATABASE_URL=postgresql+psycopg://postgres:password@postgres:5432/postgres # AppFlowy History diff --git a/libs/client-api-test/src/client.rs b/libs/client-api-test/src/client.rs index 47054d3d..e9961e68 100644 --- a/libs/client-api-test/src/client.rs +++ b/libs/client-api-test/src/client.rs @@ -15,6 +15,18 @@ lazy_static! { get_env_var("LOCALHOST_GOTRUE", "http://localhost:9999"); } +// Use following configuration when using local server with nginx +// +// #[cfg(not(target_arch = "wasm32"))] +// lazy_static! { +// pub static ref LOCALHOST_URL: Cow<'static, str> = +// get_env_var("LOCALHOST_URL", "http://localhost"); +// pub static ref LOCALHOST_WS: Cow<'static, str> = +// get_env_var("LOCALHOST_WS", "ws://localhost/ws/v1"); +// pub static ref LOCALHOST_GOTRUE: Cow<'static, str> = +// get_env_var("LOCALHOST_GOTRUE", "http://localhost/gotrue"); +// } + // The env vars are not available in wasm32-unknown-unknown #[cfg(target_arch = "wasm32")] lazy_static! { diff --git a/nginx/nginx.conf b/nginx/nginx.conf index dd338542..136639f4 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -73,6 +73,21 @@ http { return 204; } + location /api/chat { + set $appflowy_cloud appflowy_cloud; + proxy_pass http://$appflowy_cloud:8000; + + proxy_http_version 1.1; + proxy_set_header Connection ""; + chunked_transfer_encoding on; + proxy_buffering off; + proxy_cache off; + + proxy_read_timeout 600s; + proxy_connect_timeout 600s; + proxy_send_timeout 600s; + } + location /api { set $appflowy_cloud appflowy_cloud; proxy_pass http://$appflowy_cloud:8000; diff --git a/src/api/chat.rs b/src/api/chat.rs index 9cccc2d6..067e5a62 100644 --- a/src/api/chat.rs +++ b/src/api/chat.rs @@ -83,6 +83,7 @@ async fn create_chat_message_handler( state.ai_client.clone(), ) .await; + Ok( HttpResponse::Ok() .content_type("application/json") diff --git a/src/biz/chat/ops.rs b/src/biz/chat/ops.rs index c18b7be4..0c30ac0a 100644 --- a/src/biz/chat/ops.rs +++ b/src/biz/chat/ops.rs @@ -16,6 +16,7 @@ use database_entity::dto::{ }; use futures::stream::Stream; use sqlx::PgPool; +use tracing::error; use validator::Validate; @@ -118,6 +119,7 @@ pub async fn create_chat_message( ).await { Ok(question) => question, Err(err) => { + error!("Failed to insert question message: {}", err); yield Err(err); return; } @@ -125,14 +127,15 @@ pub async fn create_chat_message( let question_id = question.message_id; let question_bytes = match serde_json::to_vec(&question) { - Ok(bytes) => bytes, + Ok(s) => Bytes::from(s), Err(err) => { + error!("Failed to serialize question message: {}", err); yield Err(AppError::from(err)); return; } }; - yield Ok::(Bytes::from(question_bytes)); + yield Ok::(question_bytes); // Insert answer message match params.message_type { @@ -141,6 +144,7 @@ pub async fn create_chat_message( let content = match ai_client.send_question(&chat_id, ¶ms.content).await { Ok(response) => response.content, Err(err) => { + error!("Failed to send question to AI: {}", err); yield Err(AppError::from(err)); return; } @@ -149,20 +153,22 @@ pub async fn create_chat_message( let answer = match insert_answer_message(&pg_pool, ChatAuthor::ai(), &chat_id, content.clone(),question_id).await { Ok(answer) => answer, Err(err) => { + error!("Failed to insert answer message: {}", err); yield Err(err); return; } }; let answer_bytes = match serde_json::to_vec(&answer) { - Ok(bytes) => bytes, + Ok(s) => Bytes::from(s), Err(err) => { + error!("Failed to serialize answer message: {}", err); yield Err(AppError::from(err)); return; } }; - yield Ok::(Bytes::from(answer_bytes)); + yield Ok::(answer_bytes); } } };