clawgarden-agent 0.22.0

Agent runtime with persona/memory loader, judge, and pi RPC for ClawGarden
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! CronNlp — Natural language cron job parsing
//!
//! Detects cron intent from user messages (keyword-based, no LLM call).
//! Parses Korean and English time expressions into crontab schedules.

use serde::{Deserialize, Serialize};

/// Delivery configuration for cron results (mirrors bus-side Delivery).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Delivery {
    pub mode: String,
    pub chat_id: Option<i64>,
    pub agent_name: Option<String>,
}

impl Delivery {
    pub fn telegram(chat_id: i64) -> Self {
        Self { mode: "telegram".to_string(), chat_id: Some(chat_id), agent_name: None }
    }
    pub fn log() -> Self {
        Self { mode: "log".to_string(), chat_id: None, agent_name: None }
    }
}

/// Parsed cron request from natural language.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronRequest {
    /// Crontab expression (e.g. "0 2 * * *")
    pub schedule: String,
    /// Job display name
    pub name: String,
    /// Natural language description of the action
    pub description: String,
    /// Workflow steps inferred from the action
    pub steps: Vec<StepSpec>,
    /// Delivery configuration
    pub delivery: Delivery,
}

/// A single workflow step inferred from action keywords.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepSpec {
    /// Action type: "scrape", "translate", "email", "write_file", "agent"
    pub action: String,
    /// Natural language parameters for the step
    pub params: String,
}

/// Detect if text contains cron intent (cheap keyword check, no LLM).
/// Returns Some(lang) if cron keywords are found.
pub fn detect_cron_intent(text: &str) -> Option<&'static str> {
    let lower = text.to_lowercase();

    // Korean patterns
    let ko_patterns = [
        "매일", "매주", "매달", "매시간",
        "2시", "3시", "4시", "5시", "6시", "7시", "8시", "9시", "10시", "11시", "12시",
        "오후", "오전", "새벽", "정오", "자정", "매시",
        "30분마다", "2시간마다", "1시간마다",
    ];
    for p in ko_patterns {
        if lower.contains(p) {
            return Some("ko");
        }
    }

    // English patterns
    let en_patterns = [
        "daily", "weekly", "monthly",
        "every hour", "every 2", "every 3", "hourly",
        "cron", "schedule",
        "at 2am", "at 3pm", "at 9am",
        "every morning", "every night",
        "@daily", "@hourly",
    ];
    for p in en_patterns {
        if lower.contains(p) {
            return Some("en");
        }
    }

    None
}

/// Parse natural language text into a CronRequest.
pub fn parse(text: &str, lang: &str) -> Option<CronRequest> {
    match lang {
        "ko" => parse_korean(text),
        "en" => parse_english(text),
        _ => parse_korean(text).or_else(|| parse_english(text)),
    }
}

fn parse_korean(text: &str) -> Option<CronRequest> {
    let schedule = extract_schedule_ko(text)?;
    let name = extract_name_ko(text);
    let description = text.to_string();
    let steps = extract_steps_ko(text);

    Some(CronRequest {
        schedule,
        name,
        description,
        steps,
        delivery: Delivery::telegram(0), // chat_id filled by caller
    })
}

fn parse_english(text: &str) -> Option<CronRequest> {
    let schedule = extract_schedule_en(text)?;
    let name = extract_name_en(text);
    let description = text.to_string();
    let steps = extract_steps_en(text);

    Some(CronRequest {
        schedule,
        name,
        description,
        steps,
        delivery: Delivery::telegram(0),
    })
}

/// Extract crontab expression from Korean text.
fn extract_schedule_ko(text: &str) -> Option<String> {
    let lower = text.to_lowercase();

    // Check "매주 {day}" BEFORE "시" pattern — "매주 월요일 9시" → weekly, not daily at 9am
    let days_ko = [
        ("월요일", 1), ("화요일", 2), ("수요일", 3),
        ("목요일", 4), ("금요일", 5), ("토요일", 6), ("일요일", 0),
    ];
    for (day, dow) in days_ko {
        if lower.contains(day) {
            // Extract hour from "9시", "오후 3시", "새벽 5시"
            let hour = extract_hour_from_ko_time(&lower).unwrap_or(9);
            return Some(format!("0 {} * * {}", hour, dow));
        }
    }

    // "2시간마다" / "1시간마다" — must be before "2시" / "1시" pattern
    if lower.contains("2시간") || lower.contains("두 시간") {
        return Some("0 */2 * * *".to_string());
    }
    if lower.contains("3시간") {
        return Some("0 */3 * * *".to_string());
    }
    if lower.contains("1시간") || lower.contains("한 시간") {
        return Some("0 */1 * * *".to_string());
    }
    if lower.contains("30분") || lower.contains("15분") || lower.contains("10분") {
        return Some("*/30 * * * *".to_string());
    }

    // "시" pattern — only for plain times without day prefix
    if let Some(cap) = extract_number_before(&lower, "") {
        let hour: u32 = cap.parse().unwrap_or(0).min(23);
        return Some(format!("0 {} * * *", hour));
    }

    // "매일" / "매일 새벽 3시"
    if lower.contains("매일") || lower.contains("매일 ") {
        let hour = extract_hour_from_ko_time(&lower).unwrap_or(8);
        return Some(format!("0 {} * * *", hour));
    }

    // "매달 {N}일"
    if let Some(cap) = extract_number_before(&lower, "") {
        let day: u32 = cap.parse().unwrap_or(1).min(28);
        let hour = extract_hour_from_ko_time(&lower).unwrap_or(3);
        return Some(format!("0 {} {} * *", hour, day));
    }

    // Default: daily at 8am
    if lower.contains("") {
        return Some("0 8 * * *".to_string());
    }

    None
}

/// Extract a number that appears before a keyword (e.g. "3시" → "3").
fn extract_number_before(text: &str, keyword: &str) -> Option<String> {
    if let Some(pos) = text.find(keyword) {
        if pos > 0 {
            let before = &text[..pos];
            // Collect trailing digits
            let digits: String = before.chars().rev().take_while(|c| c.is_ascii_digit()).collect();
            if !digits.is_empty() {
                return Some(digits.chars().rev().collect());
            }
            // Also check for Korean number (한글 digit)
            if let Some(c) = before.chars().last() {
                if '\u{0030}' <= c && c <= '\u{0039}' {
                    return Some(c.to_string());
                }
            }
        }
    }
    None
}

/// Extract hour from Korean time qualifiers (새벽, 오전, 오후, 저녁).
fn extract_hour_from_ko_time(text: &str) -> Option<u32> {
    let hour = extract_number_before(text, "")?.parse::<u32>().ok()?;
    if text.contains("오후") && hour < 12 {
        return Some(hour + 12);
    }
    if text.contains("새벽") || text.contains("저녁") || text.contains("") {
        if hour < 12 {
            return Some(hour + 12);
        }
    }
    Some(hour)
}

/// Extract crontab from English text.
fn extract_schedule_en(text: &str) -> Option<String> {
    let lower = text.to_lowercase();

    if lower.contains("every hour") || lower.contains("hourly") {
        return Some("0 * * * *".to_string());
    }
    if lower.contains("every 2 hours") || lower.contains("every two hours") {
        return Some("0 */2 * * *".to_string());
    }
    if lower.contains("every 30 minutes") || lower.contains("every thirty minutes") {
        return Some("*/30 * * * *".to_string());
    }

    if let Some(cap) = regex_capture(r"at (\d+)(am|pm)?", &lower) {
        let hour: u32 = cap.parse().unwrap_or(0);
        let is_pm = lower.contains("pm");
        let h = if is_pm && hour < 12 { hour + 12 } else { hour };
        return Some(format!("0 {} * * *", h));
    }

    if lower.contains("every day") || lower.contains("daily") {
        return Some("0 8 * * *".to_string());
    }
    if lower.contains("every morning") {
        return Some("0 8 * * *".to_string());
    }
    if lower.contains("every night") || lower.contains("every evening") {
        return Some("0 21 * * *".to_string());
    }

    // "every {weekday}"
    let en_days = [
        ("monday", 1), ("tuesday", 2), ("wednesday", 3),
        ("thursday", 4), ("friday", 5), ("saturday", 6), ("sunday", 0),
    ];
    for (day, dow) in en_days {
        if lower.contains(day) {
            return Some(format!("0 9 * * {}", dow));
        }
    }

    None
}

/// Simple regex capture for English patterns like "at {N}".
fn regex_capture(pattern: &str, text: &str) -> Option<String> {
    // Very simple: just extract the digit before "am" or "pm"
    if let Some(pos) = text.find("am") {
        if pos > 0 {
            let s = &text[..pos];
            let digits: String = s.chars().rev().take_while(|c| c.is_ascii_digit()).collect();
            return Some(digits.chars().rev().collect());
        }
    }
    if let Some(pos) = text.find("pm") {
        if pos > 0 {
            let s = &text[..pos];
            let digits: String = s.chars().rev().take_while(|c| c.is_ascii_digit()).collect();
            let n: u32 = digits.chars().rev().collect::<String>().parse().unwrap_or(0);
            return Some(format!("{}", n));
        }
    }
    // "at {N}"
    if let Some(pos) = text.find("at ") {
        let rest = &text[pos + 3..];
        let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
        if !digits.is_empty() {
            return Some(digits);
        }
    }
    None
}

fn extract_name_ko(text: &str) -> String {
    let lower = text.to_lowercase();
    let words: Vec<&str> = text.split_whitespace().collect();

    // Extract action words between schedule keywords and delivery keywords
    let delivery_keywords = ["이메일", "메일", "텔레그램", "telegram", "슬랙", "slack", "옵시디언", "obsidian"];
    let schedule_keywords = ["매일", "매주", "매달", "매시간", "마다", "", "마다", "cron", "schedule"];

    let mut result = Vec::new();
    let mut found_action = false;

    for word in words {
        let w = word.to_lowercase();
        if schedule_keywords.iter().any(|k| w.contains(k)) {
            continue;
        }
        if delivery_keywords.iter().any(|k| w.contains(k)) {
            break;
        }
        result.push(word);
        if !found_action {
            found_action = true;
        }
    }

    result.join(" ")
        .trim()
        .chars()
        .take(50)
        .collect::<String>()
}

fn extract_name_en(text: &str) -> String {
    extract_name_ko(text) // same logic works for English
}

/// Extract workflow steps from Korean text keywords.
fn extract_steps_ko(text: &str) -> Vec<StepSpec> {
    let mut steps = Vec::new();
    let lower = text.to_lowercase();

    // Detect tool needs from keywords
    if lower.contains("해커뉴스") || lower.contains("hackernews") || lower.contains("뉴스") {
        steps.push(StepSpec {
            action: "hackernews".to_string(),
            params: "fetch top stories from Hacker News".to_string(),
        });
    }
    if lower.contains("번역") || lower.contains("translate") || lower.contains("번역해") {
        steps.push(StepSpec {
            action: "translate".to_string(),
            params: "translate content to Korean".to_string(),
        });
    }
    if lower.contains("이메일") || lower.contains("email") || lower.contains("메일") || lower.contains("메일보내") {
        steps.push(StepSpec {
            action: "email".to_string(),
            params: "send content as email".to_string(),
        });
    }
    if lower.contains("obsidian") || lower.contains("옵시디언") || lower.contains("옵시디안") || lower.contains("노트") || lower.contains("기록") {
        steps.push(StepSpec {
            action: "write_file".to_string(),
            params: "save as markdown note".to_string(),
        });
    }
    if lower.contains("트렌딩") || lower.contains("깃허브") || lower.contains("github") || lower.contains("레포") {
        steps.push(StepSpec {
            action: "github".to_string(),
            params: "fetch trending GitHub repositories".to_string(),
        });
    }
    if lower.contains("요약") || lower.contains("summary") {
        steps.push(StepSpec {
            action: "agent".to_string(),
            params: "summarize content".to_string(),
        });
    }

    // Default: single agent step if no specific tools detected
    if steps.is_empty() {
        steps.push(StepSpec {
            action: "agent".to_string(),
            params: text.to_string(),
        });
    }

    steps
}

/// Extract workflow steps from English text keywords.
fn extract_steps_en(text: &str) -> Vec<StepSpec> {
    let lower = text.to_lowercase();
    let mut steps = Vec::new();

    if lower.contains("hackernews") || lower.contains("scrape") || lower.contains("fetch news") {
        steps.push(StepSpec { action: "hackernews".to_string(), params: "fetch top stories".to_string() });
    }
    if lower.contains("translate") || lower.contains("번역") {
        steps.push(StepSpec { action: "translate".to_string(), params: "translate content".to_string() });
    }
    if lower.contains("email") || lower.contains("send") || lower.contains("mail") {
        steps.push(StepSpec { action: "email".to_string(), params: "send as email".to_string() });
    }
    if lower.contains("obsidian") || lower.contains("save to") || lower.contains("write to") {
        steps.push(StepSpec { action: "write_file".to_string(), params: "save as note".to_string() });
    }
    if lower.contains("github") || lower.contains("trending") || lower.contains("repository") {
        steps.push(StepSpec { action: "github".to_string(), params: "fetch trending repos".to_string() });
    }
    if lower.contains("summarize") || lower.contains("summary") {
        steps.push(StepSpec { action: "agent".to_string(), params: "summarize content".to_string() });
    }

    if steps.is_empty() {
        steps.push(StepSpec { action: "agent".to_string(), params: text.to_string() });
    }

    steps
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_cron_intent_korean() {
        assert_eq!(detect_cron_intent("매일 2시에 해커뉴스 번역해줘"), Some("ko"));
        assert_eq!(detect_cron_intent("2시간마다 보고해줘"), Some("ko"));
        assert_eq!(detect_cron_intent("매주 월요일 알림"), Some("ko"));
        assert_eq!(detect_cron_intent("안녕 오늘天气怎么样"), None);
    }

    #[test]
    fn test_detect_cron_intent_english() {
        assert_eq!(detect_cron_intent("every day at 2am fetch hackernews"), Some("en"));
        assert_eq!(detect_cron_intent("hourly check status"), Some("en"));
        assert_eq!(detect_cron_intent("hello how are you"), None);
    }

    #[test]
    fn test_parse_korean_schedule() {
        let req = parse_korean("매일 2시에 해커뉴스 번역해줘").unwrap();
        assert_eq!(req.schedule, "0 2 * * *");
        assert!(req.name.contains("해커") || req.name.contains("번역"));

        let req2 = parse_korean("2시간마다 보고해줘").unwrap();
        assert_eq!(req2.schedule, "0 */2 * * *");

        let req3 = parse_korean("매주 월요일 9시에 알림").unwrap();
        assert_eq!(req3.schedule, "0 9 * * 1");
    }

    #[test]
    fn test_parse_english_schedule() {
        let req = parse_english("daily at 8am fetch hackernews").unwrap();
        assert_eq!(req.schedule, "0 8 * * *");

        let req2 = parse_english("every 2 hours check status").unwrap();
        assert_eq!(req2.schedule, "0 */2 * * *");
    }

    #[test]
    fn test_extract_steps_ko() {
        let steps = extract_steps_ko("매일 2시에 해커뉴스 번역해서 이메일로 보내줘");
        assert!(steps.iter().any(|s| s.action == "hackernews"));
        assert!(steps.iter().any(|s| s.action == "translate"));
        assert!(steps.iter().any(|s| s.action == "email"));

        let steps2 = extract_steps_ko("매일 트렌딩 레포 분석해서 Obsidian에 저장해줘");
        assert!(steps2.iter().any(|s| s.action == "github"));
        assert!(steps2.iter().any(|s| s.action == "write_file"));
    }

    #[test]
    fn test_no_cron() {
        assert!(parse("hello world", "en").is_none());
        assert!(parse("반가워요", "ko").is_none());
    }
}