refactor: 后端仓库层收口(A1:课程/产品/素材)+ 收进工作区既有对象化重构
本提交含两部分。第一部分是本轮工作;第二部分是此前一直留在工作区、
从未提交的对象化重构,与第一部分在文件上互相咬合(internal/repository
整个包都是未跟踪状态,且 api 层已有文件引用它),无法拆成两个可编译的提交。
一、仓库层收口 A1 批(本轮工作)
把 api 层手写的 store.DB 查询收进具名仓库方法,只给真正获益的对象做方法,
不机械包裹全量。本批迁移 22 处裸查询(courses.go 9 / media.go 12 / products.go 1),
新增方法:
- MediaFileRepo.ListByBind / ListForAudit / MarkExtracted
- KnowledgeChunkRepo.CountByMediaFile
- ProductRepo.GetVisibleByID
两条业务口径改由仓库单点持有,避免各处手写漂移:
「只有 approved 素材出现在课程详情」与「已停用产品不在课程详情露出」。
修掉两个真实缺陷:
- ProductRepo.GetByID 缺 Where 条件。此前 GET /api/products/{id} 对任意 id 都返回
第一条产品、对不存在的 id 返回 200,且 PUT /api/products/{id} 会覆盖第一条产品
—— 数据损坏级。全仓扫描确认这是唯一一处同型写法。
- ProductRepo.Delete 写 status="deleted",而 DELETE 处理器文档与回包都声称
"inactive",接口在说谎;管理员用 status=all 拉列表会看到前端不认识的状态。
已对齐为 inactive(与 CourseRepo.Delete 一致)。
删除 8 个零调用且列名不存在的死方法(一调即 SQL 报错):
- media_file 上的 file_path / file_type / approval_status 三列并不存在,
GetByPath / ListByType / UpdateStatus 全废
- knowledge_chunk 上的 space_id 列不存在(模型早已改为 knowledge_space_key),
List / Total / ListBySpaceIDs / DeleteBySpace / SearchByVector 全废
取舍边界:能对当前 schema 跑通的死方法保留,跑不通的删或修。
CourseRepo.List 补齐 status=all 档(此前传给它会当作 status='all' 过滤出空列表)。
该方法此前零调用,现与产品列表语义对齐。
验证:go build ./... 与 go test ./... 全绿;另用真实 HTTP 请求验证 34 项
(课程 17 / 产品 3 / 素材 14),跑在数据库副本与独立 KB_DATA_DIR 上,
含 multipart 真上传 → 审批 → pdftotext 提取 → 分片入库的完整链路。
二、此前未提交的对象化重构(非本轮工作)
- 新增 internal/repository 仓库层、connectors、skills、specialists、xapps、jsonutil,
model/task_record|task_run|task_artifact、api/task_runtime|action_definition|chat_message
- 删除 api/app_definition、connectors、my_app_center、notification、office_skill、
export_docx|pptx|xlsx、official_account_* 等,随 XApp/Skill/Specialist/Connector
可插拔打包方向(AR10/AR11)调整
- 资产目录归位:backend-go/knowledge_source → assets/knowledge/source、
training_materials → assets/training/materials;README 内相对路径同步加深两级;
deploy env 补 ASSET_ROOT_DIR 并改 KNOWLEDGE_SOURCE_DIR / TRAINING_MATERIALS_DIR
- 前端新增 skills/ specialists/ connectors/ xapps/ 目录与对应页面
验证:前端 npm run build 通过(7.26s)。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,530 @@
|
||||
package xappapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"eai_agentplatform/backend/internal/jsonutil"
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
xappdefs "eai_agentplatform/backend/internal/xapps/model"
|
||||
)
|
||||
|
||||
type definitionReq struct {
|
||||
Key string `json:"key"`
|
||||
Label string `json:"label"`
|
||||
Badge string `json:"badge"`
|
||||
Kind string `json:"kind"`
|
||||
MarketTag string `json:"market_tag"`
|
||||
XAppMode string `json:"xapp_mode"`
|
||||
Tier string `json:"tier"`
|
||||
Source string `json:"source"`
|
||||
Color string `json:"color"`
|
||||
IconText string `json:"icon_text"`
|
||||
CoverTone string `json:"cover_tone"`
|
||||
Summary string `json:"summary"`
|
||||
Description string `json:"description"`
|
||||
OpenRoute string `json:"open_route"`
|
||||
SpecialistKey string `json:"specialist_key"`
|
||||
SkillKey string `json:"skill_key"`
|
||||
DefaultPrompt string `json:"default_prompt"`
|
||||
PromptsJSON string `json:"prompts_json"`
|
||||
TagsJSON string `json:"tags_json"`
|
||||
InstallState string `json:"install_state"`
|
||||
ExposedToUser bool `json:"exposed_to_user"`
|
||||
State string `json:"state"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
func normalizeDefinitionReq(req *definitionReq) {
|
||||
req.Key = strings.TrimSpace(req.Key)
|
||||
req.Label = strings.TrimSpace(req.Label)
|
||||
req.Badge = strings.TrimSpace(req.Badge)
|
||||
req.Kind = strings.TrimSpace(req.Kind)
|
||||
req.MarketTag = strings.TrimSpace(req.MarketTag)
|
||||
req.XAppMode = strings.TrimSpace(req.XAppMode)
|
||||
req.Tier = strings.TrimSpace(req.Tier)
|
||||
req.Source = strings.TrimSpace(req.Source)
|
||||
req.Color = strings.TrimSpace(req.Color)
|
||||
req.IconText = strings.TrimSpace(req.IconText)
|
||||
req.CoverTone = strings.TrimSpace(req.CoverTone)
|
||||
req.Summary = strings.TrimSpace(req.Summary)
|
||||
req.Description = strings.TrimSpace(req.Description)
|
||||
req.OpenRoute = strings.TrimSpace(req.OpenRoute)
|
||||
req.SpecialistKey = strings.TrimSpace(req.SpecialistKey)
|
||||
req.SkillKey = strings.TrimSpace(req.SkillKey)
|
||||
req.DefaultPrompt = strings.TrimSpace(req.DefaultPrompt)
|
||||
req.PromptsJSON = strings.TrimSpace(req.PromptsJSON)
|
||||
req.TagsJSON = strings.TrimSpace(req.TagsJSON)
|
||||
req.InstallState = strings.TrimSpace(req.InstallState)
|
||||
req.State = strings.TrimSpace(req.State)
|
||||
}
|
||||
|
||||
func normalizeXAppMode(req *definitionReq) {
|
||||
raw := strings.ToLower(req.XAppMode)
|
||||
switch raw {
|
||||
case "", "worker", "dw", "direct":
|
||||
req.XAppMode = "direct"
|
||||
case "adw", "orchestrated", "workflow", "flow":
|
||||
req.XAppMode = "orchestrated"
|
||||
default:
|
||||
req.XAppMode = raw
|
||||
}
|
||||
}
|
||||
|
||||
func validateDefinitionReq(req *definitionReq) *web.AppError {
|
||||
normalizeDefinitionReq(req)
|
||||
normalizeXAppMode(req)
|
||||
if req.Key == "" || req.Label == "" {
|
||||
return web.NewBadRequest("key、label 为必填")
|
||||
}
|
||||
if req.XAppMode != "direct" && req.XAppMode != "orchestrated" {
|
||||
return web.NewBadRequest("xapp_mode 只能是 direct 或 orchestrated")
|
||||
}
|
||||
if req.Tier == "" {
|
||||
req.Tier = "business"
|
||||
}
|
||||
if req.Source == "" {
|
||||
req.Source = "eai"
|
||||
}
|
||||
if req.InstallState == "" {
|
||||
req.InstallState = "installed"
|
||||
}
|
||||
if req.State == "" {
|
||||
req.State = "active"
|
||||
}
|
||||
if req.State != "active" && req.State != "inactive" {
|
||||
return web.NewBadRequest("state 只能是 active 或 inactive")
|
||||
}
|
||||
if !jsonutil.ValidateStringArrayJSON(req.PromptsJSON) {
|
||||
return web.NewBadRequest("prompts_json 必须是字符串数组")
|
||||
}
|
||||
if !jsonutil.ValidateStringArrayJSON(req.TagsJSON) {
|
||||
return web.NewBadRequest("tags_json 必须是字符串数组")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ListXAppDefinitions(c *gin.Context) {
|
||||
q := store.DB.Model(&xappdefs.XAppDefinition{})
|
||||
if c.Query("state") == "" {
|
||||
q = q.Where("state = ?", "active")
|
||||
} else {
|
||||
q = q.Where("state = ?", c.Query("state"))
|
||||
}
|
||||
if c.Query("exposed_to_user") != "" {
|
||||
q = q.Where("exposed_to_user = ?", c.Query("exposed_to_user") == "true")
|
||||
}
|
||||
var items []xappdefs.XAppDefinition
|
||||
if err := q.Order("sort_order ASC, id ASC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询应用定义失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
}
|
||||
|
||||
func GetXAppDefinitionByKey(c *gin.Context) {
|
||||
key := strings.TrimSpace(c.Param("key"))
|
||||
if key == "" {
|
||||
web.Fail(c, web.NewBadRequest("应用 key 不能为空"))
|
||||
return
|
||||
}
|
||||
var item xappdefs.XAppDefinition
|
||||
if err := store.DB.Where("key = ?", key).First(&item).Error; err != nil {
|
||||
web.Fail(c, web.NewNotFoundError("应用定义不存在"))
|
||||
return
|
||||
}
|
||||
web.OK(c, item)
|
||||
}
|
||||
|
||||
func CreateXAppDefinition(c *gin.Context) {
|
||||
var req definitionReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
||||
return
|
||||
}
|
||||
if appErr := validateDefinitionReq(&req); appErr != nil {
|
||||
web.Fail(c, appErr)
|
||||
return
|
||||
}
|
||||
item := xappdefs.XAppDefinition{
|
||||
Key: req.Key,
|
||||
Label: req.Label,
|
||||
Badge: req.Badge,
|
||||
Kind: req.Kind,
|
||||
MarketTag: req.MarketTag,
|
||||
XAppMode: req.XAppMode,
|
||||
Tier: req.Tier,
|
||||
Source: req.Source,
|
||||
Color: req.Color,
|
||||
IconText: req.IconText,
|
||||
CoverTone: req.CoverTone,
|
||||
Summary: req.Summary,
|
||||
Description: req.Description,
|
||||
OpenRoute: req.OpenRoute,
|
||||
SpecialistKey: req.SpecialistKey,
|
||||
SkillKey: req.SkillKey,
|
||||
DefaultPrompt: req.DefaultPrompt,
|
||||
PromptsJSON: req.PromptsJSON,
|
||||
TagsJSON: req.TagsJSON,
|
||||
InstallState: req.InstallState,
|
||||
ExposedToUser: req.ExposedToUser,
|
||||
State: req.State,
|
||||
SortOrder: req.SortOrder,
|
||||
}
|
||||
if err := store.DB.Create(&item).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("创建应用定义失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, item)
|
||||
}
|
||||
|
||||
func UpdateXAppDefinition(c *gin.Context) {
|
||||
id, ok := parseID(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var item xappdefs.XAppDefinition
|
||||
if err := store.DB.First(&item, id).Error; err != nil {
|
||||
web.Fail(c, web.NewNotFoundError("应用定义不存在"))
|
||||
return
|
||||
}
|
||||
var req definitionReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
||||
return
|
||||
}
|
||||
if appErr := validateDefinitionReq(&req); appErr != nil {
|
||||
web.Fail(c, appErr)
|
||||
return
|
||||
}
|
||||
item.Key = req.Key
|
||||
item.Label = req.Label
|
||||
item.Badge = req.Badge
|
||||
item.Kind = req.Kind
|
||||
item.MarketTag = req.MarketTag
|
||||
item.XAppMode = req.XAppMode
|
||||
item.Tier = req.Tier
|
||||
item.Source = req.Source
|
||||
item.Color = req.Color
|
||||
item.IconText = req.IconText
|
||||
item.CoverTone = req.CoverTone
|
||||
item.Summary = req.Summary
|
||||
item.Description = req.Description
|
||||
item.OpenRoute = req.OpenRoute
|
||||
item.SpecialistKey = req.SpecialistKey
|
||||
item.SkillKey = req.SkillKey
|
||||
item.DefaultPrompt = req.DefaultPrompt
|
||||
item.PromptsJSON = req.PromptsJSON
|
||||
item.TagsJSON = req.TagsJSON
|
||||
item.InstallState = req.InstallState
|
||||
item.ExposedToUser = req.ExposedToUser
|
||||
item.State = req.State
|
||||
item.SortOrder = req.SortOrder
|
||||
if err := store.DB.Save(&item).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("更新应用定义失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, item)
|
||||
}
|
||||
|
||||
func DeleteXAppDefinition(c *gin.Context) {
|
||||
id, ok := parseID(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var item xappdefs.XAppDefinition
|
||||
if err := store.DB.First(&item, id).Error; err != nil {
|
||||
web.Fail(c, web.NewNotFoundError("应用定义不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&item).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("删除应用定义失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, gin.H{"id": id, "deleted": true})
|
||||
}
|
||||
|
||||
const (
|
||||
maxFavoriteXAppCount = 24
|
||||
maxRecentXAppCount = 8
|
||||
maxCustomXAppCount = 32
|
||||
maxCustomTagCount = 6
|
||||
)
|
||||
|
||||
type centerCustomXApp struct {
|
||||
Key string `json:"key"`
|
||||
Label string `json:"label"`
|
||||
Badge string `json:"badge"`
|
||||
Kind string `json:"kind"`
|
||||
Color string `json:"color"`
|
||||
Summary string `json:"summary"`
|
||||
Description string `json:"description"`
|
||||
Tags []string `json:"tags"`
|
||||
Prompts []string `json:"prompts"`
|
||||
InstallState string `json:"install_state"`
|
||||
SkillKey string `json:"skill_key"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
IconText string `json:"icon_text"`
|
||||
CoverTone string `json:"cover_tone"`
|
||||
IsCustomXApp bool `json:"is_custom_xapp"`
|
||||
}
|
||||
|
||||
type centerPayload struct {
|
||||
FavoriteKeys []string `json:"favorite_keys"`
|
||||
RecentKeys []string `json:"recent_keys"`
|
||||
CustomXApps []centerCustomXApp `json:"custom_xapps"`
|
||||
}
|
||||
|
||||
func GetMyXAppCenter(c *gin.Context) {
|
||||
user := middleware.CurrentUser(c)
|
||||
if user == nil {
|
||||
web.Fail(c, web.NewAuthError("未登录"))
|
||||
return
|
||||
}
|
||||
row, err := findUserXAppCenter(user.ID)
|
||||
if err != nil {
|
||||
web.Fail(c, web.NewBadRequest("读取应用中心失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, normalizeXAppCenterPayload(row))
|
||||
}
|
||||
|
||||
func UpdateMyXAppCenter(c *gin.Context) {
|
||||
user := middleware.CurrentUser(c)
|
||||
if user == nil {
|
||||
web.Fail(c, web.NewAuthError("未登录"))
|
||||
return
|
||||
}
|
||||
var req centerPayload
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
||||
return
|
||||
}
|
||||
payload := normalizeXAppCenterPayload(&xappdefs.UserXAppCenter{
|
||||
FavoriteKeys: mustJSONXAppCenter(req.FavoriteKeys),
|
||||
RecentKeys: mustJSONXAppCenter(req.RecentKeys),
|
||||
CustomXApps: mustJSONXAppCenter(req.CustomXApps),
|
||||
})
|
||||
row, err := findUserXAppCenter(user.ID)
|
||||
if err != nil {
|
||||
web.Fail(c, web.NewBadRequest("读取应用中心失败"))
|
||||
return
|
||||
}
|
||||
if row == nil {
|
||||
row = &xappdefs.UserXAppCenter{UserID: user.ID}
|
||||
}
|
||||
row.FavoriteKeys = mustJSONXAppCenter(payload.FavoriteKeys)
|
||||
row.RecentKeys = mustJSONXAppCenter(payload.RecentKeys)
|
||||
row.CustomXApps = mustJSONXAppCenter(payload.CustomXApps)
|
||||
if err := store.DB.Save(row).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("保存应用中心失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, payload)
|
||||
}
|
||||
|
||||
func findUserXAppCenter(userID uint) (*xappdefs.UserXAppCenter, error) {
|
||||
var row xappdefs.UserXAppCenter
|
||||
err := store.DB.Where("user_id = ?", userID).First(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
func normalizeXAppCenterPayload(row *xappdefs.UserXAppCenter) centerPayload {
|
||||
if row == nil {
|
||||
return centerPayload{
|
||||
FavoriteKeys: []string{},
|
||||
RecentKeys: []string{},
|
||||
CustomXApps: []centerCustomXApp{},
|
||||
}
|
||||
}
|
||||
var favorites []string
|
||||
var recents []string
|
||||
var customXApps []centerCustomXApp
|
||||
_ = json.Unmarshal([]byte(strings.TrimSpace(row.FavoriteKeys)), &favorites)
|
||||
_ = json.Unmarshal([]byte(strings.TrimSpace(row.RecentKeys)), &recents)
|
||||
_ = json.Unmarshal([]byte(strings.TrimSpace(row.CustomXApps)), &customXApps)
|
||||
return centerPayload{
|
||||
FavoriteKeys: normalizeStringList(favorites, maxFavoriteXAppCount),
|
||||
RecentKeys: normalizeStringList(recents, maxRecentXAppCount),
|
||||
CustomXApps: normalizeCustomXApps(customXApps),
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeStringList(items []string, maxCount int) []string {
|
||||
result := make([]string, 0, len(items))
|
||||
seen := make(map[string]struct{}, len(items))
|
||||
for _, item := range items {
|
||||
normalized := strings.TrimSpace(item)
|
||||
if normalized == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[normalized]; ok {
|
||||
continue
|
||||
}
|
||||
seen[normalized] = struct{}{}
|
||||
result = append(result, normalized)
|
||||
if len(result) >= maxCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func normalizeCustomXApps(items []centerCustomXApp) []centerCustomXApp {
|
||||
result := make([]centerCustomXApp, 0, len(items))
|
||||
seen := make(map[string]struct{}, len(items))
|
||||
for index, item := range items {
|
||||
key := strings.TrimSpace(item.Key)
|
||||
if key == "" {
|
||||
key = fmt.Sprintf("custom-xapp-%d-%d", time.Now().UnixMilli(), index+1)
|
||||
}
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
label := strings.TrimSpace(item.Label)
|
||||
if label == "" {
|
||||
label = "未命名应用"
|
||||
}
|
||||
summary := strings.TrimSpace(item.Summary)
|
||||
if summary == "" {
|
||||
summary = "自定义应用"
|
||||
}
|
||||
description := strings.TrimSpace(item.Description)
|
||||
if description == "" {
|
||||
description = summary
|
||||
}
|
||||
prompt := ""
|
||||
if len(item.Prompts) > 0 {
|
||||
prompt = strings.TrimSpace(item.Prompts[0])
|
||||
}
|
||||
if prompt == "" {
|
||||
prompt = summary
|
||||
}
|
||||
createdAt := strings.TrimSpace(item.CreatedAt)
|
||||
if createdAt == "" {
|
||||
createdAt = time.Now().Format(time.RFC3339)
|
||||
}
|
||||
result = append(result, centerCustomXApp{
|
||||
Key: key,
|
||||
Label: truncateText(label, 24),
|
||||
Badge: "自定义应用",
|
||||
Kind: firstNonEmpty(strings.TrimSpace(item.Kind), "个人工作流"),
|
||||
Color: firstNonEmpty(strings.TrimSpace(item.Color), "#2563eb"),
|
||||
Summary: truncateText(summary, 80),
|
||||
Description: truncateText(description, 180),
|
||||
Tags: normalizeTagList(item.Tags),
|
||||
Prompts: []string{truncateText(prompt, 240)},
|
||||
InstallState: "installed",
|
||||
SkillKey: firstNonEmpty(strings.TrimSpace(item.SkillKey), "smart-assistant"),
|
||||
CreatedAt: createdAt,
|
||||
IconText: normalizeIconText(item.IconText, label),
|
||||
CoverTone: normalizeCoverTone(item.CoverTone, item.Color),
|
||||
IsCustomXApp: true,
|
||||
})
|
||||
if len(result) >= maxCustomXAppCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func normalizeTagList(items []string) []string {
|
||||
result := make([]string, 0, len(items))
|
||||
seen := make(map[string]struct{}, len(items))
|
||||
for _, item := range items {
|
||||
tag := truncateText(strings.TrimSpace(item), 12)
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[tag]; ok {
|
||||
continue
|
||||
}
|
||||
seen[tag] = struct{}{}
|
||||
result = append(result, tag)
|
||||
if len(result) >= maxCustomTagCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func truncateText(value string, limit int) string {
|
||||
text := strings.TrimSpace(value)
|
||||
runes := []rune(text)
|
||||
if len(runes) <= limit {
|
||||
return text
|
||||
}
|
||||
return string(runes[:limit])
|
||||
}
|
||||
|
||||
func normalizeIconText(iconText, label string) string {
|
||||
text := strings.TrimSpace(iconText)
|
||||
if text == "" {
|
||||
runes := []rune(strings.TrimSpace(label))
|
||||
if len(runes) == 0 {
|
||||
return "应用"
|
||||
}
|
||||
if len(runes) == 1 {
|
||||
return string(runes[:1])
|
||||
}
|
||||
return string(runes[:2])
|
||||
}
|
||||
runes := []rune(text)
|
||||
if len(runes) <= 2 {
|
||||
return string(runes)
|
||||
}
|
||||
return string(runes[:2])
|
||||
}
|
||||
|
||||
func normalizeCoverTone(coverTone, color string) string {
|
||||
text := strings.TrimSpace(coverTone)
|
||||
if text != "" {
|
||||
return text
|
||||
}
|
||||
if strings.TrimSpace(color) != "" {
|
||||
return color
|
||||
}
|
||||
return "#2563eb"
|
||||
}
|
||||
|
||||
func mustJSONXAppCenter(value any) string {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return "[]"
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func parseID(c *gin.Context, name string) (uint, bool) {
|
||||
id, err := strconv.ParseUint(c.Param(name), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
web.Fail(c, web.NewBadRequest("无效的 "+name))
|
||||
return 0, false
|
||||
}
|
||||
return uint(id), true
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package xappcore
|
||||
|
||||
type SharedReferencePolicy string
|
||||
|
||||
const (
|
||||
SharedReferenceReadonlyHistory SharedReferencePolicy = "readonly_history"
|
||||
SharedReferenceTombstone SharedReferencePolicy = "convert_to_tombstone"
|
||||
SharedReferenceDetach SharedReferencePolicy = "detach_reference"
|
||||
SharedReferenceBlockUninstall SharedReferencePolicy = "block_uninstall"
|
||||
)
|
||||
|
||||
type UninstallMeta struct {
|
||||
OwnedDefinitionKeys []string
|
||||
OwnedCatalogEntries []string
|
||||
ReferencePolicy SharedReferencePolicy
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package xappcore
|
||||
|
||||
type Manifest struct {
|
||||
Key string
|
||||
Label string
|
||||
OwnedDefinitionKeys []string
|
||||
OwnedCatalogEntries []string
|
||||
ReferencePolicy SharedReferencePolicy
|
||||
}
|
||||
|
||||
func (m Manifest) UninstallMeta() UninstallMeta {
|
||||
return UninstallMeta{
|
||||
OwnedDefinitionKeys: append([]string(nil), m.OwnedDefinitionKeys...),
|
||||
OwnedCatalogEntries: append([]string(nil), m.OwnedCatalogEntries...),
|
||||
ReferencePolicy: m.ReferencePolicy,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xappcore
|
||||
|
||||
type Registry struct {
|
||||
manifests []Manifest
|
||||
byKey map[string]Manifest
|
||||
}
|
||||
|
||||
func NewRegistry(manifests ...Manifest) *Registry {
|
||||
items := make([]Manifest, 0, len(manifests))
|
||||
byKey := make(map[string]Manifest, len(manifests))
|
||||
for _, manifest := range manifests {
|
||||
if manifest.Key == "" {
|
||||
continue
|
||||
}
|
||||
items = append(items, manifest)
|
||||
byKey[manifest.Key] = manifest
|
||||
}
|
||||
return &Registry{manifests: items, byKey: byKey}
|
||||
}
|
||||
|
||||
func (r *Registry) Manifests() []Manifest {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
items := make([]Manifest, len(r.manifests))
|
||||
copy(items, r.manifests)
|
||||
return items
|
||||
}
|
||||
|
||||
func (r *Registry) ByKey(key string) (Manifest, bool) {
|
||||
if r == nil {
|
||||
return Manifest{}, false
|
||||
}
|
||||
item, ok := r.byKey[key]
|
||||
return item, ok
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package xappcore
|
||||
|
||||
type UninstallPreview struct {
|
||||
DefinitionKeys []string
|
||||
CatalogEntries []string
|
||||
Policy SharedReferencePolicy
|
||||
}
|
||||
|
||||
func BuildUninstallPreview(manifest Manifest) UninstallPreview {
|
||||
meta := manifest.UninstallMeta()
|
||||
return UninstallPreview{
|
||||
DefinitionKeys: meta.OwnedDefinitionKeys,
|
||||
CatalogEntries: meta.OwnedCatalogEntries,
|
||||
Policy: meta.ReferencePolicy,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package xappmodel
|
||||
|
||||
import "time"
|
||||
|
||||
// XAppDefinition 面向用户的一键应用目录定义。
|
||||
// 它是“成品入口”这一层:决定应用怎么展示、默认挂哪个专员/技能、打开后走哪条路。
|
||||
type XAppDefinition struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Key string `gorm:"size:64;uniqueIndex;not null" json:"key"`
|
||||
Label string `gorm:"size:128;not null" json:"label"`
|
||||
DisplayCode string `gorm:"column:display_code;size:16;default:'';index" json:"display_code"`
|
||||
EAILogicCode string `gorm:"column:eailogic_code;size:32;default:'';index" json:"eailogic_code"`
|
||||
Badge string `gorm:"size:32;default:''" json:"badge"`
|
||||
Kind string `gorm:"size:64;default:''" json:"kind"`
|
||||
MarketTag string `gorm:"column:market_tag;size:32;default:'';index" json:"market_tag"`
|
||||
XAppMode string `gorm:"size:16;default:'';index" json:"xapp_mode"`
|
||||
Tier string `gorm:"size:16;default:'';index" json:"tier"`
|
||||
Source string `gorm:"size:32;not null;default:eai;index" json:"source"` // eai / custom / imported
|
||||
Color string `gorm:"size:32;default:''" json:"color"`
|
||||
IconText string `gorm:"column:icon_text;size:16;default:''" json:"icon_text"`
|
||||
CoverTone string `gorm:"column:cover_tone;type:text" json:"cover_tone"`
|
||||
Summary string `gorm:"type:text" json:"summary"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
OpenRoute string `gorm:"column:open_route;size:256;default:''" json:"open_route"`
|
||||
SpecialistKey string `gorm:"column:specialist_key;size:64;default:'';index" json:"specialist_key"`
|
||||
SkillKey string `gorm:"column:skill_key;size:64;default:'';index" json:"skill_key"`
|
||||
DefaultPrompt string `gorm:"column:default_prompt;type:text" json:"default_prompt"`
|
||||
PromptsJSON string `gorm:"column:prompts_json;type:text" json:"prompts_json"`
|
||||
TagsJSON string `gorm:"column:tags_json;type:text" json:"tags_json"`
|
||||
InstallState string `gorm:"column:install_state;size:16;not null;default:installed;index" json:"install_state"`
|
||||
ExposedToUser bool `gorm:"column:exposed_to_user;not null;default:true;index" json:"exposed_to_user"`
|
||||
State string `gorm:"size:16;not null;default:active;index" json:"state"` // active / inactive
|
||||
SortOrder int `gorm:"not null;default:0;index" json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (XAppDefinition) TableName() string { return "xapp_definition" }
|
||||
@@ -0,0 +1,16 @@
|
||||
package xappmodel
|
||||
|
||||
import "time"
|
||||
|
||||
// UserXAppCenter 保存用户自己的应用中心状态,用于同步我的应用、收藏和最近使用。
|
||||
type UserXAppCenter struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"not null;uniqueIndex" json:"user_id"`
|
||||
FavoriteKeys string `gorm:"type:text;not null;default:''" json:"favorite_keys"`
|
||||
RecentKeys string `gorm:"type:text;not null;default:''" json:"recent_keys"`
|
||||
CustomXApps string `gorm:"column:custom_xapps;type:text;not null;default:''" json:"custom_xapps"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (UserXAppCenter) TableName() string { return "user_xapp_center" }
|
||||
Reference in New Issue
Block a user