clawd-code-sdk 1.0.28__tar.gz → 1.1.1__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.
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/PKG-INFO +1 -1
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/pyproject.toml +1 -1
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_internal/query.py +22 -249
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/client.py +143 -47
- clawd_code_sdk-1.1.1/src/clawd_code_sdk/instrumentation.py +198 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/context_usage.py +1 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/control.py +9 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/input_types.py +5 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/permissions.py +8 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/settings.py +6 -2
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/system_messages.py +69 -17
- clawd_code_sdk-1.0.28/src/clawd_code_sdk/instrumentation.py +0 -315
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/.gitignore +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/LICENSE +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/README.md +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_bundled/.gitignore +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_errors.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_internal/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_internal/transport/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_internal/transport/subprocess_cli.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_internal/transport/subprocess_utils.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/_version.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/anthropic_stream.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/list_sessions.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/mcp_utils.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/agents.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/anthropic_types.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/base.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/content_blocks.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/hooks.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/mcp.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/messages.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/options.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/output_types.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/prompt_requests.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/prompts.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/server_info.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/session.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/thinking.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/models/usage.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/py.typed +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/session.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/storage/ARCHITECTURE.md +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/storage/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/storage/helpers.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/storage/models.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/storage/replay.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/src/clawd_code_sdk/usage.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/conftest.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/__init__.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_agents_and_settings.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_dynamic_control.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_hook_events.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_hooks.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_include_partial_messages.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_mcp_resources.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_mcp_tools.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_sdk_mcp_resources.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_sdk_mcp_tools.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_slash_commands.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_stderr_callback.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_storage_parsing.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_structured_output.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_subagent_invocation.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/e2e/test_tool_permissions.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/mcp_server.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/mock_claude_server.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_changelog.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_client.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_errors.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_image.png +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_integration.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_message_parser.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_sdk_mcp_integration.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_session.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_streaming_client.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_subprocess_buffering.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_tool_callbacks.py +0 -0
- {clawd_code_sdk-1.0.28 → clawd_code_sdk-1.1.1}/tests/test_transport.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: clawd-code-sdk
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.1
|
|
4
4
|
Summary: Python SDK for Claude Code
|
|
5
5
|
Project-URL: Documentation, https://github.com/phil65/claude-agent-sdk-python
|
|
6
6
|
Project-URL: Homepage, https://github.com/phil65/claude-agent-sdk-python
|
|
@@ -18,8 +18,6 @@ from clawd_code_sdk._internal.transport.subprocess_cli import SubprocessCLITrans
|
|
|
18
18
|
from clawd_code_sdk.mcp_utils import process_mcp_request
|
|
19
19
|
from clawd_code_sdk.models import (
|
|
20
20
|
AskUserQuestionInput,
|
|
21
|
-
ClaudeCodeServerInfo,
|
|
22
|
-
ClaudeOAuthWaitForCompletionResponse,
|
|
23
21
|
ControlErrorResponse,
|
|
24
22
|
ControlResponse,
|
|
25
23
|
JSONRPCError,
|
|
@@ -27,12 +25,10 @@ from clawd_code_sdk.models import (
|
|
|
27
25
|
McpSdkServerConfigWithInstance,
|
|
28
26
|
PermissionResultAllow,
|
|
29
27
|
SDKControlElicitationRequest,
|
|
30
|
-
SDKControlInitializeRequest,
|
|
31
28
|
SDKControlMcpMessageRequest,
|
|
32
29
|
SDKControlPermissionRequest,
|
|
33
30
|
SDKControlResponse,
|
|
34
31
|
SDKHookCallbackRequest,
|
|
35
|
-
SideQuestionResponse,
|
|
36
32
|
ToolPermissionContext,
|
|
37
33
|
incoming_control_request_adapter,
|
|
38
34
|
)
|
|
@@ -45,10 +41,8 @@ if TYPE_CHECKING:
|
|
|
45
41
|
|
|
46
42
|
from clawd_code_sdk._internal.transport import Transport
|
|
47
43
|
from clawd_code_sdk.models import (
|
|
48
|
-
AgentDefinition,
|
|
49
44
|
CanUseTool,
|
|
50
45
|
ClaudeAgentOptions,
|
|
51
|
-
ClaudeCodeAgentInfo,
|
|
52
46
|
HookCallback,
|
|
53
47
|
HookEvent,
|
|
54
48
|
HookMatcher,
|
|
@@ -57,7 +51,7 @@ if TYPE_CHECKING:
|
|
|
57
51
|
JSONRPCResponse,
|
|
58
52
|
OnElicitation,
|
|
59
53
|
OnUserQuestion,
|
|
60
|
-
|
|
54
|
+
OutgoingControlRequest,
|
|
61
55
|
PermissionResult,
|
|
62
56
|
RequestId,
|
|
63
57
|
)
|
|
@@ -90,14 +84,6 @@ class Query:
|
|
|
90
84
|
on_elicitation: OnElicitation | None = None,
|
|
91
85
|
hooks: dict[HookEvent, list[HookMatcher]] | None = None,
|
|
92
86
|
sdk_mcp_servers: dict[str, McpServer] | None = None,
|
|
93
|
-
initialize_timeout: float = 60.0,
|
|
94
|
-
agents: dict[str, AgentDefinition] | None = None,
|
|
95
|
-
system_prompt: str | None = None,
|
|
96
|
-
append_system_prompt: str | None = None,
|
|
97
|
-
exclude_dynamic_sections: bool | None = None,
|
|
98
|
-
json_schema: dict[str, Any] | None = None,
|
|
99
|
-
prompt_suggestions: bool | None = None,
|
|
100
|
-
agent_progress_summaries: bool | None = None,
|
|
101
87
|
):
|
|
102
88
|
"""Initialize Query with transport and callbacks.
|
|
103
89
|
|
|
@@ -108,16 +94,7 @@ class Query:
|
|
|
108
94
|
on_elicitation: Optional callback for MCP elicitation requests
|
|
109
95
|
hooks: Optional hook configurations
|
|
110
96
|
sdk_mcp_servers: Optional SDK MCP server instances
|
|
111
|
-
initialize_timeout: Timeout in seconds for the initialize request
|
|
112
|
-
agents: Optional agent definitions to send via initialize
|
|
113
|
-
system_prompt: Optional system prompt to send via initialize
|
|
114
|
-
append_system_prompt: Optional text to append to preset system prompt
|
|
115
|
-
exclude_dynamic_sections: Exclude dynamic sections from the system prompt
|
|
116
|
-
json_schema: Optional JSON schema for structured output
|
|
117
|
-
prompt_suggestions: Optional flag to enable prompt suggestions
|
|
118
|
-
agent_progress_summaries: Optional flag to enable agent progress summaries
|
|
119
97
|
"""
|
|
120
|
-
self._initialize_timeout = initialize_timeout
|
|
121
98
|
self.transport = transport
|
|
122
99
|
self.can_use_tool = can_use_tool
|
|
123
100
|
self.on_user_question = on_user_question
|
|
@@ -127,13 +104,6 @@ class Query:
|
|
|
127
104
|
for event, matchers in (hooks or {}).items()
|
|
128
105
|
}
|
|
129
106
|
self.sdk_mcp_servers = sdk_mcp_servers or {}
|
|
130
|
-
self._agents = agents
|
|
131
|
-
self._system_prompt = system_prompt
|
|
132
|
-
self._append_system_prompt = append_system_prompt
|
|
133
|
-
self._exclude_dynamic_sections = exclude_dynamic_sections
|
|
134
|
-
self._json_schema = json_schema
|
|
135
|
-
self._prompt_suggestions = prompt_suggestions
|
|
136
|
-
self._agent_progress_summaries = agent_progress_summaries
|
|
137
107
|
# Control protocol state
|
|
138
108
|
self.pending_control_responses: dict[str, anyio.Event] = {}
|
|
139
109
|
self.pending_control_results: dict[str, dict[str, Any] | Exception] = {}
|
|
@@ -148,19 +118,12 @@ class Query:
|
|
|
148
118
|
self._child_tasks: set[asyncio.Task[Any]] = set()
|
|
149
119
|
self._inflight_requests: dict[str, asyncio.Task[Any]] = {}
|
|
150
120
|
self._closed = False
|
|
151
|
-
self._initialization_result: ClaudeCodeServerInfo | None = None
|
|
152
121
|
# Track first result for proper stream closure with SDK MCP servers
|
|
153
122
|
self._first_result_event = anyio.Event()
|
|
154
123
|
|
|
155
124
|
@classmethod
|
|
156
125
|
def from_options(cls, options: ClaudeAgentOptions, transport: Transport | None = None) -> Query:
|
|
157
|
-
|
|
158
|
-
if options.instrument:
|
|
159
|
-
from clawd_code_sdk.instrumentation import inject_tracing_hooks
|
|
160
|
-
|
|
161
|
-
hooks = inject_tracing_hooks(options.hooks)
|
|
162
|
-
else:
|
|
163
|
-
hooks = options.hooks or {}
|
|
126
|
+
hooks = options.hooks or {}
|
|
164
127
|
|
|
165
128
|
# If on_permission is a callback, extract it for Query and replace with
|
|
166
129
|
# "stdio" so the CLI routes permission requests through the control protocol.
|
|
@@ -176,22 +139,6 @@ class Query:
|
|
|
176
139
|
if isinstance(config, McpSdkServerConfigWithInstance):
|
|
177
140
|
sdk_mcp_servers[name] = config.instance
|
|
178
141
|
|
|
179
|
-
# Calculate initialize timeout from CLAUDE_CODE_STREAM_CLOSE_TIMEOUT env var if set
|
|
180
|
-
# CLAUDE_CODE_STREAM_CLOSE_TIMEOUT is in milliseconds, convert to seconds
|
|
181
|
-
initialize_timeout_ms = int(os.environ.get("CLAUDE_CODE_STREAM_CLOSE_TIMEOUT", "60000"))
|
|
182
|
-
initialize_timeout = max(initialize_timeout_ms / 1000.0, 60.0)
|
|
183
|
-
# Extract system prompt for initialize request
|
|
184
|
-
system_prompt: str | None = None
|
|
185
|
-
append_system_prompt: str | None = None
|
|
186
|
-
if options.system_prompt is None:
|
|
187
|
-
if not options.include_builtin_system_prompt:
|
|
188
|
-
system_prompt = "" # Clear the builtin prompt
|
|
189
|
-
# else: send nothing, CLI uses its default builtin prompt
|
|
190
|
-
elif options.include_builtin_system_prompt:
|
|
191
|
-
append_system_prompt = options.system_prompt
|
|
192
|
-
else:
|
|
193
|
-
system_prompt = options.system_prompt
|
|
194
|
-
|
|
195
142
|
# Create Query to handle control protocol
|
|
196
143
|
return cls(
|
|
197
144
|
transport=transport or SubprocessCLITransport(options=options),
|
|
@@ -200,27 +147,17 @@ class Query:
|
|
|
200
147
|
on_elicitation=options.on_elicitation,
|
|
201
148
|
hooks=hooks,
|
|
202
149
|
sdk_mcp_servers=sdk_mcp_servers,
|
|
203
|
-
initialize_timeout=initialize_timeout,
|
|
204
|
-
agents=options.agents,
|
|
205
|
-
system_prompt=system_prompt,
|
|
206
|
-
append_system_prompt=append_system_prompt,
|
|
207
|
-
exclude_dynamic_sections=options.exclude_dynamic_sections,
|
|
208
|
-
json_schema=options.get_json_schema(),
|
|
209
|
-
prompt_suggestions=options.prompt_suggestions,
|
|
210
|
-
agent_progress_summaries=options.agent_progress_summaries,
|
|
211
150
|
)
|
|
212
151
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return self._initialization_result is not None
|
|
152
|
+
def build_hooks_config(self) -> dict[HookEvent, Any] | None:
|
|
153
|
+
"""Register hook callbacks and return the hooks config for initialization.
|
|
216
154
|
|
|
217
|
-
|
|
218
|
-
|
|
155
|
+
Processes the raw hooks configuration, assigns callback IDs to each hook
|
|
156
|
+
callback, and stores them in the internal dispatch table.
|
|
219
157
|
|
|
220
158
|
Returns:
|
|
221
|
-
|
|
159
|
+
Hooks configuration dict for the initialize request, or None if empty.
|
|
222
160
|
"""
|
|
223
|
-
# Build hooks configuration for initialization
|
|
224
161
|
hooks_config: dict[HookEvent, Any] = {}
|
|
225
162
|
for event, matchers in self.hooks.items():
|
|
226
163
|
if not matchers:
|
|
@@ -237,21 +174,7 @@ class Query:
|
|
|
237
174
|
if matcher.get("timeout") is not None:
|
|
238
175
|
matcher_cfg["timeout"] = matcher.get("timeout")
|
|
239
176
|
hooks_config[event].append(matcher_cfg)
|
|
240
|
-
|
|
241
|
-
hooks=hooks_config or None,
|
|
242
|
-
agents={name: i.to_wire_model() for name, i in (self._agents or {}).items()} or None,
|
|
243
|
-
system_prompt=self._system_prompt,
|
|
244
|
-
append_system_prompt=self._append_system_prompt,
|
|
245
|
-
json_schema=self._json_schema,
|
|
246
|
-
prompt_suggestions=self._prompt_suggestions,
|
|
247
|
-
exclude_dynamic_sections=self._exclude_dynamic_sections,
|
|
248
|
-
sdk_mcp_servers=list(self.sdk_mcp_servers.keys()) or None,
|
|
249
|
-
agent_progress_summaries=self._agent_progress_summaries,
|
|
250
|
-
).model_dump(by_alias=True, exclude_none=True)
|
|
251
|
-
# Use longer timeout for initialize since MCP servers may take time to start
|
|
252
|
-
response = await self._send_control_request(request, timeout=self._initialize_timeout)
|
|
253
|
-
self._initialization_result = ClaudeCodeServerInfo.model_validate(response)
|
|
254
|
-
return self._initialization_result
|
|
177
|
+
return hooks_config or None
|
|
255
178
|
|
|
256
179
|
async def start(self) -> None:
|
|
257
180
|
"""Start reading messages from transport."""
|
|
@@ -304,12 +227,10 @@ class Query:
|
|
|
304
227
|
event.set()
|
|
305
228
|
case {"type": "control_response"} as msg:
|
|
306
229
|
logger.info("unhandled control message: %s", msg)
|
|
307
|
-
case {"type": "control_request"}:
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
self._spawn_control_request_handler(
|
|
311
|
-
req_id, self._handle_control_request(req_id, req)
|
|
312
|
-
)
|
|
230
|
+
case {"type": "control_request", "request_id": req_id, "request": request}:
|
|
231
|
+
req = incoming_control_request_adapter.validate_python(request)
|
|
232
|
+
coro = self._handle_control_request(req_id, req)
|
|
233
|
+
self._spawn_control_request_handler(req_id, coro)
|
|
313
234
|
case {"type": "control_cancel_request"}:
|
|
314
235
|
if (cancel_id := message.get("request_id")) and (
|
|
315
236
|
inflight := self._inflight_requests.pop(cancel_id, None)
|
|
@@ -318,8 +239,7 @@ class Query:
|
|
|
318
239
|
case {"type": "result"}:
|
|
319
240
|
self._first_result_event.set()
|
|
320
241
|
await self._message_send.send(message)
|
|
321
|
-
case _:
|
|
322
|
-
# Regular SDK messages go to the stream
|
|
242
|
+
case _: # Regular SDK messages go to the stream
|
|
323
243
|
await self._message_send.send(message)
|
|
324
244
|
|
|
325
245
|
except anyio.get_cancelled_exc_class():
|
|
@@ -418,7 +338,7 @@ class Query:
|
|
|
418
338
|
return {k.rstrip("_"): v for k, v in hook_output.items()}
|
|
419
339
|
|
|
420
340
|
async def _send_control_request(
|
|
421
|
-
self, request:
|
|
341
|
+
self, request: OutgoingControlRequest, timeout: float = 60.0
|
|
422
342
|
) -> dict[str, Any]:
|
|
423
343
|
"""Send control request to CLI and wait for response.
|
|
424
344
|
|
|
@@ -433,7 +353,11 @@ class Query:
|
|
|
433
353
|
event = anyio.Event()
|
|
434
354
|
self.pending_control_responses[request_id] = event
|
|
435
355
|
# Build and send request
|
|
436
|
-
control_request = {
|
|
356
|
+
control_request = {
|
|
357
|
+
"type": "control_request",
|
|
358
|
+
"request_id": request_id,
|
|
359
|
+
"request": request.model_dump(by_alias=True, exclude_none=True),
|
|
360
|
+
}
|
|
437
361
|
await self.write_json(control_request)
|
|
438
362
|
# Wait for response
|
|
439
363
|
try:
|
|
@@ -451,7 +375,7 @@ class Query:
|
|
|
451
375
|
except TimeoutError as e:
|
|
452
376
|
self.pending_control_responses.pop(request_id, None)
|
|
453
377
|
self.pending_control_results.pop(request_id, None)
|
|
454
|
-
subtype = request.
|
|
378
|
+
subtype = request.subtype
|
|
455
379
|
raise ControlRequestTimeoutError(
|
|
456
380
|
f"Control request timeout: {subtype}", subtype=subtype
|
|
457
381
|
) from e
|
|
@@ -474,162 +398,11 @@ class Query:
|
|
|
474
398
|
"""
|
|
475
399
|
if server_name not in self.sdk_mcp_servers:
|
|
476
400
|
dct = JSONRPCError(code=-32601, message=f"Server '{server_name}' not found")
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
)
|
|
401
|
+
id_ = get_jsonrpc_request_id(message)
|
|
402
|
+
return JSONRPCErrorResponse(jsonrpc="2.0", id=id_, error=dct)
|
|
480
403
|
server = self.sdk_mcp_servers[server_name]
|
|
481
404
|
return await process_mcp_request(message, server)
|
|
482
405
|
|
|
483
|
-
async def supported_agents(self) -> list[ClaudeCodeAgentInfo]:
|
|
484
|
-
"""Get the list of available subagents for the current session."""
|
|
485
|
-
if self._initialization_result is None:
|
|
486
|
-
raise RuntimeError("Not initialized. Call initialize() first.")
|
|
487
|
-
return self._initialization_result.agents
|
|
488
|
-
|
|
489
|
-
async def get_mcp_status(self) -> dict[str, Any]:
|
|
490
|
-
"""Get current MCP server connection status."""
|
|
491
|
-
return await self._send_control_request({"subtype": "mcp_status"})
|
|
492
|
-
|
|
493
|
-
async def set_mcp_servers(self, servers: dict[str, dict[str, Any]]) -> dict[str, Any]:
|
|
494
|
-
"""Add, replace, or remove MCP servers dynamically."""
|
|
495
|
-
return await self._send_control_request({"subtype": "mcp_set_servers", "servers": servers})
|
|
496
|
-
|
|
497
|
-
async def mcp_reconnect(self, server_name: str) -> dict[str, Any]:
|
|
498
|
-
"""Reconnect to an MCP server."""
|
|
499
|
-
req = {"subtype": "mcp_reconnect", "serverName": server_name}
|
|
500
|
-
return await self._send_control_request(req)
|
|
501
|
-
|
|
502
|
-
async def mcp_toggle(self, server_name: str, *, enabled: bool) -> dict[str, Any]:
|
|
503
|
-
"""Enable or disable an MCP server."""
|
|
504
|
-
req = {"subtype": "mcp_toggle", "serverName": server_name, "enabled": enabled}
|
|
505
|
-
return await self._send_control_request(req)
|
|
506
|
-
|
|
507
|
-
async def set_max_thinking_tokens(self, max_thinking_tokens: int) -> dict[str, Any]:
|
|
508
|
-
"""Set the maximum number of thinking tokens."""
|
|
509
|
-
req = {"subtype": "set_max_thinking_tokens", "max_thinking_tokens": max_thinking_tokens}
|
|
510
|
-
return await self._send_control_request(req)
|
|
511
|
-
|
|
512
|
-
async def interrupt(self) -> dict[str, Any]:
|
|
513
|
-
"""Send interrupt control request."""
|
|
514
|
-
return await self._send_control_request({"subtype": "interrupt"})
|
|
515
|
-
|
|
516
|
-
async def set_permission_mode(self, mode: PermissionMode) -> dict[str, Any]:
|
|
517
|
-
"""Change permission mode."""
|
|
518
|
-
return await self._send_control_request({"subtype": "set_permission_mode", "mode": mode})
|
|
519
|
-
|
|
520
|
-
async def set_model(self, model: str | None) -> dict[str, Any]:
|
|
521
|
-
"""Change the AI model."""
|
|
522
|
-
return await self._send_control_request({"subtype": "set_model", "model": model})
|
|
523
|
-
|
|
524
|
-
async def cancel_async_message(self, message_uuid: str) -> dict[str, Any]:
|
|
525
|
-
"""Drop a pending async user message from the command queue by uuid."""
|
|
526
|
-
return await self._send_control_request(
|
|
527
|
-
{"subtype": "cancel_async_message", "message_uuid": message_uuid}
|
|
528
|
-
)
|
|
529
|
-
|
|
530
|
-
async def stop_task(self, task_id: str) -> dict[str, Any]:
|
|
531
|
-
"""Stop a running task."""
|
|
532
|
-
return await self._send_control_request({"subtype": "stop_task", "task_id": task_id})
|
|
533
|
-
|
|
534
|
-
async def channel_enable(self, server_name: str) -> dict[str, Any]:
|
|
535
|
-
"""Enable MCP channel notifications for a marketplace plugin server."""
|
|
536
|
-
req = {"subtype": "channel_enable", "serverName": server_name}
|
|
537
|
-
return await self._send_control_request(req)
|
|
538
|
-
|
|
539
|
-
async def end_session(self) -> dict[str, Any]:
|
|
540
|
-
"""End the current session."""
|
|
541
|
-
return await self._send_control_request({"subtype": "end_session"})
|
|
542
|
-
|
|
543
|
-
async def remote_control(self, *, enabled: bool) -> dict[str, Any]:
|
|
544
|
-
"""Toggle the remote control REPL bridge for external session access.
|
|
545
|
-
|
|
546
|
-
When enabled, starts a bridge that allows remote clients to send prompts,
|
|
547
|
-
permission responses, interrupts, and model changes into the session.
|
|
548
|
-
The response includes ``session_url``, ``connect_url``, and
|
|
549
|
-
``environment_id`` when enabling.
|
|
550
|
-
"""
|
|
551
|
-
req = {"subtype": "remote_control", "enabled": enabled}
|
|
552
|
-
return await self._send_control_request(req)
|
|
553
|
-
|
|
554
|
-
async def apply_flag_settings(self, settings: dict[str, Any]) -> dict[str, Any]:
|
|
555
|
-
"""Apply runtime flag settings."""
|
|
556
|
-
req = {"subtype": "apply_flag_settings", "settings": settings}
|
|
557
|
-
return await self._send_control_request(req)
|
|
558
|
-
|
|
559
|
-
async def get_settings(self) -> dict[str, Any]:
|
|
560
|
-
"""Get the effective merged settings and raw per-source settings."""
|
|
561
|
-
return await self._send_control_request({"subtype": "get_settings"})
|
|
562
|
-
|
|
563
|
-
async def get_context_usage(self) -> dict[str, Any]:
|
|
564
|
-
"""Get a breakdown of current context window usage by category."""
|
|
565
|
-
return await self._send_control_request({"subtype": "get_context_usage"})
|
|
566
|
-
|
|
567
|
-
async def mcp_authenticate(self, server_name: str) -> dict[str, Any]:
|
|
568
|
-
"""Trigger OAuth authentication for an MCP server."""
|
|
569
|
-
req = {"subtype": "mcp_authenticate", "serverName": server_name}
|
|
570
|
-
return await self._send_control_request(req)
|
|
571
|
-
|
|
572
|
-
async def mcp_clear_auth(self, server_name: str) -> dict[str, Any]:
|
|
573
|
-
"""Clear OAuth credentials for an MCP server."""
|
|
574
|
-
req = {"subtype": "mcp_clear_auth", "serverName": server_name}
|
|
575
|
-
return await self._send_control_request(req)
|
|
576
|
-
|
|
577
|
-
async def mcp_oauth_callback_url(self, server_name: str, callback_url: str) -> dict[str, Any]:
|
|
578
|
-
"""Provide an OAuth redirect callback URL to complete an MCP server OAuth flow."""
|
|
579
|
-
req = {
|
|
580
|
-
"subtype": "mcp_oauth_callback_url",
|
|
581
|
-
"serverName": server_name,
|
|
582
|
-
"callbackUrl": callback_url,
|
|
583
|
-
}
|
|
584
|
-
return await self._send_control_request(req)
|
|
585
|
-
|
|
586
|
-
async def claude_oauth_wait_for_completion(self) -> ClaudeOAuthWaitForCompletionResponse:
|
|
587
|
-
"""Wait for an in-progress Claude OAuth flow to complete.
|
|
588
|
-
|
|
589
|
-
Returns account details (email, organization, subscriptionType, etc.)
|
|
590
|
-
once the OAuth flow finishes.
|
|
591
|
-
"""
|
|
592
|
-
data = await self._send_control_request({"subtype": "claude_oauth_wait_for_completion"})
|
|
593
|
-
return ClaudeOAuthWaitForCompletionResponse.model_validate(data)
|
|
594
|
-
|
|
595
|
-
async def side_question(self, question: str) -> SideQuestionResponse:
|
|
596
|
-
"""Send a side question to the model using the current conversation context.
|
|
597
|
-
|
|
598
|
-
The model answers the question without it being added to the main
|
|
599
|
-
conversation history.
|
|
600
|
-
|
|
601
|
-
Args:
|
|
602
|
-
question: The question to ask the model.
|
|
603
|
-
"""
|
|
604
|
-
data = await self._send_control_request({"subtype": "side_question", "question": question})
|
|
605
|
-
return SideQuestionResponse.model_validate(data)
|
|
606
|
-
|
|
607
|
-
async def rewind_files(self, user_message_id: str) -> dict[str, Any]:
|
|
608
|
-
"""Rewind tracked files to their state at a specific user message.
|
|
609
|
-
|
|
610
|
-
Requires file checkpointing to be enabled via the `enable_file_checkpointing` option.
|
|
611
|
-
|
|
612
|
-
Args:
|
|
613
|
-
user_message_id: UUID of the user message to rewind to
|
|
614
|
-
"""
|
|
615
|
-
req = {"subtype": "rewind_files", "user_message_id": user_message_id}
|
|
616
|
-
return await self._send_control_request(req)
|
|
617
|
-
|
|
618
|
-
async def seed_read_state(self, path: str, mtime: int) -> None:
|
|
619
|
-
"""Seed the CLI's readFileState cache with a path+mtime entry.
|
|
620
|
-
|
|
621
|
-
Use when the client observed a Read that has since been removed from context
|
|
622
|
-
(e.g. by snip), so a subsequent Edit won't fail "file not read yet".
|
|
623
|
-
If the file changed on disk since the given mtime, the seed is skipped
|
|
624
|
-
and Edit will correctly require a fresh Read.
|
|
625
|
-
|
|
626
|
-
Args:
|
|
627
|
-
path: Path to the file that was previously Read
|
|
628
|
-
mtime: File mtime (floored ms) at the time of the observed Read
|
|
629
|
-
"""
|
|
630
|
-
req = {"subtype": "seed_read_state", "path": path, "mtime": mtime}
|
|
631
|
-
await self._send_control_request(req)
|
|
632
|
-
|
|
633
406
|
async def receive_messages(self) -> AsyncGenerator[dict[str, Any]]:
|
|
634
407
|
"""Receive SDK messages (not control messages)."""
|
|
635
408
|
async for message in self._message_receive:
|