feat: Fix vector indexing stability, add Gitea linking, enhance admin dashboard
This commit is contained in:
93
app/templates/crumbforest/crew.html
Normal file
93
app/templates/crumbforest/crew.html
Normal file
@@ -0,0 +1,93 @@
|
||||
{% extends "base_public.html" %}
|
||||
|
||||
{% block title %}The Crumbforest Crew{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<main class="container">
|
||||
<hgroup>
|
||||
<h1>🌲 The Crumbforest Crew</h1>
|
||||
<p>Meet the 15 experts ready to help you learn and build.</p>
|
||||
</hgroup>
|
||||
|
||||
<div class="crew-grid">
|
||||
{% for id, role in roles.items() %}
|
||||
<article class="role-card" style="border-left: 5px solid {{ role.color }};">
|
||||
<header>
|
||||
<div class="role-icon">{{ role.icon }}</div>
|
||||
<h3 style="color: {{ role.color }}">{{ role.name }}</h3>
|
||||
<small>{{ role.title }}</small>
|
||||
</header>
|
||||
<p>{{ role.description }}</p>
|
||||
<footer>
|
||||
{% if id == 'eule' %}
|
||||
<!-- Eule is public -->
|
||||
<a href="/crumbforest/roles/{{ id }}" role="button" class="contrast"
|
||||
style="background-color: {{ role.color }}; border-color: {{ role.color }}; width: 100%;">
|
||||
Start Chat
|
||||
</a>
|
||||
{% else %}
|
||||
<!-- Others require login (or are locked) -->
|
||||
{% if user %}
|
||||
<a href="/crumbforest/roles/{{ id }}" role="button" class="outline"
|
||||
style="color: {{ role.color }}; border-color: {{ role.color }}; width: 100%;">
|
||||
Chat
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="/login?next=/crumbforest/roles/{{ id }}" role="button" class="secondary outline"
|
||||
style="width: 100%;">
|
||||
🔒 Login to Chat
|
||||
</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.crew-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
/* 3 Column Layout for larger screens to enforce 3x5 if roles are multiple of 3 */
|
||||
@media (min-width: 992px) {
|
||||
.crew-grid {
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.role-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
margin-bottom: 0;
|
||||
/* Override pico */
|
||||
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
||||
}
|
||||
|
||||
.role-card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.role-card header {
|
||||
text-align: center;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-icon {
|
||||
font-size: 3rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-card p {
|
||||
flex-grow: 1;
|
||||
/* Push footer down */
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
@@ -5,7 +5,7 @@
|
||||
{% block content %}
|
||||
<main class="container">
|
||||
<hgroup>
|
||||
<h1>{{ role.icon }} {{ role.name }}</h1>
|
||||
<h1 style="color: {{ role.color }};">{{ role.icon }} {{ role.name }}</h1>
|
||||
<p>
|
||||
{{ role.description }}
|
||||
<a href="/crumbforest/roles/{{ role.id }}/export" role="button" class="outline secondary"
|
||||
@@ -15,6 +15,18 @@
|
||||
</p>
|
||||
</hgroup>
|
||||
|
||||
<style>
|
||||
:root {
|
||||
--role-color: {
|
||||
{
|
||||
role.color
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
}
|
||||
</style>
|
||||
|
||||
<!-- Prism.js for Syntax Highlighting -->
|
||||
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet" />
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
|
||||
@@ -148,8 +160,22 @@
|
||||
div.style.textAlign = 'left';
|
||||
div.innerHTML = `<strong>You</strong><br>${formattedContent}`;
|
||||
} else if (role === 'assistant') {
|
||||
div.style.background = 'var(--pico-secondary-background)';
|
||||
div.style.background = 'var(--role-color, var(--pico-secondary-background))';
|
||||
div.style.color = 'white'; // Always white text on colored background? Or depends on brightness?
|
||||
// Let's use a safe fallback or keep it simple. If role color is light, white text might be bad.
|
||||
// But most config colors seem dark enough?
|
||||
// Dumbo is #6c757d (grey), SnakePy #3776ab (blue). White text is fine.
|
||||
// FunkFox #f7df1e (yellow) -> White text is BAD.
|
||||
// We might need a contrast color from config or calc().
|
||||
|
||||
// For now, let's stick to the pico variable for text if we use role color for border?
|
||||
// Or use an opacity version?
|
||||
|
||||
// Let's try attempting to use the role color as a border-left accent instead, to be safe with text contrast?
|
||||
div.style.background = 'var(--pico-card-background-color)';
|
||||
div.style.borderLeft = '5px solid var(--role-color, var(--pico-primary))';
|
||||
div.style.color = 'var(--pico-color)';
|
||||
|
||||
div.style.marginRight = 'auto'; // Align left
|
||||
div.innerHTML = `<strong>{{ role.name }}</strong><br>${formattedContent}`;
|
||||
} else {
|
||||
|
||||
@@ -9,15 +9,13 @@
|
||||
<p>Choose your learning companion!</p>
|
||||
</hgroup>
|
||||
|
||||
<div class="grid">
|
||||
<div class="roles-grid">
|
||||
{% for role_id, role in roles.items() %}
|
||||
<article class="card">
|
||||
<article class="role-card" style="border-left: 5px solid {{ role.color }};">
|
||||
<header>
|
||||
<h3>
|
||||
<span style="font-size: 2em; margin-right: 0.5rem;">{{ role.icon }}</span>
|
||||
{{ role.name }}
|
||||
</h3>
|
||||
<p class="text-muted"><small>{{ role.title }}</small></p>
|
||||
<div class="role-icon">{{ role.icon }}</div>
|
||||
<h3 style="color: {{ role.color }}">{{ role.name }}</h3>
|
||||
<small>{{ role.title }}</small>
|
||||
</header>
|
||||
|
||||
<p>{{ role.description }}</p>
|
||||
@@ -26,13 +24,14 @@
|
||||
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem;">
|
||||
{% for tag in role.tags %}
|
||||
<span class="badge"
|
||||
style="background-color: {{ role.color }}20; color: {{ role.color }}; border: 1px solid {{ role.color }}40;">
|
||||
style="background-color: {{ role.color }}20; color: {{ role.color }}; border: 1px solid {{ role.color }}40; font-size: 0.7em; padding: 2px 6px; border-radius: 4px;">
|
||||
#{{ tag }}
|
||||
</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<a href="/crumbforest/roles/{{ role_id }}" role="button" style="width: 100%;">
|
||||
<a href="/crumbforest/roles/{{ role_id }}" role="button" class="outline"
|
||||
style="width: 100%; color: {{ role.color }}; border-color: {{ role.color }};">
|
||||
Chat with {{ role.name }}
|
||||
</a>
|
||||
</footer>
|
||||
@@ -40,4 +39,57 @@
|
||||
{% endfor %}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.roles-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
/* Tablet: 2 columns */
|
||||
@media (min-width: 768px) {
|
||||
.roles-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
/* Desktop: Strictly 3 columns */
|
||||
@media (min-width: 1024px) {
|
||||
.roles-grid {
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.role-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
margin-bottom: 0;
|
||||
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.role-card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.role-card header {
|
||||
text-align: center;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-icon {
|
||||
font-size: 3rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-card p {
|
||||
flex-grow: 1;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
@@ -1,201 +1,103 @@
|
||||
{% extends "home/base_home.html" %}
|
||||
|
||||
{% block title %}Crew - {{ deployment.home.hero.title }}{% endblock %}
|
||||
{% block title %}The Crumbforest Crew{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<main class="container">
|
||||
<hgroup>
|
||||
<h1>{{ t.crew.title }}</h1>
|
||||
<p>{{ t.crew.subtitle }}</p>
|
||||
<h1>🌲 The Crumbforest Crew</h1>
|
||||
<p>Meet the 15 experts ready to help you learn and build.</p>
|
||||
</hgroup>
|
||||
|
||||
<div class="crew-grid">
|
||||
{% for character in characters %}
|
||||
<div class="character-card" onclick="showCharacter('{{ character.id }}')">
|
||||
<div class="icon">{{ character.icon }}</div>
|
||||
<h3>{{ character.name }}</h3>
|
||||
<p>{{ character.short }}</p>
|
||||
</div>
|
||||
|
||||
<!-- Dialog for this character -->
|
||||
<dialog id="dialog-{{ character.id }}">
|
||||
<article>
|
||||
<header>
|
||||
<button aria-label="Close" rel="prev" onclick="closeCharacter('{{ character.id }}')"></button>
|
||||
<h2>{{ character.icon }} {{ character.name }}</h2>
|
||||
</header>
|
||||
<p>{{ character.description }}</p>
|
||||
|
||||
{% if character.id in ['eule', 'fox', 'bugsy'] %}
|
||||
<!-- Chat Interface for Eule, FunkFox and Bugsy -->
|
||||
<div class="chat-container" style="margin-top: 1rem;">
|
||||
<div id="chat-history-{{ character.id }}" class="chat-history" style="max-height: 300px; overflow-y: auto; padding: 1rem; background: rgba(0,0,0,0.1); border-radius: 0.5rem; margin-bottom: 1rem;">
|
||||
<p style="text-align: center; color: #888;"><small>Stelle mir eine Frage...</small></p>
|
||||
</div>
|
||||
|
||||
<form id="chat-form-{{ character.id }}" onsubmit="sendMessage(event, '{{ character.id }}'); return false;">
|
||||
<input
|
||||
type="text"
|
||||
id="chat-input-{{ character.id }}"
|
||||
placeholder="{% if character.id == 'eule' %}Deine Frage an die Krümeleule...{% elif character.id == 'fox' %}Deine Frage an FunkFox...{% elif character.id == 'bugsy' %}Deine Frage an Bugsy...{% else %}Deine Frage...{% endif %}"
|
||||
style="width: 100%; color: #fff; background: rgba(255,255,255,0.1); padding: 0.75rem; border: 1px solid rgba(255,255,255,0.2); border-radius: 0.25rem; margin-bottom: 0.5rem;"
|
||||
autocomplete="off"
|
||||
required
|
||||
/>
|
||||
<div style="text-align: right;">
|
||||
<button
|
||||
type="submit"
|
||||
id="chat-submit-{{ character.id }}"
|
||||
style="padding: 0.5rem 1.5rem; font-size: 0.875rem;"
|
||||
>
|
||||
Senden
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div id="chat-status-{{ character.id }}" style="margin-top: 0.5rem; text-align: center; min-height: 1.5rem;">
|
||||
<!-- Status messages appear here -->
|
||||
</div>
|
||||
</div>
|
||||
{% for id, role in roles.items() %}
|
||||
<article class="role-card" style="border-left: 5px solid {{ role.color }};">
|
||||
<header>
|
||||
<div class="role-icon">{{ role.icon }}</div>
|
||||
<h3 style="color: {{ role.color }}">{{ role.name }}</h3>
|
||||
<small>{{ role.title }}</small>
|
||||
</header>
|
||||
<p>{{ role.description }}</p>
|
||||
<footer>
|
||||
{% if id == 'eule' %}
|
||||
<!-- Eule is public -->
|
||||
<a href="/crumbforest/roles/{{ id }}" role="button" class="contrast"
|
||||
style="background-color: {{ role.color }}; border-color: {{ role.color }}; width: 100%;">
|
||||
Start Chat
|
||||
</a>
|
||||
{% else %}
|
||||
<!-- Others require login (or are locked) -->
|
||||
{% if user %}
|
||||
<a href="/crumbforest/roles/{{ id }}" role="button" class="outline"
|
||||
style="color: {{ role.color }}; border-color: {{ role.color }}; width: 100%;">
|
||||
Chat
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="/login?next=/crumbforest/roles/{{ id }}" role="button" class="secondary outline" style="width: 100%;">
|
||||
🔒 Login to Chat
|
||||
</a>
|
||||
{% endif %}
|
||||
|
||||
<footer>
|
||||
<small>{{ t.crew.tags_label }}
|
||||
{% for tag in character.tags %}
|
||||
<code>#{{ tag }}</code>{% if not loop.last %}, {% endif %}
|
||||
{% endfor %}
|
||||
</small>
|
||||
</footer>
|
||||
</article>
|
||||
</dialog>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
</main>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script>
|
||||
function showCharacter(id) {
|
||||
const dialog = document.getElementById('dialog-' + id);
|
||||
dialog.showModal();
|
||||
|
||||
// Focus input field for chat-enabled characters
|
||||
if (id === 'eule' || id === 'fox' || id === 'bugsy') {
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('chat-input-' + id);
|
||||
if (input) {
|
||||
input.focus();
|
||||
}
|
||||
}, 100);
|
||||
<style>
|
||||
.crew-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
function closeCharacter(id) {
|
||||
document.getElementById('dialog-' + id).close();
|
||||
}
|
||||
|
||||
async function sendMessage(event, characterId) {
|
||||
event.preventDefault();
|
||||
|
||||
const inputEl = document.getElementById('chat-input-' + characterId);
|
||||
const historyEl = document.getElementById('chat-history-' + characterId);
|
||||
const statusEl = document.getElementById('chat-status-' + characterId);
|
||||
const submitBtn = document.getElementById('chat-submit-' + characterId);
|
||||
|
||||
const question = inputEl.value.trim();
|
||||
if (!question) return;
|
||||
|
||||
// Disable input while processing
|
||||
inputEl.disabled = true;
|
||||
submitBtn.disabled = true;
|
||||
const statusMessages = {
|
||||
'eule': '🦉 Die Eule lauscht...',
|
||||
'fox': '🦊 FunkFox droppt den Beat...',
|
||||
'bugsy': '🐞 Bugsy analysiert...'
|
||||
};
|
||||
statusEl.innerHTML = '<small style="color: #888;">' + (statusMessages[characterId] || 'Denkt nach...') + '</small>';
|
||||
|
||||
// Add user message to history
|
||||
const userMsg = document.createElement('div');
|
||||
userMsg.style.cssText = 'margin-bottom: 0.5rem; padding: 0.5rem; background: rgba(16,185,129,0.1); border-radius: 0.25rem; text-align: right;';
|
||||
userMsg.innerHTML = '<strong>Du:</strong> ' + escapeHtml(question);
|
||||
historyEl.appendChild(userMsg);
|
||||
|
||||
// Clear welcome message if present
|
||||
if (historyEl.querySelector('p')) {
|
||||
const welcomeMsg = historyEl.querySelector('p');
|
||||
if (welcomeMsg.textContent.includes('Stelle mir eine Frage')) {
|
||||
welcomeMsg.remove();
|
||||
/* Tablet: 2 columns */
|
||||
@media (min-width: 768px) {
|
||||
.crew-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll to bottom
|
||||
historyEl.scrollTop = historyEl.scrollHeight;
|
||||
|
||||
// Clear input
|
||||
inputEl.value = '';
|
||||
|
||||
try {
|
||||
// Call API
|
||||
const response = await fetch('/api/chat', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
character_id: characterId,
|
||||
question: question,
|
||||
lang: '{{ lang }}'
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('API request failed: ' + response.status);
|
||||
/* Desktop: Strictly 3 columns */
|
||||
@media (min-width: 1024px) {
|
||||
.crew-grid {
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Add assistant message to history
|
||||
const characterNames = {
|
||||
'eule': '🦉 Krümeleule',
|
||||
'fox': '🦊 FunkFox',
|
||||
'bugsy': '🐞 Bugsy'
|
||||
};
|
||||
const assistantMsg = document.createElement('div');
|
||||
assistantMsg.style.cssText = 'margin-bottom: 0.5rem; padding: 0.5rem; background: rgba(255,255,255,0.05); border-radius: 0.25rem;';
|
||||
assistantMsg.innerHTML = '<strong>' + (characterNames[characterId] || characterId) + ':</strong><br>' + escapeHtml(data.answer);
|
||||
|
||||
// Add sources if available
|
||||
if (data.sources && data.sources.length > 0) {
|
||||
const sourcesText = document.createElement('small');
|
||||
sourcesText.style.cssText = 'color: #888; display: block; margin-top: 0.5rem;';
|
||||
sourcesText.innerHTML = '📚 Quellen: ' + data.sources.length + ' Dokumente';
|
||||
assistantMsg.appendChild(sourcesText);
|
||||
}
|
||||
|
||||
historyEl.appendChild(assistantMsg);
|
||||
|
||||
// Scroll to bottom
|
||||
historyEl.scrollTop = historyEl.scrollHeight;
|
||||
|
||||
// Clear status
|
||||
statusEl.innerHTML = '';
|
||||
|
||||
} catch (error) {
|
||||
console.error('Chat error:', error);
|
||||
statusEl.innerHTML = '<small style="color: #f87171;">❌ Fehler: ' + escapeHtml(error.message) + '</small>';
|
||||
} finally {
|
||||
// Re-enable input
|
||||
inputEl.disabled = false;
|
||||
submitBtn.disabled = false;
|
||||
inputEl.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
.role-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
margin-bottom: 0;
|
||||
/* Override pico */
|
||||
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
overflow: hidden;
|
||||
/* Ensure rounded corners clip content */
|
||||
}
|
||||
|
||||
.role-card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.role-card header {
|
||||
text-align: center;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-icon {
|
||||
font-size: 3rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.role-card p {
|
||||
flex-grow: 1;
|
||||
/* Push footer down */
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
@@ -60,6 +60,22 @@
|
||||
</footer>
|
||||
</article>
|
||||
|
||||
<!-- Logs & Stats -->
|
||||
<article class="card">
|
||||
<header>
|
||||
<h3>📈 Logs & Stats</h3>
|
||||
</header>
|
||||
<p>
|
||||
Monitor chat comparisons, token usage, and role interactions.
|
||||
</p>
|
||||
<footer>
|
||||
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
|
||||
<a href="/admin/logs" role="button" class="secondary">View Logs</a>
|
||||
<a href="/admin/vectors" role="button" class="outline">Search Brain 🧠</a>
|
||||
</div>
|
||||
</footer>
|
||||
</article>
|
||||
|
||||
<!-- System Status -->
|
||||
<article class="card">
|
||||
<header>
|
||||
@@ -138,4 +154,4 @@
|
||||
</article>
|
||||
</section>
|
||||
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
204
app/templates/pages/admin_logs.html
Normal file
204
app/templates/pages/admin_logs.html
Normal file
@@ -0,0 +1,204 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<!-- Header -->
|
||||
<section class="mb-2">
|
||||
<div class="grid">
|
||||
<div>
|
||||
<h1>System Logs & Stats</h1>
|
||||
<p class="text-muted">Monitor chat interactions and token usage.</p>
|
||||
</div>
|
||||
<div style="text-align: right;">
|
||||
<a href="/admin" role="button" class="secondary outline">← Back to Dashboard</a>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Stats Grid -->
|
||||
<section class="grid" id="stats-container">
|
||||
<article class="card">
|
||||
<header>Total Interactions</header>
|
||||
<h2 id="stat-total-interactions">-</h2>
|
||||
</article>
|
||||
<article class="card">
|
||||
<header>Vector Coverage</header>
|
||||
<h2 id="stat-vector-coverage">-</h2>
|
||||
<small class="text-muted" id="stat-vector-count">-</small>
|
||||
</article>
|
||||
<article class="card">
|
||||
<header>Estimated Cost</header>
|
||||
<h2 id="stat-cost">-</h2>
|
||||
<small class="text-muted" id="stat-tokens">-</small>
|
||||
</article>
|
||||
<article class="card">
|
||||
<header>Log Size</header>
|
||||
<h2 id="stat-file-size">-</h2>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<!-- Role Stats -->
|
||||
<section>
|
||||
<h3>Role Usage</h3>
|
||||
<div id="role-stats" class="grid-4">
|
||||
<!-- Will be populated by JS -->
|
||||
<article class="loading">Loading stats...</article>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Logs Table -->
|
||||
<section style="margin-top: 2rem;">
|
||||
<div class="grid">
|
||||
<h3>Recent Interactions</h3>
|
||||
<div style="text-align: right;">
|
||||
<button onclick="loadData()" class="outline small">↻ Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<figure>
|
||||
<table role="grid">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Time</th>
|
||||
<th scope="col">Role</th>
|
||||
<th scope="col">User</th>
|
||||
<th scope="col">Interaction</th>
|
||||
<th scope="col">Tokens</th>
|
||||
<th scope="col">Model</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="logs-table-body">
|
||||
<tr>
|
||||
<td colspan="6" style="text-align: center;">Loading logs...</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</figure>
|
||||
</section>
|
||||
|
||||
<script>
|
||||
async function loadData() {
|
||||
try {
|
||||
await Promise.all([loadStats(), loadLogs()]);
|
||||
} catch (e) {
|
||||
console.error("Error loading data:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadStats() {
|
||||
const res = await fetch('/admin/logs/stats');
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
|
||||
document.getElementById('stat-total-interactions').innerText = data.total_interactions;
|
||||
|
||||
// Vector Coverage
|
||||
const rate = data.context_hit_rate_percent;
|
||||
document.getElementById('stat-vector-coverage').innerText = rate + "%";
|
||||
document.getElementById('stat-vector-count').innerText = `${data.context_found_count} hits`;
|
||||
|
||||
// Cost & Tokens
|
||||
document.getElementById('stat-cost').innerText = "$" + data.estimated_cost_usd.toFixed(4);
|
||||
document.getElementById('stat-tokens').innerText = `~${data.total_tokens_estimated} tokens`;
|
||||
|
||||
document.getElementById('stat-file-size').innerText = data.file_size_mb + " MB";
|
||||
|
||||
// Render Role Stats
|
||||
const roleContainer = document.getElementById('role-stats');
|
||||
roleContainer.innerHTML = '';
|
||||
|
||||
// Sort roles by usage (desc)
|
||||
const sortedRoles = Object.entries(data.characters).sort((a, b) => b[1] - a[1]);
|
||||
|
||||
sortedRoles.forEach(([roleId, count]) => {
|
||||
const tokens = data.tokens_by_role[roleId] || 0;
|
||||
const article = document.createElement('article');
|
||||
article.className = 'card small-card';
|
||||
article.innerHTML = `
|
||||
<header class="capitalize"><strong>${roleId}</strong></header>
|
||||
<div class="stat-value">${count} <small>chats</small></div>
|
||||
<div class="stat-sub">${tokens} <small>tokens</small></div>
|
||||
`;
|
||||
roleContainer.appendChild(article);
|
||||
});
|
||||
}
|
||||
|
||||
async function loadLogs() {
|
||||
const res = await fetch('/admin/logs/data?limit=50');
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
|
||||
const tbody = document.getElementById('logs-table-body');
|
||||
tbody.innerHTML = '';
|
||||
|
||||
data.logs.forEach(log => {
|
||||
const row = document.createElement('tr');
|
||||
|
||||
// Format time
|
||||
const date = new Date(log.timestamp);
|
||||
const timeStr = date.toLocaleString();
|
||||
|
||||
// Truncate text
|
||||
const q = log.interaction.question;
|
||||
const qShort = q.length > 50 ? q.substring(0, 50) + '...' : q;
|
||||
|
||||
row.innerHTML = `
|
||||
<td><small>${timeStr}</small></td>
|
||||
<td class="capitalize">${log.character.name}</td>
|
||||
<td>${log.user.email || log.user.id || 'Anonymous'}</td>
|
||||
<td>
|
||||
<strong>Q:</strong> ${escapeHtml(qShort)}<br>
|
||||
<small class="text-muted">Sources: ${log.rag.sources_count}</small>
|
||||
</td>
|
||||
<td>${log.tokens_estimated}</td>
|
||||
<td><small>${log.ai.model}</small></td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
return text
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
// Initial load
|
||||
document.addEventListener('DOMContentLoaded', loadData);
|
||||
|
||||
// Auto-refresh every 30s
|
||||
setInterval(loadData, 30000);
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.capitalize {
|
||||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
.small-card {
|
||||
padding: 1rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 1.5rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.stat-sub {
|
||||
font-size: 0.9rem;
|
||||
color: var(--muted-color);
|
||||
}
|
||||
|
||||
/* Grid 4 for role stats */
|
||||
.grid-4 {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 1rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
{% endblock %}
|
||||
219
app/templates/pages/admin_vectors.html
Normal file
219
app/templates/pages/admin_vectors.html
Normal file
@@ -0,0 +1,219 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<!-- Header -->
|
||||
<section class="mb-2">
|
||||
<div class="grid">
|
||||
<div>
|
||||
<h1>🧠 Brain Search (Vector Admin)</h1>
|
||||
<p class="text-muted">Inspect the knowledge base by searching vectors directly.</p>
|
||||
</div>
|
||||
<div style="text-align: right;">
|
||||
<a href="/admin" role="button" class="secondary outline">← Back to Dashboard</a>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Search Controls -->
|
||||
<section class="card">
|
||||
<div class="grid">
|
||||
<!-- Collection Select -->
|
||||
<label for="collection">
|
||||
Collection
|
||||
<select id="collection-select" onchange="performSearch()">
|
||||
<option value="" disabled selected>Loading...</option>
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<!-- Search Input -->
|
||||
<label for="query">
|
||||
Search Query
|
||||
<input type="search" id="query-input" placeholder="e.g. 'Wer ist die Eule?'"
|
||||
onkeydown="if(event.key==='Enter') performSearch()">
|
||||
</label>
|
||||
|
||||
<!-- Action -->
|
||||
<label>
|
||||
<!-- Spacer -->
|
||||
<button onclick="performSearch()">Search Brain 🔍</button>
|
||||
</label>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Results -->
|
||||
<section style="margin-top: 2rem;">
|
||||
<h3 id="results-header">Results</h3>
|
||||
<div id="results-container">
|
||||
<!-- Results injected here -->
|
||||
<article class="card">
|
||||
<p class="text-muted text-center">Enter a query to search the vector database.</p>
|
||||
</article>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Content Modal -->
|
||||
<dialog id="contentModal">
|
||||
<article style="min-width: 70vw; min-height: 50vh;">
|
||||
<header>
|
||||
<button aria-label="Close" rel="prev" onclick="closeModal()"></button>
|
||||
<p>
|
||||
<strong>📄 Source Content</strong>
|
||||
<span id="modalTitle" style="margin-left: 1rem; opacity: 0.7;"></span>
|
||||
</p>
|
||||
</header>
|
||||
<div id="modalBody" style="white-space: pre-wrap; font-family: monospace; max-height: 60vh; overflow-y: auto;">
|
||||
Loading...
|
||||
</div>
|
||||
</article>
|
||||
</dialog>
|
||||
|
||||
<script>
|
||||
async function loadCollections() {
|
||||
try {
|
||||
const res = await fetch('/admin/vectors/collections');
|
||||
const data = await res.json();
|
||||
const select = document.getElementById('collection-select');
|
||||
|
||||
// Clear loading option
|
||||
select.innerHTML = '';
|
||||
|
||||
if (data.collections.length === 0) {
|
||||
const opt = document.createElement('option');
|
||||
opt.text = "No collections found";
|
||||
select.add(opt);
|
||||
return;
|
||||
}
|
||||
|
||||
let selectedOne = false;
|
||||
// Handle both string array and object array if format changes
|
||||
data.collections.forEach((c, index) => {
|
||||
const name = typeof c === 'string' ? c : c.name;
|
||||
const opt = document.createElement('option');
|
||||
opt.value = name;
|
||||
opt.text = name;
|
||||
|
||||
// Auto-select something that looks like main content
|
||||
if (!selectedOne && (name.includes('post') || name.includes('crumbforest') || index === 0)) {
|
||||
opt.selected = true;
|
||||
selectedOne = true;
|
||||
}
|
||||
select.add(opt);
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.error("Error loading collections:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function performSearch() { // Renamed from searchVectors to match onchange/onclick
|
||||
const query = document.getElementById('query-input').value;
|
||||
const collection = document.getElementById('collection-select').value;
|
||||
const resultsDiv = document.getElementById('results-container');
|
||||
|
||||
if (!query) return;
|
||||
|
||||
resultsDiv.innerHTML = '<article aria-busy="true">Searching...</article>';
|
||||
|
||||
try {
|
||||
const res = await fetch('/admin/vectors/search', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query, collection, limit: 10 })
|
||||
});
|
||||
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (data.error) throw new Error(data.error);
|
||||
|
||||
const results = data.results || [];
|
||||
|
||||
if (results.length === 0) {
|
||||
resultsDiv.innerHTML = '<article>No results found.</article>';
|
||||
return;
|
||||
}
|
||||
|
||||
const html = results.map(r => {
|
||||
const payload = r.payload || {};
|
||||
const source = payload.title || payload.slug || 'Unknown Source';
|
||||
// Highlight matches? Maybe later.
|
||||
const content = payload.content ? payload.content.substring(0, 150).replace(/</g, "<") + "..." : "No content preview";
|
||||
const scoreColor = r.score > 0.8 ? 'green' : (r.score > 0.7 ? 'orange' : 'red');
|
||||
const postId = payload.post_id || payload.id; // Try both
|
||||
|
||||
const giteaBase = 'https://194-164-194-191.sslip.io/git/kruemel/Crumb-Core-v.1/src/branch/main/';
|
||||
const giteaLink = payload.file_path ? (giteaBase + payload.file_path) : null;
|
||||
|
||||
return `
|
||||
<article class="card" style="margin-bottom: 1rem;">
|
||||
<header>
|
||||
<strong style="color: ${scoreColor}">${(r.score * 100).toFixed(1)}% Match</strong>
|
||||
<span class="badge secondary">${source}</span>
|
||||
${postId ? `<button class="outline contrast" style="float: right; padding: 2px 10px; font-size: 0.8em;" onclick="viewContent(${postId}, '${source}')">👀 View Full</button>` : ''}
|
||||
${giteaLink ? `<button class="outline secondary" style="float: right; padding: 2px 10px; font-size: 0.8em; margin-right: 0.5rem;" onclick="window.open('${giteaLink}', '_blank')">🐙 Gitea</button>` : ''}
|
||||
</header>
|
||||
<blockquote>${content}</blockquote>
|
||||
<details>
|
||||
<summary><small>View Full Payload (JSON)</small></summary>
|
||||
<pre><code>${JSON.stringify(payload, null, 2)}</code></pre>
|
||||
</details>
|
||||
</article>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
resultsDiv.innerHTML = html;
|
||||
|
||||
} catch (e) {
|
||||
resultsDiv.innerHTML = `<article class="headings"><p style="color:var(--pico-del-color)">Error: ${e.message}</p></article>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function viewContent(postId, title) {
|
||||
if (!postId) {
|
||||
alert("No Post ID available.");
|
||||
return;
|
||||
}
|
||||
|
||||
const modal = document.getElementById('contentModal');
|
||||
const modalTitle = document.getElementById('modalTitle');
|
||||
const modalBody = document.getElementById('modalBody');
|
||||
|
||||
modalTitle.innerText = title;
|
||||
modalBody.innerText = "Loading...";
|
||||
modal.showModal();
|
||||
|
||||
try {
|
||||
const res = await fetch(`/admin/vectors/content/${postId}`);
|
||||
if (!res.ok) throw new Error("Content not found (might be deleted or orphaned)");
|
||||
|
||||
const data = await res.json();
|
||||
modalBody.innerText = data.content || "No content text.";
|
||||
|
||||
} catch (e) {
|
||||
modalBody.innerHTML = `<span style="color: red;">Error: ${e.message}</span>
|
||||
<br><small>This confirms the vector is 'Orphaned' (exists in brain, but body is gone).</small>`;
|
||||
}
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById('contentModal').close();
|
||||
}
|
||||
|
||||
// Auto-load collections
|
||||
loadCollections();
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.text-center {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
blockquote {
|
||||
border-left: 4px solid var(--primary);
|
||||
padding-left: 1rem;
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
padding: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user