adk-surface 3.0.0

Agent surfaces for ADK-Rust: one engine that turns a kernel's sessions, decisions and activity into plain words for every surface, and the surfaces that show 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
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
//! The workflow observer's view models (spec 3.3 §12 developer track): what the web home's
//! Runs tab and `adk-rust graph steps` show, built in Rust so every renderer shares them.
//!
//! Developer panels may use technical words (spec 3.3 §8); a decision's card stays the
//! engine's, drawn from the inbox entry.

use std::collections::BTreeMap;

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A graph agent the observer shows: its nodes, edges and channels (spec 3.3 §12).
///
/// # Example
///
/// ```rust
/// use adk_surface::workflow::{EdgeView, GraphView};
///
/// let graph = GraphView {
///     name: "invoice-run".into(),
///     description: String::new(),
///     nodes: Vec::new(),
///     edges: vec![EdgeView { from: "draft".into(), to: "pay".into(), conditional: true }],
///     entry: vec!["draft".into()],
///     channels: vec!["invoice".into(), "note".into()],
/// };
/// assert_eq!(serde_json::to_value(&graph).unwrap()["edges"][0]["conditional"], true);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct GraphView {
    /// The graph agent's name: `invoice-run`.
    pub name: String,
    /// The graph agent's description.
    pub description: String,
    /// Every node, sorted by name.
    pub nodes: Vec<NodeView>,
    /// Every edge; `to` is `__end__` for an edge that ends a branch.
    pub edges: Vec<EdgeView>,
    /// The nodes a run starts with.
    pub entry: Vec<String>,
    /// The state channels the graph declares, which a fork may edit.
    pub channels: Vec<String>,
}

/// One node of a [`GraphView`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NodeView {
    /// The node's name.
    pub name: String,
    /// The node's description.
    pub description: String,
    /// What kind of step it is, as adk-graph names it: `modelStep`, `function`, `httpCall`, …
    pub kind: String,
    /// The kind in words: "Model step", "HTTP call", "Function".
    pub kind_text: String,
    /// "Asks first", "Spends money", "Can't be undone".
    pub badges: Vec<String>,
}

/// One edge of a [`GraphView`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct EdgeView {
    /// The node the edge leaves.
    pub from: String,
    /// The node the edge reaches, or `__end__`.
    pub to: String,
    /// Whether a router chooses the edge at run time.
    pub conditional: bool,
}

/// How a run stands (spec 3.3 §12).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum RunStatus {
    /// The session's thread has no checkpoint yet.
    NoRun,
    /// A fork made and not started.
    NotStarted,
    /// A step is running.
    Working,
    /// A governed node waits for a decision.
    Waiting,
    /// A node paused with a question that is not a decision.
    Paused,
    /// Everything is stopped while the run has work left.
    Stopped,
    /// The run finished.
    Done,
    /// A step failed.
    Failed,
}

/// What became of a node, as the run view shows it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum NodeState {
    /// Not reached.
    Idle,
    /// Running now.
    Working,
    /// Finished.
    Done,
    /// A governed node waiting for its decision.
    Waiting,
    /// Paused with a question.
    Paused,
    /// Failed.
    Failed,
    /// A governed call that already happened, answered from its record.
    Recorded,
    /// Not run: refused, denied, or the step failed before it started.
    NotRun,
}

/// One node's part in a run or a step (spec 3.3 §12).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NodeRun {
    /// The node's name.
    pub node: String,
    /// What became of it.
    pub state: NodeState,
    /// In words: "Done", "Waiting for you", "Not sent again", "Not run", …
    pub state_text: String,
    /// When it started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<DateTime<Utc>>,
    /// That time as the home shows it: `09:14:01`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_text: Option<String>,
    /// How long it ran, in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u64>,
    /// That duration in words: `1.2 s`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_text: Option<String>,
    /// Its attempt count when it finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attempt: Option<u32>,
    /// The state keys it wrote, sorted.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub writes: Vec<String>,
    /// The error it failed or was refused with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// The decision a waiting node waits on, from the inbox.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub decision_id: Option<String>,
    /// For a fork, the time the original run did this node: "from 09:14".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inherited_text: Option<String>,
}

/// How a step ended, as the run view lists it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum StepState {
    /// The person's message, which started the run.
    Input,
    /// Every node finished.
    Completed,
    /// A node paused or waits.
    Paused,
    /// A node failed.
    Failed,
}

/// One step of a run, as the Steps list shows it (spec 3.3 §12).
///
/// A step that paused and later completed is one row, addressed by its completed
/// checkpoint; "Step n" is a label, never an address.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct RunStep {
    /// The step's label.
    pub step: usize,
    /// "Step 1".
    pub label: String,
    /// How the step ended.
    pub state: StepState,
    /// When it started.
    pub at: DateTime<Utc>,
    /// That time as the home shows it: `09:14:01`.
    pub at_text: String,
    /// The checkpoint the step detail and a fork read: the completed checkpoint of the step,
    /// else the one its pause or failure wrote. Absent for the input.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checkpoint_id: Option<String>,
    /// The step's nodes.
    pub nodes: Vec<NodeRun>,
    /// The keys the step wrote, sorted.
    pub wrote: Vec<String>,
    /// "Your message" for the input, "nothing written yet" for a step that waits.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Whether a fork may start here: a completed step with work after it.
    pub forkable: bool,
}

/// Where a forked thread came from (spec 3.3 §12).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct RunLineage {
    /// The thread the family of forks started from.
    pub root: String,
    /// The thread this one was forked from.
    pub parent: String,
    /// The session of `parent`, when it is the caller's.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_session: Option<String>,
    /// The checkpoint of `parent` it was forked at.
    pub parent_checkpoint: String,
    /// That checkpoint's step label.
    pub parent_step: usize,
    /// The keys the fork overwrote.
    pub edited: Vec<String>,
    /// Whether the edits were checked against the graph's channels.
    pub checked: bool,
    /// "Fork of the 09:14 run at step 1 · edited note".
    pub text: String,
}

/// A fork of a run, as "Forks of this run" lists it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForkSummary {
    /// The fork's session.
    pub session_id: String,
    /// When the fork started, or was made.
    pub at: DateTime<Utc>,
    /// `09:31`.
    pub at_text: String,
    /// The step it was forked at.
    pub parent_step: usize,
    /// The keys it overwrote.
    pub edited: Vec<String>,
    /// "from step 1 · edited note".
    pub text: String,
}

/// A run of a session's graph, as the Runs tab shows it (spec 3.3 §12).
///
/// Built from the checkpoints' run and step records, the effect log and the inbox; never
/// from agent output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct WorkflowRun {
    /// The session.
    pub session_id: String,
    /// The session's checkpoint thread.
    pub thread_id: String,
    /// The graph agent.
    pub graph: String,
    /// The run's id: the invocation that started it. Absent before the first run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
    /// How the run stands.
    pub status: RunStatus,
    /// In words: "Waiting for you", "Done", …
    pub status_text: String,
    /// When the run started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<DateTime<Utc>>,
    /// From the start to its latest checkpoint, in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u64>,
    /// "Started 09:14 · 4.2 s · 3 steps", or what there is to say before a run.
    pub summary: String,
    /// Every node of the graph, as it stands in this run.
    pub nodes: Vec<NodeRun>,
    /// The edges the run took.
    pub taken: Vec<EdgeView>,
    /// The run's steps, oldest first.
    pub steps: Vec<RunStep>,
    /// Where the thread was forked from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lineage: Option<RunLineage>,
    /// The forks made from this thread, newest first.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub forks: Vec<ForkSummary>,
    /// The first step still kept, when retention removed the ones before it: "Steps before 12
    /// were removed".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub removed_before: Option<usize>,
    /// The error a failed run ended with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// The question a paused node asked: "Which month should the invoice cover?".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub question: Option<String>,
}

/// One run of a session, as `GET …/runs` lists it, from its records only.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct RunSummary {
    /// The run's id.
    pub run_id: String,
    /// When it started.
    pub started_at: DateTime<Utc>,
    /// `09:14`.
    pub started_text: String,
    /// Its steps, oldest first.
    pub steps: Vec<RunStep>,
}

/// `GET /surface/workflow/sessions/{session}/runs`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct RunsResponse {
    /// The session.
    pub session_id: String,
    /// The graph agent.
    pub graph: String,
    /// The session's checkpoint thread.
    pub thread_id: String,
    /// Every run whose records are kept, oldest first.
    pub runs: Vec<RunSummary>,
    /// Where the thread was forked from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lineage: Option<RunLineage>,
    /// The first step still kept, when retention removed the ones before it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub removed_before: Option<usize>,
}

/// `GET /surface/workflow/graphs`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct GraphsResponse {
    /// Every graph agent the home observes.
    pub graphs: Vec<GraphView>,
}

/// How a state key changed in a step.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Change {
    /// The key was not in the state before.
    Added,
    /// The key's value changed.
    Changed,
    /// The key is not in the state after.
    Removed,
}

/// One key of "What changed" (spec 3.3 §12): computed here so the web pane and the terminal
/// show the same diff. Values pass secret redaction, the host's hidden keys and a size cap.
///
/// # Example
///
/// ```rust
/// use adk_surface::workflow::{Change, StateChange};
///
/// let change = StateChange {
///     key: "invoice".into(),
///     change: Change::Added,
///     change_text: "Added".into(),
///     before: None,
///     after: Some(serde_json::json!({ "amountCents": 4200 })),
///     note: None,
/// };
/// assert_eq!(serde_json::to_value(&change).unwrap()["change"], "added");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct StateChange {
    /// The key.
    pub key: String,
    /// How it changed.
    pub change: Change,
    /// "Added", "Changed" or "Removed".
    pub change_text: String,
    /// The value before; absent when added.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub before: Option<Value>,
    /// The value after; absent when removed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<Value>,
    /// Why a value is not shown as it is: "Hidden: looks like a secret".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

/// One channel's value at a checkpoint, for the fork dialog's editor.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ChannelValue {
    /// The channel.
    pub key: String,
    /// Its value, shown as every value is; `null` when the state does not hold it.
    pub value: Value,
    /// Why the value is not shown as it is.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    /// Whether a fork may edit it here: a value too large to show in full may not. A hidden
    /// or redacted part of an edited value keeps the value it stands for.
    pub editable: bool,
}

/// A governed call that already happened, from the effect log (spec 3.3 §12).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct EffectView {
    /// The governed node.
    pub node: String,
    /// The call in short: `POST 127.0.0.1/charges`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub call: Option<String>,
    /// When it happened.
    pub at: DateTime<Utc>,
    /// `09:14`.
    pub at_text: String,
    /// The keys it wrote.
    pub writes: Vec<String>,
    /// "pay — POST 127.0.0.1/charges at 09:14".
    pub text: String,
}

/// The decision a governed node waited on, from the inbox.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NodeDecision {
    /// The decision's id.
    pub id: String,
    /// Whether it still waits.
    pub waiting: bool,
    /// "Asked at 09:14", or who answered: "You said yes from the home at 09:15".
    pub text: String,
}

/// One node of a step detail.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NodeDetail {
    /// What became of the node.
    pub run: NodeRun,
    /// "Model step", "HTTP call", "Function".
    pub kind_text: String,
    /// A model step: its model and tool activity is in the session's thread.
    pub model_step: bool,
    /// For a replayed call, the record that answered it: "Not sent again — the record from
    /// 09:14 answered it".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effect: Option<EffectView>,
    /// The decision a governed node waited on.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub decision: Option<NodeDecision>,
    /// A line under the node: "Not sent again — the record from 09:14 answered it".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

/// What a fork from a step would do, for the fork dialog (spec 3.3 §12).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForkPoint {
    /// Whether a fork may start here.
    pub allowed: bool,
    /// Why not: "Nothing runs after the last step".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub refusal: Option<String>,
    /// The nodes that run again: `pay`, `receipt`.
    pub runs_again: Vec<String>,
    /// Governed calls after this step that already happened and are answered from their
    /// record unless the fork changes them.
    pub already_happened: Vec<EffectView>,
    /// What becomes of a changed governed call, when one runs again: "A changed call waits
    /// for you." when its node asks first, else that the host's rules decide it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub changed_calls: Option<String>,
}

/// `GET /surface/workflow/sessions/{session}/checkpoints/{id}`: one step, from one
/// checkpoint (spec 3.3 §12). Reading it changes nothing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct StepDetail {
    /// The session.
    pub session_id: String,
    /// The checkpoint.
    pub checkpoint_id: String,
    /// The step's label.
    pub step: usize,
    /// "Step 2".
    pub label: String,
    /// How the step ended.
    pub state: StepState,
    /// When it started: `09:14:03`.
    pub at_text: String,
    /// The step's nodes.
    pub nodes: Vec<NodeDetail>,
    /// What changed, by key.
    pub changes: Vec<StateChange>,
    /// The state at this checkpoint, one row per channel.
    pub state_values: Vec<ChannelValue>,
    /// What a fork from here would do.
    pub fork: ForkPoint,
}

/// `POST /surface/workflow/sessions/{session}/fork` (spec 3.3 §12).
///
/// # Example
///
/// ```rust
/// use adk_surface::workflow::ForkRequest;
///
/// let request: ForkRequest = serde_json::from_value(serde_json::json!({
///     "from": "c1",
///     "set": { "note": "re-run for audit" },
///     "start": true,
/// }))
/// .unwrap();
/// assert!(request.start);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForkRequest {
    /// The completed checkpoint of the step to fork from.
    pub from: String,
    /// Channels to overwrite in the fork, whole.
    #[serde(default)]
    pub set: BTreeMap<String, Value>,
    /// Start the fork's run now.
    #[serde(default)]
    pub start: bool,
}

/// What a fork did (spec 3.3 §12).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForkOutcome {
    /// The fork's new session.
    pub session_id: String,
    /// Its checkpoint thread.
    pub thread_id: String,
    /// The checkpoint it was forked from.
    pub from: String,
    /// The keys it overwrote.
    pub edited: Vec<String>,
    /// Whether its run started.
    pub started: bool,
    /// "Forked. The original run is unchanged."
    pub text: String,
}