use serde_json::{Value, json};
pub fn create_minimal_agent(
agent_type: &str,
services: Option<Vec<Value>>,
contacts: Option<Vec<Value>>,
) -> Result<Value, String> {
let allowed_agent_types = ["human", "human-org", "hybrid", "ai"];
if !allowed_agent_types.contains(&agent_type) {
return Err(format!("Invalid agent type: {}", agent_type));
}
let services = services.ok_or_else(|| "Services are required".to_string())?;
if services.is_empty() {
return Err("At least one service is required".to_string());
}
let mut agent = json!({
"$schema": "https://hai.ai/schemas/agent/v1/agent.schema.json",
"jacsAgentType": agent_type,
"jacsType": "agent",
"jacsServices": services,
"jacsLevel": "config"
});
if let Some(contacts) = contacts {
agent["jacsContacts"] = json!(contacts);
}
Ok(agent)
}
#[allow(dead_code)]
fn add_service_to_agent(agent: &mut Value, service: Value) -> Result<(), String> {
agent["jacsServices"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?
.push(service);
Ok(())
}
#[allow(dead_code)]
fn update_service_in_agent(
agent: &mut Value,
old_service: Value,
new_service: Value,
) -> Result<(), String> {
let services = agent["jacsServices"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?;
let index = services
.iter()
.position(|s| s == &old_service)
.ok_or_else(|| "Service not found".to_string())?;
services[index] = new_service;
Ok(())
}
#[allow(dead_code)]
fn remove_service_from_agent(agent: &mut Value, service: Value) -> Result<(), String> {
let services = agent["jacsServices"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?;
let index = services
.iter()
.position(|s| s == &service)
.ok_or_else(|| "Service not found".to_string())?;
services.remove(index);
Ok(())
}
#[allow(dead_code)]
fn add_contact_to_agent(agent: &mut Value, contact: Value) -> Result<(), String> {
if agent.get("jacsContacts").is_none() {
agent["jacsContacts"] = json!([]);
}
agent["jacsContacts"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?
.push(contact);
Ok(())
}
#[allow(dead_code)]
fn update_contact_in_agent(
agent: &mut Value,
old_contact: Value,
new_contact: Value,
) -> Result<(), String> {
let contacts = agent["jacsContacts"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?;
let index = contacts
.iter()
.position(|c| c == &old_contact)
.ok_or_else(|| "Contact not found".to_string())?;
contacts[index] = new_contact;
Ok(())
}
#[allow(dead_code)]
fn remove_contact_from_agent(agent: &mut Value, contact: Value) -> Result<(), String> {
let contacts = agent["jacsContacts"]
.as_array_mut()
.ok_or_else(|| "Invalid agent format".to_string())?;
let index = contacts
.iter()
.position(|c| c == &contact)
.ok_or_else(|| "Contact not found".to_string())?;
contacts.remove(index);
Ok(())
}