18c9354095
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
349 lines
10 KiB
Go
349 lines
10 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"code.tczkiot.com/wlw/ai-agent/internal/builders"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/constants"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto/request"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto/response"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/i18nx"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx/params"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/common/strs"
|
|
"github.com/mlogclub/simple/web"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
func ConversationAnyList(ctx *gin.Context) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
cnd := params.NewPagedSqlCnd(ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "service_mode"},
|
|
params.QueryFilter{ParamName: "current_assignee_id"},
|
|
).Desc("last_message_at").Desc("id")
|
|
|
|
paging := params.GetPaging(ctx)
|
|
|
|
if keyword, _ := params.Get(ctx, "keyword"); strs.IsNotBlank(keyword) {
|
|
keywordLike := "%" + strings.TrimSpace(keyword) + "%"
|
|
cnd.Where("customer_name LIKE ? OR last_message_summary LIKE ?", keywordLike, keywordLike)
|
|
}
|
|
|
|
if agentTeamID, _ := params.GetInt64(ctx, "agent_team_id"); agentTeamID > 0 {
|
|
userIDs := services.AgentProfileService.GetUserIDsByTeamID(agentTeamID)
|
|
if len(userIDs) == 0 {
|
|
httpx.WriteJSON(ctx, &web.PageResult{
|
|
Results: []response.ConversationResponse{},
|
|
Page: paging,
|
|
})
|
|
return
|
|
}
|
|
cnd.In("current_assignee_id", userIDs)
|
|
}
|
|
|
|
list, paging := services.ConversationService.FindPageByCnd(cnd)
|
|
results := make([]response.ConversationResponse, 0, len(list))
|
|
for _, item := range list {
|
|
results = append(results, builders.BuildConversationWithLocale(&item, i18nx.Locale(ctx)))
|
|
}
|
|
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
|
}
|
|
|
|
func ConversationAnyConversations(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
filterValue, _ := params.Get(ctx, "filter")
|
|
keyword, _ := params.Get(ctx, "keyword")
|
|
paging := params.GetPaging(ctx)
|
|
|
|
list, paging, err := services.ConversationService.ListConversations(
|
|
operator.UserID,
|
|
request.AgentConversationFilter(strings.TrimSpace(filterValue)),
|
|
keyword,
|
|
paging,
|
|
)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
results := make([]response.ConversationResponse, 0, len(list))
|
|
for _, item := range list {
|
|
results = append(results, builders.BuildConversationWithLocale(&item, i18nx.Locale(ctx)))
|
|
}
|
|
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
|
}
|
|
|
|
func ConversationGetBy(ctx *gin.Context) {
|
|
id, ok := httpx.GetPathInt64(ctx, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
item := services.ConversationService.Get(id)
|
|
if item == nil {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0116"))
|
|
return
|
|
}
|
|
|
|
detail := response.ConversationDetailResponse{
|
|
ConversationResponse: builders.BuildConversationWithLocale(item, i18nx.Locale(ctx)),
|
|
Participants: builders.BuildParticipantResponses(id),
|
|
}
|
|
httpx.WriteJSON(ctx, detail)
|
|
}
|
|
|
|
func ConversationAnyMessage_list(ctx *gin.Context) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
var (
|
|
conversationID, _ = params.GetInt64(ctx, "conversation_id")
|
|
senderType, _ = params.Get(ctx, "sender_type")
|
|
messageType, _ = params.Get(ctx, "message_type")
|
|
cursor, _ = params.GetInt64(ctx, "cursor")
|
|
limit, _ = params.GetInt(ctx, "limit")
|
|
)
|
|
if conversation := services.ConversationService.Get(conversationID); conversation == nil {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0116"))
|
|
return
|
|
}
|
|
|
|
list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
|
|
conversationID, cursor, limit, senderType, messageType,
|
|
)
|
|
results := builders.BuildMessagesWithLocale(list, i18nx.Locale(ctx))
|
|
|
|
httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore))
|
|
}
|
|
|
|
func ConversationPostAssign(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationAssign)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.AssignConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.AssignConversation(req, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func ConversationPostDispatch(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationAssign)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.DispatchConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.AutoAssignConversation(req.ConversationID, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func ConversationPostTransfer(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationTransfer)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.TransferConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.TransferConversation(req.ConversationID, req.ToUserID, req.Reason, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func ConversationPostClose(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationClose)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.CloseConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.CloseConversation(req.ConversationID, req.CloseReason, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func ConversationPostSend_message(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.SendConversationMessageRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
item, err := services.MessageService.SendAgentMessageWithRequestID(req.ConversationID, 0, req.ClientMsgID, req.MessageType, req.Content, req.Payload, operator, httpx.GetRequestID(ctx))
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildMessageWithLocale(item, i18nx.Locale(ctx)))
|
|
}
|
|
|
|
func ConversationPostRecall_message(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.RecallConversationMessageRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
item, err := services.MessageService.RecallAgentMessage(req.MessageID, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildMessageWithLocale(item, i18nx.Locale(ctx)))
|
|
}
|
|
|
|
func ConversationPostRead(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.ReadConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.MarkAgentConversationReadToMessage(req.ConversationID, req.MessageID, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func ConversationPostUpload_image(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversation_id"))
|
|
if rawConv == "" {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0064"))
|
|
return
|
|
}
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
if err != nil || conversationID <= 0 {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0064"))
|
|
return
|
|
}
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeAgent, operator, nil); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
header, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0322"))
|
|
return
|
|
}
|
|
if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0090"))
|
|
return
|
|
}
|
|
|
|
item, err := services.AssetService.UploadConversationImageFile(header, "images", conversationID, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
|
}
|
|
|
|
func ConversationPostUpload_attachment(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversation_id"))
|
|
if rawConv == "" {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0064"))
|
|
return
|
|
}
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
if err != nil || conversationID <= 0 {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0064"))
|
|
return
|
|
}
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeAgent, operator, nil); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
header, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0324"))
|
|
return
|
|
}
|
|
item, err := services.AssetService.UploadConversationFile(header, "attachments", conversationID, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
|
}
|