dknowc-dsh 1.0.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/LICENSE +21 -0
- package/README.md +131 -0
- package/cordis.patch.yml +17 -0
- package/package.json +33 -0
- package/skills/dknowc-official-doc-writer/README.md +156 -0
- package/skills/dknowc-official-doc-writer/SKILL.md +462 -0
- package/skills/dknowc-official-doc-writer/config/format.json +101 -0
- package/skills/dknowc-official-doc-writer/config/user_profile.json.example +6 -0
- package/skills/dknowc-official-doc-writer/official-docs/input/mcp_intro_content.md +61 -0
- package/skills/dknowc-official-doc-writer/reference/anti_ai_patterns.md +113 -0
- package/skills/dknowc-official-doc-writer/reference/fact_discipline.md +62 -0
- package/skills/dknowc-official-doc-writer/reference/material_usage_guidance.md +138 -0
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +288 -0
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +156 -0
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +317 -0
- package/skills/dknowc-official-doc-writer/reference/search_intro.md +15 -0
- package/skills/dknowc-official-doc-writer/reference/search_policy.md +284 -0
- package/skills/dknowc-official-doc-writer/reference/standards/00_index.md +151 -0
- package/skills/dknowc-official-doc-writer/reference/standards/01_report.md +51 -0
- package/skills/dknowc-official-doc-writer/reference/standards/02_qingshi.md +53 -0
- package/skills/dknowc-official-doc-writer/reference/standards/03_pifu.md +48 -0
- package/skills/dknowc-official-doc-writer/reference/standards/04_tongzhi.md +51 -0
- package/skills/dknowc-official-doc-writer/reference/standards/05_yijian.md +55 -0
- package/skills/dknowc-official-doc-writer/reference/standards/06_han.md +81 -0
- package/skills/dknowc-official-doc-writer/reference/standards/07_minutes.md +46 -0
- package/skills/dknowc-official-doc-writer/reference/standards/08_tongbao.md +53 -0
- package/skills/dknowc-official-doc-writer/reference/standards/09_tonggao.md +45 -0
- package/skills/dknowc-official-doc-writer/reference/standards/10_gonggao.md +44 -0
- package/skills/dknowc-official-doc-writer/reference/standards/11_fuhan_approve.md +46 -0
- package/skills/dknowc-official-doc-writer/reference/standards/12_fuhan_objection.md +46 -0
- package/skills/dknowc-official-doc-writer/reference/standards/13_reminder.md +46 -0
- package/skills/dknowc-official-doc-writer/reference/standards/14_generic.md +41 -0
- package/skills/dknowc-official-doc-writer/reference/standards/15_business_docs.md +47 -0
- package/skills/dknowc-official-doc-writer/reference/standards/16_decision.md +47 -0
- package/skills/dknowc-official-doc-writer/reference/standards/17_resolution.md +45 -0
- package/skills/dknowc-official-doc-writer/reference/standards/18_order.md +44 -0
- package/skills/dknowc-official-doc-writer/reference/standards/19_gazette.md +45 -0
- package/skills/dknowc-official-doc-writer/reference/standards/20_motion.md +45 -0
- package/skills/dknowc-official-doc-writer/reference/standards/21_explanation.md +40 -0
- package/skills/dknowc-official-doc-writer/reference/standards/22_application.md +47 -0
- package/skills/dknowc-official-doc-writer/reference/standards/23_publicity.md +46 -0
- package/skills/dknowc-official-doc-writer/reference/standards/24_procurement.md +40 -0
- package/skills/dknowc-official-doc-writer/reference/standards/25_plan.md +42 -0
- package/skills/dknowc-official-doc-writer/reference/standards/26_summary.md +42 -0
- package/skills/dknowc-official-doc-writer/reference/standards/27_speech.md +43 -0
- package/skills/dknowc-official-doc-writer/reference/standards/28_rules.md +40 -0
- package/skills/dknowc-official-doc-writer/reference/standards/29_research.md +42 -0
- package/skills/dknowc-official-doc-writer/reference/standards/30_feasibility.md +42 -0
- package/skills/dknowc-official-doc-writer/reference/standards/31_review_material.md +40 -0
- package/skills/dknowc-official-doc-writer/reference/standards/32_ai_compute.md +41 -0
- package/skills/dknowc-official-doc-writer/reference/standards/99_expressions.md +569 -0
- package/skills/dknowc-official-doc-writer/reference/task_router.md +119 -0
- package/skills/dknowc-official-doc-writer/scripts/adapt_mcp_result.py +241 -0
- package/skills/dknowc-official-doc-writer/scripts/check_release.py +78 -0
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +575 -0
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +1756 -0
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +178 -0
- package/skills/dknowc-official-doc-writer/scripts/mcp_direct.py +158 -0
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +223 -0
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +202 -0
- package/skills/dknowc-official-doc-writer/scripts/prose_lint.py +242 -0
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +218 -0
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +966 -0
- package/skills/dknowc-official-doc-writer/scripts/review_document.py +127 -0
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +180 -0
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +975 -0
- package/skills/dknowc-trusted-consulting/README.md +63 -0
- package/skills/dknowc-trusted-consulting/SKILL.md +172 -0
- package/skills/dknowc-trusted-consulting/scripts/adapt_mcp_result.py +241 -0
- package/skills/dknowc-trusted-consulting/scripts/check_release.py +66 -0
- package/skills/dknowc-trusted-consulting/scripts/gov_chat.py +475 -0
- package/skills/dknowc-trusted-consulting/scripts/initialize.py +89 -0
- package/skills/dknowc-trusted-consulting/scripts/mcp_direct.py +158 -0
- package/skills/dknowc-trusted-consulting/scripts/register_key.mjs +181 -0
- package/skills/dknowc-trusted-consulting/scripts/render_trace_html.py +990 -0
- package/skills/dknowc-trusted-search/README.md +63 -0
- package/skills/dknowc-trusted-search/SKILL.md +232 -0
- package/skills/dknowc-trusted-search/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-trusted-search/official-docs/search-results/.gitkeep +0 -0
- package/skills/dknowc-trusted-search/scripts/adapt_mcp_result.py +241 -0
- package/skills/dknowc-trusted-search/scripts/check_release.py +79 -0
- package/skills/dknowc-trusted-search/scripts/deep_query.py +266 -0
- package/skills/dknowc-trusted-search/scripts/initialize.py +89 -0
- package/skills/dknowc-trusted-search/scripts/mcp_direct.py +158 -0
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +181 -0
- package/skills/dknowc-trusted-search/scripts/render_policy_visualization.py +889 -0
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +1011 -0
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +325 -0
- package/src/index.js +103 -0
|
@@ -0,0 +1,1011 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Generate an interactive local HTML trace report from DKnownAI API JSON."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import html
|
|
8
|
+
import json
|
|
9
|
+
import re
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Dict, Iterable, List, Optional
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
SKILL_ROOT = Path(__file__).resolve().parent.parent
|
|
16
|
+
# 工作区根:dsh 场景通过环境变量 DKNWOC_WS_ROOT 指向会话工作区,未设置时回退到 skill 目录(SkillHub 兼容)
|
|
17
|
+
import os as _os
|
|
18
|
+
WS_ROOT = Path(_os.environ.get("DKNWOC_WS_ROOT") or _os.getcwd()).resolve()
|
|
19
|
+
|
|
20
|
+
SEARCH_RESULTS_DIR = WS_ROOT / "official-docs" / "search-results"
|
|
21
|
+
OUTPUT_DIR = WS_ROOT / "official-docs" / "output"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _is_within(path: Path, parent: Path) -> bool:
|
|
25
|
+
try:
|
|
26
|
+
path.relative_to(parent)
|
|
27
|
+
return True
|
|
28
|
+
except ValueError:
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _safe_input_path(value: str, allowed_suffixes: set) -> Path:
|
|
33
|
+
"""把待读取文件定位到 skill 的 official-docs/search-results/ 内。"""
|
|
34
|
+
raw = Path(value).expanduser()
|
|
35
|
+
resolved = raw.resolve() if raw.is_absolute() else (SEARCH_RESULTS_DIR / raw.name).resolve()
|
|
36
|
+
if resolved.suffix.lower() not in allowed_suffixes:
|
|
37
|
+
raise ValueError(f"只允许读取 {', '.join(sorted(allowed_suffixes))} 文件: {value}")
|
|
38
|
+
if not _is_within(resolved, SEARCH_RESULTS_DIR.resolve()):
|
|
39
|
+
raise ValueError(f"输入文件必须位于 official-docs/search-results/ 内: {SEARCH_RESULTS_DIR}")
|
|
40
|
+
return resolved
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _safe_output_path(value: str, allowed_suffixes: set, default_suffix: str) -> Path:
|
|
44
|
+
"""把输出文件定位到 skill 的 official-docs/output/ 内。"""
|
|
45
|
+
raw = Path(value).expanduser()
|
|
46
|
+
resolved = raw.resolve() if raw.is_absolute() else (OUTPUT_DIR / raw.name).resolve()
|
|
47
|
+
if resolved.suffix.lower() not in allowed_suffixes:
|
|
48
|
+
resolved = resolved.with_suffix(default_suffix)
|
|
49
|
+
if not _is_within(resolved, OUTPUT_DIR.resolve()):
|
|
50
|
+
raise ValueError(f"输出文件必须位于 official-docs/output/ 内: {OUTPUT_DIR}")
|
|
51
|
+
return resolved
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def esc(value: Any) -> str:
|
|
55
|
+
return html.escape(str(value or ""), quote=True)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def load_json(path: Path) -> Dict[str, Any]:
|
|
59
|
+
with path.open("r", encoding="utf-8") as f:
|
|
60
|
+
data = json.load(f)
|
|
61
|
+
if isinstance(data, dict):
|
|
62
|
+
return data
|
|
63
|
+
return {"data": data}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def unwrap(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
67
|
+
if isinstance(data.get("data"), dict) and data.get("success") is True:
|
|
68
|
+
return data["data"]
|
|
69
|
+
if isinstance(data.get("content"), dict):
|
|
70
|
+
return data["content"]
|
|
71
|
+
return data
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def first_str(*values: Any) -> str:
|
|
75
|
+
for value in values:
|
|
76
|
+
if isinstance(value, str) and value.strip():
|
|
77
|
+
return value.strip()
|
|
78
|
+
if value not in (None, "", [], {}) and not isinstance(value, (dict, list)):
|
|
79
|
+
return str(value).strip()
|
|
80
|
+
return ""
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def short(text: Any, limit: int = 360) -> str:
|
|
84
|
+
value = " ".join(str(text or "").split())
|
|
85
|
+
if len(value) <= limit:
|
|
86
|
+
return value
|
|
87
|
+
return value[:limit].rstrip() + "..."
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def strip_tags(text: str) -> str:
|
|
91
|
+
return re.sub(r"<[^>]+>", "", text or "")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def normalize_citations(text: str) -> str:
|
|
95
|
+
text = strip_tags(text)
|
|
96
|
+
text = re.sub(r"【\s*(\d+)\s*】", r"[\1]", text)
|
|
97
|
+
return re.sub(r"\[\^?(\d+)\^?\]", r"[\1]", text)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def strip_citation_markers(text: str) -> str:
|
|
101
|
+
text = strip_tags(text)
|
|
102
|
+
text = re.sub(r"【\s*\d+\s*】", "", text)
|
|
103
|
+
text = re.sub(r"\[\^?\d+\^?\]", "", text)
|
|
104
|
+
text = re.sub(r"[ \t]+(\n)", r"\1", text)
|
|
105
|
+
text = re.sub(r"\n{3,}", "\n\n", text)
|
|
106
|
+
return text.strip() + ("\n" if text.strip() else "")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def paragraph_text(item: Dict[str, Any]) -> str:
|
|
110
|
+
paragraphs = item.get("content") or item.get("段落") or item.get("paragraphs") or item.get("paragraphList")
|
|
111
|
+
if isinstance(paragraphs, list):
|
|
112
|
+
chunks = []
|
|
113
|
+
for para in paragraphs:
|
|
114
|
+
if isinstance(para, dict):
|
|
115
|
+
chunks.append(first_str(para.get("text"), para.get("内容"), para.get("content"), para.get("summary"), para.get("标题"), para.get("title")))
|
|
116
|
+
else:
|
|
117
|
+
chunks.append(str(para))
|
|
118
|
+
text = "\n".join(chunk for chunk in chunks if chunk)
|
|
119
|
+
if text:
|
|
120
|
+
return text
|
|
121
|
+
return first_str(
|
|
122
|
+
item.get("摘录"),
|
|
123
|
+
item.get("摘要"),
|
|
124
|
+
item.get("相关段落"),
|
|
125
|
+
item.get("全文"),
|
|
126
|
+
item.get("content"),
|
|
127
|
+
item.get("text"),
|
|
128
|
+
item.get("snippet"),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def content_segments(item: Dict[str, Any]) -> List[Dict[str, str]]:
|
|
133
|
+
paragraphs = item.get("content") or item.get("段落") or item.get("paragraphs") or item.get("paragraphList")
|
|
134
|
+
segments: List[Dict[str, str]] = []
|
|
135
|
+
if not isinstance(paragraphs, list):
|
|
136
|
+
return segments
|
|
137
|
+
for para in paragraphs:
|
|
138
|
+
if isinstance(para, dict):
|
|
139
|
+
text = first_str(para.get("text"), para.get("内容"), para.get("content"), para.get("summary"))
|
|
140
|
+
title = first_str(para.get("title"), para.get("标题"), para.get("name"))
|
|
141
|
+
pid = first_str(para.get("id"), para.get("idx"), para.get("index"), para.get("seq"), para.get("编号"))
|
|
142
|
+
if text or title:
|
|
143
|
+
segments.append({"id": pid, "title": title, "text": text})
|
|
144
|
+
else:
|
|
145
|
+
text = first_str(para)
|
|
146
|
+
if text:
|
|
147
|
+
segments.append({"id": "", "title": "", "text": text})
|
|
148
|
+
return segments
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def source_from_article(item: Dict[str, Any], index: int, segment: Optional[Dict[str, str]] = None) -> Dict[str, str]:
|
|
152
|
+
vo = item.get("vo") if isinstance(item.get("vo"), dict) else {}
|
|
153
|
+
segment = segment or {}
|
|
154
|
+
explicit_id = first_str(
|
|
155
|
+
segment.get("id"),
|
|
156
|
+
item.get("角标"),
|
|
157
|
+
item.get("编号"),
|
|
158
|
+
item.get("index"),
|
|
159
|
+
item.get("idx"),
|
|
160
|
+
item.get("seq"),
|
|
161
|
+
item.get("serialNo"),
|
|
162
|
+
item.get("materialIndex"),
|
|
163
|
+
item.get("referenceIndex"),
|
|
164
|
+
item.get("引用编号"),
|
|
165
|
+
vo.get("index"),
|
|
166
|
+
vo.get("idx"),
|
|
167
|
+
)
|
|
168
|
+
if explicit_id:
|
|
169
|
+
match = re.search(r"\d+", explicit_id)
|
|
170
|
+
explicit_id = match.group(0) if match else ""
|
|
171
|
+
source = {
|
|
172
|
+
"id": explicit_id or str(index),
|
|
173
|
+
"title": first_str(
|
|
174
|
+
item.get("文章标题"),
|
|
175
|
+
item.get("title"),
|
|
176
|
+
item.get("标题"),
|
|
177
|
+
item.get("name"),
|
|
178
|
+
vo.get("showTitle"),
|
|
179
|
+
vo.get("title"),
|
|
180
|
+
"未命名材料",
|
|
181
|
+
),
|
|
182
|
+
"agency": first_str(
|
|
183
|
+
item.get("unit") if not isinstance(item.get("unit"), list) else "、".join(str(x) for x in item.get("unit")[:3]),
|
|
184
|
+
item.get("sourceElement"),
|
|
185
|
+
item.get("数据源"),
|
|
186
|
+
item.get("发布或实施机构"),
|
|
187
|
+
item.get("发布机关"),
|
|
188
|
+
item.get("来源"),
|
|
189
|
+
vo.get("typeName"),
|
|
190
|
+
vo.get("sourceName"),
|
|
191
|
+
),
|
|
192
|
+
"date": first_str(item.get("发布日期"), item.get("date"), item.get("发布时间"), item.get("createDate"), vo.get("dateTime"), vo.get("createDate")),
|
|
193
|
+
"url": first_str(item.get("sourceUrl"), item.get("source_url"), item.get("源网址"), item.get("url"), item.get("原文"), vo.get("sourceUrl"), vo.get("url")),
|
|
194
|
+
"policy_url": first_str(item.get("policyUrl"), item.get("policy_url"), item.get("知识专库原文"), vo.get("policyUrl")),
|
|
195
|
+
"excerpt": segment.get("text") or paragraph_text(item),
|
|
196
|
+
"section": first_str(segment.get("title")),
|
|
197
|
+
"kind": first_str(item.get("类型"), item.get("type"), vo.get("typeName"), "材料"),
|
|
198
|
+
"area": first_str(item.get("intentionArea"), item.get("area"), item.get("地域")),
|
|
199
|
+
"reliability": first_str(item.get("createDateReliability"), item.get("reliability"), item.get("时间可靠性")),
|
|
200
|
+
}
|
|
201
|
+
return source
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def extract_articles_from_search(payload: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
205
|
+
content = payload.get("content") if isinstance(payload.get("content"), dict) else payload
|
|
206
|
+
data = content.get("data") if isinstance(content.get("data"), dict) else content.get("data")
|
|
207
|
+
if isinstance(data, dict) and isinstance(data.get("检索文章"), list):
|
|
208
|
+
return [x for x in data["检索文章"] if isinstance(x, dict)]
|
|
209
|
+
return []
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def extract_articles_from_deep(payload: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
213
|
+
events = payload.get("events")
|
|
214
|
+
if isinstance(events, list):
|
|
215
|
+
for event in events:
|
|
216
|
+
if isinstance(event, list) and len(event) == 2:
|
|
217
|
+
name, obj = event
|
|
218
|
+
elif isinstance(event, dict):
|
|
219
|
+
name, obj = event.get("event"), event
|
|
220
|
+
else:
|
|
221
|
+
continue
|
|
222
|
+
if name == "result" and isinstance(obj, dict):
|
|
223
|
+
data = obj.get("data")
|
|
224
|
+
result_list = data.get("list") if isinstance(data, dict) else None
|
|
225
|
+
if isinstance(result_list, list):
|
|
226
|
+
return [x for x in result_list if isinstance(x, dict)]
|
|
227
|
+
data = payload.get("data")
|
|
228
|
+
result_list = data.get("list") if isinstance(data, dict) else None
|
|
229
|
+
if isinstance(result_list, list):
|
|
230
|
+
return [x for x in result_list if isinstance(x, dict)]
|
|
231
|
+
return []
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def extract_reference_materials(payload: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
235
|
+
materials = payload.get("referenceMaterials")
|
|
236
|
+
if isinstance(materials, list):
|
|
237
|
+
return [x for x in materials if isinstance(x, dict)]
|
|
238
|
+
return []
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def extract_sources(payload: Dict[str, Any]) -> List[Dict[str, str]]:
|
|
242
|
+
raw_sources: List[Dict[str, Any]] = []
|
|
243
|
+
raw_sources.extend(extract_reference_materials(payload))
|
|
244
|
+
raw_sources.extend(extract_articles_from_search(payload))
|
|
245
|
+
raw_sources.extend(extract_articles_from_deep(payload))
|
|
246
|
+
|
|
247
|
+
sources: List[Dict[str, str]] = []
|
|
248
|
+
seen = set()
|
|
249
|
+
for item in raw_sources:
|
|
250
|
+
segments = content_segments(item)
|
|
251
|
+
candidates = segments or [None]
|
|
252
|
+
for segment in candidates:
|
|
253
|
+
source = source_from_article(item, len(sources) + 1, segment=segment)
|
|
254
|
+
key = (source["id"], source["title"], source["url"], source["excerpt"][:80])
|
|
255
|
+
if key in seen:
|
|
256
|
+
continue
|
|
257
|
+
seen.add(key)
|
|
258
|
+
if not source["id"]:
|
|
259
|
+
source["id"] = str(len(sources) + 1)
|
|
260
|
+
sources.append(source)
|
|
261
|
+
return sources
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def extract_answer(payload: Dict[str, Any]) -> str:
|
|
265
|
+
resp = payload.get("resp")
|
|
266
|
+
if isinstance(resp, dict):
|
|
267
|
+
answer = first_str(resp.get("content"), resp.get("answer"), resp.get("text"))
|
|
268
|
+
if answer:
|
|
269
|
+
return normalize_citations(answer)
|
|
270
|
+
answer = first_str(payload.get("answer"), payload.get("contentText"), payload.get("text"))
|
|
271
|
+
if answer:
|
|
272
|
+
return normalize_citations(answer)
|
|
273
|
+
articles = extract_articles_from_search(payload)
|
|
274
|
+
if articles:
|
|
275
|
+
rows = ["可信搜索召回了以下重点材料:"]
|
|
276
|
+
for idx, item in enumerate(articles[:8], start=1):
|
|
277
|
+
rows.append(f"[{idx}] {first_str(item.get('文章标题'), item.get('title'), item.get('标题'), '未命名材料')}")
|
|
278
|
+
return "\n".join(rows)
|
|
279
|
+
deep_articles = extract_articles_from_deep(payload)
|
|
280
|
+
if deep_articles:
|
|
281
|
+
rows = ["深度搜索召回了以下重点材料:"]
|
|
282
|
+
for idx, item in enumerate(deep_articles[:8], start=1):
|
|
283
|
+
vo = item.get("vo") if isinstance(item.get("vo"), dict) else {}
|
|
284
|
+
rows.append(f"[{idx}] {first_str(vo.get('showTitle'), vo.get('title'), item.get('title'), '未命名材料')}")
|
|
285
|
+
return "\n".join(rows)
|
|
286
|
+
return "接口返回中未识别到正文内容;请查看右侧来源或原始 JSON。"
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def extract_trace_url(payload: Dict[str, Any]) -> str:
|
|
290
|
+
return first_str(payload.get("traceUrl"), payload.get("trace_url"), payload.get("traceReportUrl"))
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def extract_knowledge_base(payload: Dict[str, Any]) -> str:
|
|
294
|
+
content = payload.get("content") if isinstance(payload.get("content"), dict) else payload
|
|
295
|
+
data = content.get("data") if isinstance(content.get("data"), dict) else {}
|
|
296
|
+
return first_str(
|
|
297
|
+
content.get("knowledgeBase"),
|
|
298
|
+
payload.get("knowledgeBase"),
|
|
299
|
+
data.get("knowledgeBase"),
|
|
300
|
+
payload.get("knowledgeBaseUrl"),
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def citation_ids(answer: str) -> List[str]:
|
|
305
|
+
ids: List[str] = []
|
|
306
|
+
for match in re.finditer(r"\[(\d+)\]", answer):
|
|
307
|
+
item = match.group(1)
|
|
308
|
+
if item not in ids:
|
|
309
|
+
ids.append(item)
|
|
310
|
+
return ids
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def render_inline_markdown(text: str, citation_repl) -> str:
|
|
314
|
+
body = esc(text)
|
|
315
|
+
body = re.sub(r"`([^`]+)`", r"<code>\1</code>", body)
|
|
316
|
+
body = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", body)
|
|
317
|
+
body = re.sub(r"__(.+?)__", r"<strong>\1</strong>", body)
|
|
318
|
+
body = re.sub(r"(?<!\*)\*([^*\n]+)\*(?!\*)", r"<em>\1</em>", body)
|
|
319
|
+
body = re.sub(r"(?<!_)_([^_\n]+)_(?!_)", r"<em>\1</em>", body)
|
|
320
|
+
body = re.sub(r"\[(\d+)\]", citation_repl, body)
|
|
321
|
+
return body
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def is_markdown_table_separator(line: str) -> bool:
|
|
325
|
+
stripped = line.strip().strip("|")
|
|
326
|
+
if not stripped:
|
|
327
|
+
return False
|
|
328
|
+
cells = [cell.strip() for cell in stripped.split("|")]
|
|
329
|
+
return all(re.fullmatch(r":?-{3,}:?", cell or "") for cell in cells)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def split_markdown_table_row(line: str) -> List[str]:
|
|
333
|
+
return [cell.strip() for cell in line.strip().strip("|").split("|")]
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def render_markdown_table(lines: List[str], citation_repl) -> str:
|
|
337
|
+
header = split_markdown_table_row(lines[0])
|
|
338
|
+
body_lines = lines[2:] if len(lines) > 1 and is_markdown_table_separator(lines[1]) else lines[1:]
|
|
339
|
+
head_html = "".join(f"<th>{render_inline_markdown(cell, citation_repl)}</th>" for cell in header)
|
|
340
|
+
rows = []
|
|
341
|
+
for line in body_lines:
|
|
342
|
+
cells = split_markdown_table_row(line)
|
|
343
|
+
rows.append("<tr>" + "".join(f"<td>{render_inline_markdown(cell, citation_repl)}</td>" for cell in cells) + "</tr>")
|
|
344
|
+
return f'<div class="answer-table-wrap"><table class="answer-table"><thead><tr>{head_html}</tr></thead><tbody>{"".join(rows)}</tbody></table></div>'
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def render_excerpt_html(text: str) -> str:
|
|
348
|
+
value = str(text or "")
|
|
349
|
+
if "<table" not in value.lower():
|
|
350
|
+
return f'<div class="source-excerpt plain">{esc(value)}</div>'
|
|
351
|
+
cleaned = re.sub(r"(?is)<script.*?</script>", "", value)
|
|
352
|
+
cleaned = re.sub(r"(?is)<style.*?</style>", "", cleaned)
|
|
353
|
+
cleaned = re.sub(r"\s+on\w+\s*=\s*(['\"]).*?\1", "", cleaned)
|
|
354
|
+
cleaned = re.sub(r"\s+(href|src)\s*=\s*(['\"]).*?\2", "", cleaned)
|
|
355
|
+
allowed = {"table", "thead", "tbody", "tr", "td", "th", "br"}
|
|
356
|
+
|
|
357
|
+
def tag_repl(match: re.Match[str]) -> str:
|
|
358
|
+
closing, name = match.group(1), match.group(2).lower()
|
|
359
|
+
if name not in allowed:
|
|
360
|
+
return ""
|
|
361
|
+
return f"<{closing}{name}>"
|
|
362
|
+
|
|
363
|
+
cleaned = re.sub(r"<\s*(/?)\s*([a-zA-Z0-9]+)(?:\s+[^>]*)?>", tag_repl, cleaned)
|
|
364
|
+
return f'<div class="source-excerpt rich">{cleaned}</div>'
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def render_source_links(url: str, policy_url: str = "") -> str:
|
|
368
|
+
links = []
|
|
369
|
+
if url and url != "接口未返回":
|
|
370
|
+
links.append(f'<a href="{esc(url)}" target="_blank" rel="noopener">查看原文</a>')
|
|
371
|
+
if policy_url:
|
|
372
|
+
links.append(f'<a href="{esc(policy_url)}" target="_blank" rel="noopener">知识专库原文</a>')
|
|
373
|
+
if not links:
|
|
374
|
+
return ""
|
|
375
|
+
return '<div class="source-links">' + "".join(f"<span>{link}</span>" for link in links) + "</div>"
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def render_evidence_chips(ids: List[str], sources: List[Dict[str, str]]) -> str:
|
|
379
|
+
source_map = {source["id"]: source for source in sources}
|
|
380
|
+
chips = []
|
|
381
|
+
for cid in ids:
|
|
382
|
+
source = source_map.get(cid)
|
|
383
|
+
if not source:
|
|
384
|
+
continue
|
|
385
|
+
title = source.get("title") or "未命名材料"
|
|
386
|
+
section = source.get("section") or ""
|
|
387
|
+
meta = " | ".join(v for v in [source.get("agency"), source.get("date"), source.get("area"), source.get("kind")] if v)
|
|
388
|
+
excerpt = source.get("excerpt") or "接口返回中未识别到可展示的摘录。"
|
|
389
|
+
url = source.get("url")
|
|
390
|
+
links = render_source_links(url, source.get("policy_url", ""))
|
|
391
|
+
chips.append(
|
|
392
|
+
f"""
|
|
393
|
+
<details class="evidence-detail" data-source="{esc(cid)}">
|
|
394
|
+
<summary><b>[{esc(cid)}]</b>{esc(short(title, 64))}</summary>
|
|
395
|
+
<div class="evidence-panel">
|
|
396
|
+
<div class="evidence-title">{esc(title)}</div>
|
|
397
|
+
<div class="evidence-meta">{esc(meta)}</div>
|
|
398
|
+
{f'<div class="evidence-section">{esc(section)}</div>' if section else ''}
|
|
399
|
+
<p>{esc(excerpt)}</p>
|
|
400
|
+
{links}
|
|
401
|
+
</div>
|
|
402
|
+
</details>
|
|
403
|
+
"""
|
|
404
|
+
)
|
|
405
|
+
if not chips:
|
|
406
|
+
return ""
|
|
407
|
+
return f'<div class="evidence-row">{"".join(chips)}</div>'
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def render_answer(answer: str, sources: List[Dict[str, str]]) -> str:
|
|
411
|
+
valid_ids = {source["id"] for source in sources}
|
|
412
|
+
|
|
413
|
+
def repl(match: re.Match[str]) -> str:
|
|
414
|
+
cid = match.group(1)
|
|
415
|
+
disabled = "" if cid in valid_ids else " disabled"
|
|
416
|
+
return f'<button class="cite{disabled}" data-source="{esc(cid)}" type="button">[{esc(cid)}]</button>'
|
|
417
|
+
|
|
418
|
+
paragraphs = []
|
|
419
|
+
lines = answer.split("\n")
|
|
420
|
+
idx = 0
|
|
421
|
+
while idx < len(lines):
|
|
422
|
+
raw_block = lines[idx]
|
|
423
|
+
block = raw_block.strip()
|
|
424
|
+
if not block:
|
|
425
|
+
idx += 1
|
|
426
|
+
continue
|
|
427
|
+
if "|" in block and idx + 1 < len(lines) and is_markdown_table_separator(lines[idx + 1]):
|
|
428
|
+
table_lines = [block, lines[idx + 1].strip()]
|
|
429
|
+
idx += 2
|
|
430
|
+
while idx < len(lines) and "|" in lines[idx].strip() and lines[idx].strip():
|
|
431
|
+
table_lines.append(lines[idx].strip())
|
|
432
|
+
idx += 1
|
|
433
|
+
ids = citation_ids("\n".join(table_lines))
|
|
434
|
+
paragraphs.append(f'<div class="answer-block">{render_markdown_table(table_lines, repl)}{render_evidence_chips(ids, sources)}</div>')
|
|
435
|
+
continue
|
|
436
|
+
ids = citation_ids(block)
|
|
437
|
+
evidence = render_evidence_chips(ids, sources)
|
|
438
|
+
heading_match = re.match(r"^(#{1,4})\s+(.+)$", block)
|
|
439
|
+
if heading_match:
|
|
440
|
+
level = min(len(heading_match.group(1)) + 1, 4)
|
|
441
|
+
heading = render_inline_markdown(heading_match.group(2), repl)
|
|
442
|
+
paragraphs.append(f'<h{level} class="answer-heading">{heading}</h{level}>')
|
|
443
|
+
elif block in {"---", "***"}:
|
|
444
|
+
paragraphs.append('<hr class="answer-divider">')
|
|
445
|
+
elif re.match(r"^[-*]\s+", block):
|
|
446
|
+
body = render_inline_markdown(re.sub(r"^[-*]\s+", "", block), repl)
|
|
447
|
+
paragraphs.append(
|
|
448
|
+
f'<div class="answer-item"><span class="bullet">•</span><div><p>{body}</p>{evidence}</div></div>'
|
|
449
|
+
)
|
|
450
|
+
elif re.match(r"^\d+[.)]\s+", block):
|
|
451
|
+
number = re.match(r"^(\d+)[.)]\s+", block).group(1)
|
|
452
|
+
body = render_inline_markdown(re.sub(r"^\d+[.)]\s+", "", block), repl)
|
|
453
|
+
paragraphs.append(
|
|
454
|
+
f'<div class="answer-item numbered"><span class="bullet">{esc(number)}.</span><div><p>{body}</p>{evidence}</div></div>'
|
|
455
|
+
)
|
|
456
|
+
else:
|
|
457
|
+
body = render_inline_markdown(block, repl)
|
|
458
|
+
paragraphs.append(f'<div class="answer-block"><p>{body}</p>{evidence}</div>')
|
|
459
|
+
idx += 1
|
|
460
|
+
return "\n".join(paragraphs)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def render_sources(sources: List[Dict[str, str]]) -> str:
|
|
464
|
+
if not sources:
|
|
465
|
+
return '<p class="empty">接口返回中未识别到可展示的材料。</p>'
|
|
466
|
+
cards = []
|
|
467
|
+
for source in sources:
|
|
468
|
+
url = source.get("url")
|
|
469
|
+
links = render_source_links(url, source.get("policy_url", ""))
|
|
470
|
+
meta = " | ".join(v for v in [source.get("agency"), source.get("date"), source.get("area"), source.get("kind")] if v)
|
|
471
|
+
section = source.get("section") or ""
|
|
472
|
+
cards.append(
|
|
473
|
+
f"""
|
|
474
|
+
<article class="source-card" id="source-{esc(source['id'])}" data-source="{esc(source['id'])}">
|
|
475
|
+
<div class="source-top">
|
|
476
|
+
<span class="source-index">[{esc(source['id'])}]</span>
|
|
477
|
+
<h3>{esc(source.get("title"))}</h3>
|
|
478
|
+
</div>
|
|
479
|
+
<p class="meta">{esc(meta)}</p>
|
|
480
|
+
{f'<p class="section">{esc(section)}</p>' if section else ''}
|
|
481
|
+
{render_excerpt_html(source.get("excerpt") or "接口返回中未识别到可展示的摘录。")}
|
|
482
|
+
{links}
|
|
483
|
+
</article>
|
|
484
|
+
"""
|
|
485
|
+
)
|
|
486
|
+
return "\n".join(cards)
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def align_sources_to_answer(answer: str, sources: List[Dict[str, str]]) -> List[Dict[str, str]]:
|
|
490
|
+
cited = citation_ids(answer)
|
|
491
|
+
if not cited or not sources:
|
|
492
|
+
return sources
|
|
493
|
+
existing = {source["id"] for source in sources}
|
|
494
|
+
if existing.intersection(cited):
|
|
495
|
+
return sources
|
|
496
|
+
aligned = [dict(source) for source in sources]
|
|
497
|
+
for idx, citation in enumerate(cited):
|
|
498
|
+
if idx >= len(aligned):
|
|
499
|
+
break
|
|
500
|
+
aligned[idx]["id"] = citation
|
|
501
|
+
return aligned
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
def extract_question(payload: Dict[str, Any]) -> str:
|
|
505
|
+
content = payload.get("content") if isinstance(payload.get("content"), dict) else payload
|
|
506
|
+
data = content.get("data") if isinstance(content.get("data"), dict) else {}
|
|
507
|
+
return first_str(
|
|
508
|
+
payload.get("question"),
|
|
509
|
+
payload.get("input"),
|
|
510
|
+
payload.get("query"),
|
|
511
|
+
data.get("用户问题"),
|
|
512
|
+
content.get("query"),
|
|
513
|
+
content.get("question"),
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def render_entry_chip(kb_url: str, source_count: int) -> str:
|
|
518
|
+
if not kb_url:
|
|
519
|
+
return ""
|
|
520
|
+
count_text = f"{source_count}条来源" if source_count else "来源材料"
|
|
521
|
+
return (
|
|
522
|
+
f'<a class="kb-chip" href="{esc(kb_url)}" target="_blank" rel="noopener">'
|
|
523
|
+
f'<span class="kb-label">知识专库</span>'
|
|
524
|
+
f'<span class="kb-count">{esc(count_text)}</span>'
|
|
525
|
+
f'<span class="kb-arrow">查看</span>'
|
|
526
|
+
f'</a>'
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def safe_output_filename(question: str, timestamp: datetime, fallback: str = "dknowc_search_trace", suffix_ext: str = ".html") -> str:
|
|
531
|
+
raw = question.strip() or fallback
|
|
532
|
+
normalized = re.sub(r"\s+", "_", raw)
|
|
533
|
+
normalized = re.sub(r"[\\/:*?\"<>|#%&{}$!@`+=;',。、?!:;“”‘’()()【】《》\[\]]+", "", normalized)
|
|
534
|
+
normalized = normalized.strip("._-")
|
|
535
|
+
if not normalized:
|
|
536
|
+
normalized = fallback
|
|
537
|
+
suffix = timestamp.strftime("%Y%m%d_%H%M")
|
|
538
|
+
stem = normalized[:32].strip("._-") or fallback
|
|
539
|
+
return f"{stem}_{suffix}{suffix_ext}"
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
def render_html(payload: Dict[str, Any], title: str, answer_override: str = "", question_override: str = "", generated_at: Optional[datetime] = None) -> str:
|
|
543
|
+
payload = unwrap(payload)
|
|
544
|
+
answer = normalize_citations(answer_override) if answer_override.strip() else extract_answer(payload)
|
|
545
|
+
sources = align_sources_to_answer(answer, extract_sources(payload))
|
|
546
|
+
used = citation_ids(answer)
|
|
547
|
+
kb_url = extract_knowledge_base(payload)
|
|
548
|
+
question = question_override.strip() or extract_question(payload)
|
|
549
|
+
generated_at = generated_at or datetime.now()
|
|
550
|
+
generated = generated_at.strftime("%Y-%m-%d %H:%M")
|
|
551
|
+
kb_link = f'<a href="{esc(kb_url)}" target="_blank" rel="noopener">知识专库</a>' if kb_url else ""
|
|
552
|
+
links = kb_link
|
|
553
|
+
tools_html = f'<div class="tools">{links}</div>' if links else ""
|
|
554
|
+
question_html = f'<div class="question-bubble">{esc(question)}</div>' if question else ""
|
|
555
|
+
citation_warning = ""
|
|
556
|
+
if sources and not used:
|
|
557
|
+
citation_warning = (
|
|
558
|
+
'<div class="warning-box">'
|
|
559
|
+
'<b>生成检查提示:</b>本轮最终答案没有包含来源角标,因此无法在答案段落下做“结论-来源”一一对应。'
|
|
560
|
+
'请重新生成最终答案,并在关键结论后标注接口材料编号,例如 [1]、[2]。'
|
|
561
|
+
'</div>'
|
|
562
|
+
)
|
|
563
|
+
note_text = "AI综合所有相关权威材料后,参考性解读如下,建议点击角标查看所依据的材料原文。"
|
|
564
|
+
compact_answer_start = re.sub(r"\s+", "", answer.lstrip()[:120])
|
|
565
|
+
compact_note_start = re.sub(r"\s+", "", note_text[:36])
|
|
566
|
+
note_html = "" if compact_answer_start.startswith(compact_note_start) else f'<p class="ai-note">{esc(note_text)}</p>'
|
|
567
|
+
product_links_html = (
|
|
568
|
+
'<nav class="product-links" aria-label="产品链接">'
|
|
569
|
+
'<a href="https://yun.dknowc.cn/wlcb/dknowc-chat/#/" target="_blank" rel="noopener">深知晓内测</a>'
|
|
570
|
+
'<a href="https://www.dknowc.cn/" target="_blank" rel="noopener">深知可信智能官网</a>'
|
|
571
|
+
'</nav>'
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
return f"""<!doctype html>
|
|
575
|
+
<html lang="zh-CN">
|
|
576
|
+
<head>
|
|
577
|
+
<meta charset="utf-8">
|
|
578
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
579
|
+
<title>{esc(title)}</title>
|
|
580
|
+
<style>
|
|
581
|
+
:root {{
|
|
582
|
+
--purple: #6b18c9;
|
|
583
|
+
--purple-soft: #f4ebff;
|
|
584
|
+
--ink: #2f3033;
|
|
585
|
+
--muted: #6b7280;
|
|
586
|
+
--line: #e5e7eb;
|
|
587
|
+
--panel: #ffffff;
|
|
588
|
+
--page: #ffffff;
|
|
589
|
+
--soft: #f7f7f8;
|
|
590
|
+
}}
|
|
591
|
+
* {{ box-sizing: border-box; }}
|
|
592
|
+
body {{
|
|
593
|
+
margin: 0;
|
|
594
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
|
595
|
+
color: var(--ink);
|
|
596
|
+
background: var(--page);
|
|
597
|
+
}}
|
|
598
|
+
header {{
|
|
599
|
+
padding: 12px 28px;
|
|
600
|
+
background: #fff;
|
|
601
|
+
border-bottom: 1px solid var(--line);
|
|
602
|
+
position: sticky;
|
|
603
|
+
top: 0;
|
|
604
|
+
z-index: 2;
|
|
605
|
+
text-align: center;
|
|
606
|
+
}}
|
|
607
|
+
h1 {{ margin: 0 0 3px; font-size: 16px; letter-spacing: 0; font-weight: 700; }}
|
|
608
|
+
header p {{ margin: 0; color: #9ca3af; font-size: 12px; }}
|
|
609
|
+
.product-links {{
|
|
610
|
+
display: flex;
|
|
611
|
+
justify-content: center;
|
|
612
|
+
gap: 14px;
|
|
613
|
+
margin-top: 8px;
|
|
614
|
+
font-size: 12px;
|
|
615
|
+
}}
|
|
616
|
+
.product-links a {{
|
|
617
|
+
color: var(--purple);
|
|
618
|
+
text-decoration: none;
|
|
619
|
+
font-weight: 600;
|
|
620
|
+
}}
|
|
621
|
+
.product-links a:hover {{ text-decoration: underline; }}
|
|
622
|
+
.layout {{
|
|
623
|
+
display: grid;
|
|
624
|
+
grid-template-columns: minmax(0, 700px) minmax(390px, 460px);
|
|
625
|
+
gap: 30px;
|
|
626
|
+
justify-content: center;
|
|
627
|
+
max-width: 1220px;
|
|
628
|
+
margin: 0 auto;
|
|
629
|
+
padding: 30px 18px 40px;
|
|
630
|
+
}}
|
|
631
|
+
.conversation {{
|
|
632
|
+
min-width: 0;
|
|
633
|
+
}}
|
|
634
|
+
.question-bubble {{
|
|
635
|
+
max-width: 560px;
|
|
636
|
+
margin: 0 0 26px auto;
|
|
637
|
+
padding: 12px 16px;
|
|
638
|
+
border-radius: 9px;
|
|
639
|
+
background: var(--purple);
|
|
640
|
+
color: #fff;
|
|
641
|
+
line-height: 1.55;
|
|
642
|
+
font-weight: 700;
|
|
643
|
+
font-size: 14px;
|
|
644
|
+
}}
|
|
645
|
+
.answer {{
|
|
646
|
+
background: transparent;
|
|
647
|
+
padding: 0;
|
|
648
|
+
}}
|
|
649
|
+
.kb-chip {{
|
|
650
|
+
display: inline-flex;
|
|
651
|
+
align-items: center;
|
|
652
|
+
gap: 10px;
|
|
653
|
+
max-width: 100%;
|
|
654
|
+
margin: 0 0 16px;
|
|
655
|
+
padding: 7px 10px;
|
|
656
|
+
border: 1px solid var(--line);
|
|
657
|
+
border-radius: 7px;
|
|
658
|
+
color: #4b5563;
|
|
659
|
+
background: #fafafa;
|
|
660
|
+
text-decoration: none;
|
|
661
|
+
box-shadow: none;
|
|
662
|
+
font-size: 12px;
|
|
663
|
+
}}
|
|
664
|
+
.kb-chip:hover {{ border-color: #d1d5db; background: #f7f7f8; }}
|
|
665
|
+
.kb-label {{ font-weight: 700; color: #374151; }}
|
|
666
|
+
.kb-count {{
|
|
667
|
+
padding-left: 10px;
|
|
668
|
+
border-left: 1px solid var(--line);
|
|
669
|
+
color: #6b7280;
|
|
670
|
+
}}
|
|
671
|
+
.kb-arrow {{
|
|
672
|
+
color: var(--purple);
|
|
673
|
+
font-weight: 700;
|
|
674
|
+
}}
|
|
675
|
+
.ai-note {{ margin: 0 0 10px; color: #52525b; line-height: 1.75; font-size: 14px; }}
|
|
676
|
+
.answer-body {{
|
|
677
|
+
padding: 0;
|
|
678
|
+
background: transparent;
|
|
679
|
+
}}
|
|
680
|
+
.answer-block, .answer-item {{ margin: 0 0 12px; }}
|
|
681
|
+
.answer-item {{ display: grid; grid-template-columns: 20px minmax(0, 1fr); gap: 8px; }}
|
|
682
|
+
.answer-item .bullet {{ padding-top: 2px; color: #111827; font-size: 16px; line-height: 1.75; }}
|
|
683
|
+
.answer-item.numbered {{ grid-template-columns: 28px minmax(0, 1fr); }}
|
|
684
|
+
.answer p {{ margin: 0; font-size: 14px; line-height: 1.85; }}
|
|
685
|
+
.answer strong {{ font-weight: 700; color: #202124; }}
|
|
686
|
+
.answer em {{ font-style: normal; color: #4b5563; }}
|
|
687
|
+
.answer code {{
|
|
688
|
+
padding: 1px 5px;
|
|
689
|
+
border-radius: 4px;
|
|
690
|
+
background: #f3f4f6;
|
|
691
|
+
color: #374151;
|
|
692
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
693
|
+
font-size: 12px;
|
|
694
|
+
}}
|
|
695
|
+
.answer-table-wrap {{ max-width: 100%; overflow-x: auto; margin: 6px 0 8px; }}
|
|
696
|
+
.answer-table {{
|
|
697
|
+
width: 100%;
|
|
698
|
+
border-collapse: collapse;
|
|
699
|
+
border: 1px solid var(--line);
|
|
700
|
+
border-radius: 8px;
|
|
701
|
+
overflow: hidden;
|
|
702
|
+
font-size: 13px;
|
|
703
|
+
}}
|
|
704
|
+
.answer-table th, .answer-table td {{
|
|
705
|
+
padding: 9px 10px;
|
|
706
|
+
border: 1px solid var(--line);
|
|
707
|
+
text-align: left;
|
|
708
|
+
vertical-align: top;
|
|
709
|
+
line-height: 1.65;
|
|
710
|
+
}}
|
|
711
|
+
.answer-table th {{ background: #f8fafc; font-weight: 700; color: #374151; }}
|
|
712
|
+
.answer-heading {{ margin: 20px 0 9px; color: #111827; line-height: 1.45; letter-spacing: 0; }}
|
|
713
|
+
h2.answer-heading {{ font-size: 18px; }}
|
|
714
|
+
h3.answer-heading {{ font-size: 16px; }}
|
|
715
|
+
h4.answer-heading {{ font-size: 15px; }}
|
|
716
|
+
.answer-divider {{ border: 0; border-top: 1px solid var(--line); margin: 22px 0; }}
|
|
717
|
+
.warning-box {{
|
|
718
|
+
margin: 0 0 16px;
|
|
719
|
+
padding: 12px 14px;
|
|
720
|
+
border: 1px solid #f59e0b;
|
|
721
|
+
border-radius: 8px;
|
|
722
|
+
background: #fffbeb;
|
|
723
|
+
color: #92400e;
|
|
724
|
+
line-height: 1.7;
|
|
725
|
+
font-size: 14px;
|
|
726
|
+
}}
|
|
727
|
+
.cite {{
|
|
728
|
+
margin: 0 2px;
|
|
729
|
+
padding: 0 5px;
|
|
730
|
+
border: 0;
|
|
731
|
+
border-radius: 4px;
|
|
732
|
+
color: var(--purple);
|
|
733
|
+
background: var(--purple-soft);
|
|
734
|
+
font-weight: 700;
|
|
735
|
+
cursor: pointer;
|
|
736
|
+
vertical-align: super;
|
|
737
|
+
font-size: 10px;
|
|
738
|
+
line-height: 1.4;
|
|
739
|
+
}}
|
|
740
|
+
.cite.disabled {{ color: #9ca3af; background: #f3f4f6; cursor: default; }}
|
|
741
|
+
.evidence-row {{
|
|
742
|
+
display: flex;
|
|
743
|
+
flex-wrap: wrap;
|
|
744
|
+
gap: 6px;
|
|
745
|
+
margin: 5px 0 0;
|
|
746
|
+
}}
|
|
747
|
+
.evidence-detail {{
|
|
748
|
+
max-width: 100%;
|
|
749
|
+
}}
|
|
750
|
+
.evidence-detail summary {{
|
|
751
|
+
display: inline-flex;
|
|
752
|
+
align-items: center;
|
|
753
|
+
max-width: 100%;
|
|
754
|
+
border-radius: 999px;
|
|
755
|
+
padding: 4px 9px;
|
|
756
|
+
color: #6b7280;
|
|
757
|
+
background: #f3f4f6;
|
|
758
|
+
cursor: pointer;
|
|
759
|
+
overflow: hidden;
|
|
760
|
+
text-overflow: ellipsis;
|
|
761
|
+
white-space: nowrap;
|
|
762
|
+
list-style: none;
|
|
763
|
+
font-size: 12px;
|
|
764
|
+
}}
|
|
765
|
+
.evidence-detail summary::-webkit-details-marker {{ display: none; }}
|
|
766
|
+
.evidence-detail summary::after {{
|
|
767
|
+
content: "展开";
|
|
768
|
+
margin-left: 7px;
|
|
769
|
+
color: #9ca3af;
|
|
770
|
+
font-size: 11px;
|
|
771
|
+
}}
|
|
772
|
+
.evidence-detail[open] summary::after {{ content: "收起"; }}
|
|
773
|
+
.evidence-detail summary b {{ color: var(--purple); margin-right: 5px; }}
|
|
774
|
+
.evidence-panel {{
|
|
775
|
+
margin: 7px 0 4px;
|
|
776
|
+
padding: 12px 14px;
|
|
777
|
+
border: 1px solid var(--line);
|
|
778
|
+
border-radius: 8px;
|
|
779
|
+
background: #fff;
|
|
780
|
+
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
|
|
781
|
+
}}
|
|
782
|
+
.evidence-title {{ font-weight: 700; font-size: 14px; margin-bottom: 5px; color: #111827; }}
|
|
783
|
+
.evidence-meta {{ color: var(--muted); font-size: 12px; margin-bottom: 6px; }}
|
|
784
|
+
.evidence-section {{ color: var(--purple); font-size: 12px; margin-bottom: 6px; font-weight: 700; }}
|
|
785
|
+
.evidence-panel p {{ margin: 0 0 8px; color: #374151; font-size: 13px; line-height: 1.7; }}
|
|
786
|
+
.evidence-panel a {{ color: var(--purple); font-weight: 700; text-decoration: none; }}
|
|
787
|
+
.source-links {{
|
|
788
|
+
display: flex;
|
|
789
|
+
flex-wrap: wrap;
|
|
790
|
+
gap: 8px;
|
|
791
|
+
margin-top: 8px;
|
|
792
|
+
}}
|
|
793
|
+
.source-links span {{
|
|
794
|
+
display: inline-flex;
|
|
795
|
+
}}
|
|
796
|
+
.source-links a {{
|
|
797
|
+
display: inline-flex;
|
|
798
|
+
align-items: center;
|
|
799
|
+
min-height: 24px;
|
|
800
|
+
padding: 3px 8px;
|
|
801
|
+
border-radius: 6px;
|
|
802
|
+
background: var(--purple-soft);
|
|
803
|
+
color: var(--purple);
|
|
804
|
+
font-weight: 700;
|
|
805
|
+
text-decoration: none;
|
|
806
|
+
font-size: 12px;
|
|
807
|
+
}}
|
|
808
|
+
.tools {{
|
|
809
|
+
display: flex;
|
|
810
|
+
flex-wrap: wrap;
|
|
811
|
+
gap: 8px;
|
|
812
|
+
margin-top: 20px;
|
|
813
|
+
padding-top: 16px;
|
|
814
|
+
border-top: 1px solid var(--line);
|
|
815
|
+
}}
|
|
816
|
+
.tools a {{
|
|
817
|
+
color: var(--purple);
|
|
818
|
+
background: var(--purple-soft);
|
|
819
|
+
border-radius: 999px;
|
|
820
|
+
padding: 7px 12px;
|
|
821
|
+
text-decoration: none;
|
|
822
|
+
font-weight: 600;
|
|
823
|
+
}}
|
|
824
|
+
.sources {{
|
|
825
|
+
align-self: start;
|
|
826
|
+
position: sticky;
|
|
827
|
+
top: 70px;
|
|
828
|
+
max-height: calc(100vh - 92px);
|
|
829
|
+
overflow: auto;
|
|
830
|
+
background: #fff;
|
|
831
|
+
border-left: 1px solid var(--line);
|
|
832
|
+
padding: 16px 0 16px 24px;
|
|
833
|
+
}}
|
|
834
|
+
.sources h2 {{ margin: 0 0 12px; font-size: 15px; }}
|
|
835
|
+
.searchbox {{
|
|
836
|
+
width: 100%;
|
|
837
|
+
margin: 0 0 14px;
|
|
838
|
+
padding: 9px 12px;
|
|
839
|
+
border: 1px solid var(--line);
|
|
840
|
+
border-radius: 8px;
|
|
841
|
+
color: #6b7280;
|
|
842
|
+
background: #fff;
|
|
843
|
+
}}
|
|
844
|
+
.source-card {{
|
|
845
|
+
padding: 14px 0 15px;
|
|
846
|
+
border: 0;
|
|
847
|
+
border-bottom: 1px solid #f1f1f2;
|
|
848
|
+
border-radius: 0;
|
|
849
|
+
margin-bottom: 0;
|
|
850
|
+
background: #fff;
|
|
851
|
+
}}
|
|
852
|
+
.source-card.active {{
|
|
853
|
+
margin-left: -10px;
|
|
854
|
+
padding-left: 10px;
|
|
855
|
+
border-left: 3px solid var(--purple);
|
|
856
|
+
background: #fbf8ff;
|
|
857
|
+
box-shadow: none;
|
|
858
|
+
}}
|
|
859
|
+
.source-top {{
|
|
860
|
+
display: grid;
|
|
861
|
+
grid-template-columns: auto minmax(0, 1fr);
|
|
862
|
+
gap: 7px;
|
|
863
|
+
align-items: start;
|
|
864
|
+
margin-bottom: 6px;
|
|
865
|
+
}}
|
|
866
|
+
.source-index {{
|
|
867
|
+
color: var(--purple);
|
|
868
|
+
background: var(--purple-soft);
|
|
869
|
+
border-radius: 5px;
|
|
870
|
+
padding: 1px 5px;
|
|
871
|
+
font-weight: 800;
|
|
872
|
+
font-size: 12px;
|
|
873
|
+
line-height: 1.5;
|
|
874
|
+
}}
|
|
875
|
+
.source-card h3 {{ margin: 0; font-size: 13px; line-height: 1.55; letter-spacing: 0; }}
|
|
876
|
+
.source-card p {{ margin: 0 0 7px; color: #374151; line-height: 1.72; font-size: 12px; }}
|
|
877
|
+
.source-card .meta {{ color: var(--muted); font-size: 12px; line-height: 1.55; }}
|
|
878
|
+
.source-card .section {{ color: var(--purple); font-size: 12px; font-weight: 700; }}
|
|
879
|
+
.source-card a {{ color: var(--purple); font-weight: 700; text-decoration: none; font-size: 12px; }}
|
|
880
|
+
.source-card .source-excerpt {{
|
|
881
|
+
margin: 8px 0 8px;
|
|
882
|
+
padding: 9px 10px;
|
|
883
|
+
border-radius: 7px;
|
|
884
|
+
background: #f8fafc;
|
|
885
|
+
color: #374151;
|
|
886
|
+
font-size: 12px;
|
|
887
|
+
line-height: 1.75;
|
|
888
|
+
}}
|
|
889
|
+
.source-excerpt.rich {{
|
|
890
|
+
overflow-x: auto;
|
|
891
|
+
max-width: 100%;
|
|
892
|
+
}}
|
|
893
|
+
.source-excerpt.rich table {{
|
|
894
|
+
width: max-content;
|
|
895
|
+
min-width: 100%;
|
|
896
|
+
border-collapse: collapse;
|
|
897
|
+
background: #fff;
|
|
898
|
+
font-size: 12px;
|
|
899
|
+
line-height: 1.55;
|
|
900
|
+
}}
|
|
901
|
+
.source-excerpt.rich td, .source-excerpt.rich th {{
|
|
902
|
+
min-width: 92px;
|
|
903
|
+
max-width: 220px;
|
|
904
|
+
padding: 7px 8px;
|
|
905
|
+
border: 1px solid #e5e7eb;
|
|
906
|
+
vertical-align: top;
|
|
907
|
+
word-break: break-word;
|
|
908
|
+
}}
|
|
909
|
+
.source-excerpt.rich thead td, .source-excerpt.rich th {{
|
|
910
|
+
background: #f3f4f6;
|
|
911
|
+
color: #374151;
|
|
912
|
+
font-weight: 700;
|
|
913
|
+
}}
|
|
914
|
+
.empty {{ color: var(--muted); }}
|
|
915
|
+
@media (max-width: 900px) {{
|
|
916
|
+
.layout {{ grid-template-columns: 1fr; padding: 14px; }}
|
|
917
|
+
.sources {{ position: static; max-height: none; }}
|
|
918
|
+
.question-bubble {{ margin-left: 0; }}
|
|
919
|
+
}}
|
|
920
|
+
</style>
|
|
921
|
+
</head>
|
|
922
|
+
<body>
|
|
923
|
+
<header>
|
|
924
|
+
<h1>{esc(title)}</h1>
|
|
925
|
+
<p>内容为 AI 生成,仅供参考|生成时间:{esc(generated)}</p>
|
|
926
|
+
{product_links_html}
|
|
927
|
+
</header>
|
|
928
|
+
<main class="layout">
|
|
929
|
+
<section class="conversation" aria-label="回答正文">
|
|
930
|
+
{question_html}
|
|
931
|
+
<div class="answer">
|
|
932
|
+
{render_entry_chip(kb_url, len(sources))}
|
|
933
|
+
{note_html}
|
|
934
|
+
{citation_warning}
|
|
935
|
+
<div class="answer-body">{render_answer(answer, sources)}</div>
|
|
936
|
+
{tools_html}
|
|
937
|
+
</div>
|
|
938
|
+
</section>
|
|
939
|
+
<aside class="sources" aria-label="可信来源">
|
|
940
|
+
<h2>可信来源</h2>
|
|
941
|
+
<input class="searchbox" type="search" placeholder="搜索来源标题或摘录">
|
|
942
|
+
{render_sources(sources)}
|
|
943
|
+
</aside>
|
|
944
|
+
</main>
|
|
945
|
+
<script>
|
|
946
|
+
const cards = Array.from(document.querySelectorAll(".source-card"));
|
|
947
|
+
function activate(id) {{
|
|
948
|
+
cards.forEach(card => card.classList.toggle("active", card.dataset.source === id));
|
|
949
|
+
const target = document.querySelector(`#source-${{CSS.escape(id)}}`);
|
|
950
|
+
if (target) target.scrollIntoView({{ block: "nearest", behavior: "smooth" }});
|
|
951
|
+
}}
|
|
952
|
+
document.querySelectorAll(".cite").forEach(button => {{
|
|
953
|
+
if (button.classList.contains("disabled")) return;
|
|
954
|
+
button.addEventListener("click", () => activate(button.dataset.source));
|
|
955
|
+
}});
|
|
956
|
+
document.querySelectorAll(".evidence-detail").forEach(detail => {{
|
|
957
|
+
detail.addEventListener("toggle", () => {{
|
|
958
|
+
if (!detail.open) return;
|
|
959
|
+
cards.forEach(card => card.classList.toggle("active", card.dataset.source === detail.dataset.source));
|
|
960
|
+
}});
|
|
961
|
+
}});
|
|
962
|
+
const searchbox = document.querySelector(".searchbox");
|
|
963
|
+
if (searchbox) {{
|
|
964
|
+
searchbox.addEventListener("input", () => {{
|
|
965
|
+
const value = searchbox.value.trim().toLowerCase();
|
|
966
|
+
cards.forEach(card => {{
|
|
967
|
+
card.style.display = !value || card.innerText.toLowerCase().includes(value) ? "" : "none";
|
|
968
|
+
}});
|
|
969
|
+
}});
|
|
970
|
+
}}
|
|
971
|
+
</script>
|
|
972
|
+
</body>
|
|
973
|
+
</html>
|
|
974
|
+
"""
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
def main() -> None:
|
|
978
|
+
parser = argparse.ArgumentParser(description="生成深知可信搜索可交互可信溯源 HTML")
|
|
979
|
+
parser.add_argument("input_json", help="trusted_search.py 或 deep_query.py 的 --json-only 输出 JSON")
|
|
980
|
+
parser.add_argument("--output", help="输出 HTML 路径;不传时根据问题自动生成短文件名到 official-docs/output/")
|
|
981
|
+
parser.add_argument("--title", default="深知可信搜索可信溯源", help="页面标题")
|
|
982
|
+
parser.add_argument("--answer-file", help="最终回答正文文件。复杂任务综合后必须传入,确保 HTML 展示的答案与聊天答案一致。")
|
|
983
|
+
parser.add_argument("--clean-md-output", help="输出干净 Markdown 路径;内容来自同一份最终答案,并移除 [1]、【1】等溯源角标。")
|
|
984
|
+
parser.add_argument("--question", default="", help="用户问题,用于生成顶部对话气泡。")
|
|
985
|
+
args = parser.parse_args()
|
|
986
|
+
|
|
987
|
+
payload = load_json(_safe_input_path(args.input_json, {".json"}))
|
|
988
|
+
answer_override = ""
|
|
989
|
+
if args.answer_file:
|
|
990
|
+
answer_override = _safe_input_path(args.answer_file, {".txt", ".md"}).read_text(encoding="utf-8")
|
|
991
|
+
question = args.question.strip() or extract_question(unwrap(payload))
|
|
992
|
+
generated_at = datetime.now()
|
|
993
|
+
if args.output:
|
|
994
|
+
output = _safe_output_path(args.output, {".html", ".htm"}, ".html")
|
|
995
|
+
else:
|
|
996
|
+
output = _safe_output_path(safe_output_filename(question, generated_at), {".html", ".htm"}, ".html")
|
|
997
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
998
|
+
output.write_text(render_html(payload, args.title, answer_override=answer_override, question_override=args.question, generated_at=generated_at), encoding="utf-8")
|
|
999
|
+
print(f"已生成:{output}")
|
|
1000
|
+
if args.clean_md_output:
|
|
1001
|
+
clean_output = _safe_output_path(args.clean_md_output, {".md"}, ".md")
|
|
1002
|
+
else:
|
|
1003
|
+
clean_output = _safe_output_path(output.with_suffix(".clean.md").name, {".md"}, ".md")
|
|
1004
|
+
clean_output.parent.mkdir(parents=True, exist_ok=True)
|
|
1005
|
+
clean_answer = strip_citation_markers(answer_override or extract_answer(unwrap(payload)))
|
|
1006
|
+
clean_output.write_text(clean_answer, encoding="utf-8")
|
|
1007
|
+
print(f"已生成干净 Markdown:{clean_output}")
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
if __name__ == "__main__":
|
|
1011
|
+
main()
|