import { useState, useRef, useCallback, useEffect } from 'react' import { generateReport } from '../api' import PipelineFlow from '../components/PipelineFlow' import RadarChart from '../components/RadarChart' import ReportCard from '../components/ReportCard' const INITIAL_STEPS = [ { id: 'agent1', label: '简历解析', status: 'idle' }, { id: 'agent2', label: '匹配诊断', status: 'idle' }, { id: 'agent3', label: '报告生成', status: 'idle' }, ] function DiagnosePage({ jobId, jobData, resumeData, onComplete, onBack, onGoHome }) { const [pageStatus, setPageStatus] = useState('idle') const [steps, setSteps] = useState(INITIAL_STEPS) const [viewType, setViewType] = useState('student') const [jdParsed, setJdParsed] = useState(null) const [matchReport, setMatchReport] = useState(null) const [advice, setAdvice] = useState(null) const [errorMsg, setErrorMsg] = useState('') const abortRef = useRef(null) const retryCountRef = useRef(0) const retryTimeoutRef = useRef(null) const jobInfo = jobData || { name: '岗位名称', company: '公司名称', city: '', salary: '', tags: [], description: '', requirements: '', expectation_signals: [] } const updateStep = useCallback((agentId, newStatus) => { setSteps((prev) => prev.map((s) => (s.id === agentId ? { ...s, status: newStatus } : s))) }, []) const handleAnalyze = useCallback(async () => { setPageStatus('loading') setSteps(INITIAL_STEPS.map((s) => ({ ...s, status: 'idle' }))) setJdParsed(null) setMatchReport(null) setAdvice(null) retryCountRef.current = 0 const controller = new AbortController() abortRef.current = controller const doFetch = async () => { try { const response = await generateReport(jobId, resumeData) if (!response.ok) throw new Error('请求失败') const reader = response.body.getReader() const decoder = new TextDecoder() let buffer = '' while (true) { const { done, value } = await reader.read() if (done) break buffer += decoder.decode(value, { stream: true }) const lines = buffer.split('\n') buffer = lines.pop() || '' for (const line of lines) { const trimmed = line.trim() if (!trimmed) continue const jsonStr = trimmed.startsWith('data: ') ? trimmed.slice(6).trim() : trimmed if (!jsonStr) continue try { const event = JSON.parse(jsonStr) const { type, agent, data } = event if (type === 'agent_start') { updateStep(agent, 'running') } else if (type === 'agent_complete') { updateStep(agent, 'completed') if (agent === 'agent1' && data) setJdParsed(data) if (agent === 'agent2' && data) setMatchReport(data) if (agent === 'agent3' && data) setAdvice(data) } else if (type === 'report_complete') { if (data) { if (data.jd_parsed) setJdParsed(data.jd_parsed) if (data.match_report) setMatchReport(data.match_report) if (data.advice) setAdvice(data.advice) } } else if (type === 'error') { updateStep(agent || 'agent3', 'error') } } catch { // skip } } } setPageStatus('success') } catch (err) { if (err.name === 'AbortError') return retryCountRef.current += 1 if (retryCountRef.current <= 3) { const delay = retryCountRef.current * 1000 retryTimeoutRef.current = setTimeout(doFetch, delay) } else { setPageStatus('error') setErrorMsg('连接中断,请刷新页面') } } } doFetch() }, [jobId, updateStep]) const abort = useCallback(() => { clearTimeout(retryTimeoutRef.current) abortRef.current?.abort() setPageStatus('idle') }, []) useEffect(() => { return () => { clearTimeout(retryTimeoutRef.current) abortRef.current?.abort() } }, []) const studentDims = matchReport?.dimensions?.map(d => d.name) || [] const studentScores = matchReport?.dimensions?.map(d => d.score) || [] const studentEvidence = matchReport?.dimensions?.map(d => d.evidence) || [] const hrViewData = matchReport?.hr_view || {} const showResults = pageStatus === 'loading' || pageStatus === 'success' || pageStatus === 'error' const analyzeDone = pageStatus === 'success' if (!resumeData) { return (
请先上传或填写简历,才能进行匹配诊断
{jobInfo.company} · {jobInfo.city || ''} {jobInfo.salary ? '· ' + jobInfo.salary : ''}
{jobInfo.description}
{jobInfo.requirements}
{signal.evidence || ''}
分析将由 3 个 AI Agent 依次完成:
提取岗位核心要求、技术栈、加分项、文化信号
多维度拆解匹配度,输出分数和引用证据
生成简历修改建议和面试准备方向
{errorMsg}