feat: 实现自建情景功能

## 功能概述
- 用户可创建、编辑、删除自定义情景
- 支持自定义情景名称、图标、描述、Prompt、首句引导
- 完整的权限隔离,用户只能管理自己的情景
- 深度集成 Eino 框架,动态加载自建情景 Prompt

## 后端实现
### 数据库
- 新增 user_scenarios 表
- 支持用户配额(最多 20 个)
- 字段验证:description 可选,prompt 最小 10 字符

### API
- GET /api/scenarios - 获取用户情景列表
- POST /api/scenarios - 创建情景
- GET /api/scenarios/:id - 获取详情
- PATCH /api/scenarios/:id - 更新情景
- DELETE /api/scenarios/:id - 删除情景

### Eino 集成
- PipelineState 添加 UserID 字段
- nodes_history 动态加载用户自建情景
- GetScenarioPrompt 支持自建情景优先级

## 前端实现
### 组件
- CreateScenarioModal - 创建情景对话框
- EditScenarioModal - 编辑情景对话框
- ConfigPanel 改造 - 分组显示系统预置和自建情景

### Hook
- useScenarios - 合并系统和自建情景,提供 CRUD 接口

### 国际化
- 中文、英文、日文翻译支持

## 问题修复
- 修复 CORS 问题:使用 Vite 代理
- 统一验证规则:description 可选,prompt 最小 10 字符
- 修复数据库约束:使用 NULLIF 处理空字符串

## 文件变更
新增文件: 13 个
修改文件: 14 个

详见文档: docs/自建情景功能完整文档.md
This commit is contained in:
2026-06-21 15:38:28 +08:00
parent 9ad486d117
commit 1079e22699
24 changed files with 2779 additions and 62 deletions

View File

@@ -0,0 +1,208 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/hhs/camtalk/internal/logger"
"github.com/hhs/camtalk/internal/models"
"github.com/hhs/camtalk/internal/store"
)
const (
MaxScenariosPerUser = 20 // 每个用户最多 20 个自建情景
MaxPromptLength = 2000 // Prompt 最大长度
)
// UserScenarioHandler 用户情景 API Handler。
type UserScenarioHandler struct {
repo store.UserScenarioRepository
}
// NewUserScenarioHandler 创建用户情景 Handler。
func NewUserScenarioHandler(repo store.UserScenarioRepository) *UserScenarioHandler {
return &UserScenarioHandler{repo: repo}
}
// List 获取用户的所有自建情景。
// GET /api/scenarios
func (h *UserScenarioHandler) List(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
scenarios, err := h.repo.FindByUserID(c.Request.Context(), userID.(string))
if err != nil {
logger.Log.Errorw("查询用户情景失败", "user_id", userID, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询失败"})
return
}
if scenarios == nil {
scenarios = []*models.UserScenario{}
}
c.JSON(http.StatusOK, models.UserScenarioListResponse{
Scenarios: scenarios,
Total: len(scenarios),
})
}
// Create 创建用户情景。
// POST /api/scenarios
func (h *UserScenarioHandler) Create(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
var req models.CreateUserScenarioRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: " + err.Error()})
return
}
// 检查用户是否已达上限
count, err := h.repo.CountByUserID(c.Request.Context(), userID.(string))
if err != nil {
logger.Log.Errorw("统计用户情景数量失败", "user_id", userID, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建失败"})
return
}
if count >= MaxScenariosPerUser {
c.JSON(http.StatusBadRequest, gin.H{"error": "已达创建上限(最多 20 个)"})
return
}
// 创建情景
scenario := &models.UserScenario{
UserID: userID.(string),
Name: req.Name,
Icon: req.Icon,
Description: req.Description,
Prompt: req.Prompt,
Greeting: req.Greeting,
Language: req.Language,
}
if err := h.repo.Create(c.Request.Context(), scenario); err != nil {
logger.Log.Errorw("创建用户情景失败", "user_id", userID, "error", err)
if err.Error() == "duplicate key value violates unique constraint" {
c.JSON(http.StatusBadRequest, gin.H{"error": "情景名称已存在"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建失败"})
return
}
logger.Log.Infow("创建用户情景成功", "user_id", userID, "scenario_id", scenario.ID)
c.JSON(http.StatusCreated, scenario)
}
// Get 获取单个情景详情。
// GET /api/scenarios/:id
func (h *UserScenarioHandler) Get(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
scenarioID := c.Param("id")
scenario, err := h.repo.FindByIDAndUserID(c.Request.Context(), scenarioID, userID.(string))
if err != nil {
logger.Log.Errorw("查询用户情景失败", "user_id", userID, "scenario_id", scenarioID, "error", err)
c.JSON(http.StatusNotFound, gin.H{"error": "情景不存在或无权限"})
return
}
c.JSON(http.StatusOK, scenario)
}
// Update 更新用户情景。
// PATCH /api/scenarios/:id
func (h *UserScenarioHandler) Update(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
scenarioID := c.Param("id")
// 查询并校验所有权
scenario, err := h.repo.FindByIDAndUserID(c.Request.Context(), scenarioID, userID.(string))
if err != nil {
logger.Log.Errorw("查询用户情景失败", "user_id", userID, "scenario_id", scenarioID, "error", err)
c.JSON(http.StatusNotFound, gin.H{"error": "情景不存在或无权限"})
return
}
var req models.UpdateUserScenarioRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: " + err.Error()})
return
}
// 更新字段
if req.Name != nil {
scenario.Name = *req.Name
}
if req.Icon != nil {
scenario.Icon = *req.Icon
}
if req.Description != nil {
scenario.Description = *req.Description
}
if req.Prompt != nil {
scenario.Prompt = *req.Prompt
}
if req.Greeting != nil {
scenario.Greeting = *req.Greeting
}
if req.Language != nil {
scenario.Language = *req.Language
}
if err := h.repo.Update(c.Request.Context(), scenario); err != nil {
logger.Log.Errorw("更新用户情景失败", "user_id", userID, "scenario_id", scenarioID, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新失败"})
return
}
logger.Log.Infow("更新用户情景成功", "user_id", userID, "scenario_id", scenarioID)
c.JSON(http.StatusOK, scenario)
}
// Delete 删除用户情景。
// DELETE /api/scenarios/:id
func (h *UserScenarioHandler) Delete(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
scenarioID := c.Param("id")
// 查询并校验所有权
_, err := h.repo.FindByIDAndUserID(c.Request.Context(), scenarioID, userID.(string))
if err != nil {
logger.Log.Errorw("查询用户情景失败", "user_id", userID, "scenario_id", scenarioID, "error", err)
c.JSON(http.StatusNotFound, gin.H{"error": "情景不存在或无权限"})
return
}
if err := h.repo.Delete(c.Request.Context(), scenarioID); err != nil {
logger.Log.Errorw("删除用户情景失败", "user_id", userID, "scenario_id", scenarioID, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除失败"})
return
}
logger.Log.Infow("删除用户情景成功", "user_id", userID, "scenario_id", scenarioID)
c.Status(http.StatusNoContent)
}