Creates a new AI with the given ID, or generates a new UUID if none is provided. # Errors - [`PrincipalAlreadyExists`] if an AI with the given ID already exists - [`StoreError`] if a database error occurs [`PrincipalAlreadyExists`]: PrincipalError::PrincipalAlreadyExists [`StoreError`]: PrincipalError::StoreError
(
&mut self,
id: Option<Uuid>,
identifier: &str,
)
| 313 | /// [`PrincipalAlreadyExists`]: PrincipalError::PrincipalAlreadyExists |
| 314 | /// [`StoreError`]: PrincipalError::StoreError |
| 315 | pub async fn create_ai( |
| 316 | &mut self, |
| 317 | id: Option<Uuid>, |
| 318 | identifier: &str, |
| 319 | ) -> Result<AiId, Report<PrincipalError>> { |
| 320 | let ai_id = AiId::new(id.unwrap_or_else(Uuid::new_v4)); |
| 321 | if let Err(error) = self |
| 322 | .as_mut_client() |
| 323 | .execute( |
| 324 | "INSERT INTO ai_actor (id, identifier) VALUES ($1, $2)", |
| 325 | &[&ai_id, &identifier], |
| 326 | ) |
| 327 | .instrument(tracing::info_span!( |
| 328 | "INSERT", |
| 329 | otel.kind = "client", |
| 330 | db.system = "postgresql", |
| 331 | peer.service = "Postgres", |
| 332 | )) |
| 333 | .await |
| 334 | { |
| 335 | return if error.code() == Some(&SqlState::UNIQUE_VIOLATION) { |
| 336 | Err(error).change_context(PrincipalError::PrincipalAlreadyExists { |
| 337 | id: PrincipalId::Actor(ActorId::Ai(ai_id)), |
| 338 | }) |
| 339 | } else { |
| 340 | Err(error).change_context(PrincipalError::StoreError) |
| 341 | }; |
| 342 | } |
| 343 | |
| 344 | Ok(ai_id) |
| 345 | } |
| 346 | |
| 347 | /// Checks if an AI with the given ID exists. |
| 348 | /// |