运营可观测知识源目录(knowledge_sources + admin /ai),不含 Chunk/Embedding。 Co-authored-by: Cursor <cursoragent@cursor.com>
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerAIConfig(authed *gin.RouterGroup) {
|
|
g := authed.Group("/ai")
|
|
g.GET("/system-prompts", middleware.RequireAdminPermission(h.Svc, admin.PermAIConfigRead), h.ListSystemPrompts)
|
|
g.GET("/system-prompts/:id", middleware.RequireAdminPermission(h.Svc, admin.PermAIConfigRead), h.GetSystemPrompt)
|
|
g.GET("/knowledge-sources", middleware.RequireAdminPermission(h.Svc, admin.PermAIConfigRead), h.ListKnowledgeSources)
|
|
g.GET("/knowledge-sources/:id", middleware.RequireAdminPermission(h.Svc, admin.PermAIConfigRead), h.GetKnowledgeSource)
|
|
}
|
|
|
|
func (h *AdminHandler) ListSystemPrompts(c *gin.Context) {
|
|
items, err := h.Svc.ListSystemPrompts(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50027, "list system prompts failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetSystemPrompt(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
row, err := h.Svc.GetSystemPrompt(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrSystemPromptNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40404, "system prompt not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50028, "get system prompt failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) ListKnowledgeSources(c *gin.Context) {
|
|
items, err := h.Svc.ListKnowledgeSources(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50029, "list knowledge sources failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetKnowledgeSource(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
row, err := h.Svc.GetKnowledgeSource(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrKnowledgeSourceNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40405, "knowledge source not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50030, "get knowledge source failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|