feat(models): 统一模型规格价格与路由

This commit is contained in:
kinoko committed 2026-08-22 17:00:08 +08:00
1 parent d268a938e5
commit 236b850177
36 files changed
+3404 -421

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
package model
import (
"encoding/json"
"strings"
)
// CanonicalSKUSelector stores selector values as strings so an API number and its
// textual equivalent cannot create two price rows for the same purchasable SKU.
func CanonicalSKUSelector(raw map[string]string) (map[string]string, string, error) {
selector := make(map[string]string, len(raw))
for rawKey, rawValue := range raw {
key := strings.TrimSpace(rawKey)
value := strings.TrimSpace(rawValue)
if key == "" || value == "" {
continue
}
selector[key] = value
}
encoded, err := json.Marshal(selector)
if err != nil {
return nil, "", err
}
return selector, string(encoded), nil
}
func DecodeSKUSelector(raw string) map[string]string {
selector := map[string]string{}
if strings.TrimSpace(raw) == "" {
return selector
}
_ = json.Unmarshal([]byte(raw), &selector)
return selector
}
+36 -3
View File
@@ -18,6 +18,8 @@ type ModelChannel struct {
APIFormat string `json:"apiFormat" gorm:"size:24"`
ConcurrencyLimit int `json:"concurrencyLimit"`
ModelsJSON string `json:"modelsJson" gorm:"type:text"`
// RetiredModelsJSON 记录已被一个模型家族吸收的上游 SKU,防止目录拉取时重新创建重复记录。
RetiredModelsJSON string `json:"-" gorm:"type:text"`
HeadersJSON string `json:"-" gorm:"type:text"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
@@ -28,6 +30,7 @@ type ChannelModel struct {
ID string `json:"id" gorm:"primaryKey;size:36"`
ChannelID string `json:"channelId" gorm:"size:36;index;uniqueIndex:idx_channel_model_key_active,priority:1,where:deleted_at IS NULL"`
ModelKey string `json:"modelKey" gorm:"size:120;uniqueIndex:idx_channel_model_key_active,priority:2,where:deleted_at IS NULL"`
ProviderModelKey string `json:"providerModelKey" gorm:"size:120"`
DisplayName string `json:"displayName" gorm:"size:160"`
Capability string `json:"capability" gorm:"size:32;index"`
Protocol ChannelInterfaceType `json:"protocol" gorm:"size:32;index"`
@@ -42,9 +45,39 @@ type ChannelModel struct {
CapabilityConfigJSON string `json:"-" gorm:"type:text"`
CapabilityVersion int64 `json:"capabilityVersion"`
CapabilityConfig map[string]any `json:"capabilityConfig,omitempty" gorm:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// PriceTiers 是系统渠道模型的价格真相。标量价格仅为旧调用与历史数据兼容,
// 新的创作端报价必须按所选规格命中一个价格档。
PriceTiers []ChannelModelPriceTier `json:"priceTiers" gorm:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// ChannelModelPriceTier 表示渠道模型在一个规格组合下的可执行上游 SKU 和用户价格。
// SelectorJSON 是规格选择器真相,例如 {"operation":"image_to_video","vquality":"720p",
// "videoSeconds":"10"} 或 {"operation":"text_to_image","quality":"2k"}。
// SelectorKey 是规范化 JSON,用于 PostgreSQL 的活动规格唯一约束。旧的 Resolution / VideoSeconds
// 仅保留给历史 API 与升级回填,不能再作为 SKU 唯一性依据。
type ChannelModelPriceTier struct {
ID string `json:"id" gorm:"primaryKey;size:36"`
ChannelModelID string `json:"channelModelId" gorm:"size:36;index;uniqueIndex:idx_channel_model_price_tier_active,priority:1,where:deleted_at IS NULL"`
SelectorKey string `json:"selectorKey" gorm:"size:500;not null;default:{};uniqueIndex:idx_channel_model_price_tier_active,priority:2,where:deleted_at IS NULL"`
SelectorJSON string `json:"-" gorm:"type:text;not null;default:{}"`
Selector map[string]string `json:"selector,omitempty" gorm:"-"`
Resolution string `json:"resolution" gorm:"size:24;not null;default:*"`
VideoSeconds int `json:"videoSeconds" gorm:"not null;default:0"`
ProviderModelKey string `json:"providerModelKey" gorm:"size:120"`
BillingMode string `json:"billingMode" gorm:"size:32"`
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
InputTokenPriceMicrocredits int64 `json:"inputTokenPriceMicrocredits"`
OutputTokenPriceMicrocredits int64 `json:"outputTokenPriceMicrocredits"`
CachedTokenPriceMicrocredits int64 `json:"cachedTokenPriceMicrocredits"`
PriceConfigured bool `json:"priceConfigured" gorm:"index"`
Enabled bool `json:"enabled" gorm:"index"`
PriceVersion int64 `json:"priceVersion"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
type ApiCallLog struct {
+4
View File
@@ -38,6 +38,10 @@ type BillingOrder struct {
TaskID string `json:"taskId,omitempty" gorm:"index;size:36"`
ChannelID string `json:"channelId" gorm:"index;size:36"`
ChannelModelID string `json:"channelModelId" gorm:"index;size:36"`
// PriceTierID/Version 记录任务实际命中的规格档;金额字段仍是不可变结算快照。
PriceTierID string `json:"priceTierId,omitempty" gorm:"index;size:36"`
PriceTierVersion int64 `json:"priceTierVersion"`
PriceSelectorJSON string `json:"-" gorm:"type:text"`
Model string `json:"model" gorm:"index;size:120"`
Capability string `json:"capability" gorm:"index;size:32"`
Scene string `json:"scene" gorm:"index;size:80"`
@@ -20,14 +20,20 @@ type LogicalModel struct {
Enabled bool `json:"enabled" gorm:"index"`
SortOrder int `json:"sortOrder" gorm:"index"`
// RevisionSequence 记录已经分配的 revision 版本号,由数据库原子递增。
RevisionSequence int `json:"-" gorm:"not null;default:0"`
ActiveRevisionID string `json:"activeRevisionId" gorm:"size:36;index"`
RevisionSequence int `json:"-" gorm:"not null;default:0"`
ActiveRevisionID string `json:"activeRevisionId" gorm:"size:36;index"`
// SourceChannelModelID 标识这个前台目录项由哪个系统渠道模型投影而来。
// 管理员不再重复维护能力和价格,渠道模型保存时会创建新的前台 revision。
SourceChannelModelID string `json:"sourceChannelModelId,omitempty" gorm:"size:36;index"`
PricePolicy string `json:"pricePolicy" gorm:"size:24;default:unified"`
BillingMode string `json:"billingMode" gorm:"size:32"`
UnitPriceMicrocredits int64 `json:"unitPriceMicrocredits"`
InputPriceMicrocredits int64 `json:"inputPriceMicrocredits"`
OutputPriceMicrocredits int64 `json:"outputPriceMicrocredits"`
CachedPriceMicrocredits int64 `json:"cachedPriceMicrocredits"`
// LegacyModelIDsJSON 用于创作端把已保存的旧 SKU 选择无缝映射到新的模型家族。
// 这只是目录兼容信息,任务、路由尝试和账单仍固定引用其创建时的 ID 快照。
LegacyModelIDsJSON string `json:"-" gorm:"type:text"`
// ArchivedAt 仅从可选目录隐藏模型;历史任务、计费和审计仍需读取主体及不可变 revision。
ArchivedAt *time.Time `json:"-" gorm:"index"`
CreatedAt time.Time `json:"createdAt"`