Skip to content

Commit 3f555f9

Browse files
serjicclaude
andauthored
feat: narrow models to what the providers/set gateway lists (#627)
### Summary Addresses [IJAI-1783](https://youtrack.jetbrains.com/issue/IJAI-1783). An organization can restrict the models its JetBrains gateway serves, but a session routed through `providers/set` offered Codex's whole catalogue, so the picker showed models the gateway then refused. `providers/set` now lists the gateway's models before it responds, and the adapter reports only the Codex models the gateway lists. - `GET {baseUrl}/models` with the headers Codex sends to the gateway (`X-Client-Feature-ID: codex` plus the `providers/set` headers); OpenAI format, `data[].id`. The listing is stored with the gateway config, so a provider restart reuses it, and setting the same configuration again does not list again. - `fetchAvailableModels` narrows `model/list`, so new, resumed, loaded and forked sessions and `unstable_setSessionModel` all see the same list. Ids match without case and date suffix; exactly one narrowed model stays `isDefault`. - A thread model the gateway does not list is replaced by the narrowed default, also on `turn/start`; a model the gateway lists but Codex's catalogue lacks is kept. Sessions open across `providers/set` or `providers/disable` take the models of the restarted app-server, and the client gets a `config_option_update`. A listing that matches no Codex model leaves the session with no model: there is no model config option at all. - A 4xx answer means the gateway has no listing: `providers/set` succeeds and nothing is narrowed. Any other failed listing (network error, 5xx, malformed body, 10 s timeout) fails `providers/set` with `internal_error`, and the current routing stays. A listing that finishes after a newer `providers/set` or `providers/disable` was applied is dropped (a newer request that failed drops nothing), and the routing goes to the client a provider restart installed. A `baseUrl` that is not an http(s) URL, accepted on v1 before, now fails with `invalid_params`; an invalid header or a base URL with credentials fails without repeating the value. - The `gateway` auth method and providers from the Codex config (`config.toml`, `CODEX_CONFIG`) are not listed. `readme-dev.md` describes the behaviour. ### Not covered Title generation (`gpt-5.6-luna`), subagents, `/review` and compaction choose their own model and are not checked against the listing. ### Verification ```bash npm run typecheck npm run build npm test # 1755 passed, 36 skipped ``` `gateway-model-discovery.test.ts` covers the request and headers, narrowing and the default, the replaced thread model on `turn/start`, failing `providers/set` (HTTP error, malformed body, network error) with the routing kept, the empty list, no listing without `providers/set` or for the gateway auth method, `providers/disable`, reuse, and the provider restart. Tests that call `providers/set` stub the listing (`stubGatewayModelListing`). Checked locally against the JetBrains gateway with an earlier build of this branch: the adapter's log shows `/models` answering `gpt-5.6-sol`, `gpt-6-sol`, `gpt-6.1-sol` in one session and an empty list with only Bedrock configured; the empty-list and failing-listing behaviour of the final build is covered by the tests only. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a636804 commit 3f555f9

12 files changed

Lines changed: 858 additions & 88 deletions

‎readme-dev.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ schema alone:
177177
- Other AIR `_meta` keys (goal, message phase, mode kind, permission presentation) go only to AIR
178178
clients, as on v1. Every v2 permission request still gets its prompt `title` and `description`.
179179

180+
#### Models of a `providers/set` gateway
181+
182+
`providers/set` lists the models of the gateway (`GET {baseUrl}/models`, with the headers Codex sends to it)
183+
before it responds. The models reported to the client are then only the Codex models whose `model` or `id` the
184+
gateway lists, matched without case and date suffix. A session whose thread model the gateway does not list uses
185+
the default of these models; a thread model that the gateway lists but Codex does not know is kept. A gateway that
186+
lists none of the Codex models leaves no model option at all, and the session keeps the thread's model as current.
187+
Sessions already open get the models of the new routing when the provider restart resumes them, after
188+
`providers/disable` too. A 4xx answer means the gateway has no listing: `providers/set` succeeds and nothing is
189+
narrowed. A failed listing (network error, a status other than 2xx or 4xx, malformed body, or the timeout) fails
190+
`providers/set` with `internal_error` and keeps the current routing; a base URL that is not http(s) fails with
191+
`invalid_params`. A listing that finishes after a newer `providers/set` or `providers/disable` was applied is
192+
dropped. The `gateway` auth method and providers from
193+
the Codex config are not listed.
194+
180195
#### Known gaps
181196

182197
- After `providers/disable("openai")`, `providers/list` still reports native OpenAI routing as the

‎src/CodexAcpClient.ts‎

Lines changed: 141 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ import {
7373
threadActiveWriterRequestError,
7474
} from "./CodexThreadErrors";
7575
import {airCustomInstructions} from "./AirExtension";
76+
import {
77+
GatewayListingError,
78+
type GatewayModelIds,
79+
isListedModelId,
80+
listGatewayModels,
81+
narrowToListedModels,
82+
} from "./GatewayModelDiscovery";
83+
import {findSupportedEffort} from "./ModelConfigOption";
7684
export type {SessionMetadata, SessionMetadataWithThread} from "./SessionMetadata";
7785

7886
/**
@@ -444,41 +452,64 @@ export class CodexAcpClient {
444452
* method and the ACP `providers/set` method. Throws `invalid_params` for an
445453
* unsupported protocol or a malformed base URL.
446454
*/
447-
private applyGatewayConfig(params: {
448-
baseUrl: string;
449-
headers?: Record<string, string> | undefined;
450-
providerName?: string | undefined;
451-
apiType: acp.LlmProtocol;
452-
}, source: GatewayConfigSource): void {
453-
const apiType = params.apiType;
454-
const wireApi = SUPPORTED_GATEWAY_PROTOCOLS[apiType];
455-
if (!wireApi) {
455+
private applyGatewayConfig(params: GatewayRoutingParams, source: GatewayConfigSource): void {
456+
const gatewayConfig = buildGatewayConfig(params);
457+
this.gatewayConfigSource = source;
458+
this.gatewayConfig = gatewayConfig;
459+
}
460+
461+
/**
462+
* `providers/set`: the full configuration for the custom gateway provider, which {@link setProvider}
463+
* applies. Rejects unknown provider ids and malformed base URLs with `invalid_params`.
464+
*
465+
* Lists the models of the gateway, which narrow the models reported to the client;
466+
* see {@link fetchAvailableModels}. A failed listing fails the call with `internal_error`.
467+
* The same configuration again reuses the previous listing.
468+
*/
469+
async prepareProvider(request: acp.SetProviderRequest): Promise<GatewayConfig> {
470+
if (request.providerId !== OPENAI_PROVIDER_ID) {
456471
throw RequestError.invalidParams(
457-
{apiType},
458-
`Unsupported provider apiType "${apiType}"; supported: ${Object.keys(SUPPORTED_GATEWAY_PROTOCOLS).join(", ")}`,
472+
{providerId: request.providerId},
473+
`Unknown providerId "${request.providerId}"; only "${OPENAI_PROVIDER_ID}" is configurable`,
459474
);
460475
}
461-
if (typeof params.baseUrl !== "string" || params.baseUrl.trim().length === 0) {
462-
throw RequestError.invalidParams(undefined, "baseUrl must be a non-empty string");
476+
const gatewayConfig = buildGatewayConfig({
477+
apiType: request.apiType,
478+
baseUrl: request.baseUrl,
479+
headers: request.headers,
480+
});
481+
if (!URL.canParse(request.baseUrl) || !["http:", "https:"].includes(new URL(request.baseUrl).protocol)) {
482+
throw RequestError.invalidParams({baseUrl: request.baseUrl}, "baseUrl must be an http(s) URL");
463483
}
464-
const providerName = typeof params.providerName === "string" && params.providerName.trim().length > 0
465-
? params.providerName
466-
: "User-provided gateway";
467-
const headers: Record<string, string> = {
468-
"X-Client-Feature-ID": "codex",
469-
...params.headers,
470-
};
484+
// Reuse needs the very same headers, so a rotated token lists again, and a failed listing then fails that
485+
// `providers/set`. Deliberate: AIR keeps its proxy key for the IDE's lifetime and does not rotate it.
486+
const previous = this.gatewayConfigSource === "acpProviders" ? this.gatewayConfig : null;
487+
try {
488+
gatewayConfig.listedModels = previous?.listedModels && sameGatewayRouting(previous, gatewayConfig)
489+
? previous.listedModels
490+
: await listGatewayModels(gatewayConfig.config.base_url, gatewayConfig.config.http_headers);
491+
} catch (error) {
492+
if (error instanceof GatewayListingError) {
493+
throw RequestError.internalError(
494+
{providerId: request.providerId, apiType: request.apiType, baseUrl: request.baseUrl},
495+
`Model discovery for provider "${OPENAI_PROVIDER_ID}" failed: ${error.message}`,
496+
);
497+
}
498+
throw error;
499+
}
500+
return gatewayConfig;
501+
}
471502

472-
this.gatewayConfigSource = source;
473-
this.gatewayConfig = {
474-
modelProvider: CUSTOM_GATEWAY_PROVIDER_ID,
475-
config: {
476-
name: providerName,
477-
base_url: params.baseUrl,
478-
http_headers: headers,
479-
wire_api: wireApi,
480-
},
481-
};
503+
/** `providers/set`: replaces the routing with a configuration from {@link prepareProvider}. */
504+
setProvider(gatewayConfig: GatewayConfig): void {
505+
this.gatewayConfigSource = "acpProviders";
506+
this.gatewayConfig = gatewayConfig;
507+
logger.log("providers/set applied", {
508+
providerId: OPENAI_PROVIDER_ID,
509+
apiType: gatewayApiTypeFromConfig(gatewayConfig),
510+
baseUrl: gatewayConfig.config.base_url,
511+
headerNames: Object.keys(gatewayConfig.config.http_headers),
512+
});
482513
}
483514

484515
/**
@@ -525,30 +556,6 @@ export class CodexAcpClient {
525556
return {apiType: "openai", baseUrl: DEFAULT_OPENAI_BASE_URL};
526557
}
527558

528-
/**
529-
* `providers/set`: replaces the full configuration for the custom gateway
530-
* provider. Rejects unknown provider ids with `invalid_params`.
531-
*/
532-
setProvider(request: acp.SetProviderRequest): void {
533-
if (request.providerId !== OPENAI_PROVIDER_ID) {
534-
throw RequestError.invalidParams(
535-
{providerId: request.providerId},
536-
`Unknown providerId "${request.providerId}"; only "${OPENAI_PROVIDER_ID}" is configurable`,
537-
);
538-
}
539-
this.applyGatewayConfig({
540-
apiType: request.apiType,
541-
baseUrl: request.baseUrl,
542-
headers: request.headers,
543-
}, "acpProviders");
544-
logger.log("providers/set applied", {
545-
providerId: request.providerId,
546-
apiType: request.apiType,
547-
baseUrl: request.baseUrl,
548-
headerNames: Object.keys(request.headers ?? {}),
549-
});
550-
}
551-
552559
/**
553560
* `providers/disable`: disables the custom gateway provider. Disabling an
554561
* unknown provider id is idempotent success (RFD behavior §7).
@@ -812,7 +819,7 @@ export class CodexAcpClient {
812819
});
813820

814821
const codexModels = await this.fetchAvailableModels();
815-
if (codexModels.length === 0) {
822+
if (codexModels.length === 0 && !this.isNarrowedByGateway()) {
816823
throw new Error("Codex did not return any models");
817824
}
818825
const currentModelId = this.createModelId(codexModels, response.model, response.reasoningEffort).toString();
@@ -1074,6 +1081,22 @@ export class CodexAcpClient {
10741081
return ModelId.create(selectedModel.id, reasoningEffort ?? selectedModel.defaultReasoningEffort);
10751082
}
10761083

1084+
// The gateway narrowed the models, so a model it does not list must not be used. A model it lists but Codex
1085+
// does not know is kept, as below. When it lists none, no model is available and the thread keeps its own;
1086+
// the gateway then refuses the requests.
1087+
const narrowedDefault = availableModels.find(m => m.isDefault) ?? availableModels[0];
1088+
const listed = this.getGatewayListedModels();
1089+
if (modelId && narrowedDefault && listed !== null && !isListedModelId(modelId, listed)) {
1090+
const defaultModel = narrowedDefault;
1091+
logger.log("The gateway does not list the model of the thread; using the default model", {
1092+
model: modelId,
1093+
defaultModel: defaultModel.id,
1094+
});
1095+
const effort = findSupportedEffort(defaultModel.supportedReasoningEfforts, reasoningEffort ?? undefined)
1096+
?? defaultModel.defaultReasoningEffort;
1097+
return ModelId.create(defaultModel.id, effort);
1098+
}
1099+
10771100
// The configured model is not in Codex's advertised catalog. This is
10781101
// expected for custom providers (e.g. a self-hosted or third-party
10791102
// model), whose model ids the catalog does not enumerate. Keep the
@@ -1342,6 +1365,7 @@ export class CodexAcpClient {
13421365
});
13431366
}
13441367

1368+
/** The Codex models, narrowed to the ones that the `providers/set` gateway lists. */
13451369
async fetchAvailableModels(): Promise<Model[]> {
13461370
const models: Model[] = [];
13471371
let cursor: string | null = null;
@@ -1352,7 +1376,17 @@ export class CodexAcpClient {
13521376
cursor = response.nextCursor;
13531377
} while (cursor);
13541378

1355-
return models;
1379+
const listed = this.getGatewayListedModels();
1380+
return listed === null ? models : narrowToListedModels(models, listed);
1381+
}
1382+
1383+
private getGatewayListedModels(): GatewayModelIds | null {
1384+
return this.gatewayConfigSource === "acpProviders" ? this.gatewayConfig?.listedModels ?? null : null;
1385+
}
1386+
1387+
/** Whether {@link fetchAvailableModels} narrows the models to a `providers/set` gateway's listing. */
1388+
private isNarrowedByGateway(): boolean {
1389+
return this.getGatewayListedModels() !== null;
13561390
}
13571391

13581392
}
@@ -1437,14 +1471,64 @@ type WireApi = "responses";
14371471

14381472
type GatewayConfigSource = "authentication" | "acpProviders";
14391473

1440-
interface GatewayConfig {
1474+
export interface GatewayConfig {
14411475
modelProvider: string;
14421476
config: {
14431477
name: string,
14441478
base_url: string,
14451479
http_headers: Record<string, string>,
14461480
wire_api: WireApi
1481+
};
1482+
/** The models a `providers/set` gateway lists; `null` when it was not listed or the listing failed. */
1483+
listedModels: GatewayModelIds | null;
1484+
}
1485+
1486+
type GatewayRoutingParams = {
1487+
baseUrl: string;
1488+
headers?: Record<string, string> | undefined;
1489+
providerName?: string | undefined;
1490+
apiType: acp.LlmProtocol;
1491+
};
1492+
1493+
/** The custom gateway routing for `params`, not yet listed. Throws `invalid_params` like {@link CodexAcpClient.setProvider}. */
1494+
function buildGatewayConfig(params: GatewayRoutingParams): GatewayConfig {
1495+
const apiType = params.apiType;
1496+
const wireApi = SUPPORTED_GATEWAY_PROTOCOLS[apiType];
1497+
if (!wireApi) {
1498+
throw RequestError.invalidParams(
1499+
{apiType},
1500+
`Unsupported provider apiType "${apiType}"; supported: ${Object.keys(SUPPORTED_GATEWAY_PROTOCOLS).join(", ")}`,
1501+
);
1502+
}
1503+
if (typeof params.baseUrl !== "string" || params.baseUrl.trim().length === 0) {
1504+
throw RequestError.invalidParams(undefined, "baseUrl must be a non-empty string");
14471505
}
1506+
const providerName = typeof params.providerName === "string" && params.providerName.trim().length > 0
1507+
? params.providerName
1508+
: "User-provided gateway";
1509+
const headers: Record<string, string> = {
1510+
"X-Client-Feature-ID": "codex",
1511+
...params.headers,
1512+
};
1513+
return {
1514+
modelProvider: CUSTOM_GATEWAY_PROVIDER_ID,
1515+
config: {
1516+
name: providerName,
1517+
base_url: params.baseUrl,
1518+
http_headers: headers,
1519+
wire_api: wireApi,
1520+
},
1521+
listedModels: null,
1522+
};
1523+
}
1524+
1525+
function sameGatewayRouting(left: GatewayConfig, right: GatewayConfig): boolean {
1526+
const leftHeaders = Object.entries(left.config.http_headers);
1527+
const rightHeaders = right.config.http_headers;
1528+
return left.config.base_url === right.config.base_url
1529+
&& left.config.wire_api === right.config.wire_api
1530+
&& leftHeaders.length === Object.keys(rightHeaders).length
1531+
&& leftHeaders.every(([name, value]) => rightHeaders[name] === value);
14481532
}
14491533

14501534
function readMetaAdditionalRoots(meta?: Record<string, unknown> | null): string[] | undefined {

0 commit comments

Comments
 (0)