c949204662
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""极简 HTML→纯文本提取(纯标准库,零第三方依赖,避免引入许可不明的解析库)。
|
|
|
|
不追求完美排版,目标是从网页中抽取可读正文供模型摘要。会丢弃 script/style/
|
|
noscript 等非正文标签,折叠多余空白。
|
|
"""
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
_SKIP_TAGS = {"script", "style", "noscript", "template", "svg", "head"}
|
|
_BLOCK_TAGS = {
|
|
"p", "div", "br", "li", "ul", "ol", "tr", "table",
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "section", "article", "header", "footer",
|
|
}
|
|
|
|
|
|
class _Extractor(HTMLParser):
|
|
def __init__(self):
|
|
super().__init__(convert_charrefs=True)
|
|
self._parts: list[str] = []
|
|
self._skip_depth = 0
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
if tag in _SKIP_TAGS:
|
|
self._skip_depth += 1
|
|
elif tag in _BLOCK_TAGS:
|
|
self._parts.append("\n")
|
|
|
|
def handle_endtag(self, tag):
|
|
if tag in _SKIP_TAGS and self._skip_depth > 0:
|
|
self._skip_depth -= 1
|
|
elif tag in _BLOCK_TAGS:
|
|
self._parts.append("\n")
|
|
|
|
def handle_data(self, data):
|
|
if self._skip_depth == 0 and data:
|
|
self._parts.append(data)
|
|
|
|
def text(self) -> str:
|
|
raw = "".join(self._parts)
|
|
# 折叠空白:去掉行内多余空格,压缩连续空行
|
|
lines = [ " ".join(line.split()) for line in raw.splitlines() ]
|
|
out: list[str] = []
|
|
blank = False
|
|
for line in lines:
|
|
if line:
|
|
out.append(line)
|
|
blank = False
|
|
elif not blank:
|
|
out.append("")
|
|
blank = True
|
|
return "\n".join(out).strip()
|
|
|
|
|
|
def html_to_text(html: str, max_chars: int = 6000) -> str:
|
|
"""把 HTML 转为纯文本并截断到 max_chars。解析失败时退化为原文截断。"""
|
|
if not html:
|
|
return ""
|
|
try:
|
|
parser = _Extractor()
|
|
parser.feed(html)
|
|
text = parser.text()
|
|
except Exception:
|
|
text = html
|
|
if len(text) > max_chars:
|
|
text = text[:max_chars] + "…"
|
|
return text
|