use serde::Deserialize;
use serde_json::{json, Value};
use crate::api_client::CxClient;
use crate::error::Result;
const AI_BASE: &str = "/mgmt/openapi/5/ai";
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ApplicationItems {
#[serde(default)]
ai_applications: Vec<Value>,
}
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct EvaluationItems {
#[serde(default)]
ai_evaluations: Vec<Value>,
}
pub fn eval_config_type(item: &Value) -> String {
item.get("config")
.and_then(Value::as_object)
.and_then(|m| m.keys().next())
.cloned()
.unwrap_or_else(|| "-".to_string())
}
#[derive(Debug, Default, Deserialize)]
struct CustomEvaluationItems {
#[serde(default)]
items: Vec<Value>,
}
pub struct AiCenterApi<'a> {
client: &'a CxClient,
}
impl<'a> AiCenterApi<'a> {
pub fn new(client: &'a CxClient) -> Self {
Self { client }
}
pub async fn list_applications(&self, params: &[(&str, &str)]) -> Result<Vec<Value>> {
let resp: ApplicationItems = self
.client
.get(&format!("{AI_BASE}/applications/v3"), params)
.await?;
Ok(resp.ai_applications)
}
pub async fn get_application(&self, id: &str) -> Result<Value> {
self.client
.get(&format!("{AI_BASE}/applications/v3/{id}"), &[])
.await
}
pub async fn list_evaluations(&self, params: &[(&str, &str)]) -> Result<Vec<Value>> {
let resp: EvaluationItems = self
.client
.get(&format!("{AI_BASE}/evaluations/v3"), params)
.await?;
Ok(resp.ai_evaluations)
}
pub async fn get_evaluation(&self, id: &str) -> Result<Value> {
self.client
.get(&format!("{AI_BASE}/evaluations/v3/{id}"), &[])
.await
}
pub async fn count_apps_per_eval_type(&self) -> Result<Value> {
self.client
.get(&format!("{AI_BASE}/evaluation-counts/v3/per-type"), &[])
.await
}
pub async fn list_custom_evaluations(&self) -> Result<Vec<Value>> {
let resp: CustomEvaluationItems = self
.client
.get(&format!("{AI_BASE}/custom-evaluations/v3"), &[])
.await?;
Ok(resp.items)
}
pub async fn list_custom_evaluations_for_application(
&self,
application_id: &str,
) -> Result<Vec<Value>> {
let resp: CustomEvaluationItems = self
.client
.get(
&format!("{AI_BASE}/custom-evaluations/v3/by-application/{application_id}"),
&[],
)
.await?;
Ok(resp.items)
}
pub async fn get_model_pricing(&self) -> Result<Value> {
self.client
.get(&format!("{AI_BASE}/model-pricing/v3"), &[])
.await
}
pub async fn create_evaluation(&self, body: &Value) -> Result<Value> {
self.client
.post(&format!("{AI_BASE}/evaluations/v3"), body)
.await
}
pub async fn update_evaluation(&self, id: &str, body: &Value) -> Result<Value> {
self.client
.patch(&format!("{AI_BASE}/evaluations/v3/{id}"), body)
.await
}
pub async fn delete_evaluation(&self, id: &str) -> Result<Value> {
self.client
.delete(&format!("{AI_BASE}/evaluations/v3/{id}"))
.await
}
pub async fn create_custom_evaluation(&self, body: &Value) -> Result<Value> {
self.client
.post(&format!("{AI_BASE}/custom-evaluations/v3"), body)
.await
}
pub async fn update_custom_evaluation(&self, id: &str, body: &Value) -> Result<Value> {
self.client
.patch(&format!("{AI_BASE}/custom-evaluations/v3/{id}"), body)
.await
}
pub async fn add_policy_to_application(
&self,
evaluation_id: &str,
application_id: &str,
) -> Result<Value> {
self.client
.post_empty(
&format!(
"{AI_BASE}/custom-evaluations/v3/{evaluation_id}/applications/{application_id}"
),
&[],
)
.await
}
pub async fn remove_policy_from_application(
&self,
evaluation_id: &str,
application_id: &str,
) -> Result<Value> {
self.client
.delete(&format!(
"{AI_BASE}/custom-evaluations/v3/{evaluation_id}/applications/{application_id}"
))
.await
}
pub async fn set_model_pricing(&self, prices: &Value) -> Result<Value> {
let body = json!({ "prices": prices });
self.client
.put(&format!("{AI_BASE}/model-pricing/v3"), &body)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn application_items_preserve_full_object() {
let body = json!({
"aiApplications": [
{ "id": "a-1", "application": "Production", "subsystem": "Advisor",
"guardrailsIntegrated": true, "companyId": "42", "createdAt": "2026-01-01" }
]
});
let resp: ApplicationItems = serde_json::from_value(body).unwrap();
assert_eq!(resp.ai_applications.len(), 1);
assert_eq!(resp.ai_applications[0]["id"], "a-1");
assert_eq!(resp.ai_applications[0]["guardrailsIntegrated"], true);
assert_eq!(resp.ai_applications[0]["companyId"], "42");
}
#[test]
fn empty_application_items() {
let resp: ApplicationItems = serde_json::from_value(json!({})).unwrap();
assert!(resp.ai_applications.is_empty());
}
#[test]
fn evaluation_items_and_config_type() {
let body = json!({
"aiEvaluations": [
{ "id": "e-1", "application": "Prod", "subsystem": "Sub", "isEnabled": true,
"target": "RESPONSE", "threshold": 0.8, "config": { "toxicity": {} } },
{ "id": "e-2", "application": "Prod", "subsystem": "Sub" }
]
});
let resp: EvaluationItems = serde_json::from_value(body).unwrap();
assert_eq!(resp.ai_evaluations.len(), 2);
assert_eq!(eval_config_type(&resp.ai_evaluations[0]), "toxicity");
assert_eq!(resp.ai_evaluations[0]["isEnabled"], true);
assert_eq!(eval_config_type(&resp.ai_evaluations[1]), "-");
}
#[test]
fn custom_evaluation_items_preserve_full_object() {
let body = json!({
"items": [
{ "id": "c-1", "name": "No PII", "description": "block pii",
"applicationIds": ["a-1", "a-2"],
"config": { "instructions": "reject PII", "policyType": "SECURITY" } }
]
});
let resp: CustomEvaluationItems = serde_json::from_value(body).unwrap();
assert_eq!(resp.items.len(), 1);
assert_eq!(resp.items[0]["name"], "No PII");
assert_eq!(resp.items[0]["config"]["policyType"], "SECURITY");
}
}