Muyue 86eb68c0e6 feat: Enhanced mode v2 with Smart Adaptive strategy, comprehensive logging, and internationalization
Major Features:
  - Enhanced Mode v2: Smart Adaptive section selection strategy
    * AI automatically selects ONE optimal section per iteration
    * Intelligent selection based on quality assessment and strategic importance
    * Section tracking to avoid duplicate modifications
    * Fixed header level preservation (## stays ##, ### stays ###)
    * Updated document code block format (document)

  - Mermaid Auto-Fix System
    * New POST /api/ai/fix-mermaid endpoint for automatic diagram correction
    * Automatic error detection in preview and presentation modes
    * Inter-window communication for seamless editor updates
    * AI-powered syntax error resolution

  - Comprehensive Logging System
    * Centralized logger utility (utils/logger.js)
    * API request/response middleware logging
    * AI operations: rephrase, check-inconsistencies, check-duplications, give-advice, liberty-mode, fix-mermaid
    * Storage operations: create, read, update, delete journals
    * Export operations: PDF and Web ZIP generation
    * Structured logging with timestamps, levels, categories, and metadata

  Improvements:
  - Table of Contents: Clean display with markdown formatting stripped from titles
  - Section badges: Fixed visibility with hardcoded blue background (#2563eb)
  - Internationalization: Complete English translation (lang, UI text, placeholders, comments)
  - Code cleanup: Removed all emojis from codebase

  Technical Changes:
  - routes/ai.js: Enhanced mode implementation, Mermaid fix endpoint, comprehensive logging
  - routes/api.js: Storage operation logging
  - routes/export.js: Export operation logging
  - routes/index.js: Presentation mode Mermaid auto-fix
  - assets/js/app.js: TOC markdown stripping, Mermaid error handling, message listeners
  - assets/css/style.css: Dark mode comment, English placeholders, header icon
  - views/page.js: English lang attribute, favicon, scroll-to-top button
  - views/header.js: Theme toggle icon
  - utils/logger.js: New centralized logging system
  - app.js: API request/response logging middleware
2025-10-15 10:21:54 +02:00

194 lines
4.5 KiB
JavaScript

/**
* Centralized logging system for Conception Assistant
* Provides structured logging for all application events
*/
const LOG_LEVELS = {
ERROR: 'ERROR',
WARN: 'WARN',
INFO: 'INFO',
DEBUG: 'DEBUG'
};
const LOG_CATEGORIES = {
API: 'API',
AI: 'AI',
UI: 'UI',
STORAGE: 'STORAGE',
EXPORT: 'EXPORT',
AUTH: 'AUTH',
SYSTEM: 'SYSTEM'
};
class Logger {
constructor() {
this.enabled = process.env.LOGGING_ENABLED !== 'false';
this.logLevel = process.env.LOG_LEVEL || 'INFO';
}
log(level, category, message, metadata = {}) {
if (!this.enabled) return;
const timestamp = new Date().toISOString();
const logEntry = {
timestamp,
level,
category,
message,
...metadata
};
const formattedMessage = `[${timestamp}] [${level}] [${category}] ${message}`;
switch (level) {
case LOG_LEVELS.ERROR:
console.error(formattedMessage, metadata);
break;
case LOG_LEVELS.WARN:
console.warn(formattedMessage, metadata);
break;
case LOG_LEVELS.INFO:
console.info(formattedMessage, metadata);
break;
case LOG_LEVELS.DEBUG:
console.log(formattedMessage, metadata);
break;
default:
console.log(formattedMessage, metadata);
}
return logEntry;
}
// API Request logging
apiRequest(method, endpoint, payload = {}) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.API, `${method} ${endpoint}`, {
method,
endpoint,
payloadSize: JSON.stringify(payload).length
});
}
apiResponse(endpoint, statusCode, duration) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.API, `Response ${endpoint}`, {
endpoint,
statusCode,
duration: `${duration}ms`
});
}
apiError(endpoint, error) {
return this.log(LOG_LEVELS.ERROR, LOG_CATEGORIES.API, `API Error ${endpoint}`, {
endpoint,
error: error.message,
stack: error.stack
});
}
// AI Operations logging
aiRequest(operation, model, tokens = 0) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.AI, `AI Request: ${operation}`, {
operation,
model,
tokens
});
}
aiResponse(operation, success, duration) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.AI, `AI Response: ${operation}`, {
operation,
success,
duration: `${duration}ms`
});
}
aiError(operation, error) {
return this.log(LOG_LEVELS.ERROR, LOG_CATEGORIES.AI, `AI Error: ${operation}`, {
operation,
error: error.message
});
}
// UI Events logging
uiEvent(action, component, details = {}) {
return this.log(LOG_LEVELS.DEBUG, LOG_CATEGORIES.UI, `UI Event: ${action}`, {
action,
component,
...details
});
}
// Storage operations
storageWrite(operation, journalId) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.STORAGE, `Storage Write: ${operation}`, {
operation,
journalId
});
}
storageRead(operation, journalId) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.STORAGE, `Storage Read: ${operation}`, {
operation,
journalId
});
}
storageError(operation, error) {
return this.log(LOG_LEVELS.ERROR, LOG_CATEGORIES.STORAGE, `Storage Error: ${operation}`, {
operation,
error: error.message
});
}
// Export operations
exportOperation(type, format, size) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.EXPORT, `Export: ${type}`, {
type,
format,
size: `${size} bytes`
});
}
// System events
systemStart(port) {
return this.log(LOG_LEVELS.INFO, LOG_CATEGORIES.SYSTEM, 'Application started', {
port,
nodeVersion: process.version,
platform: process.platform
});
}
systemError(message, error) {
return this.log(LOG_LEVELS.ERROR, LOG_CATEGORIES.SYSTEM, message, {
error: error.message,
stack: error.stack
});
}
// Generic logging methods
error(category, message, metadata = {}) {
return this.log(LOG_LEVELS.ERROR, category, message, metadata);
}
warn(category, message, metadata = {}) {
return this.log(LOG_LEVELS.WARN, category, message, metadata);
}
info(category, message, metadata = {}) {
return this.log(LOG_LEVELS.INFO, category, message, metadata);
}
debug(category, message, metadata = {}) {
return this.log(LOG_LEVELS.DEBUG, category, message, metadata);
}
}
// Export singleton instance
const logger = new Logger();
module.exports = {
logger,
LOG_LEVELS,
LOG_CATEGORIES
};