@istmx/ui 0.1.11 → 0.1.13
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 -21
- package/README.md +533 -463
- package/bin/istmx.mjs +481 -314
- package/package.json +27 -27
- package/registry/accordion.tsx +186 -0
- package/registry/ai-chat-block/ai-chat-block.tsx +456 -460
- package/registry/ai-chat-block/ai-chat-input.tsx +694 -694
- package/registry/ai-chat-block/attachment.tsx +466 -466
- package/registry/ai-chat-block/chat-code-block.tsx +50 -50
- package/registry/ai-chat-block/chat-composer.tsx +51 -51
- package/registry/ai-chat-block/chat-logo.tsx +18 -18
- package/registry/ai-chat-block/chat-message.tsx +161 -161
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -273
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -9
- package/registry/ai-chat-block/code-highlighter.tsx +342 -357
- package/registry/ai-chat-block/default-attachments.tsx +42 -42
- package/registry/ai-chat-block/index.ts +30 -30
- package/registry/ai-chat-block/message-content.tsx +287 -287
- package/registry/ai-chat-block/message-list.tsx +93 -93
- package/registry/ai-chat-block/send-button.tsx +102 -102
- package/registry/ai-chat-block/types.ts +70 -71
- package/registry/ai-chat-block/utils.ts +6 -6
- package/registry/ai-chat-block/voice-button.tsx +66 -66
- package/registry/alert-dialog.tsx +229 -0
- package/registry/alert.tsx +188 -0
- package/registry/animated-text.tsx +228 -225
- package/registry/aspect-ratio.tsx +32 -0
- package/registry/avatar-group.tsx +66 -0
- package/registry/avatar.tsx +121 -0
- package/registry/badge.tsx +52 -0
- package/registry/bento-grid.tsx +438 -433
- package/registry/button.tsx +156 -237
- package/registry/carousel.tsx +184 -0
- package/registry/dock-navigation.tsx +200 -198
- package/registry/drift-canvas.tsx +383 -0
- package/registry/drift-wall.tsx +361 -0
- package/registry/flex-carousel.tsx +1430 -0
- package/registry/guided-gallery.tsx +1506 -0
- package/registry/image-accordion.tsx +306 -302
- package/registry/image-trail.tsx +126 -121
- package/registry/infinite-image-canvas.tsx +268 -263
- package/registry/input-otp.tsx +332 -0
- package/registry/list-card.tsx +130 -0
- package/registry/mobile-menu-dock.tsx +216 -211
- package/registry/morph-slider.tsx +756 -0
- package/registry/pixel-cat.tsx +972 -1020
- package/registry/scroll-canvas-cards.tsx +643 -0
- package/registry/scroll-parallax-hero.tsx +566 -565
- package/registry/scroll-story-cards.tsx +443 -438
- package/registry/slider.tsx +163 -0
- package/registry/spotlight-grid.tsx +226 -0
- package/registry/streaming-text.tsx +269 -278
- package/registry/text-reveal.tsx +311 -307
- package/registry/user-avatar.tsx +99 -0
- package/registry/utils.ts +6 -6
|
@@ -1,287 +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
|
-
}
|
|
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
|
+
}
|