[codex] add setting to disable cross-protocol fallback (#332)

* Add toggle to disable cross-protocol fallback

* Tighten runtime settings payload typing
This commit is contained in:
Cita
2026-03-30 20:02:51 +08:00
committed by GitHub
parent 6726da987f
commit 26f9717ad7
13 changed files with 230 additions and 4 deletions
@@ -65,6 +65,7 @@ describe('settings and auth events', () => {
(config as any).proxyDebugRetentionHours = 24;
(config as any).proxyDebugMaxBodyBytes = 262144;
config.routingFallbackUnitCost = 1;
(config as any).disableCrossProtocolFallback = false;
(config as any).telegramEnabled = false;
(config as any).telegramApiBaseUrl = 'https://api.telegram.org';
(config as any).telegramBotToken = '';
@@ -431,6 +432,33 @@ describe('settings and auth events', () => {
expect(runtime.routingFallbackUnitCost).toBe(0.25);
});
it('persists and returns disable cross protocol fallback from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
url: '/api/settings/runtime',
payload: {
disableCrossProtocolFallback: true,
},
});
expect(updateResponse.statusCode).toBe(200);
const updated = updateResponse.json() as { disableCrossProtocolFallback?: boolean };
expect(updated.disableCrossProtocolFallback).toBe(true);
expect((config as any).disableCrossProtocolFallback).toBe(true);
const saved = await db.select().from(schema.settings).where(eq(schema.settings.key, 'disable_cross_protocol_fallback')).get();
expect(saved?.value).toBe(JSON.stringify(true));
const getResponse = await app.inject({
method: 'GET',
url: '/api/settings/runtime',
});
expect(getResponse.statusCode).toBe(200);
const runtime = getResponse.json() as { disableCrossProtocolFallback?: boolean };
expect(runtime.disableCrossProtocolFallback).toBe(true);
});
it('persists and returns system proxy url from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
+25
View File
@@ -39,6 +39,7 @@ interface RuntimeSettingsBody {
proxyToken?: string;
systemProxyUrl?: string;
codexUpstreamWebsocketEnabled?: boolean;
disableCrossProtocolFallback?: boolean;
proxySessionChannelConcurrencyLimit?: number;
proxySessionChannelQueueWaitMs?: number;
proxyDebugTraceEnabled?: boolean;
@@ -399,6 +400,11 @@ function applyImportedSettingToRuntime(key: string, value: unknown) {
config.codexUpstreamWebsocketEnabled = value;
return;
}
case 'disable_cross_protocol_fallback': {
if (typeof value !== 'boolean') return;
config.disableCrossProtocolFallback = value;
return;
}
case 'proxy_error_keywords': {
try {
config.proxyErrorKeywords = parseProxyErrorKeywords(value);
@@ -667,6 +673,7 @@ function getRuntimeSettingsResponse(currentAdminIp = '') {
logCleanupProgramLogsEnabled: config.logCleanupProgramLogsEnabled,
logCleanupRetentionDays: config.logCleanupRetentionDays,
codexUpstreamWebsocketEnabled: config.codexUpstreamWebsocketEnabled,
disableCrossProtocolFallback: config.disableCrossProtocolFallback,
proxySessionChannelConcurrencyLimit: config.proxySessionChannelConcurrencyLimit,
proxySessionChannelQueueWaitMs: config.proxySessionChannelQueueWaitMs,
proxyDebugTraceEnabled: config.proxyDebugTraceEnabled,
@@ -1080,6 +1087,24 @@ export async function settingsRoutes(app: FastifyInstance) {
upsertSetting('codex_upstream_websocket_enabled', config.codexUpstreamWebsocketEnabled);
}
if (body.disableCrossProtocolFallback !== undefined) {
let nextValue = false;
try {
nextValue = parseBooleanFlag(body.disableCrossProtocolFallback, '跨协议回退开关');
} catch (err: any) {
return reply.code(400).send({
success: false,
message: err?.message || '跨协议回退开关格式无效',
});
}
if (nextValue !== config.disableCrossProtocolFallback) {
changedLabels.push('失败时不尝试其他协议');
}
config.disableCrossProtocolFallback = nextValue;
upsertSetting('disable_cross_protocol_fallback', config.disableCrossProtocolFallback);
}
if (body.proxySessionChannelConcurrencyLimit !== undefined) {
const limit = Number(body.proxySessionChannelConcurrencyLimit);
if (!Number.isFinite(limit) || limit < 0) {