AI · Sequence Diagram

Tương tác giữa các thành phần — AI

Trang này thể hiện thứ tự giao tiếp giữa client, router, controller, service và repository/worker. Mỗi mũi tên là một lần trao đổi dữ liệu có thật trong kiến trúc code.

Luồng chính của ai

complexity 3 happy path stateless

Luồng này bắt đầu khi client gọi endpoint của /api/v1/ai và kết thúc khi API trả JSON response hoặc lỗi chuẩn hóa.

ClientRouterControllerServiceRepository/Worker/api/v1/ai/moderatevalidateservice methodDB/Redis/joberrorresponse JSON
Mỗi mũi tên là một lần gọi thật trong kiến trúc route → controller → service → repository/worker.
#TừĐếnMessage/MethodDữ liệu truyền điDữ liệu trả vềGhi chú
1ClientRouter/api/v1/ai/moderateHTTP request + token/bodyRoute matchEndpoint thật trong route file.
2RouterMiddlewareauthorization/validateheaders/body/query/paramsreq.user hoặc parsed bodyRoute protected dừng nếu token sai.
3ControllerServiceservice methodDTO + userIddomain resultController không tự quyết rule phức tạp.
4ServiceRepository/ProviderPrisma/Redis/Queue/APIdomain payloadrecord/job/resultSide effect xảy ra ở đây.
5ServiceControllerreturn resultrecord/resultresponse dataLỗi throw qua error handler.
3
src/modules/ai/promt/system.promt.ts

Bước service là trọng tâm: code kiểm tra nghiệp vụ, gọi repository, quyết định enqueue job hoặc gọi provider ngoài.

Luồng lỗi phổ biến

Điểm failĐiều kiệnKết quả
UnauthorizedThiếu/sai token ở route protected401 Token invalid
Validation failedZod schema không pass400 Validation failed
Resource not foundRepository không tìm thấy record404 Resource not found
ForbiddenUser không có quyền hoặc role không đúng403 Forbidden
External dependency failRedis/DB/provider lỗi500 Internal server error