connection.rs

 1use agent_client_protocol as acp;
 2use agentic_coding_protocol::{self as acp_old, AgentRequest};
 3use anyhow::Result;
 4use futures::future::{FutureExt as _, LocalBoxFuture};
 5
 6pub trait AgentConnection {
 7    fn new_session(
 8        &self,
 9        params: acp::NewSessionToolArguments,
10    ) -> LocalBoxFuture<'static, Result<acp::SessionId>>;
11
12    fn authenticate(&self) -> LocalBoxFuture<'static, Result<()>>;
13
14    fn prompt(&self, params: acp::PromptToolArguments) -> LocalBoxFuture<'static, Result<()>>;
15
16    fn cancel(&self) -> LocalBoxFuture<'static, Result<()>>;
17}
18
19impl AgentConnection for acp_old::AgentConnection {
20    fn new_session(
21        &self,
22        _params: acp::NewSessionToolArguments,
23    ) -> LocalBoxFuture<'static, Result<acp::SessionId>> {
24        let task = self.request_any(
25            acp_old::InitializeParams {
26                protocol_version: acp_old::ProtocolVersion::latest(),
27            }
28            .into_any(),
29        );
30        async move {
31            let result = task.await?;
32            let result = acp_old::InitializeParams::response_from_any(result)?;
33
34            if !result.is_authenticated {
35                anyhow::bail!("Not authenticated");
36            }
37
38            Ok(acp::SessionId("acp-old-no-id".into()))
39        }
40        .boxed_local()
41    }
42
43    fn authenticate(&self) -> LocalBoxFuture<'static, Result<()>> {
44        let task = self.request_any(acp_old::AuthenticateParams.into_any());
45        async move {
46            task.await?;
47            anyhow::Ok(())
48        }
49        .boxed_local()
50    }
51
52    fn prompt(&self, params: acp::PromptToolArguments) -> LocalBoxFuture<'static, Result<()>> {
53        let chunks = params
54            .prompt
55            .into_iter()
56            .filter_map(|block| match block {
57                acp::ContentBlock::Text(text) => {
58                    Some(acp_old::UserMessageChunk::Text { text: text.text })
59                }
60                acp::ContentBlock::ResourceLink(link) => Some(acp_old::UserMessageChunk::Path {
61                    path: link.uri.into(),
62                }),
63                _ => None,
64            })
65            .collect();
66
67        let task = self.request_any(acp_old::SendUserMessageParams { chunks }.into_any());
68        async move {
69            task.await?;
70            anyhow::Ok(())
71        }
72        .boxed_local()
73    }
74
75    fn cancel(&self) -> LocalBoxFuture<'static, Result<()>> {
76        let task = self.request_any(acp_old::CancelSendMessageParams.into_any());
77        async move {
78            task.await?;
79            anyhow::Ok(())
80        }
81        .boxed_local()
82    }
83}