@@ -17,23 +17,15 @@ export class ChatProcessor {
1717 private onFileWritePreparation : ( pendingWrite : PendingFileWrite ) => Promise < void >
1818 ) { }
1919
20- /**
21- * Process streaming ChatResult updates
22- */
2320 public async processChatResult (
2421 chatResult : ChatResult | ChatMessage ,
2522 tabId : string ,
2623 isPartialResult ?: boolean
2724 ) : Promise < void > {
28- getLogger ( ) . info ( `[ChatProcessor] 📨 Processing ChatResult for tab ${ tabId } , isPartial: ${ isPartialResult } ` )
29-
3025 try {
31- // Handle both ChatResult and ChatMessage types
3226 if ( 'type' in chatResult && chatResult . type === 'tool' ) {
33- // This is a ChatMessage
3427 await this . processChatMessage ( chatResult as ChatMessage , tabId )
3528 } else if ( 'additionalMessages' in chatResult && chatResult . additionalMessages ) {
36- // This is a ChatResult with additional messages
3729 for ( const message of chatResult . additionalMessages ) {
3830 await this . processChatMessage ( message , tabId )
3931 }
@@ -43,43 +35,31 @@ export class ChatProcessor {
4335 }
4436 }
4537
46- /**
47- * Process individual chat messages
48- */
4938 private async processChatMessage ( message : ChatMessage , tabId : string ) : Promise < void > {
5039 if ( ! message . messageId ) {
5140 return
5241 }
5342
54- // Deduplicate messages
5543 const messageKey = `${ message . messageId } _${ message . type } `
5644 if ( this . processedMessages . has ( messageKey ) ) {
57- getLogger ( ) . info ( `[ChatProcessor] ⏭️ Already processed message: ${ messageKey } ` )
5845 return
5946 }
6047 this . processedMessages . add ( messageKey )
6148
62- // Check for fsWrite tool preparation (when tool is about to execute)
6349 if ( message . type === 'tool' && message . messageId . startsWith ( 'progress_' ) ) {
6450 await this . processFsWritePreparation ( message , tabId )
6551 }
6652 }
6753
68- /**
69- * Process fsWrite preparation - capture content BEFORE file is written
70- */
7154 private async processFsWritePreparation ( message : ChatMessage , tabId : string ) : Promise < void > {
72- // Cast to any to access properties that might not be in the type definition
7355 const messageAny = message as any
74-
7556 const fileList = messageAny . header ?. fileList
7657 if ( ! fileList ?. filePaths || fileList . filePaths . length === 0 ) {
7758 return
7859 }
7960
8061 const fileName = fileList . filePaths [ 0 ]
8162 const fileDetails = fileList . details ?. [ fileName ]
82-
8363 if ( ! fileDetails ?. description ) {
8464 return
8565 }
@@ -89,16 +69,10 @@ export class ChatProcessor {
8969 return
9070 }
9171
92- // Extract toolUseId from progress message
9372 const toolUseId = message . messageId ! . replace ( 'progress_' , '' )
94-
95- getLogger ( ) . info ( `[ChatProcessor] 🎬 Preparing for fsWrite: ${ filePath } (toolUse: ${ toolUseId } )` )
96-
97- // Capture current content IMMEDIATELY before the write happens
9873 const { content : originalContent , exists : fileExists } =
9974 await this . fileSystemManager . captureFileContent ( filePath )
10075
101- // Store pending write info
10276 const pendingWrite : PendingFileWrite = {
10377 filePath,
10478 originalContent,
@@ -107,44 +81,28 @@ export class ChatProcessor {
10781 }
10882
10983 try {
110- // Prepare file for writing
11184 await this . fileSystemManager . prepareFileForWrite ( filePath , fileExists )
112-
113- // Notify handler about the pending write
11485 await this . onFileWritePreparation ( pendingWrite )
11586 } catch ( error ) {
11687 getLogger ( ) . error ( `[ChatProcessor] ❌ Failed to prepare file write: ${ error } ` )
11788 throw error
11889 }
11990 }
12091
121- /**
122- * Process ChatUpdateParams
123- */
12492 public async processChatUpdate ( params : ChatUpdateParams ) : Promise < void > {
125- getLogger ( ) . info ( `[ChatProcessor] 🔄 Processing chat update for tab ${ params . tabId } ` )
126-
12793 if ( params . data ?. messages ) {
12894 for ( const message of params . data . messages ) {
12995 await this . processChatMessage ( message , params . tabId )
13096 }
13197 }
13298 }
13399
134- /**
135- * Clear processed messages cache
136- */
137100 public clearProcessedMessages ( ) : void {
138101 if ( this . processedMessages . size > 1000 ) {
139- const oldSize = this . processedMessages . size
140102 this . processedMessages . clear ( )
141- getLogger ( ) . info ( `[ChatProcessor] 🧹 Cleared ${ oldSize } processed messages` )
142103 }
143104 }
144105
145- /**
146- * Clear all caches
147- */
148106 public clearAll ( ) : void {
149107 this . processedMessages . clear ( )
150108 }
0 commit comments