feat: 增加日历与版本化日报

This commit is contained in:
Benature
2026-11-10 21:55:50 +08:00
parent e1d222a3fd
commit c03d935e9e
10 changed files with 585 additions and 70 deletions

View File

@ -17,9 +17,11 @@ from snooper_summary import (
escape_obsidian_tags,
format_followup_checkboxes,
load_previous_summary,
normalize_heading_breaks,
previous_workday,
require_efund_model,
resolve_day,
shorten_headings,
summarize_messages,
write_summary,
)
@ -353,12 +355,14 @@ def test_summary_orders_chats_by_participation_private_human_and_bot():
assert '"priority_rank": 4' in prompts[3]
assert '"has_bot_messages": true' in prompts[3]
final_prompt = prompts[4]
assert "## 七、运维信息(必须位于全文最后)" in final_prompt
assert "## 三、前日工作延续" in final_prompt
assert "## 八、运维信息(必须位于全文最后)" in final_prompt
assert "## 四、前日工作延续" in final_prompt
assert "## 二、今日会议" in final_prompt
assert "三级标题保持简短" in final_prompt
assert "> [!note]" in final_prompt
assert "> [!warning]" in final_prompt
assert "仅知情、他人负责、一般通知和机器人监控无需跟进" in final_prompt
assert "今日工作概览只写工作主线、关键进展和结果,不列举人名或群名" in final_prompt
assert final_prompt.index("本人参与群摘要") < final_prompt.index("私聊摘要")
assert final_prompt.index("私聊摘要") < final_prompt.index("真人群摘要")
assert final_prompt.index("真人群摘要") < final_prompt.index("机器人群摘要")
@ -367,7 +371,7 @@ def test_summary_orders_chats_by_participation_private_human_and_bot():
def test_private_chat_labels_keep_only_the_other_party():
session = FakeSession(
completions=[
(DEFAULT_MODEL, "排班([[王五]])\n模型([[王五]]、[[赵六]])"),
(DEFAULT_MODEL, "### 排班\n### 模型([[王五]]、[[赵六]])"),
(DEFAULT_MODEL, "| 事项 | 负责人 | 来源 |\n| 排班 | 未明确 | [[王五]] |"),
]
)
@ -400,8 +404,8 @@ def test_private_chat_labels_keep_only_the_other_party():
)
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
assert "排班([[赵六]])" in final_prompt
assert "模型([[赵六]])" in final_prompt
assert "### 排班([[赵六]])" in final_prompt
assert "### 模型([[赵六]])" in final_prompt
assert "私聊:" not in final_prompt
assert "[[王五]] 与 [[赵六]]" not in final_prompt
assert result.endswith("| 排班 | 未明确 | [[赵六]] |")
@ -414,10 +418,12 @@ def test_multiple_private_sources_are_repaired_from_conversation_summaries():
(DEFAULT_MODEL, "[[王五]] 私聊:模型选型"),
(
DEFAULT_MODEL,
"### 数据库(未明确)\n"
"| 事项 | 来源 |\n| 数据库迁移 | [[王五]] |\n| 模型选型 | [[王五]] |",
),
(
DEFAULT_MODEL,
"### 数据库([[周八]])\n"
"| 事项 | 来源 |\n| 数据库迁移 | [[周八]] |\n| 模型选型 | [[孙七]] |",
),
]
@ -453,6 +459,7 @@ def test_multiple_private_sources_are_repaired_from_conversation_summaries():
)
assert len(session.post_calls) == 4
assert "### 数据库([[周八]])" in result
assert "| 数据库迁移 | [[周八]] |" in result
assert "| 模型选型 | [[孙七]] |" in result
@ -488,6 +495,54 @@ def test_person_named_group_links_support_commas_and_nested_wikilinks():
assert "[[吴九, [[李四]], 王五]]" not in result
def test_calendar_events_are_included_even_without_messages():
session = FakeSession(completions=[(DEFAULT_MODEL, "会议日报")])
client = MagpieClient(DEFAULT_MODEL, session=session)
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
result = summarize_messages(
client,
[],
date(2026, 11, 3),
tz,
calendar_events=[
{
"summary": "合成会议",
"begin": "2026-11-03T09:00:00+08:00",
"end": "2026-11-03T10:00:00+08:00",
"location": "合成会议室",
"cancelled": False,
"url": "",
}
],
)
assert result == "会议日报"
prompt = session.post_calls[0][1]["json"]["messages"][1]["content"]
assert "合成会议" in prompt
assert "合成会议室" in prompt
assert "## 二、今日会议" in prompt
assert "[会议标题](url)" in prompt
def test_summary_applies_heading_normalization_to_model_output():
session = FakeSession(
completions=[
(DEFAULT_MODEL, "单群摘要"),
(DEFAULT_MODEL, "## 一、今日工作概览今日工作围绕合成事项展开。\n### 合成事项- [[张三]] 确认。\n"),
]
)
client = MagpieClient(DEFAULT_MODEL, session=session)
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
result = summarize_messages(
client, [synthetic_row()], date(2026, 11, 3), tz
)
assert "## 一、今日工作概览\n今日工作围绕合成事项展开。" in result
assert "### 合成事项\n- 张三 确认。" in result
def test_previous_day_summary_is_used_for_continuity():
session = FakeSession(completions=[
(DEFAULT_MODEL, "今日摘要"),
@ -541,13 +596,14 @@ def test_summary_enriches_names_and_requests_obsidian_wikilinks():
system_prompt = session.post_calls[0][1]["json"]["messages"][0]["content"]
first_prompt = session.post_calls[0][1]["json"]["messages"][1]["content"]
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
assert '"sender_name": "张三"' in first_prompt
assert '"full_name": "张三"' in first_prompt
assert "唯一匹配" in system_prompt
assert "[[完整姓名]]" in system_prompt
assert '"sender_name": "张三"' not in first_prompt
assert '"full_name"' not in first_prompt
assert '"sender_type": 0' in first_prompt
assert "不通过通讯录强行补全本名" in system_prompt
assert "[[会话名]]" in system_prompt
assert "#tag" in system_prompt
assert "[[张三]]确认了事项" in final_prompt
assert "[[张三]]确认了事项" in result
assert "[[张三]]" not in final_prompt
assert "[[张三]]" not in result
assert "Wikilink" in final_prompt
@ -606,9 +662,9 @@ def test_summary_normalizes_people_aliases_after_both_model_stages():
)
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
assert "[[李四]]发送了文件" in final_prompt
assert "四总" not in final_prompt
assert result == "[[李四]] 最终确认"
assert "四总发送了文件" in final_prompt
assert "[[李四]]" not in final_prompt
assert result == "四总 最终确认"
def test_unconfirmed_person_wikilink_is_removed_but_group_link_is_preserved():
@ -739,6 +795,58 @@ def test_previous_workday_skips_custom_holidays_and_weekends():
) == date(2026, 10, 29)
def test_heading_body_is_separated_before_length_normalization():
content = "### 简短标题([[张三]])- 第一条正文\n- 第二条正文\n"
assert normalize_heading_breaks(content) == (
"### 简短标题([[张三]])\n- 第一条正文\n- 第二条正文\n"
)
assert shorten_headings(content, max_chars=8) == (
"### 简短标题([[张三]])\n- 第一条正文\n- 第二条正文\n"
)
def test_heading_breaks_cover_sections_and_plain_wikilink_bodies():
content = (
"## 一、今日工作概览今日工作围绕数据治理展开。\n"
"### 数据治理[[张三]] 提醒更新数据。\n"
"### 简短标题- [[张三]] 提醒更新数据。\n"
"### 部门扩容[[张三]] 新增集群(无自杀逻辑)。[[李四]]确认。\n"
"### 简短标题([[李四]])\n"
)
assert normalize_heading_breaks(content) == (
"## 一、今日工作概览\n今日工作围绕数据治理展开。\n"
"### 数据治理\n[[张三]] 提醒更新数据。\n"
"### 简短标题\n- [[张三]] 提醒更新数据。\n"
"### 部门扩容\n[[张三]] 新增集群(无自杀逻辑)。[[李四]]确认。\n"
"### 简短标题([[李四]])\n"
)
def test_third_level_headings_are_hard_limited_without_cutting_wikilinks():
content = (
"### 一二三四五六七八九十([[张三]])\n"
"### 一二三四五六七八九十\n"
"## 二级标题不截断\n"
)
assert shorten_headings(content) == content
assert shorten_headings(content, max_chars=8) == (
"### 一二三四五六七…([[张三]])\n"
"### 一二三四五六七…\n"
"## 二级标题不截断\n"
)
def test_followup_heading_is_separated_before_checkbox_conversion():
content = "## 六、明日跟进建议- [ ] 第一项\n2. 第二项\n"
assert format_followup_checkboxes(shorten_headings(content)) == (
"## 六、明日跟进建议\n- [ ] 第一项\n- [ ] 第二项\n"
)
def test_followup_items_are_checkboxes_and_idempotent():
content = ("## 五、明日跟进建议\n"
"1. 第一项\n"
@ -758,6 +866,12 @@ def test_followup_items_are_checkboxes_and_idempotent():
assert format_followup_checkboxes(expected) == expected
def test_obsidian_tag_escaping_preserves_heading_line_endings():
content = "## 标题\n正文\n### 次标题\r\n- 列表\r\n"
assert escape_obsidian_tags(content) == content
def test_obsidian_tag_escaping_preserves_headings_existing_escapes_and_urls():
content = ("# 日报\\n"
"正文 #ETF放量 与 #123,已有 \\#标签。\\n"
@ -806,7 +920,14 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
routed_model = "maqqie/gg-chat-efund/EFundGPT-dsv4pro"
session = FakeSession(
models=[routed_model],
completions=[(routed_model, "单对话摘要"), (routed_model, "# 合成总结")],
completions=[
(routed_model, "单对话摘要"),
(
routed_model,
"## 一、今日工作概览今日工作围绕合成事项展开。\n"
"### 合成事项- [[张三]] 确认。\n# 合成总结",
),
],
)
monkeypatch.setattr("snooper_summary.requests.Session", lambda: session)
output = tmp_path / "summary.md"
@ -825,6 +946,7 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
"---\nmodel: synthetic\n---\n\n# 前日日志\n\n前日合成待办\n",
encoding="utf-8",
)
monkeypatch.setattr("lark_snooper.get_version", lambda: "0.1.0.dev0+test")
args = argparse.Namespace(
date="2026-11-03",
timezone="Asia/Shanghai",
@ -839,10 +961,12 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
dry_run=False,
no_contacts=True,
no_fetch=True,
no_calendar=True,
)
assert cmd_summarize(args) == 0
assert output.read_text(encoding="utf-8") == (
"---\nmodel: EFundGPT-dsv4pro\nis_read: false\n---\n\n# 合成总结\n")
output_text = output.read_text(encoding="utf-8")
assert "## 一、今日工作概览\n今日工作围绕合成事项展开。" in output_text
assert "### 合成事项\n- 张三 确认。" in output_text
assert not dry_run.exists()
request_text = session.post_calls[0][1]["json"]["messages"][1]["content"]
assert "合成收到内容" in request_text
@ -984,6 +1108,7 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
"chats:\n exclude:\n - 'chat-excluded'\n",
encoding="utf-8",
)
monkeypatch.setattr("lark_snooper.get_version", lambda: "0.1.0.dev0+test")
args = argparse.Namespace(
date="2026-11-03",
timezone="Asia/Shanghai",
@ -998,6 +1123,7 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
dry_run=False,
no_contacts=True,
no_fetch=False,
no_calendar=True,
force_fetch=False,
)
@ -1011,9 +1137,10 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
request_text = session.post_calls[0][1]["json"]["messages"][1]["content"]
assert "合成预取消息" in request_text
assert "不应读取的排除群消息" not in request_text
assert output.read_text(
encoding="utf-8") == (f"---\nmodel: {DEFAULT_MODEL}\nis_read: false\n"
"tags:\n- no_previous_day_log\n---\n\n# 合成总结\n")
assert output.read_text(encoding="utf-8") == (
"---\nversion: 0.1.0.dev0+test\n"
f"model: {DEFAULT_MODEL}\nis_read: false\n"
"tags:\n- no_previous_day_log\n---\n\n# 合成总结\n")
def test_cli_model_failure_happens_before_storage_is_opened(monkeypatch):