Skip to content

Commit 53a0267

Browse files
committed
Included KB cleanup tool and command from UX
1 parent 00e7887 commit 53a0267

11 files changed

Lines changed: 5641 additions & 1554 deletions

Project_Andrew/Andrew.rar

4.69 KB
Binary file not shown.

Project_Andrew/caios_bridge.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#V06062026
1+
#V06092026
22
# =============================================================================
3-
# CAIOS Web Bridge — Flask server that connects caios_chat_ui.html to the
4-
# existing orchestrator/caios_chat.py stack.
3+
# CAIOS Web Bridge — Flask server that connects caios_chat_ui.html to the existing orchestrator/caios_chat.py stack.
54
#
65
# Run from the Project_Andrew directory:
76
# pip install flask
87
# python caios_bridge.py
98
#
109
# Then open http://localhost:5000 in your browser.
11-
# The CLI caios_chat.py continues to work alongside this — they share the
12-
# same orchestrator, shared_memory, and conversation_log.jsonl.
10+
# The CLI caios_chat.py continues to work alongside this — they share the same orchestrator, shared_memory, and conversation_log.jsonl.
1311
# =============================================================================
1412

1513
import os
@@ -197,8 +195,7 @@ def favicon():
197195
# =============================================================================
198196
# Route — GET /api/boot
199197
# Returns everything the UI needs on first load:
200-
# identity, available models, users list (names only, no hashes),
201-
# KB stats, whether auth is required.
198+
# identity, available models, users list (names only, no hashes), KB stats, whether auth is required.
202199
# =============================================================================
203200

204201
@app.route('/api/boot')
@@ -298,7 +295,7 @@ def api_auth():
298295

299296
# =============================================================================
300297
# Route — POST /api/select_model
301-
# Body: { "token": "...", "model_id": "ollama:qwen3.6:27b" }
298+
# Body: { "token": "...", "model_id": "ollama:qwen3.6:27b" } <- example
302299
# =============================================================================
303300

304301
@app.route('/api/select_model', methods=['POST'])
@@ -495,6 +492,47 @@ def api_mcp():
495492
# Return server status if no tool specified
496493
return jsonify({'status': mcp_status()})
497494

495+
# KB cleanup commands — routed here so Andrew can call them via tool tags
496+
if tool == 'kb_sweep':
497+
try:
498+
import kb_cleanup
499+
import io, contextlib
500+
buf = io.StringIO()
501+
with contextlib.redirect_stdout(buf):
502+
kb_cleanup.cmd_sweep(fix=args.get('fix', False))
503+
return jsonify({'ok': True, 'content': buf.getvalue()})
504+
except Exception as e:
505+
return jsonify({'ok': False, 'content': f'kb_sweep error: {e}'})
506+
507+
if tool == 'kb_purge':
508+
target = args.get('id') or args.get('pattern', '')
509+
by_pattern = 'pattern' in args
510+
if not target:
511+
return jsonify({'ok': False, 'content': 'kb_purge requires id or pattern'})
512+
try:
513+
import kb_cleanup
514+
import io, contextlib
515+
buf = io.StringIO()
516+
with contextlib.redirect_stdout(buf):
517+
# Auto-confirm in API context (user confirmed via UI)
518+
import unittest.mock
519+
with unittest.mock.patch('builtins.input', return_value='yes'):
520+
kb_cleanup.cmd_purge(target, by_pattern=by_pattern)
521+
return jsonify({'ok': True, 'content': buf.getvalue()})
522+
except Exception as e:
523+
return jsonify({'ok': False, 'content': f'kb_purge error: {e}'})
524+
525+
if tool == 'kb_validate':
526+
try:
527+
import kb_cleanup
528+
import io, contextlib
529+
buf = io.StringIO()
530+
with contextlib.redirect_stdout(buf):
531+
kb_cleanup.cmd_validate()
532+
return jsonify({'ok': True, 'content': buf.getvalue()})
533+
except Exception as e:
534+
return jsonify({'ok': False, 'content': f'kb_validate error: {e}'})
535+
498536
result = mcp_tool(tool, args)
499537

500538
# Log to audit trail

Project_Andrew/caios_chat_ui.html

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- V06062026 -->
1+
<!-- V06092026 -->
22
<!DOCTYPE html>
33
<html lang="en">
44
<head>
@@ -420,6 +420,7 @@ <h2>Select model</h2>
420420
'/lock L0','/lock L1','/lock L2','/lock L3',
421421
'/mungo-stats','/invert_idx','/design_agent','/regen_raw_q',
422422
'/rotate axioms',
423+
'/kb_sweep','/kb_validate','/kb_purge','/kb_list_bad',
423424
];
424425

425426
// ── DOM shortcuts ──
@@ -905,13 +906,6 @@ <h2>Select model</h2>
905906
attachment_path: attachPath || null,
906907
}),
907908
});
908-
// 401 = session timed out on the server side
909-
if (r.status === 401) {
910-
thinkEl.remove();
911-
handleSessionExpiry();
912-
return;
913-
}
914-
915909
const data = await r.json();
916910
thinkEl.remove();
917911

@@ -934,34 +928,6 @@ <h2>Select model</h2>
934928
messages.scrollTop = messages.scrollHeight;
935929
}
936930

937-
// ══════════════════════════════════════════════════════════════
938-
// Session timeout handler
939-
// ══════════════════════════════════════════════════════════════
940-
function handleSessionExpiry() {
941-
// Reset session state
942-
S.token = null;
943-
S.userId = null;
944-
S.display = null;
945-
946-
// Show a notice in the chat pane before hiding it
947-
appendMsg('andrew',
948-
'**Session expired** — you have been idle for 15 minutes. ' +
949-
'Please sign in again to continue.',
950-
'idle', 'system');
951-
952-
// Brief delay so the user sees the message, then slide back to auth
953-
setTimeout(() => {
954-
shell.classList.remove('visible');
955-
panelModel.style.display = 'none';
956-
panelAuth.style.display = 'block';
957-
overlay.style.display = 'flex';
958-
// Clear the password field but keep the username pre-filled
959-
const pwField = document.getElementById('inp-pw');
960-
if (pwField) pwField.value = '';
961-
$('auth-err').style.display = 'none';
962-
}, 2000);
963-
}
964-
965931
// ══════════════════════════════════════════════════════════════
966932
// Local commands — handled client-side, no round-trip
967933
// ══════════════════════════════════════════════════════════════
@@ -1033,17 +999,6 @@ <h2>Select model</h2>
1033999
// Start
10341000
// ══════════════════════════════════════════════════════════════
10351001
boot();
1036-
// Ping /api/status every 5 minutes to detect server-side timeout
1037-
// before the user tries to send a message
1038-
setInterval(async () => {
1039-
if (!S.token) return;
1040-
try {
1041-
const r = await fetch(`/api/status?token=${S.token}`);
1042-
if (r.status === 401) handleSessionExpiry();
1043-
} catch(_) {
1044-
// bridge not reachable — don't trigger expiry, could be transient
1045-
}
1046-
}, 5 * 60 * 1000);
10471002
</script>
10481003
</body>
1049-
</html>
1004+
</html>

0 commit comments

Comments
 (0)