fix: avoid invented task percentages

This commit is contained in:
134 2026-07-31 14:48:41 +08:00
parent 55be81d3d8
commit 7c9fd8a796
2 changed files with 27 additions and 18 deletions

View File

@ -240,8 +240,8 @@ type OpenClawBackgroundTask = {
displayText: string;
updatedAt: string;
completed: number;
total: number;
progressPercent: number;
total: number | null;
progressPercent: number | null;
pollEndpoint: string;
pollAfterMs: number;
maxWaitSeconds: number;
@ -269,16 +269,17 @@ function parseOpenClawBackgroundTask(
if (!jobId || !state) return null;
const completed = Math.max(0, Number(rawTask.completed) || 0);
const total = Math.max(1, Number(rawTask.total) || 1);
const progressPercent = Math.max(
0,
Math.min(
100,
Number.isFinite(Number(rawTask.progress_percent))
? Number(rawTask.progress_percent)
: Math.round((completed * 100) / total)
)
);
const parsedTotal = Number(rawTask.total);
const total =
Number.isFinite(parsedTotal) && parsedTotal > 0 ? parsedTotal : null;
const parsedProgressPercent = Number(rawTask.progress_percent);
const progressPercent =
rawTask.progress_percent != null &&
Number.isFinite(parsedProgressPercent)
? Math.max(0, Math.min(100, parsedProgressPercent))
: total
? Math.round((completed * 100) / total)
: null;
return {
jobId,
@ -323,8 +324,10 @@ function buildOpenClawTaskProgress(task: OpenClawBackgroundTask) {
task.phase ||
(task.state === "processing" ? "任务正在处理" : "任务状态已更新"),
completed: task.completed,
total: task.total,
progress_percent: task.progressPercent,
...(task.total != null ? { total: task.total } : {}),
...(task.progressPercent != null
? { progress_percent: task.progressPercent }
: {}),
stage: task.stage,
state: task.state,
updated_at: task.updatedAt,
@ -524,7 +527,7 @@ function streamOpenClawBackgroundTask(params: {
nextTask.stage,
nextTask.updatedAt,
nextTask.completed,
nextTask.total,
nextTask.total ?? "",
progress.message,
].join("|");
if (key === lastProgressKey) return;
@ -572,8 +575,10 @@ function streamOpenClawBackgroundTask(params: {
message: "任务仍在运行,正在重新连接进度服务",
display: "任务仍在运行,正在重新连接进度服务",
completed: task.completed,
total: task.total,
progress_percent: task.progressPercent,
...(task.total != null ? { total: task.total } : {}),
...(task.progressPercent != null
? { progress_percent: task.progressPercent }
: {}),
})
)
);

View File

@ -543,8 +543,12 @@ function normalizeClawdProgressEvent(
currentOrderId,
completed:
Number.isFinite(completed) && completed >= 0 ? completed : undefined,
total: Number.isFinite(total) && total > 0 ? total : undefined,
total:
progress.total != null && Number.isFinite(total) && total > 0
? total
: undefined,
progressPercent:
progress.progress_percent != null &&
Number.isFinite(progressPercent) &&
progressPercent >= 0 &&
progressPercent <= 100