feat(groups): 分组支持强制 OpenAI Ultra Fast 档位

OpenAI / Composite 分组新增 force_openai_ultrafast:开启后该分组经 OpenAI 账号
转发的请求强制写入 service_tier=ultrafast,覆盖客户端传入的值。

- 与 force_openai_fast 互斥,服务层同时为真返回 400
  (GROUP_OPENAI_FORCED_SERVICE_TIER_CONFLICT);不支持的平台两者一并清零
- HTTP Forward、原始 body(chat / messages / 透传)与 WebSocket
  response.create 三条路径统一经 openAIGroupForcedServiceTier 取强制档位
- 分组强制先写入,全局 Fast/Flex 策略随后裁决,仍可按模型白名单过滤、
  拦截或降为 priority
- 计费不变:Ultrafast 按 Ultrafast 价计费,"免费 Fast"只覆盖 priority
- 迁移 251 仅新增列(默认 false);鉴权缓存快照版本升至 28
- 管理端分组表单把"强制使用 Fast"开关改为三选一:跟随客户端 / 强制 Fast /
  强制 Ultra Fast

Closes #8657
This commit is contained in:
shaw committed 2026-11-14 23:35:49 +08:00
1 parent 530c5e3a4f
commit 330198321f
44 files changed
+1262 -49

No files matched your search

+12 -1
View File
@@ -128,6 +128,8 @@ type Group struct {
AllowLive bool `json:"allow_live,omitempty"`
// 是否强制此 OpenAI/Composite 分组请求使用 service_tier=priority
ForceOpenaiFast bool `json:"force_openai_fast,omitempty"`
// 是否强制此 OpenAI/Composite 分组请求使用 service_tier=ultrafast(与 force_openai_fast 互斥)
ForceOpenaiUltrafast bool `json:"force_openai_ultrafast,omitempty"`
// 是否让此 OpenAI/Composite 分组的 Fast 请求按 Standard 价格计费
FreeOpenaiFast bool `json:"free_openai_fast,omitempty"`
// 仅允许非 apikey 类型账号关联到此分组
@@ -264,7 +266,7 @@ func (*Group) scanValues(columns []string) ([]any, error) {
switch columns[i] {
case group.FieldI18n, group.FieldVideoModelPrices, group.FieldModelPricing, group.FieldModelRouting, group.FieldSupportedModelScopes, group.FieldMessagesDispatchModelConfig, group.FieldModelAllowlist, group.FieldCodexModelsManifestConfig, group.FieldReasoningEffortMappings:
values[i] = new([]byte)
case group.FieldPeakRateEnabled, group.FieldIsExclusive, group.FieldAllowImageGeneration, group.FieldAllowBatchImageGeneration, group.FieldImageCountBilling, group.FieldImageRateIndependent, group.FieldVideoRateIndependent, group.FieldLongContextPricingEnabled, group.FieldClaudeCodeOnly, group.FieldModelRoutingEnabled, group.FieldMcpXMLInject, group.FieldAllowMessagesDispatch, group.FieldAllowLive, group.FieldForceOpenaiFast, group.FieldFreeOpenaiFast, group.FieldRequireOauthOnly, group.FieldRequirePrivacySet, group.FieldProfitControlEnabled:
case group.FieldPeakRateEnabled, group.FieldIsExclusive, group.FieldAllowImageGeneration, group.FieldAllowBatchImageGeneration, group.FieldImageCountBilling, group.FieldImageRateIndependent, group.FieldVideoRateIndependent, group.FieldLongContextPricingEnabled, group.FieldClaudeCodeOnly, group.FieldModelRoutingEnabled, group.FieldMcpXMLInject, group.FieldAllowMessagesDispatch, group.FieldAllowLive, group.FieldForceOpenaiFast, group.FieldForceOpenaiUltrafast, group.FieldFreeOpenaiFast, group.FieldRequireOauthOnly, group.FieldRequirePrivacySet, group.FieldProfitControlEnabled:
values[i] = new(sql.NullBool)
case group.FieldRateMultiplier, group.FieldPeakRateMultiplier, group.FieldDailyLimitUsd, group.FieldWeeklyLimitUsd, group.FieldMonthlyLimitUsd, group.FieldImageRateMultiplier, group.FieldImagePrice1k, group.FieldImagePrice2k, group.FieldImagePrice4k, group.FieldBatchImageDiscountMultiplier, group.FieldBatchImageHoldMultiplier, group.FieldVideoRateMultiplier, group.FieldVideoPrice480p, group.FieldVideoPrice720p, group.FieldVideoPrice1080p, group.FieldWebSearchPricePerCall, group.FieldSearchPricePer1k, group.FieldAudioRealtimePricePerMin, group.FieldAudioTtsPricePerMillionChars, group.FieldAudioSttPricePerHour, group.FieldProfitMinMargin, group.FieldProfitSafetyBuffer:
values[i] = new(sql.NullFloat64)
@@ -648,6 +650,12 @@ func (_m *Group) assignValues(columns []string, values []any) error {
} else if value.Valid {
_m.ForceOpenaiFast = value.Bool
}
case group.FieldForceOpenaiUltrafast:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field force_openai_ultrafast", values[i])
} else if value.Valid {
_m.ForceOpenaiUltrafast = value.Bool
}
case group.FieldFreeOpenaiFast:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field free_openai_fast", values[i])
@@ -1016,6 +1024,9 @@ func (_m *Group) String() string {
builder.WriteString("force_openai_fast=")
builder.WriteString(fmt.Sprintf("%v", _m.ForceOpenaiFast))
builder.WriteString(", ")
builder.WriteString("force_openai_ultrafast=")
builder.WriteString(fmt.Sprintf("%v", _m.ForceOpenaiUltrafast))
builder.WriteString(", ")
builder.WriteString("free_openai_fast=")
builder.WriteString(fmt.Sprintf("%v", _m.FreeOpenaiFast))
builder.WriteString(", ")
+10
View File
@@ -124,6 +124,8 @@ const (
FieldAllowLive = "allow_live"
// FieldForceOpenaiFast holds the string denoting the force_openai_fast field in the database.
FieldForceOpenaiFast = "force_openai_fast"
// FieldForceOpenaiUltrafast holds the string denoting the force_openai_ultrafast field in the database.
FieldForceOpenaiUltrafast = "force_openai_ultrafast"
// FieldFreeOpenaiFast holds the string denoting the free_openai_fast field in the database.
FieldFreeOpenaiFast = "free_openai_fast"
// FieldRequireOauthOnly holds the string denoting the require_oauth_only field in the database.
@@ -281,6 +283,7 @@ var Columns = []string{
FieldAllowMessagesDispatch,
FieldAllowLive,
FieldForceOpenaiFast,
FieldForceOpenaiUltrafast,
FieldFreeOpenaiFast,
FieldRequireOauthOnly,
FieldRequirePrivacySet,
@@ -410,6 +413,8 @@ var (
DefaultAllowLive bool
// DefaultForceOpenaiFast holds the default value on creation for the "force_openai_fast" field.
DefaultForceOpenaiFast bool
// DefaultForceOpenaiUltrafast holds the default value on creation for the "force_openai_ultrafast" field.
DefaultForceOpenaiUltrafast bool
// DefaultFreeOpenaiFast holds the default value on creation for the "free_openai_fast" field.
DefaultFreeOpenaiFast bool
// DefaultRequireOauthOnly holds the default value on creation for the "require_oauth_only" field.
@@ -699,6 +704,11 @@ func ByForceOpenaiFast(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldForceOpenaiFast, opts...).ToFunc()
}
// ByForceOpenaiUltrafast orders the results by the force_openai_ultrafast field.
func ByForceOpenaiUltrafast(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldForceOpenaiUltrafast, opts...).ToFunc()
}
// ByFreeOpenaiFast orders the results by the free_openai_fast field.
func ByFreeOpenaiFast(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldFreeOpenaiFast, opts...).ToFunc()
+15
View File
@@ -300,6 +300,11 @@ func ForceOpenaiFast(v bool) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldForceOpenaiFast, v))
}
// ForceOpenaiUltrafast applies equality check predicate on the "force_openai_ultrafast" field. It's identical to ForceOpenaiUltrafastEQ.
func ForceOpenaiUltrafast(v bool) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldForceOpenaiUltrafast, v))
}
// FreeOpenaiFast applies equality check predicate on the "free_openai_fast" field. It's identical to FreeOpenaiFastEQ.
func FreeOpenaiFast(v bool) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldFreeOpenaiFast, v))
@@ -2310,6 +2315,16 @@ func ForceOpenaiFastNEQ(v bool) predicate.Group {
return predicate.Group(sql.FieldNEQ(FieldForceOpenaiFast, v))
}
// ForceOpenaiUltrafastEQ applies the EQ predicate on the "force_openai_ultrafast" field.
func ForceOpenaiUltrafastEQ(v bool) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldForceOpenaiUltrafast, v))
}
// ForceOpenaiUltrafastNEQ applies the NEQ predicate on the "force_openai_ultrafast" field.
func ForceOpenaiUltrafastNEQ(v bool) predicate.Group {
return predicate.Group(sql.FieldNEQ(FieldForceOpenaiUltrafast, v))
}
// FreeOpenaiFastEQ applies the EQ predicate on the "free_openai_fast" field.
func FreeOpenaiFastEQ(v bool) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldFreeOpenaiFast, v))
+65
View File
@@ -738,6 +738,20 @@ func (_c *GroupCreate) SetNillableForceOpenaiFast(v *bool) *GroupCreate {
return _c
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (_c *GroupCreate) SetForceOpenaiUltrafast(v bool) *GroupCreate {
_c.mutation.SetForceOpenaiUltrafast(v)
return _c
}
// SetNillableForceOpenaiUltrafast sets the "force_openai_ultrafast" field if the given value is not nil.
func (_c *GroupCreate) SetNillableForceOpenaiUltrafast(v *bool) *GroupCreate {
if v != nil {
_c.SetForceOpenaiUltrafast(*v)
}
return _c
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (_c *GroupCreate) SetFreeOpenaiFast(v bool) *GroupCreate {
_c.mutation.SetFreeOpenaiFast(v)
@@ -1183,6 +1197,10 @@ func (_c *GroupCreate) defaults() error {
v := group.DefaultForceOpenaiFast
_c.mutation.SetForceOpenaiFast(v)
}
if _, ok := _c.mutation.ForceOpenaiUltrafast(); !ok {
v := group.DefaultForceOpenaiUltrafast
_c.mutation.SetForceOpenaiUltrafast(v)
}
if _, ok := _c.mutation.FreeOpenaiFast(); !ok {
v := group.DefaultFreeOpenaiFast
_c.mutation.SetFreeOpenaiFast(v)
@@ -1395,6 +1413,9 @@ func (_c *GroupCreate) check() error {
if _, ok := _c.mutation.ForceOpenaiFast(); !ok {
return &ValidationError{Name: "force_openai_fast", err: errors.New(`ent: missing required field "Group.force_openai_fast"`)}
}
if _, ok := _c.mutation.ForceOpenaiUltrafast(); !ok {
return &ValidationError{Name: "force_openai_ultrafast", err: errors.New(`ent: missing required field "Group.force_openai_ultrafast"`)}
}
if _, ok := _c.mutation.FreeOpenaiFast(); !ok {
return &ValidationError{Name: "free_openai_fast", err: errors.New(`ent: missing required field "Group.free_openai_fast"`)}
}
@@ -1695,6 +1716,10 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
_spec.SetField(group.FieldForceOpenaiFast, field.TypeBool, value)
_node.ForceOpenaiFast = value
}
if value, ok := _c.mutation.ForceOpenaiUltrafast(); ok {
_spec.SetField(group.FieldForceOpenaiUltrafast, field.TypeBool, value)
_node.ForceOpenaiUltrafast = value
}
if value, ok := _c.mutation.FreeOpenaiFast(); ok {
_spec.SetField(group.FieldFreeOpenaiFast, field.TypeBool, value)
_node.FreeOpenaiFast = value
@@ -2801,6 +2826,18 @@ func (u *GroupUpsert) UpdateForceOpenaiFast() *GroupUpsert {
return u
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (u *GroupUpsert) SetForceOpenaiUltrafast(v bool) *GroupUpsert {
u.Set(group.FieldForceOpenaiUltrafast, v)
return u
}
// UpdateForceOpenaiUltrafast sets the "force_openai_ultrafast" field to the value that was provided on create.
func (u *GroupUpsert) UpdateForceOpenaiUltrafast() *GroupUpsert {
u.SetExcluded(group.FieldForceOpenaiUltrafast)
return u
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (u *GroupUpsert) SetFreeOpenaiFast(v bool) *GroupUpsert {
u.Set(group.FieldFreeOpenaiFast, v)
@@ -4078,6 +4115,20 @@ func (u *GroupUpsertOne) UpdateForceOpenaiFast() *GroupUpsertOne {
})
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (u *GroupUpsertOne) SetForceOpenaiUltrafast(v bool) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.SetForceOpenaiUltrafast(v)
})
}
// UpdateForceOpenaiUltrafast sets the "force_openai_ultrafast" field to the value that was provided on create.
func (u *GroupUpsertOne) UpdateForceOpenaiUltrafast() *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.UpdateForceOpenaiUltrafast()
})
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (u *GroupUpsertOne) SetFreeOpenaiFast(v bool) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
@@ -5552,6 +5603,20 @@ func (u *GroupUpsertBulk) UpdateForceOpenaiFast() *GroupUpsertBulk {
})
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (u *GroupUpsertBulk) SetForceOpenaiUltrafast(v bool) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.SetForceOpenaiUltrafast(v)
})
}
// UpdateForceOpenaiUltrafast sets the "force_openai_ultrafast" field to the value that was provided on create.
func (u *GroupUpsertBulk) UpdateForceOpenaiUltrafast() *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.UpdateForceOpenaiUltrafast()
})
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (u *GroupUpsertBulk) SetFreeOpenaiFast(v bool) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
+34
View File
@@ -1023,6 +1023,20 @@ func (_u *GroupUpdate) SetNillableForceOpenaiFast(v *bool) *GroupUpdate {
return _u
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (_u *GroupUpdate) SetForceOpenaiUltrafast(v bool) *GroupUpdate {
_u.mutation.SetForceOpenaiUltrafast(v)
return _u
}
// SetNillableForceOpenaiUltrafast sets the "force_openai_ultrafast" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableForceOpenaiUltrafast(v *bool) *GroupUpdate {
if v != nil {
_u.SetForceOpenaiUltrafast(*v)
}
return _u
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (_u *GroupUpdate) SetFreeOpenaiFast(v bool) *GroupUpdate {
_u.mutation.SetFreeOpenaiFast(v)
@@ -1887,6 +1901,9 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if value, ok := _u.mutation.ForceOpenaiFast(); ok {
_spec.SetField(group.FieldForceOpenaiFast, field.TypeBool, value)
}
if value, ok := _u.mutation.ForceOpenaiUltrafast(); ok {
_spec.SetField(group.FieldForceOpenaiUltrafast, field.TypeBool, value)
}
if value, ok := _u.mutation.FreeOpenaiFast(); ok {
_spec.SetField(group.FieldFreeOpenaiFast, field.TypeBool, value)
}
@@ -3243,6 +3260,20 @@ func (_u *GroupUpdateOne) SetNillableForceOpenaiFast(v *bool) *GroupUpdateOne {
return _u
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (_u *GroupUpdateOne) SetForceOpenaiUltrafast(v bool) *GroupUpdateOne {
_u.mutation.SetForceOpenaiUltrafast(v)
return _u
}
// SetNillableForceOpenaiUltrafast sets the "force_openai_ultrafast" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableForceOpenaiUltrafast(v *bool) *GroupUpdateOne {
if v != nil {
_u.SetForceOpenaiUltrafast(*v)
}
return _u
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (_u *GroupUpdateOne) SetFreeOpenaiFast(v bool) *GroupUpdateOne {
_u.mutation.SetFreeOpenaiFast(v)
@@ -4137,6 +4168,9 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error)
if value, ok := _u.mutation.ForceOpenaiFast(); ok {
_spec.SetField(group.FieldForceOpenaiFast, field.TypeBool, value)
}
if value, ok := _u.mutation.ForceOpenaiUltrafast(); ok {
_spec.SetField(group.FieldForceOpenaiUltrafast, field.TypeBool, value)
}
if value, ok := _u.mutation.FreeOpenaiFast(); ok {
_spec.SetField(group.FieldFreeOpenaiFast, field.TypeBool, value)
}
+1
View File
@@ -957,6 +957,7 @@ var (
{Name: "allow_messages_dispatch", Type: field.TypeBool, Default: false},
{Name: "allow_live", Type: field.TypeBool, Default: false},
{Name: "force_openai_fast", Type: field.TypeBool, Default: false},
{Name: "force_openai_ultrafast", Type: field.TypeBool, Default: false},
{Name: "free_openai_fast", Type: field.TypeBool, Default: false},
{Name: "require_oauth_only", Type: field.TypeBool, Default: false},
{Name: "require_privacy_set", Type: field.TypeBool, Default: false},
+55 -1
View File
@@ -22161,6 +22161,7 @@ type GroupMutation struct {
allow_messages_dispatch *bool
allow_live *bool
force_openai_fast *bool
force_openai_ultrafast *bool
free_openai_fast *bool
require_oauth_only *bool
require_privacy_set *bool
@@ -25058,6 +25059,42 @@ func (m *GroupMutation) ResetForceOpenaiFast() {
m.force_openai_fast = nil
}
// SetForceOpenaiUltrafast sets the "force_openai_ultrafast" field.
func (m *GroupMutation) SetForceOpenaiUltrafast(b bool) {
m.force_openai_ultrafast = &b
}
// ForceOpenaiUltrafast returns the value of the "force_openai_ultrafast" field in the mutation.
func (m *GroupMutation) ForceOpenaiUltrafast() (r bool, exists bool) {
v := m.force_openai_ultrafast
if v == nil {
return
}
return *v, true
}
// OldForceOpenaiUltrafast returns the old "force_openai_ultrafast" field's value of the Group entity.
// If the Group object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *GroupMutation) OldForceOpenaiUltrafast(ctx context.Context) (v bool, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldForceOpenaiUltrafast is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldForceOpenaiUltrafast requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldForceOpenaiUltrafast: %w", err)
}
return oldValue.ForceOpenaiUltrafast, nil
}
// ResetForceOpenaiUltrafast resets all changes to the "force_openai_ultrafast" field.
func (m *GroupMutation) ResetForceOpenaiUltrafast() {
m.force_openai_ultrafast = nil
}
// SetFreeOpenaiFast sets the "free_openai_fast" field.
func (m *GroupMutation) SetFreeOpenaiFast(b bool) {
m.free_openai_fast = &b
@@ -25995,7 +26032,7 @@ func (m *GroupMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *GroupMutation) Fields() []string {
fields := make([]string, 0, 68)
fields := make([]string, 0, 69)
if m.created_at != nil {
fields = append(fields, group.FieldCreatedAt)
}
@@ -26158,6 +26195,9 @@ func (m *GroupMutation) Fields() []string {
if m.force_openai_fast != nil {
fields = append(fields, group.FieldForceOpenaiFast)
}
if m.force_openai_ultrafast != nil {
fields = append(fields, group.FieldForceOpenaiUltrafast)
}
if m.free_openai_fast != nil {
fields = append(fields, group.FieldFreeOpenaiFast)
}
@@ -26316,6 +26356,8 @@ func (m *GroupMutation) Field(name string) (ent.Value, bool) {
return m.AllowLive()
case group.FieldForceOpenaiFast:
return m.ForceOpenaiFast()
case group.FieldForceOpenaiUltrafast:
return m.ForceOpenaiUltrafast()
case group.FieldFreeOpenaiFast:
return m.FreeOpenaiFast()
case group.FieldRequireOauthOnly:
@@ -26461,6 +26503,8 @@ func (m *GroupMutation) OldField(ctx context.Context, name string) (ent.Value, e
return m.OldAllowLive(ctx)
case group.FieldForceOpenaiFast:
return m.OldForceOpenaiFast(ctx)
case group.FieldForceOpenaiUltrafast:
return m.OldForceOpenaiUltrafast(ctx)
case group.FieldFreeOpenaiFast:
return m.OldFreeOpenaiFast(ctx)
case group.FieldRequireOauthOnly:
@@ -26876,6 +26920,13 @@ func (m *GroupMutation) SetField(name string, value ent.Value) error {
}
m.SetForceOpenaiFast(v)
return nil
case group.FieldForceOpenaiUltrafast:
v, ok := value.(bool)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetForceOpenaiUltrafast(v)
return nil
case group.FieldFreeOpenaiFast:
v, ok := value.(bool)
if !ok {
@@ -27647,6 +27698,9 @@ func (m *GroupMutation) ResetField(name string) error {
case group.FieldForceOpenaiFast:
m.ResetForceOpenaiFast()
return nil
case group.FieldForceOpenaiUltrafast:
m.ResetForceOpenaiUltrafast()
return nil
case group.FieldFreeOpenaiFast:
m.ResetFreeOpenaiFast()
return nil
+18 -14
View File
@@ -1183,66 +1183,70 @@ func init() {
groupDescForceOpenaiFast := groupFields[50].Descriptor()
// group.DefaultForceOpenaiFast holds the default value on creation for the force_openai_fast field.
group.DefaultForceOpenaiFast = groupDescForceOpenaiFast.Default.(bool)
// groupDescForceOpenaiUltrafast is the schema descriptor for force_openai_ultrafast field.
groupDescForceOpenaiUltrafast := groupFields[51].Descriptor()
// group.DefaultForceOpenaiUltrafast holds the default value on creation for the force_openai_ultrafast field.
group.DefaultForceOpenaiUltrafast = groupDescForceOpenaiUltrafast.Default.(bool)
// groupDescFreeOpenaiFast is the schema descriptor for free_openai_fast field.
groupDescFreeOpenaiFast := groupFields[51].Descriptor()
groupDescFreeOpenaiFast := groupFields[52].Descriptor()
// group.DefaultFreeOpenaiFast holds the default value on creation for the free_openai_fast field.
group.DefaultFreeOpenaiFast = groupDescFreeOpenaiFast.Default.(bool)
// groupDescRequireOauthOnly is the schema descriptor for require_oauth_only field.
groupDescRequireOauthOnly := groupFields[52].Descriptor()
groupDescRequireOauthOnly := groupFields[53].Descriptor()
// group.DefaultRequireOauthOnly holds the default value on creation for the require_oauth_only field.
group.DefaultRequireOauthOnly = groupDescRequireOauthOnly.Default.(bool)
// groupDescRequirePrivacySet is the schema descriptor for require_privacy_set field.
groupDescRequirePrivacySet := groupFields[53].Descriptor()
groupDescRequirePrivacySet := groupFields[54].Descriptor()
// group.DefaultRequirePrivacySet holds the default value on creation for the require_privacy_set field.
group.DefaultRequirePrivacySet = groupDescRequirePrivacySet.Default.(bool)
// groupDescDefaultMappedModel is the schema descriptor for default_mapped_model field.
groupDescDefaultMappedModel := groupFields[54].Descriptor()
groupDescDefaultMappedModel := groupFields[55].Descriptor()
// group.DefaultDefaultMappedModel holds the default value on creation for the default_mapped_model field.
group.DefaultDefaultMappedModel = groupDescDefaultMappedModel.Default.(string)
// group.DefaultMappedModelValidator is a validator for the "default_mapped_model" field. It is called by the builders before save.
group.DefaultMappedModelValidator = groupDescDefaultMappedModel.Validators[0].(func(string) error)
// groupDescMessagesDispatchModelConfig is the schema descriptor for messages_dispatch_model_config field.
groupDescMessagesDispatchModelConfig := groupFields[55].Descriptor()
groupDescMessagesDispatchModelConfig := groupFields[56].Descriptor()
// group.DefaultMessagesDispatchModelConfig holds the default value on creation for the messages_dispatch_model_config field.
group.DefaultMessagesDispatchModelConfig = groupDescMessagesDispatchModelConfig.Default.(domain.OpenAIMessagesDispatchModelConfig)
// groupDescModelAllowlist is the schema descriptor for model_allowlist field.
groupDescModelAllowlist := groupFields[56].Descriptor()
groupDescModelAllowlist := groupFields[57].Descriptor()
// group.DefaultModelAllowlist holds the default value on creation for the model_allowlist field.
group.DefaultModelAllowlist = groupDescModelAllowlist.Default.(domain.GroupModelAllowlist)
// groupDescCodexModelsManifestConfig is the schema descriptor for codex_models_manifest_config field.
groupDescCodexModelsManifestConfig := groupFields[57].Descriptor()
groupDescCodexModelsManifestConfig := groupFields[58].Descriptor()
// group.DefaultCodexModelsManifestConfig holds the default value on creation for the codex_models_manifest_config field.
group.DefaultCodexModelsManifestConfig = groupDescCodexModelsManifestConfig.Default.(domain.GroupCodexModelsManifestConfig)
// groupDescRpmLimit is the schema descriptor for rpm_limit field.
groupDescRpmLimit := groupFields[58].Descriptor()
groupDescRpmLimit := groupFields[59].Descriptor()
// group.DefaultRpmLimit holds the default value on creation for the rpm_limit field.
group.DefaultRpmLimit = groupDescRpmLimit.Default.(int)
// groupDescMaxReasoningEffort is the schema descriptor for max_reasoning_effort field.
groupDescMaxReasoningEffort := groupFields[59].Descriptor()
groupDescMaxReasoningEffort := groupFields[60].Descriptor()
// group.DefaultMaxReasoningEffort holds the default value on creation for the max_reasoning_effort field.
group.DefaultMaxReasoningEffort = groupDescMaxReasoningEffort.Default.(string)
// group.MaxReasoningEffortValidator is a validator for the "max_reasoning_effort" field. It is called by the builders before save.
group.MaxReasoningEffortValidator = groupDescMaxReasoningEffort.Validators[0].(func(string) error)
// groupDescMaxReasoningEffortOverLimit is the schema descriptor for max_reasoning_effort_over_limit field.
groupDescMaxReasoningEffortOverLimit := groupFields[60].Descriptor()
groupDescMaxReasoningEffortOverLimit := groupFields[61].Descriptor()
// group.DefaultMaxReasoningEffortOverLimit holds the default value on creation for the max_reasoning_effort_over_limit field.
group.DefaultMaxReasoningEffortOverLimit = groupDescMaxReasoningEffortOverLimit.Default.(string)
// group.MaxReasoningEffortOverLimitValidator is a validator for the "max_reasoning_effort_over_limit" field. It is called by the builders before save.
group.MaxReasoningEffortOverLimitValidator = groupDescMaxReasoningEffortOverLimit.Validators[0].(func(string) error)
// groupDescReasoningEffortMappings is the schema descriptor for reasoning_effort_mappings field.
groupDescReasoningEffortMappings := groupFields[61].Descriptor()
groupDescReasoningEffortMappings := groupFields[62].Descriptor()
// group.DefaultReasoningEffortMappings holds the default value on creation for the reasoning_effort_mappings field.
group.DefaultReasoningEffortMappings = groupDescReasoningEffortMappings.Default.([]domain.ReasoningEffortMapping)
// groupDescProfitControlEnabled is the schema descriptor for profit_control_enabled field.
groupDescProfitControlEnabled := groupFields[62].Descriptor()
groupDescProfitControlEnabled := groupFields[63].Descriptor()
// group.DefaultProfitControlEnabled holds the default value on creation for the profit_control_enabled field.
group.DefaultProfitControlEnabled = groupDescProfitControlEnabled.Default.(bool)
// groupDescProfitMinMargin is the schema descriptor for profit_min_margin field.
groupDescProfitMinMargin := groupFields[63].Descriptor()
groupDescProfitMinMargin := groupFields[64].Descriptor()
// group.DefaultProfitMinMargin holds the default value on creation for the profit_min_margin field.
group.DefaultProfitMinMargin = groupDescProfitMinMargin.Default.(float64)
// groupDescProfitSafetyBuffer is the schema descriptor for profit_safety_buffer field.
groupDescProfitSafetyBuffer := groupFields[64].Descriptor()
groupDescProfitSafetyBuffer := groupFields[65].Descriptor()
// group.DefaultProfitSafetyBuffer holds the default value on creation for the profit_safety_buffer field.
group.DefaultProfitSafetyBuffer = groupDescProfitSafetyBuffer.Default.(float64)
idempotencyrecordMixin := schema.IdempotencyRecord{}.Mixin()
+3
View File
@@ -253,6 +253,9 @@ func (Group) Fields() []ent.Field {
field.Bool("force_openai_fast").
Default(false).
Comment("是否强制此 OpenAI/Composite 分组请求使用 service_tier=priority"),
field.Bool("force_openai_ultrafast").
Default(false).
Comment("是否强制此 OpenAI/Composite 分组请求使用 service_tier=ultrafast(与 force_openai_fast 互斥)"),
field.Bool("free_openai_fast").
Default(false).
Comment("是否让此 OpenAI/Composite 分组的 Fast 请求按 Standard 价格计费"),
@@ -239,6 +239,7 @@ type CreateGroupRequest struct {
AllowMessagesDispatch bool `json:"allow_messages_dispatch"`
AllowLive bool `json:"allow_live"`
ForceOpenAIFast bool `json:"force_openai_fast"`
ForceOpenAIUltrafast bool `json:"force_openai_ultrafast"`
FreeOpenAIFast bool `json:"free_openai_fast"`
RequireOAuthOnly bool `json:"require_oauth_only"`
RequirePrivacySet bool `json:"require_privacy_set"`
@@ -316,6 +317,7 @@ type UpdateGroupRequest struct {
AllowMessagesDispatch *bool `json:"allow_messages_dispatch"`
AllowLive *bool `json:"allow_live"`
ForceOpenAIFast *bool `json:"force_openai_fast"`
ForceOpenAIUltrafast *bool `json:"force_openai_ultrafast"`
FreeOpenAIFast *bool `json:"free_openai_fast"`
RequireOAuthOnly *bool `json:"require_oauth_only"`
RequirePrivacySet *bool `json:"require_privacy_set"`
@@ -719,6 +721,7 @@ func (h *GroupHandler) Create(c *gin.Context) {
AllowMessagesDispatch: req.AllowMessagesDispatch,
AllowLive: req.AllowLive,
ForceOpenAIFast: req.ForceOpenAIFast,
ForceOpenAIUltrafast: req.ForceOpenAIUltrafast,
FreeOpenAIFast: req.FreeOpenAIFast,
RequireOAuthOnly: req.RequireOAuthOnly,
RequirePrivacySet: req.RequirePrivacySet,
@@ -867,6 +870,7 @@ func (h *GroupHandler) Update(c *gin.Context) {
AllowMessagesDispatch: req.AllowMessagesDispatch,
AllowLive: req.AllowLive,
ForceOpenAIFast: req.ForceOpenAIFast,
ForceOpenAIUltrafast: req.ForceOpenAIUltrafast,
FreeOpenAIFast: req.FreeOpenAIFast,
RequireOAuthOnly: req.RequireOAuthOnly,
RequirePrivacySet: req.RequirePrivacySet,
@@ -25,3 +25,21 @@ func TestGroupRequestsDecodeForceOpenAIFast(t *testing.T) {
require.Nil(t, omitted.ForceOpenAIFast)
require.Nil(t, omitted.FreeOpenAIFast)
}
func TestGroupRequestsDecodeForceOpenAIUltrafast(t *testing.T) {
var createReq CreateGroupRequest
require.NoError(t, json.Unmarshal([]byte(`{"name":"ultrafast","force_openai_ultrafast":true}`), &createReq))
require.True(t, createReq.ForceOpenAIUltrafast)
require.False(t, createReq.ForceOpenAIFast)
var updateReq UpdateGroupRequest
require.NoError(t, json.Unmarshal([]byte(`{"force_openai_fast":false,"force_openai_ultrafast":true}`), &updateReq))
require.NotNil(t, updateReq.ForceOpenAIUltrafast)
require.True(t, *updateReq.ForceOpenAIUltrafast)
require.NotNil(t, updateReq.ForceOpenAIFast)
require.False(t, *updateReq.ForceOpenAIFast)
var omitted UpdateGroupRequest
require.NoError(t, json.Unmarshal([]byte(`{}`), &omitted))
require.Nil(t, omitted.ForceOpenAIUltrafast)
}
@@ -23,3 +23,18 @@ func TestGroupMapperExposesForceOpenAIFastOnlyToAdmins(t *testing.T) {
require.Contains(t, string(adminJSON), `"force_openai_fast":true`)
require.Contains(t, string(adminJSON), `"free_openai_fast":true`)
}
func TestGroupMapperExposesForceOpenAIUltrafastOnlyToAdmins(t *testing.T) {
group := &service.Group{
ID: 7, Name: "ultrafast", Platform: service.PlatformOpenAI, Status: service.StatusActive, ForceOpenAIUltrafast: true,
}
userJSON, err := json.Marshal(GroupFromService(group))
require.NoError(t, err)
require.NotContains(t, string(userJSON), "force_openai_ultrafast")
adminJSON, err := json.Marshal(GroupFromServiceAdmin(group))
require.NoError(t, err)
require.Contains(t, string(adminJSON), `"force_openai_ultrafast":true`)
require.Contains(t, string(adminJSON), `"force_openai_fast":false`)
}
+1
View File
@@ -149,6 +149,7 @@ func GroupFromServiceAdmin(g *service.Group) *AdminGroup {
out := &AdminGroup{
Group: groupFromServiceBase(g),
ForceOpenAIFast: g.ForceOpenAIFast,
ForceOpenAIUltrafast: g.ForceOpenAIUltrafast,
FreeOpenAIFast: g.FreeOpenAIFast,
ProfitControlEnabled: g.ProfitControlEnabled,
ProfitMinMargin: g.ProfitMinMargin,
+2 -1
View File
@@ -172,7 +172,8 @@ type Group struct {
type AdminGroup struct {
Group
// ForceOpenAIFast 是管理端请求策略,用户侧分组 DTO 无需暴露。
ForceOpenAIFast bool `json:"force_openai_fast"`
ForceOpenAIFast bool `json:"force_openai_fast"`
ForceOpenAIUltrafast bool `json:"force_openai_ultrafast"`
// FreeOpenAIFast 是管理端计费策略,用户侧分组 DTO 无需暴露。
FreeOpenAIFast bool `json:"free_openai_fast"`
@@ -212,6 +212,7 @@ func (r *apiKeyRepository) GetByKeyForAuth(ctx context.Context, key string) (*se
group.FieldAllowMessagesDispatch,
group.FieldAllowLive,
group.FieldForceOpenaiFast,
group.FieldForceOpenaiUltrafast,
group.FieldFreeOpenaiFast,
group.FieldDefaultMappedModel,
group.FieldMessagesDispatchModelConfig,
@@ -1028,6 +1029,7 @@ func groupEntityToService(g *dbent.Group) *service.Group {
AllowMessagesDispatch: g.AllowMessagesDispatch,
AllowLive: g.AllowLive,
ForceOpenAIFast: g.ForceOpenaiFast,
ForceOpenAIUltrafast: g.ForceOpenaiUltrafast,
FreeOpenAIFast: g.FreeOpenaiFast,
RequireOAuthOnly: g.RequireOauthOnly,
RequirePrivacySet: g.RequirePrivacySet,
@@ -44,3 +44,36 @@ func TestGetByKeyForAuthCarriesGroupForceOpenAIFast(t *testing.T) {
require.True(t, got.Group.ForceOpenAIFast)
require.True(t, got.Group.FreeOpenAIFast)
}
func TestGetByKeyForAuthCarriesGroupForceOpenAIUltrafast(t *testing.T) {
ctx := context.Background()
suffix := time.Now().UnixNano()
group := mustCreateGroup(t, integrationEntClient, &service.Group{
Name: fmt.Sprintf("ultrafast-proj-group-%d", suffix), Platform: service.PlatformOpenAI,
RateMultiplier: 1, ForceOpenAIUltrafast: true,
})
user := mustCreateUser(t, integrationEntClient, &service.User{
Email: fmt.Sprintf("ultrafast-proj-%d@example.com", suffix), Concurrency: 5,
})
groupID := group.ID
keyValue := fmt.Sprintf("sk-ultrafast-proj-%d", suffix)
apiKeyRepo := NewAPIKeyRepository(integrationEntClient, integrationDB)
key := &service.APIKey{UserID: user.ID, GroupID: &groupID, Key: keyValue, Name: "ultrafast-proj", Status: service.StatusActive}
require.NoError(t, apiKeyRepo.Create(ctx, key))
t.Cleanup(func() {
_, err := integrationDB.ExecContext(ctx, "DELETE FROM auth_cache_invalidation_outbox WHERE cache_key = encode(sha256(convert_to($1, 'UTF8')), 'hex')", keyValue)
require.NoError(t, err)
_, err = integrationDB.ExecContext(ctx, "DELETE FROM api_keys WHERE id = $1", key.ID)
require.NoError(t, err)
_, err = integrationDB.ExecContext(ctx, "DELETE FROM users WHERE id = $1", user.ID)
require.NoError(t, err)
_, err = integrationDB.ExecContext(ctx, "DELETE FROM groups WHERE id = $1", group.ID)
require.NoError(t, err)
})
got, err := apiKeyRepo.GetByKeyForAuth(ctx, keyValue)
require.NoError(t, err)
require.NotNil(t, got.Group)
require.True(t, got.Group.ForceOpenAIUltrafast)
require.False(t, got.Group.ForceOpenAIFast)
}
@@ -91,6 +91,7 @@ func mustCreateGroup(t *testing.T, client *dbent.Client, g *service.Group) *serv
SetRateMultiplier(g.RateMultiplier).
SetIsExclusive(g.IsExclusive).
SetForceOpenaiFast(g.ForceOpenAIFast).
SetForceOpenaiUltrafast(g.ForceOpenAIUltrafast).
SetFreeOpenaiFast(g.FreeOpenAIFast).
SetModelAllowlist(service.DomainGroupModelAllowlist(g.ModelAllowlist)).
SetCodexModelsManifestConfig(g.CodexModelsManifestConfig).
@@ -138,6 +138,7 @@ func createGroupRecord(ctx context.Context, client *dbent.Client, groupIn *servi
SetAllowMessagesDispatch(groupIn.AllowMessagesDispatch).
SetAllowLive(groupIn.AllowLive).
SetForceOpenaiFast(groupIn.ForceOpenAIFast).
SetForceOpenaiUltrafast(groupIn.ForceOpenAIUltrafast).
SetFreeOpenaiFast(groupIn.FreeOpenAIFast).
SetRequireOauthOnly(groupIn.RequireOAuthOnly).
SetRequirePrivacySet(groupIn.RequirePrivacySet).
@@ -323,6 +324,7 @@ func (r *groupRepository) Update(ctx context.Context, groupIn *service.Group) er
SetAllowMessagesDispatch(groupIn.AllowMessagesDispatch).
SetAllowLive(groupIn.AllowLive).
SetForceOpenaiFast(groupIn.ForceOpenAIFast).
SetForceOpenaiUltrafast(groupIn.ForceOpenAIUltrafast).
SetFreeOpenaiFast(groupIn.FreeOpenAIFast).
SetRequireOauthOnly(groupIn.RequireOAuthOnly).
SetRequirePrivacySet(groupIn.RequirePrivacySet).
@@ -58,6 +58,7 @@ func TestMigrationsRunner_IsIdempotent_AndSchemaIsUpToDate(t *testing.T) {
// groups: OpenAI Live 与 Fast 强制策略都默认关闭,管理员显式开启后才生效。
requireColumn(t, tx, "groups", "allow_live", "boolean", 0, false)
requireColumn(t, tx, "groups", "force_openai_fast", "boolean", 0, false)
requireColumn(t, tx, "groups", "force_openai_ultrafast", "boolean", 0, false)
requireColumn(t, tx, "groups", "free_openai_fast", "boolean", 0, false)
// api_keys: key length should be 128
+24
View File
@@ -369,11 +369,25 @@ func sanitizeGroupOpenAIFast(group *Group) {
if group == nil || !groupSupportsOpenAIFast(group.Platform) {
if group != nil {
group.ForceOpenAIFast = false
group.ForceOpenAIUltrafast = false
group.FreeOpenAIFast = false
}
}
}
// validateGroupOpenAIForcedServiceTier 拒绝同时强制 Fast 与 Ultrafast:一次请求
// 只能携带一个 service_tier,两者并存没有确定语义。须在 sanitizeGroupOpenAIFast
// 之后调用,使不支持该能力的平台先被清零而不是误报。
func validateGroupOpenAIForcedServiceTier(group *Group) error {
if group != nil && group.ForceOpenAIFast && group.ForceOpenAIUltrafast {
return infraerrors.BadRequest(
"GROUP_OPENAI_FORCED_SERVICE_TIER_CONFLICT",
"force_openai_fast and force_openai_ultrafast cannot both be enabled",
)
}
return nil
}
func normalizeCreateGroupInputForSimpleMode(input *CreateGroupInput) {
if input == nil {
return
@@ -626,6 +640,7 @@ func (s *adminServiceImpl) CreateGroup(ctx context.Context, input *CreateGroupIn
AllowMessagesDispatch: input.AllowMessagesDispatch,
AllowLive: input.AllowLive,
ForceOpenAIFast: input.ForceOpenAIFast,
ForceOpenAIUltrafast: input.ForceOpenAIUltrafast,
FreeOpenAIFast: input.FreeOpenAIFast,
RequireOAuthOnly: input.RequireOAuthOnly,
RequirePrivacySet: input.RequirePrivacySet,
@@ -642,6 +657,9 @@ func (s *adminServiceImpl) CreateGroup(ctx context.Context, input *CreateGroupIn
}
sanitizeGroupMessagesDispatchFields(group)
sanitizeGroupOpenAIFast(group)
if err := validateGroupOpenAIForcedServiceTier(group); err != nil {
return nil, err
}
if group.Platform != PlatformOpenAI && group.Platform != PlatformComposite {
group.AllowLive = false
}
@@ -1019,6 +1037,9 @@ func (s *adminServiceImpl) UpdateGroup(ctx context.Context, id int64, input *Upd
if input.ForceOpenAIFast != nil {
group.ForceOpenAIFast = *input.ForceOpenAIFast
}
if input.ForceOpenAIUltrafast != nil {
group.ForceOpenAIUltrafast = *input.ForceOpenAIUltrafast
}
if input.FreeOpenAIFast != nil {
group.FreeOpenAIFast = *input.FreeOpenAIFast
}
@@ -1070,6 +1091,9 @@ func (s *adminServiceImpl) UpdateGroup(ctx context.Context, id int64, input *Upd
}
sanitizeGroupMessagesDispatchFields(group)
sanitizeGroupOpenAIFast(group)
if err := validateGroupOpenAIForcedServiceTier(group); err != nil {
return nil, err
}
if group.Platform != PlatformOpenAI && group.Platform != PlatformComposite {
group.AllowLive = false
}
@@ -147,6 +147,7 @@ func cloneGroupForDuplicate(source *Group, operationID string) *Group {
AllowMessagesDispatch: source.AllowMessagesDispatch,
AllowLive: source.AllowLive,
ForceOpenAIFast: source.ForceOpenAIFast,
ForceOpenAIUltrafast: source.ForceOpenAIUltrafast,
FreeOpenAIFast: source.FreeOpenAIFast,
RequireOAuthOnly: source.RequireOAuthOnly,
RequirePrivacySet: source.RequirePrivacySet,
@@ -216,6 +216,7 @@ func TestDuplicateGroupCopiesConfigurationDeeplyAndResetsRuntimeState(t *testing
require.Equal(t, source.ModelRouting, duplicate.ModelRouting)
require.Equal(t, source.MessagesDispatchModelConfig, duplicate.MessagesDispatchModelConfig)
require.Equal(t, source.ForceOpenAIFast, duplicate.ForceOpenAIFast)
require.Equal(t, source.ForceOpenAIUltrafast, duplicate.ForceOpenAIUltrafast)
require.Equal(t, source.FreeOpenAIFast, duplicate.FreeOpenAIFast)
require.Equal(t, source.ModelAllowlist, duplicate.ModelAllowlist)
require.Equal(t, source.RPMLimit, duplicate.RPMLimit)
@@ -294,6 +294,7 @@ type CreateGroupInput struct {
AllowMessagesDispatch bool
AllowLive bool
ForceOpenAIFast bool
ForceOpenAIUltrafast bool
FreeOpenAIFast bool
DefaultMappedModel string
RequireOAuthOnly bool
@@ -377,6 +378,7 @@ type UpdateGroupInput struct {
AllowMessagesDispatch *bool
AllowLive *bool
ForceOpenAIFast *bool
ForceOpenAIUltrafast *bool
FreeOpenAIFast *bool
DefaultMappedModel *string
RequireOAuthOnly *bool
@@ -2350,3 +2350,136 @@ func TestAdminService_CreateGroup_CodexModelsManifestConfigDisabledAccepted(t *t
require.Equal(t, []int64{1, 2}, group.CodexModelsManifestConfig.AccountIDs)
require.False(t, repo.created.CodexModelsManifestConfig.Enabled)
}
func TestAdminService_CreateGroup_NormalizesForceOpenAIUltrafastByPlatform(t *testing.T) {
for _, tt := range []struct {
name string
platform string
want bool
}{
{name: "openai", platform: PlatformOpenAI, want: true},
{name: "composite", platform: PlatformComposite, want: true},
{name: "anthropic", platform: PlatformAnthropic, want: false},
} {
t.Run(tt.name, func(t *testing.T) {
repo := &groupRepoStubForAdmin{}
svc := &adminServiceImpl{groupRepo: repo}
group, err := svc.CreateGroup(context.Background(), &CreateGroupInput{
Name: "ultrafast-" + tt.name, Platform: tt.platform, RateMultiplier: 1, ForceOpenAIUltrafast: true,
})
require.NoError(t, err)
require.NotNil(t, group)
require.Equal(t, tt.want, repo.created.ForceOpenAIUltrafast)
require.False(t, repo.created.ForceOpenAIFast)
})
}
}
func TestAdminService_CreateGroup_RejectsForcingFastAndUltrafastTogether(t *testing.T) {
repo := &groupRepoStubForAdmin{}
svc := &adminServiceImpl{groupRepo: repo}
group, err := svc.CreateGroup(context.Background(), &CreateGroupInput{
Name: "both", Platform: PlatformOpenAI, RateMultiplier: 1, ForceOpenAIFast: true, ForceOpenAIUltrafast: true,
})
require.Nil(t, group)
require.Equal(t, "GROUP_OPENAI_FORCED_SERVICE_TIER_CONFLICT", infraerrors.Reason(err))
require.Nil(t, repo.created)
}
// 不支持该能力的平台两个开关都会被清零,不应因为"同时为真"被拒绝。
func TestAdminService_CreateGroup_UnsupportedPlatformIgnoresBothForcedTiers(t *testing.T) {
repo := &groupRepoStubForAdmin{}
svc := &adminServiceImpl{groupRepo: repo}
group, err := svc.CreateGroup(context.Background(), &CreateGroupInput{
Name: "anthropic-both", Platform: PlatformAnthropic, RateMultiplier: 1, ForceOpenAIFast: true, ForceOpenAIUltrafast: true,
})
require.NoError(t, err)
require.NotNil(t, group)
require.False(t, repo.created.ForceOpenAIFast)
require.False(t, repo.created.ForceOpenAIUltrafast)
}
func TestAdminService_UpdateGroup_ForceOpenAIUltrafast(t *testing.T) {
truth, falsity := true, false
t.Run("enables ultrafast and invalidates auth cache", func(t *testing.T) {
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive}
repo := &groupRepoStubForAdmin{getByID: existing}
invalidator := &authCacheInvalidatorStub{}
svc := &adminServiceImpl{groupRepo: repo, authCacheInvalidator: invalidator}
_, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{ForceOpenAIUltrafast: &truth})
require.NoError(t, err)
require.True(t, repo.updated.ForceOpenAIUltrafast)
require.False(t, repo.updated.ForceOpenAIFast)
require.Equal(t, []int64{existing.ID}, invalidator.groupIDs)
})
t.Run("switching fast to ultrafast in one request", func(t *testing.T) {
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIFast: true}
repo := &groupRepoStubForAdmin{getByID: existing}
svc := &adminServiceImpl{groupRepo: repo}
_, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{
ForceOpenAIFast: &falsity, ForceOpenAIUltrafast: &truth,
})
require.NoError(t, err)
require.False(t, repo.updated.ForceOpenAIFast)
require.True(t, repo.updated.ForceOpenAIUltrafast)
})
t.Run("enabling ultrafast while fast stays on is rejected", func(t *testing.T) {
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIFast: true}
repo := &groupRepoStubForAdmin{getByID: existing}
svc := &adminServiceImpl{groupRepo: repo}
group, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{ForceOpenAIUltrafast: &truth})
require.Nil(t, group)
require.Equal(t, "GROUP_OPENAI_FORCED_SERVICE_TIER_CONFLICT", infraerrors.Reason(err))
require.Nil(t, repo.updated)
})
t.Run("enabling fast while ultrafast stays on is rejected", func(t *testing.T) {
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIUltrafast: true}
repo := &groupRepoStubForAdmin{getByID: existing}
svc := &adminServiceImpl{groupRepo: repo}
group, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{ForceOpenAIFast: &truth})
require.Nil(t, group)
require.Equal(t, "GROUP_OPENAI_FORCED_SERVICE_TIER_CONFLICT", infraerrors.Reason(err))
require.Nil(t, repo.updated)
})
t.Run("platform change clears ultrafast", func(t *testing.T) {
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIUltrafast: true}
repo := &groupRepoStubForAdmin{getByID: existing}
svc := &adminServiceImpl{groupRepo: repo}
_, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{Platform: PlatformAnthropic})
require.NoError(t, err)
require.False(t, repo.updated.ForceOpenAIUltrafast)
})
t.Run("unrelated update preserves ultrafast", func(t *testing.T) {
description := "renamed"
existing := &Group{ID: 1, Name: "g", Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIUltrafast: true}
repo := &groupRepoStubForAdmin{getByID: existing}
svc := &adminServiceImpl{groupRepo: repo}
_, err := svc.UpdateGroup(context.Background(), existing.ID, &UpdateGroupInput{Description: &description})
require.NoError(t, err)
require.True(t, repo.updated.ForceOpenAIUltrafast)
})
}
@@ -108,6 +108,7 @@ type APIKeyAuthGroupSnapshot struct {
AllowMessagesDispatch bool `json:"allow_messages_dispatch"`
AllowLive bool `json:"allow_live"`
ForceOpenAIFast bool `json:"force_openai_fast"`
ForceOpenAIUltrafast bool `json:"force_openai_ultrafast"`
FreeOpenAIFast bool `json:"free_openai_fast"`
DefaultMappedModel string `json:"default_mapped_model,omitempty"`
MessagesDispatchModelConfig OpenAIMessagesDispatchModelConfig `json:"messages_dispatch_model_config,omitempty"`
@@ -14,7 +14,7 @@ import (
"github.com/dgraph-io/ristretto"
)
const apiKeyAuthSnapshotVersion = 27 // v27: group require_privacy_set (OpenAI scheduling reads it from ctx)
const apiKeyAuthSnapshotVersion = 28 // v28: group force_openai_ultrafast (OpenAI gateway reads it from ctx)
type apiKeyAuthCacheConfig struct {
l1Size int
@@ -421,6 +421,7 @@ func (s *APIKeyService) snapshotFromAPIKey(ctx context.Context, apiKey *APIKey)
AllowMessagesDispatch: apiKey.Group.AllowMessagesDispatch,
AllowLive: apiKey.Group.AllowLive,
ForceOpenAIFast: apiKey.Group.ForceOpenAIFast,
ForceOpenAIUltrafast: apiKey.Group.ForceOpenAIUltrafast,
FreeOpenAIFast: apiKey.Group.FreeOpenAIFast,
DefaultMappedModel: apiKey.Group.DefaultMappedModel,
MessagesDispatchModelConfig: apiKey.Group.MessagesDispatchModelConfig,
@@ -526,6 +527,7 @@ func (s *APIKeyService) snapshotToAPIKey(key string, snapshot *APIKeyAuthSnapsho
AllowMessagesDispatch: snapshot.Group.AllowMessagesDispatch,
AllowLive: snapshot.Group.AllowLive,
ForceOpenAIFast: snapshot.Group.ForceOpenAIFast,
ForceOpenAIUltrafast: snapshot.Group.ForceOpenAIUltrafast,
FreeOpenAIFast: snapshot.Group.FreeOpenAIFast,
DefaultMappedModel: snapshot.Group.DefaultMappedModel,
MessagesDispatchModelConfig: snapshot.Group.MessagesDispatchModelConfig,
@@ -34,3 +34,29 @@ func TestAPIKeyAuthSnapshotGroupForceOpenAIFastRoundtrip(t *testing.T) {
require.True(t, materialized.Group.FreeOpenAIFast)
require.Equal(t, apiKeyAuthSnapshotVersion, cached.Snapshot.Version)
}
func TestAPIKeyAuthSnapshotGroupForceOpenAIUltrafastRoundtrip(t *testing.T) {
groupID := int64(51)
apiKey := &APIKey{
ID: 83, UserID: 40, GroupID: &groupID, Key: "sk-ultrafast-roundtrip", Status: StatusActive,
User: &User{ID: 40, Status: StatusActive},
Group: &Group{
ID: groupID, Name: "ultrafast-roundtrip", Platform: PlatformOpenAI, Status: StatusActive,
Hydrated: true, ForceOpenAIUltrafast: true,
},
}
svc := &APIKeyService{}
payload, err := json.Marshal(&APIKeyAuthCacheEntry{Snapshot: svc.snapshotFromAPIKey(context.Background(), apiKey)})
require.NoError(t, err)
require.Contains(t, string(payload), `"force_openai_ultrafast":true`)
var cached APIKeyAuthCacheEntry
require.NoError(t, json.Unmarshal(payload, &cached))
materialized, used, err := svc.applyAuthCacheEntry(apiKey.Key, &cached)
require.NoError(t, err)
require.True(t, used)
require.NotNil(t, materialized.Group)
require.True(t, materialized.Group.ForceOpenAIUltrafast)
require.False(t, materialized.Group.ForceOpenAIFast)
}
+1
View File
@@ -103,6 +103,7 @@ type Group struct {
AllowMessagesDispatch bool
AllowLive bool
ForceOpenAIFast bool // 强制 OpenAI 网关请求使用 service_tier=priority
ForceOpenAIUltrafast bool // 强制 OpenAI 网关请求使用 service_tier=ultrafast(与 ForceOpenAIFast 互斥)
FreeOpenAIFast bool // OpenAI Fast 请求按 Standard 价格向用户计费
RequireOAuthOnly bool // 仅允许非 apikey 类型账号关联(OpenAI/Antigravity/Anthropic/Gemini)
RequirePrivacySet bool // 调度时仅允许 privacy 已成功设置的账号(OpenAI/Antigravity/Anthropic/Gemini)
@@ -583,3 +583,112 @@ func TestSetOpenAIFastPolicySettings_Validation(t *testing.T) {
require.Equal(t, BetaPolicyActionPass, got.Rules[1].Action)
require.Equal(t, OpenAIFastPolicyActionForcePriority, got.Rules[2].Action)
}
func openAIUltrafastGroupContext() context.Context {
return context.WithValue(context.Background(), ctxkey.Group, &Group{
ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true,
})
}
func TestApplyOpenAIFastPolicyToBody_GroupForceUltrafastInjectsAndOverridesTier(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, DefaultOpenAIFastPolicySettings())
account := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth}
for _, body := range [][]byte{
[]byte(`{"model":"gpt-5.6-sol","input":"hi"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"default"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"fast"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"priority"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"flex"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"ultrafast"}`),
[]byte(`{"model":"gpt-5.6-sol","service_tier":"client-unknown"}`),
} {
updated, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
require.NoError(t, err)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(updated, "service_tier").String(), "body %s", body)
}
}
// 全局策略在分组强制之后裁决:针对 ultrafast 的 filter / block / force_priority
// 规则必须作用在分组写入的 ultrafast 上,而只针对 priority 的规则不应误伤。
func TestApplyOpenAIFastPolicyToBody_GroupForceUltrafastStillHonorsGlobalPolicy(t *testing.T) {
account := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth}
body := []byte(`{"model":"gpt-5.6-sol"}`)
rule := func(tier, action string) *OpenAIFastPolicySettings {
return &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: tier, Action: action, Scope: BetaPolicyScopeAll, ErrorMessage: "ultrafast disabled",
}}}
}
t.Run("ultrafast filter strips the forced tier", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, rule(OpenAIFastTierUltrafast, BetaPolicyActionFilter))
updated, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
require.NoError(t, err)
require.False(t, gjson.GetBytes(updated, "service_tier").Exists())
})
t.Run("ultrafast block rejects the request", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, rule(OpenAIFastTierUltrafast, BetaPolicyActionBlock))
_, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
var blocked *OpenAIFastBlockedError
require.ErrorAs(t, err, &blocked)
require.Equal(t, "ultrafast disabled", blocked.Message)
})
t.Run("ultrafast force_priority downgrades to priority", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, rule(OpenAIFastTierUltrafast, OpenAIFastPolicyActionForcePriority))
updated, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
require.NoError(t, err)
require.Equal(t, OpenAIFastTierPriority, gjson.GetBytes(updated, "service_tier").String())
})
t.Run("priority-only filter leaves ultrafast alone", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, openAIFastFilterPriorityPolicy())
updated, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
require.NoError(t, err)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(updated, "service_tier").String())
})
t.Run("model whitelist fallback filters unsupported models", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: BetaPolicyActionPass, Scope: BetaPolicyScopeAll,
ModelWhitelist: []string{"gpt-5.6-sol"}, FallbackAction: BetaPolicyActionFilter,
}}})
supported, err := svc.applyOpenAIFastPolicyToBody(openAIUltrafastGroupContext(), account, "gpt-5.6-sol", body)
require.NoError(t, err)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(supported, "service_tier").String())
unsupported, err := svc.applyOpenAIFastPolicyToBody(
openAIUltrafastGroupContext(), account, "gpt-5.4-mini", []byte(`{"model":"gpt-5.4-mini"}`))
require.NoError(t, err)
require.False(t, gjson.GetBytes(unsupported, "service_tier").Exists())
})
}
func TestOpenAIGroupForcedServiceTier(t *testing.T) {
openAIAccount := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth}
groupCtx := func(g *Group) context.Context {
return context.WithValue(context.Background(), ctxkey.Group, g)
}
for _, tt := range []struct {
name string
ctx context.Context
account *Account
want string
}{
{name: "no group", ctx: context.Background(), account: openAIAccount, want: ""},
{name: "nil account", ctx: openAIUltrafastGroupContext(), account: nil, want: ""},
{name: "follow client", ctx: groupCtx(&Group{ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true}), account: openAIAccount, want: ""},
{name: "force fast", ctx: groupCtx(&Group{ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIFast: true}), account: openAIAccount, want: OpenAIFastTierPriority},
{name: "force ultrafast", ctx: openAIUltrafastGroupContext(), account: openAIAccount, want: OpenAIFastTierUltrafast},
{name: "composite group with openai account", ctx: groupCtx(&Group{ID: 7, Platform: PlatformComposite, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true}), account: openAIAccount, want: OpenAIFastTierUltrafast},
{name: "composite group with grok account", ctx: groupCtx(&Group{ID: 7, Platform: PlatformComposite, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true}), account: &Account{Platform: PlatformGrok, Type: AccountTypeOAuth}, want: ""},
{name: "unsupported group platform", ctx: groupCtx(&Group{ID: 7, Platform: PlatformAnthropic, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true}), account: openAIAccount, want: ""},
{name: "unhydrated group", ctx: groupCtx(&Group{ID: 7, Platform: PlatformOpenAI, Status: StatusActive, ForceOpenAIUltrafast: true}), account: openAIAccount, want: ""},
{name: "both flags resolve to ultrafast", ctx: groupCtx(&Group{ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIFast: true, ForceOpenAIUltrafast: true}), account: openAIAccount, want: OpenAIFastTierUltrafast},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, openAIGroupForcedServiceTier(tt.ctx, tt.account))
})
}
}
@@ -1202,3 +1202,54 @@ func TestPassthroughBilling_BlockedFrameDoesNotMutateServiceTier(t *testing.T) {
require.Equal(t, "flex", *tier,
"blocked frame is never sent upstream; billing must retain the previous turn's tier")
}
func TestWSResponseCreate_GroupForceUltrafast(t *testing.T) {
account := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth}
ctx := context.WithValue(context.Background(), ctxkey.Group, &Group{
ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true,
})
t.Run("injects and overrides tier on response.create", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, DefaultOpenAIFastPolicySettings())
for _, frame := range [][]byte{
[]byte(`{"type":"response.create","model":"gpt-5.6-sol","input":[]}`),
[]byte(`{"type":"response.create","model":"gpt-5.6-sol","service_tier":"priority"}`),
[]byte(`{"type":"response.create","model":"gpt-5.6-sol","service_tier":"flex"}`),
} {
updated, blocked, err := svc.applyOpenAIFastPolicyToWSResponseCreate(ctx, account, "gpt-5.6-sol", frame)
require.NoError(t, err)
require.Nil(t, blocked)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(updated, "service_tier").String(), "frame %s", frame)
}
})
t.Run("does not touch other frames", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, DefaultOpenAIFastPolicySettings())
frame := []byte(`{"type":"response.cancel","model":"gpt-5.6-sol"}`)
updated, blocked, err := svc.applyOpenAIFastPolicyToWSResponseCreate(ctx, account, "gpt-5.6-sol", frame)
require.NoError(t, err)
require.Nil(t, blocked)
require.Equal(t, string(frame), string(updated))
})
t.Run("global ultrafast filter remains authoritative", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: BetaPolicyActionFilter, Scope: BetaPolicyScopeAll,
}}})
updated, blocked, err := svc.applyOpenAIFastPolicyToWSResponseCreate(
ctx, account, "gpt-5.6-sol", []byte(`{"type":"response.create","model":"gpt-5.6-sol"}`))
require.NoError(t, err)
require.Nil(t, blocked)
require.False(t, gjson.GetBytes(updated, "service_tier").Exists())
})
t.Run("global ultrafast block rejects the frame", func(t *testing.T) {
svc := newOpenAIGatewayServiceWithSettings(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: BetaPolicyActionBlock, Scope: BetaPolicyScopeAll,
}}})
_, blocked, err := svc.applyOpenAIFastPolicyToWSResponseCreate(
ctx, account, "gpt-5.6-sol", []byte(`{"type":"response.create","model":"gpt-5.6-sol"}`))
require.NoError(t, err)
require.NotNil(t, blocked)
})
}
@@ -678,10 +678,10 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco
}
rawTier := requestView.ServiceTier
if openAIGroupForcesFast(ctx, account) {
rawTier = OpenAIFastTierPriority
if requestView.ServiceTier != OpenAIFastTierPriority {
markPatchSet("service_tier", OpenAIFastTierPriority)
if forcedTier := openAIGroupForcedServiceTier(ctx, account); forcedTier != "" {
rawTier = forcedTier
if requestView.ServiceTier != forcedTier {
markPatchSet("service_tier", forcedTier)
}
}
if rawTier != "" {
@@ -3270,6 +3270,18 @@ func TestGroupBillsOpenAIFastAtStandardRequiresOpenAIAccount(t *testing.T) {
))
}
// "免费 Fast" 只把 priority 降到 Standard 价;Ultrafast 是独立的更高档位,
// 分组强制 Ultrafast 时仍按 Ultrafast 计费。
func TestGroupBillsOpenAIFastAtStandardExcludesUltrafast(t *testing.T) {
apiKey := &APIKey{Group: &Group{Platform: PlatformOpenAI, FreeOpenAIFast: true, ForceOpenAIUltrafast: true}}
require.False(t, groupBillsOpenAIFastAtStandard(
apiKey,
&Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth},
OpenAIFastTierUltrafast,
))
}
func TestOpenAIGatewayServiceRecordUsage_ServiceTierNeverRaisedByUpstreamResponse(t *testing.T) {
usageRepo := &openAIRecordUsageLogRepoStub{inserted: true}
userRepo := &openAIRecordUsageUserRepoStub{}
@@ -2188,12 +2188,27 @@ func openAIFastPolicySettingsFromContext(ctx context.Context) *OpenAIFastPolicyS
return nil
}
func openAIGroupForcesFast(ctx context.Context, account *Account) bool {
// openAIGroupForcedServiceTier returns the service_tier that the trusted request
// Group forces onto OpenAI-account requests: "ultrafast" for ForceOpenAIUltrafast,
// "priority" for ForceOpenAIFast, "" when the group forces nothing. The admin
// service keeps the two flags mutually exclusive; Ultrafast is checked first so
// a row that somehow carries both still resolves deterministically.
func openAIGroupForcedServiceTier(ctx context.Context, account *Account) string {
if ctx == nil || account == nil || account.Platform != PlatformOpenAI {
return false
return ""
}
group, _ := ctx.Value(ctxkey.Group).(*Group)
return IsGroupContextValid(group) && groupSupportsOpenAIFast(group.Platform) && group.ForceOpenAIFast
if !IsGroupContextValid(group) || !groupSupportsOpenAIFast(group.Platform) {
return ""
}
switch {
case group.ForceOpenAIUltrafast:
return OpenAIFastTierUltrafast
case group.ForceOpenAIFast:
return OpenAIFastTierPriority
default:
return ""
}
}
// applyOpenAIFastPolicyToBody applies the OpenAI fast policy to a raw request
@@ -2202,7 +2217,8 @@ func openAIGroupForcesFast(ctx context.Context, account *Account) bool {
// normalizes the service_tier value (e.g. client alias "fast" → "priority").
// action=force_priority rewrites any matched known tier to "priority". Before
// the global policy is evaluated, a trusted request Group with
// ForceOpenAIFast enabled unconditionally sets service_tier to "priority".
// ForceOpenAIFast enabled unconditionally sets service_tier to "priority";
// ForceOpenAIUltrafast sets it to "ultrafast" instead.
// The global policy remains authoritative and may still pass, filter, or block
// that final value.
//
@@ -2215,10 +2231,10 @@ func (s *OpenAIGatewayService) applyOpenAIFastPolicyToBody(ctx context.Context,
if len(body) == 0 {
return body, nil
}
if openAIGroupForcesFast(ctx, account) {
updated, err := sjson.SetBytes(body, "service_tier", OpenAIFastTierPriority)
if forcedTier := openAIGroupForcedServiceTier(ctx, account); forcedTier != "" {
updated, err := sjson.SetBytes(body, "service_tier", forcedTier)
if err != nil {
return body, fmt.Errorf("force group service_tier priority on body: %w", err)
return body, fmt.Errorf("force group service_tier %s on body: %w", forcedTier, err)
}
body = updated
}
@@ -2302,7 +2318,8 @@ func writeOpenAIFastPolicyBlockedResponse(c *gin.Context, err *OpenAIFastBlocked
// - filter: returns a copy with top-level service_tier removed
// - force_priority: keeps service_tier and rewrites it to "priority"
// - block: returns (frame, *OpenAIFastBlockedError)
// - Group ForceOpenAIFast: sets priority first, then applies the global rule
// - Group ForceOpenAIFast / ForceOpenAIUltrafast: sets priority / ultrafast
// first, then applies the global rule
//
// Only frames whose "type" field strictly equals "response.create" are
// inspected/mutated. Any other frame type — including the empty string —
@@ -2341,10 +2358,10 @@ func (s *OpenAIGatewayService) applyOpenAIFastPolicyToWSResponseCreate(
if frameType != "response.create" {
return frame, nil, nil
}
if openAIGroupForcesFast(ctx, account) {
updated, err := sjson.SetBytes(frame, "service_tier", OpenAIFastTierPriority)
if forcedTier := openAIGroupForcedServiceTier(ctx, account); forcedTier != "" {
updated, err := sjson.SetBytes(frame, "service_tier", forcedTier)
if err != nil {
return frame, nil, fmt.Errorf("force group service_tier priority in ws frame: %w", err)
return frame, nil, fmt.Errorf("force group service_tier %s in ws frame: %w", forcedTier, err)
}
frame = updated
}
@@ -2769,3 +2769,111 @@ func TestOpenAIGatewayService_OAuthPassthrough_AllowTimeoutHeadersWhenConfigured
require.Equal(t, "120000", upstream.lastReq.Header.Get("x-stainless-timeout"))
require.Empty(t, upstream.lastReq.Header.Get("X-Test"))
}
func TestOpenAIGatewayService_OAuthPassthrough_GroupForceOpenAIUltrafastOverridesClientTier(t *testing.T) {
gin.SetMode(gin.TestMode)
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewReader(nil))
c.Request.Header.Set("User-Agent", "codex_cli_rs/0.144.1")
body := []byte(`{"model":"gpt-5.6-sol","stream":true,"instructions":"test","input":"hi","service_tier":"priority"}`)
upstream := &httpUpstreamRecorder{resp: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"text/event-stream"}},
Body: io.NopCloser(strings.NewReader("data: [DONE]\n\n")),
}}
svc := &OpenAIGatewayService{cfg: &config.Config{}, httpUpstream: upstream}
account := &Account{
ID: 123, Name: "oauth", Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 1,
Credentials: map[string]any{"access_token": "oauth-token", "chatgpt_account_id": "chatgpt-acc"},
Extra: map[string]any{"openai_passthrough": true}, Status: StatusActive, Schedulable: true,
RateMultiplier: f64p(1),
}
ctx := context.WithValue(context.Background(), ctxkey.Group, &Group{
ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true,
})
result, err := svc.Forward(ctx, c, account, body)
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(upstream.lastBody, "service_tier").String())
require.NotNil(t, result.ServiceTier)
require.Equal(t, OpenAIFastTierUltrafast, *result.ServiceTier)
}
func TestOpenAIGatewayService_OAuthLegacy_GroupForceOpenAIUltrafast(t *testing.T) {
gin.SetMode(gin.TestMode)
account := &Account{
ID: 123, Name: "acc", Platform: PlatformOpenAI, Type: AccountTypeOAuth, Concurrency: 1,
Credentials: map[string]any{"access_token": "oauth-token", "chatgpt_account_id": "chatgpt-acc"},
Extra: map[string]any{"openai_passthrough": false, "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeOff},
Status: StatusActive, Schedulable: true, RateMultiplier: f64p(1),
}
ctx := context.WithValue(context.Background(), ctxkey.Group, &Group{
ID: 7, Platform: PlatformOpenAI, Status: StatusActive, Hydrated: true, ForceOpenAIUltrafast: true,
})
forward := func(t *testing.T, settings *OpenAIFastPolicySettings, inputBody []byte) (*OpenAIForwardResult, *httpUpstreamRecorder, error) {
t.Helper()
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewReader(nil))
c.Request.Header.Set("User-Agent", "codex_cli_rs/0.1.0")
upstream := &httpUpstreamRecorder{resp: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"text/event-stream"}, "x-request-id": []string{"rid_group_ultrafast_legacy"}},
Body: io.NopCloser(strings.NewReader("data: [DONE]\n\n")),
}}
svc := newOpenAIGatewayServiceWithSettings(t, settings)
svc.cfg = &config.Config{Gateway: config.GatewayConfig{ForceCodexCLI: false}}
svc.httpUpstream = upstream
result, err := svc.Forward(ctx, c, account, inputBody)
return result, upstream, err
}
t.Run("injects missing tier", func(t *testing.T) {
result, upstream, err := forward(t, DefaultOpenAIFastPolicySettings(),
[]byte(`{"model":"gpt-5.6-sol","stream":false,"input":[{"type":"text","text":"hi"}]}`))
require.NoError(t, err)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(upstream.lastBody, "service_tier").String())
require.NotNil(t, result.ServiceTier)
require.Equal(t, OpenAIFastTierUltrafast, *result.ServiceTier)
})
t.Run("overrides client priority", func(t *testing.T) {
result, upstream, err := forward(t, DefaultOpenAIFastPolicySettings(),
[]byte(`{"model":"gpt-5.6-sol","stream":false,"service_tier":"fast","input":[{"type":"text","text":"hi"}]}`))
require.NoError(t, err)
require.Equal(t, OpenAIFastTierUltrafast, gjson.GetBytes(upstream.lastBody, "service_tier").String())
require.NotNil(t, result.ServiceTier)
require.Equal(t, OpenAIFastTierUltrafast, *result.ServiceTier)
})
t.Run("global ultrafast filter remains authoritative", func(t *testing.T) {
result, upstream, err := forward(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: BetaPolicyActionFilter, Scope: BetaPolicyScopeAll,
}}}, []byte(`{"model":"gpt-5.6-sol","stream":false,"input":[{"type":"text","text":"hi"}]}`))
require.NoError(t, err)
require.False(t, gjson.GetBytes(upstream.lastBody, "service_tier").Exists())
require.Nil(t, result.ServiceTier)
})
t.Run("global force_priority downgrades the forced ultrafast", func(t *testing.T) {
result, upstream, err := forward(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: OpenAIFastPolicyActionForcePriority, Scope: BetaPolicyScopeAll,
}}}, []byte(`{"model":"gpt-5.6-sol","stream":false,"input":[{"type":"text","text":"hi"}]}`))
require.NoError(t, err)
require.Equal(t, OpenAIFastTierPriority, gjson.GetBytes(upstream.lastBody, "service_tier").String())
require.NotNil(t, result.ServiceTier)
require.Equal(t, OpenAIFastTierPriority, *result.ServiceTier)
})
t.Run("global ultrafast block rejects with 403", func(t *testing.T) {
_, upstream, err := forward(t, &OpenAIFastPolicySettings{Rules: []OpenAIFastPolicyRule{{
ServiceTier: OpenAIFastTierUltrafast, Action: BetaPolicyActionBlock, Scope: BetaPolicyScopeAll,
}}}, []byte(`{"model":"gpt-5.6-sol","stream":false,"input":[{"type":"text","text":"hi"}]}`))
var blocked *OpenAIFastBlockedError
require.ErrorAs(t, err, &blocked)
require.Nil(t, upstream.lastReq)
})
}
@@ -0,0 +1,5 @@
ALTER TABLE groups
ADD COLUMN IF NOT EXISTS force_openai_ultrafast BOOLEAN NOT NULL DEFAULT FALSE;
COMMENT ON COLUMN groups.force_openai_ultrafast IS
'Force OpenAI gateway requests in this group to use service_tier=ultrafast before global Fast/Flex policy evaluation; mutually exclusive with force_openai_fast';
@@ -0,0 +1,18 @@
package migrations
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestGroupForceOpenAIUltrafastMigration(t *testing.T) {
content, err := FS.ReadFile("251_group_force_openai_ultrafast.sql")
require.NoError(t, err)
sql := strings.Join(strings.Fields(string(content)), " ")
require.Contains(t, sql,
"ADD COLUMN IF NOT EXISTS force_openai_ultrafast BOOLEAN NOT NULL DEFAULT FALSE")
require.Contains(t, sql, "COMMENT ON COLUMN groups.force_openai_ultrafast")
}
@@ -1271,8 +1271,11 @@ export default {
},
openaiFast: {
title: 'OpenAI Fast mode',
forcedTier: 'Forced service tier',
followClient: 'Follow client',
force: 'Force Fast (priority)',
hint: 'Forces service_tier=priority on OpenAI requests in this group. The global Fast/Flex policy can still filter or block it. New requests update immediately after saving; existing WebSocket sessions must reconnect.',
forceUltrafast: 'Force Ultra Fast (ultrafast)',
hint: 'When a tier is forced, OpenAI requests in this group are rewritten to service_tier=priority or service_tier=ultrafast, overriding the client value. The global Fast/Flex policy can still filter or block it. Ultra Fast is only supported by some models and accounts, is billed at the Ultra Fast rate, and is not covered by "Free Fast". New requests update immediately after saving; existing WebSocket sessions must reconnect.',
free: 'Free Fast',
freeHint: 'Fast requests in this group still use the priority tier, but customers are charged the equivalent Standard price.'
},
@@ -1269,8 +1269,11 @@ export default {
},
openaiFast: {
title: 'OpenAI Fast 模式',
forcedTier: '强制服务档位',
followClient: '跟随客户端',
force: '强制使用 Fast(priority)',
hint: '开启后,此分组的 OpenAI 请求会强制写入 service_tier=priority;全局 Fast/Flex 策略仍可过滤或拦截。保存后新请求立即生效,已建立的 WebSocket 会话需重连。',
forceUltrafast: '强制使用 Ultra Fast(ultrafast)',
hint: '选择强制档位后,此分组的 OpenAI 请求会强制写入 service_tier=priority 或 service_tier=ultrafast,覆盖客户端传入的值;全局 Fast/Flex 策略仍可过滤或拦截。Ultra Fast 仅部分模型与账号支持,且按 Ultra Fast 价格计费,不受"免费 Fast"影响。保存后新请求立即生效,已建立的 WebSocket 会话需重连。',
free: '免费 Fast',
freeHint: '该分组的 Fast 请求仍使用 priority 档位,但客户实际费用按同一请求的 Standard 价格计算。'
},
+3
View File
@@ -643,6 +643,7 @@ export interface AdminGroup extends Group {
// 管理端的 name / description 始终是原字段,各语言译文另放在 i18n 里
i18n?: GroupI18n
force_openai_fast: boolean
force_openai_ultrafast: boolean
free_openai_fast: boolean
model_pricing: import('@/api/admin/channels').ChannelModelPricing[]
// 分组利润控制(openai/anthropic/gemini/grok/antigravity 分组可启用;margin/buffer 为小数存储)。
@@ -818,6 +819,7 @@ export interface CreateGroupRequest {
monthly_limit_usd?: number | null
long_context_pricing_enabled?: boolean
force_openai_fast?: boolean
force_openai_ultrafast?: boolean
free_openai_fast?: boolean
model_pricing?: import('@/api/admin/channels').ChannelModelPricing[]
allow_image_generation?: boolean
@@ -886,6 +888,7 @@ export interface UpdateGroupRequest {
monthly_limit_usd?: number | null
long_context_pricing_enabled?: boolean
force_openai_fast?: boolean
force_openai_ultrafast?: boolean
free_openai_fast?: boolean
model_pricing?: import('@/api/admin/channels').ChannelModelPricing[]
allow_image_generation?: boolean
+37 -14
View File
@@ -1579,14 +1579,15 @@
<h4 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
{{ t("admin.groups.openaiFast.title") }}
</h4>
<div class="flex items-center justify-between gap-4">
<label class="text-sm text-gray-600 dark:text-gray-400">
{{ t("admin.groups.openaiFast.force") }}
<div>
<label class="input-label">
{{ t("admin.groups.openaiFast.forcedTier") }}
</label>
<Toggle
data-testid="create-force-openai-fast"
:aria-label="t('admin.groups.openaiFast.force')"
v-model="createForm.force_openai_fast"
<Select
data-testid="create-openai-forced-tier"
:model-value="groupOpenAIForcedTier(createForm)"
:options="openAIForcedTierOptions"
@update:model-value="setGroupOpenAIForcedTier(createForm, $event)"
/>
</div>
<p class="text-xs text-gray-500 dark:text-gray-400 mt-1">
@@ -3243,14 +3244,15 @@
<h4 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
{{ t("admin.groups.openaiFast.title") }}
</h4>
<div class="flex items-center justify-between gap-4">
<label class="text-sm text-gray-600 dark:text-gray-400">
{{ t("admin.groups.openaiFast.force") }}
<div>
<label class="input-label">
{{ t("admin.groups.openaiFast.forcedTier") }}
</label>
<Toggle
data-testid="edit-force-openai-fast"
:aria-label="t('admin.groups.openaiFast.force')"
v-model="editForm.force_openai_fast"
<Select
data-testid="edit-openai-forced-tier"
:model-value="groupOpenAIForcedTier(editForm)"
:options="openAIForcedTierOptions"
@update:model-value="setGroupOpenAIForcedTier(editForm, $event)"
/>
</div>
<p class="text-xs text-gray-500 dark:text-gray-400 mt-1">
@@ -4361,7 +4363,9 @@ import {
type MessagesDispatchMappingRow,
} from "./groupsMessagesDispatch";
import {
groupOpenAIForcedTier,
normalizeGroupOpenAIFast,
setGroupOpenAIForcedTier,
supportsGroupOpenAIFast,
} from "./groupsOpenAIFast";
import {
@@ -4702,6 +4706,12 @@ const subscriptionTypeOptions = computed(() => [
{ value: "subscription", label: t("admin.groups.subscription.subscription") },
]);
const openAIForcedTierOptions = computed(() => [
{ value: "client", label: t("admin.groups.openaiFast.followClient") },
{ value: "priority", label: t("admin.groups.openaiFast.force") },
{ value: "ultrafast", label: t("admin.groups.openaiFast.forceUltrafast") },
]);
// 降级分组选项(创建时)- 仅包含 anthropic 平台且未启用 claude_code_only 的分组
const fallbackGroupOptions = computed(() => {
const options: { value: number | null; label: string }[] = [
@@ -4985,6 +4995,7 @@ const createForm = reactive({
monthly_limit_usd: null as number | null,
long_context_pricing_enabled: true,
force_openai_fast: false,
force_openai_ultrafast: false,
free_openai_fast: false,
model_pricing: [] as PricingFormEntry[],
// 图片生成计费配置
@@ -5352,6 +5363,7 @@ const editForm = reactive({
monthly_limit_usd: null as number | null,
long_context_pricing_enabled: true,
force_openai_fast: false,
force_openai_ultrafast: false,
free_openai_fast: false,
model_pricing: [] as PricingFormEntry[],
// 图片生成计费配置
@@ -5844,6 +5856,7 @@ const closeCreateModal = () => {
createForm.video_model_prices = createVideoModelPricesForm();
createForm.long_context_pricing_enabled = true;
createForm.force_openai_fast = false;
createForm.force_openai_ultrafast = false;
createForm.free_openai_fast = false;
createForm.model_pricing = [];
createForm.web_search_price_per_call = null;
@@ -5970,6 +5983,10 @@ const handleCreateGroup = async () => {
createForm.platform,
createForm.force_openai_fast,
),
force_openai_ultrafast: normalizeGroupOpenAIFast(
createForm.platform,
createForm.force_openai_ultrafast,
),
free_openai_fast: normalizeGroupOpenAIFast(
createForm.platform,
createForm.free_openai_fast,
@@ -6119,6 +6136,7 @@ const handleEdit = async (group: AdminGroup) => {
editForm.long_context_pricing_enabled =
group.long_context_pricing_enabled ?? true;
editForm.force_openai_fast = group.force_openai_fast ?? false;
editForm.force_openai_ultrafast = group.force_openai_ultrafast ?? false;
editForm.free_openai_fast = group.free_openai_fast ?? false;
editForm.model_pricing = groupPricingFromAPI(group.model_pricing);
editForm.allow_image_generation = group.allow_image_generation ?? false;
@@ -6254,6 +6272,7 @@ const closeEditModal = () => {
editForm.video_model_prices = createVideoModelPricesForm();
editForm.long_context_pricing_enabled = true;
editForm.force_openai_fast = false;
editForm.force_openai_ultrafast = false;
editForm.free_openai_fast = false;
editForm.model_pricing = [];
editForm.web_search_price_per_call = null;
@@ -6314,6 +6333,10 @@ const handleUpdateGroup = async () => {
editForm.platform,
editForm.force_openai_fast,
),
force_openai_ultrafast: normalizeGroupOpenAIFast(
editForm.platform,
editForm.force_openai_ultrafast,
),
free_openai_fast: normalizeGroupOpenAIFast(
editForm.platform,
editForm.free_openai_fast,
@@ -79,6 +79,7 @@ const sourceGroup = {
monthly_limit_usd: null,
long_context_pricing_enabled: true,
force_openai_fast: false,
force_openai_ultrafast: false,
free_openai_fast: false,
model_pricing: [],
profit_control_enabled: false,
@@ -0,0 +1,314 @@
import { defineComponent, h } from "vue";
import { flushPromises, mount } from "@vue/test-utils";
import { createPinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { AdminGroup } from "@/types";
import GroupsView from "@/views/admin/GroupsView.vue";
const {
listGroups,
getModelAllowlistCandidates,
getUsageSummary,
getCapacitySummary,
getLiveCapability,
createGroup,
updateGroup,
} = vi.hoisted(() => ({
listGroups: vi.fn(),
getModelAllowlistCandidates: vi.fn(),
getUsageSummary: vi.fn(),
getCapacitySummary: vi.fn(),
getLiveCapability: vi.fn(),
createGroup: vi.fn(),
updateGroup: vi.fn(),
}));
vi.mock("@/api/admin", () => ({
adminAPI: {
groups: {
list: listGroups,
getAll: vi.fn(),
getModelAllowlistCandidates,
getUsageSummary,
getCapacitySummary,
getLiveCapability,
create: createGroup,
update: updateGroup,
delete: vi.fn(),
duplicate: vi.fn(),
updateSortOrder: vi.fn(),
},
accounts: {
list: vi.fn(),
getById: vi.fn(),
},
},
}));
vi.mock("@/stores/app", () => ({
useAppStore: () => ({
showError: vi.fn(),
showSuccess: vi.fn(),
}),
}));
vi.mock("@/stores/onboarding", () => ({
useOnboardingStore: () => ({
isCurrentStep: vi.fn(() => false),
nextStep: vi.fn(),
}),
}));
vi.mock("vue-i18n", async () => {
const actual = await vi.importActual<typeof import("vue-i18n")>("vue-i18n");
return {
...actual,
useI18n: () => ({ t: (key: string) => key }),
};
});
const sourceGroup = {
id: 42,
name: "OpenAI",
description: null,
platform: "openai",
rate_multiplier: 1,
rpm_limit: 0,
is_exclusive: false,
status: "active",
subscription_type: "standard",
daily_limit_usd: null,
weekly_limit_usd: null,
monthly_limit_usd: null,
long_context_pricing_enabled: true,
force_openai_fast: false,
force_openai_ultrafast: false,
free_openai_fast: false,
model_pricing: [],
profit_control_enabled: false,
profit_min_margin: 0,
profit_safety_buffer: 0,
allow_image_generation: false,
allow_batch_image_generation: false,
image_rate_independent: false,
image_rate_multiplier: 1,
batch_image_discount_multiplier: 0.5,
batch_image_hold_multiplier: 0.6,
image_price_1k: null,
image_price_2k: null,
image_price_4k: null,
video_rate_independent: false,
video_rate_multiplier: 1,
video_price_480p: null,
video_price_720p: null,
video_price_1080p: null,
web_search_price_per_call: null,
search_price_per_1k: null,
audio_realtime_price_per_min: null,
audio_tts_price_per_million_chars: null,
audio_stt_price_per_hour: null,
peak_rate_enabled: false,
peak_start: "",
peak_end: "",
peak_rate_multiplier: 1,
claude_code_only: false,
fallback_group_id: null,
fallback_group_id_on_invalid_request: null,
allow_messages_dispatch: false,
allow_live: false,
require_oauth_only: false,
require_privacy_set: false,
created_at: "2026-09-05T00:00:00Z",
updated_at: "2026-09-05T00:00:00Z",
model_routing: null,
model_routing_enabled: false,
mcp_xml_inject: true,
supported_model_scopes: [],
account_count: 1,
active_account_count: 1,
rate_limited_account_count: 0,
models_list_config: undefined,
codex_models_manifest_config: {
enabled: false,
account_ids: [],
fallback_to_scheduler: false,
},
sort_order: 10,
} satisfies AdminGroup;
const AppLayoutStub = defineComponent({
template: "<main><slot /></main>",
});
const TablePageLayoutStub = defineComponent({
template: '<section><slot name="filters" /><slot name="table" /><slot name="pagination" /></section>',
});
const DataTableStub = defineComponent({
props: {
data: { type: Array, default: () => [] },
},
template: '<div><div v-if="data.length"><slot name="cell-actions" :row="data[0]" /></div></div>',
});
const BaseDialogStub = defineComponent({
props: {
show: { type: Boolean, default: false },
},
template: '<div v-if="show"><slot /><slot name="footer" /></div>',
});
// 透传 data-testid 的 Select 桩:暴露 modelValue / options,供用例读取当前档位并切换。
const SelectStub = defineComponent({
name: "SelectStub",
props: {
modelValue: { type: [String, Number, Boolean, null], default: null },
options: { type: Array, default: () => [] },
},
emits: ["update:modelValue"],
setup: () => () => h("div"),
});
// 表单提交前会调用这些子组件的 validate(),自动桩不提供该方法。
const ValidatingFieldStub = defineComponent({
setup(_, { expose }) {
expose({ validate: () => true, resetValidation: () => undefined });
return () => h("div");
},
});
const mountView = () =>
mount(GroupsView, {
global: {
plugins: [createPinia()],
stubs: {
AppLayout: AppLayoutStub,
TablePageLayout: TablePageLayoutStub,
DataTable: DataTableStub,
Pagination: true,
BaseDialog: BaseDialogStub,
ConfirmDialog: true,
EmptyState: true,
Select: SelectStub,
PlatformIcon: true,
Icon: true,
GroupCapacityBadge: true,
GroupRateMultipliersModal: true,
GroupRPMOverridesModal: true,
ReasoningEffortPolicyFields: ValidatingFieldStub,
CodexManifestAccountsField: ValidatingFieldStub,
PricingEntryCard: true,
VueDraggable: true,
},
},
});
const openEditDialog = async (group: AdminGroup) => {
listGroups.mockResolvedValue({
items: [group],
total: 1,
page: 1,
page_size: 20,
pages: 1,
});
const wrapper = mountView();
await flushPromises();
const editButton = wrapper
.findAll("button")
.find((button) => button.text().includes("common.edit"));
expect(editButton).toBeTruthy();
await editButton!.trigger("click");
await flushPromises();
return wrapper;
};
const forcedTierSelect = (wrapper: ReturnType<typeof mountView>) =>
wrapper.getComponent<typeof SelectStub>(
'[data-testid="edit-openai-forced-tier"]',
);
describe("GroupsView OpenAI forced service tier", () => {
beforeEach(() => {
localStorage.clear();
listGroups.mockReset();
getModelAllowlistCandidates.mockReset();
getUsageSummary.mockReset();
getCapacitySummary.mockReset();
getLiveCapability.mockReset();
createGroup.mockReset();
updateGroup.mockReset();
getModelAllowlistCandidates.mockResolvedValue([]);
getUsageSummary.mockResolvedValue([]);
getCapacitySummary.mockResolvedValue([]);
getLiveCapability.mockResolvedValue({ supported: false });
updateGroup.mockResolvedValue({});
});
it("offers follow-client, Fast and Ultra Fast", async () => {
const wrapper = await openEditDialog(sourceGroup);
expect(forcedTierSelect(wrapper).props("options")).toEqual([
{ value: "client", label: "admin.groups.openaiFast.followClient" },
{ value: "priority", label: "admin.groups.openaiFast.force" },
{ value: "ultrafast", label: "admin.groups.openaiFast.forceUltrafast" },
]);
expect(forcedTierSelect(wrapper).props("modelValue")).toBe("client");
wrapper.unmount();
});
it.each([
[{ force_openai_fast: true, force_openai_ultrafast: false }, "priority"],
[{ force_openai_fast: false, force_openai_ultrafast: true }, "ultrafast"],
])("loads the saved tier %o as %s", async (flags, tier) => {
const wrapper = await openEditDialog({ ...sourceGroup, ...flags });
expect(forcedTierSelect(wrapper).props("modelValue")).toBe(tier);
wrapper.unmount();
});
it("saves Ultra Fast as the only forced tier when switching from Fast", async () => {
const wrapper = await openEditDialog({
...sourceGroup,
force_openai_fast: true,
});
forcedTierSelect(wrapper).vm.$emit("update:modelValue", "ultrafast");
await flushPromises();
expect(forcedTierSelect(wrapper).props("modelValue")).toBe("ultrafast");
await wrapper.get("#edit-group-form").trigger("submit");
await flushPromises();
expect(updateGroup).toHaveBeenCalledTimes(1);
expect(updateGroup.mock.calls[0][0]).toBe(sourceGroup.id);
expect(updateGroup.mock.calls[0][1]).toMatchObject({
force_openai_fast: false,
force_openai_ultrafast: true,
});
wrapper.unmount();
});
it("clears both flags when switching back to follow-client", async () => {
const wrapper = await openEditDialog({
...sourceGroup,
force_openai_ultrafast: true,
});
forcedTierSelect(wrapper).vm.$emit("update:modelValue", "client");
await flushPromises();
await wrapper.get("#edit-group-form").trigger("submit");
await flushPromises();
expect(updateGroup.mock.calls[0][1]).toMatchObject({
force_openai_fast: false,
force_openai_ultrafast: false,
});
wrapper.unmount();
});
});
@@ -1,7 +1,9 @@
import { describe, expect, it } from "vitest";
import {
groupOpenAIForcedTier,
normalizeGroupOpenAIFast,
setGroupOpenAIForcedTier,
supportsGroupOpenAIFast,
} from "../groupsOpenAIFast";
import en from "@/i18n/locales/en/admin/overview";
@@ -21,6 +23,60 @@ describe("groupsOpenAIFast", () => {
expect(normalizeGroupOpenAIFast("openai", false)).toBe(false);
});
it("derives the forced tier from the two mutually exclusive flags", () => {
expect(groupOpenAIForcedTier({})).toBe("client");
expect(
groupOpenAIForcedTier({
force_openai_fast: false,
force_openai_ultrafast: false,
}),
).toBe("client");
expect(groupOpenAIForcedTier({ force_openai_fast: true })).toBe("priority");
expect(groupOpenAIForcedTier({ force_openai_ultrafast: true })).toBe(
"ultrafast",
);
});
it("never leaves both forced-tier flags enabled", () => {
const flags = { force_openai_fast: true, force_openai_ultrafast: false };
setGroupOpenAIForcedTier(flags, "ultrafast");
expect(flags).toEqual({
force_openai_fast: false,
force_openai_ultrafast: true,
});
setGroupOpenAIForcedTier(flags, "priority");
expect(flags).toEqual({
force_openai_fast: true,
force_openai_ultrafast: false,
});
setGroupOpenAIForcedTier(flags, "client");
expect(flags).toEqual({
force_openai_fast: false,
force_openai_ultrafast: false,
});
setGroupOpenAIForcedTier(flags, "ultrafast");
setGroupOpenAIForcedTier(flags, null);
expect(flags).toEqual({
force_openai_fast: false,
force_openai_ultrafast: false,
});
});
it("provides localized forced-tier labels", () => {
for (const locale of [zh, en]) {
expect(locale.groups.openaiFast).toMatchObject({
forcedTier: expect.any(String),
followClient: expect.any(String),
forceUltrafast: expect.stringContaining("ultrafast"),
hint: expect.stringContaining("service_tier=ultrafast"),
});
}
});
it("provides localized switch labels", () => {
expect(zh.groups.openaiFast).toMatchObject({
title: expect.any(String),
@@ -8,3 +8,27 @@ export function normalizeGroupOpenAIFast(
): boolean {
return supportsGroupOpenAIFast(platform) && enabled;
}
// 分组强制的 OpenAI 服务档位。后端用两个互斥的布尔列表达,表单用单选避免
// 同时勾选两者(后端会以 400 拒绝)。
export type GroupOpenAIForcedTier = "client" | "priority" | "ultrafast";
interface GroupOpenAIForcedTierFlags {
force_openai_fast?: boolean;
force_openai_ultrafast?: boolean;
}
export function groupOpenAIForcedTier(
flags: GroupOpenAIForcedTierFlags,
): GroupOpenAIForcedTier {
if (flags.force_openai_ultrafast) return "ultrafast";
return flags.force_openai_fast ? "priority" : "client";
}
export function setGroupOpenAIForcedTier(
flags: GroupOpenAIForcedTierFlags,
tier: unknown,
): void {
flags.force_openai_fast = tier === "priority";
flags.force_openai_ultrafast = tier === "ultrafast";
}