每日新聞摘要
即時示範 conventions/ 中全部四條工程慣例的完整整合:
- state-file-pattern — STATE.md 跨執行狀態
- control-flow-separation — 抓取用程式碼 / 摘要用 LLM
- maker-checker — Checker 五維驗證摘要品質
- error-compact-pattern — 錯誤不撐爆上下文
工作流程
步驟 1: 讀取狀態(確定性)✅
從 STATE.md 讀取上次執行狀態和幂等鍵(idempotency keys)。
步驟 2: 抓取 RSS(確定性)✅
# 零 LLM 成本。只處理上次執行後新增的文章。
import feedparser
for url in RSS_SOURCES:
feed = feedparser.parse(url)
new = [entry for entry in feed.entries
if entry.id not in state.get("idempotency_keys", [])]
步驟 3: LLM 摘要(機率性)❌
# 每日限 5 篇,控制 token 開銷。
for article in new[:5]:
summary = await llm.complete(
f"按三點總結,每條不超過20字:\n標題:{article.title}\n{article.description}"
)
步驟 4: Checker 驗證(機率性)❌
獨立 Agent 對摘要做五維評分,≥ 40/50 PASS。
步驟 5: 更新狀態(確定性)✅
state["progress"]["articles_summarized"] += len(passed)
state["idempotency_keys"].extend([a.id for a in processed])
atomic_write("STATE.md", state) # 原子寫入 + 檔案鎖
錯誤處理
# 按 error-compact-pattern 壓縮錯誤
try:
feed = feedparser.parse(url)
except Exception as e:
compact = f"[STEP_FAILED] fetch_rss@{now}\n Error: {type(e).__name__} - {str(e)[:80]}\n Recoverable: YES (retry with backoff)"
context.append(compact)
輸出格式
# 新聞摘要 {YYYY-MM-DD}
## 📰 {article.title}
- {point 1}
- {point 2}
- {point 3}