Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async def scrape_chat_link(req: ScrapeRequest):
{
"status": "error",
"data": None,
"error": "Failed to extract messages from the provided link.",
"error": _chat_share_error_message(result),
"elapsed_ms": elapsed,
},
status_code=400,
Expand Down Expand Up @@ -532,17 +532,34 @@ def _build_memory_domain(judge: Any, weaver: Any) -> dict[str, Any] | None:
}


def _detect_chat_provider(url: str) -> str:
lowered = url.lower()
if "chatgpt.com" in lowered or "chat.openai.com" in lowered or "openai.com" in lowered:
return "chatgpt"
if "claude.ai" in lowered:
return "claude"
if "gemini.google.com" in lowered or "g.co/gemini" in lowered:
return "gemini"
def _detect_chat_provider(*urls: str) -> str:
for url in urls:
lowered = (url or "").lower()
if not lowered:
continue
if "chatgpt.com" in lowered or "chat.openai.com" in lowered or "openai.com" in lowered:
return "chatgpt"
if "claude.ai" in lowered or "claude.com" in lowered:
return "claude"
if "gemini.google.com" in lowered or "g.co/gemini" in lowered:
return "gemini"
return "unknown"


def _chat_share_error_message(result: dict[str, Any]) -> str:
provider = result.get("provider") or "unknown"
if provider == "unknown":
return (
"Failed to extract messages from the provided link. "
"Please provide a public ChatGPT, Claude, or Gemini share link."
)

return (
f"Failed to extract messages from the provided {provider} share link. "
"Please confirm the link is public, exists, and is not redirecting to a login or deleted-chat page."
)
Comment on lines +557 to +560
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message uses the raw provider ID (e.g., 'chatgpt'), which results in lowercase names in the UI. Mapping these to their proper display names (e.g., 'ChatGPT', 'Claude', 'Gemini') would provide a more professional and user-friendly error message.

Suggested change
return (
f"Failed to extract messages from the provided {provider} share link. "
"Please confirm the link is public, exists, and is not redirecting to a login or deleted-chat page."
)
names = {"chatgpt": "ChatGPT", "claude": "Claude", "gemini": "Gemini"}
name = names.get(provider, provider.capitalize())
return (
f"Failed to extract messages from the provided {name} share link. "
"Please confirm the link is public, exists, and is not redirecting to a login or deleted-chat page."
)



async def _render_chat_share(url: str) -> tuple[str, str]:
return await asyncio.to_thread(_render_chat_share_sync, url)

Expand Down Expand Up @@ -595,7 +612,7 @@ def _block_heavy_assets(route):
except Exception as exc:
print(f"[scrape] navigation warning: {exc}", flush=True)

provider = _detect_chat_provider(page.url or url)
provider = _detect_chat_provider(page.url, url)
selector = {
"chatgpt": "div[data-message-author-role]",
"claude": "script",
Expand All @@ -617,8 +634,12 @@ def _block_heavy_assets(route):
return html, final_url


def _extract_chat_pairs(url: str, html: str) -> tuple[str, str, list[dict[str, str]]]:
provider = _detect_chat_provider(url)
def _extract_chat_pairs(
url: str,
html: str,
source_url: str = "",
) -> tuple[str, str, list[dict[str, str]]]:
provider = _detect_chat_provider(url, source_url)
soup = BeautifulSoup(html, "html.parser")
pairs: list[dict[str, str]] = []
extraction_method = "none"
Expand Down Expand Up @@ -859,7 +880,7 @@ def _parse_transcript_text(text: str) -> tuple[str, list[dict[str, str]]]:

async def _scrape_chat_share(url: str) -> dict[str, Any]:
html, final_url = await _render_chat_share(url)
provider, extraction_method, pairs = _extract_chat_pairs(final_url or url, html)
provider, extraction_method, pairs = _extract_chat_pairs(final_url or url, html, url)

return {
"provider": provider,
Expand Down
48 changes: 35 additions & 13 deletions src/api/routes/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,34 @@ def _error(request: Request, detail: str, code: int, elapsed_ms: float = 0) -> J
return JSONResponse(content=body.model_dump(), status_code=code)


def _detect_chat_provider(url: str) -> str:
lowered = url.lower()
if "chatgpt.com" in lowered or "chat.openai.com" in lowered or "openai.com" in lowered:
return "chatgpt"
if "claude.ai" in lowered:
return "claude"
if "gemini.google.com" in lowered or "g.co/gemini" in lowered:
return "gemini"
def _detect_chat_provider(*urls: str) -> str:
for url in urls:
lowered = (url or "").lower()
if not lowered:
continue
if "chatgpt.com" in lowered or "chat.openai.com" in lowered or "openai.com" in lowered:
return "chatgpt"
if "claude.ai" in lowered or "claude.com" in lowered:
return "claude"
if "gemini.google.com" in lowered or "g.co/gemini" in lowered:
return "gemini"
return "unknown"


def _chat_share_error_message(result: Dict[str, Any]) -> str:
provider = result.get("provider") or "unknown"
if provider == "unknown":
return (
"Failed to extract messages from the provided link. "
"Please provide a public ChatGPT, Claude, or Gemini share link."
)

return (
f"Failed to extract messages from the provided {provider} share link. "
"Please confirm the link is public, exists, and is not redirecting to a login or deleted-chat page."
)


async def _render_chat_share(url: str) -> tuple[str, str]:
return await asyncio.to_thread(_render_chat_share_sync, url)

Expand Down Expand Up @@ -206,7 +223,7 @@ def _block_heavy_assets(route):
except Exception as exc:
logger.warning("Timeout or error during navigation: %s", exc)

provider = _detect_chat_provider(page.url or url)
provider = _detect_chat_provider(page.url, url)
selector = {
"chatgpt": "div[data-message-author-role]",
"claude": "script",
Expand All @@ -230,8 +247,12 @@ def _block_heavy_assets(route):
return html, final_url


def _extract_chat_pairs(url: str, html: str) -> tuple[str, str, List[MessagePair]]:
provider = _detect_chat_provider(url)
def _extract_chat_pairs(
url: str,
html: str,
source_url: str = "",
) -> tuple[str, str, List[MessagePair]]:
provider = _detect_chat_provider(url, source_url)
soup = BeautifulSoup(html, "html.parser")
pairs: List[MessagePair] = []
extraction_method = "none"
Expand Down Expand Up @@ -512,7 +533,7 @@ def _parse_transcript_text(text: str) -> tuple[str, List[MessagePair]]:

async def _scrape_chat_share(url: str) -> Dict[str, Any]:
html, final_url = await _render_chat_share(url)
provider, extraction_method, pairs = _extract_chat_pairs(final_url or url, html)
provider, extraction_method, pairs = _extract_chat_pairs(final_url or url, html, url)

return {
"provider": provider,
Expand Down Expand Up @@ -757,7 +778,8 @@ async def scrape_chat_link(req: ScrapeRequest, request: Request):
pairs = result["pairs"]

if not pairs:
return _error(request, "Failed to extract messages from the provided link.", 400)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _error(request, _chat_share_error_message(result), 400, elapsed)

data = ScrapeResponse(pairs=pairs)
elapsed = round((time.perf_counter() - start) * 1000, 2)
Expand Down
Loading