use croner::Cron;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::paths::{agent_toml_path, routine_toml_path};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, utoipa::ToSchema)]
pub struct Repository {
pub repository: String,
#[serde(default)]
pub branch: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, utoipa::ToSchema)]
pub struct Routine {
pub id: String,
pub schedule: String,
pub title: String,
pub agent: String,
pub prompt: String,
#[serde(default)]
pub repositories: Vec<Repository>,
pub enabled: bool,
pub source: String,
pub created_at: u64,
pub updated_at: u64,
pub last_triggered_at: Option<u64>,
}
#[derive(Debug, Clone, Serialize, JsonSchema, utoipa::ToSchema)]
pub struct RoutineResponse {
#[serde(flatten)]
pub routine: Routine,
pub agent_registered: bool,
pub file_path: String,
pub schedule_description: Option<String>,
}
impl RoutineResponse {
pub fn from_routine(routine: Routine) -> Self {
let agent_registered = agent_toml_path(&routine.agent).exists();
let file_path = routine_toml_path(&routine.id)
.to_string_lossy()
.into_owned();
let schedule_description = routine.schedule.parse::<Cron>().ok().map(|c| c.describe());
Self {
routine,
agent_registered,
file_path,
schedule_description,
}
}
}
pub type RoutineStore = Arc<Mutex<HashMap<String, Routine>>>;
#[cfg(test)]
pub fn new_store() -> RoutineStore {
Arc::new(Mutex::new(HashMap::new()))
}
pub(crate) fn bool_true() -> bool {
true
}
#[derive(Deserialize, JsonSchema, utoipa::ToSchema)]
pub struct CreateRoutineRequest {
pub schedule: String,
pub title: String,
pub agent: String,
pub prompt: String,
#[serde(default)]
pub repositories: Vec<Repository>,
#[serde(default = "bool_true")]
pub enabled: bool,
}
#[derive(Deserialize, JsonSchema, utoipa::ToSchema)]
pub struct UpdateRoutineRequest {
pub schedule: Option<String>,
pub title: Option<String>,
pub agent: Option<String>,
pub prompt: Option<String>,
pub repositories: Option<Vec<Repository>>,
pub enabled: Option<bool>,
}