Implement cron job management tools and types

- Added `JobType`, `SessionTarget`, `Schedule`, `DeliveryConfig`, `CronJob`, `CronRun`, and `CronJobPatch` types in `src/cron/types.rs` for cron job configuration and management.
- Introduced `CronAddTool`, `CronListTool`, `CronRemoveTool`, `CronRunTool`, `CronRunsTool`, and `CronUpdateTool` in `src/tools` for adding, listing, removing, running, and updating cron jobs.
- Updated the `run` function in `src/daemon/mod.rs` to conditionally start the scheduler based on the cron configuration.
- Modified command-line argument parsing in `src/lib.rs` and `src/main.rs` to support new cron job commands.
- Enhanced the onboarding wizard in `src/onboard/wizard.rs` to include cron configuration.
- Added tests for cron job tools to ensure functionality and error handling.
This commit is contained in:
mai1015 2026-02-16 20:35:20 -05:00 committed by Chummy
parent 0ec46ac3d1
commit fb2d1cea0b
24 changed files with 2682 additions and 638 deletions

101
src/tools/cron_list.rs Normal file
View file

@ -0,0 +1,101 @@
use super::traits::{Tool, ToolResult};
use crate::config::Config;
use crate::cron;
use async_trait::async_trait;
use serde_json::json;
use std::sync::Arc;
pub struct CronListTool {
config: Arc<Config>,
}
impl CronListTool {
pub fn new(config: Arc<Config>) -> Self {
Self { config }
}
}
#[async_trait]
impl Tool for CronListTool {
fn name(&self) -> &str {
"cron_list"
}
fn description(&self) -> &str {
"List all scheduled cron jobs"
}
fn parameters_schema(&self) -> serde_json::Value {
json!({
"type": "object",
"properties": {},
"additionalProperties": false
})
}
async fn execute(&self, _args: serde_json::Value) -> anyhow::Result<ToolResult> {
if !self.config.cron.enabled {
return Ok(ToolResult {
success: false,
output: String::new(),
error: Some("cron is disabled by config (cron.enabled=false)".to_string()),
});
}
match cron::list_jobs(&self.config) {
Ok(jobs) => Ok(ToolResult {
success: true,
output: serde_json::to_string_pretty(&jobs)?,
error: None,
}),
Err(e) => Ok(ToolResult {
success: false,
output: String::new(),
error: Some(e.to_string()),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use tempfile::TempDir;
fn test_config(tmp: &TempDir) -> Arc<Config> {
let config = Config {
workspace_dir: tmp.path().join("workspace"),
config_path: tmp.path().join("config.toml"),
..Config::default()
};
std::fs::create_dir_all(&config.workspace_dir).unwrap();
Arc::new(config)
}
#[tokio::test]
async fn returns_empty_list_when_no_jobs() {
let tmp = TempDir::new().unwrap();
let cfg = test_config(&tmp);
let tool = CronListTool::new(cfg);
let result = tool.execute(json!({})).await.unwrap();
assert!(result.success);
assert_eq!(result.output.trim(), "[]");
}
#[tokio::test]
async fn errors_when_cron_disabled() {
let tmp = TempDir::new().unwrap();
let mut cfg = (*test_config(&tmp)).clone();
cfg.cron.enabled = false;
let tool = CronListTool::new(Arc::new(cfg));
let result = tool.execute(json!({})).await.unwrap();
assert!(!result.success);
assert!(result
.error
.unwrap_or_default()
.contains("cron is disabled"));
}
}