162 lines
5.0 KiB
Go
162 lines
5.0 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/service"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CaptainTaskExtendedHandler handles label_suggestion and follow_up GET endpoints.
|
|
// Reference: Chatwoot Captain::ConversationInsightController (label_suggestion + follow_up)
|
|
type CaptainTaskExtendedHandler struct {
|
|
svc *service.CaptainTaskExtendedService
|
|
}
|
|
|
|
func NewCaptainTaskExtendedHandler(svc *service.CaptainTaskExtendedService) *CaptainTaskExtendedHandler {
|
|
return &CaptainTaskExtendedHandler{svc: svc}
|
|
}
|
|
|
|
// LabelSuggestion returns AI-generated label suggestions for conversations.
|
|
// GET /api/v1/accounts/:account_id/captain/tasks/label_suggestion
|
|
func (h *CaptainTaskExtendedHandler) LabelSuggestion(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
if c.Request.Method == http.MethodPost {
|
|
var req service.ChatwootLabelSuggestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
result, err := h.svc.LabelSuggestion(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Label suggestion: %v", err)
|
|
renderCaptainTaskError(c, err)
|
|
return
|
|
}
|
|
renderCaptainExtendedTaskResult(c, result)
|
|
return
|
|
}
|
|
|
|
// Parse conversation_ids from query param (comma-separated)
|
|
convIDsStr := c.Query("conversation_ids")
|
|
if convIDsStr == "" {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "conversation_ids required")
|
|
return
|
|
}
|
|
|
|
convIDs, err := parseUintSlice(convIDsStr)
|
|
if err != nil || len(convIDs) == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "invalid conversation_ids format")
|
|
return
|
|
}
|
|
|
|
assistantID, _ := strconv.ParseUint(c.Query("assistant_id"), 10, 64)
|
|
|
|
query := &service.LabelSuggestionQuery{
|
|
ConversationIDs: convIDs,
|
|
AssistantID: uint(assistantID),
|
|
}
|
|
|
|
result, err := h.svc.SuggestLabels(c.Request.Context(), uint(accountID), query)
|
|
if err != nil {
|
|
applogger.L().Errorf("Label suggestion: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate label suggestions")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// FollowUp returns AI-generated follow-up task suggestions for conversations.
|
|
// GET /api/v1/accounts/:account_id/captain/tasks/follow_up
|
|
func (h *CaptainTaskExtendedHandler) FollowUp(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
if c.Request.Method == http.MethodPost {
|
|
var req service.ChatwootFollowUpRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
result, err := h.svc.FollowUp(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Follow-up: %v", err)
|
|
renderCaptainTaskError(c, err)
|
|
return
|
|
}
|
|
renderCaptainExtendedTaskResult(c, result)
|
|
return
|
|
}
|
|
|
|
convIDsStr := c.Query("conversation_ids")
|
|
if convIDsStr == "" {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "conversation_ids required")
|
|
return
|
|
}
|
|
|
|
convIDs, err := parseUintSlice(convIDsStr)
|
|
if err != nil || len(convIDs) == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "invalid conversation_ids format")
|
|
return
|
|
}
|
|
|
|
assistantID, _ := strconv.ParseUint(c.Query("assistant_id"), 10, 64)
|
|
|
|
query := &service.FollowUpQuery{
|
|
ConversationIDs: convIDs,
|
|
AssistantID: uint(assistantID),
|
|
}
|
|
|
|
result, err := h.svc.SuggestFollowUp(c.Request.Context(), uint(accountID), query)
|
|
if err != nil {
|
|
applogger.L().Errorf("Follow-up suggestion: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate follow-up suggestions")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func renderCaptainExtendedTaskResult(c *gin.Context, result *service.ChatwootTaskResult) {
|
|
if result == nil || result.Message == nil {
|
|
c.JSON(http.StatusOK, gin.H{"message": nil})
|
|
return
|
|
}
|
|
payload := gin.H{"message": *result.Message}
|
|
if result.FollowUpContext != nil {
|
|
payload["follow_up_context"] = result.FollowUpContext
|
|
}
|
|
c.JSON(http.StatusOK, payload)
|
|
}
|
|
|
|
// parseUintSlice parses a comma-separated string of uint values.
|
|
func parseUintSlice(s string) ([]uint, error) {
|
|
parts := strings.Split(s, ",")
|
|
var result []uint
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" {
|
|
continue
|
|
}
|
|
v, err := strconv.ParseUint(p, 10, 64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, uint(v))
|
|
}
|
|
return result, nil
|
|
}
|