python-codex 0.6.0__py3-none-any.whl → 0.6.1__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.
- pycodex/__init__.py +4 -4
- pycodex/bootstrap.py +10 -12
- pycodex/cli.py +0 -14
- pycodex/context.py +3 -4
- pycodex/events.py +1 -1
- pycodex/model.py +16 -7
- pycodex/prompts/models.json +14 -0
- pycodex/protocol.py +3 -0
- pycodex/tools/__init__.py +9 -10
- pycodex/tools/clock_tool.py +57 -7
- pycodex/tools/exec_runtime.py +29 -78
- pycodex/tools/exec_tool.py +2 -6
- pycodex/utils/compat.py +41 -0
- pycodex/utils/dotenv.py +0 -7
- pycodex/utils/history.py +41 -15
- pycodex/utils/session_persist.py +33 -19
- python_codex-0.6.1.dist-info/METADATA +201 -0
- {python_codex-0.6.0.dist-info → python_codex-0.6.1.dist-info}/RECORD +29 -29
- responses_server/app.py +3 -1
- responses_server/config.py +6 -1
- responses_server/gemini_api.py +333 -0
- responses_server/messages_api.py +9 -1
- responses_server/request_translation.py +6 -0
- responses_server/stream_router.py +15 -0
- responses_server/tools/web_search.py +3 -0
- responses_server/transport.py +75 -19
- pycodex/tools/request_user_input_tool.py +0 -82
- python_codex-0.6.0.dist-info/METADATA +0 -352
- {python_codex-0.6.0.dist-info → python_codex-0.6.1.dist-info}/WHEEL +0 -0
- {python_codex-0.6.0.dist-info → python_codex-0.6.1.dist-info}/entry_points.txt +0 -0
- {python_codex-0.6.0.dist-info → python_codex-0.6.1.dist-info}/licenses/LICENSE +0 -0
pycodex/utils/compat.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"""Compatibility helpers for supported Python versions."""
|
|
2
2
|
|
|
3
|
+
import ast
|
|
3
4
|
import asyncio
|
|
4
5
|
import functools
|
|
6
|
+
import io
|
|
5
7
|
import shlex
|
|
8
|
+
import tokenize
|
|
6
9
|
|
|
7
10
|
try:
|
|
8
11
|
from http.server import ThreadingHTTPServer
|
|
@@ -117,3 +120,41 @@ def is_ascii(text):
|
|
|
117
120
|
except UnicodeEncodeError:
|
|
118
121
|
return False
|
|
119
122
|
return True
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def compile_async_cell(source, filename="<exec>"):
|
|
126
|
+
if hasattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT"):
|
|
127
|
+
tree = ast.parse(source, filename=filename)
|
|
128
|
+
if tree.body and isinstance(tree.body[-1], ast.Expr):
|
|
129
|
+
expr = tree.body[-1]
|
|
130
|
+
target = ast.Name(id="__exec_result__", ctx=ast.Store())
|
|
131
|
+
tree.body[-1] = ast.copy_location(
|
|
132
|
+
ast.Assign(targets=[target], value=expr.value), expr,
|
|
133
|
+
)
|
|
134
|
+
ast.fix_missing_locations(tree.body[-1])
|
|
135
|
+
return compile(tree, filename, "exec", flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
|
|
136
|
+
return compile_async_cell_compat(source, filename)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def compile_async_cell_compat(source, filename="<exec>"): # pragma: no cover - Python 3.6/3.7 path
|
|
140
|
+
source = source.replace("\r\n", "\n").replace("\r", "\n")
|
|
141
|
+
string_lines = set()
|
|
142
|
+
for token in tokenize.generate_tokens(io.StringIO(source).readline):
|
|
143
|
+
if tokenize.tok_name[token.type] in ("STRING", "FSTRING_MIDDLE", "TSTRING_MIDDLE"):
|
|
144
|
+
string_lines.update(range(token.start[0], token.end[0]))
|
|
145
|
+
source = (
|
|
146
|
+
"async def __exec_cell__():\n"
|
|
147
|
+
+ "\n".join(
|
|
148
|
+
line if index in string_lines else " " + line
|
|
149
|
+
for index, line in enumerate(source.split("\n"))
|
|
150
|
+
)
|
|
151
|
+
+ "\n pass\n"
|
|
152
|
+
)
|
|
153
|
+
tree = ast.parse(source, filename=filename)
|
|
154
|
+
body = tree.body[0].body
|
|
155
|
+
if len(body) > 1 and isinstance(body[-2], ast.Expr):
|
|
156
|
+
expression = body[-2]
|
|
157
|
+
body[-2] = ast.copy_location(ast.Return(value=expression.value), expression)
|
|
158
|
+
for statement in body:
|
|
159
|
+
ast.increment_lineno(statement, -1)
|
|
160
|
+
return compile(tree, filename, "exec")
|
pycodex/utils/dotenv.py
CHANGED
|
@@ -4,17 +4,12 @@ from pathlib import Path
|
|
|
4
4
|
|
|
5
5
|
ILLEGAL_ENV_VAR_PREFIX = "CODEX_"
|
|
6
6
|
DOTENV_FILENAME = ".env"
|
|
7
|
-
_LOADED_CODEX_DOTENV_HOMES: "typing.Set[str]" = set()
|
|
8
7
|
|
|
9
8
|
|
|
10
9
|
def load_codex_dotenv(config_path: "typing.Union[str, Path]") -> "None":
|
|
11
10
|
codex_home = str(Path(config_path).resolve().parent)
|
|
12
|
-
if codex_home in _LOADED_CODEX_DOTENV_HOMES:
|
|
13
|
-
return
|
|
14
|
-
|
|
15
11
|
dotenv_path = Path(codex_home) / DOTENV_FILENAME
|
|
16
12
|
if not dotenv_path.is_file():
|
|
17
|
-
_LOADED_CODEX_DOTENV_HOMES.add(codex_home)
|
|
18
13
|
return
|
|
19
14
|
|
|
20
15
|
for key, value in parse_dotenv(
|
|
@@ -24,8 +19,6 @@ def load_codex_dotenv(config_path: "typing.Union[str, Path]") -> "None":
|
|
|
24
19
|
continue
|
|
25
20
|
os.environ[key] = value
|
|
26
21
|
|
|
27
|
-
_LOADED_CODEX_DOTENV_HOMES.add(codex_home)
|
|
28
|
-
|
|
29
22
|
|
|
30
23
|
def parse_dotenv(text: "str") -> "typing.Dict[str, str]":
|
|
31
24
|
values: "typing.Dict[str, str]" = {}
|
pycodex/utils/history.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Pure conversation-history projections, independent of rollout storage."""
|
|
2
2
|
|
|
3
3
|
import typing
|
|
4
|
+
from dataclasses import replace
|
|
4
5
|
|
|
5
6
|
from ..protocol import AgentMessage, AssistantMessage, ConversationItem, UserMessage
|
|
6
7
|
|
|
@@ -10,24 +11,49 @@ def serialize_history_item(item: "ConversationItem") -> "typing.Dict[str, object
|
|
|
10
11
|
payload = item.serialize()
|
|
11
12
|
if isinstance(item, UserMessage):
|
|
12
13
|
payload["name"] = item.name
|
|
14
|
+
if isinstance(item, (UserMessage, AgentMessage, AssistantMessage)):
|
|
15
|
+
payload["visible"] = item.visible
|
|
13
16
|
return payload
|
|
14
17
|
|
|
15
18
|
|
|
19
|
+
def restore_visibility(item, payload):
|
|
20
|
+
"""Older rollouts use the message type's visibility default."""
|
|
21
|
+
visible = payload.get("visible", item.visible)
|
|
22
|
+
if not isinstance(visible, bool):
|
|
23
|
+
raise ValueError("history item visible must be a boolean")
|
|
24
|
+
return replace(item, visible=visible)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def iter_display_turns(history):
|
|
28
|
+
"""Project message visibility and grouping; None marks a compaction."""
|
|
29
|
+
prompt = None
|
|
30
|
+
response = ""
|
|
31
|
+
phase = None
|
|
32
|
+
for item in history:
|
|
33
|
+
if item is None:
|
|
34
|
+
if prompt is not None or response:
|
|
35
|
+
yield (prompt or "", response)
|
|
36
|
+
yield None
|
|
37
|
+
prompt, response, phase = None, "", None
|
|
38
|
+
elif isinstance(item, (UserMessage, AgentMessage)):
|
|
39
|
+
# Hidden mail must not split commentary from its final answer.
|
|
40
|
+
# Keep an existing unphased reply when the next input arrives.
|
|
41
|
+
if item.visible or (response and phase != "commentary"):
|
|
42
|
+
if prompt is not None or response:
|
|
43
|
+
yield (prompt or "", response)
|
|
44
|
+
prompt, response, phase = None, "", None
|
|
45
|
+
if item.visible:
|
|
46
|
+
prompt = item.text
|
|
47
|
+
elif isinstance(item, AssistantMessage) and item.visible:
|
|
48
|
+
response, phase = item.text, item.phase
|
|
49
|
+
if phase == "final":
|
|
50
|
+
yield (prompt or "", response)
|
|
51
|
+
prompt, response, phase = None, "", None
|
|
52
|
+
if prompt is not None or response:
|
|
53
|
+
yield (prompt or "", response)
|
|
54
|
+
|
|
55
|
+
|
|
16
56
|
def conversation_history_to_turns(
|
|
17
57
|
history: "typing.Iterable[ConversationItem]",
|
|
18
58
|
) -> "typing.Tuple[typing.Tuple[str, str], ...]":
|
|
19
|
-
|
|
20
|
-
current_user_text: "typing.Union[str, None]" = None
|
|
21
|
-
current_assistant_text = ""
|
|
22
|
-
for item in history:
|
|
23
|
-
if isinstance(item, (UserMessage, AgentMessage)):
|
|
24
|
-
if current_user_text is not None or current_assistant_text:
|
|
25
|
-
turns.append((current_user_text or "", current_assistant_text))
|
|
26
|
-
current_user_text = item.text
|
|
27
|
-
current_assistant_text = ""
|
|
28
|
-
continue
|
|
29
|
-
if isinstance(item, AssistantMessage):
|
|
30
|
-
current_assistant_text = item.text
|
|
31
|
-
if current_user_text is not None or current_assistant_text:
|
|
32
|
-
turns.append((current_user_text or "", current_assistant_text))
|
|
33
|
-
return tuple(turns)
|
|
59
|
+
return tuple(entry for entry in iter_display_turns(history) if entry is not None)
|
pycodex/utils/session_persist.py
CHANGED
|
@@ -23,7 +23,10 @@ from ..protocol import (
|
|
|
23
23
|
)
|
|
24
24
|
from .compactor import SUMMARY_PREFIX
|
|
25
25
|
from .event_helpers import shorten_title
|
|
26
|
-
from .history import
|
|
26
|
+
from .history import (
|
|
27
|
+
conversation_history_to_turns, iter_display_turns,
|
|
28
|
+
restore_visibility, serialize_history_item,
|
|
29
|
+
)
|
|
27
30
|
from .get_env import get_package_version
|
|
28
31
|
|
|
29
32
|
SESSION_INDEX_FILENAME = "session_index.jsonl"
|
|
@@ -159,6 +162,7 @@ class SessionRolloutRecorder:
|
|
|
159
162
|
"type": "user_message",
|
|
160
163
|
"message": item.text,
|
|
161
164
|
"name": item.name,
|
|
165
|
+
"visible": item.visible,
|
|
162
166
|
"images": [],
|
|
163
167
|
"local_images": [],
|
|
164
168
|
"text_elements": [],
|
|
@@ -321,9 +325,7 @@ def load_resumed_session_path(
|
|
|
321
325
|
|
|
322
326
|
if item_type == "event_msg" and isinstance(payload, dict):
|
|
323
327
|
if payload.get("type") == "user_message":
|
|
324
|
-
history.append(
|
|
325
|
-
text=str(payload.get("message", "")), name=payload.get("name", "user")
|
|
326
|
-
))
|
|
328
|
+
history.append(_user_message_from_event(payload))
|
|
327
329
|
saw_input = True
|
|
328
330
|
continue
|
|
329
331
|
|
|
@@ -399,28 +401,36 @@ def load_display_history(
|
|
|
399
401
|
limit: "int" = 80,
|
|
400
402
|
) -> "typing.Tuple[typing.Union[typing.Tuple[str, str], None], ...]":
|
|
401
403
|
"""Read visible turns across checkpoints; None marks a compaction."""
|
|
404
|
+
return tuple(deque(iter_display_turns(_iter_display_items(rollout_path)), maxlen=limit))
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _user_message_from_event(payload):
|
|
408
|
+
return restore_visibility(UserMessage(
|
|
409
|
+
text=str(payload.get("message", "")), name=payload.get("name", "user"),
|
|
410
|
+
), payload)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _iter_display_items(rollout_path):
|
|
402
414
|
path = Path(rollout_path).expanduser().resolve()
|
|
403
|
-
turns = deque(maxlen=limit)
|
|
404
415
|
for entry in _iter_rollout_entries(path):
|
|
405
416
|
item_type = entry.get("type")
|
|
406
417
|
payload = entry.get("payload")
|
|
407
418
|
if not isinstance(payload, dict):
|
|
408
419
|
continue
|
|
409
420
|
if item_type == "compacted":
|
|
410
|
-
|
|
421
|
+
yield None
|
|
411
422
|
elif item_type == "event_msg" and payload.get("type") == "user_message":
|
|
412
|
-
|
|
413
|
-
elif item_type == "response_item" and payload.get("type") == "agent_message":
|
|
414
|
-
turns.append((AgentMessage.from_response_item(payload).text, ""))
|
|
423
|
+
yield _user_message_from_event(payload)
|
|
415
424
|
elif (
|
|
416
425
|
item_type == "response_item"
|
|
417
|
-
and
|
|
418
|
-
|
|
426
|
+
and (
|
|
427
|
+
payload.get("type") == "agent_message"
|
|
428
|
+
or (payload.get("type") == "message" and payload.get("role") == "assistant")
|
|
429
|
+
)
|
|
419
430
|
):
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
return tuple(turns)
|
|
431
|
+
items = []
|
|
432
|
+
_append_deserialized_response_item(items, payload, {}, False)
|
|
433
|
+
yield from items
|
|
424
434
|
|
|
425
435
|
|
|
426
436
|
def _drop_unmatched_tool_calls(
|
|
@@ -625,19 +635,23 @@ def _append_deserialized_response_item(
|
|
|
625
635
|
) -> "None":
|
|
626
636
|
response_item_type = str(payload.get("type", "")).strip()
|
|
627
637
|
if response_item_type == "agent_message":
|
|
628
|
-
history.append(
|
|
638
|
+
history.append(restore_visibility(
|
|
639
|
+
AgentMessage.from_response_item(payload), payload,
|
|
640
|
+
))
|
|
629
641
|
return
|
|
630
642
|
if response_item_type == "message":
|
|
631
643
|
role = str(payload.get("role", "")).strip()
|
|
632
644
|
if role == "assistant":
|
|
633
|
-
history.append(
|
|
645
|
+
history.append(restore_visibility(
|
|
646
|
+
AssistantMessage.from_response_item(payload), payload,
|
|
647
|
+
))
|
|
634
648
|
return
|
|
635
649
|
if include_user_messages and role == "user":
|
|
636
650
|
history.append(
|
|
637
|
-
UserMessage(
|
|
651
|
+
restore_visibility(UserMessage(
|
|
638
652
|
text=_extract_response_message_text(payload), id=payload.get("id"),
|
|
639
653
|
name=payload.get("name", "user"),
|
|
640
|
-
)
|
|
654
|
+
), payload)
|
|
641
655
|
)
|
|
642
656
|
return
|
|
643
657
|
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-codex
|
|
3
|
+
Version: 0.6.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 is a Python implementation of the [Codex](https://github.com/openai/codex)
|
|
26
|
+
coding agent. Use it in a terminal, in a browser, or from your own Python code.
|
|
27
|
+
It supports file editing, command execution, multiple agents and saved sessions.
|
|
28
|
+
See [upstream alignment](docs/ALIGNMENT.md) for the supported scope and differences.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
| Package | Includes | Python |
|
|
33
|
+
| --- | --- | --- |
|
|
34
|
+
| `python-codex` | CLI and Python API | 3.6.2+ |
|
|
35
|
+
| `pycodex-ws` | Core plus browser workspace and optional Feishu integration | 3.7+ |
|
|
36
|
+
|
|
37
|
+
For the CLI and Python API:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
python3 -m pip install --upgrade python-codex
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For the browser workspace, install `pycodex-ws`; it includes the matching core version:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
python3 -m pip install --upgrade pycodex-ws
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Configure a model
|
|
50
|
+
|
|
51
|
+
Create `~/.codex/config.toml`. For example, with OpenAI:
|
|
52
|
+
|
|
53
|
+
```toml
|
|
54
|
+
model = "gpt-5.4"
|
|
55
|
+
model_provider = "openai"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Supply the API key through the environment or `~/.codex/.env`:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
export OPENAI_API_KEY="your-api-key"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For another provider, configure its `base_url` and `env_key`. Select the API with
|
|
65
|
+
`compat_api` in the profile or provider table:
|
|
66
|
+
|
|
67
|
+
| `compat_api` | API |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `"none"` (default) | Responses |
|
|
70
|
+
| `"chat_completions"` | Chat Completions, including Step |
|
|
71
|
+
| `"messages"` | Claude Messages |
|
|
72
|
+
| `"gemini"` | Gemini API |
|
|
73
|
+
|
|
74
|
+
Legacy `use_chat_completion = true/false` remains supported as `chat_completions/none`.
|
|
75
|
+
|
|
76
|
+
Use `--profile NAME` to select `NAME.config.toml` beside the base config or an
|
|
77
|
+
embedded `[profiles.NAME]` table. Use `--config PATH` for a different config file,
|
|
78
|
+
and `pycodex doctor` to check configuration and connectivity.
|
|
79
|
+
The browser workspace picks up profile changes for new sessions without a restart.
|
|
80
|
+
|
|
81
|
+
See [configuration](docs/CONTEXT.md#配置读取与-profile) and the
|
|
82
|
+
[Gemini setup example](docs/responses_server/README.md#gemini-api).
|
|
83
|
+
Gemini 4 Argon includes a prompt adapted from Antigravity.
|
|
84
|
+
Gemini tool arguments stream as they are generated; tools run after the complete call is received.
|
|
85
|
+
|
|
86
|
+
## Use the CLI
|
|
87
|
+
|
|
88
|
+
Run these commands from your project directory:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pycodex
|
|
92
|
+
pycodex "Summarize this repository."
|
|
93
|
+
pycodex --profile my_profile "Review the latest changes."
|
|
94
|
+
pycodex --json "Summarize this repository."
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The first command starts an interactive session; the others run a single task.
|
|
98
|
+
The agent can edit files and run commands with your user account's permissions.
|
|
99
|
+
|
|
100
|
+
| Command | Purpose |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| `/model`, `/effort` | View or change the model and reasoning effort |
|
|
103
|
+
| `/cd <directory>` | Change directory while idle |
|
|
104
|
+
| `/title`, `/history` | Manage the title and inspect conversation history |
|
|
105
|
+
| `/resume` | List or resume saved sessions |
|
|
106
|
+
| `/compact` | Summarize history to free context |
|
|
107
|
+
| `/queue <message>` | Submit a task after the current one finishes |
|
|
108
|
+
|
|
109
|
+
Send a message while the agent is working to redirect it, or use `/queue` to
|
|
110
|
+
wait. Sessions are saved under `~/.codex/sessions` by default.
|
|
111
|
+
Ctrl+C or Ctrl+D closes the session after issued operations finish.
|
|
112
|
+
Run `pycodex --help` for all options.
|
|
113
|
+
|
|
114
|
+
For a local vLLM server:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
pycodex --vllm-endpoint http://127.0.0.1:18000
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Use the browser workspace
|
|
121
|
+
|
|
122
|
+
After installing `pycodex-ws`, create `workspaces.json`:
|
|
123
|
+
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"workspaces": [
|
|
127
|
+
{"id": "demo", "work_dir": ".", "state_path": "demo", "enable_board": true}
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Start the server and keep the terminal open:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
pycodex-ws --listen 127.0.0.1:6007 --workspace-config workspaces.json
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Open `http://127.0.0.1:6007`. The workspace uses the same model configuration as
|
|
139
|
+
the CLI and provides conversations, a live HTML Canvas, and file/diff viewers.
|
|
140
|
+
Click file links to inspect code, then select text and use **Insert into chat**
|
|
141
|
+
to include it in your next message.
|
|
142
|
+
|
|
143
|
+
The settings menu provides interface customization and password-protected
|
|
144
|
+
workspace sharing. See the [workspace guide](packages/pycodex-ws/README.md) for
|
|
145
|
+
session controls, storage and Feishu setup. A separate
|
|
146
|
+
[VS Code extension](packages/pycodex-ws/extensions/vscode/README.md) connects to
|
|
147
|
+
the same backend, including over Remote SSH.
|
|
148
|
+
|
|
149
|
+
## Use the Python API
|
|
150
|
+
|
|
151
|
+
Save this as `example.py` and run `python3 example.py`. It uses the model
|
|
152
|
+
configuration above and the standard tools:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from contextlib import closing
|
|
156
|
+
|
|
157
|
+
from pycodex import AgentsManager, CodexConfig
|
|
158
|
+
from pycodex.bootstrap import build_model
|
|
159
|
+
from pycodex.utils.async_bridge import run_async
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
async def main():
|
|
163
|
+
config = CodexConfig.from_env()
|
|
164
|
+
with closing(build_model(config.provider)) as model:
|
|
165
|
+
runtime = AgentsManager(
|
|
166
|
+
model.client, config.context,
|
|
167
|
+
).create_root_runtime()
|
|
168
|
+
try:
|
|
169
|
+
await runtime.start()
|
|
170
|
+
receipt = await runtime.submit_input("Summarize this repository.")
|
|
171
|
+
print((await receipt.future).output_text)
|
|
172
|
+
finally:
|
|
173
|
+
await runtime.close()
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
run_async(main())
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`AgentsManager` creates the runtime; `submit_input` submits work.
|
|
180
|
+
Await `receipt.future` for the result. The example closes the runtime and any
|
|
181
|
+
local compatibility server when finished. See [runtime contracts](docs/RUNTIME.md#ownership) for
|
|
182
|
+
custom models, tools and integrations.
|
|
183
|
+
|
|
184
|
+
## More features
|
|
185
|
+
|
|
186
|
+
- [Portable configuration](docs/PORTABLE.md): transfer encrypted configuration, instructions and skills to another machine.
|
|
187
|
+
- [Frontend customization](packages/pycodex-ws/README.md#customize-the-interface): change the workspace through an Agent conversation or your own CSS/JavaScript.
|
|
188
|
+
- [Responses compatibility server](docs/responses_server/README.md): expose a supported backend through the Responses API.
|
|
189
|
+
- [Retries and fallback models](docs/RUNTIME.md#responses-reliability): configure recovery from model failures.
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
From the repository root:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
env -u VIRTUAL_ENV uv sync --dev
|
|
197
|
+
env -u VIRTUAL_ENV uv run --dev pytest
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
See the [development guide](AGENTS.md), [test guide](tests/TESTS.md),
|
|
201
|
+
[runtime architecture](docs/RUNTIME.md) and [workspace API](docs/WORKSPACE_API.md).
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
pycodex/__init__.py,sha256=
|
|
1
|
+
pycodex/__init__.py,sha256=2rP9Qd5ClJlSI3bPNB0Jmn9lJUpeeo7gcbRPUvH81eI,2904
|
|
2
2
|
pycodex/__main__.py,sha256=MHKZ_ae3fSLGTLUUMOx15fWdeOnJSHhq-zslRP5F5Lc,79
|
|
3
3
|
pycodex/agent.py,sha256=yZ75cpCBM2acDBayKo9ekqHZRvODXsKtJFjyhm78v9M,22704
|
|
4
|
-
pycodex/bootstrap.py,sha256=
|
|
5
|
-
pycodex/cli.py,sha256=
|
|
4
|
+
pycodex/bootstrap.py,sha256=MR0msG5SEAAbdT8i_wCn1ueLdNx9sETsqIVWQPhPxus,4990
|
|
5
|
+
pycodex/cli.py,sha256=HkX_fFNUyPnNo25FeSa9ZMaQG79rqk3b0oV4ZH0h68c,12576
|
|
6
6
|
pycodex/config.py,sha256=mGM7oLaciBwGXHMQjKVifWFTd7gZTfzqlQeJSFzcQic,1218
|
|
7
|
-
pycodex/context.py,sha256=
|
|
8
|
-
pycodex/events.py,sha256=
|
|
9
|
-
pycodex/model.py,sha256=
|
|
7
|
+
pycodex/context.py,sha256=Quc8VBNr7XVoan6-l1rzVJ1njH7Hj7lbdux_mJLXESQ,41096
|
|
8
|
+
pycodex/events.py,sha256=dvEzq6jH5esN4Y99M-66feD19oVfmrOBvm4LG5xEhSs,22736
|
|
9
|
+
pycodex/model.py,sha256=omwYru3BZ2ejZTCvUTFsIltjtH1LQWG3zWrzZ3hyNhI,58672
|
|
10
10
|
pycodex/multi_agents.py,sha256=3KN_Ai3UremtsQppvxd0pE7pFam_v0zBastCNi-NNx8,16150
|
|
11
|
-
pycodex/protocol.py,sha256
|
|
11
|
+
pycodex/protocol.py,sha256=-rqxpXiYkbSTzzzsAOGXwIXWREAb4uOd3sh0V0GHLW8,14650
|
|
12
12
|
pycodex/runtime.py,sha256=Szeufq_cAPSw_r7HCmpin1nm_Ugsj3FCcn-pgCCkrrI,37525
|
|
13
13
|
pycodex/portable/__init__.py,sha256=NgACNTFK5hbRK6fqbL89qjPuh44N950pr4eoDWLfKD4,1110
|
|
14
14
|
pycodex/portable/client.py,sha256=ZvXjKCB557ZZ-ELmec9DIxymWbG8QBZsaU6e0axu0T0,15508
|
|
@@ -16,7 +16,7 @@ pycodex/portable/protocol.py,sha256=jtyPHI3aVY4UldlSIzeZRQX3MCFzPYfG-0iDC6JSWYo,
|
|
|
16
16
|
pycodex/portable/server.py,sha256=UCQAqcO0OTNkEv4MQgR-fIt1y5bzdqMndFJtMm16Lqk,7503
|
|
17
17
|
pycodex/prompts/default_base_instructions.md,sha256=D65mcj6bo4CDvVom-D9cbJRJVNquo0NghKt164_fRsg,20923
|
|
18
18
|
pycodex/prompts/developer_messages.json,sha256=E-CAWMOL2KoImJ1U_1aA-OYbuVtJF0-GtZkn983B-TI,4007
|
|
19
|
-
pycodex/prompts/models.json,sha256=
|
|
19
|
+
pycodex/prompts/models.json,sha256=cMpnFFT56zmndGnESKSIVzUyKZOCuxjVFAouZiDx_Qk,619361
|
|
20
20
|
pycodex/prompts/goals/budget_limit.md,sha256=RbvI03fMD3tTy-ytnzylqAsIr9b2O_Cb81CQejR1J-k,775
|
|
21
21
|
pycodex/prompts/goals/continuation.md,sha256=K7OtQKQNoCKcgne-e-SQBGJjEpnCVcVCtML7T-82bWI,6153
|
|
22
22
|
pycodex/prompts/permissions/approval_policy/never.md,sha256=QceTG6wjkaJARjYr0HYV1aPnPcpGcrkRUW-smWRr6MQ,120
|
|
@@ -27,16 +27,16 @@ pycodex/prompts/permissions/approval_policy/unless_trusted.md,sha256=XHpi1Lfx1iI
|
|
|
27
27
|
pycodex/prompts/permissions/sandbox_mode/danger_full_access.md,sha256=nZ7YHacBd3cAHKRZc9XClOOOnXJPXPh0WFBueh5C2D0,197
|
|
28
28
|
pycodex/prompts/permissions/sandbox_mode/read_only.md,sha256=2rAPEXsBYCcuttI5j3euS-3uv_v97catIsnhxlSQSIM,173
|
|
29
29
|
pycodex/prompts/permissions/sandbox_mode/workspace_write.md,sha256=lVN-LwrBbHqlv5yVjcd_mU8tzZW8jfKpTatJKIZu9HI,277
|
|
30
|
-
pycodex/tools/__init__.py,sha256=
|
|
30
|
+
pycodex/tools/__init__.py,sha256=yE_9gmbNrbUKXMfZO79Y8chfHMmOe9NBozy4i0vn0bY,5175
|
|
31
31
|
pycodex/tools/agent_tool_schemas.py,sha256=juw4b2pp2tDhDPVTVY4rytm8IyVr2h686Qfk_5e9S0E,780
|
|
32
32
|
pycodex/tools/apply_patch_tool.py,sha256=LcyJuGtndmLLbFL1sIUQ2Y6KLCef95KBfi0adGA1bN8,13183
|
|
33
33
|
pycodex/tools/base_tool.py,sha256=dD_makVyjbnJN_cAhRAj_sJJHYh8VUDoXFiRoWDh6Ko,8644
|
|
34
34
|
pycodex/tools/clock_manager.py,sha256=Xn1i4aasN_ZZdL9WsA_cS-RAVLV-SQXmOecMc1nGCa4,5884
|
|
35
|
-
pycodex/tools/clock_tool.py,sha256=
|
|
35
|
+
pycodex/tools/clock_tool.py,sha256=14Y1uLagSuEO8eaACQcQQbPZP1R6cSAYaReZIyZen4c,3790
|
|
36
36
|
pycodex/tools/code_mode_manager.py,sha256=_bZ7RlKzAEqb2m6PBfR7Or1GJYajeuSz1xCmuJcGsV8,15975
|
|
37
37
|
pycodex/tools/exec_command_tool.py,sha256=Mf2FRsx6o5pWq68PCXNCSOce4wzwqyX6VrXNOvym_So,4076
|
|
38
|
-
pycodex/tools/exec_runtime.py,sha256=
|
|
39
|
-
pycodex/tools/exec_tool.py,sha256=
|
|
38
|
+
pycodex/tools/exec_runtime.py,sha256=vR4HfTxknxbKDhO7QFlBDgltKegFy5KJE1eAODWVYqY,12061
|
|
39
|
+
pycodex/tools/exec_tool.py,sha256=7HQ8sazJBr4WQklJsCJS30kfVaQOsg5gBgIutC6QyRY,5209
|
|
40
40
|
pycodex/tools/followup_task_tool.py,sha256=D3nRu6csiriwe1WVig_HNLRzJQfDNMmk2R75vMfSI4Y,1609
|
|
41
41
|
pycodex/tools/goal_manager.py,sha256=8zicCSjqJgYyONL6EaOIyaGjTA6EBECi1NYQgEwLFms,8480
|
|
42
42
|
pycodex/tools/goal_tool.py,sha256=TShHf6dh6YCdMPoc6zuIgj0W2D_O95ewmh6OZKFXAX0,5225
|
|
@@ -47,7 +47,6 @@ pycodex/tools/list_dir_tool.py,sha256=_U6ouRcVbGHT5mChVyUsiMgDGrFK7EFMFaspPLAHtH
|
|
|
47
47
|
pycodex/tools/python_tool_schema.py,sha256=yoJHP7GJGSu6oyT_0nxbGJWy_TaTobFwDoFQMl65464,5781
|
|
48
48
|
pycodex/tools/read_file_tool.py,sha256=YK__uhIVldBjAQega5S_ZuZU5jgP_kLOwr-YhjAmRo4,9735
|
|
49
49
|
pycodex/tools/request_permissions_tool.py,sha256=t-Y-iEhvUyE9IJAeiwnzfUdbQ73Gw7uxFcWLwS0no4o,4000
|
|
50
|
-
pycodex/tools/request_user_input_tool.py,sha256=rVjJPVzrkBPmAQkfPWiCvAuIAp0Lt8WwuVgfbNbbeHY,2893
|
|
51
50
|
pycodex/tools/runtime_services.py,sha256=oEqUyR7-oHQdTsEWXoiP7VKP_JDiG5dc8AjlSGQFjYI,7542
|
|
52
51
|
pycodex/tools/send_message_tool.py,sha256=mH51ufxTCcsHuLFI1tDKyX14AHPMij2cMJLJdlFFjZQ,1414
|
|
53
52
|
pycodex/tools/shell_command_tool.py,sha256=bJKlTdIs2i28vH5f4GZfpunezr6JNicRzsL_6Vc3HvU,3675
|
|
@@ -64,18 +63,18 @@ pycodex/utils/__init__.py,sha256=ih_KyZ2_bajR5YlZ1QMTWOHnuEBda-S3gQ7PwnBtLZQ,453
|
|
|
64
63
|
pycodex/utils/async_bridge.py,sha256=BXsKTUTUvUtPJnItQlulFRdV7opdKKDlsKLdtVTYshs,1732
|
|
65
64
|
pycodex/utils/codex_config.py,sha256=oCisEGUJcJxcZL1wIqXkon1xTShymOlz96E3VgDVTlY,3748
|
|
66
65
|
pycodex/utils/compactor.py,sha256=IzRC6bY1ab6WP33jRMFMbmlJ3TMJcoTgDFFG-Y4C86E,6299
|
|
67
|
-
pycodex/utils/compat.py,sha256=
|
|
66
|
+
pycodex/utils/compat.py,sha256=16FyHHlDES57mOHCasN4FWveEkGYd2aZex9-Eojlkg8,5277
|
|
68
67
|
pycodex/utils/crypto.py,sha256=HRCXF26Ft3S8CG2X0YpZwa8GMU9YJ0Loo8Q9PMPMcNw,1249
|
|
69
68
|
pycodex/utils/debug.py,sha256=5XMpWpwFSm6woyx6RGXvN2qL0afRtTQbwlOAv-LotTw,913
|
|
70
69
|
pycodex/utils/doctor.py,sha256=xhPflFEYFfmtQ4-LjnIyz-cNts0GjNtAK0inVTYfKiI,10764
|
|
71
|
-
pycodex/utils/dotenv.py,sha256=
|
|
70
|
+
pycodex/utils/dotenv.py,sha256=rLHBfz5zPZMbrJqyRyYPy3j4oiDwuzfrH4mDH3XRrow,1647
|
|
72
71
|
pycodex/utils/event_helpers.py,sha256=VH8MMmpAO98EFb-PeEKjqAw8LCiGGaWegENP9Jnc6kA,12021
|
|
73
72
|
pycodex/utils/get_env.py,sha256=L4S8h84tkgxZKvrxY7ezTgePbXepG-yIigUTKeY6uuE,6094
|
|
74
|
-
pycodex/utils/history.py,sha256=
|
|
73
|
+
pycodex/utils/history.py,sha256=r9Fw9HmImzgvRAcnd92zw3E2tAOgerZCp4kyJOwA1AM,2362
|
|
75
74
|
pycodex/utils/image_utils.py,sha256=OUzUs0Cow4L8Z2np0EBeEUotAqqsCmRmhrV8AxZUXRw,2460
|
|
76
75
|
pycodex/utils/model_metadata.py,sha256=uK3iI4yC7Ge7yN4I1_gQYalQ4MYKddz2qMwsmQb-P80,1337
|
|
77
76
|
pycodex/utils/random_ids.py,sha256=jLTxZZ0MBbRdQOFDFWGVRjWzIo6FKSQ5ADXbQSLPp1s,379
|
|
78
|
-
pycodex/utils/session_persist.py,sha256=
|
|
77
|
+
pycodex/utils/session_persist.py,sha256=pKAJZqQ4gnxUc1pzGC3PjBJUZD8eeclqvxk5IWXJ3cs,27289
|
|
79
78
|
pycodex/utils/truncation.py,sha256=5D6vAorT8EgXgjd7ASwtraAyIZdlCR-w5jKZqJxDzho,5446
|
|
80
79
|
pycodex/utils/_aes_gcm/LICENSE,sha256=KvdHagY3sYIK34GldJaRi34sNnGwMVJIIoDhbC9MRu8,2805
|
|
81
80
|
pycodex/utils/_aes_gcm/README.md,sha256=D9XGeewCgU2PVqJCO6WusUs0Mw2FbMnABCqaVktqCnE,3045
|
|
@@ -85,22 +84,23 @@ pycodex/utils/_aes_gcm/gcm.py,sha256=Z8fYHpnLpqJyTPmjDOZtryqe0xmT_93vscNuJe5ZY-w
|
|
|
85
84
|
pycodex/utils/_aes_gcm/source-sha256.json,sha256=g9uPWXuA0D9Wq9EGnduyiVanTgi8QWRFW-9T69FG-fc,215
|
|
86
85
|
responses_server/__init__.py,sha256=SOMO5Q-Z0dpaRih8yWuKUTclPl28tpy_XIPTOO_BPq0,447
|
|
87
86
|
responses_server/__main__.py,sha256=876xZTqhPeDBD2GKKr8pKX33Af8PhFTosAI8bYQsFgQ,61
|
|
88
|
-
responses_server/app.py,sha256=
|
|
89
|
-
responses_server/config.py,sha256=
|
|
90
|
-
responses_server/
|
|
87
|
+
responses_server/app.py,sha256=658hV1uR51RZzTe8B5xPvaasWZdjJo5rxg2GJFnxOLk,8495
|
|
88
|
+
responses_server/config.py,sha256=HxoZYNkpDwZgbIxgVgoR_8BYTRV8V2e1dCddUTo1FyQ,2663
|
|
89
|
+
responses_server/gemini_api.py,sha256=XUG05JhCPqxpDYSQ1m7-KCCturqIIow8ZMgDRZYMk14,15589
|
|
90
|
+
responses_server/messages_api.py,sha256=3Mr0v0ieN18PpEgmR48DnP6PrCDk980cAckZG-2HKrM,19274
|
|
91
91
|
responses_server/payload_processors.py,sha256=jIiGCHGFootnkwPpZiM1-Zv1WKziHlklSWGo050hBgU,3567
|
|
92
|
-
responses_server/request_translation.py,sha256=
|
|
92
|
+
responses_server/request_translation.py,sha256=9TYgGv4GV2M7m6TGXV-pqIpIFTn8v_ZBKULUq3nvaB8,18754
|
|
93
93
|
responses_server/server.py,sha256=hFSRU3GjYa9cI1YSDxKvg2gLjp3OJ_Mh_JFyau3yzl4,2535
|
|
94
94
|
responses_server/session_store.py,sha256=nwGxQL6nUVKnDVeDHz8IzqNQ7dWk8Ybf0URekOHdxTo,1133
|
|
95
95
|
responses_server/stream_events.py,sha256=nWi5VcssytFD3SDHW5hQik5LR-US-Clk-5EEWAi3t-k,4984
|
|
96
|
-
responses_server/stream_router.py,sha256=
|
|
96
|
+
responses_server/stream_router.py,sha256=iDcgNUtcd1inotuWMLfaK7gZfEyCnc403_SKvrcpxaw,15037
|
|
97
97
|
responses_server/trajectory_dump.py,sha256=5_Wy6tslszrqx3j1JeP_boyTbzIicKy8No84PzMnmHk,5111
|
|
98
|
-
responses_server/transport.py,sha256=
|
|
98
|
+
responses_server/transport.py,sha256=363GeSws0VS-yL7pPjtMvjEp7rZ60FdMJ5oD0vPPIWU,16519
|
|
99
99
|
responses_server/tools/__init__.py,sha256=ivsBSEy0SBUhY-Uea5v1XMLXShkwHdCVl0id-1FwdZg,150
|
|
100
100
|
responses_server/tools/custom_adapter.py,sha256=leYNJKp4Ute0JWNfGpe-ntSyFqRgYCdWF4dMYQFINlk,8615
|
|
101
|
-
responses_server/tools/web_search.py,sha256=
|
|
102
|
-
python_codex-0.6.
|
|
103
|
-
python_codex-0.6.
|
|
104
|
-
python_codex-0.6.
|
|
105
|
-
python_codex-0.6.
|
|
106
|
-
python_codex-0.6.
|
|
101
|
+
responses_server/tools/web_search.py,sha256=JyF6NHCS0SqoBrO18jX8cgBsiV4OYQBPz0Ky4u3juSQ,9059
|
|
102
|
+
python_codex-0.6.1.dist-info/METADATA,sha256=c6tqvH3gR_x8Cax-kzRRds6sZ4FRSSUC8rfv28Cm1f0,6855
|
|
103
|
+
python_codex-0.6.1.dist-info/WHEEL,sha256=KGYbc1zXlYddvwxnNty23BeaKzh7YuoSIvIMO4jEhvw,87
|
|
104
|
+
python_codex-0.6.1.dist-info/entry_points.txt,sha256=sNUVakoVuTrzJH505ZgRTQxmtRRPUHV_EH0i6EbYTyM,45
|
|
105
|
+
python_codex-0.6.1.dist-info/licenses/LICENSE,sha256=0X8ifk312hYAORM4hlzg8wVSEXYKNmiPgWlB1YIy2Nw,10926
|
|
106
|
+
python_codex-0.6.1.dist-info/RECORD,,
|
responses_server/app.py
CHANGED
|
@@ -33,8 +33,10 @@ async def _http_exception_response(request: "Request", exc: "HTTPException"):
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
def _format_sse_event(
|
|
36
|
-
event_name: "str", payload: "typing.Dict[str, object]"
|
|
36
|
+
event_name: "typing.Union[str, None]", payload: "typing.Dict[str, object]"
|
|
37
37
|
) -> "bytes":
|
|
38
|
+
if event_name is None:
|
|
39
|
+
return b": keep-alive\n\n"
|
|
38
40
|
data = json.dumps(payload, ensure_ascii=False)
|
|
39
41
|
return f"event: {event_name}\ndata: {data}\n\n".encode("utf-8")
|
|
40
42
|
|
responses_server/config.py
CHANGED
|
@@ -32,6 +32,11 @@ class CompatServerConfig:
|
|
|
32
32
|
base = self.outcomming_base_url.rstrip("/")
|
|
33
33
|
return f"{base}/messages"
|
|
34
34
|
|
|
35
|
+
def outcomming_gemini_url(self, model: "str") -> "str":
|
|
36
|
+
base = self.outcomming_base_url.rstrip("/")
|
|
37
|
+
model_path = urllib.parse.quote(model, safe="")
|
|
38
|
+
return f"{base}/models/{model_path}:streamGenerateContent?alt=sse"
|
|
39
|
+
|
|
35
40
|
def outcomming_models_url(self) -> "str":
|
|
36
41
|
base = self.outcomming_base_url.rstrip("/")
|
|
37
42
|
return f"{base}/models"
|
|
@@ -53,7 +58,7 @@ class CompatServerConfig:
|
|
|
53
58
|
raise ValueError(f"invalid outcomming base url: {outcomming_base_url}")
|
|
54
59
|
normalized_path = parsed.path.rstrip("/")
|
|
55
60
|
if normalized_path in {"", "/"}:
|
|
56
|
-
parsed = parsed._replace(path="/v1")
|
|
61
|
+
parsed = parsed._replace(path="/v1beta" if outcomming_api == "gemini" else "/v1")
|
|
57
62
|
outcomming_base_url = urllib.parse.urlunparse(parsed)
|
|
58
63
|
else:
|
|
59
64
|
outcomming_base_url = urllib.parse.urlunparse(
|