rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The typed errors a session request answers when the session or the
//! daemon is in no state to serve it.

use rho_protocol::{ErrorCode, RpcError, TurnOutcome};
use rho_sdk::SessionId;

use super::super::jobs::JobId;

/// The error for a session job `job` writes, so the daemon must not open it
/// a second time.
pub(super) fn writing(job: &JobId, session: &SessionId) -> RpcError {
    RpcError::new(
        ErrorCode::SessionBusy,
        format!("job {job} is writing session {session}"),
    )
}

/// The error a request that needs the session answers while a delete
/// unloads it.
pub(super) fn being_deleted() -> RpcError {
    RpcError::new(ErrorCode::SessionBusy, "the session is being deleted")
}

pub(super) fn stopping() -> RpcError {
    RpcError::new(ErrorCode::DaemonStopping, "the daemon is stopping")
}

/// The error a prompt answers when its turn ended, as `outcome` says,
/// before it began: `TurnEnded`, with the outcome as its data.
pub(super) fn ended_early(outcome: &TurnOutcome) -> RpcError {
    let message = match outcome {
        TurnOutcome::Failed { error } => error.message.clone(),
        TurnOutcome::Cancelled => "the turn was cancelled before it started".to_owned(),
        TurnOutcome::TimedOut { timeout_ms } => {
            format!("the turn reached its {timeout_ms}ms time limit before it started")
        }
        // Only those three end a turn before it begins.
        _ => "the turn ended before it started".to_owned(),
    };
    let data = serde_json::to_value(outcome).expect("a turn outcome serializes");
    RpcError::new(ErrorCode::TurnEnded, message).with_data(data)
}