Add application logging, SMTP email notifications, and supporting changes
Phase 5.4 — Application Logging: - AppLog model + migration 0010 for unified audit/usage/error logging - Usage logging middleware in app.py (after_request for /api/* requests) - Error logging in 500 handler with traceback capture - Audit logging for auth events (register, login, login_failed, logout, password_change) and admin actions (backup, restore, user_delete, registration_toggle, smtp_config, smtp_test) - Admin log viewer (LogsView.vue) with stats, category/search/date filters, paginated table with expandable detail rows - Admin logs API endpoints in admin.py (GET /logs, GET /logs/stats) - Configurable retention via LOG_RETENTION_DAYS with hourly cleanup Phase 5.5 — SMTP Email Notifications: - aiosmtplib dependency for async email sending - Email service (services/email.py) with STARTTLS/implicit TLS support - Notification service (services/notifications.py) for security alerts and task due date reminders with per-user preferences - Admin SMTP config endpoints (GET/PUT /api/admin/smtp, POST test) - SMTP config in Config class with env var + Docker secret support - Settings UI: notification preferences for all users, SMTP config section for admin with test email Other changes: - stream_chat() now accepts optional options dict (for num_predict) - Increase assist MAX_BODY_CHARS from 3000 to 8000 - get_user_by_username() added to auth service - apiStreamPost buffer processing refactored for robustness - AppHeader: admin Logs nav link - Router: /admin/logs route Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+24
-24
@@ -226,22 +226,25 @@ export async function apiStreamPost(
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
|
||||
function processLines(text: string) {
|
||||
const lines = text.split("\n");
|
||||
function processLine(line: string) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("data: ")) {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(trimmed.slice(6));
|
||||
} catch {
|
||||
return; // Skip malformed JSON lines
|
||||
}
|
||||
onChunk(data);
|
||||
}
|
||||
}
|
||||
|
||||
function processBuffer() {
|
||||
const lines = buffer.split("\n");
|
||||
// Keep the last (possibly incomplete) line in the buffer
|
||||
buffer = lines.pop() || "";
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("data: ")) {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(trimmed.slice(6));
|
||||
} catch {
|
||||
continue; // Skip malformed JSON lines
|
||||
}
|
||||
onChunk(data);
|
||||
}
|
||||
processLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,22 +253,19 @@ export async function apiStreamPost(
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
processLines(buffer);
|
||||
processBuffer();
|
||||
}
|
||||
} catch {
|
||||
// Stream may close with a network error after all data was sent.
|
||||
// Process whatever remains in the buffer before deciding to throw.
|
||||
}
|
||||
|
||||
// Process any remaining buffer
|
||||
const remaining = buffer.trim();
|
||||
if (remaining.startsWith("data: ")) {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(remaining.slice(6));
|
||||
} catch {
|
||||
return; // Skip malformed JSON
|
||||
// Flush any remaining complete lines in the buffer
|
||||
if (buffer.trim()) {
|
||||
// The buffer may contain one or more unprocessed lines
|
||||
const remaining = buffer;
|
||||
buffer = "";
|
||||
for (const line of remaining.split("\n")) {
|
||||
processLine(line);
|
||||
}
|
||||
onChunk(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user