printobserver_supervisor_api/
lib.rs1pub mod access;
28pub mod assessment;
29pub mod session;
30
31use core::future::Future;
32use core::pin::Pin;
33use std::path::PathBuf;
34use std::sync::Arc;
35
36use printobserver_types::contract::Sample;
37use printobserver_types::schemars::JsonSchema;
38use printobserver_types::serde::{Deserialize, Serialize};
39use printobserver_types::{EventPayload, EventRecord, PrintId};
40
41pub use access::{CREDENTIAL_ENV, SERVER_ENV, TurnAccess, TurnPass, TurnSession};
42pub use assessment::{AgentAssessment, Confidence};
43pub use session::{SessionPhase, SupervisionSession};
44
45pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
48
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
51#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
52#[schemars(crate = "printobserver_types::schemars")]
53pub struct TurnRequest {
54 pub print_id: PrintId,
56 pub event: EventRecord,
58 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub image_path: Option<PathBuf>,
61 pub context_command: String,
63 #[serde(default)]
65 pub situation: TurnSituation,
66}
67
68#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
76#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
77#[schemars(crate = "printobserver_types::schemars")]
78pub struct TurnSituation {
79 #[serde(default)]
82 pub printer_state: Option<String>,
84 #[serde(default)]
87 pub detector_warned: Option<bool>,
88 #[serde(default)]
91 pub detector_paused_the_print: Option<bool>,
92 #[serde(default)]
96 pub arrived_while_busy: Vec<EventRecord>,
97}
98
99impl Sample for TurnSituation {
100 fn sample_full() -> Self {
101 Self {
102 printer_state: Some("paused".to_owned()),
103 detector_warned: Some(false),
104 detector_paused_the_print: Some(true),
105 arrived_while_busy: vec![EventRecord::sample_full()],
106 }
107 }
108
109 fn sample_minimal() -> Self {
110 Self::default()
111 }
112}
113
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
116#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
117#[schemars(crate = "printobserver_types::schemars")]
118pub struct TurnOutcome {
119 pub session: SupervisionSession,
121 pub phase: SessionPhase,
123 pub assessment: AgentAssessment,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
129#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
130#[schemars(crate = "printobserver_types::schemars")]
131pub struct SupervisionSessionOpenedPayload {
132 pub session_name: String,
134 pub harness_identity: String,
136}
137
138impl EventPayload for SupervisionSessionOpenedPayload {
139 const KIND: &'static str = "supervision_session_opened";
140}
141
142impl Sample for SupervisionSessionOpenedPayload {
143 fn sample_full() -> Self {
144 Self {
145 session_name: "print-0191f0a0".to_owned(),
146 harness_identity: "printobserver-supervisor".to_owned(),
147 }
148 }
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
153#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
154#[schemars(crate = "printobserver_types::schemars")]
155pub struct SupervisionSessionClosedPayload {
156 pub session_name: String,
158 pub close_reason: String,
160}
161
162impl EventPayload for SupervisionSessionClosedPayload {
163 const KIND: &'static str = "supervision_session_closed";
164}
165
166impl Sample for SupervisionSessionClosedPayload {
167 fn sample_full() -> Self {
168 Self {
169 session_name: "print-0191f0a0".to_owned(),
170 close_reason: "the print ended".to_owned(),
171 }
172 }
173}
174
175#[derive(Debug, Clone, PartialEq, Eq)]
177pub enum SupervisorError {
178 InvalidAnswer {
180 detail: String,
182 },
183 IdentityRefused {
185 detail: String,
187 },
188 Unavailable {
190 detail: String,
192 },
193 TimedOut,
195}
196
197impl core::fmt::Display for SupervisorError {
198 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
199 match self {
200 Self::InvalidAnswer { detail } => {
201 write!(formatter, "the agent's answer failed validation: {detail}")
202 }
203 Self::IdentityRefused { detail } => {
204 write!(formatter, "the harness refused the identity: {detail}")
205 }
206 Self::Unavailable { detail } => {
207 write!(formatter, "the harness is unavailable: {detail}")
208 }
209 Self::TimedOut => formatter.write_str("the turn took too long"),
210 }
211 }
212}
213
214impl core::error::Error for SupervisorError {}
215
216pub trait SupervisorPort: Send + Sync {
222 fn run_turn(
227 &self,
228 request: TurnRequest,
229 access: Arc<dyn TurnAccess>,
230 ) -> BoxFuture<'_, Result<TurnOutcome, SupervisorError>>;
231
232 fn close_session(
234 &self,
235 print_id: PrintId,
236 close_reason: String,
237 ) -> BoxFuture<'_, Result<(), SupervisorError>>;
238}