@@ -109,53 +109,8 @@ class AnalysisWorker {
109109 console . log ( `🔄 [WORKER] Sending analysis request to agent...` ) ;
110110 const response = await this . potpieClient . sendMessage ( project_id , question ) ;
111111
112- console . log ( response . data ) ;
113- if ( ! response . success ) throw new Error ( `Failed to create conversation for project ${ project_id } . Error: ${ JSON . stringify ( response . error ) } ` ) ;
114-
115112 // 🧾 Step 4: Process agent output
116- const agentOutput = this . extractJsonFromMessage ( response ?. data ) || { } ;
117- console . log ( `✅ [WORKER] Agent output received:` , JSON . stringify ( agentOutput , null , 2 ) ) ;
118-
119- // Fallbacks per compatibilità
120- const snippets = agentOutput . snippets || [ ] ;
121- const analysisResponse = agentOutput . analysis_response || { } ;
122- const totalNodesFound = agentOutput . metadata ?. total_nodes_found || snippets . length ;
123-
124- const vectorDbData = {
125- project_id,
126- repo,
127- branch,
128- question,
129- parsing_status : parsingResult . data . status ,
130- snippets,
131- snippets_count : snippets . length ,
132- analysis_response : analysisResponse ,
133- metadata : {
134- ...agentOutput . metadata ,
135- parsed_at : new Date ( ) . toISOString ( ) ,
136- total_nodes_found : totalNodesFound ,
137- processed_nodes : snippets . length ,
138- has_github_token : ! ! github_token ,
139- processing_time_ms : Date . now ( ) - new Date ( job . timestamp ) . getTime ( ) ,
140- job_id : job . id
141- }
142- } ;
143-
144- console . log ( `✅ [WORKER] Analysis completed for ${ project_id } . Extracted ${ snippets . length } snippets.` ) ;
145-
146- // Step 5: Emit final result
147- this . emitJobUpdate ( project_id , 'finished' , 'Job finished' ) ;
148-
149- this . io . to ( room ) . emit ( 'analysis_complete' , {
150- project_id,
151- status : 'finished' ,
152- data : vectorDbData ,
153- message : 'Analysis completed successfully. Data ready for vector DB.' ,
154- timestamp : new Date ( ) . toISOString ( )
155- } ) ;
156-
157- return vectorDbData ;
158-
113+ return this . processResponse ( project_id , response , repo , branch , job ) ;
159114 } catch ( error ) {
160115 console . error ( `❌ [WORKER] Error processing analysis for project ${ project_id } :` , error ) ;
161116
@@ -184,6 +139,55 @@ class AnalysisWorker {
184139 } ) ;
185140 }
186141
142+ processResponse ( project_id , response , repo , branch , job ?) {
143+ const room = `project_${ project_id } ` ;
144+
145+ if ( ! response . success ) throw new Error ( `Failed to create conversation for project ${ project_id } . Error: ${ JSON . stringify ( response . error ) } ` ) ;
146+
147+ const agentOutput = this . extractJsonFromMessage ( response ?. data ) || { } ;
148+ console . log ( `✅ [WORKER] Agent output received:` , JSON . stringify ( agentOutput , null , 2 ) ) ;
149+
150+ // Fallbacks per compatibilità
151+ const snippets = agentOutput . snippets || [ ] ;
152+ const analysisResponse = agentOutput . analysis_response || { } ;
153+ const totalNodesFound = agentOutput . metadata ?. total_nodes_found || snippets . length ;
154+
155+ const vectorDbData = {
156+ project_id,
157+ repo,
158+ branch,
159+ snippets,
160+ snippets_count : snippets . length ,
161+ analysis_response : analysisResponse ,
162+ metadata : {
163+ ...agentOutput . metadata ,
164+ parsed_at : new Date ( ) . toISOString ( ) ,
165+ total_nodes_found : totalNodesFound ,
166+ processed_nodes : snippets . length ,
167+ has_github_token : true ,
168+ processing_time_ms : Date . now ( ) - new Date ( job ?. timestamp ) . getTime ( ) ,
169+ job_id : job ?. id
170+ }
171+ } ;
172+
173+ console . log ( `✅ [WORKER] Analysis completed for ${ project_id } . Extracted ${ snippets . length } snippets.` ) ;
174+
175+ if ( job ) {
176+ // Step 5: Emit final result
177+ this . emitJobUpdate ( project_id , 'finished' , 'Job finished' ) ;
178+
179+ this . io . to ( room ) . emit ( 'analysis_complete' , {
180+ project_id,
181+ status : 'finished' ,
182+ data : vectorDbData ,
183+ message : 'Analysis completed successfully. Data ready for vector DB.' ,
184+ timestamp : new Date ( ) . toISOString ( )
185+ } ) ;
186+ }
187+
188+ return vectorDbData ;
189+ }
190+
187191 extractJsonFromMessage ( data ) {
188192 const message = data . message ;
189193 if ( ! message ) return null ;
0 commit comments