@@ -65,30 +65,36 @@ export async function main(argv = process.argv.slice(2), io = { stdout: process.
6565 }
6666 const locale = args . locale ?? DEFAULT_LOCALE ;
6767 const mode = args . command === 'sessions' ? 'session' : args . command ;
68- const dataPaths = await resolveCodexDataPaths ( {
69- codexHome : args . codexHome ,
70- sessionsDir : args . sessionsDir ,
71- } ) ;
72- const pricingMode = await resolvePricingMode ( args , dataPaths . codexHome ) ;
73- const report = await collectUsage ( {
74- dataPaths,
75- since,
76- until,
77- timezone,
78- cacheFile : args . cacheFile ,
79- pricingCacheFile : args . pricingCacheFile ,
80- pricingOffline : args . offline ,
81- pricingTtlMs : args . pricingTtlHours != null ? Number ( args . pricingTtlHours ) * 60 * 60 * 1000 : undefined ,
82- pricingFetchTimeoutMs : args . pricingFetchTimeoutMs != null ? Number ( args . pricingFetchTimeoutMs ) : undefined ,
83- pricingTier : pricingMode . tier ,
84- pricingPriorityModels : pricingMode . priorityModels ,
85- maxCacheBytes : args . maxCacheBytes != null ? Number ( args . maxCacheBytes ) : undefined ,
86- useCache : ! args . noCache ,
87- clearCache : args . clearCache ,
88- saveCache : ! args . noSaveCache ,
89- discoveryMode : args . discovery ,
90- includePricing : ! args . noPricing ,
91- } ) ;
68+ let report ;
69+ try {
70+ const dataPaths = await resolveCodexDataPaths ( {
71+ codexHome : args . codexHome ,
72+ sessionsDir : args . sessionsDir ,
73+ } ) ;
74+ const pricingMode = await resolvePricingMode ( args , dataPaths . codexHome ) ;
75+ report = await collectUsage ( {
76+ dataPaths,
77+ since,
78+ until,
79+ timezone,
80+ cacheFile : args . cacheFile ,
81+ pricingCacheFile : args . pricingCacheFile ,
82+ pricingOffline : args . offline ,
83+ pricingTtlMs : args . pricingTtlHours != null ? Number ( args . pricingTtlHours ) * 60 * 60 * 1000 : undefined ,
84+ pricingFetchTimeoutMs : args . pricingFetchTimeoutMs != null ? Number ( args . pricingFetchTimeoutMs ) : undefined ,
85+ pricingTier : pricingMode . tier ,
86+ pricingPriorityModels : pricingMode . priorityModels ,
87+ maxCacheBytes : args . maxCacheBytes != null ? Number ( args . maxCacheBytes ) : undefined ,
88+ useCache : ! args . noCache ,
89+ clearCache : args . clearCache ,
90+ saveCache : ! args . noSaveCache ,
91+ discoveryMode : args . discovery ,
92+ includePricing : ! args . noPricing ,
93+ } ) ;
94+ } catch ( error ) {
95+ io . stderr . write ( `${ error ?. message ?? String ( error ) } \n` ) ;
96+ return 1 ;
97+ }
9298
9399 const rows = rowsForMode ( report , mode , { locale, sort : args . sort , order : args . order } ) ;
94100 if ( args . json ) {
@@ -248,6 +254,11 @@ export function parseArgs(argv) {
248254 if ( token . startsWith ( '--' ) ) {
249255 throw new Error ( `Unknown option: ${ token } ` ) ;
250256 }
257+ if ( COMMANDS . has ( token ) && ! args . commandSpecified ) {
258+ args . command = token ;
259+ args . commandSpecified = true ;
260+ break ;
261+ }
251262 throw new Error ( `Unknown command or argument: ${ token } ` ) ;
252263 }
253264 }
@@ -309,11 +320,21 @@ function compareRows(a, b, sort, mode) {
309320
310321function compareDefault ( a , b , mode ) {
311322 if ( mode === 'session' ) {
312- return String ( a . lastActivity ?? '' ) . localeCompare ( String ( b . lastActivity ?? '' ) ) ;
323+ return compareTimestamp ( a . lastActivity , b . lastActivity ) ;
313324 }
314325 return String ( a . key ?? '' ) . localeCompare ( String ( b . key ?? '' ) ) ;
315326}
316327
328+ function compareTimestamp ( left , right ) {
329+ return toSortableTimestamp ( left ) . localeCompare ( toSortableTimestamp ( right ) ) ;
330+ }
331+
332+ function toSortableTimestamp ( value ) {
333+ const text = String ( value ?? '' ) ;
334+ const ms = Date . parse ( text ) ;
335+ return Number . isFinite ( ms ) ? new Date ( ms ) . toISOString ( ) : text ;
336+ }
337+
317338function compareNumber ( a , b ) {
318339 return ( Number ( a ) || 0 ) - ( Number ( b ) || 0 ) ;
319340}
0 commit comments