- 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"
2.7 KiB
2.7 KiB
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 回调绑定不受影响
- 运行时影响:零,重构不改变业务逻辑