Skip to content

Commit 964d788

Browse files
committed
feat: added sendMessage API, refactoring
1 parent eea173c commit 964d788

2 files changed

Lines changed: 83 additions & 46 deletions

File tree

src/analysisWorker.js

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,39 @@ app.get('/health', async (req, res) => {
135135
}
136136
});
137137

138+
app.post('/sendMessage', async (req, res) => {
139+
try {
140+
const { projectId, repo, branch } = req.body;
141+
142+
if (!projectId) {
143+
return res.status(400).json({
144+
error: 'Missing required parameter: projectId',
145+
example: {
146+
projectId: "XXXXXXXXXXXX",
147+
}
148+
});
149+
}
150+
151+
const messageResponse = await this.potpieClient.sendMessage(projectId, questionPrompt);
152+
if (!messageResponse.success) throw new Error(`Failed to create conversation for project ${projectId}. Error: ${JSON.stringify(response.error)}`);
153+
154+
const data = analysisWorker.processResponse(projectId, messageResponse, repo, branch);
155+
156+
return {
157+
data,
158+
status: 'finished',
159+
};
160+
} catch (err) {
161+
console.error('sendMessage endpoint error:', error);
162+
res.status(500).json({
163+
success: false,
164+
error: 'Internal server error during sendMessage process',
165+
message: error.message,
166+
timestamp: new Date().toISOString()
167+
});
168+
}
169+
})
170+
138171
// Repository analysis endpoint - Uses BullMQ Queue
139172
app.post('/analyze', async (req, res) => {
140173
try {

0 commit comments

Comments
 (0)