use ai_registry_api::{
consts::{AGENT, FLAG_ACTIVE},
error::AgentError,
event::AgentClosedEvent,
state::Agent,
};
use steel::*;
pub fn process_close_agent(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
let [controller_info, agent_info, recipient_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
agent_info.is_writable()?.has_seeds(
&[AGENT, agent_info.as_account::<Agent>(&ai_registry_api::ID)?.creator.as_ref()],
&ai_registry_api::ID,
)?;
controller_info.is_signer()?;
recipient_info.is_writable()?;
system_program.is_program(&system_program::ID)?;
let agent = agent_info.as_account::<Agent>(&ai_registry_api::ID)?;
if controller_info.key != &agent.controller {
return Err(AgentError::UnauthorizedController.into());
}
if agent.state_flags & FLAG_ACTIVE != 0 {
return Err(AgentError::AgentActive.into());
}
let clock = Clock::get()?;
let creator = agent.creator;
let agent_key = *agent_info.key;
AgentClosedEvent {
creator,
agent: agent_key,
timestamp: clock.unix_timestamp,
}
.log();
close_account(agent_info, recipient_info)?;
Ok(())
}