Files
group_xinghuo_jinrong/app/api/visitor.py
T
zhanghongyu_0626 b841f68295 feat(visitor): Implement visitor chat functionality and enhance customer service interactions
- 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.
2026-09-09 18:32:00 +08:00

58 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""游客对话接口:免认证 /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)