(function () { const esc = (s) => String(s ?? '').replace(/&/g,'&').replace(//g,'>'); async function api(path, opts = {}) { const h = { ...(opts.headers || {}), 'Content-Type': 'application/json' }; const t = window.DeskAuth?.getToken?.(); if (t) h.Authorization = `Bearer ${t}`; const r = await fetch(`/api/v1/agents${path}`, { ...opts, headers: h }); if (!r.ok) { const err = await r.text(); throw new Error(`${r.status} ${err.slice(0, 200)}`); } return r.json(); } async function sendChat(question) { return api('/chat', { method: 'POST', body: JSON.stringify({ question, include_findings: true }) }); } async function renderAgenticOps() { const el = document.getElementById('agentic-ops-content'); if (!el) return; el.innerHTML = '

Carregando Agentic Ops…

'; try { const [health, scenarios, findings, log] = await Promise.all([ api('/health'), api('/scenarios'), api('/findings?limit=30'), api('/action-log?limit=40'), ]); const tier = health.tier === 't1' ? 'T1 LLM' : 'T0'; const ollama = health.ollama ? `Ollama OK · ${esc(health.model || '')}` : 'Ollama offline — modo T0'; const sRows = (scenarios.scenarios || []).map(s => `${esc(s.id)}${esc(s.title)}${esc(s.last_run_status||'—')}${esc(s.last_run_at||'—')}` ).join(''); const fRows = (findings.findings || []).map(f => `

${esc(f.title)} ${esc(f.severity)}

` + `

${esc(f.created_at)}

` + (f.suggested_human_action ? `

Acção: ${esc(f.suggested_human_action)}

` : '') + `
` ).join('') || '

Sem findings abertos.

'; const lRows = (log.events || []).map(e => `${esc(e.ts)}${esc(e.event_type)}${esc(e.message)}` ).join(''); el.innerHTML = `

Agentic Ops

Spec 029 · ${tier} ${ollama}

Cenários

${sRows}
IDTítuloÚltimoQuando

Findings

${fRows}

Copiloto Ops (T1)

Pergunte sobre infra, VM123, findings ou procedimentos — resposta contextual pt-BR.

Audit log

${lRows}
QuandoEventoMensagem
`; el.querySelector('#btn-agentic-refresh')?.addEventListener('click', renderAgenticOps); el.querySelectorAll('[data-ack]').forEach(btn => btn.addEventListener('click', async () => { await api(`/findings/${btn.dataset.ack}/ack`, { method: 'POST' }); await renderAgenticOps(); })); el.querySelector('#btn-agentic-chat')?.addEventListener('click', async () => { const input = el.querySelector('#agentic-chat-input'); const out = el.querySelector('#agentic-chat-answer'); const q = (input?.value || '').trim(); if (!q) return; out.hidden = false; out.innerHTML = '

A pensar…

'; try { const res = await sendChat(q); out.innerHTML = `

Resposta (${esc(res.model)})

${esc(res.answer)}

`; } catch (err) { out.innerHTML = `

${esc(err.message)}

`; } }); } catch (err) { el.innerHTML = `

Erro: ${esc(err.message)}

`; } } window.renderAgenticOps = renderAgenticOps; })();