| import { |
| PROVIDER_BINDING_KEY, |
| SUPPORTED_ADAPTER_TYPES, |
| } from "../constants.js"; |
| import type { GlobalProviderRecord } from "../types.js"; |
|
|
| type EnvBinding = |
| | { type: "plain"; value: string } |
| | { type: "secret_ref"; secretId: string; version?: number | "latest" }; |
|
|
| function buildCodexBaseUrlOverrides(baseUrl: string): string[] { |
| const encodedBaseUrl = JSON.stringify(baseUrl); |
| return [ |
| "-c", |
| 'model_provider="custom"', |
| "-c", |
| 'model_providers.custom.name="custom"', |
| "-c", |
| 'model_providers.custom.wire_api="responses"', |
| "-c", |
| "model_providers.custom.requires_openai_auth=true", |
| "-c", |
| `model_providers.custom.base_url=${encodedBaseUrl}`, |
| "-c", |
| `openai_base_url=${encodedBaseUrl}`, |
| ]; |
| } |
|
|
| function readStringArray(value: unknown): string[] { |
| if (!Array.isArray(value)) return []; |
| return value.filter((entry): entry is string => typeof entry === "string" && entry.trim().length > 0); |
| } |
|
|
| function stripManagedCodexProviderArgs(args: string[]): string[] { |
| const managedPatterns = [ |
| /^model_provider=/, |
| /^model_providers\.custom\./, |
| /^openai_base_url=/, |
| ]; |
|
|
| const next: string[] = []; |
| for (let index = 0; index < args.length; index += 1) { |
| const current = args[index]?.trim(); |
| const following = args[index + 1]?.trim(); |
| if (current === "-c" && following && managedPatterns.some((pattern) => pattern.test(following))) { |
| index += 1; |
| continue; |
| } |
| next.push(args[index]!); |
| } |
| return next; |
| } |
|
|
| function asEnvRecord(value: unknown): Record<string, EnvBinding> { |
| if (!value || typeof value !== "object" || Array.isArray(value)) return {}; |
| return value as Record<string, EnvBinding>; |
| } |
|
|
| export function isSupportedAdapterType(adapterType: string): boolean { |
| return SUPPORTED_ADAPTER_TYPES.includes(adapterType as (typeof SUPPORTED_ADAPTER_TYPES)[number]); |
| } |
|
|
| export function buildOpenAiAgentPatch(input: { |
| adapterType: string; |
| currentAdapterConfig?: Record<string, unknown> | null; |
| provider: Pick<GlobalProviderRecord, "id" | "name" | "baseUrl">; |
| apiKey: string; |
| model: string; |
| syncedAt: string; |
| }) { |
| const current = { ...(input.currentAdapterConfig ?? {}) }; |
| const env = { ...asEnvRecord(current.env) }; |
| env.OPENAI_API_KEY = { |
| type: "plain", |
| value: input.apiKey, |
| }; |
| env.CODEX_API_KEY = { |
| type: "plain", |
| value: input.apiKey, |
| }; |
| env.OPENAI_BASE_URL = { |
| type: "plain", |
| value: input.provider.baseUrl, |
| }; |
| env.OPENAI_API_BASE = { |
| type: "plain", |
| value: input.provider.baseUrl, |
| }; |
| env.OPENAI_API_BASE_URL = { |
| type: "plain", |
| value: input.provider.baseUrl, |
| }; |
|
|
| const nextConfig: Record<string, unknown> = { |
| ...current, |
| model: input.model, |
| env, |
| [PROVIDER_BINDING_KEY]: { |
| providerId: input.provider.id, |
| providerName: input.provider.name, |
| providerBaseUrl: input.provider.baseUrl, |
| managedByPlugin: true, |
| syncedAt: input.syncedAt, |
| }, |
| }; |
|
|
| if (input.adapterType === "codex_local") { |
| const existingExtraArgs = stripManagedCodexProviderArgs(readStringArray(current.extraArgs)); |
| const overrideArgs = buildCodexBaseUrlOverrides(input.provider.baseUrl); |
| nextConfig.extraArgs = [...existingExtraArgs, ...overrideArgs]; |
| } |
|
|
| return { |
| adapterConfig: nextConfig, |
| }; |
| } |
|
|