diff --git a/app/api/routers/channels.py b/app/api/routers/channels.py index f047500a..4583fffd 100644 --- a/app/api/routers/channels.py +++ b/app/api/routers/channels.py @@ -32,6 +32,22 @@ async def preset(): return STEP_PRESET +@router.post("/preset/apply") +async def apply_preset(user: User = Depends(current_user), db=Depends(get_db)): + """一键把 Step Plan 预设落成渠道(已有同名渠道则跳过)。""" + from ...core.config import settings + rows = await repos.ChannelRepo.visible_to(db, user) + for r in rows: + if r.name == STEP_PRESET["name"]: + return {"applied": False, "reason": "已存在同名渠道"} + c = Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"], + api_key_enc=settings.step_api_key or "", models=STEP_PRESET["models"], + owner_id=user.id if not user.is_admin else None, visibility="admin") + db.add(c) + await db.commit() + return {"applied": True, "channel": _view(c)} + + @router.get("") async def list_channels(user: User = Depends(current_user), db=Depends(get_db)): rows = await repos.ChannelRepo.visible_to(db, user) diff --git a/app/core/config.py b/app/core/config.py index d660b731..c915ac28 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -8,6 +8,8 @@ class Settings(BaseSettings): jwt_expire_minutes: int = 60 * 24 * 7 first_admin_user: str = "admin" first_admin_pass: str = "maopaoya-admin" + # 默认 Step Plan 渠道的 key(为空则种渠道但不填 key,到后台补) + step_api_key: str = "" class Config: env_file = ".env" diff --git a/app/main.py b/app/main.py index 9cf4bd7f..3aa8710f 100644 --- a/app/main.py +++ b/app/main.py @@ -38,6 +38,15 @@ async def startup(): password_hash=security.hash_password(settings.first_admin_pass), kdf_salt=salt.hex(), is_admin=True)) await db.commit() + # 默认渠道:空库时种 Step Plan 预设(key 走 OMO_STEP_API_KEY) + from .models.entities import Channel + from .core.config import STEP_PRESET + chans = (await db.execute(__import__('sqlalchemy').select(Channel))).scalars().all() + if not chans: + db.add(Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"], + api_key_enc=settings.step_api_key, models=STEP_PRESET["models"], + owner_id=None, visibility="admin")) + await db.commit() @app.exception_handler(Exception)