tilo-actions 0.5.2

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store.
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
//! Every invocation as a draft a person can read, change and confirm.
//!
//! Where a delegate's action asks first, the person is shown exactly what
//! will happen before it does. [`Draft::render`] derives that from the
//! action's inert descriptor and the arguments it would run with, so every
//! application's actions are draftable without code of their own: a field
//! per parameter (an object parameter's fields one level down), resource
//! references and attachments by label, secrets withheld. An action that
//! wants a richer preview names its layout with a contract tag
//! ([`MESSAGE_PREVIEW_TAG`]); a parameter shaped like a message (recipients
//! and a body) is previewed as one without it.
//!
//! What the person confirms is bound to the exact arguments
//! ([`arguments_digest`]): changing a field ([`Draft::apply_edits`]) makes
//! new arguments, and those are what run.

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::{ActionDescriptor, ParamType};

/// The error code an invocation is answered with while the person it is
/// made for has not yet confirmed it. The caller waits and asks again with
/// the same arguments; nothing has run.
pub const AWAITING_CONFIRMATION: &str = "action_awaiting_confirmation";

/// The error code an invocation an agent makes inside a turn is answered with
/// while the message that caused it has not reached this device: the caller
/// waits and asks again once it has; nothing has run.
pub const AWAITING_CAUSE: &str = "action_awaiting_cause";

/// The error code an invocation is answered with when the person declined
/// it. What follows the code is the reason they gave, if any.
pub const DECLINED: &str = "action_declined";

/// Contract tag of an action whose draft is previewed as a message: who it
/// goes to, its subject, its body and what it carries.
pub const MESSAGE_PREVIEW_TAG: &str = "draft:message";

/// Longest text a draft keeps for one field; a longer value is shown cut
/// and cannot be edited in place.
pub const MAX_FIELD_CHARS: usize = 64 * 1024;

/// Longest value a [`Draft::summary`] line shows before it is cut.
pub const SUMMARY_CHARS: usize = 48;

/// Most entries a list field names before saying how many more there are.
const MAX_LISTED: usize = 12;

/// How a draft is laid out.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DraftLayout {
    /// One labelled field per parameter.
    #[default]
    Fields,
    /// A message: recipients, subject, body and attachments.
    Message,
}

/// The role a field plays in a [`DraftLayout::Message`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageRole {
    To,
    Cc,
    Bcc,
    Subject,
    Body,
    Attachments,
}

impl MessageRole {
    fn of(name: &str) -> Option<Self> {
        Some(match name {
            "to" | "recipients" => Self::To,
            "cc" => Self::Cc,
            "bcc" => Self::Bcc,
            "subject" | "title" => Self::Subject,
            "body" | "text" | "message" => Self::Body,
            "attachments" | "files" => Self::Attachments,
            _ => return None,
        })
    }
}

/// What one field holds.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(remote = "Self", rename_all = "snake_case")]
pub enum DraftValue {
    Text {
        text: String,
    },
    Number {
        text: String,
    },
    Flag {
        on: bool,
    },
    /// Several entries, each by its label: `3 attachments: a.pdf, b.md, c.png`.
    List {
        count: usize,
        labels: Vec<String>,
    },
    /// Another resource, by its label.
    Resource {
        label: String,
    },
    /// A secret: shown as withheld, never copied into the draft.
    Withheld,
}

crate::tagged_through_json_value!(DraftValue, "type");

impl DraftValue {
    /// The value in one line, as a person reads it (a list's labels joined,
    /// a flag as `yes`/`no`). Interfaces with their own wording for counts
    /// and flags build the line themselves.
    #[must_use]
    pub fn line(&self) -> String {
        match self {
            Self::Text { text } | Self::Number { text } => text.clone(),
            Self::Flag { on } => if *on { "yes" } else { "no" }.to_owned(),
            Self::List { count, labels } => {
                let line = labels.join(", ");
                match count.checked_sub(labels.len()) {
                    Some(more) if more > 0 => format!("{line} +{more}"),
                    _ => line,
                }
            }
            Self::Resource { label } => label.clone(),
            Self::Withheld => "•••".to_owned(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DraftField {
    /// Where the value sits in the arguments: the parameter, then the
    /// field of an object parameter.
    pub path: Vec<String>,
    pub label: String,
    pub value: DraftValue,
    /// Whether a person may change it before confirming.
    #[serde(default)]
    pub editable: bool,
    /// Whether it is prose that wants several lines.
    #[serde(default)]
    pub multiline: bool,
    /// What the row reads instead of the raw value, where the value names one
    /// of its parameter's choices: a field id reads as the field's name
    /// ("Body"), a pick as the thing it picks. What runs is still `value`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shows: Option<String>,
    /// Its part in a message preview.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<MessageRole>,
}

/// An invocation as a person reviews it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Draft {
    pub action_label: String,
    pub resource_label: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default)]
    pub layout: DraftLayout,
    pub fields: Vec<DraftField>,
    /// Whether the action, once taken, can be undone
    /// ([`ActionContract::can_be_undone`](crate::ActionContract::can_be_undone)):
    /// a reviewer is told whether it sends something away or changes their
    /// own work.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub undoable: bool,
}

/// One change a person made to a draft's field.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DraftEdit {
    pub path: Vec<String>,
    pub text: String,
}

impl Draft {
    #[must_use]
    pub fn render(descriptor: &ActionDescriptor, arguments: &Map<String, Value>) -> Self {
        let mut fields = Vec::new();
        for param in &descriptor.params {
            let Some(value) = arguments.get(param.name.as_ref()) else {
                continue;
            };
            let path = vec![param.name.to_string()];
            match (param.ty, value) {
                (ParamType::Secret, _) => {
                    fields.push(field(path, &param.name, DraftValue::Withheld));
                }
                // A secret named by reference reads as a secret does: the
                // address of a vault item tells a reviewer nothing.
                (ParamType::Resource, _) if crate::remote::takes_item(param) => {
                    fields.push(field(path, &param.name, DraftValue::Withheld));
                }
                (ParamType::Object, Value::Object(object)) => {
                    let properties = param
                        .schema
                        .as_deref()
                        .and_then(|schema| schema.get("properties"))
                        .and_then(Value::as_object);
                    for (name, inner) in object {
                        let schema = properties.and_then(|properties| properties.get(name));
                        let mut path = path.clone();
                        path.push(name.clone());
                        if let Some(field) = value_field(path, name, inner, schema) {
                            fields.push(field);
                        }
                    }
                }
                (ty, value) => {
                    let schema = param.schema.as_deref();
                    let mut made = value_field(path, &param.name, value, schema);
                    if let Some(made) = made.as_mut()
                        && ty == ParamType::Resource
                    {
                        made.value = DraftValue::Resource {
                            label: made.value.line(),
                        };
                        made.editable = false;
                    }
                    // A value that names one of the parameter's choices reads by
                    // the label the surface publishes for it: a field id reads as
                    // the field's name, a pick as the thing it picks. `value`
                    // stays the value the action binds, so what runs is
                    // unchanged; only what the person reads changes.
                    if let (Some(made), Some(raw)) = (made.as_mut(), value.as_str())
                        && !raw.is_empty()
                        && let Some(label) = param.choice_label(raw)
                        && !label.trim().is_empty()
                    {
                        made.shows = Some(label.to_owned());
                    }
                    fields.extend(made);
                }
            }
        }
        let message = descriptor
            .contract
            .tags
            .iter()
            .any(|tag| tag == MESSAGE_PREVIEW_TAG)
            || (fields
                .iter()
                .any(|field| field.role == Some(MessageRole::To))
                && fields
                    .iter()
                    .any(|field| field.role == Some(MessageRole::Body)));
        if !message {
            for field in &mut fields {
                field.role = None;
            }
        }
        Self {
            action_label: descriptor.action_label.clone(),
            resource_label: descriptor.resource_label.clone(),
            description: descriptor.description.clone(),
            layout: if message {
                DraftLayout::Message
            } else {
                DraftLayout::Fields
            },
            fields,
            undoable: descriptor.contract.can_be_undone(),
        }
    }

    #[must_use]
    pub fn role(&self, role: MessageRole) -> Option<&DraftField> {
        self.fields.iter().find(|field| field.role == Some(role))
    }

    /// What a person reads in `field`'s one line: the label it shows where
    /// the value names a choice, its value otherwise.
    #[must_use]
    pub fn shown_line(field: &DraftField) -> String {
        field.shows.clone().unwrap_or_else(|| field.value.line())
    }

    /// What the draft is about, in one line of its own values: a message's
    /// recipients and subject, otherwise its first short values. `None` for a
    /// draft with nothing to show.
    #[must_use]
    pub fn gist(&self) -> Option<String> {
        if self.layout == DraftLayout::Message {
            let parts: Vec<String> = [MessageRole::To, MessageRole::Subject]
                .into_iter()
                .filter_map(|role| self.role(role))
                .map(Draft::shown_line)
                .filter(|line| !line.is_empty())
                .collect();
            if !parts.is_empty() {
                return Some(parts.join(" · "));
            }
        }
        let parts: Vec<String> = self
            .fields
            .iter()
            .filter(|field| !field.multiline && field.value != DraftValue::Withheld)
            .map(Draft::shown_line)
            .filter(|line| !line.is_empty())
            .take(2)
            .collect();
        (!parts.is_empty()).then(|| parts.join(" · "))
    }

    /// Each field as one line a reviewer reads at a glance, by its label:
    /// a long value cut at [`SUMMARY_CHARS`] with an ellipsis, a secret
    /// withheld.
    #[must_use]
    pub fn summary(&self) -> Vec<(String, String)> {
        self.fields
            .iter()
            .map(|field| {
                let line = field
                    .value
                    .line()
                    .split_whitespace()
                    .collect::<Vec<_>>()
                    .join(" ");
                let cut = if line.chars().count() > SUMMARY_CHARS {
                    let kept: String = line.chars().take(SUMMARY_CHARS).collect();
                    format!("{}…", kept.trim_end())
                } else {
                    line
                };
                (field.label.clone(), cut)
            })
            .collect()
    }

    /// `arguments` with the person's `edits` applied. Only fields this draft
    /// offers for editing change, each keeping its JSON type.
    pub fn apply_edits(
        &self,
        arguments: &Map<String, Value>,
        edits: &[DraftEdit],
    ) -> Result<Map<String, Value>, String> {
        let mut edited = arguments.clone();
        for edit in edits {
            set_path(&mut edited, &edit.path, self.edit_value(edit)?)?;
        }
        Ok(edited)
    }

    /// Whether every one of `edits` changes a field this draft offers for
    /// editing to a value of its type.
    pub fn check_edits(&self, edits: &[DraftEdit]) -> Result<(), String> {
        edits
            .iter()
            .try_for_each(|edit| self.edit_value(edit).map(drop))
    }

    fn edit_value(&self, edit: &DraftEdit) -> Result<Value, String> {
        let field = self
            .fields
            .iter()
            .find(|field| field.path == edit.path && field.editable)
            .ok_or_else(|| format!("{} cannot be changed here", edit.path.join(".")))?;
        if edit.text.chars().count() > MAX_FIELD_CHARS {
            return Err(format!("{} is too long", field.label));
        }
        Ok(match &field.value {
            DraftValue::Text { .. } => Value::String(edit.text.clone()),
            DraftValue::Number { .. } => {
                let number: serde_json::Number = edit
                    .text
                    .trim()
                    .parse()
                    .map_err(|_| format!("{} needs a number", field.label))?;
                Value::Number(number)
            }
            DraftValue::Flag { .. } => match edit.text.trim() {
                "true" | "yes" => Value::Bool(true),
                "false" | "no" => Value::Bool(false),
                _ => return Err(format!("{} needs yes or no", field.label)),
            },
            _ => return Err(format!("{} cannot be changed here", field.label)),
        })
    }

    /// The arguments this draft shows: each text, number and flag field set
    /// at its path. `None` when a field shows its value only in part (a
    /// secret, a list, a resource, a text cut short). A draft also leaves
    /// out nulls and empty texts, so what this returns is only a candidate:
    /// act on it only when it digests ([`arguments_digest`]) to the digest
    /// the draft was confirmed for.
    #[must_use]
    pub fn arguments(&self) -> Option<Map<String, Value>> {
        let mut arguments = Map::new();
        for field in &self.fields {
            let value = match &field.value {
                DraftValue::Text { text } => Value::String(text.clone()),
                DraftValue::Number { text } => Value::Number(text.parse().ok()?),
                DraftValue::Flag { on } => Value::Bool(*on),
                DraftValue::List { .. } | DraftValue::Resource { .. } | DraftValue::Withheld => {
                    return None;
                }
            };
            let (last, parents) = field.path.split_last()?;
            let mut object = &mut arguments;
            for parent in parents {
                object = object
                    .entry(parent.clone())
                    .or_insert_with(|| Value::Object(Map::new()))
                    .as_object_mut()?;
            }
            object.insert(last.clone(), value);
        }
        Some(arguments)
    }

    /// The draft as `edits` change it: what the person confirmed, shown
    /// back to them. Edits this draft does not offer are left out.
    #[must_use]
    pub fn edited(&self, edits: &[DraftEdit]) -> Self {
        let mut draft = self.clone();
        for edit in edits {
            let Ok(value) = self.edit_value(edit) else {
                continue;
            };
            if let Some(field) = draft
                .fields
                .iter_mut()
                .find(|field| field.path == edit.path)
            {
                field.value = match value {
                    Value::Bool(on) => DraftValue::Flag { on },
                    Value::Number(number) => DraftValue::Number {
                        text: number.to_string(),
                    },
                    _ => DraftValue::Text {
                        text: edit.text.clone(),
                    },
                };
                // The person typed a new value: no label of a choice stands
                // over what they wrote.
                field.shows = None;
            }
        }
        draft
    }
}

/// The digest binding a confirmation to the exact arguments it was given
/// for. Keys are ordered, so the same arguments always digest the same.
#[must_use]
pub fn arguments_digest(arguments: &Map<String, Value>) -> [u8; 32] {
    let mut hasher = blake3::Hasher::new_derive_key("Tilo draft arguments v1");
    tilo_canonical_json::write_object_bytes(arguments, |chunk| {
        hasher.update(chunk);
    });
    *hasher.finalize().as_bytes()
}

fn set_path(
    arguments: &mut Map<String, Value>,
    path: &[String],
    value: Value,
) -> Result<(), String> {
    let Some((last, parents)) = path.split_last() else {
        return Err("an edit names no field".into());
    };
    let mut object = arguments;
    for parent in parents {
        object = object
            .get_mut(parent)
            .and_then(Value::as_object_mut)
            .ok_or_else(|| format!("{} is not in the draft", path.join(".")))?;
    }
    object.insert(last.clone(), value);
    Ok(())
}

fn field(path: Vec<String>, name: &str, value: DraftValue) -> DraftField {
    let role = path.last().and_then(|last| MessageRole::of(last));
    DraftField {
        path,
        label: humanize(name),
        value,
        editable: false,
        multiline: false,
        shows: None,
        role,
    }
}

/// The field for one value; `None` for nothing worth showing (null, an
/// empty text or list).
fn value_field(
    path: Vec<String>,
    name: &str,
    value: &Value,
    schema: Option<&Value>,
) -> Option<DraftField> {
    let label = schema
        .and_then(|schema| schema.get("title"))
        .and_then(Value::as_str)
        .map_or_else(|| humanize(name), str::to_owned);
    let mut made = field(path, name, DraftValue::Withheld);
    made.label = label;
    if schema
        .and_then(|schema| schema.get("format"))
        .is_some_and(|format| format == "password")
    {
        return Some(made);
    }
    match value {
        Value::Null => return None,
        Value::String(text) if text.trim().is_empty() => return None,
        Value::String(text) => {
            let long = text.chars().count() > MAX_FIELD_CHARS;
            let shown: String = text.chars().take(MAX_FIELD_CHARS).collect();
            made.multiline = text.contains('\n')
                || text.chars().count() > 120
                || made.role == Some(MessageRole::Body);
            made.editable = !long;
            made.value = DraftValue::Text { text: shown };
        }
        Value::Number(number) => {
            made.editable = true;
            made.value = DraftValue::Number {
                text: number.to_string(),
            };
        }
        Value::Bool(on) => {
            made.editable = true;
            made.value = DraftValue::Flag { on: *on };
        }
        Value::Array(items) if items.is_empty() => return None,
        Value::Array(items) => {
            made.value = DraftValue::List {
                count: items.len(),
                labels: items.iter().take(MAX_LISTED).map(entry_label).collect(),
            };
        }
        Value::Object(object) => {
            made.value = DraftValue::Resource {
                label: object_label(object).unwrap_or_else(|| humanize(name)),
            };
        }
    }
    Some(made)
}

/// The keys an entry or referenced resource is named by, best first.
const LABEL_KEYS: [&str; 8] = [
    "label",
    "name",
    "title",
    "filename",
    "file_name",
    "path",
    "address",
    "id",
];

fn object_label(object: &Map<String, Value>) -> Option<String> {
    LABEL_KEYS.iter().find_map(|key| {
        let text = object.get(*key)?.as_str()?;
        let text = if *key == "path" {
            text.rsplit('/').next().unwrap_or(text)
        } else {
            text
        };
        (!text.is_empty()).then(|| text.to_owned())
    })
}

fn entry_label(value: &Value) -> String {
    match value {
        Value::String(text) => text.rsplit('/').next().unwrap_or(text).to_owned(),
        Value::Object(object) => object_label(object).unwrap_or_else(|| "…".to_owned()),
        other => other.to_string(),
    }
}

/// `in_reply_to` as `In reply to`.
fn humanize(name: &str) -> String {
    let spaced = name.replace(['_', '-'], " ");
    let mut chars = spaced.trim().chars();
    chars.next().map_or_else(String::new, |first| {
        first.to_uppercase().chain(chars).collect()
    })
}

#[cfg(test)]
#[path = "draft_tests.rs"]
mod tests;