neosian 3.0.2

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! The native streams a turn reads with state: Pi's, Cursor's, Vibe's and
//! Muse Code's, whose completion depends on what came before (X5a, X5b,
//! X6a).

use serde_json::{Map, Value};

use super::{Said, cursor, muse, pi, vibe};
use crate::harness::adapter::Agent;

pub enum Reader {
    Pi(pi::Reader),
    Cursor(cursor::Reader),
    Vibe(vibe::Reader),
    Muse(muse::Reader),
}

impl Reader {
    /// The stateful reader for `agent`, or None for a stream read line by line.
    pub fn of(agent: Agent) -> Option<Self> {
        match agent {
            Agent::Pi => Some(Self::Pi(pi::Reader::default())),
            Agent::Cursor => Some(Self::Cursor(cursor::Reader::default())),
            Agent::Vibe => Some(Self::Vibe(vibe::Reader::default())),
            Agent::Muse => Some(Self::Muse(muse::Reader::default())),
            _ => None,
        }
    }

    /// What the reader holds of the turn, in bytes (X5d).
    pub fn held(&self) -> u64 {
        match self {
            Self::Pi(reader) => reader.held(),
            Self::Cursor(reader) => reader.held(),
            Self::Vibe(reader) => reader.held(),
            Self::Muse(reader) => reader.held(),
        }
    }

    pub fn line(&mut self, event: &Map<String, Value>) -> Vec<Said> {
        match self {
            Self::Pi(reader) => reader.line(event),
            Self::Cursor(reader) => reader.line(event),
            Self::Vibe(reader) => reader.line(event),
            Self::Muse(reader) => reader.line(event),
        }
    }

    /// A nonblank line that is not a JSON object: a Vibe turn is then not
    /// complete (X6c U7); the other streams complete on a terminal event.
    pub fn malformed(&mut self) -> Vec<Said> {
        if let Self::Vibe(reader) = self {
            reader.malformed();
        }
        Vec::new()
    }

    /// What the end of the stream says: only Vibe's completion is known
    /// then (X6a), and only when the output reached its end (X6c U7).
    pub fn finish(&mut self, eof: bool) -> Vec<Said> {
        match self {
            Self::Vibe(reader) => reader.finish(eof),
            _ => Vec::new(),
        }
    }
}