@@ -99,3 +99,67 @@ def run_exec(
9999 )
100100 return stdout
101101
102+
103+ @dataclass (slots = True )
104+ class CodexClient :
105+ """Lightweight, synchronous client for the Codex CLI.
106+
107+ Provides defaults for repeated invocations and convenience helpers.
108+ """
109+
110+ executable : str = "codex"
111+ model : Optional [str ] = None
112+ full_auto : bool = False
113+ cd : Optional [str ] = None
114+ env : Optional [Mapping [str , str ]] = None
115+ extra_args : Optional [Sequence [str ]] = None
116+
117+ def ensure_available (self ) -> str :
118+ """Return the resolved binary path or raise CodexNotFoundError."""
119+ return find_binary (self .executable )
120+
121+ def run (
122+ self ,
123+ prompt : str ,
124+ * ,
125+ model : Optional [str ] = None ,
126+ full_auto : Optional [bool ] = None ,
127+ cd : Optional [str ] = None ,
128+ timeout : Optional [float ] = None ,
129+ env : Optional [Mapping [str , str ]] = None ,
130+ extra_args : Optional [Iterable [str ]] = None ,
131+ ) -> str :
132+ """Execute `codex exec` and return stdout.
133+
134+ Explicit arguments override the client's defaults.
135+ """
136+ eff_model = model if model is not None else self .model
137+ eff_full_auto = full_auto if full_auto is not None else self .full_auto
138+ eff_cd = cd if cd is not None else self .cd
139+
140+ # Merge environment overlays; run_exec will merge with os.environ
141+ merged_env : Optional [Mapping [str , str ]]
142+ if self .env and env :
143+ tmp = dict (self .env )
144+ tmp .update (env )
145+ merged_env = tmp
146+ else :
147+ merged_env = env or self .env
148+
149+ # Compose extra args
150+ eff_extra : list [str ] = []
151+ if self .extra_args :
152+ eff_extra .extend (self .extra_args )
153+ if extra_args :
154+ eff_extra .extend (list (extra_args ))
155+
156+ return run_exec (
157+ prompt ,
158+ model = eff_model ,
159+ full_auto = eff_full_auto ,
160+ cd = eff_cd ,
161+ timeout = timeout ,
162+ env = merged_env ,
163+ executable = self .executable ,
164+ extra_args = eff_extra ,
165+ )
0 commit comments