( baseUrl: string, accessToken: string, fetchFn?: ConnectionEffectFetch, )
| 437 | * ChatGPT/Codex picker order. |
| 438 | */ |
| 439 | export async function fetchOpenAiCodexModels( |
| 440 | baseUrl: string, |
| 441 | accessToken: string, |
| 442 | fetchFn?: ConnectionEffectFetch, |
| 443 | ): Promise<ModelInfo[]> { |
| 444 | const response = await fetchForConnectionEffect( |
| 445 | fetchFn, |
| 446 | `${stripTrailing(baseUrl)}/models?client_version=1.0.0`, |
| 447 | { |
| 448 | headers: { |
| 449 | Authorization: `Bearer ${accessToken}`, |
| 450 | ...openAiCodexHeaders(accessToken), |
| 451 | 'content-type': 'application/json', |
| 452 | }, |
| 453 | timeoutMs: MODEL_FETCH_TIMEOUT_MS, |
| 454 | }, |
| 455 | ); |
| 456 | if (!response.ok) { |
| 457 | await response.cancel(); |
| 458 | throw new OpenAiCodexDiscoveryError(response.status); |
| 459 | } |
| 460 | const payload = await readProviderJson<{ models?: unknown }>(response); |
| 461 | const models = providerObjectArray<RawOpenAiCodexModel>( |
| 462 | payload.models, |
| 463 | 'OpenAI Codex models', |
| 464 | true, |
| 465 | ); |
| 466 | const visible = models.filter((model) => { |
| 467 | if (!model || typeof model.slug !== 'string' || !model.slug.trim()) return false; |
| 468 | const visibility = |
| 469 | typeof model.visibility === 'string' ? model.visibility.trim().toLowerCase() : ''; |
| 470 | return visibility !== 'hide' && visibility !== 'hidden'; |
| 471 | }); |
| 472 | visible.sort((a, b) => priorityOfOpenAiCodexModel(a) - priorityOfOpenAiCodexModel(b)); |
| 473 | return visible.map((model) => { |
| 474 | const entry: ModelInfo = { id: (model.slug as string).trim() }; |
| 475 | const contextWindow = contextWindowOfOpenAiCodexModel(model); |
| 476 | if (contextWindow !== undefined) entry.contextWindow = contextWindow; |
| 477 | return entry; |
| 478 | }); |
| 479 | } |
| 480 | |
| 481 | function priorityOfOpenAiCodexModel(model: RawOpenAiCodexModel): number { |
| 482 | return typeof model.priority === 'number' && Number.isFinite(model.priority) |
no test coverage detected