Spaces:
Running
Running
Download patch_catch_up_fix_v2.py from bep40/ml-intern: direct link, hf CLI and curl.
- Browser
- Download file 31.9 kB
-
https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_catch_up_fix_v2.py
- Command line
-
hf download hf://spaces/bep40/ml-intern/patch_catch_up_fix_v2.py
-
curl -L -o patch_catch_up_fix_v2.py https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_catch_up_fix_v2.py
31.9 kB
| #!/usr/bin/env python3 | |
| """ | |
| Patch v2: Comprehensive catch-up + shareable URL fix for bep40/ml-intern. | |
| Addresses three problems: | |
| 1. "Catch me up" throws errors frequently | |
| 2. Context is lost after catch-up (context preserved in session but not restored) | |
| 3. Per-session shareable URLs | |
| Runs during Docker build β never fails the build. | |
| """ | |
| import ast | |
| import os | |
| import re | |
| import sys | |
| # βββ Backend paths (exist in the Python/stage-2 build) βββββββββββββββββββββββ | |
| BACKEND_SESSION_MANAGER = "/app/backend/session_manager.py" | |
| BACKEND_SESSION_LOGGER = "/app/backend/session_logger.py" | |
| BACKEND_ROUTES_AGENT = "/app/backend/routes/agent.py" | |
| MARKER = "# === PATCH: catch-up-v2 ===" | |
| def _read(path): | |
| with open(path, "r", encoding="utf-8") as f: | |
| return f.read() | |
| def _write(path, content): | |
| with open(path, "w", encoding="utf-8") as f: | |
| f.write(content) | |
| def _safe_replace(content, old, new): | |
| if old in content: | |
| return content.replace(old, new, 1), True | |
| return content, False | |
| # βββ Backend session_manager.py βββββββββββββββββββββββββββββββββββββββββββββ | |
| print("=== Patching session_manager.py ===") | |
| if os.path.exists(BACKEND_SESSION_MANAGER): | |
| content = _read(BACKEND_SESSION_MANAGER) | |
| changed = False | |
| # 1. seed_from_summary: fall back to raw messages on summary failure | |
| if 'logger.error("Summary call failed during seed: %s", e)' in content and "Fall back to raw messages" not in content: | |
| old = ( | |
| ' except Exception as e:\n' | |
| ' logger.error("Summary call failed during seed: %s", e)\n' | |
| ' raise\n' | |
| ) | |
| new = ( | |
| ' except Exception as e:\n' | |
| ' logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\n' | |
| ' for msg in parsed:\n' | |
| ' session.context_manager.items.append(msg)\n' | |
| ' self._touch(agent_session)\n' | |
| ' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n' | |
| ' return len(parsed)\n' | |
| ) | |
| if old in content: | |
| content, ok = _safe_replace(content, old, new) | |
| if ok: | |
| print(" + seed_from_summary falls back to raw messages") | |
| changed = True | |
| else: | |
| # Try regex match for the pattern | |
| pattern = re.compile( | |
| r' logger\.error\("Summary call failed during seed: %s", e\)\n raise\n' | |
| ) | |
| match = pattern.search(content) | |
| if match: | |
| content = pattern.sub( | |
| ' logger.error("Summary call failed during seed, falling back to raw messages: %s", e)\n' | |
| ' for msg in parsed:\n' | |
| ' session.context_manager.items.append(msg)\n' | |
| ' self._touch(agent_session)\n' | |
| ' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n' | |
| ' return len(parsed)\n', | |
| content, count=1 | |
| ) | |
| print(" + seed_from_summary fallback (regex match)") | |
| changed = True | |
| # 2. seed_from_summary: skip event rows | |
| if "skip event rows" not in content.lower(): | |
| old_parse = ( | |
| ' parsed: list[Message] = []\n' | |
| ' for raw in messages:\n' | |
| ' if raw.get("role") == "system":\n' | |
| ' continue # the new session has its own system prompt\n' | |
| ) | |
| new_parse = ( | |
| ' parsed: list[Message] = []\n' | |
| ' for raw in messages:\n' | |
| ' role = raw.get("role", "")\n' | |
| ' if role in ("system", "event"):\n' | |
| ' continue # not conversation messages\n' | |
| ) | |
| if old_parse in content: | |
| content, ok = _safe_replace(content, old_parse, new_parse) | |
| if ok: | |
| print(" + seed_from_summary skips event rows") | |
| changed = True | |
| # 3. Add record_catchup_done method | |
| if "def record_catchup_done" not in content: | |
| insert_after = " return len(parsed)\n\n @staticmethod" | |
| if insert_after in content: | |
| new_method = ( | |
| " return len(parsed)\n\n" | |
| " async def record_catchup_done(self, session_id: str) -> None:\n" | |
| " \"\"\"Record that a session was successfully catch-up restored.\"\"\"\n" | |
| " store = self._store()\n" | |
| " if not getattr(store, 'enabled', False):\n" | |
| " return\n" | |
| " try:\n" | |
| " await store.update_session_fields(\n" | |
| " session_id,\n" | |
| " catchup_done=True,\n" | |
| " )\n" | |
| " except Exception as e:\n" | |
| " logger.debug('record_catchup_done failed: %s', e)\n\n" | |
| " @staticmethod" | |
| ) | |
| content, ok = _safe_replace(content, insert_after, new_method) | |
| if ok: | |
| print(" + Added record_catchup_done method") | |
| changed = True | |
| # 4. Record catchup_done after seed | |
| old_seed_done = ( | |
| ' session.context_manager.items.append(seed)\n' | |
| ' self._touch(agent_session)\n' | |
| ' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n' | |
| ' return len(parsed)\n' | |
| ) | |
| new_seed_done = ( | |
| ' session.context_manager.items.append(seed)\n' | |
| ' self._touch(agent_session)\n' | |
| ' await self.persist_session_snapshot(agent_session, runtime_state="idle")\n' | |
| ' # === PATCH: catch-up-v2 ===\n' | |
| ' try:\n' | |
| ' await self.record_catchup_done(session_id)\n' | |
| ' except Exception as e:\n' | |
| ' logger.debug("record_catchup_done failed: %s", e)\n' | |
| ' return len(parsed)\n' | |
| ) | |
| if old_seed_done in content: | |
| content, ok = _safe_replace(content, old_seed_done, new_seed_done) | |
| if ok: | |
| print(" + seed_from_summary records catchup_done") | |
| changed = True | |
| if changed: | |
| if MARKER not in content: | |
| content = content + "\n" + MARKER + "\n" | |
| try: | |
| ast.parse(content) | |
| _write(BACKEND_SESSION_MANAGER, content) | |
| print("OK: session_manager.py patched") | |
| except SyntaxError as e: | |
| print(f"FAIL: session_manager.py syntax: {e}") | |
| else: | |
| print("SKIP: session_manager.py not found") | |
| # βββ Backend session_logger.py βββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching session_logger.py ===") | |
| if os.path.exists(BACKEND_SESSION_LOGGER): | |
| content = _read(BACKEND_SESSION_LOGGER) | |
| changed = False | |
| # load_session_history: filter event rows at source | |
| sort_line = ' rows.sort(key=lambda r: (r.get("seq", 0), r.get("timestamp", "")))' | |
| if sort_line in content and 'valid_roles' not in content.split(sort_line, 1)[1][:300]: | |
| idx = content.find(sort_line) | |
| end_idx = content.find("\n", idx) + 1 | |
| insert_code = ( | |
| ' # === PATCH: catch-up-v2 ===\n' | |
| ' # Filter out event/system rows so summarization never sees\n' | |
| ' # internal tracing data that cant be parsed as Message objects.\n' | |
| ' valid_roles = {"user", "assistant", "tool"}\n' | |
| ' rows = [r for r in rows if r.get("role") in valid_roles]\n' | |
| ) | |
| content = content[:end_idx] + insert_code + content[end_idx:] | |
| print(" + load_session_history filters event rows") | |
| changed = True | |
| if changed: | |
| try: | |
| ast.parse(content) | |
| _write(BACKEND_SESSION_LOGGER, content) | |
| print("OK: session_logger.py patched") | |
| except SyntaxError as e: | |
| print(f"FAIL: session_logger.py syntax: {e}") | |
| else: | |
| print("SKIP: session_logger.py not found") | |
| # βββ Backend routes/agent.py βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching routes/agent.py ===") | |
| if os.path.exists(BACKEND_ROUTES_AGENT): | |
| content = _read(BACKEND_ROUTES_AGENT) | |
| changed = False | |
| # Ensure session-history endpoint filters events | |
| old_history = ( | |
| ' from backend.session_logger import load_session_history\n' | |
| ' return load_session_history(session_id)' | |
| ) | |
| new_history = ( | |
| ' from backend.session_logger import load_session_history\n' | |
| ' rows = load_session_history(session_id)\n' | |
| ' valid_roles = {"user", "assistant", "tool"}\n' | |
| ' return [r for r in rows if r.get("role") in valid_roles]' | |
| ) | |
| if old_history in content: | |
| content, ok = _safe_replace(content, old_history, new_history) | |
| if ok: | |
| print(" + session-history endpoint filters event rows") | |
| changed = True | |
| # Add catchup-done endpoint | |
| if "/session/{session_id}/catchup-done" not in content: | |
| catchup_endpoint = ''' | |
| @router.post("/session/{session_id}/catchup-done") | |
| async def mark_catchup_done( | |
| session_id: str, user: dict = Depends(get_current_user) | |
| ) -> dict: | |
| """Mark that a session was successfully catch-up restored. | |
| The frontend calls this after a successful catch-up so the backend | |
| can track which sessions were restored from summary vs. fresh. | |
| """ | |
| await _check_session_access(session_id, user, preload_sandbox=False) | |
| try: | |
| await session_manager.record_catchup_done(session_id) | |
| except Exception as e: | |
| logger.warning("record_catchup_done failed for %s: %s", session_id, e) | |
| return {"status": "ok", "session_id": session_id} | |
| ''' | |
| content = content.rstrip() + "\n" + catchup_endpoint | |
| print(" + Added /session/{id}/catchup-done endpoint") | |
| changed = True | |
| if changed: | |
| try: | |
| ast.parse(content) | |
| _write(BACKEND_ROUTES_AGENT, content) | |
| print("OK: routes/agent.py patched") | |
| except SyntaxError as e: | |
| print(f"FAIL: routes/agent.py syntax: {e}") | |
| else: | |
| print("SKIP: routes/agent.py not found") | |
| # βββ Frontend patches ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # These files are only present in the frontend-builder stage. | |
| FRONTEND_DIR = "/source/frontend/src" | |
| FE_MARKER = "// === PATCH: catch-up-v2 ===" | |
| URL_MARKER = "// === PATCH: shareable-url-v2 ===" | |
| def patch_frontend_file(relpath, patch_fn, label): | |
| path = os.path.join(FRONTEND_DIR, relpath) | |
| if not os.path.exists(path): | |
| print(f"SKIP: {path} not found") | |
| return | |
| with open(path, "r", encoding="utf-8") as f: | |
| content = f.read() | |
| content, changed = patch_fn(content, path) | |
| if not changed: | |
| print(f"SKIP: {path} β no changes needed ({label})") | |
| return | |
| with open(path, "w", encoding="utf-8") as f: | |
| f.write(content) | |
| print(f"OK: {path} patched ({label})") | |
| # βββ useAgentChat.ts βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching useAgentChat.ts ===") | |
| def patch_useAgentChat(content, _path): | |
| changed = False | |
| if FE_MARKER in content: | |
| print(" SKIP: already patched") | |
| return content, False | |
| # 1. Add catch-up guard + URL sync helpers after chatActionsRef | |
| ref_def = " const chatActionsRef = useRef<" | |
| if ref_def in content: | |
| idx = content.find(ref_def) | |
| end_idx = content.find("});", idx) | |
| if end_idx > 0: | |
| insert_after = content.find("\n", end_idx) + 1 | |
| helper = ''' | |
| // === PATCH: catch-up-v2 (frontend) === | |
| // After catch-up (renameSession), the backend only has the summary message | |
| // but the frontend has the full conversation in localStorage. These guards | |
| // prevent the backend's sparse data from overwriting the frontend's rich | |
| // conversation. The ml-intern-catchup-done flag is set after renameSession | |
| // and cleared after the first message is sent. | |
| const _catchupDone = (() => { | |
| try { | |
| return localStorage.getItem(`ml-intern-catchup-done:${sessionId}`) === 'true'; | |
| } catch { | |
| return false; | |
| } | |
| })(); | |
| const _clearCatchupDone = useCallback(() => { | |
| try { | |
| localStorage.removeItem(`ml-intern-catchup-done:${sessionId}`); | |
| } catch {} | |
| }, [sessionId]); | |
| ''' | |
| content = content[:insert_after] + helper + content[insert_after:] | |
| print(" + Added catchup guard helper") | |
| changed = True | |
| # 2. Guard onRecoverMessages | |
| old_recover = ( | |
| " const setMsgs = chatActionsRef.current.setMessages;\n" | |
| " if (setMsgs && uiMsgs.length >= currentMessageCount) {\n" | |
| " setMsgs(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }" | |
| ) | |
| new_recover = ( | |
| " const setMsgs = chatActionsRef.current.setMessages;\n" | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " const _feCount = chatActionsRef.current.messages.length;\n" | |
| " if (setMsgs && (!_catchupDone || uiMsgs.length >= _feCount) && uiMsgs.length >= currentMessageCount) {\n" | |
| " setMsgs(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }" | |
| ) | |
| content, ok = _safe_replace(content, old_recover, new_recover) | |
| if ok: | |
| print(" + Guarded onRecoverMessages") | |
| changed = True | |
| # 3. Guard refreshMessages | |
| old_refresh = ( | |
| " const setMsgs = chatActionsRef.current.setMessages;\n" | |
| " if (setMsgs && uiMsgs.length > 0) {\n" | |
| " setMsgs(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }\n" | |
| " return true;" | |
| ) | |
| new_refresh = ( | |
| " const setMsgs = chatActionsRef.current.setMessages;\n" | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " const _feCountR = chatActionsRef.current.messages.length;\n" | |
| " if (setMsgs && (!_catchupDone || uiMsgs.length > _feCountR) && uiMsgs.length > 0) {\n" | |
| " setMsgs(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }\n" | |
| " return true;" | |
| ) | |
| content, ok = _safe_replace(content, old_refresh, new_refresh) | |
| if ok: | |
| print(" + Guarded refreshMessages") | |
| changed = True | |
| # 4. Guard all chat.setMessages(uiMsgs) in hydration paths (10-space indent) | |
| old_hydrate = ( | |
| " if (uiMsgs.length > 0) {\n" | |
| " chat.setMessages(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }" | |
| ) | |
| new_hydrate = ( | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " const _feCountH = chatActionsRef.current.messages.length;\n" | |
| " if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _feCountH)) {\n" | |
| " chat.setMessages(uiMsgs);\n" | |
| " saveMessages(sessionId, uiMsgs);\n" | |
| " }" | |
| ) | |
| count = 0 | |
| while old_hydrate in content: | |
| content, ok = _safe_replace(content, old_hydrate, new_hydrate) | |
| if ok: | |
| count += 1 | |
| changed = True | |
| else: | |
| break | |
| if count > 0: | |
| print(f" + Guarded {count} hydration setMessages paths") | |
| # 5. Wrap sendMessage to clear catchup flag + update URL | |
| old_send = " sendMessage: chat.sendMessage,\n stop,\n status: chat.status,\n" | |
| if old_send in content: | |
| new_send = ( | |
| " sendMessage,\n" | |
| " stop,\n" | |
| " status: chat.status,\n" | |
| ) | |
| # Actually, we need to insert the wrapper BEFORE the return and change sendMessage in it | |
| # Find the return block | |
| return_block = " return {\n messages: chat.messages,\n sendMessage: chat.sendMessage,\n stop,\n status: chat.status,\n undoLastTurn,\n" | |
| if return_block in content: | |
| wrapper_block = ( | |
| " const _sendMessageWrapped = useCallback((msg) => {\n" | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " _clearCatchupDone();\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const params = new URLSearchParams(window.location.search);\n" | |
| " if (params.get('chat') !== sessionId) {\n" | |
| " params.set('chat', sessionId);\n" | |
| " const qs = params.toString();\n" | |
| " window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname);\n" | |
| " }\n" | |
| " return chat.sendMessage(msg);\n" | |
| " }, [chat, _clearCatchupDone, sessionId]);\n" | |
| "\n" | |
| " return {\n messages: chat.messages,\n sendMessage: _sendMessageWrapped,\n stop,\n status: chat.status,\n undoLastTurn,\n" | |
| ) | |
| content, ok = _safe_replace(content, return_block, wrapper_block) | |
| if ok: | |
| print(" + Wrapped sendMessage (clear flag + URL sync)") | |
| changed = True | |
| else: | |
| # Try the simpler pattern | |
| simple_return = " return {\n messages: chat.messages,\n sendMessage: chat.sendMessage,\n" | |
| if simple_return in content: | |
| wrapper_block = ( | |
| " const _sendMessageWrapped = useCallback((msg) => {\n" | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " _clearCatchupDone();\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const params = new URLSearchParams(window.location.search);\n" | |
| " if (params.get('chat') !== sessionId) {\n" | |
| " params.set('chat', sessionId);\n" | |
| " const qs = params.toString();\n" | |
| " window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname);\n" | |
| " }\n" | |
| " return chat.sendMessage(msg);\n" | |
| " }, [chat, _clearCatchupDone, sessionId]);\n" | |
| "\n" | |
| " return {\n messages: chat.messages,\n sendMessage: _sendMessageWrapped,\n" | |
| ) | |
| content, ok = _safe_replace(content, simple_return, wrapper_block) | |
| if ok: | |
| print(" + Wrapped sendMessage (simple pattern)") | |
| changed = True | |
| if changed and "import { logger } from '@/utils/logger';" in content: | |
| content = content.replace( | |
| "import { logger } from '@/utils/logger';", | |
| "import { logger } from '@/utils/logger';\n" + FE_MARKER + " applied", | |
| 1 | |
| ) | |
| return content, changed | |
| # βββ ExpiredBanner.tsx ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching ExpiredBanner.tsx ===") | |
| def patch_expired_banner(content, _path): | |
| changed = False | |
| if FE_MARKER in content: | |
| print(" SKIP: already patched") | |
| return content, False | |
| old_success = ( | |
| " useAgentStore.getState().clearSessionState(sessionId);\n" | |
| " renameSession(sessionId, newId);\n" | |
| " if (data.model) updateSessionModel(newId, data.model);" | |
| ) | |
| new_success = ( | |
| " useAgentStore.getState().clearSessionState(sessionId);\n" | |
| " renameSession(sessionId, newId);\n" | |
| " if (data.model) updateSessionModel(newId, data.model);\n" | |
| " // === PATCH: catch-up-v2 (frontend) ===\n" | |
| " // Mark session as catch-up done so hydration paths don't\n" | |
| " // overwrite the full localStorage conversation with the\n" | |
| " // backend's sparse summary-only data.\n" | |
| " try {\n" | |
| " localStorage.setItem(`ml-intern-catchup-done:${newId}`, 'true');\n" | |
| " } catch {}\n" | |
| " // Also notify the backend\n" | |
| " try { await apiFetch(`/api/session/${newId}/catchup-done`, { method: 'POST' }); } catch {}" | |
| ) | |
| content, ok = _safe_replace(content, old_success, new_success) | |
| if ok: | |
| print(" + Set catchup-done flag after successful catch-up") | |
| changed = True | |
| else: | |
| print(" INFO: catch-up success pattern not found") | |
| if changed and "import { logger } from '@/utils/logger';" in content: | |
| content = content.replace( | |
| "import { logger } from '@/utils/logger';", | |
| "import { logger } from '@/utils/logger';\n" + FE_MARKER + " applied", | |
| 1 | |
| ) | |
| return content, changed | |
| # βββ AppLayout.tsx βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching AppLayout.tsx ===") | |
| def patch_app_layout(content, _path): | |
| changed = False | |
| if URL_MARKER in content: | |
| print(" SKIP: already patched") | |
| return content, False | |
| # 1. Add ShareIcon import | |
| if "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';" in content: | |
| content = content.replace( | |
| "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';", | |
| "import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';\n" | |
| "import ShareIcon from '@mui/icons-material/Share';\n" | |
| "// === PATCH: shareable-url-v2 ===", | |
| 1 | |
| ) | |
| print(" + Added ShareIcon import") | |
| changed = True | |
| # 2. Add URL-based session selection on mount | |
| old_health = " // -- LLM health check on mount -----------------------------------------" | |
| new_code = """ // === PATCH: shareable-url-v2 === | |
| // On mount, read ?chat={sessionId} from URL and auto-select that session | |
| useEffect(() => { | |
| const params = new URLSearchParams(window.location.search); | |
| const chatParam = params.get('chat'); | |
| if (chatParam) { | |
| const { sessions: currentSessions, switchSession } = useSessionStore.getState(); | |
| if (currentSessions.find((s) => s.id === chatParam)) { | |
| switchSession(chatParam); | |
| } | |
| } | |
| }, []); // eslint-disable-line react-hooks/exhaustive-deps | |
| // Update URL whenever the active session changes | |
| useEffect(() => { | |
| if (activeSessionId) { | |
| const params = new URLSearchParams(window.location.search); | |
| if (params.get('chat') !== activeSessionId) { | |
| params.set('chat', activeSessionId); | |
| const qs = params.toString(); | |
| window.history.replaceState(null, '', qs ? `${window.location.pathname}?${qs}` : window.location.pathname); | |
| } | |
| } | |
| }, [activeSessionId]); | |
| // -- LLM health check on mount -----------------------------------------""" | |
| content, ok = _safe_replace(content, old_health, new_code) | |
| if ok: | |
| print(" + URL-based session selection + URL sync on active change") | |
| changed = True | |
| # 3. Add Share button to the header | |
| old_avatar = ( | |
| " {user?.picture ? (\n" | |
| " <Avatar\n" | |
| " src={user.picture}\n" | |
| " alt={user.username || 'User'}\n" | |
| " sx={{ width: 28, height: 28, ml: 0.5 }}\n" | |
| " />\n" | |
| " ) : user?.username ? (" | |
| ) | |
| new_avatar = ( | |
| " {activeSessionId && (\n" | |
| " <Tooltip title=\"Share this chat\">\n" | |
| " <IconButton\n" | |
| " size=\"small\"\n" | |
| " onClick={async () => {\n" | |
| " const url = window.location.href;\n" | |
| " if (navigator.clipboard?.writeText) {\n" | |
| " await navigator.clipboard.writeText(url);\n" | |
| " } else {\n" | |
| " prompt('Copy this link:', url);\n" | |
| " }\n" | |
| " }}\n" | |
| " sx={{ color: 'text.secondary', ml: 0.5 }}\n" | |
| " >\n" | |
| " <ShareIcon fontSize=\"small\" />\n" | |
| " </IconButton>\n" | |
| " </Tooltip>\n" | |
| " )}\n" | |
| " {user?.picture ? (\n" | |
| " <Avatar\n" | |
| " src={user.picture}\n" | |
| " alt={user.username || 'User'}\n" | |
| " sx={{ width: 28, height: 28, ml: 0.5 }}\n" | |
| " />\n" | |
| " ) : user?.username ? (" | |
| ) | |
| content, ok = _safe_replace(content, old_avatar, new_avatar) | |
| if ok: | |
| print(" + Added Share button to header") | |
| changed = True | |
| return content, changed | |
| # βββ SessionSidebar.tsx ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching SessionSidebar.tsx ===") | |
| def patch_session_sidebar(content, _path): | |
| changed = False | |
| if URL_MARKER in content: | |
| print(" SKIP: already patched") | |
| return content, False | |
| # 1. URL update after handleNewSession | |
| old_create = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();\n" | |
| " onClose?.();" | |
| ) | |
| new_create = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const _params = new URLSearchParams(window.location.search);\n" | |
| " _params.set('chat', data.session_id);\n" | |
| " const _qs = _params.toString();\n" | |
| " window.history.replaceState(null, '', _qs ? `${window.location.pathname}?${_qs}` : window.location.pathname);\n" | |
| " onClose?.();" | |
| ) | |
| content, ok = _safe_replace(content, old_create, new_create) | |
| if ok: | |
| print(" + URL update on new session") | |
| changed = True | |
| # 2. URL update on second new session (delete-last case) | |
| old_create2 = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();" | |
| ) | |
| new_create2 = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const _params2 = new URLSearchParams(window.location.search);\n" | |
| " _params2.set('chat', data.session_id);\n" | |
| " const _qs2 = _params2.toString();\n" | |
| " window.history.replaceState(null, '', _qs2 ? `${window.location.pathname}?${_qs2}` : window.location.pathname);" | |
| ) | |
| content, ok = _safe_replace(content, old_create2, new_create2) | |
| if ok: | |
| print(" + URL update on new session (delete-last case)") | |
| changed = True | |
| # 3. URL update on session select | |
| old_select = ( | |
| " const shouldActivateSession = sessionId !== activeSessionId;\n" | |
| " switchSession(sessionId);" | |
| ) | |
| new_select = ( | |
| " const shouldActivateSession = sessionId !== activeSessionId;\n" | |
| " switchSession(sessionId);\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const _p = new URLSearchParams(window.location.search);\n" | |
| " if (_p.get('chat') !== sessionId) {\n" | |
| " _p.set('chat', sessionId);\n" | |
| " const _q = _p.toString();\n" | |
| " window.history.replaceState(null, '', _q ? `${window.location.pathname}?${_q}` : window.location.pathname);\n" | |
| " }" | |
| ) | |
| content, ok = _safe_replace(content, old_select, new_select) | |
| if ok: | |
| print(" + URL update on session select") | |
| changed = True | |
| return content, changed | |
| # βββ WelcomeScreen.tsx βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("\n=== Patching WelcomeScreen.tsx ===") | |
| WS_PATH = os.path.join(FRONTEND_DIR, "components/WelcomeScreen/WelcomeScreen.tsx") | |
| if os.path.exists(WS_PATH): | |
| with open(WS_PATH, "r", encoding="utf-8") as f: | |
| ws_content = f.read() | |
| if URL_MARKER not in ws_content: | |
| old_ws = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();\n" | |
| " } catch" | |
| ) | |
| new_ws = ( | |
| " createSession(data.session_id, data.model);\n" | |
| " void useUsageStore.getState().fetchUsage(data.session_id);\n" | |
| " setPlan([]);\n" | |
| " clearPanel();\n" | |
| " // === PATCH: shareable-url-v2 ===\n" | |
| " const _params = new URLSearchParams(window.location.search);\n" | |
| " _params.set('chat', data.session_id);\n" | |
| " const _qs = _params.toString();\n" | |
| " window.history.replaceState(null, '', _qs ? `${window.location.pathname}?${_qs}` : window.location.pathname);\n" | |
| " } catch" | |
| ) | |
| if old_ws in ws_content: | |
| ws_content = ws_content.replace(old_ws, new_ws, 1) | |
| with open(WS_PATH, "w", encoding="utf-8") as f: | |
| f.write(ws_content) | |
| print(" + URL update on welcome screen session creation") | |
| else: | |
| print(" INFO: welcome screen pattern not found") | |
| else: | |
| print(" SKIP: already patched") | |
| else: | |
| print("SKIP: WelcomeScreen.tsx not found") | |
| # βββ Execute frontend patches βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| patch_frontend_file("hooks/useAgentChat.ts", patch_useAgentChat, "catch-up guard + URL sync") | |
| patch_frontend_file("components/Chat/ExpiredBanner.tsx", patch_expired_banner, "catchup-done flag") | |
| patch_frontend_file("components/Layout/AppLayout.tsx", patch_app_layout, "URL session selection + share button") | |
| patch_frontend_file("components/SessionSidebar/SessionSidebar.tsx", patch_session_sidebar, "URL update on session actions") | |
| print("\n=== All v2 patches applied ===") | |