UNPKG

@restnfeel/agentc-starter-kit

Version:

한국어 기업용 CMS 모듈 - Task Master AI와 함께 빠르게 웹사이트를 구현할 수 있는 재사용 가능한 컴포넌트 시스템

107 lines (105 loc) 2.8 kB
"use client"; /** * @fileoverview Utility functions for the RAG chatbot system * @module utils * * This module provides common utility functions used across the * RAG chatbot system. */ /** * Creates a standardized ChatbotError object */ const createChatbotError = (code, message, details) => ({ code, message, details, timestamp: new Date(), }); /** * Formats a file size in bytes to a human-readable string */ const formatFileSize = (bytes) => { if (bytes === 0) return "0 B"; const k = 1024; const sizes = ["B", "KB", "MB", "GB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`; }; /** * Generates a unique ID for chatbot entities */ const generateId = (prefix = "cb") => { return `${prefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; }; /** * Debounces a function call */ const debounce = (func, wait) => { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; }; /** * Throttles a function call */ const throttle = (func, limit) => { let inThrottle; return (...args) => { if (!inThrottle) { func(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; }; /** * Validates an email address */ const isValidEmail = (email) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; /** * Truncates text to a specified length */ const truncateText = (text, maxLength) => { if (text.length <= maxLength) return text; return text.substring(0, maxLength - 3) + "..."; }; /** * Safely parses JSON with error handling */ const safeJsonParse = (jsonString) => { try { return JSON.parse(jsonString); } catch (_a) { return null; } }; /** * Formats a timestamp to a relative time string */ const formatRelativeTime = (timestamp) => { const now = new Date(); const diffMs = now.getTime() - timestamp.getTime(); const diffSeconds = Math.floor(diffMs / 1000); const diffMinutes = Math.floor(diffSeconds / 60); const diffHours = Math.floor(diffMinutes / 60); const diffDays = Math.floor(diffHours / 24); if (diffSeconds < 60) return "just now"; if (diffMinutes < 60) return `${diffMinutes}m ago`; if (diffHours < 24) return `${diffHours}h ago`; if (diffDays < 7) return `${diffDays}d ago`; return timestamp.toLocaleDateString(); }; export { createChatbotError, debounce, formatFileSize, formatRelativeTime, generateId, isValidEmail, safeJsonParse, throttle, truncateText }; //# sourceMappingURL=index.js.map