🎨 优化角色卡功能模块,后续待优化图像上传&前端流畅性待优化

This commit is contained in:
2026-02-11 05:33:38 +08:00
parent 56e821b222
commit cf3197929e
12 changed files with 576 additions and 198 deletions

View File

@@ -9,26 +9,31 @@ import (
// AICharacter AI 角色表
type AICharacter struct {
global.GVA_MODEL
Name string `json:"name" gorm:"type:varchar(500);not null;comment:角色名称"`
Description string `json:"description" gorm:"type:text;comment:角色描述"`
Personality string `json:"personality" gorm:"type:text;comment:角色性格"`
Scenario string `json:"scenario" gorm:"type:text;comment:角色场景"`
Avatar string `json:"avatar" gorm:"type:varchar(1024);comment:角色头像"`
CreatorID *uint `json:"creatorId" gorm:"index;comment:创建者ID"`
Creator *AppUser `json:"creator" gorm:"foreignKey:CreatorID"`
CreatorName string `json:"creatorName" gorm:"type:varchar(200);comment:创建者名称"`
CreatorNotes string `json:"creatorNotes" gorm:"type:text;comment:创建者备注"`
CardData datatypes.JSON `json:"cardData" gorm:"type:jsonb;not null;comment:角色卡片数据"`
Tags pq.StringArray `json:"tags" gorm:"type:text[];comment:角色标签"`
IsPublic bool `json:"isPublic" gorm:"default:false;index;comment:是否公开"`
Version int `json:"version" gorm:"default:1;comment:角色版本"`
FirstMessage string `json:"firstMessage" gorm:"type:text;comment:第一条消息"`
ExampleMessages pq.StringArray `json:"exampleMessages" gorm:"type:text[];comment:消息示例"`
TotalChats int `json:"totalChats" gorm:"default:0;comment:对话总数"`
TotalLikes int `json:"totalLikes" gorm:"default:0;comment:点赞总数"`
UsageCount int `json:"usageCount" gorm:"default:0;comment:使用次数"`
FavoriteCount int `json:"favoriteCount" gorm:"default:0;comment:收藏次数"`
TokenCount int `json:"tokenCount" gorm:"default:0;comment:Token数量"`
Name string `json:"name" gorm:"type:varchar(500);not null;comment:角色名称"`
Description string `json:"description" gorm:"type:text;comment:角色描述"`
Personality string `json:"personality" gorm:"type:text;comment:角色性格"`
Scenario string `json:"scenario" gorm:"type:text;comment:角色场景"`
Avatar string `json:"avatar" gorm:"type:varchar(1024);comment:角色头像"`
CreatorID *uint `json:"creatorId" gorm:"index;comment:创建者ID"`
Creator *AppUser `json:"creator" gorm:"foreignKey:CreatorID"`
CreatorName string `json:"creatorName" gorm:"type:varchar(200);comment:创建者名称"`
CreatorNotes string `json:"creatorNotes" gorm:"type:text;comment:创建者备注"`
CardData datatypes.JSON `json:"cardData" gorm:"type:jsonb;not null;comment:角色卡片数据"`
Tags pq.StringArray `json:"tags" gorm:"type:text[];comment:角色标签"`
IsPublic bool `json:"isPublic" gorm:"default:false;index;comment:是否公开"`
Version int `json:"version" gorm:"default:1;comment:角色版本"`
FirstMessage string `json:"firstMessage" gorm:"type:text;comment:第一条消息"`
ExampleMessages pq.StringArray `json:"exampleMessages" gorm:"type:text[];comment:消息示例"`
SystemPrompt string `json:"systemPrompt" gorm:"type:text;comment:系统提示词"`
PostHistoryInstructions string `json:"postHistoryInstructions" gorm:"type:text;comment:后置历史指令"`
AlternateGreetings pq.StringArray `json:"alternateGreetings" gorm:"type:text[];comment:备用问候语"`
CharacterBook datatypes.JSON `json:"characterBook" gorm:"type:jsonb;comment:角色书/世界信息"`
Extensions datatypes.JSON `json:"extensions" gorm:"type:jsonb;comment:扩展数据(depth_prompt等)"`
TotalChats int `json:"totalChats" gorm:"default:0;comment:对话总数"`
TotalLikes int `json:"totalLikes" gorm:"default:0;comment:点赞总数"`
UsageCount int `json:"usageCount" gorm:"default:0;comment:使用次数"`
FavoriteCount int `json:"favoriteCount" gorm:"default:0;comment:收藏次数"`
TokenCount int `json:"tokenCount" gorm:"default:0;comment:Token数量"`
}
func (AICharacter) TableName() string {

View File

@@ -4,33 +4,43 @@ import "mime/multipart"
// CreateCharacterRequest 创建角色卡请求
type CreateCharacterRequest struct {
Name string `json:"name" binding:"required,min=1,max=500"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
Name string `json:"name" binding:"required,min=1,max=500"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
SystemPrompt string `json:"systemPrompt"`
PostHistoryInstructions string `json:"postHistoryInstructions"`
AlternateGreetings []string `json:"alternateGreetings"`
CharacterBook map[string]interface{} `json:"characterBook"`
Extensions map[string]interface{} `json:"extensions"`
}
// UpdateCharacterRequest 更新角色卡请求
type UpdateCharacterRequest struct {
ID uint `json:"id" binding:"required"`
Name string `json:"name" binding:"required,min=1,max=500"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
ID uint `json:"id" binding:"required"`
Name string `json:"name" binding:"required,min=1,max=500"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
SystemPrompt string `json:"systemPrompt"`
PostHistoryInstructions string `json:"postHistoryInstructions"`
AlternateGreetings []string `json:"alternateGreetings"`
CharacterBook map[string]interface{} `json:"characterBook"`
Extensions map[string]interface{} `json:"extensions"`
}
// CharacterListRequest 角色卡列表请求

View File

@@ -1,34 +1,40 @@
package response
import (
"encoding/json"
"git.echol.cn/loser/st/server/model/app"
"time"
)
// CharacterResponse 角色卡响应
type CharacterResponse struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorID *uint `json:"creatorId"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
Version int `json:"version"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
TotalChats int `json:"totalChats"`
TotalLikes int `json:"totalLikes"`
UsageCount int `json:"usageCount"`
FavoriteCount int `json:"favoriteCount"`
TokenCount int `json:"tokenCount"`
IsFavorited bool `json:"isFavorited"` // 当前用户是否收藏
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Personality string `json:"personality"`
Scenario string `json:"scenario"`
Avatar string `json:"avatar"`
CreatorID *uint `json:"creatorId"`
CreatorName string `json:"creatorName"`
CreatorNotes string `json:"creatorNotes"`
Tags []string `json:"tags"`
IsPublic bool `json:"isPublic"`
Version int `json:"version"`
FirstMessage string `json:"firstMessage"`
ExampleMessages []string `json:"exampleMessages"`
SystemPrompt string `json:"systemPrompt"`
PostHistoryInstructions string `json:"postHistoryInstructions"`
AlternateGreetings []string `json:"alternateGreetings"`
CharacterBook json.RawMessage `json:"characterBook"`
Extensions json.RawMessage `json:"extensions"`
TotalChats int `json:"totalChats"`
TotalLikes int `json:"totalLikes"`
UsageCount int `json:"usageCount"`
FavoriteCount int `json:"favoriteCount"`
TokenCount int `json:"tokenCount"`
IsFavorited bool `json:"isFavorited"` // 当前用户是否收藏
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// CharacterListResponse 角色卡列表响应
@@ -52,28 +58,49 @@ func ToCharacterResponse(character *app.AICharacter, isFavorited bool) Character
exampleMessages = character.ExampleMessages
}
alternateGreetings := []string{}
if character.AlternateGreetings != nil {
alternateGreetings = character.AlternateGreetings
}
// 处理 JSON 字段,确保返回有效 JSONnil 时返回 null
characterBook := json.RawMessage(character.CharacterBook)
if len(characterBook) == 0 {
characterBook = json.RawMessage("null")
}
extensions := json.RawMessage(character.Extensions)
if len(extensions) == 0 {
extensions = json.RawMessage("null")
}
return CharacterResponse{
ID: character.ID,
Name: character.Name,
Description: character.Description,
Personality: character.Personality,
Scenario: character.Scenario,
Avatar: character.Avatar,
CreatorID: character.CreatorID,
CreatorName: character.CreatorName,
CreatorNotes: character.CreatorNotes,
Tags: tags,
IsPublic: character.IsPublic,
Version: character.Version,
FirstMessage: character.FirstMessage,
ExampleMessages: exampleMessages,
TotalChats: character.TotalChats,
TotalLikes: character.TotalLikes,
UsageCount: character.UsageCount,
FavoriteCount: character.FavoriteCount,
TokenCount: character.TokenCount,
IsFavorited: isFavorited,
CreatedAt: character.CreatedAt,
UpdatedAt: character.UpdatedAt,
ID: character.ID,
Name: character.Name,
Description: character.Description,
Personality: character.Personality,
Scenario: character.Scenario,
Avatar: character.Avatar,
CreatorID: character.CreatorID,
CreatorName: character.CreatorName,
CreatorNotes: character.CreatorNotes,
Tags: tags,
IsPublic: character.IsPublic,
Version: character.Version,
FirstMessage: character.FirstMessage,
ExampleMessages: exampleMessages,
SystemPrompt: character.SystemPrompt,
PostHistoryInstructions: character.PostHistoryInstructions,
AlternateGreetings: alternateGreetings,
CharacterBook: characterBook,
Extensions: extensions,
TotalChats: character.TotalChats,
TotalLikes: character.TotalLikes,
UsageCount: character.UsageCount,
FavoriteCount: character.FavoriteCount,
TokenCount: character.TokenCount,
IsFavorited: isFavorited,
CreatedAt: character.CreatedAt,
UpdatedAt: character.UpdatedAt,
}
}