Aug 7 - Update: knowledge base MT19

This commit is contained in:
2026-08-07 17:32:43 -04:00
parent 38ab66d021
commit 3c35835505
6 changed files with 276 additions and 2 deletions
+28 -2
View File
@@ -91,7 +91,9 @@ FAQS = [
def _system_prompt_with_kb():
"""Return the Groq system prompt, appending active knowledge base entries."""
try:
entries = SupportKnowledge.query.filter_by(active=True).order_by(SupportKnowledge.id).all()
entries = (SupportKnowledge.query.filter_by(active=True)
.order_by(SupportKnowledge.sort_order.asc(),
SupportKnowledge.id.asc()).all())
except Exception:
return _SYSTEM_PROMPT
if not entries:
@@ -516,10 +518,31 @@ def admin_conversation_detail(session_id):
@login_required
@supervisor_required
def admin_knowledge():
entries = SupportKnowledge.query.order_by(SupportKnowledge.created_at.desc()).all()
# Same order the chat prompt uses, so the admin list shows the real
# priority rather than a different one.
entries = (SupportKnowledge.query
.order_by(SupportKnowledge.sort_order.asc(),
SupportKnowledge.id.asc()).all())
return render_template('support/admin_knowledge.html', entries=entries)
def _parse_sort_order(raw, fallback=0):
"""Coerce a submitted sort_order to a sane int.
The column is NOT NULL, so a blank or non-numeric field must not reach the
DB. Clamped to 0..9999 to match the range ST validates, and falls back to
the existing value on edit so a blank field means "leave it alone" rather
than silently resetting the entry to the top.
"""
raw = (raw or '').strip()
if not raw:
return fallback
try:
return max(0, min(9999, int(raw)))
except (TypeError, ValueError):
return fallback
@bp.route('/admin/knowledge/add', methods=['POST'])
@login_required
@supervisor_required
@@ -534,6 +557,7 @@ def admin_knowledge_add():
title = title,
body = body,
active = True,
sort_order = _parse_sort_order(request.form.get('sort_order')),
created_by = current_user.id,
created_at = now_eastern(),
updated_at = now_eastern(),
@@ -561,6 +585,8 @@ def admin_knowledge_edit(entry_id):
return redirect(url_for('support.admin_knowledge_edit', entry_id=entry_id))
entry.title = title
entry.body = body
entry.sort_order = _parse_sort_order(request.form.get('sort_order'),
entry.sort_order)
entry.updated_at = now_eastern()
db.session.commit()
log_action(ACTION_UPDATE, 'SupportKnowledge', entry.id, title[:60], 'edited')