feat(*): 系统渠道与画布 - 支持多规格批量调价及 Agent 入口拖动

This commit is contained in:
ddCat committed 2026-09-20 11:18:19 +08:00
1 parent 9afe061d79
commit 8d94bde1f4
26 files changed
+1761 -44

No files matched your search

@@ -0,0 +1,130 @@
package app
import (
"errors"
"fmt"
"sort"
"strings"
"infinite-canvas/backend/internal/model"
"infinite-canvas/backend/internal/repository"
)
type ChannelModelRepriceRequest struct {
ModelID string `json:"modelId"`
PriceVersion int64 `json:"priceVersion"`
PriceTiers []ChannelModelTierRepriceRequest `json:"priceTiers"`
}
type ChannelModelTierRepriceRequest struct {
ID string `json:"id"`
PriceVersion int64 `json:"priceVersion"`
Prices map[string]*int64 `json:"prices"`
}
// RepriceAdminChannelModels only changes sale prices, never model availability or cost.
func (s *Service) RepriceAdminChannelModels(actor *model.User, channelID string, requests []ChannelModelRepriceRequest) (int, error) {
if err := s.RequireAdmin(actor); err != nil {
return 0, err
}
if _, err := s.adminSystemChannel(channelID); err != nil {
return 0, err
}
if len(requests) == 0 || len(requests) > 100 {
return 0, BadAuthRequest("单次请选择 1 到 100 个渠道模型")
}
selected := make(map[string]ChannelModelRepriceRequest, len(requests))
for _, request := range requests {
id := strings.TrimSpace(request.ModelID)
if _, duplicate := selected[id]; id == "" || duplicate || request.PriceVersion < 1 {
return 0, BadAuthRequest("渠道模型 ID 或价格版本无效,且模型不能重复")
}
selected[id] = request
}
items, err := s.repo.ChannelModels(channelID, true)
if err != nil {
return 0, err
}
updates := make([]model.ChannelModel, 0, len(selected))
for _, item := range items {
request, exists := selected[item.ID]
if !exists {
continue
}
if request.PriceVersion != item.PriceVersion {
return 0, BadAuthRequest("模型价格已变化,请刷新后重新调价;本次未修改任何价格")
}
tiers := make(map[string]ChannelModelTierRepriceRequest, len(request.PriceTiers))
for _, tier := range request.PriceTiers {
if _, duplicate := tiers[tier.ID]; tier.ID == "" || duplicate {
return 0, BadAuthRequest("规格 ID 不能为空或重复")
}
tiers[tier.ID] = tier
}
active := 0
for index := range item.PriceTiers {
tier := &item.PriceTiers[index]
if !tier.Enabled {
continue
}
active++
input, exists := tiers[tier.ID]
if !exists || input.PriceVersion != tier.PriceVersion {
return 0, BadAuthRequest("规格价格已变化或提交不完整,请刷新后重新调价")
}
if err := applyChannelModelTierPrices(tier, item.Capability, item.Protocol, input.Prices); err != nil {
return 0, BadAuthRequest(fmt.Sprintf("模型 %s(规格 %s):%s;本次未修改任何价格", item.ModelKey, tier.SelectorKey, err.Error()))
}
}
if active == 0 || active != len(tiers) {
return 0, BadAuthRequest("模型 " + item.ModelKey + " 的规格提交不完整或包含无效规格;本次未修改任何价格")
}
s.applyChannelModelPriceTierSummary(&item, item.PriceTiers)
updates = append(updates, item)
}
if len(updates) != len(selected) {
return 0, BadAuthRequest("所选模型已删除或不属于当前渠道,请刷新后重试")
}
// Consistent lock order for overlapping batch requests.
sort.Slice(updates, func(i, j int) bool { return updates[i].ID < updates[j].ID })
if err := s.repo.UpdateChannelModelSalePrices(channelID, updates); err != nil {
if errors.Is(err, repository.ErrChannelModelPriceConflict) {
return 0, BadAuthRequest("模型价格已被其他操作修改,请刷新后重试;本次未修改任何价格")
}
return 0, err
}
s.invalidateRouteCatalog()
return len(updates), nil
}
func applyChannelModelTierPrices(tier *model.ChannelModelPriceTier, capability string, protocol model.ChannelInterfaceType, prices map[string]*int64) error {
if tier.ID == "" || !tier.PriceConfigured || !tier.CostPricing.Configured {
return BadAuthRequest("请先保存完整的规格成本价和销售价配置")
}
if err := validateCreditCostPricing(capability, tier.BillingMode, tier.CostPricing); err != nil {
return err
}
fields := map[string]*int64{"unitPriceMicrocredits": &tier.UnitPriceMicrocredits}
if tier.BillingMode == "token" {
fields = map[string]*int64{"outputTokenPriceMicrocredits": &tier.OutputTokenPriceMicrocredits}
if capability != "video" {
fields["inputTokenPriceMicrocredits"] = &tier.InputTokenPriceMicrocredits
fields["cachedTokenPriceMicrocredits"] = &tier.CachedTokenPriceMicrocredits
}
}
if len(prices) != len(fields) {
return BadAuthRequest("必须提交当前计费方式的全部销售价,且不能包含其他字段")
}
for key, target := range fields {
value, exists := prices[key]
if !exists || value == nil || *value < 0 || *value > 9_007_199_254_740_991 {
return BadAuthRequest("销售价缺失或超出有效范围")
}
*target = *value
}
return validateChannelModelTierPricing(capability, protocol, tier.BillingMode, ChannelModelPriceTierRequest{
CostPricing: tier.CostPricing, UnitPriceMicrocredits: tier.UnitPriceMicrocredits,
InputTokenPriceMicrocredits: tier.InputTokenPriceMicrocredits, OutputTokenPriceMicrocredits: tier.OutputTokenPriceMicrocredits,
CachedTokenPriceMicrocredits: tier.CachedTokenPriceMicrocredits,
})
}
@@ -0,0 +1,257 @@
package app
import (
"errors"
"testing"
"infinite-canvas/backend/internal/model"
"infinite-canvas/backend/internal/repository"
"gorm.io/gorm"
)
func seedRepricingModels(t *testing.T) (*Service, *gorm.DB, *model.User) {
t.Helper()
svc, db := newChannelModelTestService(t)
admin := &model.User{ID: "admin", Role: model.UserRoleAdmin}
if err := db.Create(&model.ModelChannel{ID: "channel", UserID: admin.ID, Scope: model.ChannelScopeSystem, Enabled: true}).Error; err != nil {
t.Fatal(err)
}
for _, id := range []string{"a", "b"} {
item := model.ChannelModel{ID: id, ChannelID: "channel", ModelKey: id, DisplayName: id, Capability: "text", Protocol: model.ChannelInterfaceChatCompletion, BillingMode: "fixed_request", Enabled: true, PriceConfigured: true, PriceVersion: 1, UnitPriceMicrocredits: 9}
tier := model.ChannelModelPriceTier{ID: "tier-" + id, ChannelModelID: id, SelectorKey: "{}", SelectorJSON: "{}", Resolution: "*", BillingMode: "fixed_request", Enabled: true, PriceConfigured: true, PriceVersion: 1, UnitPriceMicrocredits: 9, CostPricing: model.CreditCostPricing{Configured: true, UnitPriceMicrocredits: 1_000_000}}
if err := db.Create(&item).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&tier).Error; err != nil {
t.Fatal(err)
}
}
return svc, db, admin
}
func repricingRequest(id string, price int64) ChannelModelRepriceRequest {
return ChannelModelRepriceRequest{ModelID: id, PriceVersion: 1, PriceTiers: []ChannelModelTierRepriceRequest{{ID: "tier-" + id, PriceVersion: 1, Prices: map[string]*int64{"unitPriceMicrocredits": &price}}}}
}
func TestRepriceAdminChannelModelsPreservesFinalManualPrices(t *testing.T) {
svc, db, admin := seedRepricingModels(t)
disabled := model.ChannelModelPriceTier{ID: "disabled", ChannelModelID: "a", SelectorKey: `{"quality":"high"}`, Enabled: false, UnitPriceMicrocredits: 99, PriceVersion: 7}
if err := db.Create(&disabled).Error; err != nil {
t.Fatal(err)
}
if err := db.Model(&model.ChannelModel{}).Where("id = ?", "b").Update("enabled", false).Error; err != nil {
t.Fatal(err)
}
requests := []ChannelModelRepriceRequest{repricingRequest("a", 1_250_000), repricingRequest("b", 1_370_000)}
updated, err := svc.RepriceAdminChannelModels(admin, "channel", requests)
if err != nil || updated != 2 {
t.Fatalf("updated=%d err=%v", updated, err)
}
for _, request := range requests {
item, err := svc.repo.ChannelModelByID("channel", request.ModelID)
if err != nil {
t.Fatal(err)
}
want := *request.PriceTiers[0].Prices["unitPriceMicrocredits"]
if item.UnitPriceMicrocredits != want || item.PriceVersion != 2 || item.Enabled != (item.ID == "a") {
t.Fatalf("model=%+v", item)
}
for _, tier := range item.PriceTiers {
if tier.ID == "disabled" {
if tier.UnitPriceMicrocredits != 99 || tier.PriceVersion != 7 || tier.Enabled {
t.Fatalf("disabled=%+v", tier)
}
} else if tier.UnitPriceMicrocredits != want || tier.PriceVersion != 2 || tier.CostPricing.UnitPriceMicrocredits != 1_000_000 {
t.Fatalf("tier=%+v", tier)
}
}
}
if _, err := svc.RepriceAdminChannelModels(admin, "channel", requests); err == nil {
t.Fatal("stale preview accepted")
}
}
func TestRepriceAdminChannelModelsMultipleSpecifications(t *testing.T) {
svc, db, admin := seedRepricingModels(t)
if err := db.Model(&model.ChannelModel{}).Where("id = ?", "a").Updates(map[string]any{
"capability": "image", "protocol": model.ChannelInterfaceOpenAIImage,
}).Error; err != nil {
t.Fatal(err)
}
request := repricingRequest("a", 4_000)
for _, spec := range []struct {
id string
price int64
}{{"2K", 5_500}, {"4K", 6_000}} {
tier := model.ChannelModelPriceTier{ID: spec.id, ChannelModelID: "a", SelectorKey: `{"vquality":"` + spec.id + `"}`,
SelectorJSON: `{"vquality":"` + spec.id + `"}`, Resolution: spec.id, BillingMode: "fixed_request",
Enabled: true, PriceConfigured: true, PriceVersion: 1, UnitPriceMicrocredits: 9,
CostPricing: model.CreditCostPricing{Configured: true, UnitPriceMicrocredits: 3_000}}
if err := db.Create(&tier).Error; err != nil {
t.Fatal(err)
}
request.PriceTiers = append(request.PriceTiers, ChannelModelTierRepriceRequest{ID: spec.id, PriceVersion: 1,
Prices: map[string]*int64{"unitPriceMicrocredits": &spec.price}})
}
if updated, err := svc.RepriceAdminChannelModels(admin, "channel", []ChannelModelRepriceRequest{request}); err != nil || updated != 1 {
t.Fatalf("updated=%d err=%v", updated, err)
}
for _, input := range request.PriceTiers {
var tier model.ChannelModelPriceTier
if err := db.First(&tier, "id = ?", input.ID).Error; err != nil {
t.Fatal(err)
}
if tier.UnitPriceMicrocredits != *input.Prices["unitPriceMicrocredits"] || tier.PriceVersion != 2 {
t.Fatalf("specification price was not saved independently: %+v", tier)
}
}
}
func TestRepriceAdminChannelModelsRejectsWholeBatch(t *testing.T) {
for _, tt := range []struct {
name string
edit func([]ChannelModelRepriceRequest) []ChannelModelRepriceRequest
mutate func(*gorm.DB) error
nonAdmin bool
}{
{name: "empty", edit: func(_ []ChannelModelRepriceRequest) []ChannelModelRepriceRequest { return nil }},
{name: "too many", edit: func(_ []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
return make([]ChannelModelRepriceRequest, 101)
}},
{name: "duplicate model", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest { return append(r, r[0]) }},
{name: "empty ID", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest { r[1].ModelID = ""; return r }},
{name: "stale model", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest { r[1].PriceVersion = 2; return r }},
{name: "stale tier", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers[0].PriceVersion = 2
return r
}},
{name: "missing tier", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest { r[1].PriceTiers = nil; return r }},
{name: "wrong tier owner", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers[0].ID = "tier-a"
return r
}},
{name: "duplicate tier", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers = append(r[1].PriceTiers, r[1].PriceTiers[0])
return r
}},
{name: "negative price", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1] = repricingRequest("b", -1)
return r
}},
{name: "missing price", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers[0].Prices = nil
return r
}},
{name: "null price", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers[0].Prices["unitPriceMicrocredits"] = nil
return r
}},
{name: "unknown field", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1].PriceTiers[0].Prices["enabled"] = nil
return r
}},
{name: "unsafe price", edit: func(r []ChannelModelRepriceRequest) []ChannelModelRepriceRequest {
r[1] = repricingRequest("b", 9_007_199_254_740_992)
return r
}},
{name: "missing cost", mutate: func(db *gorm.DB) error {
return db.Model(&model.ChannelModelPriceTier{}).Where("id = ?", "tier-b").Update("cost_configured", false).Error
}},
{name: "disabled tier", mutate: func(db *gorm.DB) error {
return db.Model(&model.ChannelModelPriceTier{}).Where("id = ?", "tier-b").Update("enabled", false).Error
}},
{name: "cross channel", mutate: func(db *gorm.DB) error {
return db.Model(&model.ChannelModel{}).Where("id = ?", "b").Update("channel_id", "other").Error
}},
{name: "deleted", mutate: func(db *gorm.DB) error { return db.Delete(&model.ChannelModel{}, "id = ?", "b").Error }},
{name: "not admin", nonAdmin: true},
} {
t.Run(tt.name, func(t *testing.T) {
svc, db, actor := seedRepricingModels(t)
requests := []ChannelModelRepriceRequest{repricingRequest("a", 1_250_000), repricingRequest("b", 1_370_000)}
if tt.edit != nil {
requests = tt.edit(requests)
}
if tt.mutate != nil {
if err := tt.mutate(db); err != nil {
t.Fatal(err)
}
}
if tt.nonAdmin {
actor = &model.User{ID: "user"}
}
if count, err := svc.RepriceAdminChannelModels(actor, "channel", requests); err == nil || count != 0 {
t.Fatalf("count=%d err=%v", count, err)
}
item, err := svc.repo.ChannelModelByID("channel", "a")
if err != nil {
t.Fatal(err)
}
if item.PriceVersion != 1 || item.PriceTiers[0].UnitPriceMicrocredits != 9 {
t.Fatal("partial save")
}
})
}
}
func TestRepriceTierFieldValidation(t *testing.T) {
for _, capability := range []string{"text", "video"} {
output, input, zero := int64(1_370_000), int64(500_000), int64(0)
tier := model.ChannelModelPriceTier{ID: "t", BillingMode: "token", PriceConfigured: true, CostPricing: model.CreditCostPricing{Configured: true}}
prices := map[string]*int64{"outputTokenPriceMicrocredits": &output}
protocol := model.ChannelInterfaceVolcengineArkVideo
if capability == "text" {
protocol = model.ChannelInterfaceChatCompletion
prices["inputTokenPriceMicrocredits"] = &input
prices["cachedTokenPriceMicrocredits"] = &zero
}
if err := applyChannelModelTierPrices(&tier, capability, protocol, prices); err != nil {
t.Fatal(err)
}
if tier.OutputTokenPriceMicrocredits != output || tier.CachedTokenPriceMicrocredits != 0 {
t.Fatalf("tier=%+v", tier)
}
output = 1_000_000_000_001
if err := applyChannelModelTierPrices(&tier, capability, protocol, prices); err == nil {
t.Fatal("token price limit missing")
}
}
}
func TestBatchRepricingRollsBackOnConcurrentChange(t *testing.T) {
for _, target := range []string{"model", "tier"} {
t.Run(target, func(t *testing.T) {
svc, db, _ := seedRepricingModels(t)
items := make([]model.ChannelModel, 0, 2)
for _, id := range []string{"a", "b"} {
item, err := svc.repo.ChannelModelByID("channel", id)
if err != nil {
t.Fatal(err)
}
item.UnitPriceMicrocredits = 123
item.PriceTiers[0].UnitPriceMicrocredits = 123
items = append(items, *item)
}
var err error
if target == "model" {
err = db.Model(&model.ChannelModel{}).Where("id = ?", "b").Update("price_version", 2).Error
} else {
err = db.Model(&model.ChannelModelPriceTier{}).Where("id = ?", "tier-b").Update("price_version", 2).Error
}
if err != nil {
t.Fatal(err)
}
if err := svc.repo.UpdateChannelModelSalePrices("channel", items); !errors.Is(err, repository.ErrChannelModelPriceConflict) {
t.Fatalf("error=%v", err)
}
item, err := svc.repo.ChannelModelByID("channel", "a")
if err != nil {
t.Fatal(err)
}
if item.UnitPriceMicrocredits != 9 || item.PriceVersion != 1 || item.PriceTiers[0].UnitPriceMicrocredits != 9 || item.PriceTiers[0].PriceVersion != 1 {
t.Fatal("transaction not rolled back")
}
})
}
}