feat: add summary configuration and sync controls

This commit is contained in:
Benature
2026-11-07 18:46:23 +08:00
parent 3e5ac07183
commit 99ee792c07
10 changed files with 1782 additions and 60 deletions

View File

@ -207,6 +207,40 @@ def test_summary_is_per_conversation_before_daily_combination():
assert "私聊原始内容" not in prompts[2]
def test_summary_applies_chat_specific_prompts_to_each_relevant_stage():
session = FakeSession(
completions=[
(DEFAULT_MODEL, "业务监控摘要"),
(DEFAULT_MODEL, "普通群摘要"),
(DEFAULT_MODEL, "最终日度总结"),
]
)
client = MagpieClient(DEFAULT_MODEL, session=session)
rows = [
synthetic_row(chat_id="monitor", chat_name="监控群"),
synthetic_row("m2", chat_id="regular", chat_name="普通群"),
]
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
custom_prompt = "放量提醒是业务监控重点,不得省略。"
result = summarize_messages(
client,
rows,
date(2026, 11, 3),
tz,
chat_prompts={"monitor": custom_prompt},
)
assert result == "最终日度总结"
prompts = [
kwargs["json"]["messages"][1]["content"]
for _, kwargs in session.post_calls
]
assert custom_prompt in prompts[0]
assert custom_prompt not in prompts[1]
assert custom_prompt in prompts[2]
def test_summary_enriches_names_and_requests_obsidian_wikilinks():
session = FakeSession(
completions=[
@ -247,6 +281,32 @@ def test_summary_enriches_names_and_requests_obsidian_wikilinks():
assert "Wikilink" in final_prompt
def test_summary_normalizes_people_aliases_after_both_model_stages():
session = FakeSession(
completions=[
(DEFAULT_MODEL, "伦总发送了文件"),
(DEFAULT_MODEL, "[[伦总]] 最终确认"),
]
)
client = MagpieClient(DEFAULT_MODEL, session=session)
rows = [synthetic_row(chat_id="group-a", chat_name="项目群")]
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
result = summarize_messages(
client,
rows,
date(2026, 11, 3),
tz,
contacts={"group-a": {"sender-test": "万轶伦"}},
people_aliases={"伦总": "万轶伦"},
)
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
assert "[[万轶伦]]发送了文件" in final_prompt
assert "伦总" not in final_prompt
assert result == "[[万轶伦]] 最终确认"
def test_storage_selects_only_received_messages_inside_day_and_chat():
storage = Storage(url="sqlite:///:memory:")
tz = ZoneInfo("Asia/Shanghai")
@ -324,27 +384,41 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
{**base, "msg_id": "outgoing", "content": "合成发出内容"}, direction="out"
)
monkeypatch.setenv("LARKX_STORAGE_URL", database_url)
routed_model = "maqqie/gg-chat-efund/EFundGPT-dsv4pro"
session = FakeSession(
completions=[(DEFAULT_MODEL, "单对话摘要"), (DEFAULT_MODEL, "# 合成总结")]
models=[routed_model],
completions=[(routed_model, "单对话摘要"), (routed_model, "# 合成总结")],
)
monkeypatch.setattr("snooper_summary.requests.Session", lambda: session)
output = tmp_path / "summary.md"
dry_run = tmp_path / "dry-runs" / "2026-11-03.json"
dry_run.parent.mkdir()
dry_run.write_text("合成 dry-run", encoding="utf-8")
config_file = tmp_path / "config.yaml"
config_file.write_text(
f"output:\n dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n",
encoding="utf-8",
)
args = argparse.Namespace(
date="2026-11-03",
timezone="Asia/Shanghai",
model=DEFAULT_MODEL,
model=routed_model,
base_url="http://127.0.0.1:3425/v1",
timeout=1,
chat=None,
max_chars=80000,
output=output,
config=config_file,
check=False,
dry_run=False,
no_contacts=True,
no_fetch=True,
)
assert cmd_summarize(args) == 0
assert output.read_text(encoding="utf-8") == "# 合成总结\n"
assert output.read_text(encoding="utf-8") == (
"---\nmodel: EFundGPT-dsv4pro\n---\n\n# 合成总结\n"
)
assert not dry_run.exists()
request_text = session.post_calls[0][1]["json"]["messages"][1]["content"]
assert "合成收到内容" in request_text
assert "合成发出内容" not in request_text
@ -419,7 +493,16 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
monkeypatch.setattr("lark_snooper.LarkAuth", lambda: object())
sync_calls = []
def fake_sync(client, storage, target_day, tz, batch_size=50, chat_ids=None):
def fake_sync(
client,
storage,
target_day,
tz,
batch_size=50,
chat_ids=None,
exclude_chat_ids=None,
force=False,
):
assert len(session.get_calls) == 1 # EFundGPT preflight still comes first.
_, start, _, _ = resolve_summary_day("2026-11-03", "Asia/Shanghai")
storage.save_message(
@ -435,11 +518,43 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
},
direction="in",
)
sync_calls.append({"batch_size": batch_size, "chat_ids": chat_ids})
return {"failed": 0, "matched": 1, "saved": 1, "chats": 1, "requests": 1}
storage.save_message(
{
"msg_id": "excluded",
"chat_id": "chat-excluded",
"chat_type": 2,
"from_id": "sender-synthetic",
"msg_type": 4,
"msg_type_name": "TEXT",
"create_time": int(start.timestamp()) + 120,
"content": "不应读取的排除群消息",
},
direction="in",
)
sync_calls.append(
{
"batch_size": batch_size,
"chat_ids": chat_ids,
"exclude_chat_ids": exclude_chat_ids,
}
)
return {
"failed": 0,
"matched": 1,
"saved": 1,
"chats": 1,
"requests": 1,
"skipped": 0,
}
monkeypatch.setattr("snooper_history.sync_all_chats_for_day", fake_sync)
output = tmp_path / "summary.md"
config_file = tmp_path / "config.yaml"
config_file.write_text(
f"output:\n dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n"
"chats:\n exclude:\n - 'chat-excluded'\n",
encoding="utf-8",
)
args = argparse.Namespace(
date="2026-11-03",
timezone="Asia/Shanghai",
@ -447,18 +562,31 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
base_url="http://127.0.0.1:3425/v1",
timeout=1,
chat=["chat-synthetic"],
config=config_file,
max_chars=80000,
output=output,
check=False,
dry_run=False,
no_contacts=True,
no_fetch=False,
force_fetch=False,
)
assert cmd_summarize(args) == 0
assert sync_calls == [{"batch_size": 50, "chat_ids": ["chat-synthetic"]}]
assert sync_calls == [
{
"batch_size": 50,
"chat_ids": ["chat-synthetic"],
"exclude_chat_ids": ["chat-excluded"],
}
]
assert len(session.post_calls) == 2
assert output.read_text(encoding="utf-8") == "# 合成总结\n"
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}\n---\n\n# 合成总结\n"
)
def test_cli_model_failure_happens_before_storage_is_opened(monkeypatch):