288 lines
10 KiB
Go
288 lines
10 KiB
Go
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/pagination"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
pkgvalidator "github.com/gochat/gochat/pkg/validator"
|
|
)
|
|
// CopilotHandler handles Copilot REST API endpoints.
|
|
// Reference: Chatwoot enterprise/app/controllers/api/v1/captain/copilot_threads_controller.rb
|
|
type CopilotHandler struct {
|
|
svc *service.CopilotService
|
|
}
|
|
|
|
// NewCopilotHandler creates a new CopilotHandler.
|
|
func NewCopilotHandler(svc *service.CopilotService) *CopilotHandler {
|
|
return &CopilotHandler{svc: svc}
|
|
}
|
|
|
|
// CreateThread creates a new copilot thread.
|
|
// POST /api/v1/accounts/:account_id/copilot_threads
|
|
func (h *CopilotHandler) CreateThread(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
|
|
}
|
|
|
|
// Extract userID from auth context header (X-User-ID).
|
|
// When auth middleware is wired, this will come from c.Get("user_id").
|
|
userIDStr := c.GetHeader("X-User-ID")
|
|
userID, err := strconv.ParseUint(userIDStr, 10, 64)
|
|
if err != nil || userID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid or missing user_id")
|
|
return
|
|
}
|
|
|
|
var req service.CreateThreadRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
thread, err := h.svc.CreateThread(c.Request.Context(), uint(accountID), uint(userID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("CreateThread: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create thread")
|
|
return
|
|
}
|
|
|
|
response.Created(c, thread)
|
|
}
|
|
|
|
// GetThread retrieves a copilot thread by ID.
|
|
// GET /api/v1/accounts/:account_id/copilot_threads/:id
|
|
func (h *CopilotHandler) GetThread(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
thread, err := h.svc.GetThread(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
applogger.L().Errorf("GetThread: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "thread not found")
|
|
return
|
|
}
|
|
|
|
response.OK(c, thread)
|
|
}
|
|
|
|
// ListThreads retrieves copilot threads for a user.
|
|
// GET /api/v1/accounts/:account_id/copilot_threads
|
|
func (h *CopilotHandler) ListThreads(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
|
|
}
|
|
|
|
userIDStr := c.GetHeader("X-User-ID")
|
|
userID, err := strconv.ParseUint(userIDStr, 10, 64)
|
|
if err != nil || userID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid or missing user_id")
|
|
return
|
|
}
|
|
|
|
p := pagination.Parse(c)
|
|
threads, count, err := h.svc.ListThreads(c.Request.Context(), uint(accountID), uint(userID), p.Offset, p.PerPage)
|
|
if err != nil {
|
|
applogger.L().Errorf("ListThreads: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list threads")
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, threads, p.Page, p.PerPage, count)
|
|
}
|
|
|
|
// SendMessage sends a message in a copilot thread and generates an assistant reply.
|
|
// POST /api/v1/accounts/:account_id/copilot_threads/:id/messages
|
|
func (h *CopilotHandler) SendMessage(c *gin.Context) {
|
|
threadID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid thread id")
|
|
return
|
|
}
|
|
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.SendMessageRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.SendMessage(c.Request.Context(), uint(threadID), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("SendMessage: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to send message")
|
|
return
|
|
}
|
|
|
|
response.Created(c, result)
|
|
}
|
|
|
|
// GetSuggestedReplies generates reply suggestions for a conversation.
|
|
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/suggested_replies
|
|
func (h *CopilotHandler) GetSuggestedReplies(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
|
|
}
|
|
|
|
// conversation_id is used to fetch context; here we accept it as a query param
|
|
// for the conversation context (in production, this would fetch the conversation)
|
|
conversationContext := c.Query("context")
|
|
if conversationContext == "" {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "missing conversation context")
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.GetSuggestedReplies(c.Request.Context(), uint(accountID), conversationContext)
|
|
if err != nil {
|
|
applogger.L().Errorf("GetSuggestedReplies: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate suggested replies")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// SummarizeConversation generates a summary of a conversation.
|
|
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/summary
|
|
func (h *CopilotHandler) SummarizeConversation(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
|
|
}
|
|
|
|
conversationContext := c.Query("context")
|
|
if conversationContext == "" {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "missing conversation context")
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.SummarizeConversation(c.Request.Context(), uint(accountID), conversationContext)
|
|
if err != nil {
|
|
applogger.L().Errorf("SummarizeConversation: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to summarize conversation")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// DeleteThread deletes a copilot thread.
|
|
// DELETE /api/v1/accounts/:account_id/copilot_threads/:id
|
|
func (h *CopilotHandler) DeleteThread(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
if err := h.svc.DeleteThread(c.Request.Context(), uint(id)); err != nil {
|
|
applogger.L().Errorf("DeleteThread: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete thread")
|
|
return
|
|
}
|
|
|
|
response.NoContent(c)
|
|
}
|
|
|
|
// TranslateMessage translates a message to a target language.
|
|
// POST /api/v1/accounts/:account_id/copilot/translate
|
|
func (h *CopilotHandler) TranslateMessage(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.TranslateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.TranslateMessage(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("TranslateMessage: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to translate message")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// --- Copilot Suggestion Messages ---
|
|
// Reference: Chatwoot enterprise/app/controllers/api/v1/copilot_messages_controller.rb
|
|
// Conversation-level suggestions (GET/POST /api/v1/accounts/:id/copilot_messages)
|
|
|
|
// ListSuggestionMessages lists copilot suggestion messages for a conversation.
|
|
// GET /api/v1/accounts/:account_id/copilot_messages
|
|
func (h *CopilotHandler) ListSuggestionMessages(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
|
|
}
|
|
|
|
conversationIDStr := c.Query("conversation_id")
|
|
conversationID, err := strconv.ParseUint(conversationIDStr, 10, 64)
|
|
if err != nil || conversationID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "conversation_id is required")
|
|
return
|
|
}
|
|
|
|
p := pagination.Parse(c)
|
|
result, err := h.svc.GetCopilotSuggestions(c.Request.Context(), uint(accountID), uint(conversationID), p.Page, p.PerPage)
|
|
if err != nil {
|
|
applogger.L().Errorf("ListSuggestionMessages: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list suggestion messages")
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, result.Messages, result.Page, result.PerPage, result.TotalCount)
|
|
}
|
|
|
|
// CreateSuggestionMessage creates a copilot suggestion message.
|
|
// POST /api/v1/accounts/:account_id/copilot_messages
|
|
func (h *CopilotHandler) CreateSuggestionMessage(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.CreateSuggestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
if err := pkgvalidator.ValidateStruct(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
msg, err := h.svc.CreateCopilotSuggestion(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("CreateSuggestionMessage: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create suggestion message")
|
|
return
|
|
}
|
|
|
|
response.Created(c, msg)
|
|
} |