@@ -6,7 +6,7 @@ defmodule Codex.Options do
66 """
77
88 require Bitwise
9- alias CliSubprocessCore.ModelRegistry
9+ alias CliSubprocessCore.ModelInput
1010 alias Codex.Auth
1111 alias Codex.Config.BaseURL
1212 alias Codex.Config.OptionNormalizers
@@ -80,22 +80,26 @@ defmodule Codex.Options do
8080 { :ok , base_url } <- fetch_base_url ( attrs ) ,
8181 { :ok , override } <- fetch_codex_path_override ( attrs ) ,
8282 { :ok , telemetry_prefix } <- fetch_telemetry_prefix ( attrs ) ,
83- { :ok , model_payload } <- resolve_model_payload ( attrs , auth_mode_for ( api_key ) ) ,
83+ { :ok , model_input } <- normalize_model_input ( attrs ) ,
84+ model_payload = model_input . selection ,
85+ normalized_attrs = model_input . attrs ,
8486 { :ok , model } <- fetch_model ( model_payload ) ,
8587 { :ok , reasoning_effort } <- fetch_reasoning_effort ( model_payload ) ,
86- { :ok , model_personality } <- fetch_model_personality ( attrs ) ,
87- { :ok , reasoning_summary } <- fetch_reasoning_summary ( attrs ) ,
88- { :ok , model_verbosity } <- fetch_model_verbosity ( attrs ) ,
89- { :ok , model_context_window } <- fetch_model_context_window ( attrs ) ,
90- { :ok , supports_reasoning_summaries } <- fetch_supports_reasoning_summaries ( attrs ) ,
91- { :ok , model_auto_compact_token_limit } <- fetch_model_auto_compact_token_limit ( attrs ) ,
92- { :ok , review_model } <- fetch_review_model ( attrs ) ,
93- { :ok , history_persistence } <- fetch_history_persistence ( attrs ) ,
94- { :ok , history_max_bytes } <- fetch_history_max_bytes ( attrs ) ,
95- { :ok , hide_agent_reasoning } <- fetch_hide_agent_reasoning ( attrs ) ,
96- { :ok , tool_output_token_limit } <- fetch_tool_output_token_limit ( attrs ) ,
97- { :ok , agent_max_threads } <- fetch_agent_max_threads ( attrs ) ,
98- { :ok , config_overrides } <- fetch_config_overrides ( attrs ) do
88+ { :ok , model_personality } <- fetch_model_personality ( normalized_attrs ) ,
89+ { :ok , reasoning_summary } <- fetch_reasoning_summary ( normalized_attrs ) ,
90+ { :ok , model_verbosity } <- fetch_model_verbosity ( normalized_attrs ) ,
91+ { :ok , model_context_window } <- fetch_model_context_window ( normalized_attrs ) ,
92+ { :ok , supports_reasoning_summaries } <-
93+ fetch_supports_reasoning_summaries ( normalized_attrs ) ,
94+ { :ok , model_auto_compact_token_limit } <-
95+ fetch_model_auto_compact_token_limit ( normalized_attrs ) ,
96+ { :ok , review_model } <- fetch_review_model ( normalized_attrs ) ,
97+ { :ok , history_persistence } <- fetch_history_persistence ( normalized_attrs ) ,
98+ { :ok , history_max_bytes } <- fetch_history_max_bytes ( normalized_attrs ) ,
99+ { :ok , hide_agent_reasoning } <- fetch_hide_agent_reasoning ( normalized_attrs ) ,
100+ { :ok , tool_output_token_limit } <- fetch_tool_output_token_limit ( normalized_attrs ) ,
101+ { :ok , agent_max_threads } <- fetch_agent_max_threads ( normalized_attrs ) ,
102+ { :ok , config_overrides } <- fetch_config_overrides ( normalized_attrs ) do
99103 { :ok ,
100104 % __MODULE__ {
101105 api_key: api_key ,
@@ -214,46 +218,34 @@ defmodule Codex.Options do
214218
215219 defp pick ( _attrs , [ ] , default ) , do: default
216220
217- defp resolve_model_payload ( attrs , _auth_mode ) do
218- requested_model = pick ( attrs , [ :model , "model" ] )
219-
220- provider_backend =
221- pick (
222- attrs ,
223- [ :provider_backend , "provider_backend" ] ,
224- System . get_env ( "CODEX_PROVIDER_BACKEND" )
225- )
226-
227- oss_provider =
228- pick ( attrs , [ :oss_provider , "oss_provider" ] , System . get_env ( "CODEX_OSS_PROVIDER" ) )
229-
230- ollama_base_url =
231- pick ( attrs , [ :ollama_base_url , "ollama_base_url" ] , System . get_env ( "CODEX_OLLAMA_BASE_URL" ) )
232-
233- requested_reasoning =
234- pick ( attrs , [ :reasoning_effort , "reasoning_effort" , :reasoning , "reasoning" ] )
235-
236- opts =
237- [ ]
238- |> maybe_put_env_model (
239- System . get_env ( "CODEX_MODEL" ) ||
240- System . get_env ( "OPENAI_DEFAULT_MODEL" ) ||
241- System . get_env ( "CODEX_MODEL_DEFAULT" )
242- )
243- |> maybe_put_reasoning ( requested_reasoning )
244- |> maybe_put_registry_opt ( :provider_backend , provider_backend )
245- |> maybe_put_registry_opt ( :oss_provider , oss_provider )
246- |> maybe_put_registry_opt ( :ollama_base_url , ollama_base_url )
247- |> maybe_put_registry_opt (
248- :ollama_http ,
249- pick ( attrs , [ :ollama_http , "ollama_http" ] )
250- )
251- |> maybe_put_registry_opt (
252- :ollama_timeout_ms ,
253- pick ( attrs , [ :ollama_timeout_ms , "ollama_timeout_ms" ] )
254- )
255-
256- ModelRegistry . build_arg_payload ( :codex , requested_model , opts )
221+ defp normalize_model_input ( attrs ) do
222+ attrs
223+ |> apply_model_env_defaults ( )
224+ |> then ( & ModelInput . normalize ( :codex , & 1 , [ ] ) )
225+ end
226+
227+ defp apply_model_env_defaults ( attrs ) when is_map ( attrs ) do
228+ attrs
229+ |> put_missing_attr (
230+ :env_model ,
231+ System . get_env ( "CODEX_MODEL" ) ||
232+ System . get_env ( "OPENAI_DEFAULT_MODEL" ) ||
233+ System . get_env ( "CODEX_MODEL_DEFAULT" )
234+ )
235+ |> put_missing_attr ( :provider_backend , System . get_env ( "CODEX_PROVIDER_BACKEND" ) )
236+ |> put_missing_attr ( :oss_provider , System . get_env ( "CODEX_OSS_PROVIDER" ) )
237+ |> put_missing_attr ( :ollama_base_url , System . get_env ( "CODEX_OLLAMA_BASE_URL" ) )
238+ end
239+
240+ defp put_missing_attr ( attrs , _key , nil ) , do: attrs
241+ defp put_missing_attr ( attrs , _key , "" ) , do: attrs
242+
243+ defp put_missing_attr ( attrs , key , value ) when is_map ( attrs ) and is_atom ( key ) do
244+ cond do
245+ Map . has_key? ( attrs , key ) -> attrs
246+ Map . has_key? ( attrs , Atom . to_string ( key ) ) -> attrs
247+ true -> Map . put ( attrs , key , value )
248+ end
257249 end
258250
259251 defp fetch_model ( model_payload ) when is_map ( model_payload ) do
@@ -267,16 +259,6 @@ defmodule Codex.Options do
267259 { :ok , normalize_reasoning_atom ( reasoning ) }
268260 end
269261
270- defp maybe_put_env_model ( opts , nil ) , do: opts
271- defp maybe_put_env_model ( opts , env_model ) , do: Keyword . put ( opts , :env_model , env_model )
272-
273- defp maybe_put_reasoning ( opts , nil ) , do: opts
274- defp maybe_put_reasoning ( opts , reasoning ) , do: Keyword . put ( opts , :reasoning_effort , reasoning )
275-
276- defp maybe_put_registry_opt ( opts , _key , nil ) , do: opts
277- defp maybe_put_registry_opt ( opts , _key , "" ) , do: opts
278- defp maybe_put_registry_opt ( opts , key , value ) , do: Keyword . put ( opts , key , value )
279-
280262 defp normalize_reasoning_atom ( nil ) , do: nil
281263 defp normalize_reasoning_atom ( value ) when is_atom ( value ) , do: value
282264 defp normalize_reasoning_atom ( value ) when is_binary ( value ) , do: String . to_atom ( value )
@@ -423,9 +405,6 @@ defmodule Codex.Options do
423405 |> Overrides . normalize_config_overrides ( )
424406 end
425407
426- defp auth_mode_for ( api_key ) when is_binary ( api_key ) and api_key != "" , do: :api
427- defp auth_mode_for ( _ ) , do: Auth . infer_auth_mode ( )
428-
429408 defp normalize_string ( nil ) , do: nil
430409
431410 defp normalize_string ( value ) when is_binary ( value ) do
0 commit comments