Refactor: split run_freshrss_pipeline into internal helpers

Extract three internal helpers to reduce the main function from ~330
lines to ~80 lines:
- _process_item(): handles per-item extract/summarize/filter/build
- _build_and_persist_delivery(): builds payload and persists keyword index
- _build_run_report(): assembles status counts and run report dict

No behavior changes. External signature and return shape unchanged.
This commit is contained in:
wdm
2026-03-29 16:27:11 +08:00
parent 78039eff47
commit 3d58408687
3 changed files with 279 additions and 135 deletions
+54
View File
@@ -0,0 +1,54 @@
# 重构计划:拆分 run_freshrss_pipeline 为内部辅助函数
## 背景
`src/summary_mcp/workflows/freshrss_pipeline.py` 中的 `run_freshrss_pipeline` 函数约 330 行,
将 6 个阶段全部写在一个函数体内,阅读、测试和后续扩展(如并发、重试策略)都比较困难。
目标是在不改变任何外部行为的前提下,提取 3 个内部辅助函数。
当前无测试覆盖,验证方式为函数签名和返回值结构保持不变。
## 涉及文件
- `src/summary_mcp/workflows/freshrss_pipeline.py`(唯一修改文件)
## 提取 3 个内部辅助函数
### 1. `_process_item(...)` — 单条 item 处理(当前 160-258 行)
提取 for 循环体(约 100 行)为独立函数。
返回 `item_report` dict;当 status 为 `delivered` 时,额外携带 `_candidate` 和 `_external_id`
两个临时键供调用方解包,写盘前剥离这两个键。
用 `return item_report` 替代循环中的 `continue`。
### 2. `_build_and_persist_delivery(...)` — 阶段 4+5(当前 260-279 行)
提取 payload 构建 + 词元索引持久化。
返回 `(delivery_payload, keyword_index_result)`。
### 3. `_build_run_report(...)` — 报告组装(当前 288-308 行)
提取 status_counts 统计 + report dict 构建。
返回 report dict,主函数拿到后再调用 `_save_json` 写盘。
## 重构后主函数结构(约 80 行)
1. 阶段 1:初始化(不变)
2. 阶段 2:拉取 FreshRSS + 加载规则/context(不变)
3. 阶段 3:for 循环调用 `_process_item(...)`,从返回值解包 candidate
4. 阶段 4+5:`delivery_payload, keyword_index_result = _build_and_persist_delivery(...)`
5. 阶段 6:标记已读,`report = _build_run_report(...)`,写盘,返回
## 约束
- `run_freshrss_pipeline` 外部签名不变
- 返回 dict 的键结构不变
- 所有文件写入路径不变
- 纯结构性重构,无行为变化
- 无需新增 import
## 验证
重构完成后运行:
python -c "from summary_mcp.workflows.freshrss_pipeline import run_freshrss_pipeline; print('ok')"