ag-ui 1.0.0

Rust SDK for AG-UI 1.0 with protocol types, SSE, a server runtime for hosting agents, and a client runtime for consuming them.
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
//! The human-in-the-loop round trip: pause, ask, resume.

#![cfg(feature = "client")]

use ag_ui::client::transport::ReplayTransport;
use ag_ui::client::{
    InterruptExt, RemoteAgent, ResumeBuilder, RunEnd, RunParams, Thread, Update, interrupts_of,
    resume_run,
};
use ag_ui::{Event, Interrupt, Message, ResumeStatus, RunAgentInput, TextMessageRole};
use futures_util::StreamExt;
use serde_json::json;

fn approval() -> Interrupt {
    Interrupt {
        message: Some("Delete the staging database?".into()),
        tool_call_id: Some("call-1".into()),
        ..Interrupt::new("i-1", "tool_approval")
    }
}

/// A run that pauses, and the run that follows it.
fn paused_then_resumed() -> ReplayTransport {
    ReplayTransport::with_runs([
        vec![
            Event::run_started("thread-1", "run-1"),
            Event::tool_call_start("call-1", "drop_database"),
            Event::tool_call_args("call-1", r#"{"name":"staging"}"#),
            Event::tool_call_end("call-1"),
            Event::run_finished_interrupt("thread-1", "run-1", vec![approval()]),
        ],
        vec![
            Event::run_started("thread-1", "run-2"),
            Event::tool_call_result("msg-2", "call-1", "dropped"),
            Event::text_message_start("msg-3", TextMessageRole::Assistant),
            Event::text_message_content("msg-3", "Done."),
            Event::text_message_end("msg-3"),
            Event::run_finished_success("thread-1", "run-2"),
        ],
    ])
    .matching_requests()
}

#[tokio::test]
async fn an_interrupt_surfaces_and_resuming_sends_the_answer() {
    let transport = paused_then_resumed();
    let mut thread = Thread::<_>::new(transport.clone(), "thread-1");

    // First run: the agent pauses.
    let mut pending = Vec::new();
    let mut ended = None;
    let mut run = thread.send("drop the staging database").unwrap();
    while let Some(update) = run.next().await {
        match update {
            Update::Interrupt(interrupt) => pending.push(interrupt),
            Update::Done(end) => ended = Some(end),
            Update::Error(error) => panic!("unexpected error: {error}"),
            _ => {}
        }
    }
    drop(run);

    assert_eq!(pending.len(), 1);
    assert_eq!(pending[0].id, "i-1");
    assert!(pending[0].is_tool_approval());
    assert!(matches!(ended, Some(RunEnd::Interrupted { .. })));
    // The thread remembers what it is waiting for.
    assert_eq!(thread.interrupts().len(), 1);

    // Second run: the human said yes.
    let mut resumed = thread
        .resume(&pending[0], json!({ "approved": true }))
        .unwrap();
    while let Some(update) = resumed.next().await {
        if let Update::Error(error) = update {
            panic!("unexpected error: {error}");
        }
    }
    drop(resumed);

    let requests = transport.requests();
    assert_eq!(requests.len(), 2);

    let resume = requests[1]
        .resume
        .as_ref()
        .expect("the resuming request must carry the answers");
    assert_eq!(resume.len(), 1);
    assert_eq!(resume[0].interrupt_id, "i-1");
    assert_eq!(resume[0].status, ResumeStatus::Resolved);
    assert_eq!(resume[0].payload, Some(json!({ "approved": true })));

    // Same thread, a new run, and the conversation so far.
    assert_eq!(requests[1].thread_id, "thread-1");
    assert_ne!(requests[1].run_id, requests[0].run_id);
    assert!(requests[1].is_resume());
    assert!(!requests[1].messages.is_empty());

    // And the paused interrupt is no longer pending.
    assert!(thread.interrupts().is_empty());
    assert_eq!(thread.applier().text_of("msg-3"), Some("Done."));
}

#[tokio::test]
async fn declining_an_interrupt_resumes_with_a_cancellation() {
    let transport = paused_then_resumed();
    let mut thread = Thread::<_>::new(transport.clone(), "thread-1");

    let mut pending = Vec::new();
    let mut run = thread.send("drop the staging database").unwrap();
    while let Some(update) = run.next().await {
        if let Update::Interrupt(interrupt) = update {
            pending.push(interrupt);
        }
    }
    drop(run);

    let mut resumed = thread.decline(&pending[0]).unwrap();
    while resumed.next().await.is_some() {}
    drop(resumed);

    let resume = transport.requests()[1]
        .resume
        .clone()
        .expect("the resuming request must carry the answers");
    assert_eq!(resume[0].status, ResumeStatus::Cancelled);
    assert_eq!(resume[0].payload, None);
}

#[tokio::test]
async fn a_confirmed_approval_decision_does_not_block_later_runs() {
    for approved in [true, false] {
        let transport = ReplayTransport::with_runs([
            vec![
                Event::run_started("thread-1", "run-1"),
                Event::tool_call_start("call-1", "drop_database"),
                Event::tool_call_end("call-1"),
                Event::run_finished_interrupt("thread-1", "run-1", vec![approval()]),
            ],
            vec![
                Event::run_started("thread-1", "run-2"),
                Event::run_finished_success("thread-1", "run-2"),
            ],
            vec![
                Event::run_started("thread-1", "run-3"),
                Event::run_finished_success("thread-1", "run-3"),
            ],
        ])
        .matching_requests();
        let mut thread = Thread::new(transport.clone(), "thread-1");
        thread.send("first").unwrap().collect_report().await;
        let pending = thread.interrupts()[0].clone();
        let report = if approved {
            thread
                .resume(&pending, json!(true))
                .unwrap()
                .collect_report()
                .await
        } else {
            thread.decline(&pending).unwrap().collect_report().await
        };
        assert_eq!(report.end, RunEnd::Success { result: None });

        // The decision, including a decline, is an interrupt response. No
        // frontend ToolMessage is fabricated for the approval-linked call.
        let snapshot = thread.snapshot();
        let mut restored =
            Thread::<_, serde_json::Value>::restore(transport.clone(), snapshot).unwrap();
        assert_eq!(
            restored
                .send("next task")
                .unwrap()
                .collect_report()
                .await
                .end,
            RunEnd::Success { result: None }
        );
        assert_eq!(transport.requests().len(), 3);
    }
}

#[tokio::test]
async fn resuming_an_approval_still_requires_unrelated_frontend_tool_answers() {
    let transport = ReplayTransport::with_runs([
        vec![
            Event::run_started("thread-1", "run-1"),
            Event::tool_call_start("approval-call", "drop_database"),
            Event::tool_call_end("approval-call"),
            Event::tool_call_start("frontend-call", "choose_file"),
            Event::tool_call_end("frontend-call"),
            Event::run_finished_interrupt(
                "thread-1",
                "run-1",
                vec![Interrupt {
                    tool_call_id: Some("approval-call".into()),
                    ..approval()
                }],
            ),
        ],
        vec![
            Event::run_started("thread-1", "run-2"),
            Event::run_finished_success("thread-1", "run-2"),
        ],
        vec![
            Event::run_started("thread-1", "run-3"),
            Event::run_finished_success("thread-1", "run-3"),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::new(transport.clone(), "thread-1");
    thread.send("first").unwrap().collect_report().await;
    let pending = thread.interrupts()[0].clone();
    let message_count = thread.messages().len();
    let error = thread
        .resume(&pending, json!(true))
        .expect_err("unrelated frontend call still needs an answer");
    assert!(error.to_string().contains("frontend-call"), "{error}");
    assert!(!error.to_string().contains("approval-call"), "{error}");
    assert_eq!(thread.messages().len(), message_count);
    assert_eq!(transport.requests().len(), 1);

    thread
        .push_message(Message::tool("answer-1", "frontend-call", "chosen"))
        .unwrap();
    assert_eq!(
        thread
            .resume(&pending, json!(true))
            .unwrap()
            .collect_report()
            .await
            .end,
        RunEnd::Success { result: None }
    );
    assert!(transport.requests()[1].resume.is_some());
    assert_eq!(
        thread.send("next task").unwrap().collect_report().await.end,
        RunEnd::Success { result: None }
    );
}

#[tokio::test]
async fn a_later_call_reusing_an_approval_id_is_pending_again() {
    let transport = ReplayTransport::with_runs([
        vec![
            Event::run_started("thread-1", "run-1"),
            Event::tool_call_start("call-1", "drop_database"),
            Event::tool_call_end("call-1"),
            Event::run_finished_interrupt("thread-1", "run-1", vec![approval()]),
        ],
        vec![
            Event::run_started("thread-1", "run-2"),
            Event::run_finished_success("thread-1", "run-2"),
        ],
        vec![
            Event::run_started("thread-1", "run-3"),
            Event::tool_call_start("call-1", "choose_file"),
            Event::tool_call_end("call-1"),
            Event::run_finished_success("thread-1", "run-3"),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::new(transport.clone(), "thread-1");
    thread.send("first").unwrap().collect_report().await;
    let pending = thread.interrupts()[0].clone();
    thread.decline(&pending).unwrap().collect_report().await;
    thread.send("second").unwrap().collect_report().await;
    let error = thread
        .send("too soon")
        .expect_err("new call needs an answer");
    assert!(error.to_string().contains("call-1"), "{error}");
    assert_eq!(transport.requests().len(), 3);
}

#[tokio::test]
async fn several_interrupts_are_answered_in_one_request() {
    let first = Interrupt::new("i-1", "tool_approval");
    let second = Interrupt::new("i-2", "tool_approval");
    let transport = ReplayTransport::with_runs([
        vec![
            Event::run_started("thread-1", "run-1"),
            Event::run_finished_interrupt("thread-1", "run-1", vec![first.clone(), second.clone()]),
        ],
        vec![
            Event::run_started("thread-1", "run-2"),
            Event::run_finished_success("thread-1", "run-2"),
        ],
    ])
    .matching_requests();
    let mut thread = Thread::<_>::new(transport.clone(), "thread-1");

    let updates: Vec<_> = thread.send("do two risky things").unwrap().collect().await;
    let interrupts: Vec<_> = updates
        .iter()
        .filter(|update| matches!(update, Update::Interrupt(_)))
        .collect();
    assert_eq!(interrupts.len(), 2, "one update per pending interrupt");

    let entries = ResumeBuilder::new()
        .resolve_with_edits(&first, json!({ "name": "staging-2" }))
        .cancel(&second)
        .build();
    let mut resumed = thread.resume_many(entries).unwrap();
    while resumed.next().await.is_some() {}
    drop(resumed);

    let resume = transport.requests()[1].resume.clone().expect("answers");
    assert_eq!(resume.len(), 2);
    assert_eq!(
        resume[0].payload,
        Some(json!({ "editedArgs": { "name": "staging-2" } }))
    );
    assert_eq!(resume[1].status, ResumeStatus::Cancelled);
}

#[tokio::test]
async fn the_low_level_api_can_do_the_round_trip_without_a_thread() {
    // A proxy holds the raw events and builds the resuming request itself.
    let transport = paused_then_resumed();
    let agent = RemoteAgent::new(transport.clone());

    let first = RunParams::new("thread-1", "run-1")
        .user("msg-1", "drop the staging database")
        .into_input();

    let mut pending = Vec::new();
    let mut events = agent.run_events(first.clone());
    while let Some(event) = events.next().await {
        let event = event.expect("the replay transport does not fail");
        pending.extend(interrupts_of(&event).iter().cloned());
    }
    assert_eq!(pending.len(), 1);

    let next = resume_run(&first, "run-2", vec![pending[0].resolve(json!(true))]);
    let mut events = agent.run_events(next);
    while events.next().await.is_some() {}

    let requests = transport.requests();
    assert_eq!(requests[1].run_id, "run-2");
    assert_eq!(requests[1].thread_id, "thread-1");
    // The resuming request carries the paused run's messages unchanged.
    assert_eq!(requests[1].messages, requests[0].messages);
    assert_eq!(
        requests[1].resume.as_ref().expect("answers")[0].payload,
        Some(json!(true))
    );
}

#[test]
fn interrupts_of_only_reports_a_paused_run() {
    let paused = Event::run_finished_interrupt("t", "r", vec![approval()]);
    assert_eq!(interrupts_of(&paused).len(), 1);

    // A finished run, however it spelled its outcome, is not paused.
    assert!(interrupts_of(&Event::run_finished_success("t", "r")).is_empty());
    assert!(interrupts_of(&Event::run_finished("t", "r")).is_empty());
    assert!(interrupts_of(&Event::text_message_end("m")).is_empty());
}

#[test]
fn a_run_error_after_a_pause_withdraws_its_interrupts() {
    // 1.0: a RUN_ERROR after RUN_FINISHED fails the run, and a failed run
    // waits for nothing.
    let mut applier = ag_ui::client::Applier::new();
    for event in [
        Event::run_started("t", "r"),
        Event::run_finished_interrupt("t", "r", vec![approval()]),
    ] {
        applier.apply(&event).expect("applies");
    }
    assert_eq!(applier.interrupts(), [approval()]);
    applier
        .apply(&Event::run_error("failed after pausing"))
        .expect("applies");
    assert!(applier.interrupts().is_empty());
}

#[test]
fn an_empty_interrupt_outcome_is_a_protocol_error() {
    // The type system cannot say "at least one", so the applier checks.
    let mut applier = ag_ui::client::Applier::new();
    let broken = Event::run_finished_interrupt("t", "r", Vec::new());
    let error = applier
        .apply(&broken)
        .expect_err("an interrupt outcome needs an interrupt");
    assert!(
        error.to_string().contains("at least one interrupt"),
        "unexpected error: {error}"
    );
}

/// AG-UI 1.0, `basic/patterns/interrupt-resume`: "Each interrupt's `id` MUST
/// be unique within the run; a resume entry answers it by this id." The
/// refusal says that, about the event. Before: "protocol violation: invalid
/// thread snapshot: interrupt ID "i-1" is empty or duplicated".
#[tokio::test]
async fn an_interrupt_id_repeated_in_one_outcome_fails_the_run_in_the_events_words() {
    let twice = vec![
        Interrupt::new("i-1", "tool_approval"),
        Interrupt::new("i-1", "input_required"),
    ];
    let transport = ReplayTransport::new([
        Event::run_started("t", "r"),
        Event::run_finished_interrupt("t", "r", twice),
    ])
    .matching_requests();
    let mut thread = Thread::new(transport, "t");
    let report = thread.send("go").unwrap().collect_report().await;
    let RunEnd::Failed { message, .. } = &report.end else {
        panic!("{report:?}");
    };
    assert_eq!(
        message,
        "protocol violation: RUN_FINISHED carries interrupt id \"i-1\" more than once: an \
         interrupt's id is unique within its run"
    );
    assert_eq!(report.diagnostics[0].kind, "protocol");
    assert!(thread.interrupts().is_empty());
}

#[test]
fn resume_run_keeps_everything_but_the_run_id_and_the_answers() {
    let previous = RunAgentInput {
        state: json!({ "count": 1 }),
        messages: vec![Message::user("m-1", "hello")],
        ..RunAgentInput::new("thread-1", "run-1")
    };

    let next = resume_run(&previous, "run-2", vec![approval().cancel()]);

    assert_eq!(next.thread_id, previous.thread_id);
    assert_eq!(next.state, previous.state);
    assert_eq!(next.messages, previous.messages);
    assert_eq!(next.run_id, "run-2");
    assert!(next.is_resume());
}