mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:29:37 +08:00
Bind mastery drafts before their opening turn and preserve stored session modes. Track chat duration from submission through terminal completion. Run async reading extensions on their event loop and allow retries after cancellable timeouts. Allow first uploads into empty LlamaIndex knowledge bases, preserve Telegram thread routing, parse embedding Retry-After dates, and enforce robots rules and host pacing during documentation crawls, including redirects. Fixes #1461 Fixes #1458 Fixes #1451 Fixes #1448 Fixes #1392 Fixes #1429 Refs #1435, #1412 Reuses contributions from PRs #1462, #1452 and #1403. Co-authored-by: sjagadee <srini.jagadeesh@gmail.com> Co-authored-by: AoHanBei <dongaohan21@gmail.com> Co-authored-by: Gao-Chenkai <DevChenkai@outlook.com>
77 lines
3.2 KiB
TypeScript
77 lines
3.2 KiB
TypeScript
/**
|
|
* Turn-level timing for the chat status header.
|
|
*
|
|
* Each assistant turn used to surface a duration label on every
|
|
* sub-trace ("Plan for 5s", "Round 3 · 8s", …). That made the
|
|
* trace card noisy and made it hard to tell at a glance how long
|
|
* the whole answer took. The current design hoists timing to the
|
|
* single ``DeepTutor reasoning…`` status row at the top of the
|
|
* answer: one number, ticking up while the turn is in flight and
|
|
* frozen once the turn is done.
|
|
*
|
|
* The clock derives its bounds from the event stream so reconnects
|
|
* keep the duration coherent — we never depend on a transient React
|
|
* state that resets on remount.
|
|
*/
|
|
|
|
import type { StreamEvent } from "@/features/chat/model/protocol";
|
|
|
|
/**
|
|
* Elapsed seconds for the turn the ``events`` belong to.
|
|
*
|
|
* Returns ``null`` when neither events nor submission bounds provide a
|
|
* timestamp. Optimistic messages carry their submission time so connection
|
|
* and provider latency are visible before the first server frame (#1435).
|
|
*
|
|
* While ``isStreaming`` is true the upper bound floats to
|
|
* ``nowSeconds`` so the label ticks up in real time; once streaming
|
|
* ends the bound collapses to the latest event timestamp and the
|
|
* duration freezes.
|
|
*
|
|
* ``bounds`` carries the turn's real span when the caller has it. A persisted
|
|
* turn arrives as a *preview* — only its tool and terminal events survive —
|
|
* and the events that mark where the turn began are never among them. Timing
|
|
* the preview alone therefore starts the clock at the first tool call and
|
|
* discards however long the model spent thinking beforehand. A round that
|
|
* thinks and then answers in a single burst has its entire duration in that
|
|
* discarded span, and rendered as ``0s``.
|
|
*/
|
|
export function getTurnDurationSeconds(
|
|
events: StreamEvent[],
|
|
nowSeconds: number,
|
|
isStreaming: boolean,
|
|
bounds?: { started_at?: number | null; ended_at?: number | null } | null,
|
|
): number | null {
|
|
let min = Number.POSITIVE_INFINITY;
|
|
let max = 0;
|
|
for (const event of events) {
|
|
const ts = event.timestamp;
|
|
if (typeof ts !== "number") continue;
|
|
if (ts < min) min = ts;
|
|
if (ts > max) max = ts;
|
|
}
|
|
const startedAt = bounds?.started_at;
|
|
if (typeof startedAt === "number" && startedAt < min) min = startedAt;
|
|
const endedAt = bounds?.ended_at;
|
|
if (typeof endedAt === "number" && endedAt > max) max = endedAt;
|
|
if (!Number.isFinite(min)) return null;
|
|
// Still live: keep ticking against the wall clock. The recorded end is only
|
|
// authoritative once the turn has stopped producing events.
|
|
const end = isStreaming ? Math.max(nowSeconds, max) : max;
|
|
return Math.max(0, end - min);
|
|
}
|
|
|
|
/** Compact human-readable duration: ``"12s"``, ``"1m 4s"``, ``"1h 2m"``. */
|
|
export function formatTurnDuration(seconds: number): string {
|
|
const total = Math.max(0, Math.round(seconds));
|
|
if (total < 60) return `${total}s`;
|
|
const minutes = Math.floor(total / 60);
|
|
const remSeconds = total % 60;
|
|
if (minutes < 60) {
|
|
return remSeconds === 0 ? `${minutes}m` : `${minutes}m ${remSeconds}s`;
|
|
}
|
|
const hours = Math.floor(minutes / 60);
|
|
const remMinutes = minutes % 60;
|
|
return remMinutes === 0 ? `${hours}h` : `${hours}h ${remMinutes}m`;
|
|
}
|