-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathformatters.ts
More file actions
163 lines (143 loc) · 4.46 KB
/
formatters.ts
File metadata and controls
163 lines (143 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Utility functions for formatting and displaying data
*/
/**
* Safely converts any value to a formatted string for display
*/
export function formatValue(value: any): string {
if (value === null) return 'null';
if (value === undefined) return 'undefined';
if (typeof value === 'string') return value;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
try {
return JSON.stringify(value, null, 2);
} catch (error) {
return String(value);
}
}
/**
* Formats JSON with proper indentation and returns a formatted string
*/
export function formatJSON(obj: any, maxLength: number = 1000): string {
try {
const jsonString = JSON.stringify(obj, null, 2);
if (jsonString.length > maxLength) {
return jsonString.substring(0, maxLength) + '...';
}
return jsonString;
} catch (error) {
return String(obj);
}
}
/**
* Escapes HTML characters to prevent XSS
*/
export function escapeHtml(text: string): string {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/**
* Formats large text with proper line breaks and structure, optimized for the new conversation flow
*/
export function formatLargeText(text: string): string {
if (!text) return '';
// Escape HTML first
const escaped = escapeHtml(text);
// Simple, safe formatting - just handle line breaks and basic markdown
const formatted = escaped
// Preserve existing double line breaks as paragraph breaks
.replace(/\n\n/g, '</p><p class="mt-3">')
// Convert single line breaks to <br> tags
.replace(/\n/g, '<br>')
// Format inline code (backticks)
.replace(/`([^`]+)`/g, '<code class="bg-gray-100 text-gray-800 px-1.5 py-0.5 rounded text-sm font-mono">$1</code>')
// Format bold text
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
// Format italic text
.replace(/\*([^*]+)\*/g, '<em>$1</em>');
// Wrap in paragraph tags
return `<p>${formatted}</p>`;
}
/**
* Determines if a value is a complex object that should be JSON-formatted
*/
export function isComplexObject(value: any): boolean {
return value !== null &&
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0;
}
/**
* Truncates text to a specified length with ellipsis
*/
export function truncateText(text: string, maxLength: number = 200): string {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength) + '...';
}
/**
* Formats timestamp for display in the conversation flow
*/
export function formatTimestamp(timestamp: string | Date): string {
try {
const date = new Date(timestamp);
const now = new Date();
const diff = now.getTime() - date.getTime();
// Less than a minute ago
if (diff < 60000) {
return 'Just now';
}
// Less than an hour ago
if (diff < 3600000) {
const minutes = Math.floor(diff / 60000);
return `${minutes}m ago`;
}
// Less than a day ago
if (diff < 86400000) {
const hours = Math.floor(diff / 3600000);
return `${hours}h ago`;
}
// More than a day ago - show time
return date.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
hour12: true
});
} catch {
return String(timestamp);
}
}
/**
* Formats file size for display
*/
export function formatFileSize(bytes: number): string {
const sizes = ['B', 'KB', 'MB', 'GB'];
if (bytes === 0) return '0 B';
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
}
/**
* Creates a content preview for message summaries
*/
export function createContentPreview(content: any, maxLength: number = 100): string {
if (typeof content === 'string') {
return content.length > maxLength ? content.substring(0, maxLength) + '...' : content;
}
if (Array.isArray(content)) {
const textContent = content.find(c => c.type === 'text')?.text || '';
if (textContent) {
return textContent.length > maxLength ? textContent.substring(0, maxLength) + '...' : textContent;
}
return `${content.length} content blocks`;
}
if (content && typeof content === 'object') {
if (content.text) {
return content.text.length > maxLength ? content.text.substring(0, maxLength) + '...' : content.text;
}
return 'Complex content';
}
return 'No content';
}