Skip to content

feat: Add support for default model per provider #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions codex-cli/src/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from "./utils/get-api-key";
import { createInputItem } from "./utils/input-utils";
import { initLogger } from "./utils/logger/log";
import { getModelForProvider } from "./utils/model-selection";
import { isModelSupportedForResponses } from "./utils/model-utils.js";
import { parseToolCall } from "./utils/parsers";
import { onExit, setInkRenderer } from "./utils/terminal";
Expand Down Expand Up @@ -289,10 +290,16 @@ let config = loadConfig(undefined, undefined, {
// via the `--history` flag. Therefore it must be declared with `let` rather
// than `const`.
let prompt = cli.input[0];
const model = cli.flags.model ?? config.model;
const imagePaths = cli.flags.image;
const provider = cli.flags.provider ?? config.provider ?? "openai";

// Get the appropriate model for the selected provider
const model = getModelForProvider(config, {
model: cli.flags.model,
provider,
});

const imagePaths = cli.flags.image;

const client = {
issuer: "https://auth.openai.com",
client_id: "app_EMoamEEZ73f0CkXaXp7hrann",
Expand Down
11 changes: 9 additions & 2 deletions codex-cli/src/components/chat/terminal-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,17 @@ export default function TerminalChat({
prev && newModel !== model ? null : prev,
);

// Save model to config
// Save model to config and update the default model for this provider
const providerDefaultModels = {
...(config.providerDefaultModels || {}),
[provider]: newModel,
};

saveConfig({
...config,
model: newModel,
provider: provider,
providerDefaultModels,
});

setItems((prev) => [
Expand Down Expand Up @@ -674,7 +680,8 @@ export default function TerminalChat({
setLoading(false);

// Select default model for the new provider.
const defaultModel = model;
const defaultModel =
config.providerDefaultModels?.[newProvider] || model;

// Save provider to config.
const updatedConfig = {
Expand Down
28 changes: 26 additions & 2 deletions codex-cli/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ export type StoredConfig = {
/** Disable server-side response storage (send full transcript each request) */
disableResponseStorage?: boolean;
flexMode?: boolean;
providers?: Record<string, { name: string; baseURL: string; envKey: string }>;
providers?: Record<
string,
{
name: string;
baseURL: string;
envKey: string;
/** Default model to use for this provider */
defaultModel?: string;
}
>;
/** Map of provider IDs to their default models */
providerDefaultModels?: Record<string, string>;
history?: {
maxSize?: number;
saveHistory?: boolean;
Expand Down Expand Up @@ -202,7 +213,18 @@ export type AppConfig = {

/** Enable the "flex-mode" processing mode for supported models (o3, o4-mini) */
flexMode?: boolean;
providers?: Record<string, { name: string; baseURL: string; envKey: string }>;
providers?: Record<
string,
{
name: string;
baseURL: string;
envKey: string;
/** Default model to use for this provider */
defaultModel?: string;
}
>;
/** Map of provider IDs to their default models */
providerDefaultModels?: Record<string, string>;
history?: {
maxSize: number;
saveHistory: boolean;
Expand Down Expand Up @@ -439,6 +461,7 @@ export const loadConfig = (
disableResponseStorage: storedConfig.disableResponseStorage === true,
reasoningEffort: storedConfig.reasoningEffort,
fileOpener: storedConfig.fileOpener,
providerDefaultModels: storedConfig.providerDefaultModels,
};

// -----------------------------------------------------------------------
Expand Down Expand Up @@ -560,6 +583,7 @@ export const saveConfig = (
disableResponseStorage: config.disableResponseStorage,
flexMode: config.flexMode,
reasoningEffort: config.reasoningEffort,
providerDefaultModels: config.providerDefaultModels,
};

// Add history settings if they exist
Expand Down
34 changes: 34 additions & 0 deletions codex-cli/src/utils/model-selection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { AppConfig } from "./config";

/**
* Get the appropriate model for the selected provider
*
* @param config The application configuration
* @param cliFlags The CLI flags containing model and provider information
* @returns The model to use
*/
export function getModelForProvider(
config: AppConfig,
cliFlags: { model?: string; provider?: string },
): string {
// CLI model flag takes precedence
if (cliFlags.model) {
return cliFlags.model;
}

// If provider is specified and there's a default model for it in providerDefaultModels, use that
if (cliFlags.provider && config.providerDefaultModels?.[cliFlags.provider]) {
return config.providerDefaultModels[cliFlags.provider] as string;
}

// If provider is specified and there's a provider config with defaultModel, use that
if (
cliFlags.provider &&
config.providers?.[cliFlags.provider]?.defaultModel
) {
return config.providers[cliFlags.provider]?.defaultModel as string;
}

// Fall back to global default model
return config.model;
}
Loading