import chalk from 'chalk';
export class TokenUsageDisplay {
formatNumber(num) {
return num.toLocaleString();
}
formatCacheTokens(tokens = {}) {
const totalCacheRead = tokens.cacheRead !== undefined ? tokens.cacheRead : (((tokens.anthropic || {}).cacheRead || 0) + ((tokens.openai || {}).cachedPrompt || 0));
const totalCacheWrite = tokens.cacheWrite !== undefined ? tokens.cacheWrite : ((tokens.anthropic || {}).cacheCreation || 0);
const totalCache = tokens.cacheTotal !== undefined ? tokens.cacheTotal : (totalCacheRead + totalCacheWrite);
return {
read: this.formatNumber(totalCacheRead),
write: this.formatNumber(totalCacheWrite),
total: this.formatNumber(totalCache)
};
}
format(usage) {
const contextWindow = usage.contextWindow || 100;
const current = usage.current || {};
const formatted = {
contextWindow: this.formatNumber(contextWindow),
current: {
request: this.formatNumber(current.request || 0),
response: this.formatNumber(current.response || 0),
total: this.formatNumber(current.total || 0),
cacheRead: this.formatNumber(current.cacheRead || 0),
cacheWrite: this.formatNumber(current.cacheWrite || 0),
cache: this.formatCacheTokens(current)
},
total: {
request: this.formatNumber((usage.total || {}).request || 0),
response: this.formatNumber((usage.total || {}).response || 0),
total: this.formatNumber((usage.total || {}).total || 0),
cacheRead: this.formatNumber((usage.total || {}).cacheRead || 0),
cacheWrite: this.formatNumber((usage.total || {}).cacheWrite || 0),
cache: this.formatCacheTokens(usage.total || {})
}
};
return formatted;
}
}