-
Notifications
You must be signed in to change notification settings - Fork 26
Improve shared chat scraping errors and coverage #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TsukinowaRin
wants to merge
2
commits into
XortexAI:main
Choose a base branch
from
TsukinowaRin:codex/context-share-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| def scrape_failure_message(result: dict[str, Any]) -> str: | ||
| provider = result.get("provider") or "unknown" | ||
|
|
||
| if provider in {"chatgpt", "claude", "gemini"}: | ||
| display_name = { | ||
| "chatgpt": "ChatGPT", | ||
| "claude": "Claude", | ||
| "gemini": "Gemini", | ||
| }[provider] | ||
| return ( | ||
| f"Could not extract messages from this {display_name} share link. " | ||
| "Make sure the link is public, still exists, and has not expired." | ||
| ) | ||
|
|
||
| return ( | ||
| "Failed to extract messages from the provided link. " | ||
| "Supported public share links are ChatGPT, Claude, and Gemini." | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import json | ||
| import os | ||
| from types import SimpleNamespace | ||
|
|
||
| os.environ.setdefault("PINECONE_API_KEY", "test-pinecone-key") | ||
| os.environ.setdefault("NEO4J_PASSWORD", "test-neo4j-password") | ||
| os.environ.setdefault("GEMINI_API_KEY", "test-gemini-key") | ||
|
|
||
| from src.api.chat_share import scrape_failure_message | ||
| from src.api.routes.memory import ( | ||
| _detect_chat_provider, | ||
| _extract_chat_pairs, | ||
| scrape_chat_link, | ||
| ) | ||
| from src.api.schemas import MessagePair, ScrapeRequest | ||
|
|
||
|
|
||
| def test_detects_supported_chat_share_providers(): | ||
| assert _detect_chat_provider("https://chatgpt.com/share/abc") == "chatgpt" | ||
| assert _detect_chat_provider("https://chat.openai.com/share/abc") == "chatgpt" | ||
| assert _detect_chat_provider("https://claude.ai/share/abc") == "claude" | ||
| assert _detect_chat_provider("https://gemini.google.com/share/abc") == "gemini" | ||
| assert _detect_chat_provider("https://g.co/gemini/share/abc") == "gemini" | ||
|
|
||
|
|
||
| def test_extracts_chatgpt_dom_pairs(): | ||
| html = """ | ||
| <div data-message-author-role="user">What is XMem?</div> | ||
| <div data-message-author-role="assistant">A long-term memory layer.</div> | ||
| """ | ||
|
|
||
| provider, method, pairs = _extract_chat_pairs("https://chatgpt.com/share/abc", html) | ||
|
|
||
| assert provider == "chatgpt" | ||
| assert method == "dom" | ||
| assert pairs == [ | ||
| MessagePair( | ||
| user_query="What is XMem?", | ||
| agent_response="A long-term memory layer.", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_extracts_claude_preloaded_state_pairs(): | ||
| state = { | ||
| "chat": { | ||
| "messages": [ | ||
| {"sender": "human", "text": "Summarize this repo."}, | ||
| {"sender": "assistant", "text": "It stores memories for agents."}, | ||
| ] | ||
| } | ||
| } | ||
| html = ( | ||
| "<script>window.__PRELOADED_STATE__ = " | ||
| f"{json.dumps(state)};" | ||
| "</script>" | ||
| ) | ||
|
|
||
| provider, method, pairs = _extract_chat_pairs("https://claude.ai/share/abc", html) | ||
|
|
||
| assert provider == "claude" | ||
| assert method == "structured" | ||
| assert pairs == [ | ||
| MessagePair( | ||
| user_query="Summarize this repo.", | ||
| agent_response="It stores memories for agents.", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_extracts_claude_current_public_share_dom_pairs(): | ||
| html = """ | ||
| <div data-testid="user-message"> | ||
| <p class="whitespace-pre-wrap break-words">test test</p> | ||
| </div> | ||
| <div class="font-claude-response relative leading"> | ||
| <div> | ||
| <div class="standard-markdown"> | ||
| <p class="font-claude-response-body">Hey! I'm here and working.</p> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| """ | ||
|
|
||
| provider, method, pairs = _extract_chat_pairs("https://claude.ai/share/abc", html) | ||
|
|
||
| assert provider == "claude" | ||
| assert method == "dom" | ||
| assert pairs == [ | ||
| MessagePair( | ||
| user_query="test test", | ||
| agent_response="Hey! I'm here and working.", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_extracts_gemini_dom_pairs(): | ||
| html = """ | ||
| <message-content role="user">Compare memory tools.</message-content> | ||
| <message-content role="model">XMem focuses on persistent agent memory.</message-content> | ||
| """ | ||
|
|
||
| provider, method, pairs = _extract_chat_pairs( | ||
| "https://gemini.google.com/share/abc", | ||
| html, | ||
| ) | ||
|
|
||
| assert provider == "gemini" | ||
| assert method == "dom" | ||
| assert pairs == [ | ||
| MessagePair( | ||
| user_query="Compare memory tools.", | ||
| agent_response="XMem focuses on persistent agent memory.", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_extracts_gemini_current_public_share_dom_pairs(): | ||
| html = """ | ||
| <div class="query-text"> | ||
| <span class="screen-reader-user-query-label"> You said </span> | ||
| <p class="query-text-line"> Test test </p> | ||
| </div> | ||
| <structured-content-container class="message-content"> | ||
| <message-content> | ||
| <div class="markdown"> | ||
| <p>Loud and clear! I'm here and ready to roll.</p> | ||
| </div> | ||
| </message-content> | ||
| </structured-content-container> | ||
| """ | ||
|
|
||
| provider, method, pairs = _extract_chat_pairs( | ||
| "https://gemini.google.com/share/abc", | ||
| html, | ||
| ) | ||
|
|
||
| assert provider == "gemini" | ||
| assert method == "dom" | ||
| assert pairs == [ | ||
| MessagePair( | ||
| user_query="Test test", | ||
| agent_response="Loud and clear! I'm here and ready to roll.", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_scrape_failure_message_names_private_or_missing_provider_links(): | ||
| message = scrape_failure_message({"provider": "claude"}) | ||
|
|
||
| assert "Claude share link" in message | ||
| assert "public" in message | ||
| assert "expired" in message | ||
|
|
||
|
|
||
| def test_scrape_failure_message_lists_supported_unknown_links(): | ||
| message = scrape_failure_message({"provider": "unknown"}) | ||
|
|
||
| assert "Supported public share links" in message | ||
| assert "ChatGPT" in message | ||
| assert "Claude" in message | ||
| assert "Gemini" in message | ||
|
|
||
|
|
||
| async def test_scrape_route_failure_uses_elapsed_ms(monkeypatch): | ||
| async def fake_scrape(url: str): | ||
| return {"provider": "gemini", "pairs": []} | ||
|
|
||
| ticks = iter([10.0, 10.12345]) | ||
|
|
||
| monkeypatch.setattr("src.api.routes.memory._scrape_chat_share", fake_scrape) | ||
| monkeypatch.setattr("src.api.routes.memory.time.perf_counter", lambda: next(ticks)) | ||
|
|
||
| response = await scrape_chat_link( | ||
| ScrapeRequest(url="https://gemini.google.com/share/abc"), | ||
| SimpleNamespace(state=SimpleNamespace(request_id="req-test")), | ||
| ) | ||
| body = json.loads(response.body) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert body["elapsed_ms"] == 123.45 | ||
| assert "Gemini share link" in body["error"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error response for missing message pairs is missing the
elapsed_mstiming information, which results in a default value of0.0. This is inconsistent with the success response and the implementation inserver.py. Calculating the elapsed time before the check ensures that the user receives accurate timing even when extraction fails.