🎨 优化扩展模块,完成ai接入和对话功能

This commit is contained in:
2026-02-12 23:12:28 +08:00
parent 4e611d3a5e
commit 572f3aa15b
779 changed files with 194400 additions and 3136 deletions

View File

@@ -67,7 +67,7 @@ func (AIExtension) TableName() string {
return "ai_extensions"
}
// AIExtensionManifest 扩展清单结构 (对应 manifest.json)
// AIExtensionManifest 扩展清单结构 (对应 manifest.json,兼容 SillyTavern 格式)
type AIExtensionManifest struct {
Name string `json:"name"`
DisplayName string `json:"display_name,omitempty"`
@@ -75,6 +75,7 @@ type AIExtensionManifest struct {
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"`
@@ -83,13 +84,54 @@ type AIExtensionManifest struct {
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 中)

View File

@@ -6,14 +6,21 @@ import (
)
// AIProvider AI 服务提供商配置
// 每个用户可以配置多个 AI 提供商(如 OpenAI、Claude、Gemini 等)
// UserID 为 NULL 表示系统预设的提供商配置
type AIProvider struct {
global.GVA_MODEL
UserID *uint `json:"userId" gorm:"index;comment:用户IDNULL表示系统配置"`
User *AppUser `json:"user" gorm:"foreignKey:UserID"`
User *AppUser `json:"user,omitempty" gorm:"foreignKey:UserID"`
ProviderName string `json:"providerName" gorm:"type:varchar(100);not null;index;comment:提供商名称"`
APIConfig datatypes.JSON `json:"apiConfig" gorm:"type:jsonb;not null;comment:API配置加密存储"`
ProviderType string `json:"providerType" gorm:"type:varchar(50);not null;default:openai;comment:提供商类型(openai/claude/gemini/custom)"`
BaseURL string `json:"baseUrl" gorm:"type:varchar(500);comment:API基础地址"`
APIKey string `json:"-" gorm:"type:varchar(500);comment:API密钥加密存储"`
APIConfig datatypes.JSON `json:"apiConfig" gorm:"type:jsonb;comment:额外API配置"`
Capabilities datatypes.JSON `json:"capabilities" gorm:"type:jsonb;comment:支持的能力(chat/image_gen等)"`
IsEnabled bool `json:"isEnabled" gorm:"default:true;comment:是否启用"`
IsDefault bool `json:"isDefault" gorm:"default:false;comment:是否为默认提供商"`
SortOrder int `json:"sortOrder" gorm:"default:0;comment:排序权重"`
}
func (AIProvider) TableName() string {
@@ -21,12 +28,14 @@ func (AIProvider) TableName() string {
}
// AIModel AI 模型配置
// 每个提供商下可以有多个模型(如 gpt-4o、gpt-4o-mini 等)
type AIModel struct {
global.GVA_MODEL
ProviderID uint `json:"providerId" gorm:"not null;index;comment:提供商ID"`
Provider *AIProvider `json:"provider" gorm:"foreignKey:ProviderID"`
ModelName string `json:"modelName" gorm:"type:varchar(200);not null;comment:模型名称"`
Provider *AIProvider `json:"provider,omitempty" gorm:"foreignKey:ProviderID"`
ModelName string `json:"modelName" gorm:"type:varchar(200);not null;comment:模型标识API调用用"`
DisplayName string `json:"displayName" gorm:"type:varchar(200);comment:模型显示名称"`
ModelType string `json:"modelType" gorm:"type:varchar(50);not null;default:chat;comment:模型类型(chat/image_gen)"`
Config datatypes.JSON `json:"config" gorm:"type:jsonb;comment:模型参数配置"`
IsEnabled bool `json:"isEnabled" gorm:"default:true;comment:是否启用"`
}

View File

@@ -0,0 +1,45 @@
package request
// CreateChatRequest 创建对话请求
type CreateChatRequest struct {
CharacterID uint `json:"characterId" binding:"required"` // 角色卡ID
Title string `json:"title"` // 对话标题(可选,默认使用角色名)
}
// ChatListRequest 对话列表请求
type ChatListRequest struct {
Page int `form:"page" binding:"min=1"`
PageSize int `form:"pageSize" binding:"min=1,max=50"`
}
// SendMessageRequest 发送消息请求
type SendMessageRequest struct {
ChatID uint `json:"chatId" binding:"required"` // 对话ID
Content string `json:"content" binding:"required"` // 消息内容
ProviderID *uint `json:"providerId"` // 指定提供商(可选,默认使用用户默认)
ModelName string `json:"modelName"` // 指定模型(可选)
}
// RegenerateRequest 重新生成请求
type RegenerateRequest struct {
ChatID uint `json:"chatId" binding:"required"` // 对话ID
MessageID uint `json:"messageId" binding:"required"` // 要重新生成的消息ID
ModelName string `json:"modelName"` // 可选,指定其他模型
}
// EditMessageRequest 编辑消息请求
type EditMessageRequest struct {
MessageID uint `json:"messageId" binding:"required"`
Content string `json:"content" binding:"required"`
}
// DeleteMessageRequest 删除消息请求
type DeleteMessageRequest struct {
MessageID uint `json:"messageId" binding:"required"`
}
// ChatMessagesRequest 获取对话消息请求
type ChatMessagesRequest struct {
Page int `form:"page" binding:"min=1"`
PageSize int `form:"pageSize" binding:"min=1,max=100"`
}

View File

@@ -0,0 +1,80 @@
package request
// CreateProviderRequest 创建AI提供商请求
type CreateProviderRequest struct {
ProviderName string `json:"providerName" binding:"required,min=1,max=100"` // 提供商名称(如"我的OpenAI"
ProviderType string `json:"providerType" binding:"required,oneof=openai claude gemini custom"` // 提供商类型
BaseURL string `json:"baseUrl"` // API基础地址自定义或中转站
APIKey string `json:"apiKey" binding:"required"` // API密钥
APIConfig map[string]interface{} `json:"apiConfig"` // 额外配置
Models []CreateModelRequest `json:"models"` // 同时创建的模型列表
}
// UpdateProviderRequest 更新AI提供商请求
type UpdateProviderRequest struct {
ID uint `json:"id" binding:"required"`
ProviderName string `json:"providerName" binding:"required,min=1,max=100"`
ProviderType string `json:"providerType" binding:"required,oneof=openai claude gemini custom"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"` // 为空表示不修改
APIConfig map[string]interface{} `json:"apiConfig"`
IsEnabled *bool `json:"isEnabled"`
IsDefault *bool `json:"isDefault"`
SortOrder *int `json:"sortOrder"`
}
// CreateModelRequest 创建AI模型请求
type CreateModelRequest struct {
ProviderID uint `json:"providerId"` // 关联提供商ID
ModelName string `json:"modelName" binding:"required,min=1,max=200"` // 模型标识(如 gpt-4o
DisplayName string `json:"displayName"` // 显示名称
ModelType string `json:"modelType" binding:"required,oneof=chat image_gen"` // 模型类型
Config map[string]interface{} `json:"config"` // 模型参数配置
IsEnabled *bool `json:"isEnabled"`
}
// UpdateModelRequest 更新AI模型请求
type UpdateModelRequest struct {
ID uint `json:"id" binding:"required"`
ModelName string `json:"modelName" binding:"required,min=1,max=200"`
DisplayName string `json:"displayName"`
ModelType string `json:"modelType" binding:"required,oneof=chat image_gen"`
Config map[string]interface{} `json:"config"`
IsEnabled *bool `json:"isEnabled"`
}
// ProviderListRequest 提供商列表请求
type ProviderListRequest struct {
Page int `form:"page" binding:"min=1"`
PageSize int `form:"pageSize" binding:"min=1,max=100"`
Keyword string `form:"keyword"`
}
// TestProviderRequest 测试提供商连通性请求
type TestProviderRequest struct {
ProviderType string `json:"providerType" binding:"required,oneof=openai claude gemini custom"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey" binding:"required"`
ModelName string `json:"modelName"` // 可选,用于测试特定模型
}
// SetDefaultProviderRequest 设置默认提供商请求
type SetDefaultProviderRequest struct {
ProviderID uint `json:"providerId" binding:"required"`
}
// FetchRemoteModelsRequest 获取远程可用模型列表请求
type FetchRemoteModelsRequest struct {
ProviderType string `json:"providerType" binding:"required,oneof=openai claude gemini custom"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey" binding:"required"`
}
// SendTestMessageRequest 发送测试消息请求
type SendTestMessageRequest struct {
ProviderType string `json:"providerType" binding:"required,oneof=openai claude gemini custom"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey" binding:"required"`
ModelName string `json:"modelName" binding:"required"` // 要测试的模型
Message string `json:"message"` // 测试消息内容,为空则使用默认
}

View File

@@ -0,0 +1,64 @@
package response
import (
"time"
)
// ChatResponse 对话响应
type ChatResponse struct {
ID uint `json:"id"`
Title string `json:"title"`
CharacterID *uint `json:"characterId"`
CharacterName string `json:"characterName"`
CharacterAvatar string `json:"characterAvatar"`
ChatType string `json:"chatType"`
LastMessageAt *time.Time `json:"lastMessageAt"`
MessageCount int `json:"messageCount"`
IsPinned bool `json:"isPinned"`
LastMessage *MessageBrief `json:"lastMessage,omitempty"` // 最后一条消息摘要
CreatedAt time.Time `json:"createdAt"`
}
// MessageBrief 消息摘要(用于对话列表显示)
type MessageBrief struct {
Content string `json:"content"`
Role string `json:"role"`
}
// ChatListResponse 对话列表响应
type ChatListResponse struct {
List []ChatResponse `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// MessageResponse 消息响应
type MessageResponse struct {
ID uint `json:"id"`
ChatID uint `json:"chatId"`
Content string `json:"content"`
Role string `json:"role"` // user / assistant / system
CharacterID *uint `json:"characterId"`
CharacterName string `json:"characterName,omitempty"`
Model string `json:"model,omitempty"`
PromptTokens int `json:"promptTokens,omitempty"`
CompletionTokens int `json:"completionTokens,omitempty"`
TotalTokens int `json:"totalTokens,omitempty"`
SequenceNumber int `json:"sequenceNumber"`
CreatedAt time.Time `json:"createdAt"`
}
// MessageListResponse 消息列表响应
type MessageListResponse struct {
List []MessageResponse `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// ChatDetailResponse 对话详情响应(包含角色信息 + 消息列表)
type ChatDetailResponse struct {
Chat ChatResponse `json:"chat"`
Messages []MessageResponse `json:"messages"`
}

View File

@@ -8,39 +8,44 @@ import (
// ExtensionResponse 扩展响应
type ExtensionResponse struct {
ID uint `json:"id"`
UserID uint `json:"userId"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Version string `json:"version"`
Author string `json:"author"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Repository string `json:"repository"`
License string `json:"license"`
Tags []string `json:"tags"`
ExtensionType string `json:"extensionType"`
Category string `json:"category"`
Dependencies map[string]string `json:"dependencies"`
Conflicts []string `json:"conflicts"`
ManifestData map[string]interface{} `json:"manifestData"`
ScriptPath string `json:"scriptPath"`
StylePath string `json:"stylePath"`
AssetsPaths []string `json:"assetsPaths"`
Settings map[string]interface{} `json:"settings"`
Options map[string]interface{} `json:"options"`
IsEnabled bool `json:"isEnabled"`
IsInstalled bool `json:"isInstalled"`
IsSystemExt bool `json:"isSystemExt"`
InstallSource string `json:"installSource"`
InstallDate time.Time `json:"installDate"`
LastEnabled time.Time `json:"lastEnabled"`
UsageCount int `json:"usageCount"`
ErrorCount int `json:"errorCount"`
LoadTime int `json:"loadTime"`
Metadata map[string]interface{} `json:"metadata"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID uint `json:"id"`
UserID uint `json:"userId"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Version string `json:"version"`
Author string `json:"author"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Repository string `json:"repository"`
License string `json:"license"`
Tags []string `json:"tags"`
ExtensionType string `json:"extensionType"`
Category string `json:"category"`
Dependencies map[string]string `json:"dependencies"`
Conflicts []string `json:"conflicts"`
ManifestData map[string]interface{} `json:"manifestData"`
ScriptPath string `json:"scriptPath"`
StylePath string `json:"stylePath"`
AssetsPaths []string `json:"assetsPaths"`
Settings map[string]interface{} `json:"settings"`
Options map[string]interface{} `json:"options"`
IsEnabled bool `json:"isEnabled"`
IsInstalled bool `json:"isInstalled"`
IsSystemExt bool `json:"isSystemExt"`
InstallSource string `json:"installSource"`
SourceURL string `json:"sourceUrl"`
Branch string `json:"branch"`
AutoUpdate bool `json:"autoUpdate"`
InstallDate time.Time `json:"installDate"`
LastEnabled time.Time `json:"lastEnabled"`
LastUpdateCheck *time.Time `json:"lastUpdateCheck"`
AvailableVersion string `json:"availableVersion"`
UsageCount int `json:"usageCount"`
ErrorCount int `json:"errorCount"`
LoadTime int `json:"loadTime"`
Metadata map[string]interface{} `json:"metadata"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// ExtensionListResponse 扩展列表响应
@@ -151,38 +156,43 @@ func ToExtensionResponse(ext *app.AIExtension) ExtensionResponse {
}
return ExtensionResponse{
ID: ext.ID,
UserID: ext.UserID,
Name: ext.Name,
DisplayName: ext.DisplayName,
Version: ext.Version,
Author: ext.Author,
Description: ext.Description,
Homepage: ext.Homepage,
Repository: ext.Repository,
License: ext.License,
Tags: tags,
ExtensionType: ext.ExtensionType,
Category: ext.Category,
Dependencies: dependencies,
Conflicts: conflicts,
ManifestData: manifestData,
ScriptPath: ext.ScriptPath,
StylePath: ext.StylePath,
AssetsPaths: assetsPaths,
Settings: settings,
Options: options,
IsEnabled: ext.IsEnabled,
IsInstalled: ext.IsInstalled,
IsSystemExt: ext.IsSystemExt,
InstallSource: ext.InstallSource,
InstallDate: ext.InstallDate,
LastEnabled: ext.LastEnabled,
UsageCount: ext.UsageCount,
ErrorCount: ext.ErrorCount,
LoadTime: ext.LoadTime,
Metadata: metadata,
CreatedAt: ext.CreatedAt,
UpdatedAt: ext.UpdatedAt,
ID: ext.ID,
UserID: ext.UserID,
Name: ext.Name,
DisplayName: ext.DisplayName,
Version: ext.Version,
Author: ext.Author,
Description: ext.Description,
Homepage: ext.Homepage,
Repository: ext.Repository,
License: ext.License,
Tags: tags,
ExtensionType: ext.ExtensionType,
Category: ext.Category,
Dependencies: dependencies,
Conflicts: conflicts,
ManifestData: manifestData,
ScriptPath: ext.ScriptPath,
StylePath: ext.StylePath,
AssetsPaths: assetsPaths,
Settings: settings,
Options: options,
IsEnabled: ext.IsEnabled,
IsInstalled: ext.IsInstalled,
IsSystemExt: ext.IsSystemExt,
InstallSource: ext.InstallSource,
SourceURL: ext.SourceURL,
Branch: ext.Branch,
AutoUpdate: ext.AutoUpdate,
InstallDate: ext.InstallDate,
LastEnabled: ext.LastEnabled,
LastUpdateCheck: ext.LastUpdateCheck,
AvailableVersion: ext.AvailableVersion,
UsageCount: ext.UsageCount,
ErrorCount: ext.ErrorCount,
LoadTime: ext.LoadTime,
Metadata: metadata,
CreatedAt: ext.CreatedAt,
UpdatedAt: ext.UpdatedAt,
}
}

View File

@@ -0,0 +1,92 @@
package response
import (
"encoding/json"
"time"
)
// ProviderResponse 提供商响应
type ProviderResponse struct {
ID uint `json:"id"`
ProviderName string `json:"providerName"`
ProviderType string `json:"providerType"`
BaseURL string `json:"baseUrl"`
APIKeySet bool `json:"apiKeySet"` // 是否已设置API密钥不返回明文
APIKeyHint string `json:"apiKeyHint"` // API密钥提示如 sk-****1234
APIConfig json.RawMessage `json:"apiConfig"`
Capabilities json.RawMessage `json:"capabilities"`
IsEnabled bool `json:"isEnabled"`
IsDefault bool `json:"isDefault"`
SortOrder int `json:"sortOrder"`
Models []ModelResponse `json:"models"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// ModelResponse 模型响应
type ModelResponse struct {
ID uint `json:"id"`
ProviderID uint `json:"providerId"`
ModelName string `json:"modelName"`
DisplayName string `json:"displayName"`
ModelType string `json:"modelType"`
Config json.RawMessage `json:"config"`
IsEnabled bool `json:"isEnabled"`
CreatedAt time.Time `json:"createdAt"`
}
// ProviderListResponse 提供商列表响应
type ProviderListResponse struct {
List []ProviderResponse `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// TestProviderResponse 测试连通性响应
type TestProviderResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Models []string `json:"models,omitempty"` // 获取到的可用模型列表
Latency int64 `json:"latency"` // 响应延迟(毫秒)
}
// ProviderTypeOption 提供商类型选项(前端下拉用)
type ProviderTypeOption struct {
Value string `json:"value"` // 类型标识
Label string `json:"label"` // 显示名称
Description string `json:"description"` // 描述
DefaultURL string `json:"defaultUrl"` // 默认API地址
}
// PresetModelOption 预设模型选项
type PresetModelOption struct {
ModelName string `json:"modelName"`
DisplayName string `json:"displayName"`
ModelType string `json:"modelType"` // chat / image_gen
}
// RemoteModel 远程获取到的模型信息
type RemoteModel struct {
ID string `json:"id"` // 模型标识API调用用
DisplayName string `json:"displayName"` // 显示名称
OwnedBy string `json:"ownedBy"` // 所有者/来源
}
// FetchRemoteModelsResponse 获取远程模型列表响应
type FetchRemoteModelsResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Models []RemoteModel `json:"models"`
Latency int64 `json:"latency"` // 响应延迟(毫秒)
}
// SendTestMessageResponse 发送测试消息响应
type SendTestMessageResponse struct {
Success bool `json:"success"`
Message string `json:"message"` // 状态信息
Reply string `json:"reply"` // AI 回复内容
Model string `json:"model"` // 实际使用的模型
Latency int64 `json:"latency"` // 响应延迟(毫秒)
Tokens int `json:"tokens"` // 消耗的token数
}