agent_client_protocol_schema/
lib.rs

1//! [![Agent Client Protocol](https://zed.dev/img/acp/banner-dark.webp)](https://agentclientprotocol.com/)
2//!
3//! # Agent Client Protocol (ACP)
4//!
5//! The Agent Client Protocol standardizes communication between code editors
6//! (IDEs, text-editors, etc.) and coding agents (programs that use generative AI
7//! to autonomously modify code).
8//!
9//! ## Protocol & Transport
10//!
11//! ACP is a JSON-RPC based protocol. While clients typically start agents as
12//! subprocesses and communicate over stdio (stdin/stdout), this crate is
13//! transport-agnostic.
14//!
15//! You can use any bidirectional stream that implements `AsyncRead` and `AsyncWrite`.
16//!
17//! ## Core Components
18//!
19//! - **Agent**: Programs that use generative AI to autonomously modify code
20//!   - See: [Agent](https://agentclientprotocol.com/protocol/overview#agent)
21//! - **Client**: Code editors that provide the interface between users and agents
22//!   - See: [Client](https://agentclientprotocol.com/protocol/overview#client)
23//!
24//! ## Getting Started
25//!
26//! To understand the protocol, start by exploring the [`Agent`] and [`Client`] traits,
27//! which define the core methods and capabilities of each side of the connection.
28//!
29//! To see working examples of these traits in action, check out the
30//! [agent](https://github.com/agentclientprotocol/rust-sdk/blob/main/examples/agent.rs)
31//! and
32//! [client](https://github.com/agentclientprotocol/rust-sdk/blob/main/examples/client.rs)
33//! example binaries included with this crate.
34//!
35//! ### Implementation Pattern
36//!
37//! ACP uses a symmetric design where each participant implements one trait and
38//! creates a connection that provides the complementary trait:
39//!
40//! - **Agent builders** implement the [`Agent`] trait to handle client requests
41//!   (like initialization, authentication, and prompts). They pass this implementation
42//!   to [`AgentSideConnection::new`], which returns a connection providing [`Client`]
43//!   methods for requesting permissions and accessing the file system.
44//!
45//! - **Client builders** implement the [`Client`] trait to handle agent requests
46//!   (like file system operations and permission checks). They pass this implementation
47//!   to [`ClientSideConnection::new`], which returns a connection providing [`Agent`]
48//!   methods for managing sessions and sending prompts.
49//!
50//! For the complete protocol specification and documentation, visit:
51//! [https://agentclientprotocol.com](https://agentclientprotocol.com)
52
53mod agent;
54mod client;
55mod content;
56mod error;
57mod ext;
58mod plan;
59#[cfg(feature = "unstable_cancel_request")]
60mod protocol_level;
61mod rpc;
62mod tool_call;
63mod version;
64
65pub use agent::*;
66pub use client::*;
67pub use content::*;
68use derive_more::{Display, From};
69pub use error::*;
70pub use ext::*;
71pub use plan::*;
72#[cfg(feature = "unstable_cancel_request")]
73pub use protocol_level::*;
74pub use rpc::*;
75pub use serde_json::value::RawValue;
76pub use tool_call::*;
77pub use version::*;
78
79use schemars::JsonSchema;
80use serde::{Deserialize, Serialize};
81use std::{
82    borrow::Cow,
83    ffi::OsStr,
84    path::{Path, PathBuf},
85    sync::Arc,
86};
87
88/// A unique identifier for a conversation session between a client and agent.
89///
90/// Sessions maintain their own context, conversation history, and state,
91/// allowing multiple independent interactions with the same agent.
92///
93/// See protocol docs: [Session ID](https://agentclientprotocol.com/protocol/session-setup#session-id)
94#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, Display, From)]
95#[serde(transparent)]
96#[from(Arc<str>, String, &'static str)]
97#[non_exhaustive]
98pub struct SessionId(pub Arc<str>);
99
100impl SessionId {
101    pub fn new(id: impl Into<Arc<str>>) -> Self {
102        Self(id.into())
103    }
104}
105
106/// Utility trait for builder methods for optional values.
107/// This allows the caller to either pass in the value itself without wrapping it in `Some`,
108/// or to just pass in an Option if that is what they have.
109pub trait IntoOption<T> {
110    fn into_option(self) -> Option<T>;
111}
112
113impl<T> IntoOption<T> for Option<T> {
114    fn into_option(self) -> Option<T> {
115        self
116    }
117}
118
119impl<T> IntoOption<T> for T {
120    fn into_option(self) -> Option<T> {
121        Some(self)
122    }
123}
124
125impl IntoOption<String> for &str {
126    fn into_option(self) -> Option<String> {
127        Some(self.into())
128    }
129}
130
131impl IntoOption<String> for &mut str {
132    fn into_option(self) -> Option<String> {
133        Some(self.into())
134    }
135}
136
137impl IntoOption<String> for &String {
138    fn into_option(self) -> Option<String> {
139        Some(self.into())
140    }
141}
142
143impl IntoOption<String> for Box<str> {
144    fn into_option(self) -> Option<String> {
145        Some(self.into())
146    }
147}
148
149impl IntoOption<String> for Cow<'_, str> {
150    fn into_option(self) -> Option<String> {
151        Some(self.into())
152    }
153}
154
155impl IntoOption<String> for Arc<str> {
156    fn into_option(self) -> Option<String> {
157        Some(self.to_string())
158    }
159}
160
161impl<T: ?Sized + AsRef<OsStr>> IntoOption<PathBuf> for &T {
162    fn into_option(self) -> Option<PathBuf> {
163        Some(self.into())
164    }
165}
166
167impl IntoOption<PathBuf> for Box<Path> {
168    fn into_option(self) -> Option<PathBuf> {
169        Some(self.into())
170    }
171}
172
173impl IntoOption<PathBuf> for Cow<'_, Path> {
174    fn into_option(self) -> Option<PathBuf> {
175        Some(self.into())
176    }
177}
178
179impl IntoOption<serde_json::Value> for &str {
180    fn into_option(self) -> Option<serde_json::Value> {
181        Some(self.into())
182    }
183}
184
185impl IntoOption<serde_json::Value> for String {
186    fn into_option(self) -> Option<serde_json::Value> {
187        Some(self.into())
188    }
189}
190
191impl IntoOption<serde_json::Value> for Cow<'_, str> {
192    fn into_option(self) -> Option<serde_json::Value> {
193        Some(self.into())
194    }
195}