Fix active run elapsed restore

This commit is contained in:
wuyang6
2026-05-11 21:46:09 +08:00
parent 535e4b2c83
commit f7e2fc66c8
3 changed files with 41 additions and 4 deletions
+1
View File
@@ -1357,6 +1357,7 @@ def create_app(state: AgentState) -> FastAPI:
started_event = {
'type': 'run_started',
'run_id': run_record.run_id,
'started_at': run_record.started_at,
'session_id': requested_session_id or '',
'account_id': request.account_id or '',
}
@@ -78,6 +78,9 @@ type ClawActiveRunStatus = ClawRunStatus & {
type ClawRunEvent = {
type?: string;
started_at?: unknown;
recorded_at?: unknown;
elapsed_ms?: unknown;
tool_name?: string;
tool_call_id?: string;
arguments?: unknown;
@@ -500,6 +503,7 @@ export function toReplayRepository(
}
if (isActiveRunStatus(runStatus)) {
const id = `${fallbackSessionId}-run-${runStatus.run_id ?? "active"}`;
const runStartedAtMs = resolveRunStartedAtMs(runStatus);
const parts = buildActiveRunParts(runStatus) as UIMessage["parts"];
const uiMessage: UIMessage = {
id,
@@ -508,7 +512,7 @@ export function toReplayRepository(
metadata: {
sessionId: session.session_id ?? fallbackSessionId,
runId: runStatus.run_id,
runStartedAtMs: normalizeRunTimestampMs(runStatus.started_at),
runStartedAtMs,
},
} as UIMessage;
const threadMessage = {
@@ -525,7 +529,7 @@ export function toReplayRepository(
custom: {
sessionId: session.session_id ?? fallbackSessionId,
runId: runStatus.run_id,
runStartedAtMs: normalizeRunTimestampMs(runStatus.started_at),
runStartedAtMs,
},
},
} as ThreadMessage;
@@ -552,7 +556,7 @@ function buildActiveRunParts(runStatus: ClawActiveRunStatus) {
? "当前会话已有任务在执行,本轮正在排队。"
: runStatus.current_stage || "后端正在执行这个会话。";
const reasoningText = eventLines.length ? eventLines.join("\n") : fallback;
const runStartedAtMs = normalizeRunTimestampMs(runStatus.started_at);
const runStartedAtMs = resolveRunStartedAtMs(runStatus);
return [
{ type: "reasoning", text: reasoningText, runStartedAtMs },
...buildRunToolParts(runStatus.events ?? []),
@@ -723,6 +727,34 @@ function normalizeElapsedMs(value: unknown) {
return Math.max(0, Math.round(value));
}
function resolveRunStartedAtMs(runStatus: ClawRunStatus) {
const direct = normalizeRunTimestampMs(runStatus.started_at);
if (direct !== undefined) return direct;
for (const event of runStatus.events ?? []) {
if (event.type !== "run_started") continue;
const eventStarted = normalizeRunTimestampMs(event.started_at);
if (eventStarted !== undefined) return eventStarted;
const recordedAt = normalizeRunTimestampMs(event.recorded_at);
if (recordedAt !== undefined) return recordedAt;
}
for (const event of runStatus.events ?? []) {
const recordedAt = normalizeRunTimestampMs(event.recorded_at);
const elapsedMs = normalizeElapsedMs(event.elapsed_ms);
if (recordedAt !== undefined && elapsedMs !== undefined) {
return Math.max(0, recordedAt - elapsedMs);
}
}
for (const event of runStatus.events ?? []) {
const recordedAt = normalizeRunTimestampMs(event.recorded_at);
if (recordedAt !== undefined) return recordedAt;
}
return undefined;
}
function resolveReplayElapsedMs(
messages: readonly ClawStoredMessage[],
index: number,
@@ -1475,7 +1475,11 @@ const ActivityChainSummary: FC<{
return;
}
const startedAt =
metadataRunStartedAtMs ?? contentRunStartedAtMs ?? startedAtRef.current;
metadataRunStartedAtMs ??
contentRunStartedAtMs ??
(storedElapsedMs === null
? startedAtRef.current
: Date.now() - storedElapsedMs);
setElapsedMs(Math.max(0, Date.now() - startedAt));
const timer = window.setInterval(() => {
setElapsedMs(Math.max(0, Date.now() - startedAt));