fix(workbench): 模型目录与助手形象 - 修正 Live2D 首帧闪现和模型默认分组 (#773)
* fix(workbench): 模型目录与助手形象 - 修正 Live2D 首帧闪现和模型默认分组
进入画布时形象配置与 Live2D 首帧未就绪不再显示默认球,改为加载提示,失败才回退。
新导入模型一级目录默认未分类、二级渠道展示名默认跟随上游 ID;历史非空分组(含与模型 ID 同名)保留。
拉取模型弹窗默认只勾选已存在模型并移除逐项已存在字样;一级分组下拉点击即展开全部选项。
* style(web): 画布与模型导入 - 按 Prettier 规范格式化改动文件
修复 PR 中 Web checks 的格式化检查失败:Live2D 启动器 JSX、模型导入选择工具函数与 Live2D 加载测试按项目 Prettier 配置重新排版,不改变行为。
* test(web): Live2D 生命周期 - 修复 bun 1.3.9 下 text 导入与模块导入冲突
agent-live2d-lifecycle 曾用 with { type: text } 导入 live2d-avatar.tsx,bun 1.3.9 会按路径复用该缓存,导致新增的模块导入拿不到 Live2DAvatar 具名导出并报 SyntaxError。改为 readFileSync 读取源码文本,本地以 bun 1.3.9 复现并验证通过。
This commit is contained in:
22 files changed
+251
-66
No files matched your search
@@ -286,6 +286,9 @@ func TestFetchAdminChannelModelsReaddsDeletedModel(t *testing.T) {
|
||||
if active.ID == deleted.ID || active.Enabled || active.PriceConfigured {
|
||||
t.Fatalf("re-added model = %#v", active)
|
||||
}
|
||||
if active.DisplayName != "未分类" || active.ProviderModelKey != "model-a" {
|
||||
t.Fatalf("discovered model defaults = %#v", active)
|
||||
}
|
||||
var total int64
|
||||
if err := db.Unscoped().Model(&model.ChannelModel{}).Where("channel_id = ? AND model_key = ?", channel.ID, "model-a").Count(&total).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -339,6 +342,24 @@ func TestImportAdminChannelModelsOnlyImportsSelectedModels(t *testing.T) {
|
||||
if len(imported) != 1 || imported[0].ModelKey != "model-b" {
|
||||
t.Fatalf("imported models = %#v, want only model-b", imported)
|
||||
}
|
||||
if imported[0].DisplayName != "未分类" || imported[0].ProviderModelKey != "model-b" {
|
||||
t.Fatalf("imported defaults = %#v", imported)
|
||||
}
|
||||
// 已有分组即使与模型 ID 相同,也不能被重复导入重置。
|
||||
if err := db.Model(&imported[0]).Updates(map[string]any{"display_name": "model-b", "channel_label": "手动名称"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = svc.ImportAdminChannelModels(context.Background(), admin, channel.ID, []string{"model-b"})
|
||||
if err != nil || result.Added != 0 {
|
||||
t.Fatalf("duplicate import = %#v, %v", result, err)
|
||||
}
|
||||
var unchanged model.ChannelModel
|
||||
if err := db.First(&unchanged, "id = ?", imported[0].ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if unchanged.DisplayName != "model-b" || unchanged.ChannelLabel != "手动名称" {
|
||||
t.Fatalf("duplicate import overwrote existing names: %#v", unchanged)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportAdminChannelModelsRejectsUnknownSelection(t *testing.T) {
|
||||
|
||||
@@ -92,7 +92,7 @@ func (s *Service) AdminReferences(actor *model.User) (*AdminReferenceData, error
|
||||
if item.Enabled {
|
||||
models = append(models, item.ModelKey)
|
||||
}
|
||||
displayNames = append(displayNames, firstNonEmpty(strings.TrimSpace(item.DisplayName), item.ModelKey))
|
||||
displayNames = append(displayNames, strings.TrimSpace(item.DisplayName))
|
||||
}
|
||||
result.Channels = append(result.Channels, AdminChannelReference{ID: channel.ID, Name: channel.Name, Enabled: channel.Enabled, Models: uniqueNonEmpty(models), ModelDisplayNames: uniqueNonEmpty(displayNames)})
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ func TestAdminReferencesIncludeExistingDisplayNameGroups(t *testing.T) {
|
||||
groups[name]++
|
||||
}
|
||||
}
|
||||
if len(groups) != 3 || groups["Shared"] != 1 || groups["Other"] != 1 || groups["d"] != 1 {
|
||||
if len(groups) != 2 || groups["Shared"] != 1 || groups["Other"] != 1 {
|
||||
t.Fatalf("groups = %#v", groups)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"yingce/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestChannelModelDirectoryDefaultsAndCustomNames(t *testing.T) {
|
||||
svc, db := newChannelModelTestService(t)
|
||||
channel := model.ModelChannel{ID: "directory-defaults", Scope: model.ChannelScopeSystem, Enabled: true, Name: "Test", BaseURL: "https://example.invalid/v1", APIKey: "synthetic", ModelsJSON: `[]`}
|
||||
if err := db.Create(&channel).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admin := &model.User{ID: "admin", Role: model.UserRoleAdmin}
|
||||
enabled := true
|
||||
request := ChannelModelRequest{
|
||||
ModelKey: "product", ProviderModelKey: "upstream", Capability: "text", Protocol: string(model.ChannelInterfaceChatCompletion),
|
||||
CapabilityConfig: DefaultModelCapabilityConfigForModel(string(model.ChannelInterfaceChatCompletion), "upstream"),
|
||||
PriceTiers: []ChannelModelPriceTierRequest{{BillingMode: "fixed_request", PriceConfigured: true, Enabled: &enabled}},
|
||||
Enabled: &enabled,
|
||||
}
|
||||
saved, err := svc.SaveAdminChannelModel(admin, channel.ID, "", request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if saved.DisplayName != "未分类" {
|
||||
t.Fatalf("default group = %q", saved.DisplayName)
|
||||
}
|
||||
public, err := svc.sanitizeChannelModel(saved)
|
||||
if err != nil || public.ChannelLabel != "upstream" {
|
||||
t.Fatalf("default second-level label = %q, %v", public.ChannelLabel, err)
|
||||
}
|
||||
request.DisplayName, request.ChannelLabel = "product", "自定义渠道"
|
||||
saved, err = svc.SaveAdminChannelModel(admin, channel.ID, saved.ID, request)
|
||||
if err != nil || saved.DisplayName != "product" || saved.ChannelLabel != "自定义渠道" {
|
||||
t.Fatalf("custom names = %#v, %v", saved, err)
|
||||
}
|
||||
// 默认名称无需写入较短的 channel_label 列,允许跟随超过 80 字的上游 ID。
|
||||
request.DisplayName = "未分类"
|
||||
request.ProviderModelKey = strings.Repeat("a", 100)
|
||||
request.ChannelLabel = request.ProviderModelKey
|
||||
saved, err = svc.SaveAdminChannelModel(admin, channel.ID, saved.ID, request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
public, err = svc.sanitizeChannelModel(saved)
|
||||
if err != nil || public.ChannelLabel != request.ProviderModelKey || saved.ChannelLabel != "" {
|
||||
t.Fatalf("long default label = %q, %v", public.ChannelLabel, err)
|
||||
}
|
||||
}
|
||||
@@ -151,9 +151,6 @@ func (s *Service) SaveAdminChannelModel(actor *model.User, channelID string, id
|
||||
if len([]rune(description)) > 500 {
|
||||
return nil, BadAuthRequest("模型描述不能超过 500 字")
|
||||
}
|
||||
if len([]rune(channelLabel)) > 80 {
|
||||
return nil, BadAuthRequest("渠道展示名不能超过 80 字")
|
||||
}
|
||||
channel, err := s.repo.AdminSystemChannel(channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -162,6 +159,13 @@ func (s *Service) SaveAdminChannelModel(actor *model.User, channelID string, id
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 空值表示跟随上游 ID;不把默认名称固化,避免上游重命名后遗留旧 ID。
|
||||
if channelLabel == providerModelKey {
|
||||
channelLabel = ""
|
||||
}
|
||||
if len([]rune(channelLabel)) > 80 {
|
||||
return nil, BadAuthRequest("渠道展示名不能超过 80 字")
|
||||
}
|
||||
// 先检查同渠道重复模型,避免无关能力校验或生成无用序列号掩盖真正的冲突。
|
||||
conflict, conflictErr := s.repo.ChannelModelByKeyIncludingDisabled(channelID, modelKey)
|
||||
if conflictErr != nil && !errors.Is(conflictErr, gorm.ErrRecordNotFound) {
|
||||
@@ -207,12 +211,12 @@ func (s *Service) SaveAdminChannelModel(actor *model.User, channelID string, id
|
||||
item.ModelKey = modelKey
|
||||
item.ProviderModelKey = providerModelKey
|
||||
item.DisplayName = strings.TrimSpace(req.DisplayName)
|
||||
if item.DisplayName == "" {
|
||||
item.DisplayName = "未分类"
|
||||
}
|
||||
item.ChannelLabel = channelLabel
|
||||
item.Tags = tags
|
||||
item.Description = description
|
||||
if item.DisplayName == "" {
|
||||
item.DisplayName = modelKey
|
||||
}
|
||||
item.Icon = strings.TrimSpace(req.Icon)
|
||||
item.Capability = capability
|
||||
item.Protocol = protocol
|
||||
@@ -387,7 +391,7 @@ func (s *Service) syncInitialChannelModels(channel *model.ModelChannel, names []
|
||||
if idErr != nil {
|
||||
return idErr
|
||||
}
|
||||
item := model.ChannelModel{ID: modelID, ChannelID: channel.ID, ModelKey: name, DisplayName: name, BillingMode: "fixed_request", Enabled: false, PriceConfigured: false, UnitPriceMicrocredits: 0, PriceVersion: 1}
|
||||
item := model.ChannelModel{ID: modelID, ChannelID: channel.ID, ModelKey: name, ProviderModelKey: name, DisplayName: "未分类", BillingMode: "fixed_request", Enabled: false, PriceConfigured: false, UnitPriceMicrocredits: 0, PriceVersion: 1}
|
||||
if err := s.repo.SaveChannelModel(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (s *Service) FetchAdminChannelModels(ctx context.Context, actor *model.User
|
||||
if idErr != nil {
|
||||
return nil, idErr
|
||||
}
|
||||
missing = append(missing, model.ChannelModel{ID: modelID, ChannelID: channelID, ModelKey: name, ProviderModelKey: name, DisplayName: name, BillingMode: "fixed_request", Enabled: false, PriceVersion: 1})
|
||||
missing = append(missing, model.ChannelModel{ID: modelID, ChannelID: channelID, ModelKey: name, ProviderModelKey: name, DisplayName: "未分类", BillingMode: "fixed_request", Enabled: false, PriceVersion: 1})
|
||||
}
|
||||
added, err := s.repo.CreateMissingChannelModels(missing)
|
||||
if err != nil {
|
||||
@@ -133,7 +133,7 @@ func (s *Service) ImportAdminChannelModels(ctx context.Context, actor *model.Use
|
||||
if idErr != nil {
|
||||
return nil, idErr
|
||||
}
|
||||
missing = append(missing, model.ChannelModel{ID: modelID, ChannelID: channelID, ModelKey: name, DisplayName: name, BillingMode: "fixed_request", Enabled: false, PriceConfigured: false, PriceVersion: 1})
|
||||
missing = append(missing, model.ChannelModel{ID: modelID, ChannelID: channelID, ModelKey: name, ProviderModelKey: name, DisplayName: "未分类", BillingMode: "fixed_request", Enabled: false, PriceConfigured: false, PriceVersion: 1})
|
||||
known[key] = struct{}{}
|
||||
}
|
||||
added, err := s.repo.CreateMissingChannelModels(missing)
|
||||
|
||||
@@ -245,7 +245,7 @@ func (s *Service) sanitizeChannelModel(cm *model.ChannelModel) (PublicChannelMod
|
||||
return PublicChannelModel{
|
||||
ID: cm.ID,
|
||||
ModelKey: cm.ModelKey,
|
||||
ChannelLabel: cm.ChannelLabel,
|
||||
ChannelLabel: firstNonEmpty(cm.ChannelLabel, cm.ProviderModelKey, cm.ModelKey),
|
||||
Tags: cm.Tags,
|
||||
Description: cm.Description,
|
||||
DisplayName: cm.DisplayName,
|
||||
|
||||
Reference in new issue
Block a user