AstrBot-Desktop/src-tauri/src/networking/astrbot_client.rs

525 行
19 KiB
Rust

use std::{collections::VecDeque, sync::Arc, time::Duration};
use desktop_protocol::{
AstrBotApiEnvelope, AstrBotAttachment, AstrBotCapabilitySnapshot, AstrBotChatRequest,
AstrBotConfigListData, AstrBotModelOption, AstrBotSessionListData, AstrBotStreamMapper,
BoundGatewayEvent, ConnectionState, GatewayEvent, PROTOCOL_VERSION,
derive_astrbot_server_instance_id,
};
use futures_util::StreamExt;
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue};
use secrecy::{ExposeSecret, SecretString};
use serde_json::{Value, json};
use thiserror::Error;
use tokio::sync::{Mutex, mpsc, watch};
use tokio_util::sync::CancellationToken;
use super::{GatewayError, SlotKind};
#[derive(Debug, Clone)]
pub struct AstrBotClientConfig {
pub profile_id: String,
pub slot: SlotKind,
pub base_url: String,
pub api_key: SecretString,
/// Remote WebChat username used as session owner identity.
pub remote_username: String,
/// Optional sticky session id. When absent, the first chat response supplies one.
pub session_id: Option<String>,
/// Expected server identity substitute. Empty means accept first observed identity.
pub expected_server_instance_id: Option<String>,
}
#[derive(Debug, Error)]
pub enum AstrBotClientError {
#[error("astrbot request failed")]
Request(#[from] reqwest::Error),
#[error("astrbot returned HTTP {0}")]
HttpStatus(reqwest::StatusCode),
#[error("astrbot api status error: {0}")]
ApiStatus(String),
#[error("invalid astrbot payload")]
Json(#[from] serde_json::Error),
#[error("astrbot stream mapping failed: {0}")]
Map(String),
#[error("server identity changed")]
ServerIdentityChanged,
#[error("astrbot event channel closed")]
ChannelClosed,
#[error("invalid configuration: {0}")]
InvalidConfig(&'static str),
}
impl From<AstrBotClientError> for GatewayError {
fn from(value: AstrBotClientError) -> Self {
match value {
AstrBotClientError::Request(error) => GatewayError::Request(error),
AstrBotClientError::HttpStatus(status) => GatewayError::HttpStatus(status),
AstrBotClientError::Json(error) => GatewayError::Json(error),
AstrBotClientError::ServerIdentityChanged => GatewayError::ServerIdentityChanged,
AstrBotClientError::ChannelClosed => GatewayError::ChannelClosed,
AstrBotClientError::ApiStatus(message) => GatewayError::Adapter(message),
AstrBotClientError::Map(message) => GatewayError::Adapter(message),
AstrBotClientError::InvalidConfig(message) => GatewayError::Adapter(message.to_owned()),
}
}
}
const MAX_DEDUPLICATED_EVENTS: usize = 4_096;
#[derive(Default)]
struct EventDeduplicator {
ids: std::collections::HashSet<String>,
order: VecDeque<String>,
}
impl EventDeduplicator {
fn insert(&mut self, event_id: String) -> bool {
if !self.ids.insert(event_id.clone()) {
return false;
}
self.order.push_back(event_id);
if self.order.len() > MAX_DEDUPLICATED_EVENTS
&& let Some(expired) = self.order.pop_front()
{
self.ids.remove(&expired);
}
true
}
}
#[derive(Clone)]
pub struct AstrBotClient {
config: AstrBotClientConfig,
http: reqwest::Client,
seen_event_ids: Arc<Mutex<EventDeduplicator>>,
session_id: Arc<Mutex<Option<String>>>,
server_instance_id: Arc<Mutex<String>>,
state_tx: watch::Sender<ConnectionState>,
}
impl AstrBotClient {
pub fn new(config: AstrBotClientConfig) -> Result<Self, AstrBotClientError> {
if config.base_url.trim().is_empty() {
return Err(AstrBotClientError::InvalidConfig("base_url is required"));
}
if config.remote_username.trim().is_empty() {
return Err(AstrBotClientError::InvalidConfig(
"remote_username is required",
));
}
if config.api_key.expose_secret().trim().is_empty() {
return Err(AstrBotClientError::InvalidConfig("api_key is required"));
}
let base = config.base_url.trim_end_matches('/').to_owned();
let derived = derive_astrbot_server_instance_id(&base);
let initial_identity = config
.expected_server_instance_id
.clone()
.filter(|value| !value.trim().is_empty())
.unwrap_or(derived);
let mut headers = HeaderMap::new();
let auth_value = format!("Bearer {}", config.api_key.expose_secret());
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&auth_value).map_err(|_| {
AstrBotClientError::InvalidConfig("api_key has invalid header chars")
})?,
);
let http = reqwest::Client::builder()
.default_headers(headers)
.redirect(reqwest::redirect::Policy::none())
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(120))
.build()?;
let (state_tx, _) = watch::channel(ConnectionState::Disconnected);
let initial_session = config.session_id.clone();
Ok(Self {
config: AstrBotClientConfig {
base_url: base,
..config
},
http,
seen_event_ids: Arc::new(Mutex::new(EventDeduplicator::default())),
session_id: Arc::new(Mutex::new(initial_session)),
server_instance_id: Arc::new(Mutex::new(initial_identity)),
state_tx,
})
}
pub fn state_receiver(&self) -> watch::Receiver<ConnectionState> {
self.state_tx.subscribe()
}
pub fn server_instance_id_blocking(&self) -> String {
self.server_instance_id
.try_lock()
.map(|guard| guard.clone())
.unwrap_or_else(|_| derive_astrbot_server_instance_id(&self.config.base_url))
}
fn set_state(&self, state: ConnectionState) {
self.state_tx.send_replace(state);
}
fn url(&self, path: &str) -> String {
format!("{}{}", self.config.base_url, path)
}
pub async fn probe(&self) -> Result<Value, AstrBotClientError> {
self.set_state(ConnectionState::Connecting);
let response = self
.http
.get(self.url(&format!(
"/api/v1/chat/sessions?username={}&page=1&page_size=1",
urlencoding_username(&self.config.remote_username)
)))
.send()
.await?;
if response.status().as_u16() == 401 || response.status().as_u16() == 403 {
self.set_state(ConnectionState::AuthExpired);
return Err(AstrBotClientError::HttpStatus(response.status()));
}
if !response.status().is_success() {
self.set_state(ConnectionState::ServerUnavailable);
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let envelope: AstrBotApiEnvelope<AstrBotSessionListData> = response.json().await?;
if !envelope.is_ok() {
self.set_state(ConnectionState::FatalError);
return Err(AstrBotClientError::ApiStatus(
envelope
.message
.unwrap_or_else(|| "chat sessions probe failed".into()),
));
}
self.set_state(ConnectionState::Connected);
Ok(json!({
"status": "ok",
"protocolVersion": PROTOCOL_VERSION,
"transport": "astrbot-openapi",
"serverInstanceId": self.server_instance_id.lock().await.clone(),
"remoteUsername": self.config.remote_username,
"sessionCount": envelope.data.map(|data| data.total.unwrap_or(0)).unwrap_or(0),
}))
}
pub async fn list_sessions(&self) -> Result<AstrBotSessionListData, AstrBotClientError> {
let response = self
.http
.get(self.url(&format!(
"/api/v1/chat/sessions?username={}&page=1&page_size=50",
urlencoding_username(&self.config.remote_username)
)))
.send()
.await?;
if !response.status().is_success() {
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let envelope: AstrBotApiEnvelope<AstrBotSessionListData> = response.json().await?;
if !envelope.is_ok() {
return Err(AstrBotClientError::ApiStatus(
envelope
.message
.unwrap_or_else(|| "list sessions failed".into()),
));
}
Ok(envelope.data.unwrap_or(AstrBotSessionListData {
sessions: Vec::new(),
page: Some(1),
page_size: Some(50),
total: Some(0),
}))
}
pub async fn list_configs(&self) -> Result<AstrBotConfigListData, AstrBotClientError> {
let response = self.http.get(self.url("/api/v1/configs")).send().await?;
if !response.status().is_success() {
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let envelope: AstrBotApiEnvelope<AstrBotConfigListData> = response.json().await?;
if !envelope.is_ok() {
return Err(AstrBotClientError::ApiStatus(
envelope
.message
.unwrap_or_else(|| "list configs failed".into()),
));
}
Ok(envelope.data.unwrap_or(AstrBotConfigListData {
configs: Vec::new(),
}))
}
pub async fn capabilities(&self) -> Result<AstrBotCapabilitySnapshot, AstrBotClientError> {
let configs = self.list_configs().await?;
let models = configs
.configs
.into_iter()
.map(|config| AstrBotModelOption {
id: config.id,
name: config.name,
is_default: config.is_default,
})
.collect::<Vec<_>>();
Ok(AstrBotCapabilitySnapshot {
transport: "astrbot-openapi".to_owned(),
text_chat: true,
streaming: true,
model_selection: !models.is_empty(),
file_upload: true,
image_input: true,
models,
})
}
pub async fn upload_file(
&self,
file_name: &str,
mime_type: &str,
bytes: Vec<u8>,
) -> Result<AstrBotAttachment, AstrBotClientError> {
if file_name.trim().is_empty() || mime_type.trim().is_empty() || bytes.is_empty() {
return Err(AstrBotClientError::InvalidConfig(
"file_name, mime_type, and file bytes are required",
));
}
let part = reqwest::multipart::Part::bytes(bytes.clone())
.file_name(file_name.to_owned())
.mime_str(mime_type)
.map_err(|_| AstrBotClientError::InvalidConfig("invalid attachment mime type"))?;
let form = reqwest::multipart::Form::new().part("file", part);
let response = self
.http
.post(self.url("/api/v1/file"))
.multipart(form)
.send()
.await?;
if !response.status().is_success() {
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let envelope: AstrBotApiEnvelope<Value> = response.json().await?;
if !envelope.is_ok() {
return Err(AstrBotClientError::ApiStatus(
envelope
.message
.unwrap_or_else(|| "file upload failed".into()),
));
}
let data = envelope.data.unwrap_or(Value::Null);
let attachment_id = data
.get("id")
.or_else(|| data.get("file_id"))
.or_else(|| data.get("attachment_id"))
.and_then(Value::as_str)
.ok_or_else(|| AstrBotClientError::Map("upload response has no attachment id".into()))?
.to_owned();
Ok(AstrBotAttachment {
attachment_id,
file_name: file_name.to_owned(),
mime_type: mime_type.to_owned(),
size_bytes: bytes.len() as u64,
url: data
.get("url")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
})
}
pub async fn send_chat_stream(
&self,
message: &str,
config_id: Option<String>,
attachments: Vec<AstrBotAttachment>,
sender: &mpsc::Sender<super::gateway_client::BoundGatewayEvent>,
cancellation: &CancellationToken,
) -> Result<Option<String>, AstrBotClientError> {
self.set_state(ConnectionState::Syncing);
let session_id = self.session_id.lock().await.clone();
let request = AstrBotChatRequest {
message: Value::String(message.to_owned()),
session_id: session_id.clone(),
conversation_id: None,
username: Some(self.config.remote_username.clone()),
config_id,
attachments: (!attachments.is_empty()).then_some(attachments),
platform_id: None,
enable_streaming: Some(true),
};
let response = self
.http
.post(self.url("/api/v1/chat"))
.header(ACCEPT, HeaderValue::from_static("text/event-stream"))
.json(&request)
.send()
.await?;
if response.status().as_u16() == 401 || response.status().as_u16() == 403 {
self.set_state(ConnectionState::AuthExpired);
return Err(AstrBotClientError::HttpStatus(response.status()));
}
if !response.status().is_success() {
self.set_state(ConnectionState::ServerUnavailable);
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let server_instance_id = self.server_instance_id.lock().await.clone();
if let Some(expected) = &self.config.expected_server_instance_id
&& !expected.is_empty()
&& expected != &server_instance_id
{
self.set_state(ConnectionState::CertificateChanged);
return Err(AstrBotClientError::ServerIdentityChanged);
}
let mut mapper = AstrBotStreamMapper::new(server_instance_id.clone());
if let Some(existing) = session_id {
let seed = serde_json::json!({
"type": "session_id",
"data": null,
"session_id": existing,
});
let _ = mapper.push_line(&format!("data: {seed}"));
}
let mut stream = response.bytes_stream();
let mut buffer = String::new();
self.set_state(ConnectionState::Connected);
loop {
tokio::select! {
_ = cancellation.cancelled() => {
self.set_state(ConnectionState::Disconnected);
return Ok(mapper.session_id().map(str::to_owned));
}
next = stream.next() => {
let Some(next) = next else { break; };
let chunk = next?;
buffer.push_str(&String::from_utf8_lossy(&chunk));
while let Some(index) = buffer.find('\n') {
let mut line = buffer[..index].to_owned();
buffer.drain(..=index);
if line.ends_with('\r') {
line.pop();
}
let events = mapper
.push_line(&line)
.map_err(|error| AstrBotClientError::Map(error.to_string()))?;
self.dispatch_events(events, sender).await?;
}
}
}
}
if !buffer.trim().is_empty() {
let events = mapper
.push_line(buffer.trim_end_matches('\r'))
.map_err(|error| AstrBotClientError::Map(error.to_string()))?;
self.dispatch_events(events, sender).await?;
}
let events = mapper.finish_stream();
self.dispatch_events(events, sender).await?;
if let Some(session) = mapper.session_id() {
*self.session_id.lock().await = Some(session.to_owned());
}
self.set_state(ConnectionState::Connected);
Ok(self.session_id.lock().await.clone())
}
async fn dispatch_events(
&self,
events: Vec<GatewayEvent>,
sender: &mpsc::Sender<super::gateway_client::BoundGatewayEvent>,
) -> Result<(), AstrBotClientError> {
for event in events {
if let Some(session_id) = event.metadata.session_id.clone() {
*self.session_id.lock().await = Some(session_id);
}
let event_id = event.metadata.event_id.clone();
let mut seen = self.seen_event_ids.lock().await;
if !seen.insert(event_id.clone()) {
continue;
}
drop(seen);
let payload = serde_json::to_value(&event.body)
.map_err(|error| AstrBotClientError::Map(error.to_string()))?;
let bound = BoundGatewayEvent {
profile_id: self.config.profile_id.clone(),
slot: protocol_slot(self.config.slot),
event,
};
sender
.send(super::gateway_client::BoundGatewayEvent {
profile_id: bound.profile_id,
slot: self.config.slot,
event_id,
event_type: bound.event.event_type().to_owned(),
server_instance_id: bound.event.metadata.server_instance_id.clone(),
session_id: bound.event.metadata.session_id.clone(),
message_id: bound.event.metadata.message_id.clone(),
payload,
})
.await
.map_err(|_| AstrBotClientError::ChannelClosed)?;
}
Ok(())
}
}
fn protocol_slot(slot: SlotKind) -> desktop_protocol::ActiveSlotKind {
match slot {
SlotKind::Companion => desktop_protocol::ActiveSlotKind::Companion,
SlotKind::Operations => desktop_protocol::ActiveSlotKind::Operations,
}
}
fn urlencoding_username(username: &str) -> String {
username
.chars()
.map(|ch| match ch {
'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => ch.to_string(),
_ => format!("%{:02X}", ch as u8),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_empty_api_key_and_username() {
let result = AstrBotClient::new(AstrBotClientConfig {
profile_id: "p".into(),
slot: SlotKind::Companion,
base_url: "http://127.0.0.1:1".into(),
api_key: SecretString::from(String::new()),
remote_username: "user".into(),
session_id: None,
expected_server_instance_id: None,
});
assert!(matches!(result, Err(AstrBotClientError::InvalidConfig(_))));
}
#[test]
fn derives_identity_from_base_url() {
let client = AstrBotClient::new(AstrBotClientConfig {
profile_id: "p".into(),
slot: SlotKind::Companion,
base_url: "http://192.168.31.210:26285/".into(),
api_key: SecretString::from("abk_test".to_owned()),
remote_username: "desktop-companion".into(),
session_id: Some("s1".into()),
expected_server_instance_id: None,
})
.expect("client");
assert!(client.server_instance_id_blocking().starts_with("astrbot-"));
}
}