python-codex 0.6.2__py3-none-any.whl → 0.7.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/agent.py +325 -314
- pycodex/bootstrap.py +8 -0
- pycodex/config.py +4 -2
- pycodex/context.py +32 -11
- pycodex/events.py +116 -7
- pycodex/model.py +214 -99
- pycodex/multi_agents.py +11 -40
- pycodex/prompts/models.json +351 -6
- pycodex/protocol.py +1 -0
- pycodex/runtime.py +173 -127
- pycodex/tools/__init__.py +1 -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 +15 -3
- pycodex/utils/compactor.py +28 -98
- pycodex/utils/compat.py +6 -0
- pycodex/utils/event_helpers.py +9 -0
- pycodex/utils/http_headers.py +25 -0
- pycodex/utils/model_metadata.py +5 -4
- {python_codex-0.6.2.dist-info → python_codex-0.7.0.dist-info}/METADATA +34 -8
- {python_codex-0.6.2.dist-info → python_codex-0.7.0.dist-info}/RECORD +35 -34
- responses_server/app.py +6 -0
- responses_server/config.py +4 -1
- responses_server/gemini_api.py +40 -13
- responses_server/messages_api.py +12 -6
- responses_server/payload_processors.py +1 -1
- responses_server/request_translation.py +55 -13
- responses_server/stream_router.py +9 -0
- responses_server/transport.py +24 -11
- {python_codex-0.6.2.dist-info → python_codex-0.7.0.dist-info}/WHEEL +0 -0
- {python_codex-0.6.2.dist-info → python_codex-0.7.0.dist-info}/entry_points.txt +0 -0
- {python_codex-0.6.2.dist-info → python_codex-0.7.0.dist-info}/licenses/LICENSE +0 -0
pycodex/bootstrap.py
CHANGED
|
@@ -8,6 +8,7 @@ from .model import (
|
|
|
8
8
|
ResponsesProviderConfig,
|
|
9
9
|
)
|
|
10
10
|
from .utils import get_debug_dir
|
|
11
|
+
from .utils.http_headers import build_http_headers
|
|
11
12
|
|
|
12
13
|
if typing.TYPE_CHECKING:
|
|
13
14
|
from responses_server import ManagedResponseServer
|
|
@@ -121,10 +122,14 @@ def build_model(
|
|
|
121
122
|
)
|
|
122
123
|
os.environ.setdefault(LOCAL_RESPONSES_SERVER_API_KEY_ENV, "dummy")
|
|
123
124
|
elif vllm_endpoint is not None or compat_api != "none":
|
|
125
|
+
headers = build_http_headers(
|
|
126
|
+
provider_config.http_headers, provider_config.env_http_headers,
|
|
127
|
+
)
|
|
124
128
|
if vllm_endpoint:
|
|
125
129
|
managed_server = launch_chat_completion_compat_server(
|
|
126
130
|
vllm_endpoint,
|
|
127
131
|
model_provider="vllm",
|
|
132
|
+
http_headers=headers,
|
|
128
133
|
)
|
|
129
134
|
else:
|
|
130
135
|
managed_server = launch_chat_completion_compat_server(
|
|
@@ -132,6 +137,7 @@ def build_model(
|
|
|
132
137
|
provider_config.api_key_env,
|
|
133
138
|
model_provider=provider_config.provider_name,
|
|
134
139
|
outcomming_api=compat_api,
|
|
140
|
+
http_headers=headers,
|
|
135
141
|
)
|
|
136
142
|
url, key_env = (
|
|
137
143
|
managed_server.base_url,
|
|
@@ -143,6 +149,8 @@ def build_model(
|
|
|
143
149
|
provider_config,
|
|
144
150
|
base_url=url,
|
|
145
151
|
api_key_env=key_env,
|
|
152
|
+
http_headers={} if uses_local_responses_compat else provider_config.http_headers,
|
|
153
|
+
env_http_headers={} if uses_local_responses_compat else provider_config.env_http_headers,
|
|
146
154
|
responses_lite_override=(
|
|
147
155
|
False
|
|
148
156
|
if uses_local_responses_compat
|
pycodex/config.py
CHANGED
|
@@ -8,6 +8,7 @@ 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
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
@dataclass(frozen=True)
|
|
@@ -28,9 +29,10 @@ class CodexConfig:
|
|
|
28
29
|
path = Path(config_path)
|
|
29
30
|
load_codex_dotenv(path)
|
|
30
31
|
selected = read_codex_config(path, profile, use_default_profile)
|
|
32
|
+
model_catalog = load_models_by_slug()
|
|
31
33
|
return cls(
|
|
32
34
|
path=path,
|
|
33
35
|
profile=selected["profile"],
|
|
34
|
-
provider=ResponsesProviderConfig.from_mapping(selected),
|
|
35
|
-
context=ContextConfig.from_mapping(selected, path),
|
|
36
|
+
provider=ResponsesProviderConfig.from_mapping(selected, model_catalog),
|
|
37
|
+
context=ContextConfig.from_mapping(selected, path, model_catalog),
|
|
36
38
|
)
|
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
|
|
@@ -10,7 +10,7 @@ from xml.sax.saxutils import escape
|
|
|
10
10
|
import yaml
|
|
11
11
|
|
|
12
12
|
from .utils.codex_config import read_codex_config
|
|
13
|
-
from .utils.model_metadata import model_metadata
|
|
13
|
+
from .utils.model_metadata import load_models_by_slug, model_metadata
|
|
14
14
|
from .protocol import (
|
|
15
15
|
ContextMessage, ConversationItem, JSONDict, Prompt, ToolCall, ToolResult, ToolSpec, UserMessage,
|
|
16
16
|
)
|
|
@@ -88,8 +88,16 @@ class ContextConfig:
|
|
|
88
88
|
multi_agent_hint: "typing.Union[str, None]" = None
|
|
89
89
|
code_mode_only: "typing.Union[bool, None]" = None
|
|
90
90
|
include_environment_context: "bool" = True
|
|
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
|
+
)
|
|
91
95
|
|
|
92
96
|
def __post_init__(self):
|
|
97
|
+
if self.model_slug is not None and (
|
|
98
|
+
not isinstance(self.model_slug, str) or self.model_slug not in self.model_catalog
|
|
99
|
+
):
|
|
100
|
+
raise ValueError("model_slug must name an entry in models.json")
|
|
93
101
|
if self.code_mode_only is not None and not isinstance(self.code_mode_only, bool):
|
|
94
102
|
raise ValueError("code_mode_only must be a boolean")
|
|
95
103
|
if (
|
|
@@ -121,6 +129,7 @@ class ContextConfig:
|
|
|
121
129
|
cls,
|
|
122
130
|
selected: "typing.Mapping[str, typing.Any]",
|
|
123
131
|
config_path: "typing.Union[str, Path]",
|
|
132
|
+
model_catalog: "typing.Union[typing.Dict[str, JSONDict], None]" = None,
|
|
124
133
|
) -> "ContextConfig":
|
|
125
134
|
path = Path(config_path)
|
|
126
135
|
model_instructions_file = selected.get("model_instructions_file")
|
|
@@ -147,6 +156,7 @@ class ContextConfig:
|
|
|
147
156
|
|
|
148
157
|
return cls(
|
|
149
158
|
base_instructions=_normalize_text(selected.get("base_instructions")),
|
|
159
|
+
model_catalog=load_models_by_slug() if model_catalog is None else model_catalog,
|
|
150
160
|
developer_instructions=_normalize_text(
|
|
151
161
|
selected.get("developer_instructions")
|
|
152
162
|
),
|
|
@@ -156,6 +166,7 @@ class ContextConfig:
|
|
|
156
166
|
codex_home=codex_home,
|
|
157
167
|
project_doc_max_bytes=_normalize_int(selected.get("project_doc_max_bytes")),
|
|
158
168
|
model=_normalize_text(selected.get("model")),
|
|
169
|
+
model_slug=selected.get("model_slug"),
|
|
159
170
|
model_context_window=_normalize_int(selected.get("model_context_window")),
|
|
160
171
|
model_auto_compact_token_limit=_normalize_int(
|
|
161
172
|
selected.get("model_auto_compact_token_limit")
|
|
@@ -393,11 +404,15 @@ class ContextManager:
|
|
|
393
404
|
def resolve_code_mode_only(self) -> "bool":
|
|
394
405
|
if self.config.code_mode_only is not None:
|
|
395
406
|
return self.config.code_mode_only
|
|
396
|
-
metadata = model_metadata(
|
|
407
|
+
metadata = model_metadata(
|
|
408
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
409
|
+
)
|
|
397
410
|
return metadata is not None and metadata.get("tool_mode") == "code_mode_only"
|
|
398
411
|
|
|
399
412
|
def resolve_model_max_context_window(self) -> "typing.Union[int, None]":
|
|
400
|
-
metadata = model_metadata(
|
|
413
|
+
metadata = model_metadata(
|
|
414
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
415
|
+
)
|
|
401
416
|
context_window = self.config.model_context_window
|
|
402
417
|
if context_window is None and metadata is not None:
|
|
403
418
|
context_window = _normalize_int(metadata.get("context_window"))
|
|
@@ -407,7 +422,9 @@ class ContextManager:
|
|
|
407
422
|
context_window = self.resolve_model_max_context_window()
|
|
408
423
|
if context_window is None:
|
|
409
424
|
return None
|
|
410
|
-
metadata = model_metadata(
|
|
425
|
+
metadata = model_metadata(
|
|
426
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
427
|
+
)
|
|
411
428
|
effective_percent = None
|
|
412
429
|
if metadata is not None:
|
|
413
430
|
effective_percent = _normalize_int(
|
|
@@ -434,19 +451,19 @@ class ContextManager:
|
|
|
434
451
|
if self.config.model_auto_compact_token_limit is not None:
|
|
435
452
|
return self.config.model_auto_compact_token_limit
|
|
436
453
|
|
|
437
|
-
model_slug = self.config.model
|
|
454
|
+
model_slug = self.config.model_slug or self.config.model
|
|
438
455
|
if model_slug is None:
|
|
439
456
|
return None
|
|
440
|
-
metadata = model_metadata(model_slug)
|
|
457
|
+
metadata = model_metadata(model_slug, self.config.model_catalog)
|
|
441
458
|
if metadata is None:
|
|
442
459
|
return None
|
|
443
460
|
return _normalize_int(metadata.get("auto_compact_token_limit"))
|
|
444
461
|
|
|
445
462
|
def _resolve_model_instructions(self) -> "typing.Union[str, None]":
|
|
446
|
-
model_slug = self.config.model
|
|
463
|
+
model_slug = self.config.model_slug or self.config.model
|
|
447
464
|
if model_slug is None:
|
|
448
465
|
return None
|
|
449
|
-
metadata = model_metadata(model_slug)
|
|
466
|
+
metadata = model_metadata(model_slug, self.config.model_catalog)
|
|
450
467
|
if metadata is None:
|
|
451
468
|
return None
|
|
452
469
|
|
|
@@ -493,7 +510,9 @@ class ContextManager:
|
|
|
493
510
|
)
|
|
494
511
|
|
|
495
512
|
def _model_message(self, *keys):
|
|
496
|
-
metadata = model_metadata(
|
|
513
|
+
metadata = model_metadata(
|
|
514
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
515
|
+
)
|
|
497
516
|
value = metadata.get("model_messages") if metadata is not None else None
|
|
498
517
|
for key in keys:
|
|
499
518
|
if value is None:
|
|
@@ -630,7 +649,9 @@ class ContextManager:
|
|
|
630
649
|
skill.path_to_skill_md.relative_to(skill.root).as_posix(),
|
|
631
650
|
)
|
|
632
651
|
lines.append(f"- {skill.name}: {skill.description} (file: {path_str})")
|
|
633
|
-
metadata = model_metadata(
|
|
652
|
+
metadata = model_metadata(
|
|
653
|
+
self.config.model_slug or self.config.model, self.config.model_catalog,
|
|
654
|
+
)
|
|
634
655
|
if metadata is None or metadata.get("include_skills_usage_instructions", True):
|
|
635
656
|
lines.append("### How to use skills")
|
|
636
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
|
|
@@ -725,3 +735,102 @@ class InputResolvedEvent(Event):
|
|
|
725
735
|
|
|
726
736
|
def render(self, display: "EventDisplay") -> "None":
|
|
727
737
|
display.set_prompt(display.main_prompt())
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
class TurnInterrupted(RuntimeError):
|
|
741
|
+
def __init__(self):
|
|
742
|
+
super().__init__("turn interrupted")
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
class ModelRequestInterrupted(RuntimeError):
|
|
746
|
+
"""A cooperative stop ended retries after an issued request finished."""
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
class EventScope:
|
|
750
|
+
"""Task-scoped observer binding, isolation and worker-thread forwarding."""
|
|
751
|
+
|
|
752
|
+
_current_observer = ContextVar("pycodex_event_observer", default=BASE_EVENT_HANDLER)
|
|
753
|
+
|
|
754
|
+
@staticmethod
|
|
755
|
+
def router(fn):
|
|
756
|
+
"""Mark an internal forwarder that delegates leaf isolation to notify."""
|
|
757
|
+
fn._event_router = True
|
|
758
|
+
return fn
|
|
759
|
+
|
|
760
|
+
@classmethod
|
|
761
|
+
def wrap(cls, observer):
|
|
762
|
+
if observer is BASE_EVENT_HANDLER or getattr(observer, "_event_router", False) is True:
|
|
763
|
+
return observer
|
|
764
|
+
return lambda event: cls.notify(event, observer)
|
|
765
|
+
|
|
766
|
+
@classmethod
|
|
767
|
+
@contextmanager
|
|
768
|
+
def _bind(cls, dispatch):
|
|
769
|
+
token = cls._current_observer.set(dispatch)
|
|
770
|
+
try:
|
|
771
|
+
yield
|
|
772
|
+
finally:
|
|
773
|
+
cls._current_observer.reset(token)
|
|
774
|
+
|
|
775
|
+
@classmethod
|
|
776
|
+
@contextmanager
|
|
777
|
+
def observe(cls, observer):
|
|
778
|
+
"""Bind a notification recipient for this execution and its child tasks."""
|
|
779
|
+
with cls._bind(cls.wrap(observer)):
|
|
780
|
+
yield
|
|
781
|
+
|
|
782
|
+
@classmethod
|
|
783
|
+
def current(cls):
|
|
784
|
+
return cls._current_observer.get()
|
|
785
|
+
|
|
786
|
+
@classmethod
|
|
787
|
+
def emit(cls, event):
|
|
788
|
+
"""Publish an event to the observer bound in the current task scope."""
|
|
789
|
+
cls._current_observer.get()(event)
|
|
790
|
+
|
|
791
|
+
@classmethod
|
|
792
|
+
def notify(cls, event, *observers):
|
|
793
|
+
for observer in observers:
|
|
794
|
+
if observer is BASE_EVENT_HANDLER:
|
|
795
|
+
continue
|
|
796
|
+
try:
|
|
797
|
+
observer(deepcopy(event))
|
|
798
|
+
except (Exception, asyncio.CancelledError) as exc:
|
|
799
|
+
cls.report_observer_error("Agent event observer failed: " + event.kind, exc)
|
|
800
|
+
|
|
801
|
+
@staticmethod
|
|
802
|
+
def report_observer_error(message, exc):
|
|
803
|
+
try:
|
|
804
|
+
loop = asyncio.get_running_loop()
|
|
805
|
+
except RuntimeError:
|
|
806
|
+
logging.getLogger(__name__).error(message, exc_info=(type(exc), exc, exc.__traceback__))
|
|
807
|
+
else:
|
|
808
|
+
loop.call_exception_handler({"message": message, "exception": exc})
|
|
809
|
+
|
|
810
|
+
@classmethod
|
|
811
|
+
@contextmanager
|
|
812
|
+
def worker_events(cls, observer=None):
|
|
813
|
+
"""Forward worker events to the caller's loop until the request exits."""
|
|
814
|
+
loop = asyncio.get_running_loop()
|
|
815
|
+
lock = threading.Lock()
|
|
816
|
+
active = True
|
|
817
|
+
|
|
818
|
+
def deliver(event):
|
|
819
|
+
if active:
|
|
820
|
+
owner.run(cls.emit, event)
|
|
821
|
+
|
|
822
|
+
def post(event):
|
|
823
|
+
with lock:
|
|
824
|
+
if active:
|
|
825
|
+
loop.call_soon_threadsafe(deliver, event)
|
|
826
|
+
|
|
827
|
+
scope = cls._bind(cls.current()) if observer is None else cls.observe(observer)
|
|
828
|
+
with scope:
|
|
829
|
+
owner = copy_context()
|
|
830
|
+
with cls._bind(post):
|
|
831
|
+
worker = copy_context()
|
|
832
|
+
try:
|
|
833
|
+
yield worker
|
|
834
|
+
finally:
|
|
835
|
+
with lock:
|
|
836
|
+
active = False
|