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
//! The person who attends a run: who the driver hands what the agent asks
//! a person for (a tool approval, a plan approval, a form), and whose answer
//! it sends back to the agent.
//!
//! A run nobody attends has no [`Person`]: its policy answers every ask
//! itself, and the driver advertises no elicitation, so the agent never asks
//! a question nobody would answer.
use rho_sdk::{HostInputRefusal, HostInputRequest, HostInputResponse};
use tokio::sync::{mpsc, oneshot};
use crate::permission::PermissionMode;
/// Whether a person attends a run, decided when its session opens.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Attendance {
/// A person opened the session and answers what its agent asks.
Attended,
/// Nobody answers: the run's policy answers every ask itself.
Unattended,
}
impl Attendance {
/// A run's attendance: attended when it has a person to ask.
pub(crate) fn of(person: Option<&Person>) -> Self {
match person {
Some(_) => Self::Attended,
None => Self::Unattended,
}
}
}
/// What an agent asks the person.
#[derive(Debug)]
pub(crate) enum PersonAsk {
/// May the agent run its tool call `tool_call`, which it titles `title`
/// when it gave one?
Tool {
tool_call: String,
title: Option<String>,
},
/// May the agent leave plan mode to carry out `plan` in mode `to`?
PlanExit {
tool_call: String,
plan: String,
to: PermissionMode,
},
/// The agent's form, as Rho's questions, asked by tool call `tool_call`
/// when one did.
Form {
tool_call: Option<String>,
request: HostInputRequest,
},
}
/// The person's answer to a [`PersonAsk`].
#[derive(Debug, PartialEq)]
pub(crate) enum PersonAnswer {
/// Run the tool call; a plan exit's approval.
Allow,
/// Do not; for a plan exit, keep planning.
Deny,
/// The answers to a form, or why there are none.
Form(Result<HostInputResponse, HostInputRefusal>),
}
/// What the person hears of a run.
pub(crate) enum Heard {
/// An ask, which waits for their answer.
Asked(Asked),
/// The agent left plan mode for this mode, as the person approved: the
/// run's later asks, and its later runs, are that mode's.
LeftPlan(PermissionMode),
}
/// One ask on its way to the person, with where the answer goes.
pub(crate) struct Asked {
pub(crate) ask: PersonAsk,
pub(crate) reply: oneshot::Sender<PersonAnswer>,
}
/// The line to the person who attends a run.
#[derive(Clone)]
pub(crate) struct Person {
heard: mpsc::UnboundedSender<Heard>,
}
impl Person {
/// A person who hears the run on the receiver.
pub(crate) fn channel() -> (Self, mpsc::UnboundedReceiver<Heard>) {
let (heard, receiver) = mpsc::unbounded_channel();
(Self { heard }, receiver)
}
/// Tells the person the agent left plan mode for `mode`.
pub(crate) fn left_plan(&self, mode: PermissionMode) {
// A person's side that went away hears nothing more.
let _ = self.heard.send(Heard::LeftPlan(mode));
}
/// Asks `ask`. `None` once nobody will answer: the person's side went
/// away, or withdrew the ask.
pub(crate) fn ask(
&self,
ask: PersonAsk,
) -> impl std::future::Future<Output = Option<PersonAnswer>> + Send + 'static {
let (reply, answer) = oneshot::channel();
let sent = self.heard.send(Heard::Asked(Asked { ask, reply })).is_ok();
async move {
match sent {
true => answer.await.ok(),
false => None,
}
}
}
}