Fix SSE route dependency and align architecture docs

This commit is contained in:
ash66
2026-05-18 16:32:42 +08:00
parent 86b9ac806a
commit 3f69cad404
149 changed files with 4786 additions and 5957 deletions

View File

@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useTheme } from '../../contexts/ThemeContext';
import React, { useRef, useState } from 'react';
import { useTheme } from '../../contexts';
import type { UploadedDoc, ComplianceChunk, Regulation, SegmentRisk, RiskDashboardData } from '../../types';
import {
mockComplianceChunks,
@@ -82,9 +82,11 @@ const getRegsByCategory = (regulations: Regulation[]) => {
export const CompliancePage: React.FC = () => {
const { theme, isDark } = useTheme();
const nextMessageIdRef = useRef(1);
// Upload & Analysis States
const [uploadedDoc, setUploadedDoc] = useState<UploadedDoc | null>(null);
const [uploadedFile, setUploadedFile] = useState<File | null>(null);
const [isAnalyzing, setIsAnalyzing] = useState<boolean>(false);
const [analyzeStep, setAnalyzeStep] = useState<number>(0);
const [analyzePercent, setAnalyzePercent] = useState<number>(0);
@@ -100,10 +102,17 @@ export const CompliancePage: React.FC = () => {
const [chatLoading, setChatLoading] = useState<boolean>(false);
const [dashboardExpanded, setDashboardExpanded] = useState<boolean>(false);
const nextMessageId = () => {
const currentId = nextMessageIdRef.current;
nextMessageIdRef.current += 1;
return currentId;
};
// Handlers
const handleUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
setUploadedFile(file);
setUploadedDoc({
name: file.name,
size: `${(file.size / 1024 / 1024).toFixed(2)}MB`,
@@ -113,27 +122,14 @@ export const CompliancePage: React.FC = () => {
};
const startAnalysis = async () => {
if (!uploadedDoc) return;
if (!uploadedDoc || !uploadedFile) return;
setIsAnalyzing(true);
setAnalyzeStep(1);
setAnalyzePercent(0);
try {
// Get file from upload input
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
const file = fileInput?.files?.[0];
console.log("123")
// if (!file) {
// // setIsAnalyzing(false);
// // return;
// }
console.log("456")
// Upload and get task ID
const uploadRes = await analyzeDocument(file);
const uploadRes = await analyzeDocument(uploadedFile);
// Simulate progress
setTimeout(() => {
@@ -174,7 +170,7 @@ export const CompliancePage: React.FC = () => {
regulations: s.regulations.map(r => ({
id: r.id,
name: r.name,
clause: r.clause,
clause: r.clause || '',
score: r.score,
matchKeyword: r.match_keyword,
category: r.category as 'high' | 'medium' | 'low',
@@ -204,7 +200,6 @@ export const CompliancePage: React.FC = () => {
}
} catch (error) {
console.error('Failed to get compliance result:', error);
// Fallback to mock data
setChunks(mockComplianceChunks);
}
@@ -215,7 +210,6 @@ export const CompliancePage: React.FC = () => {
} catch (error) {
console.error('Failed to analyze document:', error);
setIsAnalyzing(false);
// Fallback to mock data after delay
setTimeout(() => {
setChunks(mockComplianceChunks);
}, 4500);
@@ -230,7 +224,7 @@ export const CompliancePage: React.FC = () => {
setChatMessages(prev => ({
...prev,
[chunkId]: [{
id: Date.now(),
id: nextMessageId(),
role: 'assistant',
content: `您好!我是法规合规分析助手。当前段落涉及 ${chunk?.regulations.length} 条相关法规,您可以询问合规性评估、法规解读或修改建议。`,
}]
@@ -248,7 +242,7 @@ export const CompliancePage: React.FC = () => {
const chunk = chunks.find(c => c.id === activeChunkId);
if (!chunk) return;
const userMsg = { id: Date.now(), role: 'user' as const, content: chatInput };
const userMsg = { id: nextMessageId(), role: 'user' as const, content: chatInput };
setChatMessages(prev => ({
...prev,
[activeChunkId]: [...(prev[activeChunkId] || []), userMsg],
@@ -267,7 +261,7 @@ export const CompliancePage: React.FC = () => {
currentResponse += sseData.text;
setChatMessages(prev => ({
...prev,
[activeChunkId]: [...(prev[activeChunkId] || []).slice(0, -1), { id: Date.now() + 1, role: 'assistant', content: currentResponse }],
[activeChunkId]: [...(prev[activeChunkId] || []).slice(0, -1), { id: nextMessageId(), role: 'assistant', content: currentResponse }],
}));
} else if (sseData.type === 'done') {
setChatLoading(false);
@@ -291,7 +285,7 @@ export const CompliancePage: React.FC = () => {
}
setChatMessages(prev => ({
...prev,
[activeChunkId]: [...(prev[activeChunkId] || []), { id: Date.now() + 1, role: 'assistant', content: response }],
[activeChunkId]: [...(prev[activeChunkId] || []), { id: nextMessageId(), role: 'assistant', content: response }],
}));
},
() => {
@@ -1913,4 +1907,4 @@ export const CompliancePage: React.FC = () => {
)}
</div>
);
};
};