后端新增 preferences 与 prompt_customizations 表及对应接口, 支持 BYOK 模型渠道密钥落库(仅回显掩码)与按任务类型定制 平台提示词模板;前端新增 /settings 页面及入口。
191 lines
4.8 KiB
Go
191 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/ttstd/infinite-canvas/backend/internal/model"
|
|
"github.com/ttstd/infinite-canvas/backend/internal/repository"
|
|
)
|
|
|
|
const maxAPIKeyEntries = 20
|
|
|
|
// APIKeyView 对外输出的渠道配置,只回传掩码,不含明文密钥。
|
|
type APIKeyView struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"provider"`
|
|
Label string `json:"label"`
|
|
BaseURL string `json:"baseUrl"`
|
|
Model string `json:"model"`
|
|
Enabled bool `json:"enabled"`
|
|
HasKey bool `json:"hasKey"`
|
|
KeyMask string `json:"keyMask"`
|
|
}
|
|
|
|
// PreferenceView 配置与用户偏好。
|
|
type PreferenceView struct {
|
|
APIKeys []APIKeyView `json:"apiKeys"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
// APIKeyInput 前端提交的渠道配置。APIKey 留空表示沿用已保存的密钥。
|
|
type APIKeyInput struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"provider"`
|
|
Label string `json:"label"`
|
|
BaseURL string `json:"baseUrl"`
|
|
APIKey string `json:"apiKey"`
|
|
Model string `json:"model"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
type UpdatePreferenceInput struct {
|
|
APIKeys []APIKeyInput `json:"apiKeys"`
|
|
}
|
|
|
|
type PreferenceService struct {
|
|
preferences *repository.PreferenceRepository
|
|
}
|
|
|
|
func NewPreferenceService(preferences *repository.PreferenceRepository) *PreferenceService {
|
|
return &PreferenceService{preferences: preferences}
|
|
}
|
|
|
|
func (s *PreferenceService) Get() (*PreferenceView, error) {
|
|
preference, err := s.preferences.Get()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toPreferenceView(preference), nil
|
|
}
|
|
|
|
// Update 校验并覆盖保存;未填写密钥的渠道沿用已保存的值。
|
|
func (s *PreferenceService) Update(input UpdatePreferenceInput) (*PreferenceView, error) {
|
|
current, err := s.preferences.Get()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
apiKeys, err := mergeAPIKeys(current.APIKeys, input.APIKeys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
preference := &model.Preference{
|
|
ID: model.PreferenceID,
|
|
APIKeys: apiKeys,
|
|
CreatedAt: current.CreatedAt,
|
|
}
|
|
if err := s.preferences.Save(preference); err != nil {
|
|
return nil, err
|
|
}
|
|
return toPreferenceView(preference), nil
|
|
}
|
|
|
|
func toPreferenceView(preference *model.Preference) *PreferenceView {
|
|
apiKeys := make([]APIKeyView, 0, len(preference.APIKeys))
|
|
for _, item := range preference.APIKeys {
|
|
apiKeys = append(apiKeys, APIKeyView{
|
|
ID: item.ID,
|
|
Provider: item.Provider,
|
|
Label: item.Label,
|
|
BaseURL: item.BaseURL,
|
|
Model: item.Model,
|
|
Enabled: item.Enabled,
|
|
HasKey: item.APIKey != "",
|
|
KeyMask: maskSecret(item.APIKey),
|
|
})
|
|
}
|
|
|
|
return &PreferenceView{
|
|
APIKeys: apiKeys,
|
|
UpdatedAt: preference.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func mergeAPIKeys(current []model.APIKeyConfig, inputs []APIKeyInput) ([]model.APIKeyConfig, error) {
|
|
if len(inputs) > maxAPIKeyEntries {
|
|
return nil, fmt.Errorf("%w: 渠道数量不能超过 %d 个", ErrInvalidInput, maxAPIKeyEntries)
|
|
}
|
|
|
|
existing := make(map[string]model.APIKeyConfig, len(current))
|
|
for _, item := range current {
|
|
existing[item.ID] = item
|
|
}
|
|
|
|
result := make([]model.APIKeyConfig, 0, len(inputs))
|
|
for _, input := range inputs {
|
|
provider := strings.TrimSpace(input.Provider)
|
|
if provider == "" {
|
|
return nil, fmt.Errorf("%w: 渠道标识不能为空", ErrInvalidInput)
|
|
}
|
|
if len(provider) > 64 {
|
|
return nil, fmt.Errorf("%w: 渠道标识不能超过 64 个字符", ErrInvalidInput)
|
|
}
|
|
|
|
label := strings.TrimSpace(input.Label)
|
|
if label == "" {
|
|
label = provider
|
|
}
|
|
if len([]rune(label)) > 64 {
|
|
return nil, fmt.Errorf("%w: 渠道名称不能超过 64 个字符", ErrInvalidInput)
|
|
}
|
|
|
|
baseURL := strings.TrimSpace(input.BaseURL)
|
|
if baseURL != "" && !isHTTPURL(baseURL) {
|
|
return nil, fmt.Errorf("%w: Base URL 必须是 http/https 地址", ErrInvalidInput)
|
|
}
|
|
|
|
apiKey := strings.TrimSpace(input.APIKey)
|
|
if len(apiKey) > 512 {
|
|
return nil, fmt.Errorf("%w: API Key 长度超出限制", ErrInvalidInput)
|
|
}
|
|
|
|
id := strings.TrimSpace(input.ID)
|
|
if id == "" {
|
|
id = uuid.NewString()
|
|
}
|
|
if apiKey == "" {
|
|
if previous, ok := existing[id]; ok {
|
|
apiKey = previous.APIKey
|
|
}
|
|
}
|
|
|
|
result = append(result, model.APIKeyConfig{
|
|
ID: id,
|
|
Provider: provider,
|
|
Label: label,
|
|
BaseURL: baseURL,
|
|
APIKey: apiKey,
|
|
Model: strings.TrimSpace(input.Model),
|
|
Enabled: input.Enabled,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// maskSecret 只保留末 4 位,其余用圆点代替。
|
|
func maskSecret(secret string) string {
|
|
if secret == "" {
|
|
return ""
|
|
}
|
|
runes := []rune(secret)
|
|
if len(runes) <= 4 {
|
|
return strings.Repeat("•", len(runes))
|
|
}
|
|
return strings.Repeat("•", 4) + string(runes[len(runes)-4:])
|
|
}
|
|
|
|
func isHTTPURL(raw string) bool {
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != ""
|
|
}
|