ccusage 11.0.2 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import "./pricing-fetcher-DHaTs-k2.js";
2
- import { DailyUsage, MonthlyUsage, SessionUsage } from "./data-loader-dbZm5kOW.js";
1
+ import "./pricing-fetcher-BkOpRIdx.js";
2
+ import { DailyUsage, MonthlyUsage, SessionUsage } from "./data-loader-CBwn9vk0.js";
3
3
 
4
4
  //#region src/calculate-cost.d.ts
5
5
  /**
@@ -0,0 +1,464 @@
1
+ import { CostMode, PricingFetcher, SortOrder } from "./pricing-fetcher-BkOpRIdx.js";
2
+ import { z } from "zod";
3
+
4
+ //#region src/session-blocks.internal.d.ts
5
+
6
+ /**
7
+ * Represents a single usage data entry loaded from JSONL files
8
+ */
9
+ type LoadedUsageEntry = {
10
+ timestamp: Date;
11
+ usage: {
12
+ inputTokens: number;
13
+ outputTokens: number;
14
+ cacheCreationInputTokens: number;
15
+ cacheReadInputTokens: number;
16
+ };
17
+ costUSD: number | null;
18
+ model: string;
19
+ version?: string;
20
+ };
21
+ /**
22
+ * Aggregated token counts for different token types
23
+ */
24
+ type TokenCounts = {
25
+ inputTokens: number;
26
+ outputTokens: number;
27
+ cacheCreationInputTokens: number;
28
+ cacheReadInputTokens: number;
29
+ };
30
+ /**
31
+ * Represents a session block (typically 5-hour billing period) with usage data
32
+ */
33
+ type SessionBlock = {
34
+ id: string; // ISO string of block start time
35
+ startTime: Date;
36
+ endTime: Date; // startTime + 5 hours (for normal blocks) or gap end time (for gap blocks)
37
+ actualEndTime?: Date; // Last activity in block
38
+ isActive: boolean;
39
+ isGap?: boolean; // True if this is a gap block
40
+ entries: LoadedUsageEntry[];
41
+ tokenCounts: TokenCounts;
42
+ costUSD: number;
43
+ models: string[];
44
+ };
45
+ /**
46
+ * Represents usage burn rate calculations
47
+ */
48
+ //#endregion
49
+ //#region src/data-loader.d.ts
50
+ /**
51
+ * Default path for Claude data directory
52
+ * Uses environment variable CLAUDE_CONFIG_DIR if set, otherwise defaults to ~/.claude
53
+ */
54
+ declare function getDefaultClaudePath(): string;
55
+ /**
56
+ * Zod schema for validating Claude usage data from JSONL files
57
+ */
58
+ declare const usageDataSchema: z.ZodObject<{
59
+ timestamp: z.ZodString;
60
+ version: z.ZodOptional<z.ZodString>;
61
+ message: z.ZodObject<{
62
+ usage: z.ZodObject<{
63
+ input_tokens: z.ZodNumber;
64
+ output_tokens: z.ZodNumber;
65
+ cache_creation_input_tokens: z.ZodOptional<z.ZodNumber>;
66
+ cache_read_input_tokens: z.ZodOptional<z.ZodNumber>;
67
+ }, "strip", z.ZodTypeAny, {
68
+ input_tokens: number;
69
+ output_tokens: number;
70
+ cache_creation_input_tokens?: number | undefined;
71
+ cache_read_input_tokens?: number | undefined;
72
+ }, {
73
+ input_tokens: number;
74
+ output_tokens: number;
75
+ cache_creation_input_tokens?: number | undefined;
76
+ cache_read_input_tokens?: number | undefined;
77
+ }>;
78
+ model: z.ZodOptional<z.ZodString>;
79
+ id: z.ZodOptional<z.ZodString>;
80
+ }, "strip", z.ZodTypeAny, {
81
+ usage: {
82
+ input_tokens: number;
83
+ output_tokens: number;
84
+ cache_creation_input_tokens?: number | undefined;
85
+ cache_read_input_tokens?: number | undefined;
86
+ };
87
+ model?: string | undefined;
88
+ id?: string | undefined;
89
+ }, {
90
+ usage: {
91
+ input_tokens: number;
92
+ output_tokens: number;
93
+ cache_creation_input_tokens?: number | undefined;
94
+ cache_read_input_tokens?: number | undefined;
95
+ };
96
+ model?: string | undefined;
97
+ id?: string | undefined;
98
+ }>;
99
+ costUSD: z.ZodOptional<z.ZodNumber>;
100
+ requestId: z.ZodOptional<z.ZodString>;
101
+ }, "strip", z.ZodTypeAny, {
102
+ timestamp: string;
103
+ version?: string | undefined;
104
+ message: {
105
+ usage: {
106
+ input_tokens: number;
107
+ output_tokens: number;
108
+ cache_creation_input_tokens?: number | undefined;
109
+ cache_read_input_tokens?: number | undefined;
110
+ };
111
+ model?: string | undefined;
112
+ id?: string | undefined;
113
+ };
114
+ costUSD?: number | undefined;
115
+ requestId?: string | undefined;
116
+ }, {
117
+ timestamp: string;
118
+ version?: string | undefined;
119
+ message: {
120
+ usage: {
121
+ input_tokens: number;
122
+ output_tokens: number;
123
+ cache_creation_input_tokens?: number | undefined;
124
+ cache_read_input_tokens?: number | undefined;
125
+ };
126
+ model?: string | undefined;
127
+ id?: string | undefined;
128
+ };
129
+ costUSD?: number | undefined;
130
+ requestId?: string | undefined;
131
+ }>;
132
+ /**
133
+ * Type definition for Claude usage data entries from JSONL files
134
+ */
135
+ type UsageData = z.infer<typeof usageDataSchema>;
136
+ /**
137
+ * Zod schema for model-specific usage breakdown data
138
+ */
139
+ declare const modelBreakdownSchema: z.ZodObject<{
140
+ modelName: z.ZodString;
141
+ inputTokens: z.ZodNumber;
142
+ outputTokens: z.ZodNumber;
143
+ cacheCreationTokens: z.ZodNumber;
144
+ cacheReadTokens: z.ZodNumber;
145
+ cost: z.ZodNumber;
146
+ }, "strip", z.ZodTypeAny, {
147
+ modelName: string;
148
+ inputTokens: number;
149
+ outputTokens: number;
150
+ cacheCreationTokens: number;
151
+ cacheReadTokens: number;
152
+ cost: number;
153
+ }, {
154
+ modelName: string;
155
+ inputTokens: number;
156
+ outputTokens: number;
157
+ cacheCreationTokens: number;
158
+ cacheReadTokens: number;
159
+ cost: number;
160
+ }>;
161
+ /**
162
+ * Type definition for model-specific usage breakdown
163
+ */
164
+ type ModelBreakdown = z.infer<typeof modelBreakdownSchema>;
165
+ /**
166
+ * Zod schema for daily usage aggregation data
167
+ */
168
+ declare const dailyUsageSchema: z.ZodObject<{
169
+ date: z.ZodString;
170
+ inputTokens: z.ZodNumber;
171
+ outputTokens: z.ZodNumber;
172
+ cacheCreationTokens: z.ZodNumber;
173
+ cacheReadTokens: z.ZodNumber;
174
+ totalCost: z.ZodNumber;
175
+ modelsUsed: z.ZodArray<z.ZodString, "many">;
176
+ modelBreakdowns: z.ZodArray<z.ZodObject<{
177
+ modelName: z.ZodString;
178
+ inputTokens: z.ZodNumber;
179
+ outputTokens: z.ZodNumber;
180
+ cacheCreationTokens: z.ZodNumber;
181
+ cacheReadTokens: z.ZodNumber;
182
+ cost: z.ZodNumber;
183
+ }, "strip", z.ZodTypeAny, {
184
+ modelName: string;
185
+ inputTokens: number;
186
+ outputTokens: number;
187
+ cacheCreationTokens: number;
188
+ cacheReadTokens: number;
189
+ cost: number;
190
+ }, {
191
+ modelName: string;
192
+ inputTokens: number;
193
+ outputTokens: number;
194
+ cacheCreationTokens: number;
195
+ cacheReadTokens: number;
196
+ cost: number;
197
+ }>, "many">;
198
+ }, "strip", z.ZodTypeAny, {
199
+ date: string;
200
+ inputTokens: number;
201
+ outputTokens: number;
202
+ cacheCreationTokens: number;
203
+ cacheReadTokens: number;
204
+ totalCost: number;
205
+ modelsUsed: string[];
206
+ modelBreakdowns: {
207
+ modelName: string;
208
+ inputTokens: number;
209
+ outputTokens: number;
210
+ cacheCreationTokens: number;
211
+ cacheReadTokens: number;
212
+ cost: number;
213
+ }[];
214
+ }, {
215
+ date: string;
216
+ inputTokens: number;
217
+ outputTokens: number;
218
+ cacheCreationTokens: number;
219
+ cacheReadTokens: number;
220
+ totalCost: number;
221
+ modelsUsed: string[];
222
+ modelBreakdowns: {
223
+ modelName: string;
224
+ inputTokens: number;
225
+ outputTokens: number;
226
+ cacheCreationTokens: number;
227
+ cacheReadTokens: number;
228
+ cost: number;
229
+ }[];
230
+ }>;
231
+ /**
232
+ * Type definition for daily usage aggregation
233
+ */
234
+ type DailyUsage = z.infer<typeof dailyUsageSchema>;
235
+ /**
236
+ * Zod schema for session-based usage aggregation data
237
+ */
238
+ declare const sessionUsageSchema: z.ZodObject<{
239
+ sessionId: z.ZodString;
240
+ projectPath: z.ZodString;
241
+ inputTokens: z.ZodNumber;
242
+ outputTokens: z.ZodNumber;
243
+ cacheCreationTokens: z.ZodNumber;
244
+ cacheReadTokens: z.ZodNumber;
245
+ totalCost: z.ZodNumber;
246
+ lastActivity: z.ZodString;
247
+ versions: z.ZodArray<z.ZodString, "many">;
248
+ modelsUsed: z.ZodArray<z.ZodString, "many">;
249
+ modelBreakdowns: z.ZodArray<z.ZodObject<{
250
+ modelName: z.ZodString;
251
+ inputTokens: z.ZodNumber;
252
+ outputTokens: z.ZodNumber;
253
+ cacheCreationTokens: z.ZodNumber;
254
+ cacheReadTokens: z.ZodNumber;
255
+ cost: z.ZodNumber;
256
+ }, "strip", z.ZodTypeAny, {
257
+ modelName: string;
258
+ inputTokens: number;
259
+ outputTokens: number;
260
+ cacheCreationTokens: number;
261
+ cacheReadTokens: number;
262
+ cost: number;
263
+ }, {
264
+ modelName: string;
265
+ inputTokens: number;
266
+ outputTokens: number;
267
+ cacheCreationTokens: number;
268
+ cacheReadTokens: number;
269
+ cost: number;
270
+ }>, "many">;
271
+ }, "strip", z.ZodTypeAny, {
272
+ sessionId: string;
273
+ projectPath: string;
274
+ inputTokens: number;
275
+ outputTokens: number;
276
+ cacheCreationTokens: number;
277
+ cacheReadTokens: number;
278
+ totalCost: number;
279
+ lastActivity: string;
280
+ versions: string[];
281
+ modelsUsed: string[];
282
+ modelBreakdowns: {
283
+ modelName: string;
284
+ inputTokens: number;
285
+ outputTokens: number;
286
+ cacheCreationTokens: number;
287
+ cacheReadTokens: number;
288
+ cost: number;
289
+ }[];
290
+ }, {
291
+ sessionId: string;
292
+ projectPath: string;
293
+ inputTokens: number;
294
+ outputTokens: number;
295
+ cacheCreationTokens: number;
296
+ cacheReadTokens: number;
297
+ totalCost: number;
298
+ lastActivity: string;
299
+ versions: string[];
300
+ modelsUsed: string[];
301
+ modelBreakdowns: {
302
+ modelName: string;
303
+ inputTokens: number;
304
+ outputTokens: number;
305
+ cacheCreationTokens: number;
306
+ cacheReadTokens: number;
307
+ cost: number;
308
+ }[];
309
+ }>;
310
+ /**
311
+ * Type definition for session-based usage aggregation
312
+ */
313
+ type SessionUsage = z.infer<typeof sessionUsageSchema>;
314
+ /**
315
+ * Zod schema for monthly usage aggregation data
316
+ */
317
+ declare const monthlyUsageSchema: z.ZodObject<{
318
+ month: z.ZodString;
319
+ inputTokens: z.ZodNumber;
320
+ outputTokens: z.ZodNumber;
321
+ cacheCreationTokens: z.ZodNumber;
322
+ cacheReadTokens: z.ZodNumber;
323
+ totalCost: z.ZodNumber;
324
+ modelsUsed: z.ZodArray<z.ZodString, "many">;
325
+ modelBreakdowns: z.ZodArray<z.ZodObject<{
326
+ modelName: z.ZodString;
327
+ inputTokens: z.ZodNumber;
328
+ outputTokens: z.ZodNumber;
329
+ cacheCreationTokens: z.ZodNumber;
330
+ cacheReadTokens: z.ZodNumber;
331
+ cost: z.ZodNumber;
332
+ }, "strip", z.ZodTypeAny, {
333
+ modelName: string;
334
+ inputTokens: number;
335
+ outputTokens: number;
336
+ cacheCreationTokens: number;
337
+ cacheReadTokens: number;
338
+ cost: number;
339
+ }, {
340
+ modelName: string;
341
+ inputTokens: number;
342
+ outputTokens: number;
343
+ cacheCreationTokens: number;
344
+ cacheReadTokens: number;
345
+ cost: number;
346
+ }>, "many">;
347
+ }, "strip", z.ZodTypeAny, {
348
+ month: string;
349
+ inputTokens: number;
350
+ outputTokens: number;
351
+ cacheCreationTokens: number;
352
+ cacheReadTokens: number;
353
+ totalCost: number;
354
+ modelsUsed: string[];
355
+ modelBreakdowns: {
356
+ modelName: string;
357
+ inputTokens: number;
358
+ outputTokens: number;
359
+ cacheCreationTokens: number;
360
+ cacheReadTokens: number;
361
+ cost: number;
362
+ }[];
363
+ }, {
364
+ month: string;
365
+ inputTokens: number;
366
+ outputTokens: number;
367
+ cacheCreationTokens: number;
368
+ cacheReadTokens: number;
369
+ totalCost: number;
370
+ modelsUsed: string[];
371
+ modelBreakdowns: {
372
+ modelName: string;
373
+ inputTokens: number;
374
+ outputTokens: number;
375
+ cacheCreationTokens: number;
376
+ cacheReadTokens: number;
377
+ cost: number;
378
+ }[];
379
+ }>;
380
+ /**
381
+ * Type definition for monthly usage aggregation
382
+ */
383
+ type MonthlyUsage = z.infer<typeof monthlyUsageSchema>;
384
+ /**
385
+ * Formats a date string to YYYY-MM-DD format
386
+ * @param dateStr - Input date string
387
+ * @returns Formatted date string in YYYY-MM-DD format
388
+ */
389
+ declare function formatDate(dateStr: string): string;
390
+ /**
391
+ * Formats a date string to compact format with year on first line and month-day on second
392
+ * @param dateStr - Input date string
393
+ * @returns Formatted date string with newline separator (YYYY\nMM-DD)
394
+ */
395
+ declare function formatDateCompact(dateStr: string): string;
396
+ /**
397
+ * Create a unique identifier for deduplication using message ID and request ID
398
+ */
399
+ declare function createUniqueHash(data: UsageData): string | null;
400
+ /**
401
+ * Extract the earliest timestamp from a JSONL file
402
+ * Scans through the file until it finds a valid timestamp
403
+ */
404
+ declare function getEarliestTimestamp(filePath: string): Promise<Date | null>;
405
+ /**
406
+ * Sort files by their earliest timestamp
407
+ * Files without valid timestamps are placed at the end
408
+ */
409
+ declare function sortFilesByTimestamp(files: string[]): Promise<string[]>;
410
+ /**
411
+ * Calculates cost for a single usage data entry based on the specified cost calculation mode
412
+ * @param data - Usage data entry
413
+ * @param mode - Cost calculation mode (auto, calculate, or display)
414
+ * @param fetcher - Pricing fetcher instance for calculating costs from tokens
415
+ * @returns Calculated cost in USD
416
+ */
417
+ declare function calculateCostForEntry(data: UsageData, mode: CostMode, fetcher: PricingFetcher): Promise<number>;
418
+ /**
419
+ * Date range filter for limiting usage data by date
420
+ */
421
+ type DateFilter = {
422
+ since?: string; // YYYYMMDD format
423
+ until?: string; // YYYYMMDD format
424
+ };
425
+ /**
426
+ * Configuration options for loading usage data
427
+ */
428
+ type LoadOptions = {
429
+ claudePath?: string; // Custom path to Claude data directory
430
+ mode?: CostMode; // Cost calculation mode
431
+ order?: SortOrder; // Sort order for dates
432
+ offline?: boolean; // Use offline mode for pricing
433
+ sessionDurationHours?: number; // Session block duration in hours
434
+ } & DateFilter;
435
+ /**
436
+ * Loads and aggregates Claude usage data by day
437
+ * Processes all JSONL files in the Claude projects directory and groups usage by date
438
+ * @param options - Optional configuration for loading and filtering data
439
+ * @returns Array of daily usage summaries sorted by date
440
+ */
441
+ declare function loadDailyUsageData(options?: LoadOptions): Promise<DailyUsage[]>;
442
+ /**
443
+ * Loads and aggregates Claude usage data by session
444
+ * Groups usage data by project path and session ID based on file structure
445
+ * @param options - Optional configuration for loading and filtering data
446
+ * @returns Array of session usage summaries sorted by last activity
447
+ */
448
+ declare function loadSessionData(options?: LoadOptions): Promise<SessionUsage[]>;
449
+ /**
450
+ * Loads and aggregates Claude usage data by month
451
+ * Uses daily usage data as the source and groups by month
452
+ * @param options - Optional configuration for loading and filtering data
453
+ * @returns Array of monthly usage summaries sorted by month
454
+ */
455
+ declare function loadMonthlyUsageData(options?: LoadOptions): Promise<MonthlyUsage[]>;
456
+ /**
457
+ * Loads usage data and organizes it into session blocks (typically 5-hour billing periods)
458
+ * Processes all usage data and groups it into time-based blocks for billing analysis
459
+ * @param options - Optional configuration including session duration and filtering
460
+ * @returns Array of session blocks with usage and cost information
461
+ */
462
+ declare function loadSessionBlockData(options?: LoadOptions): Promise<SessionBlock[]>;
463
+ //#endregion
464
+ export { DailyUsage, DateFilter, LoadOptions, ModelBreakdown, MonthlyUsage, SessionUsage, UsageData, calculateCostForEntry, createUniqueHash, dailyUsageSchema, formatDate, formatDateCompact, getDefaultClaudePath, getEarliestTimestamp, loadDailyUsageData, loadMonthlyUsageData, loadSessionBlockData, loadSessionData, modelBreakdownSchema, monthlyUsageSchema, sessionUsageSchema, sortFilesByTimestamp, usageDataSchema };