Skip to main content

AgentRuntime

Struct AgentRuntime 

Source
pub struct AgentRuntime<H, S> { /* private fields */ }
Expand description

Agent runtime that manages the server lifecycle

Implementations§

Source§

impl<H, S> AgentRuntime<H, S>

Source

pub fn new(config: AgentConfig, handler: Arc<H>, storage: Arc<S>) -> Self

Create a new runtime

Source

pub async fn start_http(&self) -> Result<(), RuntimeError>

Start HTTP server

Source

pub async fn start_websocket(&self) -> Result<(), RuntimeError>

Start WebSocket server

Source

pub async fn start_all(&self) -> Result<(), RuntimeError>

Start both HTTP and WebSocket servers

Source

pub async fn run(self) -> Result<(), RuntimeError>

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
Hide additional 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>

§

impl<H, S> Send for AgentRuntime<H, S>
where H: Sync + Send, S: Sync + Send,

§

impl<H, S> Sync for AgentRuntime<H, S>
where H: Sync + Send, S: Sync + Send,

§

impl<H, S> Unpin for AgentRuntime<H, S>

§

impl<H, S> UnsafeUnpin for AgentRuntime<H, S>

§

impl<H, S> UnwindSafe for AgentRuntime<H, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,