agent_client_protocol_schema/v1/protocol_level.rs
1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5use crate::{IntoOption, Meta, RequestId};
6
7/// **UNSTABLE**
8///
9/// This capability is not part of the spec yet, and may be removed or changed at any point.
10///
11/// Notification to cancel an ongoing request.
12///
13/// See protocol docs: [Cancellation](https://agentclientprotocol.com/protocol/cancellation)
14#[cfg(feature = "unstable_cancel_request")]
15#[skip_serializing_none]
16#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
17#[schemars(extend("x-side" = "protocol", "x-method" = CANCEL_REQUEST_METHOD_NAME))]
18#[serde(rename_all = "camelCase")]
19#[non_exhaustive]
20pub struct CancelRequestNotification {
21 /// The ID of the request to cancel.
22 pub request_id: RequestId,
23 /// The _meta property is reserved by ACP to allow clients and agents to attach additional
24 /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
25 /// these keys.
26 ///
27 /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
28 #[serde(rename = "_meta")]
29 pub meta: Option<Meta>,
30}
31
32#[cfg(feature = "unstable_cancel_request")]
33impl CancelRequestNotification {
34 #[must_use]
35 pub fn new(request_id: impl Into<RequestId>) -> Self {
36 Self {
37 request_id: request_id.into(),
38 meta: None,
39 }
40 }
41
42 /// The _meta property is reserved by ACP to allow clients and agents to attach additional
43 /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
44 /// these keys.
45 ///
46 /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
47 #[must_use]
48 pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
49 self.meta = meta.into_option();
50 self
51 }
52}
53
54// Method schema
55
56/// Names of all methods that agents handle.
57///
58/// Provides a centralized definition of method names used in the protocol.
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
60#[non_exhaustive]
61pub struct GeneralMethodNames {
62 #[cfg(feature = "unstable_cancel_request")]
63 pub cancel_request: &'static str,
64}
65
66/// Constant containing all agent method names.
67pub const PROTOCOL_LEVEL_METHOD_NAMES: GeneralMethodNames = GeneralMethodNames {
68 #[cfg(feature = "unstable_cancel_request")]
69 cancel_request: CANCEL_REQUEST_METHOD_NAME,
70};
71
72/// Method name for general cancel notification
73pub(crate) const CANCEL_REQUEST_METHOD_NAME: &str = "$/cancel_request";
74
75/// General protocol-level notifications that all sides are expected to
76/// implement.
77///
78/// Notifications whose methods start with '$/' are messages which
79/// are protocol implementation dependent and might not be implementable in all
80/// clients or agents. For example if the implementation uses a single threaded
81/// synchronous programming language then there is little it can do to react to
82/// a `$/cancel_request` notification. If an agent or client receives
83/// notifications starting with '$/' it is free to ignore the notification.
84///
85/// Notifications do not expect a response.
86#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
87#[serde(untagged)]
88#[schemars(inline)]
89#[non_exhaustive]
90pub enum ProtocolLevelNotification {
91 /// **UNSTABLE**
92 ///
93 /// This capability is not part of the spec yet, and may be removed or
94 /// changed at any point.
95 ///
96 /// Cancels an ongoing request.
97 ///
98 /// This is a notification sent by the side that sent a request to cancel that request.
99 ///
100 /// Upon receiving this notification, the receiver:
101 ///
102 /// 1. MUST cancel the corresponding request activity and all nested activities
103 /// 2. MAY send any pending notifications.
104 /// 3. MUST send one of these responses for the original request:
105 /// - Valid response with appropriate data (partial results or cancellation marker)
106 /// - Error response with code `-32800` (Cancelled)
107 ///
108 /// See protocol docs: [Cancellation](https://agentclientprotocol.com/protocol/cancellation)
109 #[cfg(feature = "unstable_cancel_request")]
110 CancelRequestNotification(CancelRequestNotification),
111}
112
113impl ProtocolLevelNotification {
114 /// Returns the corresponding method name of the notification.
115 #[must_use]
116 pub fn method(&self) -> &str {
117 match self {
118 #[cfg(feature = "unstable_cancel_request")]
119 Self::CancelRequestNotification(..) => PROTOCOL_LEVEL_METHOD_NAMES.cancel_request,
120 }
121 }
122}