api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. from __future__ import annotations
  2. import os
  3. from fastapi import FastAPI, HTTPException, Query
  4. from fastapi.exceptions import RequestValidationError
  5. from fastapi.encoders import jsonable_encoder
  6. from fastapi.middleware.cors import CORSMiddleware
  7. from fastapi.responses import JSONResponse
  8. from content_agent.dashboard_service import DashboardService
  9. from content_agent.errors import ErrorCode, error_response, sanitize_error_detail
  10. from content_agent.run_service import RunService
  11. from content_agent.schemas import (
  12. ConfigFileResponse,
  13. ContentItemsResponse,
  14. DashboardResponse,
  15. JsonFileResponse,
  16. PlatformCatalogResponse,
  17. PlatformDescriptor,
  18. QueryListResponse,
  19. RecordsResponse,
  20. RunListResponse,
  21. RunStartRequest,
  22. RunStartResponse,
  23. RunSummaryResponse,
  24. RuntimeFileResponse,
  25. RuntimeFilesResponse,
  26. TimelineResponse,
  27. ValidationResponse,
  28. )
  29. app = FastAPI(title="Content Agent V1")
  30. service = RunService.from_env()
  31. _cors_origins = [
  32. origin.strip()
  33. for origin in os.environ.get(
  34. "CONTENT_AGENT_WEB_CORS_ORIGINS",
  35. "http://localhost:3000,http://127.0.0.1:3000,http://localhost:3010,http://127.0.0.1:3010",
  36. ).split(",")
  37. if origin.strip()
  38. ]
  39. if _cors_origins:
  40. app.add_middleware(
  41. CORSMiddleware,
  42. allow_origins=_cors_origins,
  43. allow_credentials=False,
  44. allow_methods=["GET", "POST", "OPTIONS"],
  45. allow_headers=["*"],
  46. )
  47. @app.exception_handler(RequestValidationError)
  48. async def validation_exception_handler(request, exc: RequestValidationError):
  49. return JSONResponse(
  50. status_code=422,
  51. content={
  52. "detail": error_response(
  53. ErrorCode.INVALID_REQUEST,
  54. "invalid request",
  55. {"errors": jsonable_encoder(sanitize_error_detail(exc.errors()))},
  56. )
  57. },
  58. )
  59. @app.post("/runs", response_model=RunStartResponse)
  60. def start_run(request: RunStartRequest) -> RunStartResponse:
  61. state = service.start_run(request)
  62. if state["status"] not in {"success", "partial_success"}:
  63. detail = error_response(
  64. state.get("error_code", ErrorCode.RUN_START_FAILED),
  65. state.get("error_message", "run failed"),
  66. state.get("error_detail", {"errors": state.get("errors", [])}),
  67. )
  68. raise HTTPException(status_code=state.get("http_status_code", 500), detail=detail)
  69. run_id = state["run_id"]
  70. return RunStartResponse(
  71. run_id=run_id,
  72. policy_run_id=state["policy_run_id"],
  73. status=state["status"],
  74. policy_bundle_id=state["policy_bundle_id"],
  75. strategy_version=state["strategy_version"],
  76. platform=state["platform"],
  77. platform_mode=state["platform_mode"],
  78. output_dir=str(service.runtime.run_dir(run_id)),
  79. )
  80. @app.get("/runs", response_model=RunListResponse)
  81. def list_runs(
  82. status: str | None = None,
  83. platform: str | None = None,
  84. platform_mode: str | None = None,
  85. strategy_version: str | None = None,
  86. validation_status: str | None = None,
  87. error_code: str | None = None,
  88. page: int = Query(default=1, ge=1),
  89. page_size: int = Query(default=20, ge=1, le=100),
  90. ) -> RunListResponse:
  91. return RunListResponse(
  92. **_dashboard_service().list_runs(
  93. status=status,
  94. platform=platform,
  95. platform_mode=platform_mode,
  96. strategy_version=strategy_version,
  97. validation_status=validation_status,
  98. error_code=error_code,
  99. page=page,
  100. page_size=page_size,
  101. )
  102. )
  103. @app.get("/runs/{run_id}", response_model=RunSummaryResponse)
  104. def get_run(run_id: str) -> RunSummaryResponse:
  105. _ensure_run_exists(run_id)
  106. return RunSummaryResponse(**service.get_summary(run_id))
  107. _CONFIG_FILES = {
  108. "rule-packs": "product_documents/规则包/douyin_rule_packs.v1.json",
  109. "walk-strategy": "product_documents/抖音游走策略/douyin_walk_strategy.v1.json",
  110. "query-prompts": "product_documents/配置/query_prompts.v1.json",
  111. "walk-policy": "tech_documents/数据接口与来源/walk_policy.json",
  112. }
  113. def _config_file_response(key: str) -> ConfigFileResponse:
  114. from pathlib import Path
  115. from content_agent.integrations import config_store
  116. path = Path(_CONFIG_FILES[key])
  117. if not path.exists():
  118. raise HTTPException(status_code=404, detail=error_response(
  119. ErrorCode.RUN_NOT_FOUND, f"config file missing: {key}", {"source_file": str(path)}
  120. ))
  121. data, _ = config_store.load_json(path)
  122. return ConfigFileResponse(source_file=str(path), data=data)
  123. @app.get("/config/rule-packs", response_model=ConfigFileResponse)
  124. def get_config_rule_packs() -> ConfigFileResponse:
  125. return _config_file_response("rule-packs")
  126. @app.get("/config/walk-strategy", response_model=ConfigFileResponse)
  127. def get_config_walk_strategy() -> ConfigFileResponse:
  128. return _config_file_response("walk-strategy")
  129. @app.get("/config/query-prompts", response_model=ConfigFileResponse)
  130. def get_config_query_prompts() -> ConfigFileResponse:
  131. return _config_file_response("query-prompts")
  132. @app.get("/config/walk-policy", response_model=ConfigFileResponse)
  133. def get_config_walk_policy() -> ConfigFileResponse:
  134. # 返回原始 JSON 不解包 {value,provenance,tbd}:展示层保留拍板留痕,解包由前端做。
  135. return _config_file_response("walk-policy")
  136. _PLATFORM_PROFILE_DIR = "tech_documents/数据接口与来源/platform_profiles"
  137. @app.get("/config/platforms", response_model=PlatformCatalogResponse)
  138. def get_config_platforms() -> PlatformCatalogResponse:
  139. # 平台展示目录:从 platform_profiles/*.json 派生每平台的 label + 真实可得互动指标(heat 字段)。
  140. # 前端按此渲染平台名与互动数据——加平台只改 profile JSON,前端零改动。
  141. from pathlib import Path
  142. from content_agent.integrations import config_store
  143. catalog: dict[str, PlatformDescriptor] = {}
  144. profile_dir = Path(_PLATFORM_PROFILE_DIR)
  145. for path in sorted(profile_dir.glob("*.json")):
  146. try:
  147. data, _ = config_store.load_json(path)
  148. except (FileNotFoundError, OSError, ValueError):
  149. continue
  150. platform = str(data.get("platform") or path.stem)
  151. heat_fields = [
  152. str(sig["field"])
  153. for sig in ((data.get("heat") or {}).get("signals") or [])
  154. if isinstance(sig, dict) and sig.get("field")
  155. ]
  156. catalog[platform] = PlatformDescriptor(
  157. platform=platform,
  158. label=str(data.get("platform_label") or platform),
  159. status=data.get("status"),
  160. heat_fields=heat_fields,
  161. )
  162. return PlatformCatalogResponse(platforms=catalog)
  163. @app.get("/runs/{run_id}/dashboard", response_model=DashboardResponse)
  164. def get_run_dashboard(run_id: str) -> DashboardResponse:
  165. _ensure_web_run_exists(run_id)
  166. return DashboardResponse(**_dashboard_service().dashboard(run_id))
  167. @app.get("/runs/{run_id}/queries", response_model=QueryListResponse)
  168. def get_run_queries(run_id: str) -> QueryListResponse:
  169. _ensure_web_run_exists(run_id)
  170. return QueryListResponse(**_dashboard_service().queries(run_id))
  171. @app.get("/runs/{run_id}/timeline", response_model=TimelineResponse)
  172. def get_run_timeline(run_id: str) -> TimelineResponse:
  173. _ensure_web_run_exists(run_id)
  174. return TimelineResponse(**_dashboard_service().timeline(run_id))
  175. @app.get("/runs/{run_id}/content-items", response_model=ContentItemsResponse)
  176. def get_run_content_items(run_id: str) -> ContentItemsResponse:
  177. _ensure_web_run_exists(run_id)
  178. return ContentItemsResponse(**_dashboard_service().content_items(run_id))
  179. @app.get("/runs/{run_id}/runtime-files", response_model=RuntimeFilesResponse)
  180. def get_run_runtime_files(run_id: str) -> RuntimeFilesResponse:
  181. _ensure_web_run_exists(run_id)
  182. return RuntimeFilesResponse(**_dashboard_service().runtime_files(run_id))
  183. @app.get("/runs/{run_id}/runtime-files/{filename}", response_model=RuntimeFileResponse)
  184. def get_run_runtime_file(
  185. run_id: str,
  186. filename: str,
  187. limit: int = Query(default=100, ge=1, le=500),
  188. offset: int = Query(default=0, ge=0),
  189. ) -> RuntimeFileResponse:
  190. _ensure_web_run_exists(run_id)
  191. try:
  192. return RuntimeFileResponse(
  193. **_dashboard_service().runtime_file(
  194. run_id,
  195. filename,
  196. limit=limit,
  197. offset=offset,
  198. )
  199. )
  200. except ValueError:
  201. raise HTTPException(
  202. status_code=400,
  203. detail=error_response(
  204. ErrorCode.INVALID_REQUEST,
  205. "runtime filename is not allowed",
  206. {"filename": filename},
  207. ),
  208. )
  209. @app.get("/runs/{run_id}/discovered-content-items", response_model=RecordsResponse)
  210. def get_discovered_content_items(run_id: str) -> RecordsResponse:
  211. return _jsonl_response(run_id, "discovered_content_items.jsonl")
  212. @app.get("/runs/{run_id}/rule-decisions", response_model=RecordsResponse)
  213. def get_rule_decisions(run_id: str) -> RecordsResponse:
  214. return _jsonl_response(run_id, "rule_decisions.jsonl")
  215. @app.get("/runs/{run_id}/source-path-records", response_model=RecordsResponse)
  216. def get_source_path_records(run_id: str) -> RecordsResponse:
  217. return _jsonl_response(run_id, "source_path_records.jsonl")
  218. @app.get("/runs/{run_id}/final-output", response_model=JsonFileResponse)
  219. def get_final_output(run_id: str) -> JsonFileResponse:
  220. return _json_response(run_id, "final_output.json")
  221. @app.get("/runs/{run_id}/strategy-review", response_model=JsonFileResponse)
  222. def get_strategy_review(run_id: str) -> JsonFileResponse:
  223. _ensure_run_exists(run_id)
  224. return JsonFileResponse(run_id=run_id, data=service.strategy_review(run_id))
  225. @app.post("/runs/{run_id}/strategy-review/regenerate", response_model=JsonFileResponse)
  226. def regenerate_strategy_review(run_id: str) -> JsonFileResponse:
  227. _ensure_run_exists(run_id)
  228. return JsonFileResponse(run_id=run_id, data=service.regenerate_strategy_review(run_id))
  229. @app.get("/runs/{run_id}/validation", response_model=ValidationResponse)
  230. def get_validation(run_id: str) -> ValidationResponse:
  231. _ensure_run_exists(run_id)
  232. return ValidationResponse(**service.validate_run(run_id))
  233. def _jsonl_response(run_id: str, filename: str) -> RecordsResponse:
  234. _ensure_run_exists(run_id)
  235. return RecordsResponse(run_id=run_id, records=service.read_jsonl(run_id, filename))
  236. def _json_response(run_id: str, filename: str) -> JsonFileResponse:
  237. _ensure_run_exists(run_id)
  238. return JsonFileResponse(run_id=run_id, data=service.read_json(run_id, filename))
  239. def _ensure_run_exists(run_id: str) -> None:
  240. if not service.runtime.run_dir(run_id).exists():
  241. raise HTTPException(
  242. status_code=404,
  243. detail=error_response(
  244. ErrorCode.RUN_NOT_FOUND,
  245. "run not found",
  246. {"run_id": run_id},
  247. ),
  248. )
  249. def _ensure_web_run_exists(run_id: str) -> None:
  250. if not _dashboard_service().run_exists(run_id):
  251. raise HTTPException(
  252. status_code=404,
  253. detail=error_response(
  254. ErrorCode.RUN_NOT_FOUND,
  255. "run not found",
  256. {"run_id": run_id},
  257. ),
  258. )
  259. def _dashboard_service() -> DashboardService:
  260. return DashboardService.from_runtime(service.runtime)