2026-04-01 00:58:42 +08:00
|
|
|
package transport
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"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"
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type TemplateHandler struct {
|
|
|
|
|
svc *app.TemplateService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTemplateHandler(a *bootstrap.App) *TemplateHandler {
|
2026-04-05 17:14:13 +08:00
|
|
|
knowledgeSvc := app.NewKnowledgeService(
|
|
|
|
|
a.DB,
|
|
|
|
|
a.RetrievalProvider,
|
|
|
|
|
a.VectorStore,
|
|
|
|
|
a.ObjectStorage,
|
2026-04-05 20:41:42 +08:00
|
|
|
a.LLM,
|
2026-04-05 17:14:13 +08:00
|
|
|
a.Logger,
|
2026-05-01 16:01:23 +08:00
|
|
|
a.Config().Retrieval,
|
|
|
|
|
a.Config().LLM,
|
2026-04-05 17:14:13 +08:00
|
|
|
0,
|
|
|
|
|
0,
|
2026-05-01 16:01:23 +08:00
|
|
|
).WithConfigProvider(a.ConfigStore)
|
2026-04-05 17:14:13 +08:00
|
|
|
|
2026-04-01 00:58:42 +08:00
|
|
|
return &TemplateHandler{
|
|
|
|
|
svc: app.NewTemplateService(
|
|
|
|
|
a.DB,
|
2026-04-02 00:31:28 +08:00
|
|
|
repository.NewCachedTemplateRepository(
|
|
|
|
|
repository.NewTemplateRepository(a.DB),
|
|
|
|
|
a.Cache,
|
|
|
|
|
),
|
2026-04-01 00:58:42 +08:00
|
|
|
a.LLM,
|
2026-04-15 14:20:26 +08:00
|
|
|
a.RabbitMQ,
|
2026-04-05 17:14:13 +08:00
|
|
|
knowledgeSvc,
|
2026-04-01 00:58:42 +08:00
|
|
|
a.GenerationStreams,
|
2026-05-01 16:01:23 +08:00
|
|
|
a.Config().Generation,
|
|
|
|
|
a.Config().LLM.MaxOutputTokens,
|
|
|
|
|
).WithCache(a.Cache).WithConfigProvider(a.ConfigStore),
|
2026-04-01 00:58:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) List(c *gin.Context) {
|
|
|
|
|
data, err := h.svc.List(c.Request.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) Detail(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, err := h.svc.Detail(c.Request.Context(), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) Generate(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var req app.GenerateRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, err := h.svc.Generate(c.Request.Context(), id, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
2026-04-02 00:31:28 +08:00
|
|
|
|
|
|
|
|
func (h *TemplateHandler) SaveDraft(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var req app.SaveDraftRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, err := h.svc.SaveDraft(c.Request.Context(), id, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) CreateAnalyzeTask(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req app.AnalyzeTaskRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.CreateAnalyzeTask(c.Request.Context(), id, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) GetAnalyzeTaskResult(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskID := c.Query("task_id")
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_task_id", "task id must not be empty"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.GetAnalyzeTask(c.Request.Context(), id, taskID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) CreateTitleTask(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req app.TitleTaskRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.CreateTitleTask(c.Request.Context(), id, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) GetTitleTaskResult(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskID := c.Query("task_id")
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_task_id", "task id must not be empty"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.GetTitleTask(c.Request.Context(), id, taskID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) CreateOutlineTask(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req app.OutlineTaskRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.CreateOutlineTask(c.Request.Context(), id, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *TemplateHandler) GetOutlineTaskResult(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "template id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskID := c.Query("task_id")
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_task_id", "task id must not be empty"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := h.svc.GetOutlineTask(c.Request.Context(), id, taskID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|