1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//! The protocol-v2 connection type, `agent_client_protocol::v2::ConnectionTo`.
use super::ConnectionTo as RawConnectionTo;
use super::{
Builder, ConnectTo, ConnectionTermination, Dispatch, DynamicHandlerGuard,
HandleConnectionClose, HandleDispatchFrom, JsonRpcNotification, JsonRpcRequest,
PreparedRequest, Protocol, SentRequest, V2, protocol_compat, run::RunWithConnectionTo,
};
use crate::Agent;
use crate::role::{HasPeer, Role};
use crate::v2::InitializeOutcome;
/// A protocol-v2 connection context.
///
/// Values of this type are supplied to callbacks registered on a
/// [`v2::Builder`](crate::v2::Builder). It exposes the general connection operations that are valid
/// for protocol v2 while keeping version-specific high-level helpers for other
/// protocol versions out of the typed context. The generic JSON-RPC send
/// methods remain intentionally schema-agnostic.
///
/// This is a thin, cheaply cloneable handle to the underlying JSON-RPC
/// connection. It intentionally does not implement [`Deref`](std::ops::Deref)
/// to the version-neutral [`ConnectionTo`](crate::ConnectionTo); convert
/// explicitly with [`into_raw`](Self::into_raw) and
/// [`ConnectionTo::into_protocol`](crate::ConnectionTo::into_protocol).
#[derive(Clone, Debug)]
pub struct ConnectionTo<Counterpart: Role> {
pub(crate) inner: RawConnectionTo<Counterpart>,
}
impl ConnectionTo<Agent> {
/// Send the client's `initialize` request and report how the agent
/// negotiated.
///
/// Unlike [`send_request`](Self::send_request), a v1-only agent's valid v1
/// response resolves successfully as [`InitializeOutcome::LowerVersion`]
/// with the agent's typed response, so the caller can decide whether to
/// fall back to a v1 client implementation. The connection stays
/// uninitialized in that case; the SDK never converts it to v1.
///
/// The request still resolves with an error for errors returned by the
/// agent (including the `-32022` version mismatch), malformed responses,
/// and transport failures.
///
/// After an initialization completes, `initialize` may be sent again
/// before any other request; the agent treats the latest one as
/// authoritative. Once another request has been sent, a repeat resolves
/// with an `-32600` error without being sent.
///
/// ```no_run
/// # use agent_client_protocol::{schema::ProtocolVersion, v2::schema};
/// # use agent_client_protocol::{Client, ConnectTo};
/// use agent_client_protocol::SelectedInitializeResponse;
/// use agent_client_protocol::v2::InitializeOutcome;
/// # async fn example(agent: impl ConnectTo<Client>) -> agent_client_protocol::Result<()> {
/// let outcome = Client
/// .v2()
/// .connect_with(agent, async |cx| {
/// let request = schema::InitializeRequest::new(
/// ProtocolVersion::V2,
/// schema::Implementation::new("example", "0.1.0"),
/// );
/// match cx.send_initialize(request).block_task().await? {
/// InitializeOutcome::Initialized(_response) => {
/// // Continue with v2 traffic.
/// Ok(None)
/// }
/// InitializeOutcome::LowerVersion(mismatch) => Ok(Some(mismatch)),
/// _ => Err(agent_client_protocol::Error::internal_error().data("unexpected outcome")),
/// }
/// })
/// .await?;
/// if let Some(mismatch) = outcome
/// && let SelectedInitializeResponse::V1(_v1_response) = mismatch.response
/// {
/// // Reconnect with a v1 client implementation.
/// }
/// # Ok(())
/// # }
/// ```
pub fn send_initialize(
&self,
request: crate::v2::schema::InitializeRequest,
) -> SentRequest<InitializeOutcome, V2> {
let mut prepared = self.inner.prepare_request_to_with_options::<V2, _, _>(
self.inner.counterpart(),
protocol_compat::InitializeNegotiation(request),
None,
None,
);
prepared
.publication
.pending_reply
.accepts_lower_initialize_version = true;
prepared.into_sent_request(false)
}
}
impl<Counterpart: Role> From<ConnectionTo<Counterpart>> for RawConnectionTo<Counterpart> {
/// The same as [`ConnectionTo::into_raw`]. Generic code over
/// [`Protocol`] uses it to turn a `P::Connection<Counterpart>` back into
/// the version-neutral connection.
fn from(connection: ConnectionTo<Counterpart>) -> Self {
connection.into_raw()
}
}
impl<Counterpart: Role> ConnectionTo<Counterpart> {
/// Access the underlying version-neutral connection inside the SDK.
pub(crate) fn raw_connection(&self) -> &RawConnectionTo<Counterpart> {
&self.inner
}
/// The underlying version-neutral connection.
///
/// Requests sent through it return [`Raw`](crate::Raw) handles. Convert
/// back with [`ConnectionTo::into_protocol`](crate::ConnectionTo::into_protocol).
#[must_use]
pub fn into_raw(self) -> RawConnectionTo<Counterpart> {
self.inner
}
/// Return the counterpart role this connection is talking to.
pub fn counterpart(&self) -> Counterpart {
self.inner.counterpart()
}
/// Wait until the incoming transport reaches clean EOF.
pub async fn incoming_closed(&self) {
self.inner.incoming_closed().await;
}
/// Return whether clean incoming-EOF processing has completed.
#[must_use]
pub fn is_incoming_closed(&self) -> bool {
self.inner.is_incoming_closed()
}
/// Wait until the connection driver has terminated, for any reason.
///
/// See [`ConnectionTo::terminated`](crate::ConnectionTo::terminated).
pub async fn terminated(&self) -> ConnectionTermination {
self.inner.terminated().await
}
/// Return the termination reason if the connection driver has terminated.
#[must_use]
pub fn termination(&self) -> Option<ConnectionTermination> {
self.inner.termination()
}
/// Spawn a task that runs for as long as the JSON-RPC connection is served.
#[track_caller]
pub fn spawn(
&self,
task: impl IntoFuture<Output = Result<(), crate::Error>, IntoFuture: Send + 'static>,
) -> Result<(), crate::Error> {
self.inner.spawn(task)
}
/// Spawn a JSON-RPC connection in the background.
///
/// The returned connection context is selected by `builder`; spawning a
/// [`v2::Builder`](crate::v2::Builder) therefore returns another
/// [`v2::ConnectionTo`](ConnectionTo).
///
/// ```no_run
/// # use agent_client_protocol::{
/// # Agent, Client, ConnectTo, Error,
/// # };
/// # use agent_client_protocol::v2::ConnectionTo;
/// # fn example(
/// # connection: ConnectionTo<Agent>,
/// # transport: impl ConnectTo<Client> + 'static,
/// # ) -> Result<(), Error> {
/// let child: ConnectionTo<Agent> =
/// connection.spawn_connection(Client.v2(), transport)?;
/// # drop(child);
/// # Ok(())
/// # }
/// ```
#[track_caller]
pub fn spawn_connection<R: Role, P: Protocol>(
&self,
builder: Builder<
R,
impl HandleDispatchFrom<R::Counterpart> + 'static,
impl RunWithConnectionTo<R::Counterpart> + 'static,
impl HandleConnectionClose<R::Counterpart> + 'static,
P,
>,
transport: impl ConnectTo<R> + 'static,
) -> Result<P::Connection<R::Counterpart>, crate::Error> {
self.inner.spawn_connection(builder, transport)
}
/// Send a request or notification and forward its response appropriately.
pub fn send_proxied_message<
Req: JsonRpcRequest<Response: Send>,
Notif: JsonRpcNotification,
Q: Protocol,
>(
&self,
message: Dispatch<Req, Notif, Q>,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Counterpart>,
{
self.inner.send_proxied_message(message)
}
/// Send a request or notification to a specific peer and forward its
/// response appropriately.
pub fn send_proxied_message_to<
Peer: Role,
Req: JsonRpcRequest<Response: Send>,
Notif: JsonRpcNotification,
Q: Protocol,
>(
&self,
peer: Peer,
message: Dispatch<Req, Notif, Q>,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Peer>,
{
self.inner.send_proxied_message_to(peer, message)
}
/// Send an outgoing request to the default counterpart peer.
pub fn send_request<Req: JsonRpcRequest>(&self, request: Req) -> SentRequest<Req::Response, V2>
where
Counterpart: HasPeer<Counterpart>,
{
self.send_request_to(self.inner.counterpart(), request)
}
/// Send an outgoing request to a specific peer.
pub fn send_request_to<Peer: Role, Req: JsonRpcRequest>(
&self,
peer: Peer,
request: Req,
) -> SentRequest<Req::Response, V2>
where
Counterpart: HasPeer<Peer>,
{
self.inner
.send_request_to_with_options(peer, request, false, None, None)
}
/// Prepare a request without sending it until response handling is selected.
///
/// See [`ConnectionTo::prepare_request`](crate::ConnectionTo::prepare_request) for publication and ordering semantics.
pub fn prepare_request<Req: JsonRpcRequest>(
&self,
request: Req,
) -> PreparedRequest<Req::Response, V2>
where
Counterpart: HasPeer<Counterpart>,
{
self.prepare_request_to(self.inner.counterpart(), request)
}
/// Prepare a request to a specific peer without sending it.
///
/// See [`ConnectionTo::prepare_request_to`](crate::ConnectionTo::prepare_request_to) for publication and ordering semantics.
pub fn prepare_request_to<Peer: Role, Req: JsonRpcRequest>(
&self,
peer: Peer,
request: Req,
) -> PreparedRequest<Req::Response, V2>
where
Counterpart: HasPeer<Peer>,
{
self.inner
.prepare_request_to_with_options(peer, request, None, None)
}
/// Send an outgoing notification to the default counterpart peer.
pub fn send_notification<N: JsonRpcNotification>(
&self,
notification: N,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Counterpart>,
{
self.inner.send_notification(notification)
}
/// Send an outgoing notification to a specific peer.
pub fn send_notification_to<Peer: Role, N: JsonRpcNotification>(
&self,
peer: Peer,
notification: N,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Peer>,
{
self.inner.send_notification_to(peer, notification)
}
/// Send a `$/cancel_request` notification to the default counterpart peer.
///
/// Prefer [`SentRequest::cancel`](crate::SentRequest::cancel) when you
/// have the request handle. `request_id` is a v2 request ID, such as the
/// [`id`](crate::SentRequest::id) of a request sent through this
/// connection. The raw [`ConnectionTo::send_cancel_request`](crate::ConnectionTo::send_cancel_request)
/// takes any [`RawJsonRpcId`](crate::RawJsonRpcId).
pub fn send_cancel_request(
&self,
request_id: impl Into<crate::v2::schema::RequestId>,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Counterpart>,
{
self.inner.send_cancel_request(request_id.into())
}
/// Send a `$/cancel_request` notification to a specific peer.
///
/// The notification is always sent plain, never wrapped in
/// `proxy/successor`, as the raw
/// [`ConnectionTo::send_cancel_request_to`](crate::ConnectionTo::send_cancel_request_to)
/// describes. See [`send_cancel_request`](Self::send_cancel_request).
pub fn send_cancel_request_to<Peer: Role>(
&self,
peer: Peer,
request_id: impl Into<crate::v2::schema::RequestId>,
) -> Result<(), crate::Error>
where
Counterpart: HasPeer<Peer>,
{
self.inner.send_cancel_request_to(peer, request_id.into())
}
/// Register a low-level dynamic message handler.
///
/// Dynamic handlers use the version-neutral [`HandleDispatchFrom`] trait,
/// so their callback receives the underlying version-neutral
/// [`ConnectionTo`](crate::ConnectionTo). Prefer typed handlers on
/// [`v2::Builder`](crate::v2::Builder) when registration can happen before the
/// connection starts.
///
/// ```no_run
/// # use agent_client_protocol::{
/// # Agent, Client, ConnectTo, DynamicHandlerGuard, Error, NullHandler,
/// # };
/// # async fn example(
/// # transport: impl ConnectTo<Client> + 'static,
/// # ) -> Result<(), Error> {
/// Client.v2().connect_with(transport, async |connection| {
/// let guard: DynamicHandlerGuard<Agent> =
/// connection.add_dynamic_handler(NullHandler)?;
///
/// // Keep `guard` alive for as long as the modal handler is needed.
/// drop(guard);
/// Ok(())
/// }).await
/// # }
/// ```
pub fn add_dynamic_handler(
&self,
handler: impl HandleDispatchFrom<Counterpart> + 'static,
) -> Result<DynamicHandlerGuard<Counterpart>, crate::Error> {
self.inner.add_dynamic_handler(handler)
}
}