-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpage.tsx
More file actions
505 lines (455 loc) · 18.4 KB
/
Copy pathpage.tsx
File metadata and controls
505 lines (455 loc) · 18.4 KB
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
"use client"
import { Suspense, useCallback, useRef, useEffect, useState } from "react"
import { useSearchParams, useRouter } from "next/navigation"
import { THEMES } from "@/types"
import type { Provider, Locale, ResponseLength, Theme, Message, VerdictResult } from "@/types"
import { useDebateEngine } from "@/hooks/useDebateEngine"
import ChatThread from "@/components/ChatThread"
import MessageInput from "@/components/MessageInput"
import ConsensusMeter from "@/components/ConsensusMeter"
import ChatHeader from "@/components/Header"
import dynamic from "next/dynamic"
const SettingsModal = dynamic(() => import("@/components/SettingsModal"), { ssr: false })
import { AnimatePresence, motion } from "framer-motion"
import { ChevronDown } from "lucide-react"
import { useThreadPersistence } from "@/hooks/useThreadPersistence"
import { incrementDebateCount } from "@/components/LoginGate"
const DEFAULT_MODELS: Provider[] = ["gemini", "perplexity", "claude", "gpt"]
export default function ChatPage() {
return (
<Suspense>
<ChatPageContent />
</Suspense>
)
}
function ChatPageContent() {
// Config loaded from sessionStorage (set by homepage)
const [locale, setLocale] = useState<Locale>("ko")
const [responseLength, setResponseLength] = useState<ResponseLength>("short")
const [maxRounds, setMaxRounds] = useState(1)
const [theme, setTheme] = useState<Theme>("dark")
const [isSettingsOpen, setIsSettingsOpen] = useState(false)
const [isLoadingThread, setIsLoadingThread] = useState(false)
const [fileWarning, setFileWarning] = useState<string | null>(null)
const { state, dispatch, handleSend, handleStop, handleReset, handleSendRef } =
useDebateEngine({ locale, responseLength, maxRounds })
const persistence = useThreadPersistence()
const router = useRouter()
const searchParams = useSearchParams()
const threadParam = searchParams.get("thread")
const mainRef = useRef<HTMLElement>(null)
const [showScrollDown, setShowScrollDown] = useState(false)
// Bumped on bfcache restore to force framer-motion remount
const [mountKey, setMountKey] = useState(0)
// Apply theme classes to <html>
useEffect(() => {
const cl = document.documentElement.classList
cl.remove(...THEMES.filter((t) => t !== "light"))
const isLightTheme = theme === "light" || theme === "solarized"
if (!isLightTheme) {
cl.add("dark")
if (theme !== "dark") cl.add(theme)
} else if (theme === "solarized") {
cl.add("solarized")
}
}, [theme])
// BUG-015: Re-apply theme when page becomes visible again
// (handles bfcache restore, Next.js client-side back/forward, and tab switching)
useEffect(() => {
const reapplyTheme = () => {
let saved = localStorage.getItem("quorum_theme") as string | null
if (saved === "github") { saved = "solarized"; localStorage.setItem("quorum_theme", "solarized") }
if (saved && (THEMES as readonly string[]).includes(saved)) {
setTheme(saved as Theme)
}
}
const handleVisibility = () => {
if (document.visibilityState === "visible") reapplyTheme()
}
const handlePageShow = (e: PageTransitionEvent) => {
if (e.persisted) {
reapplyTheme()
setMountKey((k) => k + 1)
}
}
document.addEventListener("visibilitychange", handleVisibility)
window.addEventListener("pageshow", handlePageShow)
window.addEventListener("focus", reapplyTheme)
return () => {
document.removeEventListener("visibilitychange", handleVisibility)
window.removeEventListener("pageshow", handlePageShow)
window.removeEventListener("focus", reapplyTheme)
}
}, [])
/* ---- Read config from sessionStorage on mount ---- */
const initialPromptSent = useRef(false)
const pendingPrompt = useRef<{ prompt: string; models: Provider[] } | null>(null)
const [configHydrated, setConfigHydrated] = useState(false)
useEffect(() => {
let savedTheme = localStorage.getItem("quorum_theme") as string | null
if (savedTheme === "github") { savedTheme = "solarized"; localStorage.setItem("quorum_theme", "solarized") }
if (savedTheme && (THEMES as readonly string[]).includes(savedTheme)) {
setTheme(savedTheme as Theme)
}
const savedLocale = localStorage.getItem("quorum_locale") as string | null
if (savedLocale === "en" || savedLocale === "ko") {
setLocale(savedLocale)
}
const savedLength = localStorage.getItem("quorum_responseLength") as string | null
if (savedLength === "short" || savedLength === "medium" || savedLength === "long") {
setResponseLength(savedLength)
}
const savedRounds = localStorage.getItem("quorum_rounds")
if (savedRounds) {
const n = parseInt(savedRounds, 10)
if ([1, 2, 3, 5].includes(n)) setMaxRounds(n)
}
const raw = sessionStorage.getItem("quorum_config")
if (!raw) {
setConfigHydrated(true)
return
}
try {
const config = JSON.parse(raw) as {
prompt?: string
models?: Provider[]
responseLength?: ResponseLength
rounds?: number
locale?: Locale
}
sessionStorage.removeItem("quorum_config")
const warningsRaw = sessionStorage.getItem("quorum_file_warnings")
if (warningsRaw) {
sessionStorage.removeItem("quorum_file_warnings")
try { setFileWarning(JSON.parse(warningsRaw).join("\n")) } catch { /* ignore */ }
}
if (config.models?.length) dispatch({ type: "SET_MODELS", models: config.models })
if (config.responseLength) setResponseLength(config.responseLength)
if (config.rounds) setMaxRounds(config.rounds)
if (config.locale) setLocale(config.locale)
if (config.prompt) {
pendingPrompt.current = {
prompt: config.prompt,
models: config.models ?? DEFAULT_MODELS,
}
}
} catch {
// malformed config - ignore
} finally {
setConfigHydrated(true)
}
const pending = sessionStorage.getItem("quorum_pending")
if (pending) {
sessionStorage.removeItem("quorum_pending")
try {
const config = JSON.parse(pending)
if (config.models?.length) dispatch({ type: "SET_MODELS", models: config.models })
if (config.responseLength) setResponseLength(config.responseLength)
if (config.rounds) setMaxRounds(config.rounds)
if (config.locale) setLocale(config.locale)
if (config.prompt) {
pendingPrompt.current = {
prompt: config.prompt,
models: config.models ?? DEFAULT_MODELS,
}
}
} catch { /* ignore */ }
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
// Fire pending prompt after state has settled
useEffect(() => {
if (!configHydrated) return
if (pendingPrompt.current && !initialPromptSent.current) {
initialPromptSent.current = true
const { prompt: p, models } = pendingPrompt.current
pendingPrompt.current = null
setTimeout(() => handleSendRef.current(p, "all", models), 0)
}
}, [configHydrated, handleSendRef])
const changeTheme = useCallback((t: Theme) => {
setTheme(t)
localStorage.setItem("quorum_theme", t)
}, [])
const toggleTheme = useCallback(() => {
const order = THEMES
setTheme((prev) => {
const next = order[(order.indexOf(prev) + 1) % order.length]
localStorage.setItem("quorum_theme", next)
return next
})
}, [])
// Persistence-related refs
const creatingThreadRef = useRef(false)
const prevMessageCount = useRef(0)
const isHydratingRef = useRef(false)
const threadLoaded = useRef<string | null>(null)
// Auto-save messages when new ones are added
useEffect(() => {
if (!persistence.isLoggedIn) return
if (isHydratingRef.current) {
prevMessageCount.current = state.messages.length
return
}
if (state.messages.length <= prevMessageCount.current) {
prevMessageCount.current = state.messages.length
return
}
prevMessageCount.current = state.messages.length
// If no thread exists yet and we have a user message, create one
if (!persistence.threadId.current && !creatingThreadRef.current && state.messages.length > 0) {
const firstUserMsg = state.messages.find(m => m.sender === "user")
if (firstUserMsg) {
creatingThreadRef.current = true
persistence.createThread({
title: firstUserMsg.content.slice(0, 80),
models: state.activeModels,
rounds: maxRounds,
responseLength,
locale,
}).then((id) => {
creatingThreadRef.current = false
if (id) {
dispatch({ type: "SET_THREAD_ID", id })
// Don't save here — auto-save handles it on next trigger,
// avoiding saving empty AI placeholders during thread creation.
}
})
return
}
}
// Skip verdict message batch — verdict-save effect handles it sequentially
if (state.showSummary && state.messages[state.messages.length - 1]?.sender === "verdict") {
return
}
// Don't save while an AI message is still streaming (empty placeholder).
// The next ADD_MESSAGE trigger will include the now-complete message.
const lastMsg = state.messages[state.messages.length - 1]
if (lastMsg && !lastMsg.content && lastMsg.sender !== "user" && lastMsg.sender !== "system" && lastMsg.sender !== "verdict") {
return
}
// Otherwise save incrementally
persistence.saveMessages(state.messages)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.messages.length])
// Auto-save verdict when summary is shown (save messages first to avoid version race)
useEffect(() => {
if (isHydratingRef.current) return
if (state.showSummary && state.verdict && persistence.threadId.current) {
const doSave = async () => {
await persistence.saveMessages(state.messages)
const afterIndex = state.messages.length - 1
await persistence.saveVerdict(state.verdict!, afterIndex)
}
doSave()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.showSummary])
const currentTitle = state.messages.find(m => m.sender === "user")?.content.slice(0, 80) ?? null
const handleNewDebate = useCallback(() => {
creatingThreadRef.current = false
threadLoaded.current = null
prevMessageCount.current = 0
persistence.reset()
handleReset()
router.replace("/chat")
}, [persistence, handleReset, router])
// When user continues a completed thread, mark it active again
const prevShowSummary = useRef(state.showSummary)
useEffect(() => {
if (prevShowSummary.current && !state.showSummary) {
persistence.continueThread()
}
prevShowSummary.current = state.showSummary
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.showSummary])
// Load thread from URL parameter
useEffect(() => {
if (!threadParam || threadLoaded.current === threadParam || !persistence.isLoggedIn) return
threadLoaded.current = threadParam
creatingThreadRef.current = false
isHydratingRef.current = true
prevMessageCount.current = 0
setIsLoadingThread(true)
handleReset()
// Prevent the continue-thread effect from misinterpreting this reset
// as the user continuing a completed thread
prevShowSummary.current = false
persistence.loadThread(threadParam).then((thread) => {
if (!thread) {
isHydratingRef.current = false
threadLoaded.current = null
setIsLoadingThread(false)
return
}
// Rebuild client messages from DB records
const messages: Message[] = thread.messages.map((m: { id: string; sender: string; displayName: string; content: string; createdAt: string }) => ({
id: `db-${m.id}`,
sender: m.sender as Message["sender"],
displayName: m.displayName,
content: m.content,
timestamp: new Date(m.createdAt),
}))
// Inject verdict data into verdict messages
for (const verdict of thread.verdicts) {
const verdictMsg = messages.find(
(m, i) => m.sender === "verdict" && i >= verdict.afterMessageIndex
)
if (verdictMsg) {
verdictMsg.verdictData = {
recommendedAnswer: verdict.recommendation,
voteSplit: verdict.voteSplit,
confidence: verdict.confidence,
reasons: verdict.reasons,
minorityView: verdict.minorityView,
oppositeCase: verdict.oppositeCase,
}
}
}
// Find the last verdict for the state
const lastVerdict = thread.verdicts[thread.verdicts.length - 1]
const verdictResult: VerdictResult | null = lastVerdict
? {
recommendedAnswer: lastVerdict.recommendation,
voteSplit: lastVerdict.voteSplit,
confidence: lastVerdict.confidence,
reasons: lastVerdict.reasons,
minorityView: lastVerdict.minorityView,
oppositeCase: lastVerdict.oppositeCase,
}
: null
// Set config from thread
if (thread.models?.length) dispatch({ type: "SET_MODELS", models: thread.models })
if (thread.responseLength) setResponseLength(thread.responseLength)
if (thread.rounds) setMaxRounds(thread.rounds)
if (thread.locale) setLocale(thread.locale)
// Hydrate debate engine
dispatch({
type: "HYDRATE_THREAD",
messages,
verdict: verdictResult,
showSummary: thread.status === "complete",
})
dispatch({ type: "SET_THREAD_ID", id: thread.id })
prevMessageCount.current = messages.length
setTimeout(() => { isHydratingRef.current = false; setIsLoadingThread(false) }, 0)
}).catch((err) => {
console.error("[thread] Failed to load thread:", err)
isHydratingRef.current = false
threadLoaded.current = null
setIsLoadingThread(false)
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [threadParam, persistence.isLoggedIn, handleReset])
// Increment free-debate counter after verdict (for login gate)
const hasIncrementedRef = useRef(false)
useEffect(() => {
if (state.showSummary && !persistence.isLoggedIn && !hasIncrementedRef.current) {
hasIncrementedRef.current = true
incrementDebateCount()
}
if (!state.showSummary) {
hasIncrementedRef.current = false
}
}, [state.showSummary, persistence.isLoggedIn])
/* ---- Render ---- */
return (
<div key={mountKey} className="relative flex flex-col h-screen bg-background overflow-hidden font-[family-name:var(--font-geist-sans)] text-foreground transition-colors duration-200">
<ChatHeader
currentRound={state.currentRound}
maxRounds={maxRounds}
responseLength={responseLength}
onChangeResponseLength={(len) => { setResponseLength(len); localStorage.setItem("quorum_responseLength", len) }}
locale={locale}
theme={theme}
onToggleTheme={toggleTheme}
onOpenSettings={() => setIsSettingsOpen(true)}
isDebating={state.isDebating}
threadTitle={currentTitle}
threadId={state.threadId}
onNewDebate={handleNewDebate}
onDeleteCurrent={handleNewDebate}
/>
<SettingsModal
isOpen={isSettingsOpen}
onClose={() => setIsSettingsOpen(false)}
locale={locale}
onToggleLocale={() => setLocale((l) => {
const next = l === "en" ? "ko" : "en"
localStorage.setItem("quorum_locale", next)
return next
})}
activeModels={state.activeModels}
onToggleModel={(m) => dispatch({ type: "TOGGLE_MODEL", model: m })}
maxRounds={maxRounds}
onChangeRounds={(r) => { setMaxRounds(r); localStorage.setItem("quorum_rounds", String(r)) }}
isDebating={state.isDebating}
theme={theme}
onChangeTheme={changeTheme}
/>
{/* Scrollable message area */}
<main
ref={mainRef}
className="flex-1 min-h-0 overflow-y-auto relative thin-scrollbar scroll-smooth"
onScroll={() => {
const el = mainRef.current
if (!el) return
const distFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight
setShowScrollDown(distFromBottom > 200)
}}
>
{isLoadingThread ? (
<div className="flex-1 flex items-center justify-center h-full">
<div className="flex flex-col items-center gap-3">
<div className="w-5 h-5 border-2 border-zinc-300 dark:border-zinc-600 border-t-zinc-600 dark:border-t-zinc-300 rounded-full animate-spin" />
<span className="text-sm text-zinc-400 dark:text-zinc-500">
{locale === "ko" ? "불러오는 중..." : "Loading..."}
</span>
</div>
</div>
) : (
<ChatThread
messages={state.messages}
typingModel={state.typingModel}
locale={locale}
activeModels={state.activeModels}
onSendMessage={(text) => handleSend(text, "all")}
onNewDiscussion={handleNewDebate}
/>
)}
</main>
<AnimatePresence>
{showScrollDown && (
<motion.button
initial={{ opacity: 0, y: 5 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 5 }}
onClick={() => mainRef.current?.scrollTo({ top: mainRef.current.scrollHeight, behavior: "smooth" })}
className="absolute left-1/2 -translate-x-1/2 bottom-20 sm:bottom-24 w-8 h-8 rounded-full bg-zinc-500/20 dark:bg-zinc-400/15 backdrop-blur-sm text-zinc-500 dark:text-zinc-400 flex items-center justify-center hover:bg-zinc-500/30 dark:hover:bg-zinc-400/25 transition-colors z-30"
>
<ChevronDown className="w-4 h-4" />
</motion.button>
)}
</AnimatePresence>
{/* Bottom bar: consensus rail + input */}
<div className="w-full shrink-0 bg-gradient-to-t from-[var(--background)] via-[var(--background)] to-transparent pt-4 z-10">
<AnimatePresence>
{(state.isDebating || state.typingModel !== null || state.verdict !== null) && (
<ConsensusMeter
score={state.verdict?.confidence ?? null}
result={state.showSummary ? state.verdict : null}
locale={locale}
/>
)}
</AnimatePresence>
<MessageInput
onSend={handleSend}
onStop={handleStop}
disabled={state.isDebating}
locale={locale}
initialFileWarning={fileWarning}
/>
</div>
</div>
)
}