|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Autohand AI LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * repeatCli — parsing and config for the --repeat CLI flag (non-interactive mode). |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * autohand --repeat "<interval>" "<prompt>" |
| 10 | + * autohand --repeat "every 5 minutes run tests" |
| 11 | + * autohand --repeat "5m" "run tests" --max-runs 10 --expires "7d" |
| 12 | + */ |
| 13 | + |
| 14 | +import { parseInput, intervalToCron } from './repeat.js'; |
| 15 | + |
| 16 | +// ─── Types ─────────────────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +export interface RepeatFlagOptions { |
| 19 | + maxRuns?: number; |
| 20 | + expires?: string; |
| 21 | +} |
| 22 | + |
| 23 | +export interface RepeatFlagResult { |
| 24 | + interval: string; |
| 25 | + prompt: string; |
| 26 | + maxRuns?: number; |
| 27 | + expiresIn?: string; |
| 28 | + error?: string; |
| 29 | +} |
| 30 | + |
| 31 | +export interface RepeatRunConfig { |
| 32 | + intervalMs: number; |
| 33 | + prompt: string; |
| 34 | + maxRuns?: number; |
| 35 | + expiresInMs: number; |
| 36 | + cronExpression: string; |
| 37 | + humanReadable: string; |
| 38 | +} |
| 39 | + |
| 40 | +// ─── Constants ─────────────────────────────────────────────────────────────── |
| 41 | + |
| 42 | +const VALID_INTERVAL_RE = /^\d+[smhd]$/; |
| 43 | +const INTERVAL_ATTEMPT_RE = /^\d+[a-zA-Z]+$/; |
| 44 | +const VALID_SHORTHAND_RE = /^\d+[smhd]$/; |
| 45 | +const THREE_DAYS_MS = 3 * 24 * 60 * 60 * 1000; |
| 46 | + |
| 47 | +// ─── parseRepeatFlag ───────────────────────────────────────────────────────── |
| 48 | + |
| 49 | +/** |
| 50 | + * Parse --repeat CLI flag arguments into a structured result. |
| 51 | + * |
| 52 | + * Supports: |
| 53 | + * - Two-arg: parseRepeatFlag('5m', 'run tests') |
| 54 | + * - Single natural language: parseRepeatFlag('every 5 minutes run tests') |
| 55 | + * - Options: { maxRuns, expires } |
| 56 | + */ |
| 57 | +export function parseRepeatFlag( |
| 58 | + firstArg: string, |
| 59 | + prompt?: string, |
| 60 | + options?: RepeatFlagOptions, |
| 61 | +): RepeatFlagResult { |
| 62 | + const trimmedFirst = firstArg.trim(); |
| 63 | + |
| 64 | + // Empty / whitespace input |
| 65 | + if (!trimmedFirst) { |
| 66 | + return { interval: '', prompt: '', error: 'No input provided. Usage: --repeat "<schedule>" "<prompt>"' }; |
| 67 | + } |
| 68 | + |
| 69 | + let interval: string; |
| 70 | + let taskPrompt: string; |
| 71 | + |
| 72 | + if (VALID_INTERVAL_RE.test(trimmedFirst)) { |
| 73 | + // First arg is a valid interval like '5m', '2h' |
| 74 | + const trimmedPrompt = prompt?.trim() ?? ''; |
| 75 | + if (!trimmedPrompt) { |
| 76 | + return { interval: trimmedFirst, prompt: '', error: 'No prompt provided. Usage: --repeat "<interval>" "<prompt>"' }; |
| 77 | + } |
| 78 | + interval = trimmedFirst; |
| 79 | + taskPrompt = trimmedPrompt; |
| 80 | + } else if (INTERVAL_ATTEMPT_RE.test(trimmedFirst)) { |
| 81 | + // Looks like an interval attempt but has invalid unit (e.g. '5x') |
| 82 | + return { interval: '', prompt: '', error: `Invalid interval format: "${trimmedFirst}". Use <number><unit> where unit is s, m, h, or d.` }; |
| 83 | + } else { |
| 84 | + // Not an interval — combine with prompt and use natural language parsing |
| 85 | + const combined = prompt ? `${trimmedFirst} ${prompt}`.trim() : trimmedFirst; |
| 86 | + const parsed = parseInput(combined); |
| 87 | + interval = parsed.interval; |
| 88 | + taskPrompt = parsed.prompt; |
| 89 | + |
| 90 | + if (!taskPrompt.trim()) { |
| 91 | + return { interval, prompt: '', error: 'Could not extract a prompt from the input.' }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Apply CLI options |
| 96 | + const maxRuns = options?.maxRuns !== undefined && options.maxRuns > 0 |
| 97 | + ? options.maxRuns |
| 98 | + : undefined; |
| 99 | + |
| 100 | + const expiresIn = options?.expires && VALID_SHORTHAND_RE.test(options.expires) |
| 101 | + ? options.expires |
| 102 | + : undefined; |
| 103 | + |
| 104 | + return { interval, prompt: taskPrompt, maxRuns, expiresIn }; |
| 105 | +} |
| 106 | + |
| 107 | +// ─── buildRepeatRunConfig ──────────────────────────────────────────────────── |
| 108 | + |
| 109 | +/** |
| 110 | + * Convert a parsed RepeatFlagResult into a runtime RepeatRunConfig. |
| 111 | + */ |
| 112 | +export function buildRepeatRunConfig( |
| 113 | + parsed: Pick<RepeatFlagResult, 'interval' | 'prompt' | 'maxRuns' | 'expiresIn'>, |
| 114 | +): RepeatRunConfig { |
| 115 | + const cron = intervalToCron(parsed.interval); |
| 116 | + const expiresInMs = parsed.expiresIn ? shorthandToMs(parsed.expiresIn) : THREE_DAYS_MS; |
| 117 | + |
| 118 | + return { |
| 119 | + intervalMs: cron.intervalMs, |
| 120 | + prompt: parsed.prompt, |
| 121 | + maxRuns: parsed.maxRuns, |
| 122 | + expiresInMs, |
| 123 | + cronExpression: cron.cronExpression, |
| 124 | + humanReadable: cron.humanReadable, |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +// ─── Helpers ───────────────────────────────────────────────────────────────── |
| 129 | + |
| 130 | +function shorthandToMs(shorthand: string): number { |
| 131 | + const match = shorthand.match(/^(\d+)([smhd])$/); |
| 132 | + if (!match) return THREE_DAYS_MS; |
| 133 | + const n = parseInt(match[1], 10); |
| 134 | + const unit = match[2]; |
| 135 | + switch (unit) { |
| 136 | + case 's': return n * 1000; |
| 137 | + case 'm': return n * 60 * 1000; |
| 138 | + case 'h': return n * 60 * 60 * 1000; |
| 139 | + case 'd': return n * 24 * 60 * 60 * 1000; |
| 140 | + default: return THREE_DAYS_MS; |
| 141 | + } |
| 142 | +} |
0 commit comments