(records: UsageRecordSummary[], now = Date.now())
| 49 | const EXTRA_CARD_SPACE = 2 |
| 50 | |
| 51 | export function formatUsageSummary(records: UsageRecordSummary[], now = Date.now()): string { |
| 52 | const content: string[] = [] |
| 53 | content.push(`Usage summary · ${new Date(now).toISOString()}`) |
| 54 | content.push("") |
| 55 | content.push(`Visit ${HELP_URL} for up-to-date`) |
| 56 | content.push("information on rate limits and credits") |
| 57 | |
| 58 | if (records.length === 0) { |
| 59 | content.push("") |
| 60 | content.push("No providers are configured for usage tracking.") |
| 61 | return renderCard(content) |
| 62 | } |
| 63 | |
| 64 | records.forEach((record, index) => { |
| 65 | const plan = record.planType ? ` (plan: ${record.planType})` : "" |
| 66 | content.push("") |
| 67 | content.push(`${record.providerName}${plan}`) |
| 68 | |
| 69 | if (record.error) { |
| 70 | content.push(` Error : ${record.error}`) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | content.push(` Access : ${formatAccess(record)}`) |
| 75 | if (record.credits) { |
| 76 | content.push(` Credits : ${formatCredits(record.credits)}`) |
| 77 | } |
| 78 | |
| 79 | const tokensLine = formatTokenUsage(record.tokenUsage) |
| 80 | if (tokensLine) { |
| 81 | content.push(` Tokens : ${tokensLine}`) |
| 82 | } |
| 83 | |
| 84 | const costLine = formatCost(record.costSummary) |
| 85 | if (costLine) { |
| 86 | content.push(` Cost : ${costLine}`) |
| 87 | } |
| 88 | |
| 89 | const limitLines = formatLimits(record.limits, now) |
| 90 | if (limitLines) { |
| 91 | content.push(" Limits") |
| 92 | content.push(...limitLines.map((line) => ` ${line}`)) |
| 93 | } |
| 94 | |
| 95 | if (index < records.length - 1) { |
| 96 | content.push("") |
| 97 | content.push(" ───────────────────────────────────────────────") |
| 98 | } |
| 99 | }) |
| 100 | |
| 101 | return renderCard(content) |
| 102 | } |
| 103 | |
| 104 | function renderCard(lines: string[]): string { |
| 105 | const measuredWidth = Math.max(...lines.map((line) => line.length), 0) |
no test coverage detected