pub trait AsAgent:
HasAgentData
+ Send
+ Sync
+ 'static {
// Required method
fn new(
ma: ModularAgent,
id: String,
spec: AgentSpec,
) -> Result<Self, AgentError>
where Self: Sized;
// Provided methods
fn configs_changed(&mut self) -> Result<(), AgentError> { ... }
fn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn process<'life0, 'async_trait>(
&'life0 mut self,
_ctx: AgentContext,
_port: String,
_value: AgentValue,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Simplified trait for implementing custom agents.
Implement this trait instead of Agent directly.
The Agent trait is automatically implemented for all types that implement AsAgent.
§Cancellation safety
The agent loop races process() against the agent’s
cancellation token, which fires when the agent (or its whole preset) is
stopped. On cancellation the in-flight process() future is dropped at
whatever await point it has reached — implementations must not rely on
running to completion. In particular, outputs emitted before the drop
stay emitted, and internal bookkeeping updated across await points (e.g.
entries in a pending map) may be left behind; keep such state consistent
at every await point or clean it up in stop().
Flow-level aborts (ModularAgent::abort_context)
are cooperative: the context’s token fires, but messages carrying it are
still delivered so that wind-down outputs (e.g. an aborted final message
replacing a dangling partial in history) can traverse the graph.
Implementations that initiate external work (network requests, DB writes,
message posts) must therefore check
ctx.is_cancelled() before starting
it, and may select on
ctx.cancel_token() at long awaits
to wind down gracefully.
Required Methods§
Sourcefn new(
ma: ModularAgent,
id: String,
spec: AgentSpec,
) -> Result<Self, AgentError>where
Self: Sized,
fn new(
ma: ModularAgent,
id: String,
spec: AgentSpec,
) -> Result<Self, AgentError>where
Self: Sized,
Constructs a new agent instance.
Provided Methods§
Sourcefn configs_changed(&mut self) -> Result<(), AgentError>
fn configs_changed(&mut self) -> Result<(), AgentError>
Called when configuration values change.
Override to react to configuration changes at runtime.
Sourcefn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called when the agent starts.
Override for initialization logic or to emit initial values.
Sourcefn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called when the agent stops.
Override for cleanup logic.
Sourcefn process<'life0, 'async_trait>(
&'life0 mut self,
_ctx: AgentContext,
_port: String,
_value: AgentValue,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn process<'life0, 'async_trait>(
&'life0 mut self,
_ctx: AgentContext,
_port: String,
_value: AgentValue,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Processes an input message.
Override to implement the agent’s main logic.
This method may be cancelled by being dropped at any await point (see
the trait-level docs). Long-running
implementations can additionally observe
AgentContext::cancel_token to abort gracefully when the flow is
cancelled via ModularAgent::abort_context, recording the
interruption as AgentError::Cancelled.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".