printobserver_supervisor_api/
lib.rs1pub mod assessment;
27pub mod session;
28
29use core::future::Future;
30use core::pin::Pin;
31use std::path::PathBuf;
32
33use printobserver_types::contract::Sample;
34use printobserver_types::schemars::JsonSchema;
35use printobserver_types::serde::{Deserialize, Serialize};
36use printobserver_types::{EventPayload, EventRecord, PrintId};
37
38pub use assessment::{AgentAssessment, Confidence};
39pub use session::{SessionPhase, SupervisionSession};
40
41pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
44
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
47#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
48#[schemars(crate = "printobserver_types::schemars")]
49pub struct TurnRequest {
50 pub print_id: PrintId,
52 pub event: EventRecord,
54 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub image_path: Option<PathBuf>,
57 pub context_command: String,
59}
60
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
63#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
64#[schemars(crate = "printobserver_types::schemars")]
65pub struct TurnOutcome {
66 pub session: SupervisionSession,
68 pub phase: SessionPhase,
70 pub assessment: AgentAssessment,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
76#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
77#[schemars(crate = "printobserver_types::schemars")]
78pub struct SupervisionSessionOpenedPayload {
79 pub session_name: String,
81 pub harness_identity: String,
83}
84
85impl EventPayload for SupervisionSessionOpenedPayload {
86 const KIND: &'static str = "supervision_session_opened";
87}
88
89impl Sample for SupervisionSessionOpenedPayload {
90 fn sample_full() -> Self {
91 Self {
92 session_name: "print-0191f0a0".to_owned(),
93 harness_identity: "printobserver-supervisor".to_owned(),
94 }
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
100#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
101#[schemars(crate = "printobserver_types::schemars")]
102pub struct SupervisionSessionClosedPayload {
103 pub session_name: String,
105 pub close_reason: String,
107}
108
109impl EventPayload for SupervisionSessionClosedPayload {
110 const KIND: &'static str = "supervision_session_closed";
111}
112
113impl Sample for SupervisionSessionClosedPayload {
114 fn sample_full() -> Self {
115 Self {
116 session_name: "print-0191f0a0".to_owned(),
117 close_reason: "the print ended".to_owned(),
118 }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq)]
124pub enum SupervisorError {
125 InvalidAnswer {
127 detail: String,
129 },
130 IdentityRefused {
132 detail: String,
134 },
135 Unavailable {
137 detail: String,
139 },
140 TimedOut,
142}
143
144impl core::fmt::Display for SupervisorError {
145 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
146 match self {
147 Self::InvalidAnswer { detail } => {
148 write!(formatter, "the agent's answer failed validation: {detail}")
149 }
150 Self::IdentityRefused { detail } => {
151 write!(formatter, "the harness refused the identity: {detail}")
152 }
153 Self::Unavailable { detail } => {
154 write!(formatter, "the harness is unavailable: {detail}")
155 }
156 Self::TimedOut => formatter.write_str("the turn took too long"),
157 }
158 }
159}
160
161impl core::error::Error for SupervisorError {}
162
163pub trait SupervisorPort: Send + Sync {
169 fn run_turn(&self, request: TurnRequest)
171 -> BoxFuture<'_, Result<TurnOutcome, SupervisorError>>;
172
173 fn close_session(
175 &self,
176 print_id: PrintId,
177 close_reason: String,
178 ) -> BoxFuture<'_, Result<(), SupervisorError>>;
179}