dknowc-dsh 2.3.1 → 2.4.1
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/cordis.patch.yml +1 -1
- package/package.json +7 -4
- 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/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 +24 -25
- 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 +18 -2
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +0 -2574
- package/skills/dknowc-ppt-assistant/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/official-docs/search-results/.gitkeep +0 -0
- 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,188 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""深知法律助手 public 版初始化检查。
|
|
3
|
+
|
|
4
|
+
API Key 读取:优先进程环境变量 DKNOWC_API_KEY,缺失时从本机专用配置文件
|
|
5
|
+
~/.config/dknowc/api_key 读取(不读取、不改写任何 shell 配置文件)
|
|
6
|
+
(宿主进程早于 key 写入启动、或宿主安全更新后不再加载 shell 导出变量时不误报缺失)。
|
|
7
|
+
注册脚本 register_key.mjs 注册成功后自动把 Key 写入专用配置文件(不写入、不改写任何 shell 配置文件),
|
|
8
|
+
本检查直读该文件,无需重启宿主。不读取、不写入本地 config.ini 中的 Key。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import shutil
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
API_KEY_ENV = "DKNOWC_API_KEY"
|
|
20
|
+
MAAS_PLATFORM_URL = "https://platform.dknowc.cn/auth/#/login"
|
|
21
|
+
PLACEHOLDER_KEYS = {"", "your_api_key_here", "你的深知法律助手 API Key", "你的深知可信搜索 API Key", "你的深知搜索 API Key"}
|
|
22
|
+
|
|
23
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _looks_like_key(value: str) -> bool:
|
|
27
|
+
value = (value or "").strip()
|
|
28
|
+
return value not in PLACEHOLDER_KEYS and value.startswith("sk-")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _in_dsh() -> bool:
|
|
32
|
+
"""dsh 环境标志:dsh 的 bash 工具会注入 DSH_SHELL=1。"""
|
|
33
|
+
return os.environ.get("DSH_SHELL") == "1"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def check_api_key_config():
|
|
37
|
+
if _in_dsh():
|
|
38
|
+
# dsh:优先 DSH_DKNOWC_API_KEY(与 MCP Bearer 同源);缺失时 resolve_api_key 兜底
|
|
39
|
+
# (环境变量 → 专用配置文件)。配置文件读取不受 dsh 环境清理影响,
|
|
40
|
+
# 注册后本会话脚本立即可用。无 Key 仅暂停检索,不阻断纯生成/计算类任务。
|
|
41
|
+
api_key = os.environ.get("DSH_DKNOWC_API_KEY", "").strip()
|
|
42
|
+
source = "environment"
|
|
43
|
+
if not _looks_like_key(api_key):
|
|
44
|
+
try:
|
|
45
|
+
from api_key import resolve_api_key as _rk
|
|
46
|
+
api_key2, source2 = _rk()
|
|
47
|
+
if _looks_like_key(api_key2):
|
|
48
|
+
api_key, source = api_key2, source2
|
|
49
|
+
except ImportError:
|
|
50
|
+
pass
|
|
51
|
+
if _looks_like_key(api_key):
|
|
52
|
+
return {"api_key_configured": True, "api_key_source": source, "api_key_hint": None}
|
|
53
|
+
return {"api_key_configured": False, "api_key_source": None,
|
|
54
|
+
"api_key_hint": f"未检测到可用的 {API_KEY_ENV}(环境变量与专用配置文件均未找到)。运行注册脚本开通后 Key 自动写入 ~/.config/dknowc/api_key,本会话立即可用。"}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# 环境变量优先,缺失时从专用配置文件读取(不涉及 shell 配置文件)
|
|
58
|
+
api_key = ""
|
|
59
|
+
source = ""
|
|
60
|
+
key_mode = None
|
|
61
|
+
try:
|
|
62
|
+
from api_key import resolve_api_key, key_file_mode
|
|
63
|
+
api_key, source = resolve_api_key()
|
|
64
|
+
key_mode = key_file_mode()
|
|
65
|
+
except ImportError:
|
|
66
|
+
api_key = os.environ.get(API_KEY_ENV, "").strip()
|
|
67
|
+
source = "environment" if api_key else ""
|
|
68
|
+
permission_warning = None
|
|
69
|
+
if source != "environment" and key_mode is not None and (key_mode & 0o077):
|
|
70
|
+
permission_warning = (
|
|
71
|
+
f"Key 文件权限过宽({oct(key_mode)});建议执行 chmod 600 "
|
|
72
|
+
f"{api_key_file_path_display()},避免同机其他用户读取。"
|
|
73
|
+
)
|
|
74
|
+
base = {
|
|
75
|
+
"api_key_env": API_KEY_ENV,
|
|
76
|
+
"api_key_source": source or None,
|
|
77
|
+
"key_file_mode": oct(key_mode) if key_mode is not None else None,
|
|
78
|
+
"key_file_permission_warning": permission_warning,
|
|
79
|
+
"key_validated": False,
|
|
80
|
+
"key_validation_error": None,
|
|
81
|
+
"key_validation_note": "默认做一次轻量检索校验;--no-verify-key 可跳过。Key 失效时按开通引导重新获取。",
|
|
82
|
+
}
|
|
83
|
+
if _looks_like_key(api_key):
|
|
84
|
+
return {
|
|
85
|
+
**base,
|
|
86
|
+
"api_key_configured": True,
|
|
87
|
+
"api_key_hint": None,
|
|
88
|
+
"search_ready": True,
|
|
89
|
+
"search_note": None,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
**base,
|
|
94
|
+
"api_key_configured": False,
|
|
95
|
+
"api_key_source": None,
|
|
96
|
+
"api_key_hint": f"本 Skill 需要通过 {API_KEY_ENV} 连接深知可信智能服务以获取可信内容。当前未检测到可用 Key,请先注册或登录深知智能 MaaS 账号并获取 API Key。",
|
|
97
|
+
"search_ready": False,
|
|
98
|
+
"search_note": f"当前缺少 {API_KEY_ENV},暂时不能查询法律法规、司法解释、政策依据和可信溯源内容。",
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def api_key_file_path_display() -> str:
|
|
103
|
+
try:
|
|
104
|
+
from api_key import api_key_file_path
|
|
105
|
+
return str(api_key_file_path())
|
|
106
|
+
except Exception:
|
|
107
|
+
return "~/.config/dknowc/api_key"
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _node_status() -> dict:
|
|
111
|
+
node = shutil.which("node")
|
|
112
|
+
if not node:
|
|
113
|
+
return {"node_available": False, "node_version": None, "node_ok": False}
|
|
114
|
+
try:
|
|
115
|
+
out = subprocess.check_output([node, "--version"], text=True, timeout=5).strip()
|
|
116
|
+
major = int(out.lstrip("v").split(".")[0])
|
|
117
|
+
return {"node_available": True, "node_version": out, "node_ok": major >= 18}
|
|
118
|
+
except Exception:
|
|
119
|
+
return {"node_available": True, "node_version": None, "node_ok": False}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _verify_key() -> tuple:
|
|
123
|
+
try:
|
|
124
|
+
from legal_client import filter_policies
|
|
125
|
+
# 初始化校验必须“轻量”:最多等 8 秒,避免网络黑洞把每次会话启动拖到 60 秒。
|
|
126
|
+
filter_policies("中华人民共和国劳动合同法", limit=1, timeout=8)
|
|
127
|
+
return True, None, None
|
|
128
|
+
except Exception as exc:
|
|
129
|
+
status = getattr(exc, "status_code", None)
|
|
130
|
+
if status in (401, 403):
|
|
131
|
+
return False, f"Key 校验失败(HTTP {status})", status
|
|
132
|
+
return False, f"Key 校验请求未完成:{str(exc)[:160]}", status
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def main():
|
|
136
|
+
status = check_api_key_config()
|
|
137
|
+
blocking_issues = []
|
|
138
|
+
if not status["api_key_configured"]:
|
|
139
|
+
blocking_issues.append("api_key_missing")
|
|
140
|
+
|
|
141
|
+
# 默认做一次轻量联网校验;--no-verify-key 可跳过(离线/快速检查时用)。
|
|
142
|
+
if status["api_key_configured"] and "--no-verify-key" not in sys.argv[1:]:
|
|
143
|
+
validated, error, status_code = _verify_key()
|
|
144
|
+
status["key_validated"] = validated
|
|
145
|
+
status["key_validation_error"] = error
|
|
146
|
+
if not validated:
|
|
147
|
+
if status_code in (401, 403):
|
|
148
|
+
status["search_ready"] = False
|
|
149
|
+
blocking_issues.append("api_key_invalid")
|
|
150
|
+
else:
|
|
151
|
+
status["search_note"] = f"{error};首次检索可能失败,联网后请重新运行 initialize.py。"
|
|
152
|
+
|
|
153
|
+
# node 运行时只影响注册链路(register_key.mjs),检索本身用 python3 即可,不作为阻断项。
|
|
154
|
+
node_status = _node_status()
|
|
155
|
+
register_script_present = (Path(__file__).resolve().parent / "register_key.mjs").is_file()
|
|
156
|
+
|
|
157
|
+
print(json.dumps({
|
|
158
|
+
**status,
|
|
159
|
+
"python_executable": sys.executable,
|
|
160
|
+
"node_available": node_status["node_available"],
|
|
161
|
+
"node_version": node_status["node_version"],
|
|
162
|
+
"node_ok": node_status["node_ok"],
|
|
163
|
+
"register_script_present": register_script_present,
|
|
164
|
+
"registration_ready": node_status["node_ok"] and register_script_present,
|
|
165
|
+
"blocking_issues": blocking_issues,
|
|
166
|
+
"ready": not blocking_issues,
|
|
167
|
+
# guide_message:需要检索但 Key 未配置/未通过校验时给用户的引导话术(S1 三段式:价值 / 开通方式 / 退路+样例钩子)。
|
|
168
|
+
# Agent 在检索方向确认后向用户转述本话术(可结合任务补充上下文),要素不得删改。
|
|
169
|
+
"guide_message": None if status["api_key_configured"] and "api_key_invalid" not in blocking_issues else (
|
|
170
|
+
"这个问题涉及法律条文和具体规则——普通搜索结果来源杂、无法核验,凭印象答法条和条号,错了会直接影响判断和决策。"
|
|
171
|
+
"开通权威检索后,每条法律、条文都带原文出处、可点开核验,还会附一份可点击的溯源报告。\n"
|
|
172
|
+
"开通是免费的:注册即赠送 10 万积分,完成实名认证再送 10 万积分。"
|
|
173
|
+
"只需手机号收一次验证码——约 10 秒,不用自己填注册表,剩下的我来办;手机号仅用于本次验证,不会有营销骚扰。\n"
|
|
174
|
+
"也可以先不开通:我先基于已有知识给你一版初步回答,涉及政策口径、数字的地方逐条标注\"依据待核验\","
|
|
175
|
+
"并说明未联网检索、口径可能过期。想先看看开通后检索结果和溯源报告长什么样,我可以发你示例看看。"
|
|
176
|
+
),
|
|
177
|
+
# env_message:注册所需运行环境缺失时给用户的统一话术——不暴露组件名(node 对用户无意义);
|
|
178
|
+
# 就绪时不输出任何环境话题。完整场景话术见 reference/onboarding_scripts.md S6。
|
|
179
|
+
"env_message": (
|
|
180
|
+
"检测到本机开通检索还差一个小程序环境(约 1 分钟,只装一次),我现在装好可以吗?"
|
|
181
|
+
if not node_status["node_ok"] or not register_script_present else None
|
|
182
|
+
),
|
|
183
|
+
"maas_platform_url": MAAS_PLATFORM_URL,
|
|
184
|
+
}, ensure_ascii=False, indent=2))
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
if __name__ == "__main__":
|
|
188
|
+
main()
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""深知法律助手 · 接口客户端。
|
|
3
|
+
|
|
4
|
+
覆盖法律场景需要的 MaaS 接口:
|
|
5
|
+
- 001 政策条件筛选 /policy-data/v1/policies/filter
|
|
6
|
+
- 002 政策正文查询 /policy-data/v1/policy-contents/get
|
|
7
|
+
- 003 政策历次版本 /policy-data/v1/policy-versions/query
|
|
8
|
+
- 005 正文下载 /policy-data/v1/policy-contents/download
|
|
9
|
+
- 012 可信搜索 V2 /open-api/maas/dependable/v2/search
|
|
10
|
+
|
|
11
|
+
统一鉴权:请求头 api-key。Key 解析优先 DKNOWC_API_KEY,其次 ~/.config/dknowc/api_key。
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
import urllib.error
|
|
20
|
+
import urllib.request
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
23
|
+
from urllib.parse import urlparse
|
|
24
|
+
|
|
25
|
+
BASE = "https://open.dknowc.cn/open-api/maas"
|
|
26
|
+
SEARCH_ENDPOINT = BASE + "/dependable/v2/search"
|
|
27
|
+
POLICY_FILTER = BASE + "/policy-data/v1/policies/filter"
|
|
28
|
+
POLICY_CONTENT = BASE + "/policy-data/v1/policy-contents/get"
|
|
29
|
+
POLICY_VERSIONS = BASE + "/policy-data/v1/policy-versions/query"
|
|
30
|
+
POLICY_DOWNLOAD = BASE + "/policy-data/v1/policy-contents/download"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class LegalApiError(RuntimeError):
|
|
34
|
+
def __init__(self, stage: str, message: str, status_code: Optional[int] = None, body: Any = None):
|
|
35
|
+
super().__init__(message)
|
|
36
|
+
self.stage = stage
|
|
37
|
+
self.status_code = status_code
|
|
38
|
+
self.body = body
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def resolve_key() -> str:
|
|
42
|
+
key = os.environ.get("DKNOWC_API_KEY", "").strip()
|
|
43
|
+
if key:
|
|
44
|
+
return key
|
|
45
|
+
try:
|
|
46
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
47
|
+
from api_key import resolve_api_key # type: ignore
|
|
48
|
+
key, _source = resolve_api_key()
|
|
49
|
+
if key:
|
|
50
|
+
return key
|
|
51
|
+
except Exception:
|
|
52
|
+
pass
|
|
53
|
+
raise LegalApiError("auth", "缺少 DKNOWC_API_KEY;请配置环境变量或 ~/.config/dknowc/api_key", None)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _headers(api_key: str) -> Dict[str, str]:
|
|
57
|
+
headers = {"api-key": api_key, "Content-Type": "application/json"}
|
|
58
|
+
try:
|
|
59
|
+
from attribution import build_attribution_header, ATTRIBUTION_HEADER # type: ignore
|
|
60
|
+
attr = build_attribution_header()
|
|
61
|
+
if attr:
|
|
62
|
+
headers[ATTRIBUTION_HEADER] = attr
|
|
63
|
+
except Exception:
|
|
64
|
+
pass
|
|
65
|
+
return headers
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _post(url: str, payload: Dict[str, Any], timeout: int = 60, key: Optional[str] = None) -> Dict[str, Any]:
|
|
69
|
+
api_key = key or resolve_key()
|
|
70
|
+
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
71
|
+
req = urllib.request.Request(url, data=data, headers=_headers(api_key))
|
|
72
|
+
try:
|
|
73
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
74
|
+
raw = resp.read().decode("utf-8", "ignore")
|
|
75
|
+
except urllib.error.HTTPError as exc:
|
|
76
|
+
detail = exc.read().decode("utf-8", "ignore")[:500]
|
|
77
|
+
raise LegalApiError("http", f"HTTP {exc.code}: {detail}", exc.code, detail)
|
|
78
|
+
except urllib.error.URLError as exc:
|
|
79
|
+
raise LegalApiError("network", f"网络错误: {exc.reason}")
|
|
80
|
+
try:
|
|
81
|
+
return json.loads(raw)
|
|
82
|
+
except json.JSONDecodeError:
|
|
83
|
+
raise LegalApiError("parse", f"返回非 JSON: {raw[:200]}")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def filter_policies(
|
|
87
|
+
title: str,
|
|
88
|
+
limit: int = 20,
|
|
89
|
+
filters: Optional[Dict[str, Any]] = None,
|
|
90
|
+
timeout: int = 60,
|
|
91
|
+
) -> List[Dict[str, Any]]:
|
|
92
|
+
payload: Dict[str, Any] = {
|
|
93
|
+
"filters": {"title": title},
|
|
94
|
+
"searchType": ["policy"],
|
|
95
|
+
"limit": limit,
|
|
96
|
+
}
|
|
97
|
+
if filters:
|
|
98
|
+
payload["filters"].update(filters)
|
|
99
|
+
body = _post(POLICY_FILTER, payload, timeout=timeout)
|
|
100
|
+
if body.get("ret") != 1 or (body.get("code") or 0) != 200:
|
|
101
|
+
raise LegalApiError("policy-filter", body.get("message") or f"ret={body.get('ret')} code={body.get('code')}", None, body)
|
|
102
|
+
content = body.get("content") or {}
|
|
103
|
+
return [x for x in (content.get("policies") or []) if isinstance(x, dict)]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def query_versions(file_id: str) -> Dict[str, Any]:
|
|
107
|
+
body = _post(POLICY_VERSIONS, {"encryptedFileId": file_id, "searchType": "policy"})
|
|
108
|
+
if body.get("ret") != 1 or (body.get("code") or 0) != 200:
|
|
109
|
+
raise LegalApiError("policy-versions", body.get("message") or f"ret={body.get('ret')} code={body.get('code')}", None, body)
|
|
110
|
+
return body.get("content") or {}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def get_content(file_id: str, version_id: Optional[str] = None) -> Dict[str, Any]:
|
|
114
|
+
payload: Dict[str, Any] = {"encryptedFileId": file_id, "searchType": "policy"}
|
|
115
|
+
if version_id:
|
|
116
|
+
payload = {"encryptedVersionId": version_id, "searchType": "policy"}
|
|
117
|
+
body = _post(POLICY_CONTENT, payload)
|
|
118
|
+
if body.get("ret") != 1 or (body.get("code") or 0) != 200:
|
|
119
|
+
raise LegalApiError("policy-content", body.get("message") or f"ret={body.get('ret')} code={body.get('code')}", None, body)
|
|
120
|
+
return body.get("content") or {}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
ALLOWED_DOWNLOAD_HOSTS = {"open.dknowc.cn", "attach.dknowc.cn"}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _validate_download_url(download_url: str) -> str:
|
|
127
|
+
"""下载地址只允许 https + 深知域名,避免把 api-key 发往接口返回的任意主机。"""
|
|
128
|
+
parsed = urlparse((download_url or "").strip())
|
|
129
|
+
if (
|
|
130
|
+
parsed.scheme != "https"
|
|
131
|
+
or parsed.hostname not in ALLOWED_DOWNLOAD_HOSTS
|
|
132
|
+
or parsed.username
|
|
133
|
+
or parsed.password
|
|
134
|
+
or parsed.port not in (None, 443)
|
|
135
|
+
):
|
|
136
|
+
raise LegalApiError("download", "下载地址不在允许的域名内,已拒绝发送 api-key")
|
|
137
|
+
return download_url
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def download_text(download_url: str, timeout: int = 90, key: Optional[str] = None) -> str:
|
|
141
|
+
download_url = _validate_download_url(download_url)
|
|
142
|
+
api_key = key or resolve_key()
|
|
143
|
+
req = urllib.request.Request(download_url, headers=_headers(api_key))
|
|
144
|
+
try:
|
|
145
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
146
|
+
return resp.read().decode("utf-8", "ignore")
|
|
147
|
+
except urllib.error.HTTPError as exc:
|
|
148
|
+
detail = exc.read().decode("utf-8", "ignore")[:300]
|
|
149
|
+
raise LegalApiError("download", f"下载失败 HTTP {exc.code}: {detail}", exc.code)
|
|
150
|
+
except urllib.error.URLError as exc:
|
|
151
|
+
raise LegalApiError("download", f"下载失败: {exc.reason}")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def get_full_text(file_id: str) -> Tuple[Dict[str, Any], str]:
|
|
155
|
+
content = get_content(file_id)
|
|
156
|
+
url = ((content.get("contentFile") or {}).get("downloadUrl") or "").strip()
|
|
157
|
+
if not url:
|
|
158
|
+
return content, ""
|
|
159
|
+
return content, download_text(url)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def semantic_search(
|
|
163
|
+
query: str,
|
|
164
|
+
limit_articles: int = 10,
|
|
165
|
+
segment_count: int = 6,
|
|
166
|
+
material_length: int = 25000,
|
|
167
|
+
service_area: Optional[str] = None,
|
|
168
|
+
policy_labels: Optional[List[str]] = None,
|
|
169
|
+
search_channel: Optional[str] = None,
|
|
170
|
+
) -> Dict[str, Any]:
|
|
171
|
+
payload: Dict[str, Any] = {
|
|
172
|
+
"query": query,
|
|
173
|
+
"searchType": ["policy"],
|
|
174
|
+
"validityFilter": False,
|
|
175
|
+
"searchGranularity": "PROVISION",
|
|
176
|
+
"segmentCount": segment_count,
|
|
177
|
+
"MaterialLength": material_length,
|
|
178
|
+
"filterPara": True,
|
|
179
|
+
# 数据权威性(2026-11-05 实测可用):返回"权威/非权威",渲染层据此打来源徽标,
|
|
180
|
+
# 与 existing 的"非官方来源"口径并用。字段很小,默认打开。
|
|
181
|
+
"dataAuthority": True,
|
|
182
|
+
}
|
|
183
|
+
if service_area:
|
|
184
|
+
payload["service_area"] = [service_area]
|
|
185
|
+
# 标签过滤(2026-11-05):传父标签"法律/法律标签/法律"可滤掉语义召回里的新闻稿/问答页
|
|
186
|
+
# (实测 T1 的中国法院网新闻稿、政府问答页都不带该标签)。注意司法解释/批复不在
|
|
187
|
+
# 该标签族内——查解释类文件时不要传,否则目标文件会被漏掉。
|
|
188
|
+
if policy_labels:
|
|
189
|
+
payload["policyLabels"] = policy_labels
|
|
190
|
+
# 联网检索(2026-11-10,阶段二):库内无果/支撑不足时,先用深知自己的
|
|
191
|
+
# webSearch 通道补公开网页原文;外部材料由接口/渲染层标“非官方来源”。
|
|
192
|
+
if search_channel:
|
|
193
|
+
payload["searchChannel"] = [search_channel]
|
|
194
|
+
body = _post(SEARCH_ENDPOINT, payload)
|
|
195
|
+
content = body.get("content") or {}
|
|
196
|
+
if (content.get("code") or 0) != 200:
|
|
197
|
+
raise LegalApiError("semantic-search", content.get("msg") or f"code={content.get('code')}", None, body)
|
|
198
|
+
data = content.get("data") or {}
|
|
199
|
+
articles = [x for x in (data.get("检索文章") or []) if isinstance(x, dict)]
|
|
200
|
+
# 带标签检索 0 条时不再静默降级重查(2026-11-06):按接口契约,标签是调用方
|
|
201
|
+
# 明确指定的范围限定;静默去掉标签会把"标签过滤没命中"伪装成"检索成功",
|
|
202
|
+
# 掩盖接口问题。这里只标注结果为空,由调用方决定是否另发一次不带标签的检索,
|
|
203
|
+
# 并把两次结果分别记录、在报告中如实展示。
|
|
204
|
+
if policy_labels and not articles:
|
|
205
|
+
body["_legal_label_zero"] = True
|
|
206
|
+
if limit_articles and len(articles) > limit_articles:
|
|
207
|
+
data["检索文章"] = articles[:limit_articles]
|
|
208
|
+
return body
|