argo-search 2.16.0 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +4 -2
- package/README.es.md +15 -5
- package/README.ja.md +15 -5
- package/README.ko.md +15 -5
- package/README.md +16 -6
- package/README.zh.md +16 -6
- package/SKILL.md +12 -11
- package/assets/readme/counts.json +1 -1
- package/assets/readme/hero.svg +3 -3
- package/assets/readme/hero.template.svg +121 -0
- package/assets/readme/proof-routes.svg +17 -16
- package/backends/domain_profiles.json +121 -18
- package/backends/engine_registry.yaml +23 -0
- package/backends/env_passthrough.txt +2 -0
- package/backends/quota_profiles.json +20 -0
- package/bin/argo +78 -12
- package/docs/CHANGELOG.md +35 -1
- package/docs/COMMIT_STANDARD.md +12 -2
- package/docs/ENGINE_CATALOG.md +36 -33
- package/docs/RELEASE_NOTES_v2.17.0.md +66 -0
- package/docs/RELEASE_NOTES_v2.18.0.md +87 -0
- package/docs/RELEASING.md +2 -2
- package/engines/specs/crossref.yaml +2 -0
- package/engines/specs/hackernews.yaml +2 -0
- package/engines/specs/helve.yaml +49 -0
- package/engines/specs/hf_papers.yaml +2 -0
- package/engines/specs/openalex.yaml +9 -2
- package/engines/specs/twitter_cli.yaml +17 -0
- package/engines/specs/twitter_syndication.yaml +2 -3
- package/package.json +3 -1
- package/packages/dsh-plugin/README.md +5 -2
- package/packages/dsh-plugin/cordis.patch.yml +4 -1
- package/packages/dsh-plugin/dsh/client.js +57 -63
- package/packages/dsh-plugin/dsh/diagnostics.js +25 -4
- package/packages/dsh-plugin/dsh/index.js +109 -21
- package/packages/dsh-plugin/dsh/native-tools.mjs +10 -0
- package/packages/dsh-plugin/dsh/route-guard.js +95 -0
- package/packages/dsh-plugin/dsh/runner.js +49 -6
- package/packages/dsh-plugin/dsh/wide-research.js +20 -18
- package/packages/dsh-plugin/dsh/workbench-client.js +119 -20
- package/packages/dsh-plugin/dsh/workbench-io.js +182 -0
- package/packages/dsh-plugin/dsh/workbench-search.js +445 -0
- package/packages/dsh-plugin/dsh/workbench-suggestions.js +387 -75
- package/packages/dsh-plugin/dsh/workbench.js +277 -497
- package/packages/dsh-plugin/package.json +4 -3
- package/packages/dsh-plugin/scripts/sync-local.mjs +4 -2
- package/packages/dsh-plugin/scripts/sync-local.sh +4 -2
- package/packages/dsh-plugin/test/client.test.js +26 -0
- package/packages/dsh-plugin/test/diagnostics.test.js +5 -2
- package/packages/dsh-plugin/test/index.test.js +150 -1
- package/packages/dsh-plugin/test/workbench-concurrency.test.js +96 -0
- package/packages/dsh-plugin/test/workbench.test.js +422 -9
- package/scripts/adaptive.py +13 -11
- package/scripts/affected_tests.py +324 -0
- package/scripts/badcase_ledger.py +93 -2
- package/scripts/browser_auth.py +178 -4
- package/scripts/cache.py +68 -69
- package/scripts/check_commit_report.py +59 -9
- package/scripts/chrome_cdp.py +71 -0
- package/scripts/circuit_breaker.py +77 -12
- package/scripts/cli_io.py +8 -3
- package/scripts/config_cache.py +4 -3
- package/scripts/daemon.py +9 -2
- package/scripts/engine_add.py +45 -0
- package/scripts/engine_dispatch.py +27 -47
- package/scripts/engine_failure.py +33 -0
- package/scripts/engine_families.py +1 -0
- package/scripts/engine_fingerprint.py +114 -0
- package/scripts/engine_health.py +130 -0
- package/scripts/engine_states.py +54 -4
- package/scripts/engines.py +2 -0
- package/scripts/engines_base.py +19 -44
- package/scripts/engines_builders_intl.py +20 -94
- package/scripts/engines_builders_search.py +19 -8
- package/scripts/engines_builders_tech.py +16 -16
- package/scripts/engines_fields.py +23 -0
- package/scripts/engines_twitter_cli.py +182 -0
- package/scripts/evidence_grounding.py +182 -0
- package/scripts/fetch_browser.py +25 -10
- package/scripts/fetch_cli_output.py +75 -0
- package/scripts/fetch_dispatch.py +248 -0
- package/scripts/fetch_quality.py +11 -0
- package/scripts/fetch_v3.py +48 -114
- package/scripts/gate_manifest_check.py +75 -0
- package/scripts/gen_readme_art.py +264 -0
- package/scripts/intent_router.py +224 -0
- package/scripts/mcp_compact.py +3 -0
- package/scripts/mcp_handlers.py +24 -4
- package/scripts/mcp_tools.py +2 -0
- package/scripts/mcp_transport.py +24 -5
- package/scripts/mcp_warm.py +15 -11
- package/scripts/numeric_verify.py +9 -0
- package/scripts/output_transparency.py +10 -0
- package/scripts/pre-commit +65 -8
- package/scripts/query_operators.py +229 -1
- package/scripts/quota.py +19 -37
- package/scripts/release.py +16 -1
- package/scripts/release_check.py +1 -2
- package/scripts/research_gates.py +85 -1
- package/scripts/research_notes.py +52 -5
- package/scripts/retry_policy.py +17 -3
- package/scripts/route_combo.py +21 -4
- package/scripts/search.py +29 -29
- package/scripts/search_cli.py +104 -8
- package/scripts/search_output.py +4 -1
- package/scripts/social_research.py +8 -6
- package/scripts/source_discovery.py +611 -0
- package/scripts/sync_backends.py +45 -0
- package/scripts/time_utils.py +28 -0
- package/scripts/twitter_single_read.py +107 -0
- package/scripts/x_keyword_route.py +138 -0
- package/scripts/x_search.py +24 -33
- package/scripts/zhihu_answer_body.py +96 -0
- package/scripts/zhihu_answer_fetch.py +66 -0
- package/scripts/zhihu_answer_lookup.py +138 -0
- package/sub-skills/ego-search/README.md +9 -0
- package/sub-skills/ego-search/scripts/ego_search.py +182 -15
- package/sub-skills/ego-search/scripts/pacing.py +291 -0
- package/sub-skills/ego-search/scripts/quality.py +262 -6
- package/sub-skills/ego-search/scripts/runtime.py +7 -1
- package/sub-skills/ego-search/scripts/site_profiles.py +157 -0
- package/sub-skills/ego-search/scripts/webbridge_adapter.py +7 -1
- package/sub-skills/local-seek/SKILL.md +3 -1
- package/sub-skills/local-seek/scripts/seek.py +80 -109
- package/sub-skills/local-seek/scripts/seek_git.py +65 -0
- package/sub-skills/local-seek/scripts/seek_inspect.py +242 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""evidence_grounding.py — 确定性原文摘录锚定、防编造数字门禁与量级单位归一化。
|
|
3
|
+
|
|
4
|
+
纯确定性证据锚定与量级换算内核(零 LLM 依赖):
|
|
5
|
+
1. fold_digits:NFKC 折叠全半角数字与千分位分隔符;
|
|
6
|
+
2. check_ungrounded_numbers:≥3 位数字必须在原文中真实存在,拦截「跨页套用模板数字」;
|
|
7
|
+
3. extract_anchored_context:优先在正文(跳过前导元信息)取距离实体最近的数值窗口,
|
|
8
|
+
输出字符 span 与 sha1 摘录哈希,防止页面通用词(如多处价格/评分)张冠李戴;
|
|
9
|
+
4. normalize_magnitude_unit:保守校准中英量级(万/亿/million/billion 等),仅当数值
|
|
10
|
+
原样抄录原文裸数字且邻近量级词与目标列单位不一致时换算,杜绝二次误换算。
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import re
|
|
16
|
+
import unicodedata
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
_CJK_MAGNITUDES: dict[str, float] = {
|
|
20
|
+
"千": 1e3,
|
|
21
|
+
"万": 1e4,
|
|
22
|
+
"十万": 1e5,
|
|
23
|
+
"百万": 1e6,
|
|
24
|
+
"千万": 1e7,
|
|
25
|
+
"亿": 1e8,
|
|
26
|
+
"十亿": 1e9,
|
|
27
|
+
"百亿": 1e10,
|
|
28
|
+
"千亿": 1e11,
|
|
29
|
+
"万亿": 1e12,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_EN_MAGNITUDES: dict[str, float] = {
|
|
33
|
+
"thousand": 1e3,
|
|
34
|
+
"million": 1e6,
|
|
35
|
+
"millions": 1e6,
|
|
36
|
+
"billion": 1e9,
|
|
37
|
+
"billions": 1e9,
|
|
38
|
+
"trillion": 1e12,
|
|
39
|
+
"trillions": 1e12,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_ALL_MAGNITUDES: dict[str, float] = {**_CJK_MAGNITUDES, **_EN_MAGNITUDES}
|
|
43
|
+
|
|
44
|
+
_NUM_RE = r"[-+]?\d[\d,,]*(?:\.\d+)?"
|
|
45
|
+
_UNIT_ALT = (
|
|
46
|
+
"万亿|千亿|百亿|十亿|千万|百万|十万|亿|万|千"
|
|
47
|
+
"|trillions|trillion|billions|billion|millions|million|thousand"
|
|
48
|
+
)
|
|
49
|
+
_NUM_UNIT_PAIR_RE = re.compile(rf"({_NUM_RE})\s*({_UNIT_ALT})", re.IGNORECASE)
|
|
50
|
+
_TARGET_UNIT_RE = re.compile(rf"({_UNIT_ALT})", re.IGNORECASE)
|
|
51
|
+
_PREAMBLE_RE = re.compile(r"Markdown Content:\s*")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def fold_digits(text: str) -> str:
|
|
55
|
+
"""NFKC 折叠全半角并去除千分位逗号(全/半角)。"""
|
|
56
|
+
folded = unicodedata.normalize("NFKC", text or "")
|
|
57
|
+
return folded.replace(",", "").replace(",", "")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def check_ungrounded_numbers(page_text: str, value: str, *, min_digits: int = 3) -> list[str]:
|
|
61
|
+
"""检查 value 中所有 ≥min_digits 位的数字是否真实出现在 page_text 中。
|
|
62
|
+
|
|
63
|
+
返回未在原文中出现的数字 token 列表;空列表表示通过。
|
|
64
|
+
仅检 ≥3 位数字,避免日期/序号/小评分产生误报。
|
|
65
|
+
"""
|
|
66
|
+
if not value:
|
|
67
|
+
return []
|
|
68
|
+
tokens = [t for t in re.findall(r"\d+", fold_digits(value)) if len(t) >= min_digits]
|
|
69
|
+
if not tokens:
|
|
70
|
+
return []
|
|
71
|
+
folded_page = fold_digits(page_text)
|
|
72
|
+
return [t for t in dict.fromkeys(tokens) if t not in folded_page]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def extract_anchored_context(
|
|
76
|
+
page_text: str,
|
|
77
|
+
value: str,
|
|
78
|
+
*,
|
|
79
|
+
entity: str = "",
|
|
80
|
+
window: int = 120,
|
|
81
|
+
) -> dict[str, Any]:
|
|
82
|
+
"""在原文中定位距离 entity 最近的 value 窗口,返回 {excerpt, span, excerpt_sha1}。"""
|
|
83
|
+
if not page_text or not value:
|
|
84
|
+
return {"excerpt": "", "span": None, "excerpt_sha1": ""}
|
|
85
|
+
|
|
86
|
+
haystack = page_text
|
|
87
|
+
preamble = _PREAMBLE_RE.search(haystack)
|
|
88
|
+
body_start = preamble.end() if preamble else 0
|
|
89
|
+
|
|
90
|
+
def _positions(needle: str) -> list[int]:
|
|
91
|
+
if not needle or len(needle.strip()) < 2:
|
|
92
|
+
return []
|
|
93
|
+
exact = [m.start() for m in re.finditer(re.escape(needle), haystack)]
|
|
94
|
+
if exact:
|
|
95
|
+
return exact
|
|
96
|
+
low_h = haystack.lower()
|
|
97
|
+
return [m.start() for m in re.finditer(re.escape(needle.lower()), low_h)]
|
|
98
|
+
|
|
99
|
+
def _prefer_body(positions: list[int]) -> list[int]:
|
|
100
|
+
in_body = [p for p in positions if p >= body_start]
|
|
101
|
+
return in_body or positions
|
|
102
|
+
|
|
103
|
+
val_pos = _prefer_body(_positions(value))
|
|
104
|
+
if not val_pos:
|
|
105
|
+
# 尝试折叠千分位后的数字在原文中定位
|
|
106
|
+
folded_val = fold_digits(value).strip()
|
|
107
|
+
if folded_val and folded_val != value:
|
|
108
|
+
val_pos = _prefer_body(_positions(folded_val))
|
|
109
|
+
|
|
110
|
+
if not val_pos:
|
|
111
|
+
return {"excerpt": "", "span": None, "excerpt_sha1": ""}
|
|
112
|
+
|
|
113
|
+
ent_pos = _prefer_body(_positions(entity)) if entity else []
|
|
114
|
+
if ent_pos:
|
|
115
|
+
anchor = min(val_pos, key=lambda v: min(abs(v - e) for e in ent_pos))
|
|
116
|
+
else:
|
|
117
|
+
anchor = val_pos[0]
|
|
118
|
+
|
|
119
|
+
start = max(0, anchor - window)
|
|
120
|
+
end = min(len(haystack), anchor + len(value) + window)
|
|
121
|
+
excerpt = " ".join(haystack[start:end].split())
|
|
122
|
+
sha1 = f"sha1:{hashlib.sha1(excerpt.encode('utf-8')).hexdigest()}"
|
|
123
|
+
return {
|
|
124
|
+
"excerpt": excerpt,
|
|
125
|
+
"span": [start, end],
|
|
126
|
+
"excerpt_sha1": sha1,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _parse_float(raw: Any) -> float | None:
|
|
131
|
+
try:
|
|
132
|
+
return float(fold_digits(str(raw)).strip())
|
|
133
|
+
except (TypeError, ValueError):
|
|
134
|
+
return None
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _format_num(val: float) -> str:
|
|
138
|
+
if abs(val - round(val)) < 1e-9:
|
|
139
|
+
return str(int(round(val)))
|
|
140
|
+
return f"{val:.6f}".rstrip("0").rstrip(".")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def normalize_magnitude_unit(
|
|
144
|
+
value: str | float | int,
|
|
145
|
+
excerpt: str,
|
|
146
|
+
target_unit: str,
|
|
147
|
+
) -> dict[str, Any]:
|
|
148
|
+
"""保守量级换算:把原样抄录的源数字按 target_unit 换算。
|
|
149
|
+
|
|
150
|
+
例:excerpt="$131 billion", value="131", target_unit="亿美元" → normalized="1310"。
|
|
151
|
+
若 value 已经换算过(不再等于 excerpt 中的源裸数字),保持原值不变。
|
|
152
|
+
"""
|
|
153
|
+
raw_str = str(value).strip()
|
|
154
|
+
num = _parse_float(raw_str)
|
|
155
|
+
if num is None or not excerpt or not target_unit:
|
|
156
|
+
return {"normalized": raw_str, "changed": False, "factor": 1.0}
|
|
157
|
+
|
|
158
|
+
tm = _TARGET_UNIT_RE.search(target_unit)
|
|
159
|
+
if not tm:
|
|
160
|
+
return {"normalized": raw_str, "changed": False, "factor": 1.0}
|
|
161
|
+
target_scale = _ALL_MAGNITUDES.get(tm.group(1).lower())
|
|
162
|
+
if not target_scale:
|
|
163
|
+
return {"normalized": raw_str, "changed": False, "factor": 1.0}
|
|
164
|
+
|
|
165
|
+
for m in _NUM_UNIT_PAIR_RE.finditer(excerpt):
|
|
166
|
+
src_num = _parse_float(m.group(1))
|
|
167
|
+
src_unit = m.group(2).lower()
|
|
168
|
+
src_scale = _ALL_MAGNITUDES.get(src_unit)
|
|
169
|
+
if src_num is None or not src_scale:
|
|
170
|
+
continue
|
|
171
|
+
# 仅当 value 仍等于源裸数字(未换算)且源/目标量级不同时才做换算
|
|
172
|
+
if abs(src_num - num) <= 1e-6 and abs(src_scale - target_scale) > 1e-6:
|
|
173
|
+
factor = src_scale / target_scale
|
|
174
|
+
scaled = num * factor
|
|
175
|
+
return {
|
|
176
|
+
"normalized": _format_num(scaled),
|
|
177
|
+
"changed": True,
|
|
178
|
+
"factor": factor,
|
|
179
|
+
"source_unit": m.group(2),
|
|
180
|
+
"target_unit": tm.group(1),
|
|
181
|
+
}
|
|
182
|
+
return {"normalized": raw_str, "changed": False, "factor": 1.0}
|
package/scripts/fetch_browser.py
CHANGED
|
@@ -58,9 +58,12 @@ def browser_fetch(url: str, max_chars: int = 8000, timeout: float = 15.0,
|
|
|
58
58
|
html = cdp.get_html()
|
|
59
59
|
text = cdp.get_text()
|
|
60
60
|
title = cdp.get_title()
|
|
61
|
+
final_url = cdp.get_url() or url
|
|
61
62
|
|
|
63
|
+
# 单份基座 + 登录态分支补充(acbd34a 曾改双抄,2026-11-20 审计还原);
|
|
64
|
+
# 显式 0 分等哨兵教训:基座字段逐个手抄两份必然漂移
|
|
62
65
|
result = {
|
|
63
|
-
"url":
|
|
66
|
+
"url": final_url,
|
|
64
67
|
"content": text[:max_chars] if text else "",
|
|
65
68
|
"html": html[:max_chars * 2] if html else "",
|
|
66
69
|
"title": title or "",
|
|
@@ -69,16 +72,28 @@ def browser_fetch(url: str, max_chars: int = 8000, timeout: float = 15.0,
|
|
|
69
72
|
"error": None if text else "empty content",
|
|
70
73
|
"fetch_method": engine_states.FETCH_CHROME_CDP,
|
|
71
74
|
}
|
|
75
|
+
|
|
72
76
|
if auth_profile:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
from browser_auth import detect_page_auth_state, touch
|
|
78
|
+
auth_state = detect_page_auth_state(final_url, title or "", text or "")
|
|
79
|
+
if engine_states.is_page_auth_non_retryable(auth_state):
|
|
80
|
+
# 诚实错误边界:登录墙/风控/真登出绝不静默当正文返回,也不误标可重试
|
|
81
|
+
return {
|
|
82
|
+
"url": final_url, "content": "", "html": "", "title": title or "",
|
|
83
|
+
"length": 0, "success": False,
|
|
84
|
+
"error": f"authenticated fetch blocked ({auth_state})",
|
|
85
|
+
"fetch_method": engine_states.FETCH_CHROME_CDP,
|
|
86
|
+
"login_state_used": True, "cache_eligible": False,
|
|
87
|
+
"auth_state": auth_state, "retryable": False,
|
|
88
|
+
"handoff_required": True,
|
|
89
|
+
}
|
|
90
|
+
result.update({
|
|
91
|
+
"login_state_used": True,
|
|
92
|
+
"cache_eligible": False,
|
|
93
|
+
"auth_state": auth_state,
|
|
94
|
+
})
|
|
95
|
+
touch(url)
|
|
96
|
+
|
|
82
97
|
return result
|
|
83
98
|
except Exception as e:
|
|
84
99
|
return _fail(url, f"CDP error: {str(e)[:100]}")
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""抓取命令的 JSON 摘要与终端正文输出。"""
|
|
2
|
+
|
|
3
|
+
from cli_io import dumps
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# 人类可读输出的默认显示上限(字符)。--limit 管的是交付窗口,这里是显示上限;
|
|
7
|
+
# 显式 --full / --limit 时按调用方要求放开,截断时打印可翻页的提示。
|
|
8
|
+
_DISPLAY_CHARS = 2000
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def print_result(r: dict, *, json_only: bool, include_links: bool, focus: str,
|
|
12
|
+
pre_focus_len: int, full: bool, limit: int) -> None:
|
|
13
|
+
focus_requested = bool(focus)
|
|
14
|
+
summary = {k: r[k] for k in ("success", "fetch_method", "dispatch_handler",
|
|
15
|
+
"content_ok", "quality_score", "page_type",
|
|
16
|
+
"source_type", "is_official", "length",
|
|
17
|
+
"url", "title", "video_meta") if k in r}
|
|
18
|
+
if json_only and "content" in r:
|
|
19
|
+
summary["content"] = r["content"]
|
|
20
|
+
# 截断必须可见:只给 8,000 字却不说明「还有多少没给」,调用方会当全文用
|
|
21
|
+
for k in ("truncated", "full_length", "full_text_path", "total_length",
|
|
22
|
+
"offset", "full_source", "final_url",
|
|
23
|
+
"login_state_used", "cache_eligible"):
|
|
24
|
+
if k in r:
|
|
25
|
+
summary[k] = r[k]
|
|
26
|
+
if "content_type" in r and "next_offset" in r:
|
|
27
|
+
summary.update({k: r[k] for k in ("status", "content_type", "next_offset")})
|
|
28
|
+
if json_only:
|
|
29
|
+
summary["content"] = r["content"]
|
|
30
|
+
if focus_requested:
|
|
31
|
+
# 显式回报聚焦是否真的生效:正文过短时 focus_applied=False,
|
|
32
|
+
# 不谎报「已省 token」
|
|
33
|
+
summary["focus_applied"] = bool(r.get("focus_applied"))
|
|
34
|
+
if r.get("error"):
|
|
35
|
+
summary["error"] = r["error"]
|
|
36
|
+
if r.get("links"):
|
|
37
|
+
summary["links_count"] = len(r["links"])
|
|
38
|
+
if include_links:
|
|
39
|
+
summary["links"] = r["links"]
|
|
40
|
+
print(dumps(summary))
|
|
41
|
+
if not json_only:
|
|
42
|
+
if r.get("title"):
|
|
43
|
+
print(f"\nTitle: {r['title']}")
|
|
44
|
+
if focus_requested:
|
|
45
|
+
print(f"\n[focus] query={focus!r} applied={bool(r.get('focus_applied'))} "
|
|
46
|
+
f"chars={pre_focus_len} → {r.get('length', 0)}")
|
|
47
|
+
print(f"\n--- CONTENT ({r['length']} chars) ---")
|
|
48
|
+
# 默认档保持 2000 字显示上限(护住 agent 上下文),但显式 --full /
|
|
49
|
+
# --limit 时必须按调用方要求显示完整交付窗口:此前这行硬编码
|
|
50
|
+
# [:2000],与 --limit 的语义(本次输出字符数上限,0 表示不限)冲突,
|
|
51
|
+
# 且表头写的是交付长度,于是「说 8000 字、只给 2000 字、不说明还剩
|
|
52
|
+
# 多少」——调用方会把截断正文当全文用(证据核验场景尤其危险)。
|
|
53
|
+
delivered = r.get("content", "") or ""
|
|
54
|
+
show_all = bool(full) or bool(limit and limit > 0)
|
|
55
|
+
shown = delivered if show_all else delivered[:_DISPLAY_CHARS]
|
|
56
|
+
print(shown)
|
|
57
|
+
if len(shown) < len(delivered):
|
|
58
|
+
# 两个字段是不同代码路径写的同一事实:fetch_v3 路径给 full_length,
|
|
59
|
+
# _full_view 路径给 total_length,故这里按可用者兜底。
|
|
60
|
+
full_len = r.get("full_length") or r.get("total_length") or len(delivered)
|
|
61
|
+
if focus_requested:
|
|
62
|
+
# --focus 之后 content 是重排后的段落抽取,不再是正文的连续窗口,
|
|
63
|
+
# 按「本页起点 + 本页长度」推 --offset 会指向无关区间。宁可不给
|
|
64
|
+
# 翻页建议,也不能给错的。
|
|
65
|
+
print(f"\n[truncated] 已显示 {len(shown)}/{len(delivered)} 字"
|
|
66
|
+
f"(页面全文 {full_len} 字)。当前为 --focus 抽取结果,"
|
|
67
|
+
f"非正文连续窗口;要完整正文请用 --full")
|
|
68
|
+
else:
|
|
69
|
+
# --offset 是相对正文全文的绝对偏移(见 _full_view),所以下一页
|
|
70
|
+
# 要从「本页起点 + 本页长度」继续,否则已翻过页的调用方会原地打转。
|
|
71
|
+
next_offset = int(r.get("offset") or 0) + len(shown)
|
|
72
|
+
print(f"\n[truncated] 已显示 {len(shown)}/{len(delivered)} 字"
|
|
73
|
+
f"(页面全文 {full_len} 字)。"
|
|
74
|
+
f"继续读:--offset {next_offset} --limit {_DISPLAY_CHARS};"
|
|
75
|
+
f"全文:--full")
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""fetch_dispatch.py — argo fetch 统一 URL 专用提取器自动分发层。
|
|
3
|
+
|
|
4
|
+
让调用方只需记住 `argo fetch <url>`,由本模块按 URL 特征自动路由至对应专有提取器:
|
|
5
|
+
- wechat : mp.weixin.qq.com/s... → wechat_article.fetch_wechat_article
|
|
6
|
+
- tweet : x.com|twitter.com/*/status/* → tweet.build_bundle + _render_text
|
|
7
|
+
- paper : arxiv.org/abs|html/<id> → paper.fetch_meta(结构化摘要与元数据)
|
|
8
|
+
- pdf : *.pdf 或 arxiv.org/pdf/<id> → pdf_extract.extract_pdf
|
|
9
|
+
|
|
10
|
+
回落原则:
|
|
11
|
+
- 微信与 PDF 链接保持显式失败契约(提取失败、空正文或抛出异常时均返回
|
|
12
|
+
`success=False` 及对应 `FETCH_*_FAILED`,不落回通用 HTML 抓取链);
|
|
13
|
+
- tweet / paper 专用提取失败时返回 None,交由 fetch_v3 常规多级降级链兜底。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
from typing import Any
|
|
20
|
+
from urllib.parse import urlparse
|
|
21
|
+
|
|
22
|
+
import engine_states
|
|
23
|
+
|
|
24
|
+
_TWEET_URL_RE = re.compile(
|
|
25
|
+
r"^https?://(?:(?:www|mobile)\.)?(?:x\.com|twitter\.com)/[^/]+/status/\d+",
|
|
26
|
+
re.IGNORECASE,
|
|
27
|
+
)
|
|
28
|
+
_ARXIV_ABS_RE = re.compile(
|
|
29
|
+
r"^https?://(?:www\.)?arxiv\.org/(?:abs|html)/([0-9]{4}\.[0-9]{4,5}(?:v\d+)?)",
|
|
30
|
+
re.IGNORECASE,
|
|
31
|
+
)
|
|
32
|
+
_ARXIV_PDF_RE = re.compile(
|
|
33
|
+
r"^https?://(?:www\.)?arxiv\.org/pdf/([0-9]{4}\.[0-9]{4,5}(?:v\d+)?)(?:\.pdf)?",
|
|
34
|
+
re.IGNORECASE,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def classify_specialized_url(url: str) -> str | None:
|
|
39
|
+
"""识别适合走专有提取器的 URL 类型;普通网页返回 None。"""
|
|
40
|
+
if not url or not isinstance(url, str):
|
|
41
|
+
return None
|
|
42
|
+
u = url.strip()
|
|
43
|
+
try:
|
|
44
|
+
parsed = urlparse(u)
|
|
45
|
+
except ValueError:
|
|
46
|
+
return None
|
|
47
|
+
host = (parsed.hostname or "").lower()
|
|
48
|
+
path = parsed.path or ""
|
|
49
|
+
|
|
50
|
+
if host == "mp.weixin.qq.com" and (path == "/s" or path.startswith("/s/")):
|
|
51
|
+
return "wechat"
|
|
52
|
+
if _TWEET_URL_RE.match(u):
|
|
53
|
+
return "tweet"
|
|
54
|
+
if _ARXIV_PDF_RE.match(u) or path.lower().endswith(".pdf"):
|
|
55
|
+
return "pdf"
|
|
56
|
+
if _ARXIV_ABS_RE.match(u):
|
|
57
|
+
return "paper"
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _dispatch_wechat(url: str, max_chars: int) -> dict[str, Any]:
|
|
62
|
+
try:
|
|
63
|
+
from wechat_article import fetch_wechat_article
|
|
64
|
+
wx = fetch_wechat_article(url)
|
|
65
|
+
except Exception as exc:
|
|
66
|
+
wx = {"ok": False, "error": f"{type(exc).__name__}: {exc}"}
|
|
67
|
+
if wx and wx.get("ok"):
|
|
68
|
+
content = (wx.get("content") or "")[:max_chars]
|
|
69
|
+
return {
|
|
70
|
+
"url": url,
|
|
71
|
+
"title": wx.get("title") or "",
|
|
72
|
+
"content": content,
|
|
73
|
+
"html": "",
|
|
74
|
+
"length": int(wx.get("char_count") or len(content)),
|
|
75
|
+
"success": True,
|
|
76
|
+
"fetch_method": engine_states.FETCH_WECHAT + str(
|
|
77
|
+
wx.get("fetch_method") or engine_states.FETCH_HTTP_DIRECT
|
|
78
|
+
),
|
|
79
|
+
"author": wx.get("author") or "",
|
|
80
|
+
"publish_time": wx.get("publish_time") or "",
|
|
81
|
+
"dispatch_handler": "wechat",
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
"url": url,
|
|
85
|
+
"title": "",
|
|
86
|
+
"content": "",
|
|
87
|
+
"html": "",
|
|
88
|
+
"length": 0,
|
|
89
|
+
"success": False,
|
|
90
|
+
"error": (wx or {}).get("error")
|
|
91
|
+
or "微信公众号文章抓取失败(HTTP/浏览器/搜狗三通道均未取到正文)",
|
|
92
|
+
"fetch_method": engine_states.FETCH_WECHAT_FAILED,
|
|
93
|
+
"dispatch_handler": "wechat",
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _dispatch_tweet(url: str, max_chars: int, timeout: float) -> dict[str, Any] | None:
|
|
98
|
+
try:
|
|
99
|
+
import tweet as tweet_mod
|
|
100
|
+
status_id, bundle = tweet_mod.build_bundle(
|
|
101
|
+
url, mode="status", timeout=max(5, int(timeout))
|
|
102
|
+
)
|
|
103
|
+
if not bundle or not bundle.get("main"):
|
|
104
|
+
return None
|
|
105
|
+
rendered = tweet_mod._render_text(status_id, "status", bundle)
|
|
106
|
+
main_post = bundle.get("main") or {}
|
|
107
|
+
author = tweet_mod._author_handle(main_post)
|
|
108
|
+
content = (rendered or "")[:max_chars]
|
|
109
|
+
if not content.strip():
|
|
110
|
+
return None
|
|
111
|
+
return {
|
|
112
|
+
"url": url,
|
|
113
|
+
"title": f"X 帖子 @{author} ({status_id})" if author else f"X 帖子 ({status_id})",
|
|
114
|
+
"content": content,
|
|
115
|
+
"html": "",
|
|
116
|
+
"length": len(content),
|
|
117
|
+
"success": True,
|
|
118
|
+
"fetch_method": engine_states.FETCH_X_ARTICLE,
|
|
119
|
+
"author": author,
|
|
120
|
+
"dispatch_handler": "tweet",
|
|
121
|
+
}
|
|
122
|
+
except Exception:
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _dispatch_paper(url: str, max_chars: int) -> dict[str, Any] | None:
|
|
127
|
+
m = _ARXIV_ABS_RE.match(url.strip())
|
|
128
|
+
if not m:
|
|
129
|
+
return None
|
|
130
|
+
arxiv_id = m.group(1)
|
|
131
|
+
try:
|
|
132
|
+
import paper as paper_mod
|
|
133
|
+
meta = paper_mod.fetch_meta(paper_mod.normalize_arxiv_id(arxiv_id))
|
|
134
|
+
if not meta or not meta.get("title"):
|
|
135
|
+
return None
|
|
136
|
+
authors = ", ".join(meta.get("authors") or [])
|
|
137
|
+
abstract = str(meta.get("abstract") or meta.get("summary") or "").strip()
|
|
138
|
+
lines = [
|
|
139
|
+
f"# {meta['title']}",
|
|
140
|
+
f"- arXiv: {meta.get('id') or meta.get('arxiv_id') or arxiv_id}",
|
|
141
|
+
]
|
|
142
|
+
if authors:
|
|
143
|
+
lines.append(f"- Authors: {authors}")
|
|
144
|
+
if meta.get("published"):
|
|
145
|
+
lines.append(f"- Published: {meta['published']}")
|
|
146
|
+
if meta.get("pdf_url"):
|
|
147
|
+
lines.append(f"- PDF: {meta['pdf_url']}")
|
|
148
|
+
if abstract:
|
|
149
|
+
lines.extend(["", "## Abstract", abstract])
|
|
150
|
+
content = "\n".join(lines)[:max_chars]
|
|
151
|
+
return {
|
|
152
|
+
"url": url,
|
|
153
|
+
"title": str(meta["title"]),
|
|
154
|
+
"content": content,
|
|
155
|
+
"html": "",
|
|
156
|
+
"length": len(content),
|
|
157
|
+
"success": True,
|
|
158
|
+
"fetch_method": engine_states.FETCH_HTTP,
|
|
159
|
+
"author": authors,
|
|
160
|
+
"publish_time": str(meta.get("published") or ""),
|
|
161
|
+
"dispatch_handler": "paper",
|
|
162
|
+
}
|
|
163
|
+
except Exception:
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _dispatch_pdf(url: str, max_chars: int) -> dict[str, Any] | None:
|
|
168
|
+
try:
|
|
169
|
+
from pdf_extract import extract_pdf
|
|
170
|
+
res = extract_pdf(url)
|
|
171
|
+
if not res:
|
|
172
|
+
return None
|
|
173
|
+
# 依赖缺失是**明确失败**,不是「交给通用链」:pdfplumber / PyMuPDF
|
|
174
|
+
# 属可选 extras,未装时 extract_pdf 返回 success=False + error。
|
|
175
|
+
# 旧实现一律 return None,于是调用方回落到通用抓取链——那条链把 PDF
|
|
176
|
+
# 二进制当 HTML 抓回、正文抽取为空,最终给出 `success=True, length=0`
|
|
177
|
+
# 的假成功(2026-11-24 实测)。这里把失败显式传回去,让 fetch_v3
|
|
178
|
+
# 至少能报「为什么没拿到」。
|
|
179
|
+
if not res.get("success"):
|
|
180
|
+
err = str(res.get("error") or "PDF 提取失败")
|
|
181
|
+
return {
|
|
182
|
+
"url": url,
|
|
183
|
+
"title": "",
|
|
184
|
+
"content": "",
|
|
185
|
+
"html": "",
|
|
186
|
+
"length": 0,
|
|
187
|
+
"success": False,
|
|
188
|
+
"error": err,
|
|
189
|
+
"fetch_method": engine_states.FETCH_PDF_FAILED,
|
|
190
|
+
"dispatch_handler": "pdf",
|
|
191
|
+
}
|
|
192
|
+
content = str(res.get("content") or "")[:max_chars]
|
|
193
|
+
if not content.strip():
|
|
194
|
+
return {
|
|
195
|
+
"url": url,
|
|
196
|
+
"title": str(res.get("title") or ""),
|
|
197
|
+
"content": "",
|
|
198
|
+
"html": "",
|
|
199
|
+
"length": 0,
|
|
200
|
+
"success": False,
|
|
201
|
+
"error": "PDF 正文为空(可能为扫描图片页,需启用 OCR)",
|
|
202
|
+
"fetch_method": engine_states.FETCH_PDF_FAILED,
|
|
203
|
+
"dispatch_handler": "pdf",
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
"url": url,
|
|
207
|
+
"title": str(res.get("title") or url.rsplit("/", 1)[-1]),
|
|
208
|
+
"content": content,
|
|
209
|
+
"html": "",
|
|
210
|
+
"length": len(content),
|
|
211
|
+
"success": True,
|
|
212
|
+
"fetch_method": engine_states.FETCH_HTTP,
|
|
213
|
+
"pages": res.get("pages"),
|
|
214
|
+
"dispatch_handler": "pdf",
|
|
215
|
+
}
|
|
216
|
+
except Exception as exc:
|
|
217
|
+
return {
|
|
218
|
+
"url": url,
|
|
219
|
+
"title": "",
|
|
220
|
+
"content": "",
|
|
221
|
+
"html": "",
|
|
222
|
+
"length": 0,
|
|
223
|
+
"success": False,
|
|
224
|
+
"error": f"PDF 提取异常: {type(exc).__name__}: {exc}",
|
|
225
|
+
"fetch_method": engine_states.FETCH_PDF_FAILED,
|
|
226
|
+
"dispatch_handler": "pdf",
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def dispatch_specialized_fetch(
|
|
231
|
+
url: str,
|
|
232
|
+
*,
|
|
233
|
+
max_chars: int = 8000,
|
|
234
|
+
timeout: float = 15.0,
|
|
235
|
+
) -> dict[str, Any] | None:
|
|
236
|
+
"""按 URL 特征自动分发至专有提取器;非专用链接或可回落失败时返回 None。"""
|
|
237
|
+
kind = classify_specialized_url(url)
|
|
238
|
+
if kind is None:
|
|
239
|
+
return None
|
|
240
|
+
if kind == "wechat":
|
|
241
|
+
return _dispatch_wechat(url, max_chars)
|
|
242
|
+
if kind == "tweet":
|
|
243
|
+
return _dispatch_tweet(url, max_chars, timeout)
|
|
244
|
+
if kind == "paper":
|
|
245
|
+
return _dispatch_paper(url, max_chars)
|
|
246
|
+
if kind == "pdf":
|
|
247
|
+
return _dispatch_pdf(url, max_chars)
|
|
248
|
+
return None
|
package/scripts/fetch_quality.py
CHANGED
|
@@ -236,3 +236,14 @@ def _compute_quality(content: str, html: str) -> float:
|
|
|
236
236
|
return quality_breakdown(content, html)["total"]
|
|
237
237
|
|
|
238
238
|
|
|
239
|
+
def guard_empty_unknown_result(result: dict[str, Any]) -> dict[str, Any]:
|
|
240
|
+
"""修正「成功但零正文 + page_type 未识别」的假成功(如二进制页被当 HTML 抓回)。"""
|
|
241
|
+
if (result.get("success") and not (result.get("content") or "").strip()
|
|
242
|
+
and result.get("page_type") in (None, "", "unknown")):
|
|
243
|
+
result["success"] = False
|
|
244
|
+
result["error"] = result.get("error") or (
|
|
245
|
+
"抓取成功但正文为空(URL 可能指向二进制文件或需专用解析器)"
|
|
246
|
+
)
|
|
247
|
+
return result
|
|
248
|
+
|
|
249
|
+
|