use ai_registry_api::{
consts::{AGENT, FLAG_KNOWLEDGE_FINALIZED},
error::AgentError,
event::KnowledgeFinalizedEvent,
state::Agent,
};
use steel::*;
pub fn process_finalize_knowledge(
accounts: &[AccountInfo<'_>],
_data: &[u8],
) -> ProgramResult {
let [controller_info, agent_info] = 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()?;
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_KNOWLEDGE_FINALIZED != 0 {
return Err(AgentError::KnowledgeFinalized.into());
}
let clock = Clock::get()?;
let agent = agent_info.as_account_mut::<Agent>(&ai_registry_api::ID)?;
agent.state_flags |= FLAG_KNOWLEDGE_FINALIZED;
agent.finalized_at = clock.unix_timestamp;
agent.updated_at = clock.unix_timestamp;
KnowledgeFinalizedEvent {
creator: agent.creator,
agent: *agent_info.key,
timestamp: clock.unix_timestamp,
}
.log();
Ok(())
}