python-codex 0.5.2__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 +22 -12
- pycodex/agent.py +89 -74
- pycodex/bootstrap.py +37 -251
- pycodex/cli.py +19 -28
- pycodex/config.py +36 -0
- pycodex/context.py +368 -51
- pycodex/events.py +14 -187
- pycodex/model.py +197 -95
- pycodex/multi_agents.py +401 -0
- pycodex/portable/__init__.py +8 -6
- pycodex/portable/client.py +24 -39
- pycodex/portable/protocol.py +28 -0
- pycodex/portable/server.py +3 -3
- pycodex/prompts/developer_messages.json +13 -0
- pycodex/prompts/goals/budget_limit.md +19 -0
- pycodex/prompts/goals/continuation.md +56 -0
- pycodex/prompts/models.json +1143 -200
- pycodex/protocol.py +81 -15
- pycodex/runtime.py +370 -226
- pycodex/tools/__init__.py +102 -10
- pycodex/tools/agent_tool_schemas.py +0 -38
- pycodex/tools/apply_patch_tool.py +17 -34
- pycodex/tools/base_tool.py +63 -35
- pycodex/tools/clock_manager.py +182 -0
- pycodex/tools/clock_tool.py +58 -187
- pycodex/tools/code_mode_manager.py +165 -246
- pycodex/tools/exec_command_tool.py +0 -18
- pycodex/tools/exec_runtime.py +332 -0
- pycodex/tools/exec_tool.py +74 -31
- pycodex/tools/followup_task_tool.py +44 -0
- pycodex/tools/goal_manager.py +205 -0
- pycodex/tools/goal_tool.py +126 -0
- pycodex/tools/interrupt_agent_tool.py +48 -0
- pycodex/tools/list_agents_tool.py +66 -0
- pycodex/tools/python_tool_schema.py +141 -0
- pycodex/tools/request_permissions_tool.py +1 -0
- pycodex/tools/runtime_services.py +122 -450
- pycodex/tools/send_message_tool.py +42 -0
- pycodex/tools/spawn_agent_tool.py +55 -94
- pycodex/tools/unified_exec_manager.py +26 -35
- pycodex/tools/wait_agent_tool.py +46 -66
- pycodex/tools/wait_tool.py +24 -13
- pycodex/tools/web_search_tool.py +1 -0
- pycodex/utils/codex_config.py +105 -0
- pycodex/utils/compactor.py +4 -0
- pycodex/utils/compat.py +43 -2
- pycodex/utils/doctor.py +5 -4
- pycodex/utils/dotenv.py +0 -7
- pycodex/utils/event_helpers.py +143 -13
- pycodex/utils/history.py +59 -0
- pycodex/utils/session_persist.py +104 -48
- python_codex-0.6.1.dist-info/METADATA +201 -0
- python_codex-0.6.1.dist-info/RECORD +106 -0
- responses_server/__init__.py +2 -0
- responses_server/app.py +10 -7
- responses_server/config.py +6 -12
- responses_server/gemini_api.py +333 -0
- responses_server/messages_api.py +9 -1
- responses_server/payload_processors.py +4 -4
- responses_server/request_translation.py +486 -0
- responses_server/server.py +31 -24
- responses_server/stream_events.py +124 -0
- responses_server/stream_router.py +48 -830
- responses_server/tools/custom_adapter.py +29 -29
- responses_server/tools/web_search.py +3 -0
- responses_server/transport.py +388 -0
- pycodex/tools/close_agent_tool.py +0 -60
- pycodex/tools/exec_runtime.js +0 -161
- pycodex/tools/request_user_input_tool.py +0 -102
- pycodex/tools/resume_agent_tool.py +0 -54
- pycodex/tools/send_input_tool.py +0 -110
- python_codex-0.5.2.dist-info/METADATA +0 -262
- python_codex-0.5.2.dist-info/RECORD +0 -90
- {python_codex-0.5.2.dist-info → python_codex-0.6.1.dist-info}/WHEEL +0 -0
- {python_codex-0.5.2.dist-info → python_codex-0.6.1.dist-info}/entry_points.txt +0 -0
- {python_codex-0.5.2.dist-info → python_codex-0.6.1.dist-info}/licenses/LICENSE +0 -0
pycodex/tools/exec_runtime.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
const readline = require('readline');
|
|
2
|
-
const { stdin, stdout } = require('process');
|
|
3
|
-
|
|
4
|
-
const pending = new Map();
|
|
5
|
-
let storedValues = {};
|
|
6
|
-
let toolMap = {};
|
|
7
|
-
let initialized = false;
|
|
8
|
-
|
|
9
|
-
function send(message) {
|
|
10
|
-
stdout.write(JSON.stringify(message) + '\n');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function stringifyValue(value) {
|
|
14
|
-
if (typeof value === 'string') {
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
if (value === undefined) {
|
|
18
|
-
return 'undefined';
|
|
19
|
-
}
|
|
20
|
-
try {
|
|
21
|
-
return JSON.stringify(value);
|
|
22
|
-
} catch (_error) {
|
|
23
|
-
return String(value);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function text(value) {
|
|
28
|
-
send({ type: 'output_text', text: stringifyValue(value) });
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function notify(value) {
|
|
32
|
-
text(value);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function image(value) {
|
|
36
|
-
if (typeof value === 'string') {
|
|
37
|
-
send({ type: 'output_image', image_url: value, detail: null });
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (value && typeof value === 'object' && typeof value.image_url === 'string') {
|
|
41
|
-
send({
|
|
42
|
-
type: 'output_image',
|
|
43
|
-
image_url: value.image_url,
|
|
44
|
-
detail: value.detail === undefined ? null : value.detail,
|
|
45
|
-
});
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
throw new Error('image(...) expects an image URL string or { image_url, detail? }');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function store(key, value) {
|
|
52
|
-
storedValues[key] = value;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function load(key) {
|
|
56
|
-
return storedValues[key];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function exit() {
|
|
60
|
-
const error = new Error('__CODEX_EXIT__');
|
|
61
|
-
error.__codexExit = true;
|
|
62
|
-
throw error;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async function yield_control() {
|
|
66
|
-
send({ type: 'yield' });
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function createToolCaller(toolName) {
|
|
70
|
-
return function callTool(argumentsValue = {}) {
|
|
71
|
-
const id = `${toolName}_${Math.random().toString(16).slice(2)}`;
|
|
72
|
-
send({
|
|
73
|
-
type: 'tool_call',
|
|
74
|
-
id,
|
|
75
|
-
tool_name: toolName,
|
|
76
|
-
arguments: argumentsValue,
|
|
77
|
-
});
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
pending.set(id, { resolve, reject });
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function initialize(message) {
|
|
85
|
-
storedValues = message.stored_values || {};
|
|
86
|
-
toolMap = {};
|
|
87
|
-
for (const tool of message.tools || []) {
|
|
88
|
-
toolMap[tool.js_name] = createToolCaller(tool.tool_name);
|
|
89
|
-
}
|
|
90
|
-
const allTools = Object.freeze(
|
|
91
|
-
(message.tools || []).map((tool) => ({
|
|
92
|
-
name: tool.js_name,
|
|
93
|
-
description: tool.description,
|
|
94
|
-
}))
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
|
|
98
|
-
const fn = new AsyncFunction(
|
|
99
|
-
'tools',
|
|
100
|
-
'text',
|
|
101
|
-
'image',
|
|
102
|
-
'store',
|
|
103
|
-
'load',
|
|
104
|
-
'exit',
|
|
105
|
-
'notify',
|
|
106
|
-
'ALL_TOOLS',
|
|
107
|
-
'yield_control',
|
|
108
|
-
'console',
|
|
109
|
-
message.source
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
(async () => {
|
|
113
|
-
try {
|
|
114
|
-
await fn(
|
|
115
|
-
toolMap,
|
|
116
|
-
text,
|
|
117
|
-
image,
|
|
118
|
-
store,
|
|
119
|
-
load,
|
|
120
|
-
exit,
|
|
121
|
-
notify,
|
|
122
|
-
allTools,
|
|
123
|
-
yield_control,
|
|
124
|
-
undefined
|
|
125
|
-
);
|
|
126
|
-
send({ type: 'result', stored_values: storedValues, error_text: null });
|
|
127
|
-
} catch (error) {
|
|
128
|
-
if (error && error.__codexExit) {
|
|
129
|
-
send({ type: 'result', stored_values: storedValues, error_text: null });
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const errorText = error && error.stack ? error.stack : String(error);
|
|
133
|
-
send({ type: 'result', stored_values: storedValues, error_text: errorText });
|
|
134
|
-
}
|
|
135
|
-
})();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const rl = readline.createInterface({ input: stdin, crlfDelay: Infinity });
|
|
139
|
-
rl.on('line', (line) => {
|
|
140
|
-
if (!line.trim()) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
const message = JSON.parse(line);
|
|
144
|
-
if (message.type === 'init' && !initialized) {
|
|
145
|
-
initialized = true;
|
|
146
|
-
initialize(message);
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
if (message.type === 'tool_result') {
|
|
150
|
-
const entry = pending.get(message.id);
|
|
151
|
-
if (!entry) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
pending.delete(message.id);
|
|
155
|
-
if (message.ok) {
|
|
156
|
-
entry.resolve(message.result);
|
|
157
|
-
} else {
|
|
158
|
-
entry.reject(new Error(message.error || 'nested tool failed'));
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
});
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"""`request_user_input` tool for the Python Codex prototype.
|
|
2
|
-
|
|
3
|
-
Original Codex mapping:
|
|
4
|
-
- Corresponds to the original Codex `request_user_input` collaboration tool.
|
|
5
|
-
|
|
6
|
-
Expected behavior:
|
|
7
|
-
- Keep the tool declaration available for upstream request compatibility.
|
|
8
|
-
- Return the upstream Default-mode unavailable message without requesting input.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
from ..protocol import JSONDict, JSONValue
|
|
12
|
-
from .base_tool import BaseTool, ToolContext
|
|
13
|
-
|
|
14
|
-
MIN_AUTO_RESOLUTION_MS = 60_000
|
|
15
|
-
MAX_AUTO_RESOLUTION_MS = 240_000
|
|
16
|
-
|
|
17
|
-
REQUEST_USER_INPUT_QUESTION_SCHEMA = {
|
|
18
|
-
"type": "object",
|
|
19
|
-
"properties": {
|
|
20
|
-
"id": {
|
|
21
|
-
"type": "string",
|
|
22
|
-
"description": "Stable identifier for mapping answers (snake_case).",
|
|
23
|
-
},
|
|
24
|
-
"header": {
|
|
25
|
-
"type": "string",
|
|
26
|
-
"description": "Short header label shown in the UI (12 or fewer chars).",
|
|
27
|
-
},
|
|
28
|
-
"question": {
|
|
29
|
-
"type": "string",
|
|
30
|
-
"description": "Single-sentence prompt shown to the user.",
|
|
31
|
-
},
|
|
32
|
-
"options": {
|
|
33
|
-
"type": "array",
|
|
34
|
-
"description": (
|
|
35
|
-
"Provide 2-3 mutually exclusive choices. Put the recommended option "
|
|
36
|
-
'first and suffix its label with "(Recommended)". Do not include '
|
|
37
|
-
'an "Other" option in this list; the client will add a free-form '
|
|
38
|
-
"Other option automatically."
|
|
39
|
-
),
|
|
40
|
-
"items": {
|
|
41
|
-
"type": "object",
|
|
42
|
-
"properties": {
|
|
43
|
-
"label": {
|
|
44
|
-
"type": "string",
|
|
45
|
-
"description": "User-facing label (1-5 words).",
|
|
46
|
-
},
|
|
47
|
-
"description": {
|
|
48
|
-
"type": "string",
|
|
49
|
-
"description": "One short sentence explaining impact/tradeoff if selected.",
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
"required": ["label", "description"],
|
|
53
|
-
"additionalProperties": False,
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
"required": ["id", "header", "question", "options"],
|
|
58
|
-
"additionalProperties": False,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
class RequestUserInputTool(BaseTool):
|
|
63
|
-
name = "request_user_input"
|
|
64
|
-
description = (
|
|
65
|
-
"Request user input for one to three short questions and wait for the "
|
|
66
|
-
f"response. Set autoResolutionMs, from {MIN_AUTO_RESOLUTION_MS} to "
|
|
67
|
-
f"{MAX_AUTO_RESOLUTION_MS} milliseconds, only when the question is "
|
|
68
|
-
"useful but non-blocking and continuing with best judgment is "
|
|
69
|
-
"acceptable if the user does not answer; omit it when explicit user "
|
|
70
|
-
"input is required. This tool is unavailable in Default mode."
|
|
71
|
-
)
|
|
72
|
-
input_schema = {
|
|
73
|
-
"type": "object",
|
|
74
|
-
"properties": {
|
|
75
|
-
"questions": {
|
|
76
|
-
"type": "array",
|
|
77
|
-
"description": "Questions to show the user. Prefer 1 and do not exceed 3",
|
|
78
|
-
"items": REQUEST_USER_INPUT_QUESTION_SCHEMA,
|
|
79
|
-
},
|
|
80
|
-
"autoResolutionMs": {
|
|
81
|
-
"type": "number",
|
|
82
|
-
"description": (
|
|
83
|
-
"Optional auto-resolution window in milliseconds, from "
|
|
84
|
-
f"{MIN_AUTO_RESOLUTION_MS} to {MAX_AUTO_RESOLUTION_MS}. "
|
|
85
|
-
"Include this only when the question is useful but "
|
|
86
|
-
"non-blocking and continuing with best judgment is "
|
|
87
|
-
"acceptable if the user does not answer; omit it when "
|
|
88
|
-
"explicit user input is required before continuing. Use "
|
|
89
|
-
f"{MIN_AUTO_RESOLUTION_MS} for lightly helpful context and "
|
|
90
|
-
f"up to {MAX_AUTO_RESOLUTION_MS} when the answer would "
|
|
91
|
-
"materially unblock better work."
|
|
92
|
-
),
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
"required": ["questions"],
|
|
96
|
-
"additionalProperties": False,
|
|
97
|
-
}
|
|
98
|
-
supports_parallel = False
|
|
99
|
-
|
|
100
|
-
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
101
|
-
del context, args
|
|
102
|
-
return "request_user_input is unavailable in Default mode"
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"""`resume_agent` tool for the Python Codex prototype.
|
|
2
|
-
|
|
3
|
-
Original Codex mapping:
|
|
4
|
-
- Corresponds to the original Codex `resume_agent` collaboration tool.
|
|
5
|
-
|
|
6
|
-
Expected behavior:
|
|
7
|
-
- Restart a previously closed local sub-agent runtime.
|
|
8
|
-
- Return the agent's current status payload.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
from ..protocol import JSONDict, JSONValue
|
|
12
|
-
from .agent_tool_schemas import AGENT_STATUS_SCHEMA
|
|
13
|
-
from .base_tool import BaseTool, ToolContext
|
|
14
|
-
from .runtime_services import SubAgentManager
|
|
15
|
-
|
|
16
|
-
RESUME_AGENT_OUTPUT_SCHEMA = {
|
|
17
|
-
"type": "object",
|
|
18
|
-
"properties": {
|
|
19
|
-
"status": AGENT_STATUS_SCHEMA,
|
|
20
|
-
},
|
|
21
|
-
"required": ["status"],
|
|
22
|
-
"additionalProperties": False,
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
class ResumeAgentTool(BaseTool):
|
|
27
|
-
name = "resume_agent"
|
|
28
|
-
description = (
|
|
29
|
-
"Resume a previously closed agent by id so it can receive send_input "
|
|
30
|
-
"and wait_agent calls."
|
|
31
|
-
)
|
|
32
|
-
input_schema = {
|
|
33
|
-
"type": "object",
|
|
34
|
-
"properties": {
|
|
35
|
-
"id": {
|
|
36
|
-
"type": "string",
|
|
37
|
-
"description": "Agent id to resume.",
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"required": ["id"],
|
|
41
|
-
"additionalProperties": False,
|
|
42
|
-
}
|
|
43
|
-
output_schema = RESUME_AGENT_OUTPUT_SCHEMA
|
|
44
|
-
supports_parallel = False
|
|
45
|
-
|
|
46
|
-
def __init__(self, subagent_manager: "SubAgentManager") -> "None":
|
|
47
|
-
self._subagent_manager = subagent_manager
|
|
48
|
-
|
|
49
|
-
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
50
|
-
del context
|
|
51
|
-
agent_id = str(args.get("id", "")).strip()
|
|
52
|
-
if not agent_id:
|
|
53
|
-
return "Error: `id` is required."
|
|
54
|
-
return await self._subagent_manager.resume_agent(agent_id)
|
pycodex/tools/send_input_tool.py
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"""`send_input` tool for the Python Codex prototype.
|
|
2
|
-
|
|
3
|
-
Original Codex mapping:
|
|
4
|
-
- Corresponds to the original Codex `send_input` collaboration tool.
|
|
5
|
-
|
|
6
|
-
Expected behavior:
|
|
7
|
-
- Send a follow-up message to an existing spawned agent.
|
|
8
|
-
- Optionally interrupt the agent's current turn before queueing the new input.
|
|
9
|
-
- Return the submission id for the queued input.
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
import typing
|
|
13
|
-
|
|
14
|
-
from ..protocol import JSONDict, JSONValue
|
|
15
|
-
from .agent_tool_schemas import COLLAB_INPUT_ITEMS_SCHEMA
|
|
16
|
-
from .base_tool import BaseTool, ToolContext
|
|
17
|
-
from .runtime_services import SubAgentManager
|
|
18
|
-
|
|
19
|
-
SEND_INPUT_OUTPUT_SCHEMA = {
|
|
20
|
-
"type": "object",
|
|
21
|
-
"properties": {
|
|
22
|
-
"submission_id": {
|
|
23
|
-
"type": "string",
|
|
24
|
-
"description": "Identifier for the queued input submission.",
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"required": ["submission_id"],
|
|
28
|
-
"additionalProperties": False,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class SendInputTool(BaseTool):
|
|
33
|
-
name = "send_input"
|
|
34
|
-
description = (
|
|
35
|
-
"Send a message to an existing agent. Use interrupt=true to redirect "
|
|
36
|
-
"work immediately. You should reuse the agent by send_input if you "
|
|
37
|
-
"believe your assigned task is highly dependent on the context of a "
|
|
38
|
-
"previous task."
|
|
39
|
-
)
|
|
40
|
-
input_schema = {
|
|
41
|
-
"type": "object",
|
|
42
|
-
"properties": {
|
|
43
|
-
"id": {
|
|
44
|
-
"type": "string",
|
|
45
|
-
"description": "Agent id to message (from spawn_agent).",
|
|
46
|
-
},
|
|
47
|
-
"message": {
|
|
48
|
-
"type": "string",
|
|
49
|
-
"description": "Legacy plain-text message to send to the agent. Use either message or items.",
|
|
50
|
-
},
|
|
51
|
-
"items": COLLAB_INPUT_ITEMS_SCHEMA,
|
|
52
|
-
"interrupt": {
|
|
53
|
-
"type": "boolean",
|
|
54
|
-
"description": "True interrupts the current task and handles this message immediately; false or omitted queues it.",
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
"required": ["id"],
|
|
58
|
-
"additionalProperties": False,
|
|
59
|
-
}
|
|
60
|
-
output_schema = SEND_INPUT_OUTPUT_SCHEMA
|
|
61
|
-
supports_parallel = False
|
|
62
|
-
|
|
63
|
-
def __init__(self, subagent_manager: "SubAgentManager") -> "None":
|
|
64
|
-
self._subagent_manager = subagent_manager
|
|
65
|
-
|
|
66
|
-
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
67
|
-
del context
|
|
68
|
-
agent_id = str(args.get("id", "")).strip()
|
|
69
|
-
if not agent_id:
|
|
70
|
-
return "Error: `id` is required."
|
|
71
|
-
items = args.get("items")
|
|
72
|
-
if items is not None and not isinstance(items, list):
|
|
73
|
-
return "Error: `items` must be a list when provided."
|
|
74
|
-
prompt_text = self._compose_prompt(
|
|
75
|
-
self._optional_string(args, "message"),
|
|
76
|
-
items,
|
|
77
|
-
)
|
|
78
|
-
if not prompt_text:
|
|
79
|
-
return "Error: `message` or `items` is required."
|
|
80
|
-
return await self._subagent_manager.send_input(
|
|
81
|
-
agent_id,
|
|
82
|
-
prompt_text,
|
|
83
|
-
interrupt=bool(args.get("interrupt", False)),
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
def _compose_prompt(
|
|
87
|
-
self,
|
|
88
|
-
message: "typing.Union[str, None]",
|
|
89
|
-
items: "typing.Union[typing.List[typing.Dict[str, object]], None]",
|
|
90
|
-
) -> "str":
|
|
91
|
-
parts: "typing.List[str]" = []
|
|
92
|
-
if message:
|
|
93
|
-
parts.append(message.strip())
|
|
94
|
-
for item in items or []:
|
|
95
|
-
item_type = str(item.get("type", ""))
|
|
96
|
-
if item_type == "text":
|
|
97
|
-
text = str(item.get("text", "")).strip()
|
|
98
|
-
if text:
|
|
99
|
-
parts.append(text)
|
|
100
|
-
else:
|
|
101
|
-
parts.append(str(item))
|
|
102
|
-
return "\n\n".join(part for part in parts if part)
|
|
103
|
-
|
|
104
|
-
def _optional_string(
|
|
105
|
-
self, args: "JSONDict", key: "str"
|
|
106
|
-
) -> "typing.Union[str, None]":
|
|
107
|
-
value = args.get(key)
|
|
108
|
-
if value in (None, ""):
|
|
109
|
-
return None
|
|
110
|
-
return str(value)
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: python-codex
|
|
3
|
-
Version: 0.5.2
|
|
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
|
-
Local PDF links open in the browser's built-in reader, with **Open in new tab**
|
|
136
|
-
and **Download** links. PDF files are streamed separately from text snapshots.
|
|
137
|
-
Keep the server's stdin open: EOF initiates shutdown.
|
|
138
|
-
**New session** directly creates a session using the launch `--profile`, or the
|
|
139
|
-
base config when unset. The **+** button on its right (**Custom profile**) lets
|
|
140
|
-
you choose a named profile from the server's Codex config; **Default** uses the
|
|
141
|
-
launch setting. Each session saves its profile name and uses it again on restart; existing sessions
|
|
142
|
-
keep their configuration. Restart the server after adding or removing profiles.
|
|
143
|
-
While the selected Agent is working, the empty composer cycles through usage
|
|
144
|
-
hints about commands, frontend customization, workspace sharing, session collaboration and optional
|
|
145
|
-
features about every six seconds, starting at a random hint. Edit the short
|
|
146
|
-
strings in the `hints` list in `workspace.html`'s `updatePromptPlaceholder` to change the tips.
|
|
147
|
-
The gear button opens **Customize Agent** and **Share**, with a short description of each.
|
|
148
|
-
Customize Agent starts an Agent conversation for editing files in
|
|
149
|
-
`~/.pycodex/frontend/`. Its setup context includes an API quick
|
|
150
|
-
reference and the path to the full API documentation shipped with the package.
|
|
151
|
-
Browse the [feature catalog](packages/pycodex-ws/workspace_server/optional_features/README.md)
|
|
152
|
-
in the view area and tell the Agent which features to adapt. You can also ask it
|
|
153
|
-
to publish a local customization as a GitHub feature PR using the
|
|
154
|
-
[contribution format](packages/pycodex-ws/workspace_server/optional_features/CONTRIBUTING.md).
|
|
155
|
-
The [custom frontend API](docs/WORKSPACE_API.md) provides structured state,
|
|
156
|
-
command results, history pagination, event replay and Agent/background-task
|
|
157
|
-
inventories. Optional `custom.js` extensions can export `mount(host)` to use
|
|
158
|
-
the built-in page's session, draft and viewer controls.
|
|
159
|
-
With a workspace server administrator password configured, **Share** sets a
|
|
160
|
-
separate password for this workspace using the existing login page. Sharing
|
|
161
|
-
passwords and visitor logins live only in memory; changing the password, stopping
|
|
162
|
-
sharing or restarting the process invalidates them. Router login is unchanged.
|
|
163
|
-
|
|
164
|
-
See the [workspace guide](packages/pycodex-ws/README.md) for session controls,
|
|
165
|
-
storage and Feishu setup. The separately distributed
|
|
166
|
-
[VS Code extension](packages/pycodex-ws/extensions/vscode/README.md) uses the same
|
|
167
|
-
workspace backend, including on Remote SSH hosts.
|
|
168
|
-
|
|
169
|
-
## Python API
|
|
170
|
-
|
|
171
|
-
Use the standard model and tool setup from Python:
|
|
172
|
-
|
|
173
|
-
```python
|
|
174
|
-
from pycodex.bootstrap import build_agent, build_model
|
|
175
|
-
|
|
176
|
-
agent = build_agent(build_model())
|
|
177
|
-
try:
|
|
178
|
-
result = agent.ask("Summarize this repository in one sentence.")
|
|
179
|
-
print(result.output_text)
|
|
180
|
-
finally:
|
|
181
|
-
agent.shutdown()
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
For custom integrations, `Agent` accepts a model client, `ToolRegistry` and
|
|
185
|
-
`ContextConfig`. `await agent.run_turn([...])` runs an asynchronous turn;
|
|
186
|
-
`AgentRuntime` adds input queues, commands and frontend subscriptions.
|
|
187
|
-
See [runtime contracts and examples](docs/RUNTIME.md).
|
|
188
|
-
|
|
189
|
-
## Portable configuration
|
|
190
|
-
|
|
191
|
-
Portable mode transfers an encrypted bundle of your configuration, instructions
|
|
192
|
-
and skills to another machine. Start a storage service in a separate terminal:
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
python3 -m pycodex.portable.server --root .tmp/pycodex_storage --host 127.0.0.1 --port 5577
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
Upload the default Codex home, or specify another directory:
|
|
199
|
-
|
|
200
|
-
```bash
|
|
201
|
-
pycodex --put @127.0.0.1:5577
|
|
202
|
-
pycodex --put /path/to/.codex/@127.0.0.1:5577
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
After verifying a download round trip, the command prints a reusable
|
|
206
|
-
`pycodex --call ...` command. Run it on the target machine using a storage
|
|
207
|
-
address reachable from that machine.
|
|
208
|
-
|
|
209
|
-
## Development and reference
|
|
210
|
-
|
|
211
|
-
```bash
|
|
212
|
-
uv sync --dev
|
|
213
|
-
uv run pycodex
|
|
214
|
-
uv run --dev pytest
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
- [Test environments and coverage](tests/TESTS.md)
|
|
218
|
-
- [Repository development rules and reading guide](AGENTS.md)
|
|
219
|
-
- [Architecture, package layout and runtime contracts](docs/RUNTIME.md)
|
|
220
|
-
- [Prompts, configuration and context](docs/CONTEXT.md)
|
|
221
|
-
- [Upstream alignment](docs/ALIGNMENT.md)
|
|
222
|
-
- [Responses compatibility server](docs/responses_server/README.md)
|
|
223
|
-
- [Portable configuration contracts](docs/PORTABLE.md)
|
|
224
|
-
|
|
225
|
-
Release notes and implementation details live in `docs/`.
|
|
226
|
-
|
|
227
|
-
## Markdown view modes
|
|
228
|
-
|
|
229
|
-
Markdown file views include a Show source / Show preview button. Switching
|
|
230
|
-
modes preserves each mode's reading position without replaying a previous line
|
|
231
|
-
target.
|
|
232
|
-
|
|
233
|
-
## Responses reliability
|
|
234
|
-
|
|
235
|
-
Providers can opt into ordered failover with `fallback_models = ["backup-one",
|
|
236
|
-
"backup-two"]` in their `[model_providers.<name>]` table. Each target uses the
|
|
237
|
-
same endpoint, credentials, history and tools; request settings are rebuilt for
|
|
238
|
-
that model, while the session's selected model stays unchanged. Transient errors
|
|
239
|
-
exhaust `stream_max_retries` before advancing. Unavailable-model errors and
|
|
240
|
-
first-data timeouts advance immediately. Existing retry notices name both models
|
|
241
|
-
and include the underlying error.
|
|
242
|
-
|
|
243
|
-
Failed models cool down for 900 seconds within that client. Subsequent requests
|
|
244
|
-
skip them, retry them after expiry, and restart the full chain when every target
|
|
245
|
-
is cooling. `fallback_cooldown_seconds = 0` disables cooldown; explicit model
|
|
246
|
-
selection clears it. New sessions and subagents have independent cooldowns.
|
|
247
|
-
An absent or empty list disables failover. Invalid input, context-length and
|
|
248
|
-
terminal incomplete errors retain their existing handling. Backups must support
|
|
249
|
-
the active tools and context size; compaction keeps the selected model's policy.
|
|
250
|
-
|
|
251
|
-
Responses HTTP 408/429 and server errors use the provider retry budget.
|
|
252
|
-
Rate-limit messages containing `try again in` or `retry after` supply the retry
|
|
253
|
-
delay. CLI and workspace retry notices include the underlying error details;
|
|
254
|
-
workspace notices survive browser refresh and active-turn reattachment, and
|
|
255
|
-
remain separate from the assistant reply. They are not persisted in rollouts.
|
|
256
|
-
|
|
257
|
-
## Workspace input history
|
|
258
|
-
|
|
259
|
-
The workspace message box supports Up/Down input history. Up recalls earlier
|
|
260
|
-
human inputs in the current conversation; Down returns to the unsent draft.
|
|
261
|
-
Automatic wake-up messages are excluded, and arrows retain normal cursor
|
|
262
|
-
movement inside multiline drafts.
|