Skip to main content

a2a_protocol_server/builder/
steering.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! Turning steering on (ADR 0036).
5
6use super::RequestHandlerBuilder;
7use crate::error::{ServerError, ServerResult};
8use crate::steering::SteeringConfig;
9
10impl RequestHandlerBuilder {
11    /// Takes steering messages: a `SendMessage` that names a running task and
12    /// lists the steering extension
13    /// ([`STEERING_EXTENSION_URI`](a2a_protocol_types::steering::STEERING_EXTENSION_URI))
14    /// is put in that turn's inbox instead of being refused, and the agent
15    /// card declares the extension. Off by default: a server that does not
16    /// call this answers such a message as it always did, and its card does
17    /// not declare the extension.
18    ///
19    /// An executor reads the inbox with
20    /// [`RequestContext::inbox`](crate::RequestContext::inbox); one that never
21    /// does is unaffected. `config` bounds the inbox and the message size; see
22    /// [`SteeringConfig`] and [`crate::steering`] for what a steering message
23    /// is checked against and how it can be refused.
24    ///
25    /// ```rust
26    /// use a2a_protocol_server::{RequestHandlerBuilder, SteeringConfig};
27    /// # use a2a_protocol_server::agent_executor;
28    /// # struct Agent;
29    /// # agent_executor!(Agent, |_ctx, _queue| async { Ok(()) });
30    ///
31    /// let handler = RequestHandlerBuilder::new(Agent)
32    ///     .without_agent_card()
33    ///     .with_steering(SteeringConfig::new().with_inbox_capacity(4))
34    ///     .build()
35    ///     .unwrap();
36    /// # drop(handler);
37    /// ```
38    #[must_use]
39    pub const fn with_steering(mut self, config: SteeringConfig) -> Self {
40        self.steering = Some(config);
41        self
42    }
43
44    /// Refuses a steering configuration that could never deliver anything.
45    pub(super) fn check_steering(&self) -> ServerResult<()> {
46        let Some(config) = &self.steering else {
47            return Ok(());
48        };
49        if config.inbox_capacity() == 0 {
50            return Err(ServerError::Config(
51                "the steering inbox capacity must be at least 1: an inbox that holds nothing \
52                 refuses every message"
53                    .to_owned(),
54            ));
55        }
56        if config.max_message_size() == 0 {
57            return Err(ServerError::Config(
58                "the steering message size limit must be at least 1 byte: no message fits \
59                 under 0"
60                    .to_owned(),
61            ));
62        }
63        Ok(())
64    }
65}