@istmx/ui 0.1.9 → 0.1.11
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 +64 -0
- package/bin/istmx.mjs +50 -11
- package/package.json +1 -1
- package/registry/ai-chat-block/ai-chat-block.tsx +460 -0
- package/registry/ai-chat-block/ai-chat-input.tsx +694 -0
- package/registry/ai-chat-block/attachment.tsx +466 -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 +30 -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/bento-grid.tsx +433 -0
- package/registry/scroll-parallax-hero.tsx +565 -0
- package/registry/streaming-text.tsx +278 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef } from 'react'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
import { cn } from './utils'
|
|
6
|
+
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
|
|
7
|
+
import type { ChatTurn } from './types'
|
|
8
|
+
import ChatMessage from './chat-message'
|
|
9
|
+
import ChatLogo from './chat-logo'
|
|
10
|
+
|
|
11
|
+
export type MessageListProps = {
|
|
12
|
+
messages: readonly ChatTurn[]
|
|
13
|
+
userName: string
|
|
14
|
+
assistantName: string
|
|
15
|
+
userAvatar?: ReactNode
|
|
16
|
+
assistantAvatar?: ReactNode
|
|
17
|
+
onRetry?: (turnId: string) => void
|
|
18
|
+
retryDisabled?: boolean
|
|
19
|
+
emptyMessage?: string
|
|
20
|
+
className?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function MessageList({
|
|
24
|
+
messages,
|
|
25
|
+
userName,
|
|
26
|
+
assistantName,
|
|
27
|
+
userAvatar,
|
|
28
|
+
assistantAvatar,
|
|
29
|
+
onRetry,
|
|
30
|
+
retryDisabled,
|
|
31
|
+
emptyMessage = 'Start a conversation. Your messages will appear here.',
|
|
32
|
+
className,
|
|
33
|
+
}: MessageListProps) {
|
|
34
|
+
const logRef = useRef<HTMLDivElement>(null)
|
|
35
|
+
const wasAtBottom = useRef(true)
|
|
36
|
+
const previousCount = useRef(messages.length)
|
|
37
|
+
const reduceMotion = useReducedMotion()
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const log = logRef.current
|
|
41
|
+
if (!log) return
|
|
42
|
+
const newMessageAdded = messages.length > previousCount.current
|
|
43
|
+
previousCount.current = messages.length
|
|
44
|
+
if (wasAtBottom.current || newMessageAdded) log.scrollTop = log.scrollHeight
|
|
45
|
+
}, [messages])
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<div
|
|
49
|
+
role="log"
|
|
50
|
+
ref={logRef}
|
|
51
|
+
onScroll={(event) => {
|
|
52
|
+
const log = event.currentTarget
|
|
53
|
+
wasAtBottom.current = log.scrollHeight - log.scrollTop - log.clientHeight < 48
|
|
54
|
+
}}
|
|
55
|
+
aria-label="Chat conversation"
|
|
56
|
+
aria-relevant="additions text"
|
|
57
|
+
className={cn('[scrollbar-width:none] [&::-webkit-scrollbar]:hidden min-h-0 flex-1 overflow-y-auto overscroll-contain px-3 py-4 sm:px-5 sm:py-5', className)}
|
|
58
|
+
>
|
|
59
|
+
{messages.length ? (
|
|
60
|
+
<div className="space-y-5 sm:space-y-6">
|
|
61
|
+
<AnimatePresence initial={false}>
|
|
62
|
+
{messages.map((turn) => (
|
|
63
|
+
<ChatMessage
|
|
64
|
+
key={turn.id}
|
|
65
|
+
turn={turn}
|
|
66
|
+
userName={userName}
|
|
67
|
+
assistantName={assistantName}
|
|
68
|
+
userAvatar={userAvatar}
|
|
69
|
+
assistantAvatar={assistantAvatar}
|
|
70
|
+
onRetry={turn.status === 'error' && onRetry ? () => onRetry(turn.id) : undefined}
|
|
71
|
+
retryDisabled={retryDisabled}
|
|
72
|
+
/>
|
|
73
|
+
))}
|
|
74
|
+
</AnimatePresence>
|
|
75
|
+
<div aria-hidden="true" />
|
|
76
|
+
</div>
|
|
77
|
+
) : (
|
|
78
|
+
<motion.div
|
|
79
|
+
initial={{ opacity: 0 }}
|
|
80
|
+
animate={{ opacity: 1 }}
|
|
81
|
+
transition={{ duration: reduceMotion ? 0 : 0.3 }}
|
|
82
|
+
className="flex min-h-48 flex-col items-center justify-center px-4 text-center sm:min-h-56"
|
|
83
|
+
>
|
|
84
|
+
<span className="bg-[var(--chat-accent,var(--foreground))]/10 text-[var(--chat-accent,var(--foreground))] mb-3 flex size-10 items-center justify-center rounded-2xl">
|
|
85
|
+
<ChatLogo className="size-5" />
|
|
86
|
+
</span>
|
|
87
|
+
<p className="font-display text-sm font-medium">Start a conversation</p>
|
|
88
|
+
<p className="text-muted mt-1 max-w-sm text-xs leading-5">{emptyMessage}</p>
|
|
89
|
+
</motion.div>
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
)
|
|
93
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AnimatePresence,
|
|
5
|
+
motion,
|
|
6
|
+
useAnimationControls,
|
|
7
|
+
useReducedMotion,
|
|
8
|
+
} from 'motion/react'
|
|
9
|
+
import { IconArrowUp, IconCheck, IconLoader, IconPlayerStop } from '@tabler/icons-react'
|
|
10
|
+
|
|
11
|
+
export type SendButtonProps = {
|
|
12
|
+
state: 'idle' | 'ready' | 'sending' | 'sent'
|
|
13
|
+
disabled?: boolean
|
|
14
|
+
label?: string
|
|
15
|
+
shakeControls?: ReturnType<typeof useAnimationControls>
|
|
16
|
+
onClick: () => void
|
|
17
|
+
onStop?: () => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default function SendButton({
|
|
21
|
+
state,
|
|
22
|
+
disabled = false,
|
|
23
|
+
label = 'Send message',
|
|
24
|
+
shakeControls,
|
|
25
|
+
onClick,
|
|
26
|
+
onStop,
|
|
27
|
+
}: SendButtonProps) {
|
|
28
|
+
const reduceMotion = useReducedMotion()
|
|
29
|
+
const active = state === 'ready' || state === 'sent'
|
|
30
|
+
const canStop = state === 'sending' && Boolean(onStop)
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<motion.button
|
|
34
|
+
type="button"
|
|
35
|
+
onClick={canStop ? onStop : onClick}
|
|
36
|
+
aria-label={canStop ? 'Stop generating response' : state === 'sending' ? 'Sending message' : label}
|
|
37
|
+
title={canStop ? 'Stop generating' : label}
|
|
38
|
+
aria-disabled={disabled || (!active && !canStop)}
|
|
39
|
+
disabled={disabled || (!active && !canStop)}
|
|
40
|
+
animate={shakeControls}
|
|
41
|
+
whileTap={reduceMotion || (!active && !canStop) ? undefined : { scale: 0.9 }}
|
|
42
|
+
whileHover={reduceMotion || state !== 'ready' ? undefined : { y: -1 }}
|
|
43
|
+
className={`focus-visible:outline-foreground relative flex size-8 shrink-0 items-center justify-center rounded-xl border transition-[color,background-color,border-color,box-shadow] duration-200 focus-visible:outline-2 focus-visible:outline-offset-2 disabled:cursor-not-allowed disabled:opacity-60 sm:size-9 ${
|
|
44
|
+
canStop
|
|
45
|
+
? 'border-[var(--chat-accent,var(--foreground))] bg-[var(--chat-accent,var(--foreground))] text-white hover:opacity-85'
|
|
46
|
+
: active
|
|
47
|
+
? 'border-[var(--chat-accent,var(--foreground))] bg-[var(--chat-accent,var(--foreground))] text-white hover:shadow-[0_10px_26px_-12px_var(--chat-accent,var(--foreground))]'
|
|
48
|
+
: 'border-border/70 bg-surface/40 text-muted/60'
|
|
49
|
+
}`}
|
|
50
|
+
>
|
|
51
|
+
<AnimatePresence mode="wait" initial={false}>
|
|
52
|
+
{state === 'sending' && canStop ? (
|
|
53
|
+
<motion.span
|
|
54
|
+
key="stop"
|
|
55
|
+
initial={{ opacity: 0, scale: 0.7 }}
|
|
56
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
57
|
+
exit={{ opacity: 0, scale: 0.7 }}
|
|
58
|
+
className="flex items-center justify-center"
|
|
59
|
+
>
|
|
60
|
+
<IconPlayerStop size={13} fill="currentColor" aria-hidden="true" />
|
|
61
|
+
</motion.span>
|
|
62
|
+
) : state === 'sending' ? (
|
|
63
|
+
<motion.span
|
|
64
|
+
key="sending"
|
|
65
|
+
initial={{ opacity: 0, scale: 0.7 }}
|
|
66
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
67
|
+
exit={{ opacity: 0, scale: 0.7 }}
|
|
68
|
+
className="flex items-center justify-center"
|
|
69
|
+
>
|
|
70
|
+
<IconLoader
|
|
71
|
+
size={16}
|
|
72
|
+
stroke={1.8}
|
|
73
|
+
aria-hidden="true"
|
|
74
|
+
className="motion-safe:animate-spin"
|
|
75
|
+
/>
|
|
76
|
+
</motion.span>
|
|
77
|
+
) : state === 'sent' ? (
|
|
78
|
+
<motion.span
|
|
79
|
+
key="sent"
|
|
80
|
+
initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}
|
|
81
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
82
|
+
exit={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}
|
|
83
|
+
transition={reduceMotion ? { duration: 0 } : { type: 'spring', stiffness: 520, damping: 24 }}
|
|
84
|
+
className="flex items-center justify-center"
|
|
85
|
+
>
|
|
86
|
+
<IconCheck size={17} stroke={2} aria-hidden="true" />
|
|
87
|
+
</motion.span>
|
|
88
|
+
) : (
|
|
89
|
+
<motion.span
|
|
90
|
+
key="idle"
|
|
91
|
+
initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 5 }}
|
|
92
|
+
animate={{ opacity: 1, y: 0 }}
|
|
93
|
+
exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -5 }}
|
|
94
|
+
className="flex items-center justify-center"
|
|
95
|
+
>
|
|
96
|
+
<IconArrowUp size={17} stroke={1.9} aria-hidden="true" />
|
|
97
|
+
</motion.span>
|
|
98
|
+
)}
|
|
99
|
+
</AnimatePresence>
|
|
100
|
+
</motion.button>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
|
|
3
|
+
export type ChatAttachmentKind = 'image' | 'video' | 'audio' | 'file'
|
|
4
|
+
|
|
5
|
+
export type ChatAttachment = {
|
|
6
|
+
id: string
|
|
7
|
+
name: string
|
|
8
|
+
kind: ChatAttachmentKind
|
|
9
|
+
size: number
|
|
10
|
+
file: File
|
|
11
|
+
url?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type AttachmentOption = {
|
|
15
|
+
id: string
|
|
16
|
+
label: string
|
|
17
|
+
description?: string
|
|
18
|
+
icon?: ReactNode
|
|
19
|
+
accept: string
|
|
20
|
+
multiple?: boolean
|
|
21
|
+
capture?: 'user' | 'environment'
|
|
22
|
+
disabled?: boolean
|
|
23
|
+
onSelect?: () => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ChatTurn = {
|
|
27
|
+
id: string
|
|
28
|
+
role: 'user' | 'assistant'
|
|
29
|
+
content: string
|
|
30
|
+
/** Text added in the latest throttled stream update, for a small reveal. */
|
|
31
|
+
streamingDelta?: string
|
|
32
|
+
attachments?: ChatAttachment[]
|
|
33
|
+
status?: 'streaming' | 'complete' | 'error'
|
|
34
|
+
error?: {
|
|
35
|
+
message: string
|
|
36
|
+
retryAfterMs?: number
|
|
37
|
+
retryAt?: number
|
|
38
|
+
}
|
|
39
|
+
thinking?: string
|
|
40
|
+
thinkingStatus?: 'thinking' | 'complete'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ChatThread = {
|
|
44
|
+
id: string
|
|
45
|
+
title: string
|
|
46
|
+
messages: readonly ChatTurn[]
|
|
47
|
+
pinned?: boolean
|
|
48
|
+
archived?: boolean
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type ChatRequest = {
|
|
52
|
+
message: string
|
|
53
|
+
attachments: readonly ChatAttachment[]
|
|
54
|
+
signal: AbortSignal
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ChatStreamChunk =
|
|
58
|
+
| string
|
|
59
|
+
| {
|
|
60
|
+
content?: unknown
|
|
61
|
+
text?: unknown
|
|
62
|
+
delta?: unknown
|
|
63
|
+
type?: 'text-delta' | 'thinking-delta' | 'reasoning-delta'
|
|
64
|
+
}
|
|
65
|
+
| readonly unknown[]
|
|
66
|
+
|
|
67
|
+
export type ChatResponse = string | AsyncIterable<ChatStreamChunk>
|
|
68
|
+
|
|
69
|
+
export type ChatSendError = Error & {
|
|
70
|
+
retryAfterMs?: number
|
|
71
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
|
|
4
|
+
import { IconMicrophone, IconPlayerStop } from '@tabler/icons-react'
|
|
5
|
+
|
|
6
|
+
export type VoiceButtonProps = {
|
|
7
|
+
listening: boolean
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
onClick: () => void
|
|
10
|
+
startLabel?: string
|
|
11
|
+
stopLabel?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default function VoiceButton({
|
|
15
|
+
listening,
|
|
16
|
+
disabled = false,
|
|
17
|
+
onClick,
|
|
18
|
+
startLabel = 'Start voice input',
|
|
19
|
+
stopLabel = 'Stop voice input',
|
|
20
|
+
}: VoiceButtonProps) {
|
|
21
|
+
const reduceMotion = useReducedMotion()
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<motion.button
|
|
25
|
+
type="button"
|
|
26
|
+
onClick={onClick}
|
|
27
|
+
aria-pressed={listening}
|
|
28
|
+
aria-label={listening ? stopLabel : startLabel}
|
|
29
|
+
title={listening ? stopLabel : startLabel}
|
|
30
|
+
disabled={disabled}
|
|
31
|
+
whileTap={reduceMotion ? undefined : { scale: 0.9 }}
|
|
32
|
+
whileHover={reduceMotion ? undefined : { y: -1 }}
|
|
33
|
+
className={`focus-visible:outline-foreground relative flex size-8 shrink-0 cursor-pointer items-center justify-center overflow-visible rounded-xl border transition-colors duration-200 focus-visible:outline-2 focus-visible:outline-offset-2 disabled:cursor-not-allowed disabled:opacity-50 sm:size-9 ${
|
|
34
|
+
listening
|
|
35
|
+
? 'border-[var(--chat-accent,var(--foreground))] bg-[var(--chat-accent,var(--foreground))] text-white'
|
|
36
|
+
: 'border-border/70 bg-surface/40 text-muted hover:border-foreground/40 hover:text-foreground'
|
|
37
|
+
}`}
|
|
38
|
+
>
|
|
39
|
+
{listening && !reduceMotion ? (
|
|
40
|
+
<motion.span
|
|
41
|
+
aria-hidden="true"
|
|
42
|
+
className="border-[var(--chat-accent,var(--foreground))] pointer-events-none absolute inset-0 rounded-[inherit] border"
|
|
43
|
+
initial={{ opacity: 0.5, scale: 1 }}
|
|
44
|
+
animate={{ opacity: 0, scale: 1.55 }}
|
|
45
|
+
transition={{ repeat: Infinity, duration: 1.5, ease: 'easeOut' }}
|
|
46
|
+
/>
|
|
47
|
+
) : null}
|
|
48
|
+
<AnimatePresence mode="wait" initial={false}>
|
|
49
|
+
<motion.span
|
|
50
|
+
key={listening ? 'on' : 'off'}
|
|
51
|
+
initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.6, rotate: -35 }}
|
|
52
|
+
animate={{ opacity: 1, scale: 1, rotate: 0 }}
|
|
53
|
+
exit={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.6, rotate: 35 }}
|
|
54
|
+
transition={{ duration: reduceMotion ? 0 : 0.16 }}
|
|
55
|
+
className="relative flex items-center justify-center"
|
|
56
|
+
>
|
|
57
|
+
{listening ? (
|
|
58
|
+
<IconPlayerStop size={13} stroke={1.6} aria-hidden="true" />
|
|
59
|
+
) : (
|
|
60
|
+
<IconMicrophone size={17} stroke={1.7} aria-hidden="true" />
|
|
61
|
+
)}
|
|
62
|
+
</motion.span>
|
|
63
|
+
</AnimatePresence>
|
|
64
|
+
</motion.button>
|
|
65
|
+
)
|
|
66
|
+
}
|