Fix DOM observer regex/container bugs and add continuous AI Chat Body scraper via HTTP Bridge

This commit is contained in:
Variet Worker
2026-04-10 23:52:56 +09:00
parent b3825e1c8a
commit 5e697cd919
3 changed files with 211 additions and 103 deletions

View File

@@ -23,6 +23,7 @@ export interface HttpBridgeContext {
sessionStalled: boolean;
lastPendingStepIndex: number;
logToFile: (msg: string) => void;
writeChatSnapshot?: (text: string) => void;
}
// ─── Module-level state ───
@@ -106,6 +107,12 @@ export function startHttpBridge(ctx: HttpBridgeContext, sdk: any): Promise<numbe
return;
}
// POST /chat — renderer posts chat snapshots directly
if (req.method === 'POST' && url.pathname === '/chat') {
_handleChatSnapshot(req, res, ctx);
return;
}
// POST /deep-inspect-result — renderer posts inspection results here
if (req.method === 'POST' && url.pathname === '/deep-inspect-result') {
_handleDeepInspectResult(req, res, ctx);
@@ -381,10 +388,8 @@ function _handleDeepInspectResult(req: any, res: any, ctx: HttpBridgeContext) {
const data = JSON.parse(body);
deepInspectResult = data;
ctx.logToFile(`[HTTP] deep-inspect result received (${body.length} bytes)`);
// Write to file for reference
const inspectFile = path.join(ctx.bridgePath, 'deep-inspect-result.json');
fs.writeFileSync(inspectFile, JSON.stringify(data, null, 2));
// Notify waiters
const waiters = [...deepInspectWaiters];
deepInspectWaiters = [];
waiters.forEach(w => w(data));
@@ -396,3 +401,22 @@ function _handleDeepInspectResult(req: any, res: any, ctx: HttpBridgeContext) {
}
});
}
function _handleChatSnapshot(req: any, res: any, ctx: HttpBridgeContext) {
let body = '';
req.on('data', (c: string) => body += c);
req.on('end', () => {
try {
const data = JSON.parse(body);
if (data.text && typeof ctx.writeChatSnapshot === 'function') {
ctx.writeChatSnapshot(`💬 **[DOM 추출] AI 응답**\n\n${data.text}`);
ctx.logToFile(`[HTTP] chat snapshot written (${data.text.length} chars)`);
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true }));
} catch (e: any) {
ctx.logToFile(`[HTTP] chat parse error: ${e.message}`);
res.writeHead(400); res.end(JSON.stringify({ error: e.message }));
}
});
}