forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex_tool.py
More file actions
1411 lines (1165 loc) · 47.7 KB
/
codex_tool.py
File metadata and controls
1411 lines (1165 loc) · 47.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import copy
import dataclasses
import inspect
import json
import os
import re
from collections.abc import AsyncGenerator, Awaitable, Callable, Mapping, MutableMapping
from dataclasses import dataclass
from typing import Any, Literal, TypeAlias, TypeGuard
from openai.types.responses.response_usage import InputTokensDetails, OutputTokensDetails
from pydantic import BaseModel, ConfigDict, Field, ValidationError, model_validator
from typing_extensions import NotRequired, TypedDict
from agents import _debug
from agents.exceptions import ModelBehaviorError, UserError
from agents.logger import logger
from agents.models import _openai_shared
from agents.run_context import RunContextWrapper
from agents.strict_schema import ensure_strict_json_schema
from agents.tool import (
FunctionTool,
ToolErrorFunction,
_build_handled_function_tool_error_handler,
_build_wrapped_function_tool,
default_tool_error_function,
)
from agents.tool_context import ToolContext
from agents.tracing import SpanError, custom_span
from agents.usage import Usage as AgentsUsage
from agents.util._types import MaybeAwaitable
from .codex import Codex
from .codex_options import CodexOptions, coerce_codex_options
from .events import (
ItemCompletedEvent,
ItemStartedEvent,
ItemUpdatedEvent,
ThreadErrorEvent,
ThreadEvent,
ThreadStartedEvent,
TurnCompletedEvent,
TurnFailedEvent,
Usage,
coerce_thread_event,
)
from .items import (
CommandExecutionItem,
ThreadItem,
is_agent_message_item,
)
from .payloads import _DictLike
from .thread import Input, Thread, UserInput
from .thread_options import SandboxMode, ThreadOptions, coerce_thread_options
from .turn_options import TurnOptions, coerce_turn_options
JSON_PRIMITIVE_TYPES = {"string", "number", "integer", "boolean"}
SPAN_TRIM_KEYS = (
"arguments",
"command",
"output",
"result",
"error",
"text",
"changes",
"items",
)
DEFAULT_CODEX_TOOL_NAME = "codex"
DEFAULT_RUN_CONTEXT_THREAD_ID_KEY = "codex_thread_id"
CODEX_TOOL_NAME_PREFIX = "codex_"
class CodexToolInputItem(BaseModel):
type: Literal["text", "local_image"]
text: str | None = None
path: str | None = None
model_config = ConfigDict(extra="forbid")
@model_validator(mode="after")
def validate_item(self) -> CodexToolInputItem:
text_value = (self.text or "").strip()
path_value = (self.path or "").strip()
if self.type == "text":
if not text_value:
raise ValueError('Text inputs must include a non-empty "text" field.')
if path_value:
raise ValueError('"path" is not allowed when type is "text".')
self.text = text_value
self.path = None
return self
if not path_value:
raise ValueError('Local image inputs must include a non-empty "path" field.')
if text_value:
raise ValueError('"text" is not allowed when type is "local_image".')
self.path = path_value
self.text = None
return self
class CodexToolParameters(BaseModel):
inputs: list[CodexToolInputItem] = Field(
...,
min_length=1,
description=(
"Structured inputs appended to the Codex task. Provide at least one input item."
),
)
thread_id: str | None = Field(
default=None,
description=(
"Optional Codex thread ID to resume. If omitted, a new thread is started unless "
"configured elsewhere."
),
)
model_config = ConfigDict(extra="forbid")
@model_validator(mode="after")
def validate_thread_id(self) -> CodexToolParameters:
if self.thread_id is None:
return self
normalized = self.thread_id.strip()
if not normalized:
raise ValueError('When provided, "thread_id" must be a non-empty string.')
self.thread_id = normalized
return self
class CodexToolRunContextParameters(BaseModel):
inputs: list[CodexToolInputItem] = Field(
...,
min_length=1,
description=(
"Structured inputs appended to the Codex task. Provide at least one input item."
),
)
model_config = ConfigDict(extra="forbid")
class OutputSchemaPrimitive(TypedDict, total=False):
type: Literal["string", "number", "integer", "boolean"]
description: NotRequired[str]
enum: NotRequired[list[str]]
class OutputSchemaArray(TypedDict, total=False):
type: Literal["array"]
description: NotRequired[str]
items: OutputSchemaPrimitive
OutputSchemaField: TypeAlias = OutputSchemaPrimitive | OutputSchemaArray
class OutputSchemaPropertyDescriptor(TypedDict, total=False):
name: str
description: NotRequired[str]
schema: OutputSchemaField
class OutputSchemaDescriptor(TypedDict, total=False):
title: NotRequired[str]
description: NotRequired[str]
properties: list[OutputSchemaPropertyDescriptor]
required: NotRequired[list[str]]
@dataclass(frozen=True)
class CodexToolResult:
thread_id: str | None
response: str
usage: Usage | None
def as_dict(self) -> dict[str, Any]:
return {
"thread_id": self.thread_id,
"response": self.response,
"usage": self.usage.as_dict() if isinstance(self.usage, Usage) else self.usage,
}
def __str__(self) -> str:
return json.dumps(self.as_dict())
@dataclass(frozen=True)
class CodexToolStreamEvent(_DictLike):
event: ThreadEvent
thread: Thread
tool_call: Any
@dataclass
class CodexToolOptions:
name: str | None = None
description: str | None = None
parameters: type[BaseModel] | None = None
output_schema: OutputSchemaDescriptor | Mapping[str, Any] | None = None
codex: Codex | None = None
codex_options: CodexOptions | Mapping[str, Any] | None = None
default_thread_options: ThreadOptions | Mapping[str, Any] | None = None
thread_id: str | None = None
sandbox_mode: SandboxMode | None = None
working_directory: str | None = None
skip_git_repo_check: bool | None = None
default_turn_options: TurnOptions | Mapping[str, Any] | None = None
span_data_max_chars: int | None = 8192
persist_session: bool = False
on_stream: Callable[[CodexToolStreamEvent], MaybeAwaitable[None]] | None = None
is_enabled: bool | Callable[[RunContextWrapper[Any], Any], MaybeAwaitable[bool]] = True
failure_error_function: ToolErrorFunction | None = default_tool_error_function
use_run_context_thread_id: bool = False
run_context_thread_id_key: str | None = None
class CodexToolCallArguments(TypedDict):
inputs: list[UserInput] | None
thread_id: str | None
class _UnsetType:
pass
_UNSET = _UnsetType()
def codex_tool(
options: CodexToolOptions | Mapping[str, Any] | None = None,
*,
name: str | None = None,
description: str | None = None,
parameters: type[BaseModel] | None = None,
output_schema: OutputSchemaDescriptor | Mapping[str, Any] | None = None,
codex: Codex | None = None,
codex_options: CodexOptions | Mapping[str, Any] | None = None,
default_thread_options: ThreadOptions | Mapping[str, Any] | None = None,
thread_id: str | None = None,
sandbox_mode: SandboxMode | None = None,
working_directory: str | None = None,
skip_git_repo_check: bool | None = None,
default_turn_options: TurnOptions | Mapping[str, Any] | None = None,
span_data_max_chars: int | None | _UnsetType = _UNSET,
persist_session: bool | None = None,
on_stream: Callable[[CodexToolStreamEvent], MaybeAwaitable[None]] | None = None,
is_enabled: bool | Callable[[RunContextWrapper[Any], Any], MaybeAwaitable[bool]] | None = None,
failure_error_function: ToolErrorFunction | None | _UnsetType = _UNSET,
use_run_context_thread_id: bool | None = None,
run_context_thread_id_key: str | None = None,
) -> FunctionTool:
resolved_options = _coerce_tool_options(options)
if name is not None:
resolved_options.name = name
if description is not None:
resolved_options.description = description
if parameters is not None:
resolved_options.parameters = parameters
if output_schema is not None:
resolved_options.output_schema = output_schema
if codex is not None:
resolved_options.codex = codex
if codex_options is not None:
resolved_options.codex_options = codex_options
if default_thread_options is not None:
resolved_options.default_thread_options = default_thread_options
if thread_id is not None:
resolved_options.thread_id = thread_id
if sandbox_mode is not None:
resolved_options.sandbox_mode = sandbox_mode
if working_directory is not None:
resolved_options.working_directory = working_directory
if skip_git_repo_check is not None:
resolved_options.skip_git_repo_check = skip_git_repo_check
if default_turn_options is not None:
resolved_options.default_turn_options = default_turn_options
if not isinstance(span_data_max_chars, _UnsetType):
resolved_options.span_data_max_chars = span_data_max_chars
if persist_session is not None:
resolved_options.persist_session = persist_session
if on_stream is not None:
resolved_options.on_stream = on_stream
if is_enabled is not None:
resolved_options.is_enabled = is_enabled
if not isinstance(failure_error_function, _UnsetType):
resolved_options.failure_error_function = failure_error_function
if use_run_context_thread_id is not None:
resolved_options.use_run_context_thread_id = use_run_context_thread_id
if run_context_thread_id_key is not None:
resolved_options.run_context_thread_id_key = run_context_thread_id_key
resolved_options.codex_options = coerce_codex_options(resolved_options.codex_options)
resolved_options.default_thread_options = coerce_thread_options(
resolved_options.default_thread_options
)
resolved_options.default_turn_options = coerce_turn_options(
resolved_options.default_turn_options
)
name = _resolve_codex_tool_name(resolved_options.name)
resolved_run_context_thread_id_key = _resolve_run_context_thread_id_key(
tool_name=name,
configured_key=resolved_options.run_context_thread_id_key,
strict_default_key=resolved_options.use_run_context_thread_id,
)
description = resolved_options.description or (
"Executes an agentic Codex task against the current workspace."
)
if resolved_options.parameters is not None:
parameters_model = resolved_options.parameters
elif resolved_options.use_run_context_thread_id:
# In run-context mode, hide thread_id from the default tool schema.
parameters_model = CodexToolRunContextParameters
else:
parameters_model = CodexToolParameters
params_schema = ensure_strict_json_schema(parameters_model.model_json_schema())
resolved_codex_options = _resolve_codex_options(resolved_options.codex_options)
resolve_codex = _create_codex_resolver(resolved_options.codex, resolved_codex_options)
validated_output_schema = _resolve_output_schema(resolved_options.output_schema)
resolved_thread_options = _resolve_thread_options(
resolved_options.default_thread_options,
resolved_options.sandbox_mode,
resolved_options.working_directory,
resolved_options.skip_git_repo_check,
)
persisted_thread: Thread | None = None
async def _on_invoke_tool(ctx: ToolContext[Any], input_json: str) -> Any:
nonlocal persisted_thread
resolved_thread_id: str | None = None
try:
parsed = _parse_tool_input(parameters_model, input_json)
args = _normalize_parameters(parsed)
if resolved_options.use_run_context_thread_id:
_validate_run_context_thread_id_context(ctx, resolved_run_context_thread_id_key)
codex = await resolve_codex()
call_thread_id = _resolve_call_thread_id(
args=args,
ctx=ctx,
configured_thread_id=resolved_options.thread_id,
use_run_context_thread_id=resolved_options.use_run_context_thread_id,
run_context_thread_id_key=resolved_run_context_thread_id_key,
)
if resolved_options.persist_session:
# Reuse a single Codex thread across tool calls.
thread = _get_or_create_persisted_thread(
codex,
call_thread_id,
resolved_thread_options,
persisted_thread,
)
if persisted_thread is None:
persisted_thread = thread
else:
thread = _get_thread(codex, call_thread_id, resolved_thread_options)
turn_options = _build_turn_options(
resolved_options.default_turn_options, validated_output_schema
)
codex_input = _build_codex_input(args)
resolved_thread_id = thread.id or call_thread_id
# Always stream and aggregate locally to enable on_stream callbacks.
stream_result = await thread.run_streamed(codex_input, turn_options)
resolved_thread_id_holder: dict[str, str | None] = {"thread_id": resolved_thread_id}
try:
response, usage, resolved_thread_id = await _consume_events(
stream_result.events,
args,
ctx,
thread,
resolved_options.on_stream,
resolved_options.span_data_max_chars,
resolved_thread_id_holder=resolved_thread_id_holder,
)
except BaseException:
resolved_thread_id = resolved_thread_id_holder["thread_id"]
raise
if usage is not None:
ctx.usage.add(_to_agent_usage(usage))
if resolved_options.use_run_context_thread_id:
_store_thread_id_in_run_context(
ctx,
resolved_run_context_thread_id_key,
resolved_thread_id,
)
return CodexToolResult(thread_id=resolved_thread_id, response=response, usage=usage)
except BaseException:
_try_store_thread_id_in_run_context_after_error(
ctx=ctx,
key=resolved_run_context_thread_id_key,
thread_id=resolved_thread_id,
enabled=resolved_options.use_run_context_thread_id,
)
raise
function_tool = _build_wrapped_function_tool(
name=name,
description=description,
params_json_schema=params_schema,
invoke_tool_impl=_on_invoke_tool,
on_handled_error=_build_handled_function_tool_error_handler(
span_message="Error running Codex tool (non-fatal)",
log_label="Codex tool",
include_input_json_in_logs=False,
include_tool_name_in_log_messages=False,
),
failure_error_function=resolved_options.failure_error_function,
strict_json_schema=True,
is_enabled=resolved_options.is_enabled,
)
# Internal marker used for codex-tool specific runtime validation.
function_tool._is_codex_tool = True
return function_tool
def _coerce_tool_options(
options: CodexToolOptions | Mapping[str, Any] | None,
) -> CodexToolOptions:
if options is None:
resolved = CodexToolOptions()
elif isinstance(options, CodexToolOptions):
resolved = options
else:
if not isinstance(options, Mapping):
raise UserError("Codex tool options must be a CodexToolOptions or a mapping.")
allowed = {field.name for field in dataclasses.fields(CodexToolOptions)}
unknown = set(options.keys()) - allowed
if unknown:
raise UserError(f"Unknown Codex tool option(s): {sorted(unknown)}")
resolved = CodexToolOptions(**dict(options))
# Normalize nested option dictionaries to their dataclass equivalents.
resolved.codex_options = coerce_codex_options(resolved.codex_options)
resolved.default_thread_options = coerce_thread_options(resolved.default_thread_options)
resolved.default_turn_options = coerce_turn_options(resolved.default_turn_options)
key = resolved.run_context_thread_id_key
if key is not None:
resolved.run_context_thread_id_key = _validate_run_context_thread_id_key(key)
return resolved
def _validate_run_context_thread_id_key(value: Any) -> str:
if not isinstance(value, str):
raise UserError("run_context_thread_id_key must be a string.")
key = value.strip()
if not key:
raise UserError("run_context_thread_id_key must be a non-empty string.")
return key
def _resolve_codex_tool_name(configured_name: str | None) -> str:
if configured_name is None:
return DEFAULT_CODEX_TOOL_NAME
if not isinstance(configured_name, str):
raise UserError("Codex tool name must be a string.")
normalized = configured_name.strip()
if not normalized:
raise UserError("Codex tool name must be a non-empty string.")
if normalized != DEFAULT_CODEX_TOOL_NAME and not normalized.startswith(CODEX_TOOL_NAME_PREFIX):
raise UserError(
f'Codex tool name must be "{DEFAULT_CODEX_TOOL_NAME}" or start with '
f'"{CODEX_TOOL_NAME_PREFIX}".'
)
return normalized
def _resolve_run_context_thread_id_key(
tool_name: str, configured_key: str | None, *, strict_default_key: bool = False
) -> str:
if configured_key is not None:
return _validate_run_context_thread_id_key(configured_key)
if tool_name == DEFAULT_CODEX_TOOL_NAME:
return DEFAULT_RUN_CONTEXT_THREAD_ID_KEY
suffix = tool_name[len(CODEX_TOOL_NAME_PREFIX) :]
if strict_default_key:
suffix = _validate_default_run_context_thread_id_suffix(suffix)
return f"{DEFAULT_RUN_CONTEXT_THREAD_ID_KEY}_{suffix}"
suffix = _normalize_name_for_context_key(suffix)
return f"{DEFAULT_RUN_CONTEXT_THREAD_ID_KEY}_{suffix}"
def _normalize_name_for_context_key(value: str) -> str:
# Keep generated context keys deterministic and broadly attribute-safe.
normalized = re.sub(r"[^0-9a-zA-Z_]+", "_", value.strip().lower())
normalized = normalized.strip("_")
return normalized or "tool"
def _validate_default_run_context_thread_id_suffix(value: str) -> str:
suffix = value.strip()
if not suffix:
raise UserError(
"When use_run_context_thread_id=True and run_context_thread_id_key is omitted, "
'codex tool names must include a non-empty suffix after "codex_".'
)
if not re.fullmatch(r"[A-Za-z0-9_]+", suffix):
raise UserError(
"When use_run_context_thread_id=True and run_context_thread_id_key is omitted, "
'the codex tool name suffix (after "codex_") must match [A-Za-z0-9_]+. '
"Use only letters, numbers, and underscores, "
"or set run_context_thread_id_key explicitly."
)
return suffix
def _parse_tool_input(parameters_model: type[BaseModel], input_json: str) -> BaseModel:
try:
json_data = json.loads(input_json) if input_json else {}
except Exception as exc: # noqa: BLE001
if _debug.DONT_LOG_TOOL_DATA:
logger.debug("Invalid JSON input for codex tool")
else:
logger.debug("Invalid JSON input for codex tool: %s", input_json)
raise ModelBehaviorError(f"Invalid JSON input for codex tool: {input_json}") from exc
try:
return parameters_model.model_validate(json_data)
except ValidationError as exc:
raise ModelBehaviorError(f"Invalid JSON input for codex tool: {exc}") from exc
def _normalize_parameters(params: BaseModel) -> CodexToolCallArguments:
inputs_value = getattr(params, "inputs", None)
if inputs_value is None:
raise UserError("Codex tool parameters must include an inputs field.")
thread_id_value = getattr(params, "thread_id", None)
inputs = [{"type": item.type, "text": item.text, "path": item.path} for item in inputs_value]
normalized_inputs: list[UserInput] = []
for item in inputs:
if item["type"] == "text":
normalized_inputs.append({"type": "text", "text": item["text"] or ""})
else:
normalized_inputs.append({"type": "local_image", "path": item["path"] or ""})
return {
"inputs": normalized_inputs if normalized_inputs else None,
"thread_id": _normalize_thread_id(thread_id_value),
}
def _build_codex_input(args: CodexToolCallArguments) -> Input:
if args.get("inputs"):
return args["inputs"] # type: ignore[return-value]
return ""
def _resolve_codex_options(
options: CodexOptions | Mapping[str, Any] | None,
) -> CodexOptions | None:
options = coerce_codex_options(options)
if options and options.api_key:
return options
api_key = _resolve_default_codex_api_key(options)
if not api_key:
return options
if options is None:
return CodexOptions(api_key=api_key)
return CodexOptions(
codex_path_override=options.codex_path_override,
base_url=options.base_url,
api_key=api_key,
env=options.env,
codex_subprocess_stream_limit_bytes=options.codex_subprocess_stream_limit_bytes,
)
def _resolve_default_codex_api_key(options: CodexOptions | None) -> str | None:
if options and options.api_key:
return options.api_key
env_override = options.env if options else None
if env_override:
env_codex = env_override.get("CODEX_API_KEY")
if env_codex:
return env_codex
env_openai = env_override.get("OPENAI_API_KEY")
if env_openai:
return env_openai
env_codex = os.environ.get("CODEX_API_KEY")
if env_codex:
return env_codex
env_openai = os.environ.get("OPENAI_API_KEY")
if env_openai:
return env_openai
return _openai_shared.get_default_openai_key()
def _create_codex_resolver(
provided: Codex | None, options: CodexOptions | None
) -> Callable[[], Awaitable[Codex]]:
if provided is not None:
async def _return_provided() -> Codex:
return provided
return _return_provided
codex_instance: Codex | None = None
async def _get_or_create() -> Codex:
nonlocal codex_instance
if codex_instance is None:
codex_instance = Codex(options)
return codex_instance
return _get_or_create
def _resolve_thread_options(
defaults: ThreadOptions | Mapping[str, Any] | None,
sandbox_mode: SandboxMode | None,
working_directory: str | None,
skip_git_repo_check: bool | None,
) -> ThreadOptions | None:
defaults = coerce_thread_options(defaults)
if not defaults and not sandbox_mode and not working_directory and skip_git_repo_check is None:
return None
return ThreadOptions(
**{
**(defaults.__dict__ if defaults else {}),
**({"sandbox_mode": sandbox_mode} if sandbox_mode else {}),
**({"working_directory": working_directory} if working_directory else {}),
**(
{"skip_git_repo_check": skip_git_repo_check}
if skip_git_repo_check is not None
else {}
),
}
)
def _build_turn_options(
defaults: TurnOptions | Mapping[str, Any] | None,
output_schema: dict[str, Any] | None,
) -> TurnOptions:
defaults = coerce_turn_options(defaults)
if defaults is None and output_schema is None:
return TurnOptions()
if defaults is None:
return TurnOptions(output_schema=output_schema, signal=None, idle_timeout_seconds=None)
merged_output_schema = output_schema if output_schema is not None else defaults.output_schema
return TurnOptions(
output_schema=merged_output_schema,
signal=defaults.signal,
idle_timeout_seconds=defaults.idle_timeout_seconds,
)
def _resolve_output_schema(
option: OutputSchemaDescriptor | Mapping[str, Any] | None,
) -> dict[str, Any] | None:
if option is None:
return None
if isinstance(option, Mapping) and _looks_like_descriptor(option):
# Descriptor input is converted to a strict JSON schema for Codex.
descriptor = _validate_descriptor(option)
return _build_codex_output_schema(descriptor)
if isinstance(option, Mapping):
schema = copy.deepcopy(dict(option))
if "type" in schema and schema.get("type") != "object":
raise UserError('Codex output schema must be a JSON object schema with type "object".')
return ensure_strict_json_schema(schema)
raise UserError("Codex output schema must be a JSON schema or descriptor.")
def _looks_like_descriptor(option: Mapping[str, Any]) -> bool:
properties = option.get("properties")
if not isinstance(properties, list):
return False
return all(isinstance(item, Mapping) and "name" in item for item in properties)
def _validate_descriptor(option: Mapping[str, Any]) -> OutputSchemaDescriptor:
properties = option.get("properties")
if not isinstance(properties, list) or not properties:
raise UserError("Codex output schema descriptor must include properties.")
seen: set[str] = set()
for prop in properties:
name = prop.get("name") if isinstance(prop, Mapping) else None
if not isinstance(name, str) or not name.strip():
raise UserError("Codex output schema properties must include non-empty names.")
if name in seen:
raise UserError(f'Duplicate property name "{name}" in output_schema.')
seen.add(name)
schema = prop.get("schema")
if not _is_valid_field(schema):
raise UserError(f'Invalid schema for output property "{name}".')
required = option.get("required")
if required is not None:
if not isinstance(required, list) or not all(isinstance(item, str) for item in required):
raise UserError("output_schema.required must be a list of strings.")
for name in required:
if name not in seen:
raise UserError(f'Required property "{name}" must also be defined in "properties".')
return option # type: ignore[return-value]
def _is_valid_field(field: Any) -> bool:
if not isinstance(field, Mapping):
return False
field_type = field.get("type")
if field_type in JSON_PRIMITIVE_TYPES:
enum = field.get("enum")
if enum is not None and (
not isinstance(enum, list) or not all(isinstance(item, str) for item in enum)
):
return False
return True
if field_type == "array":
items = field.get("items")
return _is_valid_field(items)
return False
def _build_codex_output_schema(descriptor: OutputSchemaDescriptor) -> dict[str, Any]:
# Compose the strict object schema required by Codex structured outputs.
properties: dict[str, Any] = {}
for prop in descriptor["properties"]:
prop_schema = _build_codex_output_schema_field(prop["schema"])
if prop.get("description"):
prop_schema["description"] = prop["description"]
properties[prop["name"]] = prop_schema
required = list(descriptor.get("required", []))
schema: dict[str, Any] = {
"type": "object",
"additionalProperties": False,
"properties": properties,
"required": required,
}
if "title" in descriptor and descriptor["title"]:
schema["title"] = descriptor["title"]
if "description" in descriptor and descriptor["description"]:
schema["description"] = descriptor["description"]
return schema
def _build_codex_output_schema_field(field: OutputSchemaField) -> dict[str, Any]:
if field["type"] == "array":
schema: dict[str, Any] = {
"type": "array",
"items": _build_codex_output_schema_field(field["items"]),
}
if "description" in field and field["description"]:
schema["description"] = field["description"]
return schema
result: dict[str, Any] = {"type": field["type"]}
if "description" in field and field["description"]:
result["description"] = field["description"]
if "enum" in field:
result["enum"] = field["enum"]
return result
def _get_thread(codex: Codex, thread_id: str | None, defaults: ThreadOptions | None) -> Thread:
if thread_id:
return codex.resume_thread(thread_id, defaults)
return codex.start_thread(defaults)
def _normalize_thread_id(value: Any) -> str | None:
if value is None:
return None
if not isinstance(value, str):
raise UserError("Codex thread_id must be a string when provided.")
normalized = value.strip()
if not normalized:
return None
return normalized
def _resolve_call_thread_id(
args: CodexToolCallArguments,
ctx: RunContextWrapper[Any],
configured_thread_id: str | None,
use_run_context_thread_id: bool,
run_context_thread_id_key: str,
) -> str | None:
explicit_thread_id = _normalize_thread_id(args.get("thread_id"))
if explicit_thread_id:
return explicit_thread_id
if use_run_context_thread_id:
context_thread_id = _read_thread_id_from_run_context(ctx, run_context_thread_id_key)
if context_thread_id:
return context_thread_id
return configured_thread_id
def _read_thread_id_from_run_context(ctx: RunContextWrapper[Any], key: str) -> str | None:
context = ctx.context
if context is None:
return None
if isinstance(context, Mapping):
value = context.get(key)
else:
value = getattr(context, key, None)
if value is None:
return None
if not isinstance(value, str):
raise UserError(f'Run context "{key}" must be a string when provided.')
normalized = value.strip()
if not normalized:
return None
return normalized
def _validate_run_context_thread_id_context(ctx: RunContextWrapper[Any], key: str) -> None:
context = ctx.context
if context is None:
raise UserError(
"use_run_context_thread_id=True requires a mutable run context object. "
"Pass context={} (or an object) to Runner.run()."
)
if isinstance(context, MutableMapping):
return
if isinstance(context, Mapping):
raise UserError(
"use_run_context_thread_id=True requires a mutable run context mapping "
"or a writable object context."
)
if isinstance(context, BaseModel):
if bool(context.model_config.get("frozen", False)):
raise UserError(
"use_run_context_thread_id=True requires a mutable run context object. "
"Frozen Pydantic models are not supported."
)
return
if dataclasses.is_dataclass(context):
params = getattr(type(context), "__dataclass_params__", None)
if params is not None and bool(getattr(params, "frozen", False)):
raise UserError(
"use_run_context_thread_id=True requires a mutable run context object. "
"Frozen dataclass contexts are not supported."
)
slots = getattr(type(context), "__slots__", None)
if slots is not None and not hasattr(context, "__dict__"):
slot_names = (slots,) if isinstance(slots, str) else tuple(slots)
if key not in slot_names:
raise UserError(
"use_run_context_thread_id=True requires the run context to support field "
+ f'"{key}". '
"Use a mutable dict context, or add a writable field/slot to the context object."
)
return
if not hasattr(context, "__dict__"):
raise UserError(
"use_run_context_thread_id=True requires a mutable run context mapping "
"or a writable object context."
)
def _store_thread_id_in_run_context(
ctx: RunContextWrapper[Any], key: str, thread_id: str | None
) -> None:
if thread_id is None:
return
_validate_run_context_thread_id_context(ctx, key)
context = ctx.context
assert context is not None
if isinstance(context, MutableMapping):
context[key] = thread_id
return
if isinstance(context, BaseModel):
if _set_pydantic_context_value(context, key, thread_id):
return
raise UserError(
f'Unable to store Codex thread_id in run context field "{key}". '
"Use a mutable dict context or set a writable attribute."
)
try:
setattr(context, key, thread_id)
except Exception as exc: # noqa: BLE001
raise UserError(
f'Unable to store Codex thread_id in run context field "{key}". '
"Use a mutable dict context or set a writable attribute."
) from exc
def _try_store_thread_id_in_run_context_after_error(
*,
ctx: RunContextWrapper[Any],
key: str,
thread_id: str | None,
enabled: bool,
) -> None:
if not enabled or thread_id is None:
return
try:
_store_thread_id_in_run_context(ctx, key, thread_id)
except Exception:
logger.exception("Failed to store Codex thread id in run context after error.")
def _set_pydantic_context_value(context: BaseModel, key: str, value: str) -> bool:
model_config = context.model_config
if bool(model_config.get("frozen", False)):
return False
model_fields = type(context).model_fields
if key in model_fields:
try:
setattr(context, key, value)
except Exception: # noqa: BLE001
return False
return True
try:
setattr(context, key, value)
return True
except ValueError:
pass
except Exception: # noqa: BLE001
return False
state = getattr(context, "__dict__", None)
if isinstance(state, dict):
state[key] = value
return True
return False
def _get_or_create_persisted_thread(
codex: Codex,
thread_id: str | None,
thread_options: ThreadOptions | None,
existing_thread: Thread | None,
) -> Thread:
if existing_thread is not None:
if thread_id:
existing_id = existing_thread.id
if existing_id and existing_id != thread_id:
raise UserError(
"Codex tool is configured with persist_session=true "
+ "and already has an active thread."