Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(skillturbo): 响应检视完善结构页 merge 保底与属性槽抽取

xukang9ebac014cd24
diff --git a/jiuwenswarm/server/runtime/skill_turbo/skill_codes/ppt/ppt_page_gen.py b/jiuwenswarm/server/runtime/skill_turbo/skill_codes/ppt/ppt_page_gen.py
index 91a854d2c8..89f31cba56 100644
--- a/jiuwenswarm/server/runtime/skill_turbo/skill_codes/ppt/ppt_page_gen.py
+++ b/jiuwenswarm/server/runtime/skill_turbo/skill_codes/ppt/ppt_page_gen.py
@@ -78,17 +78,17 @@ def _postprocess_structural_template_fill_html(
page_type: str,
seed_html: str = "",
) -> str:
- # 落盘权威:seed 骨架 + 仅 skill 已定义槽;页码策略在 merge 之后再跑,避免 chrome 外标记丢失。
+ # 落盘权威:优先 seed 骨架 + 仅 skill 已定义槽;merge 失败时回退旧门禁校验 LLM html。
if (seed_html or "").strip():
merged = _repair_structural_template_slots(seed_html, html)
- if not merged:
+ if merged:
+ html = merged
+ else:
logger.warning(
- "[P8.1] 结构页 seed-slot-merge 失败 page=%d type=%s",
+ "[P8.1] 结构页 seed-slot-merge 失败 page=%d type=%s;回退旧门禁校验 LLM html",
ctx.page_num,
page_type,
)
- return ""
- html = merged
html = _apply_visible_page_number_policy(
html,
user_query=ctx.user_query,
@@ -972,13 +972,6 @@ def _collect_activated_chart_scaffolds(filled_html: str) -> list[tuple[str, str]
return activated
-def _extract_chart_scaffold_region(filled_html: str) -> str | None:
- activated = _collect_activated_chart_scaffolds(filled_html)
- if not activated:
- return None
- return activated[0][1]
-
-
def _merge_chart_scaffold_from_filled(seed_html: str, filled_html: str) -> str:
"""Merge activated chart scaffolds from filled into seed comment blocks.
@@ -1130,6 +1123,7 @@ def _slice_between_anchors(
_IMG_OPEN_TAG_RE = re.compile(r"<img\b[^>]*", re.IGNORECASE)
+_STRUCTURAL_IMAGE_ATTR_SLOTS = frozenset({"STRUCTURAL_IMAGE_PATH", "STRUCTURAL_IMAGE_ALT"})
def _strip_open_tag_attrs(anchor: str) -> str:
@@ -1138,7 +1132,7 @@ def _strip_open_tag_attrs(anchor: str) -> str:
def _structural_slot_dom_fallback(name: str, filled_html: str) -> str | None:
- """DOM-based fallback for common structural slots when neighbor slice fails."""
+ """DOM extract for common structural slots (attr slots prefer this over adjacency)."""
if name == "PAGE_TITLE":
title = _extract_filled_title_inner(filled_html)
if title and not _has_placeholder_slop(_plain_text_fragment(title)):
@@ -1157,8 +1151,9 @@ def _structural_slot_dom_fallback(name: str, filled_html: str) -> str | None:
if plain and _has_placeholder_slop(plain):
return None
return footer_inner
- # Review: attribute slots (PATH/ALT) need DOM fallback; adjacency is brittle.
- if name in ("STRUCTURAL_IMAGE_PATH", "STRUCTURAL_IMAGE_ALT"):
+ # Attribute slots: values live inside <img ...> attrs; adjacency after
+ # _strip_open_tag_attrs shifts the slice start and can swallow neighboring attrs.
+ if name in _STRUCTURAL_IMAGE_ATTR_SLOTS:
match = re.search(
r"<img\b[^>]*(?:"
r"\bdata-pptx-role\s*=\s*[\"']structural-background[\"']|"
@@ -1253,6 +1248,18 @@ def _repair_structural_template_slots(seed_html: str, filled_html: str) -> str |
left_anchor = _strip_open_tag_attrs(left_anchor)
right_anchor = _strip_open_tag_attrs(right_anchor)
+ # PATH/ALT:DOM 优先。剥离 img 属性后邻接切片起点会前移,happy path
+ # 也会把相邻属性吞进槽值(如 src=" data-pptx-role=...)。
+ if name in _STRUCTURAL_IMAGE_ATTR_SLOTS:
+ value = _structural_slot_dom_fallback(name, filled_html)
+ if value is not None:
+ slots[name] = value
+ if value:
+ loc = filled_html.find(value, search_from)
+ if loc >= 0:
+ search_from = loc + len(value)
+ continue
+
sliced = _slice_between_anchors(
filled_html, left_anchor, right_anchor, search_from=search_from
)
diff --git a/tests/unit_tests/server/runtime/skill_turbo/test_ppt_slot_local_fill_alignment.py b/tests/unit_tests/server/runtime/skill_turbo/test_ppt_slot_local_fill_alignment.py
index 8710e3f469..8bf1a6dc2e 100644
--- a/tests/unit_tests/server/runtime/skill_turbo/test_ppt_slot_local_fill_alignment.py
+++ b/tests/unit_tests/server/runtime/skill_turbo/test_ppt_slot_local_fill_alignment.py
@@ -8,6 +8,7 @@ import pytest
from jiuwenswarm.server.runtime.skill_turbo.skill_codes.ppt.ppt_page_gen import (
_postprocess_content_template_fill_html,
+ _postprocess_structural_template_fill_html,
_repair_content_template_chrome,
_repair_structural_template_slots,
_validate_content_template_fill_output,
@@ -212,9 +213,43 @@ def test_structural_seed_slot_merge_duplicate_title_and_empty_optional_slot():
assert merged.count("双标题页") >= 2
assert "{{PAGE_TITLE}}" not in merged
assert "{{STRUCTURAL_IMAGE_PATH}}" not in merged
+ assert 'src=""' in merged
+ assert 'alt=""' in merged
+ assert "src=\" data-pptx-role=" not in merged
assert "正文区" in merged
+def test_structural_seed_slot_merge_perfect_fill_keeps_clean_image_attrs():
+ """Happy path:LLM 逐字保留骨架仅换 token 时,PATH/ALT 须干净,不得吞入相邻属性。"""
+ seed = """<!DOCTYPE html>
+<html><head><title>{{PAGE_TITLE}}</title>
+<script>tailwind.config={theme:{extend:{colors:{brand:'#c00'}}}}</script>
+<style>@layer utilities{.ppt-slide{width:1280px}}</style>
+</head>
+<body>
+<div class="ppt-slide w-[1280px] h-[720px]">
+ <h1>{{PAGE_TITLE}}</h1>
+ <div class="body">{{PAGE_CONTENT}}</div>
+ <img data-pptx-role="structural-background" src="{{STRUCTURAL_IMAGE_PATH}}" alt="{{STRUCTURAL_IMAGE_ALT}}"/>
+</div>
+</body></html>
+"""
+ filled = (
+ seed.replace("{{PAGE_TITLE}}", "封面标题")
+ .replace("{{PAGE_CONTENT}}", "<p>封面导语</p>")
+ .replace("{{STRUCTURAL_IMAGE_PATH}}", "assets/cover.png")
+ .replace("{{STRUCTURAL_IMAGE_ALT}}", "封面背景")
+ )
+ merged = _repair_structural_template_slots(seed, filled)
+ assert merged is not None
+ assert 'src="assets/cover.png"' in merged
+ assert 'alt="封面背景"' in merged
+ assert 'src=" data-pptx-role=' not in merged
+ assert 'data-pptx-role="structural-background" src="assets/cover.png"' in merged
+ assert merged.count("src=") == 1
+ assert merged.count("alt=") == 1
+
+
def test_structural_seed_slot_merge_survives_img_attr_reorder():
"""属性槽:LLM 把 img 属性换序时仍须 merge 成功(DOM fallback,非整页写盘)。"""
seed = """<!DOCTYPE html>
@@ -247,6 +282,8 @@ def test_structural_seed_slot_merge_survives_img_attr_reorder():
assert merged is not None
assert 'src="assets/cover-bg.png"' in merged
assert 'alt="封面背景"' in merged
+ assert 'src=" data-pptx-role=' not in merged
+ assert merged.count("src=") == 1
assert "封面标题" in merged
assert "{{STRUCTURAL_IMAGE_PATH}}" not in merged
assert "{{STRUCTURAL_IMAGE_ALT}}" not in merged
@@ -288,4 +325,59 @@ def test_structural_seed_slot_merge_survives_img_reindent():
assert merged is not None
assert 'src="assets/reindent.png"' in merged
assert 'alt="重缩进背景"' in merged
+ assert 'src=" data-pptx-role=' not in merged
+ assert merged.count("src=") == 1
assert "{{STRUCTURAL_IMAGE" not in merged
+
+
+def test_structural_postprocess_falls_back_to_old_gates_when_merge_fails():
+ """整体重缩进致邻接切片失败、且无 <main> 时 merge 失败;旧门禁通过则仍落盘 LLM html。"""
+ seed = _STRUCTURAL_SEED_HTML
+ filled = """<!DOCTYPE html>
+<html><head><title>整体重缩进封面</title>
+<script>tailwind.config={theme:{extend:{colors:{brand:'#c00'}}}}</script>
+<style type="text/tailwindcss">@layer utilities{.ppt-slide{@apply relative w-[1280px] h-[720px];}
+.content-safe{width:1220px}</style>
+</head>
+<body>
+<div class="ppt-slide w-[1280px] h-[720px]">
+<div class="content-safe cover-stage">
+<h1 class="cover-title">
+整体重缩进封面
+</h1>
+<div class="cover-body">
+<p>副标题与简介足够长以通过校验</p>
+</div>
+</div>
+</div>
+</body></html>
+"""
+ assert _repair_structural_template_slots(seed, filled) is None
+ ctx = _Ctx()
+ ctx.page_num = 1
+ out = _postprocess_structural_template_fill_html(
+ filled, ctx, "cover", seed_html=seed
+ )
+ assert out
+ assert "整体重缩进封面" in out
+ assert "{{PAGE_TITLE}}" not in out
+ assert "{{PAGE_CONTENT}}" not in out
+
+
+def test_structural_postprocess_merge_fail_still_rejects_unfilled_placeholders():
+ """merge 失败回退旧门禁时,残留 {{}} 仍须拒绝,不得假成功。"""
+ seed = _STRUCTURAL_SEED_HTML
+ filled = seed.replace("{{PAGE_TITLE}}", "只填了标题")
+ # PAGE_CONTENT 仍为 {{PAGE_CONTENT}};整体改缩进使邻接切片也失败
+ filled = filled.replace(
+ ' <div class="cover-body">{{PAGE_CONTENT}}</div>\n',
+ '<div class="cover-body">{{PAGE_CONTENT}}</div>\n',
+ 1,
+ )
+ assert _repair_structural_template_slots(seed, filled) is None
+ ctx = _Ctx()
+ ctx.page_num = 1
+ out = _postprocess_structural_template_fill_html(
+ filled, ctx, "cover", seed_html=seed
+ )
+ assert out == ""
diff --git a/tests/unit_tests/server/runtime/skill_turbo/test_pptx_craft_compat_adapt.py b/tests/unit_tests/server/runtime/skill_turbo/test_pptx_craft_compat_adapt.py
index 3c93abe40f..ec997fe8b4 100644
--- a/tests/unit_tests/server/runtime/skill_turbo/test_pptx_craft_compat_adapt.py
+++ b/tests/unit_tests/server/runtime/skill_turbo/test_pptx_craft_compat_adapt.py
@@ -15,7 +15,7 @@ from jiuwenswarm.server.runtime.skill_turbo.skill_codes.ppt.ppt_page_gen import
_build_content_template_fill_prompt,
_build_content_template_fill_system_prompt,
_chart_activation_incomplete,
- _extract_chart_scaffold_region,
+ _collect_activated_chart_scaffolds,
_extract_designer_section,
_filled_chart_scaffold_is_progressed,
_is_chart_candidate_page,
@@ -326,7 +326,7 @@ def test_custom_chart_prompt_mentions_chart_font_family():
assert "须按" in prompt and "style-custom.md" in prompt
-def test_extract_chart_scaffold_region_ignores_scripts_after_body_with_prior_comment():
+def test_collect_activated_chart_scaffolds_ignores_scripts_after_body_with_prior_comment():
"""</body> 前有 HTML 注释时,须在去注释坐标系内截断,避免误取 body 后的 script。"""
scaffold_script = (
'<script>const option = {"series":[{"data":[1]}]}; '
@@ -345,8 +345,9 @@ def test_extract_chart_scaffold_region_ignores_scripts_after_body_with_prior_com
f"{decoy_script}"
"</html>"
)
- region = _extract_chart_scaffold_region(filled)
- assert region is not None
+ activated = _collect_activated_chart_scaffolds(filled)
+ assert len(activated) == 1
+ _target_id, region = activated[0]
assert 'getElementById("chart-1")' in region
assert 'getElementById("decoy")' not in region
assert "[1]" in region
@@ -452,7 +453,7 @@ def test_chart_scaffold_path_b_ignores_main_inline_echarts_init():
"</body></html>"
)
assert _filled_chart_scaffold_is_progressed(filled) is False
- assert _extract_chart_scaffold_region(filled) is None
+ assert _collect_activated_chart_scaffolds(filled) == []
merged = _merge_chart_scaffold_from_filled(seed, filled)
assert "CHART_SCAFFOLD_BEGIN" in merged
assert "const option = null" in merged