112 lines
3.6 KiB
Rust
112 lines
3.6 KiB
Rust
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<Option<Model>, 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<uuid::Uuid>,
|
|
limit: u64,
|
|
) -> Result<Vec<Model>, 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<uuid::Uuid>,
|
|
) -> Result<Vec<Model>, 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<uuid::Uuid>,
|
|
input: impl Into<String>,
|
|
agent_type: AgentType,
|
|
) -> Result<Model, DbErr> {
|
|
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<uuid::Uuid>,
|
|
issue_id: IssueId,
|
|
input: impl Into<String>,
|
|
agent_type: AgentType,
|
|
) -> Result<Model, DbErr> {
|
|
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<uuid::Uuid>,
|
|
parent_id: i64,
|
|
input: impl Into<String>,
|
|
agent_type: AgentType,
|
|
title: Option<String>,
|
|
) -> Result<Model, DbErr> {
|
|
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<uuid::Uuid>,
|
|
parent_id: Option<i64>,
|
|
input: impl Into<String>,
|
|
agent_type: AgentType,
|
|
title: Option<String>,
|
|
issue_id: Option<IssueId>,
|
|
) -> Result<Model, DbErr> {
|
|
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
|
|
}
|
|
}
|