File size: 7,639 Bytes
dd4a03a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useCallback, useEffect, useRef, useState } from 'react'
import type { FormEvent } from 'react'
import { streamQuery } from '../api/client.ts'
import { useSession } from '../context/SessionContext.tsx'
import type { ChatMessage } from '../context/SessionContext.tsx'
import { useToast } from './shared/Toast.tsx'
import MarkdownRenderer from './shared/MarkdownRenderer.tsx'
import './chat.css'

function makeId(): string {
  return crypto.randomUUID ? crypto.randomUUID() : `m_${Date.now()}_${Math.random().toString(36).slice(2)}`
}

export default function ChatView() {
  const { sessionId, messages, loadHistory, appendMessage, updateLastMessage, isLoadingHistory } =
    useSession()
  const [question, setQuestion] = useState('')
  const [isStreaming, setIsStreaming] = useState(false)
  const abortControllerRef = useRef<AbortController | null>(null)
  const messagesAreaRef = useRef<HTMLDivElement>(null)
  const { showToast } = useToast()

  useEffect(() => {
    loadHistory()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [sessionId])

  useEffect(() => {
    const el = messagesAreaRef.current
    if (el) {
      el.scrollTop = el.scrollHeight
    }
  }, [messages])

  const handleCancel = useCallback(() => {
    abortControllerRef.current?.abort()
  }, [])

  const handleSubmit = useCallback(
    async (e: FormEvent<HTMLFormElement>) => {
      e.preventDefault()
      const trimmed = question.trim()
      if (!trimmed || isStreaming) return

      const userMessage: ChatMessage = {
        id: makeId(),
        role: 'user',
        content: trimmed,
        sources: null,
        status: 'done',
        createdAt: new Date().toISOString(),
      }
      appendMessage(userMessage)
      setQuestion('')

      const assistantMessage: ChatMessage = {
        id: makeId(),
        role: 'assistant',
        content: '',
        sources: null,
        status: 'pending',
        createdAt: new Date().toISOString(),
      }
      appendMessage(assistantMessage)

      const controller = new AbortController()
      abortControllerRef.current = controller
      setIsStreaming(true)

      try {
        for await (const event of streamQuery(trimmed, sessionId, controller.signal)) {
          if (event.type === 'token') {
            updateLastMessage((msg) => ({
              ...msg,
              content: msg.content + event.token,
              status: 'streaming',
            }))
          } else if (event.type === 'final') {
            updateLastMessage((msg) => ({
              ...msg,
              content: event.answer,
              sources: event.sources,
              status: 'done',
            }))
          } else if (event.type === 'error') {
            updateLastMessage((msg) => ({
              ...msg,
              content: msg.content || `Error: ${event.error}`,
              status: 'error',
            }))
            showToast(event.error, 'error')
          }
        }
      } catch (err) {
        if (err instanceof DOMException && err.name === 'AbortError') {
          updateLastMessage((msg) => ({
            ...msg,
            status: 'done',
            content: msg.content || '(cancelled)',
          }))
        } else {
          const message = err instanceof Error ? err.message : 'Streaming failed'
          updateLastMessage((msg) => ({ ...msg, status: 'error', content: msg.content || `Error: ${message}` }))
          showToast(message, 'error')
        }
      } finally {
        setIsStreaming(false)
        abortControllerRef.current = null
      }
    },
    [question, isStreaming, sessionId, appendMessage, updateLastMessage, showToast],
  )

  return (
    <div className="view-container active">
      <div className="view-header">
        <h2 className="view-title">Interactive Q&A</h2>
        <p className="view-description">Ask questions and get intelligent answers from your documents</p>
      </div>

      <div className="chat-container">
        <div className="messages-area" ref={messagesAreaRef}>
          {isLoadingHistory && messages.length === 0 ? (
            <div className="empty-state">
              <div className="empty-icon">⏳</div>
              <h3>Loading history…</h3>
            </div>
          ) : messages.length === 0 ? (
            <div className="empty-state">
              <div className="empty-icon">💬</div>
              <h3>Start a Conversation</h3>
              <p>Ask me anything about your indexed documents</p>
            </div>
          ) : (
            messages.map((msg) => (
              <div className={`message ${msg.role}`} key={msg.id}>
                <div className="message-bubble">
                  {msg.role === 'assistant' ? (
                    msg.content ? (
                      <MarkdownRenderer text={msg.content} className="message-content" />
                    ) : (
                      <div className="message-content typing-indicator">
                        <span />
                        <span />
                        <span />
                      </div>
                    )
                  ) : (
                    <div className="message-content">{msg.content}</div>
                  )}

                  {msg.role === 'assistant' && msg.status === 'streaming' && (
                    <div className="message-sources sources-loading">
                      <span className="loading-spinner dark" /> Sources loading…
                    </div>
                  )}

                  {msg.role === 'assistant' && msg.status === 'done' && msg.sources && msg.sources.length > 0 && (
                    <div className="message-sources">
                      <strong>Sources</strong>
                      {msg.sources.map((source, idx) => (
                        <div className="source-item" key={`${msg.id}-src-${idx}`}>
                          <strong>
                            {idx + 1}. {source.file_name}
                            {source.score != null ? ` (${source.score.toFixed(3)})` : ''}
                          </strong>
                          <p>{source.text}…</p>
                        </div>
                      ))}
                    </div>
                  )}
                </div>
              </div>
            ))
          )}
        </div>
        <form className="chat-input-area" onSubmit={handleSubmit}>
          <div className="chat-input-wrapper">
            <input
              type="text"
              className="chat-input"
              placeholder="Type your question here..."
              value={question}
              onChange={(e) => setQuestion(e.target.value)}
              disabled={isStreaming}
              required
            />
            {isStreaming ? (
              <button type="button" className="btn-send btn-cancel" onClick={handleCancel} title="Cancel">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
                  <rect x="6" y="6" width="12" height="12" rx="2" fill="currentColor" />
                </svg>
              </button>
            ) : (
              <button type="submit" className="btn-send" title="Send">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
                  <path
                    d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"
                    stroke="currentColor"
                    strokeWidth="2"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  />
                </svg>
              </button>
            )}
          </div>
        </form>
      </div>
    </div>
  )
}