claude-monitor
Version:
Real-time terminal monitoring tool for Claude AI token usage
112 lines • 3.6 kB
JavaScript
export class TokenExtractor {
static extractTokens(data) {
const tokens = {
inputTokens: 0,
outputTokens: 0,
cacheCreationTokens: 0,
cacheReadTokens: 0,
totalTokens: 0,
};
if (data.usage) {
tokens.inputTokens = data.usage.input_tokens || 0;
tokens.outputTokens = data.usage.output_tokens || 0;
tokens.cacheCreationTokens = data.usage.cache_creation_input_tokens || 0;
tokens.cacheReadTokens = data.usage.cache_read_input_tokens || 0;
}
tokens.totalTokens =
tokens.inputTokens +
tokens.outputTokens +
tokens.cacheCreationTokens +
tokens.cacheReadTokens;
return tokens;
}
static hasValidTokens(tokens) {
return tokens.totalTokens > 0;
}
}
export class DataConverter {
static extractModelName(data, defaultModel = 'claude-3-5-sonnet') {
if (data.model) {
return data.model;
}
return defaultModel;
}
static flattenNestedDict(data, prefix = '') {
const result = {};
for (const [key, value] of Object.entries(data)) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
Object.assign(result, this.flattenNestedDict(value, newKey));
}
else {
result[newKey] = value;
}
}
return result;
}
static toSerializable(obj) {
if (obj instanceof Date) {
return obj.toISOString();
}
if (obj === null || obj === undefined) {
return obj;
}
if (typeof obj === 'object') {
if (Array.isArray(obj)) {
return obj.map((item) => this.toSerializable(item));
}
const result = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = this.toSerializable(value);
}
return result;
}
return obj;
}
}
export class TimestampProcessor {
static parseTimestamp(timestampValue) {
if (!timestampValue) {
return null;
}
try {
if (timestampValue instanceof Date) {
return timestampValue;
}
if (typeof timestampValue === 'string') {
let timestamp = timestampValue;
if (timestamp.endsWith('Z')) {
timestamp = timestamp.slice(0, -1) + '+00:00';
}
const date = new Date(timestamp);
if (!isNaN(date.getTime())) {
return date;
}
}
if (typeof timestampValue === 'number') {
const timestamp = timestampValue > 10000000000 ? timestampValue : timestampValue * 1000;
return new Date(timestamp);
}
}
catch (error) {
}
return null;
}
static ensureTimezone(date) {
return date;
}
}
export class MetadataExtractor {
static extractMessageId(data) {
return data.message_id || '';
}
static extractRequestId(data) {
return data.request_id || 'unknown';
}
static createUniqueId(data) {
const messageId = this.extractMessageId(data);
const requestId = this.extractRequestId(data);
return `${messageId}:${requestId}`;
}
}
//# sourceMappingURL=data-processors.js.map