feat(image): 以插件声明统一图片参数、尺寸选择与定价合同 (#760)
* feat(image): 图片参数 - 统一插件声明配置与生成合同 * docs(image): 图片参数 - 说明声明合同与验证边界 * fix(plugins): 图片声明 - 保留内联素材支持并同步制品文档 * fix(image): 参数合同 - 对齐审批意图和尺寸预检调用方 * fix(image): 调用链 - 保留公开意图并接通独立分辨率 * fix(image): 图片参数 - 补齐分辨率接线与目录价格投影
This commit is contained in:
1 parent
88de4ce422
commit
7466119867
104 files changed
+6013
-568
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
"yingce/backend/internal/model"
|
||||
"yingce/backend/internal/protocol"
|
||||
"yingce/backend/internal/repository"
|
||||
)
|
||||
|
||||
@@ -182,17 +183,21 @@ func (s *Service) buildAdminLogicalModel(item model.LogicalModel, graph *reposit
|
||||
channelModelByID[channelModel.ID] = channelModel
|
||||
}
|
||||
admin := AdminLogicalModel{PublicLogicalModel: publicLogicalModel(cachedLogicalModel{Model: item, ProductSpec: productSpec, Defaults: map[string]any{}}, false), Enabled: item.Enabled, ActiveRevisionID: graph.Revision.ID, RevisionVersion: graph.Revision.Version, Routes: []AdminLogicalRoute{}}
|
||||
projectionRoutes := make([]cachedLogicalRoute, 0, len(graph.Routes))
|
||||
for _, route := range graph.Routes {
|
||||
channelModel, channelOK := channelModelByID[route.ChannelModelID]
|
||||
if !channelOK {
|
||||
return nil, errors.New("供应线路引用的渠道模型不存在")
|
||||
}
|
||||
capabilitySpec, specErr := channelModelCapabilitySpec(channelModel)
|
||||
capabilitySpec, specErr := channelModelCapabilitySpec(channelModel, s.protocolRegistry())
|
||||
if specErr != nil {
|
||||
return nil, specErr
|
||||
}
|
||||
_, channelOK = systemChannelByID[channelModel.ChannelID]
|
||||
structurallyAvailable := route.Enabled && route.Weight > 0 && channelModel.Enabled && channelOK
|
||||
if structurallyAvailable {
|
||||
projectionRoutes = append(projectionRoutes, cachedLogicalRoute{Route: route, CapabilitySpec: capabilitySpec, ChannelModel: channelModel})
|
||||
}
|
||||
billingAvailable := item.PricePolicy != "unified" || item.BillingMode != "token" || supportsTokenBilling(item.Capability, channelModel.Protocol)
|
||||
available := structurallyAvailable && billingAvailable && (item.PricePolicy != "channel" || channelModel.PriceConfigured)
|
||||
admin.Routes = append(admin.Routes, AdminLogicalRoute{ID: route.ID, ChannelModelID: channelModel.ID, ChannelID: channelModel.ChannelID, ChannelModelKey: channelModel.ModelKey, ChannelModelName: channelModel.DisplayName, Enabled: route.Enabled, Priority: route.Priority, Weight: route.Weight, Available: available, structurallyAvailable: structurallyAvailable, CapabilitySpec: capabilitySpec})
|
||||
@@ -206,9 +211,8 @@ func (s *Service) buildAdminLogicalModel(item model.LogicalModel, graph *reposit
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
admin.PublicLogicalModel = publicLogicalModel(cachedLogicalModel{Model: item, ProductSpec: productSpec, Defaults: defaults}, false)
|
||||
// publicLogicalModel above has no route list; admin routes are already attached
|
||||
// and the enriched product spec is the source used by the editor.
|
||||
admin.PublicLogicalModel = publicLogicalModel(cachedLogicalModel{Model: item, ProductSpec: productSpec, Defaults: defaults, Routes: projectionRoutes}, false)
|
||||
// Admin and creator share the sanitized price projection of active routes.
|
||||
admin.CapabilitySpec = productSpec
|
||||
admin.DefaultOptions = defaults
|
||||
structuralRouteSpecs := structuralAdminRouteSpecs(admin.Routes)
|
||||
@@ -381,11 +385,11 @@ func (s *Service) logicalModelBundle(actor *model.User, id string, req LogicalMo
|
||||
return nil, nil, nil, false, BadAuthRequest("前台模型只能同步系统渠道模型")
|
||||
}
|
||||
capability = normalizeCapability(source.Capability)
|
||||
derivedSpec, specErr := channelModelCapabilitySpec(*source)
|
||||
derivedSpec, specErr := channelModelCapabilitySpec(*source, s.protocolRegistry())
|
||||
if specErr != nil {
|
||||
return nil, nil, nil, false, specErr
|
||||
}
|
||||
derivedDefaults, defaultsErr := channelModelDefaultOptions(*source, derivedSpec)
|
||||
derivedDefaults, defaultsErr := channelModelDefaultOptions(*source, derivedSpec, s.protocolRegistry())
|
||||
if defaultsErr != nil {
|
||||
return nil, nil, nil, false, defaultsErr
|
||||
}
|
||||
@@ -496,7 +500,7 @@ func (s *Service) logicalModelBundle(actor *model.User, id string, req LogicalMo
|
||||
if modelErr != nil {
|
||||
return nil, nil, nil, false, BadAuthRequest("供应线路引用的渠道模型不存在")
|
||||
}
|
||||
capabilitySpec, specErr := channelModelCapabilitySpec(*channelModel)
|
||||
capabilitySpec, specErr := channelModelCapabilitySpec(*channelModel, s.protocolRegistry())
|
||||
if specErr != nil {
|
||||
return nil, nil, nil, false, specErr
|
||||
}
|
||||
@@ -532,6 +536,22 @@ func (s *Service) logicalModelBundle(actor *model.User, id string, req LogicalMo
|
||||
return nil, nil, nil, false, BadAuthRequest("Token 计费仅支持文本和视频前台模型")
|
||||
}
|
||||
}
|
||||
if capability == "image" && len(structuralRouteSpecs) > 0 {
|
||||
// Rebuild labels, types and pricing eligibility from current routes. The
|
||||
// frontend may restrict values, but cannot invent provider capabilities.
|
||||
reconciled := effectiveImageParameterSpec(req.CapabilitySpec, structuralRouteSpecs)
|
||||
if reconciled.ImageParameters != nil {
|
||||
if err := validateProductSpecWithinRoutes(req.CapabilitySpec, structuralRouteSpecs); err != nil {
|
||||
return nil, nil, nil, false, err
|
||||
}
|
||||
req.CapabilitySpec = reconciled
|
||||
encodedSpec, encodeErr := json.Marshal(reconciled)
|
||||
if encodeErr != nil {
|
||||
return nil, nil, nil, false, encodeErr
|
||||
}
|
||||
revision.CapabilitySpecJSON = string(encodedSpec)
|
||||
}
|
||||
}
|
||||
// 停用必须始终可执行,便于管理员立即阻止失效线路继续对外服务;重新启用时再强校验结构能力和计费可用性。
|
||||
if req.Enabled {
|
||||
if len(structuralRouteSpecs) == 0 {
|
||||
@@ -563,9 +583,18 @@ func supportsLogicalModelTokenBilling(capability string, enabledRouteProtocols [
|
||||
}
|
||||
|
||||
func normalizeLogicalDefaults(spec CapabilitySpec, defaults map[string]any) (map[string]any, error) {
|
||||
if spec.Capability == "image" && spec.ImageParameters != nil {
|
||||
parameters := append([]protocol.ImageParameter(nil), spec.ImageParameters...)
|
||||
for index := range parameters {
|
||||
parameters[index].Required = false
|
||||
}
|
||||
if _, err := resolveImageParameterValues(parameters, defaults); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result := make(map[string]any, len(defaults))
|
||||
for rawName, value := range defaults {
|
||||
name := canonicalCapabilityOptionName(rawName)
|
||||
name := canonicalModelOptionName(spec.Capability, rawName)
|
||||
if _, exists := result[name]; exists {
|
||||
return nil, BadAuthRequest("默认参数存在重复别名:" + name)
|
||||
}
|
||||
@@ -594,13 +623,13 @@ func defaultMap(value map[string]any) map[string]any {
|
||||
return value
|
||||
}
|
||||
|
||||
func channelModelCapabilitySpec(channelModel model.ChannelModel) (CapabilitySpec, error) {
|
||||
func channelModelCapabilitySpec(channelModel model.ChannelModel, registries ...*protocol.Registry) (CapabilitySpec, error) {
|
||||
config, err := DecodeModelCapabilityConfig(channelModel.CapabilityConfigJSON)
|
||||
if err != nil {
|
||||
return CapabilitySpec{}, BadAuthRequest("渠道模型能力配置无效,请先修复渠道模型")
|
||||
}
|
||||
if config != nil {
|
||||
config, err = NormalizeModelCapabilityConfigForModel(normalizeCapability(channelModel.Capability), string(channelModel.Protocol), firstNonEmpty(channelModel.ProviderModelKey, channelModel.ModelKey), config)
|
||||
config, err = NormalizeModelCapabilityConfigForModel(normalizeCapability(channelModel.Capability), string(channelModel.Protocol), firstNonEmpty(channelModel.ProviderModelKey, channelModel.ModelKey), config, registries...)
|
||||
if err != nil {
|
||||
return CapabilitySpec{}, err
|
||||
}
|
||||
@@ -673,18 +702,21 @@ func capabilitySpecWithPriceTiers(spec CapabilitySpec, channelModel model.Channe
|
||||
return result
|
||||
}
|
||||
|
||||
func channelModelDefaultOptions(channelModel model.ChannelModel, spec CapabilitySpec) (map[string]any, error) {
|
||||
func channelModelDefaultOptions(channelModel model.ChannelModel, spec CapabilitySpec, registries ...*protocol.Registry) (map[string]any, error) {
|
||||
config, err := DecodeModelCapabilityConfig(channelModel.CapabilityConfigJSON)
|
||||
if err != nil {
|
||||
return nil, BadAuthRequest("渠道模型能力配置无效,请先修复渠道模型")
|
||||
}
|
||||
if config != nil {
|
||||
config, err = NormalizeModelCapabilityConfigForModel(normalizeCapability(channelModel.Capability), string(channelModel.Protocol), firstNonEmpty(channelModel.ProviderModelKey, channelModel.ModelKey), config)
|
||||
config, err = NormalizeModelCapabilityConfigForModel(normalizeCapability(channelModel.Capability), string(channelModel.Protocol), firstNonEmpty(channelModel.ProviderModelKey, channelModel.ModelKey), config, registries...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
defaults := make(map[string]any)
|
||||
if spec.Capability == "image" && spec.ImageParameters != nil {
|
||||
return normalizeLogicalDefaults(spec, imageParameterDefaults(spec.ImageParameters))
|
||||
}
|
||||
if config != nil {
|
||||
switch normalizeCapability(channelModel.Capability) {
|
||||
case "image":
|
||||
|
||||
Reference in new issue
Block a user