python-codex 0.5.2__py3-none-any.whl → 0.6.0__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 +18 -8
- pycodex/agent.py +89 -74
- pycodex/bootstrap.py +27 -239
- pycodex/cli.py +19 -14
- pycodex/config.py +36 -0
- pycodex/context.py +369 -51
- pycodex/events.py +13 -186
- pycodex/model.py +181 -88
- 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 +1069 -140
- pycodex/protocol.py +78 -15
- pycodex/runtime.py +370 -226
- pycodex/tools/__init__.py +100 -7
- 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 +2 -181
- pycodex/tools/code_mode_manager.py +165 -246
- pycodex/tools/exec_command_tool.py +0 -18
- pycodex/tools/exec_runtime.py +381 -0
- pycodex/tools/exec_tool.py +78 -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/request_user_input_tool.py +3 -23
- 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 +2 -2
- pycodex/utils/doctor.py +5 -4
- pycodex/utils/event_helpers.py +143 -13
- pycodex/utils/history.py +33 -0
- pycodex/utils/session_persist.py +78 -36
- {python_codex-0.5.2.dist-info → python_codex-0.6.0.dist-info}/METADATA +117 -27
- python_codex-0.6.0.dist-info/RECORD +106 -0
- responses_server/__init__.py +2 -0
- responses_server/app.py +7 -6
- responses_server/config.py +0 -11
- responses_server/payload_processors.py +4 -4
- responses_server/request_translation.py +480 -0
- responses_server/server.py +31 -24
- responses_server/stream_events.py +124 -0
- responses_server/stream_router.py +33 -830
- responses_server/tools/custom_adapter.py +29 -29
- responses_server/transport.py +332 -0
- pycodex/tools/close_agent_tool.py +0 -60
- pycodex/tools/exec_runtime.js +0 -161
- pycodex/tools/resume_agent_tool.py +0 -54
- pycodex/tools/send_input_tool.py +0 -110
- python_codex-0.5.2.dist-info/RECORD +0 -90
- {python_codex-0.5.2.dist-info → python_codex-0.6.0.dist-info}/WHEEL +0 -0
- {python_codex-0.5.2.dist-info → python_codex-0.6.0.dist-info}/entry_points.txt +0 -0
- {python_codex-0.5.2.dist-info → python_codex-0.6.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,83 +1,47 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Create a named task using the MultiAgentV2 contract."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
- Corresponds to the original Codex `spawn_agent` collaboration tool.
|
|
5
|
-
|
|
6
|
-
Expected behavior:
|
|
7
|
-
- Spawn a sibling agent runtime that can work in parallel with the caller.
|
|
8
|
-
- Optionally seed the spawned agent with the current thread history.
|
|
9
|
-
- Return the new agent identifier plus any user-facing nickname.
|
|
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
|
-
SPAWN_AGENT_OUTPUT_SCHEMA = {
|
|
20
|
-
"type": "object",
|
|
21
|
-
"properties": {
|
|
22
|
-
"agent_id": {
|
|
23
|
-
"type": "string",
|
|
24
|
-
"description": "Thread identifier for the spawned agent.",
|
|
25
|
-
},
|
|
26
|
-
"nickname": {
|
|
27
|
-
"type": ["string", "null"],
|
|
28
|
-
"description": "User-facing nickname for the spawned agent when available.",
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
"required": ["agent_id", "nickname"],
|
|
32
|
-
"additionalProperties": False,
|
|
33
|
-
}
|
|
3
|
+
from .base_tool import BaseTool
|
|
34
4
|
|
|
35
5
|
|
|
36
6
|
class SpawnAgentTool(BaseTool):
|
|
37
7
|
name = "spawn_agent"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
### Parallel delegation patterns
|
|
62
|
-
- Run multiple independent information-seeking subtasks in parallel when you have distinct questions that can be answered independently.
|
|
63
|
-
- Split implementation into disjoint codebase slices and spawn multiple agents in parallel when the write scopes do not overlap.
|
|
64
|
-
- Delegate verification only when it can run in parallel with ongoing implementation and is likely to catch a concrete risk before final integration.
|
|
65
|
-
- The key is to find opportunities to spawn multiple independent subtasks in parallel within the same round, while ensuring each subtask is well-defined, self-contained, and materially advances the main task."""
|
|
8
|
+
namespace = "collaboration"
|
|
9
|
+
namespace_description = "Tools for spawning and managing sub-agents."
|
|
10
|
+
exec_only = False
|
|
11
|
+
description = (
|
|
12
|
+
'Spawns an agent to work on the specified task. If your current task is '
|
|
13
|
+
'`/root/task1` and you spawn_agent with task_name "task_3" the agent will '
|
|
14
|
+
'have canonical task name `/root/task1/task_3`.\n'
|
|
15
|
+
'You are then able to refer to this agent as `task_3` or '
|
|
16
|
+
'`/root/task1/task_3` interchangeably. However an agent `/root/task2/task_3` '
|
|
17
|
+
'would only be able to communicate with this agent via its canonical '
|
|
18
|
+
'name `/root/task1/task_3`.\n'
|
|
19
|
+
'The spawned agent will have the same tools as you and the ability to '
|
|
20
|
+
'spawn its own subagents.\n\n'
|
|
21
|
+
'It will be able to send you and other running agents messages, and its '
|
|
22
|
+
'final answer will be provided to you when it finishes.\n'
|
|
23
|
+
'The new agent\'s canonical task name will be provided to it along with '
|
|
24
|
+
'the message.\n\n'
|
|
25
|
+
'Note that passing `fork_turns="none"` will not pass any surrounding '
|
|
26
|
+
'context to the spawned subagent, which may cause the agent to lack the '
|
|
27
|
+
'context it needs to complete its task, whereas `fork_turns="all"` will '
|
|
28
|
+
'provide the subagent with all surrounding context.'
|
|
29
|
+
)
|
|
66
30
|
input_schema = {
|
|
67
31
|
"type": "object",
|
|
68
32
|
"properties": {
|
|
69
|
-
"
|
|
33
|
+
"task_name": {
|
|
70
34
|
"type": "string",
|
|
71
|
-
"description": "
|
|
35
|
+
"description": "Task name for the new agent. Use lowercase letters, digits, and underscores.",
|
|
72
36
|
},
|
|
73
|
-
"
|
|
74
|
-
"agent_type": {
|
|
37
|
+
"message": {
|
|
75
38
|
"type": "string",
|
|
76
|
-
"
|
|
39
|
+
"encrypted": True,
|
|
40
|
+
"description": "Initial plain-text task for the new agent.",
|
|
77
41
|
},
|
|
78
|
-
"
|
|
79
|
-
"type": "
|
|
80
|
-
"description": "
|
|
42
|
+
"fork_turns": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"description": "Parent history to inherit. Defaults to `all`; use `none` to start without parent history. Only `all` and `none` are supported.",
|
|
81
45
|
},
|
|
82
46
|
"model": {
|
|
83
47
|
"type": "string",
|
|
@@ -88,35 +52,32 @@ Do not spawn sub-agents unless the user explicitly asks for sub-agents, delegati
|
|
|
88
52
|
"description": "Reasoning effort override for the new agent. Omit to inherit the parent effort.",
|
|
89
53
|
},
|
|
90
54
|
},
|
|
55
|
+
"required": ["task_name", "message"],
|
|
56
|
+
"additionalProperties": False,
|
|
57
|
+
}
|
|
58
|
+
output_schema = {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"properties": {
|
|
61
|
+
"task_name": {
|
|
62
|
+
"type": "string",
|
|
63
|
+
"description": "Canonical task name for the spawned agent.",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
"required": ["task_name"],
|
|
91
67
|
"additionalProperties": False,
|
|
92
68
|
}
|
|
93
|
-
output_schema = SPAWN_AGENT_OUTPUT_SCHEMA
|
|
94
69
|
supports_parallel = False
|
|
95
70
|
|
|
96
|
-
def __init__(self,
|
|
97
|
-
self.
|
|
71
|
+
def __init__(self, agents_manager):
|
|
72
|
+
self._agents_manager = agents_manager
|
|
98
73
|
|
|
99
|
-
async def run(self, context
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
message=message,
|
|
108
|
-
items=items,
|
|
109
|
-
agent_type=self._optional_string(args, "agent_type"),
|
|
110
|
-
fork_context=bool(args.get("fork_context", False)),
|
|
111
|
-
model=self._optional_string(args, "model"),
|
|
112
|
-
reasoning_effort=self._optional_string(args, "reasoning_effort"),
|
|
113
|
-
history=context.history,
|
|
74
|
+
async def run(self, context, args):
|
|
75
|
+
if set(args) - set(self.input_schema["properties"]):
|
|
76
|
+
raise ValueError("unexpected spawn_agent argument")
|
|
77
|
+
agent_path = await self._agents_manager.spawn_agent(
|
|
78
|
+
context.agent_id, args["task_name"], args["message"],
|
|
79
|
+
args.get("fork_turns", "all"),
|
|
80
|
+
args.get("model"), args.get("reasoning_effort"), context.history,
|
|
81
|
+
encrypted=context.encrypted_messages,
|
|
114
82
|
)
|
|
115
|
-
|
|
116
|
-
def _optional_string(
|
|
117
|
-
self, args: "JSONDict", key: "str"
|
|
118
|
-
) -> "typing.Union[str, None]":
|
|
119
|
-
value = args.get(key)
|
|
120
|
-
if value in (None, ""):
|
|
121
|
-
return None
|
|
122
|
-
return str(value)
|
|
83
|
+
return {"task_name": agent_path}
|
|
@@ -8,7 +8,7 @@ Expected behavior:
|
|
|
8
8
|
- Start a long-lived command session for `exec_command`.
|
|
9
9
|
- Preserve unread output between calls.
|
|
10
10
|
- Accept follow-up stdin writes and polling through `write_stdin`.
|
|
11
|
-
- Return
|
|
11
|
+
- Return structured results for nested exec tool calls.
|
|
12
12
|
"""
|
|
13
13
|
|
|
14
14
|
import asyncio
|
|
@@ -22,6 +22,7 @@ from pathlib import Path
|
|
|
22
22
|
|
|
23
23
|
from loguru import logger
|
|
24
24
|
|
|
25
|
+
from ..protocol import JSONValue
|
|
25
26
|
from ..utils.compat import shlex_join, stream_writer_is_closing
|
|
26
27
|
from ..utils.truncation import (
|
|
27
28
|
DEFAULT_MAX_OUTPUT_TOKENS,
|
|
@@ -128,13 +129,14 @@ class UnifiedExecManager:
|
|
|
128
129
|
self._state_listener = None
|
|
129
130
|
self._notify_hook: "typing.Union[typing.Callable[[typing.Dict[str, object]], typing.Awaitable[typing.Any]], None]" = (None)
|
|
130
131
|
|
|
131
|
-
def
|
|
132
|
-
self
|
|
133
|
-
|
|
134
|
-
) -> "None":
|
|
135
|
-
self._notify_hook = callback
|
|
132
|
+
def bind(self, invoke, output, state_changed):
|
|
133
|
+
self._notify_hook = invoke
|
|
134
|
+
self._state_listener = state_changed
|
|
136
135
|
|
|
137
|
-
def
|
|
136
|
+
def shutdown(self):
|
|
137
|
+
self._notify_hook = None
|
|
138
|
+
|
|
139
|
+
def background_work_count(self, after_reply) -> "int":
|
|
138
140
|
return sum(
|
|
139
141
|
1
|
|
140
142
|
for session in self._sessions.values()
|
|
@@ -148,7 +150,7 @@ class UnifiedExecManager:
|
|
|
148
150
|
if self._state_listener is not None:
|
|
149
151
|
self._state_listener()
|
|
150
152
|
|
|
151
|
-
def
|
|
153
|
+
def background_work_snapshot(self):
|
|
152
154
|
with self._lock:
|
|
153
155
|
return [
|
|
154
156
|
{
|
|
@@ -173,7 +175,7 @@ class UnifiedExecManager:
|
|
|
173
175
|
tty: "bool" = DEFAULT_TTY,
|
|
174
176
|
yield_time_ms: "int" = DEFAULT_EXEC_YIELD_TIME_MS,
|
|
175
177
|
max_output_tokens: "typing.Union[int, None]" = None,
|
|
176
|
-
) -> "
|
|
178
|
+
) -> "JSONValue":
|
|
177
179
|
session_id = await self._allocate_session_id()
|
|
178
180
|
command = self._build_shell_command(cmd, shell, login)
|
|
179
181
|
logger.debug(
|
|
@@ -222,14 +224,12 @@ class UnifiedExecManager:
|
|
|
222
224
|
chars: "str" = "",
|
|
223
225
|
yield_time_ms: "int" = DEFAULT_WRITE_STDIN_YIELD_TIME_MS,
|
|
224
226
|
max_output_tokens: "typing.Union[int, None]" = None,
|
|
225
|
-
) -> "
|
|
227
|
+
) -> "JSONValue":
|
|
226
228
|
session = await self._get_session(session_id)
|
|
227
229
|
if session is None:
|
|
228
230
|
return f"Error: session_id {session_id} is not running."
|
|
229
231
|
|
|
230
232
|
if chars:
|
|
231
|
-
if session.process.stdin is None:
|
|
232
|
-
return f"Error: session_id {session_id} does not accept stdin."
|
|
233
233
|
logger.debug(
|
|
234
234
|
"write_stdin session_id={} chars_len={}", session_id, len(chars)
|
|
235
235
|
)
|
|
@@ -261,7 +261,7 @@ class UnifiedExecManager:
|
|
|
261
261
|
session_id: "int",
|
|
262
262
|
yield_time_ms: "int",
|
|
263
263
|
max_output_tokens: "typing.Union[int, None]",
|
|
264
|
-
) -> "
|
|
264
|
+
) -> "JSONValue":
|
|
265
265
|
session = await self._get_session(session_id)
|
|
266
266
|
if session is None:
|
|
267
267
|
return f"Error: session_id {session_id} is not running."
|
|
@@ -298,26 +298,24 @@ class UnifiedExecManager:
|
|
|
298
298
|
wall_time = asyncio.get_running_loop().time() - start_wait
|
|
299
299
|
output_bytes = session.unread_output.drain_bytes()
|
|
300
300
|
output_text = output_bytes.decode("utf-8", errors="replace")
|
|
301
|
-
original_token_count =
|
|
301
|
+
original_token_count = approx_token_count(output_text)
|
|
302
302
|
output_text = self._truncate_output(output_text, max_output_tokens)
|
|
303
303
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
304
|
+
result = {
|
|
305
|
+
"chunk_id": uuid.uuid4().hex[:6],
|
|
306
|
+
"wall_time_seconds": wall_time,
|
|
307
|
+
"output": output_text,
|
|
308
|
+
}
|
|
308
309
|
if session.process.returncode is None:
|
|
309
|
-
|
|
310
|
+
result["session_id"] = session_id
|
|
310
311
|
else:
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
lines.append(f"Original token count: {original_token_count}")
|
|
314
|
-
lines.append("Output:")
|
|
315
|
-
lines.append(output_text)
|
|
312
|
+
result["exit_code"] = session.process.returncode
|
|
313
|
+
result["original_token_count"] = original_token_count
|
|
316
314
|
|
|
317
315
|
if session.process.returncode is not None:
|
|
318
316
|
await self._close_session(session_id)
|
|
319
317
|
|
|
320
|
-
return
|
|
318
|
+
return result
|
|
321
319
|
|
|
322
320
|
async def _close_session(self, session_id: "int") -> "None":
|
|
323
321
|
with self._lock:
|
|
@@ -325,9 +323,7 @@ class UnifiedExecManager:
|
|
|
325
323
|
if session is None:
|
|
326
324
|
return
|
|
327
325
|
self._state_changed()
|
|
328
|
-
if session.process.stdin
|
|
329
|
-
session.process.stdin
|
|
330
|
-
):
|
|
326
|
+
if not stream_writer_is_closing(session.process.stdin):
|
|
331
327
|
session.process.stdin.close()
|
|
332
328
|
|
|
333
329
|
async def _drain_reader_after_exit(
|
|
@@ -356,8 +352,6 @@ class UnifiedExecManager:
|
|
|
356
352
|
|
|
357
353
|
async def _pump_output(self, session: "UnifiedExecSession") -> "None":
|
|
358
354
|
stream = session.process.stdout
|
|
359
|
-
if stream is None:
|
|
360
|
-
return
|
|
361
355
|
while True:
|
|
362
356
|
chunk = await stream.read(4096)
|
|
363
357
|
if not chunk:
|
|
@@ -367,7 +361,7 @@ class UnifiedExecManager:
|
|
|
367
361
|
session.output_event.set()
|
|
368
362
|
|
|
369
363
|
async def _notify_when_session_completes(self, session_id: "int") -> "None":
|
|
370
|
-
from ..
|
|
364
|
+
from ..runtime import SubmissionInterrupted
|
|
371
365
|
|
|
372
366
|
if self._notify_hook is None:
|
|
373
367
|
return
|
|
@@ -391,7 +385,7 @@ class UnifiedExecManager:
|
|
|
391
385
|
"command": session.command_display,
|
|
392
386
|
}
|
|
393
387
|
)
|
|
394
|
-
except
|
|
388
|
+
except SubmissionInterrupted:
|
|
395
389
|
# A steer ended the notification turn at a safe boundary.
|
|
396
390
|
return
|
|
397
391
|
except asyncio.CancelledError:
|
|
@@ -418,9 +412,6 @@ class UnifiedExecManager:
|
|
|
418
412
|
return [shell_path, "-NoProfile", "-Command", cmd]
|
|
419
413
|
return [shell_path, "-lc" if login else "-c", cmd]
|
|
420
414
|
|
|
421
|
-
def _estimate_token_count(self, output: "str") -> "typing.Union[int, None]":
|
|
422
|
-
return approx_token_count(output)
|
|
423
|
-
|
|
424
415
|
def _truncate_output(
|
|
425
416
|
self, output: "str", max_output_tokens: "typing.Union[int, None]"
|
|
426
417
|
) -> "str":
|
pycodex/tools/wait_agent_tool.py
CHANGED
|
@@ -1,85 +1,65 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Wait for MultiAgentV2 mailbox activity without consuming messages."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
- Corresponds to the original Codex `wait_agent` collaboration tool.
|
|
5
|
-
|
|
6
|
-
Expected behavior:
|
|
7
|
-
- Wait for one or more spawned agents to reach a final state.
|
|
8
|
-
- Return the current status map for requested agents, or an empty map when the
|
|
9
|
-
wait times out.
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
from ..protocol import JSONDict, JSONValue
|
|
13
|
-
from .agent_tool_schemas import AGENT_STATUS_SCHEMA
|
|
14
|
-
from .base_tool import BaseTool, ToolContext
|
|
15
|
-
from .runtime_services import SubAgentManager
|
|
16
|
-
|
|
17
|
-
DEFAULT_WAIT_AGENT_TIMEOUT_MS = 30_000
|
|
18
|
-
MIN_WAIT_AGENT_TIMEOUT_MS = 10_000
|
|
19
|
-
MAX_WAIT_AGENT_TIMEOUT_MS = 3_600_000
|
|
20
|
-
|
|
21
|
-
WAIT_AGENT_OUTPUT_SCHEMA = {
|
|
22
|
-
"type": "object",
|
|
23
|
-
"properties": {
|
|
24
|
-
"status": {
|
|
25
|
-
"type": "object",
|
|
26
|
-
"description": "Final statuses keyed by agent id.",
|
|
27
|
-
"additionalProperties": AGENT_STATUS_SCHEMA,
|
|
28
|
-
},
|
|
29
|
-
"timed_out": {
|
|
30
|
-
"type": "boolean",
|
|
31
|
-
"description": "Whether the wait call returned due to timeout before any agent reached a final status.",
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
"required": ["status", "timed_out"],
|
|
35
|
-
"additionalProperties": False,
|
|
36
|
-
}
|
|
3
|
+
from .base_tool import BaseTool
|
|
37
4
|
|
|
38
5
|
|
|
39
6
|
class WaitAgentTool(BaseTool):
|
|
40
7
|
name = "wait_agent"
|
|
8
|
+
namespace = "collaboration"
|
|
9
|
+
namespace_description = "Tools for spawning and managing sub-agents."
|
|
10
|
+
exec_only = False
|
|
41
11
|
description = (
|
|
42
|
-
"Wait for
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
12
|
+
"Wait for a mailbox update from any live agent, including queued messages "
|
|
13
|
+
"and final-status notifications. The wait also ends early when new user "
|
|
14
|
+
"input is steered into the active turn. Does not return the content; "
|
|
15
|
+
"returns either a summary of which agents have updates (if any), an "
|
|
16
|
+
"interruption summary for steered input, or a timeout summary if no "
|
|
17
|
+
"activity arrives before the deadline."
|
|
46
18
|
)
|
|
47
19
|
input_schema = {
|
|
48
20
|
"type": "object",
|
|
49
21
|
"properties": {
|
|
50
|
-
"ids": {
|
|
51
|
-
"type": "array",
|
|
52
|
-
"items": {"type": "string"},
|
|
53
|
-
"description": "Agent ids to wait on. Pass multiple ids to wait for whichever finishes first.",
|
|
54
|
-
},
|
|
55
22
|
"timeout_ms": {
|
|
56
|
-
"type": "
|
|
57
|
-
"description": "Timeout in milliseconds. Defaults to 30000, min 10000, max 3600000.
|
|
23
|
+
"type": "number",
|
|
24
|
+
"description": "Timeout in milliseconds. Defaults to 30000, min 10000, max 3600000.",
|
|
58
25
|
},
|
|
59
26
|
},
|
|
60
|
-
"required": ["ids"],
|
|
61
27
|
"additionalProperties": False,
|
|
62
28
|
}
|
|
63
|
-
output_schema =
|
|
29
|
+
output_schema = {
|
|
30
|
+
"type": "object",
|
|
31
|
+
"properties": {
|
|
32
|
+
"message": {
|
|
33
|
+
"type": "string",
|
|
34
|
+
"description": "Brief wait summary without the agent's final content, including any timeout adjustment.",
|
|
35
|
+
},
|
|
36
|
+
"timed_out": {
|
|
37
|
+
"type": "boolean",
|
|
38
|
+
"description": "Whether the wait call returned because no mailbox update arrived before the timeout.",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
"required": ["message", "timed_out"],
|
|
42
|
+
"additionalProperties": False,
|
|
43
|
+
}
|
|
64
44
|
supports_parallel = False
|
|
65
45
|
|
|
66
|
-
def __init__(self,
|
|
67
|
-
self.
|
|
68
|
-
|
|
69
|
-
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
70
|
-
del context
|
|
71
|
-
ids = args.get("ids")
|
|
72
|
-
if not isinstance(ids, list) or not ids:
|
|
73
|
-
return "Error: `ids` must be a non-empty list."
|
|
74
|
-
agent_ids = [str(item).strip() for item in ids if str(item).strip()]
|
|
75
|
-
if not agent_ids:
|
|
76
|
-
return "Error: `ids` must include at least one non-empty id."
|
|
77
|
-
timeout_ms = self._timeout_ms(args)
|
|
78
|
-
return await self._subagent_manager.wait_agents(agent_ids, timeout_ms)
|
|
46
|
+
def __init__(self, agents_manager):
|
|
47
|
+
self._agents_manager = agents_manager
|
|
79
48
|
|
|
80
|
-
def
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
49
|
+
async def run(self, context, args):
|
|
50
|
+
if set(args) - {"timeout_ms"}:
|
|
51
|
+
raise ValueError("unexpected wait_agent argument")
|
|
52
|
+
requested = args.get("timeout_ms", 30000)
|
|
53
|
+
if type(requested) is not int:
|
|
54
|
+
raise ValueError("timeout_ms must be an integer")
|
|
55
|
+
if requested > 3600000:
|
|
56
|
+
raise ValueError("timeout_ms must be at most 3600000")
|
|
57
|
+
timeout = max(requested, 10000)
|
|
58
|
+
result = await self._agents_manager.wait_for_activity(
|
|
59
|
+
context.agent_id, timeout,
|
|
85
60
|
)
|
|
61
|
+
if requested < timeout:
|
|
62
|
+
result["message"] += (
|
|
63
|
+
"\n\nRequested timeout of {0}ms was clamped to the minimum of {1}ms."
|
|
64
|
+
).format(requested, timeout)
|
|
65
|
+
return result
|
pycodex/tools/wait_tool.py
CHANGED
|
@@ -9,7 +9,7 @@ Expected behavior:
|
|
|
9
9
|
- Return only the new output since the previous `exec` / `wait` snapshot.
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
|
-
import
|
|
12
|
+
import math
|
|
13
13
|
|
|
14
14
|
from ..protocol import JSONDict, JSONValue
|
|
15
15
|
from .base_tool import BaseTool, ToolContext
|
|
@@ -18,6 +18,7 @@ from .code_mode_manager import DEFAULT_WAIT_YIELD_TIME_MS, CodeModeManager
|
|
|
18
18
|
|
|
19
19
|
class WaitTool(BaseTool):
|
|
20
20
|
name = "wait"
|
|
21
|
+
exec_only = False
|
|
21
22
|
description = (
|
|
22
23
|
"Waits on a yielded `exec` cell and returns new output or completion.\n"
|
|
23
24
|
"- Use `wait` only after `exec` returns `Script running with cell ID ...`.\n"
|
|
@@ -43,11 +44,11 @@ class WaitTool(BaseTool):
|
|
|
43
44
|
"description": "Identifier of the running exec cell.",
|
|
44
45
|
},
|
|
45
46
|
"yield_time_ms": {
|
|
46
|
-
"type": "
|
|
47
|
+
"type": "number",
|
|
47
48
|
"description": "Wait before yielding more output. Defaults to 10000 ms.",
|
|
48
49
|
},
|
|
49
50
|
"max_tokens": {
|
|
50
|
-
"type": "
|
|
51
|
+
"type": "number",
|
|
51
52
|
"description": "Output token budget for this wait call. Defaults to 10000 tokens.",
|
|
52
53
|
},
|
|
53
54
|
"terminate": {
|
|
@@ -65,18 +66,28 @@ class WaitTool(BaseTool):
|
|
|
65
66
|
|
|
66
67
|
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
67
68
|
del context
|
|
68
|
-
cell_id =
|
|
69
|
-
if not cell_id:
|
|
70
|
-
|
|
69
|
+
cell_id = args["cell_id"]
|
|
70
|
+
if not isinstance(cell_id, str) or not cell_id:
|
|
71
|
+
raise ValueError("cell_id must be a non-empty string")
|
|
72
|
+
terminate = args.get("terminate", False)
|
|
73
|
+
if not isinstance(terminate, bool):
|
|
74
|
+
raise ValueError("terminate must be a boolean")
|
|
71
75
|
return await self._manager.wait(
|
|
72
76
|
cell_id=cell_id,
|
|
73
|
-
yield_time_ms=
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
yield_time_ms=self._integer(
|
|
78
|
+
args.get("yield_time_ms", DEFAULT_WAIT_YIELD_TIME_MS), "yield_time_ms",
|
|
79
|
+
),
|
|
80
|
+
max_tokens=(
|
|
81
|
+
self._integer(args["max_tokens"], "max_tokens")
|
|
82
|
+
if "max_tokens" in args else None
|
|
83
|
+
),
|
|
84
|
+
terminate=terminate,
|
|
76
85
|
)
|
|
77
86
|
|
|
78
|
-
def
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
def _integer(self, value, name):
|
|
88
|
+
if (
|
|
89
|
+
isinstance(value, bool) or not isinstance(value, (int, float))
|
|
90
|
+
or not math.isfinite(value) or int(value) != value or value < 0
|
|
91
|
+
):
|
|
92
|
+
raise ValueError(name + " must be a non-negative integer")
|
|
82
93
|
return int(value)
|
pycodex/tools/web_search_tool.py
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import typing
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
import tomllib
|
|
7
|
+
except ModuleNotFoundError: # pragma: no cover - Python 3.10 and earlier
|
|
8
|
+
import tomli as tomllib
|
|
9
|
+
|
|
10
|
+
DEFAULT_CODEX_CONFIG_PATH = Path.home() / ".codex" / "config.toml"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def read_codex_config(
|
|
14
|
+
config_path: "typing.Union[str, Path]",
|
|
15
|
+
profile: "typing.Union[str, None]" = None,
|
|
16
|
+
use_default_profile: "bool" = True,
|
|
17
|
+
) -> "typing.Dict[str, typing.Any]":
|
|
18
|
+
"""Merge a file or embedded profile over the base config.
|
|
19
|
+
|
|
20
|
+
Disable default selection when restoring an explicitly saved base config.
|
|
21
|
+
The returned ``profile`` is the effective name, or None for the base config.
|
|
22
|
+
"""
|
|
23
|
+
path = Path(config_path)
|
|
24
|
+
selected = _read_config_file(path)
|
|
25
|
+
profiles = _embedded_profiles(selected, path)
|
|
26
|
+
if profile is None and use_default_profile:
|
|
27
|
+
profile = selected.get("profile")
|
|
28
|
+
if profile is not None:
|
|
29
|
+
_validate_profile_name(profile)
|
|
30
|
+
profile_path = path.parent / (profile + ".config.toml")
|
|
31
|
+
if profile in profiles:
|
|
32
|
+
if profile_path.exists():
|
|
33
|
+
_raise_profile_conflict(path, profile)
|
|
34
|
+
overrides = profiles[profile]
|
|
35
|
+
else:
|
|
36
|
+
overrides = _read_config_file(profile_path)
|
|
37
|
+
if "profile" in overrides:
|
|
38
|
+
raise ValueError(
|
|
39
|
+
"Codex profile {!r} cannot select another profile".format(profile)
|
|
40
|
+
)
|
|
41
|
+
selected = _merge_config(selected, overrides)
|
|
42
|
+
selected["profile"] = profile
|
|
43
|
+
return selected
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def list_codex_profiles(
|
|
47
|
+
config_path: "typing.Union[str, Path]",
|
|
48
|
+
) -> "typing.List[str]":
|
|
49
|
+
"""List file and embedded profiles, rejecting ambiguous names."""
|
|
50
|
+
config_path = Path(config_path)
|
|
51
|
+
profiles = _embedded_profiles(_read_config_file(config_path), config_path)
|
|
52
|
+
suffix = ".config.toml"
|
|
53
|
+
files = {
|
|
54
|
+
path.name[:-len(suffix)]
|
|
55
|
+
for path in config_path.parent.glob("*" + suffix)
|
|
56
|
+
if path.is_file()
|
|
57
|
+
and re.fullmatch(r"[A-Za-z0-9_-]+", path.name[:-len(suffix)])
|
|
58
|
+
}
|
|
59
|
+
for profile in sorted(files.intersection(profiles)):
|
|
60
|
+
_raise_profile_conflict(config_path, profile)
|
|
61
|
+
return sorted(files.union(profiles))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _validate_profile_name(profile: "str") -> "None":
|
|
65
|
+
if not isinstance(profile, str) or not re.fullmatch(r"[A-Za-z0-9_-]+", profile):
|
|
66
|
+
raise ValueError("invalid Codex profile name: {!r}".format(profile))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _embedded_profiles(
|
|
70
|
+
data: "typing.Dict[str, typing.Any]", path: "Path",
|
|
71
|
+
) -> "typing.Dict[str, typing.Any]":
|
|
72
|
+
profiles = data.get("profiles", {})
|
|
73
|
+
if not isinstance(profiles, dict):
|
|
74
|
+
raise ValueError("profiles in {} must be a table".format(path))
|
|
75
|
+
for name, settings in profiles.items():
|
|
76
|
+
_validate_profile_name(name)
|
|
77
|
+
if not isinstance(settings, dict):
|
|
78
|
+
raise ValueError("[profiles.{}] in {} must be a table".format(name, path))
|
|
79
|
+
return profiles
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _raise_profile_conflict(path: "Path", profile: "str") -> "None":
|
|
83
|
+
raise ValueError(
|
|
84
|
+
"ambiguous Codex profile {0!r}: both [profiles.{0}] in {1} and {2} "
|
|
85
|
+
"exist; keep only one definition".format(
|
|
86
|
+
profile, path, path.parent / (profile + ".config.toml"),
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _merge_config(
|
|
92
|
+
base: "typing.Dict[str, typing.Any]",
|
|
93
|
+
overrides: "typing.Dict[str, typing.Any]",
|
|
94
|
+
) -> "typing.Dict[str, typing.Any]":
|
|
95
|
+
merged = dict(base)
|
|
96
|
+
for key, value in overrides.items():
|
|
97
|
+
if isinstance(merged.get(key), dict) and isinstance(value, dict):
|
|
98
|
+
merged[key] = _merge_config(merged[key], value)
|
|
99
|
+
else:
|
|
100
|
+
merged[key] = value
|
|
101
|
+
return merged
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _read_config_file(path: "Path") -> "typing.Dict[str, typing.Any]":
|
|
105
|
+
return tomllib.loads(path.read_text(encoding="utf-8"))
|