File size: 23,585 Bytes
cdb451a
 
57b1bac
 
cdb451a
14ac924
edc2ebc
cdb451a
 
 
9282e59
 
f37c419
 
cdb451a
 
9282e59
f37c419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9282e59
 
 
f37c419
 
 
 
 
 
 
 
cdb451a
 
d6a264c
cdb451a
d6a264c
57b1bac
 
 
 
 
 
 
 
 
 
 
cdb451a
 
 
 
 
14ac924
 
cdb451a
9282e59
14ac924
cdb451a
 
 
 
14ac924
 
edc2ebc
 
 
 
 
 
 
f37c419
 
cdb451a
6c6d87f
 
 
cdb451a
9282e59
 
 
 
 
 
f37c419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb451a
 
 
 
 
 
 
 
14ac924
 
 
 
 
773173b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14ac924
 
 
 
 
 
 
 
 
 
 
 
edc2ebc
 
 
 
 
 
14ac924
 
 
 
 
 
 
 
edc2ebc
 
 
14ac924
 
 
 
 
 
 
 
 
 
edc2ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c138e
edc2ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c138e
 
 
 
edc2ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14ac924
 
 
 
 
 
 
edc2ebc
 
773173b
14ac924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc2ebc
 
 
 
 
14ac924
 
 
edc2ebc
14ac924
 
 
edc2ebc
 
14ac924
 
 
 
 
 
 
 
6c6d87f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb451a
 
 
 
f37c419
 
 
 
 
 
 
 
 
 
 
d6a264c
cdb451a
d6a264c
 
cdb451a
 
 
 
d6a264c
 
 
 
cdb451a
 
14ac924
 
 
 
 
773173b
 
 
 
 
 
 
 
 
 
14ac924
b2c138e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c7b35f
b2c138e
6c6d87f
 
 
316136a
b2c138e
 
 
 
 
 
 
 
 
2c7b35f
6c6d87f
 
 
316136a
b2c138e
 
 
cdb451a
 
fcf585e
 
 
 
 
cdb451a
 
 
 
e8ae82d
 
 
 
 
fcf585e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ae82d
 
fcf585e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb451a
fcf585e
cdb451a
 
6c6d87f
 
cdb451a
edc2ebc
 
 
 
 
 
 
 
 
 
 
 
 
cdb451a
 
 
d6a264c
cdb451a
f37c419
 
 
cdb451a
f37c419
 
 
 
 
 
 
 
 
 
 
cdb451a
6c6d87f
cdb451a
 
d6a264c
cdb451a
 
14ac924
 
 
 
 
 
 
70c34c5
14ac924
 
 
 
9282e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14ac924
 
 
f37c419
 
 
 
 
 
14ac924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f37c419
 
 
 
 
 
14ac924
 
6c6d87f
 
 
 
 
 
 
 
 
 
f37c419
6c6d87f
 
 
14ac924
cdb451a
edc2ebc
 
 
 
 
 
 
 
 
cdb451a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import { useState, useRef, useEffect } from 'react'
import { Client } from '@langchain/langgraph-sdk'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import { useAuth } from '../../context/useAuth.js'
import Sidebar from '../Sidebar/Sidebar.jsx'
import TimeTravelPanel from '../TimeTravel/TimeTravelPanel.jsx'
import styles from './Chat.module.css'

const API_BASE = import.meta.env.VITE_API_BASE || 'https://cripple-lee-personal-assistant-server.hf.space'

// Module-scope client whose Authorization header tracks the current session.
// The SDK reads the protected `defaultHeaders` field on every request, so
// mutating it on login/logout is enough — no need to recreate the client.
const client = new Client({ apiUrl: API_BASE })

export function setClientAccessToken(token) {
  // `new Client(config)` builds separate sub-client instances (threads, runs,
  // …) and each BaseClient constructor copies `config.defaultHeaders` into its
  // OWN object. So `client.threads.defaultHeaders` and `client.defaultHeaders`
  // are different objects — mutating only the parent never reaches the
  // sub-clients that actually issue requests, and the Authorization header is
  // silently dropped (server responds 401 "Missing Authorization header").
  // Set/clear the header on the parent AND every sub-client.
  const targets = [client, client.threads, client.runs, client.assistants, client.crons, client.store]
  for (const c of targets) {
    if (!c) continue
    if (token) {
      c.defaultHeaders = {
        ...(c.defaultHeaders || {}),
        Authorization: `Bearer ${token}`,
      }
    } else if (c.defaultHeaders) {
      delete c.defaultHeaders.Authorization
    }
  }
}

// Fetch the current user's monthly token usage from the custom /usage route.
async function fetchUsage() {
  const headers = { ...(client.defaultHeaders || {}) }
  const res = await fetch(`${API_BASE}/usage`, { headers })
  if (!res.ok) throw new Error(`Failed to load usage (${res.status})`)
  return res.json()
}

function ChatMessage({ role, content, isStreaming }) {
  return (
    <div className={`${styles.message} ${role === 'human' ? styles.user : styles.agent}`}>
      <div className={styles.bubble}>
        <span className={styles.role}>{role === 'human' ? 'You' : 'AI'}</span>
        {role === 'human' ? (
          <p className={styles.content}>
            {content}
            {role === 'agent' && isStreaming && <span className={styles.cursor} aria-hidden="true" />}
          </p>
        ) : (
          <div className={styles.content}>
            <ReactMarkdown remarkPlugins={[remarkGfm]}>{content}</ReactMarkdown>
            {isStreaming && <span className={styles.cursor} aria-hidden="true" />}
          </div>
        )}
      </div>
    </div>
  )
}

const GREETING = { role: 'assistant', content: 'Hello! How can I help you today?', id: 0 }

export default function Chat() {
  const { accessToken, user, logout } = useAuth()
  const [messages, setMessages] = useState([GREETING])
  const [input, setInput] = useState('')
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState(null)
  const [streamingId, setStreamingId] = useState(null)
  const [threadId, setThreadId] = useState(null)
  const [threads, setThreads] = useState([])
  const [showTimeTravel, setShowTimeTravel] = useState(false)
  const [history, setHistory] = useState([])
  const [historyLoading, setHistoryLoading] = useState(false)
  const [activeCheckpointId, setActiveCheckpointId] = useState(null)
  // Remembers the forked checkpoint per thread, so reopening a conversation
  // from the sidebar shows the messages at the fork point instead of the tip.
  const [forkedCheckpoints, setForkedCheckpoints] = useState({})
  // Monthly token usage: { tokens_used, token_limit, remaining, limit_reached }.
  const [usage, setUsage] = useState(null)
  const messagesEndRef = useRef(null)
  // Identifies the in-flight server-side run ({ threadId, runId }) so
  // Pause can cancel it on the server, not just close the local stream.
  const activeRunRef = useRef(null)

  // Point the SDK client at the current session's access token.
  useEffect(() => {
    setClientAccessToken(accessToken)
    return () => setClientAccessToken(null)
  }, [accessToken])

  // Load monthly token usage on mount / login so the banner and composer
  // reflect the current allowance. State updates are deferred to a microtask
  // so no setState runs synchronously in the effect body.
  useEffect(() => {
    let cancelled = false
    queueMicrotask(() => {
      if (!accessToken) {
        if (!cancelled) setUsage(null)
        return
      }
      fetchUsage()
        .then((u) => { if (!cancelled) setUsage(u) })
        .catch((err) => console.error('Failed to load token usage:', err))
    })
    return () => { cancelled = true }
  }, [accessToken])

  const scrollToBottom = () => {
    messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
  }

  useEffect(() => {
    scrollToBottom()
  }, [messages])

  // Load persisted threads on mount so past conversations can be reopened.
  useEffect(() => {
    const loadThreads = async () => {
      try {
        const result = await client.threads.search({ limit: 20 })
        // Fetch message previews for each thread to show summaries in sidebar
        const threadsWithPreviews = await Promise.all(
          result.map(async (thread) => {
            try {
              const state = await client.threads.getState(thread.thread_id)
              const messages = state.values?.messages || []
              const firstUserMessage = messages.find((m) => m.type === 'human')
              const preview = firstUserMessage?.content
                ? String(firstUserMessage.content).slice(0, 50) +
                  (String(firstUserMessage.content).length > 50 ? '…' : '')
                : 'New conversation'
              return { ...thread, preview }
            } catch {
              return { ...thread, preview: 'New conversation' }
            }
          })
        )
        setThreads(threadsWithPreviews)
      } catch (err) {
        console.error('Failed to load threads:', err)
      }
    }
    loadThreads()
  }, [])

  const loadThreadHistory = async (id) => {
    if (loading) return
    setLoading(true)
    setError(null)
    try {
      // If this conversation was forked at a checkpoint earlier, check out
      // the messages at that checkpoint instead of the thread tip.
      const forkCheckpointId = forkedCheckpoints[id]
      const state = forkCheckpointId
        ? await client.threads.getState(id, forkCheckpointId)
        : await client.threads.getState(id)
      const history = (state.values?.messages || [])
        .filter((m) => m?.content)
        .map((m, i) => ({
          role: m.type === 'human' ? 'human' : 'assistant',
          content: typeof m.content === 'string' ? m.content : '',
          id: m.id || i,
        }))
      setThreadId(id)
      // Drop the previous thread's checkpoints; the sync effect reloads them
      // for the newly selected thread if the panel is open.
      setHistory([])
      setMessages(history.length > 0 ? [GREETING, ...history] : [GREETING])
      setStreamingId(null)
      setInput('')
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }

  // Fetch the checkpoint history for the given thread (newest first).
  const refreshHistory = async (id) => {
    if (!id) {
      setHistory([])
      return
    }
    setHistoryLoading(true)
    setError(null)
    try {
      const states = await client.threads.getHistory(id, { limit: 50 })
      setHistory(states)
    } catch (err) {
      setError(err.message)
    } finally {
      setHistoryLoading(false)
    }
  }

  // Keep the time travel panel in sync with the selected conversation:
  // whenever the active thread changes while the panel is open, reload that
  // thread's checkpoints. The fetch is deferred to a microtask so no setState
  // runs synchronously in the effect body.
  useEffect(() => {
    if (!showTimeTravel) return
    let cancelled = false
    queueMicrotask(async () => {
      setHistoryLoading(true)
      try {
        const states = await client.threads.getHistory(threadId, { limit: 50 })
        if (!cancelled) setHistory(states)
      } catch (err) {
        if (!cancelled) setError(err.message)
      } finally {
        if (!cancelled) setHistoryLoading(false)
      }
    })
    return () => {
      cancelled = true
    }
  }, [showTimeTravel, threadId])

  const handleToggleTimeTravel = () => {
    setShowTimeTravel((open) => !open)
  }

  const handleForkFromCheckpoint = async (state) => {
    console.log('Forking from checkpoint state:', state)
    if (loading || !threadId) return
    const checkpointId = state.checkpoint?.checkpoint_id
    if (!checkpointId) return
    setLoading(true)
    setError(null)
    try {
      // Load the state at the chosen checkpoint so the chat view shows the
      // conversation exactly as it was at that point in time.
      const pastState = await client.threads.getState(threadId, checkpointId)
      const pastMessages = (pastState.values?.messages || [])
        .filter((m) => m?.content)
        .map((m, i) => ({
          role: m.type === 'human' ? 'human' : 'assistant',
          content: typeof m.content === 'string' ? m.content : '',
          id: m.id || i,
        }))
      // Restore the last human message as the input so the user can continue
      // the conversation from that point, and show the rest of the messages in the chat view.
      const lastMessage = pastMessages.pop()
      setInput(lastMessage?.role === 'human' ? lastMessage.content : '')
      setMessages(pastMessages.length > 0 ? [GREETING, ...pastMessages] : [GREETING])
      // Remember the fork point; the next run passes it as checkpointId so
      // LangGraph forks the thread from this checkpoint instead of the tip.
      setActiveCheckpointId(checkpointId)
      // Associate the fork with this thread so selecting the conversation in
      // the sidebar later checks out the messages at this checkpoint.
      setForkedCheckpoints((prev) => ({ ...prev, [threadId]: checkpointId }))
      // Trim the checkpoint list to the timeline at the fork point: history
      // is newest-first, so keep everything from the forked checkpoint's
      // index onward (i.e. that checkpoint and all earlier ones).
      setHistory((prev) => {
        const forkIndex = prev.findIndex(
          (s) => s.checkpoint?.checkpoint_id === checkpointId
        )
        return forkIndex >= 0 ? prev.slice(forkIndex) : prev
      })
      // Clear the remaining transient states so the chat starts fresh from
      // the forked checkpoint: close the panel and reset input/streaming.
      setStreamingId(null)
      setShowTimeTravel(false)
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }

  const handleNewConversation = async () => {
    if (loading) return
    setLoading(true)
    setError(null)
    try {
      const thread = await client.threads.create()
      setThreadId(thread.thread_id)
      setActiveCheckpointId(null)
      setHistory([])
      setThreads((prev) => [{ ...thread, preview: 'New conversation' }, ...prev])
      setMessages([GREETING])
      setStreamingId(null)
      setInput('')
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }

  const handleDeleteThread = async (id) => {
    if (loading) return
    setLoading(true)
    setError(null)
    try {
      await client.threads.delete(id)
      setThreads((prev) => prev.filter((t) => t.thread_id !== id))
      setForkedCheckpoints((prev) => {
        const next = { ...prev }
        delete next[id]
        return next
      })
      // If the deleted conversation is currently open, reset the chat view.
      if (id === threadId) {
        setThreadId(null)
        setActiveCheckpointId(null)
        setMessages([GREETING])
        setStreamingId(null)
        setInput('')
        setShowTimeTravel(false)
        setHistory([])
      }
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }

  const handleStopStream = async (e) => {
    e.preventDefault()
    // Cancel the run on the server so the agent stops generating; the
    // server closing the run also ends the SSE stream and the loop below.
    const activeRun = activeRunRef.current
    if (activeRun) {
      activeRunRef.current = null
      try {
        await client.runs.cancel(activeRun.threadId, activeRun.runId)
      } catch (err) {
        console.error('Failed to cancel run:', err)
      }
    }
  }


  const handleSubmit = async (e) => {
    e.preventDefault()
    const text = input.trim()
    if (!text || loading) return

    // Client-side guard: don't even create a run when the limit is known to
    // be reached (the server enforces it too, returning 403).
    if (usage?.limit_reached) {
      setError(
        `Monthly token limit reached (${usage.tokens_used}/${usage.token_limit} tokens used). ` +
        'Your allowance refreshes on the first day of next month.'
      )
      return
    }

    const humanMessage = { role: 'human', content: text, id: messages.length }

    const updatedMessages = [...messages, humanMessage]
    setMessages(updatedMessages)
    setInput('')
    setLoading(true)
    setError(null)

    updatedMessages.forEach(
      message => console.log('Message: <' + message.role + '> ' + message.content)
    )

    try {
      const assistantId = 'agent'
      let currentThreadId = threadId
      if (!currentThreadId) {
        const thread = await client.threads.create()
        currentThreadId = thread.thread_id
        setThreadId(currentThreadId)
        setThreads((prev) => [{ ...thread, preview: text.slice(0, 50) + (text.length > 50 ? '…' : '') }, ...prev])
      } else {
        // Update preview with latest user message for existing thread
        setThreads((prev) =>
          prev.map((t) =>
            t.thread_id === currentThreadId
              ? { ...t, preview: text.slice(0, 50) + (text.length > 50 ? '…' : '') }
              : t
          )
        )
      }

      let stream = null
      if (activeCheckpointId) {
        console.log('Sending message with checkpointId:', activeCheckpointId)

        const newConfig = await client.threads.updateState(
          currentThreadId,
          {
            values: { messages: [humanMessage] },
            checkpointId: activeCheckpointId,
          }
        )

        stream = client.runs.stream(
          currentThreadId,
          assistantId,
          {
            input: null,
            streamMode: ['events'],
            checkpointId: newConfig.configurable?.checkpoint_id,
            onRunCreated: ({ run_id }) => {
              activeRunRef.current = { threadId: currentThreadId, runId: run_id }
            },
            onDisconnect: "cancel",
          }
        )
      } else {
        console.log('Sending message without checkpointId')
        stream = client.runs.stream(
          currentThreadId,
          assistantId,
          {
            input: { messages: [humanMessage] },
            streamMode: ['events'],
            onRunCreated: ({ run_id }) => {
              activeRunRef.current = { threadId: currentThreadId, runId: run_id }
            },
            onDisconnect: "cancel",
          },
        )
      }

      let hasStartedStreaming = false
      // Depth of nested tool execution. While > 0 we are inside a subagent
      // (navigate/search) tool call, and any chat-model chunks belong to the
      // subagent — NOT to the supervisor's own answer. Tracking this prevents
      // subagent output from being interleaved/duplicated into the final text.
      let toolDepth = 0

      for await (const event of stream) {
        console.log('Event:', event) // Debug log to see what events we're getting

        // Some stream events (e.g. metadata, updates, end) carry a null or
        // shapeless data payload. Skip them before reading the inner event
        // name, otherwise `event.data.event` throws and aborts the stream.
        if (!event.data || typeof event.data !== 'object') continue

        const eventName = event.data.event

        if (eventName === 'on_tool_start') {
          toolDepth += 1
          continue
        }
        if (eventName === 'on_tool_end') {
          toolDepth = Math.max(0, toolDepth - 1)
          continue
        }

        if (eventName !== 'on_chat_model_stream') {
          continue
        }

        // Ignore chunks produced by subagents running inside a tool call.
        if (toolDepth > 0) {
          continue
        }

        const chunk = event.data.data?.chunk
        if (!chunk) continue

        // Skip chunks that are part of a tool call. Qwen emits raw tool-call
        // markup (e.g. `<function=...>` / `</function>`) as content tokens;
        // these carry tool-call metadata and must not be treated as
        // user-facing text.
        if (chunk?.additional_kwargs?.tool_calls) {
          continue
        }

        const value = chunk?.content
        if (value === undefined || value === null || value === '') {
          continue
        }

        const text = String(value)

        // Defensive filter: drop any residual tool-markup fragments that
        // leaked into plain content (the source of the `se}</function>`
        // artifacts).
        if (
          text.includes('<function') ||
          text.includes('</function') ||
          text.includes('<tool') ||
          text.includes('</tool')
        ) {
          continue
        }

        hasStartedStreaming = true

        setMessages((messages) => {
          // Find the last message if it's from the agent and currently streaming
          const lastMessageIndex = messages.length - 1
          if (lastMessageIndex >= 0 && messages[lastMessageIndex].role === 'assistant') {
            // Update the last message with accumulated content
            const updatedMessages = [...messages]
            updatedMessages[lastMessageIndex] = {
              ...updatedMessages[lastMessageIndex],
              content: updatedMessages[lastMessageIndex].content + text
            }
            return updatedMessages
          } else {
            // Create the first assistant message
            setStreamingId(messages.length)
            return [...messages, { role: 'assistant', content: text, id: messages.length }]
          }
        })
      }

      // The stream ended (completed or stopped); forget the run handle.
      activeRunRef.current = null
      setStreamingId(null)
      // The run created a new checkpoint at the thread tip; clear the fork
      // point so following messages continue from the latest state, and drop
      // the thread's fork record since the fork is now part of its history.
      setActiveCheckpointId(null)
      setForkedCheckpoints((prev) => {
        const next = { ...prev }
        delete next[currentThreadId]
        return next
      })
      // Keep the time travel panel up to date with the new checkpoint.
      if (showTimeTravel) {
        refreshHistory(currentThreadId)
      }

      // If we didn't get any streaming content, show a message
      if (!hasStartedStreaming) {
        setMessages((messages) => [...messages, { role: 'assistant', content: 'There are some problems with the agent.' }])
      }

      // Refresh the usage banner now that the run consumed tokens.
      fetchUsage().then(setUsage).catch(() => {})
    } catch (err) {
      // A 403 from runs.create means the monthly token limit was reached;
      // surface the server's message and refresh the usage banner.
      if (err?.status === 403 || /token limit/i.test(err?.message || '')) {
        fetchUsage().then(setUsage).catch(() => {})
        setError(
          err.message ||
          'Monthly token limit reached. Your allowance refreshes on the first day of next month.'
        )
      } else {
        setError(err.message)
      }
    } finally {
      activeRunRef.current = null
      setLoading(false)
    }
}

  return (
    <div className={styles.layout}>
      <Sidebar
        threads={threads}
        activeThreadId={threadId}
        loading={loading}
        onSelectThread={loadThreadHistory}
        onNewChat={handleNewConversation}
        onDeleteThread={handleDeleteThread}
      />
      <div className={styles.chat}>
        <header className={styles.header}>
          <h1>ASSISTANT</h1>
          <div className={styles.headerActions}>
            <button
              type="button"
              className={styles.timeTravelToggle}
              onClick={handleToggleTimeTravel}
              disabled={loading || !threadId}
              title={threadId ? 'Browse checkpoints and fork the conversation' : 'Start a conversation first'}
              aria-pressed={showTimeTravel}
            >
              ⏱ Time travel
            </button>
            <span className={styles.userName} title={user?.username}>
              {user?.display_name || user?.username}
            </span>
            <button
              type="button"
              className={styles.logoutButton}
              onClick={logout}
              title="Sign out"
            >
              Sign out
            </button>
          </div>
        </header>

        <div className={styles.messages}>
          {usage?.limit_reached && (
            <div className={styles.usageBanner} role="alert">
              Monthly token limit reached ({usage.tokens_used}/{usage.token_limit} tokens used).
              Your allowance refreshes on the first day of next month.
            </div>
          )}
          {messages.map((msg, index) => (
            <ChatMessage key={index} role={msg.role} content={msg.content} isStreaming={msg.id === streamingId} />
          ))}
          {loading && (
            <div className={styles.loading}>
              <span className={styles.spinner} aria-hidden="true" />
              Agent is thinking…
            </div>
          )}
          {error && <div className={styles.error}>Error: {error}</div>}
          <div ref={messagesEndRef} />
        </div>

        <form className={styles.inputArea} onSubmit={handleSubmit}>
          <input
            type="text"
            value={input}
            onChange={(e) => setInput(e.target.value)}
            placeholder={
              usage?.limit_reached
                ? 'Monthly token limit reached — refreshes on the 1st of next month'
                : 'Type your message…'
            }
            disabled={loading || usage?.limit_reached}
            aria-label="Message"
          />
          {loading ? (
            <button
              type="button"
              className={styles.pauseButton}
              onClick={handleStopStream}
              title="Stop generating"
            >
              Pause
            </button>
          ) : (
            <button type="submit" disabled={!input.trim() || usage?.limit_reached}>
              Send
            </button>
          )}
        </form>
      </div>
      {showTimeTravel && (
        <TimeTravelPanel
          history={history}
          loading={historyLoading}
          activeCheckpointId={activeCheckpointId}
          onFork={handleForkFromCheckpoint}
          onClose={() => setShowTimeTravel(false)}
        />
      )}
    </div>
  )
}