新增功能: - execution_log 模块:自动记录引擎调用耗时、成功/失败、错误详情 - 所有搜索引擎自动跟踪每次调用的性能指标 - CLI 搜索后自动显示执行摘要 - health 命令使用 ParallelSearch 检测所有引擎连通性 - 新增 log 命令查看历史执行日志
87 lines
3.4 KiB
Python
87 lines
3.4 KiB
Python
"""GitHub code search engine — uses GitHub API (no key needed for basic search)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
import logging
|
|
from urllib.parse import quote_plus
|
|
|
|
import httpx
|
|
|
|
from scripts.engines.base import BaseEngine, EngineResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
HEADERS = {
|
|
"Accept": "application/vnd.github.v3+json",
|
|
"User-Agent": "web-search-skills/1.0",
|
|
}
|
|
|
|
|
|
class GitHubEngine(BaseEngine):
|
|
"""GitHub code & repo search via public API."""
|
|
|
|
name = "github"
|
|
|
|
async def search(self, query: str, max_results: int = 10,
|
|
search_type: str = "repositories", **kwargs) -> list[EngineResult]:
|
|
"""search_type: 'repositories' | 'code' | 'issues'"""
|
|
t0 = time.time()
|
|
endpoint = f"https://api.github.com/search/{search_type}"
|
|
params = {"q": query, "per_page": min(max_results, 30), "sort": "stars"}
|
|
|
|
try:
|
|
async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client:
|
|
resp = await client.get(endpoint, headers=HEADERS, params=params)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
elapsed = time.time() - t0
|
|
|
|
results = []
|
|
for item in data.get("items", [])[:max_results]:
|
|
if search_type == "repositories":
|
|
results.append(EngineResult(
|
|
title=item.get("full_name", ""),
|
|
url=item.get("html_url", ""),
|
|
snippet=item.get("description", "") or "",
|
|
source="github",
|
|
category="github",
|
|
extra={
|
|
"stars": item.get("stargazers_count", 0),
|
|
"language": item.get("language", ""),
|
|
"updated": item.get("updated_at", ""),
|
|
},
|
|
))
|
|
elif search_type == "code":
|
|
results.append(EngineResult(
|
|
title=item.get("name", ""),
|
|
url=item.get("html_url", ""),
|
|
snippet=item.get("text_matches", [{}])[0].get("fragment", "")
|
|
if item.get("text_matches") else "",
|
|
source="github",
|
|
category="github",
|
|
extra={
|
|
"repo": item.get("repository", {}).get("full_name", ""),
|
|
"language": item.get("language", ""),
|
|
},
|
|
))
|
|
elif search_type == "issues":
|
|
results.append(EngineResult(
|
|
title=item.get("title", ""),
|
|
url=item.get("html_url", ""),
|
|
snippet=item.get("body", "")[:300] if item.get("body") else "",
|
|
source="github",
|
|
category="github",
|
|
extra={
|
|
"state": item.get("state", ""),
|
|
"repo": item.get("repository_url", "").split("/")[-2:]
|
|
if item.get("repository_url") else [],
|
|
},
|
|
))
|
|
|
|
logger.info(f"[github] {len(results)} results in {elapsed:.2f}s")
|
|
return results
|
|
except Exception as e:
|
|
logger.warning(f"[github] failed: {e}")
|
|
return []
|