@istmx/ui 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -1
- package/bin/istmx.mjs +53 -12
- package/package.json +2 -2
- package/registry/ai-chat-block/ai-chat-block.tsx +460 -0
- package/registry/ai-chat-block/ai-chat-input.tsx +690 -0
- package/registry/ai-chat-block/attachment.tsx +605 -0
- package/registry/ai-chat-block/chat-code-block.tsx +50 -0
- package/registry/ai-chat-block/chat-composer.tsx +51 -0
- package/registry/ai-chat-block/chat-logo.tsx +18 -0
- package/registry/ai-chat-block/chat-message.tsx +161 -0
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -0
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -0
- package/registry/ai-chat-block/code-highlighter.tsx +357 -0
- package/registry/ai-chat-block/default-attachments.tsx +42 -0
- package/registry/ai-chat-block/index.ts +16 -0
- package/registry/ai-chat-block/message-content.tsx +287 -0
- package/registry/ai-chat-block/message-list.tsx +93 -0
- package/registry/ai-chat-block/send-button.tsx +102 -0
- package/registry/ai-chat-block/types.ts +71 -0
- package/registry/ai-chat-block/utils.ts +6 -0
- package/registry/ai-chat-block/voice-button.tsx +66 -0
- package/registry/ai-chat-input/ai-chat-input.tsx +690 -0
- package/registry/ai-chat-input/attachment.tsx +605 -0
- package/registry/ai-chat-input/send-button.tsx +102 -0
- package/registry/ai-chat-input/types.ts +71 -0
- package/registry/ai-chat-input/utils.ts +6 -0
- package/registry/ai-chat-input/voice-button.tsx +66 -0
- package/registry/button.tsx +4 -7
- package/registry/streaming-text.tsx +278 -0
- package/registry/text-reveal.tsx +307 -0
- package/registry/image-effects-card.tsx +0 -197
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { cn } from './utils'
|
|
2
|
+
|
|
3
|
+
export type TokenType =
|
|
4
|
+
| 'tag'
|
|
5
|
+
| 'attribute'
|
|
6
|
+
| 'string'
|
|
7
|
+
| 'keyword'
|
|
8
|
+
| 'component'
|
|
9
|
+
| 'type'
|
|
10
|
+
| 'function'
|
|
11
|
+
| 'literal'
|
|
12
|
+
| 'number'
|
|
13
|
+
| 'comment'
|
|
14
|
+
| 'punctuation'
|
|
15
|
+
| 'identifier'
|
|
16
|
+
| 'space'
|
|
17
|
+
|
|
18
|
+
export type Token = {
|
|
19
|
+
type: TokenType
|
|
20
|
+
value: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Visual color palette matching the target editor reference
|
|
24
|
+
export const TOKEN_COLORS: Record<TokenType, string> = {
|
|
25
|
+
tag: '#fb923c', // Warm orange for JSX / HTML tags (<AppleCarouselRoot>, <AnimatedText>, </MotionLink>)
|
|
26
|
+
attribute: '#fde047', // Light / warm yellow for JSX attributes (aria-label, prefix, href, initial, animate)
|
|
27
|
+
string: '#4ade80', // Emerald green for string values ("Featured", "blur", "@/components/...")
|
|
28
|
+
keyword: '#f472b6', // Pink / magenta for language keywords (import, from, const, return, export)
|
|
29
|
+
function: '#38bdf8', // Sky blue for function calls and methods (motion.create, useState)
|
|
30
|
+
component: '#2dd4bf', // Teal / cyan for capitalized components & types in expressions
|
|
31
|
+
type: '#2dd4bf', // Teal / cyan for TypeScript types
|
|
32
|
+
literal: '#fdba74', // Warm peach for booleans & null (false, true, null)
|
|
33
|
+
number: '#fdba74', // Warm peach for numbers
|
|
34
|
+
comment: '#71717a', // Muted slate gray for comments
|
|
35
|
+
punctuation: '#94a3b8', // Subtle neutral gray for punctuation (<, >, =, ;, {}, ())
|
|
36
|
+
identifier: '#e2e8f0', // Warm off-white for plain variables & identifiers
|
|
37
|
+
space: 'inherit',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const KEYWORDS = new Set([
|
|
41
|
+
'import',
|
|
42
|
+
'from',
|
|
43
|
+
'export',
|
|
44
|
+
'default',
|
|
45
|
+
'const',
|
|
46
|
+
'let',
|
|
47
|
+
'var',
|
|
48
|
+
'function',
|
|
49
|
+
'return',
|
|
50
|
+
'if',
|
|
51
|
+
'else',
|
|
52
|
+
'switch',
|
|
53
|
+
'case',
|
|
54
|
+
'break',
|
|
55
|
+
'continue',
|
|
56
|
+
'for',
|
|
57
|
+
'while',
|
|
58
|
+
'do',
|
|
59
|
+
'try',
|
|
60
|
+
'catch',
|
|
61
|
+
'finally',
|
|
62
|
+
'throw',
|
|
63
|
+
'new',
|
|
64
|
+
'delete',
|
|
65
|
+
'typeof',
|
|
66
|
+
'instanceof',
|
|
67
|
+
'void',
|
|
68
|
+
'in',
|
|
69
|
+
'of',
|
|
70
|
+
'yield',
|
|
71
|
+
'await',
|
|
72
|
+
'async',
|
|
73
|
+
'as',
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
const TYPES = new Set([
|
|
77
|
+
'type',
|
|
78
|
+
'interface',
|
|
79
|
+
'extends',
|
|
80
|
+
'implements',
|
|
81
|
+
'class',
|
|
82
|
+
'enum',
|
|
83
|
+
'string',
|
|
84
|
+
'number',
|
|
85
|
+
'boolean',
|
|
86
|
+
'any',
|
|
87
|
+
'unknown',
|
|
88
|
+
'never',
|
|
89
|
+
'symbol',
|
|
90
|
+
'bigint',
|
|
91
|
+
'void',
|
|
92
|
+
'Record',
|
|
93
|
+
'ReactNode',
|
|
94
|
+
'React',
|
|
95
|
+
'keyof',
|
|
96
|
+
'readonly',
|
|
97
|
+
])
|
|
98
|
+
|
|
99
|
+
const LITERALS = new Set(['true', 'false', 'null', 'undefined'])
|
|
100
|
+
|
|
101
|
+
export function tokenizeCode(code: string): Token[][] {
|
|
102
|
+
const lines = code.replace(/\r\n/g, '\n').split('\n')
|
|
103
|
+
const result: Token[][] = []
|
|
104
|
+
let inTag = false
|
|
105
|
+
let braceDepth = 0
|
|
106
|
+
|
|
107
|
+
for (const line of lines) {
|
|
108
|
+
const tokens: Token[] = []
|
|
109
|
+
let i = 0
|
|
110
|
+
const len = line.length
|
|
111
|
+
|
|
112
|
+
while (i < len) {
|
|
113
|
+
// 1. Whitespace
|
|
114
|
+
if (/\s/.test(line[i])) {
|
|
115
|
+
let space = ''
|
|
116
|
+
while (i < len && /\s/.test(line[i])) {
|
|
117
|
+
space += line[i++]
|
|
118
|
+
}
|
|
119
|
+
tokens.push({ type: 'space', value: space })
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 2. Comments
|
|
124
|
+
if (line.startsWith('{/*', i)) {
|
|
125
|
+
const end = line.indexOf('*/}', i)
|
|
126
|
+
const val = end === -1 ? line.slice(i) : line.slice(i, end + 3)
|
|
127
|
+
tokens.push({ type: 'comment', value: val })
|
|
128
|
+
i += val.length
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
if (line.startsWith('//', i)) {
|
|
132
|
+
const val = line.slice(i)
|
|
133
|
+
tokens.push({ type: 'comment', value: val })
|
|
134
|
+
break
|
|
135
|
+
}
|
|
136
|
+
if (line.startsWith('/*', i)) {
|
|
137
|
+
const end = line.indexOf('*/', i)
|
|
138
|
+
const val = end === -1 ? line.slice(i) : line.slice(i, end + 2)
|
|
139
|
+
tokens.push({ type: 'comment', value: val })
|
|
140
|
+
i += val.length
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 3. Strings
|
|
145
|
+
if (line[i] === '"' || line[i] === "'" || line[i] === '`') {
|
|
146
|
+
const quote = line[i]
|
|
147
|
+
let str = quote
|
|
148
|
+
i++
|
|
149
|
+
while (i < len) {
|
|
150
|
+
if (line[i] === '\\') {
|
|
151
|
+
str += line[i++]
|
|
152
|
+
if (i < len) str += line[i++]
|
|
153
|
+
} else if (line[i] === quote) {
|
|
154
|
+
str += line[i++]
|
|
155
|
+
break
|
|
156
|
+
} else {
|
|
157
|
+
str += line[i++]
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
tokens.push({ type: 'string', value: str })
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 4. Closing JSX Tag: </Tag
|
|
165
|
+
if (line.startsWith('</', i)) {
|
|
166
|
+
const m = line.slice(i + 2).match(/^[a-zA-Z_$][a-zA-Z0-9_$.-]*/)
|
|
167
|
+
if (m) {
|
|
168
|
+
tokens.push({ type: 'punctuation', value: '</' })
|
|
169
|
+
tokens.push({ type: 'tag', value: m[0] })
|
|
170
|
+
i += 2 + m[0].length
|
|
171
|
+
inTag = true
|
|
172
|
+
braceDepth = 0
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 5. Opening JSX Tag: <Tag
|
|
178
|
+
if (line[i] === '<') {
|
|
179
|
+
const m = line.slice(i + 1).match(/^[a-zA-Z_$][a-zA-Z0-9_$.-]*/)
|
|
180
|
+
if (m && !line.startsWith('<=', i) && !line.startsWith('<<', i)) {
|
|
181
|
+
tokens.push({ type: 'punctuation', value: '<' })
|
|
182
|
+
tokens.push({ type: 'tag', value: m[0] })
|
|
183
|
+
i += 1 + m[0].length
|
|
184
|
+
inTag = true
|
|
185
|
+
braceDepth = 0
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Track braces inside JSX attributes
|
|
191
|
+
if (inTag && line[i] === '{') {
|
|
192
|
+
braceDepth++
|
|
193
|
+
tokens.push({ type: 'punctuation', value: '{' })
|
|
194
|
+
i++
|
|
195
|
+
continue
|
|
196
|
+
}
|
|
197
|
+
if (inTag && line[i] === '}') {
|
|
198
|
+
if (braceDepth > 0) braceDepth--
|
|
199
|
+
tokens.push({ type: 'punctuation', value: '}' })
|
|
200
|
+
i++
|
|
201
|
+
continue
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Tag closers: /> or > (when outside attribute expressions)
|
|
205
|
+
if (inTag && braceDepth === 0 && line.startsWith('/>', i)) {
|
|
206
|
+
tokens.push({ type: 'punctuation', value: '/>' })
|
|
207
|
+
i += 2
|
|
208
|
+
inTag = false
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
if (inTag && braceDepth === 0 && line[i] === '>') {
|
|
212
|
+
tokens.push({ type: 'punctuation', value: '>' })
|
|
213
|
+
i += 1
|
|
214
|
+
inTag = false
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// 6. Multi-character operators
|
|
219
|
+
const two = line.slice(i, i + 2)
|
|
220
|
+
const three = line.slice(i, i + 3)
|
|
221
|
+
if (three === '===' || three === '!==') {
|
|
222
|
+
tokens.push({ type: 'punctuation', value: three })
|
|
223
|
+
i += 3
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
if (
|
|
227
|
+
two === '=>' ||
|
|
228
|
+
two === '==' ||
|
|
229
|
+
two === '!=' ||
|
|
230
|
+
two === '<=' ||
|
|
231
|
+
two === '>=' ||
|
|
232
|
+
two === '&&' ||
|
|
233
|
+
two === '||' ||
|
|
234
|
+
two === '??' ||
|
|
235
|
+
two === '?.'
|
|
236
|
+
) {
|
|
237
|
+
tokens.push({ type: 'punctuation', value: two })
|
|
238
|
+
i += 2
|
|
239
|
+
continue
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Check preceding non-space token in this line
|
|
243
|
+
const prevToken = [...tokens].reverse().find((t) => t.type !== 'space')
|
|
244
|
+
const isAfterDeclaration =
|
|
245
|
+
prevToken && ['const', 'let', 'var', 'export'].includes(prevToken.value)
|
|
246
|
+
|
|
247
|
+
// 7. JSX Attribute: word before = (inTag or in multiline JSX)
|
|
248
|
+
const attrMatch = line
|
|
249
|
+
.slice(i)
|
|
250
|
+
.match(/^[a-zA-Z_$][a-zA-Z0-9_$-]*(?=\s*=)/)
|
|
251
|
+
if (attrMatch && (inTag || !isAfterDeclaration)) {
|
|
252
|
+
tokens.push({ type: 'attribute', value: attrMatch[0] })
|
|
253
|
+
i += attrMatch[0].length
|
|
254
|
+
continue
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 8. Boolean attribute inside tag when braceDepth === 0
|
|
258
|
+
if (inTag && braceDepth === 0) {
|
|
259
|
+
const boolAttrMatch = line.slice(i).match(/^[a-zA-Z_$][a-zA-Z0-9_$-]*/)
|
|
260
|
+
if (boolAttrMatch) {
|
|
261
|
+
const val = boolAttrMatch[0]
|
|
262
|
+
let nextIdx = i + val.length
|
|
263
|
+
while (nextIdx < len && /\s/.test(line[nextIdx])) nextIdx++
|
|
264
|
+
if (
|
|
265
|
+
nextIdx >= len ||
|
|
266
|
+
line[nextIdx] === '>' ||
|
|
267
|
+
line.startsWith('/>', nextIdx) ||
|
|
268
|
+
/^[a-zA-Z]/.test(line[nextIdx])
|
|
269
|
+
) {
|
|
270
|
+
tokens.push({ type: 'attribute', value: val })
|
|
271
|
+
i += val.length
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 9. Numbers
|
|
278
|
+
const numMatch = line.slice(i).match(/^\d+(?:\.\d+)?\b/)
|
|
279
|
+
if (numMatch) {
|
|
280
|
+
tokens.push({ type: 'number', value: numMatch[0] })
|
|
281
|
+
i += numMatch[0].length
|
|
282
|
+
continue
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 10. Word / identifier
|
|
286
|
+
const idMatch = line.slice(i).match(/^[a-zA-Z_$][a-zA-Z0-9_$]*/)
|
|
287
|
+
if (idMatch) {
|
|
288
|
+
const val = idMatch[0]
|
|
289
|
+
i += val.length
|
|
290
|
+
|
|
291
|
+
let j = i
|
|
292
|
+
while (j < len && /\s/.test(line[j])) j++
|
|
293
|
+
const isCall = line[j] === '('
|
|
294
|
+
|
|
295
|
+
if (KEYWORDS.has(val)) {
|
|
296
|
+
tokens.push({ type: 'keyword', value: val })
|
|
297
|
+
} else if (LITERALS.has(val)) {
|
|
298
|
+
tokens.push({ type: 'literal', value: val })
|
|
299
|
+
} else if (TYPES.has(val)) {
|
|
300
|
+
tokens.push({ type: 'type', value: val })
|
|
301
|
+
} else if (isCall) {
|
|
302
|
+
tokens.push({ type: 'function', value: val })
|
|
303
|
+
} else if (/^[A-Z]/.test(val)) {
|
|
304
|
+
tokens.push({ type: 'component', value: val })
|
|
305
|
+
} else {
|
|
306
|
+
tokens.push({ type: 'identifier', value: val })
|
|
307
|
+
}
|
|
308
|
+
continue
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 11. Single-char punctuation
|
|
312
|
+
tokens.push({ type: 'punctuation', value: line[i] })
|
|
313
|
+
i++
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
result.push(tokens)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return result
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function TokenSpan({ token }: { token: Token }) {
|
|
323
|
+
if (token.type === 'space') {
|
|
324
|
+
return <>{token.value}</>
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const color = TOKEN_COLORS[token.type] ?? '#e2e8f0'
|
|
328
|
+
const isItalic = token.type === 'comment'
|
|
329
|
+
|
|
330
|
+
return (
|
|
331
|
+
<span style={{ color }} className={isItalic ? 'italic' : undefined}>
|
|
332
|
+
{token.value}
|
|
333
|
+
</span>
|
|
334
|
+
)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
export function CodeHighlighter({
|
|
339
|
+
code,
|
|
340
|
+
className,
|
|
341
|
+
}: {
|
|
342
|
+
code: string
|
|
343
|
+
className?: string
|
|
344
|
+
}) {
|
|
345
|
+
const lines = tokenizeCode(code)
|
|
346
|
+
return (
|
|
347
|
+
<pre className={cn('w-full max-w-full overflow-x-auto p-4 font-mono text-[13.5px] leading-[1.7] antialiased select-text sm:p-5 sm:text-[14.5px] sm:leading-[1.72]', className)} style={{ fontFamily: 'var(--font-geist-mono), ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace' }}>
|
|
348
|
+
<code>{lines.map((line, lineIndex) => (
|
|
349
|
+
<span key={`highlight-line-${lineIndex}`} className="block min-h-[1.7em] whitespace-pre">
|
|
350
|
+
{line.length ? line.map((token, tokenIndex) => (
|
|
351
|
+
<TokenSpan key={`highlight-${lineIndex}-${tokenIndex}`} token={token} />
|
|
352
|
+
)) : <> </>}
|
|
353
|
+
</span>
|
|
354
|
+
))}</code>
|
|
355
|
+
</pre>
|
|
356
|
+
)
|
|
357
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IconFile,
|
|
3
|
+
IconHeadphones,
|
|
4
|
+
IconPhoto,
|
|
5
|
+
IconVideo,
|
|
6
|
+
} from '@tabler/icons-react'
|
|
7
|
+
import type { AttachmentOption } from './types'
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_ATTACHMENT_OPTIONS: readonly AttachmentOption[] = [
|
|
10
|
+
{
|
|
11
|
+
id: 'images',
|
|
12
|
+
label: 'Images',
|
|
13
|
+
description: 'Add photos and images',
|
|
14
|
+
icon: <IconPhoto size={17} stroke={1.7} aria-hidden="true" />,
|
|
15
|
+
accept: 'image/*',
|
|
16
|
+
multiple: true,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
id: 'documents',
|
|
20
|
+
label: 'Files and documents',
|
|
21
|
+
description: 'PDF, text, office files, and more',
|
|
22
|
+
icon: <IconFile size={17} stroke={1.7} aria-hidden="true" />,
|
|
23
|
+
accept: '.pdf,.txt,.md,.csv,.json,.doc,.docx,.xls,.xlsx,.ppt,.pptx,application/*,text/*',
|
|
24
|
+
multiple: true,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: 'audio',
|
|
28
|
+
label: 'Audio',
|
|
29
|
+
description: 'Add an audio recording',
|
|
30
|
+
icon: <IconHeadphones size={17} stroke={1.7} aria-hidden="true" />,
|
|
31
|
+
accept: 'audio/*',
|
|
32
|
+
multiple: true,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: 'video',
|
|
36
|
+
label: 'Video',
|
|
37
|
+
description: 'Add a video clip',
|
|
38
|
+
icon: <IconVideo size={17} stroke={1.7} aria-hidden="true" />,
|
|
39
|
+
accept: 'video/*',
|
|
40
|
+
multiple: true,
|
|
41
|
+
},
|
|
42
|
+
]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
export { default as AiChatBlock } from './ai-chat-block'
|
|
4
|
+
export type { AiChatBlockProps } from './ai-chat-block'
|
|
5
|
+
export { DEFAULT_ATTACHMENT_OPTIONS } from './default-attachments'
|
|
6
|
+
export type {
|
|
7
|
+
AttachmentOption,
|
|
8
|
+
ChatAttachment,
|
|
9
|
+
ChatAttachmentKind,
|
|
10
|
+
ChatRequest,
|
|
11
|
+
ChatResponse,
|
|
12
|
+
ChatSendError,
|
|
13
|
+
ChatStreamChunk,
|
|
14
|
+
ChatThread,
|
|
15
|
+
ChatTurn,
|
|
16
|
+
} from './types'
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useMemo } from 'react'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
import { motion, useReducedMotion } from 'motion/react'
|
|
6
|
+
import ChatCodeBlock from './chat-code-block'
|
|
7
|
+
|
|
8
|
+
function inlineContent(value: string): ReactNode[] {
|
|
9
|
+
const parts: ReactNode[] = []
|
|
10
|
+
const pattern = /(`[^`]+`|\*\*[^*]+\*\*|\*[^*]+\*|\[[^\]]+\]\([^)]+\))/gu
|
|
11
|
+
let cursor = 0
|
|
12
|
+
let match: RegExpExecArray | null
|
|
13
|
+
|
|
14
|
+
while ((match = pattern.exec(value))) {
|
|
15
|
+
if (match.index > cursor) parts.push(value.slice(cursor, match.index))
|
|
16
|
+
const token = match[0]
|
|
17
|
+
const key = `inline-${match.index}`
|
|
18
|
+
|
|
19
|
+
if (token.startsWith('`')) {
|
|
20
|
+
parts.push(
|
|
21
|
+
<code
|
|
22
|
+
key={key}
|
|
23
|
+
className="border-border/70 bg-surface/70 rounded-md border px-1.5 py-0.5 font-mono text-[0.9em] text-[var(--chat-accent,var(--foreground))]"
|
|
24
|
+
>
|
|
25
|
+
{token.slice(1, -1)}
|
|
26
|
+
</code>,
|
|
27
|
+
)
|
|
28
|
+
} else if (token.startsWith('**')) {
|
|
29
|
+
parts.push(<strong key={key}>{token.slice(2, -2)}</strong>)
|
|
30
|
+
} else if (token.startsWith('*')) {
|
|
31
|
+
parts.push(<em key={key}>{token.slice(1, -1)}</em>)
|
|
32
|
+
} else {
|
|
33
|
+
const link = token.match(/^\[([^\]]+)\]\(([^)]+)\)$/u)
|
|
34
|
+
if (link) {
|
|
35
|
+
const [, label, href] = link
|
|
36
|
+
const safe = /^(https?:|mailto:|\/|#)/u.test(href)
|
|
37
|
+
parts.push(
|
|
38
|
+
safe ? (
|
|
39
|
+
<a
|
|
40
|
+
key={key}
|
|
41
|
+
href={href}
|
|
42
|
+
target={href.startsWith('http') ? '_blank' : undefined}
|
|
43
|
+
rel={href.startsWith('http') ? 'noreferrer' : undefined}
|
|
44
|
+
className="text-[var(--chat-accent,var(--foreground))] underline underline-offset-4"
|
|
45
|
+
>
|
|
46
|
+
{label}
|
|
47
|
+
</a>
|
|
48
|
+
) : (
|
|
49
|
+
label
|
|
50
|
+
),
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
cursor = pattern.lastIndex
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (cursor < value.length) parts.push(value.slice(cursor))
|
|
58
|
+
return parts
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type MessageBlock =
|
|
62
|
+
| { kind: 'code'; language: string; value: string }
|
|
63
|
+
| { kind: 'heading'; level: number; value: string }
|
|
64
|
+
| { kind: 'list'; ordered: boolean; items: string[] }
|
|
65
|
+
| { kind: 'paragraph'; value: string }
|
|
66
|
+
|
|
67
|
+
type LiveBlock = { kind: 'text'; value: string } | { kind: 'code'; language: string; value: string }
|
|
68
|
+
|
|
69
|
+
function parseLiveBlocks(value: string): LiveBlock[] {
|
|
70
|
+
const lines = value.split('\n')
|
|
71
|
+
const blocks: LiveBlock[] = []
|
|
72
|
+
let text: string[] = []
|
|
73
|
+
let code: string[] | null = null
|
|
74
|
+
let language = 'code'
|
|
75
|
+
|
|
76
|
+
const flushText = () => {
|
|
77
|
+
if (text.length) blocks.push({ kind: 'text', value: text.join('\n') })
|
|
78
|
+
text = []
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
const opening = line.match(/^\s*```([\w+-]*)\s*$/u)
|
|
83
|
+
if (code === null && opening) {
|
|
84
|
+
flushText()
|
|
85
|
+
code = []
|
|
86
|
+
language = opening[1] || 'code'
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
if (code !== null && /^\s*```\s*$/u.test(line)) {
|
|
90
|
+
blocks.push({ kind: 'code', language, value: code.join('\n') })
|
|
91
|
+
code = null
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
if (code !== null) code.push(line)
|
|
95
|
+
else text.push(line)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (code !== null) blocks.push({ kind: 'code', language, value: code.join('\n') })
|
|
99
|
+
flushText()
|
|
100
|
+
return blocks
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function parseBlocks(value: string): MessageBlock[] {
|
|
104
|
+
const lines = value.split('\n')
|
|
105
|
+
const blocks: MessageBlock[] = []
|
|
106
|
+
let index = 0
|
|
107
|
+
|
|
108
|
+
while (index < lines.length) {
|
|
109
|
+
const line = lines[index]
|
|
110
|
+
if (!line.trim()) {
|
|
111
|
+
index += 1
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const fence = line.match(/^\s*```([\w+-]*)\s*$/u)
|
|
116
|
+
if (fence) {
|
|
117
|
+
const code: string[] = []
|
|
118
|
+
index += 1
|
|
119
|
+
while (index < lines.length && !/^\s*```\s*$/u.test(lines[index])) {
|
|
120
|
+
code.push(lines[index])
|
|
121
|
+
index += 1
|
|
122
|
+
}
|
|
123
|
+
if (index < lines.length) index += 1
|
|
124
|
+
blocks.push({ kind: 'code', language: fence[1] || 'code', value: code.join('\n') })
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const heading = line.match(/^\s*(#{1,3})\s+(.+)$/u)
|
|
129
|
+
if (heading) {
|
|
130
|
+
blocks.push({ kind: 'heading', level: heading[1].length, value: heading[2] })
|
|
131
|
+
index += 1
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const listItem = line.match(/^\s*(?:[-*+]\s+|\d+[.)]\s+)/u)
|
|
136
|
+
if (listItem) {
|
|
137
|
+
const ordered = /^\s*\d+[.)]/u.test(line)
|
|
138
|
+
const items: string[] = []
|
|
139
|
+
while (index < lines.length) {
|
|
140
|
+
const current = lines[index].match(/^\s*(?:[-*+]\s+|\d+[.)]\s+)(.+)$/u)
|
|
141
|
+
if (!current) break
|
|
142
|
+
items.push(current[1])
|
|
143
|
+
index += 1
|
|
144
|
+
}
|
|
145
|
+
blocks.push({ kind: 'list', ordered, items })
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const paragraph = [line]
|
|
150
|
+
index += 1
|
|
151
|
+
while (
|
|
152
|
+
index < lines.length &&
|
|
153
|
+
lines[index].trim() &&
|
|
154
|
+
!/^\s*```/u.test(lines[index]) &&
|
|
155
|
+
!/^\s*#{1,3}\s/u.test(lines[index]) &&
|
|
156
|
+
!/^\s*(?:[-*+]\s+|\d+[.)]\s+)/u.test(lines[index])
|
|
157
|
+
) {
|
|
158
|
+
paragraph.push(lines[index])
|
|
159
|
+
index += 1
|
|
160
|
+
}
|
|
161
|
+
blocks.push({ kind: 'paragraph', value: paragraph.join('\n') })
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return blocks
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default function MessageContent({
|
|
168
|
+
content,
|
|
169
|
+
streaming = false,
|
|
170
|
+
streamingDelta = '',
|
|
171
|
+
}: {
|
|
172
|
+
content: string
|
|
173
|
+
streaming?: boolean
|
|
174
|
+
streamingDelta?: string
|
|
175
|
+
}) {
|
|
176
|
+
const reduceMotion = useReducedMotion()
|
|
177
|
+
const blocks = useMemo(() => streaming ? [] : parseBlocks(content), [content, streaming])
|
|
178
|
+
const liveBlocks = useMemo(() => streaming ? parseLiveBlocks(content) : [], [content, streaming])
|
|
179
|
+
|
|
180
|
+
if (!content && streaming) {
|
|
181
|
+
return (
|
|
182
|
+
<span className="inline-flex items-center gap-1.5" aria-label="Writing response">
|
|
183
|
+
{[0, 1, 2].map((dot) => (
|
|
184
|
+
<span
|
|
185
|
+
key={dot}
|
|
186
|
+
aria-hidden="true"
|
|
187
|
+
className="bg-[var(--chat-accent,var(--foreground))]/70 size-1.5 animate-pulse rounded-full"
|
|
188
|
+
style={{ animationDelay: `${dot * 130}ms` }}
|
|
189
|
+
/>
|
|
190
|
+
))}
|
|
191
|
+
</span>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (streaming) {
|
|
196
|
+
return (
|
|
197
|
+
<div className="space-y-3 break-words text-sm leading-6">
|
|
198
|
+
{liveBlocks.map((block, index) => {
|
|
199
|
+
const isLast = index === liveBlocks.length - 1
|
|
200
|
+
if (block.kind === 'code') {
|
|
201
|
+
return (
|
|
202
|
+
<div key={`live-code-${index}`} className="border-border/70 bg-[#09090b] my-2 overflow-hidden rounded-xl border text-left">
|
|
203
|
+
<div className="border-neutral-800/80 bg-[#0d0d10]/95 flex items-center gap-2 border-b px-3.5 py-2">
|
|
204
|
+
<span className="text-neutral-500 font-mono text-[11px]">{block.language}</span>
|
|
205
|
+
{isLast ? <span className="bg-[var(--chat-accent)] ml-auto size-1.5 animate-pulse rounded-full" aria-label="Code is streaming" /> : null}
|
|
206
|
+
</div>
|
|
207
|
+
<pre className="max-w-full overflow-x-auto p-4 font-mono text-[13px] leading-6 text-neutral-100"><code>{block.value}</code></pre>
|
|
208
|
+
</div>
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
const animateDelta = isLast && !reduceMotion && streamingDelta.length > 0 && streamingDelta.length <= 240 && block.value.endsWith(streamingDelta)
|
|
212
|
+
const stableText = animateDelta ? block.value.slice(0, -streamingDelta.length) : block.value
|
|
213
|
+
return (
|
|
214
|
+
<p key={`live-text-${index}`} className="whitespace-pre-wrap">
|
|
215
|
+
{animateDelta ? stableText : null}
|
|
216
|
+
{animateDelta ? (
|
|
217
|
+
<motion.span
|
|
218
|
+
key={`${block.value.length}-${streamingDelta.length}`}
|
|
219
|
+
initial={{ opacity: 0.35, y: 1.5 }}
|
|
220
|
+
animate={{ opacity: 1, y: 0 }}
|
|
221
|
+
transition={{ duration: 0.11, ease: 'easeOut' }}
|
|
222
|
+
>
|
|
223
|
+
{streamingDelta}
|
|
224
|
+
</motion.span>
|
|
225
|
+
) : block.value}
|
|
226
|
+
{isLast ? <span aria-hidden="true" className="bg-[var(--chat-accent,var(--foreground))] ml-1 inline-block h-4 w-[2px] animate-pulse rounded-full align-middle" /> : null}
|
|
227
|
+
</p>
|
|
228
|
+
)
|
|
229
|
+
})}
|
|
230
|
+
{liveBlocks.length > 0 && liveBlocks[liveBlocks.length - 1].kind === 'code' ? (
|
|
231
|
+
<span aria-hidden="true" className="bg-[var(--chat-accent,var(--foreground))] inline-block h-4 w-[2px] animate-pulse rounded-full align-middle" />
|
|
232
|
+
) : null}
|
|
233
|
+
</div>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return (
|
|
238
|
+
<div className="space-y-3 text-sm leading-6">
|
|
239
|
+
{blocks.map((block, index) => {
|
|
240
|
+
if (block.kind === 'code') {
|
|
241
|
+
return <ChatCodeBlock key={`code-${index}`} label={block.language} className="my-0">{block.value}</ChatCodeBlock>
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (block.kind === 'heading') {
|
|
245
|
+
const Heading = `h${block.level}` as 'h1' | 'h2' | 'h3'
|
|
246
|
+
return (
|
|
247
|
+
<Heading key={`heading-${index}`} className="font-display font-semibold tracking-tight">
|
|
248
|
+
{inlineContent(block.value)}
|
|
249
|
+
</Heading>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (block.kind === 'list') {
|
|
254
|
+
const List = block.ordered ? 'ol' : 'ul'
|
|
255
|
+
return (
|
|
256
|
+
<List
|
|
257
|
+
key={`list-${index}`}
|
|
258
|
+
className={block.ordered ? 'list-decimal space-y-1 pl-5' : 'list-disc space-y-1 pl-5'}
|
|
259
|
+
>
|
|
260
|
+
{block.items.map((item, itemIndex) => (
|
|
261
|
+
<li key={itemIndex}>{inlineContent(item)}</li>
|
|
262
|
+
))}
|
|
263
|
+
</List>
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<p key={`paragraph-${index}`} className="whitespace-pre-wrap">
|
|
269
|
+
{inlineContent(block.value)}
|
|
270
|
+
{streaming && index === blocks.length - 1 ? (
|
|
271
|
+
<span
|
|
272
|
+
aria-hidden="true"
|
|
273
|
+
className="bg-[var(--chat-accent,var(--foreground))] ml-1 inline-block h-4 w-[2px] translate-y-[3px] animate-pulse rounded-full"
|
|
274
|
+
/>
|
|
275
|
+
) : null}
|
|
276
|
+
</p>
|
|
277
|
+
)
|
|
278
|
+
})}
|
|
279
|
+
{streaming && blocks.length > 0 && blocks[blocks.length - 1].kind !== 'paragraph' ? (
|
|
280
|
+
<span
|
|
281
|
+
aria-hidden="true"
|
|
282
|
+
className="bg-[var(--chat-accent,var(--foreground))] inline-block h-4 w-[2px] animate-pulse rounded-full"
|
|
283
|
+
/>
|
|
284
|
+
) : null}
|
|
285
|
+
</div>
|
|
286
|
+
)
|
|
287
|
+
}
|