feat: 优化周记周期与总结输出

This commit is contained in:
Benature
2026-11-21 00:52:00 +08:00
parent 81b6ff7a39
commit f177a857a5
6 changed files with 110 additions and 30 deletions

View File

@ -178,6 +178,18 @@ def require_efund_model(model: str) -> str:
return model
def clean_model_output(content: str) -> str:
"""Keep only the final answer after a model reasoning block."""
if "<think>" in content and "</think>" not in content:
raise SummaryError("Magpie 返回了未闭合的思考内容,已拒绝写入。")
if "</think>" in content:
content = content.rsplit("</think>", 1)[1]
content = content.strip()
if not content:
raise SummaryError("Magpie 返回了空总结。")
return content
def resolve_day(day: str | None, timezone: str) -> tuple[date, datetime, datetime, ZoneInfo]:
try:
tz = ZoneInfo(timezone)
@ -303,18 +315,34 @@ def resolve_people_aliases(config: dict[str, Any]) -> dict[str, str]:
return resolved
def render_output_template(template: str, kind: str, target_day: date) -> str:
"""Support legacy {date}/{week} and YYYY-MM-DD / YYYY-WW templates."""
if not isinstance(template, str) or not template.strip():
raise SummaryError(f"YAML 的 output.{kind} 必须是非空路径字符串。")
week_label = f"{target_day.year}-W{target_day.strftime('%U')}"
try:
rendered = template.format(
date=target_day.isoformat(), week=week_label
)
except (KeyError, IndexError, ValueError) as exc:
raise SummaryError(f"YAML 的 output.{kind} 路径模板不正确。") from exc
for token, value in {
"YYYY": f"{target_day.year:04d}",
"MM": f"{target_day.month:02d}",
"DD": f"{target_day.day:02d}",
"WW": target_day.strftime("%U"),
}.items():
rendered = rendered.replace(token, value)
return rendered
def configured_output_path(
config: dict[str, Any], kind: str, target_day: date
) -> Path | None:
template = config.get(kind)
if template is None:
return None
if not isinstance(template, str) or not template.strip():
raise SummaryError(f"YAML 的 output.{kind} 必须是非空路径字符串。")
try:
rendered = template.format(date=target_day.isoformat())
except (KeyError, IndexError, ValueError) as exc:
raise SummaryError(f"YAML 的 output.{kind} 路径模板不正确。") from exc
rendered = render_output_template(template, kind, target_day)
return Path(os.path.expandvars(rendered)).expanduser()
@ -374,6 +402,8 @@ def load_previous_summary(
f"【上周最后工作日 {workday.isoformat()}】\n"
f"{workday_content or '<日志不存在>'}",
]
# Monday has valid previous context when either Sunday or the last workday exists.
previous_missing = previous_content is None and workday_content is None
return "\n\n".join(sections), previous_missing
@ -558,7 +588,7 @@ class MagpieClient:
content = message.get("content") if isinstance(message, dict) else None
if not isinstance(content, str) or not content.strip():
raise SummaryError("Magpie 返回了空图片读取结果。")
return content.strip()
return clean_model_output(content)
def complete_with_image(self, image: bytes, mime_type: str) -> str:
"""Read one image through the approved multimodal model."""
@ -597,7 +627,7 @@ class MagpieClient:
)
if not isinstance(content, str) or not content.strip():
raise SummaryError("Magpie 返回了空总结。")
return content.strip()
return clean_model_output(content)
def normalize_self_name(content: str, self_name: str, own_names) -> str: