"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import clsx from "clsx"; type EvolutionConfig = { mode: "controlled" | "autonomous"; autoApplyLowRiskRules: boolean; }; type EvolutionSignal = { signalId: string; kind: string; requestText: string; createdAt: string; }; type EvolutionProposal = { proposalId: string; proposalType: string; title: string; summary: string; status: string; confidence: number; riskLevel: string; createdAt: string; }; type EvolutionRule = { ruleId: string; ruleType: string; createdAt: string; sourceProposalId?: string; }; function formatTime(value?: string) { if (!value) { return "-"; } return new Date(value).toLocaleString("zh-CN", { hour12: false }); } async function readJson(response: Response): Promise { return (await response.json()) as T; } export function MasterAgentEvolutionClient({ isAdmin, config, signals, proposals, rules, }: { isAdmin: boolean; config: EvolutionConfig; signals: EvolutionSignal[]; proposals: EvolutionProposal[]; rules: EvolutionRule[]; }) { const router = useRouter(); const [busyKey, setBusyKey] = useState(null); const [message, setMessage] = useState(""); async function switchMode(mode: "controlled" | "autonomous") { setBusyKey(`mode:${mode}`); const response = await fetch("/api/v1/master-agent/evolution/config", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ mode }), }); const result = await readJson<{ ok: boolean; message?: string }>(response); setBusyKey(null); setMessage(result.ok ? `已切到 ${mode === "autonomous" ? "完全自我进化" : "受控自动进化"}。` : result.message ?? "切换失败。"); if (result.ok) { router.refresh(); } } async function reviewProposal(proposalId: string, action: "approve" | "reject") { setBusyKey(`${action}:${proposalId}`); const response = await fetch(`/api/v1/master-agent/evolution/proposals/${proposalId}/${action}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}), }); const result = await readJson<{ ok: boolean; message?: string }>(response); setBusyKey(null); setMessage(result.ok ? (action === "approve" ? "提案已批准。" : "提案已拒绝。") : result.message ?? "提交失败。"); if (result.ok) { router.refresh(); } } const pendingProposals = proposals.filter((item) => item.status === "pending_review"); return (
自动进化模式
当前模式:{config.mode === "autonomous" ? "完全自我进化" : "受控自动进化"}。 {config.autoApplyLowRiskRules ? "低风险提案会自动采纳。" : "所有提案都需要人工确认。"}
{config.mode === "autonomous" ? "全自动" : "受控"}
{isAdmin ? (
) : null}
待处理提案
{pendingProposals.length} 条
{pendingProposals.length === 0 ? (
当前没有待审批提案。
) : ( pendingProposals.map((proposal) => (
{proposal.title}
{proposal.summary}
{proposal.riskLevel} / {Math.round(proposal.confidence * 100)}%
{proposal.proposalType} · {formatTime(proposal.createdAt)}
{isAdmin ? (
) : null}
)) )}
最近信号
{signals.length}
{signals.slice(0, 8).map((signal) => (
{signal.kind}
{signal.requestText}
{formatTime(signal.createdAt)}
))}
已生效规则
{rules.length}
{rules.slice(0, 8).map((rule) => (
{rule.ruleType}
{formatTime(rule.createdAt)} {rule.sourceProposalId ? ` · ${rule.sourceProposalId}` : ""}
))}
{message ? (
{message}
) : null}
); }