import type { LanguageModel } from "ai"; import { generateText } from "ai"; const CRON_RE = /(?:^|\s)((?:[\d*/,-]+\s+){4,5}[\d*/,-]+)(?:\s|$)/; export function extractCron(raw: string): string { const cleaned = raw.replace(/`/g, "").trim(); const match = cleaned.match(CRON_RE); if (match) return match[1]!.trim(); const firstLine = cleaned.split("\n")[0]!.trim(); if (/^[\d*/,\s-]+$/.test(firstLine)) return firstLine; return cleaned; } const CRON_SYSTEM_PROMPT = "Convert the following natural-language timing description to a cron expression. Reply with ONLY the cron expression, nothing else. No explanation, no markdown, no backticks. Use 5-field format (minute hour day-of-month month day-of-week)."; export async function translateToCron(model: LanguageModel, timing: string): Promise { const { text } = await generateText({ model, system: CRON_SYSTEM_PROMPT, prompt: timing, }); if (!text?.trim()) throw new Error("LLM returned empty cron expression"); return extractCron(text); }