ceylon_next/
tasks.rs

1//! Task management types for agent execution.
2//!
3//! This module provides types for creating task requests and handling task responses.
4
5use crate::llm::types::Message;
6
7/// A request for an agent to perform a task.
8///
9/// `TaskRequest` encapsulates all the information needed for an agent to
10/// process a user request, including optional metadata like name, description,
11/// and priority.
12///
13/// # Examples
14///
15/// ```rust
16/// use ceylon_next::tasks::TaskRequest;
17///
18/// // Simple task
19/// let task = TaskRequest::new("What is 2 + 2?");
20///
21/// // Task with metadata
22/// let mut task = TaskRequest::new("Analyze this data");
23/// task.with_name("Data Analysis")
24///     .with_description("Perform statistical analysis on user data")
25///     .with_priority(8);
26/// ```
27pub struct TaskRequest {
28    id: String,
29    name: String,
30    description: String,
31    priority: u64,
32    input_data: String,
33}
34
35impl TaskRequest {
36    /// Creates a new task request with the given input.
37    ///
38    /// # Arguments
39    ///
40    /// * `input_data` - The user's request or question
41    ///
42    /// # Examples
43    ///
44    /// ```rust
45    /// use ceylon_next::tasks::TaskRequest;
46    ///
47    /// let task = TaskRequest::new("Explain quantum computing");
48    /// ```
49    pub fn new(input_data: &str) -> Self {
50        let id = uuid::Uuid::new_v4().to_string();
51        Self {
52            id,
53            name: "".into(),
54            description: "".into(),
55            priority: 5,
56            input_data: input_data.into(),
57        }
58    }
59
60    /// Sets the name of the task.
61    ///
62    /// # Arguments
63    ///
64    /// * `name` - A short name for the task
65    pub fn with_name(&mut self, name: &str) -> &mut Self {
66        self.name = name.into();
67        self
68    }
69
70    /// Sets the description of the task.
71    ///
72    /// # Arguments
73    ///
74    /// * `description` - A detailed description of what the task should accomplish
75    pub fn with_description(&mut self, description: &str) -> &mut Self {
76        self.description = description.into();
77        self
78    }
79
80    /// Sets the priority of the task (1-10, where 10 is highest).
81    ///
82    /// # Arguments
83    ///
84    /// * `priority` - Task priority level
85    pub fn with_priority(&mut self, priority: u64) -> &mut Self {
86        self.priority = priority;
87        self
88    }
89}
90
91impl TaskRequest {
92    /// Returns the unique ID of this task.
93    pub fn id(&self) -> &str {
94        &self.id
95    }
96
97    /// Converts the task to an LLM message.
98    ///
99    /// This is used internally by the agent to send the task to the LLM.
100    pub fn message(&self) -> Message {
101        Message {
102            role: "user".into(),
103            content: self.input_data.clone(),
104        }
105    }
106
107    /// Returns the task description.
108    pub fn description(&self) -> &str {
109        &self.description
110    }
111}
112/// The output data from a completed task.
113///
114/// Supports various output types including text, binary files, images, audio, and video.
115///
116/// # Examples
117///
118/// ```rust
119/// use ceylon_next::tasks::OutputData;
120///
121/// let text_output = OutputData::Text("Hello, world!".to_string());
122/// let file_output = OutputData::File(vec![0x89, 0x50, 0x4E, 0x47]); // PNG header
123/// ```
124#[derive(Debug, Clone)]
125pub enum OutputData {
126    /// Text response
127    Text(String),
128    /// Binary file data
129    File(Vec<u8>),
130    /// Image data
131    Image(Vec<u8>),
132    /// Audio data
133    Audio(Vec<u8>),
134    /// Video data
135    Video(Vec<u8>),
136    /// Raw binary data
137    Raw(Vec<u8>),
138}
139
140/// The response from an agent after completing a task.
141///
142/// Contains the task ID and the output data produced by the agent.
143///
144/// # Examples
145///
146/// ```rust,no_run
147/// use ceylon_next::agent::Agent;
148/// use ceylon_next::tasks::{TaskRequest, OutputData};
149///
150/// # #[tokio::main]
151/// # async fn main() {
152/// let mut agent = Agent::new("Assistant", "openai::gpt-4");
153/// let task = TaskRequest::new("Say hello");
154/// let response = agent.run(task).await;
155///
156/// match response.result() {
157///     OutputData::Text(text) => println!("Agent said: {}", text),
158///     _ => println!("Non-text response"),
159/// }
160/// # }
161/// ```
162pub struct TaskResponse {
163    id: String,
164    data: OutputData,
165}
166
167impl TaskResponse {
168    /// Returns a clone of the output data.
169    ///
170    /// # Examples
171    ///
172    /// ```rust
173    /// use ceylon_next::tasks::{TaskResponse, OutputData};
174    ///
175    /// let response = TaskResponse::new("task-123", OutputData::Text("Hello".to_string()));
176    /// let output = response.result();
177    /// ```
178    pub fn result(&self) -> OutputData {
179        self.data.clone()
180    }
181}
182
183impl TaskResponse {
184    /// Creates a new task response.
185    ///
186    /// # Arguments
187    ///
188    /// * `id` - The ID of the task this is responding to
189    /// * `output` - The output data produced by the agent
190    pub fn new(id: &str, output: OutputData) -> Self {
191        Self {
192            id: id.into(),
193            data: output,
194        }
195    }
196}
197
198impl TaskResponse {
199    /// Returns the ID of the task this response is for.
200    pub fn id(&self) -> &str {
201        &self.id
202    }
203}