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