agent_client_protocol/component.rs
1//! ConnectTo abstraction for agents and proxies.
2//!
3//! This module provides the [`ConnectTo`] trait that defines the interface for things
4//! that can be run as part of a conductor's chain - agents, proxies, or any ACP-speaking component.
5//!
6//! ## Usage
7//!
8//! Components connect to other components, creating a chain of message processors.
9//! The type parameter `R` is the role that this component connects to (its counterpart).
10//!
11//! To implement a component, implement the `connect_to` method:
12//!
13//! ```rust,ignore
14//! use agent_client_protocol::{Agent, Client, Connect, Result};
15//!
16//! struct MyAgent {
17//! // configuration fields
18//! }
19//!
20//! // An agent connects to clients
21//! impl ConnectTo<Client> for MyAgent {
22//! async fn connect_to(self, client: impl ConnectTo<Agent>) -> Result<()> {
23//! Agent.builder()
24//! .name("my-agent")
25//! // configure handlers here
26//! .connect_to(client)
27//! .await
28//! }
29//! }
30//! ```
31
32use futures::future::BoxFuture;
33use std::{fmt::Debug, future::Future, marker::PhantomData};
34
35use crate::{Channel, Result, role::Role};
36
37/// A component that can exchange JSON-RPC messages to an endpoint playing the role `R`
38/// (e.g., an ACP [`Agent`](`crate::role::acp::Agent`) or an MCP [`Server`](`crate::role::mcp::Server`)).
39///
40/// This trait represents anything that can communicate via JSON-RPC messages over channels -
41/// agents, proxies, in-process connections, or any ACP-speaking component.
42///
43/// The type parameter `R` is the role that this component serves (its counterpart).
44/// For example:
45/// - An agent implements `Serve<Client>` - it serves clients
46/// - A proxy implements `Serve<Conductor>` - it serves conductors
47/// - Transports like `Channel` implement `Serve<R>` for all `R` since they're role-agnostic
48///
49/// # Component Types
50///
51/// The trait is implemented by several built-in types representing different communication patterns:
52///
53/// - **[`ByteStreams`]**: A component communicating over byte streams (stdin/stdout, sockets, etc.)
54/// - **[`Channel`]**: A component communicating via in-process message channels (for testing or direct connections)
55/// - **[`AcpAgent`]**: An external agent running in a separate process with stdio communication
56/// - **Custom components**: Proxies, transformers, or any ACP-aware service
57///
58/// # Two Ways to Serve
59///
60/// Components can be used in two ways:
61///
62/// 1. **`serve(client)`** - Serve by forwarding to another component (most components implement this)
63/// 2. **`into_server()`** - Convert into a channel endpoint and server future (base cases implement this)
64///
65/// Most components only need to implement `serve(client)` - the `into_server()` method has a default
66/// implementation that creates an intermediate channel and calls `serve`.
67///
68/// # Implementation Example
69///
70/// ```rust,ignore
71/// use agent_client_protocol::{Agent, Result, Serve, role::Client};
72///
73/// struct MyAgent {
74/// config: AgentConfig,
75/// }
76///
77/// impl Serve<Client> for MyAgent {
78/// async fn serve(self, client: impl Serve<Client::Counterpart>) -> Result<()> {
79/// // Set up connection that forwards to client
80/// Agent.builder()
81/// .name("my-agent")
82/// .on_receive_request(async |req: MyRequest, cx| {
83/// // Handle request
84/// cx.respond(MyResponse { status: "ok".into() })
85/// })
86/// .serve(client)
87/// .await
88/// }
89/// }
90/// ```
91///
92/// # Heterogeneous Collections
93///
94/// For storing different component types in the same collection, use [`DynConnectTo`]:
95///
96/// ```rust,ignore
97/// use agent_client_protocol::Client;
98///
99/// let components: Vec<DynConnectTo<Client>> = vec![
100/// DynConnectTo::new(proxy1),
101/// DynConnectTo::new(proxy2),
102/// DynConnectTo::new(agent),
103/// ];
104/// ```
105///
106/// [`ByteStreams`]: crate::ByteStreams
107/// [`AcpAgent`]: https://docs.rs/agent-client-protocol-tokio/latest/agent_client_protocol_tokio/struct.AcpAgent.html
108/// [`Builder`]: crate::Builder
109pub trait ConnectTo<R: Role>: Send + 'static {
110 /// Serve this component by forwarding to a client component.
111 ///
112 /// Most components implement this method to set up their connection and
113 /// forward messages to the provided client.
114 ///
115 /// # Arguments
116 ///
117 /// * `client` - The component to forward messages to (implements `Serve<R::Counterpart>`)
118 ///
119 /// # Returns
120 ///
121 /// A future that resolves when the component stops serving, either successfully
122 /// or with an error. The future must be `Send`.
123 fn connect_to(
124 self,
125 client: impl ConnectTo<R::Counterpart>,
126 ) -> impl Future<Output = Result<()>> + Send;
127
128 /// Convert this component into a channel endpoint and server future.
129 ///
130 /// This method returns:
131 /// - A `Channel` that can be used to communicate with this component
132 /// - A `BoxFuture` that runs the component's server logic
133 ///
134 /// The default implementation creates an intermediate channel pair and calls `serve`
135 /// on one endpoint while returning the other endpoint for the caller to use.
136 ///
137 /// Base cases like `Channel` and `ByteStreams` override this to avoid unnecessary copying.
138 ///
139 /// # Returns
140 ///
141 /// A tuple of `(Channel, BoxFuture)` where the channel is for the caller to use
142 /// and the future must be spawned to run the server.
143 fn into_channel_and_future(self) -> (Channel, BoxFuture<'static, Result<()>>)
144 where
145 Self: Sized,
146 {
147 let (channel_a, channel_b) = Channel::duplex();
148 let future = Box::pin(self.connect_to(channel_b));
149 (channel_a, future)
150 }
151}
152
153/// Type-erased connect trait for object-safe dynamic dispatch.
154///
155/// This trait is internal and used by [`DynConnectTo`]. Users should implement
156/// [`ConnectTo`] instead, which is automatically converted to `ErasedConnectTo`
157/// via a blanket implementation.
158trait ErasedConnectTo<R: Role>: Send {
159 fn type_name(&self) -> String;
160
161 fn connect_to_erased(
162 self: Box<Self>,
163 client: Box<dyn ErasedConnectTo<R::Counterpart>>,
164 ) -> BoxFuture<'static, Result<()>>;
165
166 fn into_channel_and_future_erased(self: Box<Self>)
167 -> (Channel, BoxFuture<'static, Result<()>>);
168}
169
170/// Blanket implementation: any `Serve<R>` can be type-erased.
171impl<C: ConnectTo<R>, R: Role> ErasedConnectTo<R> for C {
172 fn type_name(&self) -> String {
173 std::any::type_name::<C>().to_string()
174 }
175
176 fn connect_to_erased(
177 self: Box<Self>,
178 client: Box<dyn ErasedConnectTo<R::Counterpart>>,
179 ) -> BoxFuture<'static, Result<()>> {
180 Box::pin(async move {
181 (*self)
182 .connect_to(DynConnectTo {
183 inner: client,
184 _marker: PhantomData,
185 })
186 .await
187 })
188 }
189
190 fn into_channel_and_future_erased(
191 self: Box<Self>,
192 ) -> (Channel, BoxFuture<'static, Result<()>>) {
193 (*self).into_channel_and_future()
194 }
195}
196
197/// A dynamically-typed component for heterogeneous collections.
198///
199/// This type wraps any [`ConnectTo`] implementation and provides dynamic dispatch,
200/// allowing you to store different component types in the same collection.
201///
202/// The type parameter `R` is the role that all components in the
203/// collection serve (their counterpart).
204///
205/// # Examples
206///
207/// ```rust,ignore
208/// use agent_client_protocol::{DynConnectTo, Client};
209///
210/// let components: Vec<DynConnectTo<Client>> = vec![
211/// DynConnectTo::new(Proxy1),
212/// DynConnectTo::new(Proxy2),
213/// DynConnectTo::new(Agent),
214/// ];
215/// ```
216pub struct DynConnectTo<R: Role> {
217 inner: Box<dyn ErasedConnectTo<R>>,
218 _marker: PhantomData<R>,
219}
220
221impl<R: Role> DynConnectTo<R> {
222 /// Create a new `DynConnectTo` from any type implementing [`ConnectTo`].
223 pub fn new<C: ConnectTo<R>>(component: C) -> Self {
224 Self {
225 inner: Box::new(component),
226 _marker: PhantomData,
227 }
228 }
229
230 /// Returns the type name of the wrapped component.
231 #[must_use]
232 pub fn type_name(&self) -> String {
233 self.inner.type_name()
234 }
235}
236
237impl<R: Role> ConnectTo<R> for DynConnectTo<R> {
238 async fn connect_to(self, client: impl ConnectTo<R::Counterpart>) -> Result<()> {
239 self.inner
240 .connect_to_erased(Box::new(client) as Box<dyn ErasedConnectTo<R::Counterpart>>)
241 .await
242 }
243
244 fn into_channel_and_future(self) -> (Channel, BoxFuture<'static, Result<()>>) {
245 self.inner.into_channel_and_future_erased()
246 }
247}
248
249impl<R: Role> Debug for DynConnectTo<R> {
250 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
251 f.debug_struct("DynServe")
252 .field("type_name", &self.type_name())
253 .finish()
254 }
255}