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
//! The human-in-the-loop round trip.
//!
//! A run does not only succeed or fail. It can *pause*: the agent finishes with
//! an [interrupt outcome](https://kimsoungryoul.github.io/ag-ui-rust/api/ag_ui/outcome/enum.RunOutcome.html#variant.Interrupt), listing what it
//! needs a human to decide, and the conversation continues when the client
//! sends the answers back in [`RunAgentInput::resume`](https://kimsoungryoul.github.io/ag-ui-rust/api/ag_ui/input/struct.RunAgentInput.html#structfield.resume).
//!
//! That round trip is the whole reason `RUN_FINISHED` carries an outcome, and
//! this module is the client half of it. With a [`Thread`](crate::client::Thread) it
//! is two calls:
//!
//! ```
//! # use ag_ui::client::{Thread, Update, transport::ReplayTransport};
//! # use ag_ui::{Event, Interrupt};
//! # use futures_util::StreamExt;
//! # let transport = ReplayTransport::with_runs([
//! # vec![
//! # Event::run_started("thread-1", "run-1"),
//! # Event::run_finished_interrupt("thread-1", "run-1", vec![Interrupt::new("i-1", "tool_approval")]),
//! # ],
//! # vec![
//! # Event::run_started("thread-1", "run-2"),
//! # Event::run_finished_success("thread-1", "run-2"),
//! # ],
//! # ]).matching_requests();
//! # let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
//! # rt.block_on(async {
//! let mut thread = Thread::<_>::new(transport, "thread-1");
//! let mut pending = Vec::new();
//!
//! let mut run = thread.send("delete the staging database").unwrap();
//! while let Some(update) = run.next().await {
//! if let Update::Interrupt(interrupt) = update {
//! pending.push(interrupt);
//! }
//! }
//! drop(run);
//!
//! // Ask the human, then answer the agent.
//! let mut resumed = thread.resume(&pending[0], serde_json::json!({ "approved": true })).unwrap();
//! while resumed.next().await.is_some() {}
//! # });
//! ```
//!
//! Without one — a proxy, or anything driving [`crate::client::RemoteAgent`] directly —
//! [`interrupts_of`] finds the interrupts on the event and [`resume_run`]
//! builds the next request.
use crate::;
use Value;
/// The interrupts a `RUN_FINISHED` paused on, or an empty slice for any other
/// event.
///
/// A `RUN_FINISHED` with no outcome at all is a success: producers that predate
/// the interrupt protocol omit the field, and reading that as "paused" would
/// hang every one of them.
/// Builds the request that resumes a paused run.
///
/// Everything the paused run was given — messages, state, tools, context — is
/// carried over, because the agent is continuing the same conversation. What
/// changes is the run id and the `resume` payload.
///
/// A new run id, not the paused one: the resumed run emits its own
/// `RUN_STARTED`, and reusing the finished run's id would make two runs in one
/// thread indistinguishable in a log. Servers that key resumption on the
/// original id should be passed that id explicitly.
/// Answering interrupts, one call per decision.
///
/// A run can pause on several interrupts at once — three tool approvals, say —
/// and they are answered together, in one request. This collects the answers.
///
/// ```
/// use ag_ui::client::interrupts::ResumeBuilder;
/// use ag_ui::{Interrupt, ResumeStatus};
///
/// let approve = Interrupt::new("i-1", "tool_approval");
/// let deny = Interrupt::new("i-2", "tool_approval");
///
/// let entries = ResumeBuilder::new()
/// .resolve(&approve, serde_json::json!({ "approved": true }))
/// .cancel(&deny)
/// .build();
///
/// assert_eq!(entries[0].interrupt_id, "i-1");
/// assert_eq!(entries[1].status, ResumeStatus::Cancelled);
/// ```
/// Answering one interrupt, on the interrupt itself.
///
/// The protocol type lives in `ag-ui-core`, which has no opinion about
/// consuming a run; these are the two things a client always does with one.