Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

1.9 KB
Raw Blame
import { formatOutputTokenRateValue } from '@/lib/turnUsageTooltip';

export function formatRunningTokenCount(tokenUsage: number): string {
  return tokenUsage >= 1000 ? `${(tokenUsage / 1000).toFixed(1)}k` : `${tokenUsage}`;
}

export function formatLiveOutputTokenRate(
  outputTokens: number,
  durationMs: number,
  generationReliable: boolean,
): string | null {
  if (!generationReliable) return null;
  return formatOutputTokenRateValue(outputTokens, durationMs);
}

export type RunningUsageMeta =
  { kind: 'rate'; rate: string } | { kind: 'tokens' } | { kind: 'none' };

/**
 * Live status prefers generation-only TPS when the harness can prove it.
 * Otherwise fall back to cumulative tokens as soon as they exist. Hiding
 * tokens for the whole `generationActive` window leaves a long empty timer
 * because live output often arrives only at message end.
 */
export function resolveRunningUsageMeta(input: {
  outputTokens: number;
  generationDurationMs: number;
  generationReliable: boolean;
  tokenUsage: number;
  /** Latest paired sample for the current turn; historical points alone are insufficient. */
  latestRate?: number | null;
}): RunningUsageMeta {
  const rate = formatLiveOutputTokenRate(
    input.outputTokens,
    input.generationDurationMs,
    input.generationReliable,
  );
  if (rate) return { kind: 'rate', rate };
  if (
    input.latestRate === 0 &&
    input.outputTokens === 0 &&
    input.generationReliable &&
    Number.isFinite(input.generationDurationMs) &&
    input.generationDurationMs > 0
  )
    return { kind: 'rate', rate: '0' };
  if (input.tokenUsage > 0) return { kind: 'tokens' };
  return { kind: 'none' };
}

/** Keep recent and peak rates consistent with the cumulative rate's low-speed threshold. */
export function formatRecentOutputTokenRate(rate: number): string | null {
  return rate === 0 ? '0' : formatOutputTokenRateValue(rate, 1000);
}