2026-04-15 17:12:30 +08:00
|
|
|
package dashboard
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"cs-agent/internal/ai/rag"
|
|
|
|
|
"cs-agent/internal/pkg/constants"
|
|
|
|
|
"cs-agent/internal/pkg/dto/request"
|
|
|
|
|
"cs-agent/internal/services"
|
|
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-04-09 10:01:23 +08:00
|
|
|
"github.com/mlogclub/simple/web"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type KnowledgeRetrieveController struct {
|
2026-05-23 22:10:20 +08:00
|
|
|
Ctx *gin.Context
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *KnowledgeRetrieveController) PostDebugSearch() *web.JsonResult {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := request.KnowledgeSearchRequest{}
|
|
|
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := rag.Answer.DebugSearch(context.Background(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
return web.JsonData(resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *KnowledgeRetrieveController) PostDebugAnswer() *web.JsonResult {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := request.KnowledgeAnswerRequest{}
|
|
|
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := rag.Answer.DebugAnswer(context.Background(), req, operator)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
return web.JsonData(resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *KnowledgeRetrieveController) PostBuild() *web.JsonResult {
|
|
|
|
|
req := struct {
|
|
|
|
|
DocumentID int64 `json:"documentId"`
|
|
|
|
|
FAQID int64 `json:"faqId"`
|
|
|
|
|
}{}
|
|
|
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.DocumentID > 0 {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentUpdate); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
if err := rag.Answer.BuildDocumentIndex(context.Background(), req.DocumentID); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
return web.JsonSuccess()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.FAQID > 0 {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQUpdate); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
if err := rag.Index.IndexFAQByID(context.Background(), req.FAQID); err != nil {
|
|
|
|
|
return web.JsonError(err)
|
|
|
|
|
}
|
|
|
|
|
return web.JsonSuccess()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return web.JsonErrorMsg("documentId或faqId不能为空")
|
|
|
|
|
}
|