Files
ewoooc/routes
OoO c7d6db31f2
Some checks are pending
CD Pipeline / deploy (push) Has started running
feat(p11): RAG 自主學習 + Promotion Gate 4 階段護欄(feature flag OFF)
Operation Ollama-First v5.0 / Phase 11 / RAG 自主學習迴圈

services/rag_service.py (532 行)
- RAGService.query() — bge-m3 embed + cosine 0.85 threshold + top_k=5
- get_embedding_signature() — v5.0 護欄 #3 一致性檢查 (SHA1[:12])
- fire-and-forget rag_query_log INSERT (不阻塞主流程)
- feedback() — Telegram 👍/👎 寫回 feedback_score
- RAG_ENABLED 預設 OFF(戰前行為不變)

services/learning_pipeline.py (750 行)
- Distiller — 純 Hermes 規則引擎,零 LLM 成本
  Quality 規則:MCP >200 字 0.8 / LLM JSON ok 0.9 / TextRank 0.6 / 👍 1.0 / 👎 0.0
- PromotionGate — Owen v5.0 護欄 #1 鐵律
  Stage 1: quality_score >= 0.7
  Stage 2: 無幻覺檢測(規則引擎,零 LLM)
  Stage 3: 與既有 insight 相似度 < 0.95(Stage 3 在 episode embed 後啟用)
  Stage 4: weight >= 0.8 必經 Telegram 👍/👎
- expire_stale_reviews() — 24h 無回應自動降級 weight=0.5
- hash_human_approver — Telegram username SHA1[:8] PII 保護

services/hermes_analyst_service.py — 新增 analyze() RAG-first
- RAG hit → return synthesize(不燒 LLM)
- RAG miss → 既有 LLM 路徑 + enqueue learning_episodes

services/openclaw_strategist_service.py — Q&A 入口接 RAG-first
- 不動週/月/年報(敘事報告 RAG hit 機率低)

services/telegram_templates.py
- rag_feedback_keyboard() — 👍/👎 inline keyboard
- promotion_review_keyboard() — Stage 4 人工驗收按鈕

routes/openclaw_bot_routes.py — 3 組 callback handler
- rag_fb:{id}:{score} → rag_service.feedback()
- pg_ok:{episode_id} → PromotionGate.promote()
- pg_no:{episode_id} → PromotionGate.reject()

70 unit tests 全綠 + 全戰役 196 tests zero regression(4:17 跑完)

剩餘 limitations(Phase 12+ 補):
1. learning_episodes.embedding 寫入路徑(Stage 3 dedup 暫 skip)
2. PromotionGate worker cron 未掛
3. Telegram awaiting_review 推播未接(callback handler 已就位)

灰度開啟條件(建議 1 週後):
- ANTHROPIC_API_KEY 設定 + RAG_ENABLED=true + threshold=0.90 保守
- feedback_score >= 4 比率 > 70% → threshold 降至 0.85

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 23:56:12 +08:00
..

路由模組說明

app.py 直接註冊所有 Flask BlueprintUSE_MODULAR_ROUTESregister_blueprints()MODULAR_ENDPOINTS 與 duplicate cleanup 開關已在 ADR-017 Phase 3f-1 移除。

啟動防線

app.py 會在啟動時檢查 app.url_map,同一組 (URL, HTTP methods) 不允許被兩個 endpoint 重複註冊;若發現衝突會直接 SystemExit

模組清單

模組 說明 主要路由
dashboard_routes.py 商品看板首頁 /
sales_routes.py 業績分析與 ABC 明細 /sales_analysis, /growth_analysis, /abc_analysis/detail, /api/sales_analysis/*
system_public_routes.py 無 prefix 公開系統頁與監控 /health, /metrics, /ai_automation_smoke, /api/ai-automation/smoke*, /settings, /system_settings, /logs, /api/logs, /api/backup
system_routes.py 內部系統維護 API /api/system/*
edm_routes.py EDM 與節慶儀表板 /edm, /festival
monthly_routes.py 月結分析 /monthly_summary_analysis, /api/monthly_summary_data
daily_sales_routes.py 當日業績 /daily_sales, /daily_sales/export*
api_routes.py 通用任務與查詢 API /api/run_task, /api/history/*
export_routes.py 匯出功能 /api/export/*
import_routes.py 匯入功能 /api/import_excel, /api/import/monthly_summary

新增 route 時請優先放入對應 Blueprint並用本機 app.url_map duplicate check 驗證。 若 route 內開始出現大量資料處理、SQL、AI prompt 或外部 API 呼叫,請依 docs/guides/modularization_governance.md 抽到 services/utils/,不要讓 Blueprint 變成新巨檔。