pub struct AgentRuntime<H, S> { /* private fields */ }Expand description
Agent runtime that manages the server lifecycle
Implementations§
Source§impl<H, S> AgentRuntime<H, S>where
H: AsyncMessageHandler + Clone + Send + Sync + 'static,
S: AsyncTaskManager + AsyncNotificationManager + Clone + Send + Sync + 'static,
impl<H, S> AgentRuntime<H, S>where
H: AsyncMessageHandler + Clone + Send + Sync + 'static,
S: AsyncTaskManager + AsyncNotificationManager + Clone + Send + Sync + 'static,
Sourcepub fn new(config: AgentConfig, handler: Arc<H>, storage: Arc<S>) -> Self
pub fn new(config: AgentConfig, handler: Arc<H>, storage: Arc<S>) -> Self
Create a new runtime
Sourcepub async fn start_http(&self) -> Result<(), RuntimeError>
pub async fn start_http(&self) -> Result<(), RuntimeError>
Start HTTP server
Sourcepub async fn start_websocket(&self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
pub async fn start_websocket(&self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
Start WebSocket server
Sourcepub async fn start_all(&self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
pub async fn start_all(&self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
Start both HTTP and WebSocket servers
Sourcepub async fn run(self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
pub async fn run(self) -> Result<(), RuntimeError>where
S: AsyncStreamingHandler,
Start the appropriate server(s) based on configuration
Examples found in repository?
examples/minimal_agent.rs (line 88)
70async fn main() -> Result<(), BuildError> {
71 // Initialize logging
72 tracing_subscriber::fmt()
73 .with_env_filter(
74 tracing_subscriber::EnvFilter::from_default_env()
75 .add_directive(tracing::Level::INFO.into()),
76 )
77 .init();
78
79 println!("🚀 Starting Echo Agent with declarative configuration");
80 println!();
81
82 // Build and run the agent from configuration file
83 // This is all you need - the rest is handled by the framework!
84 AgentBuilder::from_file("examples/minimal_agent.toml")?
85 .with_handler(EchoHandler)
86 .with_storage(InMemoryTaskStorage::new())
87 .build()?
88 .run()
89 .await
90 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
91
92 Ok(())
93}More examples
examples/auto_storage_agent.rs (line 92)
68async fn main() -> Result<(), BuildError> {
69 // Initialize logging
70 tracing_subscriber::fmt()
71 .with_env_filter(
72 tracing_subscriber::EnvFilter::from_default_env()
73 .add_directive(tracing::Level::INFO.into()),
74 )
75 .init();
76
77 println!("🚀 Starting agent with automatic storage creation");
78 println!(" Storage type is configured in auto_storage.toml");
79 println!();
80
81 // This is ALL you need! The builder:
82 // 1. Reads the TOML config
83 // 2. Creates the appropriate storage (in-memory or SQLx)
84 // 3. Wires everything together
85 // 4. Starts the servers
86 //
87 // No manual storage creation, no boilerplate!
88 AgentBuilder::from_file("examples/auto_storage.toml")?
89 .with_handler(EchoHandler)
90 .build_with_auto_storage() // <- Magic happens here!
91 .await?
92 .run()
93 .await
94 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
95
96 Ok(())
97}examples/auto_storage_sqlx.rs (line 113)
71async fn main() -> Result<(), BuildError> {
72 // Load .env file if present
73 dotenvy::dotenv().ok();
74
75 // Initialize logging
76 tracing_subscriber::fmt()
77 .with_env_filter(
78 tracing_subscriber::EnvFilter::from_default_env()
79 .add_directive(tracing::Level::INFO.into()),
80 )
81 .init();
82
83 println!("🚀 Starting agent with automatic SQLx storage");
84 println!(" Database URL from config: ${{DATABASE_URL}}");
85 println!(" Migrations will be run automatically");
86 println!();
87
88 // Optional: define custom migrations for your agent
89 // These will be run automatically before the agent starts
90 let migrations = &[
91 // Example migration - you can add agent-specific tables here
92 r#"
93 CREATE TABLE IF NOT EXISTS echo_stats (
94 id INTEGER PRIMARY KEY AUTOINCREMENT,
95 message_count INTEGER NOT NULL DEFAULT 0,
96 last_echo TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
97 )
98 "#,
99 ];
100
101 // The builder:
102 // 1. Reads TOML config (including database URL from env)
103 // 2. Creates SQLx connection pool
104 // 3. Runs all migrations (framework + your custom ones)
105 // 4. Wires storage with push notification support
106 // 5. Starts the servers
107 //
108 // All from configuration!
109 AgentBuilder::from_file("examples/auto_storage_sqlx.toml")?
110 .with_handler(PersistentEchoHandler)
111 .build_with_auto_storage_and_migrations(migrations)
112 .await?
113 .run()
114 .await
115 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
116
117 Ok(())
118}examples/reimbursement_builder.rs (line 62)
14async fn main() -> Result<(), Box<dyn std::error::Error>> {
15 // Load environment variables from .env file
16 dotenvy::dotenv().ok();
17
18 // Initialize logging
19 tracing_subscriber::fmt()
20 .with_env_filter(
21 tracing_subscriber::EnvFilter::from_default_env()
22 .add_directive(tracing::Level::INFO.into()),
23 )
24 .init();
25
26 println!("🚀 Starting Reimbursement Agent with Builder API");
27 println!();
28
29 // Get database URL from environment or use default
30 let database_url =
31 env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite:reimbursement_tasks.db".to_string());
32
33 // Create storage with custom migrations
34 let migrations = &[include_str!("../migrations/001_create_reimbursements.sql")];
35 let storage = SqlxTaskStorage::with_migrations(&database_url, migrations)
36 .await
37 .map_err(|e| format!("Failed to create storage: {}", e))?;
38
39 // Create the handler
40 let handler = ReimbursementHandler::new(storage.clone());
41
42 // Build and run the agent - this is where the magic happens!
43 // The configuration file defines all the metadata, skills, and features
44 // The builder wires everything together automatically
45 AgentBuilder::from_file("reimbursement.toml")?
46 .with_config(|config| {
47 // Override config from environment if needed
48 if let Ok(port) = env::var("HTTP_PORT") {
49 if let Ok(port_num) = port.parse() {
50 config.server.http_port = port_num;
51 }
52 }
53 if let Ok(port) = env::var("WS_PORT") {
54 if let Ok(port_num) = port.parse() {
55 config.server.ws_port = port_num;
56 }
57 }
58 })
59 .with_handler(handler)
60 .with_storage(storage)
61 .build()?
62 .run()
63 .await
64 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
65
66 Ok(())
67}Auto Trait Implementations§
impl<H, S> Freeze for AgentRuntime<H, S>
impl<H, S> RefUnwindSafe for AgentRuntime<H, S>where
H: RefUnwindSafe,
S: RefUnwindSafe,
impl<H, S> Send for AgentRuntime<H, S>
impl<H, S> Sync for AgentRuntime<H, S>
impl<H, S> Unpin for AgentRuntime<H, S>
impl<H, S> UnsafeUnpin for AgentRuntime<H, S>
impl<H, S> UnwindSafe for AgentRuntime<H, S>where
H: RefUnwindSafe,
S: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more