sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -7,29 +7,37 @@ import { extractReasoningContentFromMessage } from "@/core/messages/utils";
import { getUsageMetadata } from "@/core/messages/usage";
import { textOfMessage } from "@/core/threads/utils";
/**
* Two characters ≈ one token. Same heuristic we use elsewhere; gives a stable
* speed indicator for mixed Chinese/English replies without dragging in a
* real tokeniser.
*/
const CHARS_PER_TOKEN = 2;
/** Sample throttle while a message is streaming. */
const SAMPLE_THROTTLE_MS = 60;
import {
GENERATION_IDLE_MS,
SAMPLE_THROTTLE_MS,
charsToTokensPerSec,
messageHasToolCalls,
pushSpeedSample,
rollingTokensPerSec,
shouldFreezeGeneration,
usageTokensPerSec,
type SpeedSample,
} from "./speed-metrics";
/**
* Per-AI-message metric: real-time during streaming, frozen once the run ends.
* Per-AI-message metric: live rolling rate during that model-call segment,
* frozen once the segment ends (not when the whole agent turn ends).
*/
export interface MessageMetric {
/** Rolling tokens/sec, or 0 when we cannot estimate yet. */
tokensPerSec: number;
/**
* Wall-clock duration from when the message first appeared with text to
* either *now* (active) or to when the stream ended (frozen).
* Wall-clock duration of this model-call segment: first visible character
* → last visible character (tool-wait after the call is excluded).
*/
durationMs: number;
/** True while this is the currently streaming message. */
/** True while this message is still generating visible tokens. */
isActive: boolean;
/**
* True while this message is the live generator but the rolling window
* has seen no new characters — the pill should not keep a stale tok/s.
*/
isPaused: boolean;
/**
* Whether the speed value was computed from real usage_metadata output
* tokens (``true``) vs. a char-count fallback (``false``). UI uses this
@@ -45,15 +53,12 @@ export interface MessageMetric {
isTurnFinal: boolean;
}
type Sample = { at: number; chars: number };
type TimingEntry = {
firstSeenAt: number;
endedAt: number | null;
samples: Sample[];
lastGrewAt: number;
samples: SpeedSample[];
lastChars: number;
/** True iff we *have already* observed `usage_metadata.output_tokens`. */
hadUsage: boolean;
};
function isAiMessage(message: Message): boolean {
@@ -82,11 +87,12 @@ function messageText(message: Message): string {
*
* Recording strategy:
* - Whenever a message renders with text, we anchor `firstSeenAt`.
* - On every text-length change we push a sample and recompute the rate.
* - When `isLoading` flips to false we mark *all* still-active messages as
* ended so subsequent renders return a frozen duration.
* - Token totals come from usage_metadata when present (most accurate);
* otherwise we fall back to char-count / 2.
* - On every text-length change we push a sample.
* - The segment freezes when this model call ends (next AI message, tool
* calls, usage + idle, or the run stops) so tool-wait is not in the
* denominator.
* - Live rate is a rolling window that decays to 0 on pause.
* - `usage_metadata.output_tokens` only corrects the frozen final value.
*/
export function useMessageMetrics(
messages: Message[],
@@ -104,7 +110,8 @@ export function useMessageMetrics(
// synchronously inside an effect so React isn't tracking the map identity.
useEffect(() => {
const now = performance.now();
let activeId: string | null = null;
const grewIds = new Set<string>();
let latestId: string | null = null;
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
@@ -118,50 +125,62 @@ export function useMessageMetrics(
entry = {
firstSeenAt: now,
endedAt: null,
lastGrewAt: now,
samples: [{ at: now, chars: text.length }],
lastChars: text.length,
hadUsage: false,
};
timingRef.current.set(id, entry);
}
// Track the most recent AI message as the active one (while streaming).
activeId = id;
latestId = id;
// Sample only when actively streaming OR when content actually grew —
// throttle to keep arrays bounded.
const grew = text.length !== entry.lastChars;
if (grew && now - lastSampleAtRef.current >= SAMPLE_THROTTLE_MS) {
entry.samples.push({ at: now, chars: text.length });
entry.lastChars = text.length;
if (entry.samples.length > 256) {
entry.samples.splice(0, entry.samples.length - 256);
if (grew) {
grewIds.add(id);
entry.lastGrewAt = now;
if (now - lastSampleAtRef.current >= SAMPLE_THROTTLE_MS) {
pushSpeedSample(entry.samples, now, text.length);
lastSampleAtRef.current = now;
}
lastSampleAtRef.current = now;
} else if (grew) {
// Still update lastChars so the next render captures it eventually.
entry.lastChars = text.length;
}
if (getUsageMetadata(message)) {
entry.hadUsage = true;
}
}
// When streaming stops, freeze any non-ended entry to the current time.
if (!isLoading) {
for (const entry of timingRef.current.values()) {
if (entry.endedAt === null) {
entry.endedAt = now;
}
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
if (!message || !isAiMessage(message)) continue;
const id = getMessageId(message, i);
const entry = timingRef.current.get(id);
if (!entry) continue;
const grewThisTick = grewIds.has(id);
if (grewThisTick && entry.endedAt != null && isLoading) {
entry.endedAt = null;
continue;
}
const usage = getUsageMetadata(message);
if (
shouldFreezeGeneration({
endedAt: entry.endedAt,
isLoading,
isLatestGenerating: id === latestId,
hasToolCalls: messageHasToolCalls(message),
grewThisTick,
lastGrewAt: entry.lastGrewAt,
now,
outputTokens: usage?.outputTokens ?? 0,
})
) {
entry.endedAt = entry.lastGrewAt;
}
}
lastActiveIdRef.current = activeId;
lastActiveIdRef.current = latestId;
setTick((v) => v + 1);
}, [messages, isLoading]);
// Drive a slow tick while streaming so live durations refresh smoothly.
// Drive a slow tick while streaming so live durations / pause decay refresh.
useEffect(() => {
if (!isLoading) return;
const interval = window.setInterval(() => setTick((v) => v + 1), 300);
@@ -198,45 +217,36 @@ export function useMessageMetrics(
const entry = timingRef.current.get(id);
if (!entry) continue;
const isActive =
isLoading && id === lastActiveIdRef.current && entry.endedAt === null;
const endedAt = entry.endedAt ?? now;
const durationMs = Math.max(0, endedAt - entry.firstSeenAt);
const isActive = isLoading && id === lastActiveIdRef.current;
let tokensPerSec = 0;
let fromUsage = false;
let isPaused = false;
const usage = getUsageMetadata(message);
if (usage && usage.outputTokens > 0 && durationMs > 0) {
tokensPerSec = (usage.outputTokens / durationMs) * 1000;
fromUsage = true;
} else {
// Char-count fallback. Uses the rolling window during streaming so a
// mid-stream snapshot reflects current speed rather than average since
// the start.
if (isActive && entry.samples.length >= 2) {
const samples = entry.samples;
let start = samples.findIndex((s) => s.at >= now - 1500);
if (start === -1) start = samples.length - 2;
if (start < 0) start = 0;
const head = samples[start];
const tail = samples[samples.length - 1];
if (head && tail) {
const elapsedMs = tail.at - head.at;
const chars = Math.max(0, tail.chars - head.chars);
if (elapsedMs > 0) {
tokensPerSec = (chars / elapsedMs) * 1000 / CHARS_PER_TOKEN;
}
}
if (entry.endedAt != null) {
if (usage && usage.outputTokens > 0 && durationMs > 0) {
tokensPerSec = usageTokensPerSec(usage.outputTokens, durationMs);
fromUsage = true;
} else if (durationMs > 0) {
// Finished message without usage data: average over full duration.
const chars = entry.lastChars;
tokensPerSec = (chars / durationMs) * 1000 / CHARS_PER_TOKEN;
tokensPerSec = charsToTokensPerSec(entry.lastChars, durationMs);
}
} else {
tokensPerSec = rollingTokensPerSec(entry.samples, entry.lastChars, now);
isPaused =
isActive &&
tokensPerSec <= 0 &&
now - entry.lastGrewAt >= GENERATION_IDLE_MS;
}
out.set(id, {
tokensPerSec,
durationMs,
isActive,
isPaused,
fromUsage,
isTurnFinal: turnFinalIds.has(id),
});