Skip to main content

agent_client_protocol_schema/
ext.rs

1//! Extension types and constants for protocol extensibility.
2use derive_more::From;
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::value::RawValue;
6use std::sync::Arc;
7
8/// Value attached to a given ACP type on the `_meta` field.
9///
10/// The _meta property is reserved by ACP to allow clients and agents to attach
11/// additional metadata to their interactions. Implementations MUST NOT make assumptions about
12/// values at these keys.
13///
14/// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
15pub type Meta = serde_json::Map<String, serde_json::Value>;
16
17/// Allows for sending an arbitrary request that is not part of the ACP spec.
18/// Extension methods provide a way to add custom functionality while maintaining
19/// protocol compatibility.
20///
21/// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
22#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
23#[serde(transparent)]
24#[non_exhaustive]
25pub struct ExtRequest {
26    /// Wire method name for this extension request.
27    ///
28    /// Extension method names must start with `_`.
29    #[serde(skip)] // this is used for routing, but when serializing we only want the params
30    pub method: Arc<str>,
31    #[schemars(with = "serde_json::Value")]
32    pub params: Arc<RawValue>,
33}
34
35impl ExtRequest {
36    #[must_use]
37    pub fn new(method: impl Into<Arc<str>>, params: Arc<RawValue>) -> Self {
38        Self {
39            method: method.into(),
40            params,
41        }
42    }
43}
44
45/// Allows for sending an arbitrary response to an [`ExtRequest`] that is not part of the ACP spec.
46/// Extension methods provide a way to add custom functionality while maintaining
47/// protocol compatibility.
48///
49/// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
50#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, From)]
51#[serde(transparent)]
52#[non_exhaustive]
53pub struct ExtResponse(#[schemars(with = "serde_json::Value")] pub Arc<RawValue>);
54
55impl ExtResponse {
56    #[must_use]
57    pub fn new(params: Arc<RawValue>) -> Self {
58        Self(params)
59    }
60}
61
62/// Allows the Agent to send an arbitrary notification that is not part of the ACP spec.
63/// Extension notifications provide a way to send one-way messages for custom functionality
64/// while maintaining protocol compatibility.
65///
66/// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
67#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
68#[serde(transparent)]
69#[non_exhaustive]
70pub struct ExtNotification {
71    /// Wire method name for this extension notification.
72    ///
73    /// Extension method names must start with `_`.
74    #[serde(skip)] // this is used for routing, but when serializing we only want the params
75    pub method: Arc<str>,
76    #[schemars(with = "serde_json::Value")]
77    pub params: Arc<RawValue>,
78}
79
80impl ExtNotification {
81    #[must_use]
82    pub fn new(method: impl Into<Arc<str>>, params: Arc<RawValue>) -> Self {
83        Self {
84            method: method.into(),
85            params,
86        }
87    }
88}