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/clock_tool.py
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Periodic wake-ups and interruptible waits in the clock namespace.
|
|
2
2
|
|
|
3
|
-
`
|
|
4
|
-
clock starts counting only after the Agent completes a reply. Every new turn
|
|
5
|
-
cancels the pending countdown, and every successful reply starts a fresh one.
|
|
3
|
+
`set_period` is a local extension; `sleep` follows upstream's active-turn wait.
|
|
6
4
|
"""
|
|
7
5
|
|
|
8
|
-
import
|
|
9
|
-
import math
|
|
10
|
-
import typing
|
|
11
|
-
from datetime import datetime, timedelta
|
|
6
|
+
from time import monotonic
|
|
12
7
|
|
|
13
|
-
from ..events import (
|
|
14
|
-
CompactCompletedEvent,
|
|
15
|
-
CompactStartedEvent,
|
|
16
|
-
Event,
|
|
17
|
-
TurnCompletedEvent,
|
|
18
|
-
TurnStartedEvent,
|
|
19
|
-
)
|
|
20
8
|
from ..protocol import JSONDict, JSONValue
|
|
21
9
|
from .base_tool import BaseTool, ToolContext
|
|
10
|
+
from .clock_manager import ClockManager
|
|
22
11
|
|
|
23
12
|
CLOCK_STATE_OUTPUT_SCHEMA = {
|
|
24
13
|
"type": "object",
|
|
@@ -40,155 +29,11 @@ CLOCK_STATE_OUTPUT_SCHEMA = {
|
|
|
40
29
|
}
|
|
41
30
|
|
|
42
31
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def __init__(self, seconds_per_minute: "float" = 60.0) -> "None":
|
|
49
|
-
self._seconds_per_minute = seconds_per_minute
|
|
50
|
-
self._period_m: "typing.Union[float, None]" = None
|
|
51
|
-
self._timer_task: "typing.Union[asyncio.Task, None]" = None
|
|
52
|
-
self._generation = 0
|
|
53
|
-
self._next_fire_at = None
|
|
54
|
-
self._state_listener = None
|
|
55
|
-
self._notify_hook: "typing.Union[typing.Callable[[typing.Dict[str, object]], typing.Awaitable[typing.Any]], None]" = (None)
|
|
56
|
-
|
|
57
|
-
def set_notify_hook(
|
|
58
|
-
self,
|
|
59
|
-
callback: "typing.Union[typing.Callable[[typing.Dict[str, object]], typing.Awaitable[typing.Any]], None]",
|
|
60
|
-
) -> "None":
|
|
61
|
-
self._notify_hook = callback
|
|
62
|
-
|
|
63
|
-
def set_state_listener(self, listener):
|
|
64
|
-
self._state_listener = listener
|
|
65
|
-
|
|
66
|
-
def _state_changed(self):
|
|
67
|
-
if self._state_listener is not None:
|
|
68
|
-
self._state_listener()
|
|
69
|
-
|
|
70
|
-
def background_snapshot(self):
|
|
71
|
-
return dict(
|
|
72
|
-
self.snapshot(),
|
|
73
|
-
kind="clock",
|
|
74
|
-
next_fire_at=self._next_fire_at,
|
|
75
|
-
status=(
|
|
76
|
-
"armed" if self._next_fire_at is not None
|
|
77
|
-
else "paused" if self.enabled else "disabled"
|
|
78
|
-
),
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
def set_period(self, value: "object") -> "JSONDict":
|
|
82
|
-
self._cancel_pending()
|
|
83
|
-
if value is None:
|
|
84
|
-
self._period_m = None
|
|
85
|
-
self._state_changed()
|
|
86
|
-
return self.snapshot()
|
|
87
|
-
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
88
|
-
raise ValueError("period_m must be a positive finite number or null")
|
|
89
|
-
period_m = float(value)
|
|
90
|
-
if not math.isfinite(period_m) or period_m <= 0:
|
|
91
|
-
raise ValueError("period_m must be a positive finite number or null")
|
|
92
|
-
self._period_m = period_m
|
|
93
|
-
self._state_changed()
|
|
94
|
-
return self.snapshot()
|
|
95
|
-
|
|
96
|
-
def snapshot(self) -> "JSONDict":
|
|
97
|
-
return {
|
|
98
|
-
"enabled": self.enabled,
|
|
99
|
-
"period_m": self._period_m,
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
@property
|
|
103
|
-
def enabled(self) -> "bool":
|
|
104
|
-
return self._period_m is not None
|
|
105
|
-
|
|
106
|
-
@property
|
|
107
|
-
def armed(self) -> "bool":
|
|
108
|
-
return self._next_fire_at is not None
|
|
109
|
-
|
|
110
|
-
def turn_started(self) -> "None":
|
|
111
|
-
self._cancel_pending()
|
|
112
|
-
self._state_changed()
|
|
113
|
-
|
|
114
|
-
def arm_after_reply(self) -> "None":
|
|
115
|
-
self._cancel_pending()
|
|
116
|
-
period_m = self._period_m
|
|
117
|
-
if period_m is None or self._notify_hook is None:
|
|
118
|
-
self._state_changed()
|
|
119
|
-
return
|
|
120
|
-
generation = self._generation
|
|
121
|
-
self._next_fire_at = (
|
|
122
|
-
datetime.now().astimezone()
|
|
123
|
-
+ timedelta(seconds=period_m * self._seconds_per_minute)
|
|
124
|
-
).isoformat()
|
|
125
|
-
self._timer_task = asyncio.create_task(
|
|
126
|
-
self._wait_and_notify(generation, period_m)
|
|
127
|
-
)
|
|
128
|
-
self._timer_task.add_done_callback(
|
|
129
|
-
lambda task: None if task.cancelled() else task.exception()
|
|
130
|
-
)
|
|
131
|
-
self._state_changed()
|
|
132
|
-
|
|
133
|
-
def cancel(self) -> "None":
|
|
134
|
-
self.set_period(None)
|
|
135
|
-
|
|
136
|
-
def _cancel_pending(self) -> "None":
|
|
137
|
-
self._generation += 1
|
|
138
|
-
task = self._timer_task
|
|
139
|
-
self._timer_task = None
|
|
140
|
-
self._next_fire_at = None
|
|
141
|
-
if task is not None and not task.done():
|
|
142
|
-
task.cancel()
|
|
143
|
-
|
|
144
|
-
async def _wait_and_notify(self, generation: "int", period_m: "float") -> "None":
|
|
145
|
-
from ..agent import TurnInterrupted
|
|
146
|
-
|
|
147
|
-
try:
|
|
148
|
-
await asyncio.sleep(period_m * self._seconds_per_minute)
|
|
149
|
-
except asyncio.CancelledError:
|
|
150
|
-
return
|
|
151
|
-
if generation != self._generation or period_m != self._period_m:
|
|
152
|
-
return
|
|
153
|
-
|
|
154
|
-
self._timer_task = None
|
|
155
|
-
self._next_fire_at = None
|
|
156
|
-
self._state_changed()
|
|
157
|
-
callback = self._notify_hook
|
|
158
|
-
if callback is None:
|
|
159
|
-
return
|
|
160
|
-
try:
|
|
161
|
-
started = await callback(
|
|
162
|
-
{
|
|
163
|
-
"type": "clock_tick",
|
|
164
|
-
"period_m": period_m,
|
|
165
|
-
"current_time": _current_time(),
|
|
166
|
-
}
|
|
167
|
-
)
|
|
168
|
-
except TurnInterrupted:
|
|
169
|
-
# The next successful reply will arm the clock after a steer.
|
|
170
|
-
return
|
|
171
|
-
except asyncio.CancelledError:
|
|
172
|
-
raise
|
|
173
|
-
except Exception as exc:
|
|
174
|
-
asyncio.get_running_loop().call_exception_handler(
|
|
175
|
-
{
|
|
176
|
-
"message": "Clock notification failed",
|
|
177
|
-
"exception": exc,
|
|
178
|
-
}
|
|
179
|
-
)
|
|
180
|
-
return
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
not started
|
|
184
|
-
and generation == self._generation
|
|
185
|
-
and period_m == self._period_m
|
|
186
|
-
):
|
|
187
|
-
self.arm_after_reply()
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
class ClockTool(BaseTool):
|
|
191
|
-
name = "clock"
|
|
32
|
+
class SetPeriodTool(BaseTool):
|
|
33
|
+
exec_only = False
|
|
34
|
+
name = "set_period"
|
|
35
|
+
namespace = "clock"
|
|
36
|
+
namespace_description = "Tools for waiting on time and scheduling periodic wake-ups."
|
|
192
37
|
description = (
|
|
193
38
|
"Set a periodic clock in minutes, or cancel it with null. After each "
|
|
194
39
|
"Agent reply, the clock waits period_m minutes and then sends a "
|
|
@@ -214,30 +59,56 @@ class ClockTool(BaseTool):
|
|
|
214
59
|
def __init__(self, manager: "ClockManager") -> "None":
|
|
215
60
|
self._manager = manager
|
|
216
61
|
|
|
217
|
-
def bind_agent(self, agent) -> "None":
|
|
218
|
-
self._manager.set_notify_hook(agent.maybe_invoke)
|
|
219
|
-
|
|
220
|
-
def handle_agent_event(self, event: "Event") -> "None":
|
|
221
|
-
if isinstance(event, (TurnStartedEvent, CompactStartedEvent)):
|
|
222
|
-
self._manager.turn_started()
|
|
223
|
-
elif isinstance(event, (TurnCompletedEvent, CompactCompletedEvent)):
|
|
224
|
-
self._manager.arm_after_reply()
|
|
225
|
-
|
|
226
|
-
def shutdown(self) -> "None":
|
|
227
|
-
self._manager.cancel()
|
|
228
|
-
self._manager.set_notify_hook(None)
|
|
229
|
-
|
|
230
|
-
def background_work_count(self, after_reply: "bool") -> "int":
|
|
231
|
-
return int(self._manager.enabled if after_reply else self._manager.armed)
|
|
232
|
-
|
|
233
|
-
def background_work_snapshot(self):
|
|
234
|
-
return [self._manager.background_snapshot()]
|
|
235
|
-
|
|
236
|
-
def set_state_listener(self, listener):
|
|
237
|
-
self._manager.set_state_listener(listener)
|
|
238
|
-
|
|
239
62
|
async def run(self, context: "ToolContext", args: "JSONDict") -> "JSONValue":
|
|
240
63
|
del context
|
|
241
64
|
if not isinstance(args, dict) or "period_m" not in args:
|
|
242
|
-
raise ValueError("
|
|
65
|
+
raise ValueError("set_period requires period_m")
|
|
243
66
|
return self._manager.set_period(args["period_m"])
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class SleepTool(BaseTool):
|
|
70
|
+
name = "sleep"
|
|
71
|
+
namespace = "clock"
|
|
72
|
+
namespace_description = "Tools for waiting on time and scheduling periodic wake-ups."
|
|
73
|
+
exec_only = False
|
|
74
|
+
description = (
|
|
75
|
+
"Pause execution for a specified duration. The sleep ends early when new "
|
|
76
|
+
"input arrives for the active turn. Returns the elapsed wall-clock time."
|
|
77
|
+
)
|
|
78
|
+
input_schema = {
|
|
79
|
+
"type": "object",
|
|
80
|
+
"properties": {
|
|
81
|
+
"duration_ms": {
|
|
82
|
+
"type": "number",
|
|
83
|
+
"description": (
|
|
84
|
+
"How long to sleep in milliseconds. Must be between 1 and 43200000."
|
|
85
|
+
),
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
"required": ["duration_ms"],
|
|
89
|
+
"additionalProperties": False,
|
|
90
|
+
}
|
|
91
|
+
supports_parallel = False
|
|
92
|
+
|
|
93
|
+
def __init__(self, agents_manager):
|
|
94
|
+
self._agents_manager = agents_manager
|
|
95
|
+
|
|
96
|
+
async def run(self, context, args):
|
|
97
|
+
if set(args) != {"duration_ms"}:
|
|
98
|
+
raise ValueError("sleep requires only duration_ms")
|
|
99
|
+
duration_ms = args["duration_ms"]
|
|
100
|
+
if type(duration_ms) is not int:
|
|
101
|
+
raise ValueError("duration_ms must be an integer")
|
|
102
|
+
if not 1 <= duration_ms <= 43200000:
|
|
103
|
+
raise ValueError("duration_ms must be between 1 and 43200000")
|
|
104
|
+
started = monotonic()
|
|
105
|
+
result = await self._agents_manager.wait_for_activity(
|
|
106
|
+
context.agent_id, duration_ms,
|
|
107
|
+
)
|
|
108
|
+
message = (
|
|
109
|
+
"Sleep completed." if result["timed_out"]
|
|
110
|
+
else "Sleep interrupted by new input."
|
|
111
|
+
)
|
|
112
|
+
return "Wall time: {0:.4f} seconds\n{1}".format(
|
|
113
|
+
monotonic() - started, message,
|
|
114
|
+
)
|