char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
3.8 KiB155 linesraw
1import {
2	MEDIUM_DURATION_THRESHOLD,
3	MS_PER_SECOND,
4	SECONDS_PER_HOUR,
5	SECONDS_PER_MINUTE,
6	SHORT_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 {
17	if (typeof bytes !== 'number') return 'Unknown';
18
19	if (bytes === 0) return '0 Bytes';
20
21	const k = 1024;
22	const sizes = ['Bytes', 'KB', 'MB', 'GB'];
23	const i = Math.floor(Math.log(bytes) / Math.log(k));
24
25	return 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 {
35	if (typeof params !== 'number') return 'Unknown';
36
37	if (params >= 1e9) {
38		return `${(params / 1e9).toFixed(2)}B`;
39	}
40
41	if (params >= 1e6) {
42		return `${(params / 1e6).toFixed(2)}M`;
43	}
44
45	if (params >= 1e3) {
46		return `${(params / 1e3).toFixed(2)}K`;
47	}
48
49	return 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 {
59	if (typeof num !== 'number') return 'Unknown';
60
61	return 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 {
72	try {
73		const parsed = JSON.parse(jsonString);
74
75		return JSON.stringify(parsed, null, 2);
76	} catch {
77		return 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 {
88	return date.toLocaleTimeString('en-US', {
89		hour: '2-digit',
90		hour12: false,
91		minute: '2-digit',
92		second: '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 {
104	if (ms < 0) return '0s';
105
106	const totalSeconds = ms / MS_PER_SECOND;
107
108	if (totalSeconds < SHORT_DURATION_THRESHOLD) {
109		return `${totalSeconds.toFixed(1)}s`;
110	}
111
112	if (totalSeconds < MEDIUM_DURATION_THRESHOLD) {
113		return `${totalSeconds.toFixed(1)}s`;
114	}
115
116	const hours = Math.floor(totalSeconds / SECONDS_PER_HOUR);
117	const minutes = Math.floor((totalSeconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
118	const seconds = Math.floor(totalSeconds % SECONDS_PER_MINUTE);
119	const parts: string[] = [];
120
121	if (hours > 0) {
122		parts.push(`${hours}h`);
123	}
124
125	if (minutes > 0) {
126		parts.push(`${minutes}min`);
127	}
128
129	if (seconds > 0 || parts.length === 0) {
130		parts.push(`${seconds}s`);
131	}
132
133	return 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(
147	label: string,
148	name: string,
149	content: string,
150	extra?: string
151): string {
152	const header = extra ? `${name} (${extra})` : name;
153
154	return `\n\n--- ${label}: ${header} ---\n${content}`;
155}