sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -12,8 +12,11 @@ use a literal value) and appends to ``url`` as query string.
Reads are open to any authenticated user; writes require admin.
Tags reuse the shared ``/api/tags`` catalog with ``tag_type=light_app``. List
and get responses embed the assigned tags; ``tag_ids`` is an OR filter.
Routes (prefix ``/api/light-apps``):
GET "" list apps (filters: app_type / mount_location / enabled_only / search)
GET "" list apps (filters: app_type / mount_location / enabled_only / search / tag_ids)
POST "" create app (admin)
GET "/{app_id}" read one
PUT "/{app_id}" update one (admin)
@@ -22,6 +25,7 @@ Routes (prefix ``/api/light-apps``):
from __future__ import annotations
import logging
from datetime import datetime
from fastapi import APIRouter, HTTPException, Query, Request
@@ -30,6 +34,8 @@ from pydantic import BaseModel, Field
from app.gateway.deps import get_current_user, get_optional_user_from_request
from deerflow.persistence.light_apps import DuplicateLightAppNameError, LightAppStore
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/light-apps", tags=["light-apps"])
_VALID_TYPES = {"window_open", "iframe"}
@@ -64,6 +70,7 @@ class LightAppResponse(BaseModel):
updated_by: str | None = None
created_at: datetime | str | None = None
updated_at: datetime | str | None = None
tags: list[dict] = Field(default_factory=list, description="Tags assigned to this app (tag_type=light_app)")
class LightAppListResponse(BaseModel):
@@ -122,6 +129,31 @@ def _dump_params(params) -> list[dict]:
return [p.model_dump() if isinstance(p, RouteParam) else dict(p) for p in (params or [])]
async def _attach_tags(request: Request, responses: list[LightAppResponse]) -> list[LightAppResponse]:
"""Bulk-embed ``light_app`` tags. A missing/failing tag store never breaks the list."""
tag_store = getattr(request.app.state, "tag_store", None)
if tag_store is None or not responses:
return responses
try:
assignments = await tag_store.list_assignments("light_app", [r.app_id for r in responses])
except Exception:
logger.debug("light-app tag attach failed", exc_info=True)
assignments = {}
for resp in responses:
resp.tags = assignments.get(resp.app_id, [])
return responses
async def _unassign_tags(request: Request, app_id: str) -> None:
tag_store = getattr(request.app.state, "tag_store", None)
if tag_store is None:
return
try:
await tag_store.unassign_all("light_app", app_id)
except Exception:
logger.debug("light-app tag unassign failed", exc_info=True)
# ── routes ───────────────────────────────────────────────────────────────
@@ -132,13 +164,18 @@ async def list_light_apps(
mount_location: str | None = Query(default=None),
enabled_only: bool = Query(default=False),
search: str | None = Query(default=None),
tag_ids: list[str] | None = Query(default=None, description="Keep apps carrying any of these tags (OR)"),
) -> LightAppListResponse:
store = _get_store(request)
rows = await store.list_apps(app_type=app_type, mount_location=mount_location, enabled_only=enabled_only)
if search:
needle = search.strip().lower()
rows = [r for r in rows if needle in str(r.get("name", "")).lower() or needle in str(r.get("description") or "").lower()]
return LightAppListResponse(apps=[LightAppResponse(**r) for r in rows])
responses = await _attach_tags(request, [LightAppResponse(**r) for r in rows])
if tag_ids:
wanted = set(tag_ids)
responses = [r for r in responses if any(t.get("id") in wanted for t in r.tags)]
return LightAppListResponse(apps=responses)
@router.post("", response_model=LightAppResponse)
@@ -164,7 +201,8 @@ async def create_light_app(request: Request, body: LightAppCreateRequest) -> Lig
)
except DuplicateLightAppNameError:
raise HTTPException(status_code=409, detail="应用名称已存在")
return LightAppResponse(**row)
attached = await _attach_tags(request, [LightAppResponse(**row)])
return attached[0]
@router.get("/{app_id}", response_model=LightAppResponse)
@@ -173,7 +211,8 @@ async def get_light_app(request: Request, app_id: str) -> LightAppResponse:
row = await store.get_app(app_id)
if row is None:
raise HTTPException(status_code=404, detail="应用不存在")
return LightAppResponse(**row)
attached = await _attach_tags(request, [LightAppResponse(**row)])
return attached[0]
@router.put("/{app_id}", response_model=LightAppResponse)
@@ -203,7 +242,8 @@ async def update_light_app(request: Request, app_id: str, body: LightAppUpdateRe
raise HTTPException(status_code=409, detail="应用名称已存在")
if row is None:
raise HTTPException(status_code=404, detail="应用不存在")
return LightAppResponse(**row)
attached = await _attach_tags(request, [LightAppResponse(**row)])
return attached[0]
@router.delete("/{app_id}")
@@ -213,4 +253,5 @@ async def delete_light_app(request: Request, app_id: str) -> dict:
deleted = await store.delete_app(app_id)
if not deleted:
raise HTTPException(status_code=404, detail="应用不存在")
await _unassign_tags(request, app_id)
return {"success": True}