oino-acp 1.0.0

ACP v1 (Agent Client Protocol) agent adapter over oino-core sessions
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! One ACP session: the runtime, its in-process backend, and the event pump that turns
//! Oino client events into `session/update` notifications and client requests
//! (modes §6.3, §6.4).

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use futures::StreamExt;
use oino_agent::AgentToolResult;
use oino_ai::{
    AgentMessage, AssistantContent, AssistantMessageEvent, JsonObject, JsonValue, StopReason,
};
use oino_client::{InProcessBackend, SessionBackend};
use oino_core::events::{AgentSessionEvent, ClientEvent, OinoEvent};
use oino_core::runtime::AgentSessionRuntime;
use oino_core::session::AgentSession;
use oino_core::ui::{UiAnswer, UiRequest, UiResponse, is_dialog};
use oino_tools::env::SpawnRequest;
use serde_json::json;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;

use crate::client::ClientLink;
use crate::commands::available_commands;
use crate::config::config_options;
use crate::convert::{last_assistant_stop, stop_reason, tool_call_created};
use crate::env::TerminalObserver;
use crate::tools::{FileSnapshot, result_content, tool_kind, tool_locations, tool_title};
use crate::ui::{Presentation, fire_update};

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}

/// A tool call the client knows about.
#[derive(Debug, Default)]
struct ToolState {
    name: String,
    args: JsonObject,
    executing: bool,
    snapshot: Option<FileSnapshot>,
    terminal: Option<String>,
}

/// A `session/prompt` waiting for `agent_settled`.
#[derive(Debug)]
struct Waiter {
    id: u64,
    /// Resolve only after a run started (the prompt started one).
    needs_start: bool,
    started: bool,
    tx: oneshot::Sender<&'static str>,
}

#[derive(Debug, Default)]
struct State {
    tools: HashMap<String, ToolState>,
    /// Tool-call ids in execution order (the latest one owns `confirm` permissions).
    executing: Vec<String>,
    waiters: Vec<Waiter>,
    next_waiter: u64,
    cancel_requested: bool,
    compactions: u64,
    compaction: Option<String>,
}

/// State shared by the pump, prompt handlers and the execution environment.
pub(crate) struct Shared {
    link: ClientLink,
    cwd: PathBuf,
    state: Mutex<State>,
}

impl std::fmt::Debug for Shared {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Shared")
            .field("cwd", &self.cwd)
            .finish_non_exhaustive()
    }
}

/// A registered prompt; await [`PromptTicket::settled`] for the stop reason.
pub(crate) struct PromptTicket {
    id: u64,
    rx: oneshot::Receiver<&'static str>,
}

impl PromptTicket {
    /// The stop reason (`cancelled` if the session went away).
    pub(crate) async fn settled(self) -> &'static str {
        self.rx.await.unwrap_or("cancelled")
    }
}

impl Shared {
    /// State for a session at `cwd` talking through `link`.
    pub(crate) fn new(link: ClientLink, cwd: PathBuf) -> Arc<Self> {
        Arc::new(Self {
            link,
            cwd,
            state: Mutex::new(State::default()),
        })
    }

    pub(crate) fn link(&self) -> &ClientLink {
        &self.link
    }

    fn update(&self, update: JsonValue) {
        if let Err(error) = self.link.update(update) {
            tracing::debug!("session/update dropped: {error}");
        }
    }

    /// Registers a prompt before it is sent (so its `agent_settled` cannot be missed).
    pub(crate) fn register_prompt(&self) -> PromptTicket {
        let (tx, rx) = oneshot::channel();
        let mut state = lock(&self.state);
        if state.waiters.is_empty() {
            state.cancel_requested = false;
        }
        state.next_waiter += 1;
        let id = state.next_waiter;
        state.waiters.push(Waiter {
            id,
            needs_start: true,
            started: false,
            tx,
        });
        PromptTicket { id, rx }
    }

    /// The prompt was queued into a running turn: it settles with that turn.
    pub(crate) fn prompt_queued(&self, ticket: &PromptTicket) {
        if let Some(waiter) = lock(&self.state)
            .waiters
            .iter_mut()
            .find(|w| w.id == ticket.id)
        {
            waiter.needs_start = false;
        }
    }

    /// Resolves a prompt now (handled by a command, failed to start).
    pub(crate) fn resolve_prompt(&self, ticket: &PromptTicket, reason: &'static str) {
        let mut state = lock(&self.state);
        if let Some(index) = state.waiters.iter().position(|w| w.id == ticket.id) {
            let waiter = state.waiters.remove(index);
            let _ = waiter.tx.send(reason);
        }
    }

    /// `session/cancel` arrived.
    pub(crate) fn request_cancel(&self) {
        lock(&self.state).cancel_requested = true;
    }

    fn latest_executing(&self) -> Option<String> {
        lock(&self.state).executing.last().cloned()
    }

    /// The `toolCall` (`ToolCallUpdate`) object of a permission request.
    fn permission_tool_call(&self, id: &str, title: &str, detail: Option<&str>) -> JsonValue {
        let state = lock(&self.state);
        let mut call = json!({ "toolCallId": id });
        if let Some(tool) = state.tools.get(id) {
            call["title"] = json!(tool_title(&tool.name, &tool.args));
            call["kind"] = json!(tool_kind(&tool.name));
            call["rawInput"] = JsonValue::Object(tool.args.clone());
            call["locations"] = json!(tool_locations(&tool.name, &tool.args, &self.cwd));
        }
        if !title.is_empty() {
            call["title"] = json!(title);
        }
        if let Some(detail) = detail {
            call["content"] =
                json!([{ "type": "content", "content": {"type": "text", "text": detail} }]);
        }
        call
    }
}

impl TerminalObserver for Shared {
    fn terminal_started(&self, request: &SpawnRequest, terminal_id: &str) {
        let script = request.args.last().map(String::as_str).unwrap_or_default();
        let owner = {
            let mut state = lock(&self.state);
            let owner = state
                .executing
                .iter()
                .rev()
                .find(|id| {
                    state.tools.get(*id).is_some_and(|tool| {
                        tool.terminal.is_none()
                            && tool
                                .args
                                .get("command")
                                .and_then(JsonValue::as_str)
                                .is_some_and(|command| script.contains(command))
                    })
                })
                .cloned();
            if let Some(tool) = owner.as_ref().and_then(|id| state.tools.get_mut(id)) {
                tool.terminal = Some(terminal_id.to_owned());
            }
            owner
        };
        if let Some(id) = owner {
            self.update(json!({
                "sessionUpdate": "tool_call_update",
                "toolCallId": id,
                "content": [{ "type": "terminal", "terminalId": terminal_id }],
            }));
        }
    }
}

/// One live ACP session.
pub(crate) struct AcpSession {
    /// ACP session id (the Oino id of the first session of the runtime).
    pub(crate) id: String,
    /// Session cwd.
    pub(crate) cwd: PathBuf,
    /// Commands and events.
    pub(crate) backend: Arc<InProcessBackend>,
    /// Pump/prompt state.
    pub(crate) shared: Arc<Shared>,
    pump: JoinHandle<()>,
}

impl std::fmt::Debug for AcpSession {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AcpSession")
            .field("id", &self.id)
            .field("cwd", &self.cwd)
            .finish_non_exhaustive()
    }
}

impl AcpSession {
    /// Wraps `runtime` and starts the event pump.
    pub(crate) fn start(runtime: Arc<AgentSessionRuntime>, shared: Arc<Shared>) -> Self {
        let id = runtime.session().session_id();
        shared.link.bind(&id);
        let cwd = shared.cwd.clone();
        let backend = Arc::new(InProcessBackend::new(runtime));
        let events = backend.events();
        let pump = tokio::spawn(pump(events, Arc::clone(&backend), Arc::clone(&shared)));
        Self {
            id,
            cwd,
            backend,
            shared,
            pump,
        }
    }

    /// The current session of the runtime.
    pub(crate) fn session(&self) -> AgentSession {
        self.backend.runtime().session()
    }

    /// Sends the updates that follow `session/new` / `session/load` responses.
    pub(crate) fn announce(&self) {
        self.shared.update(available_commands(&self.session()));
    }

    /// Disposes the runtime.
    pub(crate) async fn close(&self) {
        self.backend.shutdown().await;
        self.pump.abort();
        let waiters = std::mem::take(&mut lock(&self.shared.state).waiters);
        for waiter in waiters {
            let _ = waiter.tx.send("cancelled");
        }
    }
}

async fn pump(
    mut events: futures::stream::BoxStream<'static, ClientEvent>,
    backend: Arc<InProcessBackend>,
    shared: Arc<Shared>,
) {
    while let Some(event) = events.next().await {
        match event {
            ClientEvent::Session(event) => on_session_event(event, &backend, &shared).await,
            ClientEvent::Ui(request) => on_ui(request, &backend, &shared),
            ClientEvent::ExtensionError(error) => shared.update(json!({
                "sessionUpdate": "notice",
                "severity": "error",
                "title": format!("Extension error ({})", error.error.extension_path),
                "description": error.error.error,
            })),
            ClientEvent::Oino(event) => on_oino_event(&event, &backend, &shared),
        }
    }
}

fn usage_update(session: &AgentSession) -> Option<JsonValue> {
    let usage = session.context_usage()?;
    let used = usage.tokens?;
    Some(json!({
        "sessionUpdate": "usage_update",
        "used": used,
        "size": usage.context_window,
        "cost": { "amount": session.session_stats().cost, "currency": "USD" },
    }))
}

fn config_update(session: &AgentSession) -> JsonValue {
    json!({ "sessionUpdate": "config_option_update", "configOptions": config_options(session) })
}

#[allow(
    clippy::too_many_lines,
    reason = "one arm per AgentSessionEvent, mirroring the modes §6.3 table"
)]
async fn on_session_event(event: AgentSessionEvent, backend: &InProcessBackend, shared: &Shared) {
    let session = backend.runtime().session();
    match event {
        AgentSessionEvent::MessageUpdate {
            assistant_message_event,
            ..
        } => on_stream_event(assistant_message_event, shared),
        AgentSessionEvent::ToolExecutionStart {
            tool_call_id,
            tool_name,
            args,
            ..
        } => {
            let snapshot = FileSnapshot::take(&tool_name, &args, &shared.cwd).await;
            let known = {
                let mut state = lock(&shared.state);
                state.executing.push(tool_call_id.clone());
                let known = state.tools.contains_key(&tool_call_id);
                let tool = state.tools.entry(tool_call_id.clone()).or_default();
                tool.name.clone_from(&tool_name);
                tool.args.clone_from(&args);
                tool.executing = true;
                tool.snapshot = snapshot;
                known
            };
            if !known {
                shared.update(tool_call_created(
                    &tool_call_id,
                    &tool_name,
                    &args,
                    "in_progress",
                    &shared.cwd,
                ));
            }
            shared.update(json!({
                "sessionUpdate": "tool_call_update",
                "toolCallId": tool_call_id,
                "status": "in_progress",
            }));
        }
        AgentSessionEvent::ToolExecutionUpdate {
            tool_call_id,
            partial_result,
            ..
        } => {
            let terminal = lock(&shared.state)
                .tools
                .get(&tool_call_id)
                .is_some_and(|t| t.terminal.is_some());
            if !terminal {
                shared.update(json!({
                    "sessionUpdate": "tool_call_update",
                    "toolCallId": tool_call_id,
                    "content": result_content(&partial_result),
                }));
            }
        }
        AgentSessionEvent::ToolExecutionEnd {
            tool_call_id,
            result,
            is_error,
            ..
        } => on_tool_end(&tool_call_id, &result, is_error, shared).await,
        AgentSessionEvent::TurnEnd { .. } => {
            if let Some(update) = usage_update(&session) {
                shared.update(update);
            }
        }
        AgentSessionEvent::AgentStart => {
            for waiter in &mut lock(&shared.state).waiters {
                waiter.started = true;
            }
        }
        AgentSessionEvent::AgentSettled { aborted } => on_settled(aborted, &session, shared),
        AgentSessionEvent::CompactionStart { .. } => {
            let id = {
                let mut state = lock(&shared.state);
                state.compactions += 1;
                let id = format!("compaction-{}", state.compactions);
                state.compaction = Some(id.clone());
                id
            };
            shared.update(json!({
                "sessionUpdate": "compaction_update",
                "compactionId": id,
                "status": "in_progress",
            }));
        }
        AgentSessionEvent::CompactionEnd {
            result,
            aborted,
            error_message,
            ..
        } => {
            let id = lock(&shared.state)
                .compaction
                .take()
                .unwrap_or_else(|| "compaction".to_owned());
            if let Some(result) = &result {
                shared.update(json!({
                    "sessionUpdate": "compaction_summary_chunk",
                    "compactionId": id,
                    "content": {"type": "text", "text": result.summary},
                }));
            }
            let mut update = json!({ "sessionUpdate": "compaction_update", "compactionId": id });
            if aborted {
                update["status"] = json!("cancelled");
            } else if let Some(error) = error_message.filter(|_| result.is_none()) {
                update["status"] = json!("failed");
                update["error"] = json!(error);
            } else {
                update["status"] = json!("completed");
            }
            shared.update(update);
            if let Some(update) = usage_update(&session) {
                shared.update(update);
            }
        }
        AgentSessionEvent::SessionInfoChanged { name } => shared.update(json!({
            "sessionUpdate": "session_info_update",
            "title": name,
        })),
        AgentSessionEvent::ThinkingLevelChanged { .. } => shared.update(config_update(&session)),
        AgentSessionEvent::AutoRetryStart {
            attempt,
            max_attempts,
            delay_ms,
            error_message,
        } => shared.update(json!({
            "sessionUpdate": "notice",
            "severity": "warning",
            "title": format!(
                "Retrying in {}s (attempt {attempt}/{max_attempts})",
                delay_ms.div_ceil(1000)
            ),
            "description": error_message,
        })),
        AgentSessionEvent::AutoRetryEnd {
            success: false,
            final_error,
            attempt,
        } => shared.update(json!({
            "sessionUpdate": "notice",
            "severity": "error",
            "title": format!("Retry failed after {attempt} attempts"),
            "description": final_error,
        })),
        AgentSessionEvent::QueueUpdate {
            steering,
            follow_up,
        } => shared.link.ext_notification(
            "_oino/queue_update",
            json!({ "steering": steering, "followUp": follow_up }),
        ),
        _ => {}
    }
}

fn on_stream_event(event: AssistantMessageEvent, shared: &Shared) {
    match event {
        AssistantMessageEvent::TextDelta { delta, .. } => shared.update(json!({
            "sessionUpdate": "agent_message_chunk",
            "content": {"type": "text", "text": delta},
        })),
        AssistantMessageEvent::ThinkingDelta { delta, .. } => shared.update(json!({
            "sessionUpdate": "agent_thought_chunk",
            "content": {"type": "text", "text": delta},
        })),
        AssistantMessageEvent::ToolcallStart {
            content_index,
            partial,
        } => {
            let Some(AssistantContent::ToolCall(call)) = partial.content.get(content_index) else {
                return;
            };
            if call.id.is_empty() {
                return;
            }
            let fresh = {
                let mut state = lock(&shared.state);
                let fresh = !state.tools.contains_key(&call.id);
                state
                    .tools
                    .entry(call.id.clone())
                    .or_default()
                    .name
                    .clone_from(&call.name);
                fresh
            };
            if fresh {
                shared.update(tool_call_created(
                    &call.id,
                    &call.name,
                    &JsonObject::new(),
                    "pending",
                    &shared.cwd,
                ));
            }
        }
        AssistantMessageEvent::ToolcallEnd { tool_call, .. } => {
            let fresh = {
                let mut state = lock(&shared.state);
                let fresh = !state.tools.contains_key(&tool_call.id);
                let tool = state.tools.entry(tool_call.id.clone()).or_default();
                tool.name.clone_from(&tool_call.name);
                tool.args.clone_from(&tool_call.arguments);
                fresh
            };
            if fresh {
                shared.update(tool_call_created(
                    &tool_call.id,
                    &tool_call.name,
                    &tool_call.arguments,
                    "pending",
                    &shared.cwd,
                ));
            } else {
                shared.update(json!({
                    "sessionUpdate": "tool_call_update",
                    "toolCallId": tool_call.id,
                    "title": tool_title(&tool_call.name, &tool_call.arguments),
                    "rawInput": tool_call.arguments,
                    "locations": tool_locations(&tool_call.name, &tool_call.arguments, &shared.cwd),
                }));
            }
        }
        _ => {}
    }
}

async fn on_tool_end(id: &str, result: &AgentToolResult, is_error: bool, shared: &Shared) {
    let tool = {
        let mut state = lock(&shared.state);
        state.executing.retain(|e| e != id);
        state.tools.remove(id)
    };
    let mut content = match tool.as_ref().and_then(|t| t.terminal.clone()) {
        Some(terminal) => vec![json!({ "type": "terminal", "terminalId": terminal })],
        None => result_content(result),
    };
    if !is_error
        && let Some(snapshot) = tool.as_ref().and_then(|t| t.snapshot.as_ref())
        && let Some(diff) = snapshot.diff().await
    {
        content.push(diff);
    }
    shared.update(json!({
        "sessionUpdate": "tool_call_update",
        "toolCallId": id,
        "status": if is_error { "failed" } else { "completed" },
        "content": content,
        "rawOutput": serde_json::to_value(result).unwrap_or(JsonValue::Null),
    }));
}

fn on_settled(aborted: bool, session: &AgentSession, shared: &Shared) {
    if let Some(update) = usage_update(session) {
        shared.update(update);
    }
    let last = last_assistant_stop(&session.messages());
    let (waiters, cancelled) = {
        let mut state = lock(&shared.state);
        let (ready, waiting): (Vec<_>, Vec<_>) = state
            .waiters
            .drain(..)
            .partition(|w| !w.needs_start || w.started);
        state.waiters = waiting;
        let cancelled = state.cancel_requested;
        if state.waiters.is_empty() {
            state.cancel_requested = false;
        }
        (ready, cancelled)
    };
    if waiters.is_empty() {
        return;
    }
    if let Some((StopReason::Error, Some(message))) = &last
        && !aborted
    {
        shared.update(json!({
            "sessionUpdate": "notice",
            "severity": "error",
            "title": message,
        }));
    }
    let reason = stop_reason(aborted || cancelled, last.map(|(reason, _)| reason));
    for waiter in waiters {
        let _ = waiter.tx.send(reason);
    }
}

fn on_oino_event(event: &OinoEvent, backend: &InProcessBackend, shared: &Shared) {
    let session = backend.runtime().session();
    match event {
        OinoEvent::ModelChanged { .. } => shared.update(config_update(&session)),
        OinoEvent::CommandsChanged => shared.update(available_commands(&session)),
        OinoEvent::SessionReplaced { .. } => {
            shared.update(available_commands(&session));
            shared.update(config_update(&session));
            shared.update(json!({
                "sessionUpdate": "session_info_update",
                "title": session.session_name(),
            }));
        }
        OinoEvent::RunState { .. } => shared.link.ext_notification(
            "_oino/run_state",
            serde_json::to_value(event).unwrap_or(JsonValue::Null),
        ),
        OinoEvent::ExtensionUiDismiss { .. } => shared.link.ext_notification(
            "_oino/extension_ui",
            serde_json::to_value(event).unwrap_or(JsonValue::Null),
        ),
        _ => {}
    }
}

fn on_ui(request: UiRequest, backend: &Arc<InProcessBackend>, shared: &Arc<Shared>) {
    if !is_dialog(&request.method) {
        match fire_update(&request) {
            Some(update) => shared.update(update),
            None => shared.link.ext_notification(
                "_oino/extension_ui",
                serde_json::to_value(&request).unwrap_or(JsonValue::Null),
            ),
        }
        return;
    }
    let backend = Arc::clone(backend);
    let shared = Arc::clone(shared);
    tokio::spawn(async move {
        let answer = answer_dialog(&request, &shared).await;
        backend.ui_respond(UiResponse {
            id: request.id,
            answer,
        });
    });
}

async fn answer_dialog(request: &UiRequest, shared: &Shared) -> UiAnswer {
    let running = shared.latest_executing();
    let caps = shared.link.caps();
    match Presentation::of(request, running.is_some(), caps.elicitation_form) {
        Presentation::Permission {
            tool_call_id,
            title,
            detail,
            options,
            confirm,
        } => {
            let Some(id) = tool_call_id.or(running) else {
                return Presentation::permission_answer(confirm, None);
            };
            let tool_call = shared.permission_tool_call(&id, &title, detail.as_deref());
            match shared
                .link
                .request_permission(tool_call, JsonValue::Array(options))
                .await
            {
                Ok(selected) => Presentation::permission_answer(confirm, selected),
                Err(error) => {
                    tracing::warn!("{error}");
                    Presentation::permission_answer(confirm, None)
                }
            }
        }
        Presentation::Elicitation {
            message,
            schema,
            confirm,
        } => match shared
            .link
            .elicit_form(&message, schema, running.as_deref())
            .await
        {
            Ok(content) => Presentation::elicitation_answer(confirm, content),
            Err(error) => {
                tracing::warn!("{error}");
                Presentation::elicitation_answer(confirm, None)
            }
        },
        Presentation::Form {
            message,
            schema,
            questions,
        } => match shared
            .link
            .elicit_form(&message, schema, running.as_deref())
            .await
        {
            Ok(content) => Presentation::form_answer(&questions, content),
            Err(error) => {
                tracing::warn!("{error}");
                Presentation::form_answer(&questions, None)
            }
        },
        Presentation::Immediate(answer) => answer,
    }
}

/// `session/load` replay of the current branch.
pub(crate) fn replay(session: &AgentSession, cwd: &Path, link: &ClientLink) {
    let messages: Vec<AgentMessage> = session.messages();
    for update in crate::convert::replay(&messages, cwd) {
        if let Err(error) = link.update(update) {
            tracing::debug!("replay dropped: {error}");
        }
    }
}