import uuid from fasthtml.common import * from starlette.staticfiles import StaticFiles from starlette.responses import FileResponse # Imports ultra-propres depuis les modules dédiés from modules.theme import get_theme_hdrs from modules.database import nettoyer_vieilles_discussions, HISTORY_FILE, load_history, save_history from modules.ui_components import render_main_page, render_sidebar from modules.chat_handler import handle_chat_request # On exécute le nettoyage au démarrage nettoyer_vieilles_discussions() # Initialisation du serveur FastHTML app, rt = fast_app(hdrs=get_theme_hdrs(), secret_key="super_secret_aethas") # Montage du dossier statique pour rendre accessible le logo app.mount("/Logo", StaticFiles(directory="Logo"), name="Logo") @rt('/') def get(session): if 'current_chat_id' not in session or not session['current_chat_id']: session['current_chat_id'] = str(uuid.uuid4()) return render_main_page(session['current_chat_id']) @rt('/chat') async def post(request, session: dict): return await handle_chat_request(request, session) @rt('/favicon.ico') def get(): return HTTPResponse(status_code=204) @rt('/new_chat') def get(session): session['current_chat_id'] = str(uuid.uuid4()); return RedirectResponse('/', status_code=303) @rt('/load_chat/{cid}') def get(cid: str, session): if cid and cid != "null": session['current_chat_id'] = cid return RedirectResponse('/', status_code=303) @rt('/rename/{cid}') def post(cid: str, req): h = load_history() if cid in h and (nom := req.headers.get("HX-Prompt")): h[cid]['title'] = nom; save_history(h) return render_sidebar() @rt('/pin/{cid}') def post(cid: str): h = load_history() if cid in h: h[cid]['pinned'] = not h[cid].get('pinned', False); save_history(h) return render_sidebar() @rt('/delete/{cid}') def post(cid: str): h, cid_str = load_history(), str(cid) if cid_str in h: del h[cid_str]; save_history(h) return render_sidebar() @rt('/export_json') def get(): return FileResponse(HISTORY_FILE, media_type='application/json', filename='Aethas38_Discussion_Globale.json') if __name__ == '__main__': serve(port=5001)