use models::IssueId; use models::agent_task::{ActiveModel, AgentType, Entity, Model}; use sea_orm::{ ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect, }; impl super::TaskService { /// Get a task by ID. pub async fn get(&self, task_id: i64) -> Result, DbErr> { Entity::find_by_id(task_id).one(self.db()).await } /// List all tasks (root only) for a project. pub async fn list( &self, project_uuid: impl Into, limit: u64, ) -> Result, DbErr> { let uuid: uuid::Uuid = project_uuid.into(); Entity::find() .filter(models::agent_task::Column::ProjectUuid.eq(uuid)) .filter(models::agent_task::Column::ParentId.is_null()) .order_by_desc(models::agent_task::Column::CreatedAt) .limit(limit) .all(self.db()) .await } /// List all active (non-terminal) tasks for a project. pub async fn active_tasks( &self, project_uuid: impl Into, ) -> Result, DbErr> { let uuid: uuid::Uuid = project_uuid.into(); Entity::find() .filter(models::agent_task::Column::ProjectUuid.eq(uuid)) .filter(models::agent_task::Column::Status.is_in([ models::agent_task::TaskStatus::Pending, models::agent_task::TaskStatus::Running, models::agent_task::TaskStatus::Paused, ])) .order_by_desc(models::agent_task::Column::CreatedAt) .all(self.db()) .await } /// Create a new task (root or sub-task) with status = pending. pub async fn create( &self, project_uuid: impl Into, input: impl Into, agent_type: AgentType, ) -> Result { self.create_with_parent(project_uuid, None, input, agent_type, None, None) .await } /// Create a new task bound to an issue. pub async fn create_for_issue( &self, project_uuid: impl Into, issue_id: IssueId, input: impl Into, agent_type: AgentType, ) -> Result { self.create_with_parent(project_uuid, None, input, agent_type, None, Some(issue_id)) .await } /// Create a new sub-task with a parent reference. pub async fn create_subtask( &self, project_uuid: impl Into, parent_id: i64, input: impl Into, agent_type: AgentType, title: Option, ) -> Result { self.create_with_parent( project_uuid, Some(parent_id), input, agent_type, title, None, ) .await } async fn create_with_parent( &self, project_uuid: impl Into, parent_id: Option, input: impl Into, agent_type: AgentType, title: Option, issue_id: Option, ) -> Result { let model = ActiveModel { project_uuid: sea_orm::Set(project_uuid.into()), parent_id: sea_orm::Set(parent_id), issue_id: sea_orm::Set(issue_id), agent_type: sea_orm::Set(agent_type), status: sea_orm::Set(models::agent_task::TaskStatus::Pending), title: sea_orm::Set(title), input: sea_orm::Set(input.into()), ..Default::default() }; model.insert(self.db()).await } }