dknowc-dsh 2.3.1 → 2.4.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/README.md +11 -3
- package/package.json +2 -2
- package/skills/dk-trace-report/SKILL.md +288 -0
- package/skills/dk-trace-report/config/examples/doc.json +56 -0
- package/skills/dk-trace-report/config/examples/legal.json +51 -0
- package/skills/dk-trace-report/config/examples/ppt.json +47 -0
- package/skills/dk-trace-report/config/examples/tax.json +46 -0
- package/skills/dk-trace-report/config/examples/trusted.json +122 -0
- package/skills/dk-trace-report/reference/sample_input.json +60 -0
- package/skills/dk-trace-report/resources/ECHARTS-NOTICE.txt +10 -0
- package/skills/dk-trace-report/resources/echarts.custom.min.js +48 -0
- package/skills/dk-trace-report/scripts/build_report_input.py +333 -0
- package/skills/{dknowc-trusted-search → dk-trace-report}/scripts/chart_embed.py +4 -1
- package/skills/{dknowc-trusted-search → dk-trace-report}/scripts/render_trace_html.py +1071 -536
- package/skills/dk-trace-report/scripts/run_paths.py +95 -0
- package/skills/dk-trace-report/scripts/trust_digest.py +77 -0
- package/skills/dknowc-legal-assistant/README.md +95 -0
- package/skills/dknowc-legal-assistant/SKILL.md +122 -0
- package/skills/dknowc-legal-assistant/attribution.json +5 -0
- package/skills/dknowc-legal-assistant/reference/answer-and-report.md +221 -0
- package/skills/dknowc-legal-assistant/reference/capability-layer.md +143 -0
- package/skills/dknowc-legal-assistant/reference/legal_claims.example.json +96 -0
- package/skills/dknowc-legal-assistant/reference/onboarding_scripts.md +198 -0
- package/skills/dknowc-legal-assistant/reference/policy_tags.json +2002 -0
- package/skills/dknowc-legal-assistant/reference/retrieval-statute.md +81 -0
- package/skills/dknowc-legal-assistant/reference/retrieval-v2.md +133 -0
- package/skills/dknowc-legal-assistant/reference/sample_search_result.md +33 -0
- package/skills/dknowc-legal-assistant/reference/sample_trace_report.html +940 -0
- package/skills/dknowc-legal-assistant/reference/tags.md +53 -0
- package/skills/dknowc-legal-assistant/reference/troubleshooting.md +50 -0
- package/skills/dknowc-legal-assistant/reference/verification-pass.md +59 -0
- package/skills/dknowc-legal-assistant/resources/echarts.custom.min.js +48 -0
- package/skills/dknowc-legal-assistant/scripts/api_key.py +68 -0
- package/skills/dknowc-legal-assistant/scripts/attribution.py +61 -0
- package/skills/dknowc-legal-assistant/scripts/calc_legal.py +375 -0
- package/skills/dknowc-legal-assistant/scripts/calc_period.py +155 -0
- package/skills/dknowc-legal-assistant/scripts/check_answer.py +664 -0
- package/skills/dknowc-legal-assistant/scripts/initialize.py +188 -0
- package/skills/dknowc-legal-assistant/scripts/legal_client.py +208 -0
- package/skills/dknowc-legal-assistant/scripts/legal_search.py +1059 -0
- package/skills/dknowc-legal-assistant/scripts/merge_search_results.py +630 -0
- package/skills/dknowc-legal-assistant/scripts/path_guard.py +92 -0
- package/skills/dknowc-legal-assistant/scripts/question_profile.py +215 -0
- package/skills/dknowc-legal-assistant/scripts/register_key.mjs +1152 -0
- package/skills/dknowc-legal-assistant/scripts/run_eval.py +109 -0
- package/skills/dknowc-legal-assistant/scripts/source_cards.py +321 -0
- package/skills/dknowc-legal-assistant/scripts/tag_catalog.py +162 -0
- package/skills/dknowc-legal-assistant/scripts/trace_report.config.json +66 -0
- package/skills/dknowc-legal-assistant/scripts/verify_claims.py +448 -0
- package/skills/dknowc-legal-assistant/scripts/websearch_convert.py +396 -0
- package/skills/dknowc-official-doc-writer/README.md +32 -21
- package/skills/dknowc-official-doc-writer/SKILL.md +28 -15
- package/skills/dknowc-official-doc-writer/config/trace_report.config.json +56 -0
- package/skills/dknowc-official-doc-writer/official-docs/input//345/274/225/347/224/250/345/257/271/347/205/247_/351/252/214/346/224/266_materials.json +628 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/baseline_1555.json +345 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/baseline_my.json +728 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/baseline_wp.json +159 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/baseline_wp2.json +1006 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/test_tz.json +28 -0
- package/skills/dknowc-official-doc-writer/official-docs/search-results/tmp_q.json +1419 -0
- package/skills/dknowc-official-doc-writer/reference/anti_ai_patterns.md +14 -2
- package/skills/dknowc-official-doc-writer/reference/fact_discipline.md +4 -0
- package/skills/dknowc-official-doc-writer/reference/genre_writing_notes.md +4 -0
- package/skills/dknowc-official-doc-writer/reference/initialization.md +13 -8
- package/skills/dknowc-official-doc-writer/reference/local_memory_guide.md +7 -7
- package/skills/dknowc-official-doc-writer/reference/material_usage_guidance.md +21 -0
- package/skills/dknowc-official-doc-writer/reference/materials_pipeline.md +64 -7
- package/skills/dknowc-official-doc-writer/reference/onboarding_scripts.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/outline_rules.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +13 -13
- package/skills/dknowc-official-doc-writer/reference/research_notes.md +5 -3
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +6 -2
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +44 -32
- package/skills/dknowc-official-doc-writer/reference/search_policy.md +63 -36
- package/skills/dknowc-official-doc-writer/reference/standards/01_report.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/02_qingshi.md +6 -3
- package/skills/dknowc-official-doc-writer/reference/standards/03_pifu.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/04_tongzhi.md +12 -3
- package/skills/dknowc-official-doc-writer/reference/standards/05_yijian.md +8 -2
- package/skills/dknowc-official-doc-writer/reference/standards/06_han.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/08_tongbao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/09_tonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/10_gonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/11_fuhan_approve.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/12_fuhan_objection.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/13_reminder.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/14_generic.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/16_decision.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/18_order.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/19_gazette.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/20_motion.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/22_application.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/23_publicity.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/25_plan.md +2 -1
- package/skills/dknowc-official-doc-writer/reference/standards/26_summary.md +7 -1
- package/skills/dknowc-official-doc-writer/reference/standards/27_speech.md +4 -1
- package/skills/dknowc-official-doc-writer/reference/standards/99_expressions.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/task_router.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/working_principles.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/writing_method.md +166 -0
- package/skills/dknowc-official-doc-writer/reference/writing_rules.md +10 -4
- package/skills/dknowc-official-doc-writer/scripts/api_key.py +22 -2
- package/skills/dknowc-official-doc-writer/scripts/attribution.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/build_trace_json.py +205 -13
- package/skills/dknowc-official-doc-writer/scripts/check_materials.py +367 -22
- package/skills/dknowc-official-doc-writer/scripts/deep_convert.py +5 -0
- package/skills/dknowc-official-doc-writer/scripts/deep_query.py +2 -1
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +20 -7
- package/skills/dknowc-official-doc-writer/scripts/extract_reference.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +88 -30
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +232 -6
- package/skills/dknowc-official-doc-writer/scripts/local_memory.py +2 -6
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +100 -7
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +2 -1
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +265 -32
- package/skills/dknowc-official-doc-writer/scripts/review_document.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +117 -10
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/update_skill.py +339 -0
- package/skills/dknowc-official-doc-writer/scripts/websearch_convert.py +326 -0
- package/skills/dknowc-ppt-assistant/README.md +7 -9
- package/skills/dknowc-ppt-assistant/SKILL.md +28 -19
- package/skills/dknowc-ppt-assistant/config/trace_report.config.json +65 -0
- package/skills/dknowc-ppt-assistant/references/initialization.md +20 -8
- package/skills/dknowc-ppt-assistant/references/material_usage.md +46 -9
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +7 -5
- package/skills/dknowc-ppt-assistant/references/sample_trace_report.html +372 -920
- package/skills/dknowc-ppt-assistant/references/search_intro.md +2 -2
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +2 -2
- package/skills/dknowc-ppt-assistant/scripts/api_key.py +5 -28
- package/skills/dknowc-ppt-assistant/scripts/build_trace_input.py +188 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +238 -3
- package/skills/dknowc-ppt-assistant/scripts/mcp_convert.py +1 -10
- package/skills/dknowc-ppt-assistant/scripts/merge_search_results.py +604 -0
- package/skills/dknowc-ppt-assistant/scripts/preview_slide_html.py +4 -11
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +224 -63
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +29 -12
- package/skills/dknowc-ppt-assistant/scripts/update_skill.py +314 -0
- package/skills/dknowc-ppt-assistant/scripts/websearch_convert.py +326 -0
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +92 -29
- package/skills/dknowc-ppt-assistant/workflows/routing.md +1 -1
- package/skills/dknowc-tax-assistant/README.md +54 -0
- package/skills/dknowc-tax-assistant/SKILL.md +99 -0
- package/skills/dknowc-tax-assistant/attribution.json +5 -0
- package/skills/dknowc-tax-assistant/reference/answer-and-report.md +100 -0
- package/skills/dknowc-tax-assistant/reference/capability-layer.md +205 -0
- package/skills/dknowc-tax-assistant/reference/onboarding_scripts.md +68 -0
- package/skills/dknowc-tax-assistant/reference/policy_tags.json +2002 -0
- package/skills/dknowc-tax-assistant/reference/retrieval-policy.md +43 -0
- package/skills/dknowc-tax-assistant/reference/retrieval-v2.md +58 -0
- package/skills/dknowc-tax-assistant/reference/tags.md +53 -0
- package/skills/dknowc-tax-assistant/reference/tax_claims.example.json +76 -0
- package/skills/dknowc-tax-assistant/reference/trace_report.config.json +46 -0
- package/skills/dknowc-tax-assistant/reference/troubleshooting.md +70 -0
- package/skills/dknowc-tax-assistant/reference/websearch-fallback.md +59 -0
- package/skills/dknowc-tax-assistant/resources/THIRD_PARTY_NOTICES.md +9 -0
- package/skills/dknowc-tax-assistant/resources/echarts.custom.min.js +48 -0
- package/skills/dknowc-tax-assistant/scripts/api_key.py +60 -0
- package/skills/dknowc-tax-assistant/scripts/attribution.py +61 -0
- package/skills/dknowc-tax-assistant/scripts/calc_period.py +156 -0
- package/skills/dknowc-tax-assistant/scripts/calc_tax.py +179 -0
- package/skills/dknowc-tax-assistant/scripts/check_answer.py +210 -0
- package/skills/dknowc-tax-assistant/scripts/initialize.py +208 -0
- package/skills/dknowc-tax-assistant/scripts/merge_search_results.py +631 -0
- package/skills/dknowc-tax-assistant/scripts/path_guard.py +23 -0
- package/skills/dknowc-tax-assistant/scripts/question_profile.py +444 -0
- package/skills/dknowc-tax-assistant/scripts/register_key.mjs +1173 -0
- package/skills/dknowc-tax-assistant/scripts/run_dir.py +149 -0
- package/skills/dknowc-tax-assistant/scripts/source_cards.py +372 -0
- package/skills/dknowc-tax-assistant/scripts/tag_catalog.py +164 -0
- package/skills/dknowc-tax-assistant/scripts/tax_client.py +218 -0
- package/skills/dknowc-tax-assistant/scripts/tax_search.py +403 -0
- package/skills/dknowc-tax-assistant/scripts/verify_claims.py +587 -0
- package/skills/dknowc-tax-assistant/scripts/websearch_convert.py +352 -0
- package/skills/dknowc-trusted-search/README.md +25 -19
- package/skills/dknowc-trusted-search/SKILL.md +205 -73
- package/skills/dknowc-trusted-search/config/trace_report.config.json +119 -0
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +3 -3
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +286 -191
- package/skills/dknowc-trusted-search/resources/ECHARTS-NOTICE.txt +10 -0
- package/skills/dknowc-trusted-search/scripts/api_key.py +0 -1
- package/skills/dknowc-trusted-search/scripts/attribution.py +1 -1
- package/skills/dknowc-trusted-search/scripts/build_trace_input.py +173 -0
- package/skills/dknowc-trusted-search/scripts/deep_convert.py +79 -30
- package/skills/dknowc-trusted-search/scripts/deep_query.py +22 -33
- package/skills/dknowc-trusted-search/scripts/initialize.py +343 -2
- package/skills/dknowc-trusted-search/scripts/merge_search_results.py +222 -35
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +241 -29
- package/skills/dknowc-trusted-search/scripts/source_display.py +59 -0
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +67 -34
- package/skills/dknowc-trusted-search/scripts/update_skill.py +300 -0
- package/skills/dknowc-trusted-search/scripts/websearch_convert.py +328 -0
- package/src/index.js +15 -0
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +0 -2574
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +0 -2642
- package/skills/dknowc-trusted-search/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-trusted-search/official-docs/search-results/.gitkeep +0 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""深知税务助手 · 税务检索脚本。
|
|
3
|
+
|
|
4
|
+
两种模式:
|
|
5
|
+
1) 语义模式(--query,主路):可信搜索接口 V2 条款级召回;可用 --policy-labels
|
|
6
|
+
传完整路径标签圈定领域(税务族 / 社保族)。
|
|
7
|
+
2) 全文模式(--file <文件标题>,兜底):政策条件筛选定位文件 → 政策正文 → 全文下载,
|
|
8
|
+
用于核对文件全文/文号/条款上下文。税务不做"按第X条切分"——需要看条款时直接读全文。
|
|
9
|
+
|
|
10
|
+
输出为可信搜索同构 JSON(content.data.检索文章),可直接交给 merge_search_results.py
|
|
11
|
+
与 render_trace_html.py 生成溯源核验报告。
|
|
12
|
+
|
|
13
|
+
设计口径(2026-11-06 交接定稿):
|
|
14
|
+
- 可信搜索 V2 是主路,任何税务检索第一步都是 V2;全文/版本接口只在 V2 没取全、
|
|
15
|
+
需要核对全文或版本时兜底。
|
|
16
|
+
- 标签必须传完整路径;带标签返回 0 条不静默降级(记录 label_zero,需要无标签
|
|
17
|
+
结果时另发一次独立检索,两次分别落盘)。
|
|
18
|
+
- 仅覆盖中国大陆税务;社保是相邻领域、另一个标签族。
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import argparse
|
|
24
|
+
import json
|
|
25
|
+
import re
|
|
26
|
+
import sys
|
|
27
|
+
from datetime import date, datetime
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
30
|
+
from urllib.parse import urlparse
|
|
31
|
+
|
|
32
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
33
|
+
import tax_client as client
|
|
34
|
+
import run_dir
|
|
35
|
+
try:
|
|
36
|
+
import tag_catalog
|
|
37
|
+
except Exception: # 目录缺失时不影响检索
|
|
38
|
+
tag_catalog = None # type: ignore
|
|
39
|
+
|
|
40
|
+
# 全文兜底时按标题挑副本要剔除的噪音词(出现在"标题减去查询名"的剩余部分时剔除)
|
|
41
|
+
BAD_TITLE_WORDS = (
|
|
42
|
+
"征求意见稿", "草案", "解读", "问答", "宣传", "学习", "培训", "讲话",
|
|
43
|
+
"情况汇报", "征文", "审议", "说明",
|
|
44
|
+
)
|
|
45
|
+
# 全文返回的字符上限:防超长全文挤爆下游报告;截断时在产物里如实标注
|
|
46
|
+
MAX_FULL_TEXT_CHARS = 60000
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# ---------- 工具 ----------
|
|
50
|
+
|
|
51
|
+
def normalize_width(text: str) -> str:
|
|
52
|
+
out = []
|
|
53
|
+
for ch in text or "":
|
|
54
|
+
code = ord(ch)
|
|
55
|
+
if 0xFF10 <= code <= 0xFF19: # 全角数字
|
|
56
|
+
out.append(chr(code - 0xFF10 + ord("0")))
|
|
57
|
+
elif code == 0x3000:
|
|
58
|
+
out.append(" ")
|
|
59
|
+
else:
|
|
60
|
+
out.append(ch)
|
|
61
|
+
return "".join(out)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def parse_date(value: Any) -> Optional[date]:
|
|
65
|
+
if not value:
|
|
66
|
+
return None
|
|
67
|
+
text = normalize_width(str(value)).strip()
|
|
68
|
+
for fmt in ("%Y-%m-%d", "%Y年%m月%d日", "%Y/%m/%d", "%Y-%m", "%Y年%m月"):
|
|
69
|
+
try:
|
|
70
|
+
return datetime.strptime(text, fmt).date()
|
|
71
|
+
except ValueError:
|
|
72
|
+
continue
|
|
73
|
+
m = re.search(r"(\d{4})\D+(\d{1,2})\D+(\d{1,2})", text)
|
|
74
|
+
if m:
|
|
75
|
+
try:
|
|
76
|
+
return date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
|
|
77
|
+
except ValueError:
|
|
78
|
+
return None
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def format_date(value: Optional[date]) -> str:
|
|
83
|
+
return value.strftime("%Y-%m-%d") if value else ""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def norm_title(text: str) -> str:
|
|
87
|
+
return re.sub(r"[\s《》()()\[\]【】·—\-]", "", text or "")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def title_score(title: str, target: str) -> int:
|
|
91
|
+
"""标题与查询名的匹配度:完全一致 10;前缀 8;包含 5;不含 0。"""
|
|
92
|
+
t = norm_title(title)
|
|
93
|
+
if not t:
|
|
94
|
+
return 0
|
|
95
|
+
bases = [target]
|
|
96
|
+
if target.startswith("中华人民共和国"):
|
|
97
|
+
bases.append(target[len("中华人民共和国"):])
|
|
98
|
+
best = 0
|
|
99
|
+
for base in bases:
|
|
100
|
+
b = norm_title(base)
|
|
101
|
+
if not b:
|
|
102
|
+
continue
|
|
103
|
+
if t == b:
|
|
104
|
+
best = max(best, 10)
|
|
105
|
+
elif t.startswith(b):
|
|
106
|
+
best = max(best, 8)
|
|
107
|
+
elif b in t:
|
|
108
|
+
best = max(best, 5)
|
|
109
|
+
return best
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def host_of(url: str) -> str:
|
|
113
|
+
try:
|
|
114
|
+
host = urlparse(url).hostname or ""
|
|
115
|
+
except Exception:
|
|
116
|
+
host = ""
|
|
117
|
+
return host
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def snippet(text: str, limit: int = 220) -> str:
|
|
121
|
+
text = re.sub(r"\s+", " ", text or "").strip()
|
|
122
|
+
return text[:limit]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
# ---------- 全文兜底 ----------
|
|
126
|
+
|
|
127
|
+
def pick_copy(target: str, candidates: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
128
|
+
"""按标题匹配度 + 状态 + 成文日期挑副本。
|
|
129
|
+
|
|
130
|
+
税务文件多为公告/通知,正文不带修正日标记,不做法律场景的"修正日标记校验";
|
|
131
|
+
validityStatus 只作排序参考(库内效力状态有错标先例,不据此下结论)。
|
|
132
|
+
"""
|
|
133
|
+
scored: List[Tuple[float, Dict[str, Any]]] = []
|
|
134
|
+
for c in candidates:
|
|
135
|
+
ts = title_score(str(c.get("title") or ""), target)
|
|
136
|
+
if ts == 0:
|
|
137
|
+
continue
|
|
138
|
+
extra = norm_title(str(c.get("title") or "")).replace(norm_title(target), "")
|
|
139
|
+
if any(w in extra for w in BAD_TITLE_WORDS):
|
|
140
|
+
continue
|
|
141
|
+
status = str(c.get("validityStatus") or "")
|
|
142
|
+
written = parse_date(c.get("writtenDate"))
|
|
143
|
+
length = int(c.get("contentLength") or 0)
|
|
144
|
+
score = ts * 10 + min(length / 10000.0, 5.0)
|
|
145
|
+
if status == "EXPIRED_OR_REPEALED":
|
|
146
|
+
score -= 8
|
|
147
|
+
if written:
|
|
148
|
+
score += min((written - date(2000, 1, 1)).days / 365.0, 5.0) # 新文件微加分
|
|
149
|
+
scored.append((score, c))
|
|
150
|
+
if not scored:
|
|
151
|
+
raise client.TaxApiError("resolve", f"「{target}」的候选均被标题规则排除")
|
|
152
|
+
scored.sort(key=lambda x: -x[0])
|
|
153
|
+
return scored[0][1]
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def build_fulltext_article(target: str, cand: Dict[str, Any], text: str, versions: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
157
|
+
truncated = len(text) > MAX_FULL_TEXT_CHARS
|
|
158
|
+
body_text = text[:MAX_FULL_TEXT_CHARS] if truncated else text
|
|
159
|
+
source_url = str(cand.get("sourceUrl") or "")
|
|
160
|
+
written = parse_date(cand.get("writtenDate"))
|
|
161
|
+
return {
|
|
162
|
+
"文章Id": str(cand.get("encryptedFileId") or ""),
|
|
163
|
+
"文章类型": "policy",
|
|
164
|
+
"文章标题": str(cand.get("title") or target),
|
|
165
|
+
"发布或实施机构": str(cand.get("issuingAuthority") or ""),
|
|
166
|
+
"数据源": host_of(source_url) or "深知政策文件库",
|
|
167
|
+
"源网址": source_url,
|
|
168
|
+
"发布日期": format_date(written),
|
|
169
|
+
"文号": str(cand.get("documentNumber") or ""),
|
|
170
|
+
"生效日期": str(cand.get("effectiveDate") or ""),
|
|
171
|
+
"文件标签": cand.get("policyTopicTags") or [],
|
|
172
|
+
"版本状态": str(cand.get("validityStatus") or ""),
|
|
173
|
+
"历次版本数": len(versions),
|
|
174
|
+
"核验": ("原文取自全文下载" + (";超长已截断,完整全文请再跑 --file" if truncated else "")),
|
|
175
|
+
"检索通道": "深知税务检索·全文",
|
|
176
|
+
"检索类型": "tax-fulltext",
|
|
177
|
+
"段落": [{"id": 1, "标题": "全文", "内容": body_text, "匹配等级": "原文"}],
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def run_fulltext(target: str, max_chars: Optional[int] = None) -> Dict[str, Any]:
|
|
182
|
+
"""政策条件筛选 → 选副本 → 历次版本(尽力) → 政策正文 → 全文下载。"""
|
|
183
|
+
global MAX_FULL_TEXT_CHARS
|
|
184
|
+
if max_chars:
|
|
185
|
+
MAX_FULL_TEXT_CHARS = max_chars
|
|
186
|
+
# 条件筛选的标题匹配对长标题偏严:全名 → 去"关于"前缀 → 去"中华人民共和国"前缀逐步放宽
|
|
187
|
+
variants: List[str] = [target]
|
|
188
|
+
if "关于" in target:
|
|
189
|
+
variants.append(target.split("关于", 1)[1])
|
|
190
|
+
if target.startswith("中华人民共和国"):
|
|
191
|
+
variants.append(target[len("中华人民共和国"):])
|
|
192
|
+
candidates: Dict[str, Dict[str, Any]] = {}
|
|
193
|
+
for variant in variants:
|
|
194
|
+
if not variant.strip():
|
|
195
|
+
continue
|
|
196
|
+
try:
|
|
197
|
+
items = client.filter_policies(variant.strip(), limit=30)
|
|
198
|
+
except client.TaxApiError:
|
|
199
|
+
continue
|
|
200
|
+
for item in items:
|
|
201
|
+
fid = str(item.get("encryptedFileId") or "")
|
|
202
|
+
if fid and fid not in candidates:
|
|
203
|
+
candidates[fid] = item
|
|
204
|
+
if not candidates:
|
|
205
|
+
raise client.TaxApiError("resolve", f"政策条件筛选未找到标题包含「{target}」的文件")
|
|
206
|
+
cand = pick_copy(target, list(candidates.values()))
|
|
207
|
+
versions: List[Dict[str, Any]] = []
|
|
208
|
+
try:
|
|
209
|
+
versions = client.query_versions(str(cand.get("encryptedFileId") or "")).get("versions") or []
|
|
210
|
+
except client.TaxApiError:
|
|
211
|
+
versions = []
|
|
212
|
+
content, text = client.get_full_text(str(cand.get("encryptedFileId") or ""))
|
|
213
|
+
if not text.strip():
|
|
214
|
+
raise client.TaxApiError("download", f"「{target}」未取到可下载正文")
|
|
215
|
+
article = build_fulltext_article(target, cand, text, versions)
|
|
216
|
+
meta = {
|
|
217
|
+
"mode": "fulltext",
|
|
218
|
+
"file": target,
|
|
219
|
+
"selected_file_id": str(cand.get("encryptedFileId") or ""),
|
|
220
|
+
"selected_title": str(cand.get("title") or ""),
|
|
221
|
+
"selected_status": str(cand.get("validityStatus") or ""),
|
|
222
|
+
"versions_count": len(versions),
|
|
223
|
+
"truncated": len(text) > MAX_FULL_TEXT_CHARS,
|
|
224
|
+
}
|
|
225
|
+
return output_body(target, date.today().isoformat(), [article], meta)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
# ---------- V2 语义主路 ----------
|
|
229
|
+
|
|
230
|
+
def build_semantic_articles(body: Dict[str, Any], search_channel: str = "") -> List[Dict[str, Any]]:
|
|
231
|
+
data = (body.get("content") or {}).get("data") or {}
|
|
232
|
+
articles = [x for x in (data.get("检索文章") or []) if isinstance(x, dict)]
|
|
233
|
+
for article in articles:
|
|
234
|
+
# 与深知可信搜索 1.7.2 完全一致:只有 webSearch 通道标"·联网"并补非官方来源;
|
|
235
|
+
# govSearch/wxSearch 不打该标(渲染层仍按 数据权威性=非权威 双口径兜底)。
|
|
236
|
+
article.setdefault("检索通道", "深知税务检索·联网" if search_channel == "webSearch" else "深知税务检索·可信")
|
|
237
|
+
article.setdefault("检索类型", "tax-semantic")
|
|
238
|
+
if search_channel == "webSearch":
|
|
239
|
+
article["非官方来源"] = True
|
|
240
|
+
# 外部联网材料统一降权:不依赖接口 dataAuthority 的返回值(可能仍为“权威”)。
|
|
241
|
+
# 来源性质由 source_cards 按发布机构/域名推断,原始材料不伪造。
|
|
242
|
+
article["数据权威性"] = "非权威"
|
|
243
|
+
# V2 文号键名是「发文字号」,统一补一份「文号」供下游渲染/核对使用
|
|
244
|
+
if article.get("发文字号") and not article.get("文号"):
|
|
245
|
+
article["文号"] = article["发文字号"]
|
|
246
|
+
# V2 的段落字段已是原文原段;统一匹配等级文案,避免渲染层把它当低置信段落
|
|
247
|
+
for para in article.get("段落") or []:
|
|
248
|
+
if isinstance(para, dict) and not para.get("匹配等级"):
|
|
249
|
+
para["匹配等级"] = "原文"
|
|
250
|
+
return articles
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def output_body(query: str, ref: str, articles: List[Dict[str, Any]], tax_meta: Dict[str, Any]) -> Dict[str, Any]:
|
|
254
|
+
return {
|
|
255
|
+
"tid": tax_meta.get("tid") or "",
|
|
256
|
+
"ret": 0,
|
|
257
|
+
"content": {
|
|
258
|
+
"code": 200,
|
|
259
|
+
"msg": "success",
|
|
260
|
+
"data": {
|
|
261
|
+
"用户问题": query,
|
|
262
|
+
"咨询日期": ref,
|
|
263
|
+
"办理地域": "中国",
|
|
264
|
+
"文章数量": len(articles),
|
|
265
|
+
"检索文章": articles,
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
"_tax": tax_meta,
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def resolve_output_path(args: argparse.Namespace) -> Path:
|
|
273
|
+
"""产物落盘位置(2026-11-20 运行目录口径):
|
|
274
|
+
--out 新建运行目录 / --run 使用已有运行目录 / 都不传则在当前工作目录 runs/ 下新建;
|
|
275
|
+
拒绝写入 Skill 安装目录(含 official-docs/),不覆盖已有运行结果。
|
|
276
|
+
"""
|
|
277
|
+
try:
|
|
278
|
+
if getattr(args, "out", "") and getattr(args, "run", ""):
|
|
279
|
+
raise ValueError("--out 与 --run 只能二选一(新建运行目录 / 使用已有运行目录)")
|
|
280
|
+
if getattr(args, "out", ""):
|
|
281
|
+
run = run_dir.new_run_dir(args.out, "--out")
|
|
282
|
+
elif getattr(args, "run", ""):
|
|
283
|
+
run = run_dir.open_run_dir(args.run, "--run")
|
|
284
|
+
else:
|
|
285
|
+
run = run_dir.auto_run_dir()
|
|
286
|
+
except ValueError as exc:
|
|
287
|
+
raise client.TaxApiError("output", str(exc)) from None
|
|
288
|
+
name = args.output or ("tax_fulltext.json" if args.file else "tax_search.json")
|
|
289
|
+
try:
|
|
290
|
+
target = run_dir.out_file(run, name)
|
|
291
|
+
except ValueError as exc:
|
|
292
|
+
raise client.TaxApiError("output", str(exc)) from None
|
|
293
|
+
return target
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def run(args: argparse.Namespace) -> Dict[str, Any]:
|
|
297
|
+
ref_date = date.today().isoformat()
|
|
298
|
+
|
|
299
|
+
if args.file:
|
|
300
|
+
return run_fulltext(args.file.strip(), max_chars=args.max_chars)
|
|
301
|
+
|
|
302
|
+
query = (args.query or "").strip()
|
|
303
|
+
if not query:
|
|
304
|
+
raise client.TaxApiError("input", "必须提供 --query(V2 主路),或 --file 走全文兜底")
|
|
305
|
+
|
|
306
|
+
labels = [x.strip() for x in re.split(r"[,,]", args.policy_labels or "") if x.strip()]
|
|
307
|
+
# 标签目录:允许传短名/叶子名,自动解析成接口要求的完整路径
|
|
308
|
+
if labels and tag_catalog:
|
|
309
|
+
try:
|
|
310
|
+
labels = tag_catalog.resolve_many(labels)
|
|
311
|
+
except Exception:
|
|
312
|
+
pass
|
|
313
|
+
# 阶段二(可信联网)不传标签:联网网页不带库内标签,传了会把结果全部过滤掉
|
|
314
|
+
if args.search_channel and labels:
|
|
315
|
+
print("阶段二(联网检索)不使用 policyLabels:联网网页不携带库内标签,已忽略标签参数",
|
|
316
|
+
file=sys.stderr)
|
|
317
|
+
labels = []
|
|
318
|
+
body = client.semantic_search(
|
|
319
|
+
query,
|
|
320
|
+
limit_articles=args.limit_articles,
|
|
321
|
+
segment_count=args.segment_count,
|
|
322
|
+
service_area=(args.service_area or None),
|
|
323
|
+
policy_labels=labels or None,
|
|
324
|
+
search_channel=(args.search_channel or None),
|
|
325
|
+
)
|
|
326
|
+
if body.get("_tax_label_zero"):
|
|
327
|
+
print(f"检索结果:带标签 {labels} 返回 0 条(按契约不作静默降级;如需无标签结果请另发一次检索)",
|
|
328
|
+
file=sys.stderr)
|
|
329
|
+
articles = build_semantic_articles(body, args.search_channel or "")
|
|
330
|
+
# 校验返回的文件标签是否都在目录里(目录外标签说明数据侧有一致性问题)
|
|
331
|
+
tag_unknown = []
|
|
332
|
+
if tag_catalog:
|
|
333
|
+
try:
|
|
334
|
+
tag_unknown = tag_catalog.Catalog.load().unknown_in_articles(articles)
|
|
335
|
+
if tag_unknown:
|
|
336
|
+
print(f"标签校验:{len(tag_unknown)} 个返回标签不在目录中,例如 "
|
|
337
|
+
f"{tag_unknown[0]['文件标签']}", file=sys.stderr)
|
|
338
|
+
except Exception:
|
|
339
|
+
pass
|
|
340
|
+
meta = {
|
|
341
|
+
"mode": "semantic",
|
|
342
|
+
"query": query,
|
|
343
|
+
"policy_labels": labels,
|
|
344
|
+
"service_area": args.service_area or "",
|
|
345
|
+
"search_channel": args.search_channel or "",
|
|
346
|
+
"tid": body.get("tid") or "",
|
|
347
|
+
"article_count": len(articles),
|
|
348
|
+
"note": ("阶段二:可信联网检索(外部网页,非官方来源)" if args.search_channel
|
|
349
|
+
else "V2 条款级召回是主路;需要核对全文/文号/版本时再用 --file 兜底"),
|
|
350
|
+
"semantic_params": ((body.get("content") or {}).get("data") or {}).get("用户问题"),
|
|
351
|
+
"tag_unknown": tag_unknown,
|
|
352
|
+
"label_zero": bool(body.get("_tax_label_zero")),
|
|
353
|
+
}
|
|
354
|
+
return output_body(query, ref_date, articles, meta)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def main() -> None:
|
|
358
|
+
parser = argparse.ArgumentParser(description="深知税务助手 · 税务检索(可信搜索 V2 主路 + 全文兜底)")
|
|
359
|
+
parser.add_argument("--query", help="检索词:一税种/一事项一条聚焦短查询(主路)")
|
|
360
|
+
parser.add_argument("--file", help="文件标题:全文兜底(政策条件筛选→正文→全文下载),不切条")
|
|
361
|
+
parser.add_argument("--policy-labels", default="", help="标签过滤(完整路径,逗号分隔)。税务族=政策/主题标签/财政、金融、审计/税务;社保族=政策/主题标签/劳动、人事、监察/社会保障;可传短名自动解析")
|
|
362
|
+
parser.add_argument("--service-area", default="", help="地域(如 北京市);注意核对结果是否真的限定在该地")
|
|
363
|
+
parser.add_argument("--search-channel", default="", choices=["", "webSearch", "govSearch", "wxSearch"],
|
|
364
|
+
help="阶段二联网检索:webSearch=互联网网页(非官方来源,自动打标);govSearch=政府站定向;wxSearch=微信生态。不传=只查库内")
|
|
365
|
+
parser.add_argument("--limit-articles", type=int, default=10, help="最多返回材料数(默认 10)")
|
|
366
|
+
parser.add_argument("--segment-count", type=int, default=8, help="返回段落数(默认 8)")
|
|
367
|
+
parser.add_argument("--max-chars", type=int, default=60000, help="全文模式的字符上限(默认 60000,截断会如实标注)")
|
|
368
|
+
parser.add_argument("--out", help="新建运行目录(会话工作区,如 ./runs/20261120-B01-<时间>);已有结果的目录会被拒绝")
|
|
369
|
+
parser.add_argument("--run", help="使用已存在的运行目录(补查/续跑时用);目录内已有同名产物不会被覆盖")
|
|
370
|
+
parser.add_argument("--output", "-o", help="结果 JSON 文件名(配合 --out/--run 放进运行目录)或显式输出路径(不得指向 Skill 安装目录)")
|
|
371
|
+
parser.add_argument("--json-only", action="store_true", help="仅输出 JSON")
|
|
372
|
+
parser.add_argument("--timeout", type=int, default=90, help="单次请求超时秒数(默认 90)")
|
|
373
|
+
args = parser.parse_args()
|
|
374
|
+
|
|
375
|
+
try:
|
|
376
|
+
body = run(args)
|
|
377
|
+
except client.TaxApiError as exc:
|
|
378
|
+
print(json.dumps({
|
|
379
|
+
"status": "error",
|
|
380
|
+
"stage": exc.stage,
|
|
381
|
+
"message": str(exc),
|
|
382
|
+
"status_code": exc.status_code,
|
|
383
|
+
}, ensure_ascii=False, indent=2), file=sys.stderr)
|
|
384
|
+
sys.exit(1)
|
|
385
|
+
|
|
386
|
+
path = resolve_output_path(args)
|
|
387
|
+
body.setdefault("_tax", {})["run_dir"] = str(path.parent)
|
|
388
|
+
raw = json.dumps(body, ensure_ascii=False, indent=2)
|
|
389
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
390
|
+
path.write_text(raw, encoding="utf-8")
|
|
391
|
+
print(f"已保存税务检索 JSON:{path}")
|
|
392
|
+
print(f"运行目录:{path.parent}")
|
|
393
|
+
if not args.json_only:
|
|
394
|
+
data = (body.get("content") or {}).get("data") or {}
|
|
395
|
+
print(f"模式:{(body.get('_tax') or {}).get('mode')}|材料:{data.get('文章数量')} 条")
|
|
396
|
+
for idx, article in enumerate(data.get("检索文章") or [], start=1):
|
|
397
|
+
print(f"{idx}. {article.get('文章标题')}|{article.get('文号') or article.get('数据源')}|{article.get('发布日期')}")
|
|
398
|
+
for para in (article.get("段落") or [])[:1]:
|
|
399
|
+
print(" " + snippet(str(para.get("内容") or ""), 160))
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
if __name__ == "__main__":
|
|
403
|
+
main()
|