-
Notifications
You must be signed in to change notification settings - Fork 15
feat: search.open page reading via Jina Reader + top-10 search default #112
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
faresobeid
wants to merge
2
commits into
main
Choose a base branch
from
feat/batched-search-open-webpage
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 1 commit
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ dependencies = [ | |
| "scipy", | ||
| "beautifulsoup4", | ||
| "lxml", | ||
| "pdfminer.six", | ||
| "pydantic", | ||
| ] | ||
|
|
||
|
|
||
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,92 @@ | ||
| """Built-in ``open_webpage`` skill — fetch a URL and return its text content. | ||
|
|
||
| Enabled via ``RLM_SKILLS``; pre-imported into the IPython kernel so the agent calls | ||
| ``await open_webpage(url="...")``. When ``JINA_API_KEY`` is set, pages are fetched | ||
| through the Jina Reader API (markdown output, handles JS-rendered pages and PDFs), | ||
| falling back to a direct fetch parsed locally on failure. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import io | ||
| import logging | ||
| import os | ||
| import re | ||
|
|
||
| import httpx | ||
|
|
||
| JINA_READER_URL = "https://r.jina.ai" | ||
|
|
||
|
|
||
| def _pdf_to_text(pdf_bytes: bytes) -> str: | ||
| from pdfminer.high_level import extract_text | ||
|
|
||
| logging.getLogger("pdfminer").setLevel(logging.ERROR) | ||
| return extract_text(io.BytesIO(pdf_bytes)) or "" | ||
|
|
||
|
|
||
| def _html_to_text(html_text: str) -> str: | ||
| from bs4 import BeautifulSoup | ||
|
|
||
| soup = BeautifulSoup(html_text, "lxml") | ||
| for tag in soup(["script", "style", "noscript", "svg"]): | ||
| tag.decompose() | ||
| text = soup.get_text("\n") | ||
| text = re.sub(r"[ \t]{2,}", " ", text) | ||
| text = re.sub(r"\n{3,}", "\n\n", text) | ||
| return text | ||
|
|
||
|
|
||
| def _fetch_jina(url: str, api_key: str, timeout: float) -> str: | ||
| response = httpx.get( | ||
| f"{JINA_READER_URL}/{url}", | ||
| timeout=timeout, | ||
| headers={"Authorization": f"Bearer {api_key}", "X-Timeout": str(int(timeout))}, | ||
| ) | ||
| response.raise_for_status() | ||
| return response.text.strip() | ||
|
|
||
|
|
||
| def _fetch_direct(url: str, timeout: float) -> str: | ||
| response = httpx.get( | ||
| url, | ||
| timeout=timeout, | ||
| follow_redirects=True, | ||
| headers={"User-Agent": "Mozilla/5.0"}, | ||
| ) | ||
| response.raise_for_status() | ||
| content_type = (response.headers.get("content-type") or "").lower() | ||
| body = response.content | ||
| if body.startswith(b"%PDF-") or "application/pdf" in content_type: | ||
| return _pdf_to_text(body).strip() | ||
| if "text/html" in content_type or "<html" in response.text[:2048].lower(): | ||
| return _html_to_text(response.text).strip() | ||
| return response.text.strip() | ||
|
|
||
|
|
||
| def open_webpage(url: str, timeout: float = 30) -> str: | ||
| """Fetch a URL and return its text content, via Jina Reader when available.""" | ||
| api_key = os.environ.get("JINA_API_KEY", "") | ||
| if api_key: | ||
| try: | ||
| return _fetch_jina(url, api_key, timeout) | ||
| except httpx.HTTPError: | ||
| pass # fall back to direct fetch | ||
| return _fetch_direct(url, timeout) | ||
|
|
||
|
|
||
| async def run(url: str, *, timeout: float = 30) -> str: | ||
| """Fetch a URL and return its text content. Handles HTML and PDF. | ||
|
|
||
| Uses the Jina Reader API (markdown; handles JS-rendered pages) when | ||
| ``JINA_API_KEY`` is set, falling back to a direct fetch parsed locally. | ||
|
|
||
| Args: | ||
| url: The URL to fetch. | ||
| timeout: Request timeout in seconds. | ||
|
|
||
| Returns: | ||
| The page text. | ||
| """ | ||
| return await asyncio.to_thread(open_webpage, url, timeout) | ||
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
Oops, something went wrong.
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.
Jina request drops URL query
Medium Severity
When
JINA_API_KEYis set,_fetch_jinabuilds the Reader request ashttps://r.jina.ai/plus the raw target URL. If the target includes a?query string, HTTP parsing treats everything after the first?as query parameters onr.jina.ai, not as part of the embedded page URL, so Jina may fetch the wrong resource or apply unintended Reader options while still returning 200.Reviewed by Cursor Bugbot for commit bbb518e. Configure here.