python-codex 0.6.3__py3-none-any.whl → 0.7.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/agent.py +325 -321
- pycodex/config.py +19 -2
- pycodex/context.py +24 -9
- pycodex/events.py +127 -7
- pycodex/model.py +269 -95
- pycodex/multi_agents.py +13 -42
- pycodex/prompts/models.json +8 -8
- pycodex/protocol.py +4 -2
- pycodex/runtime.py +227 -131
- pycodex/tools/__init__.py +3 -3
- pycodex/tools/clock_manager.py +0 -7
- pycodex/tools/exec_command_tool.py +16 -5
- pycodex/tools/exec_runtime.py +5 -1
- pycodex/tools/goal_manager.py +25 -32
- pycodex/tools/runtime_services.py +21 -11
- pycodex/tools/unified_exec_manager.py +46 -3
- pycodex/utils/compactor.py +28 -101
- pycodex/utils/compat.py +6 -0
- pycodex/utils/event_helpers.py +47 -0
- pycodex/utils/history.py +22 -7
- pycodex/utils/model_metadata.py +5 -4
- pycodex/utils/session_persist.py +6 -2
- {python_codex-0.6.3.dist-info → python_codex-0.7.1.dist-info}/METADATA +40 -13
- {python_codex-0.6.3.dist-info → python_codex-0.7.1.dist-info}/RECORD +30 -30
- responses_server/app.py +4 -0
- responses_server/gemini_api.py +26 -7
- responses_server/transport.py +12 -5
- {python_codex-0.6.3.dist-info → python_codex-0.7.1.dist-info}/WHEEL +0 -0
- {python_codex-0.6.3.dist-info → python_codex-0.7.1.dist-info}/entry_points.txt +0 -0
- {python_codex-0.6.3.dist-info → python_codex-0.7.1.dist-info}/licenses/LICENSE +0 -0
pycodex/config.py
CHANGED
|
@@ -8,6 +8,20 @@ from .context import ContextConfig
|
|
|
8
8
|
from .model import ResponsesProviderConfig
|
|
9
9
|
from .utils.codex_config import DEFAULT_CODEX_CONFIG_PATH, read_codex_config
|
|
10
10
|
from .utils.dotenv import load_codex_dotenv
|
|
11
|
+
from .utils.model_metadata import load_models_by_slug
|
|
12
|
+
|
|
13
|
+
SUPPORTED_HARNESSES = ("pycodex", "pyagy")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _parse_harness(selected: "typing.Mapping[str, typing.Any]") -> "str":
|
|
17
|
+
value = selected.get("harness", "pycodex")
|
|
18
|
+
if not isinstance(value, str) or value not in SUPPORTED_HARNESSES:
|
|
19
|
+
raise ValueError(
|
|
20
|
+
"harness must be one of {0}, got {1!r}".format(
|
|
21
|
+
", ".join(SUPPORTED_HARNESSES), value
|
|
22
|
+
)
|
|
23
|
+
)
|
|
24
|
+
return value
|
|
11
25
|
|
|
12
26
|
|
|
13
27
|
@dataclass(frozen=True)
|
|
@@ -16,6 +30,7 @@ class CodexConfig:
|
|
|
16
30
|
profile: "typing.Union[str, None]"
|
|
17
31
|
provider: "ResponsesProviderConfig"
|
|
18
32
|
context: "ContextConfig"
|
|
33
|
+
harness: "str" = "pycodex"
|
|
19
34
|
|
|
20
35
|
@classmethod
|
|
21
36
|
def from_env(
|
|
@@ -28,9 +43,11 @@ class CodexConfig:
|
|
|
28
43
|
path = Path(config_path)
|
|
29
44
|
load_codex_dotenv(path)
|
|
30
45
|
selected = read_codex_config(path, profile, use_default_profile)
|
|
46
|
+
model_catalog = load_models_by_slug()
|
|
31
47
|
return cls(
|
|
32
48
|
path=path,
|
|
33
49
|
profile=selected["profile"],
|
|
34
|
-
provider=ResponsesProviderConfig.from_mapping(selected),
|
|
35
|
-
context=ContextConfig.from_mapping(selected, path),
|
|
50
|
+
provider=ResponsesProviderConfig.from_mapping(selected, model_catalog),
|
|
51
|
+
context=ContextConfig.from_mapping(selected, path, model_catalog),
|
|
52
|
+
harness=_parse_harness(selected),
|
|
36
53
|
)
|
pycodex/context.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import typing
|
|
3
3
|
from copy import deepcopy
|
|
4
|
-
from dataclasses import dataclass, replace
|
|
4
|
+
from dataclasses import dataclass, field, replace
|
|
5
5
|
from datetime import datetime
|
|
6
6
|
from pathlib import Path
|
|
7
7
|
from uuid import UUID, uuid5
|
|
@@ -89,10 +89,13 @@ class ContextConfig:
|
|
|
89
89
|
code_mode_only: "typing.Union[bool, None]" = None
|
|
90
90
|
include_environment_context: "bool" = True
|
|
91
91
|
model_slug: "typing.Union[str, None]" = None
|
|
92
|
+
model_catalog: "typing.Dict[str, JSONDict]" = field(
|
|
93
|
+
default_factory=load_models_by_slug, repr=False, compare=False,
|
|
94
|
+
)
|
|
92
95
|
|
|
93
96
|
def __post_init__(self):
|
|
94
97
|
if self.model_slug is not None and (
|
|
95
|
-
not isinstance(self.model_slug, str) or self.model_slug not in
|
|
98
|
+
not isinstance(self.model_slug, str) or self.model_slug not in self.model_catalog
|
|
96
99
|
):
|
|
97
100
|
raise ValueError("model_slug must name an entry in models.json")
|
|
98
101
|
if self.code_mode_only is not None and not isinstance(self.code_mode_only, bool):
|
|
@@ -126,6 +129,7 @@ class ContextConfig:
|
|
|
126
129
|
cls,
|
|
127
130
|
selected: "typing.Mapping[str, typing.Any]",
|
|
128
131
|
config_path: "typing.Union[str, Path]",
|
|
132
|
+
model_catalog: "typing.Union[typing.Dict[str, JSONDict], None]" = None,
|
|
129
133
|
) -> "ContextConfig":
|
|
130
134
|
path = Path(config_path)
|
|
131
135
|
model_instructions_file = selected.get("model_instructions_file")
|
|
@@ -152,6 +156,7 @@ class ContextConfig:
|
|
|
152
156
|
|
|
153
157
|
return cls(
|
|
154
158
|
base_instructions=_normalize_text(selected.get("base_instructions")),
|
|
159
|
+
model_catalog=load_models_by_slug() if model_catalog is None else model_catalog,
|
|
155
160
|
developer_instructions=_normalize_text(
|
|
156
161
|
selected.get("developer_instructions")
|
|
157
162
|
),
|
|
@@ -399,11 +404,15 @@ class ContextManager:
|
|
|
399
404
|
def resolve_code_mode_only(self) -> "bool":
|
|
400
405
|
if self.config.code_mode_only is not None:
|
|
401
406
|
return self.config.code_mode_only
|
|
402
|
-
metadata = model_metadata(
|
|
407
|
+
metadata = model_metadata(
|
|
408
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
409
|
+
)
|
|
403
410
|
return metadata is not None and metadata.get("tool_mode") == "code_mode_only"
|
|
404
411
|
|
|
405
412
|
def resolve_model_max_context_window(self) -> "typing.Union[int, None]":
|
|
406
|
-
metadata = model_metadata(
|
|
413
|
+
metadata = model_metadata(
|
|
414
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
415
|
+
)
|
|
407
416
|
context_window = self.config.model_context_window
|
|
408
417
|
if context_window is None and metadata is not None:
|
|
409
418
|
context_window = _normalize_int(metadata.get("context_window"))
|
|
@@ -413,7 +422,9 @@ class ContextManager:
|
|
|
413
422
|
context_window = self.resolve_model_max_context_window()
|
|
414
423
|
if context_window is None:
|
|
415
424
|
return None
|
|
416
|
-
metadata = model_metadata(
|
|
425
|
+
metadata = model_metadata(
|
|
426
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
427
|
+
)
|
|
417
428
|
effective_percent = None
|
|
418
429
|
if metadata is not None:
|
|
419
430
|
effective_percent = _normalize_int(
|
|
@@ -443,7 +454,7 @@ class ContextManager:
|
|
|
443
454
|
model_slug = self.config.model_slug or self.config.model
|
|
444
455
|
if model_slug is None:
|
|
445
456
|
return None
|
|
446
|
-
metadata = model_metadata(model_slug)
|
|
457
|
+
metadata = model_metadata(model_slug, self.config.model_catalog)
|
|
447
458
|
if metadata is None:
|
|
448
459
|
return None
|
|
449
460
|
return _normalize_int(metadata.get("auto_compact_token_limit"))
|
|
@@ -452,7 +463,7 @@ class ContextManager:
|
|
|
452
463
|
model_slug = self.config.model_slug or self.config.model
|
|
453
464
|
if model_slug is None:
|
|
454
465
|
return None
|
|
455
|
-
metadata = model_metadata(model_slug)
|
|
466
|
+
metadata = model_metadata(model_slug, self.config.model_catalog)
|
|
456
467
|
if metadata is None:
|
|
457
468
|
return None
|
|
458
469
|
|
|
@@ -499,7 +510,9 @@ class ContextManager:
|
|
|
499
510
|
)
|
|
500
511
|
|
|
501
512
|
def _model_message(self, *keys):
|
|
502
|
-
metadata = model_metadata(
|
|
513
|
+
metadata = model_metadata(
|
|
514
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
515
|
+
)
|
|
503
516
|
value = metadata.get("model_messages") if metadata is not None else None
|
|
504
517
|
for key in keys:
|
|
505
518
|
if value is None:
|
|
@@ -636,7 +649,9 @@ class ContextManager:
|
|
|
636
649
|
skill.path_to_skill_md.relative_to(skill.root).as_posix(),
|
|
637
650
|
)
|
|
638
651
|
lines.append(f"- {skill.name}: {skill.description} (file: {path_str})")
|
|
639
|
-
metadata = model_metadata(
|
|
652
|
+
metadata = model_metadata(
|
|
653
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
654
|
+
)
|
|
640
655
|
if metadata is None or metadata.get("include_skills_usage_instructions", True):
|
|
641
656
|
lines.append("### How to use skills")
|
|
642
657
|
lines.extend(SKILLS_GUIDANCE.splitlines())
|
pycodex/events.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Task-scoped notifications, typed events and frontend presentation."""
|
|
2
2
|
|
|
3
|
+
import asyncio
|
|
3
4
|
import json
|
|
5
|
+
import logging
|
|
6
|
+
import threading
|
|
4
7
|
import typing
|
|
8
|
+
from contextlib import contextmanager
|
|
9
|
+
from copy import deepcopy
|
|
5
10
|
from dataclasses import dataclass
|
|
6
11
|
from typing import ClassVar
|
|
7
12
|
|
|
8
|
-
from .protocol import
|
|
13
|
+
from .protocol import (
|
|
14
|
+
AgentMessage, ConversationItem, JSONDict, ToolCall, ToolResult, UserMessage,
|
|
15
|
+
)
|
|
16
|
+
from .utils.compat import ContextVar, copy_context
|
|
9
17
|
from .utils.event_helpers import (
|
|
10
18
|
colorize_cli_message,
|
|
11
19
|
colorize_tool_message,
|
|
@@ -15,6 +23,7 @@ from .utils.event_helpers import (
|
|
|
15
23
|
percent_of_context_window_remaining,
|
|
16
24
|
shorten_title,
|
|
17
25
|
summarize_tool_result,
|
|
26
|
+
visible_input_text,
|
|
18
27
|
)
|
|
19
28
|
|
|
20
29
|
DEFAULT_MAIN_PROMPT = "pycodex> "
|
|
@@ -128,6 +137,10 @@ class Event:
|
|
|
128
137
|
pass
|
|
129
138
|
|
|
130
139
|
|
|
140
|
+
EventHandler = typing.Callable[[Event], None]
|
|
141
|
+
BASE_EVENT_HANDLER: "EventHandler" = lambda _event: None
|
|
142
|
+
|
|
143
|
+
|
|
131
144
|
class TurnEvent(Event):
|
|
132
145
|
turn_id: "str"
|
|
133
146
|
submission_id: "typing.Union[str, None]"
|
|
@@ -152,10 +165,7 @@ class TurnStartedEvent(TurnEvent):
|
|
|
152
165
|
kind: ClassVar[str] = "turn_started"
|
|
153
166
|
|
|
154
167
|
def visualize(self) -> "str":
|
|
155
|
-
return
|
|
156
|
-
message.text for message in self.messages
|
|
157
|
-
if isinstance(message, (UserMessage, AgentMessage)) and message.visible
|
|
158
|
-
)
|
|
168
|
+
return visible_input_text(self.messages)
|
|
159
169
|
|
|
160
170
|
def render(self, display: "EventDisplay") -> "None":
|
|
161
171
|
display.begin_turn(self)
|
|
@@ -163,7 +173,7 @@ class TurnStartedEvent(TurnEvent):
|
|
|
163
173
|
|
|
164
174
|
@dataclass(frozen=True)
|
|
165
175
|
class TurnInputReadyEvent(TurnEvent):
|
|
166
|
-
"""
|
|
176
|
+
"""Pending input has been delivered for the next sample."""
|
|
167
177
|
|
|
168
178
|
turn_id: "str"
|
|
169
179
|
submission_id: "typing.Union[str, None]" = None
|
|
@@ -258,6 +268,17 @@ class AssistantDeltaEvent(ModelEvent):
|
|
|
258
268
|
display.set_status("talking")
|
|
259
269
|
|
|
260
270
|
|
|
271
|
+
@dataclass(frozen=True)
|
|
272
|
+
class ReasoningEvent(ModelEvent):
|
|
273
|
+
text: "str"
|
|
274
|
+
turn_id: "str" = ""
|
|
275
|
+
submission_id: "typing.Union[str, None]" = None
|
|
276
|
+
kind: ClassVar[str] = "reasoning"
|
|
277
|
+
|
|
278
|
+
def visualize(self) -> "str":
|
|
279
|
+
return self.text
|
|
280
|
+
|
|
281
|
+
|
|
261
282
|
@dataclass(frozen=True)
|
|
262
283
|
class TokenCountEvent(ModelEvent):
|
|
263
284
|
usage: "typing.Union[JSONDict, None]"
|
|
@@ -725,3 +746,102 @@ class InputResolvedEvent(Event):
|
|
|
725
746
|
|
|
726
747
|
def render(self, display: "EventDisplay") -> "None":
|
|
727
748
|
display.set_prompt(display.main_prompt())
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
class TurnInterrupted(RuntimeError):
|
|
752
|
+
def __init__(self):
|
|
753
|
+
super().__init__("turn interrupted")
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
class ModelRequestInterrupted(RuntimeError):
|
|
757
|
+
"""A cooperative stop ended retries after an issued request finished."""
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
class EventScope:
|
|
761
|
+
"""Task-scoped observer binding, isolation and worker-thread forwarding."""
|
|
762
|
+
|
|
763
|
+
_current_observer = ContextVar("pycodex_event_observer", default=BASE_EVENT_HANDLER)
|
|
764
|
+
|
|
765
|
+
@staticmethod
|
|
766
|
+
def router(fn):
|
|
767
|
+
"""Mark an internal forwarder that delegates leaf isolation to notify."""
|
|
768
|
+
fn._event_router = True
|
|
769
|
+
return fn
|
|
770
|
+
|
|
771
|
+
@classmethod
|
|
772
|
+
def wrap(cls, observer):
|
|
773
|
+
if observer is BASE_EVENT_HANDLER or getattr(observer, "_event_router", False) is True:
|
|
774
|
+
return observer
|
|
775
|
+
return lambda event: cls.notify(event, observer)
|
|
776
|
+
|
|
777
|
+
@classmethod
|
|
778
|
+
@contextmanager
|
|
779
|
+
def _bind(cls, dispatch):
|
|
780
|
+
token = cls._current_observer.set(dispatch)
|
|
781
|
+
try:
|
|
782
|
+
yield
|
|
783
|
+
finally:
|
|
784
|
+
cls._current_observer.reset(token)
|
|
785
|
+
|
|
786
|
+
@classmethod
|
|
787
|
+
@contextmanager
|
|
788
|
+
def observe(cls, observer):
|
|
789
|
+
"""Bind a notification recipient for this execution and its child tasks."""
|
|
790
|
+
with cls._bind(cls.wrap(observer)):
|
|
791
|
+
yield
|
|
792
|
+
|
|
793
|
+
@classmethod
|
|
794
|
+
def current(cls):
|
|
795
|
+
return cls._current_observer.get()
|
|
796
|
+
|
|
797
|
+
@classmethod
|
|
798
|
+
def emit(cls, event):
|
|
799
|
+
"""Publish an event to the observer bound in the current task scope."""
|
|
800
|
+
cls._current_observer.get()(event)
|
|
801
|
+
|
|
802
|
+
@classmethod
|
|
803
|
+
def notify(cls, event, *observers):
|
|
804
|
+
for observer in observers:
|
|
805
|
+
if observer is BASE_EVENT_HANDLER:
|
|
806
|
+
continue
|
|
807
|
+
try:
|
|
808
|
+
observer(deepcopy(event))
|
|
809
|
+
except (Exception, asyncio.CancelledError) as exc:
|
|
810
|
+
cls.report_observer_error("Agent event observer failed: " + event.kind, exc)
|
|
811
|
+
|
|
812
|
+
@staticmethod
|
|
813
|
+
def report_observer_error(message, exc):
|
|
814
|
+
try:
|
|
815
|
+
loop = asyncio.get_running_loop()
|
|
816
|
+
except RuntimeError:
|
|
817
|
+
logging.getLogger(__name__).error(message, exc_info=(type(exc), exc, exc.__traceback__))
|
|
818
|
+
else:
|
|
819
|
+
loop.call_exception_handler({"message": message, "exception": exc})
|
|
820
|
+
|
|
821
|
+
@classmethod
|
|
822
|
+
@contextmanager
|
|
823
|
+
def worker_events(cls, observer=None):
|
|
824
|
+
"""Forward worker events to the caller's loop until the request exits."""
|
|
825
|
+
loop = asyncio.get_running_loop()
|
|
826
|
+
lock = threading.Lock()
|
|
827
|
+
active = True
|
|
828
|
+
|
|
829
|
+
def deliver(event):
|
|
830
|
+
if active:
|
|
831
|
+
owner.run(cls.emit, event)
|
|
832
|
+
|
|
833
|
+
def post(event):
|
|
834
|
+
with lock:
|
|
835
|
+
if active:
|
|
836
|
+
loop.call_soon_threadsafe(deliver, event)
|
|
837
|
+
|
|
838
|
+
scope = cls._bind(cls.current()) if observer is None else cls.observe(observer)
|
|
839
|
+
with scope:
|
|
840
|
+
owner = copy_context()
|
|
841
|
+
with cls._bind(post):
|
|
842
|
+
worker = copy_context()
|
|
843
|
+
try:
|
|
844
|
+
yield worker
|
|
845
|
+
finally:
|
|
846
|
+
with lock:
|
|
847
|
+
active = False
|