2026-07-20 11:31:41 +08:00
|
|
|
// IPC wrappers for dual-slot runtime. Secrets never leave Rust.
|
|
|
|
|
use desktop_protocol::TransportKind;
|
|
|
|
|
use secrecy::SecretString;
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
use tauri::State;
|
2026-07-20 12:01:18 +08:00
|
|
|
use tracing::{info, warn};
|
2026-07-20 11:31:41 +08:00
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
|
ConnectionRuntimeStatus, DualSlotRuntime, RuntimeCommandError, SlotActivationRequest, SlotKind,
|
|
|
|
|
SlotRuntimeSnapshot,
|
|
|
|
|
};
|
|
|
|
|
use crate::credentials::{CredentialCommandError, load_profile_secret};
|
|
|
|
|
use crate::database::ApplicationDatabase;
|
|
|
|
|
use crate::secure_store::WindowsCredentialStore;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct ProbeConnectionSlotRequest {
|
|
|
|
|
pub slot: SlotKind,
|
|
|
|
|
pub profile_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct DisconnectConnectionSlotRequest {
|
|
|
|
|
pub slot: SlotKind,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct SendConnectionSlotMessageRequest {
|
|
|
|
|
pub slot: SlotKind,
|
|
|
|
|
pub message: String,
|
2026-07-20 13:30:29 +08:00
|
|
|
pub config_id: Option<String>,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub attachments: Vec<desktop_protocol::AstrBotAttachment>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct SlotFileUploadRequest {
|
|
|
|
|
pub slot: SlotKind,
|
|
|
|
|
pub file_name: String,
|
|
|
|
|
pub mime_type: String,
|
|
|
|
|
pub bytes: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct SlotCapabilitiesRequest {
|
|
|
|
|
pub slot: SlotKind,
|
2026-07-20 11:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct DrainConnectionRuntimeEventsRequest {
|
|
|
|
|
pub limit: Option<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct LoadChatMessagesRequest {
|
|
|
|
|
pub profile_id: String,
|
|
|
|
|
pub server_instance_id: String,
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
pub limit: Option<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct ListChatSessionsRequest {
|
|
|
|
|
pub profile_id: String,
|
|
|
|
|
pub server_instance_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct MarkChatSessionReadRequest {
|
|
|
|
|
pub profile_id: String,
|
|
|
|
|
pub server_instance_id: String,
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn connection_runtime_status(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
) -> Result<ConnectionRuntimeStatus, RuntimeCommandError> {
|
|
|
|
|
Ok(runtime.status().await)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 12:01:18 +08:00
|
|
|
/// Starts profiles explicitly marked for startup connection.
|
|
|
|
|
///
|
|
|
|
|
/// Each slot is attempted independently. A failure is retained in that slot's
|
|
|
|
|
/// runtime snapshot and does not prevent the other configured slot from being attempted.
|
|
|
|
|
pub async fn auto_connect_configured_slots(
|
|
|
|
|
database: &ApplicationDatabase,
|
|
|
|
|
runtime: &DualSlotRuntime,
|
|
|
|
|
) {
|
|
|
|
|
let profiles = match database.list_profiles() {
|
|
|
|
|
Ok(profiles) => profiles,
|
|
|
|
|
Err(error) => {
|
|
|
|
|
warn!(error = %error, "startup auto-connect could not read profiles");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for profile in profiles.into_iter().filter(|profile| profile.auto_connect) {
|
|
|
|
|
let Some(active_slot) = profile.auto_connect_slot else {
|
|
|
|
|
warn!(profile_id = %profile.id, "startup auto-connect skipped profile without a slot");
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
let slot = match active_slot {
|
|
|
|
|
desktop_protocol::ActiveSlotKind::Companion => SlotKind::Companion,
|
|
|
|
|
desktop_protocol::ActiveSlotKind::Operations => SlotKind::Operations,
|
|
|
|
|
};
|
|
|
|
|
let profile_id = profile.id.clone();
|
|
|
|
|
let activation = match startup_activation_request(database, &profile, slot) {
|
|
|
|
|
Ok(request) => request,
|
|
|
|
|
Err(error) => {
|
|
|
|
|
warn!(profile_id = %profile_id, slot = ?slot, error = %error, "startup auto-connect skipped");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match runtime.activate_slot(activation).await {
|
|
|
|
|
Ok(snapshot) => info!(
|
|
|
|
|
profile_id = %profile_id,
|
|
|
|
|
slot = ?slot,
|
|
|
|
|
state = ?snapshot.state,
|
|
|
|
|
"startup auto-connect completed"
|
|
|
|
|
),
|
|
|
|
|
Err(error) => warn!(
|
|
|
|
|
profile_id = %profile_id,
|
|
|
|
|
slot = ?slot,
|
|
|
|
|
error = %error,
|
|
|
|
|
"startup auto-connect failed"
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn startup_activation_request(
|
|
|
|
|
database: &ApplicationDatabase,
|
|
|
|
|
profile: &desktop_protocol::ConnectionProfile,
|
|
|
|
|
slot: SlotKind,
|
|
|
|
|
) -> Result<SlotActivationRequest, RuntimeCommandError> {
|
|
|
|
|
let secret = load_profile_secret(database, &WindowsCredentialStore, &profile.id).map_err(
|
|
|
|
|
|error: CredentialCommandError| RuntimeCommandError {
|
|
|
|
|
code: error.code,
|
|
|
|
|
message: error.message,
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
match profile.transport_kind {
|
|
|
|
|
TransportKind::AstrBotOpenApi => {
|
|
|
|
|
let secret = secret.ok_or(RuntimeCommandError {
|
|
|
|
|
code: "credentialMissing",
|
|
|
|
|
message: "Enroll an API key before probing an AstrBot profile.",
|
|
|
|
|
})?;
|
|
|
|
|
let api_key =
|
|
|
|
|
String::from_utf8(secret.secret().to_vec()).map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "credentialInvalid",
|
|
|
|
|
message: "Stored API key is not valid UTF-8.",
|
|
|
|
|
})?;
|
|
|
|
|
let remote_username = profile.remote_username.clone().ok_or(RuntimeCommandError {
|
|
|
|
|
code: "validationFailed",
|
|
|
|
|
message: "AstrBot profiles require remote_username.",
|
|
|
|
|
})?;
|
|
|
|
|
Ok(SlotActivationRequest {
|
|
|
|
|
profile_id: profile.id.clone(),
|
|
|
|
|
slot,
|
|
|
|
|
base_url: profile.base_url.clone(),
|
|
|
|
|
transport: TransportKind::AstrBotOpenApi,
|
|
|
|
|
api_key: Some(SecretString::from(api_key)),
|
|
|
|
|
bearer_token: None,
|
|
|
|
|
remote_username: Some(remote_username),
|
2026-07-20 16:28:42 +08:00
|
|
|
remote_display_name: profile.remote_display_name.clone(),
|
2026-07-20 12:01:18 +08:00
|
|
|
session_id: profile.sticky_session_id.clone(),
|
|
|
|
|
expected_server_instance_id: profile.server_instance_id.clone(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
TransportKind::DesktopV1 => {
|
|
|
|
|
let bearer = secret.and_then(|value| {
|
|
|
|
|
String::from_utf8(value.secret().to_vec())
|
|
|
|
|
.ok()
|
|
|
|
|
.map(SecretString::from)
|
|
|
|
|
});
|
|
|
|
|
let expected = profile
|
|
|
|
|
.server_instance_id
|
|
|
|
|
.clone()
|
|
|
|
|
.ok_or(RuntimeCommandError {
|
|
|
|
|
code: "validationFailed",
|
|
|
|
|
message: "Desktop gateway profiles require server_instance_id before probe.",
|
|
|
|
|
})?;
|
|
|
|
|
Ok(SlotActivationRequest {
|
|
|
|
|
profile_id: profile.id.clone(),
|
|
|
|
|
slot,
|
|
|
|
|
base_url: profile.base_url.clone(),
|
|
|
|
|
transport: TransportKind::DesktopV1,
|
|
|
|
|
api_key: None,
|
|
|
|
|
bearer_token: bearer,
|
|
|
|
|
remote_username: None,
|
2026-07-20 16:28:42 +08:00
|
|
|
remote_display_name: None,
|
2026-07-20 12:01:18 +08:00
|
|
|
session_id: None,
|
|
|
|
|
expected_server_instance_id: Some(expected),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn drain_connection_runtime_events(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: DrainConnectionRuntimeEventsRequest,
|
|
|
|
|
) -> Result<Vec<super::BoundGatewayEvent>, RuntimeCommandError> {
|
|
|
|
|
let events = runtime.drain_events(request.limit.unwrap_or(64)).await;
|
|
|
|
|
for event in &events {
|
|
|
|
|
persist_runtime_event(&database, event);
|
|
|
|
|
}
|
|
|
|
|
Ok(events)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn list_chat_sessions(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
request: ListChatSessionsRequest,
|
|
|
|
|
) -> Result<Vec<crate::database::StoredChatSession>, RuntimeCommandError> {
|
|
|
|
|
database
|
|
|
|
|
.list_chat_sessions(&request.profile_id, &request.server_instance_id)
|
|
|
|
|
.map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "databaseUnavailable",
|
|
|
|
|
message: "Chat sessions are unavailable.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn mark_chat_session_read(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
request: MarkChatSessionReadRequest,
|
|
|
|
|
) -> Result<(), RuntimeCommandError> {
|
|
|
|
|
database
|
|
|
|
|
.mark_chat_session_read(
|
|
|
|
|
&request.profile_id,
|
|
|
|
|
&request.server_instance_id,
|
|
|
|
|
&request.session_id,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "databaseUnavailable",
|
|
|
|
|
message: "Chat session state is unavailable.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn load_chat_messages(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
request: LoadChatMessagesRequest,
|
|
|
|
|
) -> Result<Vec<crate::database::StoredChatMessage>, RuntimeCommandError> {
|
|
|
|
|
database
|
|
|
|
|
.list_chat_messages(
|
|
|
|
|
&request.profile_id,
|
|
|
|
|
&request.server_instance_id,
|
|
|
|
|
&request.session_id,
|
|
|
|
|
request.limit.unwrap_or(100),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "databaseUnavailable",
|
|
|
|
|
message: "Chat history is unavailable.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn persist_runtime_event(database: &ApplicationDatabase, event: &super::BoundGatewayEvent) {
|
|
|
|
|
let Some(session_id) = event.session_id.as_deref() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let Some((role, content, streaming, message_id)) = (match event.event_type.as_str() {
|
|
|
|
|
"message.delta" => Some((
|
|
|
|
|
"assistant",
|
|
|
|
|
event
|
|
|
|
|
.payload
|
|
|
|
|
.get("delta")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
true,
|
|
|
|
|
event
|
|
|
|
|
.message_id
|
|
|
|
|
.as_deref()
|
|
|
|
|
.unwrap_or(event.event_id.as_str()),
|
|
|
|
|
)),
|
|
|
|
|
"message.completed" => Some((
|
|
|
|
|
"assistant",
|
|
|
|
|
event
|
|
|
|
|
.payload
|
|
|
|
|
.get("content")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
false,
|
|
|
|
|
event
|
|
|
|
|
.message_id
|
|
|
|
|
.as_deref()
|
|
|
|
|
.unwrap_or(event.event_id.as_str()),
|
|
|
|
|
)),
|
|
|
|
|
"message.proactive" => Some((
|
|
|
|
|
"assistant",
|
|
|
|
|
event
|
|
|
|
|
.payload
|
|
|
|
|
.get("content")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
false,
|
|
|
|
|
event.event_id.as_str(),
|
|
|
|
|
)),
|
|
|
|
|
"error" => Some((
|
|
|
|
|
"system",
|
|
|
|
|
event
|
|
|
|
|
.payload
|
|
|
|
|
.get("message")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.unwrap_or("Connection error"),
|
|
|
|
|
false,
|
|
|
|
|
event.event_id.as_str(),
|
|
|
|
|
)),
|
|
|
|
|
_ => None,
|
|
|
|
|
}) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let slot = match event.slot {
|
|
|
|
|
SlotKind::Companion => "companion",
|
|
|
|
|
SlotKind::Operations => "operations",
|
|
|
|
|
};
|
|
|
|
|
let _ = database.upsert_chat_message(
|
|
|
|
|
"local-default",
|
|
|
|
|
&event.profile_id,
|
|
|
|
|
slot,
|
|
|
|
|
&event.server_instance_id,
|
|
|
|
|
None,
|
|
|
|
|
session_id,
|
|
|
|
|
message_id,
|
|
|
|
|
role,
|
|
|
|
|
content,
|
|
|
|
|
streaming,
|
|
|
|
|
event.event_type == "error",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn probe_connection_slot(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: ProbeConnectionSlotRequest,
|
|
|
|
|
) -> Result<SlotRuntimeSnapshot, RuntimeCommandError> {
|
|
|
|
|
let profile = database
|
|
|
|
|
.profile(&request.profile_id)
|
|
|
|
|
.map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "databaseUnavailable",
|
|
|
|
|
message: "The local profile database is unavailable.",
|
|
|
|
|
})?
|
|
|
|
|
.ok_or(RuntimeCommandError {
|
|
|
|
|
code: "notFound",
|
|
|
|
|
message: "The connection profile no longer exists.",
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if profile.transport_kind != TransportKind::AstrBotOpenApi
|
|
|
|
|
&& profile.transport_kind != TransportKind::DesktopV1
|
|
|
|
|
{
|
|
|
|
|
return Err(RuntimeCommandError {
|
|
|
|
|
code: "validationFailed",
|
|
|
|
|
message: "Unsupported transport kind.",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let secret = load_profile_secret(&database, &WindowsCredentialStore, &profile.id).map_err(
|
|
|
|
|
|error: CredentialCommandError| RuntimeCommandError {
|
|
|
|
|
code: error.code,
|
|
|
|
|
message: error.message,
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let activation = match profile.transport_kind {
|
|
|
|
|
TransportKind::AstrBotOpenApi => {
|
|
|
|
|
let secret = secret.ok_or(RuntimeCommandError {
|
|
|
|
|
code: "credentialMissing",
|
|
|
|
|
message: "Enroll an API key before probing an AstrBot profile.",
|
|
|
|
|
})?;
|
|
|
|
|
let api_key =
|
|
|
|
|
String::from_utf8(secret.secret().to_vec()).map_err(|_| RuntimeCommandError {
|
|
|
|
|
code: "credentialInvalid",
|
|
|
|
|
message: "Stored API key is not valid UTF-8.",
|
|
|
|
|
})?;
|
|
|
|
|
let remote_username = profile.remote_username.clone().ok_or(RuntimeCommandError {
|
|
|
|
|
code: "validationFailed",
|
|
|
|
|
message: "AstrBot profiles require remote_username.",
|
|
|
|
|
})?;
|
|
|
|
|
SlotActivationRequest {
|
|
|
|
|
profile_id: profile.id.clone(),
|
|
|
|
|
slot: request.slot,
|
|
|
|
|
base_url: profile.base_url.clone(),
|
|
|
|
|
transport: TransportKind::AstrBotOpenApi,
|
|
|
|
|
api_key: Some(SecretString::from(api_key)),
|
|
|
|
|
bearer_token: None,
|
|
|
|
|
remote_username: Some(remote_username),
|
2026-07-20 16:28:42 +08:00
|
|
|
remote_display_name: profile.remote_display_name.clone(),
|
2026-07-20 11:31:41 +08:00
|
|
|
session_id: profile.sticky_session_id.clone(),
|
|
|
|
|
expected_server_instance_id: profile.server_instance_id.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TransportKind::DesktopV1 => {
|
|
|
|
|
let bearer = secret.and_then(|value| {
|
|
|
|
|
String::from_utf8(value.secret().to_vec())
|
|
|
|
|
.ok()
|
|
|
|
|
.map(SecretString::from)
|
|
|
|
|
});
|
|
|
|
|
let expected = profile
|
|
|
|
|
.server_instance_id
|
|
|
|
|
.clone()
|
|
|
|
|
.ok_or(RuntimeCommandError {
|
|
|
|
|
code: "validationFailed",
|
|
|
|
|
message: "Desktop gateway profiles require server_instance_id before probe.",
|
|
|
|
|
})?;
|
|
|
|
|
SlotActivationRequest {
|
|
|
|
|
profile_id: profile.id.clone(),
|
|
|
|
|
slot: request.slot,
|
|
|
|
|
base_url: profile.base_url.clone(),
|
|
|
|
|
transport: TransportKind::DesktopV1,
|
|
|
|
|
api_key: None,
|
|
|
|
|
bearer_token: bearer,
|
|
|
|
|
remote_username: None,
|
2026-07-20 16:28:42 +08:00
|
|
|
remote_display_name: None,
|
2026-07-20 11:31:41 +08:00
|
|
|
session_id: None,
|
|
|
|
|
expected_server_instance_id: Some(expected),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let snapshot = runtime.activate_slot(activation).await?;
|
|
|
|
|
|
|
|
|
|
// Persist observed identity / sticky session without exposing secrets.
|
|
|
|
|
if let Ok(Some(mut stored)) = database.profile(&request.profile_id) {
|
|
|
|
|
let mut dirty = false;
|
|
|
|
|
if stored.server_instance_id.is_none()
|
|
|
|
|
&& let Some(server_instance_id) = snapshot.server_instance_id.clone()
|
|
|
|
|
{
|
|
|
|
|
stored.server_instance_id = Some(server_instance_id);
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if let Some(session_id) = snapshot.active_session_id.clone()
|
|
|
|
|
&& stored.sticky_session_id.as_deref() != Some(session_id.as_str())
|
|
|
|
|
{
|
|
|
|
|
stored.sticky_session_id = Some(session_id);
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if dirty {
|
|
|
|
|
let _ = database.update_profile(&stored);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(snapshot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn disconnect_connection_slot(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: DisconnectConnectionSlotRequest,
|
|
|
|
|
) -> Result<(), RuntimeCommandError> {
|
|
|
|
|
runtime.deactivate_slot(request.slot).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2026-07-20 12:08:06 +08:00
|
|
|
pub async fn cancel_connection_slot_message(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: DisconnectConnectionSlotRequest,
|
|
|
|
|
) -> Result<(), RuntimeCommandError> {
|
|
|
|
|
runtime.cancel_slot_message(request.slot).await
|
|
|
|
|
}
|
2026-07-20 14:32:07 +08:00
|
|
|
#[derive(Debug, serde::Serialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct ChatRemoteSession {
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
pub display_name: Option<String>,
|
|
|
|
|
pub updated_at: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
|
pub struct SwitchChatSessionRequest {
|
|
|
|
|
pub slot: SlotKind,
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn connection_slot_sessions(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: SlotCapabilitiesRequest,
|
|
|
|
|
) -> Result<Vec<ChatRemoteSession>, RuntimeCommandError> {
|
|
|
|
|
let sessions = runtime.slot_sessions(request.slot).await?;
|
|
|
|
|
Ok(sessions
|
|
|
|
|
.sessions
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|session| ChatRemoteSession {
|
|
|
|
|
session_id: session.session_id,
|
|
|
|
|
display_name: session.display_name,
|
|
|
|
|
updated_at: session.updated_at,
|
|
|
|
|
})
|
|
|
|
|
.collect())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn switch_connection_slot_session(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: SwitchChatSessionRequest,
|
|
|
|
|
) -> Result<SlotRuntimeSnapshot, RuntimeCommandError> {
|
|
|
|
|
runtime
|
|
|
|
|
.switch_slot_session(request.slot, request.session_id)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn connection_slot_capabilities(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: SlotCapabilitiesRequest,
|
|
|
|
|
) -> Result<desktop_protocol::AstrBotCapabilitySnapshot, RuntimeCommandError> {
|
|
|
|
|
runtime.slot_capabilities(request.slot).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn upload_connection_slot_file(
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: SlotFileUploadRequest,
|
|
|
|
|
) -> Result<desktop_protocol::AstrBotAttachment, RuntimeCommandError> {
|
|
|
|
|
if request.bytes.len() > 16 * 1024 * 1024 {
|
|
|
|
|
return Err(RuntimeCommandError::validation(
|
|
|
|
|
"Attachment exceeds the 16 MiB limit.",
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
runtime
|
|
|
|
|
.upload_slot_file(
|
|
|
|
|
request.slot,
|
|
|
|
|
request.file_name,
|
|
|
|
|
request.mime_type,
|
|
|
|
|
request.bytes,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
#[tauri::command]
|
2026-07-20 11:31:41 +08:00
|
|
|
pub async fn send_connection_slot_message(
|
|
|
|
|
database: State<'_, ApplicationDatabase>,
|
|
|
|
|
runtime: State<'_, DualSlotRuntime>,
|
|
|
|
|
request: SendConnectionSlotMessageRequest,
|
|
|
|
|
) -> Result<SlotRuntimeSnapshot, RuntimeCommandError> {
|
|
|
|
|
let status = runtime.status().await;
|
|
|
|
|
let snapshot = match request.slot {
|
|
|
|
|
SlotKind::Companion => status.companion,
|
|
|
|
|
SlotKind::Operations => status.operations,
|
|
|
|
|
};
|
|
|
|
|
if let (Some(profile_id), Some(server_instance_id), Some(session_id)) = (
|
|
|
|
|
snapshot.profile_id.as_deref(),
|
|
|
|
|
snapshot.server_instance_id.as_deref(),
|
|
|
|
|
snapshot.active_session_id.as_deref(),
|
|
|
|
|
) {
|
|
|
|
|
let slot = match request.slot {
|
|
|
|
|
SlotKind::Companion => "companion",
|
|
|
|
|
SlotKind::Operations => "operations",
|
|
|
|
|
};
|
|
|
|
|
let _ = database.upsert_chat_message(
|
|
|
|
|
"local-default",
|
|
|
|
|
profile_id,
|
|
|
|
|
slot,
|
|
|
|
|
server_instance_id,
|
|
|
|
|
None,
|
|
|
|
|
session_id,
|
|
|
|
|
&format!("local-{}", uuid::Uuid::new_v4()),
|
|
|
|
|
"user",
|
|
|
|
|
&request.message,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
runtime
|
2026-07-20 13:30:29 +08:00
|
|
|
.send_slot_message(
|
|
|
|
|
request.slot,
|
|
|
|
|
request.message,
|
|
|
|
|
request.config_id,
|
|
|
|
|
request.attachments,
|
|
|
|
|
)
|
2026-07-20 11:31:41 +08:00
|
|
|
.await
|
|
|
|
|
}
|