python-codex 0.4.8__tar.gz → 0.5.1__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 (94) hide show
  1. python_codex-0.5.1/PKG-INFO +235 -0
  2. python_codex-0.5.1/README.md +215 -0
  3. python_codex-0.5.1/README_ZH.md +168 -0
  4. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/agent.py +2 -1
  5. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/events.py +17 -1
  6. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/model.py +187 -21
  7. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/runtime.py +18 -5
  8. {python_codex-0.4.8 → python_codex-0.5.1}/pyproject.toml +2 -2
  9. python_codex-0.4.8/PKG-INFO +0 -916
  10. python_codex-0.4.8/README.md +0 -896
  11. python_codex-0.4.8/README_ZH.md +0 -526
  12. {python_codex-0.4.8 → python_codex-0.5.1}/.gitignore +0 -0
  13. {python_codex-0.4.8 → python_codex-0.5.1}/LICENSE +0 -0
  14. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/__init__.py +0 -0
  15. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/__main__.py +0 -0
  16. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/bootstrap.py +0 -0
  17. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/cli.py +0 -0
  18. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/context.py +0 -0
  19. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/portable/__init__.py +0 -0
  20. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/portable/client.py +0 -0
  21. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/portable/server.py +0 -0
  22. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/default_base_instructions.md +0 -0
  23. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/models.json +0 -0
  24. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/approval_policy/never.md +0 -0
  25. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/approval_policy/on_failure.md +0 -0
  26. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/approval_policy/on_request.md +0 -0
  27. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/approval_policy/on_request_rule_request_permission.md +0 -0
  28. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/approval_policy/unless_trusted.md +0 -0
  29. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/sandbox_mode/danger_full_access.md +0 -0
  30. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/sandbox_mode/read_only.md +0 -0
  31. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/prompts/permissions/sandbox_mode/workspace_write.md +0 -0
  32. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/protocol.py +0 -0
  33. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/__init__.py +0 -0
  34. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/agent_tool_schemas.py +0 -0
  35. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/apply_patch_tool.py +0 -0
  36. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/base_tool.py +0 -0
  37. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/clock_tool.py +0 -0
  38. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/close_agent_tool.py +0 -0
  39. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/code_mode_manager.py +0 -0
  40. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/exec_command_tool.py +0 -0
  41. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/exec_runtime.js +0 -0
  42. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/exec_tool.py +0 -0
  43. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/grep_files_tool.py +0 -0
  44. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/list_dir_tool.py +0 -0
  45. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/read_file_tool.py +0 -0
  46. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/request_permissions_tool.py +0 -0
  47. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/request_user_input_tool.py +0 -0
  48. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/resume_agent_tool.py +0 -0
  49. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/runtime_services.py +0 -0
  50. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/send_input_tool.py +0 -0
  51. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/shell_command_tool.py +0 -0
  52. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/shell_tool.py +0 -0
  53. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/spawn_agent_tool.py +0 -0
  54. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/unified_exec_manager.py +0 -0
  55. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/update_plan_tool.py +0 -0
  56. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/view_image_tool.py +0 -0
  57. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/wait_agent_tool.py +0 -0
  58. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/wait_tool.py +0 -0
  59. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/web_search_tool.py +0 -0
  60. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/tools/write_stdin_tool.py +0 -0
  61. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/__init__.py +0 -0
  62. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/LICENSE +0 -0
  63. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/README.md +0 -0
  64. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/__init__.py +0 -0
  65. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/aes.py +0 -0
  66. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/gcm.py +0 -0
  67. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/_aes_gcm/source-sha256.json +0 -0
  68. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/async_bridge.py +0 -0
  69. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/compactor.py +0 -0
  70. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/compat.py +0 -0
  71. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/crypto.py +0 -0
  72. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/debug.py +0 -0
  73. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/doctor.py +0 -0
  74. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/dotenv.py +0 -0
  75. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/event_helpers.py +0 -0
  76. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/get_env.py +0 -0
  77. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/image_utils.py +0 -0
  78. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/model_metadata.py +0 -0
  79. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/random_ids.py +0 -0
  80. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/session_persist.py +0 -0
  81. {python_codex-0.4.8 → python_codex-0.5.1}/pycodex/utils/truncation.py +0 -0
  82. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/__init__.py +0 -0
  83. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/__main__.py +0 -0
  84. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/app.py +0 -0
  85. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/config.py +0 -0
  86. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/messages_api.py +0 -0
  87. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/payload_processors.py +0 -0
  88. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/server.py +0 -0
  89. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/session_store.py +0 -0
  90. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/stream_router.py +0 -0
  91. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/tools/__init__.py +0 -0
  92. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/tools/custom_adapter.py +0 -0
  93. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/tools/web_search.py +0 -0
  94. {python_codex-0.4.8 → python_codex-0.5.1}/responses_server/trajectory_dump.py +0 -0
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-codex
3
+ Version: 0.5.1
4
+ Summary: A minimal Python extraction of Codex's main agent loop
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.6.2
7
+ Requires-Dist: dataclasses>=0.8; python_version < '3.7'
8
+ Requires-Dist: importlib-metadata>=4.8.3; python_version < '3.8'
9
+ Requires-Dist: loguru>=0.7.3
10
+ Requires-Dist: prompt-toolkit>=3.0.36
11
+ Requires-Dist: pyyaml>=6.0
12
+ Requires-Dist: requests>=2.27.1
13
+ Requires-Dist: starlette<0.20,>=0.19.1; python_version < '3.7'
14
+ Requires-Dist: starlette>=0.19.1; python_version >= '3.7'
15
+ Requires-Dist: tomli<2,>=1.2.3; python_version < '3.11'
16
+ Requires-Dist: typing-extensions>=4.1.1; python_version < '3.8'
17
+ Requires-Dist: uvicorn<0.17,>=0.16.0; python_version < '3.7'
18
+ Requires-Dist: uvicorn>=0.16.0; python_version >= '3.7'
19
+ Description-Content-Type: text/markdown
20
+
21
+ # pycodex
22
+
23
+ [中文](README_ZH.md)
24
+
25
+ pycodex brings the [Codex](https://github.com/openai/codex) agent loop to Python,
26
+ with a CLI, reusable Agent API and browser workspace. It aims to follow upstream
27
+ behavior; supported features and deliberate differences are documented in
28
+ [Alignment](docs/ALIGNMENT.md).
29
+
30
+ | Package | Provides | Python |
31
+ | --- | --- | --- |
32
+ | `python-codex` | Agent, runtime, CLI, tools, Responses compatibility server and portable storage | 3.6.2+ |
33
+ | `pycodex-ws` | Browser workspace, Canvas, file/diff viewers and optional Feishu integration | 3.7+ |
34
+
35
+ The workspace package installs the matching core version. Python imports use
36
+ `pycodex`; the commands are `pycodex` and `pycodex-ws`.
37
+
38
+ ## Install and run
39
+
40
+ Install the core CLI:
41
+
42
+ ```bash
43
+ python3 -m pip install python-codex
44
+ pycodex
45
+ ```
46
+
47
+ Or install the browser workspace, which includes core:
48
+
49
+ ```bash
50
+ python3 -m pip install pycodex-ws
51
+ pycodex-ws --help
52
+ ```
53
+
54
+ Use `python3 -m pip install --upgrade pycodex-ws` to upgrade both packages
55
+ together. If a command is missing from `PATH`, use `python3 -m pycodex` or
56
+ `python3 -m workspace_server` with the interpreter used for installation.
57
+
58
+ ## Model configuration
59
+
60
+ pycodex reads `~/.codex/config.toml` and the adjacent `.env`. Configure `model`,
61
+ `model_provider`, and the provider's `base_url` and `env_key` as needed.
62
+ The built-in `openai` provider uses `OPENAI_API_KEY`. Authentication uses API
63
+ keys; ChatGPT sign-in credentials from `auth.json` or the OS keychain are not
64
+ loaded.
65
+
66
+ ```bash
67
+ pycodex --profile my_profile "Summarize this repository."
68
+ pycodex --config /path/to/config.toml
69
+ pycodex doctor
70
+ ```
71
+
72
+ Models use the streaming Responses API. For a Chat Completions backend, set
73
+ `use_chat_completion = true` in its provider table. Other backend options include:
74
+
75
+ ```bash
76
+ pycodex --vllm-endpoint http://127.0.0.1:18000
77
+ pycodex --profile my_profile --use-messages
78
+ ```
79
+
80
+ See [context and configuration](docs/CONTEXT.md) and the
81
+ [Responses compatibility server](docs/responses_server/README.md).
82
+
83
+ ## CLI
84
+
85
+ Run `pycodex` without a prompt for an interactive session, or supply a prompt
86
+ for a single turn:
87
+
88
+ ```bash
89
+ pycodex "Summarize this repository in one sentence."
90
+ printf 'Reply with exactly OK.' | pycodex
91
+ pycodex --json "Reply with exactly OK."
92
+ ```
93
+
94
+ | Command | Purpose |
95
+ | --- | --- |
96
+ | `/model`, `/effort` | Show or change the model and reasoning effort |
97
+ | `/cd <directory>` | Change the session's working directory while idle |
98
+ | `/title`, `/history` | Manage the title and inspect conversation history |
99
+ | `/resume` | List saved sessions; pass an index or session UUID to resume |
100
+ | `/compact` | Summarize history to free context |
101
+ | `/fork` | Continue the current history under a new session ID |
102
+ | `/queue <message>` | Wait for the active turn to finish before submitting |
103
+
104
+ Ordinary input during a turn steers at the next safe sampling boundary.
105
+ Slash input uses Codex's command rules: `/data/project` and `/tmp/` are
106
+ ordinary messages, while an unknown single-component name such as `/modle`
107
+ or `/tmp` reports a command error. Start the input with a space to send any
108
+ slash-prefixed text literally, including known commands such as ` /model`.
109
+ These rules also apply to the browser workspace and Feishu.
110
+ Sessions are saved under `CODEX_HOME/sessions`. Set
111
+ `model_auto_compact_token_limit` in the configuration to compact automatically.
112
+
113
+ Ctrl+D or Ctrl+C closes the CLI after accepted work finishes. A second Ctrl+C
114
+ during closing exits immediately. Use `pycodex --help` for startup options.
115
+
116
+ ## Browser workspace
117
+
118
+ Create `workspaces.json`:
119
+
120
+ ```json
121
+ {
122
+ "workspaces": [
123
+ {"id": "demo", "work_dir": ".", "state_path": "demo", "enable_board": true}
124
+ ]
125
+ }
126
+ ```
127
+
128
+ ```bash
129
+ pycodex-ws --listen 127.0.0.1:6007 --workspace-config workspaces.json
130
+ ```
131
+
132
+ Open `http://127.0.0.1:6007`. The workspace combines conversations with a live
133
+ HTML Canvas and read-only file/diff viewers. Click file links in replies to open
134
+ them, and use **Insert into chat** to add selected code to an editable draft.
135
+ Keep the server's stdin open: EOF initiates shutdown.
136
+ The gear button starts an Agent conversation for customizing the interface
137
+ through files in `~/.pycodex/frontend/`.
138
+
139
+ See the [workspace guide](packages/pycodex-ws/README.md) for session controls,
140
+ storage and Feishu setup. The separately distributed
141
+ [VS Code extension](packages/pycodex-ws/extensions/vscode/README.md) uses the same
142
+ workspace backend, including on Remote SSH hosts.
143
+
144
+ ## Python API
145
+
146
+ Use the standard model and tool setup from Python:
147
+
148
+ ```python
149
+ from pycodex.bootstrap import build_agent, build_model
150
+
151
+ agent = build_agent(build_model())
152
+ try:
153
+ result = agent.ask("Summarize this repository in one sentence.")
154
+ print(result.output_text)
155
+ finally:
156
+ agent.shutdown()
157
+ ```
158
+
159
+ For custom integrations, `Agent` accepts a model client, `ToolRegistry` and
160
+ `ContextConfig`. `await agent.run_turn([...])` runs an asynchronous turn;
161
+ `AgentRuntime` adds input queues, commands and frontend subscriptions.
162
+ See [runtime contracts and examples](docs/RUNTIME.md).
163
+
164
+ ## Portable configuration
165
+
166
+ Portable mode transfers an encrypted bundle of your configuration, instructions
167
+ and skills to another machine. Start a storage service in a separate terminal:
168
+
169
+ ```bash
170
+ python3 -m pycodex.portable.server --root .tmp/pycodex_storage --host 127.0.0.1 --port 5577
171
+ ```
172
+
173
+ Upload the default Codex home, or specify another directory:
174
+
175
+ ```bash
176
+ pycodex --put @127.0.0.1:5577
177
+ pycodex --put /path/to/.codex/@127.0.0.1:5577
178
+ ```
179
+
180
+ After verifying a download round trip, the command prints a reusable
181
+ `pycodex --call ...` command. Run it on the target machine using a storage
182
+ address reachable from that machine.
183
+
184
+ ## Development and reference
185
+
186
+ ```bash
187
+ uv sync --dev
188
+ uv run pycodex
189
+ uv run --dev pytest
190
+ ```
191
+
192
+ - [Test environments and coverage](tests/TESTS.md)
193
+ - [Architecture, package layout and runtime contracts](docs/RUNTIME.md)
194
+ - [Prompts, configuration and context](docs/CONTEXT.md)
195
+ - [Upstream alignment](docs/ALIGNMENT.md)
196
+ - [Responses compatibility server](docs/responses_server/README.md)
197
+
198
+ Release notes and implementation details live in `docs/`.
199
+
200
+ ## Markdown view modes
201
+
202
+ Markdown file views include a Show source / Show preview button. Switching
203
+ modes preserves each mode's reading position without replaying a previous line
204
+ target.
205
+
206
+ ## Responses reliability
207
+
208
+ Providers can opt into ordered failover with `fallback_models = ["backup-one",
209
+ "backup-two"]` in their `[model_providers.<name>]` table. Each target uses the
210
+ same endpoint, credentials, history and tools; request settings are rebuilt for
211
+ that model, while the session's selected model stays unchanged. Transient errors
212
+ exhaust `stream_max_retries` before advancing. Unavailable-model errors and
213
+ first-data timeouts advance immediately. Existing retry notices name both models
214
+ and include the underlying error.
215
+
216
+ Failed models cool down for 900 seconds within that client. Subsequent requests
217
+ skip them, retry them after expiry, and restart the full chain when every target
218
+ is cooling. `fallback_cooldown_seconds = 0` disables cooldown; explicit model
219
+ selection clears it. New sessions and subagents have independent cooldowns.
220
+ An absent or empty list disables failover. Invalid input, context-length and
221
+ terminal incomplete errors retain their existing handling. Backups must support
222
+ the active tools and context size; compaction keeps the selected model's policy.
223
+
224
+ Responses HTTP 408/429 and server errors use the provider retry budget.
225
+ Rate-limit messages containing `try again in` or `retry after` supply the retry
226
+ delay. CLI and workspace retry notices include the underlying error details;
227
+ workspace notices survive browser refresh and active-turn reattachment, and
228
+ remain separate from the assistant reply. They are not persisted in rollouts.
229
+
230
+ ## Workspace input history
231
+
232
+ The workspace message box supports Up/Down input history. Up recalls earlier
233
+ human inputs in the current conversation; Down returns to the unsent draft.
234
+ Automatic wake-up messages are excluded, and arrows retain normal cursor
235
+ movement inside multiline drafts.
@@ -0,0 +1,215 @@
1
+ # pycodex
2
+
3
+ [中文](README_ZH.md)
4
+
5
+ pycodex brings the [Codex](https://github.com/openai/codex) agent loop to Python,
6
+ with a CLI, reusable Agent API and browser workspace. It aims to follow upstream
7
+ behavior; supported features and deliberate differences are documented in
8
+ [Alignment](docs/ALIGNMENT.md).
9
+
10
+ | Package | Provides | Python |
11
+ | --- | --- | --- |
12
+ | `python-codex` | Agent, runtime, CLI, tools, Responses compatibility server and portable storage | 3.6.2+ |
13
+ | `pycodex-ws` | Browser workspace, Canvas, file/diff viewers and optional Feishu integration | 3.7+ |
14
+
15
+ The workspace package installs the matching core version. Python imports use
16
+ `pycodex`; the commands are `pycodex` and `pycodex-ws`.
17
+
18
+ ## Install and run
19
+
20
+ Install the core CLI:
21
+
22
+ ```bash
23
+ python3 -m pip install python-codex
24
+ pycodex
25
+ ```
26
+
27
+ Or install the browser workspace, which includes core:
28
+
29
+ ```bash
30
+ python3 -m pip install pycodex-ws
31
+ pycodex-ws --help
32
+ ```
33
+
34
+ Use `python3 -m pip install --upgrade pycodex-ws` to upgrade both packages
35
+ together. If a command is missing from `PATH`, use `python3 -m pycodex` or
36
+ `python3 -m workspace_server` with the interpreter used for installation.
37
+
38
+ ## Model configuration
39
+
40
+ pycodex reads `~/.codex/config.toml` and the adjacent `.env`. Configure `model`,
41
+ `model_provider`, and the provider's `base_url` and `env_key` as needed.
42
+ The built-in `openai` provider uses `OPENAI_API_KEY`. Authentication uses API
43
+ keys; ChatGPT sign-in credentials from `auth.json` or the OS keychain are not
44
+ loaded.
45
+
46
+ ```bash
47
+ pycodex --profile my_profile "Summarize this repository."
48
+ pycodex --config /path/to/config.toml
49
+ pycodex doctor
50
+ ```
51
+
52
+ Models use the streaming Responses API. For a Chat Completions backend, set
53
+ `use_chat_completion = true` in its provider table. Other backend options include:
54
+
55
+ ```bash
56
+ pycodex --vllm-endpoint http://127.0.0.1:18000
57
+ pycodex --profile my_profile --use-messages
58
+ ```
59
+
60
+ See [context and configuration](docs/CONTEXT.md) and the
61
+ [Responses compatibility server](docs/responses_server/README.md).
62
+
63
+ ## CLI
64
+
65
+ Run `pycodex` without a prompt for an interactive session, or supply a prompt
66
+ for a single turn:
67
+
68
+ ```bash
69
+ pycodex "Summarize this repository in one sentence."
70
+ printf 'Reply with exactly OK.' | pycodex
71
+ pycodex --json "Reply with exactly OK."
72
+ ```
73
+
74
+ | Command | Purpose |
75
+ | --- | --- |
76
+ | `/model`, `/effort` | Show or change the model and reasoning effort |
77
+ | `/cd <directory>` | Change the session's working directory while idle |
78
+ | `/title`, `/history` | Manage the title and inspect conversation history |
79
+ | `/resume` | List saved sessions; pass an index or session UUID to resume |
80
+ | `/compact` | Summarize history to free context |
81
+ | `/fork` | Continue the current history under a new session ID |
82
+ | `/queue <message>` | Wait for the active turn to finish before submitting |
83
+
84
+ Ordinary input during a turn steers at the next safe sampling boundary.
85
+ Slash input uses Codex's command rules: `/data/project` and `/tmp/` are
86
+ ordinary messages, while an unknown single-component name such as `/modle`
87
+ or `/tmp` reports a command error. Start the input with a space to send any
88
+ slash-prefixed text literally, including known commands such as ` /model`.
89
+ These rules also apply to the browser workspace and Feishu.
90
+ Sessions are saved under `CODEX_HOME/sessions`. Set
91
+ `model_auto_compact_token_limit` in the configuration to compact automatically.
92
+
93
+ Ctrl+D or Ctrl+C closes the CLI after accepted work finishes. A second Ctrl+C
94
+ during closing exits immediately. Use `pycodex --help` for startup options.
95
+
96
+ ## Browser workspace
97
+
98
+ Create `workspaces.json`:
99
+
100
+ ```json
101
+ {
102
+ "workspaces": [
103
+ {"id": "demo", "work_dir": ".", "state_path": "demo", "enable_board": true}
104
+ ]
105
+ }
106
+ ```
107
+
108
+ ```bash
109
+ pycodex-ws --listen 127.0.0.1:6007 --workspace-config workspaces.json
110
+ ```
111
+
112
+ Open `http://127.0.0.1:6007`. The workspace combines conversations with a live
113
+ HTML Canvas and read-only file/diff viewers. Click file links in replies to open
114
+ them, and use **Insert into chat** to add selected code to an editable draft.
115
+ Keep the server's stdin open: EOF initiates shutdown.
116
+ The gear button starts an Agent conversation for customizing the interface
117
+ through files in `~/.pycodex/frontend/`.
118
+
119
+ See the [workspace guide](packages/pycodex-ws/README.md) for session controls,
120
+ storage and Feishu setup. The separately distributed
121
+ [VS Code extension](packages/pycodex-ws/extensions/vscode/README.md) uses the same
122
+ workspace backend, including on Remote SSH hosts.
123
+
124
+ ## Python API
125
+
126
+ Use the standard model and tool setup from Python:
127
+
128
+ ```python
129
+ from pycodex.bootstrap import build_agent, build_model
130
+
131
+ agent = build_agent(build_model())
132
+ try:
133
+ result = agent.ask("Summarize this repository in one sentence.")
134
+ print(result.output_text)
135
+ finally:
136
+ agent.shutdown()
137
+ ```
138
+
139
+ For custom integrations, `Agent` accepts a model client, `ToolRegistry` and
140
+ `ContextConfig`. `await agent.run_turn([...])` runs an asynchronous turn;
141
+ `AgentRuntime` adds input queues, commands and frontend subscriptions.
142
+ See [runtime contracts and examples](docs/RUNTIME.md).
143
+
144
+ ## Portable configuration
145
+
146
+ Portable mode transfers an encrypted bundle of your configuration, instructions
147
+ and skills to another machine. Start a storage service in a separate terminal:
148
+
149
+ ```bash
150
+ python3 -m pycodex.portable.server --root .tmp/pycodex_storage --host 127.0.0.1 --port 5577
151
+ ```
152
+
153
+ Upload the default Codex home, or specify another directory:
154
+
155
+ ```bash
156
+ pycodex --put @127.0.0.1:5577
157
+ pycodex --put /path/to/.codex/@127.0.0.1:5577
158
+ ```
159
+
160
+ After verifying a download round trip, the command prints a reusable
161
+ `pycodex --call ...` command. Run it on the target machine using a storage
162
+ address reachable from that machine.
163
+
164
+ ## Development and reference
165
+
166
+ ```bash
167
+ uv sync --dev
168
+ uv run pycodex
169
+ uv run --dev pytest
170
+ ```
171
+
172
+ - [Test environments and coverage](tests/TESTS.md)
173
+ - [Architecture, package layout and runtime contracts](docs/RUNTIME.md)
174
+ - [Prompts, configuration and context](docs/CONTEXT.md)
175
+ - [Upstream alignment](docs/ALIGNMENT.md)
176
+ - [Responses compatibility server](docs/responses_server/README.md)
177
+
178
+ Release notes and implementation details live in `docs/`.
179
+
180
+ ## Markdown view modes
181
+
182
+ Markdown file views include a Show source / Show preview button. Switching
183
+ modes preserves each mode's reading position without replaying a previous line
184
+ target.
185
+
186
+ ## Responses reliability
187
+
188
+ Providers can opt into ordered failover with `fallback_models = ["backup-one",
189
+ "backup-two"]` in their `[model_providers.<name>]` table. Each target uses the
190
+ same endpoint, credentials, history and tools; request settings are rebuilt for
191
+ that model, while the session's selected model stays unchanged. Transient errors
192
+ exhaust `stream_max_retries` before advancing. Unavailable-model errors and
193
+ first-data timeouts advance immediately. Existing retry notices name both models
194
+ and include the underlying error.
195
+
196
+ Failed models cool down for 900 seconds within that client. Subsequent requests
197
+ skip them, retry them after expiry, and restart the full chain when every target
198
+ is cooling. `fallback_cooldown_seconds = 0` disables cooldown; explicit model
199
+ selection clears it. New sessions and subagents have independent cooldowns.
200
+ An absent or empty list disables failover. Invalid input, context-length and
201
+ terminal incomplete errors retain their existing handling. Backups must support
202
+ the active tools and context size; compaction keeps the selected model's policy.
203
+
204
+ Responses HTTP 408/429 and server errors use the provider retry budget.
205
+ Rate-limit messages containing `try again in` or `retry after` supply the retry
206
+ delay. CLI and workspace retry notices include the underlying error details;
207
+ workspace notices survive browser refresh and active-turn reattachment, and
208
+ remain separate from the assistant reply. They are not persisted in rollouts.
209
+
210
+ ## Workspace input history
211
+
212
+ The workspace message box supports Up/Down input history. Up recalls earlier
213
+ human inputs in the current conversation; Down returns to the unsent draft.
214
+ Automatic wake-up messages are excluded, and arrows retain normal cursor
215
+ movement inside multiline drafts.
@@ -0,0 +1,168 @@
1
+ # pycodex
2
+
3
+ [English](README.md)
4
+
5
+ pycodex 用 Python 实现 [Codex](https://github.com/openai/codex) 的 Agent 主循环,
6
+ 提供 CLI、可复用的 Agent API 和浏览器工作区。项目以对齐上游行为为目标;
7
+ 支持范围和刻意保留的差异见[对齐文档](docs/ALIGNMENT.md)。
8
+
9
+ | 包 | 内容 | Python |
10
+ | --- | --- | --- |
11
+ | `python-codex` | Agent、Runtime、CLI、工具、Responses 兼容服务和 portable 存储 | 3.6.2+ |
12
+ | `pycodex-ws` | 浏览器工作区、Canvas、文件/diff 查看器和可选飞书集成 | 3.7+ |
13
+
14
+ workspace 包会安装匹配版本的 core。Python 导入路径为 `pycodex`,
15
+ 命令为 `pycodex` 和 `pycodex-ws`。
16
+
17
+ ## 安装与启动
18
+
19
+ 安装核心 CLI:
20
+
21
+ ```bash
22
+ python3 -m pip install python-codex
23
+ pycodex
24
+ ```
25
+
26
+ 也可以安装包含 core 的浏览器工作区:
27
+
28
+ ```bash
29
+ python3 -m pip install pycodex-ws
30
+ pycodex-ws --help
31
+ ```
32
+
33
+ 使用 `python3 -m pip install --upgrade pycodex-ws` 一起升级两个包。
34
+ 如果命令不在 `PATH` 中,用安装时的解释器运行 `python3 -m pycodex`
35
+ 或 `python3 -m workspace_server`。
36
+
37
+ ## 模型配置
38
+
39
+ 默认读取 `~/.codex/config.toml` 和同目录的 `.env`。根据需要配置 `model`、
40
+ `model_provider` 及 provider 的 `base_url`、`env_key`。
41
+ 内置 `openai` provider 使用 `OPENAI_API_KEY`。认证使用 API key,
42
+ 不读取 `auth.json` 或系统钥匙串中的 ChatGPT 登录凭据。
43
+
44
+ ```bash
45
+ pycodex --profile my_profile "概括这个仓库。"
46
+ pycodex --config /path/to/config.toml
47
+ pycodex doctor
48
+ ```
49
+
50
+ 默认使用流式 Responses API。接入 Chat Completions 后端时,在对应 provider
51
+ 配置中设置 `use_chat_completion = true`。其他后端入口包括:
52
+
53
+ ```bash
54
+ pycodex --vllm-endpoint http://127.0.0.1:18000
55
+ pycodex --profile my_profile --use-messages
56
+ ```
57
+
58
+ 详见[配置与上下文](docs/CONTEXT.md)和
59
+ [Responses 兼容服务](docs/responses_server/README.md)。
60
+
61
+ ## CLI
62
+
63
+ 不带 prompt 时进入交互会话,也可以直接运行单轮任务:
64
+
65
+ ```bash
66
+ pycodex "用一句话概括这个仓库。"
67
+ printf 'Reply with exactly OK.' | pycodex
68
+ pycodex --json "Reply with exactly OK."
69
+ ```
70
+
71
+ | 命令 | 用途 |
72
+ | --- | --- |
73
+ | `/model`、`/effort` | 查看或切换模型及推理强度 |
74
+ | `/cd <directory>` | 空闲时切换当前会话的工作目录 |
75
+ | `/title`、`/history` | 管理标题、查看历史 |
76
+ | `/resume` | 列出已保存会话;传入序号或 UUID 恢复 |
77
+ | `/compact` | 压缩历史,释放上下文空间 |
78
+ | `/fork` | 保留当前历史,以新会话 ID 继续 |
79
+ | `/queue <message>` | 等当前 turn 完成后再执行 |
80
+
81
+ 运行中提交普通输入,会在下一个安全采样边界插入。
82
+ 斜杠输入遵循 Codex 的命令规则:`/data/project` 和 `/tmp/` 是普通消息;
83
+ `/modle` 或 `/tmp` 这样的未知单层名称会提示命令错误。
84
+ 在输入开头加一个空格即可按文本发送,包括已知命令,例如 ` /model`。
85
+ 这些规则同样适用于浏览器工作区和飞书。会话保存在
86
+ `CODEX_HOME/sessions`;配置 `model_auto_compact_token_limit` 可启用自动压缩。
87
+
88
+ Ctrl+D 或 Ctrl+C 发起退出,等待已接收任务完成;关闭等待期间再次 Ctrl+C
89
+ 立即退出。完整启动参数见 `pycodex --help`。
90
+
91
+ ## 浏览器工作区
92
+
93
+ 创建 `workspaces.json`:
94
+
95
+ ```json
96
+ {
97
+ "workspaces": [
98
+ {"id": "demo", "work_dir": ".", "state_path": "demo", "enable_board": true}
99
+ ]
100
+ }
101
+ ```
102
+
103
+ ```bash
104
+ pycodex-ws --listen 127.0.0.1:6007 --workspace-config workspaces.json
105
+ ```
106
+
107
+ 访问 `http://127.0.0.1:6007`。工作区同时提供对话、实时 HTML Canvas 和只读
108
+ 文件/diff 查看器。点击回复中的文件链接打开视图,通过 **Insert into chat**
109
+ 把选中的代码插入可编辑草稿。运行期间保持服务器 stdin 打开,EOF 会触发关闭。
110
+ 点击齿轮按钮可开启界面定制会话,让 Agent 修改 `~/.pycodex/frontend/` 中的文件。
111
+
112
+ 会话操作、存储和飞书配置见[工作区说明](packages/pycodex-ws/README.md)。
113
+ 独立分发的 [VS Code 扩展](packages/pycodex-ws/extensions/vscode/README.md)
114
+ 复用同一后端,支持 Remote SSH 工作区。
115
+
116
+ ## Python API
117
+
118
+ 在 Python 中复用标准模型和工具装配:
119
+
120
+ ```python
121
+ from pycodex.bootstrap import build_agent, build_model
122
+
123
+ agent = build_agent(build_model())
124
+ try:
125
+ result = agent.ask("用一句话概括这个仓库。")
126
+ print(result.output_text)
127
+ finally:
128
+ agent.shutdown()
129
+ ```
130
+
131
+ 自定义集成时,`Agent` 接收 model client、`ToolRegistry` 和 `ContextConfig`。
132
+ 通过 `await agent.run_turn([...])` 异步运行 turn;`AgentRuntime` 提供输入队列、
133
+ 会话命令和前端订阅。详见[运行时契约与示例](docs/RUNTIME.md)。
134
+
135
+ ## Portable 配置
136
+
137
+ Portable mode 将配置、指令和 skills 加密打包,供其他机器恢复使用。
138
+ 先在单独的终端启动存储服务:
139
+
140
+ ```bash
141
+ python3 -m pycodex.portable.server --root .tmp/pycodex_storage --host 127.0.0.1 --port 5577
142
+ ```
143
+
144
+ 上传默认 Codex home,或指定其他目录:
145
+
146
+ ```bash
147
+ pycodex --put @127.0.0.1:5577
148
+ pycodex --put /path/to/.codex/@127.0.0.1:5577
149
+ ```
150
+
151
+ 上传并验证下载成功后,命令会打印可复用的 `pycodex --call ...` 启动命令。
152
+ 在目标机器运行时,使用该机器能够访问的存储服务地址。
153
+
154
+ ## 开发与参考
155
+
156
+ ```bash
157
+ uv sync --dev
158
+ uv run pycodex
159
+ uv run --dev pytest
160
+ ```
161
+
162
+ - [测试环境与覆盖范围](tests/TESTS.md)
163
+ - [架构、包布局与运行时契约](docs/RUNTIME.md)
164
+ - [提示、配置与上下文](docs/CONTEXT.md)
165
+ - [上游对齐](docs/ALIGNMENT.md)
166
+ - [Responses 兼容服务](docs/responses_server/README.md)
167
+
168
+ 发布记录和实现细节放在 `docs/`。
@@ -17,6 +17,7 @@ from .events import (
17
17
  ModelCalledEvent,
18
18
  ModelCompletedEvent,
19
19
  ModelEvent,
20
+ ModelRequestTimingEvent,
20
21
  StreamErrorEvent,
21
22
  TerminalEvent,
22
23
  TokenCountEvent,
@@ -558,7 +559,7 @@ class Agent:
558
559
  self._emit(started_type(*details))
559
560
 
560
561
  def handle_compact_stream_event(event: "ModelEvent") -> "None":
561
- if isinstance(event, StreamErrorEvent) or (
562
+ if isinstance(event, (StreamErrorEvent, ModelRequestTimingEvent)) or (
562
563
  phase == "manual" and isinstance(event, TokenCountEvent)
563
564
  ):
564
565
  self._emit(replace(event, turn_id=turn_id))
@@ -271,6 +271,17 @@ class TokenCountEvent(ModelEvent):
271
271
  display.set_prompt(display.main_prompt())
272
272
 
273
273
 
274
+ @dataclass(frozen=True)
275
+ class ModelRequestTimingEvent(ModelEvent):
276
+ model: "str"
277
+ elapsed_seconds: "float"
278
+ first_data_seconds: "typing.Union[float, None]"
279
+ outcome: "str"
280
+ turn_id: "str" = ""
281
+ submission_id: "typing.Union[str, None]" = None
282
+ kind: ClassVar[str] = "model_request_timing"
283
+
284
+
274
285
  @dataclass(frozen=True)
275
286
  class StreamErrorEvent(ModelEvent):
276
287
  message: "str"
@@ -285,9 +296,14 @@ class StreamErrorEvent(ModelEvent):
285
296
  def visualize(self) -> "str":
286
297
  return self.message
287
298
 
299
+ def detail(self) -> "str":
300
+ if not self.error:
301
+ return self.message
302
+ return "{0}\n{1}".format(self.message, self.error)
303
+
288
304
  def render(self, display: "EventDisplay") -> "None":
289
305
  display.stream_buffer = ""
290
- display.write("[status] " + self.visualize(), "status")
306
+ display.write("[status] " + self.detail(), "status")
291
307
  display.set_status("reconnecting")
292
308
 
293
309