🎨 优化扩展模块

This commit is contained in:
2026-02-14 06:20:05 +08:00
parent 572f3aa15b
commit 0f9c9c9b9c
16 changed files with 2334 additions and 3123 deletions

View File

@@ -3,148 +3,65 @@ package app
import (
"git.echol.cn/loser/st/server/global"
"gorm.io/datatypes"
"time"
)
// AIExtension 扩展表 (兼容 SillyTavern Extension 规范)
// AIExtension 扩展Extension)表
type AIExtension struct {
global.GVA_MODEL
UserID uint `json:"userId" gorm:"not null;index;comment:所属用户ID"`
User *AppUser `json:"user" gorm:"foreignKey:UserID"`
Name string `json:"name" gorm:"type:varchar(500);not null;index;comment:扩展名称"`
DisplayName string `json:"displayName" gorm:"type:varchar(500);comment:显示名称"`
Version string `json:"version" gorm:"type:varchar(50);comment:版本号"`
Author string `json:"author" gorm:"type:varchar(200);comment:作者"`
Description string `json:"description" gorm:"type:text;comment:扩展描述"`
Homepage string `json:"homepage" gorm:"type:varchar(1024);comment:主页地址"`
Repository string `json:"repository" gorm:"type:varchar(1024);comment:仓库地址"`
License string `json:"license" gorm:"type:varchar(100);comment:许可证"`
Tags datatypes.JSON `json:"tags" gorm:"type:jsonb;comment:标签列表"`
UserID uint `json:"userId" gorm:"not null;index;comment:所属用户ID"`
User *AppUser `json:"user" gorm:"foreignKey:UserID"`
// 扩展类型和功能
ExtensionType string `json:"extensionType" gorm:"type:varchar(50);default:'ui';comment:扩展类型(ui/server/hybrid)"` // ui, server, hybrid
Category string `json:"category" gorm:"type:varchar(100);comment:分类(utilities/themes/integrations/tools)"`
// 基础信息
Name string `json:"name" gorm:"type:varchar(200);not null;index;comment:扩展名称(唯一标识)"`
DisplayName string `json:"displayName" gorm:"type:varchar(200);comment:扩展显示名称"`
Version string `json:"version" gorm:"type:varchar(50);default:'1.0.0';comment:版本号"`
Author string `json:"author" gorm:"type:varchar(200);comment:作者"`
Description string `json:"description" gorm:"type:text;comment:扩展描述"`
Homepage string `json:"homepage" gorm:"type:varchar(500);comment:主页链接"`
Repository string `json:"repository" gorm:"type:varchar(500);comment:仓库地址"`
License string `json:"license" gorm:"type:varchar(100);comment:许可证"`
// 依赖关系
Dependencies datatypes.JSON `json:"dependencies" gorm:"type:jsonb;comment:依赖的其他扩展"`
Conflicts datatypes.JSON `json:"conflicts" gorm:"type:jsonb;comment:冲突的扩展列表"`
// 分类与标签
ExtensionType string `json:"extensionType" gorm:"type:varchar(20);default:'ui';comment:扩展类型:ui,server,hybrid"`
Category string `json:"category" gorm:"type:varchar(50);comment:分类:utilities,themes,integrations,tools"`
Tags datatypes.JSON `json:"tags" gorm:"type:jsonb;comment:标签列表"`
// 扩展文件
ManifestData datatypes.JSON `json:"manifestData" gorm:"type:jsonb;not null;comment:manifest.json 完整内容"`
ScriptPath string `json:"scriptPath" gorm:"type:varchar(1024);comment:主脚本文件路径"`
StylePath string `json:"stylePath" gorm:"type:varchar(1024);comment:样式文件路径"`
AssetsPaths datatypes.JSON `json:"assetsPaths" gorm:"type:jsonb;comment:资源文件路径列表"`
// 依赖管理
Dependencies datatypes.JSON `json:"dependencies" gorm:"type:jsonb;comment:依赖扩展"`
Conflicts datatypes.JSON `json:"conflicts" gorm:"type:jsonb;comment:冲突扩展"`
// 扩展配置
Settings datatypes.JSON `json:"settings" gorm:"type:jsonb;comment:扩展配置项"`
Options datatypes.JSON `json:"options" gorm:"type:jsonb;comment:扩展选项"`
// 文件路径
ScriptPath string `json:"scriptPath" gorm:"type:varchar(500);comment:脚本文件路径"`
StylePath string `json:"stylePath" gorm:"type:varchar(500);comment:样式文件路径"`
AssetPaths datatypes.JSON `json:"assetPaths" gorm:"type:jsonb;comment:资源文件路径列表"`
// 状态
IsEnabled bool `json:"isEnabled" gorm:"default:false;index;comment:是否启用"`
IsInstalled bool `json:"isInstalled" gorm:"default:true;index;comment:是否已安装"`
IsSystemExt bool `json:"isSystemExt" gorm:"default:false;comment:是否系统内置扩展"`
InstallSource string `json:"installSource" gorm:"type:varchar(500);comment:安装来源(url/git/file/marketplace)"`
SourceURL string `json:"sourceUrl" gorm:"type:varchar(1000);comment:原始安装 URL用于更新"`
Branch string `json:"branch" gorm:"type:varchar(100);comment:Git 分支名称"`
InstallDate time.Time `json:"installDate" gorm:"comment:安装日期"`
LastEnabled time.Time `json:"lastEnabled" gorm:"comment:最后启用时间"`
// 配置与元数据
ManifestData datatypes.JSON `json:"manifestData" gorm:"type:jsonb;comment:manifest 元数据"`
Settings datatypes.JSON `json:"settings" gorm:"type:jsonb;comment:扩展配置"`
Options datatypes.JSON `json:"options" gorm:"type:jsonb;comment:扩展选项"`
Metadata datatypes.JSON `json:"metadata" gorm:"type:jsonb;comment:额外元数据"`
// 更新相关
AutoUpdate bool `json:"autoUpdate" gorm:"default:false;comment:是否自动更新"`
LastUpdateCheck *time.Time `json:"lastUpdateCheck" gorm:"comment:最后检查更新时间"`
AvailableVersion string `json:"availableVersion" gorm:"type:varchar(50);comment:可用的新版本"`
// 状态管理
IsEnabled bool `json:"isEnabled" gorm:"default:false;comment:是否启用"`
IsInstalled bool `json:"isInstalled" gorm:"default:true;comment:是否已安装"`
IsSystemExt bool `json:"isSystemExt" gorm:"default:false;comment:是否系统扩展"`
// 统计
// 安装信息
InstallSource string `json:"installSource" gorm:"type:varchar(50);comment:安装来源:url,git,file,marketplace"`
SourceURL string `json:"sourceUrl" gorm:"type:varchar(500);comment:源地址"`
Branch string `json:"branch" gorm:"type:varchar(100);default:'main';comment:Git 分支"`
AutoUpdate bool `json:"autoUpdate" gorm:"default:false;comment:是否自动更新"`
LastUpdateCheck *int64 `json:"lastUpdateCheck" gorm:"comment:最后检查更新时间戳"`
AvailableVersion string `json:"availableVersion" gorm:"type:varchar(50);comment:可用的新版本"`
InstallDate *int64 `json:"installDate" gorm:"comment:安装日期时间戳"`
LastEnabled *int64 `json:"lastEnabled" gorm:"comment:最后启用时间戳"`
// 统计信息
UsageCount int `json:"usageCount" gorm:"default:0;comment:使用次数"`
ErrorCount int `json:"errorCount" gorm:"default:0;comment:错误次数"`
LoadTime int `json:"loadTime" gorm:"default:0;comment:平均加载时间(ms)"`
// 元数据
Metadata datatypes.JSON `json:"metadata" gorm:"type:jsonb;comment:扩展元数据"`
LoadTime int `json:"loadTime" gorm:"default:0;comment:加载时间(ms)"`
}
func (AIExtension) TableName() string {
return "ai_extensions"
}
// AIExtensionManifest 扩展清单结构 (对应 manifest.json兼容 SillyTavern 格式)
type AIExtensionManifest struct {
Name string `json:"name"`
DisplayName string `json:"display_name,omitempty"`
Version string `json:"version"`
Description string `json:"description"`
Author string `json:"author"`
Homepage string `json:"homepage,omitempty"`
HomePage string `json:"homePage,omitempty"` // SillyTavern 兼容(驼峰命名)
Repository string `json:"repository,omitempty"`
License string `json:"license,omitempty"`
Tags []string `json:"tags,omitempty"`
Type string `json:"type,omitempty"` // ui, server, hybrid
Category string `json:"category,omitempty"` // utilities, themes, integrations, tools
Dependencies map[string]string `json:"dependencies,omitempty"` // {"extension-name": ">=1.0.0"}
Conflicts []string `json:"conflicts,omitempty"`
Entry string `json:"entry,omitempty"` // 主入口文件
Js string `json:"js,omitempty"` // SillyTavern 兼容: JS 入口文件
Style string `json:"style,omitempty"` // 样式文件
Css string `json:"css,omitempty"` // SillyTavern 兼容: CSS 样式文件
Assets []string `json:"assets,omitempty"` // 资源文件列表
Settings map[string]interface{} `json:"settings,omitempty"` // 默认设置
Options map[string]interface{} `json:"options,omitempty"` // 扩展选项
Metadata map[string]interface{} `json:"metadata,omitempty"` // 扩展元数据
AutoUpdate bool `json:"auto_update,omitempty"` // 是否自动更新SillyTavern 兼容)
InlineScript string `json:"inline_script,omitempty"` // 内联脚本SillyTavern 兼容)
Requires []string `json:"requires,omitempty"` // SillyTavern 兼容: 依赖列表
Optional []string `json:"optional,omitempty"` // SillyTavern 兼容: 可选依赖
LoadingOrder int `json:"loading_order,omitempty"` // SillyTavern 兼容: 加载顺序
I18n map[string]string `json:"i18n,omitempty"` // SillyTavern 兼容: 国际化文件
}
// GetEffectiveName 获取有效名称,兼容 SillyTavern manifest 没有 name 字段的情况
func (m *AIExtensionManifest) GetEffectiveName() string {
if m.Name != "" {
return m.Name
}
if m.DisplayName != "" {
return m.DisplayName
}
return ""
}
// GetEffectiveHomepage 获取有效主页地址
func (m *AIExtensionManifest) GetEffectiveHomepage() string {
if m.Homepage != "" {
return m.Homepage
}
return m.HomePage
}
// GetEffectiveEntry 获取有效的 JS 入口文件路径
func (m *AIExtensionManifest) GetEffectiveEntry() string {
if m.Entry != "" {
return m.Entry
}
return m.Js
}
// GetEffectiveStyle 获取有效的 CSS 样式文件路径
func (m *AIExtensionManifest) GetEffectiveStyle() string {
if m.Style != "" {
return m.Style
}
return m.Css
}
// AIExtensionSettings 用户的扩展配置(已废弃,配置现在直接存储在 AIExtension.Settings 中)
// type AIExtensionSettings struct {
// global.GVA_MODEL
// UserID uint `json:"userId" gorm:"not null;index:idx_user_ext,unique;comment:用户ID"`
// User *AppUser `json:"user" gorm:"foreignKey:UserID"`
// ExtensionID uint `json:"extensionId" gorm:"not null;index:idx_user_ext,unique;comment:扩展ID"`
// Extension *AIExtension `json:"extension" gorm:"foreignKey:ExtensionID"`
// Settings datatypes.JSON `json:"settings" gorm:"type:jsonb;not null;comment:用户配置"`
// IsEnabled bool `json:"isEnabled" gorm:"default:true;comment:用户是否启用"`
// }
//
// func (AIExtensionSettings) TableName() string {
// return "ai_extension_settings"
// }