#!/usr/bin/env python3 """ Patch frontend: Add dataset fallback to ExpiredBanner handleCatchUp. When localStorage caches (backend-messages, ui-messages) are empty (user cleared cache, new browser, Space restarted), the catch-up flow shows "Nothing to summarize from this chat.". This patch adds a fallback that fetches messages from /api/session-history/{session_id} — which reads from the permanent HF dataset (bep40/ml-intern-sessions). This ensures the "Catch me up" button always works as long as the session was previously logged to the permanent dataset. """ import os import sys PATCHED_MARKER = "// === PATCH: catch-up dataset fallback ===" TARGET_FILE = "/source/frontend/src/components/Chat/ExpiredBanner.tsx" def patch_expired_banner(content: str) -> tuple[str, bool]: changed = False if PATCHED_MARKER in content: print(f" SKIP: already patched") return content, False # Add a fallback to /api/session-history/{session_id} before the error. # The old block checks if messages array is empty and bails with error. old_block = ( " if (!messages || messages.length === 0) {\n" " setError('Nothing to summarize from this chat.');\n" " setBusy(null);\n" " return;\n" " }\n" ) new_block = ( PATCHED_MARKER + "\n" " // Fallback: when localStorage caches are empty (Space restart,\n" " // new browser, cache cleared), read from the permanent HF dataset\n" " // via /api/session-history/{session_id}.\n" " if (!messages || messages.length === 0) {\n" " try {\n" " const res = await apiFetch(`/api/session-history/${sessionId}`);\n" " if (res.ok) {\n" " const history = await res.json();\n" " if (Array.isArray(history) && history.length > 0) {\n" " messages = history.map((row: any) => ({\n" " role: row.role || 'assistant',\n" " content: row.content || '',\n" " }));\n" " }\n" " }\n" " } catch {\n" " /* dataset fetch failed — continue to error below */\n" " }\n" " }\n" " if (!messages || messages.length === 0) {\n" " setError('Nothing to summarize from this chat.');\n" " setBusy(null);\n" " return;\n" " }\n" ) if old_block in content: content = content.replace(old_block, new_block, 1) changed = True print(" + Added dataset fallback to handleCatchUp") else: print(" SKIP: target block not found (pattern may have changed)") return content, changed def main(): if not os.path.exists(TARGET_FILE): print(f"SKIP: {TARGET_FILE} not found — frontend source not available") return with open(TARGET_FILE, "r", encoding="utf-8") as f: content = f.read() if PATCHED_MARKER in content: print(f"SKIP: {TARGET_FILE} already patched") return content, changed = patch_expired_banner(content) if changed: with open(TARGET_FILE, "w", encoding="utf-8") as f: f.write(content) print(f"OK: {TARGET_FILE} patched with dataset fallback") else: print(f"SKIP: {TARGET_FILE} — no changes applied") if __name__ == "__main__": try: main() except Exception as e: print(f"WARN: patch_catch_up_dataset_fallback.py error: {e}") # Never fail the Docker build sys.exit(0)