refactor: 精简环境配置,移除废弃配置项

.env:
- 移除 HOST/PORT/DEBUG(代码未读取,由 uvicorn 命令行控制)
- 移除 WEWORK_BOT_SEARCH_LIMIT(已被 SEARCH_LIMIT 取代)
- 补充硅基流动配置注释

.env.example:
- 同步移除 HOST/PORT/DEBUG/WEWORK_BOT_SEARCH_LIMIT
- 补充 OpenAI 兼容硅基流动的说明
- 更新机器人注释(集成模式自动启动)

config.py:
- 移除 HOST/PORT/DEBUG 字段定义
- 移除 WEWORK_BOT_SEARCH_LIMIT 字段定义

wework_bot.py:
- 改为从 settings 统一读取配置(不再 os.getenv)
- _get_search_limit fallback 改为 settings.SEARCH_LIMIT
- 更新模块文档注释
This commit is contained in:
EduBrain Dev
2026-04-14 13:54:16 +08:00
parent eb18b5cffc
commit 36a6debcb3
3 changed files with 17 additions and 30 deletions

View File

@@ -4,7 +4,9 @@
使用官方 SDK (wecom-aibot-python-sdk) 的 WSClient 类,
封装了连接管理、心跳、断线重连等能力。
作为独立进程运行python -m app.wework_bot
支持两种运行方式:
1. 集成模式后端启动时自动拉起BotManager
2. 独立进程python -m app.wework_bot
"""
from __future__ import annotations
@@ -28,6 +30,8 @@ from dotenv import load_dotenv
# 加载 .env 文件(独立运行时需要)
load_dotenv()
from app.config import settings
logger = logging.getLogger("wework_bot")
# 分片上传参数(参考官方文档)
@@ -38,13 +42,10 @@ class WeComBotService:
"""企业微信智能机器人服务WebSocket 长连接模式)"""
def __init__(self):
self.bot_id = os.getenv("WEWORK_BOT_ID", "")
self.bot_secret = os.getenv("WEWORK_BOT_SECRET", "")
self.edubrain_base_url = os.getenv(
"EDUBRAIN_BASE_URL", "http://localhost:8765"
)
self.search_limit = int(os.getenv("WEWORK_BOT_SEARCH_LIMIT", "3"))
self.enabled = os.getenv("WEWORK_BOT_ENABLED", "false").lower() == "true"
self.bot_id = settings.WEWORK_BOT_ID or ""
self.bot_secret = settings.WEWORK_BOT_SECRET or ""
self.edubrain_base_url = settings.EDUBRAIN_BASE_URL
self.enabled = settings.WEWORK_BOT_ENABLED
self.ws_client: WSClient | None = None
def start(self):
@@ -430,10 +431,12 @@ class WeComBotService:
resp = await client.get(f"{self.edubrain_base_url}/api/v1/settings")
if resp.status_code == 200:
data = resp.json()
return data.get("search_limit", self.search_limit)
limit = data.get("search_limit")
if limit:
return limit
except Exception as e:
logger.warning("获取 search_limit 失败,使用默认值: %s", e)
return self.search_limit
return settings.SEARCH_LIMIT
async def _search_images(self, keyword: str) -> list:
"""