Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: read the code behind a GitHub file link in web search (#13754)

Nilayeff9bb4a8198
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index fd73cbac0f8..4df7afab1f7 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -16195,6 +16195,21 @@ def _github_repo_readme_api_url(url: str) -> str | None:
return f"https://api.github.com/repos/{owner}/{repo}/readme"
+def _github_blob_raw_url(url: str) -> str | None:
+ from urllib.parse import urlsplit
+
+ parsed = urlsplit(url)
+ if (parsed.hostname or "").lower() not in ("github.com", "www.github.com"):
+ return None
+ parts = [p for p in parsed.path.split("/") if p]
+ if len(parts) < 5 or parts[2] != "blob":
+ return None
+ owner, repo = parts[:2]
+ if not (_GITHUB_NAME_RE.match(owner) and _GITHUB_NAME_RE.match(repo)):
+ return None
+ return f"https://raw.githubusercontent.com/{owner}/{repo}/" + "/".join(parts[3:])
+
+
# A single fetch can chain several steps (README API attempt, HTML fallback, up to five redirect hops, each reading a
# body). A per-operation socket timeout bounds one stalled step but not their sum, and nothing aborts on client
# disconnect, so one overall wall-clock deadline (plus a cooperative cancel_event) bounds the whole fetch instead.
@@ -17697,6 +17712,18 @@ def _fetch_page_text(
max_chars,
)
+ raw_file_url = _github_blob_raw_url(url)
+ if raw_file_url:
+ err, body, _ctype = _fetch_url_raw(
+ raw_file_url,
+ timeout = timeout,
+ deadline = deadline,
+ cancel_event = cancel_event,
+ **policy_kwargs,
+ )
+ if err is None and body.strip():
+ return _truncate_page_text(body.strip(), max_chars)
+
err, body, content_type = _fetch_url_raw(
url,
timeout = timeout,
diff --git a/studio/backend/tests/test_web_fetch_extraction.py b/studio/backend/tests/test_web_fetch_extraction.py
index 7e6bb8cffd1..644d4f8b96b 100644
--- a/studio/backend/tests/test_web_fetch_extraction.py
+++ b/studio/backend/tests/test_web_fetch_extraction.py
@@ -1127,6 +1127,103 @@ def test_fetch_page_text_falls_back_to_html_when_readme_api_fails(monkeypatch):
assert "There was an error while loading" not in out
+def test_fetch_page_text_reads_github_file_link_as_raw_file(monkeypatch):
+ source = "<!doctype html>\n<html>\n <body><script>run()</script></body>\n</html>\n"
+ calls = []
+
+ def fake_fetch(
+ url,
+ timeout = 30,
+ extra_headers = None,
+ deadline = None,
+ cancel_event = None,
+ ):
+ calls.append(url)
+ if url.startswith("https://raw.githubusercontent.com/"):
+ return None, source, "text/plain"
+ return None, _GITHUB_PAGE, "text/html"
+
+ monkeypatch.setattr("core.inference.tools._fetch_url_raw", fake_fetch)
+ out = _fetch_page_text(
+ "https://github.com/unslothai/unsloth/blob/main/studio/frontend/index.html?plain=1#L3"
+ )
+ assert calls == [
+ "https://raw.githubusercontent.com/unslothai/unsloth/main/studio/frontend/index.html"
+ ]
+ assert out == source.strip()
+
+
+def test_fetch_page_text_falls_back_to_github_file_page_when_raw_fails(monkeypatch):
+ calls = []
+
+ def fake_fetch(
+ url,
+ timeout = 30,
+ extra_headers = None,
+ deadline = None,
+ cancel_event = None,
+ ):
+ calls.append(url)
+ if url.startswith("https://raw.githubusercontent.com/"):
+ return "Failed to fetch URL: HTTP 404 Not Found", "", ""
+ return None, _GITHUB_PAGE, "text/html"
+
+ monkeypatch.setattr("core.inference.tools._fetch_url_raw", fake_fetch)
+ out = _fetch_page_text("https://github.com/owner/repo/blob/v1.2/src/app.py")
+ assert calls == [
+ "https://raw.githubusercontent.com/owner/repo/v1.2/src/app.py",
+ "https://github.com/owner/repo/blob/v1.2/src/app.py",
+ ]
+ assert "Unsloth Studio" in out
+
+
+def test_fetch_page_text_keeps_semicolon_in_github_file_name(monkeypatch):
+ calls = []
+
+ def fake_fetch(
+ url,
+ timeout = 30,
+ extra_headers = None,
+ deadline = None,
+ cancel_event = None,
+ ):
+ calls.append(url)
+ return None, "file body", "text/plain"
+
+ monkeypatch.setattr("core.inference.tools._fetch_url_raw", fake_fetch)
+ _fetch_page_text("https://github.com/owner/repo/blob/main/t/foo;bar.py")
+ assert calls == ["https://raw.githubusercontent.com/owner/repo/main/t/foo;bar.py"]
+
+
+ "url",
+ [
+ "https://github.com/unslothai/unsloth",
+ "https://github.com/unslothai/unsloth/tree/main/studio",
+ "https://github.com/unslothai/unsloth/blob/main",
+ "https://github.com/unslothai/unsloth/issues/123",
+ "https://example.com/owner/repo/blob/main/app.py",
+ ],
+)
+def test_fetch_page_text_leaves_other_github_urls_on_their_page(monkeypatch, url):
+ calls = []
+
+ def fake_fetch(
+ url,
+ timeout = 30,
+ extra_headers = None,
+ deadline = None,
+ cancel_event = None,
+ ):
+ calls.append(url)
+ return "Failed to fetch URL: HTTP 403", "", ""
+
+ monkeypatch.setattr("core.inference.tools._fetch_url_raw", fake_fetch)
+ _fetch_page_text(url)
+ assert not any("raw.githubusercontent.com" in c for c in calls)
+ assert calls[-1] == url
+
+
_RAW_TEXT_PAGE = "line one\n indented code\nline three"
_HTML_FRAGMENT = "<article><h1>Doc Title</h1><p>Readable fragment body.</p></article>"