refactoring
This commit is contained in:
parent
04ece6afb1
commit
13e17770ca
12 changed files with 634 additions and 598 deletions
131
src/domain/tasks.rs
Normal file
131
src/domain/tasks.rs
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
use chrono::Utc;
|
||||
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::domain::agent::Agent;
|
||||
use crate::entities::task::Entity as Task;
|
||||
use crate::entities::task_run::{self, Entity as TaskRun};
|
||||
use crate::scheduler::Scheduler;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateTaskRequest {
|
||||
pub goal: String,
|
||||
pub cron: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateTaskRequest {
|
||||
pub goal: String,
|
||||
pub cron: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TaskResponse {
|
||||
pub id: Uuid,
|
||||
pub goal: String,
|
||||
pub cron: Option<String>,
|
||||
pub created_at: chrono::DateTime<chrono::FixedOffset>,
|
||||
pub runs: Vec<TaskRunResponse>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TaskRunResponse {
|
||||
pub id: Uuid,
|
||||
pub status: String,
|
||||
pub logs: String,
|
||||
pub answer: Option<String>,
|
||||
pub created_at: chrono::DateTime<chrono::FixedOffset>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct RecentRunResponse {
|
||||
pub id: Uuid,
|
||||
pub task_id: Uuid,
|
||||
pub goal: String,
|
||||
pub status: String,
|
||||
pub created_at: chrono::DateTime<chrono::FixedOffset>,
|
||||
}
|
||||
|
||||
pub async fn execute_agent_run(
|
||||
db: &DatabaseConnection,
|
||||
_scheduler: &Arc<Scheduler>,
|
||||
config: &Arc<Config>,
|
||||
task_id: Uuid,
|
||||
goal: String,
|
||||
) -> Result<TaskResponse, Box<dyn std::error::Error>> {
|
||||
let run_id = Uuid::new_v4();
|
||||
|
||||
let new_run = task_run::ActiveModel {
|
||||
id: Set(run_id),
|
||||
task_id: Set(task_id),
|
||||
status: Set("running".to_string()),
|
||||
logs: Set(String::new()),
|
||||
answer: Set(None),
|
||||
created_at: Set(Utc::now().into()),
|
||||
};
|
||||
|
||||
new_run.insert(db).await?;
|
||||
|
||||
let mut agent = Agent::new(
|
||||
config.zen_api_key.clone(),
|
||||
config.tavily_api_key.clone(),
|
||||
goal.clone(),
|
||||
)?;
|
||||
|
||||
let (logs, answer, status) = match agent.run(config).await {
|
||||
Ok((logs, answer)) => (logs, answer, "completed".to_string()),
|
||||
Err(e) => (
|
||||
format!("Execution failed: {}", e),
|
||||
None,
|
||||
"failed".to_string(),
|
||||
),
|
||||
};
|
||||
|
||||
let mut run: task_run::ActiveModel = TaskRun::find_by_id(run_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or("Run not found after insert")?
|
||||
.into();
|
||||
|
||||
run.logs = Set(logs.clone());
|
||||
run.answer = Set(answer.clone());
|
||||
run.status = Set(status);
|
||||
|
||||
run.update(db).await?;
|
||||
|
||||
get_task_inner(task_id, db).await
|
||||
}
|
||||
|
||||
pub async fn get_task_inner(
|
||||
id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> Result<TaskResponse, Box<dyn std::error::Error>> {
|
||||
let results = Task::find_by_id(id)
|
||||
.find_with_related(TaskRun)
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
let (t, mut runs) = results.into_iter().next().ok_or("Task not found")?;
|
||||
|
||||
runs.sort_by(|a, b| b.created_at.cmp(&a.created_at));
|
||||
|
||||
Ok(TaskResponse {
|
||||
id: t.id,
|
||||
goal: t.goal,
|
||||
cron: t.cron,
|
||||
created_at: t.created_at,
|
||||
runs: runs
|
||||
.into_iter()
|
||||
.map(|r| TaskRunResponse {
|
||||
id: r.id,
|
||||
status: r.status,
|
||||
logs: r.logs,
|
||||
answer: r.answer,
|
||||
created_at: r.created_at,
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue