char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { 2MEDIUM_DURATION_THRESHOLD , 3MS_PER_SECOND , 4SECONDS_PER_HOUR , 5SECONDS_PER_MINUTE , 6SHORT_DURATION_THRESHOLD 7} from '$lib/constants' ; 8 9/** 10* Formats file size in bytes to human readable format 11* Supports Bytes, KB, MB, and GB 12* 13* @param bytes - File size in bytes (or unknown for safety) 14* @returns Formatted file size string 15*/ 16export function formatFileSize ( bytes :number | unknown ) :string { 17if ( typeof bytes !== 'number' ) return 'Unknown' ; 18 19if ( bytes === 0 ) return '0 Bytes' ; 20 21const k = 1024 ; 22const sizes = [ 'Bytes' , 'KB' , 'MB' , 'GB' ]; 23const i = Math . floor ( Math . log ( bytes ) / Math . log ( k )); 24 25return parseFloat (( bytes / Math . pow ( k , i )). toFixed ( 2 )) + ' ' + sizes [ i ]; 26} 27 28/** 29* Format parameter count to human-readable format (B, M, K) 30* 31* @param params - Parameter count 32* @returns Human-readable parameter count 33*/ 34export function formatParameters ( params :number | unknown ) :string { 35if ( typeof params !== 'number' ) return 'Unknown' ; 36 37if ( params >= 1e9 ) { 38return ` ${( params / 1e9 ). toFixed ( 2 )} B` ; 39} 40 41if ( params >= 1e6 ) { 42return ` ${( params / 1e6 ). toFixed ( 2 )} M` ; 43} 44 45if ( params >= 1e3 ) { 46return ` ${( params / 1e3 ). toFixed ( 2 )} K` ; 47} 48 49return params . toString (); 50} 51 52/** 53* Format number with locale-specific thousands separators 54* 55* @param num - Number to format 56* @returns Human-readable number 57*/ 58export function formatNumber ( num :number | unknown ) :string { 59if ( typeof num !== 'number' ) return 'Unknown' ; 60 61return num . toLocaleString (); 62} 63 64/** 65* Format JSON string with pretty printing (2-space indentation) 66* Returns original string if parsing fails 67* 68* @param jsonString - JSON string to format 69* @returns Pretty-printed JSON string or original if invalid 70*/ 71export function formatJsonPretty ( jsonString :string ) :string { 72try { 73const parsed = JSON . parse ( jsonString ); 74 75return JSON . stringify ( parsed , null , 2 ); 76} catch { 77return jsonString ; 78} 79} 80 81/** 82* Format time as HH:MM:SS in 24-hour format 83* 84* @param date - Date object to format 85* @returns Formatted time string (HH:MM:SS) 86*/ 87export function formatTime ( date :Date ) :string { 88return date . toLocaleTimeString ( 'en-US' , { 89hour :'2-digit' , 90hour12 :false , 91minute :'2-digit' , 92second :'2-digit' 93}); 94} 95 96/** 97* Formats milliseconds to a human-readable time string for performance metrics. 98* Examples: "4h 12min 54s", "12min 34s", "45s", "0.5s" 99* 100* @param ms - Time in milliseconds 101* @returns Formatted time string 102*/ 103export function formatPerformanceTime ( ms :number ) :string { 104if ( ms < 0 ) return '0s' ; 105 106const totalSeconds = ms / MS_PER_SECOND ; 107 108if ( totalSeconds < SHORT_DURATION_THRESHOLD ) { 109return ` ${ totalSeconds . toFixed ( 1 )} s` ; 110} 111 112if ( totalSeconds < MEDIUM_DURATION_THRESHOLD ) { 113return ` ${ totalSeconds . toFixed ( 1 )} s` ; 114} 115 116const hours = Math . floor ( totalSeconds / SECONDS_PER_HOUR ); 117const minutes = Math . floor (( totalSeconds % SECONDS_PER_HOUR ) / SECONDS_PER_MINUTE ); 118const seconds = Math . floor ( totalSeconds % SECONDS_PER_MINUTE ); 119const parts :string [] = []; 120 121if ( hours > 0 ) { 122parts . push ( ` ${ hours } h` ); 123} 124 125if ( minutes > 0 ) { 126parts . push ( ` ${ minutes } min` ); 127} 128 129if ( seconds > 0 || parts . length === 0 ) { 130parts . push ( ` ${ seconds } s` ); 131} 132 133return parts . join ( ' ' ); 134} 135 136/** 137* Formats attachment content for API requests with consistent header style. 138* Used when converting message attachments to text content parts. 139* 140* @param label - Type label (e.g., 'File', 'PDF File', 'MCP Prompt') 141* @param name - File or attachment name 142* @param content - The actual content to include 143* @param extra - Optional extra info to append to name (e.g., server name for MCP) 144* @returns Formatted string with header and content 145*/ 146export function formatAttachmentText ( 147label :string , 148name :string , 149content :string , 150extra ?:string 151) :string { 152const header = extra ?` ${ name } ( ${ extra } )` :name ; 153 154return `\n\n--- ${ label } : ${ header } ---\n ${ content } ` ; 155}