rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Paging back through a session's saved conversation
//! (`rho/session/history`), for a client whose attach a window bounded
//! ([`rho_protocol::ReplayWindow`]). A loaded session's page comes from its
//! feed's transcript; one the daemon has not loaded is read from its file as
//! an observe reads it, without loading it, once per page: about 75 ms for
//! an 18 MB session file, against 11 ms for the same page loaded. A session
//! an agent runtime ran has no saved entries to page through.

use rho_protocol::{ErrorCode, RpcError, SessionHistoryParams, SessionHistoryResponse};

use super::{locate::Located, reopen::parse_session, unloaded::Unloaded, Sessions};

impl Sessions {
    /// A page of the session's saved conversation through `params.through`
    /// ([`super::Feed::history`]), rendered off the async workers.
    pub(in crate::app::daemon_host) async fn history(
        &self,
        params: SessionHistoryParams,
    ) -> Result<SessionHistoryResponse, RpcError> {
        self.refuse_when_stopping()?;
        let id = parse_session(&params.session)?;
        let page = match self.locate(&id).await {
            Located::Loaded(session) => {
                session
                    .feed
                    .history(&params.through, params.max_bytes, params.defer_tool_output)?
            }
            // A run's stream keeps no saved entries to page through, so a
            // session a run writes is read from its file, once the run
            // lets it be.
            Located::Unloaded { run: _, reopening } => {
                match self.read_unloaded(&reopening).await? {
                    Unloaded::Engine { .. } => {
                        return Err(RpcError::new(
                            ErrorCode::ConversationMoved,
                            format!(
                            "session {id} is run by an agent, whose journal names no saved entries"
                        ),
                        ));
                    }
                    Unloaded::Saved { feed, .. } => {
                        feed.history(&params.through, params.max_bytes, params.defer_tool_output)?
                    }
                }
            }
        };
        tokio::task::spawn_blocking(move || page.render())
            .await
            .map_err(|error| {
                RpcError::new(
                    ErrorCode::InternalError,
                    format!("could not render the history page: {error}"),
                )
            })
    }
}