feat: 新增后端模型管理,第三方快捷登录

This commit is contained in:
npc0-hue
2026-02-11 17:41:59 +08:00
parent 7947b7976b
commit 9226a3d795
49 changed files with 2724 additions and 99 deletions
@@ -0,0 +1,37 @@
package gaia
import "time"
// ModelProviderConfig 模型提供商配置表
type ModelProviderConfig struct {
Id uint `json:"id" form:"id" gorm:"primarykey;column:id;comment:id;"`
ProviderName string `json:"provider_name" gorm:"unique;not null;column:provider_name;comment:提供商名称"`
Enabled bool `json:"enabled" gorm:"default:false;column:enabled;comment:是否开启"`
Models string `json:"models" gorm:"type:text;column:models;comment:开启的模型列表(JSON数组)"`
Config string `json:"config" gorm:"type:text;column:config;comment:额外配置(JSON)"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;comment:更新时间"`
}
// TableName ModelProviderConfig自定义表名 model_provider_config
func (ModelProviderConfig) TableName() string {
return "model_provider_config_extend"
}
// ModelProxyLog 模型中转请求日志表
type ModelProxyLog struct {
Id uint `json:"id" form:"id" gorm:"primarykey;column:id;comment:id;"`
UserId string `json:"user_id" gorm:"type:uuid;not null;column:user_id;comment:用户ID"`
ProviderName string `json:"provider_name" gorm:"column:provider_name;comment:提供商"`
ModelName string `json:"model_name" gorm:"column:model_name;comment:模型名"`
RequestTokens int `json:"request_tokens" gorm:"column:request_tokens;comment:请求token数"`
ResponseTokens int `json:"response_tokens" gorm:"column:response_tokens;comment:响应token数"`
Status string `json:"status" gorm:"column:status;comment:状态"`
ErrorMessage string `json:"error_message" gorm:"type:text;column:error_message;comment:错误信息"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;comment:创建时间"`
}
// TableName ModelProxyLog自定义表名 model_proxy_log
func (ModelProxyLog) TableName() string {
return "model_proxy_log_extend"
}
@@ -0,0 +1,41 @@
package gaia
// 模型提供商逻辑名称(列表展示与内部 key)
const (
ProviderOpenai = "openai"
ProviderTongyi = "tongyi"
ProviderGoogle = "google"
ProviderAnthropic = "anthropic"
)
// DifyProviderTypeCustom Dify providers 表 provider_type 枚举
const DifyProviderTypeCustom = "custom"
// 凭证配置中的 key 名
const (
ConfigKeyOpenaiAPIKey = "openai_api_key"
ConfigKeyOpenaiAPIBase = "openai_api_base"
ConfigKeyDashScopeAPIKey = "dashscope_api_key"
ConfigKeyAPIKey = "api_key"
)
// SupportedProviders 列表展示的提供商顺序
var SupportedProviders = []string{ProviderOpenai, ProviderTongyi, ProviderGoogle, ProviderAnthropic}
// DefaultChatCompletionsEndpoints 各提供商聊天接口默认完整 URL(兼容旧 ProxyChat
var DefaultChatCompletionsEndpoints = map[string]string{
ProviderOpenai: "https://api.openai.com/v1/chat/completions",
ProviderTongyi: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
ProviderGoogle: "https://generativelanguage.googleapis.com/v1beta/chat/completions",
}
// DefaultAPIBase 各提供商 API 根地址(无路径,用于通用代理;当 provider_credentials.encrypted_config 无 openai_api_base 时使用)
var DefaultAPIBase = map[string]string{
ProviderOpenai: "https://api.openai.com",
ProviderTongyi: "https://dashscope.aliyuncs.com/compatible-mode",
ProviderGoogle: "https://generativelanguage.googleapis.com",
ProviderAnthropic: "https://api.anthropic.com",
}
// CredentialKeyFallback 未知提供商时依次尝试的配置 key
var CredentialKeyFallback = []string{ConfigKeyOpenaiAPIKey, ConfigKeyAPIKey, ConfigKeyDashScopeAPIKey}
@@ -0,0 +1,13 @@
package gaia
import "time"
type ProviderCredential struct {
ID string `json:"id" gorm:"index;comment:凭证ID"`
TenantID string `json:"tenant_id" gorm:"comment:租户ID"`
ProviderName string `json:"provider_name" gorm:"comment:提供者名称"`
CredentialName string `json:"credential_name" gorm:"comment:凭证名称"`
EncryptedConfig string `json:"encrypted_config" gorm:"comment:加密配置"`
CreatedAt time.Time `json:"created_at" gorm:"not null;default:CURRENT_TIMESTAMP;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"not null;default:CURRENT_TIMESTAMP;comment:更新时间"`
}
@@ -0,0 +1,16 @@
package request
// GaiaOAuth2LoginReq OAuth2 登录请求(code 与 access_token 二选一;Extend: access_token 兼容 casdoor implicit/hybrid
type GaiaOAuth2LoginReq struct {
Code string `json:"code"`
AccessToken string `json:"access_token"` // Extend: 兼容 casdoor,无 code 时直接使用回调中的 access_token
RedirectURI string `json:"redirect_uri"`
State string `json:"state"`
}
// GaiaDingTalkLoginReq 钉钉登录请求
type GaiaDingTalkLoginReq struct {
AuthCode string `json:"auth_code" binding:"required"`
RedirectURI string `json:"redirect_uri"`
State string `json:"state"`
}
@@ -0,0 +1,12 @@
package request
// ChatRequest 聊天请求(OpenAI 兼容)
type ChatRequest struct {
Model string `json:"model"`
Messages []map[string]interface{} `json:"messages"`
Stream bool `json:"stream"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Tools []map[string]interface{} `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
}
@@ -0,0 +1,13 @@
package response
import (
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
)
// GaiaLoginResult 登录结果(含 JWT 与第三方回调参数)
type GaiaLoginResult struct {
User system.SysUser `json:"user"`
Token string `json:"token"`
RedirectURI string `json:"redirect_uri,omitempty"`
State string `json:"state,omitempty"`
}
@@ -0,0 +1,66 @@
package response
// ProviderCredentials 提供商凭证(内部/代理用)
type ProviderCredentials struct {
APIKey string `json:"api_key"`
Endpoint string `json:"endpoint,omitempty"`
}
// ModelInfo 模型信息
type ModelInfo struct {
ID string `json:"id"`
Name string `json:"name"`
}
// ProviderListItem 提供商列表项
type ProviderListItem struct {
ProviderName string `json:"provider_name"`
Enabled bool `json:"enabled"`
Models []string `json:"models"`
AvailableModels []ModelInfo `json:"available_models"`
}
// OpenAIModelsResponse OpenAI 格式的模型列表响应
type OpenAIModelsResponse struct {
Data []ModelInfo `json:"data"`
}
// OpenAIModelListItem GET /v1/models 返回的单项
type OpenAIModelListItem struct {
ID string `json:"id"`
}
// OpenAIModelsListResponse GET /v1/models 接口响应
type OpenAIModelsListResponse struct {
Data []OpenAIModelListItem `json:"data"`
}
// TongyiModelsListResponse 通义 GET /v1/models 返回的格式:success + output.models
type TongyiModelsListResponse struct {
Success bool `json:"success"`
Output struct {
Total int `json:"total"`
PageNo int `json:"page_no"`
PageSize int `json:"page_size"`
Models []TongyiModelItem `json:"models"`
} `json:"output"`
}
// TongyiModelItem 通义模型列表单项,id 为 model 字段
type TongyiModelItem struct {
Model string `json:"model"`
Name string `json:"name"`
}
// GeminiModelsListResponse Google Gemini GET /v1beta/models 返回:models[] + nextPageToken
type GeminiModelsListResponse struct {
Models []GeminiModelItem `json:"models"`
NextPageToken string `json:"nextPageToken"`
}
// GeminiModelItem Gemini 模型单项,name 为 "models/gemini-xxx"baseModelId 用于请求
type GeminiModelItem struct {
Name string `json:"name"`
BaseModelID string `json:"baseModelId"`
DisplayName string `json:"displayName"`
}