foxfire-acp 0.1.6

Agent Client Protocol (ACP) client utilities and Aether protocol extensions
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! In-memory test harness for ACP connections.

mod fake_agent;
pub use fake_agent::{FakeAgent, FakeAgentRequests, HeldRequests, Question, QueuedTurns, SessionTools};

use crate::aether::{
    AetherMeta, GitDiffEventPayload, McpNotification, PromptSearchResponse, PromptSearchResult, SessionPreviewResponse,
    SessionPreviewRole, SessionPreviewTurn, SubAgentMeta,
};
use crate::aether::{ConfigOptionMeta, SelectOptionMeta};
use crate::artifact::comment::{Anchor, ArtifactComment, Target};
use crate::artifact::{Artifact, ArtifactContent, ArtifactPayload, ArtifactSlot};
use crate::client::AcpEvent;
pub use agent_client_protocol::Channel;
use agent_client_protocol::schema::{IntoOption, ProtocolVersion};
use agent_client_protocol::v2::schema::{
    AuthStatusUpdateNotification, CompleteElicitationNotification, ContentBlock, ContentChunk,
    CreateElicitationRequest, CreateElicitationResponse, ElicitationFormMode, ElicitationSchema,
    ElicitationSessionScope, IdleStateUpdate, Implementation, InitializeRequest, InitializeResponse, PlanEntry, PlanId,
    PlanUpdate, PlanUpdateContent, RunningStateUpdate, SessionConfigOption, SessionConfigOptionCategory,
    SessionConfigSelectGroup, SessionConfigSelectOption, SessionConfigSelectOptions, SessionId, SessionUpdate,
    StateUpdate, StopReason, SubagentUpdate, ToolCallStatus, ToolCallUpdate, UpdateSessionNotification, UserMessage,
};
use agent_client_protocol::{
    self as acp, Agent, Client, ConnectionTo, HandleConnectionClose, HandleDispatchFrom, NullRun, Responder,
    RunWithConnectionTo, v2,
};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use tokio::sync::{mpsc, oneshot};
use tokio::task::{LocalSet, spawn_local};

pub struct TestPeer {
    session_notifications: mpsc::UnboundedReceiver<UpdateSessionNotification>,
    mcp_notifications: mpsc::UnboundedReceiver<McpNotification>,
    git_diff_notifications: mpsc::UnboundedReceiver<GitDiffEventPayload>,
    auth_status_updates: mpsc::UnboundedReceiver<AuthStatusUpdateNotification>,
    elicitation_requests: mpsc::UnboundedReceiver<CreateElicitationRequest>,
    elicitation_completions: mpsc::UnboundedReceiver<CompleteElicitationNotification>,
    elicitation_responses: Arc<Mutex<VecDeque<CreateElicitationResponse>>>,
    responder_capture: Arc<Mutex<Option<oneshot::Sender<Responder<CreateElicitationResponse>>>>>,
}

impl TestPeer {
    pub fn new() -> (Self, v2::Builder<Client, impl HandleDispatchFrom<Agent>, NullRun>) {
        let (session_tx, session_notifications) = mpsc::unbounded_channel();
        let (mcp_tx, mcp_notifications) = mpsc::unbounded_channel();
        let (git_diff_tx, git_diff_notifications) = mpsc::unbounded_channel();
        let (auth_tx, auth_status_updates) = mpsc::unbounded_channel();
        let (elicitation_tx, elicitation_requests) = mpsc::unbounded_channel();
        let (completion_tx, elicitation_completions) = mpsc::unbounded_channel();
        let elicitation_responses = Arc::new(Mutex::new(VecDeque::new()));
        let responder_capture: Arc<Mutex<Option<oneshot::Sender<Responder<CreateElicitationResponse>>>>> =
            Arc::new(Mutex::new(None));

        let builder = Client
            .v2()
            .name("test-client")
            .on_receive_notification(
                async move |n: UpdateSessionNotification, _cx| forward(&session_tx, n),
                acp::on_receive_notification!(),
            )
            .on_receive_notification(
                async move |n: McpNotification, _cx| forward(&mcp_tx, n),
                acp::on_receive_notification!(),
            )
            .on_receive_notification(
                async move |n: GitDiffEventPayload, _cx| forward(&git_diff_tx, n),
                acp::on_receive_notification!(),
            )
            .on_receive_notification(
                async move |n: AuthStatusUpdateNotification, _cx| forward(&auth_tx, n),
                acp::on_receive_notification!(),
            )
            .on_receive_notification(
                async move |n: CompleteElicitationNotification, _cx| forward(&completion_tx, n),
                acp::on_receive_notification!(),
            )
            .on_receive_request(
                {
                    let responses = elicitation_responses.clone();
                    let capture = responder_capture.clone();
                    async move |req: CreateElicitationRequest,
                                responder: v2::Responder<CreateElicitationResponse>,
                                _cx| {
                        if let Some(capture_tx) = capture.lock().unwrap().take() {
                            let _ = capture_tx.send(responder.into_raw());
                            return Ok(());
                        }
                        let _ = elicitation_tx.send(req);
                        let queued = responses.lock().unwrap().pop_front();
                        match queued {
                            Some(response) => responder.respond(response),
                            None => responder.respond_with_error(acp::Error::method_not_found()),
                        }
                    }
                },
                acp::on_receive_request!(),
            );

        let peer = Self {
            session_notifications,
            mcp_notifications,
            git_diff_notifications,
            auth_status_updates,
            elicitation_requests,
            elicitation_completions,
            elicitation_responses,
            responder_capture,
        };
        (peer, builder)
    }

    pub async fn next_session_notification(&mut self) -> UpdateSessionNotification {
        self.session_notifications.recv().await.expect("peer channel closed")
    }

    pub async fn next_mcp_notification(&mut self) -> McpNotification {
        self.mcp_notifications.recv().await.expect("peer channel closed")
    }

    pub async fn next_git_diff_notification(&mut self) -> GitDiffEventPayload {
        self.git_diff_notifications.recv().await.expect("peer channel closed")
    }

    pub async fn next_auth_status_update(&mut self) -> AuthStatusUpdateNotification {
        self.auth_status_updates.recv().await.expect("peer channel closed")
    }

    pub async fn next_elicitation_request(&mut self) -> CreateElicitationRequest {
        self.elicitation_requests.recv().await.expect("peer channel closed")
    }

    pub async fn next_elicitation_completion(&mut self) -> CompleteElicitationNotification {
        self.elicitation_completions.recv().await.expect("peer channel closed")
    }

    pub fn queue_elicitation_response(&self, response: CreateElicitationResponse) {
        self.elicitation_responses.lock().unwrap().push_back(response);
    }

    pub fn capture_next_elicitation(&self) -> oneshot::Receiver<Responder<CreateElicitationResponse>> {
        let (sender, receiver) = oneshot::channel();
        *self.responder_capture.lock().unwrap() = Some(sender);
        receiver
    }

    pub async fn fake_elicitation(
        &mut self,
        cx: &ConnectionTo<Client>,
    ) -> (Responder<CreateElicitationResponse>, oneshot::Receiver<CreateElicitationResponse>) {
        let responder_rx = self.capture_next_elicitation();

        let (response_tx, response_rx) = oneshot::channel::<CreateElicitationResponse>();
        let cx = cx.clone();
        spawn_local(async move {
            if let Ok(resp) = cx.send_request(placeholder_params()).block_task().await {
                let _ = response_tx.send(resp);
            }
        });

        let responder = responder_rx.await.expect("client handler must capture responder");
        (responder, response_rx)
    }
}

pub fn texts(conversation: &crate::conversation::Conversation) -> Vec<String> {
    conversation.items().iter().filter_map(|item| item.text().map(std::borrow::Cow::into_owned)).collect()
}

pub fn message_chunk(message_id: &str, text: &str) -> SessionUpdate {
    SessionUpdate::AgentMessageChunk(ContentChunk::new(text.into(), message_id.to_owned()))
}

pub fn thought_chunk(message_id: &str, text: &str) -> SessionUpdate {
    SessionUpdate::AgentThoughtChunk(ContentChunk::new(text.into(), message_id.to_owned()))
}

pub fn user_message(message_id: &str, content: Vec<ContentBlock>) -> SessionUpdate {
    SessionUpdate::UserMessage(UserMessage::new(message_id.to_owned()).content(content))
}

pub fn plan(plan_id: impl Into<PlanId>, entries: Vec<PlanEntry>) -> SessionUpdate {
    SessionUpdate::PlanUpdate(PlanUpdate::new(PlanUpdateContent::items(plan_id, entries)))
}

pub fn tool_call(id: &str, title: &str, status: ToolCallStatus) -> SessionUpdate {
    SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(id.to_owned()).title(title).status(status))
}

pub fn select(id: &str, name: &str, current: &str, values: &[(&str, &str)]) -> SessionConfigOption {
    SessionConfigOption::select(id.to_owned(), name.to_owned(), current.to_owned(), choices(values))
}

pub fn choices(values: &[(&str, &str)]) -> Vec<SessionConfigSelectOption> {
    values.iter().map(|&(value, label)| SessionConfigSelectOption::new(value.to_owned(), label.to_owned())).collect()
}

pub fn model_picker(current: &str, choices: impl Into<SessionConfigSelectOptions>) -> SessionConfigOption {
    SessionConfigOption::select("model", "Model", current.to_owned(), choices)
        .category(SessionConfigOptionCategory::Model)
        .meta(ConfigOptionMeta { multi_select: true }.into_meta())
}

/// A single-select model option, as most agents offer it.
pub fn model_select(current: &str, values: &[(&str, &str)]) -> SessionConfigOption {
    select("model", "Model", current, values).category(SessionConfigOptionCategory::Model)
}

pub fn mode_select(current: &str, values: &[(&str, &str)]) -> SessionConfigOption {
    select("mode", "Mode", current, values).category(SessionConfigOptionCategory::Mode)
}

pub fn thought_level_select(current: &str, values: &[(&str, &str)]) -> SessionConfigOption {
    select("reasoning_effort", "Reasoning", current, values).category(SessionConfigOptionCategory::ThoughtLevel)
}

pub fn model_choice(value: &str, name: &str, meta: SelectOptionMeta) -> SessionConfigSelectOption {
    SessionConfigSelectOption::new(value.to_owned(), name.to_owned()).meta(meta.into_meta())
}

pub fn option_group(name: &str, options: Vec<SessionConfigSelectOption>) -> SessionConfigSelectGroup {
    SessionConfigSelectGroup::new(name.to_owned(), name.to_owned(), options)
}

pub fn session_preview(session_id: &str, cwd: &str) -> SessionPreviewResponse {
    SessionPreviewResponse {
        session_id: session_id.to_owned(),
        cwd: cwd.into(),
        created_at: "2025-01-01T00:00:00Z".to_owned(),
        model: "claude".to_owned(),
        selected_mode: Some("code".to_owned()),
        transcript: vec![
            SessionPreviewTurn { role: SessionPreviewRole::User, text: "hello".to_owned() },
            SessionPreviewTurn { role: SessionPreviewRole::Assistant, text: "hi there".to_owned() },
        ],
        tool_call_count: 1,
        truncated: false,
    }
}

pub fn prompt_search_hit(prompt: &str, match_start: usize, match_end: usize) -> PromptSearchResult {
    PromptSearchResult {
        session_id: "s1".to_owned(),
        cwd: "/tmp/repo".into(),
        session_created_at: "2026-05-17T00:00:00Z".to_owned(),
        prompt: prompt.to_owned(),
        match_start,
        match_end,
    }
}

pub fn prompt_search_response(query: &str, results: Vec<PromptSearchResult>) -> PromptSearchResponse {
    PromptSearchResponse { query: query.to_owned(), results, truncated: false }
}

pub fn run_local<T: Future>(test: T) -> T::Output {
    let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().expect("a test runtime");
    LocalSet::new().block_on(&runtime, Box::pin(test))
}

/// Build a live `ConnectionTo<Client>` over an in-memory duplex transport with
/// a peer on the other end. Must be called inside a `LocalSet`.
pub async fn test_connection() -> (ConnectionTo<Client>, TestPeer) {
    let (peer, client_builder) = TestPeer::new();
    let agent = Agent.v2().name("test-agent").on_receive_request(
        async |_: InitializeRequest, responder: v2::Responder<InitializeResponse>, _cx| {
            responder.respond(initialize_response())
        },
        acp::on_receive_request!(),
    );
    let pair = connect_pair(agent, client_builder).await;
    pair.client.send_request(initialize_request()).block_task().await.expect("initialize test peers");
    (pair.agent, peer)
}

pub struct ConnectedPair {
    pub agent: ConnectionTo<Client>,
    pub client: ConnectionTo<Agent>,
    pub agent_task: tokio::task::JoinHandle<Result<(), acp::Error>>,
    pub client_task: tokio::task::JoinHandle<Result<(), acp::Error>>,
}

pub async fn connect_pair<T, U, V, X, Y, Z>(
    agent: v2::Builder<Agent, T, U, V>,
    client: v2::Builder<Client, X, Y, Z>,
) -> ConnectedPair
where
    T: HandleDispatchFrom<Client> + 'static,
    U: RunWithConnectionTo<Client> + 'static,
    V: HandleConnectionClose<Client> + 'static,
    X: HandleDispatchFrom<Agent> + 'static,
    Y: RunWithConnectionTo<Agent> + 'static,
    Z: HandleConnectionClose<Agent> + 'static,
{
    let (agent_transport, client_transport) = Channel::duplex();
    let (agent_tx, agent_rx) = oneshot::channel();
    let (client_tx, client_rx) = oneshot::channel();
    let agent_task =
        spawn_local(async move { agent.with_runner(CaptureConnection(agent_tx)).connect_to(agent_transport).await });
    let client_task =
        spawn_local(async move { client.with_runner(CaptureConnection(client_tx)).connect_to(client_transport).await });
    ConnectedPair {
        agent: agent_rx.await.expect("agent connection"),
        client: client_rx.await.expect("client connection"),
        agent_task,
        client_task,
    }
}

pub struct CaptureConnection<R: acp::Role>(pub oneshot::Sender<ConnectionTo<R>>);

impl<R: acp::Role> RunWithConnectionTo<R> for CaptureConnection<R> {
    async fn run_with_connection_to(self, cx: ConnectionTo<R>) -> Result<(), acp::Error> {
        let _ = self.0.send(cx.clone());
        cx.incoming_closed().await;
        Ok(())
    }
}

/// Initialization request from an in-memory v2 client.
pub fn initialize_request() -> InitializeRequest {
    InitializeRequest::new(ProtocolVersion::V2, Implementation::new("test-client", "0.0.0"))
}

/// Initialization response from an in-memory v2 agent.
pub fn initialize_response() -> InitializeResponse {
    InitializeResponse::new(ProtocolVersion::V2, Implementation::new("test-agent", "0.0.0"))
}

pub fn running_state() -> StateUpdate {
    StateUpdate::Running(RunningStateUpdate::new())
}

pub fn idle_state(stop_reason: impl IntoOption<StopReason>) -> StateUpdate {
    StateUpdate::Idle(IdleStateUpdate::new().stop_reason(stop_reason))
}

/// A live foreground turn has started.
pub fn running_notification(session_id: impl Into<SessionId>) -> UpdateSessionNotification {
    UpdateSessionNotification::new(session_id, SessionUpdate::StateUpdate(running_state()))
}

/// Foreground work is idle, optionally with a reported stop reason.
pub fn idle_notification(
    session_id: impl Into<SessionId>,
    stop_reason: Option<StopReason>,
) -> UpdateSessionNotification {
    UpdateSessionNotification::new(session_id, SessionUpdate::StateUpdate(idle_state(stop_reason)))
}

pub fn announced(
    parent: impl Into<SessionId>,
    tool_call_id: &str,
    update: SubagentUpdate,
) -> UpdateSessionNotification {
    let update = update.meta(SubAgentMeta::new(tool_call_id).to_meta());
    UpdateSessionNotification::new(parent, SessionUpdate::SubagentUpdate(update))
}

pub fn pricing_artifact(version: u32) -> Artifact {
    let content = pricing_content(version);
    Artifact {
        slot: ArtifactSlot::new("pricing"),
        version,
        title: "Pricing".to_owned(),
        name: "pricing.html".to_owned(),
        kind: content.kind(),
        id: content.id(),
        versions: (1..=version).map(|version| pricing_content(version).id()).collect(),
        comments: Vec::new(),
    }
}

/// A comment on the heading of [`pricing_artifact`]'s first version.
pub fn pricing_comment(number: u32, note: &str) -> ArtifactComment {
    let anchor = Anchor {
        selector: "body > h1".to_owned(),
        element: "<h1>".to_owned(),
        label: "Heading “Pricing v1”".to_owned(),
        quote: "Pricing v1".to_owned(),
        target: Target::Element,
    };
    let (slot, name) = (ArtifactSlot::new("pricing"), "pricing.html".to_owned());
    ArtifactComment { number, slot, name, version: 1, anchor, note: note.to_owned(), follow_up: false }
}

pub fn pricing_content(version: u32) -> ArtifactContent {
    ArtifactContent::Html(format!("<h1>Pricing v{version}</h1>"))
}

pub fn artifact_submitted(session_id: &str, version: u32) -> AcpEvent {
    AcpEvent::Artifact(ArtifactPayload {
        session_id: SessionId::new(session_id),
        set: pricing_artifact(version).into(),
    })
}

pub fn on_session(session_id: impl Into<SessionId>, update: SessionUpdate) -> AcpEvent {
    UpdateSessionNotification::new(session_id, update).into()
}

pub fn sub_agent_tool_update(session_id: impl Into<SessionId>, update: ToolCallUpdate) -> AcpEvent {
    on_session(session_id, SessionUpdate::ToolCallUpdate(update))
}

/// Replace the entries of an agent-owned plan.
pub fn plan_notification(
    session_id: impl Into<SessionId>,
    plan_id: impl Into<PlanId>,
    entries: Vec<PlanEntry>,
) -> UpdateSessionNotification {
    UpdateSessionNotification::new(session_id, plan(plan_id, entries))
}

#[expect(clippy::unnecessary_wraps, reason = "notification handlers must return a Result")]
fn forward<T>(tx: &mpsc::UnboundedSender<T>, value: T) -> Result<(), acp::Error> {
    let _ = tx.send(value);
    Ok(())
}

fn placeholder_params() -> CreateElicitationRequest {
    CreateElicitationRequest::new(
        ElicitationFormMode::new(ElicitationSessionScope::new("test-session"), ElicitationSchema::new()),
        String::new(),
    )
}