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