feat: AI writing history, Word export, roundtable dispatch chain

This commit is contained in:
fengxiangdebaozi committed 2026-06-04 15:40:15 +08:00
1 parent c0e3c5d692
commit 10334ddfe1
21 files changed
+1888 -143

No files matched your search

@@ -0,0 +1,339 @@
/**
* 派活链路流程图弹窗(Phase 7)。
*
* 大弹窗 + 横向流程图:总控 ──▶ 席位1 ──▶ 席位2 ──▶ … ──▶[共识]──▶[报告绘制]。
* - 节点 = 智能体席位(头像 + 名称),按 `getAvatarTypeFor` 取不同颜色;
* - 正在发言的节点(state==='active')和它**左侧的连接线**带加载动画(脉冲光环 + 流动虚线);
* - 已完成节点打勾,未开始节点置灰;
* - footer 显示 轮次/状态 + 「查看」按钮 → 进入正在进行的任务(onView)。
*
* 本组件是**纯展示组件**:所有状态由 props 传入。Phase 8 会接 `streamJob` 把后台 SSE
* 进度喂进来;当前可由 `DispatchChainPreviewPage` 用假数据 + 定时器模拟驱动预览。
*
* 颜色 / 头像复用圆桌既有体系:`getAvatar`(Avatars.tsx)+ `chainTagClasses`
* (business-chain.ts)。连线动画 CSS 在 `styles/roundtable-planning.css` 全局段
* (Radix Dialog portal 到 body,不在 .roundtable-planning 作用域内,故用全局类 + 语义 token)。
*/
import { Fragment } from "react";
import { CheckIcon, ChevronRightIcon, EyeIcon, Loader2Icon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { cn } from "@/lib/utils";
import { getAvatar } from "./Avatars";
import { chainTagClasses } from "../lib/business-chain";
import type {
ChainNodeState,
DispatchChainNode,
JobPhase,
JobStatus,
} from "../lib/job-types";
export interface DispatchChainModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
/** 任务标题。 */
title?: string;
status: JobStatus;
phase: JobPhase;
/** 当前轮次(0..maxCycles)。 */
cycle: number;
/** 最大轮次(默认 8,对齐 MAX_CYCLES)。 */
maxCycles?: number;
/** 派活链路节点(横向排列)。 */
nodes: DispatchChainNode[];
/** 共识百分比(0..100)。 */
consensusPercentage: number;
/** 「查看」按钮回调 → 进入正在进行的任务。 */
onView?: () => void;
}
const STATUS_LABEL: Record<JobStatus, string> = {
queued: "排队中",
running: "进行中",
awaiting_input: "等待澄清",
done: "已完成",
error: "出错",
cancelled: "已取消",
};
const PHASE_LABEL: Record<JobPhase, string> = {
initializing: "初始化席位…",
leader_thinking: "总控决策派活中…",
dispatching: "席位发言中…",
consensus: "共识达成",
report_drawing: "结果绘制中…",
report_done: "绘制完成",
};
export function DispatchChainModal({
open,
onOpenChange,
title = "多智能体研讨",
status,
phase,
cycle,
maxCycles = 8,
nodes,
consensusPercentage,
onView,
}: DispatchChainModalProps) {
// 总控节点:初始化 / 决策派活阶段高亮
const coordinatorState: ChainNodeState =
phase === "initializing" || phase === "leader_thinking" ? "active" : "done";
// 共识里程碑
const consensusDone =
consensusPercentage >= 100 ||
phase === "consensus" ||
phase === "report_drawing" ||
phase === "report_done" ||
status === "done";
const consensusActive = phase === "consensus";
// 报告绘制里程碑
const reportActive = phase === "report_drawing";
const reportDone = phase === "report_done" || status === "done";
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="flex max-h-[88vh] flex-col gap-0 sm:max-w-5xl">
<DialogHeader className="shrink-0">
<DialogTitle className="flex items-center gap-2">
{status === "running" && (
<Loader2Icon className="size-5 shrink-0 animate-spin text-primary" />
)}
<span className="truncate">{title}</span>
<span
className={cn(
"ml-1 shrink-0 rounded-full px-2 py-0.5 text-[11px] font-semibold",
status === "running" && "bg-primary/10 text-primary",
status === "awaiting_input" &&
"bg-amber-500/10 text-amber-600 dark:text-amber-400",
status === "done" &&
"bg-emerald-500/10 text-emerald-600 dark:text-emerald-400",
status === "error" && "bg-destructive/10 text-destructive",
(status === "queued" || status === "cancelled") &&
"bg-muted text-muted-foreground",
)}
>
{STATUS_LABEL[status]}
</span>
</DialogTitle>
<DialogDescription>{PHASE_LABEL[phase]}</DialogDescription>
</DialogHeader>
{/* 横向流程图(横向滚动) */}
<div className="custom-scrollbar min-h-0 flex-1 overflow-x-auto overflow-y-hidden py-10">
<div className="flex min-w-max items-start gap-0 px-6">
{/* 总控节点 */}
<FlowNode
label="总控协调"
avatarType="human"
state={coordinatorState}
/>
{/* 席位节点 */}
{nodes.map((node) => (
<Fragment key={node.agentId}>
<Connector
active={node.state === "active"}
done={node.state === "done"}
/>
<FlowNode
label={node.name}
avatarType={node.avatarType}
state={node.state}
order={node.order}
/>
</Fragment>
))}
{/* 共识里程碑 */}
<Connector active={consensusActive} done={consensusDone} />
<MilestoneNode
label="共识达成"
emoji="🤝"
state={
consensusActive ? "active" : consensusDone ? "done" : "pending"
}
/>
{/* 报告绘制里程碑 */}
<Connector active={reportActive} done={reportDone} />
<MilestoneNode
label="结果绘制"
emoji="📊"
state={reportActive ? "active" : reportDone ? "done" : "pending"}
/>
</div>
</div>
<DialogFooter className="shrink-0 flex-row items-center justify-between gap-2 border-t pt-3 sm:justify-between">
<span className="text-xs text-muted-foreground">
轮次 {Math.min(cycle, maxCycles)}/{maxCycles}
<span className="mx-2 text-muted-foreground/40">·</span>
共识 {Math.round(consensusPercentage)}%
<span className="mx-2 text-muted-foreground/40">·</span>
{PHASE_LABEL[phase]}
</span>
<Button onClick={onView} disabled={!onView} className="gap-1.5">
<EyeIcon className="size-4" />
查看
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
// ── 链路节点(席位 / 总控)─────────────────────────────────────────────────
function FlowNode({
label,
avatarType,
state,
order,
}: {
label: string;
avatarType: string;
state: ChainNodeState;
order?: number;
}) {
const tag = chainTagClasses(avatarType);
return (
<div className="flex w-24 shrink-0 flex-col items-center gap-2 text-center">
<div className="relative flex size-16 items-center justify-center">
{/* 活动光环:脉冲 ping */}
{state === "active" && (
<span className="absolute inset-0 animate-ping rounded-full bg-primary/30" />
)}
{/* 头像 + 状态描边 */}
<span
className={cn(
"relative flex size-16 items-center justify-center rounded-full border-2 transition-all",
state === "active" &&
"border-primary shadow-[0_0_0_4px_rgba(22,119,255,0.12)]",
state === "done" && "border-emerald-400/70",
state === "pending" && "border-border opacity-45 grayscale",
)}
>
{getAvatar(avatarType, "h-14 w-14")}
{/* 完成打勾 */}
{state === "done" && (
<span className="absolute -right-0.5 -top-0.5 flex size-5 items-center justify-center rounded-full bg-emerald-500 text-white shadow">
<CheckIcon className="size-3" />
</span>
)}
{/* 进行中转圈 */}
{state === "active" && (
<span className="absolute -right-0.5 -top-0.5 flex size-5 items-center justify-center rounded-full bg-primary text-primary-foreground shadow">
<Loader2Icon className="size-3 animate-spin" />
</span>
)}
</span>
</div>
<span
className={cn(
"max-w-full truncate rounded border px-1.5 py-0.5 text-[11px] font-medium",
state === "pending" ? "border-border bg-muted text-muted-foreground" : tag,
)}
title={label}
>
{order != null ? `${order}. ` : ""}
{label}
</span>
</div>
);
}
// ── 里程碑节点(共识 / 报告绘制)─────────────────────────────────────────────
function MilestoneNode({
label,
emoji,
state,
}: {
label: string;
emoji: string;
state: ChainNodeState;
}) {
return (
<div className="flex w-24 shrink-0 flex-col items-center gap-2 text-center">
<div className="relative flex size-16 items-center justify-center">
{state === "active" && (
<span className="absolute inset-0 animate-ping rounded-full bg-primary/30" />
)}
<span
className={cn(
"relative flex size-16 items-center justify-center rounded-full border-2 text-2xl transition-all",
state === "active" &&
"border-primary bg-primary/5 shadow-[0_0_0_4px_rgba(22,119,255,0.12)]",
state === "done" && "border-emerald-400/70 bg-emerald-500/5",
state === "pending" && "border-border bg-muted opacity-45 grayscale",
)}
>
{emoji}
{state === "done" && (
<span className="absolute -right-0.5 -top-0.5 flex size-5 items-center justify-center rounded-full bg-emerald-500 text-white shadow">
<CheckIcon className="size-3" />
</span>
)}
{state === "active" && (
<span className="absolute -right-0.5 -top-0.5 flex size-5 items-center justify-center rounded-full bg-primary text-primary-foreground shadow">
<Loader2Icon className="size-3 animate-spin" />
</span>
)}
</span>
</div>
<span
className={cn(
"rounded border px-1.5 py-0.5 text-[11px] font-semibold",
state === "pending"
? "border-border bg-muted text-muted-foreground"
: state === "active"
? "border-primary/40 bg-primary/10 text-primary"
: "border-emerald-400/40 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400",
)}
>
{label}
</span>
</div>
);
}
// ── 连接线(带流动动画)─────────────────────────────────────────────────────
function Connector({ active, done }: { active: boolean; done: boolean }) {
return (
<div className="flex h-16 w-16 shrink-0 items-center justify-center">
<div className="relative flex w-full items-center">
<span
className={cn(
"rt-chain-connector",
active && "rt-chain-connector--active",
done && !active && "rt-chain-connector--done",
)}
/>
<ChevronRightIcon
className={cn(
"absolute -right-1 size-4 transition-colors",
active
? "text-primary"
: done
? "text-primary/70"
: "text-muted-foreground/40",
)}
/>
</div>
</div>
);
}
@@ -0,0 +1,56 @@
/**
* 圆桌后台作业(roundtable job)共享类型。
*
* 对应 `docs/multi-agent-background-run-dev.md` §2「数据契约」。本文件只放纯类型,
* 不含请求逻辑(后续 Phase 4 的 `api/roundtable-jobs.ts` 会引用这些类型)。
*
* Phase 7(派活链路流程图弹窗)只用到其中的 `JobStatus` / `JobPhase` /
* `DispatchChainNode`,其余类型为后续阶段预留,先一并定义好避免反复改动。
*/
/** 作业状态机(§2.1)。 */
export type JobStatus =
| "queued" // 已创建,等待 worker 拉起
| "running" // 编排进行中(Step2 派活循环 / Step3 绘制)
| "awaiting_input" // 命中 clarification,等用户回答(interrupt 挂起)
| "done" // 已跑到 Step3 绘制完成
| "error" // 出错终止
| "cancelled"; // 用户取消
/** 作业阶段(§2.2)——给流程图 / 进度条用。 */
export type JobPhase =
| "initializing" // 建 thread / 总控就绪
| "leader_thinking" // 总控在决策派活
| "dispatching" // 正在顺序派活给席位(配合 activeAgentId)
| "consensus" // 共识达成(dispatched=[])
| "report_drawing" // Step3 报告绘制中
| "report_done"; // 绘制完成
/** 派活链路单节点状态(驱动颜色 / 动画)。 */
export type ChainNodeState = "pending" | "active" | "done";
/** 派活链路节点(§2.3)——流程图核心数据。 */
export interface DispatchChainNode {
/** 席位 agent_id(小写)。 */
agentId: string;
/** 显示名。 */
name: string;
/** 复用 getAvatarTypeFor(agentId, order) → 决定颜色 / 头像。 */
avatarType: string;
/** 横向排列次序(chain 模式=链条顺序;recommend 模式=席位/发言顺序)。 */
order: number;
/** 节点状态。 */
state: ChainNodeState;
/** 最近发言轮次(可选,做 tooltip)。 */
lastSpokeCycle?: number;
}
/** 归一化进度事件(§2.4)——SSE 推送。Phase 7 暂未用到,先占位。 */
export type JobProgressEvent =
| { type: "status"; status: JobStatus }
| { type: "phase"; phase: JobPhase; cycle: number }
| { type: "chain"; nodes: DispatchChainNode[] }
| { type: "active"; agentId: string | null }
| { type: "consensus"; percentage: number }
| { type: "clarification"; question: string }
| { type: "error"; detail: string };
@@ -0,0 +1,165 @@
/**
* 派活链路流程图弹窗 —— 预览/自测页(Phase 7 临时入口)。
*
* 路由:/page/workspace/roundtable/chain-preview
*
* 用假数据 + 定时器模拟后台作业推进(初始化 → 总控派活 → 席位逐个发言 → 共识 →
* 结果绘制 → 完成),驱动 `DispatchChainModal` 演示节点 / 连线的加载动画与状态流转。
* 后续 Phase 8 接通真实 `streamJob` 后,本页可删除(连同 WorkspaceRoutes 里的路由)。
*/
import { useEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { DispatchChainModal } from "../components/DispatchChainModal";
import {
DEFAULT_SELECTED_AGENTS,
getAvatarTypeFor,
getDisplayNameFor,
} from "../lib/roundtable-constants";
import type {
DispatchChainNode,
JobPhase,
JobStatus,
} from "../lib/job-types";
import "../styles/roundtable-planning.css";
interface SimFrame {
phase: JobPhase;
status: JobStatus;
cycle: number;
consensus: number;
activeAgentId: string | null;
doneAgentIds: string[];
}
const AGENTS = DEFAULT_SELECTED_AGENTS;
/** 预先生成一条完整的推进时间线(每帧约 1.1s)。 */
function buildFrames(): SimFrame[] {
const ids = AGENTS.map((a) => a.agent_id);
const frames: SimFrame[] = [];
frames.push({ phase: "initializing", status: "running", cycle: 0, consensus: 0, activeAgentId: null, doneAgentIds: [] });
frames.push({ phase: "leader_thinking", status: "running", cycle: 1, consensus: 12, activeAgentId: null, doneAgentIds: [] });
// 第 1 轮:逐个席位发言
ids.forEach((id, i) => {
frames.push({
phase: "dispatching",
status: "running",
cycle: 1,
consensus: 12 + Math.round(((i + 1) / ids.length) * 50),
activeAgentId: id,
doneAgentIds: ids.slice(0, i),
});
});
// 第 2 轮:总控再决策(此时第 1 轮席位都已发言)
frames.push({ phase: "leader_thinking", status: "running", cycle: 2, consensus: 78, activeAgentId: null, doneAgentIds: [...ids] });
// 共识达成
frames.push({ phase: "consensus", status: "running", cycle: 2, consensus: 100, activeAgentId: null, doneAgentIds: [...ids] });
// 结果绘制中
frames.push({ phase: "report_drawing", status: "running", cycle: 2, consensus: 100, activeAgentId: null, doneAgentIds: [...ids] });
// 完成
frames.push({ phase: "report_done", status: "done", cycle: 2, consensus: 100, activeAgentId: null, doneAgentIds: [...ids] });
return frames;
}
export default function DispatchChainPreviewPage() {
const frames = useMemo(buildFrames, []);
const [idx, setIdx] = useState(0);
const [playing, setPlaying] = useState(true);
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
// 自动播放:逐帧推进,跑到最后一帧停 2.2s 再循环
useEffect(() => {
if (!playing) return;
const isLast = idx >= frames.length - 1;
timerRef.current = setTimeout(
() => setIdx((i) => (i >= frames.length - 1 ? 0 : i + 1)),
isLast ? 2200 : 1100,
);
return () => {
if (timerRef.current) clearTimeout(timerRef.current);
};
}, [idx, playing, frames.length]);
const frame = frames[idx];
// 由当前帧推导各席位节点状态
const nodes: DispatchChainNode[] = AGENTS.map((a, i) => {
const state =
frame.doneAgentIds.includes(a.agent_id)
? "done"
: frame.activeAgentId === a.agent_id
? "active"
: "pending";
return {
agentId: a.agent_id,
name: getDisplayNameFor(a),
avatarType: getAvatarTypeFor(a.agent_id, i),
order: i + 1,
state,
};
});
return (
<div className="flex h-full min-h-0 flex-col items-center justify-center gap-6 bg-muted/30 p-8">
<div className="text-center">
<h1 className="text-lg font-bold">派活链路流程图 · 预览</h1>
<p className="mt-1 text-sm text-muted-foreground">
模拟后台作业推进(假数据)。帧 {idx + 1}/{frames.length} · {frame.phase}
</p>
</div>
<div className="flex items-center gap-2">
<Button variant="outline" size="sm" onClick={() => setPlaying((p) => !p)}>
{playing ? "暂停" : "继续"}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
setIdx((i) => (i > 0 ? i - 1 : i));
setPlaying(false);
}}
>
上一帧
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
setIdx((i) => (i < frames.length - 1 ? i + 1 : i));
setPlaying(false);
}}
>
下一帧
</Button>
<Button
size="sm"
onClick={() => {
setIdx(0);
setPlaying(true);
}}
>
重新播放
</Button>
</div>
<DispatchChainModal
open
onOpenChange={() => {}}
title="新能源汽车出口策略研讨(预览)"
status={frame.status}
phase={frame.phase}
cycle={frame.cycle}
nodes={nodes}
consensusPercentage={frame.consensus}
onView={() => toast.info("(预览)点击「查看」→ Phase 8 会进入正在进行的任务")}
/>
</div>
);
}
@@ -360,6 +360,50 @@
color: var(--foreground) !important;
}
/* =============================================================================
* 派活链路流程图连接线(DispatchChainModal)。
*
* 弹窗经 Radix Dialog portal 到 <body>,**不在** .roundtable-planning 作用域内,
* 因此这些类必须是**全局**(无作用域前缀),并用语义 token(--primary/--border)
* 自适应明暗。连线三态:pending(灰虚线静止) / done(主色实线) / active(主色流动虚线)。
* ========================================================================== */
@keyframes rt-chain-flow {
to {
background-position: 18px 0;
}
}
.rt-chain-connector {
width: 100%;
height: 3px;
border-radius: 9999px;
/* 默认(pending):灰色等距虚线 */
background-image: linear-gradient(
90deg,
var(--border) 45%,
transparent 45%
);
background-size: 12px 100%;
background-repeat: repeat-x;
}
.rt-chain-connector--done {
background-image: none;
background-color: var(--primary);
opacity: 0.7;
}
.rt-chain-connector--active {
background-image: linear-gradient(
90deg,
var(--primary) 45%,
transparent 45%
);
background-size: 18px 100%;
background-repeat: repeat-x;
animation: rt-chain-flow 0.55s linear infinite;
}
/* Custom elegant scrollbar styling */
::-webkit-scrollbar {
width: 6px;