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

7.1 KiB
Raw Blame History

1. 基础结构搭建

  • 1.1 创建 services/ 目录及 services/__init__.py 空文件
  • 1.2 确认 ui/__init__.py 已存在(上一轮已创建)

2. 迁移 services/rate_limiter.py

  • 2.1 创建 services/rate_limiter.py,迁移 _auto_running_op_history_daily_statsDAILY_LIMITS_consecutive_errors_error_cooldown_until 等模块级变量
  • 2.2 迁移函数:_reset_daily_stats_if_needed_check_daily_limit_increment_stat_record_error_clear_error_streak_is_in_cooldown_is_in_operating_hours_get_stats_summary
  • 2.3 在 main.py 中删除对应变量和函数,添加 from services.rate_limiter import ...
  • 2.4 运行 ast.parse() 验证 main.pyservices/rate_limiter.py 语法正确

3. 迁移 services/autostart.py

  • 3.1 创建 services/autostart.py,迁移 _APP_NAME_STARTUP_REG_KEY 及所有 autostart 函数(_get_startup_script_path_get_startup_bat_path_create_startup_scriptsis_autostart_enabledenable_autostartdisable_autostarttoggle_autostart
  • 3.2 在 main.py 中删除对应代码,添加 from services.autostart import ...
  • 3.3 运行 ast.parse() 验证语法正确

4. 迁移 services/persona.py

  • 4.1 创建 services/persona.py,迁移常量:DEFAULT_PERSONASRANDOM_PERSONA_LABELPERSONA_POOL_MAPDEFAULT_TOPICSDEFAULT_STYLESDEFAULT_COMMENT_KEYWORDS
  • 4.2 迁移函数:_match_persona_poolsget_persona_topicsget_persona_keywordson_persona_changed_resolve_persona
  • 4.3 在 main.py 中删除对应代码,添加 from services.persona import ...
  • 4.4 运行 ast.parse() 验证语法正确

5. 迁移 services/connection.py

  • 5.1 创建 services/connection.py,迁移函数:_get_llm_configconnect_llmadd_llm_providerremove_llm_provideron_provider_selected
  • 5.2 迁移 SD 相关函数:connect_sdon_sd_model_change
  • 5.3 迁移 MCP / 登录相关函数:check_mcp_statusget_login_qrcodelogout_xhs_auto_fetch_xsec_tokencheck_loginsave_my_user_idupload_face_imageload_saved_face_image
  • 5.4 确保所有函数通过参数接收 cfgllmsdmcp 等依赖,不在模块顶层初始化单例
  • 5.5 在 main.py 中删除对应函数,添加 from services.connection import ...
  • 5.6 运行 ast.parse() 验证语法正确

6. 迁移 services/profile.py

  • 6.1 创建 services/profile.py,迁移函数:_parse_profile_json_parse_countfetch_my_profile
  • 6.2 在 main.py 中删除对应函数,添加 from services.profile import ...
  • 6.3 运行 ast.parse() 验证语法正确

7. 迁移 services/hotspot.py

  • 7.1 创建 services/hotspot.py,迁移缓存相关:_cache_lock_set_cache_get_cache_fetch_and_cache_pick_from_cache
  • 7.2 迁移热点函数:search_hotspotsanalyze_and_suggestgenerate_from_hotspotfetch_proactive_noteson_proactive_note_selected
  • 7.3 在 main.py 中删除对应代码,添加 from services.hotspot import ...
  • 7.4 运行 ast.parse() 验证语法正确

8. 迁移 services/content.py

  • 8.1 创建 services/content.py,迁移函数:generate_copygenerate_imagesone_click_exportpublish_to_xhs
  • 8.2 确保 publish_to_xhs 的输入验证逻辑和 finally 临时文件清理逻辑完整保留
  • 8.3 在 main.py 中删除对应函数,添加 from services.content import ...
  • 8.4 运行 ast.parse() 验证语法正确

9. 迁移 services/engagement.py

  • 9.1 创建 services/engagement.py,迁移笔记/评论相关函数:load_note_for_commentai_generate_commentsend_commentfetch_my_noteson_my_note_selectedfetch_my_note_commentsai_reply_commentsend_reply
  • 9.2 迁移自动化函数:auto_comment_onceauto_like_onceauto_favorite_onceauto_reply_once 及各 _with_log 包装
  • 9.3 将 _with_log 函数改为接收 log_fn 回调参数,不直接引用外部 _auto_log
  • 9.4 在 main.py 中删除对应函数,添加 from services.engagement import ...
  • 9.5 运行 ast.parse() 验证语法正确

10. 迁移 services/scheduler.py

  • 10.1 创建 services/scheduler.py,迁移状态变量和日志:_auto_log_scheduler_next_times_auto_log_append
  • 10.2 迁移调度器函数:_scheduler_loopstart_schedulerstop_schedulerget_auto_logget_scheduler_status
  • 10.3 迁移学习调度器:_learn_running_learn_scheduler_loopstart_learn_schedulerstop_learn_scheduler
  • 10.4 确保 _scheduler_loop 调用 engagement 函数时传入 log_fn=_auto_log_append
  • 10.5 在 main.py 中删除对应代码,添加 from services.scheduler import ...
  • 10.6 运行 ast.parse() 验证语法正确

11. 迁移 services/queue_ops.py

  • 11.1 创建 services/queue_ops.py,迁移所有 queue 操作函数:generate_to_queue_queue_publish_callbackqueue_refresh_tablequeue_refresh_calendarqueue_preview_itemqueue_approve_itemqueue_reject_itemqueue_delete_itemqueue_retry_itemqueue_publish_nowqueue_start_processorqueue_stop_processorqueue_get_statusqueue_batch_approvequeue_generate_and_refresh
  • 11.2 确保 pub_queuequeue_publisher 通过参数传入各函数,不在模块顶层初始化
  • 11.3 在 main.py 中删除对应函数,添加 from services.queue_ops import ...;保留 pub_queue.set_publish_callback(_queue_publish_callback)main.py 初始化段调用
  • 11.4 运行 ast.parse() 验证语法正确

12. 拆分 UI Tab 模块

  • 12.1 创建 ui/tab_hotspot.py,提取 Tab 2🔥 热点探测)的所有 Gradio 组件和事件绑定,暴露 build_tab(fn_*, ...) 函数
  • 12.2 创建 ui/tab_engage.py,提取 Tab 3💬 互动运营)的所有 Gradio 组件和事件绑定
  • 12.3 创建 ui/tab_profile.py,提取 Tab 4👤 我的主页)的所有 Gradio 组件和事件绑定
  • 12.4 创建 ui/tab_auto.py,提取 Tab 5🤖 自动运营)的所有 Gradio 组件和事件绑定
  • 12.5 创建 ui/tab_queue.py,提取 Tab 6📅 内容排期)的所有 Gradio 组件和事件绑定
  • 12.6 创建 ui/tab_analytics.py,提取 Tab 7📊 数据分析)的所有 Gradio 组件和事件绑定
  • 12.7 创建 ui/tab_settings.py,提取 Tab 8⚙️ 系统设置)的所有 Gradio 组件和事件绑定
  • 12.8 在 main.py 中用相应的 build_tab(...) 调用替换各 Tab 代码块,完成后删除空白 Tab 块
  • 12.9 运行 ast.parse() 验证所有新建 UI 模块语法正确

13. 入口层清理与验证

  • 13.1 验证 main.py 行数不超过 400 行
  • 13.2 检查 main.py 不包含任何业务逻辑函数定义(除 lambda 内联外)
  • 13.3 运行应用 python main.py,确认启动无报错
  • 13.4 在浏览器中切换所有 Tab确认 UI 正常渲染、事件响应正常