Skip to content

Commit ca3d3d6

Browse files
authored
feat(unstable): Add support for session/close methods (#77)
1 parent 67ce48d commit ca3d3d6

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tokio = { version = "1.48", features = ["full"] }
1717
tokio-util = { version = "0.7", features = ["compat"] }
1818

1919
# Protocol
20-
agent-client-protocol-schema = { version = "=0.11.1" }
20+
agent-client-protocol-schema = { version = "=0.11.2" }
2121

2222
# Serialization
2323
serde = { version = "1.0", features = ["derive", "rc"] }

src/agent-client-protocol/src/agent.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use agent_client_protocol_schema::{
77
NewSessionResponse, PromptRequest, PromptResponse, Result, SetSessionConfigOptionRequest,
88
SetSessionConfigOptionResponse, SetSessionModeRequest, SetSessionModeResponse,
99
};
10+
#[cfg(feature = "unstable_session_close")]
11+
use agent_client_protocol_schema::{CloseSessionRequest, CloseSessionResponse};
1012
#[cfg(feature = "unstable_session_fork")]
1113
use agent_client_protocol_schema::{ForkSessionRequest, ForkSessionResponse};
1214
#[cfg(feature = "unstable_session_resume")]
@@ -183,6 +185,21 @@ pub trait Agent {
183185
Err(Error::method_not_found())
184186
}
185187

188+
/// **UNSTABLE**
189+
///
190+
/// This capability is not part of the spec yet, and may be removed or changed at any point.
191+
///
192+
/// Closes an active session, freeing up any resources associated with it.
193+
///
194+
/// The agent must cancel any ongoing work (as if `session/cancel` was called)
195+
/// and then free up any resources associated with the session.
196+
///
197+
/// Only available if the Agent supports the `session.close` capability.
198+
#[cfg(feature = "unstable_session_close")]
199+
async fn close_session(&self, _args: CloseSessionRequest) -> Result<CloseSessionResponse> {
200+
Err(Error::method_not_found())
201+
}
202+
186203
/// Handles extension method requests from the client.
187204
///
188205
/// Extension methods provide a way to add custom functionality while maintaining
@@ -255,6 +272,10 @@ impl<T: Agent> Agent for Rc<T> {
255272
async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
256273
self.as_ref().resume_session(args).await
257274
}
275+
#[cfg(feature = "unstable_session_close")]
276+
async fn close_session(&self, args: CloseSessionRequest) -> Result<CloseSessionResponse> {
277+
self.as_ref().close_session(args).await
278+
}
258279
async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
259280
self.as_ref().ext_method(args).await
260281
}
@@ -313,6 +334,10 @@ impl<T: Agent> Agent for Arc<T> {
313334
async fn resume_session(&self, args: ResumeSessionRequest) -> Result<ResumeSessionResponse> {
314335
self.as_ref().resume_session(args).await
315336
}
337+
#[cfg(feature = "unstable_session_close")]
338+
async fn close_session(&self, args: CloseSessionRequest) -> Result<CloseSessionResponse> {
339+
self.as_ref().close_session(args).await
340+
}
316341
async fn ext_method(&self, args: ExtRequest) -> Result<ExtResponse> {
317342
self.as_ref().ext_method(args).await
318343
}

src/agent-client-protocol/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ impl Agent for ClientSideConnection {
184184
.await
185185
}
186186

187+
#[cfg(feature = "unstable_session_close")]
188+
async fn close_session(&self, args: CloseSessionRequest) -> Result<CloseSessionResponse> {
189+
self.conn
190+
.request::<Option<_>>(
191+
AGENT_METHOD_NAMES.session_close,
192+
Some(ClientRequest::CloseSessionRequest(args)),
193+
)
194+
.await
195+
.map(Option::unwrap_or_default)
196+
}
197+
187198
async fn set_session_config_option(
188199
&self,
189200
args: SetSessionConfigOptionRequest,
@@ -567,6 +578,10 @@ impl Side for AgentSide {
567578
m if m == AGENT_METHOD_NAMES.session_resume => serde_json::from_str(params.get())
568579
.map(ClientRequest::ResumeSessionRequest)
569580
.map_err(Into::into),
581+
#[cfg(feature = "unstable_session_close")]
582+
m if m == AGENT_METHOD_NAMES.session_close => serde_json::from_str(params.get())
583+
.map(ClientRequest::CloseSessionRequest)
584+
.map_err(Into::into),
570585
m if m == AGENT_METHOD_NAMES.session_set_config_option => {
571586
serde_json::from_str(params.get())
572587
.map(ClientRequest::SetSessionConfigOptionRequest)
@@ -655,6 +670,11 @@ impl<T: Agent> MessageHandler<AgentSide> for T {
655670
let response = self.resume_session(args).await?;
656671
Ok(AgentResponse::ResumeSessionResponse(response))
657672
}
673+
#[cfg(feature = "unstable_session_close")]
674+
ClientRequest::CloseSessionRequest(args) => {
675+
let response = self.close_session(args).await?;
676+
Ok(AgentResponse::CloseSessionResponse(response))
677+
}
658678
ClientRequest::SetSessionConfigOptionRequest(args) => {
659679
let response = self.set_session_config_option(args).await?;
660680
Ok(AgentResponse::SetSessionConfigOptionResponse(response))

0 commit comments

Comments
 (0)