refactor: fix security hardcode, dead code, resource leaks, operator bugs

This commit is contained in:
2026-05-06 12:31:58 +09:00
parent 0c9def0241
commit 17e6503ccd
8 changed files with 134 additions and 59 deletions
+20 -3
View File
@@ -135,9 +135,26 @@ export class BridgeServer {
}
const result = await this.aiService.call(`Analyze chat history for metrics (JSON):\n${historyText.slice(-6000)}`);
const jsonMatch = result.match(/\{[\s\S]*?\}/);
res.writeHead(jsonMatch ? 200 : 500, { 'Content-Type': 'application/json' });
res.end(jsonMatch ? jsonMatch[0] : JSON.stringify({ error: "Parse failed", raw: result }));
// Try to extract valid JSON from the AI response
let parsed: any = null;
try {
parsed = JSON.parse(result);
} catch {
// AI may wrap JSON in markdown code fences — try to extract
const fenceMatch = result.match(/```(?:json)?\s*([\s\S]*?)```/);
if (fenceMatch) {
try { parsed = JSON.parse(fenceMatch[1].trim()); } catch { /* fall through */ }
}
}
if (parsed) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(parsed));
} else {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: "Failed to parse AI response as JSON", raw: summarizeText(result, 500) }));
}
}
private async processBrainInject(data: any, res: http.ServerResponse) {