2026-05-21 23:20:39 +08:00
|
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
2026-05-18 16:32:42 +08:00
|
|
|
|
import { useTheme } from '../../contexts';
|
2026-05-14 15:07:34 +08:00
|
|
|
|
import type { ChatMessage, RetrievalData } from '../../types';
|
|
|
|
|
|
import { getQuickQuestions, ragChat } from '../../api/rag';
|
2026-05-20 23:34:08 +08:00
|
|
|
|
import { CitedAnswer } from './CitedAnswer';
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
|
|
const ragQuickQuestionsDefault = [
|
|
|
|
|
|
'电动自行车上路需要什么条件?',
|
|
|
|
|
|
'驾驶证如何申请?',
|
|
|
|
|
|
'超速行驶如何处罚?',
|
|
|
|
|
|
'车辆年检有哪些规定?',
|
|
|
|
|
|
'电动汽车电池安全标准?',
|
|
|
|
|
|
'正面碰撞测试要求?',
|
|
|
|
|
|
'AEB系统测试标准?',
|
|
|
|
|
|
'高速公路安全距离?',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
export const RagChatPage: React.FC = () => {
|
|
|
|
|
|
const { theme } = useTheme();
|
2026-05-18 16:32:42 +08:00
|
|
|
|
const nextMessageIdRef = useRef(1);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
// retrievals: right-panel shows sources of the most recent assistant reply
|
2026-05-14 15:07:34 +08:00
|
|
|
|
const [retrievals, setRetrievals] = useState<RetrievalData[]>([]);
|
|
|
|
|
|
const [input, setInput] = useState<string>('');
|
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
const [showClearConfirm, setShowClearConfirm] = useState<boolean>(false);
|
|
|
|
|
|
const [selectedRetrieval, setSelectedRetrieval] = useState<RetrievalData | null>(null);
|
|
|
|
|
|
const [quickQuestions, setQuickQuestions] = useState<string[]>(ragQuickQuestionsDefault);
|
2026-05-20 23:34:08 +08:00
|
|
|
|
const [filterRegulationType, setFilterRegulationType] = useState<string>('');
|
|
|
|
|
|
const [highlightedSourceIdx, setHighlightedSourceIdx] = useState<number | null>(null);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const [sessionId, setSessionId] = useState<string | undefined>();
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-scroll ref
|
|
|
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
// AbortController for cancelling in-flight requests
|
|
|
|
|
|
const abortRef = useRef<AbortController | null>(null);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-18 16:32:42 +08:00
|
|
|
|
function nextMessageId() {
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const id = nextMessageIdRef.current;
|
2026-05-18 16:32:42 +08:00
|
|
|
|
nextMessageIdRef.current += 1;
|
2026-05-21 23:20:39 +08:00
|
|
|
|
return id;
|
2026-05-18 16:32:42 +08:00
|
|
|
|
}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
// Scroll to bottom whenever messages change
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
2026-05-18 16:32:42 +08:00
|
|
|
|
async function loadQuickQuestions() {
|
2026-05-14 15:07:34 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const response = await getQuickQuestions();
|
|
|
|
|
|
setQuickQuestions(response.questions.map(q => q.question));
|
2026-05-21 23:20:39 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
// keep defaults
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}
|
2026-05-18 16:32:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const timerId = window.setTimeout(() => { void loadQuickQuestions(); }, 0);
|
2026-05-18 16:32:42 +08:00
|
|
|
|
return () => window.clearTimeout(timerId);
|
|
|
|
|
|
}, []);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Core query executor — shared by sendMessage and regenerateLastAnswer.
|
|
|
|
|
|
* Manages session_id, AbortController, SSE parsing, and state updates.
|
|
|
|
|
|
*/
|
|
|
|
|
|
const executeQuery = useCallback((text: string) => {
|
|
|
|
|
|
// Cancel any in-flight request
|
|
|
|
|
|
abortRef.current?.abort();
|
|
|
|
|
|
abortRef.current = new AbortController();
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-20 23:34:08 +08:00
|
|
|
|
const activeFilters = filterRegulationType.trim() || undefined;
|
2026-05-21 23:20:39 +08:00
|
|
|
|
let currentResponse = '';
|
|
|
|
|
|
// Capture the assistant message id so we can attach sources later
|
|
|
|
|
|
let assistantMsgId: number | null = null;
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
|
|
void ragChat(
|
|
|
|
|
|
text,
|
|
|
|
|
|
5,
|
2026-05-21 23:20:39 +08:00
|
|
|
|
(data) => {
|
|
|
|
|
|
if (data.type === 'session' && data.session_id) {
|
|
|
|
|
|
setSessionId(data.session_id);
|
|
|
|
|
|
} else if (data.type === 'retrieved' && data.docs) {
|
|
|
|
|
|
const docs: RetrievalData[] = data.docs.map(d => ({
|
2026-05-14 15:07:34 +08:00
|
|
|
|
id: parseInt(d.id.replace('chunk-', ''), 10) || 1,
|
|
|
|
|
|
file: d.doc_name,
|
|
|
|
|
|
clause: d.clause,
|
|
|
|
|
|
score: d.score,
|
|
|
|
|
|
content: d.preview,
|
|
|
|
|
|
docId: d.doc_id,
|
|
|
|
|
|
downloadUrl: d.download_url,
|
|
|
|
|
|
}));
|
2026-05-21 23:20:39 +08:00
|
|
|
|
setRetrievals(docs);
|
|
|
|
|
|
// Attach sources to the assistant message once we know its id
|
|
|
|
|
|
if (assistantMsgId !== null) {
|
|
|
|
|
|
setMessages(prev => prev.map(m =>
|
|
|
|
|
|
m.id === assistantMsgId ? { ...m, sources: docs } : m
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (data.type === 'chunk' && data.text) {
|
|
|
|
|
|
currentResponse += data.text;
|
|
|
|
|
|
setMessages(prev => {
|
|
|
|
|
|
const last = prev[prev.length - 1];
|
|
|
|
|
|
if (last?.role === 'assistant' && last.id === assistantMsgId) {
|
|
|
|
|
|
return [...prev.slice(0, -1), { ...last, content: currentResponse }];
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
// First chunk: create assistant message
|
|
|
|
|
|
const newId = nextMessageId();
|
|
|
|
|
|
assistantMsgId = newId;
|
|
|
|
|
|
return [...prev, { id: newId, role: 'assistant' as const, content: currentResponse }];
|
2026-05-14 15:07:34 +08:00
|
|
|
|
});
|
2026-05-21 23:20:39 +08:00
|
|
|
|
} else if (data.type === 'done') {
|
|
|
|
|
|
if (data.session_id) setSessionId(data.session_id);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
setLoading(false);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
} else if (data.type === 'error') {
|
2026-05-14 15:07:34 +08:00
|
|
|
|
setLoading(false);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
setMessages(prev => [
|
|
|
|
|
|
...prev,
|
|
|
|
|
|
{ id: nextMessageId(), role: 'assistant' as const, content: '抱歉,生成回答时出错,请稍后再试。' },
|
|
|
|
|
|
]);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2026-05-21 23:20:39 +08:00
|
|
|
|
(error) => {
|
2026-05-14 15:07:34 +08:00
|
|
|
|
console.error('RAG chat error:', error);
|
|
|
|
|
|
setLoading(false);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
setMessages(prev => [
|
2026-05-14 15:07:34 +08:00
|
|
|
|
...prev,
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{ id: nextMessageId(), role: 'assistant' as const, content: '抱歉,连接服务器时出错,请稍后再试。' },
|
2026-05-14 15:07:34 +08:00
|
|
|
|
]);
|
|
|
|
|
|
},
|
2026-05-21 23:20:39 +08:00
|
|
|
|
() => { setLoading(false); },
|
|
|
|
|
|
activeFilters,
|
|
|
|
|
|
sessionId,
|
|
|
|
|
|
abortRef.current.signal,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
|
}, [filterRegulationType, sessionId]);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const sendMessage = (text: string) => {
|
|
|
|
|
|
if (!text.trim() || loading) return;
|
|
|
|
|
|
setMessages(prev => [...prev, { id: nextMessageId(), role: 'user' as const, content: text }]);
|
|
|
|
|
|
setInput('');
|
|
|
|
|
|
setLoading(true);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
setRetrievals([]);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
setHighlightedSourceIdx(null);
|
|
|
|
|
|
executeQuery(text);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const regenerateLastAnswer = () => {
|
2026-05-21 23:20:39 +08:00
|
|
|
|
if (loading) return;
|
|
|
|
|
|
const lastUserMsg = [...messages].reverse().find(m => m.role === 'user');
|
2026-05-14 15:07:34 +08:00
|
|
|
|
if (!lastUserMsg) return;
|
2026-05-21 23:20:39 +08:00
|
|
|
|
// Remove the last assistant message
|
|
|
|
|
|
setMessages(prev => {
|
|
|
|
|
|
const lastAssistantIdx = [...prev].reverse().findIndex(m => m.role === 'assistant');
|
|
|
|
|
|
if (lastAssistantIdx === -1) return prev;
|
|
|
|
|
|
const idx = prev.length - 1 - lastAssistantIdx;
|
|
|
|
|
|
return [...prev.slice(0, idx)];
|
|
|
|
|
|
});
|
2026-05-14 15:07:34 +08:00
|
|
|
|
setLoading(true);
|
|
|
|
|
|
setRetrievals([]);
|
2026-05-20 23:34:08 +08:00
|
|
|
|
setHighlightedSourceIdx(null);
|
2026-05-21 23:20:39 +08:00
|
|
|
|
executeQuery(lastUserMsg.content);
|
|
|
|
|
|
};
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const clearMessages = () => {
|
|
|
|
|
|
abortRef.current?.abort();
|
|
|
|
|
|
setMessages([]);
|
|
|
|
|
|
setRetrievals([]);
|
|
|
|
|
|
setSessionId(undefined);
|
|
|
|
|
|
setShowClearConfirm(false);
|
|
|
|
|
|
setLoading(false);
|
2026-05-14 15:07:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ flex: 1, display: 'flex', height: 'calc(100vh - 128px)' }}>
|
|
|
|
|
|
{/* ── Left: chat panel ─────────────────────────────────── */}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{
|
|
|
|
|
|
flex: '0 0 60%',
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
|
borderRight: `1px solid ${theme.border}`,
|
|
|
|
|
|
}}>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* Message list */}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{
|
|
|
|
|
|
flex: 1,
|
|
|
|
|
|
overflowY: 'auto',
|
|
|
|
|
|
padding: '24px 32px',
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
|
gap: 20,
|
|
|
|
|
|
}}>
|
|
|
|
|
|
{messages.length === 0 ? (
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ textAlign: 'center', padding: 60, color: theme.text3 }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 72, height: 72, borderRadius: 16,
|
|
|
|
|
|
background: theme.bgCard, display: 'flex', alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'center', margin: '0 auto 20px',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
border: `1px solid ${theme.border}`,
|
|
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="28" height="28" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z" stroke={theme.accent} strokeWidth="1.5"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div style={{ fontSize: 15, fontWeight: 500, marginBottom: 6, color: theme.text }}>开始法规对话</div>
|
|
|
|
|
|
<div className="mono" style={{ fontSize: 11 }}>选择快捷问题或输入您的问题</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
messages.map(msg => (
|
|
|
|
|
|
<div key={msg.id} style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
display: 'flex', gap: 12,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
flexDirection: msg.role === 'user' ? 'row-reverse' : 'row',
|
|
|
|
|
|
}}>
|
|
|
|
|
|
{msg.role === 'assistant' && (
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 32, height: 32, borderRadius: 8,
|
|
|
|
|
|
background: theme.gradientAccent, display: 'flex',
|
|
|
|
|
|
alignItems: 'center', justifyContent: 'center', flexShrink: 0,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<circle cx="12" cy="12" r="6" fill="#fff"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<div style={{
|
|
|
|
|
|
maxWidth: '80%',
|
|
|
|
|
|
padding: msg.role === 'user' ? '12px 18px' : '14px 18px',
|
|
|
|
|
|
background: msg.role === 'user' ? theme.gradientAccent : theme.bgCard,
|
|
|
|
|
|
borderRadius: 12,
|
|
|
|
|
|
color: msg.role === 'user' ? '#fff' : theme.text,
|
2026-05-21 23:20:39 +08:00
|
|
|
|
fontSize: 14, lineHeight: 1.6, whiteSpace: 'pre-wrap',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
border: msg.role === 'assistant' ? `1px solid ${theme.border}` : 'none',
|
|
|
|
|
|
}}>
|
2026-05-20 23:34:08 +08:00
|
|
|
|
{msg.role === 'assistant' ? (
|
|
|
|
|
|
<CitedAnswer
|
|
|
|
|
|
text={msg.content}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
sources={msg.sources ?? retrievals}
|
2026-05-20 23:34:08 +08:00
|
|
|
|
onCiteClick={(idx) => {
|
2026-05-21 23:20:39 +08:00
|
|
|
|
const msgSources = msg.sources ?? retrievals;
|
|
|
|
|
|
setRetrievals(msgSources);
|
2026-05-20 23:34:08 +08:00
|
|
|
|
setHighlightedSourceIdx(idx);
|
|
|
|
|
|
const el = document.getElementById(`source-${idx}`);
|
|
|
|
|
|
if (el) el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
) : msg.content}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
{msg.role === 'assistant' && msg.retrievalIds && msg.retrievalIds.length > 0 && (
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
marginTop: 10, paddingTop: 10,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
borderTop: `1px solid ${theme.border}`,
|
2026-05-21 23:20:39 +08:00
|
|
|
|
display: 'flex', alignItems: 'center', gap: 6,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<path d="M14 2H6C5 2 4 3 4 4V20C4 21 5 22 6 22H18C19 22 20 21 20 20V8L14 2Z" stroke={theme.accent} strokeWidth="1.5"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
<span className="mono" style={{ fontSize: 11, color: theme.accent }}>
|
|
|
|
|
|
{msg.retrievalIds.length} 个法规引用
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))
|
|
|
|
|
|
)}
|
|
|
|
|
|
{loading && (
|
|
|
|
|
|
<div style={{ display: 'flex', gap: 12 }}>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 32, height: 32, borderRadius: 8,
|
|
|
|
|
|
background: theme.gradientAccent, display: 'flex',
|
|
|
|
|
|
alignItems: 'center', justifyContent: 'center',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<circle cx="12" cy="12" r="6" fill="#fff"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '14px 18px', background: theme.bgCard,
|
|
|
|
|
|
borderRadius: 12, border: `1px solid ${theme.border}`,
|
|
|
|
|
|
display: 'flex', alignItems: 'center', gap: 8,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 6, height: 6, borderRadius: '50%',
|
|
|
|
|
|
background: theme.accent, animation: 'pulse 1s infinite',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}} />
|
|
|
|
|
|
<span style={{ fontSize: 13, color: theme.text2 }}>检索中...</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* Scroll anchor */}
|
|
|
|
|
|
<div ref={messagesEndRef} />
|
2026-05-14 15:07:34 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* Input area */}
|
|
|
|
|
|
<div style={{ padding: '16px 32px 20px', background: theme.bg, borderTop: `1px solid ${theme.border}` }}>
|
|
|
|
|
|
{/* Filter row */}
|
|
|
|
|
|
<div style={{ display: 'flex', gap: 8, marginBottom: 10, alignItems: 'center' }}>
|
2026-05-20 23:34:08 +08:00
|
|
|
|
<span className="mono" style={{ fontSize: 11, color: theme.text3, whiteSpace: 'nowrap' }}>法规类型</span>
|
|
|
|
|
|
<input
|
|
|
|
|
|
value={filterRegulationType}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
onChange={e => setFilterRegulationType(e.target.value)}
|
2026-05-20 23:34:08 +08:00
|
|
|
|
placeholder="如: GB / UN-ECE / IATF(留空不过滤)"
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
flex: 1, maxWidth: 280, padding: '5px 10px', fontSize: 12,
|
|
|
|
|
|
background: theme.bgHover, border: `1px solid ${theme.border}`,
|
|
|
|
|
|
borderRadius: 6, color: theme.text, outline: 'none',
|
2026-05-20 23:34:08 +08:00
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* Quick questions */}
|
|
|
|
|
|
<div style={{ display: 'flex', gap: 8, marginBottom: 12, flexWrap: 'wrap' }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
{quickQuestions.map(q => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={q}
|
|
|
|
|
|
onClick={() => sendMessage(q)}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
disabled={loading}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '6px 14px', fontSize: 12, background: theme.bgCard,
|
|
|
|
|
|
border: `1px solid ${theme.border}`, borderRadius: 6,
|
|
|
|
|
|
color: theme.text2, cursor: loading ? 'not-allowed' : 'pointer',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>{q}</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* Send row */}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{ display: 'flex', gap: 10 }}>
|
|
|
|
|
|
<input
|
|
|
|
|
|
value={input}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
onChange={e => setInput(e.target.value)}
|
|
|
|
|
|
onKeyDown={e => e.key === 'Enter' && !e.shiftKey && sendMessage(input)}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
placeholder="输入法规问题..."
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
flex: 1, padding: 12, fontSize: 14,
|
|
|
|
|
|
background: theme.bgCard, border: `1px solid ${theme.border}`,
|
|
|
|
|
|
borderRadius: 8, color: theme.text, outline: 'none',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => sendMessage(input)}
|
|
|
|
|
|
disabled={loading || !input.trim()}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '12px 24px', fontSize: 14, fontWeight: 600,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
background: loading || !input.trim() ? theme.bgHover : theme.gradientAccent,
|
|
|
|
|
|
color: loading || !input.trim() ? theme.text3 : '#fff',
|
2026-05-21 23:20:39 +08:00
|
|
|
|
border: 'none', borderRadius: 8,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
cursor: loading || !input.trim() ? 'not-allowed' : 'pointer',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>发送</button>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{loading && (
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => { abortRef.current?.abort(); setLoading(false); }}
|
|
|
|
|
|
style={{
|
|
|
|
|
|
padding: '12px 16px', fontSize: 13, background: theme.bgCard,
|
|
|
|
|
|
border: `1px solid ${theme.border}`, borderRadius: 8,
|
|
|
|
|
|
color: theme.text2, cursor: 'pointer',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>停止</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{!loading && messages.length > 0 && (
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setShowClearConfirm(true)}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '12px 16px', fontSize: 13, background: theme.bgCard,
|
|
|
|
|
|
border: `1px solid ${theme.border}`, borderRadius: 8,
|
|
|
|
|
|
color: theme.text2, cursor: 'pointer',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>清空</button>
|
|
|
|
|
|
)}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{!loading && messages.filter(m => m.role === 'assistant').length > 0 && (
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={regenerateLastAnswer}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '12px 16px', fontSize: 13, background: theme.bgCard,
|
|
|
|
|
|
border: `1px solid ${theme.border}`, borderRadius: 8,
|
|
|
|
|
|
color: theme.text2, cursor: 'pointer',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>重生成</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* ── Right: retrieved sources panel ───────────────────── */}
|
|
|
|
|
|
<div style={{ flex: '0 0 40%', display: 'flex', flexDirection: 'column', background: theme.bgCard }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '20px 24px', borderBottom: `1px solid ${theme.border}`,
|
|
|
|
|
|
display: 'flex', alignItems: 'center', gap: 10,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 28, height: 28, borderRadius: 6,
|
|
|
|
|
|
background: theme.gradientAccent, display: 'flex',
|
|
|
|
|
|
alignItems: 'center', justifyContent: 'center',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<path d="M14 2H6C5 2 4 3 4 4V20C4 21 5 22 6 22H18C19 22 20 21 20 20V8L14 2Z" stroke="#fff" strokeWidth="1.5"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<span className="mono" style={{ fontSize: 12, fontWeight: 600, color: theme.accent, letterSpacing: '1px' }}>
|
|
|
|
|
|
RETRIEVED FRAGMENTS
|
|
|
|
|
|
</span>
|
|
|
|
|
|
{retrievals.length > 0 && (
|
|
|
|
|
|
<span className="mono" style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
fontSize: 11, padding: '4px 10px',
|
|
|
|
|
|
background: theme.bgHover, borderRadius: 4, color: theme.text3,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>{retrievals.length}</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ flex: 1, overflowY: 'auto', padding: '16px 24px' }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
{retrievals.length > 0 ? (
|
|
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
|
|
|
|
|
|
{retrievals.map((r, i) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
key={r.id}
|
2026-05-20 23:34:08 +08:00
|
|
|
|
id={`source-${i + 1}`}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
onClick={() => setSelectedRetrieval(r)}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: 16, background: highlightedSourceIdx === i + 1 ? theme.bgElevated : theme.bgHover,
|
|
|
|
|
|
borderRadius: 10, border: `1px solid ${highlightedSourceIdx === i + 1 ? theme.accent : theme.border}`,
|
|
|
|
|
|
cursor: 'pointer', position: 'relative',
|
2026-05-20 23:34:08 +08:00
|
|
|
|
transition: 'border-color 0.2s, background 0.2s',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
position: 'absolute', left: 0, top: 16, bottom: 16,
|
|
|
|
|
|
width: 3, background: theme.gradientAccent, borderRadius: 2,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}} />
|
|
|
|
|
|
<div style={{ paddingLeft: 8 }}>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
display: 'flex', alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'space-between', marginBottom: 8,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<span className="mono" style={{ fontSize: 11, fontWeight: 700, color: theme.accent }}>#{i + 1}</span>
|
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
|
|
|
|
|
|
{r.downloadUrl && (
|
|
|
|
|
|
<a
|
|
|
|
|
|
href={r.downloadUrl}
|
|
|
|
|
|
target="_blank"
|
|
|
|
|
|
rel="noreferrer"
|
2026-05-21 23:20:39 +08:00
|
|
|
|
onClick={e => e.stopPropagation()}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
style={{ fontSize: 11, color: theme.accent, textDecoration: 'none' }}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
>下载文档</a>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
)}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<span className="mono" style={{ fontSize: 11, fontWeight: 600, color: theme.accent }}>
|
|
|
|
|
|
{(r.score * 100).toFixed(0)}%
|
|
|
|
|
|
</span>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div style={{ fontSize: 13, fontWeight: 500, marginBottom: 4, color: theme.text }}>{r.file}</div>
|
|
|
|
|
|
<div className="mono" style={{ fontSize: 11, color: theme.text3, marginBottom: 8 }}>
|
|
|
|
|
|
{r.clause}{r.docId ? ` · ${r.docId}` : ''}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
fontSize: 12, color: theme.text2, lineHeight: 1.5,
|
|
|
|
|
|
display: '-webkit-box', WebkitLineClamp: 3,
|
|
|
|
|
|
WebkitBoxOrient: 'vertical', overflow: 'hidden',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>{r.content}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ textAlign: 'center', padding: 40, color: theme.text3 }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 48, height: 48, borderRadius: 10,
|
|
|
|
|
|
background: theme.bgHover, display: 'flex', alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'center', margin: '0 auto 16px',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<path d="M14 2H6C5 2 4 3 4 4V20C4 21 5 22 6 22H18C19 22 20 21 20 20V8L14 2Z" stroke={theme.text3} strokeWidth="1.5"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="mono" style={{ fontSize: 11 }}>对话后显示相关法规</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* ── Clear confirm modal ───────────────────────────────── */}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
{showClearConfirm && (
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
position: 'fixed', top: 0, left: 0, right: 0, bottom: 0,
|
|
|
|
|
|
background: 'rgba(0,0,0,0.6)', display: 'flex',
|
|
|
|
|
|
alignItems: 'center', justifyContent: 'center', zIndex: 1000,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: 24, background: theme.bgCard, borderRadius: 16,
|
|
|
|
|
|
maxWidth: 400, border: `1px solid ${theme.border}`,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<div style={{ fontSize: 15, fontWeight: 600, marginBottom: 12, color: theme.text }}>确定清空对话?</div>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ fontSize: 13, color: theme.text2, marginBottom: 20 }}>此操作不可恢复,会话历史将被重置</div>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<div style={{ display: 'flex', gap: 10, justifyContent: 'flex-end' }}>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setShowClearConfirm(false)}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '10px 18px', fontSize: 13, background: theme.bgHover,
|
|
|
|
|
|
border: 'none', borderRadius: 8, color: theme.text2, cursor: 'pointer',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>取消</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={clearMessages}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
padding: '10px 18px', fontSize: 13, fontWeight: 600,
|
|
|
|
|
|
background: theme.accent, border: 'none', borderRadius: 8,
|
|
|
|
|
|
color: '#fff', cursor: 'pointer',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>确认</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-05-21 23:20:39 +08:00
|
|
|
|
{/* ── Source detail modal ───────────────────────────────── */}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
{selectedRetrieval && (
|
|
|
|
|
|
<div
|
|
|
|
|
|
onClick={() => setSelectedRetrieval(null)}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
position: 'fixed', top: 0, left: 0, right: 0, bottom: 0,
|
|
|
|
|
|
background: 'rgba(0,0,0,0.6)', display: 'flex',
|
|
|
|
|
|
alignItems: 'center', justifyContent: 'center', zIndex: 1000,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
2026-05-21 23:20:39 +08:00
|
|
|
|
onClick={e => e.stopPropagation()}
|
2026-05-14 15:07:34 +08:00
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 520, maxWidth: '90%', maxHeight: '80%',
|
|
|
|
|
|
overflowY: 'auto', padding: 24, background: theme.bgCard,
|
|
|
|
|
|
borderRadius: 16, border: `1px solid ${theme.accent}`,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
boxShadow: '0 8px 32px rgba(0,0,0,0.3)',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
display: 'flex', alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'space-between', marginBottom: 16,
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}>
|
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 10, flexWrap: 'wrap' }}>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ padding: '4px 10px', background: theme.gradientAccent, borderRadius: 6 }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<span className="mono" style={{ fontSize: 11, fontWeight: 600, color: '#fff' }}>
|
|
|
|
|
|
{(selectedRetrieval.score * 100).toFixed(0)}%
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<span style={{ fontSize: 14, fontWeight: 600, color: theme.text }}>{selectedRetrieval.file}</span>
|
|
|
|
|
|
{selectedRetrieval.downloadUrl && (
|
|
|
|
|
|
<a
|
|
|
|
|
|
href={selectedRetrieval.downloadUrl}
|
|
|
|
|
|
target="_blank"
|
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
|
style={{ fontSize: 12, color: theme.accent, textDecoration: 'none' }}
|
2026-05-21 23:20:39 +08:00
|
|
|
|
>下载关联文档</a>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setSelectedRetrieval(null)}
|
|
|
|
|
|
style={{
|
2026-05-21 23:20:39 +08:00
|
|
|
|
width: 28, height: 28, background: theme.bgHover,
|
|
|
|
|
|
border: 'none', borderRadius: 6, cursor: 'pointer',
|
|
|
|
|
|
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
2026-05-14 15:07:34 +08:00
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
|
<path d="M18 6L6 18M6 6L18 18" stroke={theme.text3} strokeWidth="2" strokeLinecap="round"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ padding: '10px 14px', background: theme.bgHover, borderRadius: 8, marginBottom: 16 }}>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
<span className="mono" style={{ fontSize: 12, color: theme.accent }}>{selectedRetrieval.clause}</span>
|
|
|
|
|
|
{selectedRetrieval.docId && (
|
|
|
|
|
|
<span className="mono" style={{ fontSize: 11, color: theme.text3, marginLeft: 8 }}>{selectedRetrieval.docId}</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2026-05-21 23:20:39 +08:00
|
|
|
|
<div style={{ fontSize: 14, lineHeight: 1.7, color: theme.text2, whiteSpace: 'pre-wrap' }}>
|
|
|
|
|
|
{selectedRetrieval.content}
|
|
|
|
|
|
</div>
|
2026-05-14 15:07:34 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|