815cbf9d8c
- 更新 .gitignore:全面覆盖环境变量、数据库、日志、缓存、上传文件 - 移除误跟踪的 server/venv/、crm_data.db、.env 文件 - 新增 server/.env.example 模板 - 新增合同管理、利润核算、AI教练等功能模块 - 新增 Playwright e2e 测试套件 - 前后端多项功能升级和 bug 修复
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""
|
|
AI 教练引擎路由 —— /api/ai-coaching
|
|
Dify 回调 + SSE 通知流
|
|
"""
|
|
from __future__ import annotations
|
|
import uuid
|
|
from fastapi import APIRouter, Depends, Request
|
|
from fastapi.responses import StreamingResponse
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.api.deps import get_current_user
|
|
from app.db.database import get_db
|
|
from app.schemas.auth import CurrentUserPayload
|
|
from app.schemas.response import ok
|
|
from app.services import ai_coaching_service as svc
|
|
|
|
router = APIRouter(prefix="/ai-coaching", tags=["AI教练引擎"])
|
|
|
|
|
|
@router.post("/dify-callback/{sales_log_id}", summary="Dify Workflow 回调端点")
|
|
async def dify_coaching_callback(
|
|
sales_log_id: uuid.UUID,
|
|
request: Request,
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> dict:
|
|
"""接收 Dify Workflow 的异步回调,写回教练反馈"""
|
|
import json
|
|
body = await request.json()
|
|
await svc.handle_dify_coaching_callback(db, sales_log_id, body)
|
|
return ok(message="教练反馈已回写")
|
|
|
|
|
|
@router.get("/notifications/stream", summary="SSE 通知流")
|
|
async def sse_notifications(
|
|
current_user: CurrentUserPayload = Depends(get_current_user),
|
|
):
|
|
"""Server-Sent Events 推送通知(AI 教练反馈、系统通知等)"""
|
|
return StreamingResponse(
|
|
svc.sse_notification_generator(current_user.user_id),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"Connection": "keep-alive",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
)
|