fix(定时任务): 模板子页面空内容回退原数据,避免子页面显示不全

- 引擎 merge_with_reference:按顶层键(=子页面)逐一合并,转换结果里没抽到有效内容的
  子页面/字段回退模板原有数据(_has_content/_merge_fill 递归,列表/标量整体保留,
  字典逐键回退空分支);整体为空等价于返回模板默认数据
- _run_html_template_reference:描述与任务prompt都为空→直接用原数据不检索;转换成功后
  对结果做 merge_with_reference 补空子页面
- _run_template_fill_attempt 同样套用 merge,空子页面一致回退
- 测试覆盖 _has_content/merge 各场景 + 端到端空子页面回退/空描述用原数据

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
董宏凯andClaude Opus 4.8 committed 2026-06-24 12:20:12 +08:00
1 parent ba885ca201
commit 7d9a0637ca
3 files changed
+69 -4

No files matched your search

@@ -302,6 +302,50 @@ def field_coverage_gaps(template_id: str, obj: Any, *, max_items: int = 40) -> l
return gaps
def _has_content(value: Any) -> bool:
"""该值是否含「有效内容」(用于判断子页面 / 字段是否抽到东西)。
空 / None / 空串 / 全空的列表或字典 视为无内容;数字(含 0)、布尔、非空字符串
视为有内容。
"""
if value is None:
return False
if isinstance(value, str):
return bool(value.strip())
if isinstance(value, (list, tuple)):
return any(_has_content(v) for v in value)
if isinstance(value, dict):
return any(_has_content(v) for v in value.values())
return True # 数字 / 布尔 / 其它标量
def _merge_fill(got: Any, ref: Any) -> Any:
"""用 ``got`` 覆盖,但 ``got`` 里「没有有效内容」的部分回退 ``ref``。
字典逐键递归(空分支回退);列表/标量有内容就整体保留 ``got``(列表不逐元素合并,
避免与原模板元素错配)。
"""
if not _has_content(got):
return ref
if isinstance(ref, dict) and isinstance(got, dict):
out = dict(got) # 保留 got 里 ref 没有的额外键
for key, ref_val in ref.items():
out[key] = _merge_fill(got.get(key), ref_val)
return out
return got
def merge_with_reference(template_id: str, data: Any) -> dict[str, Any]:
"""用转换结果覆盖,但「没抽到有效内容」的子页面 / 字段回退模板原有数据。
顶层键即子页面:某子页面(及其内部空字段)无有效内容时,用模板自带的原始数据补上,
保证页面与子页面不空白。整体为空时等价于返回模板默认数据。
"""
ref = reference_data(template_id)
merged = _merge_fill(data, ref)
return merged if isinstance(merged, dict) else dict(ref)
def _dump_const(var: str, obj: Any) -> str:
body = json.dumps(obj, ensure_ascii=False)
return f"const {var}={body};"