Files
Agents-Orchestrator/public/js/api.js
Frederico Castro da22154f66 Evolução da plataforma: dashboard com gráficos, notificações, relatórios automáticos, ícones Lucide local e melhorias gerais
- Dashboard com 5 gráficos Chart.js (execuções, status, custo, agentes, pipelines)
- Sistema de notificações com polling, badge e Browser Notification API
- Relatórios MD automáticos para execuções de agentes e pipelines (data/reports/)
- Lucide local (v0.475.0) com nomes de ícones atualizados e refreshIcons centralizado
- Correção de ícones icon-only (padding CSS sobrescrito por btn-sm)
- Cards de agentes e pipelines com botões alinhados na base (flex column)
- Terminal com busca, download, cópia e auto-scroll toggle
- Histórico com export CSV, retry, paginação e truncamento de texto
- Webhooks com edição e teste inline
- Duplicação de agentes e export/import JSON
- Rate limiting, CORS, correlação de requests e health check no backend
- Escrita atômica em JSON (temp + rename) e store de notificações
- Tema claro/escuro com toggle e persistência em localStorage
- Atalhos de teclado 1-9 para navegação entre seções
2026-02-26 20:41:17 -03:00

143 lines
5.6 KiB
JavaScript

const API = {
baseUrl: '/api',
clientId: sessionStorage.getItem('clientId') || (() => {
const id = crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36).slice(2);
sessionStorage.setItem('clientId', id);
return id;
})(),
async request(method, path, body = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
'X-Client-Id': API.clientId,
},
};
if (body !== null) {
options.body = JSON.stringify(body);
}
const response = await fetch(`${API.baseUrl}${path}`, options);
if (response.status === 204) return null;
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || `Erro HTTP ${response.status}`);
}
return data;
},
agents: {
list() { return API.request('GET', '/agents'); },
get(id) { return API.request('GET', `/agents/${id}`); },
create(data) { return API.request('POST', '/agents', data); },
update(id, data) { return API.request('PUT', `/agents/${id}`, data); },
delete(id) { return API.request('DELETE', `/agents/${id}`); },
execute(id, task, instructions) { return API.request('POST', `/agents/${id}/execute`, { task, instructions }); },
cancel(id, executionId) { return API.request('POST', `/agents/${id}/cancel/${executionId}`); },
continue(id, sessionId, message) { return API.request('POST', `/agents/${id}/continue`, { sessionId, message }); },
export(id) { return API.request('GET', `/agents/${id}/export`); },
import(data) { return API.request('POST', '/agents/import', data); },
duplicate(id) { return API.request('POST', `/agents/${id}/duplicate`); },
},
tasks: {
list() { return API.request('GET', '/tasks'); },
create(data) { return API.request('POST', '/tasks', data); },
update(id, data) { return API.request('PUT', `/tasks/${id}`, data); },
delete(id) { return API.request('DELETE', `/tasks/${id}`); },
},
schedules: {
list() { return API.request('GET', '/schedules'); },
create(data) { return API.request('POST', '/schedules', data); },
update(id, data) { return API.request('PUT', `/schedules/${id}`, data); },
delete(taskId) { return API.request('DELETE', `/schedules/${taskId}`); },
history() { return API.request('GET', '/schedules/history'); },
},
pipelines: {
list() { return API.request('GET', '/pipelines'); },
get(id) { return API.request('GET', `/pipelines/${id}`); },
create(data) { return API.request('POST', '/pipelines', data); },
update(id, data) { return API.request('PUT', `/pipelines/${id}`, data); },
delete(id) { return API.request('DELETE', `/pipelines/${id}`); },
execute(id, input, workingDirectory) {
const body = { input };
if (workingDirectory) body.workingDirectory = workingDirectory;
return API.request('POST', `/pipelines/${id}/execute`, body);
},
cancel(id) { return API.request('POST', `/pipelines/${id}/cancel`); },
approve(id) { return API.request('POST', `/pipelines/${id}/approve`); },
reject(id) { return API.request('POST', `/pipelines/${id}/reject`); },
},
webhooks: {
list() { return API.request('GET', '/webhooks'); },
create(data) { return API.request('POST', '/webhooks', data); },
update(id, data) { return API.request('PUT', `/webhooks/${id}`, data); },
delete(id) { return API.request('DELETE', `/webhooks/${id}`); },
test(id) { return API.request('POST', `/webhooks/${id}/test`); },
},
stats: {
costs(days) { return API.request('GET', `/stats/costs${days ? '?days=' + days : ''}`); },
charts(days) { return API.request('GET', `/stats/charts${days ? '?days=' + days : ''}`); },
},
notifications: {
list() { return API.request('GET', '/notifications'); },
markRead(id) { return API.request('POST', `/notifications/${id}/read`); },
markAllRead() { return API.request('POST', '/notifications/read-all'); },
clear() { return API.request('DELETE', '/notifications'); },
},
system: {
status() { return API.request('GET', '/system/status'); },
info() { return API.request('GET', '/system/info'); },
activeExecutions() { return API.request('GET', '/executions/active'); },
},
settings: {
get() { return API.request('GET', '/settings'); },
save(data) { return API.request('PUT', '/settings', data); },
},
reports: {
list() { return API.request('GET', '/reports'); },
get(filename) { return API.request('GET', `/reports/${encodeURIComponent(filename)}`); },
delete(filename) { return API.request('DELETE', `/reports/${encodeURIComponent(filename)}`); },
},
executions: {
recent(limit = 20) { return API.request('GET', `/executions/recent?limit=${limit}`); },
history(params = {}) {
const qs = new URLSearchParams(params).toString();
return API.request('GET', `/executions/history${qs ? '?' + qs : ''}`);
},
get(id) { return API.request('GET', `/executions/history/${id}`); },
delete(id) { return API.request('DELETE', `/executions/history/${id}`); },
clearAll() { return API.request('DELETE', '/executions/history'); },
retry(id) { return API.request('POST', `/executions/${id}/retry`); },
async exportCsv() {
const response = await fetch('/api/executions/export', {
headers: { 'X-Client-Id': API.clientId },
});
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `execucoes_${new Date().toISOString().split('T')[0]}.csv`;
a.click();
URL.revokeObjectURL(url);
},
},
};
window.API = API;