more refactoring
This commit is contained in:
parent
13e17770ca
commit
7268d49b4a
12 changed files with 319 additions and 189 deletions
|
|
@ -49,13 +49,15 @@ pub struct RecentRunResponse {
|
|||
pub created_at: chrono::DateTime<chrono::FixedOffset>,
|
||||
}
|
||||
|
||||
use crate::error::AppResult;
|
||||
|
||||
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>> {
|
||||
) -> AppResult<TaskResponse> {
|
||||
let run_id = Uuid::new_v4();
|
||||
|
||||
let new_run = task_run::ActiveModel {
|
||||
|
|
@ -67,7 +69,10 @@ pub async fn execute_agent_run(
|
|||
created_at: Set(Utc::now().into()),
|
||||
};
|
||||
|
||||
new_run.insert(db).await?;
|
||||
new_run
|
||||
.insert(db)
|
||||
.await
|
||||
.map_err(crate::error::AppError::Database)?;
|
||||
|
||||
let mut agent = Agent::new(
|
||||
config.zen_api_key.clone(),
|
||||
|
|
@ -84,31 +89,36 @@ pub async fn execute_agent_run(
|
|||
),
|
||||
};
|
||||
|
||||
let mut run: task_run::ActiveModel = TaskRun::find_by_id(run_id)
|
||||
let run: task_run::ActiveModel = TaskRun::find_by_id(run_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or("Run not found after insert")?
|
||||
.await
|
||||
.map_err(crate::error::AppError::Database)?
|
||||
.ok_or_else(|| crate::error::AppError::NotFound("Run not found after insert".into()))?
|
||||
.into();
|
||||
|
||||
let mut run = run;
|
||||
run.logs = Set(logs.clone());
|
||||
run.answer = Set(answer.clone());
|
||||
run.status = Set(status);
|
||||
|
||||
run.update(db).await?;
|
||||
run.update(db)
|
||||
.await
|
||||
.map_err(crate::error::AppError::Database)?;
|
||||
|
||||
get_task_inner(task_id, db).await
|
||||
}
|
||||
|
||||
pub async fn get_task_inner(
|
||||
id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> Result<TaskResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn get_task_inner(id: Uuid, db: &DatabaseConnection) -> AppResult<TaskResponse> {
|
||||
let results = Task::find_by_id(id)
|
||||
.find_with_related(TaskRun)
|
||||
.all(db)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(crate::error::AppError::Database)?;
|
||||
|
||||
let (t, mut runs) = results.into_iter().next().ok_or("Task not found")?;
|
||||
let (t, mut runs) = results
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| crate::error::AppError::NotFound("Task not found".into()))?;
|
||||
|
||||
runs.sort_by(|a, b| b.created_at.cmp(&a.created_at));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue