diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 44e0c7b..f64ccc9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -183,7 +183,8 @@ pub fn run() { networking::commands::load_chat_messages, networking::commands::probe_connection_slot, networking::commands::disconnect_connection_slot, - networking::commands::send_connection_slot_message, + networking::commands::send_connection_slot_message, + networking::commands::cancel_connection_slot_message, ]) .run(tauri::generate_context!()) .expect("failed to run AstrBot Desktop"); diff --git a/src-tauri/src/networking/commands.rs b/src-tauri/src/networking/commands.rs index 361240d..896899a 100644 --- a/src-tauri/src/networking/commands.rs +++ b/src-tauri/src/networking/commands.rs @@ -400,6 +400,13 @@ pub async fn disconnect_connection_slot( } #[tauri::command] +pub async fn cancel_connection_slot_message( + runtime: State<'_, DualSlotRuntime>, + request: DisconnectConnectionSlotRequest, +) -> Result<(), RuntimeCommandError> { + runtime.cancel_slot_message(request.slot).await +} +#[tauri::command] pub async fn send_connection_slot_message( database: State<'_, ApplicationDatabase>, runtime: State<'_, DualSlotRuntime>, diff --git a/src-tauri/src/networking/mod.rs b/src-tauri/src/networking/mod.rs index 95c158f..42b4b6c 100644 --- a/src-tauri/src/networking/mod.rs +++ b/src-tauri/src/networking/mod.rs @@ -8,8 +8,8 @@ pub use astrbot_client::{AstrBotClient, AstrBotClientConfig, AstrBotClientError} pub use commands::{ DisconnectConnectionSlotRequest, DrainConnectionRuntimeEventsRequest, ProbeConnectionSlotRequest, SendConnectionSlotMessageRequest, auto_connect_configured_slots, - connection_runtime_status, disconnect_connection_slot, drain_connection_runtime_events, - probe_connection_slot, send_connection_slot_message, + cancel_connection_slot_message, connection_runtime_status, disconnect_connection_slot, + drain_connection_runtime_events, probe_connection_slot, send_connection_slot_message, }; pub use gateway_client::{BoundGatewayEvent, GatewayClient, GatewayClientConfig, GatewayError}; pub use manager::{DualConnectionManager, SlotKind}; diff --git a/src-tauri/src/networking/runtime.rs b/src-tauri/src/networking/runtime.rs index f03746b..a21bc72 100644 --- a/src-tauri/src/networking/runtime.rs +++ b/src-tauri/src/networking/runtime.rs @@ -1,11 +1,12 @@ use desktop_protocol::{ConnectionState, TransportKind}; use secrecy::SecretString; use serde::Serialize; +use std::time::Duration; use tokio::sync::{Mutex, mpsc}; use super::{ - AstrBotClient, AstrBotClientConfig, BoundGatewayEvent, GatewayClient, GatewayClientConfig, - GatewayError, SlotKind, + AstrBotClient, AstrBotClientConfig, AstrBotClientError, BoundGatewayEvent, GatewayClient, + GatewayClientConfig, GatewayError, SlotKind, }; #[derive(Debug, Clone, Serialize)] @@ -48,6 +49,7 @@ enum ActiveClient { struct SlotRuntime { snapshot: SlotRuntimeSnapshot, client: Option, + cancellation: Option, } impl SlotRuntime { @@ -63,6 +65,7 @@ impl SlotRuntime { last_error: None, }, client: None, + cancellation: None, } } } @@ -186,6 +189,7 @@ impl DualSlotRuntime { last_error: None, }, client: None, + cancellation: None, }; match request.transport { @@ -282,6 +286,17 @@ impl DualSlotRuntime { Ok(runtime.snapshot.clone()) } + pub async fn cancel_slot_message(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> { + let guard = self.inner.lock().await; + let runtime = slot_ref(&guard, slot); + let Some(cancellation) = runtime.cancellation.as_ref() else { + return Err(RuntimeCommandError::unavailable( + "No chat request is active.", + )); + }; + cancellation.cancel(); + Ok(()) + } pub async fn send_slot_message( &self, slot: SlotKind, @@ -294,12 +309,14 @@ impl DualSlotRuntime { } // Take the client out so we can await without holding the mutex across network IO. - let (client, profile_id, transport, server_instance_id, session_id) = { + let (client, profile_id, transport, server_instance_id, session_id, cancellation) = { let mut guard = self.inner.lock().await; let runtime = slot_mut(&mut guard, slot); let client = runtime.client.take().ok_or_else(|| { RuntimeCommandError::unavailable("The selected slot is not connected.") })?; + let cancellation = tokio_util::sync::CancellationToken::new(); + runtime.cancellation = Some(cancellation.clone()); runtime.snapshot.state = ConnectionState::Syncing; ( client, @@ -307,12 +324,12 @@ impl DualSlotRuntime { runtime.snapshot.transport, runtime.snapshot.server_instance_id.clone(), runtime.snapshot.active_session_id.clone(), + cancellation, ) }; let send_result = match client { ActiveClient::Desktop(client) => { - // Desktop mock path remains stream-oriented; chat POST is a later wire-up. let restored = ActiveClient::Desktop(client); Err(( restored, @@ -322,16 +339,37 @@ impl DualSlotRuntime { )) } ActiveClient::AstrBot(client) => { - let cancel = tokio_util::sync::CancellationToken::new(); - match client - .send_chat_stream(&message, &self.event_tx, &cancel) - .await - { - Ok(session) => Ok((ActiveClient::AstrBot(client), session)), - Err(_) => Err(( - ActiveClient::AstrBot(client), - RuntimeCommandError::unavailable("AstrBot chat send failed."), - )), + let mut attempts = 0; + loop { + match client + .send_chat_stream(&message, &self.event_tx, &cancellation) + .await + { + Ok(_session) if cancellation.is_cancelled() => { + break Err(( + ActiveClient::AstrBot(client), + RuntimeCommandError { + code: "cancelled", + message: "The chat request was cancelled.", + }, + )); + } + Ok(session) => break Ok((ActiveClient::AstrBot(client), session)), + Err(error) if should_retry_astrbot(&error) && attempts < 2 => { + attempts += 1; + let mut guard = self.inner.lock().await; + slot_mut(&mut guard, slot).snapshot.state = + ConnectionState::Reconnecting; + drop(guard); + tokio::time::sleep(Duration::from_millis(250 * attempts)).await; + } + Err(_) => { + break Err(( + ActiveClient::AstrBot(client), + RuntimeCommandError::unavailable("AstrBot chat send failed."), + )); + } + } } } }; @@ -369,6 +407,23 @@ impl Default for DualSlotRuntime { } } +fn slot_ref(inner: &DualSlotRuntimeInner, slot: SlotKind) -> &SlotRuntime { + match slot { + SlotKind::Companion => &inner.companion, + SlotKind::Operations => &inner.operations, + } +} + +fn should_retry_astrbot(error: &AstrBotClientError) -> bool { + match error { + AstrBotClientError::Request(_) | AstrBotClientError::ChannelClosed => true, + AstrBotClientError::HttpStatus(status) => { + status.is_server_error() || status.as_u16() == 408 || status.as_u16() == 429 + } + _ => false, + } +} + fn slot_mut(inner: &mut DualSlotRuntimeInner, slot: SlotKind) -> &mut SlotRuntime { match slot { SlotKind::Companion => &mut inner.companion,