fix(sse): add bounded chat retry and cancellation
这个提交存在于:
父节点
2e84ea52e9
当前提交
5dfac5960f
共有 4 个文件被更改,包括 80 次插入 和 17 次删除
|
|
@ -183,7 +183,8 @@ pub fn run() {
|
||||||
networking::commands::load_chat_messages,
|
networking::commands::load_chat_messages,
|
||||||
networking::commands::probe_connection_slot,
|
networking::commands::probe_connection_slot,
|
||||||
networking::commands::disconnect_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!())
|
.run(tauri::generate_context!())
|
||||||
.expect("failed to run AstrBot Desktop");
|
.expect("failed to run AstrBot Desktop");
|
||||||
|
|
|
||||||
|
|
@ -400,6 +400,13 @@ pub async fn disconnect_connection_slot(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[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(
|
pub async fn send_connection_slot_message(
|
||||||
database: State<'_, ApplicationDatabase>,
|
database: State<'_, ApplicationDatabase>,
|
||||||
runtime: State<'_, DualSlotRuntime>,
|
runtime: State<'_, DualSlotRuntime>,
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ pub use astrbot_client::{AstrBotClient, AstrBotClientConfig, AstrBotClientError}
|
||||||
pub use commands::{
|
pub use commands::{
|
||||||
DisconnectConnectionSlotRequest, DrainConnectionRuntimeEventsRequest,
|
DisconnectConnectionSlotRequest, DrainConnectionRuntimeEventsRequest,
|
||||||
ProbeConnectionSlotRequest, SendConnectionSlotMessageRequest, auto_connect_configured_slots,
|
ProbeConnectionSlotRequest, SendConnectionSlotMessageRequest, auto_connect_configured_slots,
|
||||||
connection_runtime_status, disconnect_connection_slot, drain_connection_runtime_events,
|
cancel_connection_slot_message, connection_runtime_status, disconnect_connection_slot,
|
||||||
probe_connection_slot, send_connection_slot_message,
|
drain_connection_runtime_events, probe_connection_slot, send_connection_slot_message,
|
||||||
};
|
};
|
||||||
pub use gateway_client::{BoundGatewayEvent, GatewayClient, GatewayClientConfig, GatewayError};
|
pub use gateway_client::{BoundGatewayEvent, GatewayClient, GatewayClientConfig, GatewayError};
|
||||||
pub use manager::{DualConnectionManager, SlotKind};
|
pub use manager::{DualConnectionManager, SlotKind};
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
use desktop_protocol::{ConnectionState, TransportKind};
|
use desktop_protocol::{ConnectionState, TransportKind};
|
||||||
use secrecy::SecretString;
|
use secrecy::SecretString;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::sync::{Mutex, mpsc};
|
use tokio::sync::{Mutex, mpsc};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
AstrBotClient, AstrBotClientConfig, BoundGatewayEvent, GatewayClient, GatewayClientConfig,
|
AstrBotClient, AstrBotClientConfig, AstrBotClientError, BoundGatewayEvent, GatewayClient,
|
||||||
GatewayError, SlotKind,
|
GatewayClientConfig, GatewayError, SlotKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
|
@ -48,6 +49,7 @@ enum ActiveClient {
|
||||||
struct SlotRuntime {
|
struct SlotRuntime {
|
||||||
snapshot: SlotRuntimeSnapshot,
|
snapshot: SlotRuntimeSnapshot,
|
||||||
client: Option<ActiveClient>,
|
client: Option<ActiveClient>,
|
||||||
|
cancellation: Option<tokio_util::sync::CancellationToken>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SlotRuntime {
|
impl SlotRuntime {
|
||||||
|
|
@ -63,6 +65,7 @@ impl SlotRuntime {
|
||||||
last_error: None,
|
last_error: None,
|
||||||
},
|
},
|
||||||
client: None,
|
client: None,
|
||||||
|
cancellation: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -186,6 +189,7 @@ impl DualSlotRuntime {
|
||||||
last_error: None,
|
last_error: None,
|
||||||
},
|
},
|
||||||
client: None,
|
client: None,
|
||||||
|
cancellation: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match request.transport {
|
match request.transport {
|
||||||
|
|
@ -282,6 +286,17 @@ impl DualSlotRuntime {
|
||||||
Ok(runtime.snapshot.clone())
|
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(
|
pub async fn send_slot_message(
|
||||||
&self,
|
&self,
|
||||||
slot: SlotKind,
|
slot: SlotKind,
|
||||||
|
|
@ -294,12 +309,14 @@ impl DualSlotRuntime {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take the client out so we can await without holding the mutex across network IO.
|
// 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 mut guard = self.inner.lock().await;
|
||||||
let runtime = slot_mut(&mut guard, slot);
|
let runtime = slot_mut(&mut guard, slot);
|
||||||
let client = runtime.client.take().ok_or_else(|| {
|
let client = runtime.client.take().ok_or_else(|| {
|
||||||
RuntimeCommandError::unavailable("The selected slot is not connected.")
|
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;
|
runtime.snapshot.state = ConnectionState::Syncing;
|
||||||
(
|
(
|
||||||
client,
|
client,
|
||||||
|
|
@ -307,12 +324,12 @@ impl DualSlotRuntime {
|
||||||
runtime.snapshot.transport,
|
runtime.snapshot.transport,
|
||||||
runtime.snapshot.server_instance_id.clone(),
|
runtime.snapshot.server_instance_id.clone(),
|
||||||
runtime.snapshot.active_session_id.clone(),
|
runtime.snapshot.active_session_id.clone(),
|
||||||
|
cancellation,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let send_result = match client {
|
let send_result = match client {
|
||||||
ActiveClient::Desktop(client) => {
|
ActiveClient::Desktop(client) => {
|
||||||
// Desktop mock path remains stream-oriented; chat POST is a later wire-up.
|
|
||||||
let restored = ActiveClient::Desktop(client);
|
let restored = ActiveClient::Desktop(client);
|
||||||
Err((
|
Err((
|
||||||
restored,
|
restored,
|
||||||
|
|
@ -322,16 +339,37 @@ impl DualSlotRuntime {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
ActiveClient::AstrBot(client) => {
|
ActiveClient::AstrBot(client) => {
|
||||||
let cancel = tokio_util::sync::CancellationToken::new();
|
let mut attempts = 0;
|
||||||
match client
|
loop {
|
||||||
.send_chat_stream(&message, &self.event_tx, &cancel)
|
match client
|
||||||
.await
|
.send_chat_stream(&message, &self.event_tx, &cancellation)
|
||||||
{
|
.await
|
||||||
Ok(session) => Ok((ActiveClient::AstrBot(client), session)),
|
{
|
||||||
Err(_) => Err((
|
Ok(_session) if cancellation.is_cancelled() => {
|
||||||
ActiveClient::AstrBot(client),
|
break Err((
|
||||||
RuntimeCommandError::unavailable("AstrBot chat send failed."),
|
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 {
|
fn slot_mut(inner: &mut DualSlotRuntimeInner, slot: SlotKind) -> &mut SlotRuntime {
|
||||||
match slot {
|
match slot {
|
||||||
SlotKind::Companion => &mut inner.companion,
|
SlotKind::Companion => &mut inner.companion,
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用