roboticus-api 0.11.4

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Roboticus agent runtime
Documentation
//! Scheduled (cron) task execution via the unified pipeline.
//!
//! Thin connector: subagent routing check → `run_pipeline()` → extract content.

use super::AppState;
use super::pipeline::{
    PipelineConfig, PipelineError, PipelineOutcome, PipelineRequest, run_pipeline,
};

pub(crate) async fn execute_scheduled_agent_task(
    state: &AppState,
    agent_id: &str,
    task: &str,
    job_name: Option<&str>,
) -> Result<String, String> {
    // ── Call: invoke the factory ────────────────────────────────────
    // Thin connector: parse → call → format. ALL business logic (delegation
    // wrapping, local model preference) is handled by the pipeline via
    // PipelineConfig flags (cron_delegation_wrap, prefer_local_model).
    let mut cron_config = PipelineConfig::cron();
    if let Some(name) = job_name {
        cron_config.session_nickname_override = Some(format!("Scheduled: {name}"));
    }
    let request = PipelineRequest {
        deps: state.pipeline_deps(),
        config: cron_config,
        raw_content: task,
        session_id_hint: None,
        scope_hint: None,
        is_correction_turn: false,
        channel_context: None,
        content_parts: None,
        addressed: None,
        task_agent_id: Some(agent_id.to_string()),
        diagnostics_note: None,
        bot_command_reply: None,
    };

    let outcome = run_pipeline(request).await.map_err(|e| match e {
        PipelineError::InjectionBlocked { threat_score } => {
            format!("scheduled task blocked: injection detected (score={threat_score:.2})")
        }
        other => other.to_string(),
    })?;

    // ── Format: extract content from outcome ───────────────────────
    match outcome {
        PipelineOutcome::Complete { result, .. } => Ok(result.content),
        PipelineOutcome::SpecialistProposal { prompt, .. } => Err(format!(
            "scheduled task requires specialist creation before execution: {prompt}"
        )),
        PipelineOutcome::StreamReady(_) => {
            Err("unexpected streaming outcome on cron endpoint".into())
        }
    }
}