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 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 84)
66async fn main() -> Result<(), BuildError> {
67 // Initialize logging
68 tracing_subscriber::fmt()
69 .with_env_filter(
70 tracing_subscriber::EnvFilter::from_default_env()
71 .add_directive(tracing::Level::INFO.into()),
72 )
73 .init();
74
75 println!("🚀 Starting Echo Agent with declarative configuration");
76 println!();
77
78 // Build and run the agent from configuration file
79 // This is all you need - the rest is handled by the framework!
80 AgentBuilder::from_file("examples/minimal_agent.toml")?
81 .with_handler(EchoHandler)
82 .with_storage(InMemoryTaskStorage::new())
83 .build()?
84 .run()
85 .await
86 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
87
88 Ok(())
89}More examples
examples/auto_storage_agent.rs (line 88)
64async fn main() -> Result<(), BuildError> {
65 // Initialize logging
66 tracing_subscriber::fmt()
67 .with_env_filter(
68 tracing_subscriber::EnvFilter::from_default_env()
69 .add_directive(tracing::Level::INFO.into()),
70 )
71 .init();
72
73 println!("🚀 Starting agent with automatic storage creation");
74 println!(" Storage type is configured in auto_storage.toml");
75 println!();
76
77 // This is ALL you need! The builder:
78 // 1. Reads the TOML config
79 // 2. Creates the appropriate storage (in-memory or SQLx)
80 // 3. Wires everything together
81 // 4. Starts the servers
82 //
83 // No manual storage creation, no boilerplate!
84 AgentBuilder::from_file("examples/auto_storage.toml")?
85 .with_handler(EchoHandler)
86 .build_with_auto_storage() // <- Magic happens here!
87 .await?
88 .run()
89 .await
90 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
91
92 Ok(())
93}examples/ap2_merchant_agent.rs (line 386)
363async fn main() -> Result<(), BuildError> {
364 tracing_subscriber::fmt()
365 .with_env_filter(
366 tracing_subscriber::EnvFilter::from_default_env()
367 .add_directive(tracing::Level::INFO.into()),
368 )
369 .init();
370
371 println!("AP2 Merchant Agent - Shoe Store Demo");
372 println!("=====================================");
373 println!();
374 println!("This agent implements the AP2 payment protocol flow:");
375 println!(" 1. Send a text message to browse the catalog");
376 println!(" 2. Send an IntentMandate to search for products");
377 println!(" 3. Review the CartMandate artifact returned");
378 println!(" 4. Send a PaymentMandate to complete the purchase");
379 println!(" 5. Receive a PaymentReceipt confirming the transaction");
380 println!();
381
382 AgentBuilder::from_file("examples/ap2_merchant.toml")?
383 .with_handler(MerchantHandler)
384 .with_storage(InMemoryTaskStorage::new())
385 .build()?
386 .run()
387 .await
388 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
389
390 Ok(())
391}examples/auto_storage_sqlx.rs (line 109)
67async fn main() -> Result<(), BuildError> {
68 // Load .env file if present
69 dotenvy::dotenv().ok();
70
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 agent with automatic SQLx storage");
80 println!(" Database URL from config: ${{DATABASE_URL}}");
81 println!(" Migrations will be run automatically");
82 println!();
83
84 // Optional: define custom migrations for your agent
85 // These will be run automatically before the agent starts
86 let migrations = &[
87 // Example migration - you can add agent-specific tables here
88 r#"
89 CREATE TABLE IF NOT EXISTS echo_stats (
90 id INTEGER PRIMARY KEY AUTOINCREMENT,
91 message_count INTEGER NOT NULL DEFAULT 0,
92 last_echo TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
93 )
94 "#,
95 ];
96
97 // The builder:
98 // 1. Reads TOML config (including database URL from env)
99 // 2. Creates SQLx connection pool
100 // 3. Runs all migrations (framework + your custom ones)
101 // 4. Wires storage with push notification support
102 // 5. Starts the servers
103 //
104 // All from configuration!
105 AgentBuilder::from_file("examples/auto_storage_sqlx.toml")?
106 .with_handler(PersistentEchoHandler)
107 .build_with_auto_storage_and_migrations(migrations)
108 .await?
109 .run()
110 .await
111 .map_err(|e| BuildError::RuntimeError(e.to_string()))?;
112
113 Ok(())
114}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