agent_client_protocol/
agent.rs1use std::{rc::Rc, sync::Arc};
2
3use agent_client_protocol_schema::{
4 AuthenticateRequest, AuthenticateResponse, CancelNotification, Error, ExtNotification,
5 ExtRequest, ExtResponse, InitializeRequest, InitializeResponse, LoadSessionRequest,
6 LoadSessionResponse, NewSessionRequest, NewSessionResponse, PromptRequest, PromptResponse,
7 Result, SetSessionModeRequest, SetSessionModeResponse,
8};
9#[cfg(feature = "unstable_session_fork")]
10use agent_client_protocol_schema::{ForkSessionRequest, ForkSessionResponse};
11#[cfg(feature = "unstable_session_list")]
12use agent_client_protocol_schema::{ListSessionsRequest, ListSessionsResponse};
13#[cfg(feature = "unstable_session_resume")]
14use agent_client_protocol_schema::{ResumeSessionRequest, ResumeSessionResponse};
15#[cfg(feature = "unstable_session_config_options")]
16use agent_client_protocol_schema::{SetSessionConfigOptionRequest, SetSessionConfigOptionResponse};
17#[cfg(feature = "unstable_session_model")]
18use agent_client_protocol_schema::{SetSessionModelRequest, SetSessionModelResponse};
19use serde_json::value::RawValue;
20
21#[async_trait::async_trait(?Send)]
26pub trait Agent {
27 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse>;
38
39 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse>;
49
50 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse>;
63
64 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse>;
76
77 async fn cancel(&self, args: CancelNotification) -> Result<()>;
89
90 async fn load_session(&self, _args: LoadSessionRequest) -> Result<LoadSessionResponse> {
101 Err(Error::method_not_found())
102 }
103
104 async fn set_session_mode(
118 &self,
119 _args: SetSessionModeRequest,
120 ) -> Result<SetSessionModeResponse> {
121 Err(Error::method_not_found())
122 }
123
124 #[cfg(feature = "unstable_session_model")]
130 async fn set_session_model(
131 &self,
132 _args: SetSessionModelRequest,
133 ) -> Result<SetSessionModelResponse> {
134 Err(Error::method_not_found())
135 }
136
137 #[cfg(feature = "unstable_session_config_options")]
149 async fn set_session_config_option(
150 &self,
151 _args: SetSessionConfigOptionRequest,
152 ) -> Result<SetSessionConfigOptionResponse> {
153 Err(Error::method_not_found())
154 }
155
156 #[cfg(feature = "unstable_session_list")]
164 async fn list_sessions(&self, _args: ListSessionsRequest) -> Result<ListSessionsResponse> {
165 Err(Error::method_not_found())
166 }
167
168 #[cfg(feature = "unstable_session_fork")]
176 async fn fork_session(&self, _args: ForkSessionRequest) -> Result<ForkSessionResponse> {
177 Err(Error::method_not_found())
178 }
179
180 #[cfg(feature = "unstable_session_resume")]
191 async fn resume_session(&self, _args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
192 Err(Error::method_not_found())
193 }
194
195 async fn ext_method(&self, _args: ExtRequest) -> Result<ExtResponse> {
202 Ok(ExtResponse::new(RawValue::NULL.to_owned().into()))
203 }
204
205 async fn ext_notification(&self, _args: ExtNotification) -> Result<()> {
212 Ok(())
213 }
214}
215
216#[async_trait::async_trait(?Send)]
217impl<T: Agent> Agent for Rc<T> {
218 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse> {
219 self.as_ref().initialize(args).await
220 }
221 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse> {
222 self.as_ref().authenticate(args).await
223 }
224 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse> {
225 self.as_ref().new_session(args).await
226 }
227 async fn load_session(&self, args: LoadSessionRequest) -> Result<LoadSessionResponse> {
228 self.as_ref().load_session(args).await
229 }
230 async fn set_session_mode(
231 &self,
232 args: SetSessionModeRequest,
233 ) -> Result<SetSessionModeResponse> {
234 self.as_ref().set_session_mode(args).await
235 }
236 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse> {
237 self.as_ref().prompt(args).await
238 }
239 async fn cancel(&self, args: CancelNotification) -> Result<()> {
240 self.as_ref().cancel(args).await
241 }
242 #[cfg(feature = "unstable_session_model")]
243 async fn set_session_model(
244 &self,
245 args: SetSessionModelRequest,
246 ) -> Result<SetSessionModelResponse> {
247 self.as_ref().set_session_model(args).await
248 }
249 #[cfg(feature = "unstable_session_config_options")]
250 async fn set_session_config_option(
251 &self,
252 args: SetSessionConfigOptionRequest,
253 ) -> Result<SetSessionConfigOptionResponse> {
254 self.as_ref().set_session_config_option(args).await
255 }
256 #[cfg(feature = "unstable_session_list")]
257 async fn list_sessions(&self, args: ListSessionsRequest) -> Result<ListSessionsResponse> {
258 self.as_ref().list_sessions(args).await
259 }
260 #[cfg(feature = "unstable_session_fork")]
261 async fn fork_session(&self, args: ForkSessionRequest) -> Result<ForkSessionResponse> {
262 self.as_ref().fork_session(args).await
263 }
264 #[cfg(feature = "unstable_session_resume")]
265 async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
266 self.as_ref().resume_session(args).await
267 }
268 async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
269 self.as_ref().ext_method(args).await
270 }
271 async fn ext_notification(&self, args: ExtNotification) -> Result<()> {
272 self.as_ref().ext_notification(args).await
273 }
274}
275
276#[async_trait::async_trait(?Send)]
277impl<T: Agent> Agent for Arc<T> {
278 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse> {
279 self.as_ref().initialize(args).await
280 }
281 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse> {
282 self.as_ref().authenticate(args).await
283 }
284 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse> {
285 self.as_ref().new_session(args).await
286 }
287 async fn load_session(&self, args: LoadSessionRequest) -> Result<LoadSessionResponse> {
288 self.as_ref().load_session(args).await
289 }
290 async fn set_session_mode(
291 &self,
292 args: SetSessionModeRequest,
293 ) -> Result<SetSessionModeResponse> {
294 self.as_ref().set_session_mode(args).await
295 }
296 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse> {
297 self.as_ref().prompt(args).await
298 }
299 async fn cancel(&self, args: CancelNotification) -> Result<()> {
300 self.as_ref().cancel(args).await
301 }
302 #[cfg(feature = "unstable_session_model")]
303 async fn set_session_model(
304 &self,
305 args: SetSessionModelRequest,
306 ) -> Result<SetSessionModelResponse> {
307 self.as_ref().set_session_model(args).await
308 }
309 #[cfg(feature = "unstable_session_config_options")]
310 async fn set_session_config_option(
311 &self,
312 args: SetSessionConfigOptionRequest,
313 ) -> Result<SetSessionConfigOptionResponse> {
314 self.as_ref().set_session_config_option(args).await
315 }
316 #[cfg(feature = "unstable_session_list")]
317 async fn list_sessions(&self, args: ListSessionsRequest) -> Result<ListSessionsResponse> {
318 self.as_ref().list_sessions(args).await
319 }
320 #[cfg(feature = "unstable_session_fork")]
321 async fn fork_session(&self, args: ForkSessionRequest) -> Result<ForkSessionResponse> {
322 self.as_ref().fork_session(args).await
323 }
324 #[cfg(feature = "unstable_session_resume")]
325 async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
326 self.as_ref().resume_session(args).await
327 }
328 async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
329 self.as_ref().ext_method(args).await
330 }
331 async fn ext_notification(&self, args: ExtNotification) -> Result<()> {
332 self.as_ref().ext_notification(args).await
333 }
334}