RunWorkspace.tsx
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* 一次 AI 建模的工作区:顶栏(状态、耗时、暂停 / 继续 / 终止 / 重跑)→ 流水线 → 草案工作台。
* 运行中每 2 秒刷新一次;点流水线节点在右侧抽屉里看这一步的日志和 AI 调用。
*/
import { CircleSlash, Download, PauseCircle, Play, RotateCcw, Trash2 } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Card, Spinner } from "@/components/ui";
import { confirmDialog } from "@/lib/dialog";
import { go } from "@/lib/nav";
import { errText, onto, type ModelRunFull } from "@/lib/onto";
import { toast } from "@/store/useAppStore";
import { DraftWorkbench } from "./DraftWorkbench";
import { PipelineBar } from "./PipelineBar";
import { StageDrawer } from "./StageDrawer";
import { fmtDur, RUN_STATUS } from "./util";
export function RunWorkspace({ id, canEdit, onChanged }: { id: string; canEdit: boolean; onChanged: () => void }) {
const [run, setRun] = useState<ModelRunFull | null>(null);
const [stage, setStage] = useState("");
const load = useCallback(async () => {
try {
const r = await onto.modelingRun(id);
setRun(r);
return r;
} catch (e) { toast("error", "加载失败", errText(e)); return null; }
}, [id]);
const live = run?.status === "running";
useEffect(() => { void load(); }, [load]);
useEffect(() => {
if (!live) return undefined;
const h = window.setInterval(() => { void load(); }, 2000);
return () => window.clearInterval(h);
}, [live, load]);
// 跑完的那一下刷新左栏(状态、计数)
const [wasLive, setWasLive] = useState(false);
useEffect(() => {
if (live) setWasLive(true);
else if (wasLive) { setWasLive(false); onChanged(); }
}, [live, wasLive, onChanged]);
if (!run) return <Spinner />;
const st = RUN_STATUS[run.status] || RUN_STATUS.draft;
const act = async (fn: () => Promise<unknown>, ok: string) => {
try { await fn(); toast("ok", ok); await load(); onChanged(); } catch (e) { toast("error", errText(e)); }
};
const retry = async (from: string) => {
const label = run.stages.find((s) => s.key === from)?.label || from;
if (!(await confirmDialog(`从「${label}」重跑?`, { desc: "这一步和之后各步上次的产出会先清掉(还没决定的草案、没采纳的流程卡);已经采纳上线的不受影响。" }))) return;
await act(() => onto.startModelingRun(run.id, from), "已重新开始");
};
const inputs = [...run.inputs.files, ...run.inputs.doc_ids.map((d) => `知识库文档 #${d}`)];
const dbs = (run.options?.ds_ids || []).length;
return (
<div className="flex min-h-0 flex-1 flex-col gap-3 overflow-auto pr-1">
<div className="flex flex-wrap items-center gap-2">
<h2 className="min-w-0 break-words text-section font-semibold">{run.title}</h2>
<Badge variant={st.variant}>{st.label}</Badge>
{run.control && <Badge variant="warn">正在{run.control === "stop" ? "暂停" : "终止"}…</Badge>}
<span className="text-label text-muted">
{run.started_at ? `开始于 ${run.started_at.replace("T", " ").slice(5, 16)} · ` : ""}共用时 {fmtDur(run.elapsed_ms)}
{" · "}{inputs.length ? `${inputs.length} 份文档` : "无文档"}{dbs ? ` · ${dbs} 个数据库` : ""}
{run.model ? ` · 模型 ${run.model}` : ""}
</span>
<div className="ml-auto flex flex-wrap gap-1.5">
{canEdit && live && <>
<button className="btn btn-sm" disabled={!!run.control} onClick={() => void act(() => onto.stopModelingRun(run.id), "正在暂停")}>
<PauseCircle className="h-3.5 w-3.5" />暂停</button>
<button className="btn btn-sm" disabled={run.control === "cancel"} onClick={async () => {
if (await confirmDialog("终止这次建模?", { desc: "正在进行的 AI 调用会被打断。已经产出的草案保留,之后可以从任意一步重跑。", danger: true }))
await act(() => onto.cancelModelingRun(run.id), "正在终止");
}}><CircleSlash className="h-3.5 w-3.5" />终止</button>
</>}
{canEdit && !live && ["stopped", "failed", "draft"].includes(run.status) && (
<button className="btn btn-primary btn-sm" onClick={() => void act(() => onto.startModelingRun(run.id), "继续运行")}>
<Play className="h-3.5 w-3.5" />{run.status === "draft" ? "开始" : "继续"}</button>
)}
{canEdit && !live && run.status !== "draft" && (
<button className="btn btn-sm" onClick={() => void retry(run.stages.find((s) => s.status !== "skipped")?.key || "convert")}>
<RotateCcw className="h-3.5 w-3.5" />全部重跑</button>
)}
<a className="btn btn-sm" href={onto.modelingExportUrl(run.id)} download={`${run.title}-草案.md`}><Download className="h-3.5 w-3.5" />导出</a>
{canEdit && !live && (
<button className="btn btn-sm" title="删除这条建模记录" onClick={async () => {
if (!(await confirmDialog("删除这条建模记录?", { desc: "还没决定的草案一起删掉;已经采纳上线的元素不受影响。", danger: true }))) return;
try { await onto.deleteModelingRun(run.id); onChanged(); go("modeling"); } catch (e) { toast("error", errText(e)); }
}}><Trash2 className="h-3.5 w-3.5" /></button>
)}
</div>
</div>
<Card className="p-3">
<PipelineBar run={run} selected={stage} onSelect={(k) => setStage(stage === k ? "" : k)} />
{run.error && (
<p className="mt-2 rounded-lg border border-bad/30 bg-bad/5 px-3 py-2 text-label text-bad">
{run.error}{run.status === "stopped" || run.status === "failed" ? "。点上面的节点看详情,或「继续」接着跑。" : ""}
</p>
)}
{!stage && <p className="mt-1.5 text-caption text-muted">点任意一步,看它的处理日志、AI 思考过程和耗时,也可以从那一步重跑。</p>}
</Card>
<DraftWorkbench run={run} canEdit={canEdit} onChanged={() => { void load(); onChanged(); }} />
{stage && (
<StageDrawer run={run} stageKey={stage} canEdit={canEdit} onClose={() => setStage("")}
onRetry={(k) => void retry(k)}
onStop={() => void act(() => onto.stopModelingRun(run.id), "正在暂停")}
onCancel={() => void act(() => onto.cancelModelingRun(run.id), "正在终止")} />
)}
</div>
);
}