/ utils / mcp / dateTimeParser.ts
dateTimeParser.ts
  1  import { queryHaiku } from '../../services/api/claude.js'
  2  import { logError } from '../log.js'
  3  import { extractTextContent } from '../messages.js'
  4  import { asSystemPrompt } from '../systemPromptType.js'
  5  
  6  export type DateTimeParseResult =
  7    | { success: true; value: string }
  8    | { success: false; error: string }
  9  
 10  /**
 11   * Parse natural language date/time input into ISO 8601 format using Haiku.
 12   *
 13   * Examples:
 14   * - "tomorrow at 3pm" → "2025-10-15T15:00:00-07:00"
 15   * - "next Monday" → "2025-10-20"
 16   * - "in 2 hours" → "2025-10-14T12:30:00-07:00"
 17   *
 18   * @param input The natural language date/time string from the user
 19   * @param format Whether to parse as 'date' (YYYY-MM-DD) or 'date-time' (full ISO 8601 with time)
 20   * @param signal AbortSignal for cancellation
 21   * @returns Parsed ISO 8601 string or error message
 22   */
 23  export async function parseNaturalLanguageDateTime(
 24    input: string,
 25    format: 'date' | 'date-time',
 26    signal: AbortSignal,
 27  ): Promise<DateTimeParseResult> {
 28    // Get current datetime with timezone for context
 29    const now = new Date()
 30    const currentDateTime = now.toISOString()
 31    const timezoneOffset = -now.getTimezoneOffset() // minutes, inverted sign
 32    const tzHours = Math.floor(Math.abs(timezoneOffset) / 60)
 33    const tzMinutes = Math.abs(timezoneOffset) % 60
 34    const tzSign = timezoneOffset >= 0 ? '+' : '-'
 35    const timezone = `${tzSign}${String(tzHours).padStart(2, '0')}:${String(tzMinutes).padStart(2, '0')}`
 36    const dayOfWeek = now.toLocaleDateString('en-US', { weekday: 'long' })
 37  
 38    // Build system prompt with context
 39    const systemPrompt = asSystemPrompt([
 40      'You are a date/time parser that converts natural language into ISO 8601 format.',
 41      'You MUST respond with ONLY the ISO 8601 formatted string, with no explanation or additional text.',
 42      'If the input is ambiguous, prefer future dates over past dates.',
 43      "For times without dates, use today's date.",
 44      'For dates without times, do not include a time component.',
 45      'If the input is incomplete or you cannot confidently parse it into a valid date, respond with exactly "INVALID" (nothing else).',
 46      'Examples of INVALID input: partial dates like "2025-01-", lone numbers like "13", gibberish.',
 47      'Examples of valid natural language: "tomorrow", "next Monday", "jan 1st 2025", "in 2 hours", "yesterday".',
 48    ])
 49  
 50    // Build user prompt with rich context
 51    const formatDescription =
 52      format === 'date'
 53        ? 'YYYY-MM-DD (date only, no time)'
 54        : `YYYY-MM-DDTHH:MM:SS${timezone} (full date-time with timezone)`
 55  
 56    const userPrompt = `Current context:
 57  - Current date and time: ${currentDateTime} (UTC)
 58  - Local timezone: ${timezone}
 59  - Day of week: ${dayOfWeek}
 60  
 61  User input: "${input}"
 62  
 63  Output format: ${formatDescription}
 64  
 65  Parse the user's input into ISO 8601 format. Return ONLY the formatted string, or "INVALID" if the input is incomplete or unparseable.`
 66  
 67    try {
 68      const result = await queryHaiku({
 69        systemPrompt,
 70        userPrompt,
 71        signal,
 72        options: {
 73          querySource: 'mcp_datetime_parse',
 74          agents: [],
 75          isNonInteractiveSession: false,
 76          hasAppendSystemPrompt: false,
 77          mcpTools: [],
 78          enablePromptCaching: false,
 79        },
 80      })
 81  
 82      // Extract text from result
 83      const parsedText = extractTextContent(result.message.content).trim()
 84  
 85      // Validate that we got something usable
 86      if (!parsedText || parsedText === 'INVALID') {
 87        return {
 88          success: false,
 89          error: 'Unable to parse date/time from input',
 90        }
 91      }
 92  
 93      // Basic sanity check - should start with a digit (year)
 94      if (!/^\d{4}/.test(parsedText)) {
 95        return {
 96          success: false,
 97          error: 'Unable to parse date/time from input',
 98        }
 99      }
100  
101      return { success: true, value: parsedText }
102    } catch (error) {
103      // Log error but don't expose details to user
104      logError(error)
105      return {
106        success: false,
107        error:
108          'Unable to parse date/time. Please enter in ISO 8601 format manually.',
109      }
110    }
111  }
112  
113  /**
114   * Check if a string looks like it might be an ISO 8601 date/time.
115   * Used to decide whether to attempt NL parsing.
116   */
117  export function looksLikeISO8601(input: string): boolean {
118    // ISO 8601 date: YYYY-MM-DD
119    // ISO 8601 datetime: YYYY-MM-DDTHH:MM:SS...
120    return /^\d{4}-\d{2}-\d{2}(T|$)/.test(input.trim())
121  }