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")
}
})
}
}
@@ -81,6 +81,86 @@ func TestChannelCostHTTPAuthorizationAndPublicProjection(t *testing.T) {
t.Fatalf("admin cost missing: %d %s", w.Code, w.Body.String()) t.Fatalf("admin cost missing: %d %s", w.Code, w.Body.String())
} }
} }
t.Run("batch repricing HTTP contract", func(t *testing.T) {
var item model.ChannelModel
if err := db.First(&item, "channel_id = ?", channel.ID).Error; err != nil {
t.Fatal(err)
}
path := "/api/admin/channels/channel/models/batch-reprice"
makePayload := func(price any) []byte {
t.Helper()
var latest model.ChannelModel
var tier model.ChannelModelPriceTier
if err := db.First(&latest, "id = ?", item.ID).Error; err != nil {
t.Fatal(err)
}
if err := db.First(&tier, "channel_model_id = ?", item.ID).Error; err != nil {
t.Fatal(err)
}
payload, err := json.Marshal(map[string]any{"models": []any{map[string]any{
"modelId": item.ID, "priceVersion": latest.PriceVersion,
"priceTiers": []any{map[string]any{"id": tier.ID, "priceVersion": tier.PriceVersion,
"prices": map[string]any{"unitPriceMicrocredits": price}}},
}}})
if err != nil {
t.Fatal(err)
}
return payload
}
payload := makePayload(170_000)
for _, role := range []string{"", "user"} {
w := call(role, http.MethodPost, path, payload)
want := http.StatusForbidden
if role == "" {
want = http.StatusUnauthorized
}
if w.Code != want {
t.Fatalf("role %q: %d %s", role, w.Code, w.Body.String())
}
}
for _, invalid := range []string{
`{}`, `{"models":[]}`, `{"models":null}`,
`{"models":[{"modelId":"` + item.ID + `","priceVersion":1,"priceTiers":[]}]}`,
string(makePayload(nil)), string(makePayload(-1)), string(makePayload(1.5)),
} {
w := call("admin", http.MethodPost, path, []byte(invalid))
if w.Code != http.StatusBadRequest {
t.Fatalf("invalid payload: %d %s", w.Code, w.Body.String())
}
}
w := call("admin", http.MethodPost, path, payload)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"updated":1`) {
t.Fatalf("reprice: %d %s", w.Code, w.Body.String())
}
var tier model.ChannelModelPriceTier
if err := db.First(&tier, "channel_model_id = ?", item.ID).Error; err != nil {
t.Fatal(err)
}
if tier.UnitPriceMicrocredits != 170_000 || tier.CostPricing.UnitPriceMicrocredits != 123_456 {
t.Fatalf("unexpected stored tier: %+v", tier)
}
w = call("admin", http.MethodPost, path, payload)
if w.Code != http.StatusBadRequest {
t.Fatalf("stale version: %d %s", w.Code, w.Body.String())
}
// Explicit zero is a valid final sale price, not a missing field.
payload = makePayload(0)
w = call("admin", http.MethodPost, path, payload)
if w.Code != http.StatusOK {
t.Fatalf("zero target: %d %s", w.Code, w.Body.String())
}
if err := db.First(&tier, "channel_model_id = ?", item.ID).Error; err != nil {
t.Fatal(err)
}
if tier.UnitPriceMicrocredits != 0 || tier.CostPricing.UnitPriceMicrocredits != 123_456 {
t.Fatalf("zero price not preserved: %+v", tier)
}
payload = makePayload(170_000)
w = call("admin", http.MethodPost, path, payload)
if w.Code != http.StatusOK {
t.Fatalf("restore target: %d %s", w.Code, w.Body.String())
}
})
for _, path := range []string{"/api/model-catalog", "/api/model-catalog/available", "/api/model-catalog/quote"} { for _, path := range []string{"/api/model-catalog", "/api/model-catalog/available", "/api/model-catalog/quote"} {
method := http.MethodPost method := http.MethodPost
payload := []byte(`{"capability":"text"}`) payload := []byte(`{"capability":"text"}`)
+21
View File
@@ -296,6 +296,27 @@ func RegisterFinanceRoutes(r *gin.RouterGroup, svc *service.Service) {
} }
ok(c, gin.H{"deleted": deleted}) ok(c, gin.H{"deleted": deleted})
}) })
r.POST("/admin/channels/:id/models/batch-reprice", func(c *gin.Context) {
user, err := currentUser(c, svc)
if err != nil {
failService(c, err)
return
}
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 2<<20)
var req struct {
Models []service.ChannelModelRepriceRequest `json:"models" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
fail(c, http.StatusBadRequest, err)
return
}
updated, err := svc.RepriceAdminChannelModels(user, c.Param("id"), req.Models)
if err != nil {
failService(c, err)
return
}
ok(c, gin.H{"updated": updated})
})
r.PATCH("/admin/channels/:id/models/:modelId", func(c *gin.Context) { r.PATCH("/admin/channels/:id/models/:modelId", func(c *gin.Context) {
saveChannelModel(c, svc, c.Param("modelId")) saveChannelModel(c, svc, c.Param("modelId"))
}) })
@@ -1,6 +1,8 @@
package handler package handler
import ( import (
"net/http"
"net/http/httptest"
"testing" "testing"
"infinite-canvas/backend/internal/service" "infinite-canvas/backend/internal/service"
@@ -21,3 +23,24 @@ func TestFinanceRoutesExposeChannelModelBatchDelete(t *testing.T) {
} }
t.Fatalf("route %s is not registered", wanted) t.Fatalf("route %s is not registered", wanted)
} }
func TestFinanceRoutesExposeChannelModelBatchReprice(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
RegisterFinanceRoutes(router.Group("/api"), &service.Service{})
const wanted = "POST /api/admin/channels/:id/models/batch-reprice"
found := false
for _, route := range router.Routes() {
if route.Method+" "+route.Path == wanted {
found = true
}
}
if !found {
t.Fatalf("route %s is not registered", wanted)
}
response := httptest.NewRecorder()
router.ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/api/admin/channels/channel/models/batch-reprice", nil))
if response.Code != http.StatusUnauthorized {
t.Fatalf("unauthenticated status = %d, body = %s", response.Code, response.Body.String())
}
}
+72
View File
@@ -721,6 +721,78 @@ paths:
$ref: "#/components/responses/Error" $ref: "#/components/responses/Error"
"404": "404":
$ref: "#/components/responses/Error" $ref: "#/components/responses/Error"
/admin/channels/{id}/models/batch-reprice:
post:
tags: [admin]
summary: 批量保存系统渠道模型最终销售价
description: |
仅管理员可用。接收浏览器计算或手工修改后的最终售价及原始价格版本,不在服务端重算。
前端按成本/(1−利润率)计算,支持0–6位积分小数四舍五入及重新计算。
必须提交所选模型全部已启用规格,不改变成本、规格启停或模型启停。
当前计费方式的价格字段必须完整且不能多余,价格为非负整数微积分,null无效。
缺少成本/售价配置、重复或失效 ID、越权、超限价格、过期版本或并发版本冲突均整批拒绝。
parameters:
- in: path
name: id
required: true
schema: { type: string }
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [models]
properties:
models:
type: array
minItems: 1
maxItems: 100
items:
type: object
required: [modelId, priceVersion, priceTiers]
properties:
modelId: { type: string, minLength: 1 }
priceVersion: { type: integer, format: int64, minimum: 1 }
priceTiers:
type: array
minItems: 1
items:
type: object
required: [id, priceVersion, prices]
properties:
id: { type: string, minLength: 1 }
priceVersion: { type: integer, format: int64, minimum: 1 }
prices:
type: object
additionalProperties: false
description: 按次/秒仅unit字段;文本Token为输入、输出、缓存;视频Token仅输出。必须完整提交对应字段。
properties:
unitPriceMicrocredits: { type: integer, format: int64, minimum: 0, maximum: 9007199254740991 }
inputTokenPriceMicrocredits: { type: integer, format: int64, minimum: 0, maximum: 1000000000000 }
outputTokenPriceMicrocredits: { type: integer, format: int64, minimum: 0, maximum: 1000000000000 }
cachedTokenPriceMicrocredits: { type: integer, format: int64, minimum: 0, maximum: 1000000000000 }
example: { models: [{ modelId: MODEL001, priceVersion: 4, priceTiers: [{ id: TIER001, priceVersion: 8, prices: { unitPriceMicrocredits: 1370000 } }] }] }
responses:
"200":
description: 更新成功,data.updated 为更新的模型数量
content:
application/json:
schema:
type: object
properties:
code: { type: integer, enum: [0] }
msg: { type: string }
data:
type: object
properties:
updated: { type: integer }
"400":
$ref: "#/components/responses/Error"
"401":
$ref: "#/components/responses/Error"
"403":
$ref: "#/components/responses/Error"
/admin/agent-lessons: /admin/agent-lessons:
get: get:
tags: [admin] tags: [admin]
@@ -0,0 +1,56 @@
package repository
import (
"errors"
"infinite-canvas/backend/internal/model"
"gorm.io/gorm"
)
var ErrChannelModelPriceConflict = errors.New("channel model prices changed concurrently")
// UpdateChannelModelSalePrices commits the entire selection or nothing. Only
// pricing columns are written; version checks protect the cost snapshot used by app.
func (r *Repository) UpdateChannelModelSalePrices(channelID string, items []model.ChannelModel) error {
return r.db.Transaction(func(tx *gorm.DB) error {
for _, item := range items {
result := tx.Model(&model.ChannelModel{}).
Where("id = ? AND channel_id = ? AND price_version = ? AND updated_at = ?", item.ID, channelID, item.PriceVersion, item.UpdatedAt).
Updates(map[string]any{
"unit_price_microcredits": item.UnitPriceMicrocredits,
"input_token_price_microcredits": item.InputTokenPriceMicrocredits,
"output_token_price_microcredits": item.OutputTokenPriceMicrocredits,
"cached_token_price_microcredits": item.CachedTokenPriceMicrocredits,
"price_version": item.PriceVersion + 1,
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != 1 {
return ErrChannelModelPriceConflict
}
for _, tier := range item.PriceTiers {
if !tier.Enabled {
continue
}
result = tx.Model(&model.ChannelModelPriceTier{}).
Where("id = ? AND channel_model_id = ? AND price_version = ? AND updated_at = ?", tier.ID, item.ID, tier.PriceVersion, tier.UpdatedAt).
Updates(map[string]any{
"unit_price_microcredits": tier.UnitPriceMicrocredits,
"input_token_price_microcredits": tier.InputTokenPriceMicrocredits,
"output_token_price_microcredits": tier.OutputTokenPriceMicrocredits,
"cached_token_price_microcredits": tier.CachedTokenPriceMicrocredits,
"price_version": tier.PriceVersion + 1,
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != 1 {
return ErrChannelModelPriceConflict
}
}
}
return nil
})
}
@@ -98,6 +98,7 @@ type (
ChannelModelCatalogOptions = app.ChannelModelCatalogOptions ChannelModelCatalogOptions = app.ChannelModelCatalogOptions
ChannelModelPriceTierRequest = app.ChannelModelPriceTierRequest ChannelModelPriceTierRequest = app.ChannelModelPriceTierRequest
ChannelModelRequest = app.ChannelModelRequest ChannelModelRequest = app.ChannelModelRequest
ChannelModelRepriceRequest = app.ChannelModelRepriceRequest
ChannelModelQuoteRequest = app.ChannelModelQuoteRequest ChannelModelQuoteRequest = app.ChannelModelQuoteRequest
ChannelModelSortRequest = app.ChannelModelSortRequest ChannelModelSortRequest = app.ChannelModelSortRequest
ChannelModelsRequest = app.ChannelModelsRequest ChannelModelsRequest = app.ChannelModelsRequest
+1
View File
@@ -20,6 +20,7 @@ description: 从产品功能快速定位主要代码入口
| 视频 Token 报价与结算 | `web/src/pages/admin/components/channel-model-price-tier-fields.tsx`、`web/src/components/model-picker.tsx` | `lib/model-pricing.ts`、`services/api/logical-models.ts`;后端 `app/video_token_billing.go` 管像素与时长估算,`app/logical_model_quote.go` 复用准入与价格规则,`app/analytics.go` 提取可信用量,`repository/finance.go` 按订单快照退补差额 | | 视频 Token 报价与结算 | `web/src/pages/admin/components/channel-model-price-tier-fields.tsx`、`web/src/components/model-picker.tsx` | `lib/model-pricing.ts`、`services/api/logical-models.ts`;后端 `app/video_token_billing.go` 管像素与时长估算,`app/logical_model_quote.go` 复用准入与价格规则,`app/analytics.go` 提取可信用量,`repository/finance.go` 按订单快照退补差额 |
| 任务中心 | `web/src/pages/tasks/` | `services/api/task-center.ts`、`services/api/task-text-stream.ts` | | 任务中心 | `web/src/pages/tasks/` | `services/api/task-center.ts`、`services/api/task-text-stream.ts` |
| 产品模型与渠道选择 | `web/src/components/model-picker.tsx`、`pages/admin/components/channel-model-editor.tsx` | `lib/model-picker-groups.ts` 只构建跨渠道展示分组;`lib/user-session.ts` 透传渠道展示名;`components/model-logo.tsx` 按需加载原色图标;后端 `app/channel_models.go` 校验写入,`app/model_catalog.go` 发布目录,schema v24 增加 `channel_label` | | 产品模型与渠道选择 | `web/src/components/model-picker.tsx`、`pages/admin/components/channel-model-editor.tsx` | `lib/model-picker-groups.ts` 只构建跨渠道展示分组;`lib/user-session.ts` 透传渠道展示名;`components/model-logo.tsx` 按需加载原色图标;后端 `app/channel_models.go` 校验写入,`app/model_catalog.go` 发布目录,schema v24 增加 `channel_label` |
| 系统渠道规格利润率与批量调价 | `web/src/pages/admin/components/channel-model-manager.tsx`、`channel-model-reprice-dialog.tsx` | 弹窗按模型分组展示多规格、重新计算与逐项编辑;`channel-model-cost-summary.tsx` 展示价格,`channel-model-pricing.ts` 精确四舍五入并组装最终售价;后端 `handler/finance.go` 接收批量请求,`app/channel_model_pricing.go` 校验权限、最终价格及快照版本,`repository/channel_model_pricing.go` 事务保存及版本冲突检测 |
| 我的素材 | `web/src/pages/assets/` | `stores/use-asset-store.ts`、`services/image-storage.ts` | | 我的素材 | `web/src/pages/assets/` | `stores/use-asset-store.ts`、`services/image-storage.ts` |
| 技能库 | `web/src/pages/skills/` | `services/api/skills.ts`、`services/skill-runtime.ts`、`components/skills/skill-runtime-picker.tsx` | | 技能库 | `web/src/pages/skills/` | `services/api/skills.ts`、`services/skill-runtime.ts`、`components/skills/skill-runtime-picker.tsx` |
| 画布列表 | `web/src/pages/canvas/index.tsx` | `stores/canvas/use-canvas-store.ts` | | 画布列表 | `web/src/pages/canvas/index.tsx` | `stores/canvas/use-canvas-store.ts` |
+26
View File
@@ -15,6 +15,32 @@ description: 信封、错误码与 OpenAPI
## 模型规格报价 ## 模型规格报价
### 管理员批量调价
`POST /admin/channels/:id/models/batch-reprice` 接收最终售价及原始价格版本,返回 `{ code: 0, data: { updated: number }, msg }`。仅管理员可调用;单批 1–100 个模型,空 ID、重复 ID 或不属于当前系统渠道的记录整批拒绝。请求结构如下(价格为整数微积分):
```json
{
"models": [{
"modelId": "MODEL001",
"priceVersion": 4,
"priceTiers": [{
"id": "TIER001",
"priceVersion": 8,
"prices": { "unitPriceMicrocredits": 1370000 }
}]
}]
}
```
弹窗一行一个模型,列为模型展示名、渠道展示名(上游模型 ID)、规格、成本价和销售价输入框;多个规格在模型行内纵向对齐。利润率沿用财务口径:`(销售价 − 成本价) / 销售价 × 100%`,并非成本加价率。目标利润率为 0–99.99%,最多两位小数;售价按 `成本价 / (1 − 目标利润率)` 计算,按所选 0–6 位积分小数四舍五入(默认 2 位)。成本 1 积分、目标 20%、保留 2 位时售价为 1.25 积分。修改比例或精度实时计算,也可点击「重新计算」按当前参数覆盖全部手动售价;随后允许逐规格手动修改,保存以最终输入值为准。舍入和手动修改可能使实际利润率偏离目标;售价为零时利润率显示 `—`。
每个模型必须提交全部已启用规格(模型本身可停用),且每个规格只提交当前计费方式需要的完整 `prices` 字段:按次/秒为 `unitPriceMicrocredits`;文本 Token 为 `inputTokenPriceMicrocredits`、`outputTokenPriceMicrocredits`、`cachedTokenPriceMicrocredits`;视频 Token 仅为 `outputTokenPriceMicrocredits`。缺失、多余、重复、停用规格、null、负数或非整数价格均拒绝。成本和售价配置必须已保存;成本及 Token 售价上限为 1,000,000 积分,按次/秒售价不得超出 JavaScript 安全整数微积分范围。
服务端校验最终售价和模型/规格版本,不重新计算覆盖手工价格。价格快照过期必须刷新后重试。所有模型与价格档在单事务中更新,增加价格版本、失效目录缓存;无效配置、归属错误、超限或并发版本冲突均不部分提交。不改变成本、规格、计费方式和启停状态;未启用规格保持原值。此接口不新增数据表或迁移,不追溯修改已下单的计费快照。
### 用户模型目录
`GET /model-catalog` 固定返回 `source=system`,直接查询所有启用系统渠道下的渠道模型,目录数据在 `channels[].models[]`,`models` 固定为空数组。不受逻辑模型开关影响,不反查逻辑模型或供应线路。每项的 `modelKey` 是调用标识,`displayName` 是模型展示名,`icon` 是模型图标键,`channelLabel` 是可选的渠道展示名(默认空字符串)。后台渠道模型创建与更新请求使用相同的 `channelLabel` 字段;服务端去除首尾空白并限制为 80 字,空字符串表示使用所属渠道名称。 `GET /model-catalog` 固定返回 `source=system`,直接查询所有启用系统渠道下的渠道模型,目录数据在 `channels[].models[]`,`models` 固定为空数组。不受逻辑模型开关影响,不反查逻辑模型或供应线路。每项的 `modelKey` 是调用标识,`displayName` 是模型展示名,`icon` 是模型图标键,`channelLabel` 是可选的渠道展示名(默认空字符串)。后台渠道模型创建与更新请求使用相同的 `channelLabel` 字段;服务端去除首尾空白并限制为 80 字,空字符串表示使用所属渠道名称。
渠道模型创建、更新、管理列表、公开目录和会话模型价格信息均使用 `description` 表达模型描述。服务端去除首尾空白后限制为 500 字,允许空字符串清除描述;无管理员权限或超长内容拒绝保存。创作端二级选项在悬浮、聚焦或选中时展示该字段,长内容可悬浮查看完整文本;未填写的系统模型不按名称生成宣传文案。描述区域保留固定行高,悬浮不改变菜单项尺寸。 渠道模型创建、更新、管理列表、公开目录和会话模型价格信息均使用 `description` 表达模型描述。服务端去除首尾空白后限制为 500 字,允许空字符串清除描述;无管理员权限或超长内容拒绝保存。创作端二级选项在悬浮、聚焦或选中时展示该字段,长内容可悬浮查看完整文本;未填写的系统模型不按名称生成宣传文案。描述区域保留固定行高,悬浮不改变菜单项尺寸。
+2
View File
@@ -132,6 +132,8 @@ description: 当前项目已实现的核心工作台能力与使用入口
渠道模型价格档支持管理员单独配置积分成本价;后台请求记录同时显示销售金额和成本金额。成本按下单快照计算,不追随之后改价;普通用户页面及接口不返回成本字段。 渠道模型价格档支持管理员单独配置积分成本价;后台请求记录同时显示销售金额和成本金额。成本按下单快照计算,不追随之后改价;普通用户页面及接口不返回成本字段。
系统渠道模型列表按规格显示成本价 / 销售价及利润率;文本输入、输出、缓存 Token 分开呈现。勾选多个模型可统一调价:弹窗一行一个模型,展示模型名、渠道展示名(上游 ID)、规格、成本与可编辑售价,多规格在模型行内纵向对齐。输入目标利润率并选择保留 0–6 位小数(默认 2 位),按 `成本 / (1 − 利润率)` 四舍五入实时计算;也可点击「重新计算」覆盖手动值,再逐项修改。保存最终输入售价,服务端校验价格与版本并原子保存,不改变成本或启停状态。
后台系统渠道及渠道模型列表通过“设置排序”打开完整列表,支持拖动、上移和下移,点击“保存排序”统一生效;取消不会修改线上顺序。渠道行的“更多”菜单支持复制渠道,复制会生成独立的渠道、模型和价格档,并保留原连接配置与凭证。界面不显示内部排序数字,排序不受筛选及分页影响;并发修改会提示重新加载,避免覆盖其他管理员的调整。系统渠道模式下,用户端模型选择器沿用同一渠道及模型顺序。模型排序独立保存,不改变价格、能力版本或启用状态。 后台系统渠道及渠道模型列表通过“设置排序”打开完整列表,支持拖动、上移和下移,点击“保存排序”统一生效;取消不会修改线上顺序。渠道行的“更多”菜单支持复制渠道,复制会生成独立的渠道、模型和价格档,并保留原连接配置与凭证。界面不显示内部排序数字,排序不受筛选及分页影响;并发修改会提示重新加载,避免覆盖其他管理员的调整。系统渠道模式下,用户端模型选择器沿用同一渠道及模型顺序。模型排序独立保存,不改变价格、能力版本或启用状态。
系统渠道可设置可选的前台别名;为空时显示渠道名,设置后公开目录和用户会话只返回别名,后台仍保留原名。别名最长 80 字,支持后台搜索。前台模型目录模式保留其独立配置。 系统渠道可设置可选的前台别名;为空时显示渠道名,设置后公开目录和用户会话只返回别名,后台仍保留原名。别名最长 80 字,支持后台搜索。前台模型目录模式保留其独立配置。
+15 -1
View File
@@ -5,10 +5,24 @@ description: 当前项目已实现功能的手工验证清单与未确认边界
# 待测试 # 待测试
## 画布 Agent 入口拖动
- [x] 右下角 Agent 图标支持鼠标和触屏拖动,使用指针捕获与 6px 阈值区分点击和拖动;拖动结束不打开面板,中断或失去捕获时结束手势,不传播到画布。
- [x] 位置作为独立本机 UI 偏好保存,重新打开画布或收起面板后恢复;限制在可视范围,窗口缩小时自动回位。方向键移动 10px,Shift + 方向键移动 40px,保留键盘点击打开。
- [x] 前端类型检查与 20 项专项/相邻测试通过,覆盖真实生产 hook 的点击、拖动、取消、丢失捕获、多指隔离、持久化和键盘/缩屏行为。
- [ ] 本地浏览器访问跳转登录,未完成登录后真实画布验收;需验证鼠标/触屏拖动不带动画布、不误开面板、刷新恢复,以及明暗主题和窄屏边缘显示。专项测试不替代浏览器验收。
## 系统模型同页渠道与模型管理 ## 系统模型同页渠道与模型管理
- [x] 规格列更新为成本价 / 销售价 / 利润率,按次、按秒、文本输入/输出/缓存和视频 Token 分别展示;缺失配置、零售价利润率显示 `—`,亏损显示负利润率。
- [x] 支持跨页多选最多 100 个模型统一调价。弹窗打开即展示模型,一行一个模型,列为模型展示名、渠道展示名(上游模型 ID)、规格、成本及售价输入框;多规格在模型行内纵向对齐,不拆成多个模型行,规格上游覆盖单独标注。
- [x] 调价弹窗模型价格列表进一步压缩行高:上下留白 6px、行内文字间距 2px,常规尺寸售价输入框与利润率同排,扩大售价列并保持规格对齐。弹窗居中固定高度 900px(不超过视窗高度减 48px),列表填充剩余空间并内部滚动,短屏允许内容区滚动;紧凑效果与滚动仍待真实页面视觉验收。
- [x] 目标利润率和 0–6 位小数(默认 2 位)实时重算,旁边增加「重新计算」按钮,按当前参数对所有规格精确四舍五入。重算覆盖手动值并明确提示;之后可逐项修改,提交最终输入售价。服务端强校验归属、成本、价格和模型/规格版本,事务保存,冲突整批回滚,不改成本或启停状态。
- [x] 前端类型检查、69 项专项及相邻回归测试通过,包含真实表格组件渲染、多规格单模型行、输入框最终值、精度和手动价重算。后端调价与相邻成本/删除测试通过,覆盖真实 HTTP 登录/管理员权限、整数/null/零售价、最终手工售价保存、过期版本拒绝及并发冲突事务回滚。验证使用内存 SQLite,未进行 PostgreSQL 或浏览器验收。
- [ ] 待登录后的真实页面验收:多选/跨页/切换渠道,修改比例/精度后实时计算,手动编辑后点击「重新计算」恢复所有规格价格,逐项编辑保存、成功刷新与错误保留;覆盖未配置成本、零成本、Token 多计费项、长名称、明暗主题、窄屏、表头与规格对齐及滚动。不操作现有环境真实价格数据。
- [x] 样式细化:渠道列表改为留白分组与中性选中描边,数量与启停状态分别呈现;模型头部说明成本口径,规格与成本分行,操作列固定在右侧,超过三个规格可展开查看。 - [x] 样式细化:渠道列表改为留白分组与中性选中描边,数量与启停状态分别呈现;模型头部说明成本口径,规格与成本分行,操作列固定在右侧,超过三个规格可展开查看。
- [x] “规格价格”改为“规格成本价”,仅读取管理员接口已有的 `priceTiers[].costPricing`,不再显示用户售价。支持按次、按秒、文本输入/输出/缓存 Token 和视频 Token;未配置成本明确提示,显式零成本正常显示,异常或缺失数值显示 `—`。不修改后端、计费或用户售价。 - [x] 规格成本读取管理员接口已有的 `priceTiers[].costPricing`,与销售价分开呈现;未配置成本明确提示,显式零成本正常显示,异常或缺失数值显示 `—`。
- [x] 本轮 49 项专项与相邻回归测试、前端类型检查通过;新增成本摘要真实组件渲染测试,覆盖成本/售价隔离、零成本、缺失/异常值、Token 单位、规格选择器与展开内容。样式细化仍需在登录后的真实页面检查明暗主题、长名称、多规格行高、横向滚动与固定操作列。 - [x] 本轮 49 项专项与相邻回归测试、前端类型检查通过;新增成本摘要真实组件渲染测试,覆盖成本/售价隔离、零成本、缺失/异常值、Token 单位、规格选择器与展开内容。样式细化仍需在登录后的真实页面检查明暗主题、长名称、多规格行高、横向滚动与固定操作列。
- [x] 系统渠道入口改为同页工作区:桌面左侧约 20% 展示渠道搜索、状态筛选、新增和排序,右侧约 80% 展示当前渠道信息与模型管理;移除模型管理的二级页面和返回入口,窄屏改为上下布局。 - [x] 系统渠道入口改为同页工作区:桌面左侧约 20% 展示渠道搜索、状态筛选、新增和排序,右侧约 80% 展示当前渠道信息与模型管理;移除模型管理的二级页面和返回入口,窄屏改为上下布局。
- [x] 保留渠道编辑、复制、启停、删除及模型拉取、选择导入、编辑、排序和批量删除。新增或复制后自动选中渠道;切换渠道时隔离模型列表、筛选、勾选和编辑状态,渠道与模型加载失败显示重试入口。服务端仍使用原分页接口,左栏分批读取全部渠道,避免默认仅展示前 20 个。 - [x] 保留渠道编辑、复制、启停、删除及模型拉取、选择导入、编辑、排序和批量删除。新增或复制后自动选中渠道;切换渠道时隔离模型列表、筛选、勾选和编辑状态,渠道与模型加载失败显示重试入口。服务端仍使用原分页接口,左栏分批读取全部渠道,避免默认仅展示前 20 个。
@@ -32,6 +32,7 @@ import { AgentChatComposer, AgentChatMessage, AgentPlanBar, AgentQuestionBar, Ag
import { CanvasAgentSkillLibraryModal } from "./canvas-agent-skill-library-modal"; import { CanvasAgentSkillLibraryModal } from "./canvas-agent-skill-library-modal";
import { CanvasCloudAgentSettings, agentPermissionLabel, agentPermissionMenuItems, agentPermissionVisual, type AgentContextKey } from "./canvas-cloud-agent-settings"; import { CanvasCloudAgentSettings, agentPermissionLabel, agentPermissionMenuItems, agentPermissionVisual, type AgentContextKey } from "./canvas-cloud-agent-settings";
import { useAgentPanelLayout } from "./use-agent-panel-layout"; import { useAgentPanelLayout } from "./use-agent-panel-layout";
import { useAgentLauncherPosition } from "./use-agent-launcher-position";
import { AgentWelcome } from "./canvas-agent-welcome"; import { AgentWelcome } from "./canvas-agent-welcome";
import "./canvas-cloud-agent.css"; import "./canvas-cloud-agent.css";
@@ -797,17 +798,18 @@ export function CanvasCloudAgentPanel({ canvasId, domainProjectId, nodeCount, re
} }
function AgentLauncher({ theme, statusColor, approvalPending, reducedMotion, onOpen }: { theme: CanvasTheme; statusColor: string; approvalPending: boolean; reducedMotion: boolean; onOpen: () => void }) { function AgentLauncher({ theme, statusColor, approvalPending, reducedMotion, onOpen }: { theme: CanvasTheme; statusColor: string; approvalPending: boolean; reducedMotion: boolean; onOpen: () => void }) {
const { position, dragging, handlers } = useAgentLauncherPosition(onOpen);
return ( return (
<motion.button <motion.button
type="button" type="button"
aria-label="打开云端 Agent" aria-label="打开云端 Agent"
title={approvalPending ? "Agent 等待你的审批" : "打开 Agent 助手"} title={`${approvalPending ? "Agent 等待你的审批" : "打开 Agent 助手"} · 拖动可调整位置,聚焦后可用方向键移动`}
className="canvas-agent-launcher fixed bottom-5 right-5 z-[var(--z-modal-overlay)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-current/35" className={cn("canvas-agent-launcher fixed z-[var(--z-modal-overlay)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-current/35", dragging && "is-dragging")}
style={{ color: theme.node.text, "--canvas-agent-launcher-shadow": theme.spatial.shadow } as CSSProperties} style={{ ...position, color: theme.node.text, "--canvas-agent-launcher-shadow": theme.spatial.shadow } as CSSProperties}
data-canvas-no-zoom data-canvas-no-zoom
onClick={onOpen} {...handlers}
whileHover={reducedMotion ? undefined : { y: -3, scale: 1.035 }} whileHover={reducedMotion || dragging ? undefined : { scale: 1.035 }}
whileTap={reducedMotion ? undefined : { scale: 0.96 }} whileTap={reducedMotion || dragging ? undefined : { scale: 0.96 }}
transition={{ duration: reducedMotion ? 0 : 0.18 }} transition={{ duration: reducedMotion ? 0 : 0.18 }}
> >
<FluidOrb size={62} color="#7164f6" /> <FluidOrb size={62} color="#7164f6" />
@@ -522,10 +522,16 @@
background: transparent; background: transparent;
padding: 7px; padding: 7px;
box-shadow: 0 16px 38px color-mix(in srgb, var(--canvas-agent-launcher-shadow) 64%, transparent); box-shadow: 0 16px 38px color-mix(in srgb, var(--canvas-agent-launcher-shadow) 64%, transparent);
cursor: pointer; cursor: grab;
touch-action: none;
user-select: none;
isolation: isolate; isolation: isolate;
} }
.canvas-agent-launcher.is-dragging {
cursor: grabbing;
}
.canvas-agent-launcher::before { .canvas-agent-launcher::before {
position: absolute; position: absolute;
inset: 3px; inset: 3px;
@@ -0,0 +1,99 @@
import { useEffect, useRef, useState, type KeyboardEvent, type MouseEvent, type PointerEvent } from "react";
import { AGENT_LAUNCHER_POSITION_KEY, clampAgentLauncherPosition, moveAgentLauncher, restoreAgentLauncherPosition, type AgentLauncherGesture, type AgentLauncherPosition } from "@/lib/canvas/agent-launcher-position";
const viewport = () => ({ width: window.innerWidth, height: window.innerHeight });
export function useAgentLauncherPosition(onOpen: () => void) {
const [position, setPosition] = useState(() => {
try {
return restoreAgentLauncherPosition(localStorage.getItem(AGENT_LAUNCHER_POSITION_KEY), viewport());
} catch (error) {
console.warn("Agent 入口位置无法读取", error);
return restoreAgentLauncherPosition(null, viewport());
}
});
const [dragging, setDragging] = useState(false);
const gestureRef = useRef<(AgentLauncherGesture & { pointerId: number }) | null>(null);
const suppressClick = useRef(false);
const save = (next: AgentLauncherPosition) => {
try {
localStorage.setItem(AGENT_LAUNCHER_POSITION_KEY, JSON.stringify(next));
} catch (error) {
console.warn("Agent 入口位置无法保存", error);
}
};
useEffect(() => {
const resize = () => setPosition((current) => clampAgentLauncherPosition(current, viewport()));
window.addEventListener("resize", resize);
return () => window.removeEventListener("resize", resize);
}, []);
const onPointerDown = (event: PointerEvent<HTMLButtonElement>) => {
event.stopPropagation();
if (event.button !== 0 || !event.isPrimary || gestureRef.current) return;
suppressClick.current = false;
gestureRef.current = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, start: position, dragged: false };
event.currentTarget.setPointerCapture(event.pointerId);
};
const onPointerMove = (event: PointerEvent<HTMLButtonElement>) => {
event.stopPropagation();
const gesture = gestureRef.current;
if (!gesture || gesture.pointerId !== event.pointerId) return;
const next = moveAgentLauncher(gesture, event.clientX, event.clientY, viewport());
gesture.dragged = next.dragged;
if (!next.dragged) return;
event.preventDefault();
suppressClick.current = true;
setDragging(true);
setPosition(next.position);
};
const finish = (event: PointerEvent<HTMLButtonElement>, cancelled: boolean) => {
event.stopPropagation();
const gesture = gestureRef.current;
if (!gesture || gesture.pointerId !== event.pointerId) return;
gestureRef.current = null;
const next = cancelled ? { dragged: gesture.dragged, position: clampAgentLauncherPosition(position, viewport()) } : moveAgentLauncher(gesture, event.clientX, event.clientY, viewport());
suppressClick.current = cancelled || next.dragged;
setDragging(false);
setPosition(next.position);
if (next.dragged) save(next.position);
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
};
const onClick = (event: MouseEvent<HTMLButtonElement>) => {
event.stopPropagation();
// Keyboard/assistive activation remains available after a drag or cancellation.
if (event.detail !== 0 && suppressClick.current) {
event.preventDefault();
return;
}
onOpen();
};
const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {
event.stopPropagation();
const delta = event.shiftKey ? 40 : 10;
const offsets: Record<string, [number, number]> = { ArrowLeft: [delta, 0], ArrowRight: [-delta, 0], ArrowUp: [0, delta], ArrowDown: [0, -delta] };
const offset = offsets[event.key];
if (!offset) return;
event.preventDefault();
const next = clampAgentLauncherPosition({ right: position.right + offset[0], bottom: position.bottom + offset[1] }, viewport());
setPosition(next);
save(next);
};
return {
position,
dragging,
handlers: {
onPointerDown,
onPointerMove,
onPointerUp: (event: PointerEvent<HTMLButtonElement>) => finish(event, false),
onPointerCancel: (event: PointerEvent<HTMLButtonElement>) => finish(event, true),
onLostPointerCapture: (event: PointerEvent<HTMLButtonElement>) => finish(event, true),
onClick,
onKeyDown,
onDoubleClick: (event: MouseEvent<HTMLButtonElement>) => event.stopPropagation(),
},
};
}
@@ -0,0 +1,38 @@
export type AgentLauncherPosition = { right: number; bottom: number };
export type AgentLauncherViewport = { width: number; height: number };
export type AgentLauncherGesture = { x: number; y: number; start: AgentLauncherPosition; dragged: boolean };
export const AGENT_LAUNCHER_POSITION_KEY = "canvas:agent-launcher-position:v1";
export const AGENT_LAUNCHER_SIZE = 76;
const MARGIN = 20; // Includes the label below the orb.
export function clampAgentLauncherPosition(position: AgentLauncherPosition, viewport: AgentLauncherViewport): AgentLauncherPosition {
const clamp = (value: number, extent: number) => {
const available = Math.max(0, extent - AGENT_LAUNCHER_SIZE);
const margin = Math.min(MARGIN, available / 2);
return Math.min(Math.max(value, margin), available - margin);
};
return { right: clamp(position.right, viewport.width), bottom: clamp(position.bottom, viewport.height) };
}
export function restoreAgentLauncherPosition(raw: string | null, viewport: AgentLauncherViewport): AgentLauncherPosition {
if (raw) {
try {
const value = JSON.parse(raw);
if (value && Number.isFinite(value.right) && Number.isFinite(value.bottom)) return clampAgentLauncherPosition(value, viewport);
} catch {
// Damaged UI preferences must not hide the launcher.
}
}
return clampAgentLauncherPosition({ right: MARGIN, bottom: MARGIN }, viewport);
}
export function moveAgentLauncher(gesture: AgentLauncherGesture, x: number, y: number, viewport: AgentLauncherViewport) {
const dx = x - gesture.x;
const dy = y - gesture.y;
const dragged = gesture.dragged || Math.hypot(dx, dy) >= 6;
return {
dragged,
position: clampAgentLauncherPosition(dragged ? { right: gesture.start.right - dx, bottom: gesture.start.bottom - dy } : gesture.start, viewport),
};
}
@@ -218,6 +218,145 @@
line-height: 1.6; line-height: 1.6;
} }
.admin-model-reprice {
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
gap: 16px;
padding-block: 8px;
}
.admin-model-reprice > :not(.admin-model-reprice-table) {
flex-shrink: 0;
}
.admin-model-reprice-controls {
display: flex;
align-items: flex-end;
flex-wrap: wrap;
gap: 24px;
padding: 24px;
border-radius: var(--admin-radius);
background: var(--admin-surface-2);
}
.admin-model-reprice-field {
display: flex;
flex-direction: column;
gap: 10px;
width: 190px;
max-width: 100%;
}
.admin-model-reprice-field > .ant-input-number {
width: 100%;
}
.admin-model-reprice-formula,
.admin-model-reprice-identity {
display: flex;
flex-direction: column;
gap: 2px;
overflow-wrap: anywhere;
}
.admin-model-reprice-sale {
display: flex;
align-items: center;
gap: 8px;
}
.admin-model-reprice-sale > .ant-input-number {
flex: 0 0 120px;
width: 120px;
}
.admin-model-reprice-sale > small {
white-space: nowrap;
}
.admin-model-reprice-formula {
padding-block: 4px;
gap: 8px;
}
.admin-model-reprice-note,
.admin-model-reprice-formula span,
.admin-model-reprice-identity small,
.admin-model-reprice-sale small,
.admin-model-reprice-list-heading > span {
color: var(--admin-text-muted);
font-size: 12px;
line-height: 1.7;
}
.admin-model-reprice-note {
margin: 0;
}
.admin-model-reprice-list-heading {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 8px;
}
.admin-model-reprice-table {
flex: 1;
min-height: 160px;
overflow: auto;
}
.admin-model-reprice-table .ant-table-tbody > tr > td {
padding: 6px 16px;
vertical-align: middle;
overflow-wrap: anywhere;
}
.admin-model-reprice-table .ant-table-tbody > tr > td.admin-model-reprice-spec-cell {
padding: 0;
}
.admin-model-reprice-spec-row {
display: grid;
grid-template-columns: minmax(0, 140fr) minmax(0, 160fr) minmax(0, 340fr);
align-items: center;
}
.admin-model-reprice-spec-row > div {
min-width: 0;
padding: 6px 16px;
}
.admin-model-reprice-spec-row + .admin-model-reprice-spec-row {
border-top: 1px solid var(--admin-divider);
}
.admin-model-reprice-no-spec {
padding: 6px 16px;
color: var(--admin-text-muted);
}
.admin-model-reprice-modal .ant-modal-container {
display: flex;
flex-direction: column;
height: min(900px, calc(100dvh - 48px));
}
.admin-model-reprice-modal .ant-modal-header,
.admin-model-reprice-modal .ant-modal-footer {
flex-shrink: 0;
}
.admin-model-reprice-modal .ant-modal-body {
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
overflow-y: auto;
}
.admin-model-cost-tier + .admin-model-cost-tier { .admin-model-cost-tier + .admin-model-cost-tier {
margin-top: 8px; margin-top: 8px;
padding-top: 8px; padding-top: 8px;
@@ -1,14 +1,14 @@
import type { ChannelModel, ChannelModelPriceTier } from "@/services/api/wallet"; import type { ChannelModel, ChannelModelPriceTier } from "@/services/api/wallet";
import { AdminStatusBadge } from "./admin-ui"; import { AdminStatusBadge } from "./admin-ui";
import { formatModelMargin, formatModelPrice, modelPriceFields } from "./channel-model-pricing";
export function ChannelModelCostSummary({ item }: { item: ChannelModel }) { export function ChannelModelCostSummary({ item }: { item: ChannelModel }) {
// Cost configuration is independent of the user-facing sale price.
const tiers = item.priceTiers?.filter((tier) => tier.enabled) || []; const tiers = item.priceTiers?.filter((tier) => tier.enabled) || [];
if (!tiers.length) return <AdminStatusBadge label="未配置成本" tone="warning" />; if (!tiers.length) return <AdminStatusBadge label="无已启用规格" tone="warning" />;
const renderTier = (tier: ChannelModelPriceTier) => ( const renderTier = (tier: ChannelModelPriceTier) => (
<div className="admin-model-cost-tier" key={tier.id}> <div className="admin-model-cost-tier" key={tier.id}>
<div className="admin-model-cost-spec">{specificationLabel(tier)}</div> <div className="admin-model-cost-spec">{specificationLabel(tier)}</div>
<div className="admin-model-cost-value">{costLabel(tier, item.capability)}</div> <div className="admin-model-cost-value">{priceLabel(tier, item.capability)}</div>
</div> </div>
); );
return ( return (
@@ -16,7 +16,7 @@ export function ChannelModelCostSummary({ item }: { item: ChannelModel }) {
{tiers.slice(0, 3).map(renderTier)} {tiers.slice(0, 3).map(renderTier)}
{tiers.length > 3 ? ( {tiers.length > 3 ? (
<details className="admin-model-cost-more"> <details className="admin-model-cost-more">
<summary>其余 {tiers.length - 3} 个规格成本</summary> <summary>其余 {tiers.length - 3} 个规格价格</summary>
{tiers.slice(3).map(renderTier)} {tiers.slice(3).map(renderTier)}
</details> </details>
) : null} ) : null}
@@ -24,25 +24,20 @@ export function ChannelModelCostSummary({ item }: { item: ChannelModel }) {
); );
} }
function costLabel(tier: ChannelModelPriceTier, capability: ChannelModel["capability"]) { function priceLabel(tier: ChannelModelPriceTier, capability: ChannelModel["capability"]) {
const cost = tier.costPricing; return modelPriceFields(tier, capability).map(({ key, label, unit }) => {
if (!cost?.configured) return <span className="admin-model-cost-missing">未配置成本</span>; const cost = tier.costPricing?.configured ? tier.costPricing[key] : undefined;
const format = (value: number) => (Number.isFinite(value) && value >= 0 ? (value / 1_000_000).toLocaleString("zh-CN", { maximumFractionDigits: 6 }) : "—"); const sale = tier.priceConfigured ? tier[key] : undefined;
if (tier.billingMode === "token") {
if (capability === "video") return `${format(cost.outputTokenPriceMicrocredits)} 积分 / 百万视频 Token`;
return ( return (
<> <div key={key}>
<span> <span>{label ? `${label} ` : ""}{tier.costPricing?.configured ? formatModelPrice(cost) : "未配置成本"} / {tier.priceConfigured ? formatModelPrice(sale) : "未配置售价"}</span>
输入 {format(cost.inputTokenPriceMicrocredits)} · 输出 {format(cost.outputTokenPriceMicrocredits)} · 缓存 {format(cost.cachedTokenPriceMicrocredits)} <span className="admin-model-cost-unit">积分 / {unit} · 利润率 {formatModelMargin(cost, sale)}</span>
</span> </div>
<span className="admin-model-cost-unit">积分 / 百万 Token</span>
</>
); );
} });
return `${format(cost.unitPriceMicrocredits)} 积分 / ${tier.billingMode === "per_second" ? "秒" : "次"}`;
} }
function specificationLabel(tier: ChannelModelPriceTier) { export function specificationLabel(tier: ChannelModelPriceTier) {
const selector = tier.selector || {}; const selector = tier.selector || {};
const specific = (value?: string) => (value && value !== "*" ? value : ""); const specific = (value?: string) => (value && value !== "*" ? value : "");
const operation = specific(selector.operation); const operation = specific(selector.operation);
@@ -13,12 +13,14 @@ import { ChannelModelEditor } from "./channel-model-editor";
import { AdminBatchBar, AdminDataTable, AdminFilterChip, AdminStatusBadge, AdminTableEmpty } from "./admin-ui"; import { AdminBatchBar, AdminDataTable, AdminFilterChip, AdminStatusBadge, AdminTableEmpty } from "./admin-ui";
import { ChannelOrderDialog } from "./channel-order-dialog"; import { ChannelOrderDialog } from "./channel-order-dialog";
import { ChannelModelCostSummary } from "./channel-model-cost-summary"; import { ChannelModelCostSummary } from "./channel-model-cost-summary";
import { ChannelModelRepriceDialog } from "./channel-model-reprice-dialog";
export function ChannelModelManager({ channel, onChanged }: { channel: ModelChannel; onChanged: () => void | Promise<void> }) { export function ChannelModelManager({ channel, onChanged }: { channel: ModelChannel; onChanged: () => void | Promise<void> }) {
const { message, modal } = App.useApp(); const { message, modal } = App.useApp();
const [items, setItems] = useState<ChannelModel[]>([]); const [items, setItems] = useState<ChannelModel[]>([]);
const [selectedModelIds, setSelectedModelIds] = useState<string[]>([]); const [selectedModelIds, setSelectedModelIds] = useState<string[]>([]);
const [deletingSelected, setDeletingSelected] = useState(false); const [deletingSelected, setDeletingSelected] = useState(false);
const [repriceItems, setRepriceItems] = useState<ChannelModel[]>([]);
const [editing, setEditing] = useState<ChannelModel | null>(null); const [editing, setEditing] = useState<ChannelModel | null>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [loadError, setLoadError] = useState(""); const [loadError, setLoadError] = useState("");
@@ -70,6 +72,7 @@ export function ChannelModelManager({ channel, onChanged }: { channel: ModelChan
setEditing(null); setEditing(null);
setEditorOpen(false); setEditorOpen(false);
setSelectedModelIds([]); setSelectedModelIds([]);
setRepriceItems([]);
resetFetchPreview(); resetFetchPreview();
setKeyword(""); setKeyword("");
setCapability("all"); setCapability("all");
@@ -209,7 +212,7 @@ export function ChannelModelManager({ channel, onChanged }: { channel: ModelChan
<AdminStatusBadge label="待配置" tone="warning" /> <AdminStatusBadge label="待配置" tone="warning" />
), ),
}, },
{ title: "规格成本价", width: 290, render: (_, item) => <ChannelModelCostSummary item={item} /> }, { title: "规格成本价 / 销售价 / 利润率", width: 310, render: (_, item) => <ChannelModelCostSummary item={item} /> },
{ title: "版本", dataIndex: "priceVersion", width: 75, render: (value) => `v${value}` }, { title: "版本", dataIndex: "priceVersion", width: 75, render: (value) => `v${value}` },
{ title: "状态", dataIndex: "enabled", width: 85, render: (enabled) => <AdminStatusBadge label={enabled ? "启用" : "停用"} tone={enabled ? "success" : "neutral"} /> }, { title: "状态", dataIndex: "enabled", width: 85, render: (enabled) => <AdminStatusBadge label={enabled ? "启用" : "停用"} tone={enabled ? "success" : "neutral"} /> },
{ {
@@ -264,7 +267,7 @@ export function ChannelModelManager({ channel, onChanged }: { channel: ModelChan
<h3 className="admin-channel-model-heading"> <h3 className="admin-channel-model-heading">
模型管理 <span className="admin-channel-count">{items.length}</span> 模型管理 <span className="admin-channel-count">{items.length}</span>
</h3> </h3>
<p className="admin-channel-model-hint">规格展示上游成本,不影响用户售价</p> <p className="admin-channel-model-hint">规格展示成本价 / 销售价及利润率,勾选模型可统一调价</p>
</div> </div>
<Space wrap> <Space wrap>
<ChannelOrderDialog <ChannelOrderDialog
@@ -382,6 +385,9 @@ export function ChannelModelManager({ channel, onChanged }: { channel: ModelChan
}} }}
batchActions={ batchActions={
<AdminBatchBar count={selectedModelIds.length} onClear={() => setSelectedModelIds([])}> <AdminBatchBar count={selectedModelIds.length} onClear={() => setSelectedModelIds([])}>
<Button size="small" disabled={deletingSelected || loading || Boolean(loadError)} onClick={() => setRepriceItems(items.filter((item) => selectedModelIds.includes(item.id)))}>
统一调价
</Button>
<Button danger size="small" icon={<Trash2 className="size-3.5" />} loading={deletingSelected} onClick={confirmBatchRemove}> <Button danger size="small" icon={<Trash2 className="size-3.5" />} loading={deletingSelected} onClick={confirmBatchRemove}>
批量删除 批量删除
</Button> </Button>
@@ -419,6 +425,11 @@ export function ChannelModelManager({ channel, onChanged }: { channel: ModelChan
/> />
} }
/> />
{repriceItems.length > 0 && <ChannelModelRepriceDialog channelId={channel.id} channelName={channel.name} items={repriceItems} onClose={() => setRepriceItems([])} onSaved={async () => {
setSelectedModelIds([]);
await reload();
await onChanged();
}} />}
<Modal <Modal
title="选择要导入的模型" title="选择要导入的模型"
open={fetchPreviewOpen} open={fetchPreviewOpen}
@@ -0,0 +1,120 @@
import type { ChannelModel, ChannelModelPriceTier, ChannelModelRepriceInput } from "@/services/api/wallet";
const validPrice = (value: number | undefined): value is number => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
export function formatModelPrice(value: number | undefined) {
return validPrice(value) ? (value / 1_000_000).toLocaleString("zh-CN", { maximumFractionDigits: 6 }) : "—";
}
export function formatModelMargin(cost: number | undefined, sale: number | undefined) {
if (!validPrice(cost) || !validPrice(sale) || sale === 0) return "—";
return `${(((sale - cost) * 100) / sale).toLocaleString("zh-CN", { maximumFractionDigits: 2 })}%`;
}
export function modelPriceFields(tier: ChannelModelPriceTier, capability: ChannelModel["capability"]) {
if (tier.billingMode !== "token") return [{ key: "unitPriceMicrocredits" as const, label: "", unit: tier.billingMode === "per_second" ? "秒" : "次" }];
if (capability === "video") return [{ key: "outputTokenPriceMicrocredits" as const, label: "视频", unit: "百万视频 Token" }];
return [
{ key: "inputTokenPriceMicrocredits" as const, label: "输入", unit: "百万 Token" },
{ key: "outputTokenPriceMicrocredits" as const, label: "输出", unit: "百万 Token" },
{ key: "cachedTokenPriceMicrocredits" as const, label: "缓存", unit: "百万 Token" },
];
}
export function previewModelRepricing(items: ChannelModel[], marginPercent: number | null, decimalPlaces = 2): ChannelModel[] {
if (marginPercent === null || !Number.isFinite(marginPercent) || marginPercent < 0 || marginPercent > 99.99 || Math.abs(marginPercent * 100 - Math.round(marginPercent * 100)) > 1e-8) {
throw new Error("请输入 0% 到 99.99% 的目标利润率,最多两位小数");
}
if (!items.length || items.length > 100) throw new Error("请选择 1 到 100 个模型");
if (!Number.isInteger(decimalPlaces) || decimalPlaces < 0 || decimalPlaces > 6) throw new Error("请选择保留 0 到 6 位小数");
const denominator = BigInt(10_000 - Math.round(marginPercent * 100));
const quantum = BigInt(10 ** (6 - decimalPlaces));
return items.map((item) => {
if (!item.priceTiers.some((tier) => tier.enabled)) throw new Error(`${item.displayName || item.modelKey} 没有已启用规格`);
return {
...item,
priceTiers: item.priceTiers.map((tier) => {
if (!tier.enabled) return tier;
const name = `${item.displayName || item.modelKey}(${tier.selectorKey})`;
if (!tier.id || !tier.priceConfigured || !tier.costPricing?.configured) throw new Error(`${name}:请先保存完整的规格成本价和销售价配置`);
const next = { ...tier };
for (const { key } of modelPriceFields(tier, item.capability)) {
const cost = tier.costPricing[key];
if (!validPrice(cost) || cost > 1_000_000_000_000) throw new Error(`${name}:成本价无效`);
// Round once at the selected credit precision, with exact half-up ties.
const numerator = BigInt(cost) * BigInt(10_000);
const divisor = denominator * quantum;
const sale = Number(((numerator * BigInt(2) + divisor) / (divisor * BigInt(2))) * quantum);
if (!validPrice(sale) || (tier.billingMode === "token" && sale > 1_000_000_000_000)) throw new Error(`${name}:调整后售价超过上限`);
next[key] = sale;
}
return next;
}),
};
});
}
export function saleInputValue(microcredits: number) {
if (!validPrice(microcredits)) return null;
const value = BigInt(microcredits);
return `${value / BigInt(1_000_000)}.${String(value % BigInt(1_000_000)).padStart(6, "0")}`.replace(/\.?0+$/, "");
}
export function parseSaleInput(value: string | null) {
if (value === null || !/^\d+(?:\.\d{0,6})?$/.test(value)) throw new Error("销售价须为非负数,最多保留 6 位小数");
const [integer, fraction = ""] = value.split(".");
const amount = Number(BigInt(integer) * BigInt(1_000_000) + BigInt(fraction.padEnd(6, "0")));
if (!validPrice(amount)) throw new Error("销售价超出有效范围");
return amount;
}
export type ModelRepriceRow = {
key: string;
model: ChannelModel;
tier: ChannelModelPriceTier;
field: ReturnType<typeof modelPriceFields>[number];
sale: string | null;
manual: boolean;
};
export function modelRepriceRows(items: ChannelModel[]): ModelRepriceRow[] {
return items.flatMap((model) =>
model.priceTiers
.filter((tier) => tier.enabled)
.flatMap((tier) =>
modelPriceFields(tier, model.capability).map((field) => ({
key: `${tier.id}:${field.key}`,
model,
tier,
field,
sale: tier.priceConfigured ? saleInputValue(tier[field.key]) : null,
manual: false,
})),
),
);
}
export function modelRepricePayload(items: ChannelModel[], rows: ModelRepriceRow[]): ChannelModelRepriceInput[] {
if (!items.length || items.length > 100) throw new Error("请选择 1 到 100 个模型");
const byKey = new Map(rows.map((row) => [row.key, row]));
return items.map((item) => {
const tiers = item.priceTiers.filter((tier) => tier.enabled);
if (!tiers.length) throw new Error(`${item.displayName || item.modelKey} 没有已启用规格`);
return {
modelId: item.id,
priceVersion: item.priceVersion,
priceTiers: tiers.map((tier) => {
if (!tier.id || !tier.priceConfigured || !tier.costPricing?.configured) throw new Error(`${item.displayName || item.modelKey}:请先配置规格成本价和销售价`);
const prices: ChannelModelRepriceInput["priceTiers"][number]["prices"] = {};
for (const { key } of modelPriceFields(tier, item.capability)) {
const cost = tier.costPricing[key];
if (!validPrice(cost) || cost > 1_000_000_000_000) throw new Error(`${item.displayName || item.modelKey}:成本价无效`);
const sale = parseSaleInput(byKey.get(`${tier.id}:${key}`)?.sale ?? null);
if (tier.billingMode === "token" && sale > 1_000_000_000_000) throw new Error("Token 售价不能超过 1,000,000 积分");
prices[key] = sale;
}
return { id: tier.id, priceVersion: tier.priceVersion, prices };
}),
};
});
}
@@ -0,0 +1,224 @@
import { useState } from "react";
import { Alert, App, Button, InputNumber, Select, Table } from "antd";
import { AdminModal } from "@/pages/admin/ui/overlays";
import { repriceAdminChannelModels, type ChannelModel } from "@/services/api/wallet";
import { specificationLabel } from "./channel-model-cost-summary";
import { formatModelMargin, formatModelPrice, modelRepricePayload, modelRepriceRows, parseSaleInput, previewModelRepricing, type ModelRepriceRow } from "./channel-model-pricing";
export function ChannelModelRepriceDialog({ channelId, channelName, items, onClose, onSaved }: { channelId: string; channelName: string; items: ChannelModel[]; onClose: () => void; onSaved: () => Promise<void> }) {
const { message } = App.useApp();
const [margin, setMargin] = useState<number | null>(null);
const [decimalPlaces, setDecimalPlaces] = useState(2);
const [rows, setRows] = useState(() => modelRepriceRows(items));
const [calculationError, setCalculationError] = useState("");
const [saving, setSaving] = useState(false);
let validationError = "";
try {
modelRepricePayload(items, rows);
} catch (cause) {
validationError = cause instanceof Error ? cause.message : "销售价无效";
}
const original = new Map(modelRepriceRows(items).map((row) => [row.key, row.sale]));
const changed = rows.filter((row) => {
try {
return parseSaleInput(row.sale) !== parseSaleInput(original.get(row.key) ?? null);
} catch {
return false;
}
}).length;
const recalculate = (nextMargin: number | null, places: number) => {
setMargin(nextMargin);
setDecimalPlaces(places);
if (nextMargin === null) {
setCalculationError("");
return;
}
try {
setRows(modelRepriceRows(previewModelRepricing(items, nextMargin, places)));
setCalculationError("");
} catch (cause) {
setCalculationError(cause instanceof Error ? cause.message : "无法计算销售价");
}
};
const save = async () => {
if (validationError || calculationError || !changed || saving) return;
setSaving(true);
try {
const result = await repriceAdminChannelModels(channelId, modelRepricePayload(items, rows));
message.success(`已保存 ${result.updated} 个模型的销售价`);
} catch (cause) {
message.error(cause instanceof Error ? cause.message : "统一调价失败");
setSaving(false);
return;
}
try {
await onSaved();
} catch {
message.warning("价格已保存,但列表刷新失败,请手动刷新");
} finally {
setSaving(false);
onClose();
}
};
return (
<AdminModal
title={`统一调价 · ${items.length} 个模型`}
open
centered
width={1180}
rootClassName="admin-model-reprice-modal"
onCancel={onClose}
closable={!saving}
mask={{ closable: !saving }}
keyboard={!saving}
confirmLoading={saving}
cancelButtonProps={{ disabled: saving }}
okButtonProps={{ disabled: Boolean(validationError || calculationError) || !changed }}
okText={`保存销售价${changed ? `(${changed} 项)` : ""}`}
onOk={() => void save()}
>
<div className="admin-model-reprice">
<div className="admin-model-reprice-controls">
<div className="admin-model-reprice-field">
<label htmlFor="reprice-margin">目标利润率</label>
<InputNumber id="reprice-margin" aria-label="目标利润率" size="large" value={margin} onChange={(value) => recalculate(value, decimalPlaces)} min={0} max={99.99} step={1} suffix="%" placeholder="输入百分比" disabled={saving} />
</div>
<div className="admin-model-reprice-field">
<label htmlFor="reprice-decimals">保留小数位</label>
<Select
id="reprice-decimals"
aria-label="保留小数位"
size="large"
value={decimalPlaces}
disabled={saving}
onChange={(value) => recalculate(margin, value)}
options={Array.from({ length: 7 }, (_, value) => ({ value, label: `${value} 位小数` }))}
/>
</div>
<Button size="large" disabled={saving || margin === null} onClick={() => recalculate(margin, decimalPlaces)}>
重新计算
</Button>
<div className="admin-model-reprice-formula">
<strong>按成本计算,四舍五入</strong>
<span>新售价 = 成本价 ÷(1 − 利润率)</span>
</div>
</div>
<p className="admin-model-reprice-note">修改利润率、小数位或点击「重新计算」会按成本重算全部规格售价,覆盖手动修改。计算后可继续单独编辑,保存以输入框的最终值为准。</p>
<div className="admin-model-reprice-list-heading">
<strong>模型价格</strong>
<span>
{rows.length} 个计费项 · 已修改 {changed} 项
</span>
</div>
<ChannelModelRepriceTable
items={items}
rows={rows}
channelName={channelName}
decimalPlaces={decimalPlaces}
disabled={saving}
onSaleChange={(key, value) => setRows((current) => current.map((entry) => (entry.key === key ? { ...entry, sale: value, manual: true } : entry)))}
/>
{calculationError || validationError ? <Alert type="warning" showIcon title={calculationError || validationError} /> : null}
<p className="admin-model-reprice-note">仅修改已启用规格的售价,不改变成本或启停状态。四舍五入及手动修改可能使实际利润率偏离目标;价格配置发生变化时整批拒绝保存,请刷新后重试。</p>
</div>
</AdminModal>
);
}
export function ChannelModelRepriceTable({
items,
rows,
channelName,
decimalPlaces,
disabled,
onSaleChange,
}: {
items: ChannelModel[];
rows: ModelRepriceRow[];
channelName: string;
decimalPlaces: number;
disabled: boolean;
onSaleChange: (key: string, value: string | null) => void;
}) {
const byModel = new Map<string, ModelRepriceRow[]>();
for (const row of rows) {
const group = byModel.get(row.model.id) ?? [];
group.push(row);
byModel.set(row.model.id, group);
}
return (
<Table<ChannelModel>
className="admin-model-reprice-table"
rowKey="id"
dataSource={items}
pagination={false}
tableLayout="fixed"
scroll={{ x: 1040 }}
locale={{ emptyText: "暂无所选模型" }}
columns={[
{ title: "模型展示名", width: 180, render: (_, model) => <strong>{model.displayName || model.modelKey}</strong> },
{
title: "渠道展示名(上游模型ID)",
width: 220,
render: (_, model) => (
<div className="admin-model-reprice-identity">
<span>{model.channelLabel || channelName}</span>
<small className="admin-monospace">{model.providerModelKey || model.modelKey}</small>
</div>
),
},
// One model per table row; the shared grid keeps each specification and its prices aligned even when labels wrap.
{
title: "规格",
width: 140,
onCell: () => ({ colSpan: 3, className: "admin-model-reprice-spec-cell" }),
render: (_, model) => (
<div className="admin-model-reprice-specs">
{(byModel.get(model.id) ?? []).map((row) => {
let sale: number | undefined;
try {
sale = parseSaleInput(row.sale);
} catch {
/* Incomplete input remains visible and blocks saving. */
}
const cost = row.tier.costPricing?.configured ? row.tier.costPricing[row.field.key] : undefined;
return (
<div key={row.key} className="admin-model-reprice-spec-row">
<div className="admin-model-reprice-identity">
<span>{specificationLabel(row.tier)}</span>
{row.field.label && <small>{row.field.label}</small>}
{row.tier.providerModelKey && row.tier.providerModelKey !== (model.providerModelKey || model.modelKey) && <small className="admin-monospace">上游:{row.tier.providerModelKey}</small>}
</div>
<div className="admin-model-reprice-identity">
<span>{cost === undefined ? "未配置成本" : formatModelPrice(cost)}</span>
<small>积分 / {row.field.unit}</small>
</div>
<div className="admin-model-reprice-sale">
<InputNumber<string>
stringMode
aria-label={`${model.displayName || model.modelKey} ${specificationLabel(row.tier)} ${row.field.label} 销售价`}
size="middle"
value={row.sale}
min="0"
step={String(10 ** -decimalPlaces)}
disabled={disabled}
status={sale === undefined ? "error" : undefined}
onChange={(value) => onSaleChange(row.key, value)}
/>
<small>
{row.manual ? "手动修改 · " : ""}利润率 {formatModelMargin(cost, sale)}
</small>
</div>
</div>
);
})}
{!byModel.get(model.id)?.length && <div className="admin-model-reprice-no-spec">暂无已启用规格</div>}
</div>
),
},
{ title: "成本价", width: 160, onCell: () => ({ colSpan: 0 }) },
{ title: "销售价(积分)", width: 340, onCell: () => ({ colSpan: 0 }) },
]}
/>
);
}
+10
View File
@@ -320,6 +320,16 @@ export function deleteAdminChannelModels(channelId: string, modelIds: string[])
return http.post<{ deleted: number }>(`/admin/channels/${encodeURIComponent(channelId)}/models/batch-delete`, { modelIds }); return http.post<{ deleted: number }>(`/admin/channels/${encodeURIComponent(channelId)}/models/batch-delete`, { modelIds });
} }
export type ChannelModelRepriceInput = {
modelId: string;
priceVersion: number;
priceTiers: { id: string; priceVersion: number; prices: Partial<Record<"unitPriceMicrocredits" | "inputTokenPriceMicrocredits" | "outputTokenPriceMicrocredits" | "cachedTokenPriceMicrocredits", number>> }[];
};
export function repriceAdminChannelModels(channelId: string, models: ChannelModelRepriceInput[]) {
return http.post<{ updated: number }>(`/admin/channels/${encodeURIComponent(channelId)}/models/batch-reprice`, { models });
}
export type AdminFinanceListParams = { keyword?: string; status?: string; validity?: string; page?: number; pageSize?: number }; export type AdminFinanceListParams = { keyword?: string; status?: string; validity?: string; page?: number; pageSize?: number };
export function listAdminRedeemBatches(params: AdminFinanceListParams = {}) { export function listAdminRedeemBatches(params: AdminFinanceListParams = {}) {
+174
View File
@@ -0,0 +1,174 @@
import { expect, test } from "bun:test";
import { runInNewContext } from "node:vm";
import { clampAgentLauncherPosition, moveAgentLauncher, restoreAgentLauncherPosition } from "../src/lib/canvas/agent-launcher-position";
const viewport = { width: 1280, height: 800 };
const start = { right: 20, bottom: 20 };
test("launcher restores a valid position and defaults safely for corrupt preferences", () => {
expect(restoreAgentLauncherPosition('{"right":240,"bottom":180}', viewport)).toEqual({ right: 240, bottom: 180 });
for (const raw of [null, "invalid", "null", "{}", '{"right":"20","bottom":20}', '{"right":1e999,"bottom":20}']) {
expect(restoreAgentLauncherPosition(raw, viewport)).toEqual(start);
}
});
test("launcher stays within all four edges and clamps after a viewport shrink", () => {
expect(clampAgentLauncherPosition({ right: -500, bottom: -500 }, viewport)).toEqual(start);
expect(clampAgentLauncherPosition({ right: 5000, bottom: 5000 }, viewport)).toEqual({ right: 1184, bottom: 704 });
expect(clampAgentLauncherPosition({ right: 1000, bottom: 500 }, { width: 320, height: 300 })).toEqual({ right: 224, bottom: 204 });
expect(clampAgentLauncherPosition(start, { width: 90, height: 90 })).toEqual({ right: 7, bottom: 7 });
});
test("small pointer jitter remains a click, but dragging back to the origin remains a drag", () => {
const gesture = { start, x: 500, y: 500, dragged: false };
expect(moveAgentLauncher(gesture, 502, 503, viewport)).toEqual({ dragged: false, position: start });
expect(moveAgentLauncher(gesture, 400, 450, viewport)).toEqual({ dragged: true, position: { right: 120, bottom: 70 } });
expect(moveAgentLauncher({ ...gesture, dragged: true }, 500, 500, viewport)).toEqual({ dragged: true, position: start });
});
// Execute the production hook with a tiny hook scheduler; only React and browser
// side effects are substituted, not the pointer/click handlers being tested.
const build = await Bun.build({
entrypoints: [new URL("../src/components/canvas/use-agent-launcher-position.ts", import.meta.url).pathname],
target: "browser",
format: "cjs",
plugins: [
{
name: "hook-scheduler",
setup(builder) {
builder.onResolve({ filter: /^react$/ }, () => ({ path: "react", namespace: "test" }));
builder.onLoad({ filter: /.*/, namespace: "test" }, () => ({ contents: "export const { useState, useRef, useEffect } = globalThis.hooks;", loader: "js" }));
},
},
],
});
expect(build.success).toBe(true);
const source = await build.outputs[0].text();
function harness() {
const cells = [];
let cursor = 0;
let opens = 0;
const stored = new Map();
const listeners = new Map();
const window = { innerWidth: 1280, innerHeight: 800, addEventListener: (name, fn) => listeners.set(name, fn), removeEventListener: (name) => listeners.delete(name) };
const module = { exports: {} };
const hooks = {
useState(initial) {
const i = cursor++;
if (!(i in cells)) cells[i] = typeof initial === "function" ? initial() : initial;
return [
cells[i],
(next) => {
cells[i] = typeof next === "function" ? next(cells[i]) : next;
},
];
},
useRef(initial) {
const i = cursor++;
return (cells[i] ||= { current: initial });
},
useEffect(fn) {
const i = cursor++;
if (!cells[i]) {
cells[i] = true;
fn();
}
},
};
runInNewContext(source, { module, exports: module.exports, hooks, window, localStorage: { getItem: (key) => stored.get(key) ?? null, setItem: (key, value) => stored.set(key, value) }, console });
const render = () => {
cursor = 0;
return module.exports.useAgentLauncherPosition(() => opens++);
};
let current = render();
let captured = null;
const target = {
setPointerCapture: (id) => {
captured = id;
},
hasPointerCapture: (id) => captured === id,
releasePointerCapture: () => {
captured = null;
},
};
return {
fire(name, overrides = {}) {
const event = { currentTarget: target, pointerId: 1, isPrimary: true, button: 0, clientX: 500, clientY: 500, detail: 1, preventDefault() {}, stopPropagation() {}, ...overrides };
current.handlers[name](event);
current = render();
},
resize(width, height) {
window.innerWidth = width;
window.innerHeight = height;
listeners.get("resize")();
current = render();
},
get state() {
return current;
},
get opens() {
return opens;
},
get saved() {
return [...stored.values()].map(JSON.parse);
},
get captured() {
return captured;
},
};
}
test("production handlers preserve click and suppress drag-release activation", () => {
const h = harness();
h.fire("onPointerDown");
h.fire("onPointerMove", { clientX: 502 });
h.fire("onPointerUp", { clientX: 502 });
h.fire("onClick");
expect(h.opens).toBe(1);
h.fire("onPointerDown");
h.fire("onPointerMove", { clientX: 400, clientY: 450 });
expect(h.state.dragging).toBe(true);
h.fire("onPointerUp", { clientX: 400, clientY: 450 });
h.fire("onClick");
expect(h.opens).toBe(1);
expect(h.state.position).toEqual({ right: 120, bottom: 70 });
expect(h.saved).toEqual([{ right: 120, bottom: 70 }]);
expect(h.captured).toBe(null);
h.fire("onClick", { detail: 0 });
expect(h.opens).toBe(2);
h.fire("onPointerDown");
h.fire("onPointerUp");
h.fire("onClick");
expect(h.opens).toBe(3);
});
for (const ending of ["onPointerCancel", "onLostPointerCapture"]) {
test(`production handlers stop safely on ${ending} and ignore other pointers`, () => {
const h = harness();
h.fire("onPointerDown", { button: 2 });
expect(h.captured).toBe(null);
h.fire("onPointerDown", { pointerType: "touch" });
h.fire("onPointerMove", { pointerId: 2, clientX: 100 });
expect(h.state.position).toEqual(start);
h.fire("onPointerMove", { clientX: 300 });
h.fire(ending);
h.fire("onPointerMove", { clientX: 100 });
h.fire("onClick");
expect(h.state.position).toEqual({ right: 220, bottom: 20 });
expect(h.state.dragging).toBe(false);
expect(h.captured).toBe(null);
expect(h.opens).toBe(0);
});
}
test("production handlers support keyboard movement and viewport resizing", () => {
const h = harness();
h.fire("onKeyDown", { key: "ArrowLeft" });
h.fire("onKeyDown", { key: "ArrowUp", shiftKey: true });
expect(h.state.position).toEqual({ right: 30, bottom: 60 });
h.resize(100, 100);
expect(h.state.position).toEqual({ right: 12, bottom: 12 });
h.fire("onClick", { detail: 0 });
expect(h.opens).toBe(1);
});
+25 -15
View File
@@ -45,24 +45,26 @@ const item: ChannelModel = {
}; };
const render = (tiers: ChannelModelPriceTier[], capability: ChannelModel["capability"] = "image") => renderToStaticMarkup(<ChannelModelCostSummary item={{ ...item, capability, priceTiers: tiers }} />); const render = (tiers: ChannelModelPriceTier[], capability: ChannelModel["capability"] = "image") => renderToStaticMarkup(<ChannelModelCostSummary item={{ ...item, capability, priceTiers: tiers }} />);
test("fixed and per-second costs use costPricing, not the sale price", () => { test("fixed and per-second specifications show cost, sale and margin", () => {
const fixed = render([tier]); const fixed = render([tier]);
expect(fixed).toContain("默认规格"); expect(fixed).toContain("默认规格");
expect(fixed).toContain("1.25 积分 / 次"); expect(fixed).toContain("1.25 / 99");
expect(fixed).not.toContain("99 积分"); expect(fixed).toContain("积分 / 次 · 利润率 98.74%");
expect(render([{ ...tier, billingMode: "per_second" }])).toContain("1.25 积分 / 秒"); expect(render([{ ...tier, billingMode: "per_second" }])).toContain("积分 / 秒");
}); });
test("text token costs include input, output and cached tokens", () => { test("text token costs include input, output and cached tokens", () => {
const html = render([{ ...tier, billingMode: "token" }], "text"); const html = render([{ ...tier, billingMode: "token" }], "text");
expect(html).toContain("输入 2 · 输出 3 · 缓存 0.5"); expect(html).toContain("输入 2 / 88");
expect(html).toContain("输出 3 / 77");
expect(html).toContain("缓存 0.5 / 66");
expect(html).toContain("积分 / 百万 Token"); expect(html).toContain("积分 / 百万 Token");
expect(html).not.toContain("77");
}); });
test("video token costs use only output video tokens", () => { test("video token costs use only output video tokens", () => {
const html = render([{ ...tier, billingMode: "token" }], "video"); const html = render([{ ...tier, billingMode: "token" }], "video");
expect(html).toContain("3 积分 / 百万视频 Token"); expect(html).toContain("视频 3 / 77");
expect(html).toContain("积分 / 百万视频 Token");
expect(html).not.toContain("输入"); expect(html).not.toContain("输入");
}); });
@@ -70,21 +72,23 @@ test("missing and unconfigured costs never fall back to sale prices or zero", ()
for (const costPricing of [undefined, { ...tier.costPricing!, configured: false }]) { for (const costPricing of [undefined, { ...tier.costPricing!, configured: false }]) {
const html = render([{ ...tier, costPricing }]); const html = render([{ ...tier, costPricing }]);
expect(html).toContain("未配置成本"); expect(html).toContain("未配置成本");
expect(html).not.toContain("积分 /"); expect(html).toContain("未配置成本 / 99");
expect(html).toContain("利润率 —");
} }
}); });
test("explicit free costs remain zero even if sale pricing is unconfigured", () => { test("explicit free costs remain zero even if sale pricing is unconfigured", () => {
const html = renderToStaticMarkup(<ChannelModelCostSummary item={{ ...item, priceConfigured: false, priceTiers: [{ ...tier, priceConfigured: false, costPricing: { ...tier.costPricing!, unitPriceMicrocredits: 0 } }] }} />); const html = renderToStaticMarkup(<ChannelModelCostSummary item={{ ...item, priceConfigured: false, priceTiers: [{ ...tier, priceConfigured: false, costPricing: { ...tier.costPricing!, unitPriceMicrocredits: 0 } }] }} />);
expect(html).toContain("0 积分 / 次"); expect(html).toContain("0 / 未配置售价");
expect(html).toContain("利润率 —");
expect(html).not.toContain("未配置成本"); expect(html).not.toContain("未配置成本");
}); });
test("invalid or missing numeric costs are not silently displayed as free", () => { test("invalid or missing numeric costs are not silently displayed as free", () => {
for (const unitPriceMicrocredits of [NaN, Infinity, -1, undefined]) { for (const unitPriceMicrocredits of [NaN, Infinity, -1, undefined]) {
const html = render([{ ...tier, costPricing: { ...tier.costPricing!, unitPriceMicrocredits: unitPriceMicrocredits as number } }]); const html = render([{ ...tier, costPricing: { ...tier.costPricing!, unitPriceMicrocredits: unitPriceMicrocredits as number } }]);
expect(html).toContain("— 积分 / 次"); expect(html).toContain("— / 99");
expect(html).not.toContain("0 积分"); expect(html).toContain("利润率 —");
} }
}); });
@@ -93,17 +97,23 @@ test("specification labels prefer canonical selectors and additional tiers remai
expect(html).toContain("图生视频 / 1080P / 10 秒"); expect(html).toContain("图生视频 / 1080P / 10 秒");
expect(html).not.toContain("720P"); expect(html).not.toContain("720P");
expect(html).toContain("<details"); expect(html).toContain("<details");
expect(html).toContain("其余 1 个规格成本"); expect(html).toContain("其余 1 个规格价格");
}); });
test("disabled tiers are excluded and empty costs are explicit", () => { test("disabled tiers are excluded and empty costs are explicit", () => {
expect(render([{ ...tier, enabled: false }])).toContain("未配置成本"); expect(render([{ ...tier, enabled: false }])).toContain("无已启用规格");
expect(render([])).toContain("未配置成本"); expect(render([])).toContain("无已启用规格");
}); });
test("model list uses the cost component without gating it on user prices", async () => { test("model list uses the cost component without gating it on user prices", async () => {
const source = await Bun.file(new URL("../src/pages/admin/components/channel-model-manager.tsx", import.meta.url)).text(); const source = await Bun.file(new URL("../src/pages/admin/components/channel-model-manager.tsx", import.meta.url)).text();
expect(source).toContain('title: "规格成本价"'); expect(source).toContain('title: "规格成本价 / 销售价 / 利润率"');
expect(source).toContain("<ChannelModelCostSummary item={item} />"); expect(source).toContain("<ChannelModelCostSummary item={item} />");
expect(source).not.toContain("billingSummary(item)"); expect(source).not.toContain("billingSummary(item)");
}); });
test("zero revenue is undefined, free costs have 100% margin and losses stay negative", () => {
expect(render([{ ...tier, unitPriceMicrocredits: 0 }])).toContain("利润率 —");
expect(render([{ ...tier, unitPriceMicrocredits: 1_000_000 }])).toContain("利润率 -25%");
expect(render([{ ...tier, costPricing: { ...tier.costPricing!, unitPriceMicrocredits: 0 } }])).toContain("利润率 100%");
});
+125
View File
@@ -0,0 +1,125 @@
import { expect, test } from "bun:test";
import { formatModelMargin, previewModelRepricing, modelRepriceRows, modelRepricePayload, parseSaleInput, saleInputValue } from "../src/pages/admin/components/channel-model-pricing";
import type { ChannelModel } from "../src/services/api/wallet";
const item = {
id: "m",
displayName: "Model",
capability: "text",
priceTiers: [
{
id: "t",
selectorKey: "{}",
enabled: true,
priceConfigured: true,
billingMode: "fixed_request",
unitPriceMicrocredits: 9,
inputTokenPriceMicrocredits: 0,
outputTokenPriceMicrocredits: 0,
cachedTokenPriceMicrocredits: 0,
costPricing: { configured: true, unitPriceMicrocredits: 1_000_000, inputTokenPriceMicrocredits: 2_000_000, outputTokenPriceMicrocredits: 4_000_000, cachedTokenPriceMicrocredits: 0 },
},
],
} as ChannelModel;
test("20% margin uses cost / 0.8, not markup, and leaves inputs unchanged", () => {
const result = previewModelRepricing([item], 20);
expect(result[0]!.priceTiers[0]!.unitPriceMicrocredits).toBe(1_250_000);
expect(item.priceTiers[0]!.unitPriceMicrocredits).toBe(9);
expect(result[0]!.priceTiers[0]!.costPricing).toEqual(item.priceTiers[0]!.costPricing);
});
test("token prices are independent and zero costs remain free", () => {
const token = { ...item, priceTiers: [{ ...item.priceTiers[0]!, billingMode: "token" as const }] };
const tier = previewModelRepricing([token], 20)[0]!.priceTiers[0]!;
expect(tier.inputTokenPriceMicrocredits).toBe(2_500_000);
expect(tier.outputTokenPriceMicrocredits).toBe(5_000_000);
expect(tier.cachedTokenPriceMicrocredits).toBe(0);
expect(formatModelMargin(0, 0)).toBe("—");
});
test("video token adjusts only output; per-second adjusts unit price", () => {
const video = { ...item, capability: "video" as const, priceTiers: [{ ...item.priceTiers[0]!, billingMode: "token" as const }] };
const tier = previewModelRepricing([video], 20)[0]!.priceTiers[0]!;
expect(tier.outputTokenPriceMicrocredits).toBe(5_000_000);
expect(tier.inputTokenPriceMicrocredits).toBe(0);
video.priceTiers[0]!.billingMode = "per_second";
expect(previewModelRepricing([video], 20)[0]!.priceTiers[0]!.unitPriceMicrocredits).toBe(1_250_000);
});
test("half-up rounding at microcredit precision and disabled tiers stay untouched", () => {
const tiny = {
...item,
priceTiers: [
{ ...item.priceTiers[0]!, costPricing: { ...item.priceTiers[0]!.costPricing!, unitPriceMicrocredits: 1 } },
{ ...item.priceTiers[0]!, id: "disabled", enabled: false },
],
};
const result = previewModelRepricing([tiny], 20, 6)[0]!;
expect(result.priceTiers[0]!.unitPriceMicrocredits).toBe(1);
expect(result.priceTiers[1]).toBe(tiny.priceTiers[1]);
expect(previewModelRepricing([tiny], 0, 6)[0]!.priceTiers[0]!.unitPriceMicrocredits).toBe(1);
});
test("selected decimals round half up exactly, including binary floating point ties", () => {
for (const [cost, places, expected] of [
[1_005_000, 2, 1_010_000],
[1_004_999, 2, 1_000_000],
[1_500_000, 0, 2_000_000],
[1_234_500, 3, 1_235_000],
[5_000, 2, 10_000],
[4_999, 2, 0],
]) {
const input = { ...item, priceTiers: [{ ...item.priceTiers[0]!, costPricing: { ...item.priceTiers[0]!.costPricing!, unitPriceMicrocredits: cost! } }] };
expect(previewModelRepricing([input], 0, places)[0]!.priceTiers[0]!.unitPriceMicrocredits).toBe(expected);
}
for (const places of [-1, 7, 1.5]) expect(() => previewModelRepricing([item], 20, places)).toThrow();
});
test("rows are present before a percentage is set and final manual prices survive payload creation", () => {
const input = { ...item, priceVersion: 4, priceTiers: [{ ...item.priceTiers[0]!, priceVersion: 8 }] };
const initial = modelRepriceRows([input]);
expect(initial).toHaveLength(1);
expect(initial[0]!.sale).toBe("0.000009");
const recalculated = modelRepriceRows(previewModelRepricing([input], 20));
expect(recalculated[0]!.sale).toBe("1.25");
recalculated[0]!.sale = "1.37";
recalculated[0]!.manual = true;
expect(modelRepricePayload([input], recalculated)).toEqual([{ modelId: "m", priceVersion: 4, priceTiers: [{ id: "t", priceVersion: 8, prices: { unitPriceMicrocredits: 1_370_000 } }] }]);
expect(modelRepriceRows(previewModelRepricing([input], 50, 2))[0]!.sale).toBe("2");
expect(modelRepriceRows(previewModelRepricing([input], 20, 0))[0]!.sale).toBe("1");
});
test("manual token edits affect only their own field and invalid input never silently becomes zero", () => {
const token = { ...item, priceTiers: [{ ...item.priceTiers[0]!, billingMode: "token" as const }] };
const rows = modelRepriceRows(previewModelRepricing([token], 20));
rows[1]!.sale = "7.123456";
const prices = modelRepricePayload([token], rows)[0]!.priceTiers[0]!.prices;
expect(prices).toEqual({ inputTokenPriceMicrocredits: 2_500_000, outputTokenPriceMicrocredits: 7_123_456, cachedTokenPriceMicrocredits: 0 });
for (const invalid of [null, "", "-1", "1.1234567", "1e3", "NaN", "9007199254.740992"]) expect(() => parseSaleInput(invalid)).toThrow();
for (const value of [0, 1, 10_000_000, Number.MAX_SAFE_INTEGER]) expect(parseSaleInput(saleInputValue(value))).toBe(value);
rows[1]!.sale = null;
expect(() => modelRepricePayload([token], rows)).toThrow();
});
test("reject invalid rates, incomplete costs, invalid amounts and overflow", () => {
for (const margin of [null, NaN, Infinity, -1, 100, 20.001]) expect(() => previewModelRepricing([item], margin)).toThrow();
for (const cost of [undefined, NaN, -1, 1_000_000_000_001]) {
const invalid = { ...item, priceTiers: [{ ...item.priceTiers[0]!, costPricing: { ...item.priceTiers[0]!.costPricing!, unitPriceMicrocredits: cost as number } }] };
expect(() => previewModelRepricing([item, invalid], 20)).toThrow();
}
expect(() => previewModelRepricing([{ ...item, priceTiers: [] }], 20)).toThrow();
expect(() => previewModelRepricing([{ ...item, priceTiers: [{ ...item.priceTiers[0]!, costPricing: undefined }] }], 20)).toThrow();
expect(() => previewModelRepricing([{ ...item, priceTiers: [{ ...item.priceTiers[0]!, priceConfigured: false }] }], 20)).toThrow();
const huge = { ...item, priceTiers: [{ ...item.priceTiers[0]!, costPricing: { ...item.priceTiers[0]!.costPricing!, unitPriceMicrocredits: 1_000_000_000_000 } }] };
expect(() => previewModelRepricing([huge], 99.99)).toThrow();
});
test("manager preserves cross-page selection and API uses one atomic request", async () => {
const source = await Bun.file(new URL("../src/pages/admin/components/channel-model-manager.tsx", import.meta.url)).text();
const api = await Bun.file(new URL("../src/services/api/wallet.ts", import.meta.url)).text();
expect(source).toContain("preserveSelectedRowKeys: true");
expect(source).toContain("items.filter((item) => selectedModelIds.includes(item.id))");
expect(source).toContain("统一调价");
expect(api).toContain("/models/batch-reprice");
});
@@ -0,0 +1,81 @@
import { expect, test } from "bun:test";
import { renderToStaticMarkup } from "react-dom/server";
import { ChannelModelRepriceTable } from "../src/pages/admin/components/channel-model-reprice-dialog";
import { modelRepricePayload, modelRepriceRows, previewModelRepricing } from "../src/pages/admin/components/channel-model-pricing";
import type { ChannelModel } from "../src/services/api/wallet";
const model = {
id: "image",
modelKey: "image-model",
displayName: "GPT Image 2.5",
channelLabel: "特惠渠道",
providerModelKey: "gpt-image-2.5",
capability: "image",
priceVersion: 9,
priceTiers: ["1K", "2K", "4K"].map((resolution, index) => ({
id: `tier-${index}`,
selector: { vquality: resolution },
selectorKey: resolution,
resolution,
enabled: true,
priceConfigured: true,
priceVersion: index + 1,
billingMode: "fixed_request",
unitPriceMicrocredits: 4_000 + index * 1_000,
costPricing: { configured: true, unitPriceMicrocredits: 3_000 },
})),
} as ChannelModel;
function render(items = [model], rows = modelRepriceRows(items)) {
return renderToStaticMarkup(<ChannelModelRepriceTable items={items} rows={rows} channelName="所属渠道" decimalPlaces={3} disabled={false} onSaleChange={() => {}} />);
}
test("multi-spec models render once with aligned specifications and separate sale inputs", () => {
const html = render();
expect(html.match(/data-row-key="image"/g)).toHaveLength(1);
expect(html.match(/class="admin-model-reprice-spec-row"/g)).toHaveLength(3);
expect(html.match(/role="spinbutton"/g)).toHaveLength(3);
expect(html).toContain('colSpan="3"');
expect(html).toContain("渠道展示名(上游模型ID)");
expect(html.match(/>GPT Image 2.5</g)).toHaveLength(1);
expect(html.match(/>特惠渠道</g)).toHaveLength(1);
expect(html).toContain("gpt-image-2.5");
for (const value of ["0.004", "0.005", "0.006"]) expect(html).toContain(`value="${value}"`);
for (const spec of ["1K", "2K", "4K"]) expect(html).toContain(`>${spec}<`);
});
test("recalculation resets all specifications; manual edit changes only its final submitted price", () => {
const rows = modelRepriceRows(previewModelRepricing([model], 25, 3));
expect(rows.map((row) => row.sale)).toEqual(["0.004", "0.004", "0.004"]);
rows[1]!.sale = "0.0055";
rows[1]!.manual = true;
expect(modelRepricePayload([model], rows)[0]!.priceTiers.map((tier) => tier.prices.unitPriceMicrocredits)).toEqual([4_000, 5_500, 4_000]);
const html = render([model], rows);
expect(html).toContain('value="0.0055"');
expect(html).toContain("手动修改");
const reset = modelRepriceRows(previewModelRepricing([model], 25, 3));
expect(reset.map((row) => row.sale)).toEqual(["0.004", "0.004", "0.004"]);
expect(reset.every((row) => !row.manual)).toBe(true);
});
test("token fields stay inside a single model row and tier upstream overrides stay visible", () => {
const token = {
...model,
capability: "text" as const,
channelLabel: "",
priceTiers: [{ ...model.priceTiers[0]!, billingMode: "token" as const, providerModelKey: "spec-upstream", inputTokenPriceMicrocredits: 10, outputTokenPriceMicrocredits: 20, cachedTokenPriceMicrocredits: 30 }],
};
const html = render([token]);
expect(html.match(/data-row-key="image"/g)).toHaveLength(1);
expect(html.match(/role="spinbutton"/g)).toHaveLength(3);
expect(html).toContain("所属渠道");
expect(html).toContain("spec-upstream");
for (const label of ["输入", "输出", "缓存"]) expect(html).toContain(`>${label}<`);
});
test("recalculate button reruns the current margin and precision even when they are unchanged", async () => {
const source = await Bun.file(new URL("../src/pages/admin/components/channel-model-reprice-dialog.tsx", import.meta.url)).text();
expect(source).toContain("onClick={() => recalculate(margin, decimalPlaces)}");
expect(source).toContain("重新计算");
expect(source).toContain("previewModelRepricing(items, nextMargin, places)");
});