codex-sdk-py 0.0.3__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.
codex_sdk/__init__.py ADDED
@@ -0,0 +1,135 @@
1
+ """
2
+ Codex SDK for Python.
3
+
4
+ A Python SDK for the OpenAI Codex agent.
5
+
6
+ Corresponds to: src/index.ts
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ # Main classes
12
+ from .codex import Codex
13
+
14
+ # Options
15
+ from .codex_options import (
16
+ CodexConfigObject,
17
+ CodexConfigValue,
18
+ CodexOptions,
19
+ )
20
+
21
+ # Events
22
+ from .events import (
23
+ ItemCompletedEvent,
24
+ ItemStartedEvent,
25
+ ItemUpdatedEvent,
26
+ ThreadError,
27
+ ThreadErrorEvent,
28
+ ThreadEvent,
29
+ ThreadStartedEvent,
30
+ TurnCompletedEvent,
31
+ TurnFailedEvent,
32
+ TurnStartedEvent,
33
+ Usage,
34
+ )
35
+
36
+ # Items
37
+ from .items import (
38
+ AgentMessageItem,
39
+ CommandExecutionItem,
40
+ CommandExecutionStatus,
41
+ ErrorItem,
42
+ FileChangeItem,
43
+ FileUpdateChange,
44
+ McpContentBlock,
45
+ McpToolCallItem,
46
+ McpToolCallStatus,
47
+ McpToolError,
48
+ McpToolResult,
49
+ PatchApplyStatus,
50
+ PatchChangeKind,
51
+ ReasoningItem,
52
+ ThreadItem,
53
+ TodoItem,
54
+ TodoListItem,
55
+ WebSearchItem,
56
+ )
57
+ from .thread import (
58
+ ImageUserInput,
59
+ Input,
60
+ RunResult,
61
+ RunStreamedResult,
62
+ StreamedTurn,
63
+ TextUserInput,
64
+ Thread,
65
+ Turn,
66
+ UserInput,
67
+ )
68
+ from .thread_options import (
69
+ ApprovalMode,
70
+ ModelReasoningEffort,
71
+ SandboxMode,
72
+ ThreadOptions,
73
+ WebSearchMode,
74
+ )
75
+ from .turn_options import TurnOptions
76
+
77
+ __version__ = "0.0.3"
78
+
79
+ __all__ = [
80
+ # Version
81
+ "__version__",
82
+ # Main classes
83
+ "Codex",
84
+ "Thread",
85
+ "Turn",
86
+ "RunResult",
87
+ "StreamedTurn",
88
+ "RunStreamedResult",
89
+ # Input types
90
+ "Input",
91
+ "UserInput",
92
+ "TextUserInput",
93
+ "ImageUserInput",
94
+ # Events
95
+ "ThreadEvent",
96
+ "ThreadStartedEvent",
97
+ "TurnStartedEvent",
98
+ "TurnCompletedEvent",
99
+ "TurnFailedEvent",
100
+ "ItemStartedEvent",
101
+ "ItemUpdatedEvent",
102
+ "ItemCompletedEvent",
103
+ "ThreadError",
104
+ "ThreadErrorEvent",
105
+ "Usage",
106
+ # Items
107
+ "ThreadItem",
108
+ "AgentMessageItem",
109
+ "ReasoningItem",
110
+ "CommandExecutionItem",
111
+ "CommandExecutionStatus",
112
+ "FileChangeItem",
113
+ "FileUpdateChange",
114
+ "PatchChangeKind",
115
+ "PatchApplyStatus",
116
+ "McpToolCallItem",
117
+ "McpToolCallStatus",
118
+ "McpToolResult",
119
+ "McpToolError",
120
+ "McpContentBlock",
121
+ "WebSearchItem",
122
+ "TodoListItem",
123
+ "TodoItem",
124
+ "ErrorItem",
125
+ # Options
126
+ "CodexOptions",
127
+ "CodexConfigValue",
128
+ "CodexConfigObject",
129
+ "ThreadOptions",
130
+ "ApprovalMode",
131
+ "SandboxMode",
132
+ "ModelReasoningEffort",
133
+ "WebSearchMode",
134
+ "TurnOptions",
135
+ ]
codex_sdk/codex.py ADDED
@@ -0,0 +1,79 @@
1
+ """
2
+ Main Codex class for interacting with the Codex agent.
3
+
4
+ Corresponds to: src/codex.ts
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from .codex_options import CodexOptions
10
+ from .exec import CodexExec
11
+ from .thread import Thread
12
+ from .thread_options import ThreadOptions
13
+
14
+
15
+ class Codex:
16
+ """
17
+ Codex is the main class for interacting with the Codex agent.
18
+
19
+ Use the `start_thread()` method to start a new thread or
20
+ `resume_thread()` to resume a previously started thread.
21
+
22
+ Example:
23
+ ```python
24
+ from codex_sdk import Codex
25
+
26
+ codex = Codex()
27
+ thread = codex.start_thread()
28
+ result = await thread.run("Hello, world!")
29
+ print(result.final_response)
30
+ ```
31
+ """
32
+
33
+ def __init__(self, options: CodexOptions | None = None) -> None:
34
+ """
35
+ Initialize the Codex client.
36
+
37
+ Args:
38
+ options: Optional configuration for the Codex client.
39
+ """
40
+ options = options or {}
41
+ self._exec = CodexExec(
42
+ executable_path=options.get("codex_path_override"),
43
+ env=options.get("env"),
44
+ config_overrides=options.get("config"),
45
+ )
46
+ self._options = options
47
+
48
+ def start_thread(self, options: ThreadOptions | None = None) -> Thread:
49
+ """
50
+ Starts a new conversation with an agent.
51
+
52
+ Args:
53
+ options: Optional thread-specific configuration.
54
+
55
+ Returns:
56
+ A new Thread instance.
57
+ """
58
+ options = options or {}
59
+ return Thread(self._exec, self._options, options)
60
+
61
+ def resume_thread(
62
+ self,
63
+ thread_id: str,
64
+ options: ThreadOptions | None = None,
65
+ ) -> Thread:
66
+ """
67
+ Resumes a conversation with an agent based on the thread id.
68
+
69
+ Threads are persisted in ~/.codex/sessions.
70
+
71
+ Args:
72
+ thread_id: The id of the thread to resume.
73
+ options: Optional thread-specific configuration.
74
+
75
+ Returns:
76
+ A Thread instance connected to the existing conversation.
77
+ """
78
+ options = options or {}
79
+ return Thread(self._exec, self._options, options, thread_id)
@@ -0,0 +1,53 @@
1
+ """
2
+ Codex options type definitions.
3
+
4
+ Corresponds to: src/codexOptions.ts
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TypedDict, Union
10
+
11
+ # Recursive type for config values
12
+ CodexConfigValue = Union[
13
+ str,
14
+ int,
15
+ float,
16
+ bool,
17
+ list["CodexConfigValue"],
18
+ "CodexConfigObject",
19
+ ]
20
+
21
+
22
+ class CodexConfigObject(TypedDict, total=False):
23
+ """A nested configuration object for Codex CLI overrides."""
24
+
25
+ pass
26
+
27
+
28
+ class CodexOptions(TypedDict, total=False):
29
+ """Configuration options for the Codex client."""
30
+
31
+ codex_path_override: str
32
+ """Custom path to the Codex CLI binary."""
33
+
34
+ base_url: str
35
+ """Base URL for the API."""
36
+
37
+ api_key: str
38
+ """API key for authentication."""
39
+
40
+ config: CodexConfigObject
41
+ """
42
+ Additional --config key=value overrides to pass to the Codex CLI.
43
+
44
+ Provide a dict and the SDK will flatten it into dotted paths and
45
+ serialize values as TOML literals so they are compatible with the CLI's
46
+ --config parsing.
47
+ """
48
+
49
+ env: dict[str, str]
50
+ """
51
+ Environment variables passed to the Codex CLI process.
52
+ When provided, the SDK will not inherit variables from os.environ.
53
+ """
codex_sdk/events.py ADDED
@@ -0,0 +1,107 @@
1
+ """
2
+ Thread event type definitions.
3
+
4
+ Based on event types from codex-rs/exec/src/exec_events.rs
5
+
6
+ Corresponds to: src/events.ts
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import Literal, TypedDict
12
+
13
+ from .items import ThreadItem
14
+
15
+
16
+ class Usage(TypedDict):
17
+ """Describes the usage of tokens during a turn."""
18
+
19
+ input_tokens: int
20
+ """The number of input tokens used during the turn."""
21
+ cached_input_tokens: int
22
+ """The number of cached input tokens used during the turn."""
23
+ output_tokens: int
24
+ """The number of output tokens used during the turn."""
25
+
26
+
27
+ class ThreadError(TypedDict):
28
+ """Fatal error emitted by the stream."""
29
+
30
+ message: str
31
+
32
+
33
+ class ThreadStartedEvent(TypedDict):
34
+ """Emitted when a new thread is started as the first event."""
35
+
36
+ type: Literal["thread.started"]
37
+ thread_id: str
38
+ """The identifier of the new thread. Can be used to resume the thread later."""
39
+
40
+
41
+ class TurnStartedEvent(TypedDict):
42
+ """
43
+ Emitted when a turn is started by sending a new prompt to the model.
44
+
45
+ A turn encompasses all events that happen while the agent is processing the prompt.
46
+ """
47
+
48
+ type: Literal["turn.started"]
49
+
50
+
51
+ class TurnCompletedEvent(TypedDict):
52
+ """Emitted when a turn is completed. Typically right after the assistant's response."""
53
+
54
+ type: Literal["turn.completed"]
55
+ usage: Usage
56
+
57
+
58
+ class TurnFailedEvent(TypedDict):
59
+ """Indicates that a turn failed with an error."""
60
+
61
+ type: Literal["turn.failed"]
62
+ error: ThreadError
63
+
64
+
65
+ class ItemStartedEvent(TypedDict):
66
+ """
67
+ Emitted when a new item is added to the thread.
68
+
69
+ Typically the item is initially 'in progress'.
70
+ """
71
+
72
+ type: Literal["item.started"]
73
+ item: ThreadItem
74
+
75
+
76
+ class ItemUpdatedEvent(TypedDict):
77
+ """Emitted when an item is updated."""
78
+
79
+ type: Literal["item.updated"]
80
+ item: ThreadItem
81
+
82
+
83
+ class ItemCompletedEvent(TypedDict):
84
+ """Signals that an item has reached a terminal state—either success or failure."""
85
+
86
+ type: Literal["item.completed"]
87
+ item: ThreadItem
88
+
89
+
90
+ class ThreadErrorEvent(TypedDict):
91
+ """Represents an unrecoverable error emitted directly by the event stream."""
92
+
93
+ type: Literal["error"]
94
+ message: str
95
+
96
+
97
+ # Top-level JSONL events emitted by codex exec
98
+ ThreadEvent = (
99
+ ThreadStartedEvent
100
+ | TurnStartedEvent
101
+ | TurnCompletedEvent
102
+ | TurnFailedEvent
103
+ | ItemStartedEvent
104
+ | ItemUpdatedEvent
105
+ | ItemCompletedEvent
106
+ | ThreadErrorEvent
107
+ )