File size: 14,581 Bytes
212c959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import AppShell from './AppShell'
import { useState, useEffect, useCallback, useRef } from 'react'
import { api, getToken, saveToken } from './utils/api'
import Sidebar from './components/Sidebar'
import TopBar from './components/TopBar'
import ChatArea from './components/ChatArea'
import Composer from './components/Composer'
import AuthModal from './components/AuthModal'
import ModelPicker from './components/ModelPicker'
import FeedbackModal from './components/FeedbackModal'
import SettingsModal from './components/SettingsModal'

function newUid() {
  return crypto.randomUUID ? crypto.randomUUID()
    : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        const r = Math.random() * 16 | 0; return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
      })
}

function LegacyApp() {
  /* ── Auth ─────────────────────────────────── */
  const [token,   setToken]   = useState(() => getToken())
  const [user,    setUser]    = useState(null)
  const [authMode, setAuthMode] = useState(null) // 'login' | 'register' | null

  /* ── Sessions ─────────────────────────────── */
  const [sessions,          setSessions]         = useState([])
  const [currentSessionId,  setCurrentSessionId] = useState(null)
  const [sessionTitle,      setSessionTitle]     = useState('New Chat')

  /* ── Messages ─────────────────────────────── */
  const [messages,  setMessages]  = useState([])
  const [loading,   setLoading]   = useState(false)

  /* ── Model registry ───────────────────────── */
  const [registry,  setRegistry]  = useState({})
  const [provider,  setProvider]  = useState('groq')
  const [model,     setModel]     = useState('llama-3.3-70b-versatile')
  const [agentMode, setAgentMode] = useState(false)

  /* ── Attachments ──────────────────────────── */
  const [attachments, setAttachments] = useState([])

  /* ── UI state ─────────────────────────────── */
  const [sidebarOpen,  setSidebarOpen]  = useState(true)
  const [modal,        setModal]        = useState(null)  // 'model'|'feedback'|'settings'
  const [theme,        setTheme]        = useState(() => localStorage.getItem('owngpt_v2_theme') || 'dark')
  const [searchQ,      setSearchQ]      = useState('')

  const chatRef = useRef(null)

  /* ── Apply theme ──────────────────────────── */
  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme)
    localStorage.setItem('owngpt_v2_theme', theme)
  }, [theme])

  /* ── Boot: capture URL token β†’ load me + models ── */
  useEffect(() => {
    const params = new URLSearchParams(window.location.search)
    const urlToken = params.get('access_token')
    if (urlToken) {
      saveToken(urlToken); setToken(urlToken)
      window.history.replaceState({}, '', window.location.pathname)
    }
    // load model registry (always)
    api.get('/models').then(d => {
      if (d?.providers) setRegistry(d.providers)
    }).catch(() => {})
  }, [])

  /* ── Auth check ───────────────────────────── */
  useEffect(() => {
    if (!token) { setUser(null); return }
    api.get('/auth/me').then(me => {
      setUser(me)
      loadSessions('')
    }).catch(() => {
      saveToken(null); setToken(null); setUser(null)
    })
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [token])

  /* ── Session search ───────────────────────── */
  useEffect(() => {
    if (!user) return
    const t = setTimeout(() => loadSessions(searchQ), 300)
    return () => clearTimeout(t)
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [searchQ, user])

  /* ── Helpers ──────────────────────────────── */
  const scrollBottom = useCallback(() => {
    setTimeout(() => { chatRef.current?.scrollTo({ top: 9999999, behavior: 'smooth' }) }, 30)
  }, [])

  const loadSessions = useCallback(async (q) => {
    const url = q ? `/search?query=${encodeURIComponent(q)}` : '/search'
    try { const d = await api.get(url); setSessions(d.sessions || []) } catch {}
  }, [])

  /* ── Auth handlers ────────────────────────── */
  const handleLogin = useCallback(async (username, password) => {
    const d = await api.post('/auth/login', { username, password })
    saveToken(d.access_token); setToken(d.access_token)
    setAuthMode(null)
  }, [])

  const handleRegister = useCallback(async (username, password) => {
    await api.post('/auth/register', { username, password })
    await handleLogin(username, password)
  }, [handleLogin])

  const handleLogout = useCallback(() => {
    saveToken(null); setToken(null); setUser(null)
    setSessions([]); setMessages([]); setCurrentSessionId(null); setSessionTitle('New Chat')
  }, [])

  /* ── Session handlers ─────────────────────── */
  const handleNewSession = useCallback(() => {
    setCurrentSessionId(null); setMessages([]); setSessionTitle('New Chat'); setAttachments([])
    setSearchQ('')
  }, [])

  const handleSelectSession = useCallback(async (s) => {
    setCurrentSessionId(s.session_id); setSessionTitle(s.title || 'New Chat')
    setSessions(prev => prev.map(x => ({ ...x, _active: x.session_id === s.session_id })))
    setMessages([])
    try {
      const d = await api.get(`/history/${encodeURIComponent(s.session_id)}`)
      setMessages(d.messages || [])
    } catch {}
    scrollBottom()
  }, [scrollBottom])

  const handleDeleteSession = useCallback(async (sessionId) => {
    if (!window.confirm('Delete this chat? This cannot be undone.')) return
    try { await api.delete(`/session/${encodeURIComponent(sessionId)}`) } catch {}
    if (currentSessionId === sessionId) handleNewSession()
    setSessions(prev => prev.filter(s => s.session_id !== sessionId))
  }, [currentSessionId, handleNewSession])

  const handleRenameSession = useCallback(async (newTitle) => {
    if (!currentSessionId) return
    try { await api.patch(`/session/${encodeURIComponent(currentSessionId)}/rename`, { title: newTitle }) } catch {}
    setSessionTitle(newTitle)
    setSessions(prev => prev.map(s => s.session_id === currentSessionId ? { ...s, title: newTitle } : s))
  }, [currentSessionId])

  /* ── File upload ──────────────────────────── */
  const handleUpload = useCallback(async (files) => {
    for (const file of Array.from(files)) {
      const form = new FormData(); form.append('file', file)
      try {
        const d = await api.postForm('/upload', form)
        setAttachments(prev => [...prev, { filename: d.filename, kind: d.kind, extracted_text: d.extracted_text }])
      } catch (e) { alert('Upload failed: ' + e.message) }
    }
  }, [])

  /* ── Send message ─────────────────────────── */
  const handleSend = useCallback(async (text) => {
    if (!user) { setAuthMode('login'); return }
    if (!text.trim() && attachments.length === 0) return

    const sessionId = currentSessionId || newUid()
    if (!currentSessionId) {
      setCurrentSessionId(sessionId)
      setSessionTitle(text.slice(0, 48) || 'New Chat')
    }

    const userMsg = {
      role: 'user', content: text, session_id: sessionId,
      attachments: [...attachments], _id: newUid(),
    }
    setMessages(prev => [...prev, userMsg])
    setAttachments([])
    setLoading(true)
    scrollBottom()

    try {
      const respId = newUid();
      setMessages(prev => [...prev, {
        role: 'assistant', content: '',
        model_used: model, provider,
        session_id: sessionId, _id: respId,
      }]);

      const response = await fetch('/chat', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          ...(getToken() ? { 'Authorization': `Bearer ${getToken()}` } : {})
        },
        body: JSON.stringify({
          message: text, session_id: sessionId,
          provider, model,
          agent_mode: agentMode,
          attachments: attachments.map(a => ({ filename: a.filename, kind: a.kind, extracted_text: a.extracted_text })),
        })
      });

      if (!response.ok) throw new Error(`Server returned ${response.status}`);
      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let done = false;
      let fullText = "";
      let buffer = "";

      while (!done) {
        const { value, done: doneReading } = await reader.read();
        done = doneReading;
        if (value) {
          buffer += decoder.decode(value, { stream: true });
          let boundaries = buffer.split(/\r?\n\r?\n/);
          buffer = boundaries.pop() || ""; // retain incomplete event in buffer
          
          for (let block of boundaries) {
            const lines = block.split(/\r?\n/);
            let dataStr = null;
            for (let line of lines) {
              if (line.startsWith('data: ')) {
                dataStr = line.slice(6);
              }
            }
            if (!dataStr) continue;
            
            try {
              const data = JSON.parse(dataStr);
              if (data.text) {
                fullText += data.text;
                // incrementally update state so UI renders instantly
                setMessages(prev => prev.map(m => m._id === respId ? { ...m, content: fullText } : m));
              } else if (data.model_used) {
                // Done event metadata payload
                setMessages(prev => prev.map(m => m._id === respId ? { 
                  ...m, 
                  model_used: data.model_used, 
                  provider: data.provider,
                  content: data.response ? data.response : m.content
                } : m));
              } else if (data.detail) {
                // Error event 
                fullText += `\n\n⚠️ Error: ${data.detail}`;
                setMessages(prev => prev.map(m => m._id === respId ? { ...m, content: fullText } : m));
              }
            } catch (e) {
              console.warn("Failed to parse SSE data block", e, dataStr);
            }
          }
        }
      }
      loadSessions(searchQ)
    } catch (e) {
      setMessages(prev => [...prev, { role: 'assistant', content: `⚠️ Error: ${e.message}`, _id: newUid() }])
    } finally {
      setLoading(false); scrollBottom()
    }
  }, [user, currentSessionId, attachments, provider, model, agentMode, scrollBottom, loadSessions, searchQ])

  /* ── Feedback ─────────────────────────────── */
  const handleFeedback = useCallback(async (data) => {
    await api.post('/feedback', data)
  }, [])

  /* ── Model select ─────────────────────────── */
  const handleModelSelect = useCallback((prov, mod) => {
    setProvider(prov); setModel(mod)
    setModal(null)
  }, [])

  const requireAuth = useCallback((action) => {
    if (!user) { setAuthMode('login'); return false }
    action?.()
    return true
  }, [user])

  return (
    <div className="app-shell">
      <div className="bg-mesh"></div>
      <Sidebar
        open={sidebarOpen}
        sessions={sessions}
        currentSessionId={currentSessionId}
        searchQ={searchQ}
        onSearchChange={setSearchQ}
        onNewSession={handleNewSession}
        onSelectSession={handleSelectSession}
        onDeleteSession={handleDeleteSession}
        onFeedback={() => requireAuth(() => setModal('feedback'))}
        onSettings={() => setModal('settings')}
      />

      <div className="main">
        <TopBar
          onToggleSidebar={() => setSidebarOpen(o => !o)}
          sessionTitle={sessionTitle}
          user={user}
          onRename={handleRenameSession}
          onLogin={() => setAuthMode('login')}
          onLogout={handleLogout}
          onFeedback={() => requireAuth(() => setModal('feedback'))}
          onSettings={() => setModal('settings')}
          theme={theme}
          onTheme={setTheme}
          currentSessionId={currentSessionId}
        />

        <ChatArea
          ref={chatRef}
          messages={messages}
          loading={loading}
          user={user}
          onSuggestion={t => { requireAuth(() => handleSend(t)) }}
        />

        <Composer
          onSend={handleSend}
          onUpload={handleUpload}
          attachments={attachments}
          onRemoveAttachment={i => setAttachments(prev => prev.filter((_, idx) => idx !== i))}
          provider={provider}
          model={model}
          registry={registry}
          agentMode={agentMode}
          onToggleAgent={() => setAgentMode(a => !a)}
          onOpenModelPicker={() => setModal('model')}
          disabled={!user}
          onClickDisabled={() => setAuthMode('login')}
        />
      </div>

      {/* ── Modals ── */}
      {authMode && (
        <AuthModal
          mode={authMode}
          onModeChange={setAuthMode}
          onLogin={handleLogin}
          onRegister={handleRegister}
          onClose={() => setAuthMode(null)}
        />
      )}
      {modal === 'model' && (
        <ModelPicker
          registry={registry}
          currentProvider={provider}
          currentModel={model}
          onSelect={handleModelSelect}
          onClose={() => setModal(null)}
        />
      )}
      {modal === 'feedback' && (
        <FeedbackModal
          onSubmit={handleFeedback}
          onClose={() => setModal(null)}
        />
      )}
      {modal === 'settings' && (
        <SettingsModal
          theme={theme}
          onTheme={setTheme}
          onClearChats={() => {
            if (window.confirm('Clear the current chat from this screen? Saved history stays in the sidebar.')) {
              setMessages([]); setCurrentSessionId(null); setSessionTitle('New Chat'); setAttachments([])
            }
          }}
          onClose={() => setModal(null)}
        />
      )}
    </div>
  )
}

export default AppShell