prompt_config_controller.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/2930134478/AI-CS/backend/service"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // PromptConfigController 提示词配置控制器(供「提示词」页)
  8. type PromptConfigController struct {
  9. service *service.PromptConfigService
  10. }
  11. // NewPromptConfigController 创建控制器实例
  12. func NewPromptConfigController(s *service.PromptConfigService) *PromptConfigController {
  13. return &PromptConfigController{service: s}
  14. }
  15. // Get 获取所有提示词项(含默认内容)
  16. // GET /agent/prompts?user_id=1
  17. func (p *PromptConfigController) Get(c *gin.Context) {
  18. _, err := parseUintQuery(c, "user_id")
  19. if err != nil {
  20. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  21. return
  22. }
  23. list, err := p.service.GetAllForAPI()
  24. if err != nil {
  25. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  26. return
  27. }
  28. c.JSON(http.StatusOK, gin.H{"prompts": list})
  29. }
  30. // Update 更新单条提示词(仅管理员)
  31. // PUT /agent/prompts
  32. // Body: { "user_id": 1, "key": "rag_prompt", "content": "..." }
  33. func (p *PromptConfigController) Update(c *gin.Context) {
  34. var req struct {
  35. UserID uint `json:"user_id" binding:"required"`
  36. Key string `json:"key" binding:"required"`
  37. Content string `json:"content"`
  38. }
  39. if err := c.ShouldBindJSON(&req); err != nil {
  40. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
  41. return
  42. }
  43. if err := p.service.Update(req.UserID, req.Key, req.Content); err != nil {
  44. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  45. return
  46. }
  47. c.JSON(http.StatusOK, gin.H{"message": "保存成功"})
  48. }