import type { OpenAI } from 'openai'; type OpenAIChatCompletionResponseType = OpenAI.Chat.Completions.ChatCompletion & { _request_id?: string | null; }; export type ValidateHumanReadableCronExpressionPromptReturnType = { isValid: boolean; reason: string; _completion: OpenAIChatCompletionResponseType; }; const model = 'gpt-4o-mini'; /** * Validates if a human-readable prompt is related to generating cron expressions or scheduling tasks * @param openai - The OpenAI client * @param prompt - The human-readable prompt * @returns The validation result */ export async function validateHumanReadableCronExpressionPrompt( openai: OpenAI, prompt: string, ): Promise { const response = await openai.chat.completions.create({ model, messages: [ { role: 'system', content: `You are a validator that determines if a user's prompt is related to generating cron expressions or scheduling tasks. Return a JSON object with two fields: 1. "isValid": boolean - true if the prompt is asking about creating a cron expression or scheduling, false otherwise. It should also be false if the prompt is asking to generate an invalid cron expression. 2. "reason": string - a brief explanation of your decision Examples of valid prompts: - "I need a cron job that runs every day at midnight" - "every 15 minutes" - "Every year" - "every 50 minutes" - "every hour" - "every day at 2 AM" Examples of invalid prompts: - "What's the weather like today?" - "Write me a poem about cats" - "How do I make chocolate chip cookies?" `, }, { role: 'user', content: prompt, }, ], }); const content = response.choices[0]?.message?.content; if (!content) { throw new Error('Failed to validate the prompt'); } try { // Parse the JSON response const validation = JSON.parse(content); return { ...validation, _completion: response, }; } catch (error) { throw new Error('Error parsing validation response'); } } export type HumanReadableToCronExpressionReturnType = { cronExpression: string; _completion: OpenAIChatCompletionResponseType; }; /** * Generates a cron expression from a human-readable prompt * @param openai - The OpenAI client * @param prompt - The human-readable prompt * @returns The cron expression */ export async function humanReadableToCronExpression( openai: OpenAI, prompt: string, ): Promise { // Request the OpenAI API for the response without streaming const response = await openai.chat.completions.create({ model, temperature: 0, messages: [ { role: 'system', content: `Below is text describing a cron expression. Your goal is to: - Convert the text to a valid cron expression. - The cron expression you generate must be in 5-field format: minute hour day month dayOfWeek - The cron expression must match this pattern: minute hour day month dayOfWeek - Return only the generated cron expression and nothing else. Here are some examples: - Text: A cron that runs every hour - Cron: 0 * * * * - Text: A cron that runs every 12 hours - Cron: 0 */12 * * * - Text: A cron that runs every 30 minutes - Cron: */30 * * * * - Text: A cron that runs every day at 2 AM - Cron: 0 2 * * * - Text: A cron that runs every 50 minutes - Cron: */50 * * * * - Text: A cron that runs every week on Monday at 9 AM - Cron: 0 9 * * 1`, }, { role: 'user', content: prompt, }, ], }); // Validate the response const content = response.choices[0]?.message?.content?.trim(); if (!content) { throw new Error('Failed to generate a cron expression'); } return { cronExpression: content, _completion: response, }; }