Files
geo/server/internal/tenant/transport/question_expansion_handler.go
T
root 2c4f7d6fcf fix(brand-questions): surface seed topic min length as actionable error
Reject AI seed topics shorter than 2 characters on both client and server with
a dedicated invalid_seed_topic error code, and render an inline hint listing
the offending terms so users know exactly what to fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 10:18:28 +08:00

135 lines
3.5 KiB
Go

package transport
import (
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/geo-platform/tenant-api/internal/bootstrap"
"github.com/geo-platform/tenant-api/internal/shared/response"
"github.com/geo-platform/tenant-api/internal/tenant/app"
)
type QuestionExpansionHandler struct {
svc *app.QuestionExpansionService
}
func NewQuestionExpansionHandler(a *bootstrap.App) *QuestionExpansionHandler {
return &QuestionExpansionHandler{svc: a.QuestionExpansion}
}
func (h *QuestionExpansionHandler) CombinationPreview(c *gin.Context) {
brandID, ok := parseBrandIDParam(c)
if !ok {
return
}
var req app.QuestionCombinationRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
data, err := h.svc.GenerateByCombination(c.Request.Context(), brandID, req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *QuestionExpansionHandler) CombinationFill(c *gin.Context) {
brandID, ok := parseBrandIDParam(c)
if !ok {
return
}
var req app.QuestionCombinationFillRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
data, err := h.svc.FillQuestionCombination(c.Request.Context(), brandID, c.ClientIP(), req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *QuestionExpansionHandler) AIDistill(c *gin.Context) {
brandID, ok := parseBrandIDParam(c)
if !ok {
return
}
var req app.QuestionDistillRequest
if err := c.ShouldBindJSON(&req); err != nil {
if isInvalidSeedTopicBindError(err) {
response.Error(c, response.ErrBadRequest(40091, "invalid_seed_topic", "seed_topic must contain at least 2 characters"))
return
}
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
data, err := h.svc.GenerateByAIDistill(c.Request.Context(), brandID, req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *QuestionExpansionHandler) ClassifyMetadata(c *gin.Context) {
brandID, ok := parseBrandIDParam(c)
if !ok {
return
}
var req struct {
Texts []string `json:"texts" binding:"required,min=1"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
data, err := h.svc.ClassifyMetadata(c.Request.Context(), brandID, req.Texts)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *QuestionExpansionHandler) Materialize(c *gin.Context) {
brandID, ok := parseBrandIDParam(c)
if !ok {
return
}
var req app.MaterializeQuestionsRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
data, err := h.svc.MaterializeQuestions(c.Request.Context(), brandID, req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func parseBrandIDParam(c *gin.Context) (int64, bool) {
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || brandID <= 0 {
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
return 0, false
}
return brandID, true
}
func isInvalidSeedTopicBindError(err error) bool {
if err == nil {
return false
}
message := err.Error()
return strings.Contains(message, "QuestionDistillRequest.SeedTopic") ||
strings.Contains(message, "seed_topic")
}