adk_surface/workflow/view.rs
1//! The workflow observer's view models (spec 3.3 §12 developer track): what the web home's
2//! Runs tab and `adk-rust graph steps` show, built in Rust so every renderer shares them.
3//!
4//! Developer panels may use technical words (spec 3.3 §8); a decision's card stays the
5//! engine's, drawn from the inbox entry.
6
7use std::collections::BTreeMap;
8
9use chrono::{DateTime, Utc};
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12use serde_json::Value;
13
14/// A graph agent the observer shows: its nodes, edges and channels (spec 3.3 §12).
15///
16/// # Example
17///
18/// ```rust
19/// use adk_surface::workflow::{EdgeView, GraphView};
20///
21/// let graph = GraphView {
22/// name: "invoice-run".into(),
23/// description: String::new(),
24/// nodes: Vec::new(),
25/// edges: vec![EdgeView { from: "draft".into(), to: "pay".into(), conditional: true }],
26/// entry: vec!["draft".into()],
27/// channels: vec!["invoice".into(), "note".into()],
28/// };
29/// assert_eq!(serde_json::to_value(&graph).unwrap()["edges"][0]["conditional"], true);
30/// ```
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
32#[serde(rename_all = "camelCase")]
33pub struct GraphView {
34 /// The graph agent's name: `invoice-run`.
35 pub name: String,
36 /// The graph agent's description.
37 pub description: String,
38 /// Every node, sorted by name.
39 pub nodes: Vec<NodeView>,
40 /// Every edge; `to` is `__end__` for an edge that ends a branch.
41 pub edges: Vec<EdgeView>,
42 /// The nodes a run starts with.
43 pub entry: Vec<String>,
44 /// The state channels the graph declares, which a fork may edit.
45 pub channels: Vec<String>,
46}
47
48/// One node of a [`GraphView`].
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
50#[serde(rename_all = "camelCase")]
51pub struct NodeView {
52 /// The node's name.
53 pub name: String,
54 /// The node's description.
55 pub description: String,
56 /// What kind of step it is, as adk-graph names it: `modelStep`, `function`, `httpCall`, …
57 pub kind: String,
58 /// The kind in words: "Model step", "HTTP call", "Function".
59 pub kind_text: String,
60 /// "Asks first", "Spends money", "Can't be undone".
61 pub badges: Vec<String>,
62}
63
64/// One edge of a [`GraphView`].
65#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
66#[serde(rename_all = "camelCase")]
67pub struct EdgeView {
68 /// The node the edge leaves.
69 pub from: String,
70 /// The node the edge reaches, or `__end__`.
71 pub to: String,
72 /// Whether a router chooses the edge at run time.
73 pub conditional: bool,
74}
75
76/// How a run stands (spec 3.3 §12).
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
78#[serde(rename_all = "camelCase")]
79pub enum RunStatus {
80 /// The session's thread has no checkpoint yet.
81 NoRun,
82 /// A fork made and not started.
83 NotStarted,
84 /// A step is running.
85 Working,
86 /// A governed node waits for a decision.
87 Waiting,
88 /// A node paused with a question that is not a decision.
89 Paused,
90 /// Everything is stopped while the run has work left.
91 Stopped,
92 /// The run finished.
93 Done,
94 /// A step failed.
95 Failed,
96}
97
98/// What became of a node, as the run view shows it.
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
100#[serde(rename_all = "camelCase")]
101pub enum NodeState {
102 /// Not reached.
103 Idle,
104 /// Running now.
105 Working,
106 /// Finished.
107 Done,
108 /// A governed node waiting for its decision.
109 Waiting,
110 /// Paused with a question.
111 Paused,
112 /// Failed.
113 Failed,
114 /// A governed call that already happened, answered from its record.
115 Recorded,
116 /// Not run: refused, denied, or the step failed before it started.
117 NotRun,
118}
119
120/// One node's part in a run or a step (spec 3.3 §12).
121#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
122#[serde(rename_all = "camelCase")]
123pub struct NodeRun {
124 /// The node's name.
125 pub node: String,
126 /// What became of it.
127 pub state: NodeState,
128 /// In words: "Done", "Waiting for you", "Not sent again", "Not run", …
129 pub state_text: String,
130 /// When it started.
131 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub started_at: Option<DateTime<Utc>>,
133 /// That time as the home shows it: `09:14:01`.
134 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub started_text: Option<String>,
136 /// How long it ran, in milliseconds.
137 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub duration_ms: Option<u64>,
139 /// That duration in words: `1.2 s`.
140 #[serde(default, skip_serializing_if = "Option::is_none")]
141 pub duration_text: Option<String>,
142 /// Its attempt count when it finished.
143 #[serde(default, skip_serializing_if = "Option::is_none")]
144 pub attempt: Option<u32>,
145 /// The state keys it wrote, sorted.
146 #[serde(default, skip_serializing_if = "Vec::is_empty")]
147 pub writes: Vec<String>,
148 /// The error it failed or was refused with.
149 #[serde(default, skip_serializing_if = "Option::is_none")]
150 pub error: Option<String>,
151 /// The decision a waiting node waits on, from the inbox.
152 #[serde(default, skip_serializing_if = "Option::is_none")]
153 pub decision_id: Option<String>,
154 /// For a fork, the time the original run did this node: "from 09:14".
155 #[serde(default, skip_serializing_if = "Option::is_none")]
156 pub inherited_text: Option<String>,
157}
158
159/// How a step ended, as the run view lists it.
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
161#[serde(rename_all = "camelCase")]
162pub enum StepState {
163 /// The person's message, which started the run.
164 Input,
165 /// Every node finished.
166 Completed,
167 /// A node paused or waits.
168 Paused,
169 /// A node failed.
170 Failed,
171}
172
173/// One step of a run, as the Steps list shows it (spec 3.3 §12).
174///
175/// A step that paused and later completed is one row, addressed by its completed
176/// checkpoint; "Step n" is a label, never an address.
177#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
178#[serde(rename_all = "camelCase")]
179pub struct RunStep {
180 /// The step's label.
181 pub step: usize,
182 /// "Step 1".
183 pub label: String,
184 /// How the step ended.
185 pub state: StepState,
186 /// When it started.
187 pub at: DateTime<Utc>,
188 /// That time as the home shows it: `09:14:01`.
189 pub at_text: String,
190 /// The checkpoint the step detail and a fork read: the completed checkpoint of the step,
191 /// else the one its pause or failure wrote. Absent for the input.
192 #[serde(default, skip_serializing_if = "Option::is_none")]
193 pub checkpoint_id: Option<String>,
194 /// The step's nodes.
195 pub nodes: Vec<NodeRun>,
196 /// The keys the step wrote, sorted.
197 pub wrote: Vec<String>,
198 /// "Your message" for the input, "nothing written yet" for a step that waits.
199 #[serde(default, skip_serializing_if = "Option::is_none")]
200 pub text: Option<String>,
201 /// Whether a fork may start here: a completed step with work after it.
202 pub forkable: bool,
203}
204
205/// Where a forked thread came from (spec 3.3 §12).
206#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
207#[serde(rename_all = "camelCase")]
208pub struct RunLineage {
209 /// The thread the family of forks started from.
210 pub root: String,
211 /// The thread this one was forked from.
212 pub parent: String,
213 /// The session of `parent`, when it is the caller's.
214 #[serde(default, skip_serializing_if = "Option::is_none")]
215 pub parent_session: Option<String>,
216 /// The checkpoint of `parent` it was forked at.
217 pub parent_checkpoint: String,
218 /// That checkpoint's step label.
219 pub parent_step: usize,
220 /// The keys the fork overwrote.
221 pub edited: Vec<String>,
222 /// Whether the edits were checked against the graph's channels.
223 pub checked: bool,
224 /// "Fork of the 09:14 run at step 1 · edited note".
225 pub text: String,
226}
227
228/// A fork of a run, as "Forks of this run" lists it.
229#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
230#[serde(rename_all = "camelCase")]
231pub struct ForkSummary {
232 /// The fork's session.
233 pub session_id: String,
234 /// When the fork started, or was made.
235 pub at: DateTime<Utc>,
236 /// `09:31`.
237 pub at_text: String,
238 /// The step it was forked at.
239 pub parent_step: usize,
240 /// The keys it overwrote.
241 pub edited: Vec<String>,
242 /// "from step 1 · edited note".
243 pub text: String,
244}
245
246/// A run of a session's graph, as the Runs tab shows it (spec 3.3 §12).
247///
248/// Built from the checkpoints' run and step records, the effect log and the inbox; never
249/// from agent output.
250#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
251#[serde(rename_all = "camelCase")]
252pub struct WorkflowRun {
253 /// The session.
254 pub session_id: String,
255 /// The session's checkpoint thread.
256 pub thread_id: String,
257 /// The graph agent.
258 pub graph: String,
259 /// The run's id: the invocation that started it. Absent before the first run.
260 #[serde(default, skip_serializing_if = "Option::is_none")]
261 pub run_id: Option<String>,
262 /// How the run stands.
263 pub status: RunStatus,
264 /// In words: "Waiting for you", "Done", …
265 pub status_text: String,
266 /// When the run started.
267 #[serde(default, skip_serializing_if = "Option::is_none")]
268 pub started_at: Option<DateTime<Utc>>,
269 /// From the start to its latest checkpoint, in milliseconds.
270 #[serde(default, skip_serializing_if = "Option::is_none")]
271 pub duration_ms: Option<u64>,
272 /// "Started 09:14 · 4.2 s · 3 steps", or what there is to say before a run.
273 pub summary: String,
274 /// Every node of the graph, as it stands in this run.
275 pub nodes: Vec<NodeRun>,
276 /// The edges the run took.
277 pub taken: Vec<EdgeView>,
278 /// The run's steps, oldest first.
279 pub steps: Vec<RunStep>,
280 /// Where the thread was forked from.
281 #[serde(default, skip_serializing_if = "Option::is_none")]
282 pub lineage: Option<RunLineage>,
283 /// The forks made from this thread, newest first.
284 #[serde(default, skip_serializing_if = "Vec::is_empty")]
285 pub forks: Vec<ForkSummary>,
286 /// The first step still kept, when retention removed the ones before it: "Steps before 12
287 /// were removed".
288 #[serde(default, skip_serializing_if = "Option::is_none")]
289 pub removed_before: Option<usize>,
290 /// The error a failed run ended with.
291 #[serde(default, skip_serializing_if = "Option::is_none")]
292 pub error: Option<String>,
293 /// The question a paused node asked: "Which month should the invoice cover?".
294 #[serde(default, skip_serializing_if = "Option::is_none")]
295 pub question: Option<String>,
296}
297
298/// One run of a session, as `GET …/runs` lists it, from its records only.
299#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
300#[serde(rename_all = "camelCase")]
301pub struct RunSummary {
302 /// The run's id.
303 pub run_id: String,
304 /// When it started.
305 pub started_at: DateTime<Utc>,
306 /// `09:14`.
307 pub started_text: String,
308 /// Its steps, oldest first.
309 pub steps: Vec<RunStep>,
310}
311
312/// `GET /surface/workflow/sessions/{session}/runs`.
313#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
314#[serde(rename_all = "camelCase")]
315pub struct RunsResponse {
316 /// The session.
317 pub session_id: String,
318 /// The graph agent.
319 pub graph: String,
320 /// The session's checkpoint thread.
321 pub thread_id: String,
322 /// Every run whose records are kept, oldest first.
323 pub runs: Vec<RunSummary>,
324 /// Where the thread was forked from.
325 #[serde(default, skip_serializing_if = "Option::is_none")]
326 pub lineage: Option<RunLineage>,
327 /// The first step still kept, when retention removed the ones before it.
328 #[serde(default, skip_serializing_if = "Option::is_none")]
329 pub removed_before: Option<usize>,
330}
331
332/// `GET /surface/workflow/graphs`.
333#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
334#[serde(rename_all = "camelCase")]
335pub struct GraphsResponse {
336 /// Every graph agent the home observes.
337 pub graphs: Vec<GraphView>,
338}
339
340/// How a state key changed in a step.
341#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
342#[serde(rename_all = "camelCase")]
343pub enum Change {
344 /// The key was not in the state before.
345 Added,
346 /// The key's value changed.
347 Changed,
348 /// The key is not in the state after.
349 Removed,
350}
351
352/// One key of "What changed" (spec 3.3 §12): computed here so the web pane and the terminal
353/// show the same diff. Values pass secret redaction, the host's hidden keys and a size cap.
354///
355/// # Example
356///
357/// ```rust
358/// use adk_surface::workflow::{Change, StateChange};
359///
360/// let change = StateChange {
361/// key: "invoice".into(),
362/// change: Change::Added,
363/// change_text: "Added".into(),
364/// before: None,
365/// after: Some(serde_json::json!({ "amountCents": 4200 })),
366/// note: None,
367/// };
368/// assert_eq!(serde_json::to_value(&change).unwrap()["change"], "added");
369/// ```
370#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
371#[serde(rename_all = "camelCase")]
372pub struct StateChange {
373 /// The key.
374 pub key: String,
375 /// How it changed.
376 pub change: Change,
377 /// "Added", "Changed" or "Removed".
378 pub change_text: String,
379 /// The value before; absent when added.
380 #[serde(default, skip_serializing_if = "Option::is_none")]
381 pub before: Option<Value>,
382 /// The value after; absent when removed.
383 #[serde(default, skip_serializing_if = "Option::is_none")]
384 pub after: Option<Value>,
385 /// Why a value is not shown as it is: "Hidden: looks like a secret".
386 #[serde(default, skip_serializing_if = "Option::is_none")]
387 pub note: Option<String>,
388}
389
390/// One channel's value at a checkpoint, for the fork dialog's editor.
391#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
392#[serde(rename_all = "camelCase")]
393pub struct ChannelValue {
394 /// The channel.
395 pub key: String,
396 /// Its value, shown as every value is; `null` when the state does not hold it.
397 pub value: Value,
398 /// Why the value is not shown as it is.
399 #[serde(default, skip_serializing_if = "Option::is_none")]
400 pub note: Option<String>,
401 /// Whether a fork may edit it here: a value too large to show in full may not. A hidden
402 /// or redacted part of an edited value keeps the value it stands for.
403 pub editable: bool,
404}
405
406/// A governed call that already happened, from the effect log (spec 3.3 §12).
407#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
408#[serde(rename_all = "camelCase")]
409pub struct EffectView {
410 /// The governed node.
411 pub node: String,
412 /// The call in short: `POST 127.0.0.1/charges`.
413 #[serde(default, skip_serializing_if = "Option::is_none")]
414 pub call: Option<String>,
415 /// When it happened.
416 pub at: DateTime<Utc>,
417 /// `09:14`.
418 pub at_text: String,
419 /// The keys it wrote.
420 pub writes: Vec<String>,
421 /// "pay — POST 127.0.0.1/charges at 09:14".
422 pub text: String,
423}
424
425/// The decision a governed node waited on, from the inbox.
426#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
427#[serde(rename_all = "camelCase")]
428pub struct NodeDecision {
429 /// The decision's id.
430 pub id: String,
431 /// Whether it still waits.
432 pub waiting: bool,
433 /// "Asked at 09:14", or who answered: "You said yes from the home at 09:15".
434 pub text: String,
435}
436
437/// One node of a step detail.
438#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
439#[serde(rename_all = "camelCase")]
440pub struct NodeDetail {
441 /// What became of the node.
442 pub run: NodeRun,
443 /// "Model step", "HTTP call", "Function".
444 pub kind_text: String,
445 /// A model step: its model and tool activity is in the session's thread.
446 pub model_step: bool,
447 /// For a replayed call, the record that answered it: "Not sent again — the record from
448 /// 09:14 answered it".
449 #[serde(default, skip_serializing_if = "Option::is_none")]
450 pub effect: Option<EffectView>,
451 /// The decision a governed node waited on.
452 #[serde(default, skip_serializing_if = "Option::is_none")]
453 pub decision: Option<NodeDecision>,
454 /// A line under the node: "Not sent again — the record from 09:14 answered it".
455 #[serde(default, skip_serializing_if = "Option::is_none")]
456 pub text: Option<String>,
457}
458
459/// What a fork from a step would do, for the fork dialog (spec 3.3 §12).
460#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
461#[serde(rename_all = "camelCase")]
462pub struct ForkPoint {
463 /// Whether a fork may start here.
464 pub allowed: bool,
465 /// Why not: "Nothing runs after the last step".
466 #[serde(default, skip_serializing_if = "Option::is_none")]
467 pub refusal: Option<String>,
468 /// The nodes that run again: `pay`, `receipt`.
469 pub runs_again: Vec<String>,
470 /// Governed calls after this step that already happened and are answered from their
471 /// record unless the fork changes them.
472 pub already_happened: Vec<EffectView>,
473 /// What becomes of a changed governed call, when one runs again: "A changed call waits
474 /// for you." when its node asks first, else that the host's rules decide it.
475 #[serde(default, skip_serializing_if = "Option::is_none")]
476 pub changed_calls: Option<String>,
477}
478
479/// `GET /surface/workflow/sessions/{session}/checkpoints/{id}`: one step, from one
480/// checkpoint (spec 3.3 §12). Reading it changes nothing.
481#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
482#[serde(rename_all = "camelCase")]
483pub struct StepDetail {
484 /// The session.
485 pub session_id: String,
486 /// The checkpoint.
487 pub checkpoint_id: String,
488 /// The step's label.
489 pub step: usize,
490 /// "Step 2".
491 pub label: String,
492 /// How the step ended.
493 pub state: StepState,
494 /// When it started: `09:14:03`.
495 pub at_text: String,
496 /// The step's nodes.
497 pub nodes: Vec<NodeDetail>,
498 /// What changed, by key.
499 pub changes: Vec<StateChange>,
500 /// The state at this checkpoint, one row per channel.
501 pub state_values: Vec<ChannelValue>,
502 /// What a fork from here would do.
503 pub fork: ForkPoint,
504}
505
506/// `POST /surface/workflow/sessions/{session}/fork` (spec 3.3 §12).
507///
508/// # Example
509///
510/// ```rust
511/// use adk_surface::workflow::ForkRequest;
512///
513/// let request: ForkRequest = serde_json::from_value(serde_json::json!({
514/// "from": "c1",
515/// "set": { "note": "re-run for audit" },
516/// "start": true,
517/// }))
518/// .unwrap();
519/// assert!(request.start);
520/// ```
521#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
522#[serde(rename_all = "camelCase")]
523pub struct ForkRequest {
524 /// The completed checkpoint of the step to fork from.
525 pub from: String,
526 /// Channels to overwrite in the fork, whole.
527 #[serde(default)]
528 pub set: BTreeMap<String, Value>,
529 /// Start the fork's run now.
530 #[serde(default)]
531 pub start: bool,
532}
533
534/// What a fork did (spec 3.3 §12).
535#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
536#[serde(rename_all = "camelCase")]
537pub struct ForkOutcome {
538 /// The fork's new session.
539 pub session_id: String,
540 /// Its checkpoint thread.
541 pub thread_id: String,
542 /// The checkpoint it was forked from.
543 pub from: String,
544 /// The keys it overwrote.
545 pub edited: Vec<String>,
546 /// Whether its run started.
547 pub started: bool,
548 /// "Forked. The original run is unchanged."
549 pub text: String,
550}