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> {
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(),
})?;
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())
}
}
}