feat: 新增配置与提示词偏好页
后端新增 preferences 与 prompt_customizations 表及对应接口, 支持 BYOK 模型渠道密钥落库(仅回显掩码)与按任务类型定制 平台提示词模板;前端新增 /settings 页面及入口。
This commit is contained in:
1 parent
ae6cdf1551
commit
b9df2f84ab
27 files changed
+1515
-22
No files matched your search
@@ -0,0 +1,190 @@
|
||||
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 != ""
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/ttstd/infinite-canvas/backend/internal/model"
|
||||
"github.com/ttstd/infinite-canvas/backend/internal/prompts"
|
||||
"github.com/ttstd/infinite-canvas/backend/internal/repository"
|
||||
)
|
||||
|
||||
const maxPromptContentRunes = 8000
|
||||
|
||||
// PromptTemplateView 平台模板视图。模板内置在代码里,这里给出前端需要的元信息与内容。
|
||||
type PromptTemplateView struct {
|
||||
Operation string `json:"operation"`
|
||||
Name string `json:"name"`
|
||||
Version int `json:"version"`
|
||||
Content string `json:"content"`
|
||||
OutputType string `json:"outputType"`
|
||||
}
|
||||
|
||||
// PromptPreferenceView 一个任务类型的提示词配置,结构对齐 open-ai-canvas 的 UserPromptPreference。
|
||||
type PromptPreferenceView struct {
|
||||
Definition prompts.Definition `json:"definition"`
|
||||
Template *PromptTemplateView `json:"template"`
|
||||
Customization *model.PromptCustomization `json:"customization,omitempty"`
|
||||
Outdated bool `json:"outdated"`
|
||||
}
|
||||
|
||||
type PromptService struct {
|
||||
prompts *repository.PromptRepository
|
||||
}
|
||||
|
||||
func NewPromptService(promptsRepo *repository.PromptRepository) *PromptService {
|
||||
return &PromptService{prompts: promptsRepo}
|
||||
}
|
||||
|
||||
// Preferences 返回全部任务类型及其平台模板与用户定制。
|
||||
func (s *PromptService) Preferences() ([]PromptPreferenceView, error) {
|
||||
rows, err := s.prompts.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
byOperation := make(map[string]*model.PromptCustomization, len(rows))
|
||||
for index := range rows {
|
||||
byOperation[rows[index].Operation] = &rows[index]
|
||||
}
|
||||
|
||||
definitions := prompts.Definitions()
|
||||
result := make([]PromptPreferenceView, 0, len(definitions))
|
||||
for _, definition := range definitions {
|
||||
result = append(result, buildPromptPreference(definition, byOperation[definition.Operation]))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Customize 保存定制。mode=inherit 视为放弃定制,直接删除已有记录。
|
||||
func (s *PromptService) Customize(operation, mode, content string) (*PromptPreferenceView, error) {
|
||||
definition, ok := prompts.Find(operation)
|
||||
if !ok {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
|
||||
switch strings.TrimSpace(mode) {
|
||||
case model.PromptModeInherit:
|
||||
if err := s.prompts.Delete(operation); err != nil && !errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
preference := buildPromptPreference(definition, nil)
|
||||
return &preference, nil
|
||||
case model.PromptModeAppend, model.PromptModeRewrite:
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: 不支持的定制模式", ErrInvalidInput)
|
||||
}
|
||||
|
||||
trimmed := strings.TrimSpace(content)
|
||||
if trimmed == "" {
|
||||
return nil, fmt.Errorf("%w: 请填写个人提示词内容", ErrInvalidInput)
|
||||
}
|
||||
if len([]rune(trimmed)) > maxPromptContentRunes {
|
||||
return nil, fmt.Errorf("%w: 提示词内容不能超过 %d 个字符", ErrInvalidInput, maxPromptContentRunes)
|
||||
}
|
||||
|
||||
customization := &model.PromptCustomization{
|
||||
ID: uuid.NewString(),
|
||||
Operation: operation,
|
||||
Mode: strings.TrimSpace(mode),
|
||||
Content: trimmed,
|
||||
BaseTemplateVersion: prompts.TemplateVersion,
|
||||
}
|
||||
|
||||
// 同一任务类型只保留一条记录:复用原有 ID 与创建时间。
|
||||
existing, err := s.prompts.Get(operation)
|
||||
switch {
|
||||
case err == nil:
|
||||
customization.ID = existing.ID
|
||||
customization.CreatedAt = existing.CreatedAt
|
||||
case errors.Is(err, repository.ErrNotFound):
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.prompts.Save(customization); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
preference := buildPromptPreference(definition, customization)
|
||||
return &preference, nil
|
||||
}
|
||||
|
||||
// Reset 恢复平台模板。
|
||||
func (s *PromptService) Reset(operation string) error {
|
||||
if _, ok := prompts.Find(operation); !ok {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
if err := s.prompts.Delete(operation); err != nil && !errors.Is(err, repository.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildPromptPreference(definition prompts.Definition, customization *model.PromptCustomization) PromptPreferenceView {
|
||||
preference := PromptPreferenceView{
|
||||
Definition: definition,
|
||||
Template: &PromptTemplateView{
|
||||
Operation: definition.Operation,
|
||||
Name: "默认" + definition.Label + "模板",
|
||||
Version: prompts.TemplateVersion,
|
||||
Content: definition.DefaultContent,
|
||||
OutputType: definition.OutputType,
|
||||
},
|
||||
Customization: customization,
|
||||
}
|
||||
if customization != nil {
|
||||
preference.Outdated = customization.BaseTemplateVersion < prompts.TemplateVersion
|
||||
}
|
||||
return preference
|
||||
}
|
||||
Reference in new issue
Block a user