codexed 0.1.0__tar.gz → 0.1.2__tar.gz
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.
- {codexed-0.1.0 → codexed-0.1.2}/PKG-INFO +1 -1
- {codexed-0.1.0 → codexed-0.1.2}/pyproject.toml +1 -1
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/__init__.py +6 -13
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/client.py +55 -266
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/__init__.py +2 -4
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/misc.py +1 -23
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/request_params.py +32 -2
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/responses.py +14 -6
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/tool_config.py +33 -30
- codexed-0.1.2/src/codexed/session.py +234 -0
- {codexed-0.1.0 → codexed-0.1.2}/LICENSE +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/README.md +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/README.md +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/exceptions.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/helpers.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/log.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/base.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/codex_types.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/command_action.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/event_data.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/events.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/fuzzy_search.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/hooks.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/mcp_server.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/realtime.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/response_item.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/terminal.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/thread_item.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/thread_status.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/token_usage.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/user_input.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/models/web_search.py +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/py.typed +0 -0
- {codexed-0.1.0 → codexed-0.1.2}/src/codexed/request_handlers.py +0 -0
|
@@ -21,7 +21,8 @@ __all__ = [
|
|
|
21
21
|
]
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
from codexed.client import CodexClient
|
|
24
|
+
from codexed.client import CodexClient
|
|
25
|
+
from codexed.session import Session
|
|
25
26
|
from codexed.exceptions import (
|
|
26
27
|
CodexError,
|
|
27
28
|
CodexProcessError,
|
|
@@ -54,7 +55,7 @@ from codexed.models import (
|
|
|
54
55
|
ReviewStartResponse,
|
|
55
56
|
SkillData,
|
|
56
57
|
ThreadArchivedData,
|
|
57
|
-
|
|
58
|
+
Thread,
|
|
58
59
|
ThreadListResponse,
|
|
59
60
|
ThreadNameUpdatedData,
|
|
60
61
|
ThreadReadResponse,
|
|
@@ -142,16 +143,8 @@ from codexed.models.events import (
|
|
|
142
143
|
TurnPlanUpdatedEvent,
|
|
143
144
|
TurnStartedEvent,
|
|
144
145
|
)
|
|
145
|
-
from codexed.models.mcp_server import
|
|
146
|
-
|
|
147
|
-
McpServerConfig,
|
|
148
|
-
StdioMcpServer,
|
|
149
|
-
)
|
|
150
|
-
from codexed.models.tool_config import (
|
|
151
|
-
BuiltinToolsConfig,
|
|
152
|
-
ToolConfig,
|
|
153
|
-
tools_to_config_dict,
|
|
154
|
-
)
|
|
146
|
+
from codexed.models.mcp_server import HttpMcpServer, McpServerConfig, StdioMcpServer
|
|
147
|
+
from codexed.models.tool_config import BuiltinToolsConfig, ToolConfig, tools_to_config_dict
|
|
155
148
|
from codexed.request_handlers import (
|
|
156
149
|
ApprovalHandler,
|
|
157
150
|
ApprovalParams,
|
|
@@ -242,11 +235,11 @@ __all__ = [
|
|
|
242
235
|
"SkillData",
|
|
243
236
|
"SkillScope",
|
|
244
237
|
"StdioMcpServer",
|
|
238
|
+
"Thread",
|
|
245
239
|
"ThreadActiveFlag",
|
|
246
240
|
"ThreadArchivedData",
|
|
247
241
|
"ThreadArchivedEvent",
|
|
248
242
|
"ThreadCompactedEvent",
|
|
249
|
-
"ThreadData",
|
|
250
243
|
"ThreadListResponse",
|
|
251
244
|
"ThreadNameUpdatedData",
|
|
252
245
|
"ThreadNameUpdatedEvent",
|
|
@@ -89,8 +89,6 @@ from codexed.models import (
|
|
|
89
89
|
ThreadRollbackResponse,
|
|
90
90
|
ThreadSetNameParams,
|
|
91
91
|
ThreadStartParams,
|
|
92
|
-
ThreadTokenUsageUpdatedData,
|
|
93
|
-
ThreadTokenUsageUpdatedEvent,
|
|
94
92
|
ThreadUnarchiveParams,
|
|
95
93
|
ThreadUnarchiveResponse,
|
|
96
94
|
ThreadUnsubscribeParams,
|
|
@@ -107,6 +105,7 @@ from codexed.models import (
|
|
|
107
105
|
UserInputText,
|
|
108
106
|
codex_event_adapter,
|
|
109
107
|
)
|
|
108
|
+
from codexed.models.tool_config import tools_to_config_dict
|
|
110
109
|
from codexed.request_handlers import (
|
|
111
110
|
SERVER_REQUEST_COMMAND_APPROVAL,
|
|
112
111
|
SERVER_REQUEST_DYNAMIC_TOOL_CALL,
|
|
@@ -145,7 +144,6 @@ if TYPE_CHECKING:
|
|
|
145
144
|
SkillData,
|
|
146
145
|
ThreadSortKey,
|
|
147
146
|
ThreadSourceKind,
|
|
148
|
-
ThreadTokenUsage,
|
|
149
147
|
ToolConfig,
|
|
150
148
|
Turn,
|
|
151
149
|
UserInput,
|
|
@@ -159,206 +157,11 @@ if TYPE_CHECKING:
|
|
|
159
157
|
ServerRequestHandler,
|
|
160
158
|
UserInputHandler,
|
|
161
159
|
)
|
|
160
|
+
from codexed.session import Session
|
|
162
161
|
|
|
163
162
|
logger = logging.getLogger(__name__)
|
|
164
163
|
|
|
165
164
|
|
|
166
|
-
class Session:
|
|
167
|
-
"""Runtime handle for an active Codex thread.
|
|
168
|
-
|
|
169
|
-
Wraps a :class:`CodexClient` and a ``thread_id`` so that every
|
|
170
|
-
thread-scoped operation can be called without passing the ID again.
|
|
171
|
-
|
|
172
|
-
Returned by :meth:`CodexClient.thread_start`,
|
|
173
|
-
:meth:`CodexClient.thread_resume`, and :meth:`CodexClient.thread_fork`.
|
|
174
|
-
|
|
175
|
-
Example::
|
|
176
|
-
|
|
177
|
-
session = await client.thread_start(cwd="/my/project")
|
|
178
|
-
async for event in session.turn_stream("Explain this repo"):
|
|
179
|
-
...
|
|
180
|
-
await session.set_name("exploration")
|
|
181
|
-
"""
|
|
182
|
-
|
|
183
|
-
def __init__(self, client: CodexClient, response: ThreadResponse) -> None:
|
|
184
|
-
self._client = client
|
|
185
|
-
self.response = response
|
|
186
|
-
self.usage: ThreadTokenUsage | None = None
|
|
187
|
-
|
|
188
|
-
@property
|
|
189
|
-
def thread_id(self) -> str:
|
|
190
|
-
"""The underlying thread ID."""
|
|
191
|
-
return self.response.thread.id
|
|
192
|
-
|
|
193
|
-
# -- Turn operations -----------------------------------------------------
|
|
194
|
-
|
|
195
|
-
async def turn_stream(
|
|
196
|
-
self,
|
|
197
|
-
user_input: str | list[UserInput],
|
|
198
|
-
*,
|
|
199
|
-
model: str | None = None,
|
|
200
|
-
effort: ReasoningEffort | None = None,
|
|
201
|
-
approval_policy: ApprovalPolicy | None = None,
|
|
202
|
-
cwd: str | None = None,
|
|
203
|
-
sandbox_policy: SandboxMode | dict[str, Any] | None = None,
|
|
204
|
-
output_schema: dict[str, Any] | type[Any] | None = None,
|
|
205
|
-
personality: Personality | None = None,
|
|
206
|
-
summary: ReasoningSummary | None = None,
|
|
207
|
-
collaboration_mode: CollaborationMode | None = None,
|
|
208
|
-
) -> AsyncIterator[CodexEvent]:
|
|
209
|
-
"""Start a turn and stream events. See :meth:`CodexClient.turn_stream`."""
|
|
210
|
-
async for event in self._client.turn_stream(
|
|
211
|
-
self.thread_id,
|
|
212
|
-
user_input,
|
|
213
|
-
model=model,
|
|
214
|
-
effort=effort,
|
|
215
|
-
approval_policy=approval_policy,
|
|
216
|
-
cwd=cwd,
|
|
217
|
-
sandbox_policy=sandbox_policy,
|
|
218
|
-
output_schema=output_schema,
|
|
219
|
-
personality=personality,
|
|
220
|
-
summary=summary,
|
|
221
|
-
collaboration_mode=collaboration_mode,
|
|
222
|
-
):
|
|
223
|
-
match event:
|
|
224
|
-
case ThreadTokenUsageUpdatedEvent(
|
|
225
|
-
data=ThreadTokenUsageUpdatedData(token_usage=usage)
|
|
226
|
-
):
|
|
227
|
-
self.usage = usage
|
|
228
|
-
yield event
|
|
229
|
-
|
|
230
|
-
async def turn_steer(
|
|
231
|
-
self,
|
|
232
|
-
user_input: str | list[UserInput],
|
|
233
|
-
*,
|
|
234
|
-
expected_turn_id: str,
|
|
235
|
-
) -> TurnSteerResponse:
|
|
236
|
-
"""Steer a running turn. See :meth:`CodexClient.turn_steer`."""
|
|
237
|
-
return await self._client.turn_steer(
|
|
238
|
-
self.thread_id,
|
|
239
|
-
user_input,
|
|
240
|
-
expected_turn_id=expected_turn_id,
|
|
241
|
-
)
|
|
242
|
-
|
|
243
|
-
async def turn_interrupt(self, turn_id: str) -> None:
|
|
244
|
-
"""Interrupt a running turn. See :meth:`CodexClient.turn_interrupt`."""
|
|
245
|
-
await self._client.turn_interrupt(self.thread_id, turn_id)
|
|
246
|
-
|
|
247
|
-
async def turn_stream_structured[ResultType: BaseModel](
|
|
248
|
-
self,
|
|
249
|
-
user_input: str | list[UserInput],
|
|
250
|
-
result_type: type[ResultType],
|
|
251
|
-
*,
|
|
252
|
-
model: str | None = None,
|
|
253
|
-
effort: ReasoningEffort | None = None,
|
|
254
|
-
approval_policy: ApprovalPolicy | None = None,
|
|
255
|
-
cwd: str | None = None,
|
|
256
|
-
sandbox_policy: SandboxMode | dict[str, Any] | None = None,
|
|
257
|
-
personality: Personality | None = None,
|
|
258
|
-
summary: ReasoningSummary | None = None,
|
|
259
|
-
collaboration_mode: CollaborationMode | None = None,
|
|
260
|
-
) -> ResultType:
|
|
261
|
-
"""Structured-output turn. See :meth:`CodexClient.turn_stream_structured`."""
|
|
262
|
-
return await self._client.turn_stream_structured(
|
|
263
|
-
self.thread_id,
|
|
264
|
-
user_input,
|
|
265
|
-
result_type,
|
|
266
|
-
model=model,
|
|
267
|
-
effort=effort,
|
|
268
|
-
approval_policy=approval_policy,
|
|
269
|
-
cwd=cwd,
|
|
270
|
-
sandbox_policy=sandbox_policy,
|
|
271
|
-
personality=personality,
|
|
272
|
-
summary=summary,
|
|
273
|
-
collaboration_mode=collaboration_mode,
|
|
274
|
-
)
|
|
275
|
-
|
|
276
|
-
# -- Thread operations ---------------------------------------------------
|
|
277
|
-
|
|
278
|
-
async def read(self, *, include_turns: bool = False) -> ThreadReadResponse:
|
|
279
|
-
"""Read thread data. See :meth:`CodexClient.thread_read`."""
|
|
280
|
-
return await self._client.thread_read(self.thread_id, include_turns=include_turns)
|
|
281
|
-
|
|
282
|
-
async def unsubscribe(self) -> ThreadUnsubscribeResponse:
|
|
283
|
-
"""Unsubscribe from thread events. See :meth:`CodexClient.thread_unsubscribe`."""
|
|
284
|
-
return await self._client.thread_unsubscribe(self.thread_id)
|
|
285
|
-
|
|
286
|
-
async def archive(self) -> None:
|
|
287
|
-
"""Archive this thread. See :meth:`CodexClient.thread_archive`."""
|
|
288
|
-
await self._client.thread_archive(self.thread_id)
|
|
289
|
-
|
|
290
|
-
async def unarchive(self) -> ThreadUnarchiveResponse:
|
|
291
|
-
"""Unarchive this thread. See :meth:`CodexClient.thread_unarchive`."""
|
|
292
|
-
return await self._client.thread_unarchive(self.thread_id)
|
|
293
|
-
|
|
294
|
-
async def set_name(self, name: str) -> None:
|
|
295
|
-
"""Set thread name. See :meth:`CodexClient.thread_set_name`."""
|
|
296
|
-
await self._client.thread_set_name(self.thread_id, name)
|
|
297
|
-
|
|
298
|
-
async def compact_start(self) -> None:
|
|
299
|
-
"""Trigger context compaction. See :meth:`CodexClient.thread_compact_start`."""
|
|
300
|
-
await self._client.thread_compact_start(self.thread_id)
|
|
301
|
-
|
|
302
|
-
async def rollback(self, turns: int) -> ThreadRollbackResponse:
|
|
303
|
-
"""Rollback last N turns. See :meth:`CodexClient.thread_rollback`."""
|
|
304
|
-
return await self._client.thread_rollback(self.thread_id, turns)
|
|
305
|
-
|
|
306
|
-
async def rollback_to_turn(self, turn_id: str) -> ThreadRollbackResponse:
|
|
307
|
-
"""Rollback to a specific turn. See :meth:`CodexClient.thread_rollback_to_turn`."""
|
|
308
|
-
return await self._client.thread_rollback_to_turn(self.thread_id, turn_id)
|
|
309
|
-
|
|
310
|
-
# -- Review --------------------------------------------------------------
|
|
311
|
-
|
|
312
|
-
async def review_start(
|
|
313
|
-
self,
|
|
314
|
-
target: ReviewTarget,
|
|
315
|
-
*,
|
|
316
|
-
delivery: ReviewDelivery | None = None,
|
|
317
|
-
) -> ReviewStartResponse:
|
|
318
|
-
"""Start a code review. See :meth:`CodexClient.review_start`."""
|
|
319
|
-
return await self._client.review_start(self.thread_id, target, delivery=delivery)
|
|
320
|
-
|
|
321
|
-
# -- Fork ----------------------------------------------------------------
|
|
322
|
-
|
|
323
|
-
async def fork(
|
|
324
|
-
self,
|
|
325
|
-
*,
|
|
326
|
-
path: str | None = None,
|
|
327
|
-
cwd: str | None = None,
|
|
328
|
-
model: str | None = None,
|
|
329
|
-
model_provider: str | None = None,
|
|
330
|
-
base_instructions: str | None = None,
|
|
331
|
-
developer_instructions: str | None = None,
|
|
332
|
-
approval_policy: ApprovalPolicy | None = None,
|
|
333
|
-
sandbox: SandboxMode | None = None,
|
|
334
|
-
config: dict[str, Any] | None = None,
|
|
335
|
-
tools: list[ToolConfig] | None = None,
|
|
336
|
-
code_mode: bool | None = None,
|
|
337
|
-
personality: Personality | None = None,
|
|
338
|
-
turn_id: str | None = None,
|
|
339
|
-
) -> Session:
|
|
340
|
-
"""Fork this thread. See :meth:`CodexClient.thread_fork`."""
|
|
341
|
-
return await self._client.thread_fork(
|
|
342
|
-
self.thread_id,
|
|
343
|
-
path=path,
|
|
344
|
-
cwd=cwd,
|
|
345
|
-
model=model,
|
|
346
|
-
model_provider=model_provider,
|
|
347
|
-
base_instructions=base_instructions,
|
|
348
|
-
developer_instructions=developer_instructions,
|
|
349
|
-
approval_policy=approval_policy,
|
|
350
|
-
sandbox=sandbox,
|
|
351
|
-
config=config,
|
|
352
|
-
tools=tools,
|
|
353
|
-
code_mode=code_mode,
|
|
354
|
-
personality=personality,
|
|
355
|
-
turn_id=turn_id,
|
|
356
|
-
)
|
|
357
|
-
|
|
358
|
-
def __repr__(self) -> str:
|
|
359
|
-
return f"Session(thread_id={self.thread_id!r})"
|
|
360
|
-
|
|
361
|
-
|
|
362
165
|
class CodexClient:
|
|
363
166
|
"""Client for the Codex app-server JSON-RPC protocol.
|
|
364
167
|
|
|
@@ -378,6 +181,7 @@ class CodexClient:
|
|
|
378
181
|
on_user_input: UserInputHandler | None = None,
|
|
379
182
|
on_dynamic_tool_call: DynamicToolCallHandler | None = None,
|
|
380
183
|
on_mcp_elicitation: McpElicitationHandler | None = None,
|
|
184
|
+
mcp_elicitation_for_approvals: bool = False,
|
|
381
185
|
) -> None:
|
|
382
186
|
"""Initialize the Codex app-server client.
|
|
383
187
|
|
|
@@ -392,6 +196,13 @@ class CodexClient:
|
|
|
392
196
|
on_user_input: Handler for tool user input requests.
|
|
393
197
|
on_dynamic_tool_call: Handler for dynamic tool call requests.
|
|
394
198
|
on_mcp_elicitation: Handler for MCP elicitation requests.
|
|
199
|
+
mcp_elicitation_for_approvals: If True, enables the Codex
|
|
200
|
+
``tool_call_mcp_elicitation`` feature flag which routes MCP tool
|
|
201
|
+
approval prompts through ``mcpServer/elicitation/request`` instead
|
|
202
|
+
of ``item/tool/requestUserInput``. Requires *on_mcp_elicitation*
|
|
203
|
+
to handle approvals (the schema is empty; context is in ``_meta``).
|
|
204
|
+
Default is False because the default elicitation handler auto-declines,
|
|
205
|
+
which would break MCP tool approvals.
|
|
395
206
|
"""
|
|
396
207
|
self._codex_command = codex_command
|
|
397
208
|
self._profile = profile
|
|
@@ -405,6 +216,8 @@ class CodexClient:
|
|
|
405
216
|
self._reader_task: asyncio.Task[None] | None = None
|
|
406
217
|
self._writer_lock = asyncio.Lock()
|
|
407
218
|
self._active_threads: set[str] = set()
|
|
219
|
+
self._mcp_elicitation_enabled = on_mcp_elicitation is not None
|
|
220
|
+
self._mcp_elicitation_for_approvals = mcp_elicitation_for_approvals
|
|
408
221
|
self._server_request_handlers: dict[str, ServerRequestHandler] = {}
|
|
409
222
|
if on_approval:
|
|
410
223
|
self.register_handler(SERVER_REQUEST_COMMAND_APPROVAL, on_approval) # type: ignore[arg-type]
|
|
@@ -502,6 +315,7 @@ class CodexClient:
|
|
|
502
315
|
tools: list[ToolConfig] | None,
|
|
503
316
|
code_mode: bool | None,
|
|
504
317
|
mcp_servers: Mapping[str, McpServerConfig] | None = None,
|
|
318
|
+
mcp_elicitation_for_approvals: bool = False,
|
|
505
319
|
) -> dict[str, Any] | None:
|
|
506
320
|
"""Merge tools, code_mode, and mcp_servers into a config dict."""
|
|
507
321
|
merged = dict(config) if config else {}
|
|
@@ -509,9 +323,9 @@ class CodexClient:
|
|
|
509
323
|
merged.setdefault("features", {})["code_mode"] = code_mode
|
|
510
324
|
merged.setdefault("features", {})["codex_hooks"] = True
|
|
511
325
|
merged.setdefault("features", {})["realtime_conversation"] = True
|
|
326
|
+
if mcp_elicitation_for_approvals:
|
|
327
|
+
merged.setdefault("features", {})["tool_call_mcp_elicitation"] = True
|
|
512
328
|
if tools is not None:
|
|
513
|
-
from codexed.models.tool_config import tools_to_config_dict
|
|
514
|
-
|
|
515
329
|
tool_config = tools_to_config_dict(tools)
|
|
516
330
|
for key, value in tool_config.items():
|
|
517
331
|
if key not in merged:
|
|
@@ -566,6 +380,8 @@ class CodexClient:
|
|
|
566
380
|
Returns:
|
|
567
381
|
Session wrapping the new thread
|
|
568
382
|
"""
|
|
383
|
+
from codexed.session import Session
|
|
384
|
+
|
|
569
385
|
params = ThreadStartParams(
|
|
570
386
|
cwd=cwd,
|
|
571
387
|
model=model,
|
|
@@ -574,7 +390,13 @@ class CodexClient:
|
|
|
574
390
|
developer_instructions=developer_instructions,
|
|
575
391
|
approval_policy=approval_policy,
|
|
576
392
|
sandbox=sandbox,
|
|
577
|
-
config=self._merge_config(
|
|
393
|
+
config=self._merge_config(
|
|
394
|
+
config,
|
|
395
|
+
tools,
|
|
396
|
+
code_mode,
|
|
397
|
+
mcp_servers,
|
|
398
|
+
mcp_elicitation_for_approvals=self._mcp_elicitation_for_approvals,
|
|
399
|
+
),
|
|
578
400
|
service_name=service_name,
|
|
579
401
|
personality=personality,
|
|
580
402
|
ephemeral=ephemeral,
|
|
@@ -623,6 +445,15 @@ class CodexClient:
|
|
|
623
445
|
Returns:
|
|
624
446
|
Session wrapping the resumed thread
|
|
625
447
|
"""
|
|
448
|
+
from codexed.session import Session
|
|
449
|
+
|
|
450
|
+
cfg = self._merge_config(
|
|
451
|
+
config,
|
|
452
|
+
tools,
|
|
453
|
+
code_mode,
|
|
454
|
+
mcp_servers,
|
|
455
|
+
mcp_elicitation_for_approvals=self._mcp_elicitation_for_approvals,
|
|
456
|
+
)
|
|
626
457
|
params = ThreadResumeParams(
|
|
627
458
|
thread_id=thread_id,
|
|
628
459
|
path=path,
|
|
@@ -633,7 +464,7 @@ class CodexClient:
|
|
|
633
464
|
developer_instructions=developer_instructions,
|
|
634
465
|
approval_policy=approval_policy,
|
|
635
466
|
sandbox=sandbox,
|
|
636
|
-
config=
|
|
467
|
+
config=cfg,
|
|
637
468
|
personality=personality,
|
|
638
469
|
)
|
|
639
470
|
result = await self._send_request("thread/resume", params)
|
|
@@ -682,6 +513,15 @@ class CodexClient:
|
|
|
682
513
|
Returns:
|
|
683
514
|
Session wrapping the new forked thread
|
|
684
515
|
"""
|
|
516
|
+
from codexed.session import Session
|
|
517
|
+
|
|
518
|
+
cfg = self._merge_config(
|
|
519
|
+
config,
|
|
520
|
+
tools,
|
|
521
|
+
code_mode,
|
|
522
|
+
mcp_servers,
|
|
523
|
+
mcp_elicitation_for_approvals=self._mcp_elicitation_for_approvals,
|
|
524
|
+
)
|
|
685
525
|
params = ThreadForkParams(
|
|
686
526
|
thread_id=thread_id,
|
|
687
527
|
path=path,
|
|
@@ -692,7 +532,7 @@ class CodexClient:
|
|
|
692
532
|
developer_instructions=developer_instructions,
|
|
693
533
|
approval_policy=approval_policy,
|
|
694
534
|
sandbox=sandbox,
|
|
695
|
-
config=
|
|
535
|
+
config=cfg,
|
|
696
536
|
personality=personality,
|
|
697
537
|
)
|
|
698
538
|
result = await self._send_request("thread/fork", params)
|
|
@@ -824,18 +664,12 @@ class CodexClient:
|
|
|
824
664
|
"""
|
|
825
665
|
thread_response = await self.thread_read(thread_id, include_turns=True)
|
|
826
666
|
turns = thread_response.thread.turns
|
|
827
|
-
turn_index
|
|
828
|
-
for i, turn in enumerate(turns):
|
|
829
|
-
if turn.id == turn_id:
|
|
830
|
-
turn_index = i
|
|
831
|
-
break
|
|
667
|
+
turn_index = next((i for i, turn in enumerate(turns) if turn.id == turn_id), None)
|
|
832
668
|
if turn_index is None:
|
|
833
|
-
|
|
834
|
-
raise ValueError(msg)
|
|
669
|
+
raise ValueError(f"Turn {turn_id!r} not found in thread {thread_id!r}")
|
|
835
670
|
num_turns_to_drop = len(turns) - turn_index - 1
|
|
836
671
|
if num_turns_to_drop < 1:
|
|
837
|
-
|
|
838
|
-
raise ValueError(msg)
|
|
672
|
+
raise ValueError(f"Turn {turn_id!r} is already the last turn in thread {thread_id!r}")
|
|
839
673
|
return await self.thread_rollback(thread_id, num_turns_to_drop)
|
|
840
674
|
|
|
841
675
|
# ========================================================================
|
|
@@ -1008,22 +842,6 @@ class CodexClient:
|
|
|
1008
842
|
Raises:
|
|
1009
843
|
ValidationError: If the agent's response doesn't match the schema
|
|
1010
844
|
CodexRequestError: If the turn fails
|
|
1011
|
-
|
|
1012
|
-
Example:
|
|
1013
|
-
class FileInfo(BaseModel):
|
|
1014
|
-
name: str
|
|
1015
|
-
type: str
|
|
1016
|
-
|
|
1017
|
-
class FileList(BaseModel):
|
|
1018
|
-
files: list[FileInfo]
|
|
1019
|
-
total: int
|
|
1020
|
-
|
|
1021
|
-
result = await client.turn_stream_structured(
|
|
1022
|
-
thread.id,
|
|
1023
|
-
"List Python files in current directory",
|
|
1024
|
-
FileList, # Must be a Pydantic type, not a dict
|
|
1025
|
-
)
|
|
1026
|
-
print(f"Found {result.total} files: {result.files}")
|
|
1027
845
|
"""
|
|
1028
846
|
turn: Turn | None = None
|
|
1029
847
|
async for event in self.turn_stream(
|
|
@@ -1046,12 +864,10 @@ class CodexClient:
|
|
|
1046
864
|
raise TurnFailedError(error, turn_id="unknown")
|
|
1047
865
|
|
|
1048
866
|
if turn is None:
|
|
1049
|
-
|
|
1050
|
-
raise CodexRequestError(code=-1, message=msg)
|
|
867
|
+
raise CodexRequestError(code=-1, message="Turn completed without a TurnCompletedEvent")
|
|
1051
868
|
response_text = turn.final_response
|
|
1052
869
|
if response_text is None:
|
|
1053
|
-
|
|
1054
|
-
raise CodexRequestError(code=-1, message=msg)
|
|
870
|
+
raise CodexRequestError(code=-1, message="Turn completed without a final response")
|
|
1055
871
|
return result_type.model_validate_json(response_text)
|
|
1056
872
|
|
|
1057
873
|
# ========================================================================
|
|
@@ -1310,25 +1126,15 @@ class CodexClient:
|
|
|
1310
1126
|
params = FsRemoveParams(path=path, recursive=recursive, force=force)
|
|
1311
1127
|
await self._send_request("fs/remove", params)
|
|
1312
1128
|
|
|
1313
|
-
async def fs_copy(
|
|
1314
|
-
self,
|
|
1315
|
-
source_path: str,
|
|
1316
|
-
destination_path: str,
|
|
1317
|
-
*,
|
|
1318
|
-
recursive: bool = False,
|
|
1319
|
-
) -> None:
|
|
1129
|
+
async def fs_copy(self, source: str, destination: str, *, recursive: bool = False) -> None:
|
|
1320
1130
|
"""Copy a file or directory tree on the host filesystem.
|
|
1321
1131
|
|
|
1322
1132
|
Args:
|
|
1323
|
-
|
|
1324
|
-
|
|
1133
|
+
source: Absolute source path.
|
|
1134
|
+
destination: Absolute destination path.
|
|
1325
1135
|
recursive: Required for directory copies; ignored for file copies.
|
|
1326
1136
|
"""
|
|
1327
|
-
params = FsCopyParams(
|
|
1328
|
-
source_path=source_path,
|
|
1329
|
-
destination_path=destination_path,
|
|
1330
|
-
recursive=recursive,
|
|
1331
|
-
)
|
|
1137
|
+
params = FsCopyParams(source_path=source, destination_path=destination, recursive=recursive)
|
|
1332
1138
|
await self._send_request("fs/copy", params)
|
|
1333
1139
|
|
|
1334
1140
|
# ========================================================================
|
|
@@ -1729,10 +1535,7 @@ class CodexClient:
|
|
|
1729
1535
|
message: Raw JSON-RPC message
|
|
1730
1536
|
"""
|
|
1731
1537
|
match message:
|
|
1732
|
-
case {
|
|
1733
|
-
"method": _,
|
|
1734
|
-
"id": _,
|
|
1735
|
-
}: # Server request - the server is asking us to do something
|
|
1538
|
+
case {"method": _, "id": _}: # Server request - the server is asking us to do something
|
|
1736
1539
|
await self._handle_server_request(message)
|
|
1737
1540
|
case {"id": _}: # Response to one of our requests
|
|
1738
1541
|
self._handle_response(message)
|
|
@@ -1803,13 +1606,8 @@ class CodexClient:
|
|
|
1803
1606
|
|
|
1804
1607
|
params_type, _ = type_entry
|
|
1805
1608
|
handler = self._server_request_handlers.get(method)
|
|
1806
|
-
|
|
1807
1609
|
if handler is None:
|
|
1808
|
-
logger.warning(
|
|
1809
|
-
"No handler registered for server request: %s (id=%s)",
|
|
1810
|
-
method,
|
|
1811
|
-
request_id,
|
|
1812
|
-
)
|
|
1610
|
+
logger.warning("No handler registered for request: %s (id=%s)", method, request_id)
|
|
1813
1611
|
await self._send_server_request_error(request_id, -32603, f"No handler for: {method}")
|
|
1814
1612
|
return
|
|
1815
1613
|
|
|
@@ -1862,15 +1660,6 @@ class CodexClient:
|
|
|
1862
1660
|
Args:
|
|
1863
1661
|
method: Server request method name (use SERVER_REQUEST_* constants)
|
|
1864
1662
|
handler: Async callback that processes the request and returns a response
|
|
1865
|
-
|
|
1866
|
-
Example::
|
|
1867
|
-
|
|
1868
|
-
async def handle_approval(
|
|
1869
|
-
params: CommandExecutionRequestApprovalParams,
|
|
1870
|
-
) -> CommandExecutionRequestApprovalResponse:
|
|
1871
|
-
return CommandExecutionRequestApprovalResponse(decision="allow")
|
|
1872
|
-
|
|
1873
|
-
client.register_handler(SERVER_REQUEST_COMMAND_APPROVAL, handle_approval)
|
|
1874
1663
|
"""
|
|
1875
1664
|
if method not in SERVER_REQUEST_TYPES:
|
|
1876
1665
|
msg = (
|
|
@@ -262,7 +262,6 @@ from codexed.models.misc import (
|
|
|
262
262
|
SkillsContainer,
|
|
263
263
|
SkillToolDependency,
|
|
264
264
|
Thread,
|
|
265
|
-
ThreadData,
|
|
266
265
|
ToolRequestUserInputAnswer,
|
|
267
266
|
ToolRequestUserInputOption,
|
|
268
267
|
ToolRequestUserInputQuestion,
|
|
@@ -453,7 +452,7 @@ from codexed.models.tool_config import (
|
|
|
453
452
|
ToolSearchToolConfig,
|
|
454
453
|
ToolSuggestToolConfig,
|
|
455
454
|
ViewImageToolConfig,
|
|
456
|
-
|
|
455
|
+
WebSearchLocation,
|
|
457
456
|
WebSearchToolConfig,
|
|
458
457
|
tools_to_config_dict,
|
|
459
458
|
)
|
|
@@ -807,7 +806,6 @@ __all__ = [
|
|
|
807
806
|
"ThreadCompactStartParams",
|
|
808
807
|
"ThreadCompactedData",
|
|
809
808
|
"ThreadCompactedEvent",
|
|
810
|
-
"ThreadData",
|
|
811
809
|
"ThreadForkParams",
|
|
812
810
|
"ThreadItem",
|
|
813
811
|
"ThreadItemAgentMessage",
|
|
@@ -904,7 +902,7 @@ __all__ = [
|
|
|
904
902
|
"WebSearchActionOther",
|
|
905
903
|
"WebSearchActionSearch",
|
|
906
904
|
"WebSearchCallResponseItem",
|
|
907
|
-
"
|
|
905
|
+
"WebSearchLocation",
|
|
908
906
|
"WebSearchToolConfig",
|
|
909
907
|
"WindowsWorldWritableWarningData",
|
|
910
908
|
"WindowsWorldWritableWarningEvent",
|
|
@@ -13,7 +13,6 @@ from codexed.models.codex_types import ( # noqa: TC001
|
|
|
13
13
|
InputModality,
|
|
14
14
|
McpAuthStatusValue,
|
|
15
15
|
MergeStrategy,
|
|
16
|
-
ModelProvider,
|
|
17
16
|
NetworkApprovalProtocol,
|
|
18
17
|
NetworkPolicyRuleAction,
|
|
19
18
|
PlanType,
|
|
@@ -209,7 +208,7 @@ class Turn(CodexBaseModel):
|
|
|
209
208
|
|
|
210
209
|
|
|
211
210
|
class Thread(CodexBaseModel):
|
|
212
|
-
"""Thread data structure."""
|
|
211
|
+
"""Thread data structure (matches upstream Codex Thread type)."""
|
|
213
212
|
|
|
214
213
|
id: str
|
|
215
214
|
preview: str = ""
|
|
@@ -229,27 +228,6 @@ class Thread(CodexBaseModel):
|
|
|
229
228
|
turns: list[Turn] = Field(default_factory=list)
|
|
230
229
|
|
|
231
230
|
|
|
232
|
-
class ThreadData(CodexBaseModel):
|
|
233
|
-
"""Thread data in responses."""
|
|
234
|
-
|
|
235
|
-
id: str
|
|
236
|
-
preview: str = ""
|
|
237
|
-
ephemeral: bool = False
|
|
238
|
-
model_provider: ModelProvider = "openai"
|
|
239
|
-
created_at: int = 0
|
|
240
|
-
updated_at: int = 0
|
|
241
|
-
status: ThreadStatusValue | None = None
|
|
242
|
-
path: str | None = None
|
|
243
|
-
cwd: str | None = None
|
|
244
|
-
cli_version: str | None = None
|
|
245
|
-
source: str | None = None
|
|
246
|
-
agent_nickname: str | None = None
|
|
247
|
-
agent_role: str | None = None
|
|
248
|
-
git_info: GitInfo | None = None
|
|
249
|
-
name: str | None = None
|
|
250
|
-
turns: list[Turn] = Field(default_factory=list)
|
|
251
|
-
|
|
252
|
-
|
|
253
231
|
class TurnData(CodexBaseModel):
|
|
254
232
|
"""Turn data in responses."""
|
|
255
233
|
|
|
@@ -521,6 +521,26 @@ class _McpElicitationBase(CodexBaseModel):
|
|
|
521
521
|
turn_id: str | None = None
|
|
522
522
|
server_name: str
|
|
523
523
|
meta: Any | None = None
|
|
524
|
+
"""Optional metadata from the server.
|
|
525
|
+
|
|
526
|
+
For MCP tool approval requests (when ``tool_call_mcp_elicitation`` is enabled),
|
|
527
|
+
this contains:
|
|
528
|
+
|
|
529
|
+
- ``kind`` (str): Always ``"mcp_tool_call"`` for tool approvals.
|
|
530
|
+
- ``persist`` (str | list[str]): Allowed persistence options —
|
|
531
|
+
``"session"`` (remember for this session) and/or ``"always"``
|
|
532
|
+
(remember permanently).
|
|
533
|
+
- ``tool_title`` (str, optional): Human-readable tool name.
|
|
534
|
+
- ``tool_description`` (str, optional): Tool description.
|
|
535
|
+
- ``tool_params`` (dict, optional): The tool call arguments.
|
|
536
|
+
- ``tool_params_display`` (list, optional): Rendered parameter display.
|
|
537
|
+
- ``source`` (str, optional): ``"connector"`` if from a ChatGPT App.
|
|
538
|
+
- ``connector_id``, ``connector_name``, ``connector_description``
|
|
539
|
+
(str, optional): Connector details when source is ``"connector"``.
|
|
540
|
+
|
|
541
|
+
For regular MCP server elicitations (auth flows etc.), the meta content
|
|
542
|
+
is defined by the MCP server itself.
|
|
543
|
+
"""
|
|
524
544
|
message: str
|
|
525
545
|
|
|
526
546
|
|
|
@@ -531,10 +551,13 @@ class McpElicitationFormParams(_McpElicitationBase):
|
|
|
531
551
|
requested_schema: dict[str, Any]
|
|
532
552
|
|
|
533
553
|
def to_mcp(self) -> mcp.types.ElicitRequestFormParams:
|
|
534
|
-
|
|
554
|
+
params = mcp.types.ElicitRequestFormParams(
|
|
535
555
|
message=self.message,
|
|
536
556
|
requestedSchema=self.requested_schema,
|
|
537
557
|
)
|
|
558
|
+
if self.meta is not None:
|
|
559
|
+
params.meta = mcp.types.RequestParams.Meta(**self.meta)
|
|
560
|
+
return params
|
|
538
561
|
|
|
539
562
|
@classmethod
|
|
540
563
|
def from_mcp(
|
|
@@ -545,9 +568,11 @@ class McpElicitationFormParams(_McpElicitationBase):
|
|
|
545
568
|
turn_id: str | None = None,
|
|
546
569
|
) -> McpElicitationFormParams:
|
|
547
570
|
"""Create from MCP ElicitRequestFormParams."""
|
|
571
|
+
meta = params.meta.model_dump(exclude={"progressToken"}) if params.meta else None
|
|
548
572
|
return cls(
|
|
549
573
|
message=params.message,
|
|
550
574
|
requested_schema=dict(params.requestedSchema),
|
|
575
|
+
meta=meta,
|
|
551
576
|
thread_id=thread_id,
|
|
552
577
|
server_name=server_name,
|
|
553
578
|
turn_id=turn_id,
|
|
@@ -562,11 +587,14 @@ class McpElicitationUrlParams(_McpElicitationBase):
|
|
|
562
587
|
elicitation_id: str
|
|
563
588
|
|
|
564
589
|
def to_mcp(self) -> mcp.types.ElicitRequestURLParams:
|
|
565
|
-
|
|
590
|
+
params = mcp.types.ElicitRequestURLParams(
|
|
566
591
|
message=self.message,
|
|
567
592
|
url=self.url,
|
|
568
593
|
elicitationId=self.elicitation_id,
|
|
569
594
|
)
|
|
595
|
+
if self.meta is not None:
|
|
596
|
+
params.meta = mcp.types.RequestParams.Meta(**self.meta)
|
|
597
|
+
return params
|
|
570
598
|
|
|
571
599
|
@classmethod
|
|
572
600
|
def from_mcp(
|
|
@@ -577,10 +605,12 @@ class McpElicitationUrlParams(_McpElicitationBase):
|
|
|
577
605
|
turn_id: str | None = None,
|
|
578
606
|
) -> McpElicitationUrlParams:
|
|
579
607
|
"""Create from MCP ElicitRequestURLParams."""
|
|
608
|
+
meta = params.meta.model_dump(exclude={"progressToken"}) if params.meta else None
|
|
580
609
|
return cls(
|
|
581
610
|
message=params.message,
|
|
582
611
|
url=params.url,
|
|
583
612
|
elicitation_id=params.elicitationId,
|
|
613
|
+
meta=meta,
|
|
584
614
|
thread_id=thread_id,
|
|
585
615
|
server_name=server_name,
|
|
586
616
|
turn_id=turn_id,
|
|
@@ -23,7 +23,7 @@ from codexed.models.misc import ( # noqa: TC001
|
|
|
23
23
|
McpServerStatusEntry,
|
|
24
24
|
ModelData,
|
|
25
25
|
SkillsContainer,
|
|
26
|
-
|
|
26
|
+
Thread,
|
|
27
27
|
ToolRequestUserInputAnswer,
|
|
28
28
|
Turn,
|
|
29
29
|
TurnData,
|
|
@@ -62,6 +62,14 @@ class McpServerElicitationResponse(CodexBaseModel):
|
|
|
62
62
|
action: Literal["accept", "decline", "cancel"]
|
|
63
63
|
content: Any | None = None
|
|
64
64
|
meta: Any | None = None
|
|
65
|
+
"""Optional metadata to include in the response.
|
|
66
|
+
|
|
67
|
+
For MCP tool approval responses, this can contain:
|
|
68
|
+
|
|
69
|
+
- ``persist`` (str): ``"session"`` to remember the approval for
|
|
70
|
+
the current session, or ``"always"`` to remember permanently.
|
|
71
|
+
Must be one of the options listed in the request's ``meta.persist``.
|
|
72
|
+
"""
|
|
65
73
|
|
|
66
74
|
def to_mcp(self) -> mcp.types.ElicitResult:
|
|
67
75
|
return mcp.types.ElicitResult(action=self.action, content=self.content)
|
|
@@ -75,13 +83,13 @@ class McpServerElicitationResponse(CodexBaseModel):
|
|
|
75
83
|
class ThreadReadResponse(CodexBaseModel):
|
|
76
84
|
"""Response for thread/read request."""
|
|
77
85
|
|
|
78
|
-
thread:
|
|
86
|
+
thread: Thread
|
|
79
87
|
|
|
80
88
|
|
|
81
89
|
class ThreadResponse(CodexBaseModel):
|
|
82
90
|
"""Response for thread/start, thread/resume, and thread/fork."""
|
|
83
91
|
|
|
84
|
-
thread:
|
|
92
|
+
thread: Thread
|
|
85
93
|
model: str
|
|
86
94
|
model_provider: str
|
|
87
95
|
cwd: str
|
|
@@ -113,7 +121,7 @@ class ReviewStartResponse(CodexBaseModel):
|
|
|
113
121
|
class ThreadListResponse(CodexBaseModel):
|
|
114
122
|
"""Response for thread/list request."""
|
|
115
123
|
|
|
116
|
-
data: list[
|
|
124
|
+
data: list[Thread]
|
|
117
125
|
next_cursor: str | None = None
|
|
118
126
|
|
|
119
127
|
|
|
@@ -126,14 +134,14 @@ class ThreadLoadedListResponse(CodexBaseModel):
|
|
|
126
134
|
class ThreadRollbackResponse(CodexBaseModel):
|
|
127
135
|
"""Response for thread/rollback request."""
|
|
128
136
|
|
|
129
|
-
thread:
|
|
137
|
+
thread: Thread
|
|
130
138
|
turns: list[Turn]
|
|
131
139
|
|
|
132
140
|
|
|
133
141
|
class ThreadUnarchiveResponse(CodexBaseModel):
|
|
134
142
|
"""Response for thread/unarchive request."""
|
|
135
143
|
|
|
136
|
-
thread:
|
|
144
|
+
thread: Thread
|
|
137
145
|
|
|
138
146
|
|
|
139
147
|
class SkillsListResponse(CodexBaseModel):
|
|
@@ -11,9 +11,9 @@ into the ``config`` dict accepted by ``ThreadStartParams``.
|
|
|
11
11
|
|
|
12
12
|
from __future__ import annotations
|
|
13
13
|
|
|
14
|
-
from typing import Annotated, Any, Literal
|
|
14
|
+
from typing import Annotated, Any, Literal, assert_never
|
|
15
15
|
|
|
16
|
-
from pydantic import BaseModel, ConfigDict, Discriminator
|
|
16
|
+
from pydantic import BaseModel, ConfigDict, Discriminator
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
class _ToolConfigBase(BaseModel):
|
|
@@ -55,7 +55,7 @@ class ApplyPatchToolConfig(_ToolConfigBase):
|
|
|
55
55
|
# ---------------------------------------------------------------------------
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
class
|
|
58
|
+
class WebSearchLocation(_ToolConfigBase):
|
|
59
59
|
"""Location hints for web search results."""
|
|
60
60
|
|
|
61
61
|
country: str | None = None
|
|
@@ -78,7 +78,7 @@ class WebSearchToolConfig(_ToolConfigBase):
|
|
|
78
78
|
allowed_domains: list[str] | None = None
|
|
79
79
|
"""Restrict search to these domains only."""
|
|
80
80
|
|
|
81
|
-
location:
|
|
81
|
+
location: WebSearchLocation | None = None
|
|
82
82
|
"""Approximate user location for localised results."""
|
|
83
83
|
|
|
84
84
|
|
|
@@ -259,24 +259,24 @@ class McpResourceToolsConfig(_ToolConfigBase):
|
|
|
259
259
|
# ===========================================================================
|
|
260
260
|
|
|
261
261
|
ToolConfig = Annotated[
|
|
262
|
-
|
|
263
|
-
|
|
|
264
|
-
|
|
|
265
|
-
|
|
|
266
|
-
|
|
|
267
|
-
|
|
|
268
|
-
|
|
|
269
|
-
|
|
|
270
|
-
|
|
|
271
|
-
|
|
|
272
|
-
|
|
|
273
|
-
|
|
|
274
|
-
|
|
|
275
|
-
|
|
|
276
|
-
|
|
|
277
|
-
|
|
|
278
|
-
|
|
|
279
|
-
|
|
|
262
|
+
ShellToolConfig
|
|
263
|
+
| ApplyPatchToolConfig
|
|
264
|
+
| WebSearchToolConfig
|
|
265
|
+
| ImageGenerationToolConfig
|
|
266
|
+
| ViewImageToolConfig
|
|
267
|
+
| PlanToolConfig
|
|
268
|
+
| JsReplToolConfig
|
|
269
|
+
| CollabToolsConfig
|
|
270
|
+
| AgentJobsToolsConfig
|
|
271
|
+
| RequestUserInputToolConfig
|
|
272
|
+
| RequestPermissionsToolConfig
|
|
273
|
+
| ArtifactsToolConfig
|
|
274
|
+
| GrepFilesToolConfig
|
|
275
|
+
| ReadFileToolConfig
|
|
276
|
+
| ListDirToolConfig
|
|
277
|
+
| ToolSearchToolConfig
|
|
278
|
+
| ToolSuggestToolConfig
|
|
279
|
+
| McpResourceToolsConfig,
|
|
280
280
|
Discriminator("type"),
|
|
281
281
|
]
|
|
282
282
|
"""Discriminated union of all builtin tool configurations."""
|
|
@@ -340,10 +340,15 @@ def tools_to_config_dict(tools: list[ToolConfig]) -> dict[str, Any]:
|
|
|
340
340
|
|
|
341
341
|
case ApplyPatchToolConfig(variant=variant):
|
|
342
342
|
config["include_apply_patch_tool"] = True
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
343
|
+
match variant:
|
|
344
|
+
case "freeform":
|
|
345
|
+
features["apply_patch_freeform"] = True
|
|
346
|
+
case "function":
|
|
347
|
+
features["apply_patch_freeform"] = False
|
|
348
|
+
case None:
|
|
349
|
+
pass
|
|
350
|
+
case _ as unreachable:
|
|
351
|
+
assert_never(unreachable)
|
|
347
352
|
|
|
348
353
|
case WebSearchToolConfig(
|
|
349
354
|
mode=mode,
|
|
@@ -358,10 +363,8 @@ def tools_to_config_dict(tools: list[ToolConfig]) -> dict[str, Any]:
|
|
|
358
363
|
ws_config["context_size"] = context_size
|
|
359
364
|
if allowed_domains is not None:
|
|
360
365
|
ws_config["allowed_domains"] = allowed_domains
|
|
361
|
-
if location is not None:
|
|
362
|
-
|
|
363
|
-
if loc:
|
|
364
|
-
ws_config["location"] = loc
|
|
366
|
+
if location is not None and (loc := location.model_dump(exclude_none=True)):
|
|
367
|
+
ws_config["location"] = loc
|
|
365
368
|
if ws_config:
|
|
366
369
|
tools_section["web_search"] = ws_config
|
|
367
370
|
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""Codex app-server client."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator # noqa: TC003
|
|
6
|
+
import logging
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel
|
|
10
|
+
|
|
11
|
+
from codexed.models import ThreadTokenUsageUpdatedData, ThreadTokenUsageUpdatedEvent
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from codexed.client import CodexClient
|
|
16
|
+
from codexed.models import (
|
|
17
|
+
ApprovalPolicy,
|
|
18
|
+
CodexEvent,
|
|
19
|
+
CollaborationMode,
|
|
20
|
+
Personality,
|
|
21
|
+
ReasoningEffort,
|
|
22
|
+
ReasoningSummary,
|
|
23
|
+
ReviewDelivery,
|
|
24
|
+
ReviewStartResponse,
|
|
25
|
+
ReviewTarget,
|
|
26
|
+
SandboxMode,
|
|
27
|
+
ThreadReadResponse,
|
|
28
|
+
ThreadResponse,
|
|
29
|
+
ThreadRollbackResponse,
|
|
30
|
+
ThreadTokenUsage,
|
|
31
|
+
ThreadUnarchiveResponse,
|
|
32
|
+
ThreadUnsubscribeResponse,
|
|
33
|
+
ToolConfig,
|
|
34
|
+
TurnSteerResponse,
|
|
35
|
+
UserInput,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Session:
|
|
42
|
+
"""Runtime handle for an active Codex thread.
|
|
43
|
+
|
|
44
|
+
Wraps a :class:`CodexClient` and a ``thread_id`` so that every
|
|
45
|
+
thread-scoped operation can be called without passing the ID again.
|
|
46
|
+
|
|
47
|
+
Returned by :meth:`CodexClient.thread_start`,
|
|
48
|
+
:meth:`CodexClient.thread_resume`, and :meth:`CodexClient.thread_fork`.
|
|
49
|
+
|
|
50
|
+
Example::
|
|
51
|
+
|
|
52
|
+
session = await client.thread_start(cwd="/my/project")
|
|
53
|
+
async for event in session.turn_stream("Explain this repo"):
|
|
54
|
+
...
|
|
55
|
+
await session.set_name("exploration")
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(self, client: CodexClient, response: ThreadResponse) -> None:
|
|
59
|
+
self._client = client
|
|
60
|
+
self.response = response
|
|
61
|
+
self.usage: ThreadTokenUsage | None = None
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def thread_id(self) -> str:
|
|
65
|
+
"""The underlying thread ID."""
|
|
66
|
+
return self.response.thread.id
|
|
67
|
+
|
|
68
|
+
# -- Turn operations -----------------------------------------------------
|
|
69
|
+
|
|
70
|
+
async def turn_stream(
|
|
71
|
+
self,
|
|
72
|
+
user_input: str | list[UserInput],
|
|
73
|
+
*,
|
|
74
|
+
model: str | None = None,
|
|
75
|
+
effort: ReasoningEffort | None = None,
|
|
76
|
+
approval_policy: ApprovalPolicy | None = None,
|
|
77
|
+
cwd: str | None = None,
|
|
78
|
+
sandbox_policy: SandboxMode | dict[str, Any] | None = None,
|
|
79
|
+
output_schema: dict[str, Any] | type[Any] | None = None,
|
|
80
|
+
personality: Personality | None = None,
|
|
81
|
+
summary: ReasoningSummary | None = None,
|
|
82
|
+
collaboration_mode: CollaborationMode | None = None,
|
|
83
|
+
) -> AsyncIterator[CodexEvent]:
|
|
84
|
+
"""Start a turn and stream events. See :meth:`CodexClient.turn_stream`."""
|
|
85
|
+
async for event in self._client.turn_stream(
|
|
86
|
+
self.thread_id,
|
|
87
|
+
user_input,
|
|
88
|
+
model=model,
|
|
89
|
+
effort=effort,
|
|
90
|
+
approval_policy=approval_policy,
|
|
91
|
+
cwd=cwd,
|
|
92
|
+
sandbox_policy=sandbox_policy,
|
|
93
|
+
output_schema=output_schema,
|
|
94
|
+
personality=personality,
|
|
95
|
+
summary=summary,
|
|
96
|
+
collaboration_mode=collaboration_mode,
|
|
97
|
+
):
|
|
98
|
+
match event:
|
|
99
|
+
case ThreadTokenUsageUpdatedEvent(
|
|
100
|
+
data=ThreadTokenUsageUpdatedData(token_usage=usage)
|
|
101
|
+
):
|
|
102
|
+
self.usage = usage
|
|
103
|
+
yield event
|
|
104
|
+
|
|
105
|
+
async def turn_steer(
|
|
106
|
+
self,
|
|
107
|
+
user_input: str | list[UserInput],
|
|
108
|
+
*,
|
|
109
|
+
expected_turn_id: str,
|
|
110
|
+
) -> TurnSteerResponse:
|
|
111
|
+
"""Steer a running turn. See :meth:`CodexClient.turn_steer`."""
|
|
112
|
+
return await self._client.turn_steer(
|
|
113
|
+
self.thread_id,
|
|
114
|
+
user_input,
|
|
115
|
+
expected_turn_id=expected_turn_id,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
async def turn_interrupt(self, turn_id: str) -> None:
|
|
119
|
+
"""Interrupt a running turn. See :meth:`CodexClient.turn_interrupt`."""
|
|
120
|
+
await self._client.turn_interrupt(self.thread_id, turn_id)
|
|
121
|
+
|
|
122
|
+
async def turn_stream_structured[ResultType: BaseModel](
|
|
123
|
+
self,
|
|
124
|
+
user_input: str | list[UserInput],
|
|
125
|
+
result_type: type[ResultType],
|
|
126
|
+
*,
|
|
127
|
+
model: str | None = None,
|
|
128
|
+
effort: ReasoningEffort | None = None,
|
|
129
|
+
approval_policy: ApprovalPolicy | None = None,
|
|
130
|
+
cwd: str | None = None,
|
|
131
|
+
sandbox_policy: SandboxMode | dict[str, Any] | None = None,
|
|
132
|
+
personality: Personality | None = None,
|
|
133
|
+
summary: ReasoningSummary | None = None,
|
|
134
|
+
collaboration_mode: CollaborationMode | None = None,
|
|
135
|
+
) -> ResultType:
|
|
136
|
+
"""Structured-output turn. See :meth:`CodexClient.turn_stream_structured`."""
|
|
137
|
+
return await self._client.turn_stream_structured(
|
|
138
|
+
self.thread_id,
|
|
139
|
+
user_input,
|
|
140
|
+
result_type,
|
|
141
|
+
model=model,
|
|
142
|
+
effort=effort,
|
|
143
|
+
approval_policy=approval_policy,
|
|
144
|
+
cwd=cwd,
|
|
145
|
+
sandbox_policy=sandbox_policy,
|
|
146
|
+
personality=personality,
|
|
147
|
+
summary=summary,
|
|
148
|
+
collaboration_mode=collaboration_mode,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
# -- Thread operations ---------------------------------------------------
|
|
152
|
+
|
|
153
|
+
async def read(self, *, include_turns: bool = False) -> ThreadReadResponse:
|
|
154
|
+
"""Read thread data. See :meth:`CodexClient.thread_read`."""
|
|
155
|
+
return await self._client.thread_read(self.thread_id, include_turns=include_turns)
|
|
156
|
+
|
|
157
|
+
async def unsubscribe(self) -> ThreadUnsubscribeResponse:
|
|
158
|
+
"""Unsubscribe from thread events. See :meth:`CodexClient.thread_unsubscribe`."""
|
|
159
|
+
return await self._client.thread_unsubscribe(self.thread_id)
|
|
160
|
+
|
|
161
|
+
async def archive(self) -> None:
|
|
162
|
+
"""Archive this thread. See :meth:`CodexClient.thread_archive`."""
|
|
163
|
+
await self._client.thread_archive(self.thread_id)
|
|
164
|
+
|
|
165
|
+
async def unarchive(self) -> ThreadUnarchiveResponse:
|
|
166
|
+
"""Unarchive this thread. See :meth:`CodexClient.thread_unarchive`."""
|
|
167
|
+
return await self._client.thread_unarchive(self.thread_id)
|
|
168
|
+
|
|
169
|
+
async def set_name(self, name: str) -> None:
|
|
170
|
+
"""Set thread name. See :meth:`CodexClient.thread_set_name`."""
|
|
171
|
+
await self._client.thread_set_name(self.thread_id, name)
|
|
172
|
+
|
|
173
|
+
async def compact_start(self) -> None:
|
|
174
|
+
"""Trigger context compaction. See :meth:`CodexClient.thread_compact_start`."""
|
|
175
|
+
await self._client.thread_compact_start(self.thread_id)
|
|
176
|
+
|
|
177
|
+
async def rollback(self, turns: int) -> ThreadRollbackResponse:
|
|
178
|
+
"""Rollback last N turns. See :meth:`CodexClient.thread_rollback`."""
|
|
179
|
+
return await self._client.thread_rollback(self.thread_id, turns)
|
|
180
|
+
|
|
181
|
+
async def rollback_to_turn(self, turn_id: str) -> ThreadRollbackResponse:
|
|
182
|
+
"""Rollback to a specific turn. See :meth:`CodexClient.thread_rollback_to_turn`."""
|
|
183
|
+
return await self._client.thread_rollback_to_turn(self.thread_id, turn_id)
|
|
184
|
+
|
|
185
|
+
# -- Review --------------------------------------------------------------
|
|
186
|
+
|
|
187
|
+
async def review_start(
|
|
188
|
+
self,
|
|
189
|
+
target: ReviewTarget,
|
|
190
|
+
*,
|
|
191
|
+
delivery: ReviewDelivery | None = None,
|
|
192
|
+
) -> ReviewStartResponse:
|
|
193
|
+
"""Start a code review. See :meth:`CodexClient.review_start`."""
|
|
194
|
+
return await self._client.review_start(self.thread_id, target, delivery=delivery)
|
|
195
|
+
|
|
196
|
+
# -- Fork ----------------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
async def fork(
|
|
199
|
+
self,
|
|
200
|
+
*,
|
|
201
|
+
path: str | None = None,
|
|
202
|
+
cwd: str | None = None,
|
|
203
|
+
model: str | None = None,
|
|
204
|
+
model_provider: str | None = None,
|
|
205
|
+
base_instructions: str | None = None,
|
|
206
|
+
developer_instructions: str | None = None,
|
|
207
|
+
approval_policy: ApprovalPolicy | None = None,
|
|
208
|
+
sandbox: SandboxMode | None = None,
|
|
209
|
+
config: dict[str, Any] | None = None,
|
|
210
|
+
tools: list[ToolConfig] | None = None,
|
|
211
|
+
code_mode: bool | None = None,
|
|
212
|
+
personality: Personality | None = None,
|
|
213
|
+
turn_id: str | None = None,
|
|
214
|
+
) -> Session:
|
|
215
|
+
"""Fork this thread. See :meth:`CodexClient.thread_fork`."""
|
|
216
|
+
return await self._client.thread_fork(
|
|
217
|
+
self.thread_id,
|
|
218
|
+
path=path,
|
|
219
|
+
cwd=cwd,
|
|
220
|
+
model=model,
|
|
221
|
+
model_provider=model_provider,
|
|
222
|
+
base_instructions=base_instructions,
|
|
223
|
+
developer_instructions=developer_instructions,
|
|
224
|
+
approval_policy=approval_policy,
|
|
225
|
+
sandbox=sandbox,
|
|
226
|
+
config=config,
|
|
227
|
+
tools=tools,
|
|
228
|
+
code_mode=code_mode,
|
|
229
|
+
personality=personality,
|
|
230
|
+
turn_id=turn_id,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
def __repr__(self) -> str:
|
|
234
|
+
return f"Session(thread_id={self.thread_id!r})"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|