pub mod access;
pub mod assessment;
pub mod session;
use core::future::Future;
use core::pin::Pin;
use std::path::PathBuf;
use std::sync::Arc;
use printobserver_types::contract::Sample;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{EventPayload, EventRecord, PrintId, PrinterState, Timestamp};
pub use access::{CREDENTIAL_ENV, SERVER_ENV, TurnAccess, TurnPass, TurnSession};
pub use assessment::{AgentAssessment, Confidence};
pub use session::{SessionPhase, SupervisionSession};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct TurnRequest {
pub print_id: PrintId,
pub event: EventRecord,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub image_path: Option<PathBuf>,
pub context_command: String,
#[serde(default)]
pub situation: TurnSituation,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct TurnSituation {
#[serde(default)]
pub printer_state: Option<PrinterState>,
#[serde(default)]
pub detector_warned: Option<bool>,
#[serde(default)]
pub detector_paused_the_print: Option<bool>,
#[serde(default)]
pub arrived_while_busy: Vec<EventRecord>,
#[serde(default)]
pub noticed: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub frame_captured_at: Option<Timestamp>,
}
impl Sample for TurnSituation {
fn sample_full() -> Self {
Self {
printer_state: Some(PrinterState::Paused),
detector_warned: Some(false),
detector_paused_the_print: Some(true),
arrived_while_busy: vec![EventRecord::sample_full()],
noticed: Some(
"print_ended: the print ended interrupted, the printer reporting Offline \
(the print reached Offline)"
.to_owned(),
),
frame_captured_at: Some(Timestamp::sample_full()),
}
}
fn sample_minimal() -> Self {
Self::default()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct TurnOutcome {
pub session: SupervisionSession,
pub phase: SessionPhase,
pub assessment: AgentAssessment,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct SupervisionSessionOpenedPayload {
pub session_name: String,
pub harness_identity: String,
}
impl EventPayload for SupervisionSessionOpenedPayload {
const KIND: &'static str = "supervision_session_opened";
}
impl Sample for SupervisionSessionOpenedPayload {
fn sample_full() -> Self {
Self {
session_name: "print-0191f0a0".to_owned(),
harness_identity: "printobserver-supervisor".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct SupervisionSessionClosedPayload {
pub session_name: String,
pub close_reason: String,
}
impl EventPayload for SupervisionSessionClosedPayload {
const KIND: &'static str = "supervision_session_closed";
}
impl Sample for SupervisionSessionClosedPayload {
fn sample_full() -> Self {
Self {
session_name: "print-0191f0a0".to_owned(),
close_reason: "the print ended".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SupervisorError {
InvalidAnswer {
detail: String,
},
IdentityRefused {
detail: String,
},
Unavailable {
detail: String,
},
TimedOut,
}
impl core::fmt::Display for SupervisorError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidAnswer { detail } => {
write!(formatter, "the agent's answer failed validation: {detail}")
}
Self::IdentityRefused { detail } => {
write!(formatter, "the harness refused the identity: {detail}")
}
Self::Unavailable { detail } => {
write!(formatter, "the harness is unavailable: {detail}")
}
Self::TimedOut => formatter.write_str("the turn took too long"),
}
}
}
impl core::error::Error for SupervisorError {}
pub trait SupervisorPort: Send + Sync {
fn run_turn(
&self,
request: TurnRequest,
access: Arc<dyn TurnAccess>,
) -> BoxFuture<'_, Result<TurnOutcome, SupervisorError>>;
fn close_session(
&self,
print_id: PrintId,
close_reason: String,
) -> BoxFuture<'_, Result<(), SupervisorError>>;
}