Skip to main content

systemprompt_agent/repository/task/constructor/
mod.rs

1//! Reassembly of persisted A2A [`Task`] aggregates from their relational rows.
2//!
3//! [`TaskConstructor`] fans out across the task, message, message-part,
4//! artifact, and execution-step tables and rebuilds the nested [`Task`] graph,
5//! offering both a single-task path and a batched path that amortises the
6//! per-table round trips across many task ids.
7
8mod batch;
9mod batch_builders;
10pub(in crate::repository) mod batch_queries;
11mod converters;
12mod single;
13
14use crate::models::a2a::Task;
15use crate::repository::content::ArtifactRepository;
16use sqlx::PgPool;
17use std::sync::Arc;
18use systemprompt_database::DbPool;
19use systemprompt_identifiers::TaskId;
20use systemprompt_traits::RepositoryError;
21
22#[derive(Debug, Clone)]
23pub struct TaskConstructor {
24    pool: Arc<PgPool>,
25    db_pool: DbPool,
26    artifact_repo: ArtifactRepository,
27}
28
29impl TaskConstructor {
30    pub fn new(db: &DbPool) -> Result<Self, crate::error::AgentError> {
31        let pool = db
32            .pool_arc()
33            .map_err(|e| crate::error::AgentError::Init(e.to_string()))?;
34        let artifact_repo = ArtifactRepository::new(db)?;
35        Ok(Self {
36            pool,
37            db_pool: Arc::clone(db),
38            artifact_repo,
39        })
40    }
41
42    pub(crate) const fn pool(&self) -> &Arc<PgPool> {
43        &self.pool
44    }
45
46    pub(crate) const fn artifact_repo(&self) -> &ArtifactRepository {
47        &self.artifact_repo
48    }
49
50    pub(crate) const fn db_pool(&self) -> &DbPool {
51        &self.db_pool
52    }
53
54    pub async fn construct_task_from_task_id(
55        &self,
56        task_id: &TaskId,
57    ) -> Result<Task, RepositoryError> {
58        single::construct_task_from_task_id(self, task_id).await
59    }
60
61    pub async fn construct_tasks_batch(
62        &self,
63        task_ids: &[TaskId],
64    ) -> Result<Vec<Task>, RepositoryError> {
65        batch::construct_tasks_batch(self, task_ids).await
66    }
67}