codexapi 0.7.2__tar.gz → 0.9.0__tar.gz

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.
Files changed (32) hide show
  1. {codexapi-0.7.2/src/codexapi.egg-info → codexapi-0.9.0}/PKG-INFO +122 -33
  2. {codexapi-0.7.2 → codexapi-0.9.0}/README.md +121 -32
  3. {codexapi-0.7.2 → codexapi-0.9.0}/pyproject.toml +1 -1
  4. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/__init__.py +2 -2
  5. codexapi-0.9.0/src/codexapi/agent.py +389 -0
  6. codexapi-0.9.0/src/codexapi/agents.py +1380 -0
  7. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/cli.py +531 -27
  8. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/foreach.py +4 -0
  9. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/gh_integration.py +4 -1
  10. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/lead.py +16 -7
  11. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/ralph.py +5 -1
  12. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/rate_limits.py +16 -1
  13. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/science.py +5 -2
  14. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/task.py +44 -10
  15. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/taskfile.py +3 -0
  16. {codexapi-0.7.2 → codexapi-0.9.0/src/codexapi.egg-info}/PKG-INFO +122 -33
  17. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi.egg-info/SOURCES.txt +4 -0
  18. codexapi-0.9.0/tests/test_agent_backend.py +83 -0
  19. codexapi-0.9.0/tests/test_agents.py +764 -0
  20. codexapi-0.9.0/tests/test_rate_limits.py +71 -0
  21. {codexapi-0.7.2 → codexapi-0.9.0}/tests/test_science.py +1 -0
  22. {codexapi-0.7.2 → codexapi-0.9.0}/tests/test_task_progress.py +10 -5
  23. codexapi-0.7.2/src/codexapi/agent.py +0 -173
  24. {codexapi-0.7.2 → codexapi-0.9.0}/LICENSE +0 -0
  25. {codexapi-0.7.2 → codexapi-0.9.0}/setup.cfg +0 -0
  26. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/__main__.py +0 -0
  27. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/pushover.py +0 -0
  28. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi/welfare.py +0 -0
  29. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi.egg-info/dependency_links.txt +0 -0
  30. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi.egg-info/entry_points.txt +0 -0
  31. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi.egg-info/requires.txt +0 -0
  32. {codexapi-0.7.2 → codexapi-0.9.0}/src/codexapi.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: codexapi
3
- Version: 0.7.2
3
+ Version: 0.9.0
4
4
  Summary: Minimal Python API for running the Codex CLI.
5
5
  License: MIT
6
6
  Keywords: codex,agent,cli,openai
@@ -15,13 +15,14 @@ Requires-Dist: tqdm>=4.64
15
15
 
16
16
  # CodexAPI
17
17
 
18
- Use OpenAI's codex from python as easily as calling a function with your codex credits instead of the API.
18
+ Use Codex or Cursor agents from python as easily as calling a function, using your CLI auth instead of the API.
19
19
 
20
20
  *Note: this project is not affiliated with OpenAI in any way. Thanks for the awesome tools and models though!*
21
21
 
22
22
  ## Requirements
23
23
 
24
- - Codex CLI installed and authenticated (`codex` must be on your PATH).
24
+ - Codex CLI installed and authenticated (`codex` must be on your PATH), or
25
+ - Cursor Agent CLI installed and authenticated (`cursor` must be on your PATH).
25
26
  - Python 3.8+.
26
27
 
27
28
  ## Install
@@ -59,14 +60,19 @@ result = task()
59
60
  print(result.success, result.summary)
60
61
  ```
61
62
 
63
+ Use `backend="cursor"` (or set `CODEXAPI_BACKEND=cursor`) to switch to the
64
+ Cursor agent backend.
65
+
62
66
  ## CLI
63
67
 
64
68
  After installing, use the `codexapi` command:
65
69
 
66
70
  ```bash
71
+ codexapi --version
67
72
  codexapi run "Summarize this repo."
68
73
  codexapi run --cwd /path/to/project "Fix the failing tests."
69
74
  echo "Say hello." | codexapi run
75
+ codexapi run --backend cursor "Summarize this repo."
70
76
  ```
71
77
 
72
78
  `codexapi task` exits with code 0 on success and 1 on failure.
@@ -122,15 +128,16 @@ Show running sessions and their latest activity:
122
128
  codexapi top
123
129
  ```
124
130
  Press `h` for keys.
131
+ `codexapi top` and `codexapi limit` are Codex-only.
125
132
 
126
- Resume a session and print the thread id to stderr:
133
+ Resume a session and print the thread/session id to stderr:
127
134
 
128
135
  ```bash
129
136
  codexapi run --thread-id THREAD_ID --print-thread-id "Continue where we left off."
130
137
  ```
131
138
 
132
- Use `--no-yolo` to run Codex with `--full-auto` instead.
133
- Use `--include-thinking` to return all agent messages joined together for `codexapi run`.
139
+ Use `--no-yolo` to disable `--yolo` (Codex uses `--full-auto`).
140
+ Use `--include-thinking` to return all agent messages joined together for `codexapi run` (Codex only).
134
141
 
135
142
  Lead mode periodically checks in on a long-running agent session with the
136
143
  current time and prints JSON status updates. The agent controls the loop by
@@ -150,14 +157,77 @@ If the leadbook does not exist, lead creates it with a template.
150
157
 
151
158
  ```bash
152
159
  codexapi lead 5 "Run the benchmark and wait for results."
160
+ codexapi lead 0 "Do a rapid triage pass and report."
161
+ ```
162
+
163
+ ### Durable agents
164
+
165
+ `codexapi agent` is the durable long-running control plane. It stores state
166
+ under `CODEXAPI_HOME` (default `~/.codexapi`), wakes agents on cron, and lets
167
+ you inspect or message them from any shell that points at the same home.
153
168
 
154
- Run without waiting between check-ins:
169
+ Start by checking the effective host/home pair and installing the scheduler:
155
170
 
156
171
  ```bash
157
- codexapi lead 0 "Do a rapid triage pass and report."
172
+ codexapi agent whoami
173
+ codexapi agent install-cron
174
+ ```
175
+
176
+ Start a goal-directed agent that decides for itself when it is done:
177
+
178
+ ```bash
179
+ codexapi agent start --name ci-fixer \
180
+ "Watch CI, fix failing tests, open or update a PR, and stop when the work is done."
181
+ ```
182
+
183
+ Start a persistent watcher that keeps running until you stop it:
184
+
185
+ ```bash
186
+ codexapi agent start --name issue-watcher \
187
+ --stop-policy until_stopped \
188
+ --heartbeat-minutes 30 \
189
+ "Every wake, scan for newly assigned issues that look actionable and report or start follow-up work."
190
+ ```
191
+
192
+ Inspect and talk to agents:
193
+
194
+ ```bash
195
+ codexapi agent list
196
+ codexapi agent show ci-fixer
197
+ codexapi agent read ci-fixer
198
+ codexapi agent book ci-fixer
199
+ codexapi agent send ci-fixer "Prefer the smallest safe fix."
200
+ codexapi agent wake ci-fixer
201
+ codexapi agent pause ci-fixer
202
+ codexapi agent resume ci-fixer
203
+ codexapi agent cancel ci-fixer
204
+ codexapi agent delete ci-fixer
158
205
  ```
206
+
207
+ Create a child agent explicitly:
208
+
209
+ ```bash
210
+ codexapi agent start --name child-fix --parent ci-fixer \
211
+ "Investigate the flaky integration test and report back."
159
212
  ```
160
213
 
214
+ Useful environment overrides:
215
+
216
+ ```bash
217
+ CODEXAPI_HOME=/tmp/codexapi-test-home codexapi agent list
218
+ CODEXAPI_HOSTNAME=stable-host codexapi agent whoami
219
+ ```
220
+
221
+ `CODEXAPI_HOME` isolates independent agent installations and is the right seam
222
+ for tests. `CODEXAPI_HOSTNAME` is useful when cron, shells, sandboxes, or test
223
+ wrappers report inconsistent hostnames for the same machine.
224
+
225
+ `codexapi agent show` also prints the resolved `AGENTBOOK.md` path so you can
226
+ jump directly to the durable working memory file.
227
+
228
+ See [docs/agent-v1.md](docs/agent-v1.md) for the filesystem model and scheduling
229
+ details.
230
+
161
231
  Ralph loop mode repeats the same prompt until a completion promise or a max
162
232
  iteration cap is hit (0 means unlimited). Cancel by deleting
163
233
  `.codexapi/ralph-loop.local.md` or running `codexapi ralph --cancel`.
@@ -206,31 +276,34 @@ codexapi foreach list.txt task.yaml --retry-all
206
276
 
207
277
  ## API
208
278
 
209
- ### `agent(prompt, cwd=None, yolo=True, flags=None, include_thinking=False) -> str`
279
+ ### `agent(prompt, cwd=None, yolo=True, flags=None, include_thinking=False, backend=None) -> str`
210
280
 
211
- Runs a single Codex turn and returns only the agent's message. Any reasoning
281
+ Runs a single agent turn and returns only the agent's message. Any reasoning
212
282
  items are filtered out.
213
283
 
214
- - `prompt` (str): prompt to send to Codex.
215
- - `cwd` (str | PathLike | None): working directory for the Codex session.
216
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
217
- - `flags` (str | None): extra CLI flags to pass to Codex.
284
+ - `prompt` (str): prompt to send to the agent backend.
285
+ - `cwd` (str | PathLike | None): working directory for the agent session.
286
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
287
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
218
288
  - `include_thinking` (bool): when true, return all agent messages joined.
289
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
219
290
 
220
- ### `Agent(cwd=None, yolo=True, thread_id=None, flags=None, welfare=False, include_thinking=False)`
291
+ ### `Agent(cwd=None, yolo=True, thread_id=None, flags=None, welfare=False, include_thinking=False, backend=None)`
221
292
 
222
293
  Creates a stateful session wrapper. Calling the instance sends the prompt into
223
294
  the same conversation and returns only the agent's message.
224
295
 
225
- - `__call__(prompt) -> str`: send a prompt to Codex and return the message.
296
+ - `__call__(prompt) -> str`: send a prompt to the agent backend and return the message.
226
297
  - `thread_id -> str | None`: expose the underlying session id once created.
227
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
228
- - `flags` (str | None): extra CLI flags to pass to Codex.
298
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
299
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
229
300
  - `welfare` (bool): when true, append welfare stop instructions to each prompt
230
301
  and raise `WelfareStop` if the agent outputs `MAKE IT STOP`.
231
302
  - `include_thinking` (bool): when true, return all agent messages joined.
303
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
304
+ For Cursor, `thread_id` corresponds to the `session_id` returned by the agent.
232
305
 
233
- ### `lead(minutes, prompt, cwd=None, yolo=True, flags=None, leadbook=None) -> dict`
306
+ ### `lead(minutes, prompt, cwd=None, yolo=True, flags=None, leadbook=None, backend=None) -> dict`
234
307
 
235
308
  Runs a long-lived agent session and periodically checks in with the current
236
309
  local time and a reminder of `prompt`. Each check-in expects JSON with keys:
@@ -241,8 +314,9 @@ JSON is invalid, lead asks the agent once to retry. The loop stops when
241
314
  Lead also injects the leadbook content into each prompt. By default it uses
242
315
  `LEADBOOK.md` in the working directory. Pass `leadbook=False` to disable or a
243
316
  path string to override the location.
317
+ Set `backend="cursor"` (or `CODEXAPI_BACKEND=cursor`) to use Cursor.
244
318
 
245
- ### `task(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None) -> str`
319
+ ### `task(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None, backend=None) -> str`
246
320
 
247
321
  Runs a task with checker-driven retries and returns the success summary.
248
322
  Raises `TaskFailed` when the maximum iterations are reached.
@@ -251,16 +325,17 @@ Raises `TaskFailed` when the maximum iterations are reached.
251
325
  - `max_iterations` (int): maximum number of task iterations (0 means unlimited).
252
326
  - `progress` (bool): show a tqdm progress bar with a one-line status after each round.
253
327
  - `set_up`/`tear_down`/`on_success`/`on_failure` (str | None): optional hook prompts.
328
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
254
329
 
255
- ### `task_result(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None) -> TaskResult`
330
+ ### `task_result(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None, backend=None) -> TaskResult`
256
331
 
257
332
  Runs a task with checker-driven retries and returns a `TaskResult` without
258
333
  raising `TaskFailed`.
259
334
  Arguments mirror `task()` (including hooks).
260
335
 
261
- ### `Task(prompt, max_iterations=10, cwd=None, yolo=True, thread_id=None, flags=None)`
336
+ ### `Task(prompt, max_iterations=10, cwd=None, yolo=True, thread_id=None, flags=None, backend=None)`
262
337
 
263
- Runs a Codex task with checker-driven retries. Subclass it and implement
338
+ Runs an agent task with checker-driven retries. Subclass it and implement
264
339
  `check()` to return an error string when the task is incomplete, or return
265
340
  `None`/`""` when the task passes.
266
341
  If you do not override `check()`, the default verifier wrapper runs with the
@@ -281,7 +356,7 @@ Simple result object returned by `Task.__call__`.
281
356
  - `summary` (str): agent summary of what happened.
282
357
  - `iterations` (int): how many iterations were used.
283
358
  - `errors` (str | None): last checker error, if any.
284
- - `thread_id` (str | None): Codex thread id for the session.
359
+ - `thread_id` (str | None): thread/session id for the session.
285
360
 
286
361
  ### `TaskFailed`
287
362
 
@@ -291,16 +366,17 @@ Exception raised by `task()` when iterations are exhausted.
291
366
  - `iterations` (int | None): iterations made when the task failed.
292
367
  - `errors` (str | None): last checker error, if any.
293
368
 
294
- ### `foreach(list_file, task_file, n=None, cwd=None, yolo=True, flags=None) -> ForeachResult`
369
+ ### `foreach(list_file, task_file, n=None, cwd=None, yolo=True, flags=None, backend=None) -> ForeachResult`
295
370
 
296
371
  Runs a task file over a list of items, updating the list file in place.
297
372
 
298
373
  - `list_file` (str | PathLike): path to the list file to process.
299
374
  - `task_file` (str | PathLike): YAML task file (must include `prompt`).
300
375
  - `n` (int | None): limit parallelism to N (default: run all items in parallel).
301
- - `cwd` (str | PathLike | None): working directory for the Codex session.
302
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
303
- - `flags` (str | None): extra CLI flags to pass to Codex.
376
+ - `cwd` (str | PathLike | None): working directory for the agent session.
377
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
378
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
379
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
304
380
 
305
381
  ### `ForeachResult(succeeded, failed, skipped, results)`
306
382
 
@@ -313,16 +389,29 @@ Simple result object returned by `foreach()`.
313
389
 
314
390
  ## Behavior notes
315
391
 
316
- - Uses `codex exec --json` and parses JSONL events for `agent_message` items.
317
- - Returns the last `agent_message` by default; set `include_thinking=True` to join all messages.
318
- - Automatically passes `--skip-git-repo-check` so it can run outside a git repo.
319
- - Passes `--yolo` by default (use `--no-yolo` or `yolo=False` for `--full-auto`).
320
- - Raises `RuntimeError` if Codex exits non-zero or returns no agent message.
392
+ - Codex backend uses `codex exec --json` and parses JSONL `agent_message` items.
393
+ - Codex backend passes `--skip-git-repo-check` so it can run outside a git repo.
394
+ - Cursor backend uses `cursor agent --print --output-format json --trust` and parses the JSON result.
395
+ - `include_thinking=True` only affects Codex; Cursor returns a single result string.
396
+ - Passes `--yolo` by default (Codex uses `--full-auto` when disabled).
397
+ - Raises `RuntimeError` if the backend exits non-zero or returns no agent message.
321
398
 
322
399
  ## Configuration
323
400
 
401
+ Set the default backend:
402
+
403
+ ```bash
404
+ export CODEXAPI_BACKEND=cursor
405
+ ```
406
+
324
407
  Set `CODEX_BIN` to point at a non-default Codex binary:
325
408
 
326
409
  ```bash
327
410
  export CODEX_BIN=/path/to/codex
328
411
  ```
412
+
413
+ Set `CURSOR_BIN` to point at a non-default Cursor binary:
414
+
415
+ ```bash
416
+ export CURSOR_BIN=/path/to/cursor
417
+ ```
@@ -1,12 +1,13 @@
1
1
  # CodexAPI
2
2
 
3
- Use OpenAI's codex from python as easily as calling a function with your codex credits instead of the API.
3
+ Use Codex or Cursor agents from python as easily as calling a function, using your CLI auth instead of the API.
4
4
 
5
5
  *Note: this project is not affiliated with OpenAI in any way. Thanks for the awesome tools and models though!*
6
6
 
7
7
  ## Requirements
8
8
 
9
- - Codex CLI installed and authenticated (`codex` must be on your PATH).
9
+ - Codex CLI installed and authenticated (`codex` must be on your PATH), or
10
+ - Cursor Agent CLI installed and authenticated (`cursor` must be on your PATH).
10
11
  - Python 3.8+.
11
12
 
12
13
  ## Install
@@ -44,14 +45,19 @@ result = task()
44
45
  print(result.success, result.summary)
45
46
  ```
46
47
 
48
+ Use `backend="cursor"` (or set `CODEXAPI_BACKEND=cursor`) to switch to the
49
+ Cursor agent backend.
50
+
47
51
  ## CLI
48
52
 
49
53
  After installing, use the `codexapi` command:
50
54
 
51
55
  ```bash
56
+ codexapi --version
52
57
  codexapi run "Summarize this repo."
53
58
  codexapi run --cwd /path/to/project "Fix the failing tests."
54
59
  echo "Say hello." | codexapi run
60
+ codexapi run --backend cursor "Summarize this repo."
55
61
  ```
56
62
 
57
63
  `codexapi task` exits with code 0 on success and 1 on failure.
@@ -107,15 +113,16 @@ Show running sessions and their latest activity:
107
113
  codexapi top
108
114
  ```
109
115
  Press `h` for keys.
116
+ `codexapi top` and `codexapi limit` are Codex-only.
110
117
 
111
- Resume a session and print the thread id to stderr:
118
+ Resume a session and print the thread/session id to stderr:
112
119
 
113
120
  ```bash
114
121
  codexapi run --thread-id THREAD_ID --print-thread-id "Continue where we left off."
115
122
  ```
116
123
 
117
- Use `--no-yolo` to run Codex with `--full-auto` instead.
118
- Use `--include-thinking` to return all agent messages joined together for `codexapi run`.
124
+ Use `--no-yolo` to disable `--yolo` (Codex uses `--full-auto`).
125
+ Use `--include-thinking` to return all agent messages joined together for `codexapi run` (Codex only).
119
126
 
120
127
  Lead mode periodically checks in on a long-running agent session with the
121
128
  current time and prints JSON status updates. The agent controls the loop by
@@ -135,14 +142,77 @@ If the leadbook does not exist, lead creates it with a template.
135
142
 
136
143
  ```bash
137
144
  codexapi lead 5 "Run the benchmark and wait for results."
145
+ codexapi lead 0 "Do a rapid triage pass and report."
146
+ ```
147
+
148
+ ### Durable agents
149
+
150
+ `codexapi agent` is the durable long-running control plane. It stores state
151
+ under `CODEXAPI_HOME` (default `~/.codexapi`), wakes agents on cron, and lets
152
+ you inspect or message them from any shell that points at the same home.
138
153
 
139
- Run without waiting between check-ins:
154
+ Start by checking the effective host/home pair and installing the scheduler:
140
155
 
141
156
  ```bash
142
- codexapi lead 0 "Do a rapid triage pass and report."
157
+ codexapi agent whoami
158
+ codexapi agent install-cron
159
+ ```
160
+
161
+ Start a goal-directed agent that decides for itself when it is done:
162
+
163
+ ```bash
164
+ codexapi agent start --name ci-fixer \
165
+ "Watch CI, fix failing tests, open or update a PR, and stop when the work is done."
166
+ ```
167
+
168
+ Start a persistent watcher that keeps running until you stop it:
169
+
170
+ ```bash
171
+ codexapi agent start --name issue-watcher \
172
+ --stop-policy until_stopped \
173
+ --heartbeat-minutes 30 \
174
+ "Every wake, scan for newly assigned issues that look actionable and report or start follow-up work."
175
+ ```
176
+
177
+ Inspect and talk to agents:
178
+
179
+ ```bash
180
+ codexapi agent list
181
+ codexapi agent show ci-fixer
182
+ codexapi agent read ci-fixer
183
+ codexapi agent book ci-fixer
184
+ codexapi agent send ci-fixer "Prefer the smallest safe fix."
185
+ codexapi agent wake ci-fixer
186
+ codexapi agent pause ci-fixer
187
+ codexapi agent resume ci-fixer
188
+ codexapi agent cancel ci-fixer
189
+ codexapi agent delete ci-fixer
143
190
  ```
191
+
192
+ Create a child agent explicitly:
193
+
194
+ ```bash
195
+ codexapi agent start --name child-fix --parent ci-fixer \
196
+ "Investigate the flaky integration test and report back."
144
197
  ```
145
198
 
199
+ Useful environment overrides:
200
+
201
+ ```bash
202
+ CODEXAPI_HOME=/tmp/codexapi-test-home codexapi agent list
203
+ CODEXAPI_HOSTNAME=stable-host codexapi agent whoami
204
+ ```
205
+
206
+ `CODEXAPI_HOME` isolates independent agent installations and is the right seam
207
+ for tests. `CODEXAPI_HOSTNAME` is useful when cron, shells, sandboxes, or test
208
+ wrappers report inconsistent hostnames for the same machine.
209
+
210
+ `codexapi agent show` also prints the resolved `AGENTBOOK.md` path so you can
211
+ jump directly to the durable working memory file.
212
+
213
+ See [docs/agent-v1.md](docs/agent-v1.md) for the filesystem model and scheduling
214
+ details.
215
+
146
216
  Ralph loop mode repeats the same prompt until a completion promise or a max
147
217
  iteration cap is hit (0 means unlimited). Cancel by deleting
148
218
  `.codexapi/ralph-loop.local.md` or running `codexapi ralph --cancel`.
@@ -191,31 +261,34 @@ codexapi foreach list.txt task.yaml --retry-all
191
261
 
192
262
  ## API
193
263
 
194
- ### `agent(prompt, cwd=None, yolo=True, flags=None, include_thinking=False) -> str`
264
+ ### `agent(prompt, cwd=None, yolo=True, flags=None, include_thinking=False, backend=None) -> str`
195
265
 
196
- Runs a single Codex turn and returns only the agent's message. Any reasoning
266
+ Runs a single agent turn and returns only the agent's message. Any reasoning
197
267
  items are filtered out.
198
268
 
199
- - `prompt` (str): prompt to send to Codex.
200
- - `cwd` (str | PathLike | None): working directory for the Codex session.
201
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
202
- - `flags` (str | None): extra CLI flags to pass to Codex.
269
+ - `prompt` (str): prompt to send to the agent backend.
270
+ - `cwd` (str | PathLike | None): working directory for the agent session.
271
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
272
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
203
273
  - `include_thinking` (bool): when true, return all agent messages joined.
274
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
204
275
 
205
- ### `Agent(cwd=None, yolo=True, thread_id=None, flags=None, welfare=False, include_thinking=False)`
276
+ ### `Agent(cwd=None, yolo=True, thread_id=None, flags=None, welfare=False, include_thinking=False, backend=None)`
206
277
 
207
278
  Creates a stateful session wrapper. Calling the instance sends the prompt into
208
279
  the same conversation and returns only the agent's message.
209
280
 
210
- - `__call__(prompt) -> str`: send a prompt to Codex and return the message.
281
+ - `__call__(prompt) -> str`: send a prompt to the agent backend and return the message.
211
282
  - `thread_id -> str | None`: expose the underlying session id once created.
212
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
213
- - `flags` (str | None): extra CLI flags to pass to Codex.
283
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
284
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
214
285
  - `welfare` (bool): when true, append welfare stop instructions to each prompt
215
286
  and raise `WelfareStop` if the agent outputs `MAKE IT STOP`.
216
287
  - `include_thinking` (bool): when true, return all agent messages joined.
288
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
289
+ For Cursor, `thread_id` corresponds to the `session_id` returned by the agent.
217
290
 
218
- ### `lead(minutes, prompt, cwd=None, yolo=True, flags=None, leadbook=None) -> dict`
291
+ ### `lead(minutes, prompt, cwd=None, yolo=True, flags=None, leadbook=None, backend=None) -> dict`
219
292
 
220
293
  Runs a long-lived agent session and periodically checks in with the current
221
294
  local time and a reminder of `prompt`. Each check-in expects JSON with keys:
@@ -226,8 +299,9 @@ JSON is invalid, lead asks the agent once to retry. The loop stops when
226
299
  Lead also injects the leadbook content into each prompt. By default it uses
227
300
  `LEADBOOK.md` in the working directory. Pass `leadbook=False` to disable or a
228
301
  path string to override the location.
302
+ Set `backend="cursor"` (or `CODEXAPI_BACKEND=cursor`) to use Cursor.
229
303
 
230
- ### `task(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None) -> str`
304
+ ### `task(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None, backend=None) -> str`
231
305
 
232
306
  Runs a task with checker-driven retries and returns the success summary.
233
307
  Raises `TaskFailed` when the maximum iterations are reached.
@@ -236,16 +310,17 @@ Raises `TaskFailed` when the maximum iterations are reached.
236
310
  - `max_iterations` (int): maximum number of task iterations (0 means unlimited).
237
311
  - `progress` (bool): show a tqdm progress bar with a one-line status after each round.
238
312
  - `set_up`/`tear_down`/`on_success`/`on_failure` (str | None): optional hook prompts.
313
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
239
314
 
240
- ### `task_result(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None) -> TaskResult`
315
+ ### `task_result(prompt, check=None, max_iterations=10, cwd=None, yolo=True, flags=None, progress=False, set_up=None, tear_down=None, on_success=None, on_failure=None, backend=None) -> TaskResult`
241
316
 
242
317
  Runs a task with checker-driven retries and returns a `TaskResult` without
243
318
  raising `TaskFailed`.
244
319
  Arguments mirror `task()` (including hooks).
245
320
 
246
- ### `Task(prompt, max_iterations=10, cwd=None, yolo=True, thread_id=None, flags=None)`
321
+ ### `Task(prompt, max_iterations=10, cwd=None, yolo=True, thread_id=None, flags=None, backend=None)`
247
322
 
248
- Runs a Codex task with checker-driven retries. Subclass it and implement
323
+ Runs an agent task with checker-driven retries. Subclass it and implement
249
324
  `check()` to return an error string when the task is incomplete, or return
250
325
  `None`/`""` when the task passes.
251
326
  If you do not override `check()`, the default verifier wrapper runs with the
@@ -266,7 +341,7 @@ Simple result object returned by `Task.__call__`.
266
341
  - `summary` (str): agent summary of what happened.
267
342
  - `iterations` (int): how many iterations were used.
268
343
  - `errors` (str | None): last checker error, if any.
269
- - `thread_id` (str | None): Codex thread id for the session.
344
+ - `thread_id` (str | None): thread/session id for the session.
270
345
 
271
346
  ### `TaskFailed`
272
347
 
@@ -276,16 +351,17 @@ Exception raised by `task()` when iterations are exhausted.
276
351
  - `iterations` (int | None): iterations made when the task failed.
277
352
  - `errors` (str | None): last checker error, if any.
278
353
 
279
- ### `foreach(list_file, task_file, n=None, cwd=None, yolo=True, flags=None) -> ForeachResult`
354
+ ### `foreach(list_file, task_file, n=None, cwd=None, yolo=True, flags=None, backend=None) -> ForeachResult`
280
355
 
281
356
  Runs a task file over a list of items, updating the list file in place.
282
357
 
283
358
  - `list_file` (str | PathLike): path to the list file to process.
284
359
  - `task_file` (str | PathLike): YAML task file (must include `prompt`).
285
360
  - `n` (int | None): limit parallelism to N (default: run all items in parallel).
286
- - `cwd` (str | PathLike | None): working directory for the Codex session.
287
- - `yolo` (bool): pass `--yolo` to Codex when true (defaults to true).
288
- - `flags` (str | None): extra CLI flags to pass to Codex.
361
+ - `cwd` (str | PathLike | None): working directory for the agent session.
362
+ - `yolo` (bool): pass `--yolo` when true (defaults to true).
363
+ - `flags` (str | None): extra CLI flags to pass to the agent backend.
364
+ - `backend` (str | None): `codex` or `cursor` (defaults to `CODEXAPI_BACKEND` or `codex`).
289
365
 
290
366
  ### `ForeachResult(succeeded, failed, skipped, results)`
291
367
 
@@ -298,16 +374,29 @@ Simple result object returned by `foreach()`.
298
374
 
299
375
  ## Behavior notes
300
376
 
301
- - Uses `codex exec --json` and parses JSONL events for `agent_message` items.
302
- - Returns the last `agent_message` by default; set `include_thinking=True` to join all messages.
303
- - Automatically passes `--skip-git-repo-check` so it can run outside a git repo.
304
- - Passes `--yolo` by default (use `--no-yolo` or `yolo=False` for `--full-auto`).
305
- - Raises `RuntimeError` if Codex exits non-zero or returns no agent message.
377
+ - Codex backend uses `codex exec --json` and parses JSONL `agent_message` items.
378
+ - Codex backend passes `--skip-git-repo-check` so it can run outside a git repo.
379
+ - Cursor backend uses `cursor agent --print --output-format json --trust` and parses the JSON result.
380
+ - `include_thinking=True` only affects Codex; Cursor returns a single result string.
381
+ - Passes `--yolo` by default (Codex uses `--full-auto` when disabled).
382
+ - Raises `RuntimeError` if the backend exits non-zero or returns no agent message.
306
383
 
307
384
  ## Configuration
308
385
 
386
+ Set the default backend:
387
+
388
+ ```bash
389
+ export CODEXAPI_BACKEND=cursor
390
+ ```
391
+
309
392
  Set `CODEX_BIN` to point at a non-default Codex binary:
310
393
 
311
394
  ```bash
312
395
  export CODEX_BIN=/path/to/codex
313
396
  ```
397
+
398
+ Set `CURSOR_BIN` to point at a non-default Cursor binary:
399
+
400
+ ```bash
401
+ export CURSOR_BIN=/path/to/cursor
402
+ ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "codexapi"
7
- version = "0.7.2"
7
+ version = "0.9.0"
8
8
  description = "Minimal Python API for running the Codex CLI."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -1,4 +1,4 @@
1
- """Minimal Python API for running the Codex CLI."""
1
+ """Minimal Python API for running agent CLIs."""
2
2
 
3
3
  from .agent import Agent, WelfareStop, agent
4
4
  from .foreach import ForeachResult, foreach
@@ -27,4 +27,4 @@ __all__ = [
27
27
  "task_result",
28
28
  "lead",
29
29
  ]
30
- __version__ = "0.7.2"
30
+ __version__ = "0.9.0"