use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use crate::transport::api_error::ApiError;
use crate::{ActivitySource, AppState};
use super::{to_value, CallerParams, NoParams, PalaceParams};
pub use crate::service::StatusPayload;
pub async fn status(state: &AppState, _params: NoParams) -> Result<Value, ApiError> {
to_value(
crate::service::MemoryService::new(state.clone())
.status()
.await,
)
}
#[derive(Debug, Serialize)]
pub struct ConfigPayload {
pub openrouter_configured: bool,
pub model: String,
pub data_root: String,
}
pub async fn config(state: &AppState, _params: NoParams) -> Result<Value, ApiError> {
let cfg = crate::service::load_user_config().unwrap_or_default();
to_value(ConfigPayload {
openrouter_configured: !cfg.openrouter_api_key.is_empty(),
model: cfg.openrouter_model,
data_root: state.data_root.display().to_string(),
})
}
pub async fn get_palace(state: &AppState, params: PalaceParams) -> Result<Value, ApiError> {
to_value(
crate::service::MemoryService::new(state.clone())
.get_palace(¶ms.palace_id)
.await?,
)
}
#[derive(Debug, Serialize)]
pub struct PalaceListRow {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub palace: Option<crate::service::PalaceInfo>,
pub error: Option<String>,
}
pub async fn palaces_list(state: &AppState, _params: NoParams) -> Result<Value, ApiError> {
let rows = crate::service::MemoryService::new(state.clone())
.list_palaces_with_counts()
.await?;
let rows: Vec<PalaceListRow> = rows
.into_iter()
.map(|(id, result)| match result {
Ok(info) => PalaceListRow {
id,
palace: Some(info),
error: None,
},
Err(error) => PalaceListRow {
id,
palace: None,
error: Some(error),
},
})
.collect();
to_value(json!({ "palaces": rows }))
}
#[derive(Debug, Deserialize)]
pub struct ListDrawersParams {
pub palace_id: String,
#[serde(flatten)]
pub query: crate::service::ListDrawersQuery,
}
pub async fn list_drawers(state: &AppState, params: ListDrawersParams) -> Result<Value, ApiError> {
Ok(crate::service::MemoryService::new(state.clone())
.list_drawers(¶ms.palace_id, params.query)
.await?)
}
#[derive(Debug, Deserialize)]
pub struct CreateDrawerParams {
pub palace_id: String,
#[serde(flatten)]
pub body: crate::service::CreateDrawerBody,
#[serde(flatten)]
pub caller: CallerParams,
}
pub async fn create_drawer(
state: &AppState,
params: CreateDrawerParams,
) -> Result<Value, ApiError> {
let creator = params.caller.creator();
let drawer_id = crate::service::MemoryService::new(state.clone())
.create_drawer(
¶ms.palace_id,
params.body,
creator,
ActivitySource::Http,
)
.await?;
Ok(json!({ "id": drawer_id }))
}
#[derive(Debug, Deserialize)]
pub struct DeleteDrawerParams {
pub palace_id: String,
pub drawer_id: String,
}
pub async fn delete_drawer(
state: &AppState,
params: DeleteDrawerParams,
) -> Result<Value, ApiError> {
crate::service::MemoryService::new(state.clone())
.delete_drawer(¶ms.palace_id, ¶ms.drawer_id, ActivitySource::Http)
.await?;
Ok(json!({ "deleted": true }))
}