autoagents_core/runtime/
mod.rs

1use crate::agent::{RunnableAgent, RunnableAgentError};
2use crate::error::Error;
3use crate::protocol::{AgentID, Event, RuntimeID, SubmissionId};
4use async_trait::async_trait;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::fmt::Debug;
8use std::sync::Arc;
9use tokio::sync::mpsc::error::SendError;
10use tokio::task::JoinError;
11use tokio_stream::wrappers::ReceiverStream;
12use uuid::Uuid;
13
14pub(crate) mod manager;
15mod single_threaded;
16pub use single_threaded::SingleThreadedRuntime;
17
18/// Error types for Session operations
19#[derive(Debug, thiserror::Error)]
20pub enum RuntimeError {
21    #[error("Agent not found: {0}")]
22    AgentNotFound(Uuid),
23
24    #[error("No task set for agent: {0}")]
25    NoTaskSet(Uuid),
26
27    #[error("Task is None")]
28    EmptyTask,
29
30    #[error("Task join error: {0}")]
31    TaskJoinError(#[from] JoinError),
32
33    #[error("Event error: {0}")]
34    EventError(#[from] SendError<Event>),
35
36    #[error("RunnableAgent error: {0}")]
37    RunnableAgentError(#[from] RunnableAgentError),
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct Task {
42    pub prompt: String,
43    pub submission_id: SubmissionId,
44    pub completed: bool,
45    pub result: Option<Value>,
46    agent_id: Option<AgentID>,
47}
48
49impl Task {
50    pub fn new<T: Into<String>>(task: T, agent_id: Option<AgentID>) -> Self {
51        Self {
52            prompt: task.into(),
53            submission_id: Uuid::new_v4(),
54            completed: false,
55            result: None,
56            agent_id,
57        }
58    }
59}
60
61#[async_trait]
62pub trait Runtime: Send + Sync + 'static + Debug {
63    fn id(&self) -> RuntimeID;
64    async fn send_message(&self, message: String, agent_id: AgentID) -> Result<(), Error>;
65    async fn publish_message(&self, message: String, topic: String) -> Result<(), Error>;
66    async fn subscribe(&self, agent_id: AgentID, topic: String) -> Result<(), Error>;
67    async fn register_agent(&self, agent: Arc<dyn RunnableAgent>) -> Result<(), Error>;
68    async fn take_event_receiver(&self) -> Option<ReceiverStream<Event>>;
69    async fn run(&self) -> Result<(), Error>;
70    async fn stop(&self) -> Result<(), Error>;
71}