- Added a new visitor chat API endpoint (`/api/chat/visitor`) to allow unauthenticated users to engage in conversations without requiring customer data. - Introduced a visitor context dependency to manage visitor interactions seamlessly. - Enhanced the chat API to support explicit session termination and improved response handling for customer service interactions. - Updated the database configuration to include Redis client support for caching visitor data. - Added a new customer note repository to persist user notes independently of the L1 profile slots. This update significantly improves the customer service experience by enabling visitor interactions and ensuring efficient data handling for both registered and unregistered users.
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
"""游客对话接口:免认证 /api/chat/visitor。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import uuid
|
||
from typing import Annotated
|
||
|
||
from fastapi import APIRouter, Depends, Request
|
||
|
||
from app.gateway.visitor_deps import get_visitor_context
|
||
from app.model.schemas import AuthContext, VisitorChatRequest, VisitorChatResponseData
|
||
from app.repository.audit_repository import AuditRepository
|
||
from app.service.visitor_service import run_visitor_chat
|
||
from app.utils.input_guard import validate_user_message
|
||
from app.utils.response import ok
|
||
|
||
router = APIRouter(prefix="/api", tags=["visitor"])
|
||
|
||
|
||
@router.post("/chat/visitor")
|
||
def visitor_chat(
|
||
body: VisitorChatRequest,
|
||
request: Request,
|
||
ctx: Annotated[AuthContext, Depends(get_visitor_context)],
|
||
):
|
||
"""游客对话:免认证,不查客户数据,不写 agent_session/agent_message。"""
|
||
message = validate_user_message(body.message)
|
||
session_id = body.session_id or str(uuid.uuid4())
|
||
|
||
reply, has_disclaimer, intent, transfer = run_visitor_chat(ctx, message, session_id)
|
||
|
||
# 审计落库(agent_type=platform,ENUM 已含);审计失败不阻断对话主流程
|
||
try:
|
||
AuditRepository().insert(
|
||
trace_id=ctx.trace_id,
|
||
event_type="visitor_chat_completed",
|
||
agent_type="platform",
|
||
actor_id="visitor",
|
||
input_summary={
|
||
"session_id": session_id,
|
||
"intent": intent,
|
||
"transfer": transfer,
|
||
"msg_len": len(message),
|
||
},
|
||
decision="transfer_human" if transfer else "success",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
data = VisitorChatResponseData(
|
||
session_id=session_id,
|
||
reply=reply,
|
||
intent=intent,
|
||
has_disclaimer=has_disclaimer,
|
||
transfer_to_human=transfer,
|
||
)
|
||
return ok(data.model_dump(), ctx.trace_id)
|