use std::sync::Arc;
use aonyx_memory::{Palace, SessionStore};
use serde::Serialize;
use crate::agent::ApiAgent;
#[derive(Debug, Clone)]
pub struct AuthConfig {
pub token: Option<String>,
pub allow_destructive: bool,
}
impl AuthConfig {
pub fn new(token: Option<String>, allow_destructive: bool) -> Self {
Self {
token,
allow_destructive,
}
}
pub fn check(&self, auth_header: Option<&str>) -> bool {
match &self.token {
None => true,
Some(expected) => auth_header
.map(|h| h.strip_prefix("Bearer ").unwrap_or(h) == expected)
.unwrap_or(false),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ServerInfo {
pub name: &'static str,
pub version: &'static str,
pub provider: String,
pub model: String,
pub features: Vec<String>,
}
impl ServerInfo {
pub fn new(
provider: impl Into<String>,
model: impl Into<String>,
features: Vec<String>,
) -> Self {
Self {
name: "aonyx-agent",
version: env!("CARGO_PKG_VERSION"),
provider: provider.into(),
model: model.into(),
features,
}
}
}
#[derive(Clone)]
pub struct ApiState {
pub auth: Arc<AuthConfig>,
pub info: Arc<ServerInfo>,
pub sessions: Arc<dyn SessionStore>,
pub palace: Arc<Palace>,
pub agent: Arc<dyn ApiAgent>,
pub default_project: Arc<String>,
}
impl ApiState {
pub fn new(
auth: AuthConfig,
info: ServerInfo,
sessions: Arc<dyn SessionStore>,
palace: Arc<Palace>,
agent: Arc<dyn ApiAgent>,
default_project: impl Into<String>,
) -> Self {
Self {
auth: Arc::new(auth),
info: Arc::new(info),
sessions,
palace,
agent,
default_project: Arc::new(default_project.into()),
}
}
pub(crate) fn project_or_default(&self, project: Option<String>) -> String {
project
.filter(|s| !s.is_empty())
.unwrap_or_else(|| self.default_project.as_ref().clone())
}
}