Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

feat: shared OpenCLI call helper; move xiaohongshu to unified entry

Pnant4fa78284cd16
diff --git a/agent_reach/backends/opencli.py b/agent_reach/backends/opencli.py
index 5118bed0..a43eb981 100644
--- a/agent_reach/backends/opencli.py
+++ b/agent_reach/backends/opencli.py
@@ -194,3 +194,83 @@ def opencli_summary(st: OpenCLIStatus) -> str:
if st.daemon_running:
return "OpenCLI 已安装,等待 Chrome 扩展安装"
return "OpenCLI 已安装(daemon 未运行,使用时自动启动;需 Chrome 扩展)"
+
+
+# ── unified entry: run one OpenCLI site command ──
+
+_NAV_REJECTED_HINT = (
+ "OpenCLI's browser extension could not open the page. This is a known open "
+ "OpenCLI issue on recent Chrome (github.com/jackwener/opencli/issues/2487); "
+ "retrying a few seconds later sometimes works. Do not retry in a loop."
+)
+
+
+def _opencli_error(text: str, login_hint: str):
+ """Map OpenCLI's YAML error block (printed even with -f json) to a ReachError."""
+ import yaml
+
+ from agent_reach.runtime.result import NEED_LOGIN, UPSTREAM_BROKEN, ReachError
+
+ try:
+ doc = yaml.safe_load(text)
+ except yaml.YAMLError:
+ doc = None
+ err = (doc or {}).get("error") if isinstance(doc, dict) else None
+ code = str((err or {}).get("code", "")).upper()
+ message = str((err or {}).get("message", "") or text.strip()[:300] or "OpenCLI failed")[:300]
+ low = message.lower()
+ if "AUTH" in code or "LOGIN" in code or "login" in low or "登录" in message:
+ return ReachError(NEED_LOGIN, message, hint=login_hint or None)
+ if "navigation rejected" in low:
+ return ReachError(UPSTREAM_BROKEN, message, hint=_NAV_REJECTED_HINT)
+ return ReachError(UPSTREAM_BROKEN, f"OpenCLI: {message}")
+
+
+def opencli_call(site: str, args, *, timeout: float, login_hint: str = ""):
+ """Run `opencli <site> <args> -f json` and return parsed JSON, or raise ReachError."""
+ from agent_reach.runtime.run import CommandFailed, run_cmd
+
+ try:
+ out = run_cmd(["opencli", site, *args, "-f", "json"], timeout=timeout)
+ except CommandFailed as exc:
+ raise _opencli_error(exc.stdout or exc.stderr, login_hint) from None
+ try:
+ data = json.loads(out)
+ except ValueError:
+ raise _opencli_error(out, login_hint) from None
+ if isinstance(data, dict) and data.get("ok") is False:
+ raise _opencli_error(out, login_hint)
+ return data
+
+
+def opencli_items(rows) -> list:
+ """Map OpenCLI result rows to unified items, keeping each row in `raw`."""
+ from agent_reach.runtime.result import make_item
+
+ if isinstance(rows, dict):
+ rows = rows.get("items") or rows.get("data") or [rows]
+ items = []
+ for r in rows if isinstance(rows, list) else []:
+ if not isinstance(r, dict):
+ continue
+
+ def pick(*keys):
+ for k in keys:
+ v = r.get(k)
+ if isinstance(v, dict):
+ v = v.get("nickname") or v.get("name") or v.get("username")
+ if v not in (None, ""):
+ return str(v)
+ return None
+
+ items.append(
+ make_item(
+ title=pick("title", "name"),
+ url=pick("url", "link", "note_url"),
+ author=pick("author", "user", "nickname", "username"),
+ published_at=pick("published_at", "created_at", "time", "date"),
+ text=pick("content", "text", "desc", "description", "body"),
+ raw=r,
+ )
+ )
+ return items
diff --git a/agent_reach/channels/xiaohongshu.py b/agent_reach/channels/xiaohongshu.py
index a72dd0bf..5e082c1e 100644
--- a/agent_reach/channels/xiaohongshu.py
+++ b/agent_reach/channels/xiaohongshu.py
@@ -16,18 +16,24 @@ import urllib.error
import urllib.request
from pathlib import Path
+from agent_reach.backends.opencli import opencli_call, opencli_items
+from agent_reach.runtime.result import BAD_INPUT, NOT_FOUND, UPSTREAM_BROKEN, ReachError
from agent_reach.utils.paths import (
PrivatePathError,
read_small_text_no_follow,
)
-from .base import Channel
+from .base import Action, Channel, Param
from .mcporter import McporterConfigError, inspect_mcporter_config
_MCP_ENDPOINT = "http://localhost:18060/mcp"
_MCP_INSTALL_URL = "https://github.com/xpzouying/xiaohongshu-mcp"
_XHS_COOKIE_TTL_SECONDS = 7 * 86400
_MAX_XHS_COOKIE_BYTES = 1024 * 1024
+_XHS_LOGIN_HINT = (
+ "Ask the user to log in to xiaohongshu.com in their own Chrome (the one with "
+ "the OpenCLI extension). Never log in or read browser cookies for them."
+)
def _mcp_service_reachable(timeout: int = 3) -> bool:
@@ -163,6 +169,26 @@ class XiaoHongShuChannel(Channel):
backends = ["OpenCLI", "xiaohongshu-mcp", "xhs-cli (xiaohongshu-cli)"]
tier = 1
+ level = "optional"
+ actions = (
+ Action(
+ "search",
+ "Search notes",
+ (
+ Param("query", "what to search for"),
+ Param("limit", "max results", required=False, type=int, default=5),
+ ),
+ (("opencli", "_opencli_search"),),
+ live_test={"query": "咖啡", "limit": 2},
+ ),
+ Action(
+ "read",
+ "Read a note (full link including xsec_token, as returned by search)",
+ (Param("target", "full note link with xsec_token"),),
+ (("opencli", "_opencli_read"),),
+ ),
+ )
+
def can_handle(self, url: str) -> bool:
from agent_reach.utils.url import host_matches
@@ -303,3 +329,31 @@ class XiaoHongShuChannel(Channel):
"再用 Cookie-Editor 导出并运行 "
"agent-reach configure xhs-cookies。"
)
+
+ # ── unified entry backends ──
+
+ def _opencli_search(self, *, query: str, limit: int, timeout: float) -> list:
+ rows = opencli_call(
+ "xiaohongshu", ["search", query, "--limit", str(limit)],
+ timeout=timeout, login_hint=_XHS_LOGIN_HINT,
+ )
+ items = opencli_items(rows)[:limit]
+ if not items:
+ raise ReachError(
+ UPSTREAM_BROKEN,
+ "OpenCLI returned no notes; the page most likely did not load",
+ )
+ return items
+
+ def _opencli_read(self, *, target: str, timeout: float) -> list:
+ from agent_reach.utils.url import host_matches
+
+ url = str(target).strip()
+ if not host_matches(url, "xiaohongshu.com", "xhslink.com"):
+ raise ReachError(BAD_INPUT, "target must be a full Xiaohongshu note link including xsec_token")
+ items = opencli_items(
+ opencli_call("xiaohongshu", ["note", url], timeout=timeout, login_hint=_XHS_LOGIN_HINT)
+ )
+ if not items:
+ raise ReachError(NOT_FOUND, "note not found")
+ return items[:1]