feat(定时任务): 模板转换支持纯文本/含JSON文本/纯JSON三种源输入

- template_fill.extract_embedded_json:字符串感知平衡扫描,从含 JSON 的文本里抠出
  内嵌 {…}/[…](纯文本返回 None)
- 调度器转换 prompt 预识别内嵌 JSON,单独标注「识别到的结构化数据」并保留原文;
  纯文本则让模型按语义理解;conversion prompt 重写覆盖三种形态
- 内置 agent SOUL 说明三种源数据形态
- 文档同步

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
董宏凯andClaude Opus 4.8 committed 2026-06-23 20:28:05 +08:00
1 parent 8b022bffc5
commit 00181ebde8
5 files changed
+103 -12

No files matched your search

@@ -30,6 +30,73 @@ from functools import lru_cache
from pathlib import Path
from typing import Any
_FENCE_RE = re.compile(r"```(?:json)?\s*(.*?)```", re.DOTALL | re.IGNORECASE)
def _scan_balanced(text: str, open_ch: str, close_ch: str) -> Any | None:
"""从 text 里扫第一个**平衡**的 ``open_ch…close_ch`` 块并 ``json.loads``。
字符串感知(跳过引号内的括号/转义),失败返回 None。供从「含 JSON 的文本」里
把 JSON 段抠出来用。
"""
start = text.find(open_ch)
if start == -1:
return None
depth = 0
in_string = False
escape = False
for i in range(start, len(text)):
ch = text[i]
if in_string:
if escape:
escape = False
elif ch == "\\":
escape = True
elif ch == '"':
in_string = False
continue
if ch == '"':
in_string = True
elif ch == open_ch:
depth += 1
elif ch == close_ch:
depth -= 1
if depth == 0:
blob = text[start : i + 1]
try:
return json.loads(blob)
except (ValueError, TypeError):
return None
return None
def extract_embedded_json(text: str | None) -> Any | None:
"""尽力从一段文本里抽出内嵌的 JSON(对象或数组)。
覆盖三种用户输入:纯 JSON(直接解析)、含 JSON 的文本(抠出第一个平衡的
``{…}`` 或 ``[…]``)、纯文本(返回 ``None``)。也识别 ```json fence。
"""
if not text or not isinstance(text, str):
return None
s = text.strip()
fence = _FENCE_RE.search(s)
if fence:
inner = fence.group(1).strip()
try:
return json.loads(inner)
except (ValueError, TypeError):
s = inner # fence 内不是完整 JSON,继续往下扫
try:
return json.loads(s)
except (ValueError, TypeError):
pass
# 取最靠前出现的那种括号块(对象优先于数组,谁先出现谁优先)。
brace = _scan_balanced(s, "{", "}")
bracket = _scan_balanced(s, "[", "]")
if brace is not None and bracket is not None:
return brace if s.find("{") <= s.find("[") else bracket
return brace if brace is not None else bracket
# 模板资源根目录(随 git 发布,与本模块同级 ``templates/`` 子目录)。
_TEMPLATES_ROOT = Path(__file__).parent / "templates"