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,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