1- import type { CostMode , CurrentSessionInfo , SessionWindow , SessionWindowStats , SortOrder } from './types.internal.ts' ;
1+ import type { CostMode , SortOrder } from './types.internal.ts' ;
22import { readFile } from 'node:fs/promises' ;
33import { homedir } from 'node:os' ;
44import path from 'node:path' ;
5- import { unreachable } from '@core/errorutil' ;
5+
6+ function unreachable ( x : never ) : never {
7+ throw new Error ( `Unreachable case: ${ String ( x ) } ` ) ;
8+ }
9+
610import { sort } from 'fast-sort' ;
711import { glob } from 'tinyglobby' ;
812import * as v from 'valibot' ;
@@ -15,6 +19,50 @@ export function getDefaultClaudePath(): string {
1519 return path . join ( homedir ( ) , '.claude' ) ;
1620}
1721
22+ const SessionWindowSchema = v . object ( {
23+ windowId : v . string ( ) , // Format: YYYY-MM-DD-HH (start of 5-hour window)
24+ startTime : v . string ( ) , // ISO timestamp of first message in window
25+ endTime : v . string ( ) , // ISO timestamp of last message in window
26+ inputTokens : v . number ( ) ,
27+ outputTokens : v . number ( ) ,
28+ cacheCreationTokens : v . number ( ) ,
29+ cacheReadTokens : v . number ( ) ,
30+ totalCost : v . number ( ) ,
31+ messageCount : v . number ( ) , // Number of messages in this window
32+ conversationCount : v . number ( ) , // Number of unique conversations in this window
33+ } ) ;
34+
35+ type SessionWindow = v . InferOutput < typeof SessionWindowSchema > ;
36+
37+ const CurrentSessionInfoSchema = v . object ( {
38+ hasActiveSession : v . boolean ( ) ,
39+ timeRemainingMs : v . number ( ) ,
40+ timeRemainingFormatted : v . string ( ) ,
41+ activeWindow : v . optional ( SessionWindowSchema ) ,
42+ } ) ;
43+
44+ type CurrentSessionInfo = v . InferOutput < typeof CurrentSessionInfoSchema > ;
45+
46+ // eslint-disable-next-line ts/no-unused-vars
47+ const _SessionWindowStatsSchema = v . object ( {
48+ month : v . pipe (
49+ v . string ( ) ,
50+ v . regex ( / ^ \d { 4 } - \d { 2 } $ / ) , // YYYY-MM format
51+ ) ,
52+ totalSessions : v . number ( ) , // Total number of 5-hour windows with activity
53+ remainingSessions : v . number ( ) , // sessionLimit - totalSessions
54+ utilizationPercent : v . number ( ) , // (totalSessions / sessionLimit) * 100
55+ sessionLimit : v . number ( ) , // Plan-specific session limit (50 for Max plan, etc.)
56+ totalCost : v . number ( ) ,
57+ totalTokens : v . number ( ) ,
58+ averageCostPerSession : v . number ( ) ,
59+ averageTokensPerSession : v . number ( ) ,
60+ windows : v . array ( SessionWindowSchema ) ,
61+ currentSession : v . optional ( CurrentSessionInfoSchema ) ,
62+ } ) ;
63+
64+ export type SessionWindowStats = v . InferOutput < typeof _SessionWindowStatsSchema > ;
65+
1866export const UsageDataSchema = v . object ( {
1967 timestamp : v . string ( ) ,
2068 version : v . optional ( v . string ( ) ) , // Claude Code version
@@ -125,6 +173,8 @@ export type LoadOptions = {
125173 claudePath ?: string ; // Custom path to Claude data directory
126174 mode ?: CostMode ; // Cost calculation mode
127175 order ?: SortOrder ; // Sort order for dates
176+ sessionLimit ?: number ; // Monthly session limit
177+ warningThreshold ?: number ; // Warning threshold for remaining sessions
128178} & DateFilter ;
129179
130180export async function loadDailyUsageData (
@@ -636,7 +686,12 @@ export async function loadSessionWindowData(
636686 let newWindowStart = entry . timestamp ;
637687 let newWindowId = getSessionWindowId ( entry . data . timestamp ) ;
638688
639- // Ensure we don't overlap with existing windows
689+ // Ensure we don't overlap with existing windows. If a window already exists
690+ // for the calculated time slot, shift to the next 5-hour window until we
691+ // find an empty slot. This handles edge cases where multiple conversation
692+ // windows might start very close in time, ensuring each gets its own window ID.
693+ // Note: The resulting windowId might not directly correspond to the message's
694+ // actual timestamp, but this maintains unique window identification.
640695 while ( sessionWindows . has ( newWindowId ) ) {
641696 newWindowStart = new Date ( newWindowStart . getTime ( ) + 5 * 60 * 60 * 1000 ) ;
642697 newWindowId = getSessionWindowId ( newWindowStart . toISOString ( ) ) ;
@@ -703,8 +758,9 @@ export async function loadSessionWindowData(
703758 }
704759
705760 const totalSessions = windows . length ;
706- const remainingSessions = Math . max ( 0 , 50 - totalSessions ) ;
707- const utilizationPercent = ( totalSessions / 50 ) * 100 ;
761+ const sessionLimit = options ?. sessionLimit ?? 50 ;
762+ const remainingSessions = Math . max ( 0 , sessionLimit - totalSessions ) ;
763+ const utilizationPercent = ( totalSessions / sessionLimit ) * 100 ;
708764
709765 const totals = windows . reduce ( ( acc , window ) => ( {
710766 cost : acc . cost + window . totalCost ,
@@ -719,6 +775,7 @@ export async function loadSessionWindowData(
719775 totalSessions,
720776 remainingSessions,
721777 utilizationPercent,
778+ sessionLimit,
722779 totalCost : totals . cost ,
723780 totalTokens : totals . tokens ,
724781 averageCostPerSession : totalSessions > 0 ? totals . cost / totalSessions : 0 ,
0 commit comments