Skip to main content

agent_client_protocol/schema/
proxy_protocol.rs

1//! Protocol types for proxy communication.
2//!
3//! These types are intended to become part of the ACP protocol specification.
4
5use crate::{JsonRpcMessage, JsonRpcNotification, JsonRpcRequest, UntypedMessage};
6use agent_client_protocol_schema::v1::{InitializeRequest, InitializeResponse};
7use serde::{Deserialize, Serialize};
8
9// =============================================================================
10// Successor forwarding protocol
11// =============================================================================
12
13/// JSON-RPC method name for successor forwarding.
14pub const METHOD_SUCCESSOR_MESSAGE: &str = "_proxy/successor";
15
16/// A message being sent to the successor component.
17///
18/// Used in `_proxy/successor` when the proxy wants to forward a message downstream.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct SuccessorMessage<M: JsonRpcMessage = UntypedMessage> {
21    /// The message to be sent to the successor component.
22    #[serde(flatten)]
23    pub message: M,
24
25    /// Optional `_meta` metadata.
26    #[serde(
27        rename = "_meta",
28        alias = "meta",
29        skip_serializing_if = "Option::is_none"
30    )]
31    pub meta: Option<serde_json::Value>,
32}
33
34impl<M: JsonRpcMessage> JsonRpcMessage for SuccessorMessage<M> {
35    fn matches_method(method: &str) -> bool {
36        method == METHOD_SUCCESSOR_MESSAGE
37    }
38
39    fn method(&self) -> &str {
40        METHOD_SUCCESSOR_MESSAGE
41    }
42
43    fn to_untyped_message(&self) -> Result<UntypedMessage, crate::Error> {
44        UntypedMessage::new(
45            METHOD_SUCCESSOR_MESSAGE,
46            SuccessorMessage {
47                message: self.message.to_untyped_message()?,
48                meta: self.meta.clone(),
49            },
50        )
51    }
52
53    fn parse_message(method: &str, params: &impl Serialize) -> Result<Self, crate::Error> {
54        if method != METHOD_SUCCESSOR_MESSAGE {
55            return Err(crate::Error::method_not_found());
56        }
57        let outer = crate::util::json_cast_params::<_, SuccessorMessage<UntypedMessage>>(params)?;
58        if !M::matches_method(&outer.message.method) {
59            return Err(crate::Error::method_not_found());
60        }
61        let inner = M::parse_message(&outer.message.method, &outer.message.params)?;
62        Ok(SuccessorMessage {
63            message: inner,
64            meta: outer.meta,
65        })
66    }
67}
68
69impl<Req: JsonRpcRequest> JsonRpcRequest for SuccessorMessage<Req> {
70    type Response = Req::Response;
71}
72
73impl<Notif: JsonRpcNotification> JsonRpcNotification for SuccessorMessage<Notif> {}
74
75// =============================================================================
76// Proxy initialization protocol
77// =============================================================================
78
79/// JSON-RPC method name for proxy initialization.
80pub const METHOD_INITIALIZE_PROXY: &str = "_proxy/initialize";
81
82/// Initialize request for proxy components.
83///
84/// This is sent to components that have a successor in the chain.
85/// Components that receive this (instead of `InitializeRequest`) know they
86/// are operating as a proxy and should forward messages to their successor.
87#[derive(Debug, Clone, Serialize, Deserialize, crate::JsonRpcRequest)]
88#[request(method = "_proxy/initialize", response = InitializeResponse, crate = crate)]
89pub struct InitializeProxyRequest {
90    /// The underlying initialize request data.
91    #[serde(flatten)]
92    pub initialize: InitializeRequest,
93}
94
95impl From<InitializeRequest> for InitializeProxyRequest {
96    fn from(initialize: InitializeRequest) -> Self {
97        Self { initialize }
98    }
99}