60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
小程序 AI 缓存查询路由 —— 查询各 AI 应用的最新缓存结果。
|
||
|
||
端点清单:
|
||
- GET /api/ai/cache/{cache_type}?target_id=xxx — 查询最新缓存
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||
|
||
from app.ai.cache_service import AICacheService
|
||
from app.ai.schemas import CacheTypeEnum
|
||
from app.auth.dependencies import CurrentUser, get_current_user
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
router = APIRouter(prefix="/api/ai", tags=["小程序 AI 缓存"])
|
||
|
||
|
||
@router.get("/cache/{cache_type}")
|
||
async def get_ai_cache(
|
||
cache_type: str,
|
||
target_id: str = Query(..., description="目标 ID(member_id / assistant_id_member_id / 时间维度编码)"),
|
||
user: CurrentUser = Depends(get_current_user),
|
||
):
|
||
"""查询指定类型的最新 AI 缓存结果。
|
||
|
||
site_id 从 JWT 提取,强制过滤,确保门店隔离。
|
||
"""
|
||
# 校验 cache_type 合法性
|
||
valid_types = {e.value for e in CacheTypeEnum}
|
||
if cache_type not in valid_types:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||
detail=f"无效的 cache_type: {cache_type},合法值: {sorted(valid_types)}",
|
||
)
|
||
|
||
cache_svc = AICacheService()
|
||
result = cache_svc.get_latest(
|
||
cache_type=cache_type,
|
||
site_id=user.site_id,
|
||
target_id=target_id,
|
||
)
|
||
|
||
if result is None:
|
||
return None
|
||
|
||
return {
|
||
"id": result.get("id"),
|
||
"cache_type": result.get("cache_type"),
|
||
"target_id": result.get("target_id"),
|
||
"result_json": result.get("result_json"),
|
||
"score": result.get("score"),
|
||
"created_at": result.get("created_at"),
|
||
}
|