from __future__ import annotations import os import subprocess from dataclasses import dataclass @dataclass class CodexResult: final_response: str finish_reason: str = "completed" class CodexHarness: """ Adapter for the official OpenAI Codex CLI. The application keeps the old dsh.run(...) interface, but the actual reasoning engine is Codex. """ def __init__(self, model, api_key, base_url=None, session_root=None, system_prompt=None, **kwargs): self.model = model or os.environ.get("CODEX_MODEL", "gpt-5-codex") self.system_prompt = system_prompt or "" def run(self, prompt, session_id=None): full_prompt = self.system_prompt + "\n\n" + prompt cmd = [ "codex", "exec", "--model", self.model, full_prompt, ] env = os.environ.copy() proc = subprocess.run( cmd, env=env, capture_output=True, text=True, timeout=600, ) if proc.returncode != 0: raise RuntimeError(proc.stderr[-1000:] or "Codex failed") return CodexResult(final_response=proc.stdout.strip())