feat(models): 统一模型规格价格与路由
This commit is contained in:
1 parent
d268a938e5
commit
236b850177
36 files changed
+3404
-421
No files matched your search
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -18,22 +19,27 @@ import (
|
||||
var logicalModelCodePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]{1,79}$`)
|
||||
|
||||
type LogicalModelRequest struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Description string `json:"description"`
|
||||
Capability string `json:"capability"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
PricePolicy string `json:"pricePolicy"`
|
||||
BillingMode string `json:"billingMode"`
|
||||
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
|
||||
InputPriceMicrocredits int64 `json:"inputPriceMicrocredits"`
|
||||
OutputPriceMicrocredits int64 `json:"outputPriceMicrocredits"`
|
||||
CachedPriceMicrocredits int64 `json:"cachedPriceMicrocredits"`
|
||||
CapabilitySpec CapabilitySpec `json:"capabilitySpec"`
|
||||
DefaultOptions map[string]any `json:"defaultOptions"`
|
||||
Routes []LogicalRouteRequest `json:"routes"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Description string `json:"description"`
|
||||
Capability string `json:"capability"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
PricePolicy string `json:"pricePolicy"`
|
||||
BillingMode string `json:"billingMode"`
|
||||
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
|
||||
InputPriceMicrocredits int64 `json:"inputPriceMicrocredits"`
|
||||
OutputPriceMicrocredits int64 `json:"outputPriceMicrocredits"`
|
||||
CachedPriceMicrocredits int64 `json:"cachedPriceMicrocredits"`
|
||||
// LegacyModelIDs 只用于将用户本地保存的旧目录选择迁移到当前模型家族,
|
||||
// 不能用它重写任务、账单或路由尝试中的不可变快照。
|
||||
LegacyModelIDs []string `json:"legacyModelIds"`
|
||||
CapabilitySpec CapabilitySpec `json:"capabilitySpec"`
|
||||
DefaultOptions map[string]any `json:"defaultOptions"`
|
||||
Routes []LogicalRouteRequest `json:"routes"`
|
||||
// SourceChannelModelID 仅供系统渠道同步流程使用,前台模型不再拥有独立的能力和价格真相。
|
||||
SourceChannelModelID string `json:"-"`
|
||||
}
|
||||
|
||||
type LogicalRouteRequest struct {
|
||||
@@ -44,26 +50,41 @@ type LogicalRouteRequest struct {
|
||||
}
|
||||
|
||||
type PublicLogicalModel struct {
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Description string `json:"description"`
|
||||
Capability string `json:"capability"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
PricePolicy string `json:"pricePolicy"`
|
||||
BillingMode string `json:"billingMode"`
|
||||
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
|
||||
InputPriceMicrocredits int64 `json:"inputPriceMicrocredits"`
|
||||
OutputPriceMicrocredits int64 `json:"outputPriceMicrocredits"`
|
||||
CachedPriceMicrocredits int64 `json:"cachedPriceMicrocredits"`
|
||||
CapabilitySpec CapabilitySpec `json:"capabilitySpec"`
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Description string `json:"description"`
|
||||
Capability string `json:"capability"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
PricePolicy string `json:"pricePolicy"`
|
||||
BillingMode string `json:"billingMode"`
|
||||
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
|
||||
InputPriceMicrocredits int64 `json:"inputPriceMicrocredits"`
|
||||
OutputPriceMicrocredits int64 `json:"outputPriceMicrocredits"`
|
||||
CachedPriceMicrocredits int64 `json:"cachedPriceMicrocredits"`
|
||||
PriceTiers []PublicLogicalModelPriceTier `json:"priceTiers"`
|
||||
LegacyModelIDs []string `json:"legacyModelIds"`
|
||||
CapabilitySpec CapabilitySpec `json:"capabilitySpec"`
|
||||
// CapabilityProfiles 是创作端可见的匿名能力组合,不暴露其背后的供应线路关系。
|
||||
CapabilityProfiles []CapabilitySpec `json:"capabilityProfiles"`
|
||||
DefaultOptions map[string]any `json:"defaultOptions"`
|
||||
Available bool `json:"available"`
|
||||
}
|
||||
|
||||
// PublicLogicalModelPriceTier 是创作端用于约束规格选择和展示当前报价的安全投影,
|
||||
// 不暴露供应渠道、上游模型 ID 或内部路由信息。
|
||||
type PublicLogicalModelPriceTier struct {
|
||||
Selector map[string]string `json:"selector"`
|
||||
Resolution string `json:"resolution"`
|
||||
VideoSeconds int `json:"videoSeconds"`
|
||||
BillingMode string `json:"billingMode"`
|
||||
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
|
||||
InputTokenPriceMicrocredits int64 `json:"inputTokenPriceMicrocredits"`
|
||||
OutputTokenPriceMicrocredits int64 `json:"outputTokenPriceMicrocredits"`
|
||||
CachedTokenPriceMicrocredits int64 `json:"cachedTokenPriceMicrocredits"`
|
||||
}
|
||||
|
||||
type AdminLogicalRoute struct {
|
||||
ID string `json:"id"`
|
||||
ChannelModelID string `json:"channelModelId"`
|
||||
@@ -128,7 +149,7 @@ func (s *Service) PublicLogicalModels(intent *ModelRequestIntent) ([]PublicLogic
|
||||
available = false
|
||||
if coverageValid {
|
||||
for _, route := range cached.Routes {
|
||||
if route.Route.Enabled && route.Route.Weight > 0 && !s.logicalRouteBlocked(route) && MatchCapability(route.CapabilitySpec, resolvedIntent).Matched {
|
||||
if route.Route.Enabled && route.Route.Weight > 0 && !s.logicalRouteBlocked(route) && MatchCapability(route.CapabilitySpec, resolvedIntent).Matched && (cached.Model.PricePolicy != "channel" || channelModelPriceTierForIntent(route.ChannelModel, resolvedIntent) != nil) {
|
||||
available = true
|
||||
break
|
||||
}
|
||||
@@ -159,7 +180,59 @@ func publicLogicalModel(cached cachedLogicalModel, available bool) PublicLogical
|
||||
profiles = append(profiles, route.CapabilitySpec)
|
||||
}
|
||||
}
|
||||
return PublicLogicalModel{ID: item.ID, Code: item.Code, Name: item.Name, Icon: item.Icon, Description: item.Description, Capability: item.Capability, SortOrder: item.SortOrder, PricePolicy: item.PricePolicy, BillingMode: item.BillingMode, UnitPriceMicrocredits: item.UnitPriceMicrocredits, InputPriceMicrocredits: item.InputPriceMicrocredits, OutputPriceMicrocredits: item.OutputPriceMicrocredits, CachedPriceMicrocredits: item.CachedPriceMicrocredits, CapabilitySpec: productSpec, CapabilityProfiles: profiles, DefaultOptions: cached.Defaults, Available: available}
|
||||
return PublicLogicalModel{ID: item.ID, Code: item.Code, Name: item.Name, Icon: item.Icon, Description: item.Description, Capability: item.Capability, SortOrder: item.SortOrder, PricePolicy: item.PricePolicy, BillingMode: item.BillingMode, UnitPriceMicrocredits: item.UnitPriceMicrocredits, InputPriceMicrocredits: item.InputPriceMicrocredits, OutputPriceMicrocredits: item.OutputPriceMicrocredits, CachedPriceMicrocredits: item.CachedPriceMicrocredits, PriceTiers: publicLogicalModelPriceTiers(cached), LegacyModelIDs: decodeLegacyModelIDs(item.LegacyModelIDsJSON), CapabilitySpec: productSpec, CapabilityProfiles: profiles, DefaultOptions: cached.Defaults, Available: available}
|
||||
}
|
||||
|
||||
func publicLogicalModelPriceTiers(cached cachedLogicalModel) []PublicLogicalModelPriceTier {
|
||||
if cached.Model.PricePolicy != "channel" {
|
||||
return []PublicLogicalModelPriceTier{}
|
||||
}
|
||||
result := make([]PublicLogicalModelPriceTier, 0)
|
||||
seen := make(map[string]bool)
|
||||
for _, route := range cached.Routes {
|
||||
if !route.Route.Enabled || route.Route.Weight <= 0 {
|
||||
continue
|
||||
}
|
||||
for _, tier := range route.ChannelModel.PriceTiers {
|
||||
if !tier.Enabled || !tier.PriceConfigured {
|
||||
continue
|
||||
}
|
||||
selector := skuSelectorForTier(tier)
|
||||
_, selectorKey, selectorErr := model.CanonicalSKUSelector(selector)
|
||||
if selectorErr != nil {
|
||||
continue
|
||||
}
|
||||
key := fmt.Sprintf("%s:%s:%d:%d:%d:%d", selectorKey, tier.BillingMode, tier.UnitPriceMicrocredits, tier.InputTokenPriceMicrocredits, tier.OutputTokenPriceMicrocredits, tier.CachedTokenPriceMicrocredits)
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
result = append(result, PublicLogicalModelPriceTier{Selector: selector, Resolution: normalizeChannelModelTierResolution(tier.Resolution), VideoSeconds: tier.VideoSeconds, BillingMode: tier.BillingMode, UnitPriceMicrocredits: tier.UnitPriceMicrocredits, InputTokenPriceMicrocredits: tier.InputTokenPriceMicrocredits, OutputTokenPriceMicrocredits: tier.OutputTokenPriceMicrocredits, CachedTokenPriceMicrocredits: tier.CachedTokenPriceMicrocredits})
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func decodeLegacyModelIDs(raw string) []string {
|
||||
var values []string
|
||||
if err := json.Unmarshal([]byte(raw), &values); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
return normalizeLegacyModelIDs(values)
|
||||
}
|
||||
|
||||
func normalizeLegacyModelIDs(values []string) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
seen := make(map[string]bool, len(values))
|
||||
for _, raw := range values {
|
||||
value := strings.TrimSpace(raw)
|
||||
if value == "" || seen[value] {
|
||||
continue
|
||||
}
|
||||
seen[value] = true
|
||||
result = append(result, value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// capabilitySpecWithRoutePresets repairs old front-model snapshots that stored
|
||||
@@ -423,6 +496,9 @@ func (s *Service) DeleteAdminLogicalModel(actor *model.User, id string) error {
|
||||
if item.ArchivedAt != nil {
|
||||
return BadAuthRequest("前台模型不存在或已删除")
|
||||
}
|
||||
if item.SourceChannelModelID != "" {
|
||||
return BadAuthRequest("该前台模型由系统渠道自动同步,请在系统渠道模型中停用")
|
||||
}
|
||||
audit, err := newAdminAuditEvent(actor, "logical_model.archive", "logical_model", item.ID, "归档前台模型", map[string]any{"code": item.Code, "name": item.Name})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -450,6 +526,37 @@ func (s *Service) logicalModelBundle(actor *model.User, id string, req LogicalMo
|
||||
if name == "" || len([]rune(name)) > 120 {
|
||||
return nil, nil, nil, false, BadAuthRequest("请填写 1-120 个字符的模型名称")
|
||||
}
|
||||
sourceChannelModelID := strings.TrimSpace(req.SourceChannelModelID)
|
||||
if sourceChannelModelID != "" {
|
||||
source, sourceErr := s.repo.ChannelModel(sourceChannelModelID)
|
||||
if sourceErr != nil {
|
||||
return nil, nil, nil, false, BadAuthRequest("系统渠道模型不存在")
|
||||
}
|
||||
if _, channelErr := s.repo.AdminSystemChannel(source.ChannelID); channelErr != nil {
|
||||
return nil, nil, nil, false, BadAuthRequest("前台模型只能同步系统渠道模型")
|
||||
}
|
||||
capability = normalizeCapability(source.Capability)
|
||||
derivedSpec, specErr := channelModelCapabilitySpec(*source)
|
||||
if specErr != nil {
|
||||
return nil, nil, nil, false, specErr
|
||||
}
|
||||
derivedDefaults, defaultsErr := channelModelDefaultOptions(*source, derivedSpec)
|
||||
if defaultsErr != nil {
|
||||
return nil, nil, nil, false, defaultsErr
|
||||
}
|
||||
if len(req.Routes) == 0 {
|
||||
req.CapabilitySpec = derivedSpec
|
||||
req.DefaultOptions = derivedDefaults
|
||||
req.Routes = []LogicalRouteRequest{{ChannelModelID: source.ID, Enabled: true, Priority: 100, Weight: 100}}
|
||||
}
|
||||
req.PricePolicy = "channel"
|
||||
req.BillingMode = "fixed_request"
|
||||
req.UnitPriceMicrocredits, req.InputPriceMicrocredits, req.OutputPriceMicrocredits, req.CachedPriceMicrocredits = 0, 0, 0, 0
|
||||
// 未完成定价的系统模型仅在后台目录保留同步记录,不能暴露到创作端。
|
||||
if strings.TrimSpace(id) == "" {
|
||||
req.Enabled = source.Enabled && channelModelHasActivePriceTier(*source)
|
||||
}
|
||||
}
|
||||
normalizedSpec, err := NormalizeCapabilitySpec(req.CapabilitySpec)
|
||||
if err != nil {
|
||||
return nil, nil, nil, false, err
|
||||
@@ -498,8 +605,18 @@ func (s *Service) logicalModelBundle(actor *model.User, id string, req LogicalMo
|
||||
}
|
||||
}
|
||||
item.Code, item.Name, item.Icon, item.Description, item.Capability = code, name, strings.TrimSpace(req.Icon), strings.TrimSpace(req.Description), capability
|
||||
if sourceChannelModelID != "" {
|
||||
item.SourceChannelModelID = sourceChannelModelID
|
||||
}
|
||||
item.Enabled, item.SortOrder, item.PricePolicy, item.BillingMode = req.Enabled, req.SortOrder, pricePolicy, billingMode
|
||||
item.UnitPriceMicrocredits, item.InputPriceMicrocredits, item.OutputPriceMicrocredits, item.CachedPriceMicrocredits = req.UnitPriceMicrocredits, req.InputPriceMicrocredits, req.OutputPriceMicrocredits, req.CachedPriceMicrocredits
|
||||
if req.LegacyModelIDs != nil {
|
||||
legacyJSON, marshalErr := json.Marshal(normalizeLegacyModelIDs(req.LegacyModelIDs))
|
||||
if marshalErr != nil {
|
||||
return nil, nil, nil, false, marshalErr
|
||||
}
|
||||
item.LegacyModelIDsJSON = string(legacyJSON)
|
||||
}
|
||||
item.UpdatedAt = time.Now()
|
||||
revisionID, err := s.repo.NextPrefixedID("REVISION")
|
||||
if err != nil {
|
||||
@@ -629,7 +746,109 @@ func channelModelCapabilitySpec(channelModel model.ChannelModel) (CapabilitySpec
|
||||
if err != nil {
|
||||
return CapabilitySpec{}, BadAuthRequest("渠道模型能力配置无效,请先修复渠道模型")
|
||||
}
|
||||
return CapabilitySpecFromModelCapabilityConfig(config, normalizeCapability(channelModel.Capability))
|
||||
spec, err := CapabilitySpecFromModelCapabilityConfig(config, normalizeCapability(channelModel.Capability))
|
||||
if err != nil {
|
||||
return CapabilitySpec{}, err
|
||||
}
|
||||
return capabilitySpecWithPriceTiers(spec, channelModel), nil
|
||||
}
|
||||
|
||||
// capabilitySpecWithPriceTiers 只让创作端选择已经启用且可结算的规格。规格组合最终仍由
|
||||
// 路由时的精确价格档匹配保证,避免独立枚举无法表达“分辨率 × 时长”非笛卡尔组合的问题。
|
||||
func capabilitySpecWithPriceTiers(spec CapabilitySpec, channelModel model.ChannelModel) CapabilitySpec {
|
||||
if normalizeCapability(spec.Capability) != "video" || len(channelModel.PriceTiers) == 0 {
|
||||
return spec
|
||||
}
|
||||
tiers := make([]model.ChannelModelPriceTier, 0, len(channelModel.PriceTiers))
|
||||
for _, tier := range channelModel.PriceTiers {
|
||||
if tier.Enabled && tier.PriceConfigured {
|
||||
tiers = append(tiers, tier)
|
||||
}
|
||||
}
|
||||
if len(tiers) == 0 {
|
||||
return spec
|
||||
}
|
||||
result := spec
|
||||
result.Options = make(map[string]OptionConstraint, len(spec.Options))
|
||||
for name, option := range spec.Options {
|
||||
result.Options[name] = option
|
||||
}
|
||||
hasResolutionWildcard, hasDurationWildcard := false, false
|
||||
resolutions := make([]any, 0, len(tiers))
|
||||
durations := make([]any, 0, len(tiers))
|
||||
seenResolutions := make(map[string]bool, len(tiers))
|
||||
seenDurations := make(map[int]bool, len(tiers))
|
||||
for _, tier := range tiers {
|
||||
if normalizeChannelModelTierResolution(tier.Resolution) == "*" {
|
||||
hasResolutionWildcard = true
|
||||
} else if value := normalizeChannelModelTierResolution(tier.Resolution); !seenResolutions[value] {
|
||||
seenResolutions[value] = true
|
||||
resolutions = append(resolutions, value)
|
||||
}
|
||||
if tier.VideoSeconds == 0 {
|
||||
hasDurationWildcard = true
|
||||
} else if !seenDurations[tier.VideoSeconds] {
|
||||
seenDurations[tier.VideoSeconds] = true
|
||||
durations = append(durations, tier.VideoSeconds)
|
||||
}
|
||||
}
|
||||
if !hasResolutionWildcard && len(resolutions) > 0 {
|
||||
result.Options["vquality"] = OptionConstraint{Values: resolutions}
|
||||
}
|
||||
if !hasDurationWildcard && len(durations) > 0 {
|
||||
result.Options["videoSeconds"] = OptionConstraint{Values: durations}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func channelModelDefaultOptions(channelModel model.ChannelModel, spec CapabilitySpec) (map[string]any, error) {
|
||||
config, err := DecodeModelCapabilityConfig(channelModel.CapabilityConfigJSON)
|
||||
if err != nil {
|
||||
return nil, BadAuthRequest("渠道模型能力配置无效,请先修复渠道模型")
|
||||
}
|
||||
defaults := make(map[string]any)
|
||||
if config != nil {
|
||||
switch normalizeCapability(channelModel.Capability) {
|
||||
case "image":
|
||||
if config.Image != nil {
|
||||
defaults["size"] = config.Image.Size.Default
|
||||
if config.Image.Quality.Supported {
|
||||
defaults["quality"] = config.Image.Quality.Default
|
||||
}
|
||||
if config.Image.TransparentBackground.Supported {
|
||||
defaults["transparentBackground"] = config.Image.TransparentBackground.Default
|
||||
}
|
||||
}
|
||||
case "video":
|
||||
if config.Video != nil {
|
||||
defaults["videoSeconds"] = config.Video.Duration.Default
|
||||
defaults["vquality"] = normalizeChannelModelTierResolution(config.Video.DefaultResolution)
|
||||
defaults["size"] = config.Video.DefaultRatio
|
||||
if config.Video.GenerateAudio.Supported {
|
||||
defaults["videoGenerateAudio"] = config.Video.GenerateAudio.Default
|
||||
}
|
||||
if config.Video.Watermark.Supported {
|
||||
defaults["videoWatermark"] = config.Video.Watermark.Default
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 当渠道默认规格没有价格档时,优先选择第一个可结算档,确保初始选择可直接生成。
|
||||
if normalizeCapability(channelModel.Capability) == "video" && channelModelPriceTierForIntent(channelModel, ModelRequestIntent{Capability: "video", Options: defaults}) == nil {
|
||||
for _, tier := range channelModel.PriceTiers {
|
||||
if !tier.Enabled || !tier.PriceConfigured {
|
||||
continue
|
||||
}
|
||||
if tier.Resolution != "*" {
|
||||
defaults["vquality"] = normalizeChannelModelTierResolution(tier.Resolution)
|
||||
}
|
||||
if tier.VideoSeconds > 0 {
|
||||
defaults["videoSeconds"] = tier.VideoSeconds
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return normalizeLogicalDefaults(spec, defaults)
|
||||
}
|
||||
|
||||
func (s *Service) SimulateLogicalModelRoute(actor *model.User, id string, intent ModelRequestIntent) (*RouteSimulationResult, error) {
|
||||
|
||||
Reference in new issue
Block a user