codex-python 0.1.1__py3-none-any.whl → 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
codex/__init__.py CHANGED
@@ -27,4 +27,4 @@ __all__ = [
27
27
  ]
28
28
 
29
29
  # Managed by Hatch via pyproject.toml [tool.hatch.version]
30
- __version__ = "0.1.1"
30
+ __version__ = "0.1.2"
codex/api.py CHANGED
@@ -51,12 +51,15 @@ def run_exec(
51
51
  prompt: str,
52
52
  *,
53
53
  model: str | None = None,
54
+ oss: bool = False,
54
55
  full_auto: bool = False,
55
56
  cd: str | None = None,
57
+ skip_git_repo_check: bool = False,
56
58
  timeout: float | None = None,
57
59
  env: Mapping[str, str] | None = None,
58
60
  executable: str = "codex",
59
61
  extra_args: Iterable[str] | None = None,
62
+ json: bool = False,
60
63
  ) -> str:
61
64
  """
62
65
  Run `codex exec` with the given prompt and return stdout as text.
@@ -72,12 +75,19 @@ def run_exec(
72
75
  cmd.extend(["--cd", cd])
73
76
  if model:
74
77
  cmd.extend(["-m", model])
78
+ if oss:
79
+ cmd.append("--oss")
75
80
  if full_auto:
76
81
  cmd.append("--full-auto")
82
+ if skip_git_repo_check:
83
+ cmd.append("--skip-git-repo-check")
77
84
  if extra_args:
78
85
  cmd.extend(list(extra_args))
79
86
 
80
- cmd.extend(["exec", prompt])
87
+ cmd.append("exec")
88
+ if json:
89
+ cmd.append("--json")
90
+ cmd.append(prompt)
81
91
 
82
92
  completed = subprocess.run(
83
93
  cmd,
@@ -123,8 +133,10 @@ class CodexClient:
123
133
  prompt: str,
124
134
  *,
125
135
  model: str | None = None,
136
+ oss: bool | None = None,
126
137
  full_auto: bool | None = None,
127
138
  cd: str | None = None,
139
+ skip_git_repo_check: bool | None = None,
128
140
  timeout: float | None = None,
129
141
  env: Mapping[str, str] | None = None,
130
142
  extra_args: Iterable[str] | None = None,
@@ -136,6 +148,8 @@ class CodexClient:
136
148
  eff_model = model if model is not None else self.model
137
149
  eff_full_auto = full_auto if full_auto is not None else self.full_auto
138
150
  eff_cd = cd if cd is not None else self.cd
151
+ eff_oss = bool(oss) if oss is not None else False
152
+ eff_skip_git = bool(skip_git_repo_check) if skip_git_repo_check is not None else False
139
153
 
140
154
  # Merge environment overlays; run_exec will merge with os.environ
141
155
  merged_env: Mapping[str, str] | None
@@ -156,8 +170,10 @@ class CodexClient:
156
170
  return run_exec(
157
171
  prompt,
158
172
  model=eff_model,
173
+ oss=eff_oss,
159
174
  full_auto=eff_full_auto,
160
175
  cd=eff_cd,
176
+ skip_git_repo_check=eff_skip_git,
161
177
  timeout=timeout,
162
178
  env=merged_env,
163
179
  executable=self.executable,
@@ -0,0 +1,80 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import subprocess
6
+ from collections.abc import Iterator
7
+ from typing import Any
8
+
9
+ from pydantic import BaseModel
10
+
11
+
12
+ class Event(BaseModel):
13
+ """Protocol event envelope emitted by `codex exec --json`.
14
+
15
+ Note: `msg` is kept as a raw mapping to preserve all fields from
16
+ intersection types. If you need strong typing, try validating
17
+ against `codex.protocol.types.EventMsg` manually.
18
+ """
19
+
20
+ id: str
21
+ msg: dict[str, Any]
22
+
23
+
24
+ def stream_exec_events(
25
+ prompt: str,
26
+ *,
27
+ executable: str = "codex",
28
+ model: str | None = None,
29
+ oss: bool = False,
30
+ full_auto: bool = False,
31
+ cd: str | None = None,
32
+ skip_git_repo_check: bool = False,
33
+ env: dict[str, str] | None = None,
34
+ ) -> Iterator[Event]:
35
+ """Spawn `codex exec --json` and yield Event objects from NDJSON stdout.
36
+
37
+ Non-event lines (config summary, prompt echo) are ignored.
38
+ """
39
+ cmd: list[str] = [executable]
40
+ if cd:
41
+ cmd += ["--cd", cd]
42
+ if model:
43
+ cmd += ["-m", model]
44
+ if oss:
45
+ cmd.append("--oss")
46
+ if full_auto:
47
+ cmd.append("--full-auto")
48
+ if skip_git_repo_check:
49
+ cmd.append("--skip-git-repo-check")
50
+ cmd += ["exec", "--json", prompt]
51
+
52
+ with subprocess.Popen(
53
+ cmd,
54
+ stdout=subprocess.PIPE,
55
+ stderr=subprocess.PIPE,
56
+ text=True,
57
+ env={**os.environ, **(env or {})},
58
+ ) as proc:
59
+ assert proc.stdout is not None
60
+ for line in proc.stdout:
61
+ line = line.strip()
62
+ if not line:
63
+ continue
64
+ try:
65
+ obj = json.loads(line)
66
+ except json.JSONDecodeError:
67
+ continue
68
+
69
+ # Filter out non-event helper lines
70
+ if not isinstance(obj, dict):
71
+ continue
72
+ if "id" in obj and "msg" in obj:
73
+ # Attempt to validate into our Pydantic Event model
74
+ yield Event.model_validate(obj)
75
+
76
+ # Drain stderr for diagnostics if the process failed
77
+ ret = proc.wait()
78
+ if ret != 0 and proc.stderr is not None:
79
+ err = proc.stderr.read()
80
+ raise RuntimeError(f"codex exec failed with {ret}: {err}")