Files
rpa_vision_v3/web_dashboard/templates/knowledge_base.html

588 lines
26 KiB
HTML

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RPA Vision V3 - Base de connaissances</title>
<style>
/* === Reset & base — identique au dashboard === */
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #0f172a; color: #e2e8f0; min-height: 100vh; }
/* === Header === */
.header { background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%); color: white; padding: 20px 30px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 15px; }
.header h1 { font-size: 24px; display: flex; align-items: center; gap: 10px; }
.header-subtitle { color: rgba(255,255,255,0.75); font-size: 13px; margin-top: 4px; }
.header-nav { display: flex; align-items: center; gap: 8px; }
.header-nav a {
color: rgba(255,255,255,0.8); text-decoration: none; font-size: 13px;
padding: 6px 14px; border-radius: 6px; transition: all 0.2s;
background: rgba(255,255,255,0.1);
}
.header-nav a:hover { background: rgba(255,255,255,0.2); }
.header-nav a.active { background: rgba(255,255,255,0.25); color: #fff; font-weight: 600; }
/* === Layout === */
.container { max-width: 1600px; margin: 0 auto; padding: 20px; }
/* === Cards === */
.card { background: #1e293b; border-radius: 12px; padding: 20px; border: 1px solid #334155; margin-bottom: 20px; }
.card h2 { font-size: 16px; margin-bottom: 15px; color: #94a3b8; display: flex; align-items: center; gap: 8px; }
/* === Grille indicateurs === */
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-bottom: 20px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin-bottom: 20px; }
@media (max-width: 900px) { .grid-3, .grid-4 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 500px) { .grid-3, .grid-4 { grid-template-columns: 1fr; } }
.stat-card { text-align: center; }
.stat-value { font-size: 36px; font-weight: bold; color: #3b82f6; }
.stat-value.warning { color: #f59e0b; }
.stat-value.success { color: #22c55e; }
.stat-label { font-size: 12px; color: #64748b; margin-top: 5px; text-transform: uppercase; }
/* === Section title === */
.section-title {
font-size: 18px; font-weight: 600; color: #e2e8f0;
margin-bottom: 15px; display: flex; align-items: center; gap: 10px;
padding-bottom: 10px; border-bottom: 1px solid #334155;
}
.section-title .icon { font-size: 22px; }
/* === Alertes === */
.alert {
padding: 12px 16px; border-radius: 8px; font-size: 13px;
margin-bottom: 15px; display: flex; align-items: center; gap: 10px;
}
.alert-warning {
background: #78350f; color: #fcd34d; border: 1px solid #92400e;
}
.alert-info {
background: #1e3a5f; color: #93c5fd; border: 1px solid #1e40af;
}
.alert-success {
background: #064e3b; color: #6ee7b7; border: 1px solid #065f46;
}
/* === Tableau === */
.table-wrapper { overflow-x: auto; margin-top: 15px; }
.kb-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.kb-table thead th {
background: #334155; color: #94a3b8; padding: 12px 10px;
text-align: left; font-weight: 600; font-size: 11px;
text-transform: uppercase; letter-spacing: 0.5px;
white-space: nowrap;
}
.kb-table tbody tr { border-bottom: 1px solid #1e293b; transition: background 0.15s; }
.kb-table tbody tr:hover { background: #334155; }
.kb-table td { padding: 10px; vertical-align: middle; }
/* === Badges catégories === */
.category-list { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 10px; }
.category-item {
display: inline-flex; align-items: center; gap: 8px;
padding: 8px 14px; background: #0f172a; border-radius: 8px;
border: 1px solid #334155; font-size: 13px;
}
.category-count {
background: #3b82f6; color: white; border-radius: 50%;
width: 24px; height: 24px; display: flex; align-items: center;
justify-content: center; font-size: 11px; font-weight: 700;
}
.state-badge {
display: inline-flex; align-items: center; justify-content: center;
min-width: 78px; padding: 4px 8px; border-radius: 6px;
border: 1px solid #334155; background: #0f172a;
color: #cbd5e1; font-size: 11px; font-weight: 700;
text-transform: uppercase;
}
.state-badge.observed { color: #fbbf24; border-color: #92400e; }
.state-badge.candidate { color: #93c5fd; border-color: #1d4ed8; }
.state-badge.stable { color: #6ee7b7; border-color: #047857; }
.verdict-strip { display: flex; flex-wrap: wrap; gap: 6px; }
.verdict-pill {
display: inline-flex; gap: 5px; align-items: center;
padding: 4px 7px; border-radius: 6px; background: #0f172a;
border: 1px solid #334155; font-size: 12px;
}
.verdict-pill.valid { color: #6ee7b7; }
.verdict-pill.invalid { color: #fca5a5; }
.verdict-pill.inconclusive { color: #fcd34d; }
.action-row { display: flex; flex-wrap: wrap; gap: 8px; }
.action-btn {
border: 1px solid #2563eb; background: #1d4ed8; color: #fff;
border-radius: 6px; padding: 7px 10px; font-size: 12px;
font-weight: 700; cursor: pointer; min-height: 32px;
}
.action-btn:hover:not(:disabled) { background: #2563eb; }
.action-btn:disabled {
cursor: not-allowed; opacity: 0.45; border-color: #475569;
background: #1e293b; color: #94a3b8;
}
.modal-backdrop {
display: none; position: fixed; inset: 0; z-index: 50;
background: rgba(15,23,42,0.82); padding: 24px;
align-items: center; justify-content: center;
}
.modal-backdrop.visible { display: flex; }
.modal {
width: min(980px, 96vw); max-height: 90vh; overflow: auto;
background: #1e293b; border: 1px solid #475569;
border-radius: 8px; box-shadow: 0 24px 70px rgba(0,0,0,0.35);
}
.modal-header {
display: flex; justify-content: space-between; align-items: center;
padding: 16px 18px; border-bottom: 1px solid #334155;
}
.modal-title { font-size: 17px; font-weight: 700; color: #f8fafc; }
.modal-close {
border: 1px solid #475569; background: #0f172a; color: #cbd5e1;
border-radius: 6px; padding: 5px 8px; cursor: pointer;
}
.modal-body { padding: 18px; }
.diff-box {
white-space: pre-wrap; overflow-x: auto; background: #020617;
border: 1px solid #334155; border-radius: 6px; padding: 12px;
color: #cbd5e1; font-size: 12px; line-height: 1.4;
max-height: 320px;
}
.evidence-list { margin: 10px 0 16px; color: #cbd5e1; font-size: 13px; }
.modal-actions {
display: flex; justify-content: flex-end; gap: 10px;
padding: 14px 18px; border-top: 1px solid #334155;
}
.secondary-btn {
border: 1px solid #475569; background: #0f172a; color: #cbd5e1;
border-radius: 6px; padding: 8px 12px; cursor: pointer;
}
.danger-note { color: #fcd34d; font-size: 12px; margin-top: 8px; }
/* === Loading === */
.loading {
text-align: center; padding: 40px; color: #64748b; font-size: 14px;
}
.loading::after {
content: ''; display: inline-block; width: 20px; height: 20px;
border: 2px solid #334155; border-top-color: #3b82f6;
border-radius: 50%; animation: spin 0.8s linear infinite;
margin-left: 10px; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }
</style>
</head>
<body>
<!-- Header -->
<div class="header">
<div>
<h1>&#x1F4DA; Base de connaissances</h1>
<div class="header-subtitle">État de la mémoire et des apprentissages de Léa</div>
</div>
<nav class="header-nav">
<a href="/">&#x1F39B;&#xFE0F; Dashboard</a>
<a href="/audit">&#x2696;&#xFE0F; Audit</a>
<a href="/process-mining">&#x1F5FA;&#xFE0F; Cartographie</a>
<a href="/knowledge-base" class="active">&#x1F4DA; Connaissances</a>
</nav>
</div>
<div class="container">
<!-- Section 1 : Mémoire visuelle (FAISS) -->
<div class="section-title"><span class="icon">&#x1F9E0;</span> Mémoire visuelle</div>
<div class="grid-3" id="faissGrid">
<div class="card stat-card">
<div class="stat-value" id="faissVectors">--</div>
<div class="stat-label">Vecteurs indexés</div>
</div>
<div class="card stat-card">
<div class="stat-value" id="faissEmbeddings">--</div>
<div class="stat-label">Embeddings calculés</div>
</div>
<div class="card stat-card">
<div class="stat-value" id="faissSize">--</div>
<div class="stat-label">Taille de l'index</div>
</div>
</div>
<div id="faissAlert" style="display:none;"></div>
<!-- Section 2 : Sessions observées -->
<div class="section-title"><span class="icon">&#x1F441;&#xFE0F;</span> Sessions observées</div>
<div class="grid-3" id="sessionsGrid">
<div class="card stat-card">
<div class="stat-value" id="sessionCount">--</div>
<div class="stat-label">Sessions totales</div>
</div>
<div class="card stat-card">
<div class="stat-value" id="sessionVolume">--</div>
<div class="stat-label">Volume de données</div>
</div>
<div class="card stat-card">
<div class="stat-value" id="machineCount">--</div>
<div class="stat-label">Machines distinctes</div>
</div>
</div>
<div class="card" id="machinesCard">
<h2><span class="icon">&#x1F5A5;&#xFE0F;</span> Répartition par machine</h2>
<div class="table-wrapper">
<table class="kb-table">
<thead>
<tr>
<th>Machine</th>
<th>Sessions</th>
<th>Dernière activité</th>
</tr>
</thead>
<tbody id="machinesTableBody">
<tr><td colspan="3" class="loading">Chargement...</td></tr>
</tbody>
</table>
</div>
</div>
<!-- Section 3 : Réflexes natifs (patterns UI) -->
<div class="section-title"><span class="icon">&#x26A1;</span> Réflexes natifs</div>
<div class="grid-3" id="patternsGrid">
<div class="card stat-card" style="grid-column: span 1;">
<div class="stat-value success" id="patternTotal">--</div>
<div class="stat-label">Patterns connus</div>
</div>
</div>
<div class="card" id="patternsCard">
<h2><span class="icon">&#x1F3AF;</span> Par catégorie</h2>
<ul class="category-list" id="categoryList">
<li class="loading">Chargement...</li>
</ul>
</div>
<!-- Section 4 : Compétences apprises par supervision -->
<div class="section-title"><span class="icon">&#x1F9EA;</span> Compétences apprises par supervision</div>
<div class="grid-3" id="competencesGrid">
<div class="card stat-card">
<div class="stat-value" id="competenceTotal">--</div>
<div class="stat-label">Compétences YAML</div>
</div>
<div class="card stat-card">
<div class="stat-value success" id="competenceCandidate">--</div>
<div class="stat-label">Candidates</div>
</div>
<div class="card stat-card">
<div class="stat-value warning" id="competenceObserved">--</div>
<div class="stat-label">Observées</div>
</div>
</div>
<div class="card" id="competencesCard">
<h2><span class="icon">&#x1F4CC;</span> Promotions supervisées</h2>
<div class="alert alert-info">
Les boutons ci-dessous font un dry-run, affichent le diff YAML, puis demandent confirmation. Aucun write-back silencieux.
</div>
<div class="table-wrapper">
<table class="kb-table">
<thead>
<tr>
<th>Compétence</th>
<th>État</th>
<th>Verdicts</th>
<th>Contextes</th>
<th>Promotion</th>
</tr>
</thead>
<tbody id="competencesTableBody">
<tr><td colspan="5" class="loading">Chargement...</td></tr>
</tbody>
</table>
</div>
</div>
<!-- Section 5 : Workflows -->
<div class="section-title"><span class="icon">&#x1F504;</span> Workflows</div>
<div class="grid-3" id="workflowsGrid">
<div class="card stat-card">
<div class="stat-value" id="workflowCount">--</div>
<div class="stat-label">Workflows appris</div>
</div>
</div>
</div>
<div class="modal-backdrop" id="promotionModal">
<div class="modal">
<div class="modal-header">
<div class="modal-title" id="promotionTitle">Promotion supervisée</div>
<button class="modal-close" onclick="closePromotionModal()">Fermer</button>
</div>
<div class="modal-body">
<div id="promotionStatus"></div>
<h2 style="font-size:14px;margin:14px 0 8px;color:#94a3b8;">Verdicts utilisés</h2>
<div class="evidence-list" id="promotionEvidence">--</div>
<h2 style="font-size:14px;margin:14px 0 8px;color:#94a3b8;">Diff YAML</h2>
<pre class="diff-box" id="promotionDiff">Chargement...</pre>
<div class="danger-note">Le write-back ne s'exécute qu'après confirmation explicite.</div>
</div>
<div class="modal-actions">
<button class="secondary-btn" onclick="closePromotionModal()">Annuler</button>
<button class="action-btn" id="confirmPromotionButton" onclick="confirmPromotion()" disabled>Confirmer la promotion</button>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', loadKnowledgeBase);
let knowledgeBaseData = null;
let currentPromotion = null;
async function loadKnowledgeBase() {
try {
const resp = await fetch('/api/knowledge-base/stats');
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const data = await resp.json();
renderFaiss(data.faiss);
renderSessions(data.sessions);
renderPatterns(data.patterns);
renderCompetences(data.competences);
renderWorkflows(data.workflows);
knowledgeBaseData = data;
} catch (err) {
console.error('Erreur chargement base de connaissances:', err);
}
}
function renderFaiss(faiss) {
document.getElementById('faissVectors').textContent = faiss.vectors_indexed.toLocaleString('fr-FR');
document.getElementById('faissEmbeddings').textContent = faiss.embeddings_computed.toLocaleString('fr-FR');
document.getElementById('faissSize').textContent = faiss.index_size_mb;
// Alerte si embeddings non consolidés
const diff = faiss.embeddings_computed - faiss.vectors_indexed;
const alertEl = document.getElementById('faissAlert');
if (diff > 10) {
alertEl.style.display = 'block';
alertEl.innerHTML = `<div class="alert alert-warning">&#x26A0;&#xFE0F; ${diff.toLocaleString('fr-FR')} embeddings non consolidés dans l'index FAISS</div>`;
} else if (faiss.vectors_indexed === 0 && !faiss.available) {
alertEl.style.display = 'block';
alertEl.innerHTML = `<div class="alert alert-info">&#x2139;&#xFE0F; FAISS non disponible sur ce système</div>`;
} else {
alertEl.style.display = 'none';
}
}
function renderSessions(sessions) {
document.getElementById('sessionCount').textContent = sessions.total_sessions.toLocaleString('fr-FR');
document.getElementById('sessionVolume').textContent = sessions.total_volume;
document.getElementById('machineCount').textContent = sessions.machines.length.toLocaleString('fr-FR');
const tbody = document.getElementById('machinesTableBody');
if (sessions.machines.length === 0) {
tbody.innerHTML = '<tr><td colspan="3" style="color:#64748b;text-align:center;padding:20px;">Aucune session enregistrée</td></tr>';
return;
}
tbody.innerHTML = sessions.machines.map(m => `
<tr>
<td><strong>${escapeHtml(m.machine_id)}</strong></td>
<td>${m.session_count}</td>
<td style="color:#94a3b8;font-size:12px;">${m.last_activity || '—'}</td>
</tr>
`).join('');
}
function renderPatterns(patterns) {
document.getElementById('patternTotal').textContent = patterns.total.toLocaleString('fr-FR');
const list = document.getElementById('categoryList');
const cats = patterns.by_category;
if (!cats || Object.keys(cats).length === 0) {
list.innerHTML = '<li style="color:#64748b;">Aucun pattern chargé</li>';
return;
}
list.innerHTML = Object.entries(cats)
.sort((a, b) => b[1] - a[1])
.map(([cat, count]) => `
<li class="category-item">
<span class="category-count">${count}</span>
<span>${escapeHtml(cat)}</span>
</li>
`).join('');
}
function renderWorkflows(workflows) {
document.getElementById('workflowCount').textContent = workflows.total.toLocaleString('fr-FR');
}
function renderCompetences(competences) {
const items = competences?.items || [];
const byState = competences?.by_state || {};
document.getElementById('competenceTotal').textContent = (competences?.total || 0).toLocaleString('fr-FR');
document.getElementById('competenceCandidate').textContent = (byState.candidate || 0).toLocaleString('fr-FR');
document.getElementById('competenceObserved').textContent = (byState.observed || 0).toLocaleString('fr-FR');
const tbody = document.getElementById('competencesTableBody');
if (items.length === 0) {
tbody.innerHTML = '<tr><td colspan="5" style="color:#64748b;text-align:center;padding:20px;">Aucune compétence YAML chargée</td></tr>';
return;
}
tbody.innerHTML = items.map(item => {
const targetEntries = Object.entries(item.eligible_targets || {});
const buttons = targetEntries.length === 0
? '<span style="color:#64748b;font-size:12px;">Aucune promotion disponible</span>'
: targetEntries.map(([target, info]) => {
const title = (info.blocking_reasons || []).join(' · ') || `Promouvoir vers ${target}`;
const verdictIds = JSON.stringify(info.recommended_verdict_ids || []).replace(/"/g, '&quot;');
return `<button class="action-btn" ${info.eligible ? '' : 'disabled'} title="${escapeHtml(title)}" onclick="openPromotion('${escapeAttr(item.id)}','${escapeAttr(target)}',${verdictIds})">Promouvoir ${escapeHtml(target)}</button>`;
}).join('');
const counts = item.verdict_counts || {};
const warning = item.regression_suspected
? '<div class="alert alert-warning" style="margin:8px 0 0;padding:7px 9px;">Régression suspectée</div>'
: '';
return `
<tr>
<td>
<strong>${escapeHtml(item.id)}</strong>
<div style="color:#94a3b8;font-size:12px;margin-top:4px;">${escapeHtml(item.intent_fr || item.name || '')}</div>
${warning}
</td>
<td><span class="state-badge ${escapeAttr(item.learning_state)}">${escapeHtml(item.learning_state)}</span></td>
<td>
<div class="verdict-strip">
<span class="verdict-pill valid">${counts.valid || 0} valid</span>
<span class="verdict-pill invalid">${counts.invalid || 0} invalid</span>
<span class="verdict-pill inconclusive">${counts.inconclusive || 0} incertain</span>
</div>
</td>
<td>${item.distinct_contexts || 0}</td>
<td><div class="action-row">${buttons}</div></td>
</tr>
`;
}).join('');
}
async function openPromotion(competenceId, targetState, verdictIds) {
currentPromotion = null;
document.getElementById('promotionModal').classList.add('visible');
document.getElementById('promotionTitle').textContent = `Promotion ${competenceId} -> ${targetState}`;
document.getElementById('promotionStatus').innerHTML = '<div class="alert alert-info">Dry-run en cours...</div>';
document.getElementById('promotionEvidence').textContent = '--';
document.getElementById('promotionDiff').textContent = 'Chargement...';
document.getElementById('confirmPromotionButton').disabled = true;
const promotionId = newPromotionId();
const payload = {
promotion_id: promotionId,
target_state: targetState,
verdict_ids: verdictIds,
confirmed_by: 'human:dom',
dry_run: true,
};
try {
const resp = await fetch(`/api/v1/lea/competences/${encodeURIComponent(competenceId)}/promote`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload),
});
const data = await resp.json();
if (!resp.ok || !data.success) throw new Error(data.error || resp.statusText);
currentPromotion = {
competenceId,
payload: {
...payload,
dry_run: false,
dry_run_token: data.promotion.dry_run_token,
},
};
renderPromotionDryRun(data.promotion);
} catch (err) {
document.getElementById('promotionStatus').innerHTML = `<div class="alert alert-warning">${escapeHtml(err.message || String(err))}</div>`;
document.getElementById('promotionDiff').textContent = '';
}
}
function renderPromotionDryRun(promotion) {
const evidence = promotion.evidence_summary?.verdicts || [];
document.getElementById('promotionEvidence').innerHTML = evidence.length
? evidence.map(v => `
<div>
<strong>${escapeHtml(String(v.verdict_id || '').slice(0, 8))}</strong>
· ${escapeHtml(v.verdict_kind || '')}
· ${escapeHtml(v.machine_id || 'machine inconnue')}
· steps ${v.step_results_count || 0}
</div>
`).join('')
: '<span style="color:#64748b;">Aucun verdict utilisable</span>';
document.getElementById('promotionDiff').textContent = promotion.yaml_diff || '(aucun changement)';
if (promotion.eligible) {
document.getElementById('promotionStatus').innerHTML = "<div class=\"alert alert-success\">Dry-run OK. Le YAML n'a pas été modifié.</div>";
document.getElementById('confirmPromotionButton').disabled = false;
} else {
const reasons = (promotion.blocking_reasons || []).join(' · ');
document.getElementById('promotionStatus').innerHTML = `<div class="alert alert-warning">Promotion bloquée : ${escapeHtml(reasons)}</div>`;
document.getElementById('confirmPromotionButton').disabled = true;
}
}
async function confirmPromotion() {
if (!currentPromotion) return;
const button = document.getElementById('confirmPromotionButton');
button.disabled = true;
button.textContent = 'Promotion...';
try {
const resp = await fetch(`/api/v1/lea/competences/${encodeURIComponent(currentPromotion.competenceId)}/promote`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(currentPromotion.payload),
});
const data = await resp.json();
if (!resp.ok || !data.success) throw new Error(data.error || resp.statusText);
document.getElementById('promotionStatus').innerHTML = '<div class="alert alert-success">Promotion appliquée. Backup et audit trail enregistrés.</div>';
setTimeout(() => {
closePromotionModal();
loadKnowledgeBase();
}, 900);
} catch (err) {
document.getElementById('promotionStatus').innerHTML = `<div class="alert alert-warning">${escapeHtml(err.message || String(err))}</div>`;
button.disabled = false;
} finally {
button.textContent = 'Confirmer la promotion';
}
}
function closePromotionModal() {
document.getElementById('promotionModal').classList.remove('visible');
currentPromotion = null;
}
function newPromotionId() {
if (window.crypto?.randomUUID) return window.crypto.randomUUID();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, char => {
const value = Math.floor(Math.random() * 16);
const resolved = char === 'x' ? value : (value & 0x3) | 0x8;
return resolved.toString(16);
});
}
function escapeHtml(text) {
const div = document.createElement('div');
div.appendChild(document.createTextNode(text));
return div.innerHTML;
}
function escapeAttr(text) {
return String(text).replace(/[^a-zA-Z0-9_.:-]/g, '');
}
</script>
</body>
</html>