101 lines
3.3 KiB
Plaintext
101 lines
3.3 KiB
Plaintext
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CaptainTaskHandler handles Captain AI task API endpoints.
|
|
// Reference: Chatwoot enterprise/app/controllers/api/v1/accounts/captain/tasks_controller.rb
|
|
// Tasks are standalone AI suggestions (reply_suggestion, summarize, rewrite)
|
|
// independent of the CopilotThread chat flow.
|
|
type CaptainTaskHandler struct {
|
|
svc *service.CaptainTaskService
|
|
}
|
|
|
|
// NewCaptainTaskHandler creates a new CaptainTaskHandler.
|
|
func NewCaptainTaskHandler(svc *service.CaptainTaskService) *CaptainTaskHandler {
|
|
return &CaptainTaskHandler{svc: svc}
|
|
}
|
|
|
|
// ReplySuggestion generates reply suggestions for a conversation.
|
|
// POST /api/v1/accounts/:account_id/captain/tasks/reply_suggestion
|
|
func (h *CaptainTaskHandler) ReplySuggestion(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
var req service.TaskReplySuggestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.ReplySuggestion(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("ReplySuggestion: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate reply suggestions")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Summarize generates a conversation summary.
|
|
// POST /api/v1/accounts/:account_id/captain/tasks/summarize
|
|
func (h *CaptainTaskHandler) Summarize(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
var req service.TaskSummarizeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.Summarize(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Summarize: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to summarize conversation")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Rewrite rewrites a draft message to improve tone and clarity.
|
|
// POST /api/v1/accounts/:account_id/captain/tasks/rewrite
|
|
func (h *CaptainTaskHandler) Rewrite(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
var req service.TaskRewriteRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.Rewrite(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Rewrite: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to rewrite message")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|