@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,460 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useRef, useState } from 'react'
|
|
4
|
+
import type { CSSProperties, ReactNode } from 'react'
|
|
5
|
+
import { IconLayoutSidebarLeftCollapse, IconLayoutSidebarLeftExpand } from '@tabler/icons-react'
|
|
6
|
+
import { cn } from './utils'
|
|
7
|
+
import ChatComposer from './chat-composer'
|
|
8
|
+
import ChatSidebar from './chat-sidebar'
|
|
9
|
+
import ChatUserAvatar from './chat-user-avatar'
|
|
10
|
+
import ChatLogo from './chat-logo'
|
|
11
|
+
import { DEFAULT_ATTACHMENT_OPTIONS } from './default-attachments'
|
|
12
|
+
import MessageList from './message-list'
|
|
13
|
+
import type {
|
|
14
|
+
AttachmentOption,
|
|
15
|
+
ChatAttachment,
|
|
16
|
+
ChatRequest,
|
|
17
|
+
ChatResponse,
|
|
18
|
+
ChatSendError,
|
|
19
|
+
ChatStreamChunk,
|
|
20
|
+
ChatThread,
|
|
21
|
+
ChatTurn,
|
|
22
|
+
} from './types'
|
|
23
|
+
|
|
24
|
+
export type AiChatBlockProps = {
|
|
25
|
+
onSend: (request: ChatRequest) => Promise<ChatResponse>
|
|
26
|
+
initialMessages?: readonly ChatTurn[]
|
|
27
|
+
initialThreads?: readonly ChatThread[]
|
|
28
|
+
title?: string
|
|
29
|
+
description?: string
|
|
30
|
+
assistantName?: string
|
|
31
|
+
userName?: string
|
|
32
|
+
assistantAvatar?: ReactNode
|
|
33
|
+
userAvatar?: ReactNode
|
|
34
|
+
attachmentOptions?: readonly AttachmentOption[]
|
|
35
|
+
maxAttachments?: number
|
|
36
|
+
maxFileSizeBytes?: number
|
|
37
|
+
/** Maximum streamed response size in characters; defaults to 60,000. */
|
|
38
|
+
maxResponseCharacters?: number
|
|
39
|
+
accentColor?: string
|
|
40
|
+
allowVoiceInput?: boolean
|
|
41
|
+
placeholder?: string
|
|
42
|
+
emptyMessage?: string
|
|
43
|
+
className?: string
|
|
44
|
+
fullScreen?: boolean
|
|
45
|
+
onListeningChange?: (listening: boolean) => void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type RetryRequest = {
|
|
49
|
+
message: string
|
|
50
|
+
attachments: ChatAttachment[]
|
|
51
|
+
userTurnId: string
|
|
52
|
+
assistantTurnId: string
|
|
53
|
+
threadId: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type StreamParts = { text: string; thinking: string }
|
|
57
|
+
type ChatState = { threads: ChatThread[]; activeThreadId: string }
|
|
58
|
+
const STREAM_RENDER_INTERVAL_MS = 32
|
|
59
|
+
|
|
60
|
+
function createId() {
|
|
61
|
+
return globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random()}`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isAsyncIterable(value: unknown): value is AsyncIterable<ChatStreamChunk> {
|
|
65
|
+
return Boolean(
|
|
66
|
+
value && typeof value === 'object' && Symbol.asyncIterator in value &&
|
|
67
|
+
typeof (value as AsyncIterable<ChatStreamChunk>)[Symbol.asyncIterator] === 'function',
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function readParts(chunk: unknown): StreamParts {
|
|
72
|
+
if (typeof chunk === 'string') return { text: chunk, thinking: '' }
|
|
73
|
+
if (Array.isArray(chunk)) {
|
|
74
|
+
return chunk.reduce<StreamParts>((parts, item) => {
|
|
75
|
+
const next = readParts(item)
|
|
76
|
+
return { text: parts.text + next.text, thinking: parts.thinking + next.thinking }
|
|
77
|
+
}, { text: '', thinking: '' })
|
|
78
|
+
}
|
|
79
|
+
if (!chunk || typeof chunk !== 'object') return { text: '', thinking: '' }
|
|
80
|
+
const value = chunk as { content?: unknown; text?: unknown; delta?: unknown; type?: string }
|
|
81
|
+
const part = value.delta ?? value.content ?? value.text
|
|
82
|
+
const content = readParts(part)
|
|
83
|
+
return value.type === 'thinking-delta' || value.type === 'reasoning-delta'
|
|
84
|
+
? { text: '', thinking: content.text + content.thinking }
|
|
85
|
+
: content
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default function AiChatBlock({
|
|
89
|
+
onSend,
|
|
90
|
+
initialMessages = [],
|
|
91
|
+
initialThreads,
|
|
92
|
+
title = 'AI chat',
|
|
93
|
+
assistantName = 'Assistant',
|
|
94
|
+
userName = 'You',
|
|
95
|
+
assistantAvatar,
|
|
96
|
+
userAvatar,
|
|
97
|
+
attachmentOptions = DEFAULT_ATTACHMENT_OPTIONS,
|
|
98
|
+
maxAttachments = 10,
|
|
99
|
+
maxFileSizeBytes,
|
|
100
|
+
maxResponseCharacters = 60_000,
|
|
101
|
+
accentColor = '#0369a1',
|
|
102
|
+
allowVoiceInput = true,
|
|
103
|
+
placeholder = 'Message the assistant…',
|
|
104
|
+
emptyMessage,
|
|
105
|
+
className,
|
|
106
|
+
fullScreen = false,
|
|
107
|
+
onListeningChange,
|
|
108
|
+
}: AiChatBlockProps) {
|
|
109
|
+
const generatedId = useId()
|
|
110
|
+
const [chat, setChat] = useState<ChatState>(() => {
|
|
111
|
+
const threads = initialThreads?.length
|
|
112
|
+
? initialThreads.map((thread) => ({ ...thread, messages: [...thread.messages] }))
|
|
113
|
+
: [{ id: generatedId, title: 'New chat', messages: [...initialMessages] }]
|
|
114
|
+
return { threads, activeThreadId: threads[0].id }
|
|
115
|
+
})
|
|
116
|
+
const [sending, setSending] = useState(false)
|
|
117
|
+
const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false)
|
|
118
|
+
const [desktopSidebarHidden, setDesktopSidebarHidden] = useState(false)
|
|
119
|
+
const [attachmentError, setAttachmentError] = useState('')
|
|
120
|
+
const [retryAt, setRetryAt] = useState(0)
|
|
121
|
+
const [clock, setClock] = useState(0)
|
|
122
|
+
const activeController = useRef<AbortController | null>(null)
|
|
123
|
+
const activeRequest = useRef<RetryRequest | null>(null)
|
|
124
|
+
const streamedTextRef = useRef<string[]>([])
|
|
125
|
+
const streamedThinkingRef = useRef<string[]>([])
|
|
126
|
+
const retryRequests = useRef(new Map<string, RetryRequest>())
|
|
127
|
+
const attachmentErrorTimer = useRef<number | null>(null)
|
|
128
|
+
|
|
129
|
+
const activeThread = chat.threads.find((thread) => thread.id === chat.activeThreadId) ?? chat.threads[0]
|
|
130
|
+
const messages = activeThread?.messages ?? []
|
|
131
|
+
const profileAvatar = userAvatar ?? <ChatUserAvatar />
|
|
132
|
+
|
|
133
|
+
function updateThread(threadId: string, update: (thread: ChatThread) => ChatThread) {
|
|
134
|
+
setChat((current) => ({
|
|
135
|
+
...current,
|
|
136
|
+
threads: current.threads.map((thread) => thread.id === threadId ? update(thread) : thread),
|
|
137
|
+
}))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
if (!retryAt) return
|
|
142
|
+
const timer = window.setInterval(() => setClock(Date.now()), 250)
|
|
143
|
+
return () => window.clearInterval(timer)
|
|
144
|
+
}, [retryAt])
|
|
145
|
+
|
|
146
|
+
useEffect(() => () => {
|
|
147
|
+
activeController.current?.abort()
|
|
148
|
+
if (attachmentErrorTimer.current) window.clearTimeout(attachmentErrorTimer.current)
|
|
149
|
+
}, [])
|
|
150
|
+
|
|
151
|
+
async function performRequest(request: RetryRequest) {
|
|
152
|
+
const controller = new AbortController()
|
|
153
|
+
activeController.current?.abort()
|
|
154
|
+
activeController.current = controller
|
|
155
|
+
activeRequest.current = request
|
|
156
|
+
const textChunks: string[] = []
|
|
157
|
+
const thinkingChunks: string[] = []
|
|
158
|
+
streamedTextRef.current = textChunks
|
|
159
|
+
streamedThinkingRef.current = thinkingChunks
|
|
160
|
+
setSending(true)
|
|
161
|
+
setRetryAt(0)
|
|
162
|
+
setAttachmentError('')
|
|
163
|
+
updateThread(request.threadId, (thread) => ({
|
|
164
|
+
...thread,
|
|
165
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId
|
|
166
|
+
? { ...turn, content: '', thinking: '', status: 'streaming', thinkingStatus: 'thinking', error: undefined }
|
|
167
|
+
: turn),
|
|
168
|
+
}))
|
|
169
|
+
|
|
170
|
+
let textLength = 0
|
|
171
|
+
let renderedTextLength = 0
|
|
172
|
+
let streamTimer: number | null = null
|
|
173
|
+
const flushStream = () => {
|
|
174
|
+
streamTimer = null
|
|
175
|
+
if (controller.signal.aborted) return
|
|
176
|
+
const content = textChunks.join('')
|
|
177
|
+
const thinking = thinkingChunks.join('')
|
|
178
|
+
const streamingDelta = content.slice(renderedTextLength)
|
|
179
|
+
renderedTextLength = content.length
|
|
180
|
+
updateThread(request.threadId, (thread) => ({
|
|
181
|
+
...thread,
|
|
182
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId
|
|
183
|
+
? { ...turn, content, streamingDelta, thinking, status: 'streaming', thinkingStatus: 'thinking' }
|
|
184
|
+
: turn),
|
|
185
|
+
}))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
const response = await onSend({ message: request.message, attachments: request.attachments, signal: controller.signal })
|
|
190
|
+
if (controller.signal.aborted) return
|
|
191
|
+
if (typeof response === 'string') {
|
|
192
|
+
const limit = Number.isFinite(maxResponseCharacters) ? Math.max(1, Math.floor(maxResponseCharacters)) : 60_000
|
|
193
|
+
const truncated = response.length > limit
|
|
194
|
+
const content = truncated ? `${response.slice(0, limit)}\n\n[Response stopped at the configured length limit.]` : response
|
|
195
|
+
updateThread(request.threadId, (thread) => ({
|
|
196
|
+
...thread,
|
|
197
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId
|
|
198
|
+
? { ...turn, content, status: 'complete', thinkingStatus: turn.thinking ? 'complete' : undefined }
|
|
199
|
+
: turn),
|
|
200
|
+
}))
|
|
201
|
+
} else if (isAsyncIterable(response)) {
|
|
202
|
+
let truncated = false
|
|
203
|
+
let thinkingLength = 0
|
|
204
|
+
let lastEventLoopYield = performance.now()
|
|
205
|
+
const limit = Number.isFinite(maxResponseCharacters) ? Math.max(1, Math.floor(maxResponseCharacters)) : 60_000
|
|
206
|
+
for await (const chunk of response) {
|
|
207
|
+
if (controller.signal.aborted) break
|
|
208
|
+
const parts = readParts(chunk)
|
|
209
|
+
if (parts.text) {
|
|
210
|
+
const remaining = limit - textLength
|
|
211
|
+
const accepted = parts.text.slice(0, Math.max(0, remaining))
|
|
212
|
+
textChunks.push(accepted)
|
|
213
|
+
textLength += accepted.length
|
|
214
|
+
if (accepted.length < parts.text.length) truncated = true
|
|
215
|
+
}
|
|
216
|
+
if (parts.thinking && thinkingLength < 8_000) {
|
|
217
|
+
const thinking = parts.thinking.slice(0, 8_000 - thinkingLength)
|
|
218
|
+
thinkingChunks.push(thinking)
|
|
219
|
+
thinkingLength += thinking.length
|
|
220
|
+
}
|
|
221
|
+
if ((parts.text || parts.thinking) && streamTimer === null) {
|
|
222
|
+
// Throttle markdown parsing and layout work while model tokens arrive.
|
|
223
|
+
streamTimer = window.setTimeout(flushStream, textLength < 16_000 ? STREAM_RENDER_INTERVAL_MS : 80)
|
|
224
|
+
}
|
|
225
|
+
if (truncated) break
|
|
226
|
+
// Some async iterators resolve chunks continuously in microtasks.
|
|
227
|
+
// Yield to a browser task periodically so input, paint, and Stop remain responsive.
|
|
228
|
+
if (performance.now() - lastEventLoopYield >= 16) {
|
|
229
|
+
await new Promise<void>((resolve) => window.setTimeout(resolve, 0))
|
|
230
|
+
lastEventLoopYield = performance.now()
|
|
231
|
+
}
|
|
232
|
+
if (controller.signal.aborted) break
|
|
233
|
+
}
|
|
234
|
+
if (controller.signal.aborted) return
|
|
235
|
+
if (streamTimer !== null) window.clearTimeout(streamTimer)
|
|
236
|
+
const text = textChunks.join('')
|
|
237
|
+
const thinking = thinkingChunks.join('')
|
|
238
|
+
const content = truncated ? `${text}\n\n[Response stopped at the configured length limit.]` : text
|
|
239
|
+
updateThread(request.threadId, (thread) => ({
|
|
240
|
+
...thread,
|
|
241
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId
|
|
242
|
+
? { ...turn, content, thinking, status: 'complete', thinkingStatus: thinking ? 'complete' : undefined }
|
|
243
|
+
: turn),
|
|
244
|
+
}))
|
|
245
|
+
} else {
|
|
246
|
+
throw new Error('The chat handler must return text or an async stream.')
|
|
247
|
+
}
|
|
248
|
+
} catch (cause) {
|
|
249
|
+
if (controller.signal.aborted) return
|
|
250
|
+
if (streamTimer !== null) window.clearTimeout(streamTimer)
|
|
251
|
+
const error = cause as Partial<ChatSendError>
|
|
252
|
+
const message = typeof error.message === 'string' && error.message.trim()
|
|
253
|
+
? error.message : 'The message could not be sent. Please try again.'
|
|
254
|
+
const waitMs = typeof error.retryAfterMs === 'number' && error.retryAfterMs > 0 ? error.retryAfterMs : undefined
|
|
255
|
+
const nextRetryAt = waitMs ? Date.now() + waitMs : 0
|
|
256
|
+
setRetryAt(nextRetryAt)
|
|
257
|
+
updateThread(request.threadId, (thread) => ({
|
|
258
|
+
...thread,
|
|
259
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId
|
|
260
|
+
? { ...turn, content: textChunks.join(''), thinking: thinkingChunks.join(''), status: 'error', thinkingStatus: thinkingChunks.length ? 'complete' : undefined,
|
|
261
|
+
error: { message, retryAfterMs: waitMs, retryAt: nextRetryAt || undefined } }
|
|
262
|
+
: turn),
|
|
263
|
+
}))
|
|
264
|
+
} finally {
|
|
265
|
+
if (streamTimer !== null) window.clearTimeout(streamTimer)
|
|
266
|
+
if (activeController.current === controller) {
|
|
267
|
+
activeController.current = null
|
|
268
|
+
activeRequest.current = null
|
|
269
|
+
streamedTextRef.current = []
|
|
270
|
+
streamedThinkingRef.current = []
|
|
271
|
+
setSending(false)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function handleSubmit(message: string, attachments: ChatAttachment[]) {
|
|
277
|
+
const thread = activeThread
|
|
278
|
+
if (!thread) return
|
|
279
|
+
const userTurnId = createId()
|
|
280
|
+
const assistantTurnId = createId()
|
|
281
|
+
const storedAttachments = attachments.map((attachment) => ({ ...attachment }))
|
|
282
|
+
const request = { message, attachments: storedAttachments, userTurnId, assistantTurnId, threadId: thread.id }
|
|
283
|
+
retryRequests.current.set(assistantTurnId, request)
|
|
284
|
+
const firstMessage = thread.messages.length === 0
|
|
285
|
+
updateThread(thread.id, (current) => ({
|
|
286
|
+
...current,
|
|
287
|
+
title: firstMessage ? (message.trim().slice(0, 36) || attachments[0]?.name || 'New chat') : current.title,
|
|
288
|
+
messages: [...current.messages,
|
|
289
|
+
{ id: userTurnId, role: 'user', content: message, attachments: storedAttachments, status: 'complete' },
|
|
290
|
+
{ id: assistantTurnId, role: 'assistant', content: '', status: 'streaming', thinkingStatus: 'thinking' },
|
|
291
|
+
],
|
|
292
|
+
}))
|
|
293
|
+
void performRequest(request)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function retryMessage(assistantTurnId: string) {
|
|
297
|
+
const request = retryRequests.current.get(assistantTurnId)
|
|
298
|
+
if (request && !sending && clock >= retryAt) void performRequest(request)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function stopGeneration() {
|
|
302
|
+
const controller = activeController.current
|
|
303
|
+
const request = activeRequest.current
|
|
304
|
+
if (!controller || !request) return
|
|
305
|
+
const partialText = streamedTextRef.current.join('')
|
|
306
|
+
const partialThinking = streamedThinkingRef.current.join('')
|
|
307
|
+
controller.abort()
|
|
308
|
+
activeController.current = null
|
|
309
|
+
activeRequest.current = null
|
|
310
|
+
streamedTextRef.current = []
|
|
311
|
+
streamedThinkingRef.current = []
|
|
312
|
+
setSending(false)
|
|
313
|
+
updateThread(request.threadId, (thread) => ({
|
|
314
|
+
...thread,
|
|
315
|
+
messages: thread.messages.map((turn) => turn.id === request.assistantTurnId && turn.status === 'streaming'
|
|
316
|
+
? { ...turn, content: turn.content || partialText || 'Generation stopped.', thinking: partialThinking, status: 'complete', thinkingStatus: partialThinking ? 'complete' : undefined }
|
|
317
|
+
: turn),
|
|
318
|
+
}))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function startNewChat() {
|
|
322
|
+
stopGeneration()
|
|
323
|
+
const id = createId()
|
|
324
|
+
setChat((current) => ({ threads: [{ id, title: 'New chat', messages: [] }, ...current.threads], activeThreadId: id }))
|
|
325
|
+
setMobileSidebarOpen(false)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function selectThread(id: string) {
|
|
329
|
+
if (sending) stopGeneration()
|
|
330
|
+
setChat((current) => ({ ...current, activeThreadId: id }))
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function togglePinnedThread(id: string) {
|
|
334
|
+
updateThread(id, (thread) => ({ ...thread, pinned: !thread.pinned }))
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function toggleArchivedThread(id: string) {
|
|
338
|
+
const thread = chat.threads.find((item) => item.id === id)
|
|
339
|
+
if (!thread) return
|
|
340
|
+
if (activeRequest.current?.threadId === id) stopGeneration()
|
|
341
|
+
const replacement = { id: createId(), title: 'New chat', messages: [] as ChatTurn[] }
|
|
342
|
+
setChat((current) => {
|
|
343
|
+
const threads = current.threads.map((item) => item.id === id ? { ...item, archived: !item.archived, pinned: item.archived ? item.pinned : false } : item)
|
|
344
|
+
if (current.activeThreadId !== id || thread.archived) return { ...current, threads }
|
|
345
|
+
const next = threads.find((item) => item.id !== id && !item.archived)
|
|
346
|
+
if (next) return { threads, activeThreadId: next.id }
|
|
347
|
+
return { threads: [replacement, ...threads], activeThreadId: replacement.id }
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function deleteThread(id: string) {
|
|
352
|
+
if (activeRequest.current?.threadId === id) stopGeneration()
|
|
353
|
+
const replacement = { id: createId(), title: 'New chat', messages: [] as ChatTurn[] }
|
|
354
|
+
setChat((current) => {
|
|
355
|
+
const threads = current.threads.filter((thread) => thread.id !== id)
|
|
356
|
+
retryRequests.current.forEach((request, key) => {
|
|
357
|
+
if (request.threadId === id) retryRequests.current.delete(key)
|
|
358
|
+
})
|
|
359
|
+
if (current.activeThreadId !== id) return { ...current, threads }
|
|
360
|
+
const next = threads.find((thread) => !thread.archived)
|
|
361
|
+
if (next) return { threads, activeThreadId: next.id }
|
|
362
|
+
return { threads: [replacement, ...threads], activeThreadId: replacement.id }
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function handleAttachmentError(message: string) {
|
|
367
|
+
setAttachmentError(message)
|
|
368
|
+
if (attachmentErrorTimer.current) window.clearTimeout(attachmentErrorTimer.current)
|
|
369
|
+
attachmentErrorTimer.current = window.setTimeout(() => setAttachmentError(''), 4000)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function toggleSidebar() {
|
|
373
|
+
if (window.matchMedia('(min-width: 768px)').matches) {
|
|
374
|
+
setDesktopSidebarHidden((hidden) => !hidden)
|
|
375
|
+
return
|
|
376
|
+
}
|
|
377
|
+
setMobileSidebarOpen((open) => !open)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const style = { '--chat-accent': accentColor } as CSSProperties
|
|
381
|
+
const retryDisabled = sending || (retryAt > 0 && clock < retryAt)
|
|
382
|
+
|
|
383
|
+
return (
|
|
384
|
+
<section
|
|
385
|
+
aria-label={title}
|
|
386
|
+
style={style}
|
|
387
|
+
className={cn('border-border/70 bg-background relative flex h-[min(76vh,54rem)] min-h-[34rem] w-full overflow-hidden rounded-2xl border shadow-[0_24px_64px_-44px_rgba(0,0,0,0.8)] [&_*]:[scrollbar-width:none] [&_*::-webkit-scrollbar]:hidden', fullScreen && 'h-full min-h-0 rounded-none border-0 shadow-none', className)}
|
|
388
|
+
>
|
|
389
|
+
<ChatSidebar
|
|
390
|
+
items={chat.threads.map(({ id, title: threadTitle, pinned, archived }) => ({ id, title: threadTitle, pinned, archived }))}
|
|
391
|
+
activeId={chat.activeThreadId}
|
|
392
|
+
userName={userName}
|
|
393
|
+
userAvatar={profileAvatar}
|
|
394
|
+
mobileOpen={mobileSidebarOpen}
|
|
395
|
+
desktopHidden={desktopSidebarHidden}
|
|
396
|
+
onToggleDesktop={() => setDesktopSidebarHidden((hidden) => !hidden)}
|
|
397
|
+
onCloseMobile={() => setMobileSidebarOpen(false)}
|
|
398
|
+
onNewChat={() => {
|
|
399
|
+
startNewChat()
|
|
400
|
+
setMobileSidebarOpen(false)
|
|
401
|
+
}}
|
|
402
|
+
onSelect={selectThread}
|
|
403
|
+
onPin={togglePinnedThread}
|
|
404
|
+
onArchive={toggleArchivedThread}
|
|
405
|
+
onDelete={deleteThread}
|
|
406
|
+
/>
|
|
407
|
+
|
|
408
|
+
<div className="relative flex min-h-0 min-w-0 flex-1 flex-col">
|
|
409
|
+
<header className="pointer-events-none absolute left-3 top-3 z-10 flex">
|
|
410
|
+
<button
|
|
411
|
+
type="button"
|
|
412
|
+
onClick={toggleSidebar}
|
|
413
|
+
aria-label={desktopSidebarHidden ? 'Show chat history' : 'Toggle chat history'}
|
|
414
|
+
title="Toggle chat history"
|
|
415
|
+
className={cn('border-border/70 bg-background/90 text-[var(--chat-accent)] pointer-events-auto flex size-10 cursor-pointer items-center justify-center rounded-xl border p-2 shadow-lg backdrop-blur-xl transition-transform duration-200 hover:scale-105 active:scale-95 motion-reduce:transition-none md:hidden', mobileSidebarOpen && 'rotate-[-8deg]')}
|
|
416
|
+
>
|
|
417
|
+
<ChatLogo className="size-full lg:hidden" />
|
|
418
|
+
<span className="hidden lg:flex">
|
|
419
|
+
{desktopSidebarHidden ? <IconLayoutSidebarLeftExpand size={20} stroke={1.7} /> : <IconLayoutSidebarLeftCollapse size={20} stroke={1.7} />}
|
|
420
|
+
</span>
|
|
421
|
+
</button>
|
|
422
|
+
</header>
|
|
423
|
+
|
|
424
|
+
<MessageList
|
|
425
|
+
className="pt-16"
|
|
426
|
+
messages={messages}
|
|
427
|
+
userName={userName}
|
|
428
|
+
assistantName={assistantName}
|
|
429
|
+
userAvatar={profileAvatar}
|
|
430
|
+
assistantAvatar={assistantAvatar}
|
|
431
|
+
onRetry={retryMessage}
|
|
432
|
+
retryDisabled={retryDisabled}
|
|
433
|
+
emptyMessage={emptyMessage}
|
|
434
|
+
/>
|
|
435
|
+
|
|
436
|
+
<div className="border-border/60 shrink-0 border-t p-3 sm:p-4">
|
|
437
|
+
<div className="mx-auto w-full md:max-w-3xl">
|
|
438
|
+
{attachmentError ? (
|
|
439
|
+
<p className="mb-2 rounded-lg border border-rose-500/30 bg-rose-500/5 px-3 py-2 text-xs text-rose-200" role="alert">{attachmentError}</p>
|
|
440
|
+
) : null}
|
|
441
|
+
<ChatComposer
|
|
442
|
+
actions={attachmentOptions}
|
|
443
|
+
disabled={false}
|
|
444
|
+
busy={sending}
|
|
445
|
+
maxAttachments={maxAttachments}
|
|
446
|
+
maxFileSizeBytes={maxFileSizeBytes}
|
|
447
|
+
showVoice={allowVoiceInput}
|
|
448
|
+
placeholder={placeholder}
|
|
449
|
+
accentColor={accentColor}
|
|
450
|
+
onSubmit={handleSubmit}
|
|
451
|
+
onStop={stopGeneration}
|
|
452
|
+
onListeningChange={onListeningChange}
|
|
453
|
+
onAttachmentError={handleAttachmentError}
|
|
454
|
+
/>
|
|
455
|
+
</div>
|
|
456
|
+
</div>
|
|
457
|
+
</div>
|
|
458
|
+
</section>
|
|
459
|
+
)
|
|
460
|
+
}
|