Skip to content

Commit 09263a9

Browse files
agu-zmaxbrunsfeldConradIrwinnathansobo
committed
Pass spawn in
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com> Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com> Co-authored-by: Nathan Sobo <nathan@zed.dev>
1 parent ada2b29 commit 09263a9

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

rust/acp.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl AgentConnection {
4040
handler: H,
4141
outgoing_bytes: impl Unpin + AsyncWrite,
4242
incoming_bytes: impl Unpin + AsyncRead,
43+
spawn: impl Fn(LocalBoxFuture<'static, ()>),
4344
) -> (
4445
Self,
4546
impl Future<Output = ()>,
@@ -53,6 +54,7 @@ impl AgentConnection {
5354
}),
5455
outgoing_bytes,
5556
incoming_bytes,
57+
spawn,
5658
);
5759
(Self(connection), handler_task, io_task)
5860
}
@@ -81,6 +83,7 @@ impl ClientConnection {
8183
handler: H,
8284
outgoing_bytes: impl Unpin + AsyncWrite,
8385
incoming_bytes: impl Unpin + AsyncRead,
86+
spawn: impl Fn(LocalBoxFuture<'static, ()>),
8487
) -> (
8588
Self,
8689
impl Future<Output = ()>,
@@ -94,6 +97,7 @@ impl ClientConnection {
9497
}),
9598
outgoing_bytes,
9699
incoming_bytes,
100+
spawn,
97101
);
98102
(Self(connection), handler_task, io_task)
99103
}
@@ -189,6 +193,7 @@ where
189193
request_handler: Box<dyn 'static + Fn(In) -> LocalBoxFuture<'static, Result<In::Response>>>,
190194
outgoing_bytes: impl Unpin + AsyncWrite,
191195
incoming_bytes: impl Unpin + AsyncRead,
196+
spawn: impl Fn(LocalBoxFuture<'static, ()>),
192197
) -> (
193198
Self,
194199
impl Future<Output = ()>,
@@ -201,7 +206,7 @@ where
201206
outgoing_tx: outgoing_tx.clone(),
202207
next_id: AtomicI32::new(0),
203208
};
204-
let handler_task = Self::handle_incoming(outgoing_tx, incoming_rx, request_handler);
209+
let handler_task = Self::handle_incoming(outgoing_tx, incoming_rx, request_handler, spawn);
205210
let io_task = Self::handle_io(
206211
outgoing_rx,
207212
incoming_tx,
@@ -309,27 +314,35 @@ where
309314
incoming_handler: Box<
310315
dyn 'static + Fn(In) -> LocalBoxFuture<'static, Result<In::Response>>,
311316
>,
317+
spawn: impl Fn(LocalBoxFuture<'static, ()>),
312318
) {
313319
while let Some((id, params)) = incoming_rx.next().await {
314-
let result = incoming_handler(params).await;
315-
match result {
316-
Ok(result) => {
317-
outgoing_tx
318-
.unbounded_send(OutgoingMessage::OkResponse { id, result })
319-
.ok();
320-
}
321-
Err(error) => {
322-
outgoing_tx
323-
.unbounded_send(OutgoingMessage::ErrorResponse {
324-
id,
325-
error: Error {
326-
code: -32603,
327-
message: error.to_string(),
328-
},
329-
})
330-
.ok();
320+
let result = incoming_handler(params);
321+
let outgoing_tx = outgoing_tx.clone();
322+
spawn(
323+
async move {
324+
let result = result.await;
325+
match result {
326+
Ok(result) => {
327+
outgoing_tx
328+
.unbounded_send(OutgoingMessage::OkResponse { id, result })
329+
.ok();
330+
}
331+
Err(error) => {
332+
outgoing_tx
333+
.unbounded_send(OutgoingMessage::ErrorResponse {
334+
id,
335+
error: Error {
336+
code: -32603,
337+
message: error.to_string(),
338+
},
339+
})
340+
.ok();
341+
}
342+
}
331343
}
332-
}
344+
.boxed_local(),
345+
)
333346
}
334347
}
335348
}

rust/acp_tests.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,23 @@ async fn test_client_agent_communication() {
6767
let (agent_to_client_tx, agent_to_client_rx) = async_pipe::pipe();
6868

6969
let (client_connection, client_handle_task, client_io_task) =
70-
AgentConnection::connect_to_agent(client, client_to_agent_tx, agent_to_client_rx);
70+
AgentConnection::connect_to_agent(
71+
client,
72+
client_to_agent_tx,
73+
agent_to_client_rx,
74+
|fut| {
75+
tokio::task::spawn_local(fut);
76+
},
77+
);
7178
let (agent_connection, agent_handle_task, agent_io_task) =
72-
ClientConnection::connect_to_client(agent, agent_to_client_tx, client_to_agent_rx);
79+
ClientConnection::connect_to_client(
80+
agent,
81+
agent_to_client_tx,
82+
client_to_agent_rx,
83+
|fut| {
84+
tokio::task::spawn_local(fut);
85+
},
86+
);
7387

7488
let _task = tokio::task::spawn_local(client_handle_task);
7589
let _task = tokio::task::spawn_local(agent_handle_task);

rust/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub struct Method {
1616
pub response_payload: bool,
1717
}
1818

19-
pub trait AnyRequest: Serialize + Sized {
20-
type Response: Serialize;
19+
pub trait AnyRequest: Serialize + Sized + 'static {
20+
type Response: Serialize + 'static;
2121
fn from_method_and_params(method: &str, params: &RawValue) -> Result<Self>;
2222
fn response_from_method_and_result(method: &str, params: &RawValue) -> Result<Self::Response>;
2323
}

0 commit comments

Comments
 (0)