1 //! The way that a plugin sends a channel and an artifact.2 //!3 //! `emit` waits for credit and returns one of three values, so a plugin never4 //! knows the window and never sleeps by itself (protocol.md section 8).5 //! `Blocked` spends nothing: it means the caller should look at `cancelled` and6 //! try again.7 8 use inf3r_proto::Emit;9 10 use crate::capability::Artifact;11 12 /// Why a channel could not take the bytes.13 #[derive(Clone, Copy, PartialEq, Eq, Debug)]14 pub enum EmitError {15 /// The window is still full after the wait. There is no point in retrying16 /// without doing other work first.17 Blocked,18 /// The caller cancelled, or the session ended. Stop the model.19 Cancelled,20 }21 22 impl std::fmt::Display for EmitError {23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {24 f.write_str(match self {25 EmitError::Blocked => "the window is full",26 EmitError::Cancelled => "the job was cancelled",27 })28 }29 }30 31 impl std::error::Error for EmitError {}32 33 /// What a plugin writes through.34 ///35 /// The three operations are the whole surface: a chunk, an artifact, and the36 /// two questions that let a long model loop stop. A plugin cannot ask where the37 /// job comes from, because the answer is always a socket that it does not own.38 pub trait Emitter {39 /// Sends one chunk on one channel.40 ///41 /// This blocks until the peer can take the bytes, the job is cancelled, or42 /// the budget ends.43 fn emit(&mut self, channel: &str, mime: &str, bytes: &[u8]) -> Result<Emit, EmitError>;44 45 /// Adds an artifact to the terminal frame.46 ///47 /// The bytes are not sent now: they travel with `done`, so a client sees48 /// the artifact at the moment the job ends and not before.49 fn artifact(&mut self, channel: &str, mime: &str, body: Artifact) -> Result<(), EmitError>;50 51 /// Whether the caller cancelled.52 ///53 /// A model loop checks this between steps, because a cancel cannot reach a54 /// kernel.55 fn cancelled(&self) -> bool;56 57 /// Milliseconds left of the job's budget.58 fn remaining_ms(&self) -> u32;59 60 /// Records one counter for the terminal frame.61 ///62 /// A model learns its rate only after the last token, so the terminal frame63 /// is the first place where the number can travel.64 fn usage(&mut self, key: &str, value: u64);65 }