use core::future::Future;
use core::pin::Pin;
use std::path::PathBuf;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{
AgentAssessment, EventRecord, PrintId, 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,
}
#[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)]
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)
-> BoxFuture<'_, Result<TurnOutcome, SupervisorError>>;
fn close_session(
&self,
print_id: PrintId,
close_reason: String,
) -> BoxFuture<'_, Result<(), SupervisorError>>;
}