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 /// Neither pick nor skip: the user typed a question instead — `text` is
28 /// their words, verbatim. Answer it in the reply and carry on; the picker
29 /// is gone (the "Chat about this" pseudo-row is how a user gets here).
30 Clarify { text: String },
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 /// The ask vocabulary is wire data (a cell's host-call answer is JSON) —
38 /// it must round-trip without loss, every variant included.
39 #[test]
40 fn ask_vocabulary_round_trips_through_json() {
41 let option = AskOption {
42 id: "login".into(),
43 label: "登录".into(),
44 note: Some("先打通会话".into()),
45 };
46 let back: AskOption =
47 serde_json::from_str(&serde_json::to_string(&option).unwrap()).unwrap();
48 assert_eq!(back, option);
49
50 for answer in [
51 AskAnswer::Picked {
52 id: "login".into(),
53 label: "登录".into(),
54 },
55 AskAnswer::Skipped,
56 AskAnswer::Clarify {
57 text: "先说说两种方案的差别?".into(),
58 },
59 ] {
60 let json = serde_json::to_string(&answer).unwrap();
61 let back: AskAnswer = serde_json::from_str(&json).unwrap();
62 assert_eq!(back, answer);
63 }
64
65 // Clarify is additive: JSON written before the variant existed still
66 // parses byte-identically (old Picked/Skipped shapes unchanged).
67 let old_skipped: AskAnswer = serde_json::from_str("\"Skipped\"").unwrap();
68 assert_eq!(old_skipped, AskAnswer::Skipped);
69 let old_picked: AskAnswer =
70 serde_json::from_str(r#"{"Picked":{"id":"login","label":"登录"}}"#).unwrap();
71 assert_eq!(
72 old_picked,
73 AskAnswer::Picked {
74 id: "login".into(),
75 label: "登录".into(),
76 }
77 );
78 }
79}