Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fail to send logging (#1255)
Eric Hayese8c5da28d810
diff --git a/js/app/packages/service-clients/service-cognition/websocket.ts b/js/app/packages/service-clients/service-cognition/websocket.tsindex a8fab99dd9..886fc7cc0c 100644--- a/js/app/packages/service-clients/service-cognition/websocket.ts+++ b/js/app/packages/service-clients/service-cognition/websocket.ts@@ -17,6 +17,7 @@ import { createRoot, createSignal } from 'solid-js';import type { StreamError } from './generated/schemas';import type { FromWebSocketMessage } from './generated/schemas/fromWebSocketMessage';import type { ToWebSocketMessage } from './generated/schemas/toWebSocketMessage';+import { toast } from '@core/component/Toast/Toast';export type CognitionWebsocket = Websocket<ToWebSocketMessage,@@ -161,7 +162,9 @@ export function createMessageStream(send: Send): MessageStream {cleanup();};- ws.send(send);+ if (!ws.send(send)) {+ toast.failure('Disconnected');+ }return {close: setClosed,@@ -175,5 +178,7 @@ export function createMessageStream(send: Send): MessageStream {/** Sends a message to the dcs websocket */export function sendCognitionWebsocketMessage(message: ToWebSocketMessage) {- ws.send(message);+ if (!ws.send(message)) {+ toast.failure('Disconnected');+ }}diff --git a/js/app/packages/websocket/core/websocket.ts b/js/app/packages/websocket/core/websocket.tsindex 1f45695885..dec379b9ce 100644--- a/js/app/packages/websocket/core/websocket.ts+++ b/js/app/packages/websocket/core/websocket.ts@@ -257,8 +257,8 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send* @param data to send.*/- public send(rawData: Send): void {- if (this.closedByUser) return; // no-op if closed by user+ public send(rawData: Send): boolean {+ if (this.closedByUser) return false; // no-op if closed by userlet data = serializeIfNeeded(rawData, this._options.serializer);@@ -267,9 +267,11 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {this._underlyingWebsocket.readyState === this._underlyingWebsocket.OPEN) {this._underlyingWebsocket.send(data); // websocket is connected, send data+ return true;} else if (this.buffer !== undefined) {this.buffer.add(rawData); // websocket is not connected, add data to buffer}+ return false;}/**diff --git a/rust/cloud-storage/document_cognition_service/src/api/ws/chat_message/mod.rs b/rust/cloud-storage/document_cognition_service/src/api/ws/chat_message/mod.rsindex 284a4d839d..bfd1fec862 100644--- a/rust/cloud-storage/document_cognition_service/src/api/ws/chat_message/mod.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/ws/chat_message/mod.rs@@ -160,7 +160,7 @@ pub async fn stream_chat_response(message_id: message_id.to_string(),content: message_part,};- ws_send(sender, response);+ ws_send(sender, response)?;}StreamPart::ToolCall(call) => {let message_part = AssistantMessagePart::ToolCall {@@ -175,7 +175,7 @@ pub async fn stream_chat_response(message_id: message_id.to_string(),content: message_part,};- ws_send(sender, response);+ ws_send(sender, response)?;}StreamPart::Usage(usage) => {tracing::debug!(record=?usage, "usage");@@ -191,7 +191,7 @@ pub async fn stream_chat_response(chat_id: chat_id.to_string(),content: message_part,},- )+ )?;}StreamPart::ToolResponse(ai::tool::types::ToolResponse::Err {id,@@ -212,7 +212,7 @@ pub async fn stream_chat_response(chat_id: chat_id.to_string(),content: message_part,},- )+ )?;}}}@@ -362,7 +362,10 @@ pub async fn handle_send_chat_message(FromWebSocketMessage::StreamEnd {stream_id: incoming_message.stream_id.clone(),},- );+ )+ .map_err(|_| StreamError::InternalError {+ stream_id: incoming_message.stream_id.clone(),+ })?;store_conversation_messages(ctx.clone(),@@ -380,20 +383,20 @@ pub async fn handle_send_chat_message(})?;// The chat is empty and we want to auto generate a name for the chat- if is_first_message {- let _ = maybe_rename_chat(&incoming_message.chat_id, &ctx, user_id.0.as_ref())+ if is_first_message+ && let Ok(new_name) = maybe_rename_chat(&incoming_message.chat_id, &ctx, user_id.0.as_ref()).await.inspect_err(|err| tracing::error!(error=?err, "failed to rename chat"))- .map(|new_name| {- ws_send(- sender,- FromWebSocketMessage::ChatRenamed {- chat_id: incoming_message.chat_id.clone(),- stream_id: incoming_message.stream_id.clone(),- name: new_name,- },- )- });+ {+ ws_send(+ sender,+ FromWebSocketMessage::ChatRenamed {+ chat_id: incoming_message.chat_id.clone(),+ stream_id: incoming_message.stream_id.clone(),+ name: new_name,+ },+ )+ .ok();}Ok(())}diff --git a/rust/cloud-storage/document_cognition_service/src/api/ws/completion.rs b/rust/cloud-storage/document_cognition_service/src/api/ws/completion.rsindex f5cf188abe..233d5737c9 100644--- a/rust/cloud-storage/document_cognition_service/src/api/ws/completion.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/ws/completion.rs@@ -183,7 +183,7 @@ async fn stream_completion(done: false,};- ws_send(sender, response);+ ws_send(sender, response)?;}}@@ -194,7 +194,7 @@ async fn stream_completion(content: response_message,done: true,},- );+ )?;Ok(())}diff --git a/rust/cloud-storage/document_cognition_service/src/api/ws/connection.rs b/rust/cloud-storage/document_cognition_service/src/api/ws/connection.rsindex 5c51276136..7c0aefc885 100644--- a/rust/cloud-storage/document_cognition_service/src/api/ws/connection.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/ws/connection.rs@@ -1,4 +1,5 @@use super::chat_permissions;+use ai::types::AiError;use anyhow::Result;use axum::extract::State;use axum::{@@ -74,7 +75,7 @@ async fn process_incoming_message(Message::Text(text) => {// Handle heartbeat ping messagesif text == "ping" {- ws_send(message_sender, FromWebSocketMessage::Pong);+ ws_send(message_sender, FromWebSocketMessage::Pong)?;return Ok(None);}@@ -192,7 +193,7 @@ async fn handle_websocket_connection(if let Err(err) =ensure_user_attachment_access(&ctx, user_context.clone(), attachments).await{- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err));+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err)).ok();continue;}@@ -202,27 +203,24 @@ async fn handle_websocket_connection(match e {UserQuotaError::InvalidMacroUserId| UserQuotaError::UnableToGetUserPermissions- | UserQuotaError::UnableToGetUserQuota => {- ws_send(- &message_sender_clone,- FromWebSocketMessage::Error(WebSocketError::StreamError(- StreamError::InternalError {- stream_id: payload.stream_id,- },- )),- );- }- UserQuotaError::ExceededMaxChatMessages => {- ws_send(- &message_sender_clone,- FromWebSocketMessage::Error(WebSocketError::StreamError(- StreamError::PaymentRequired {- stream_id: payload.stream_id,- },- )),- );- }+ | UserQuotaError::UnableToGetUserQuota => ws_send(+ &message_sender_clone,+ FromWebSocketMessage::Error(WebSocketError::StreamError(+ StreamError::InternalError {+ stream_id: payload.stream_id,+ },+ )),+ ),+ UserQuotaError::ExceededMaxChatMessages => ws_send(+ &message_sender_clone,+ FromWebSocketMessage::Error(WebSocketError::StreamError(+ StreamError::PaymentRequired {+ stream_id: payload.stream_id,+ },+ )),+ ),}+ .ok();continue;}@@ -237,7 +235,8 @@ async fn handle_websocket_connection(stream_id: payload.stream_id.clone(),},)),- );+ )+ .ok();continue;}};@@ -254,7 +253,7 @@ async fn handle_websocket_connection(.await{Err(e) => {- ws_send(&message_sender_clone, FromWebSocketMessage::Error(e));+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(e)).ok();continue;}Ok(access) => match access {@@ -266,7 +265,8 @@ async fn handle_websocket_connection(stream_id: payload.stream_id,},)),- );+ )+ .ok();continue;}_ => (),@@ -286,7 +286,7 @@ async fn handle_websocket_connection(&jwt_token)) => {if let Err(err) = result {- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err.into()))+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err.into())).ok();}}}@@ -304,7 +304,8 @@ async fn handle_websocket_connection(stream_id: payload.stream_id.clone(),},)),- );+ )+ .ok();continue;}};@@ -321,7 +322,7 @@ async fn handle_websocket_connection(.await{Err(e) => {- ws_send(&message_sender_clone, FromWebSocketMessage::Error(e));+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(e)).ok();continue;}Ok(access) => match access {@@ -333,14 +334,15 @@ async fn handle_websocket_connection(stream_id: payload.stream_id,},)),- );+ )+ .ok();continue;}_ => (),},};tokio::spawn(async move {- let result = handle_edit_last_user_message(+ if let Err(err) = handle_edit_last_user_message(ctx,&message_sender_clone,payload,@@ -348,12 +350,13 @@ async fn handle_websocket_connection(&connection_id,&jwt_token,)- .await;- if let Err(err) = result {+ .await+ {ws_send(&message_sender_clone,FromWebSocketMessage::Error(err.into()),)+ .ok();}});}@@ -369,7 +372,7 @@ async fn handle_websocket_connection(details: Some(err.to_string()),}){- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err));+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err)).ok();}}ToWebSocketMessage::ExtractionStatus(payload) => {@@ -383,7 +386,7 @@ async fn handle_websocket_connection(.await.map_err(|_err| WebSocketError::ExtractionStatusFailed { attachment_id }){- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err))+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err)).ok();}}ToWebSocketMessage::SendCompletion(payload) => {@@ -396,20 +399,20 @@ async fn handle_websocket_connection().await{- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err))+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err)).ok();}}ToWebSocketMessage::GetSimpleCompletionStream(payload) => {let user_id = &user_context_clone.user_id;- let response = handle_simple_completion(+ if let Err(err) = handle_simple_completion(ctx_clone.clone(),&message_sender_clone,&payload,user_id,)- .await;- if let Err(err) = response {- ws_send(&message_sender_clone, FromWebSocketMessage::Error(err));+ .await+ {+ ws_send(&message_sender_clone, FromWebSocketMessage::Error(err)).ok();}}};@@ -435,12 +438,26 @@ async fn handle_websocket_connection(CONNECTION_MAP.remove(&connection_id);}-pub fn ws_send(sender: &UnboundedSender<FromWebSocketMessage>, message: FromWebSocketMessage) {- if let Err(e) = sender.send(message) {- tracing::error!(error=%e, "failed to send message to channel");+/// Error returned when failing to send a websocket message+#[derive(Debug, thiserror::Error)]+#[error("failed to send websocket message: channel closed")]+pub struct WsSendError;++impl From<WsSendError> for AiError {+ fn from(err: WsSendError) -> Self {+ AiError::Generic(err.into())}}+/// Sends a message over the websocket channel+#[tracing::instrument(skip(sender), err)]+pub fn ws_send(+ sender: &UnboundedSender<FromWebSocketMessage>,+ message: FromWebSocketMessage,+) -> Result<(), WsSendError> {+ sender.send(message).map_err(|_| WsSendError)+}+/// Errors that can occur when checking the user quota#[derive(Debug, thiserror::Error)]enum UserQuotaError {diff --git a/rust/cloud-storage/document_cognition_service/src/api/ws/edit_message.rs b/rust/cloud-storage/document_cognition_service/src/api/ws/edit_message.rsindex 517a4d2ab9..7f0c5d8fc1 100644--- a/rust/cloud-storage/document_cognition_service/src/api/ws/edit_message.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/ws/edit_message.rs@@ -62,7 +62,11 @@ pub async fn handle_edit_last_user_message(message_id: message_id.clone(),chat_id: incoming_message.chat_id.clone(),},- );+ )+ .map_err(|_| StreamWebSocketError::FailedToEditMessage {+ reason: "failed to send message".to_string(),+ stream_id: incoming_message.stream_id.clone(),+ })?;// delete last messages and message after if it existslet mut last = chat.messages.iter().rev().take(2).collect::<Vec<_>>();diff --git a/rust/cloud-storage/document_cognition_service/src/api/ws/extraction_status.rs b/rust/cloud-storage/document_cognition_service/src/api/ws/extraction_status.rsindex f1c541f701..6eb653172e 100644--- a/rust/cloud-storage/document_cognition_service/src/api/ws/extraction_status.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/ws/extraction_status.rs@@ -84,7 +84,13 @@ pub async fn extraction_status_handler(attachment_id: payload.attachment_id.clone(),status: extraction_status.into(),},- );+ )+ .map_err(|err| {+ tracing::error!(error=?err, "failed to send extraction status ack");+ WebSocketError::ExtractionStatusFailed {+ attachment_id: payload.attachment_id.clone(),+ }+ })?;if extraction_status != ExtractionStatusEnum::Incomplete {return Ok(());@@ -215,7 +221,8 @@ pub fn spawn_poller() {attachment_id: attachment_id.clone(),status: extraction_status.into(),},- );+ )+ .ok();remove_polling_connection(&attachment_id, connection_id);} else {if *start_time + MAX_POLL_TIME > Instant::now() {@@ -229,7 +236,8 @@ pub fn spawn_poller() {FromWebSocketMessage::Error(WebSocketError::ExtractionStatusFailed {attachment_id: attachment_id.to_string(),}),- );+ )+ .ok();remove_polling_connection(&attachment_id, connection_id)}}