2026-07-20 11:31:41 +08:00
|
|
|
use desktop_protocol::{ActiveSlotKind, AstrBotProfileRole, AuthenticationType, ConnectionProfile};
|
|
|
|
|
use rusqlite::{Row, params};
|
|
|
|
|
|
|
|
|
|
use super::{ApplicationDatabase, DatabaseError, ProfileMessageStore};
|
|
|
|
|
|
|
|
|
|
const PROFILE_COLUMNS: &str = "
|
|
|
|
|
id,
|
|
|
|
|
display_name,
|
|
|
|
|
role,
|
|
|
|
|
base_url,
|
|
|
|
|
event_stream_url,
|
|
|
|
|
authentication_type,
|
|
|
|
|
transport_kind,
|
|
|
|
|
credential_reference,
|
|
|
|
|
allow_insecure_http,
|
|
|
|
|
pinned_certificate_fingerprint,
|
|
|
|
|
auto_connect_slot,
|
|
|
|
|
auto_connect,
|
|
|
|
|
server_instance_id,
|
|
|
|
|
server_version,
|
|
|
|
|
protocol_version,
|
|
|
|
|
local_tool_policy_id,
|
|
|
|
|
last_connected_at,
|
|
|
|
|
remote_username,
|
|
|
|
|
sticky_session_id,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
impl ApplicationDatabase {
|
|
|
|
|
pub fn list_profiles(&self) -> Result<Vec<ConnectionProfile>, DatabaseError> {
|
|
|
|
|
self.store
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| DatabaseError::LockPoisoned)?
|
|
|
|
|
.list_profiles()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn profile(&self, id: &str) -> Result<Option<ConnectionProfile>, DatabaseError> {
|
|
|
|
|
self.store
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| DatabaseError::LockPoisoned)?
|
|
|
|
|
.profile(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_profile(&self, profile: &ConnectionProfile) -> Result<(), DatabaseError> {
|
|
|
|
|
self.store
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| DatabaseError::LockPoisoned)?
|
|
|
|
|
.create_profile(profile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_profile(&self, profile: &ConnectionProfile) -> Result<(), DatabaseError> {
|
|
|
|
|
self.store
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| DatabaseError::LockPoisoned)?
|
|
|
|
|
.update_profile(profile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete_profile(&self, id: &str) -> Result<(), DatabaseError> {
|
|
|
|
|
self.store
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| DatabaseError::LockPoisoned)?
|
|
|
|
|
.delete_profile(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProfileMessageStore {
|
|
|
|
|
fn list_profiles(&self) -> Result<Vec<ConnectionProfile>, DatabaseError> {
|
|
|
|
|
let mut statement = self.connection.prepare(&format!(
|
|
|
|
|
"SELECT {PROFILE_COLUMNS}
|
|
|
|
|
FROM connection_profiles
|
|
|
|
|
ORDER BY display_name COLLATE NOCASE, id"
|
|
|
|
|
))?;
|
|
|
|
|
let mut rows = statement.query([])?;
|
|
|
|
|
let mut profiles = Vec::new();
|
|
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
|
profiles.push(profile_from_row(row)?);
|
|
|
|
|
}
|
|
|
|
|
Ok(profiles)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn profile(&self, id: &str) -> Result<Option<ConnectionProfile>, DatabaseError> {
|
|
|
|
|
let mut statement = self.connection.prepare(&format!(
|
|
|
|
|
"SELECT {PROFILE_COLUMNS}
|
|
|
|
|
FROM connection_profiles
|
|
|
|
|
WHERE id = ?1"
|
|
|
|
|
))?;
|
|
|
|
|
let mut rows = statement.query(params![id])?;
|
|
|
|
|
match rows.next()? {
|
|
|
|
|
Some(row) => Ok(Some(profile_from_row(row)?)),
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_profile(&self, profile: &ConnectionProfile) -> Result<(), DatabaseError> {
|
|
|
|
|
self.connection.execute(
|
|
|
|
|
"
|
|
|
|
|
INSERT INTO connection_profiles (
|
|
|
|
|
id,
|
|
|
|
|
display_name,
|
|
|
|
|
role,
|
|
|
|
|
base_url,
|
|
|
|
|
event_stream_url,
|
|
|
|
|
authentication_type,
|
|
|
|
|
transport_kind,
|
|
|
|
|
credential_reference,
|
|
|
|
|
allow_insecure_http,
|
|
|
|
|
pinned_certificate_fingerprint,
|
|
|
|
|
auto_connect_slot,
|
|
|
|
|
auto_connect,
|
|
|
|
|
server_instance_id,
|
|
|
|
|
server_version,
|
|
|
|
|
protocol_version,
|
|
|
|
|
local_tool_policy_id,
|
|
|
|
|
last_connected_at,
|
|
|
|
|
remote_username,
|
|
|
|
|
sticky_session_id,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at
|
|
|
|
|
) VALUES (
|
|
|
|
|
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9,
|
|
|
|
|
?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18,
|
|
|
|
|
?19, ?20, ?21
|
|
|
|
|
)
|
|
|
|
|
",
|
|
|
|
|
params![
|
|
|
|
|
profile.id,
|
|
|
|
|
profile.display_name,
|
|
|
|
|
role_to_str(profile.role),
|
|
|
|
|
profile.base_url,
|
|
|
|
|
profile.event_stream_url,
|
|
|
|
|
authentication_to_str(profile.authentication_type),
|
|
|
|
|
transport_to_str(profile.transport_kind),
|
|
|
|
|
profile.credential_reference,
|
|
|
|
|
profile.allow_insecure_http,
|
|
|
|
|
profile.pinned_certificate_fingerprint,
|
|
|
|
|
profile.auto_connect_slot.map(slot_to_str),
|
|
|
|
|
profile.auto_connect,
|
|
|
|
|
profile.server_instance_id,
|
|
|
|
|
profile.server_version,
|
|
|
|
|
profile.protocol_version,
|
|
|
|
|
profile.local_tool_policy_id,
|
|
|
|
|
profile.last_connected_at,
|
|
|
|
|
profile.remote_username,
|
|
|
|
|
profile.sticky_session_id,
|
|
|
|
|
profile.created_at,
|
|
|
|
|
profile.updated_at,
|
|
|
|
|
],
|
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_profile(&self, profile: &ConnectionProfile) -> Result<(), DatabaseError> {
|
|
|
|
|
let changed = self.connection.execute(
|
|
|
|
|
"
|
|
|
|
|
UPDATE connection_profiles
|
|
|
|
|
SET display_name = ?2,
|
|
|
|
|
role = ?3,
|
|
|
|
|
base_url = ?4,
|
|
|
|
|
event_stream_url = ?5,
|
|
|
|
|
authentication_type = ?6,
|
|
|
|
|
transport_kind = ?7,
|
|
|
|
|
credential_reference = ?8,
|
|
|
|
|
allow_insecure_http = ?9,
|
|
|
|
|
pinned_certificate_fingerprint = ?10,
|
|
|
|
|
auto_connect_slot = ?11,
|
|
|
|
|
auto_connect = ?12,
|
|
|
|
|
server_instance_id = ?13,
|
|
|
|
|
server_version = ?14,
|
|
|
|
|
protocol_version = ?15,
|
|
|
|
|
local_tool_policy_id = ?16,
|
|
|
|
|
last_connected_at = ?17,
|
|
|
|
|
remote_username = ?18,
|
|
|
|
|
sticky_session_id = ?19,
|
|
|
|
|
created_at = ?20,
|
|
|
|
|
updated_at = ?21
|
|
|
|
|
WHERE id = ?1
|
|
|
|
|
",
|
|
|
|
|
params![
|
|
|
|
|
profile.id,
|
|
|
|
|
profile.display_name,
|
|
|
|
|
role_to_str(profile.role),
|
|
|
|
|
profile.base_url,
|
|
|
|
|
profile.event_stream_url,
|
|
|
|
|
authentication_to_str(profile.authentication_type),
|
|
|
|
|
transport_to_str(profile.transport_kind),
|
|
|
|
|
profile.credential_reference,
|
|
|
|
|
profile.allow_insecure_http,
|
|
|
|
|
profile.pinned_certificate_fingerprint,
|
|
|
|
|
profile.auto_connect_slot.map(slot_to_str),
|
|
|
|
|
profile.auto_connect,
|
|
|
|
|
profile.server_instance_id,
|
|
|
|
|
profile.server_version,
|
|
|
|
|
profile.protocol_version,
|
|
|
|
|
profile.local_tool_policy_id,
|
|
|
|
|
profile.last_connected_at,
|
|
|
|
|
profile.remote_username,
|
|
|
|
|
profile.sticky_session_id,
|
|
|
|
|
profile.created_at,
|
|
|
|
|
profile.updated_at,
|
|
|
|
|
],
|
|
|
|
|
)?;
|
|
|
|
|
if changed == 0 {
|
|
|
|
|
return Err(DatabaseError::ProfileNotFound);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn delete_profile(&self, id: &str) -> Result<(), DatabaseError> {
|
|
|
|
|
if self
|
|
|
|
|
.connection
|
|
|
|
|
.execute("DELETE FROM connection_profiles WHERE id = ?1", params![id])?
|
|
|
|
|
== 0
|
|
|
|
|
{
|
|
|
|
|
return Err(DatabaseError::ProfileNotFound);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn profile_from_row(row: &Row<'_>) -> Result<ConnectionProfile, DatabaseError> {
|
|
|
|
|
let role = match row.get::<_, String>(2)?.as_str() {
|
|
|
|
|
"companion" => AstrBotProfileRole::Companion,
|
|
|
|
|
"operations" => AstrBotProfileRole::Operations,
|
|
|
|
|
"general" => AstrBotProfileRole::General,
|
|
|
|
|
_ => return Err(DatabaseError::InvalidStoredProfile),
|
|
|
|
|
};
|
|
|
|
|
let authentication_type = match row.get::<_, String>(5)?.as_str() {
|
|
|
|
|
"device-token" => AuthenticationType::DeviceToken,
|
2026-07-20 15:55:36 +08:00
|
|
|
"api-key" => AuthenticationType::ApiKey,
|
|
|
|
|
"jwt" => AuthenticationType::Jwt,
|
2026-07-20 11:31:41 +08:00
|
|
|
_ => return Err(DatabaseError::InvalidStoredProfile),
|
|
|
|
|
};
|
|
|
|
|
let transport_kind = match row.get::<_, String>(6)?.as_str() {
|
|
|
|
|
"desktop-v1" => desktop_protocol::TransportKind::DesktopV1,
|
|
|
|
|
"astrbot-openapi" => desktop_protocol::TransportKind::AstrBotOpenApi,
|
|
|
|
|
_ => return Err(DatabaseError::InvalidStoredProfile),
|
|
|
|
|
};
|
|
|
|
|
let auto_connect_slot = match row.get::<_, Option<String>>(10)? {
|
|
|
|
|
Some(value) => Some(match value.as_str() {
|
|
|
|
|
"companion" => ActiveSlotKind::Companion,
|
|
|
|
|
"operations" => ActiveSlotKind::Operations,
|
|
|
|
|
_ => return Err(DatabaseError::InvalidStoredProfile),
|
|
|
|
|
}),
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
Ok(ConnectionProfile {
|
|
|
|
|
id: row.get(0)?,
|
|
|
|
|
display_name: row.get(1)?,
|
|
|
|
|
role,
|
|
|
|
|
base_url: row.get(3)?,
|
|
|
|
|
event_stream_url: row.get(4)?,
|
|
|
|
|
authentication_type,
|
|
|
|
|
transport_kind,
|
|
|
|
|
credential_reference: row.get(7)?,
|
|
|
|
|
allow_insecure_http: row.get::<_, i64>(8)? != 0,
|
|
|
|
|
pinned_certificate_fingerprint: row.get(9)?,
|
|
|
|
|
auto_connect_slot,
|
|
|
|
|
auto_connect: row.get::<_, i64>(11)? != 0,
|
|
|
|
|
server_instance_id: row.get(12)?,
|
|
|
|
|
server_version: row.get(13)?,
|
|
|
|
|
protocol_version: row.get(14)?,
|
|
|
|
|
local_tool_policy_id: row.get(15)?,
|
|
|
|
|
last_connected_at: row.get(16)?,
|
|
|
|
|
remote_username: row.get(17)?,
|
|
|
|
|
sticky_session_id: row.get(18)?,
|
|
|
|
|
created_at: row.get(19)?,
|
|
|
|
|
updated_at: row.get(20)?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn role_to_str(role: AstrBotProfileRole) -> &'static str {
|
|
|
|
|
match role {
|
|
|
|
|
AstrBotProfileRole::Companion => "companion",
|
|
|
|
|
AstrBotProfileRole::Operations => "operations",
|
|
|
|
|
AstrBotProfileRole::General => "general",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn authentication_to_str(authentication_type: AuthenticationType) -> &'static str {
|
|
|
|
|
match authentication_type {
|
|
|
|
|
AuthenticationType::DeviceToken => "device-token",
|
2026-07-20 15:55:36 +08:00
|
|
|
AuthenticationType::ApiKey => "api-key",
|
|
|
|
|
AuthenticationType::Jwt => "jwt",
|
2026-07-20 11:31:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn transport_to_str(transport_kind: desktop_protocol::TransportKind) -> &'static str {
|
|
|
|
|
transport_kind.as_str()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn slot_to_str(slot: ActiveSlotKind) -> &'static str {
|
|
|
|
|
match slot {
|
|
|
|
|
ActiveSlotKind::Companion => "companion",
|
|
|
|
|
ActiveSlotKind::Operations => "operations",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
fn profile(index: usize) -> ConnectionProfile {
|
|
|
|
|
ConnectionProfile {
|
|
|
|
|
id: format!("00000000-0000-4000-8000-{index:012}"),
|
|
|
|
|
display_name: format!("Profile {index}"),
|
|
|
|
|
role: AstrBotProfileRole::General,
|
|
|
|
|
base_url: format!("https://example-{index}.test/"),
|
|
|
|
|
event_stream_url: None,
|
|
|
|
|
authentication_type: AuthenticationType::DeviceToken,
|
|
|
|
|
transport_kind: desktop_protocol::TransportKind::DesktopV1,
|
|
|
|
|
credential_reference: None,
|
|
|
|
|
allow_insecure_http: false,
|
|
|
|
|
pinned_certificate_fingerprint: None,
|
|
|
|
|
auto_connect_slot: None,
|
|
|
|
|
auto_connect: false,
|
|
|
|
|
server_instance_id: None,
|
|
|
|
|
server_version: None,
|
|
|
|
|
protocol_version: None,
|
|
|
|
|
local_tool_policy_id: None,
|
|
|
|
|
last_connected_at: None,
|
|
|
|
|
remote_username: None,
|
|
|
|
|
sticky_session_id: None,
|
|
|
|
|
created_at: "2026-07-18T00:00:00Z".into(),
|
|
|
|
|
updated_at: "2026-07-18T00:00:00Z".into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stores_lists_updates_and_deletes_five_profiles() {
|
|
|
|
|
let database = ApplicationDatabase::in_memory().expect("database");
|
|
|
|
|
for index in 0..5 {
|
|
|
|
|
database
|
|
|
|
|
.create_profile(&profile(index))
|
|
|
|
|
.expect("create profile");
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(database.list_profiles().expect("list profiles").len(), 5);
|
|
|
|
|
|
|
|
|
|
let mut updated = database
|
|
|
|
|
.profile(&profile(2).id)
|
|
|
|
|
.expect("read profile")
|
|
|
|
|
.expect("existing profile");
|
|
|
|
|
updated.display_name = "Updated Profile".into();
|
|
|
|
|
updated.updated_at = "2026-07-18T01:00:00Z".into();
|
|
|
|
|
database.update_profile(&updated).expect("update profile");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
database
|
|
|
|
|
.profile(&updated.id)
|
|
|
|
|
.expect("read updated profile")
|
|
|
|
|
.expect("existing profile")
|
|
|
|
|
.display_name,
|
|
|
|
|
"Updated Profile"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
database
|
|
|
|
|
.delete_profile(&updated.id)
|
|
|
|
|
.expect("delete profile");
|
|
|
|
|
assert_eq!(database.list_profiles().expect("list profiles").len(), 4);
|
|
|
|
|
assert!(matches!(
|
|
|
|
|
database.delete_profile(&updated.id),
|
|
|
|
|
Err(DatabaseError::ProfileNotFound)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn profile_metadata_persists_after_reopening_database() {
|
|
|
|
|
let directory = tempfile::tempdir().expect("temporary directory");
|
|
|
|
|
let path = directory.path().join("profiles.sqlite3");
|
|
|
|
|
let expected = profile(7);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let store = ProfileMessageStore::open(&path).expect("open database");
|
|
|
|
|
store.create_profile(&expected).expect("create profile");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let reopened = ProfileMessageStore::open(&path).expect("reopen database");
|
|
|
|
|
let actual = reopened
|
|
|
|
|
.profile(&expected.id)
|
|
|
|
|
.expect("read profile")
|
|
|
|
|
.expect("persisted profile");
|
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
|
}
|
|
|
|
|
}
|