UNPKG

langflow-chatbot

Version:

Add a Langflow-powered chatbot to your website.

62 lines (61 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDefaultDatetimeHandler = createDefaultDatetimeHandler; exports.isValidDatetimeHandler = isValidDatetimeHandler; exports.normalizeLangflowTimestamp = normalizeLangflowTimestamp; const date_fns_1 = require("date-fns"); /** * Creates a default datetime handler function. * This default handler can be configured with a format string at creation time. * @param format - The format string (e.g., 'relative', 'MM/dd/yyyy', or others supported by date-fns). * Defaults to 'relative' if not provided. * @returns A DatetimeHandler function. */ function createDefaultDatetimeHandler(format = 'relative') { return (datetime) => { try { const dateObj = new Date(datetime); if (format === 'relative') { return (0, date_fns_1.formatDistanceToNow)(dateObj, { addSuffix: true }); } else if (typeof format === 'string' && format.trim() !== '' && format !== 'default') { return (0, date_fns_1.format)(dateObj, format); } else { return dateObj.toLocaleString(); } } catch (e) { // console.error("Error in defaultDatetimeHandler execution:", e); return datetime; // Fallback to original datetime string on error } }; } /** * Validates if a given handler function conforms to the new DatetimeHandler signature ((datetime: string) => string). * It checks if the handler is a function and can be called with a typical ISO date string * without throwing an error, and returns a string. * @param handler - The function to validate. * @returns True if the handler is a valid DatetimeHandler, false otherwise. */ function isValidDatetimeHandler(handler) { if (typeof handler !== 'function') { return false; } try { const testDate = new Date().toISOString(); const result = handler(testDate); // Test with only one argument return typeof result === 'string'; } catch (e) { console.error("Datetime handler validation: Threw an error during test call:", e); return false; } } // Helper to normalize Langflow timestamps (e.g., '2025-05-19 13:33:46 UTC') to ISO format function normalizeLangflowTimestamp(ts) { if (!ts) return undefined; // Replace ' ' with 'T' (only the first occurrence), and ' UTC' with 'Z' return ts.replace(' ', 'T').replace(' UTC', 'Z'); }