ceylon_core/mesh.rs
1use crate::agent::Agent;
2use crate::error::Result;
3use crate::message::Message;
4use async_trait::async_trait;
5
6/// Trait for agent coordination and message routing.
7///
8/// A mesh manages a collection of agents and handles communication between them.
9/// The primary implementation is [`ceylon_local::LocalMesh`] for single-process scenarios.
10///
11/// # Lifecycle
12///
13/// 1. Create a mesh with `new()`
14/// 2. Add agents with `add_agent()`
15/// 3. Start the mesh with `start()` - calls `on_start` for all agents
16/// 4. Send messages with `send()`
17/// 5. Stop the mesh with `stop()` - calls `on_stop` for all agents
18///
19/// # Example
20///
21/// ```rust,no_run
22/// use ceylon_local::LocalMesh;
23/// use ceylon_core::Mesh;
24///
25/// # async fn example() -> anyhow::Result<()> {
26/// let mesh = LocalMesh::new("my-mesh");
27/// // mesh.add_agent(Box::new(my_agent)).await?;
28/// mesh.start().await?;
29/// // ... use the mesh ...
30/// mesh.stop().await?;
31/// # Ok(())
32/// # }
33/// ```
34#[async_trait]
35pub trait Mesh: Send + Sync {
36 /// Start the mesh and all registered agents.
37 ///
38 /// Calls `on_start` for each agent.
39 async fn start(&self) -> Result<()>;
40
41 /// Stop the mesh and all agents.
42 ///
43 /// Calls `on_stop` for each agent.
44 async fn stop(&self) -> Result<()>;
45
46 /// Add an agent to the mesh.
47 ///
48 /// The agent will be started when `start()` is called, or immediately
49 /// if the mesh is already running.
50 async fn add_agent(&self, agent: Box<dyn Agent + 'static>) -> Result<()>;
51
52 /// Send a message to a specific agent by name.
53 async fn send(&self, message: Message, target: &str) -> Result<()>;
54}