Skip to content

Commit 2b2bddd

Browse files
committed
feat(api/native): introduce proper stateful Conversation via native bindings\n\n- Native: add pyclass with iteration and control methods\n (submit_user_turn, submit_review, approve_exec, approve_patch, interrupt,\n shutdown) and constructor. Stores per-turn defaults\n derived from Config and updates them per turn.
- API: restore and wrappers over the native handle. - Keep , , and the convenience for one-offs. - Update exports and smoke tests.\n\nBuild & tests:\n- make dev-native\n- make lint && make test (all green).
1 parent 60d93ae commit 2b2bddd

11 files changed

Lines changed: 1608 additions & 283 deletions

File tree

CODE_OF_CONDUCT.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ If there’s no prebuilt wheel for your platform/Python, pip will build from sou
3131
Run a prompt and collect structured events (typed):
3232

3333
```python
34-
from codex.api import run_exec, CodexClient, CodexNativeError
34+
from codex import run_exec, CodexClient, CodexNativeError
3535
from codex.config import CodexConfig, ApprovalPolicy, SandboxMode
3636

3737
cfg = CodexConfig(
@@ -51,26 +51,42 @@ except CodexNativeError as e:
5151

5252
# Conversation (streaming)
5353
client = CodexClient(config=cfg)
54-
for ev in client.start_conversation("Add a smoke test"):
55-
print(ev.id, ev.msg.type)
54+
conv = client.start_conversation()
55+
conv.submit_user_turn("Add a smoke test")
56+
for ev in conv:
57+
print(ev.id, getattr(getattr(ev.msg, "root", ev.msg), "type", "unknown"))
5658
```
5759

5860
Notes
5961
- `Event.msg` is a typed union (`EventMsg`). For raw dicts from the native layer, use `codex.native.start_exec_stream`.
6062

6163
## 3) API Overview
6264

63-
- `codex.api.run_exec(prompt, config, load_default_config=True) -> list[Event]`
64-
Synchronous one‑shot execution; returns all events.
65+
- `codex.run_exec(prompt, *, config=None, load_default_config=True, output_schema=None) -> list[Event]`
66+
Synchronous one‑shot; returns all events.
6567

66-
- `codex.api.CodexClient(config).start_conversation(prompt, load_default_config=None) -> Iterable[Event]`
67-
Streaming conversation iterator.
68+
- `codex.run_review(prompt, *, user_facing_hint=None, config=None, load_default_config=True) -> list[Event]`
69+
Synchronous review flow; returns all events.
70+
71+
- `codex.run_prompt(prompt, *, config=None, load_default_config=True, output_schema=None) -> str | Any`
72+
Convenience: returns the final assistant message (or parsed JSON when `output_schema` is set).
73+
74+
- `codex.CodexClient(config).start_conversation(... ) -> Conversation`
75+
Creates a stateful session. Iterate over `Conversation` to stream events.
76+
- `Conversation.submit_user_turn(prompt, *, cwd=None, approval_policy=..., sandbox_mode=..., model=None, effort=..., summary=..., output_schema=None)`
77+
- `Conversation.submit_review(prompt, user_facing_hint=None)`
78+
- `Conversation.approve_exec(id, decision)` / `approve_patch(id, decision)`
79+
- `Conversation.interrupt()`, `Conversation.shutdown()`
80+
- Utility: `user_input_text`, `override_turn_context`, `add_to_history`, `get_history_entry`, `get_path`, `list_mcp_tools`, `list_custom_prompts`, `compact`
81+
82+
- `await codex.CodexClient(...).astart_conversation() -> AsyncConversation`
83+
Async iterator with the same methods as `Conversation` (async variants).
6884

6985
- Exceptions
70-
- `CodexError` base class; `CodexNativeError` wraps native failures and missing extension.
86+
- `CodexError` base; `CodexNativeError` wraps native failures / missing extension.
7187

72-
- Native helpers
73-
- `codex.native.preview_config(config_overrides, load_default_config)` returns a compact snapshot of the effective configuration (useful in tests).
88+
- Native helper
89+
- `codex.native.preview_config(config_overrides, load_default_config)` compact effective config snapshot (testing aid).
7490

7591
## 4) Configuration
7692

codex/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
events = run_exec("explain this codebase to me")
88
"""
99

10-
from .api import (
11-
CodexClient,
12-
CodexError,
13-
CodexNativeError,
14-
Conversation,
15-
run_exec,
16-
)
10+
from .client import AsyncConversation, CodexClient, CodexError, CodexNativeError, Conversation
1711
from .config import CodexConfig
1812
from .event import Event
13+
from .exec import run_exec, run_prompt, run_review
1914
from .protocol.types import EventMsg
2015

2116
__all__ = [
@@ -24,7 +19,10 @@
2419
"CodexNativeError",
2520
"CodexClient",
2621
"Conversation",
22+
"AsyncConversation",
23+
"run_prompt",
2724
"run_exec",
25+
"run_review",
2826
"Event",
2927
"EventMsg",
3028
"CodexConfig",

codex/api.py

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)