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
//! The handle a host shares with whoever acts on a session without holding
//! its runtime: cancelling or steering its prompt, withdrawing a steer, or
//! closing it.
use std::{future::Future, sync::Arc};
use rho_sdk::{SteeringId, SteeringRetraction, UserInput};
use super::prompt_gate::PromptGate;
/// Why [`SessionHandle::steer`] staged nothing, or
/// [`SessionHandle::retract`] asked nothing.
#[derive(Debug, thiserror::Error)]
pub(crate) enum SteerError {
/// No model run takes input: no prompt runs, it is still starting or
/// was cancelled, its run already ended, or it runs a command such as
/// `/compact`.
#[error("session has no running model turn to steer")]
NotRunning,
/// The running model turn refused the input, as when its command queue
/// is full.
#[error(transparent)]
Refused(rho_sdk::Error),
}
impl SteerError {
/// Why the SDK refused a steer, a retraction, or one's receipt: a run
/// that already finished takes no input; anything else refused it.
pub(crate) fn from_sdk(error: rho_sdk::Error) -> Self {
match error {
rho_sdk::Error::RunFinished => Self::NotRunning,
error => Self::Refused(error),
}
}
}
/// A cloneable handle on one runtime. A host keeps it beside the runtime,
/// as the daemon does for each loaded session, so a cancel, steer, or close
/// acts at once instead of waiting for the running prompt to give the
/// runtime back.
/// Clones are the same handle.
#[derive(Clone)]
pub(crate) struct SessionHandle(Arc<PromptGate>);
impl SessionHandle {
pub(super) fn new() -> Self {
Self(Arc::new(PromptGate::new()))
}
/// A handle on no runtime, for host tests that publish an empty slot.
#[cfg(test)]
pub(in crate::app) fn detached_for_tests() -> Self {
Self::new()
}
pub(super) fn gate(&self) -> &PromptGate {
&self.0
}
/// Cancels the running prompt; a no-op while none runs.
pub(crate) fn cancel(&self) {
self.0.cancel();
}
/// Stages `input` for the running prompt's next provider turn, as a
/// delegated child's parent does. The receipt resolves to the id the
/// run accepted it under, which its `SteeringApplied` event names, or
/// fails if the run ends first. Never waits, so a caller may hold a lock
/// across it.
pub(crate) fn steer(
&self,
input: UserInput,
) -> Result<impl Future<Output = Result<SteeringId, rho_sdk::Error>> + Send + 'static, SteerError>
{
self.0
.steer(input)
.ok_or(SteerError::NotRunning)?
.map_err(SteerError::from_sdk)
}
/// Asks the running prompt's model run to withdraw steer `id`. The
/// receipt resolves to what became of it, which the run decides against
/// applying it, or fails if the run ends first. Never waits, as
/// [`Self::steer`] does not.
pub(crate) fn retract(
&self,
id: SteeringId,
) -> Result<
impl Future<Output = Result<SteeringRetraction, rho_sdk::Error>> + Send + 'static,
SteerError,
> {
self.0
.retract(id)
.ok_or(SteerError::NotRunning)?
.map_err(SteerError::from_sdk)
}
/// Cancels the running prompt and every later one, for a session that
/// is being torn down.
pub(crate) fn close(&self) {
self.0.close();
}
}
#[cfg(test)]
#[path = "handle_tests.rs"]
mod tests;