124 lines
13 KiB
TypeScript
124 lines
13 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { ArrowRight, Check, Download, Fingerprint, Link2, LoaderCircle, Play, RefreshCw, Square, Unplug } from 'lucide-react';
|
||
import { api, downloadEngineReport } from './api';
|
||
import { Modal } from './components';
|
||
import type { ArtifactPreview, EngineAvailability, EngineJob, Project, Session } from './types';
|
||
import './EnginePanel.css';
|
||
|
||
const active = ['queued', 'starting', 'initializing', 'checking', 'stopping'];
|
||
const states: Record<string, string> = { queued: '等待启动', starting: '原程序启动中', initializing: '正在连接', checking: '正在核验',
|
||
stopping: '正在停止', completed: '连接通过', failed: '连接未通过', cancelled: '已停止', unknown: '结果待核对' };
|
||
const date = (value: string) => new Date(value).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' });
|
||
type Preview = { job: EngineJob; page: ArtifactPreview };
|
||
|
||
export function EnginePanel({ project, session, busy, onSession, onError }: {
|
||
project: Project; session: Session; busy: boolean; onSession: (value: Session) => void; onError: (error: unknown) => void;
|
||
}) {
|
||
const [root, setRoot] = useState(''), [installation, setInstallation] = useState<EngineAvailability | null>(null);
|
||
const [checking, setChecking] = useState(false), [pending, setPending] = useState(false);
|
||
const [retry, setRetry] = useState<EngineJob | null>(null), [preview, setPreview] = useState<Preview | null>(null);
|
||
const [viewBusy, setViewBusy] = useState(false), [notice, setNotice] = useState('');
|
||
const mounted = useRef(true), detection = useRef(0), scope = useRef(0), viewRequest = useRef(0);
|
||
const taskId = session.execution?.task?.id ?? null;
|
||
const jobs = (session.execution?.engineJobs ?? []).slice().reverse();
|
||
const running = jobs.some(job => active.includes(job.status)), disabled = busy || running || pending;
|
||
useEffect(() => { mounted.current = true; return () => { mounted.current = false; detection.current++; scope.current++; viewRequest.current++; }; }, []);
|
||
useEffect(() => { scope.current++; viewRequest.current++; setPreview(null); setRetry(null); setViewBusy(false); setPending(false); setNotice(''); }, [taskId]);
|
||
useEffect(() => { void detect(); }, []);
|
||
async function detect(candidate = root) {
|
||
const current = ++detection.current; setChecking(true); setNotice('');
|
||
try {
|
||
const value = await api<EngineAvailability>('/engines/deepseek/availability' + (candidate.trim() ? '?root=' + encodeURIComponent(candidate.trim()) : ''));
|
||
if (mounted.current && current === detection.current) { setInstallation(value); if (!candidate.trim() && value.root) setRoot(value.root); }
|
||
} catch (error) { if (mounted.current && current === detection.current) onError(error); }
|
||
finally { if (mounted.current && current === detection.current) setChecking(false); }
|
||
}
|
||
function editRoot(value: string) { detection.current++; setRoot(value); setInstallation(null); setChecking(false); setNotice('目录已改变,请先检测这个安装。'); }
|
||
async function start(previous?: EngineJob) {
|
||
const current = scope.current; setPending(true); setNotice('');
|
||
try {
|
||
const value = await api<{ job: EngineJob; session: Session }>('/sessions/' + session.id + '/engines/jobs', {
|
||
root: previous?.installation.root ?? installation?.root ?? root.trim(),
|
||
...(previous ? { retry: true } : { identitySha256: installation?.identitySha256 }),
|
||
});
|
||
if (mounted.current && current === scope.current) { onSession(value.session); setRetry(null); }
|
||
} catch (error) { if (mounted.current && current === scope.current) onError(error); }
|
||
finally { if (mounted.current && current === scope.current) setPending(false); }
|
||
}
|
||
async function stop() {
|
||
const current = scope.current; setPending(true);
|
||
try { const value = await api<Session>('/sessions/' + session.id + '/engines/stop', {}); if (mounted.current && current === scope.current) onSession(value); }
|
||
catch (error) { if (mounted.current && current === scope.current) onError(error); }
|
||
finally { if (mounted.current && current === scope.current) setPending(false); }
|
||
}
|
||
async function openRecord(job: EngineJob, offset = 0) {
|
||
const current = ++viewRequest.current, currentScope = scope.current; setViewBusy(true);
|
||
try {
|
||
const page = await api<ArtifactPreview>('/sessions/' + session.id + '/engines/jobs/' + job.id + '/report?offset=' + offset + '&limit=4000');
|
||
if (mounted.current && currentScope === scope.current && current === viewRequest.current) setPreview({ job, page });
|
||
} catch (error) { if (mounted.current && currentScope === scope.current && current === viewRequest.current) onError(error); }
|
||
finally { if (mounted.current && currentScope === scope.current && current === viewRequest.current) setViewBusy(false); }
|
||
}
|
||
async function download(job: EngineJob) {
|
||
const current = scope.current;
|
||
try { await downloadEngineReport(session.id, job.id); }
|
||
catch (error) { if (mounted.current && current === scope.current) onError(error); }
|
||
}
|
||
return <div className="engine-panel">
|
||
<header className="engine-heading"><div><span className="engine-eyebrow">{project.name} · 当前任务</span><h3>连接本地代理引擎</h3>
|
||
<p>核对本机的 DeepSeek Harness,并将实际检查记录保存到这个任务。</p></div>
|
||
<span className={'engine-install-state ' + (installation?.status === 'available' ? 'found' : '')} aria-live="polite">
|
||
{checking ? '正在检测安装' : installation?.status === 'available' ? '已找到 ' + installation.version : '等待检测'}</span></header>
|
||
<div className="engine-layout"><section className="engine-connection">
|
||
<form onSubmit={event => { event.preventDefault(); void detect(); }}>
|
||
<h4><Link2 size={16} />DeepSeek Harness</h4>
|
||
<label className="form-field">本地安装目录<input aria-label="DeepSeek安装目录" value={root} onChange={event => editRoot(event.target.value)}
|
||
placeholder="D:\Ai-project\DeepSeek-Harness-Desktop\deepseek-harness" disabled={disabled} /></label>
|
||
<button type="submit" className="button secondary" disabled={checking || disabled}><RefreshCw size={14} className={checking ? 'spin' : ''} />检测安装</button>
|
||
{installation && <p className={'engine-detection-note ' + installation.status} role="status">{installation.message}</p>}
|
||
{notice && <p className="engine-detection-note" role="status">{notice}</p>}
|
||
{installation?.status === 'available' && <dl className="engine-install-details"><dt>发行版本</dt><dd>{installation.version}</dd><dt>连接范围</dt><dd>启动、协议、会话目录与停止</dd>
|
||
<dt>运行环境</dt><dd>Node.js {installation.node?.version}</dd></dl>}
|
||
<button type="button" className="button primary engine-start" onClick={() => void start()} disabled={disabled || checking || installation?.status !== 'available'}>
|
||
{pending ? <LoaderCircle size={14} className="spin" /> : <Play size={14} />}核验连接</button>
|
||
</form>
|
||
<div className="engine-scope"><h4>当前可用能力</h4><p><Check size={14} />原程序连接与独立进程停止</p><p><Check size={14} />同任务保存原记录与来源</p>
|
||
<div className="engine-scope-next"><span>工程执行接入中</span><p>本次检查不发送模型任务。完成连接核验后,继续接入工程工具与交付流程。</p></div>
|
||
<details><summary>连接与执行范围</summary><p>每次检查启动一个独立进程。停止只作用于这个检查;SDK 会话目录、协议版本与完整错误保存在原记录。当前版本没有开放外部引擎修改工程。</p></details></div>
|
||
</section><section className="engine-results"><div className="engine-results-heading"><h4>本任务连接记录 <span>{jobs.length}</span></h4>
|
||
{running && <button type="button" className="button secondary" onClick={() => void stop()} disabled={pending}><Square size={13} />停止检查</button>}</div>
|
||
{!jobs.length && <div className="engine-empty"><Unplug size={25} /><strong>先验证这个安装能否连接</strong><p>检查完成后,可以查看实际启动、协议返回和进程结束记录;检查不会调用已配置的模型。</p></div>}
|
||
{jobs.map(job => { const verified = job.status === 'completed' && job.verification?.verified === true;
|
||
return <article key={job.id} data-engine-job={job.id} className={'engine-job ' + (verified ? 'verified' : '')}>
|
||
<div className="engine-job-heading"><div><strong>DeepSeek Harness {job.installation.version}</strong><small>{date(job.startedAt)}</small></div>
|
||
<span className={'engine-job-state ' + job.status}>{active.includes(job.status) && <LoaderCircle size={12} className="spin" />}{states[job.status] ?? job.status}</span></div>
|
||
<p className="engine-job-summary">{job.summary}</p>
|
||
<div className="engine-proof" aria-label="引擎实际检查依据">
|
||
<div className={job.installation.identitySha256 ? 'observed' : ''}><Fingerprint size={15} /><span>安装身份<code>{job.installation.identitySha256?.slice(0, 12) ?? '等待核对'}</code></span></div>
|
||
<ArrowRight size={12} /><div className={job.readiness ? 'observed' : ''}><span>启动完成<code>{job.readiness ? 'PID ' + job.readiness.pid : '尚无信号'}</code></span></div>
|
||
<ArrowRight size={12} /><div className={job.serverInfo ? 'observed' : ''}><span>协议返回<code>{job.serverInfo?.version ?? '尚无返回'}</code></span></div>
|
||
<ArrowRight size={12} /><div className={job.reportSummary?.rootExited ? 'observed' : ''}><span>进程结束<code>{job.reportSummary?.rootExited ? '已核对' : job.finishedAt ? '待核对' : '检查中'}</code></span></div>
|
||
</div>
|
||
<div className="engine-job-actions"><button className="button secondary" onClick={() => void openRecord(job)} disabled={!job.reportArtifactId || viewBusy}><Fingerprint size={13} />查看原记录</button>
|
||
<button className="text-button" onClick={() => void download(job)} disabled={!job.reportArtifactId}><Download size={13} />下载完整记录</button>
|
||
{!active.includes(job.status) && <button className="text-button" onClick={() => setRetry(job)} disabled={disabled}><RefreshCw size={13} />重新核验</button>}</div>
|
||
<details className="engine-origin"><summary>任务与安装来源</summary><dl><dt>项目</dt><dd>{project.name}</dd><dt>Task</dt><dd>{job.taskId}</dd><dt>Run</dt><dd>{job.runId}</dd>
|
||
<dt>SDK 会话</dt><dd>{job.sdkSessionId}</dd><dt>安装目录</dt><dd>{job.installation.root}</dd><dt>安装 SHA256</dt><dd>{job.installation.identitySha256}</dd>
|
||
<dt>原记录 SHA256</dt><dd>{job.reportSha256 ?? '尚未封存'}</dd><dt>模型请求</dt><dd>{job.reportSummary?.modelRequests ?? '尚未核对'}</dd></dl></details>
|
||
</article>;
|
||
})}
|
||
</section></div>
|
||
{retry && <Modal title="重新核验引擎连接" description="启动一个新的独立检查;原检查记录继续保留。" onClose={() => setRetry(null)}>
|
||
<p className="engine-retry-note">{retry.installation.root}</p><p>将重新核对当前安装、启动完成、SDK 返回和进程关闭。本次不会发送模型任务。</p>
|
||
<div className="modal-actions"><button className="button secondary" onClick={() => setRetry(null)}>返回记录</button><button className="button primary" disabled={disabled} onClick={() => void start(retry)}><RefreshCw size={14} />开始新的核验</button></div></Modal>}
|
||
{preview && <Modal title="引擎原检查记录" description="核对封存的安装、任务身份和原记录 SHA256 后读取。" wide onClose={() => { viewRequest.current++; setPreview(null); setViewBusy(false); }}>
|
||
<div className="engine-record-meta"><span>原记录 SHA256</span><code>{preview.page.sha256}</code></div>
|
||
<pre className="engine-original" aria-label="引擎原记录内容">{preview.page.content}</pre>
|
||
<div className="engine-page-controls"><span>{preview.page.offset.toLocaleString()}–{(preview.page.offset + preview.page.content.length).toLocaleString()} / {preview.page.totalCharacters.toLocaleString()} 字符</span>
|
||
<button className="button secondary" onClick={() => void openRecord(preview.job, 0)} disabled={viewBusy || preview.page.offset === 0}>回到首页</button>
|
||
<button className="button secondary" onClick={() => void openRecord(preview.job, preview.page.offset + preview.page.content.length)} disabled={viewBusy || !preview.page.truncated}><ArrowRight size={13} />下一页</button></div>
|
||
<div className="modal-actions"><button className="button secondary" onClick={() => void download(preview.job)}><Download size={14} />下载完整记录</button></div>
|
||
</Modal>}
|
||
</div>;
|
||
}
|