/** * ============================================================ * src/ui/StatusLine.tsx —— 底部状态栏 * ------------------------------------------------------------ * 显示: * cwd ~/proj · {provider}/{model} · tokens 12.3K / 200K · 距压缩 7.7K * 接近压缩阈值时 tokens / 距压缩 变黄;超过阈值时变红。 * ============================================================ */ import React from 'react'; import { Box, Text } from 'ink'; import { homedir } from 'node:os'; import { sep } from 'node:path'; import { getWorkingDir } from '../bootstrap/workingDir.js'; import type { Message, Provider } from '../types.js'; import type { ContextUsageInfo } from './hooks/useChat.js'; import { useTokenUsage } from './hooks/useTokenUsage.js'; import { theme } from './theme.js'; import { terminalSingleLine } from './terminalText.js'; interface Props { provider: Provider; history: Message[]; /** * 内核每轮算出的真实上下文用量(见 loop.ts 的 context_usage 事件),比本组件 * 自算的 useTokenUsage 估算更准(含 tools-schema 开销 + 真实 provider usage)。 * 缺省/null(如刚启动、尚未跑过一轮)时降级回 useTokenUsage 估算。 */ contextUsage?: ContextUsageInfo | null; } /** * React.memo 包装。 * * props: provider 引用启动后稳定;history 引用变化(useMemo slice)但内容变化 * 才需要重算 useTokenUsage —— React.memo 浅比较 history 引用就够了(slice 后引用 * 变了说明真有新消息);pluginProgress 仅插件循环时更新。 * * 流式 streamingText 高频更新不影响 history 引用,StatusLine 不会被带着 rerender。 */ export const StatusLine = React.memo(function StatusLine({ provider, history, contextUsage, }: Props): JSX.Element { const estimated = useTokenUsage(history, provider); const usage = contextUsage ? { tokens: contextUsage.usedTokens, threshold: contextUsage.compactThreshold, contextWindow: contextUsage.contextWindow, toThreshold: contextUsage.compactThreshold - contextUsage.usedTokens, } : estimated; const ratio = usage.threshold > 0 ? usage.tokens / usage.threshold : 1; const color = ratio >= 1 ? theme.error : ratio >= 0.7 ? theme.warning : theme.ok; const cwdDisplay = shortenPath(getWorkingDir()); const cacheHit = contextUsage?.promptCacheUsage?.reported ? formatCacheHit(contextUsage) : null; return ( cwd {terminalSingleLine(cwdDisplay)} {' · '} {terminalSingleLine(`${provider.name}/${provider.model}`)} {' · '} tokens {fmt(usage.tokens)} / {fmt(usage.contextWindow)} {' · '} {usage.threshold <= 0 ? '无输入空间' : usage.toThreshold > 0 ? `距压缩 ${fmt(usage.toThreshold)}` : '上下文已到压缩阈值'} {cacheHit && ( <> {' · '} 缓存 {cacheHit} )} ); }); /** * 提示词缓存命中率显示串,provider 没报告缓存信息时返回 null(整段不渲染)。 * * 「没报告」与「报告了 0 命中」必须区分:前者是 provider 不支持自动前缀缓存(或没开 * include_usage),显示 0% 会误导排障;后者是真实的冷启动/前缀失配,值得显示出来。 * 这个区分由 loop.ts 只在有真实 usage 时才附带字段来保证。 */ export function formatCacheHit(usage?: ContextUsageInfo | null): string | null { if (!usage?.promptCacheUsage?.reported) return usage ? '未报告' : null; const read = usage.promptCacheUsage.cacheReadTokens; const write = usage.promptCacheUsage.cacheWriteTokens; if (read === undefined) return write === undefined ? '读取未报告' : `读取未报告 · 写入 ${fmt(write)}`; const total = usage.promptCacheUsage.promptTokens; if (total === undefined) return read > 0 ? `命中 ${fmt(read)}` : '冷启动 0%'; if (total <= 0) return '冷启动 0%'; const percent = Math.round((read / total) * 100); const miss = usage.promptCacheUsage.cacheMissTokens; return `${read === 0 ? '冷启动 ' : '命中 '}${percent}% · ${fmt(read)}${miss !== undefined ? ` · Miss ${fmt(miss)}` : ''}${write !== undefined ? ` · 写入 ${fmt(write)}` : ''}`; } /** 把整数格式化为 1.2K / 12.3K / 1.5M */ function fmt(n: number): string { if (n < 1000) return `${n}`; if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`; return `${(n / 1_000_000).toFixed(2)}M`; } /** * 把绝对路径精简成适合状态栏显示的形式: * - home 下 → 以 ~ 开头 * - 超过 40 字符 → 保留首段 + … + 末两段 */ export function shortenPath(abs: string): string { const home = homedir(); let p = abs; if (home && (p === home || p.startsWith(home + sep))) { p = '~' + p.slice(home.length); } if (p.length <= 40) return p; const parts = p.split(/[\\/]/).filter(Boolean); if (parts.length <= 3) return p; const head = p.startsWith('~') ? '~' : parts[0]; const tail = parts.slice(-2).join(sep); return `${head}${sep}…${sep}${tail}`; }