Files
Agent-Workbench/src/client/ModelUsageDetails.tsx
T
2026-11-07 11:20:02 +08:00

31 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { tokenAccountingView, type TokenMetric, type TokenUsage } from '../shared/contracts/model-usage.mjs';
import './ModelUsageDetails.css';
const count = (metric: TokenMetric) => metric.status === 'invalid' ? '上游数值无效' : metric.tokens === null ? '上游未报告' : metric.tokens.toLocaleString('zh-CN') + ' tokens';
const protocols: Record<string, string> = { 'anthropic-messages': 'Messages', responses: 'Responses', 'chat-completions': 'Chat' };
const labels = { cacheRead: '缓存读取', cacheWrite: '缓存写入', uncached: '未缓存输入', reasoning: '推理输出' } as const;
const sourceLabels = { input: '全部输入', output: '全部输出', total: '合计', ...labels } as const;
const checkLabel = (value: string) => value === 'consistent' ? '一致' : value === 'inconsistent' ? '不一致' : '尚未完整报告';
const lifetimeNames: Record<string, string> = { ephemeral_5m_input_tokens: '5 分钟写入', ephemeral_1h_input_tokens: '1 小时写入', ttl_30m_tokens: '30 分钟写入', ttl_12h_tokens: '12 小时写入' };
export function ModelUsageDetails({ usage, expanded = false }: { usage: TokenUsage | null | undefined; expanded?: boolean }) {
const view = tokenAccountingView(usage);
if (!view) return null;
return <details className="model-usage-details" open={expanded || undefined}>
<summary>缓存与用量明细</summary>
<dl>{(Object.keys(labels) as (keyof typeof labels)[]).map(key => <div key={key}>
<dt>{key === 'uncached' && view.scheme === 'read-and-miss' ? '未命中缓存输入' : labels[key]}{view.metrics[key].status === 'derived' && <small>按上游用量计算</small>}</dt>
<dd>{count(view.metrics[key])}</dd>
</div>)}{view.lifetimes.map(value => <div key={value.name}><dt>{lifetimeNames[value.name] ?? value.name}</dt><dd>{count(value)}</dd></div>)}</dl>
{view.issues.length > 0 && <p className="model-usage-warning" role="status">上游用量字段存在不一致或无效值,原值已保留,请查看来源与原响应。</p>}
<p>缓存写入不等于命中;推理已包含在输出中。这些 token 统计不代表实际账单。</p>
<details className="model-usage-sources"><summary>用量来源与核对</summary>
<p>{protocols[view.protocol] ?? view.protocol} · {view.projection === 'original-usage' ? '原始上游 usage' : '旧归一记录'}<br />输入分类合计{checkLabel(view.checks.inputPartition)} · 输入输出合计{checkLabel(view.checks.total)}{view.lifetimes.length > 0 && <> · 写入时长分类{checkLabel(view.checks.cacheWriteLifetimes)}</>}</p>
<dl>{(Object.keys(sourceLabels) as (keyof typeof sourceLabels)[]).map(key => <div key={key}><dt>{sourceLabels[key]}</dt><dd><code>{view.metrics[key].sources.join(';')}</code></dd></div>)}
{view.lifetimes.map(value => <div key={value.name}><dt>{lifetimeNames[value.name] ?? value.name}</dt><dd><code>{value.sources.join(';')}</code></dd></div>)}
</dl>
{view.issues.length > 0 && <code>{view.issues.join(' · ')}</code>}
</details>
</details>;
}