use std::fmt;
use std::time::Duration;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::error::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskOutput {
pub result: serde_json::Value,
pub duration: Duration,
}
impl TaskOutput {
pub fn new(result: serde_json::Value, duration: Duration) -> Self {
Self { result, duration }
}
pub fn text(text: impl Into<String>, duration: Duration) -> Self {
Self {
result: serde_json::Value::String(text.into()),
duration,
}
}
pub fn empty(duration: Duration) -> Self {
Self {
result: serde_json::Value::Null,
duration,
}
}
}
#[derive(Debug, Clone)]
pub struct TaskContext {
pub task_id: Uuid,
pub parent_id: Option<Uuid>,
pub metadata: serde_json::Value,
}
impl TaskContext {
pub fn new(task_id: Uuid) -> Self {
Self {
task_id,
parent_id: None,
metadata: serde_json::Value::Null,
}
}
pub fn with_parent(mut self, parent_id: Uuid) -> Self {
self.parent_id = Some(parent_id);
self
}
pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
self.metadata = metadata;
self
}
}
#[async_trait]
pub trait TaskHandler: Send + Sync {
async fn run(&self, ctx: TaskContext) -> Result<TaskOutput, Error>;
fn description(&self) -> &str {
"background task"
}
}
#[derive(Clone)]
pub enum Task {
Job {
id: Uuid,
title: String,
description: String,
},
ToolExec {
parent_id: Uuid,
tool_name: String,
params: serde_json::Value,
},
Background {
id: Uuid,
handler: std::sync::Arc<dyn TaskHandler>,
},
}
impl Task {
pub fn job(title: impl Into<String>, description: impl Into<String>) -> Self {
Self::Job {
id: Uuid::new_v4(),
title: title.into(),
description: description.into(),
}
}
pub fn job_with_id(id: Uuid, title: impl Into<String>, description: impl Into<String>) -> Self {
Self::Job {
id,
title: title.into(),
description: description.into(),
}
}
pub fn tool_exec(
parent_id: Uuid,
tool_name: impl Into<String>,
params: serde_json::Value,
) -> Self {
Self::ToolExec {
parent_id,
tool_name: tool_name.into(),
params,
}
}
pub fn background(handler: std::sync::Arc<dyn TaskHandler>) -> Self {
Self::Background {
id: Uuid::new_v4(),
handler,
}
}
pub fn background_with_id(id: Uuid, handler: std::sync::Arc<dyn TaskHandler>) -> Self {
Self::Background { id, handler }
}
pub fn id(&self) -> Option<Uuid> {
match self {
Self::Job { id, .. } => Some(*id),
Self::ToolExec { .. } => None, Self::Background { id, .. } => Some(*id),
}
}
pub fn parent_id(&self) -> Option<Uuid> {
match self {
Self::Job { .. } => None,
Self::ToolExec { parent_id, .. } => Some(*parent_id),
Self::Background { .. } => None,
}
}
pub fn description(&self) -> String {
match self {
Self::Job { title, .. } => format!("job: {}", title),
Self::ToolExec { tool_name, .. } => format!("tool: {}", tool_name),
Self::Background { handler, .. } => format!("background: {}", handler.description()),
}
}
}
impl fmt::Debug for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Job {
id,
title,
description,
} => f
.debug_struct("Task::Job")
.field("id", id)
.field("title", title)
.field("description", description)
.finish(),
Self::ToolExec {
parent_id,
tool_name,
params,
} => f
.debug_struct("Task::ToolExec")
.field("parent_id", parent_id)
.field("tool_name", tool_name)
.field("params", params)
.finish(),
Self::Background { id, handler } => f
.debug_struct("Task::Background")
.field("id", id)
.field("handler", &handler.description())
.finish(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskStatus {
Queued,
Running,
Completed,
Failed,
Cancelled,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_task_output() {
let output = TaskOutput::text("hello", Duration::from_secs(1));
assert_eq!(output.result, serde_json::json!("hello"));
assert_eq!(output.duration, Duration::from_secs(1));
}
#[test]
fn test_task_context() {
let parent = Uuid::new_v4();
let ctx = TaskContext::new(Uuid::new_v4()).with_parent(parent);
assert_eq!(ctx.parent_id, Some(parent));
}
#[test]
fn test_task_job() {
let task = Task::job("Test Job", "Test description");
assert!(task.id().is_some());
assert!(task.parent_id().is_none());
assert!(task.description().contains("job:"));
}
#[test]
fn test_task_tool_exec() {
let parent_id = Uuid::new_v4();
let task = Task::tool_exec(parent_id, "echo", serde_json::json!({"message": "hi"}));
assert!(task.id().is_none());
assert_eq!(task.parent_id(), Some(parent_id));
assert!(task.description().contains("tool:"));
}
}