@istmx/ui 0.1.9 → 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.
@@ -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
+
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,6 @@
1
+ import { clsx, type ClassValue } from 'clsx'
2
+ import { twMerge } from 'tailwind-merge'
3
+
4
+ export function cn(...inputs: ClassValue[]) {
5
+ return twMerge(clsx(inputs))
6
+ }
@@ -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
+ }
@@ -0,0 +1,278 @@
1
+ 'use client'
2
+
3
+ import { useEffect, useMemo, useRef, useState } from 'react'
4
+ import type { ReactNode } from 'react'
5
+ import { motion, useReducedMotion } from 'motion/react'
6
+ import { cn } from '@/lib/utils'
7
+
8
+ export type StreamingTextEffect = 'fade' | 'blur' | 'slide' | 'wave'
9
+
10
+ export type StreamingTextProps = {
11
+ /** The string to stream, as if the model is still writing it. */
12
+ text: string
13
+ /** Characters revealed per second. Defaults to 34. */
14
+ speed?: number
15
+ /** Delay in milliseconds before streaming starts. Defaults to 0. */
16
+ startDelay?: number
17
+ /** How each new character enters. Defaults to blur. */
18
+ effect?: StreamingTextEffect
19
+ /** Time in seconds for a single character to settle. Defaults to 0.34. */
20
+ glyphDuration?: number
21
+ /** Blur radius used by the blur effect. Defaults to 6. */
22
+ blurAmount?: number
23
+ /** Rise distance used by the slide, blur, and wave effects. Defaults to 6. */
24
+ slideDistance?: number
25
+ /** Show the blinking caret while streaming. Defaults to true. */
26
+ cursor?: boolean
27
+ /** Keep the caret blinking after the string finishes. Defaults to false. */
28
+ cursorAfterComplete?: boolean
29
+ /** Restart the stream after the string finishes. Defaults to false. */
30
+ loop?: boolean
31
+ /** Pause in milliseconds between loop cycles. Defaults to 1200. */
32
+ loopDelay?: number
33
+ /** Called once the full string has been revealed. */
34
+ onComplete?: () => void
35
+ className?: string
36
+ textClassName?: string
37
+ cursorClassName?: string
38
+ }
39
+
40
+ const WHITESPACE = /^\s$/u
41
+
42
+ function splitGlyphs(value: string): string[] {
43
+ if (typeof Intl !== 'undefined' && 'Segmenter' in Intl) {
44
+ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
45
+ return Array.from(segmenter.segment(value), (part) => part.segment)
46
+ }
47
+
48
+ return Array.from(value)
49
+ }
50
+
51
+ type GlyphGroup = {
52
+ start: number
53
+ space: boolean
54
+ glyphs: string[]
55
+ }
56
+
57
+ function groupGlyphs(glyphs: string[]): GlyphGroup[] {
58
+ const groups: GlyphGroup[] = []
59
+
60
+ glyphs.forEach((glyph, index) => {
61
+ const space = WHITESPACE.test(glyph)
62
+ const last = groups[groups.length - 1]
63
+
64
+ if (last && last.space === space) {
65
+ last.glyphs.push(glyph)
66
+ return
67
+ }
68
+
69
+ groups.push({ start: index, space, glyphs: [glyph] })
70
+ })
71
+
72
+ return groups
73
+ }
74
+
75
+ function glyphStates(
76
+ effect: StreamingTextEffect,
77
+ blurAmount: number,
78
+ slideDistance: number,
79
+ ) {
80
+ switch (effect) {
81
+ case 'fade':
82
+ return { initial: { opacity: 0 }, animate: { opacity: 1 } }
83
+ case 'slide':
84
+ return {
85
+ initial: { opacity: 0, y: slideDistance },
86
+ animate: { opacity: 1, y: 0 },
87
+ }
88
+ case 'wave':
89
+ return {
90
+ initial: { opacity: 0, y: slideDistance, scale: 0.94 },
91
+ animate: { opacity: 1, y: 0, scale: 1 },
92
+ }
93
+ case 'blur':
94
+ default:
95
+ return {
96
+ initial: {
97
+ opacity: 0,
98
+ y: slideDistance * 0.5,
99
+ filter: `blur(${blurAmount}px)`,
100
+ },
101
+ animate: { opacity: 1, y: 0, filter: 'blur(0px)' },
102
+ }
103
+ }
104
+ }
105
+
106
+ export function StreamingText({
107
+ text,
108
+ speed = 34,
109
+ startDelay = 0,
110
+ effect = 'blur',
111
+ glyphDuration = 0.34,
112
+ blurAmount = 6,
113
+ slideDistance = 6,
114
+ cursor = true,
115
+ cursorAfterComplete = false,
116
+ loop = false,
117
+ loopDelay = 1200,
118
+ onComplete,
119
+ className,
120
+ textClassName,
121
+ cursorClassName,
122
+ }: StreamingTextProps) {
123
+ const shouldReduceMotion = useReducedMotion()
124
+ const glyphs = useMemo(() => splitGlyphs(text), [text])
125
+ const [revealed, setRevealed] = useState(0)
126
+ const [cycle, setCycle] = useState(0)
127
+ const onCompleteRef = useRef(onComplete)
128
+
129
+ useEffect(() => {
130
+ onCompleteRef.current = onComplete
131
+ })
132
+
133
+ useEffect(() => {
134
+ let frame = 0
135
+ let timeout = 0
136
+
137
+ const cancel = () => {
138
+ window.cancelAnimationFrame(frame)
139
+ window.clearTimeout(timeout)
140
+ }
141
+
142
+ if (shouldReduceMotion) {
143
+ queueMicrotask(() => {
144
+ setRevealed(glyphs.length)
145
+ })
146
+ return cancel
147
+ }
148
+
149
+ queueMicrotask(() => {
150
+ setRevealed(0)
151
+ })
152
+
153
+ let startedAt: number | null = null
154
+ const perGlyph = 1000 / Math.max(1, speed)
155
+ const delay = Math.max(0, startDelay)
156
+ const pause = Math.max(0, loopDelay)
157
+
158
+ const step = (now: number) => {
159
+ if (startedAt === null) startedAt = now
160
+
161
+ const elapsed = now - startedAt - delay
162
+
163
+ if (elapsed < 0) {
164
+ frame = window.requestAnimationFrame(step)
165
+ return
166
+ }
167
+
168
+ const count = Math.min(glyphs.length, Math.floor(elapsed / perGlyph) + 1)
169
+ setRevealed(count)
170
+
171
+ if (count < glyphs.length) {
172
+ frame = window.requestAnimationFrame(step)
173
+ return
174
+ }
175
+
176
+ onCompleteRef.current?.()
177
+
178
+ if (loop) {
179
+ timeout = window.setTimeout(() => {
180
+ setCycle((value) => value + 1)
181
+ }, pause)
182
+ }
183
+ }
184
+
185
+ frame = window.requestAnimationFrame(step)
186
+
187
+ return cancel
188
+ }, [cycle, glyphs, loop, loopDelay, shouldReduceMotion, speed, startDelay])
189
+
190
+ const visibleGlyphs = useMemo(
191
+ () => glyphs.slice(0, Math.max(0, revealed)),
192
+ [glyphs, revealed],
193
+ )
194
+ const groups = useMemo(() => groupGlyphs(visibleGlyphs), [visibleGlyphs])
195
+ const streaming = revealed < glyphs.length
196
+ const showCursor = cursor && (streaming || cursorAfterComplete)
197
+ const states = glyphStates(effect, blurAmount, slideDistance)
198
+
199
+ const content: ReactNode[] = groups.map((group) => {
200
+ if (group.space) {
201
+ return (
202
+ <span key={`space-${group.start}`} className="whitespace-pre">
203
+ {group.glyphs.join('')}
204
+ </span>
205
+ )
206
+ }
207
+
208
+ return (
209
+ <span
210
+ key={`word-${group.start}`}
211
+ className="inline-block whitespace-nowrap"
212
+ >
213
+ {group.glyphs.map((glyph, offset) => {
214
+ const index = group.start + offset
215
+
216
+ return (
217
+ <motion.span
218
+ key={index}
219
+ initial={shouldReduceMotion ? false : states.initial}
220
+ animate={states.animate}
221
+ transition={
222
+ shouldReduceMotion
223
+ ? { duration: 0 }
224
+ : effect === 'wave'
225
+ ? { type: 'spring', stiffness: 360, damping: 24, mass: 0.6 }
226
+ : {
227
+ duration: Math.max(0.05, glyphDuration),
228
+ ease: 'easeOut',
229
+ }
230
+ }
231
+ className="inline-block"
232
+ >
233
+ {glyph}
234
+ </motion.span>
235
+ )
236
+ })}
237
+ </span>
238
+ )
239
+ })
240
+
241
+ return (
242
+ <span
243
+ role="status"
244
+ aria-live="polite"
245
+ aria-atomic="true"
246
+ data-state={streaming ? 'streaming' : 'complete'}
247
+ className={cn('relative inline', className)}
248
+ >
249
+ <span
250
+ aria-hidden="true"
251
+ className={cn('whitespace-pre-wrap', textClassName)}
252
+ >
253
+ {content}
254
+ {showCursor ? (
255
+ <motion.span
256
+ aria-hidden="true"
257
+ initial={{ opacity: 1 }}
258
+ animate={
259
+ shouldReduceMotion ? { opacity: 1 } : { opacity: [1, 0.15, 1] }
260
+ }
261
+ transition={
262
+ shouldReduceMotion
263
+ ? { duration: 0 }
264
+ : { duration: 1, ease: 'easeInOut', repeat: Infinity }
265
+ }
266
+ className={cn(
267
+ 'bg-foreground ml-0.5 inline-block h-[0.95em] w-[2px] translate-y-[0.1em] rounded-full align-middle',
268
+ cursorClassName,
269
+ )}
270
+ />
271
+ ) : null}
272
+ </span>
273
+ <span className="sr-only">{text}</span>
274
+ </span>
275
+ )
276
+ }
277
+
278
+ export default StreamingText