Files
gochat/backend/internal/handler/api/v1/captain_task_handler.go.bak
rogee aeddedf2a3 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.
2026-07-07 14:44:12 +08:00

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)
}