@@ -199,7 +199,7 @@ export class Cline extends EventEmitter<ClineEvents> {
199199
200200 this . rooIgnoreController = new RooIgnoreController ( this . cwd )
201201 this . rooIgnoreController . initialize ( ) . catch ( ( error ) => {
202- console . error ( " Failed to initialize RooIgnoreController:" , error )
202+ this . providerRef . deref ( ) ?. log ( ` Failed to initialize RooIgnoreController: ${ error } ` )
203203 } )
204204
205205 this . taskId = historyItem ? historyItem . id : crypto . randomUUID ( )
@@ -317,7 +317,7 @@ export class Cline extends EventEmitter<ClineEvents> {
317317 await fs . writeFile ( filePath , JSON . stringify ( this . apiConversationHistory ) )
318318 } catch ( error ) {
319319 // in the off chance this fails, we don't want to stop the task
320- console . error ( " Failed to save API conversation history:" , error )
320+ this . providerRef . deref ( ) ?. log ( ` Failed to save API conversation history: ${ error } ` )
321321 }
322322 }
323323
@@ -348,7 +348,7 @@ export class Cline extends EventEmitter<ClineEvents> {
348348 this . emit ( "message" , { action : "created" , message } )
349349 await this . saveClineMessages ( )
350350 } catch ( error ) {
351- console . error ( " Failed to add to Cline messages:" , error )
351+ this . providerRef . deref ( ) ?. log ( ` Failed to add to Cline messages: ${ error } ` )
352352 }
353353 }
354354
@@ -357,7 +357,7 @@ export class Cline extends EventEmitter<ClineEvents> {
357357 this . clineMessages = newMessages
358358 await this . saveClineMessages ( )
359359 } catch ( error ) {
360- console . error ( " Failed to overwrite Cline messages:" , error )
360+ this . providerRef . deref ( ) ?. log ( ` Failed to overwrite Cline messages: ${ error } ` )
361361 }
362362 }
363363
@@ -369,7 +369,7 @@ export class Cline extends EventEmitter<ClineEvents> {
369369 }
370370 this . emit ( "message" , { action : "updated" , message : partialMessage } )
371371 } catch ( error ) {
372- console . error ( " Failed to update Cline message:" , error )
372+ this . providerRef . deref ( ) ?. log ( ` Failed to update Cline message: ${ error } ` )
373373 }
374374 }
375375
@@ -400,9 +400,11 @@ export class Cline extends EventEmitter<ClineEvents> {
400400 try {
401401 taskDirSize = await getFolderSize . loose ( taskDir )
402402 } catch ( err ) {
403- console . error (
404- `[saveClineMessages] failed to get task directory size (${ taskDir } ): ${ err instanceof Error ? err . message : String ( err ) } ` ,
405- )
403+ this . providerRef
404+ . deref ( )
405+ ?. log (
406+ `[saveClineMessages] failed to get task directory size (${ taskDir } ): ${ err instanceof Error ? err . message : String ( err ) } ` ,
407+ )
406408 }
407409
408410 const provider = this . providerRef . deref ( )
@@ -421,7 +423,7 @@ export class Cline extends EventEmitter<ClineEvents> {
421423 } )
422424 }
423425 } catch ( error ) {
424- console . error ( " Failed to save cline messages:" , error )
426+ this . providerRef . deref ( ) ?. log ( ` Failed to save cline messages: ${ error } ` )
425427 }
426428 }
427429
@@ -1140,7 +1142,7 @@ export class Cline extends EventEmitter<ClineEvents> {
11401142 }
11411143 // Wait for MCP servers to be connected before generating system prompt
11421144 await pWaitFor ( ( ) => mcpHub ! . isConnecting !== true , { timeout : 10_000 } ) . catch ( ( ) => {
1143- console . error ( "MCP servers failed to connect in time" )
1145+ this . providerRef . deref ( ) ?. log ( "MCP servers failed to connect in time" )
11441146 } )
11451147 }
11461148
@@ -1184,13 +1186,24 @@ export class Cline extends EventEmitter<ClineEvents> {
11841186 if ( previousApiReqIndex >= 0 ) {
11851187 const previousRequest = this . clineMessages [ previousApiReqIndex ] ?. text
11861188 if ( ! previousRequest ) return
1189+ console . log ( "Previous request:" , previousRequest )
1190+ console . log ( "previous type is of " , typeof previousRequest )
1191+
1192+ let apiReqInfo : ClineApiReqInfo = {
1193+ tokensIn : 0 ,
1194+ tokensOut : 0 ,
1195+ cacheWrites : 0 ,
1196+ cacheReads : 0 ,
1197+ }
11871198
1188- const {
1189- tokensIn = 0 ,
1190- tokensOut = 0 ,
1191- cacheWrites = 0 ,
1192- cacheReads = 0 ,
1193- } : ClineApiReqInfo = JSON . parse ( previousRequest )
1199+ try {
1200+ apiReqInfo = JSON . parse ( previousRequest )
1201+ } catch ( error ) {
1202+ // If parsing fails, we'll use the default values
1203+ console . log ( "Failed to parse previous request as JSON, using default values" )
1204+ }
1205+
1206+ const { tokensIn = 0 , tokensOut = 0 , cacheWrites = 0 , cacheReads = 0 } = apiReqInfo
11941207
11951208 const totalTokens = tokensIn + tokensOut + cacheWrites + cacheReads
11961209
@@ -1363,7 +1376,7 @@ export class Cline extends EventEmitter<ClineEvents> {
13631376 } catch ( error ) {
13641377 // Outermost error handling for catastrophic failures
13651378 // This includes API initialization errors or other fatal issues
1366- console . error ( " Fatal error in attemptApiRequest:" , error )
1379+ this . providerRef . deref ( ) ?. log ( ` Fatal error in attemptApiRequest: ${ error } ` )
13671380 throw error
13681381 }
13691382
@@ -1880,7 +1893,7 @@ export class Cline extends EventEmitter<ClineEvents> {
18801893 await provider . postStateToWebview ( )
18811894 }
18821895 } catch ( error ) {
1883- console . error ( " Failed to post state to webview:" , error )
1896+ this . providerRef . deref ( ) ?. log ( ` Failed to post state to webview: ${ error } ` )
18841897 }
18851898
18861899 try {
@@ -2508,13 +2521,13 @@ export class Cline extends EventEmitter<ClineEvents> {
25082521
25092522 this . say ( "checkpoint_saved" , to , undefined , undefined , { isFirst, from, to } ) . catch ( ( err ) => {
25102523 log ( "[Cline#initializeCheckpoints] caught unexpected error in say('checkpoint_saved')" )
2511- console . error ( err )
2524+ this . providerRef . deref ( ) ?. log ( err )
25122525 } )
25132526 } catch ( err ) {
25142527 log (
25152528 "[Cline#initializeCheckpoints] caught unexpected error in on('checkpoint'), disabling checkpoints" ,
25162529 )
2517- console . error ( err )
2530+ this . providerRef . deref ( ) ?. log ( err )
25182531 this . enableCheckpoints = false
25192532 }
25202533 } )
@@ -2523,7 +2536,7 @@ export class Cline extends EventEmitter<ClineEvents> {
25232536 log (
25242537 `[Cline#initializeCheckpoints] caught unexpected error in initShadowGit, disabling checkpoints (${ err . message } )` ,
25252538 )
2526- console . error ( err )
2539+ this . providerRef . deref ( ) ?. log ( err )
25272540 this . enableCheckpoints = false
25282541 } )
25292542
@@ -2615,7 +2628,7 @@ export class Cline extends EventEmitter<ClineEvents> {
26152628 provider . log ( "[checkpointDiff] disabling checkpoints for this task" )
26162629 }
26172630 } catch ( error ) {
2618- console . error ( " Failed to log message:" , error )
2631+ this . providerRef . deref ( ) ?. log ( ` Failed to log message: ${ error } ` )
26192632 }
26202633 this . enableCheckpoints = false
26212634 }
@@ -2637,7 +2650,7 @@ export class Cline extends EventEmitter<ClineEvents> {
26372650 )
26382651 }
26392652 } catch ( error ) {
2640- console . error ( " Failed to log message:" , error )
2653+ this . providerRef . deref ( ) ?. log ( ` Failed to log message: ${ error } ` )
26412654 }
26422655 this . enableCheckpoints = false
26432656 return
@@ -2647,7 +2660,9 @@ export class Cline extends EventEmitter<ClineEvents> {
26472660
26482661 // Start the checkpoint process in the background.
26492662 service . saveCheckpoint ( `Task: ${ this . taskId } , Time: ${ Date . now ( ) } ` ) . catch ( ( err ) => {
2650- console . error ( "[Cline#checkpointSave] caught unexpected error, disabling checkpoints" , err )
2663+ this . providerRef
2664+ . deref ( )
2665+ ?. log ( `[Cline#checkpointSave] caught unexpected error, disabling checkpoints: ${ err } ` )
26512666 this . enableCheckpoints = false
26522667 } )
26532668 }
@@ -2725,7 +2740,7 @@ export class Cline extends EventEmitter<ClineEvents> {
27252740 provider . cancelTask ( )
27262741 }
27272742 } catch ( error ) {
2728- console . error ( " Failed to cancel task:" , error )
2743+ this . providerRef . deref ( ) ?. log ( ` Failed to cancel task: ${ error } ` )
27292744 }
27302745 } catch ( err ) {
27312746 try {
@@ -2734,7 +2749,7 @@ export class Cline extends EventEmitter<ClineEvents> {
27342749 provider . log ( "[checkpointRestore] disabling checkpoints for this task" )
27352750 }
27362751 } catch ( error ) {
2737- console . error ( " Failed to log message:" , error )
2752+ this . providerRef . deref ( ) ?. log ( ` Failed to log message: ${ error } ` )
27382753 }
27392754 this . enableCheckpoints = false
27402755 }
0 commit comments