zhoujie b635108b89 refactor: split monolithic main.py into services/ + ui/ modules (improve-maintainability)
- main.py: 4360 → 146 lines (96.6% reduction), entry layer only
- services/: rate_limiter, autostart, persona, connection, profile,
  hotspot, content, engagement, scheduler, queue_ops (10 business modules)
- ui/app.py: all Gradio UI code extracted into build_app(cfg, analytics)
- Fix: with gr.Blocks() indented inside build_app function
- Fix: cfg.all property (not get_all method)
- Fix: STATUS_LABELS, get_persona_keywords, fetch_proactive_notes imports
- Fix: queue_ops module-level set_publish_callback moved into configure()
- Fix: pub_queue.format_*() wrapped as queue_format_table/calendar helpers
- All 14 files syntax-verified, build_app() runtime-verified
- 58/58 tasks complete"
2026-02-24 22:50:56 +08:00

39 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Why
`main.py` 目前共 4359 行将连接管理、内容生成、自动化运营、调度、队列、UI 等 10+ 个业务域全部混入单一文件,导致阅读困难、修改风险高、模块间依赖不清晰。随着功能继续增长,维护成本将持续上升。现在是在文件进一步膨胀前完成结构化拆分的最佳时机。
## What Changes
- 按业务域将 `main.py` 中的函数提取为独立的 `services/` 模块
- 将剩余 UI Tab 提取为独立的 `ui/tab_*.py` 模块(`tab_create.py` 已完成,需继续完成其余 Tab
- `main.py` 保留为**入口层**:仅负责组装 Gradio UI、注册事件、启动应用
- 所有模块保持向后兼容,不改变对外行为
## Capabilities
### New Capabilities
- `services-connection`: LLM / SD / MCP 连接管理(`connect_llm``connect_sd``check_mcp_status`、登录相关)
- `services-content`: 内容生成(`generate_copy``generate_images``publish_to_xhs``one_click_export`、face image 上传)
- `services-hotspot`: 热点探测(`search_hotspots``analyze_and_suggest``generate_from_hotspot`
- `services-engagement`: 互动自动化(`auto_comment_once``auto_like_once``auto_favorite_once``auto_reply_once` 及对应 `_with_log` 包装)
- `services-rate-limiter`: 频率控制与每日限额(`_reset_daily_stats_if_needed``_check_daily_limit``_is_in_cooldown` 等)
- `services-profile`: 用户主页解析(`fetch_my_profile``_parse_profile_json``_parse_count`
- `services-persona`: 人设管理(`_match_persona_pools``get_persona_topics``get_persona_keywords``on_persona_changed`
- `services-scheduler`: 自动调度器(`_scheduler_loop``start_scheduler``stop_scheduler``get_scheduler_status`
- `services-queue`: 内容排期队列(`generate_to_queue``queue_*` 系列函数、`_queue_publish_callback`
- `services-autostart`: 开机自启管理(`enable_autostart``disable_autostart``toggle_autostart` 等)
- `ui-tabs-split`: 将其余 Gradio Tab热点、互动、我的主页、自动运营、队列、数据分析、设置提取为 `ui/tab_*.py`
### Modified Capabilities
(无需求层面变更,仅为实现重构)
## Impact
- **主要受影响文件**`main.py`(从 4359 行缩减至 ~300 行入口层)
- **新增目录**`services/`10 个模块)、`ui/`8 个 Tab 模块,`tab_create.py` 已存在)
- **依赖关系**`services/` 模块之间通过函数参数传递依赖,避免循环导入;`main.py` 统一导入并组装
- **无 API 变更**所有函数签名保持不变Gradio 回调绑定不受影响
- **运行时影响**:零,重构不改变业务逻辑