@@ -19,16 +19,14 @@ import {
1919 resolveWindowsSpawnProgram ,
2020} from "openclaw/plugin-sdk/windows-spawn" ;
2121import { formatCodexDisplayText } from "./command-formatters.js" ;
22+ import { JSONL_FIRST_LINE_CHUNK_BYTES , visitJsonlLines } from "./jsonl-lines.js" ;
2223
2324const CODEX_CLI_SESSIONS_LIST_COMMAND = "codex.cli.sessions.list" ;
2425export const CODEX_CLI_SESSION_RESUME_COMMAND = "codex.cli.session.resume" ;
2526
2627const DEFAULT_SESSION_LIMIT = 10 ;
2728const MAX_SESSION_LIMIT = 50 ;
2829const DEFAULT_RESUME_TIMEOUT_MS = 20 * 60_000 ;
29- const JSONL_STREAM_THRESHOLD_BYTES = 4 * 1024 * 1024 ;
30- const JSONL_READ_CHUNK_BYTES = 1024 * 1024 ;
31- const JSONL_FIRST_LINE_CHUNK_BYTES = 64 * 1024 ;
3230const SESSION_ID_PATTERN = / ^ [ A - Z a - z 0 - 9 . _ : - ] { 1 , 128 } $ / ;
3331const activeResumeSessions = new Set < string > ( ) ;
3432
@@ -350,7 +348,7 @@ async function readHistorySessions(
350348) : Promise < Map < string , CodexCliSessionSummary > > {
351349 const summaries = new Map < string , CodexCliSessionSummary > ( ) ;
352350 const historyPath = path . join ( codexHome , "history.jsonl" ) ;
353- const result = await visitLines ( historyPath , ( line ) => {
351+ const result = await visitJsonlLines ( historyPath , ( line ) => {
354352 const trimmed = line . trim ( ) ;
355353 if ( ! trimmed ) {
356354 return ;
@@ -450,7 +448,7 @@ async function readSessionFileSummary(file: string): Promise<CodexCliSessionSumm
450448 let updatedAt : string | undefined ;
451449 let lastMessage : string | undefined ;
452450 let messageCount = 0 ;
453- const result = await visitLines ( file , ( line ) => {
451+ const result = await visitJsonlLines ( file , ( line ) => {
454452 const trimmed = line . trim ( ) ;
455453 if ( ! trimmed ) {
456454 return ;
@@ -665,98 +663,9 @@ function resolveCodexHome(): string {
665663 return process . env . CODEX_HOME ?. trim ( ) || path . join ( os . homedir ( ) , ".codex" ) ;
666664}
667665
668- async function visitLines (
669- file : string ,
670- visitor : ( line : string ) => boolean | void ,
671- chunkBytes = JSONL_READ_CHUNK_BYTES ,
672- ) : Promise < { ok : boolean ; lineCount : number } > {
673- let size : number ;
674- try {
675- size = ( await fs . stat ( file ) ) . size ;
676- } catch {
677- return { ok : false , lineCount : 0 } ;
678- }
679- if ( size <= JSONL_STREAM_THRESHOLD_BYTES ) {
680- let content : string ;
681- try {
682- content = await fs . readFile ( file , "utf8" ) ;
683- } catch {
684- return { ok : false , lineCount : 0 } ;
685- }
686- if ( content . length === 0 ) {
687- return { ok : true , lineCount : 0 } ;
688- }
689- let lineCount = 0 ;
690- for ( const line of content . split ( / \r ? \n / u) ) {
691- lineCount += 1 ;
692- if ( visitor ( line ) === false ) {
693- break ;
694- }
695- }
696- return { ok : true , lineCount } ;
697- }
698-
699- let handle : Awaited < ReturnType < typeof fs . open > > ;
700- try {
701- handle = await fs . open ( file , "r" ) ;
702- } catch {
703- return { ok : false , lineCount : 0 } ;
704- }
705- const buffer = Buffer . allocUnsafe ( chunkBytes ) ;
706- const decoder = new TextDecoder ( ) ;
707- let pendingFragments : string [ ] = [ ] ;
708- let lineCount = 0 ;
709- try {
710- while ( true ) {
711- const { bytesRead } = await handle . read ( buffer , 0 , buffer . length , null ) ;
712- if ( bytesRead === 0 ) {
713- break ;
714- }
715- const content = decoder . decode ( buffer . subarray ( 0 , bytesRead ) , { stream : true } ) ;
716- let lineStart = 0 ;
717- while ( true ) {
718- const newline = content . indexOf ( "\n" , lineStart ) ;
719- if ( newline === - 1 ) {
720- break ;
721- }
722- let rawLine = content . slice ( lineStart , newline ) ;
723- if ( pendingFragments . length > 0 ) {
724- pendingFragments . push ( rawLine ) ;
725- rawLine = pendingFragments . join ( "" ) ;
726- pendingFragments = [ ] ;
727- }
728- const line = rawLine . endsWith ( "\r" ) ? rawLine . slice ( 0 , - 1 ) : rawLine ;
729- lineCount += 1 ;
730- if ( visitor ( line ) === false ) {
731- return { ok : true , lineCount } ;
732- }
733- lineStart = newline + 1 ;
734- }
735- if ( lineStart < content . length ) {
736- pendingFragments . push ( content . slice ( lineStart ) ) ;
737- }
738- }
739- const decoderTail = decoder . decode ( ) ;
740- if ( decoderTail . length > 0 ) {
741- pendingFragments . push ( decoderTail ) ;
742- }
743- if ( pendingFragments . length > 0 ) {
744- const rawLine = pendingFragments . join ( "" ) ;
745- const line = rawLine . endsWith ( "\r" ) ? rawLine . slice ( 0 , - 1 ) : rawLine ;
746- lineCount += 1 ;
747- visitor ( line ) ;
748- }
749- return { ok : true , lineCount } ;
750- } catch {
751- return { ok : false , lineCount : 0 } ;
752- } finally {
753- await handle . close ( ) . catch ( ( ) => undefined ) ;
754- }
755- }
756-
757666async function readFirstLine ( file : string ) : Promise < string | undefined > {
758667 let firstLine : string | undefined ;
759- const result = await visitLines (
668+ const result = await visitJsonlLines (
760669 file ,
761670 ( line ) => {
762671 firstLine = line ;
0 commit comments