Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
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/:id/captain/tasks/label_suggestion
|
||||
func (h *CaptainTaskExtendedHandler) LabelSuggestion(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
|
||||
}
|
||||
|
||||
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/:id/captain/tasks/follow_up
|
||||
func (h *CaptainTaskExtendedHandler) FollowUp(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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user