stynx_code_tools/infrastructure/
cron_create_tool.rs1use stynx_code_errors::AppResult;
2use stynx_code_types::{PermissionLevel, Tool};
3use serde_json::{Value, json};
4
5pub struct CronCreateTool;
6
7impl CronCreateTool {
8 pub fn new() -> Self {
9 Self
10 }
11}
12
13#[async_trait::async_trait]
14impl Tool for CronCreateTool {
15 fn name(&self) -> &str {
16 "cron_create"
17 }
18
19 fn description(&self) -> &str {
20 "Create a new cron job with a schedule expression and command."
21 }
22
23 fn input_schema(&self) -> Value {
24 json!({
25 "type": "object",
26 "properties": {
27 "schedule": {
28 "type": "string",
29 "description": "Cron expression (e.g. \"0 */5 * * *\")"
30 },
31 "command": {
32 "type": "string",
33 "description": "Command to execute on the schedule"
34 },
35 "name": {
36 "type": "string",
37 "description": "Optional human-readable name for the cron job"
38 }
39 },
40 "required": ["schedule", "command"]
41 })
42 }
43
44 fn permission_level(&self) -> PermissionLevel {
45 PermissionLevel::Dangerous
46 }
47
48 async fn execute(&self, input: Value) -> AppResult<String> {
49 let schedule = input
50 .get("schedule")
51 .and_then(|v| v.as_str())
52 .ok_or_else(|| stynx_code_errors::AppError::Tool("missing 'schedule' field".into()))?;
53
54 let command = input
55 .get("command")
56 .and_then(|v| v.as_str())
57 .ok_or_else(|| stynx_code_errors::AppError::Tool("missing 'command' field".into()))?;
58
59 let name = input.get("name").and_then(|v| v.as_str());
60
61 let cron_id = format!("cron-{:08x}", rand_id());
62
63 tracing::info!(schedule, command, ?name, cron_id, "creating cron job (stub)");
64
65 Ok(format!(
66 "Cron job created.\n id: {cron_id}\n schedule: {schedule}\n command: {command}{}",
67 name.map(|n| format!("\n name: {n}")).unwrap_or_default()
68 ))
69 }
70}
71
72fn rand_id() -> u32 {
73 use std::collections::hash_map::DefaultHasher;
74 use std::hash::{Hash, Hasher};
75 let mut h = DefaultHasher::new();
76 std::time::SystemTime::now().hash(&mut h);
77 h.finish() as u32
78}