mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
feat(groups): add switch to keep upstream-hidden Codex automatic modes
Codex manifests fetched from upstream carry automatic modes such as codex-auto-review, which upstream already hides from the model picker and Codex uses to review approvals. The gateway removes every automatic mode the group model list does not select, so Codex falls back to the main model for reviews and the forwarded manifest always differs from upstream. Add keep_hidden_auto_modes to the group Codex manifest config. When enabled, automatic modes that upstream marks as hidden are forwarded byte-for-byte; visible ones are still removed unless selected, and explicit selections keep listing them in the picker. The switch is off by default, so existing groups behave as before, and the admin group dialog exposes it next to the pinned accounts settings.
This commit is contained in:
1 parent
f53641e53c
commit
79f84cb2aa
13 files changed
+179
-5
No files matched your search
@@ -12,4 +12,11 @@ type GroupCodexModelsManifestConfig struct {
|
||||
// usable or all upstream fetches fail: fall back to the scheduler loop
|
||||
// (true) or return the upstream error / 503 (false, default).
|
||||
FallbackToScheduler bool `json:"fallback_to_scheduler,omitempty"`
|
||||
// KeepHiddenAutoModes keeps Codex automatic modes (codex-auto-*) that the
|
||||
// upstream manifest already hides from the picker, byte-for-byte, instead
|
||||
// of removing them when the group model list does not select them. Codex
|
||||
// then reviews approvals with its dedicated reviewer model, and an
|
||||
// otherwise untouched upstream manifest keeps the upstream ETag. It applies
|
||||
// to manifests fetched from upstream, with or without pinned accounts.
|
||||
KeepHiddenAutoModes bool `json:"keep_hidden_auto_modes,omitempty"`
|
||||
}
|
||||
@@ -17,6 +17,7 @@ func TestGroupMapperRoundTripsCodexModelsManifestConfig(t *testing.T) {
|
||||
Enabled: true,
|
||||
AccountIDs: []int64{101, 202},
|
||||
FallbackToScheduler: true,
|
||||
KeepHiddenAutoModes: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -27,6 +28,7 @@ func TestGroupMapperRoundTripsCodexModelsManifestConfig(t *testing.T) {
|
||||
Enabled bool `json:"enabled"`
|
||||
AccountIDs []int64 `json:"account_ids"`
|
||||
FallbackToScheduler bool `json:"fallback_to_scheduler"`
|
||||
KeepHiddenAutoModes bool `json:"keep_hidden_auto_modes"`
|
||||
} `json:"codex_models_manifest_config"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(adminJSON, &adminEnvelope))
|
||||
@@ -34,6 +36,7 @@ func TestGroupMapperRoundTripsCodexModelsManifestConfig(t *testing.T) {
|
||||
require.True(t, adminEnvelope.CodexModelsManifestConfig.Enabled)
|
||||
require.Equal(t, []int64{101, 202}, adminEnvelope.CodexModelsManifestConfig.AccountIDs)
|
||||
require.True(t, adminEnvelope.CodexModelsManifestConfig.FallbackToScheduler)
|
||||
require.True(t, adminEnvelope.CodexModelsManifestConfig.KeepHiddenAutoModes)
|
||||
|
||||
userJSON, err := json.Marshal(GroupFromService(group))
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -25,6 +25,7 @@ func normalizeCodexModelsManifestConfig(platform string, cfg GroupCodexModelsMan
|
||||
out := GroupCodexModelsManifestConfig{
|
||||
Enabled: cfg.Enabled,
|
||||
FallbackToScheduler: cfg.FallbackToScheduler,
|
||||
KeepHiddenAutoModes: cfg.KeepHiddenAutoModes,
|
||||
}
|
||||
if len(cfg.AccountIDs) == 0 {
|
||||
return out
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func keepHiddenAutoModesGroup(groupID int64, keep bool) *Group {
|
||||
return &Group{
|
||||
ID: groupID,
|
||||
Platform: PlatformOpenAI,
|
||||
CodexModelsManifestConfig: GroupCodexModelsManifestConfig{KeepHiddenAutoModes: keep},
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario: with the group switch on, an upstream manifest whose automatic
|
||||
// mode is hidden by upstream is forwarded byte-for-byte and keeps its ETag.
|
||||
func TestMergeGroupConfiguredCodexModelsKeepsHiddenAutoModesWhenEnabled(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := &OpenAIGatewayService{accountRepo: codexModelsVisibilityAccountRepo{}}
|
||||
upstreamBody := []byte(`{"models":[{"slug":"gpt-5.6","visibility":"list"},{"slug":"codex-auto-review","visibility":"hide","model_messages":{"auto_review":{"enabled":true}}}]}`)
|
||||
manifest := &OpenAIModelsResponse{
|
||||
Body: append([]byte(nil), upstreamBody...),
|
||||
ETag: `W/"upstream-catalog"`,
|
||||
}
|
||||
|
||||
require.NoError(t, svc.MergeGroupConfiguredCodexModels(context.Background(), keepHiddenAutoModesGroup(81, true), manifest, ""))
|
||||
require.Equal(t, upstreamBody, manifest.Body)
|
||||
require.Equal(t, `W/"upstream-catalog"`, manifest.ETag)
|
||||
}
|
||||
|
||||
// Scenario: the switch is off by default and hidden automatic modes are still
|
||||
// removed, matching the existing opt-in behavior.
|
||||
func TestMergeGroupConfiguredCodexModelsDropsHiddenAutoModesByDefault(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := &OpenAIGatewayService{accountRepo: codexModelsVisibilityAccountRepo{}}
|
||||
manifest := &OpenAIModelsResponse{
|
||||
Body: []byte(`{"models":[{"slug":"gpt-5.6","visibility":"list"},{"slug":"codex-auto-review","visibility":"hide"}]}`),
|
||||
ETag: `W/"upstream-catalog"`,
|
||||
}
|
||||
|
||||
require.NoError(t, svc.MergeGroupConfiguredCodexModels(context.Background(), keepHiddenAutoModesGroup(82, false), manifest, ""))
|
||||
require.Equal(t, []string{"gpt-5.6"}, codexManifestModelSlugs(t, manifest.Body))
|
||||
require.Equal(t, codexModelsManifestBodyETag(manifest.Body), manifest.ETag)
|
||||
}
|
||||
|
||||
// Scenario: the switch never exposes automatic modes in the picker; entries
|
||||
// that upstream marks as visible are still removed unless selected.
|
||||
func TestMergeGroupConfiguredCodexModelsKeepHiddenAutoModesStillDropsVisibleOnes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := &OpenAIGatewayService{accountRepo: codexModelsVisibilityAccountRepo{}}
|
||||
manifest := &OpenAIModelsResponse{
|
||||
Body: []byte(`{"models":[{"slug":"codex-auto-review","visibility":"list"},{"slug":"codex-auto-hidden","visibility":"hide"},{"slug":"gpt-5.6","visibility":"list"}]}`),
|
||||
}
|
||||
|
||||
require.NoError(t, svc.MergeGroupConfiguredCodexModels(context.Background(), keepHiddenAutoModesGroup(83, true), manifest, ""))
|
||||
require.Equal(t, []string{"codex-auto-hidden", "gpt-5.6"}, codexManifestModelSlugs(t, manifest.Body))
|
||||
}
|
||||
|
||||
// Scenario: an explicit model-list selection still lists the automatic mode in
|
||||
// the picker, with or without the switch.
|
||||
func TestMergeGroupConfiguredCodexModelsKeepHiddenAutoModesHonorsExplicitSelection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := &OpenAIGatewayService{accountRepo: codexModelsVisibilityAccountRepo{}}
|
||||
group := keepHiddenAutoModesGroup(84, true)
|
||||
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"codex-auto-review", "gpt-5.6"}}
|
||||
manifest := &OpenAIModelsResponse{
|
||||
Body: []byte(`{"models":[{"slug":"codex-auto-review","visibility":"hide"},{"slug":"gpt-5.6","visibility":"list"}]}`),
|
||||
}
|
||||
|
||||
require.NoError(t, svc.MergeGroupConfiguredCodexModels(context.Background(), group, manifest, ""))
|
||||
models := decodeCodexManifestModels(t, manifest.Body)
|
||||
require.Equal(t, []string{"codex-auto-review", "gpt-5.6"}, codexManifestModelSlugs(t, manifest.Body))
|
||||
require.Equal(t, "list", models[0]["visibility"])
|
||||
}
|
||||
|
||||
func TestNormalizeCodexModelsManifestConfigKeepsHiddenAutoModesSwitch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := normalizeCodexModelsManifestConfig(PlatformOpenAI, GroupCodexModelsManifestConfig{KeepHiddenAutoModes: true})
|
||||
require.True(t, got.KeepHiddenAutoModes)
|
||||
|
||||
got = normalizeCodexModelsManifestConfig(PlatformAnthropic, GroupCodexModelsManifestConfig{KeepHiddenAutoModes: true})
|
||||
require.False(t, got.KeepHiddenAutoModes, "non-OpenAI groups persist a zero config")
|
||||
}
|
||||
@@ -156,6 +156,7 @@ func (s *OpenAIGatewayService) BuildGroupConfiguredCodexModelsManifest(
|
||||
nil,
|
||||
group.ModelAllowlist.Models,
|
||||
group.ModelAllowlistEnabled(),
|
||||
false,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("build group configured Codex models: %w", err)
|
||||
@@ -201,6 +202,7 @@ func (s *OpenAIGatewayService) MergeGroupConfiguredCodexModels(
|
||||
configuredModels,
|
||||
group.ModelAllowlist.Models,
|
||||
group.ModelAllowlistEnabled(),
|
||||
group.CodexModelsManifestConfig.KeepHiddenAutoModes,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("merge group configured Codex models: %w", err)
|
||||
@@ -1453,11 +1455,17 @@ func BuildDeepSeekCodexModelsManifest(modelIDs []string) ([]byte, error) {
|
||||
return BuildCodexModelsManifest(modelIDs)
|
||||
}
|
||||
|
||||
// mergeConfiguredCodexModelsManifest applies the group's picker policy to a
|
||||
// Codex manifest. keepHiddenAutoModes keeps automatic modes (codex-auto-*) that
|
||||
// the group did not explicitly select, provided the source already hides them
|
||||
// from the picker, byte-for-byte as they appear in body. Visible automatic
|
||||
// modes are still removed unless selected.
|
||||
func mergeConfiguredCodexModelsManifest(
|
||||
body []byte,
|
||||
configuredModels []string,
|
||||
selectedModels []string,
|
||||
filterBySelection bool,
|
||||
keepHiddenAutoModes bool,
|
||||
) ([]byte, bool, error) {
|
||||
var envelope map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &envelope); err != nil {
|
||||
@@ -1505,7 +1513,15 @@ func mergeConfiguredCodexModelsManifest(
|
||||
_, explicitlyEnabled := selected[descriptor.Slug]
|
||||
explicitlyEnabled = filterBySelection && explicitlyEnabled
|
||||
if !explicitlyEnabled {
|
||||
changed = true
|
||||
if !keepHiddenAutoModes || !codexModelHidden(rawModel) {
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
// Codex resolves its approval reviewer from this catalog, and
|
||||
// upstream already hides the entry from the picker; forward it
|
||||
// untouched so the manifest can keep the upstream ETag.
|
||||
seen[descriptor.Slug] = struct{}{}
|
||||
merged = append(merged, rawModel)
|
||||
continue
|
||||
}
|
||||
visibleModel, visibilityChanged, err := codexModelWithVisibility(rawModel, "list")
|
||||
@@ -1558,6 +1574,17 @@ func mergeConfiguredCodexModelsManifest(
|
||||
return mergedBody, true, nil
|
||||
}
|
||||
|
||||
// codexModelHidden reports whether a manifest entry is hidden from the picker.
|
||||
func codexModelHidden(rawModel json.RawMessage) bool {
|
||||
var descriptor struct {
|
||||
Visibility string `json:"visibility"`
|
||||
}
|
||||
if err := json.Unmarshal(rawModel, &descriptor); err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(descriptor.Visibility), "hide")
|
||||
}
|
||||
|
||||
func codexModelWithVisibility(rawModel json.RawMessage, visibility string) (json.RawMessage, bool, error) {
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(rawModel, &fields); err != nil {
|
||||
|
||||
@@ -122,6 +122,24 @@
|
||||
<p v-else class="text-xs text-gray-500 dark:text-gray-400">
|
||||
{{ t("admin.groups.codexModelsManifest.disabledHint") }}
|
||||
</p>
|
||||
|
||||
<!-- Independent of pinned accounts: also applies to scheduler discovery. -->
|
||||
<div class="mt-3 flex items-start justify-between gap-3">
|
||||
<div class="min-w-0 flex-1">
|
||||
<label class="text-sm text-gray-700 dark:text-gray-300">
|
||||
{{ t("admin.groups.codexModelsManifest.keepHiddenAutoModes") }}
|
||||
</label>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
{{ t("admin.groups.codexModelsManifest.keepHiddenAutoModesHint") }}
|
||||
</p>
|
||||
</div>
|
||||
<Toggle
|
||||
data-testid="codex-manifest-keep-hidden-auto-modes-toggle"
|
||||
:aria-label="t('admin.groups.codexModelsManifest.keepHiddenAutoModes')"
|
||||
:model-value="config.keep_hidden_auto_modes ?? false"
|
||||
@update:model-value="emitUpdate({ keep_hidden_auto_modes: $event })"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -102,6 +102,20 @@ describe("CodexManifestAccountsField", () => {
|
||||
expect(enabled.text()).toContain("admin.groups.codexModelsManifest.enabledHint");
|
||||
});
|
||||
|
||||
it("offers the hidden automatic modes switch independently of pinned accounts", async () => {
|
||||
const wrapper = mountField(disabledConfig());
|
||||
const toggle = wrapper.find('[data-testid="codex-manifest-keep-hidden-auto-modes-toggle"]');
|
||||
|
||||
expect(toggle.exists()).toBe(true);
|
||||
expect(wrapper.text()).toContain("admin.groups.codexModelsManifest.keepHiddenAutoModes");
|
||||
|
||||
await toggle.trigger("click");
|
||||
|
||||
const emitted = wrapper.emitted("update:modelValue");
|
||||
expect(emitted).toHaveLength(1);
|
||||
expect(emitted?.[0]?.[0]).toEqual({ ...disabledConfig(), keep_hidden_auto_modes: true });
|
||||
});
|
||||
|
||||
it("selects accounts from search results and removes them via tags", async () => {
|
||||
vi.mocked(adminAPI.accounts.list).mockResolvedValue({
|
||||
items: [
|
||||
|
||||
@@ -1178,7 +1178,9 @@ export default {
|
||||
searchEmpty: 'No matching accounts',
|
||||
fallback: 'Fall back to the scheduler when all pinned accounts are unavailable',
|
||||
fallbackHint: 'Off: return 503 / the upstream error. On: fall back to the existing scheduler path.',
|
||||
selectAtLeastOne: 'Select at least one account after enabling pinned accounts'
|
||||
selectAtLeastOne: 'Select at least one account after enabling pinned accounts',
|
||||
keepHiddenAutoModes: 'Keep upstream-hidden Codex automatic modes',
|
||||
keepHiddenAutoModesHint: 'Keep automatic modes such as codex-auto-review that upstream already hides from the model picker instead of removing them. Codex then reviews approvals with its dedicated reviewer model, and an otherwise unchanged upstream manifest keeps its ETag. Off by default.'
|
||||
},
|
||||
compositeRoutes: {
|
||||
action: 'Routes',
|
||||
|
||||
@@ -1175,7 +1175,9 @@ export default {
|
||||
searchEmpty: '未找到匹配账号',
|
||||
fallback: '选定账号全部不可用时回退调度器',
|
||||
fallbackHint: '关闭时返回 503 / 上游错误;开启时回退到现有调度器选账路径。',
|
||||
selectAtLeastOne: '开启固定账号后至少选择一个账号'
|
||||
selectAtLeastOne: '开启固定账号后至少选择一个账号',
|
||||
keepHiddenAutoModes: '保留上游隐藏的 Codex 自动模式',
|
||||
keepHiddenAutoModesHint: '保留 codex-auto-review 等上游已在模型选择器中隐藏的自动模式,不再移除。Codex 将使用专用审查模型完成自动审批;上游清单没有其他改动时可沿用上游 ETag。默认关闭。'
|
||||
},
|
||||
compositeRoutes: {
|
||||
action: '路由',
|
||||
|
||||
@@ -687,6 +687,8 @@ export interface CodexModelsManifestConfig {
|
||||
enabled: boolean
|
||||
account_ids: number[]
|
||||
fallback_to_scheduler: boolean
|
||||
// Keep upstream-hidden Codex automatic modes (codex-auto-*) in the manifest.
|
||||
keep_hidden_auto_modes?: boolean
|
||||
}
|
||||
|
||||
export type CompositeRouteMatchType = 'exact' | 'prefix'
|
||||
|
||||
@@ -4818,6 +4818,7 @@ const createCodexManifestDefaults = (): CodexModelsManifestConfig => ({
|
||||
enabled: false,
|
||||
account_ids: [],
|
||||
fallback_to_scheduler: false,
|
||||
keep_hidden_auto_modes: false,
|
||||
});
|
||||
const editCodexManifestConfig = ref<CodexModelsManifestConfig>(createCodexManifestDefaults());
|
||||
const editCodexManifestAccountNames = ref<Record<number, string>>({});
|
||||
@@ -6096,6 +6097,7 @@ const handleEdit = async (group: AdminGroup) => {
|
||||
enabled: savedCodexManifestConfig.enabled ?? false,
|
||||
account_ids: [...(savedCodexManifestConfig.account_ids ?? [])],
|
||||
fallback_to_scheduler: savedCodexManifestConfig.fallback_to_scheduler ?? false,
|
||||
keep_hidden_auto_modes: savedCodexManifestConfig.keep_hidden_auto_modes ?? false,
|
||||
};
|
||||
editCodexManifestAccountNames.value = {};
|
||||
for (const id of editCodexManifestConfig.value.account_ids) {
|
||||
@@ -6249,6 +6251,7 @@ const handleUpdateGroup = async () => {
|
||||
enabled: editCodexManifestConfig.value.enabled,
|
||||
account_ids: [...editCodexManifestConfig.value.account_ids],
|
||||
fallback_to_scheduler: editCodexManifestConfig.value.fallback_to_scheduler,
|
||||
keep_hidden_auto_modes: editCodexManifestConfig.value.keep_hidden_auto_modes ?? false,
|
||||
}
|
||||
: createCodexManifestDefaults(),
|
||||
supported_model_scopes: normalizeSupportedModelScopesForPlatform(
|
||||
|
||||
@@ -263,7 +263,10 @@ describe("GroupsView Codex manifest binding", () => {
|
||||
await flushPromises();
|
||||
|
||||
expect(wrapper.get('[data-testid="codex-manifest-value"]').text()).toBe(
|
||||
JSON.stringify(sourceGroup.codex_models_manifest_config),
|
||||
JSON.stringify({
|
||||
...sourceGroup.codex_models_manifest_config,
|
||||
keep_hidden_auto_modes: false,
|
||||
}),
|
||||
);
|
||||
|
||||
await wrapper.get('[data-testid="codex-manifest-enable"]').trigger("click");
|
||||
@@ -281,6 +284,7 @@ describe("GroupsView Codex manifest binding", () => {
|
||||
enabled: true,
|
||||
account_ids: [17],
|
||||
fallback_to_scheduler: false,
|
||||
keep_hidden_auto_modes: false,
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -434,7 +434,7 @@ describe('GroupsView duplicate action', () => {
|
||||
await flushPromises()
|
||||
expect(updateGroup).toHaveBeenCalledWith(42, expect.objectContaining({
|
||||
codex_models_manifest_config: {
|
||||
enabled: true, account_ids: [5], fallback_to_scheduler: true
|
||||
enabled: true, account_ids: [5], fallback_to_scheduler: true, keep_hidden_auto_modes: false
|
||||
}
|
||||
}))
|
||||
|
||||
|
||||
Reference in new issue
Block a user