circle-codex 0.154.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.
openai_codex/errors.py ADDED
@@ -0,0 +1,121 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+
5
+
6
+ class CodexError(Exception):
7
+ """Base exception for SDK errors."""
8
+
9
+
10
+ class JsonRpcError(CodexError):
11
+ """Raw JSON-RPC error wrapper from the server."""
12
+
13
+ def __init__(self, code: int, message: str, data: Any = None):
14
+ super().__init__(f"JSON-RPC error {code}: {message}")
15
+ self.code = code
16
+ self.message = message
17
+ self.data = data
18
+
19
+
20
+ class TransportClosedError(CodexError):
21
+ """Raised when the Codex transport closes unexpectedly."""
22
+
23
+
24
+ class CodexRpcError(JsonRpcError):
25
+ """Base typed error for JSON-RPC failures."""
26
+
27
+
28
+ class ParseError(CodexRpcError):
29
+ """Raised when a request or response cannot be parsed."""
30
+
31
+
32
+ class InvalidRequestError(CodexRpcError):
33
+ """Raised when the runtime rejects the request shape."""
34
+
35
+
36
+ class MethodNotFoundError(CodexRpcError):
37
+ """Raised when the requested operation is unavailable."""
38
+
39
+
40
+ class InvalidParamsError(CodexRpcError):
41
+ """Raised when an operation receives invalid parameters."""
42
+
43
+
44
+ class InternalRpcError(CodexRpcError):
45
+ """Raised when the runtime reports an internal RPC failure."""
46
+
47
+
48
+ class ServerBusyError(CodexRpcError):
49
+ """Server is overloaded / unavailable and caller should retry."""
50
+
51
+
52
+ class RetryLimitExceededError(ServerBusyError):
53
+ """Server exhausted internal retry budget for a retryable operation."""
54
+
55
+
56
+ def _contains_retry_limit_text(message: str) -> bool:
57
+ lowered = message.lower()
58
+ return "retry limit" in lowered or "too many failed attempts" in lowered
59
+
60
+
61
+ def _is_server_overloaded(data: Any) -> bool:
62
+ if data is None:
63
+ return False
64
+
65
+ if isinstance(data, str):
66
+ return data.lower() == "server_overloaded"
67
+
68
+ if isinstance(data, dict):
69
+ direct = data.get("codex_error_info") or data.get("codexErrorInfo") or data.get("errorInfo")
70
+ if isinstance(direct, str) and direct.lower() == "server_overloaded":
71
+ return True
72
+ if isinstance(direct, dict):
73
+ for value in direct.values():
74
+ if isinstance(value, str) and value.lower() == "server_overloaded":
75
+ return True
76
+ for value in data.values():
77
+ if _is_server_overloaded(value):
78
+ return True
79
+
80
+ if isinstance(data, list):
81
+ return any(_is_server_overloaded(value) for value in data)
82
+
83
+ return False
84
+
85
+
86
+ def map_jsonrpc_error(code: int, message: str, data: Any = None) -> JsonRpcError:
87
+ """Map a raw JSON-RPC error into a richer SDK exception class."""
88
+
89
+ if code == -32700:
90
+ return ParseError(code, message, data)
91
+ if code == -32600:
92
+ return InvalidRequestError(code, message, data)
93
+ if code == -32601:
94
+ return MethodNotFoundError(code, message, data)
95
+ if code == -32602:
96
+ return InvalidParamsError(code, message, data)
97
+ if code == -32603:
98
+ return InternalRpcError(code, message, data)
99
+
100
+ if -32099 <= code <= -32000:
101
+ if _is_server_overloaded(data):
102
+ if _contains_retry_limit_text(message):
103
+ return RetryLimitExceededError(code, message, data)
104
+ return ServerBusyError(code, message, data)
105
+ if _contains_retry_limit_text(message):
106
+ return RetryLimitExceededError(code, message, data)
107
+ return CodexRpcError(code, message, data)
108
+
109
+ return JsonRpcError(code, message, data)
110
+
111
+
112
+ def is_retryable_error(exc: BaseException) -> bool:
113
+ """True if the exception is a transient overload-style error."""
114
+
115
+ if isinstance(exc, ServerBusyError):
116
+ return True
117
+
118
+ if isinstance(exc, JsonRpcError):
119
+ return _is_server_overloaded(exc.data)
120
+
121
+ return False
@@ -0,0 +1 @@
1
+ """Auto-generated Python types derived from the app-server schemas."""
@@ -0,0 +1,314 @@
1
+ # Auto-generated by scripts/update_sdk_artifacts.py
2
+ # DO NOT EDIT MANUALLY.
3
+
4
+ from __future__ import annotations
5
+
6
+ from typing import TypeAlias
7
+
8
+ from pydantic import BaseModel
9
+
10
+ from .v2_all import AccountLoginCompletedNotification
11
+ from .v2_all import AccountRateLimitsUpdatedNotification
12
+ from .v2_all import AccountUpdatedNotification
13
+ from .v2_all import AgentMessageDeltaNotification
14
+ from .v2_all import AppListUpdatedNotification
15
+ from .v2_all import AuthRecoveryNotification
16
+ from .v2_all import CommandExecOutputDeltaNotification
17
+ from .v2_all import CommandExecutionOutputDeltaNotification
18
+ from .v2_all import ConfigWarningNotification
19
+ from .v2_all import ContextCompactedNotification
20
+ from .v2_all import DeprecationNoticeNotification
21
+ from .v2_all import EnvironmentConnectionNotification
22
+ from .v2_all import ErrorNotification
23
+ from .v2_all import ExternalAgentConfigImportCompletedNotification
24
+ from .v2_all import ExternalAgentConfigImportProgressNotification
25
+ from .v2_all import FileChangeOutputDeltaNotification
26
+ from .v2_all import FileChangePatchUpdatedNotification
27
+ from .v2_all import FsChangedNotification
28
+ from .v2_all import FuzzyFileSearchSessionCompletedNotification
29
+ from .v2_all import FuzzyFileSearchSessionUpdatedNotification
30
+ from .v2_all import GatewayOAuthChangedNotification
31
+ from .v2_all import GuardianWarningNotification
32
+ from .v2_all import HookCompletedNotification
33
+ from .v2_all import HookStartedNotification
34
+ from .v2_all import ItemCompletedNotification
35
+ from .v2_all import ItemGuardianApprovalReviewCompletedNotification
36
+ from .v2_all import ItemGuardianApprovalReviewStartedNotification
37
+ from .v2_all import ItemStartedNotification
38
+ from .v2_all import McpServerEventStreamNotification
39
+ from .v2_all import McpServerOauthLoginCompletedNotification
40
+ from .v2_all import McpServerStatusUpdatedNotification
41
+ from .v2_all import McpToolCallProgressNotification
42
+ from .v2_all import ModelReroutedNotification
43
+ from .v2_all import ModelSafetyBufferingUpdatedNotification
44
+ from .v2_all import ModelVerificationNotification
45
+ from .v2_all import PlanDeltaNotification
46
+ from .v2_all import ProcessExitedNotification
47
+ from .v2_all import ProcessOutputDeltaNotification
48
+ from .v2_all import ProjectChangedNotification
49
+ from .v2_all import ReasoningSummaryPartAddedNotification
50
+ from .v2_all import ReasoningSummaryTextDeltaNotification
51
+ from .v2_all import ReasoningTextDeltaNotification
52
+ from .v2_all import RemoteControlStatusChangedNotification
53
+ from .v2_all import ServerRequestResolvedNotification
54
+ from .v2_all import SkillsChangedNotification
55
+ from .v2_all import StrictReviewRequiredNotification
56
+ from .v2_all import TerminalInteractionNotification
57
+ from .v2_all import ThreadArchivedNotification
58
+ from .v2_all import ThreadAttachmentUpdatedNotification
59
+ from .v2_all import ThreadClosedNotification
60
+ from .v2_all import ThreadDeletedNotification
61
+ from .v2_all import ThreadGoalClearedNotification
62
+ from .v2_all import ThreadGoalUpdatedNotification
63
+ from .v2_all import ThreadNameUpdatedNotification
64
+ from .v2_all import ThreadPredictionUpdatedNotification
65
+ from .v2_all import ThreadProjectUpdatedNotification
66
+ from .v2_all import ThreadQueueChangedNotification
67
+ from .v2_all import ThreadReadStateChangedNotification
68
+ from .v2_all import ThreadRealtimeClosedNotification
69
+ from .v2_all import ThreadRealtimeErrorNotification
70
+ from .v2_all import ThreadRealtimeItemAddedNotification
71
+ from .v2_all import ThreadRealtimeItemCompletedNotification
72
+ from .v2_all import ThreadRealtimeItemStartedNotification
73
+ from .v2_all import ThreadRealtimeItemTranscriptDeltaNotification
74
+ from .v2_all import ThreadRealtimeOutputAudioDeltaNotification
75
+ from .v2_all import ThreadRealtimeSdpNotification
76
+ from .v2_all import ThreadRealtimeStartedNotification
77
+ from .v2_all import ThreadRealtimeTranscriptDeltaNotification
78
+ from .v2_all import ThreadRealtimeTranscriptDoneNotification
79
+ from .v2_all import ThreadRevertedNotification
80
+ from .v2_all import ThreadSectionChangedNotification
81
+ from .v2_all import ThreadSettingsUpdatedNotification
82
+ from .v2_all import ThreadStartedNotification
83
+ from .v2_all import ThreadStatusChangedNotification
84
+ from .v2_all import ThreadTokenUsageUpdatedNotification
85
+ from .v2_all import ThreadUnarchivedNotification
86
+ from .v2_all import TurnCompletedNotification
87
+ from .v2_all import TurnDiffUpdatedNotification
88
+ from .v2_all import TurnModerationMetadataNotification
89
+ from .v2_all import TurnPlanUpdatedNotification
90
+ from .v2_all import TurnStartedNotification
91
+ from .v2_all import WarningNotification
92
+ from .v2_all import WindowsSandboxSetupCompletedNotification
93
+ from .v2_all import WindowsWorldWritableWarningNotification
94
+
95
+ KnownNotificationPayload: TypeAlias = (
96
+ AccountLoginCompletedNotification
97
+ | AccountRateLimitsUpdatedNotification
98
+ | AccountUpdatedNotification
99
+ | AgentMessageDeltaNotification
100
+ | AppListUpdatedNotification
101
+ | AuthRecoveryNotification
102
+ | CommandExecOutputDeltaNotification
103
+ | CommandExecutionOutputDeltaNotification
104
+ | ConfigWarningNotification
105
+ | ContextCompactedNotification
106
+ | DeprecationNoticeNotification
107
+ | EnvironmentConnectionNotification
108
+ | ErrorNotification
109
+ | ExternalAgentConfigImportCompletedNotification
110
+ | ExternalAgentConfigImportProgressNotification
111
+ | FileChangeOutputDeltaNotification
112
+ | FileChangePatchUpdatedNotification
113
+ | FsChangedNotification
114
+ | FuzzyFileSearchSessionCompletedNotification
115
+ | FuzzyFileSearchSessionUpdatedNotification
116
+ | GatewayOAuthChangedNotification
117
+ | GuardianWarningNotification
118
+ | HookCompletedNotification
119
+ | HookStartedNotification
120
+ | ItemCompletedNotification
121
+ | ItemGuardianApprovalReviewCompletedNotification
122
+ | ItemGuardianApprovalReviewStartedNotification
123
+ | ItemStartedNotification
124
+ | McpServerEventStreamNotification
125
+ | McpServerOauthLoginCompletedNotification
126
+ | McpServerStatusUpdatedNotification
127
+ | McpToolCallProgressNotification
128
+ | ModelReroutedNotification
129
+ | ModelSafetyBufferingUpdatedNotification
130
+ | ModelVerificationNotification
131
+ | PlanDeltaNotification
132
+ | ProcessExitedNotification
133
+ | ProcessOutputDeltaNotification
134
+ | ProjectChangedNotification
135
+ | ReasoningSummaryPartAddedNotification
136
+ | ReasoningSummaryTextDeltaNotification
137
+ | ReasoningTextDeltaNotification
138
+ | RemoteControlStatusChangedNotification
139
+ | ServerRequestResolvedNotification
140
+ | SkillsChangedNotification
141
+ | StrictReviewRequiredNotification
142
+ | TerminalInteractionNotification
143
+ | ThreadArchivedNotification
144
+ | ThreadAttachmentUpdatedNotification
145
+ | ThreadClosedNotification
146
+ | ThreadDeletedNotification
147
+ | ThreadGoalClearedNotification
148
+ | ThreadGoalUpdatedNotification
149
+ | ThreadNameUpdatedNotification
150
+ | ThreadPredictionUpdatedNotification
151
+ | ThreadProjectUpdatedNotification
152
+ | ThreadQueueChangedNotification
153
+ | ThreadReadStateChangedNotification
154
+ | ThreadRealtimeClosedNotification
155
+ | ThreadRealtimeErrorNotification
156
+ | ThreadRealtimeItemAddedNotification
157
+ | ThreadRealtimeItemCompletedNotification
158
+ | ThreadRealtimeItemStartedNotification
159
+ | ThreadRealtimeItemTranscriptDeltaNotification
160
+ | ThreadRealtimeOutputAudioDeltaNotification
161
+ | ThreadRealtimeSdpNotification
162
+ | ThreadRealtimeStartedNotification
163
+ | ThreadRealtimeTranscriptDeltaNotification
164
+ | ThreadRealtimeTranscriptDoneNotification
165
+ | ThreadRevertedNotification
166
+ | ThreadSectionChangedNotification
167
+ | ThreadSettingsUpdatedNotification
168
+ | ThreadStartedNotification
169
+ | ThreadStatusChangedNotification
170
+ | ThreadTokenUsageUpdatedNotification
171
+ | ThreadUnarchivedNotification
172
+ | TurnCompletedNotification
173
+ | TurnDiffUpdatedNotification
174
+ | TurnModerationMetadataNotification
175
+ | TurnPlanUpdatedNotification
176
+ | TurnStartedNotification
177
+ | WarningNotification
178
+ | WindowsSandboxSetupCompletedNotification
179
+ | WindowsWorldWritableWarningNotification
180
+ )
181
+
182
+ NOTIFICATION_MODELS: dict[str, type[KnownNotificationPayload]] = {
183
+ "account/gatewayOAuth/changed": GatewayOAuthChangedNotification,
184
+ "account/login/completed": AccountLoginCompletedNotification,
185
+ "account/rateLimits/updated": AccountRateLimitsUpdatedNotification,
186
+ "account/updated": AccountUpdatedNotification,
187
+ "app/list/updated": AppListUpdatedNotification,
188
+ "autoApprovalReview/strictReviewRequired": StrictReviewRequiredNotification,
189
+ "command/exec/outputDelta": CommandExecOutputDeltaNotification,
190
+ "configWarning": ConfigWarningNotification,
191
+ "deprecationNotice": DeprecationNoticeNotification,
192
+ "error": ErrorNotification,
193
+ "externalAgentConfig/import/completed": ExternalAgentConfigImportCompletedNotification,
194
+ "externalAgentConfig/import/progress": ExternalAgentConfigImportProgressNotification,
195
+ "fs/changed": FsChangedNotification,
196
+ "fuzzyFileSearch/sessionCompleted": FuzzyFileSearchSessionCompletedNotification,
197
+ "fuzzyFileSearch/sessionUpdated": FuzzyFileSearchSessionUpdatedNotification,
198
+ "guardianWarning": GuardianWarningNotification,
199
+ "hook/completed": HookCompletedNotification,
200
+ "hook/started": HookStartedNotification,
201
+ "item/agentMessage/delta": AgentMessageDeltaNotification,
202
+ "item/autoApprovalReview/completed": ItemGuardianApprovalReviewCompletedNotification,
203
+ "item/autoApprovalReview/started": ItemGuardianApprovalReviewStartedNotification,
204
+ "item/commandExecution/outputDelta": CommandExecutionOutputDeltaNotification,
205
+ "item/commandExecution/terminalInteraction": TerminalInteractionNotification,
206
+ "item/completed": ItemCompletedNotification,
207
+ "item/fileChange/outputDelta": FileChangeOutputDeltaNotification,
208
+ "item/fileChange/patchUpdated": FileChangePatchUpdatedNotification,
209
+ "item/mcpToolCall/progress": McpToolCallProgressNotification,
210
+ "item/plan/delta": PlanDeltaNotification,
211
+ "item/reasoning/summaryPartAdded": ReasoningSummaryPartAddedNotification,
212
+ "item/reasoning/summaryTextDelta": ReasoningSummaryTextDeltaNotification,
213
+ "item/reasoning/textDelta": ReasoningTextDeltaNotification,
214
+ "item/started": ItemStartedNotification,
215
+ "mcpServer/event/stream/notification": McpServerEventStreamNotification,
216
+ "mcpServer/oauthLogin/completed": McpServerOauthLoginCompletedNotification,
217
+ "mcpServer/startupStatus/updated": McpServerStatusUpdatedNotification,
218
+ "model/rerouted": ModelReroutedNotification,
219
+ "model/safetyBuffering/updated": ModelSafetyBufferingUpdatedNotification,
220
+ "model/verification": ModelVerificationNotification,
221
+ "modelProvider/authRecoveryCompleted": AuthRecoveryNotification,
222
+ "modelProvider/authRecoveryStarted": AuthRecoveryNotification,
223
+ "process/exited": ProcessExitedNotification,
224
+ "process/outputDelta": ProcessOutputDeltaNotification,
225
+ "project/changed": ProjectChangedNotification,
226
+ "remoteControl/status/changed": RemoteControlStatusChangedNotification,
227
+ "serverRequest/resolved": ServerRequestResolvedNotification,
228
+ "skills/changed": SkillsChangedNotification,
229
+ "thread/archived": ThreadArchivedNotification,
230
+ "thread/attachment/updated": ThreadAttachmentUpdatedNotification,
231
+ "thread/closed": ThreadClosedNotification,
232
+ "thread/compacted": ContextCompactedNotification,
233
+ "thread/deleted": ThreadDeletedNotification,
234
+ "thread/environment/connected": EnvironmentConnectionNotification,
235
+ "thread/environment/disconnected": EnvironmentConnectionNotification,
236
+ "thread/goal/cleared": ThreadGoalClearedNotification,
237
+ "thread/goal/updated": ThreadGoalUpdatedNotification,
238
+ "thread/name/updated": ThreadNameUpdatedNotification,
239
+ "thread/prediction/updated": ThreadPredictionUpdatedNotification,
240
+ "thread/project/updated": ThreadProjectUpdatedNotification,
241
+ "thread/queue/changed": ThreadQueueChangedNotification,
242
+ "thread/readState/changed": ThreadReadStateChangedNotification,
243
+ "thread/realtime/closed": ThreadRealtimeClosedNotification,
244
+ "thread/realtime/error": ThreadRealtimeErrorNotification,
245
+ "thread/realtime/item/completed": ThreadRealtimeItemCompletedNotification,
246
+ "thread/realtime/item/started": ThreadRealtimeItemStartedNotification,
247
+ "thread/realtime/item/transcript/delta": ThreadRealtimeItemTranscriptDeltaNotification,
248
+ "thread/realtime/itemAdded": ThreadRealtimeItemAddedNotification,
249
+ "thread/realtime/outputAudio/delta": ThreadRealtimeOutputAudioDeltaNotification,
250
+ "thread/realtime/sdp": ThreadRealtimeSdpNotification,
251
+ "thread/realtime/started": ThreadRealtimeStartedNotification,
252
+ "thread/realtime/transcript/delta": ThreadRealtimeTranscriptDeltaNotification,
253
+ "thread/realtime/transcript/done": ThreadRealtimeTranscriptDoneNotification,
254
+ "thread/reverted": ThreadRevertedNotification,
255
+ "thread/section/changed": ThreadSectionChangedNotification,
256
+ "thread/settings/updated": ThreadSettingsUpdatedNotification,
257
+ "thread/started": ThreadStartedNotification,
258
+ "thread/status/changed": ThreadStatusChangedNotification,
259
+ "thread/tokenUsage/updated": ThreadTokenUsageUpdatedNotification,
260
+ "thread/unarchived": ThreadUnarchivedNotification,
261
+ "turn/completed": TurnCompletedNotification,
262
+ "turn/diff/updated": TurnDiffUpdatedNotification,
263
+ "turn/moderationMetadata": TurnModerationMetadataNotification,
264
+ "turn/plan/updated": TurnPlanUpdatedNotification,
265
+ "turn/started": TurnStartedNotification,
266
+ "warning": WarningNotification,
267
+ "windows/worldWritableWarning": WindowsWorldWritableWarningNotification,
268
+ "windowsSandbox/setupCompleted": WindowsSandboxSetupCompletedNotification,
269
+ }
270
+
271
+ DIRECT_TURN_ID_NOTIFICATION_TYPES: tuple[type[BaseModel], ...] = (
272
+ AgentMessageDeltaNotification,
273
+ AuthRecoveryNotification,
274
+ CommandExecutionOutputDeltaNotification,
275
+ ContextCompactedNotification,
276
+ ErrorNotification,
277
+ FileChangeOutputDeltaNotification,
278
+ FileChangePatchUpdatedNotification,
279
+ HookCompletedNotification,
280
+ HookStartedNotification,
281
+ ItemCompletedNotification,
282
+ ItemGuardianApprovalReviewCompletedNotification,
283
+ ItemGuardianApprovalReviewStartedNotification,
284
+ ItemStartedNotification,
285
+ McpToolCallProgressNotification,
286
+ ModelReroutedNotification,
287
+ ModelSafetyBufferingUpdatedNotification,
288
+ ModelVerificationNotification,
289
+ PlanDeltaNotification,
290
+ ReasoningSummaryPartAddedNotification,
291
+ ReasoningSummaryTextDeltaNotification,
292
+ ReasoningTextDeltaNotification,
293
+ StrictReviewRequiredNotification,
294
+ TerminalInteractionNotification,
295
+ ThreadGoalUpdatedNotification,
296
+ ThreadTokenUsageUpdatedNotification,
297
+ TurnDiffUpdatedNotification,
298
+ TurnModerationMetadataNotification,
299
+ TurnPlanUpdatedNotification,
300
+ )
301
+
302
+ NESTED_TURN_NOTIFICATION_TYPES: tuple[type[BaseModel], ...] = (
303
+ TurnCompletedNotification,
304
+ TurnStartedNotification,
305
+ )
306
+
307
+
308
+ def notification_turn_id(payload: BaseModel) -> str | None:
309
+ """Return the turn id carried by generated notification payload metadata."""
310
+ if isinstance(payload, DIRECT_TURN_ID_NOTIFICATION_TYPES):
311
+ return payload.turn_id if isinstance(payload.turn_id, str) else None
312
+ if isinstance(payload, NESTED_TURN_NOTIFICATION_TYPES):
313
+ return payload.turn.id
314
+ return None