adk_surface/origin.rs
1//! Who acts, from which surface, and how the input was given (spec 3.3 §6, §7.2, §11).
2
3use adk_core::principal::SubjectRef;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use crate::words;
8
9/// The surface a person uses (spec 3.3 §5, §7.2).
10///
11/// What a surface may decide is fixed by §7.2 and computed by
12/// [`rules::allowed_answers`](crate::rules::allowed_answers); a surface never grants more
13/// than the web home.
14///
15/// # Example
16///
17/// ```rust
18/// use adk_surface::SurfaceKind;
19///
20/// assert_eq!(serde_json::to_value(SurfaceKind::Tray).unwrap(), "tray");
21/// ```
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
23#[serde(rename_all = "camelCase")]
24pub enum SurfaceKind {
25 /// The web home (spec 3.3 §5.1).
26 Web,
27 /// The terminal (spec 3.3 §5.2).
28 Terminal,
29 /// The web home installed on a phone (spec 3.3 §5.4).
30 Phone,
31 /// The desktop tray (spec 3.3 §5.5).
32 Tray,
33 /// A chat channel (spec 3.3 §5.3).
34 Chat,
35 /// Voice and video (spec 3.3 §5.6).
36 Voice,
37}
38
39/// How an input was given (spec 3.3 §6, §11): every command carries one, and it reaches the
40/// audit record, so "What your agents did" can say "from Telegram".
41///
42/// # Example
43///
44/// ```rust
45/// use adk_surface::Origin;
46///
47/// let origin = Origin::Chat { channel: "Telegram".into() };
48/// assert_eq!(serde_json::to_value(&origin).unwrap(), serde_json::json!({ "kind": "chat", "channel": "Telegram" }));
49/// assert_eq!(serde_json::to_value(Origin::Touch).unwrap(), serde_json::json!({ "kind": "touch" }));
50/// ```
51#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
52#[serde(tag = "kind", rename_all = "camelCase")]
53pub enum Origin {
54 /// Typed.
55 Keyboard,
56 /// Clicked.
57 Pointer,
58 /// Tapped.
59 Touch,
60 /// Spoken.
61 Voice,
62 /// Sent from a chat channel.
63 Chat {
64 /// The channel's name, such as `Telegram`.
65 channel: String,
66 },
67 /// Requested by an agent (spec 3.3 §7.4 rule 6: never an answer to a decision).
68 Agent {
69 /// The requesting agent.
70 agent: String,
71 },
72}
73
74impl Origin {
75 /// The wire name of the kind: `keyboard`, `pointer`, `touch`, `voice`, `chat` or `agent`.
76 pub fn kind(&self) -> &'static str {
77 match self {
78 Self::Keyboard => "keyboard",
79 Self::Pointer => "pointer",
80 Self::Touch => "touch",
81 Self::Voice => "voice",
82 Self::Chat { .. } => "chat",
83 Self::Agent { .. } => "agent",
84 }
85 }
86}
87
88/// Where and how an answer was given, as a decision answered elsewhere shows it
89/// (spec 3.3 §8.1 "Decided").
90///
91/// # Example
92///
93/// ```rust
94/// use adk_surface::{Origin, SurfaceKind, Via};
95///
96/// let via = Via { surface: SurfaceKind::Chat, origin: Origin::Chat { channel: "Telegram".into() } };
97/// assert_eq!(via.phrase(), "in Telegram");
98/// let via = Via { surface: SurfaceKind::Phone, origin: Origin::Touch };
99/// assert_eq!(via.phrase(), "on your phone");
100/// ```
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
102#[serde(rename_all = "camelCase")]
103pub struct Via {
104 /// The surface.
105 pub surface: SurfaceKind,
106 /// How the answer was given.
107 pub origin: Origin,
108}
109
110impl Via {
111 /// The place in plain words: `"in Telegram"`, `"on your phone"`, `"by voice"`.
112 pub fn phrase(&self) -> String {
113 match (&self.origin, self.surface) {
114 (Origin::Chat { channel }, _) => words::in_channel(channel),
115 (Origin::Voice, _) | (_, SurfaceKind::Voice) => words::BY_VOICE.to_string(),
116 (_, SurfaceKind::Web) => words::IN_THE_HOME_APP.to_string(),
117 (_, SurfaceKind::Phone) => words::ON_YOUR_PHONE.to_string(),
118 (_, SurfaceKind::Terminal) => words::IN_THE_TERMINAL.to_string(),
119 (_, SurfaceKind::Tray) => words::FROM_THE_TRAY.to_string(),
120 (_, SurfaceKind::Chat) => words::in_channel("chat"),
121 }
122 }
123}
124
125/// The authenticated caller of an engine read or command (spec 3.3 §6, §11 S-I8).
126///
127/// The subject and its groups come from the server's authentication, never from a request
128/// body. So do the two facts that widen what a caller may decide —
129/// [`presence_confirmed`](Self::presence_confirmed) and [`paired_local`](Self::paired_local) —
130/// which the web router reads from the request context the server's extractor built. The
131/// surface and origin a client declares can only narrow what it may decide: no surface allows
132/// more than the web home.
133///
134/// # Example
135///
136/// ```rust
137/// use adk_surface::{Caller, Origin, SurfaceKind};
138///
139/// let caller = Caller::human("ana", SurfaceKind::Chat, Origin::Chat { channel: "Telegram".into() });
140/// assert_eq!(caller.subject.id, "ana");
141/// assert!(!caller.presence_confirmed);
142/// ```
143#[derive(Debug, Clone, PartialEq, Eq)]
144pub struct Caller {
145 /// Who: the session owner a surface checks every read and command against (S-I8).
146 pub subject: SubjectRef,
147 /// The groups authentication established, for the approval service's eligibility check.
148 pub groups: Vec<String>,
149 /// The surface.
150 pub surface: SurfaceKind,
151 /// How the input was given.
152 pub origin: Origin,
153 /// The person confirmed it is them just now (a passkey or the device's lock), which
154 /// high-risk approvals on the web home and the phone need (spec 3.3 §7.2, §8.2).
155 pub presence_confirmed: bool,
156 /// A terminal on the kernel's own machine, paired, which may approve high-risk calls and
157 /// resume after a stop (spec 3.3 §7.2, §11 S-I10).
158 pub paired_local: bool,
159 /// The paired device the caller acts from, when it authenticated with a device token
160 /// (spec 3.3 §10 K2, §11): it reaches every audit record, so "Where you can say yes
161 /// from" and the record name the device.
162 pub device: Option<String>,
163}
164
165impl Caller {
166 /// A person, by their user id.
167 pub fn human(user_id: impl Into<String>, surface: SurfaceKind, origin: Origin) -> Self {
168 Self {
169 subject: SubjectRef::human(user_id),
170 groups: Vec::new(),
171 surface,
172 origin,
173 presence_confirmed: false,
174 paired_local: false,
175 device: None,
176 }
177 }
178
179 /// Sets the groups.
180 #[must_use]
181 pub fn with_groups(mut self, groups: impl IntoIterator<Item = impl Into<String>>) -> Self {
182 self.groups = groups.into_iter().map(Into::into).collect();
183 self
184 }
185
186 /// Marks that the person confirmed it is them just now. Set only from server-side
187 /// authentication.
188 #[must_use]
189 pub fn with_presence_confirmed(mut self) -> Self {
190 self.presence_confirmed = true;
191 self
192 }
193
194 /// Marks a paired local terminal. Set only from the transport (the kernel's socket or
195 /// pipe) or a device token scoped to it.
196 #[must_use]
197 pub fn with_paired_local(mut self) -> Self {
198 self.paired_local = true;
199 self
200 }
201
202 /// Sets the paired device the caller acts from (spec 3.3 §10 K2, §11). Set only from
203 /// server-side authentication: a device token's context.
204 ///
205 /// # Example
206 ///
207 /// ```rust
208 /// use adk_surface::{Caller, Origin, SurfaceKind};
209 ///
210 /// let caller = Caller::human("ana", SurfaceKind::Phone, Origin::Touch).with_device("dev_1");
211 /// assert_eq!(caller.device.as_deref(), Some("dev_1"));
212 /// ```
213 #[must_use]
214 pub fn with_device(mut self, id: impl Into<String>) -> Self {
215 self.device = Some(id.into());
216 self
217 }
218
219 /// The session owner the caller acts as.
220 pub fn user_id(&self) -> &str {
221 &self.subject.id
222 }
223
224 /// Where and how this caller answers.
225 pub fn via(&self) -> Via {
226 Via { surface: self.surface, origin: self.origin.clone() }
227 }
228}