()
| 42 | } |
| 43 | |
| 44 | function createHttpServer() { |
| 45 | return http.createServer((req, res) => { |
| 46 | const url = req.url.split('?')[0]; |
| 47 | |
| 48 | // --- API: Hook approval endpoint --- |
| 49 | if (req.method === 'POST' && url === '/hook/pre-tool-use') { |
| 50 | let body = ''; |
| 51 | req.on('data', chunk => (body += chunk)); |
| 52 | req.on('end', () => { |
| 53 | let data; |
| 54 | try { data = JSON.parse(body); } catch { |
| 55 | res.writeHead(400, { 'Content-Type': 'application/json' }); |
| 56 | res.end(JSON.stringify({ decision: 'ask' })); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | maybeAttachHookSession(data, 'pre-tool-use'); |
| 61 | const effectiveApprovalMode = recomputeEffectiveApprovalMode('pre-tool-use'); |
| 62 | |
| 63 | if (ALWAYS_AUTO_ALLOW.has(data.tool_name)) { |
| 64 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 65 | res.end(JSON.stringify({ decision: 'allow' })); |
| 66 | log(`Permission auto-allowed (always): ${data.tool_name}`); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (effectiveApprovalMode === 'all') { |
| 71 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 72 | res.end(JSON.stringify({ decision: 'allow' })); |
| 73 | log(`Permission auto-allowed (mode=all): ${data.tool_name}`); |
| 74 | return; |
| 75 | } |
| 76 | if (effectiveApprovalMode === 'partial' && PARTIAL_AUTO_ALLOW.has(data.tool_name)) { |
| 77 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 78 | res.end(JSON.stringify({ decision: 'allow' })); |
| 79 | log(`Permission auto-allowed (mode=partial): ${data.tool_name}`); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | const clients = [...state.wss.clients].filter(isAuthenticatedClient); |
| 84 | if (clients.length === 0) { |
| 85 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 86 | res.end(JSON.stringify({ decision: 'ask' })); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | const id = String(++state.approvalSeq); |
| 91 | log(`Permission #${id}: ${data.tool_name} → ${clients.length} WebUI client(s)`); |
| 92 | |
| 93 | broadcast({ |
| 94 | type: 'permission_request', |
| 95 | id, |
| 96 | toolName: data.tool_name, |
| 97 | toolInput: data.tool_input, |
| 98 | permissionMode: data.permission_mode, |
| 99 | }); |
| 100 | |
| 101 | const timer = setTimeout(() => { |
no test coverage detected