Spaces:
Sleeping
Sleeping
Download src/pages/DiagnosePage.jsx from tryprogram/offer-puplayer: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/spaces/tryprogram/offer-puplayer/resolve/main/src/pages/DiagnosePage.jsx
- Command line
-
hf download hf://spaces/tryprogram/offer-puplayer/src/pages/DiagnosePage.jsx
-
curl -L -o DiagnosePage.jsx https://huggingface.co/spaces/tryprogram/offer-puplayer/resolve/main/src/pages/DiagnosePage.jsx
10.9 kB
| 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 ( | |
| <div className="empty-state card"> | |
| <h2 className="font-bold text-xl mb-16">匹配诊断</h2> | |
| <p className="text-secondary mb-24">请先上传或填写简历,才能进行匹配诊断</p> | |
| <div className="flex-center gap-12"> | |
| <button className="btn btn-primary" onClick={onGoHome}>上传简历</button> | |
| <button className="btn btn-secondary" onClick={onGoHome}>手工填写</button> | |
| </div> | |
| <div className="mt-16"> | |
| <button className="btn btn-secondary" onClick={onBack}>返回推荐</button> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| return ( | |
| <div> | |
| <div className="card mb-24"> | |
| <div className="flex-between mb-8 flex-wrap"> | |
| <div className="min-width-0 flex-1"> | |
| <h2 className="font-bold text-xl">{jobInfo.name}</h2> | |
| <p className="text-secondary">{jobInfo.company} · {jobInfo.city || ''} {jobInfo.salary ? '· ' + jobInfo.salary : ''}</p> | |
| </div> | |
| <div className="flex gap-8 flex-wrap justify-end max-w-50p"> | |
| {(jobInfo.tags || []).map((tag, i) => ( | |
| <span key={i} className="tag">{tag}</span> | |
| ))} | |
| </div> | |
| </div> | |
| {jobInfo.description && ( | |
| <div className="mb-12"> | |
| <h4 className="font-bold text-sm mb-4">职位描述</h4> | |
| <p className="text-sm text-secondary whitespace-pre-wrap">{jobInfo.description}</p> | |
| </div> | |
| )} | |
| {jobInfo.requirements && ( | |
| <div className="mb-12"> | |
| <h4 className="font-bold text-sm mb-4">职位要求</h4> | |
| <p className="text-sm text-secondary whitespace-pre-wrap">{jobInfo.requirements}</p> | |
| </div> | |
| )} | |
| {jobInfo.expectation_signals && jobInfo.expectation_signals.length > 0 && ( | |
| <div className="mb-12"> | |
| <h4 className="font-bold text-sm mb-4">岗位画像</h4> | |
| <div className="flex flex-col gap-8"> | |
| {jobInfo.expectation_signals.map((signal, i) => { | |
| const pct = Math.round((signal.confidence || 0) * 100) | |
| const barColor = (signal.confidence || 0) > 0.7 | |
| ? 'var(--color-success)' | |
| : (signal.confidence || 0) > 0.4 | |
| ? 'var(--color-warning)' | |
| : 'var(--color-error)' | |
| return ( | |
| <div key={i} className="info-box"> | |
| <div className="flex-between mb-4"> | |
| <span className="text-sm font-bold">{signal.tag || signal.name}</span> | |
| <span className="text-xs text-light">可信度 {pct}%</span> | |
| </div> | |
| <p className="text-xs text-secondary mb-4">{signal.evidence || ''}</p> | |
| <div className="progress-track"> | |
| <div className="progress-bar" style={{ width: `${pct}%`, backgroundColor: barColor }} /> | |
| </div> | |
| </div> | |
| ) | |
| })} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| {pageStatus === 'idle' && ( | |
| <div className="card mb-24 bg-light"> | |
| <h4 className="font-bold mb-8">分析流程说明</h4> | |
| <p className="text-sm text-secondary mb-12">分析将由 3 个 AI Agent 依次完成:</p> | |
| <div className="flex flex-col gap-8"> | |
| <div className="card p-8 px-12"> | |
| <span className="text-sm font-bold">① JD 解析器</span> | |
| <p className="text-xs text-secondary mt-4">提取岗位核心要求、技术栈、加分项、文化信号</p> | |
| </div> | |
| <div className="card p-8 px-12"> | |
| <span className="text-sm font-bold">② 匹配诊断器</span> | |
| <p className="text-xs text-secondary mt-4">多维度拆解匹配度,输出分数和引用证据</p> | |
| </div> | |
| <div className="card p-8 px-12"> | |
| <span className="text-sm font-bold">③ 建议生成器</span> | |
| <p className="text-xs text-secondary mt-4">生成简历修改建议和面试准备方向</p> | |
| </div> | |
| </div> | |
| <button className="btn btn-primary mt-16" onClick={handleAnalyze}>开始分析</button> | |
| </div> | |
| )} | |
| {showResults && ( | |
| <> | |
| <div className="card mb-24"><PipelineFlow steps={steps} /></div> | |
| {pageStatus !== 'loading' && matchReport && ( | |
| <> | |
| <div className="flex gap-12 mb-24 flex-wrap"> | |
| <button className={`btn ${viewType === 'student' ? 'btn-primary' : 'btn-secondary'}`} onClick={() => setViewType('student')}>学生视角</button> | |
| <button className={`btn ${viewType === 'hr' ? 'btn-primary' : 'btn-secondary'}`} onClick={() => setViewType('hr')}>HR 视角</button> | |
| </div> | |
| {viewType === 'student' ? ( | |
| <> | |
| <div className="card mb-24"><RadarChart dimensions={studentDims} scores={studentScores} /></div> | |
| <div className="flex flex-col gap-16"> | |
| {studentDims.map((dim, i) => ( | |
| <ReportCard key={dim} dimension={dim} score={studentScores[i]} evidence={studentEvidence[i]} viewType={viewType} /> | |
| ))} | |
| </div> | |
| </> | |
| ) : ( | |
| <div className="flex flex-col gap-16"> | |
| <ReportCard dimension="HR 快速筛查模拟" viewType="hr" hrView={hrViewData} /> | |
| </div> | |
| )} | |
| </> | |
| )} | |
| {pageStatus === 'error' && ( | |
| <div className="error-state"> | |
| <p className="error-state-text">{errorMsg}</p> | |
| <button className="btn btn-primary" onClick={handleAnalyze}>重新分析</button> | |
| </div> | |
| )} | |
| </> | |
| )} | |
| {analyzeDone && ( | |
| <div className="mt-24 flex-center gap-12 flex-wrap"> | |
| <button className="btn btn-primary" onClick={() => onComplete({ advice })}>查看改进建议</button> | |
| <button className="btn btn-secondary" onClick={onBack}>返回推荐</button> | |
| </div> | |
| )} | |
| </div> | |
| ) | |
| } | |
| export default DiagnosePage | |