280 行
8.7 KiB
Rust
280 行
8.7 KiB
Rust
|
|
use desktop_protocol::{ActiveSlotKind, ConnectionProfile};
|
||
|
|
use serde::Serialize;
|
||
|
|
|
||
|
|
use crate::database::{ApplicationDatabase, DatabaseError};
|
||
|
|
|
||
|
|
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
|
||
|
|
#[serde(rename_all = "camelCase")]
|
||
|
|
pub struct DuplicateServerInstanceWarning {
|
||
|
|
pub code: &'static str,
|
||
|
|
pub server_instance_id: String,
|
||
|
|
pub profile_ids: Vec<String>,
|
||
|
|
pub display_names: Vec<String>,
|
||
|
|
pub message: &'static str,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
|
||
|
|
#[serde(rename_all = "camelCase")]
|
||
|
|
pub struct ConnectionPolicyStatus {
|
||
|
|
pub operations_switch_locked: bool,
|
||
|
|
pub active_operations_task_count: u32,
|
||
|
|
pub duplicate_server_instance_warnings: Vec<DuplicateServerInstanceWarning>,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn duplicate_server_instance_warnings(
|
||
|
|
profiles: &[ConnectionProfile],
|
||
|
|
) -> Vec<DuplicateServerInstanceWarning> {
|
||
|
|
let mut groups: Vec<(String, Vec<&ConnectionProfile>)> = Vec::new();
|
||
|
|
|
||
|
|
for profile in profiles {
|
||
|
|
let Some(server_instance_id) = profile
|
||
|
|
.server_instance_id
|
||
|
|
.as_deref()
|
||
|
|
.map(str::trim)
|
||
|
|
.filter(|value| !value.is_empty())
|
||
|
|
else {
|
||
|
|
continue;
|
||
|
|
};
|
||
|
|
|
||
|
|
if let Some((_, members)) = groups
|
||
|
|
.iter_mut()
|
||
|
|
.find(|(existing, _)| existing == server_instance_id)
|
||
|
|
{
|
||
|
|
members.push(profile);
|
||
|
|
} else {
|
||
|
|
groups.push((server_instance_id.to_owned(), vec![profile]));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
groups
|
||
|
|
.into_iter()
|
||
|
|
.filter(|(_, members)| members.len() > 1)
|
||
|
|
.map(|(server_instance_id, members)| {
|
||
|
|
let mut ordered = members;
|
||
|
|
ordered.sort_by(|left, right| {
|
||
|
|
left.display_name
|
||
|
|
.cmp(&right.display_name)
|
||
|
|
.then_with(|| left.id.cmp(&right.id))
|
||
|
|
});
|
||
|
|
DuplicateServerInstanceWarning {
|
||
|
|
code: "duplicateServerInstance",
|
||
|
|
server_instance_id,
|
||
|
|
profile_ids: ordered.iter().map(|profile| profile.id.clone()).collect(),
|
||
|
|
display_names: ordered
|
||
|
|
.iter()
|
||
|
|
.map(|profile| profile.display_name.clone())
|
||
|
|
.collect(),
|
||
|
|
message: "Multiple saved profiles point at the same server instance. Keep them separate and choose deliberately which profile to assign.",
|
||
|
|
}
|
||
|
|
})
|
||
|
|
.collect()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn list_duplicate_server_instance_warnings(
|
||
|
|
database: &ApplicationDatabase,
|
||
|
|
) -> Result<Vec<DuplicateServerInstanceWarning>, DatabaseError> {
|
||
|
|
let profiles = database.list_profiles()?;
|
||
|
|
Ok(duplicate_server_instance_warnings(&profiles))
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||
|
|
pub enum AutoConnectValidationError {
|
||
|
|
SlotRequired,
|
||
|
|
SlotForbidden,
|
||
|
|
TooManyAutoConnectProfiles,
|
||
|
|
DuplicateAutoConnectSlot { slot: ActiveSlotKind },
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AutoConnectValidationError {
|
||
|
|
pub fn message(self) -> &'static str {
|
||
|
|
match self {
|
||
|
|
Self::SlotRequired => {
|
||
|
|
"Automatic connection requires an explicit companion or operations slot."
|
||
|
|
}
|
||
|
|
Self::SlotForbidden => {
|
||
|
|
"A profile that is not set to auto-connect must not declare an auto-connect slot."
|
||
|
|
}
|
||
|
|
Self::TooManyAutoConnectProfiles => {
|
||
|
|
"At most two profiles may be configured for automatic connection."
|
||
|
|
}
|
||
|
|
Self::DuplicateAutoConnectSlot { .. } => {
|
||
|
|
"Two automatic-connection profiles cannot target the same slot."
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn validate_auto_connect_configuration(
|
||
|
|
profiles: &[ConnectionProfile],
|
||
|
|
profile_id: Option<&str>,
|
||
|
|
auto_connect: bool,
|
||
|
|
auto_connect_slot: Option<ActiveSlotKind>,
|
||
|
|
) -> Result<(), AutoConnectValidationError> {
|
||
|
|
if auto_connect {
|
||
|
|
let Some(slot) = auto_connect_slot else {
|
||
|
|
return Err(AutoConnectValidationError::SlotRequired);
|
||
|
|
};
|
||
|
|
|
||
|
|
let auto_connected = profiles
|
||
|
|
.iter()
|
||
|
|
.filter(|profile| profile_id != Some(profile.id.as_str()))
|
||
|
|
.filter(|profile| profile.auto_connect)
|
||
|
|
.collect::<Vec<_>>();
|
||
|
|
|
||
|
|
if auto_connected.len() >= 2 {
|
||
|
|
return Err(AutoConnectValidationError::TooManyAutoConnectProfiles);
|
||
|
|
}
|
||
|
|
|
||
|
|
if auto_connected
|
||
|
|
.iter()
|
||
|
|
.any(|profile| profile.auto_connect_slot == Some(slot))
|
||
|
|
{
|
||
|
|
return Err(AutoConnectValidationError::DuplicateAutoConnectSlot { slot });
|
||
|
|
}
|
||
|
|
} else if auto_connect_slot.is_some() {
|
||
|
|
return Err(AutoConnectValidationError::SlotForbidden);
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use desktop_protocol::{AstrBotProfileRole, AuthenticationType};
|
||
|
|
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
fn profile(
|
||
|
|
id: &str,
|
||
|
|
name: &str,
|
||
|
|
auto_connect: bool,
|
||
|
|
slot: Option<ActiveSlotKind>,
|
||
|
|
server_instance_id: Option<&str>,
|
||
|
|
) -> ConnectionProfile {
|
||
|
|
ConnectionProfile {
|
||
|
|
id: id.into(),
|
||
|
|
display_name: name.into(),
|
||
|
|
role: AstrBotProfileRole::General,
|
||
|
|
base_url: "https://example.test/".into(),
|
||
|
|
event_stream_url: None,
|
||
|
|
authentication_type: AuthenticationType::DeviceToken,
|
||
|
|
credential_reference: None,
|
||
|
|
allow_insecure_http: false,
|
||
|
|
pinned_certificate_fingerprint: None,
|
||
|
|
auto_connect_slot: slot,
|
||
|
|
auto_connect,
|
||
|
|
server_instance_id: server_instance_id.map(str::to_owned),
|
||
|
|
server_version: None,
|
||
|
|
protocol_version: None,
|
||
|
|
local_tool_policy_id: None,
|
||
|
|
last_connected_at: None,
|
||
|
|
created_at: "2026-07-19T00:00:00Z".into(),
|
||
|
|
updated_at: "2026-07-19T00:00:00Z".into(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn accepts_two_auto_connect_profiles_on_distinct_slots() {
|
||
|
|
let existing = vec![profile(
|
||
|
|
"00000000-0000-4000-8000-000000000001",
|
||
|
|
"Companion",
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Companion),
|
||
|
|
None,
|
||
|
|
)];
|
||
|
|
assert!(
|
||
|
|
validate_auto_connect_configuration(
|
||
|
|
&existing,
|
||
|
|
None,
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Operations),
|
||
|
|
)
|
||
|
|
.is_ok()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn rejects_a_third_auto_connect_profile_and_duplicate_slots() {
|
||
|
|
let existing = vec![
|
||
|
|
profile(
|
||
|
|
"00000000-0000-4000-8000-000000000001",
|
||
|
|
"Companion",
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Companion),
|
||
|
|
None,
|
||
|
|
),
|
||
|
|
profile(
|
||
|
|
"00000000-0000-4000-8000-000000000002",
|
||
|
|
"Operations",
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Operations),
|
||
|
|
None,
|
||
|
|
),
|
||
|
|
];
|
||
|
|
assert!(matches!(
|
||
|
|
validate_auto_connect_configuration(
|
||
|
|
&existing,
|
||
|
|
None,
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Companion),
|
||
|
|
),
|
||
|
|
Err(AutoConnectValidationError::TooManyAutoConnectProfiles)
|
||
|
|
));
|
||
|
|
|
||
|
|
let one = vec![profile(
|
||
|
|
"00000000-0000-4000-8000-000000000001",
|
||
|
|
"Companion",
|
||
|
|
true,
|
||
|
|
Some(ActiveSlotKind::Companion),
|
||
|
|
None,
|
||
|
|
)];
|
||
|
|
assert!(matches!(
|
||
|
|
validate_auto_connect_configuration(&one, None, true, Some(ActiveSlotKind::Companion),),
|
||
|
|
Err(AutoConnectValidationError::DuplicateAutoConnectSlot {
|
||
|
|
slot: ActiveSlotKind::Companion
|
||
|
|
})
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn reports_duplicate_server_instance_ids_without_merging_profiles() {
|
||
|
|
let profiles = vec![
|
||
|
|
profile(
|
||
|
|
"00000000-0000-4000-8000-000000000002",
|
||
|
|
"Beta",
|
||
|
|
false,
|
||
|
|
None,
|
||
|
|
Some("server-shared"),
|
||
|
|
),
|
||
|
|
profile(
|
||
|
|
"00000000-0000-4000-8000-000000000001",
|
||
|
|
"Alpha",
|
||
|
|
false,
|
||
|
|
None,
|
||
|
|
Some("server-shared"),
|
||
|
|
),
|
||
|
|
profile(
|
||
|
|
"00000000-0000-4000-8000-000000000003",
|
||
|
|
"Gamma",
|
||
|
|
false,
|
||
|
|
None,
|
||
|
|
Some("server-unique"),
|
||
|
|
),
|
||
|
|
];
|
||
|
|
let warnings = duplicate_server_instance_warnings(&profiles);
|
||
|
|
assert_eq!(warnings.len(), 1);
|
||
|
|
assert_eq!(warnings[0].server_instance_id, "server-shared");
|
||
|
|
assert_eq!(
|
||
|
|
warnings[0].profile_ids,
|
||
|
|
vec![
|
||
|
|
"00000000-0000-4000-8000-000000000001".to_owned(),
|
||
|
|
"00000000-0000-4000-8000-000000000002".to_owned(),
|
||
|
|
]
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
warnings[0].display_names,
|
||
|
|
vec!["Alpha".to_owned(), "Beta".to_owned()]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|