优化 Agent 过程展示,增加时间格式化和状态管理
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo, useState } from "react";
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import {
|
||||
Box,
|
||||
Collapse,
|
||||
@@ -22,6 +22,46 @@ import KeyboardArrowDownRounded from "@mui/icons-material/KeyboardArrowDownRound
|
||||
|
||||
import type { ChatProgress } from "./GlobalChatbox.types";
|
||||
|
||||
const formatDuration = (durationMs: number) => {
|
||||
if (!Number.isFinite(durationMs) || durationMs < 0) {
|
||||
return "0s";
|
||||
}
|
||||
if (durationMs < 10_000) {
|
||||
return `${(durationMs / 1000).toFixed(1)}s`;
|
||||
}
|
||||
const totalSeconds = Math.round(durationMs / 1000);
|
||||
if (totalSeconds < 60) {
|
||||
return `${totalSeconds}s`;
|
||||
}
|
||||
const minutes = Math.floor(totalSeconds / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ${seconds.toString().padStart(2, "0")}s`;
|
||||
}
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const remainMinutes = minutes % 60;
|
||||
return `${hours}h ${remainMinutes.toString().padStart(2, "0")}m`;
|
||||
};
|
||||
|
||||
const getProgressElapsedMs = (item: ChatProgress, nowMs: number) => {
|
||||
if (item.durationMs !== undefined) {
|
||||
return item.durationMs;
|
||||
}
|
||||
if (item.status === "running") {
|
||||
if (item.elapsedMs !== undefined && item.elapsedSnapshotAt !== undefined) {
|
||||
return Math.max(0, item.elapsedMs + (nowMs - item.elapsedSnapshotAt));
|
||||
}
|
||||
if (item.startedAt !== undefined) {
|
||||
return Math.max(0, nowMs - item.startedAt);
|
||||
}
|
||||
return item.elapsedMs;
|
||||
}
|
||||
if (item.startedAt !== undefined && item.endedAt !== undefined) {
|
||||
return Math.max(0, item.endedAt - item.startedAt);
|
||||
}
|
||||
return item.elapsedMs;
|
||||
};
|
||||
|
||||
const phaseIcon = (phase: string, status: ChatProgress["status"]) => {
|
||||
const sx = { fontSize: 16 };
|
||||
if (status === "completed") return <CheckCircleRounded sx={{ ...sx, color: "success.main" }} />;
|
||||
@@ -46,6 +86,7 @@ const formatToolTitle = (item: ChatProgress) => {
|
||||
|
||||
export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatProgress[], isAborted?: boolean }) => {
|
||||
const theme = useTheme();
|
||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||
|
||||
// 判断是否最终完成(哪怕中间有报错,只要有完整的标记就算成功)
|
||||
const isOverallComplete = progress.some(
|
||||
@@ -55,6 +96,16 @@ export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatP
|
||||
// 修正状态判断:如果外部标记为中断,或者没有完成标记
|
||||
const hasRunning = !isAborted && !isOverallComplete && progress.some((item) => item.status === "running");
|
||||
const hasError = isAborted || progress.some((item) => item.status === "error");
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasRunning) {
|
||||
return;
|
||||
}
|
||||
const timer = window.setInterval(() => {
|
||||
setNowMs(Date.now());
|
||||
}, 500);
|
||||
return () => window.clearInterval(timer);
|
||||
}, [hasRunning]);
|
||||
|
||||
// 展开状态逻辑:默认折叠,保持界面整洁
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
@@ -70,6 +121,31 @@ export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatP
|
||||
return `已执行 ${progress.length} 步`;
|
||||
}, [isOverallComplete, hasError, progress, isAborted]);
|
||||
|
||||
const totalDurationLabel = useMemo(() => {
|
||||
const requestProgress = progress.find((item) => item.id === "request-received");
|
||||
const requestElapsed =
|
||||
requestProgress ? getProgressElapsedMs(requestProgress, nowMs) : undefined;
|
||||
if (requestElapsed !== undefined) {
|
||||
return formatDuration(requestElapsed);
|
||||
}
|
||||
const startedAtValues = progress
|
||||
.map((item) => item.startedAt)
|
||||
.filter((value): value is number => value !== undefined);
|
||||
if (startedAtValues.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const minStartedAt = Math.min(...startedAtValues);
|
||||
const endedAtValues = progress
|
||||
.map((item) => item.endedAt)
|
||||
.filter((value): value is number => value !== undefined);
|
||||
const endAnchor = isOverallComplete
|
||||
? endedAtValues.length > 0
|
||||
? Math.max(...endedAtValues)
|
||||
: nowMs
|
||||
: nowMs;
|
||||
return formatDuration(Math.max(0, endAnchor - minStartedAt));
|
||||
}, [isOverallComplete, nowMs, progress]);
|
||||
|
||||
// 根据整体状态决定顶部卡片的颜色主题
|
||||
const statusColor = isOverallComplete
|
||||
? "#4caf50" // Success Green
|
||||
@@ -120,6 +196,7 @@ export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatP
|
||||
|
||||
<Typography variant="caption" fontWeight={700} color="text.primary" sx={{ flex: 1, letterSpacing: 0.3 }}>
|
||||
Agent 过程: {summary}
|
||||
{totalDurationLabel ? ` · 耗时 ${totalDurationLabel}` : ""}
|
||||
</Typography>
|
||||
|
||||
<KeyboardArrowDownRounded
|
||||
@@ -159,6 +236,7 @@ export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatP
|
||||
{progress.map((item, index) => {
|
||||
const isLast = index === progress.length - 1;
|
||||
const isHiddenWhenCollapsed = isCollapsible && index < progress.length - visibleCount;
|
||||
const stepElapsedMs = getProgressElapsedMs(item, nowMs);
|
||||
|
||||
const itemColor = isAborted && isLast
|
||||
? theme.palette.error.main
|
||||
@@ -219,9 +297,20 @@ export const AgentProgressTimeline = ({ progress, isAborted }: { progress: ChatP
|
||||
</Box>
|
||||
</Box>
|
||||
<Box sx={{ minWidth: 0, flex: 1, pb: isLast ? 0 : 2 }}>
|
||||
<Typography variant="caption" color="text.primary" fontWeight={600} sx={{ fontSize: "0.75rem" }}>
|
||||
{item.phase === "tool" ? formatToolTitle(item) : item.title}
|
||||
</Typography>
|
||||
<Stack direction="row" spacing={1} alignItems="center" justifyContent="space-between">
|
||||
<Typography variant="caption" color="text.primary" fontWeight={600} sx={{ fontSize: "0.75rem" }}>
|
||||
{item.phase === "tool" ? formatToolTitle(item) : item.title}
|
||||
</Typography>
|
||||
{stepElapsedMs !== undefined ? (
|
||||
<Typography
|
||||
variant="caption"
|
||||
color="text.secondary"
|
||||
sx={{ fontSize: "0.68rem", fontFamily: "var(--font-mono, monospace)" }}
|
||||
>
|
||||
{formatDuration(stepElapsedMs)}
|
||||
</Typography>
|
||||
) : null}
|
||||
</Stack>
|
||||
|
||||
{item.detail && (
|
||||
<Collapse in={expanded || isLast} timeout="auto">
|
||||
|
||||
Reference in New Issue
Block a user