app.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. """FastAPI application — category tree API on port 8080."""
  2. from __future__ import annotations
  3. from contextlib import asynccontextmanager
  4. from pathlib import Path
  5. from typing import Literal
  6. from fastapi import FastAPI, HTTPException, Query, Request, status
  7. from fastapi.middleware.cors import CORSMiddleware
  8. from fastapi.staticfiles import StaticFiles
  9. from pydantic import BaseModel, Field
  10. from sqlalchemy import text
  11. from starlette.exceptions import HTTPException as StarletteHTTPException
  12. from api.auth_middleware import AuthenticationMiddleware
  13. from api.routers.auth import router as auth_router
  14. from api.routers.pipeline import router as pipeline_router
  15. from api.schemas.demand_feedback import CreateDemandFeedbackBody
  16. from api.services.agent_catalog import (
  17. get_agent_detail,
  18. update_agent_document_injection,
  19. )
  20. from api.services.auth import ensure_bootstrap_admin
  21. from api.services.category_tree import build_category_tree
  22. from api.services.demand_belong_category import list_demand_belong_categories
  23. from api.services.demand_grade import list_demand_grades
  24. from api.services.demand_grade_videos import list_videos_for_demand_grade
  25. from api.services.demand_feedback import (
  26. FeedbackRequestConflictError,
  27. FeedbackTargetConflictError,
  28. FeedbackTargetNotFoundError,
  29. create_demand_feedback,
  30. list_demand_feedback,
  31. )
  32. from api.services.demand_videos import list_videos_for_demand_belong
  33. from api.services.oss_logs import list_agent_oss_logs, list_demand_belong_oss_logs
  34. from api.services.scheduler import (
  35. get_scheduler_job_run,
  36. list_triggerable_jobs,
  37. run_scheduler_job,
  38. run_supply_pipeline,
  39. scheduler_status as get_persistent_scheduler_status,
  40. )
  41. from api.services.video_discovery import (
  42. get_video_discovery_demand,
  43. list_video_discovery_demands,
  44. )
  45. from supply_infra.config import get_infra_settings
  46. from supply_infra.db import dispose_engine, get_session, init_db
  47. class SPAStaticFiles(StaticFiles):
  48. """Serve index.html for browser routes handled by Vue Router."""
  49. async def get_response(self, path: str, scope):
  50. try:
  51. return await super().get_response(path, scope)
  52. except StarletteHTTPException as exc:
  53. is_backend_path = (
  54. path == "api"
  55. or path.startswith("api/")
  56. or path == "health"
  57. or path.startswith("health/")
  58. )
  59. if exc.status_code != 404 or is_backend_path or Path(path).suffix:
  60. raise
  61. return await super().get_response("index.html", scope)
  62. @asynccontextmanager
  63. async def lifespan(_app: FastAPI):
  64. if get_infra_settings().database_auto_create:
  65. init_db()
  66. ensure_bootstrap_admin()
  67. yield
  68. dispose_engine()
  69. app = FastAPI(title="SupplyAgent API", version="0.1.0", lifespan=lifespan)
  70. app.include_router(auth_router)
  71. app.include_router(pipeline_router)
  72. app.add_middleware(
  73. CORSMiddleware,
  74. allow_origins=[
  75. "http://localhost:5173",
  76. "http://127.0.0.1:5173",
  77. "http://localhost:4173",
  78. "http://127.0.0.1:4173",
  79. ],
  80. allow_credentials=True,
  81. allow_methods=["*"],
  82. allow_headers=["*"],
  83. )
  84. app.add_middleware(AuthenticationMiddleware)
  85. @app.get("/health")
  86. def health() -> dict[str, str]:
  87. return {"status": "ok"}
  88. @app.get("/health/live")
  89. def health_live() -> dict[str, str]:
  90. return {"status": "ok"}
  91. @app.get("/health/ready")
  92. def health_ready() -> dict[str, str]:
  93. try:
  94. with get_session() as session:
  95. session.execute(text("SELECT 1"))
  96. except Exception as exc:
  97. raise HTTPException(status_code=503, detail="database unavailable") from exc
  98. return {"status": "ready"}
  99. @app.get("/api/scheduler/status")
  100. def scheduler_status() -> dict:
  101. """Deprecated compatibility view backed by MySQL pipeline state."""
  102. return get_persistent_scheduler_status()
  103. class RunPipelineBody(BaseModel):
  104. biz_dt: str | None = Field(
  105. default=None,
  106. pattern=r"^\d{8}$",
  107. description="业务日 YYYYMMDD;省略则取当天",
  108. )
  109. class TriggerSchedulerJobBody(BaseModel):
  110. biz_dt: str | None = Field(
  111. default=None,
  112. pattern=r"^\d{8}$",
  113. description="业务日 YYYYMMDD;省略则取当天",
  114. )
  115. class AgentDocumentInjectionBody(BaseModel):
  116. content: str = Field(
  117. default="",
  118. max_length=100_000,
  119. description="追加到 Agent System Prompt 后的业务文档",
  120. )
  121. enabled: bool = Field(
  122. default=True,
  123. description="是否在新建 Agent 时启用该文档注入",
  124. )
  125. @app.post("/api/pipeline/run", status_code=202)
  126. def run_pipeline(
  127. body: RunPipelineBody | None = None,
  128. biz_dt: str | None = Query(
  129. default=None,
  130. pattern=r"^\d{8}$",
  131. description="业务日 YYYYMMDD(与 body 二选一,body 优先)",
  132. ),
  133. ) -> dict:
  134. """
  135. 异步一键执行供给数据全流程,立即返回 run_id,后台串行执行:
  136. 全局树同步 → 需求池同步 → 需求分级 → 视频点位拓展 → find_agent 找片 → AIGC 发布。
  137. """
  138. resolved_biz_dt = body.biz_dt if body and body.biz_dt is not None else biz_dt
  139. return run_supply_pipeline(biz_dt=resolved_biz_dt)
  140. @app.get("/api/scheduler/jobs")
  141. def scheduler_jobs() -> dict:
  142. """Return jobs that can be triggered manually."""
  143. return {"items": list_triggerable_jobs()}
  144. @app.post("/api/scheduler/jobs/{job_id}/run")
  145. def trigger_scheduler_job(
  146. job_id: str,
  147. body: TriggerSchedulerJobBody | None = None,
  148. biz_dt: str | None = Query(
  149. default=None,
  150. pattern=r"^\d{8}$",
  151. description="业务日 YYYYMMDD(与 body 二选一,body 优先)",
  152. ),
  153. ) -> dict:
  154. """Manually trigger a scheduler job."""
  155. resolved_biz_dt = body.biz_dt if body and body.biz_dt is not None else biz_dt
  156. try:
  157. return run_scheduler_job(job_id, biz_dt=resolved_biz_dt)
  158. except KeyError:
  159. raise HTTPException(status_code=404, detail=f"scheduler job not found: {job_id}") from None
  160. @app.get("/api/scheduler/runs/{run_id}")
  161. def scheduler_job_run(run_id: str) -> dict:
  162. """Return durable pipeline run status."""
  163. result = get_scheduler_job_run(run_id)
  164. if result is None:
  165. raise HTTPException(status_code=404, detail="scheduler run not found")
  166. return result
  167. @app.get("/api/category-tree")
  168. def category_tree(
  169. biz_dt: str | None = Query(
  170. default=None,
  171. description="业务日 YYYYMMDD;省略则取 category_tree_weight 最新一日",
  172. ),
  173. ) -> dict:
  174. """Return nested global_tree_category with per-dim avg score."""
  175. return build_category_tree(biz_dt=biz_dt)
  176. @app.get("/api/demand-belong-category")
  177. def demand_belong_category() -> dict:
  178. """Return all active demand_belong_category rows in one response."""
  179. items = list_demand_belong_categories()
  180. return {"items": items}
  181. @app.get("/api/demand-belong-category/{belong_id}/videos")
  182. def demand_belong_videos(belong_id: int) -> dict:
  183. """Return videos linked to a demand_belong_category row (vid + title + points JSON)."""
  184. result = list_videos_for_demand_belong(belong_id)
  185. if result is None:
  186. raise HTTPException(status_code=404, detail="demand_belong_category not found")
  187. return result
  188. @app.get("/api/demand-grade")
  189. def demand_grade(
  190. biz_dt: str | None = Query(
  191. default=None,
  192. description="业务日 YYYYMMDD;省略则取 demand_grade 最新一日",
  193. ),
  194. ) -> dict:
  195. """Return demand_grade rows (one per category_id) for the given/latest biz_dt."""
  196. items = list_demand_grades(biz_dt=biz_dt)
  197. return {"items": items}
  198. @app.get("/api/demand-grade/{demand_grade_id}/videos")
  199. def demand_grade_videos(demand_grade_id: int) -> dict:
  200. """Return expansion videos/points for a demand_grade row (by its biz_dt)."""
  201. result = list_videos_for_demand_grade(demand_grade_id)
  202. if result is None:
  203. raise HTTPException(status_code=404, detail="demand_grade not found")
  204. return result
  205. @app.get("/api/video-discovery/demands")
  206. def video_discovery_demands(
  207. biz_dt: str | None = Query(
  208. default=None,
  209. description="业务日 YYYYMMDD;省略则取 demand_grade 最新一日",
  210. ),
  211. ) -> dict:
  212. """Return demand-first video discovery cards for the selected/latest day."""
  213. return list_video_discovery_demands(biz_dt=biz_dt)
  214. @app.get("/api/video-discovery/demands/{demand_grade_id}")
  215. def video_discovery_demand(demand_grade_id: int) -> dict:
  216. """Return one demand and its videos/points resolved from the source tables."""
  217. result = get_video_discovery_demand(demand_grade_id)
  218. if result is None:
  219. raise HTTPException(status_code=404, detail="demand_grade not found")
  220. return result
  221. @app.post(
  222. "/api/video-discovery/feedback",
  223. status_code=status.HTTP_201_CREATED,
  224. )
  225. def submit_video_discovery_feedback(
  226. body: CreateDemandFeedbackBody,
  227. request: Request,
  228. ) -> dict:
  229. """Append feedback for one demand, video or hit-content record."""
  230. try:
  231. return create_demand_feedback(body, request.state.current_user)
  232. except FeedbackTargetNotFoundError as exc:
  233. raise HTTPException(status_code=404, detail=str(exc)) from exc
  234. except (FeedbackTargetConflictError, FeedbackRequestConflictError) as exc:
  235. raise HTTPException(status_code=409, detail=str(exc)) from exc
  236. @app.get("/api/video-discovery/feedback")
  237. def video_discovery_feedback_history(
  238. target_type: Literal["demand", "video", "hit_content"],
  239. demand_grade_id: int = Query(gt=0),
  240. video_id: str | None = Query(default=None, max_length=64),
  241. demand_video_expansion_id: int | None = Query(default=None, gt=0),
  242. limit: int = Query(default=50, ge=1, le=100),
  243. offset: int = Query(default=0, ge=0),
  244. ) -> dict:
  245. """Return feedback records and feedback people for one target."""
  246. try:
  247. return list_demand_feedback(
  248. target_type=target_type,
  249. demand_grade_id=demand_grade_id,
  250. video_id=video_id,
  251. demand_video_expansion_id=demand_video_expansion_id,
  252. limit=limit,
  253. offset=offset,
  254. )
  255. except FeedbackTargetNotFoundError as exc:
  256. raise HTTPException(status_code=404, detail=str(exc)) from exc
  257. except FeedbackTargetConflictError as exc:
  258. raise HTTPException(status_code=409, detail=str(exc)) from exc
  259. @app.get("/api/demand-belong-oss-logs")
  260. def demand_belong_oss_logs() -> dict:
  261. """Return demand_belong_category_agent oss_logs ordered by create_time desc."""
  262. items = list_demand_belong_oss_logs()
  263. return {"items": items}
  264. @app.get("/api/agent-oss-logs")
  265. def agent_oss_logs(
  266. agent_name: str | None = Query(
  267. default=None,
  268. description="Agent 名称;省略则返回全部 Agent 日志",
  269. ),
  270. ) -> dict:
  271. """Return Agent oss_logs and the available Agent names."""
  272. return list_agent_oss_logs(agent_name=agent_name)
  273. @app.get("/api/agents/{agent_name}")
  274. def agent_detail(agent_name: str) -> dict:
  275. """Return one business Agent's current system prompt and tools."""
  276. result = get_agent_detail(agent_name)
  277. if result is None:
  278. raise HTTPException(status_code=404, detail=f"agent not found: {agent_name}")
  279. return result
  280. @app.put("/api/agents/{agent_name}/document-injection")
  281. def agent_document_injection(
  282. agent_name: str,
  283. body: AgentDocumentInjectionBody,
  284. ) -> dict:
  285. """Create or update the web-managed document injected into an Agent prompt."""
  286. result = update_agent_document_injection(
  287. agent_name,
  288. content=body.content,
  289. enabled=body.enabled,
  290. )
  291. if result is None:
  292. raise HTTPException(status_code=404, detail=f"agent not found: {agent_name}")
  293. return result
  294. _web_dist = Path(__file__).resolve().parent.parent / "web" / "dist"
  295. if _web_dist.is_dir():
  296. app.mount("/", SPAStaticFiles(directory=_web_dist, html=True), name="web")