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, ListSessionsRequest,
6 ListSessionsResponse, LoadSessionRequest, LoadSessionResponse, NewSessionRequest,
7 NewSessionResponse, PromptRequest, PromptResponse, Result, SetSessionConfigOptionRequest,
8 SetSessionConfigOptionResponse, SetSessionModeRequest, SetSessionModeResponse,
9};
10#[cfg(feature = "unstable_session_close")]
11use agent_client_protocol_schema::{CloseSessionRequest, CloseSessionResponse};
12#[cfg(feature = "unstable_session_fork")]
13use agent_client_protocol_schema::{ForkSessionRequest, ForkSessionResponse};
14#[cfg(feature = "unstable_logout")]
15use agent_client_protocol_schema::{LogoutRequest, LogoutResponse};
16#[cfg(feature = "unstable_session_resume")]
17use agent_client_protocol_schema::{ResumeSessionRequest, ResumeSessionResponse};
18#[cfg(feature = "unstable_session_model")]
19use agent_client_protocol_schema::{SetSessionModelRequest, SetSessionModelResponse};
20use serde_json::value::RawValue;
21
22#[async_trait::async_trait(?Send)]
27pub trait Agent {
28 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse>;
39
40 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse>;
50
51 #[cfg(feature = "unstable_logout")]
62 async fn logout(&self, _args: LogoutRequest) -> Result<LogoutResponse> {
63 Err(Error::method_not_found())
64 }
65
66 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse>;
79
80 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse>;
92
93 async fn cancel(&self, args: CancelNotification) -> Result<()>;
105
106 async fn load_session(&self, _args: LoadSessionRequest) -> Result<LoadSessionResponse> {
117 Err(Error::method_not_found())
118 }
119
120 async fn set_session_mode(
134 &self,
135 _args: SetSessionModeRequest,
136 ) -> Result<SetSessionModeResponse> {
137 Err(Error::method_not_found())
138 }
139
140 #[cfg(feature = "unstable_session_model")]
146 async fn set_session_model(
147 &self,
148 _args: SetSessionModelRequest,
149 ) -> Result<SetSessionModelResponse> {
150 Err(Error::method_not_found())
151 }
152
153 async fn set_session_config_option(
161 &self,
162 _args: SetSessionConfigOptionRequest,
163 ) -> Result<SetSessionConfigOptionResponse> {
164 Err(Error::method_not_found())
165 }
166
167 async fn list_sessions(&self, _args: ListSessionsRequest) -> Result<ListSessionsResponse> {
175 Err(Error::method_not_found())
176 }
177
178 #[cfg(feature = "unstable_session_fork")]
186 async fn fork_session(&self, _args: ForkSessionRequest) -> Result<ForkSessionResponse> {
187 Err(Error::method_not_found())
188 }
189
190 #[cfg(feature = "unstable_session_resume")]
201 async fn resume_session(&self, _args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
202 Err(Error::method_not_found())
203 }
204
205 #[cfg(feature = "unstable_session_close")]
216 async fn close_session(&self, _args: CloseSessionRequest) -> Result<CloseSessionResponse> {
217 Err(Error::method_not_found())
218 }
219
220 async fn ext_method(&self, _args: ExtRequest) -> Result<ExtResponse> {
227 Ok(ExtResponse::new(RawValue::NULL.to_owned().into()))
228 }
229
230 async fn ext_notification(&self, _args: ExtNotification) -> Result<()> {
237 Ok(())
238 }
239}
240
241#[async_trait::async_trait(?Send)]
242impl<T: Agent> Agent for Rc<T> {
243 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse> {
244 self.as_ref().initialize(args).await
245 }
246 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse> {
247 self.as_ref().authenticate(args).await
248 }
249 #[cfg(feature = "unstable_logout")]
250 async fn logout(&self, args: LogoutRequest) -> Result<LogoutResponse> {
251 self.as_ref().logout(args).await
252 }
253 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse> {
254 self.as_ref().new_session(args).await
255 }
256 async fn load_session(&self, args: LoadSessionRequest) -> Result<LoadSessionResponse> {
257 self.as_ref().load_session(args).await
258 }
259 async fn set_session_mode(
260 &self,
261 args: SetSessionModeRequest,
262 ) -> Result<SetSessionModeResponse> {
263 self.as_ref().set_session_mode(args).await
264 }
265 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse> {
266 self.as_ref().prompt(args).await
267 }
268 async fn cancel(&self, args: CancelNotification) -> Result<()> {
269 self.as_ref().cancel(args).await
270 }
271 #[cfg(feature = "unstable_session_model")]
272 async fn set_session_model(
273 &self,
274 args: SetSessionModelRequest,
275 ) -> Result<SetSessionModelResponse> {
276 self.as_ref().set_session_model(args).await
277 }
278 async fn set_session_config_option(
279 &self,
280 args: SetSessionConfigOptionRequest,
281 ) -> Result<SetSessionConfigOptionResponse> {
282 self.as_ref().set_session_config_option(args).await
283 }
284
285 async fn list_sessions(&self, args: ListSessionsRequest) -> Result<ListSessionsResponse> {
286 self.as_ref().list_sessions(args).await
287 }
288 #[cfg(feature = "unstable_session_fork")]
289 async fn fork_session(&self, args: ForkSessionRequest) -> Result<ForkSessionResponse> {
290 self.as_ref().fork_session(args).await
291 }
292 #[cfg(feature = "unstable_session_resume")]
293 async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
294 self.as_ref().resume_session(args).await
295 }
296 #[cfg(feature = "unstable_session_close")]
297 async fn close_session(&self, args: CloseSessionRequest) -> Result<CloseSessionResponse> {
298 self.as_ref().close_session(args).await
299 }
300 async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
301 self.as_ref().ext_method(args).await
302 }
303 async fn ext_notification(&self, args: ExtNotification) -> Result<()> {
304 self.as_ref().ext_notification(args).await
305 }
306}
307
308#[async_trait::async_trait(?Send)]
309impl<T: Agent> Agent for Arc<T> {
310 async fn initialize(&self, args: InitializeRequest) -> Result<InitializeResponse> {
311 self.as_ref().initialize(args).await
312 }
313 async fn authenticate(&self, args: AuthenticateRequest) -> Result<AuthenticateResponse> {
314 self.as_ref().authenticate(args).await
315 }
316 #[cfg(feature = "unstable_logout")]
317 async fn logout(&self, args: LogoutRequest) -> Result<LogoutResponse> {
318 self.as_ref().logout(args).await
319 }
320 async fn new_session(&self, args: NewSessionRequest) -> Result<NewSessionResponse> {
321 self.as_ref().new_session(args).await
322 }
323 async fn load_session(&self, args: LoadSessionRequest) -> Result<LoadSessionResponse> {
324 self.as_ref().load_session(args).await
325 }
326 async fn set_session_mode(
327 &self,
328 args: SetSessionModeRequest,
329 ) -> Result<SetSessionModeResponse> {
330 self.as_ref().set_session_mode(args).await
331 }
332 async fn prompt(&self, args: PromptRequest) -> Result<PromptResponse> {
333 self.as_ref().prompt(args).await
334 }
335 async fn cancel(&self, args: CancelNotification) -> Result<()> {
336 self.as_ref().cancel(args).await
337 }
338 #[cfg(feature = "unstable_session_model")]
339 async fn set_session_model(
340 &self,
341 args: SetSessionModelRequest,
342 ) -> Result<SetSessionModelResponse> {
343 self.as_ref().set_session_model(args).await
344 }
345 async fn set_session_config_option(
346 &self,
347 args: SetSessionConfigOptionRequest,
348 ) -> Result<SetSessionConfigOptionResponse> {
349 self.as_ref().set_session_config_option(args).await
350 }
351 async fn list_sessions(&self, args: ListSessionsRequest) -> Result<ListSessionsResponse> {
352 self.as_ref().list_sessions(args).await
353 }
354 #[cfg(feature = "unstable_session_fork")]
355 async fn fork_session(&self, args: ForkSessionRequest) -> Result<ForkSessionResponse> {
356 self.as_ref().fork_session(args).await
357 }
358 #[cfg(feature = "unstable_session_resume")]
359 async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
360 self.as_ref().resume_session(args).await
361 }
362 #[cfg(feature = "unstable_session_close")]
363 async fn close_session(&self, args: CloseSessionRequest) -> Result<CloseSessionResponse> {
364 self.as_ref().close_session(args).await
365 }
366 async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
367 self.as_ref().ext_method(args).await
368 }
369 async fn ext_notification(&self, args: ExtNotification) -> Result<()> {
370 self.as_ref().ext_notification(args).await
371 }
372}