agent_types/interaction.rs
1//! Interaction-channel pure types: the ask half of the `ExecCtx` suspension
2//! vocabulary (the consent half is [`crate::approval`]).
3//!
4//! One vocabulary across the seam: the product's `ask_user` picker (question +
5//! candidates → the user's pick) and a code cell's host-call ask ride these
6//! shapes, so the channel face and the product surface cannot drift apart.
7
8use serde::{Deserialize, Serialize};
9
10/// One candidate the user can pick. `id` is the caller's stable key (echoed
11/// back on pick); `label` is what the picker shows and what comes back with
12/// the answer.
13#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
14pub struct AskOption {
15 pub id: String,
16 pub label: String,
17 pub note: Option<String>,
18}
19
20/// What the user did with an ask.
21#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
22pub enum AskAnswer {
23 /// Picked a candidate — `id` echoed, `label` as shown.
24 Picked { id: String, label: String },
25 /// Declined to pick (Esc). A skip is an answer too; never re-ask.
26 Skipped,
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 /// The ask vocabulary is wire data (a cell's host-call answer is JSON) —
34 /// it must round-trip without loss.
35 #[test]
36 fn ask_vocabulary_round_trips_through_json() {
37 let option = AskOption {
38 id: "login".into(),
39 label: "登录".into(),
40 note: Some("先打通会话".into()),
41 };
42 let back: AskOption =
43 serde_json::from_str(&serde_json::to_string(&option).unwrap()).unwrap();
44 assert_eq!(back, option);
45
46 for answer in [
47 AskAnswer::Picked {
48 id: "login".into(),
49 label: "登录".into(),
50 },
51 AskAnswer::Skipped,
52 ] {
53 let json = serde_json::to_string(&answer).unwrap();
54 let back: AskAnswer = serde_json::from_str(&json).unwrap();
55 assert_eq!(back, answer);
56 }
57 }
58}