feat: add Clear History button to delete all consumed instructions

- Backend: instruction_service.clear_consumed() bulk-deletes consumed rows
- Backend: DELETE /api/instructions/consumed route (preserves pending)
- Frontend: Clear button in consumed panel header (hidden when empty)
- Frontend: SSE handler for history.cleared event - instant UI update
- Frontend: api.clearConsumed() fetch wrapper
This commit is contained in:
2026-03-27 04:16:24 +08:00
parent 86eba27a24
commit 256a445e2f
6 changed files with 57 additions and 7 deletions

View File

@@ -162,10 +162,11 @@ function renderConsumedCard(item) {
// ── List renderers ────────────────────────────────────────────────────────
export function initInstructions() {
const pendingList = document.getElementById('pending-list');
const pendingBadge = document.getElementById('pending-badge');
const consumedList = document.getElementById('consumed-list');
const consumedBadge = document.getElementById('consumed-badge');
const pendingList = document.getElementById('pending-list');
const pendingBadge = document.getElementById('pending-badge');
const consumedList = document.getElementById('consumed-list');
const consumedBadge = document.getElementById('consumed-badge');
const clearHistoryBtn = document.getElementById('clear-history-btn');
function render(instructions) {
if (!instructions) return;
@@ -190,14 +191,30 @@ export function initInstructions() {
consumedList.innerHTML = '';
if (consumed.length === 0) {
consumedList.innerHTML = `<div class="empty-state"><div class="empty-state__icon">◈</div>No consumed instructions yet</div>`;
clearHistoryBtn.style.display = 'none';
} else {
consumed.forEach(item => consumedList.appendChild(renderConsumedCard(item)));
clearHistoryBtn.style.display = '';
}
consumedBadge.textContent = consumed.length;
consumedBadge.className = `badge ${consumed.length > 0 ? 'badge--amber' : 'badge--muted'}`;
}
state.subscribe('instructions', render);
// Clear history button
clearHistoryBtn.addEventListener('click', async () => {
if (!confirm('Clear all consumed instruction history? This cannot be undone.')) return;
clearHistoryBtn.disabled = true;
try {
const res = await api.clearConsumed();
toast(`Cleared ${res.cleared} consumed instruction${res.cleared !== 1 ? 's' : ''}`, 'info');
} catch (e) {
toast(e.message, 'error');
} finally {
clearHistoryBtn.disabled = false;
}
});
}
// ── Composer ──────────────────────────────────────────────────────────────