clawd-code-sdk 1.2.5__tar.gz → 1.2.7__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.
Files changed (96) hide show
  1. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/PKG-INFO +1 -1
  2. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/pyproject.toml +1 -1
  3. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/client.py +62 -4
  4. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/__init__.py +6 -0
  5. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/messages.py +23 -0
  6. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/server_info.py +5 -1
  7. clawd_code_sdk-1.2.7/tests/test_apply_flag_settings.py +28 -0
  8. clawd_code_sdk-1.2.7/tests/test_steer.py +89 -0
  9. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/.gitignore +0 -0
  10. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/LICENSE +0 -0
  11. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/README.md +0 -0
  12. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/__init__.py +0 -0
  13. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_bundled/.gitignore +0 -0
  14. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_errors.py +0 -0
  15. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/__init__.py +0 -0
  16. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/mcp_compat.py +0 -0
  17. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/query.py +0 -0
  18. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/sdk_mcp_bridge.py +0 -0
  19. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/session_mutations.py +0 -0
  20. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/sessions.py +0 -0
  21. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/transport/__init__.py +0 -0
  22. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/transport/subprocess_cli.py +0 -0
  23. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_internal/transport/subprocess_utils.py +0 -0
  24. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/_version.py +0 -0
  25. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/anthropic_stream.py +0 -0
  26. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/instrumentation.py +0 -0
  27. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/list_sessions.py +0 -0
  28. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/mcp_utils.py +0 -0
  29. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/agents.py +0 -0
  30. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/anthropic_types.py +0 -0
  31. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/base.py +0 -0
  32. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/content_blocks.py +0 -0
  33. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/context_usage.py +0 -0
  34. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/control.py +0 -0
  35. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/hooks.py +0 -0
  36. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/input_types.py +0 -0
  37. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/mcp.py +0 -0
  38. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/options.py +0 -0
  39. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/output_types.py +0 -0
  40. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/permissions.py +0 -0
  41. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/prompt_requests.py +0 -0
  42. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/prompts.py +0 -0
  43. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/session.py +0 -0
  44. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/settings.py +0 -0
  45. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/system_messages.py +0 -0
  46. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/thinking.py +0 -0
  47. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/models/usage.py +0 -0
  48. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/py.typed +0 -0
  49. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/session.py +0 -0
  50. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/storage/ARCHITECTURE.md +0 -0
  51. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/storage/__init__.py +0 -0
  52. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/storage/helpers.py +0 -0
  53. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/storage/models.py +0 -0
  54. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/src/clawd_code_sdk/storage/replay.py +0 -0
  55. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/__init__.py +0 -0
  56. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/conftest.py +0 -0
  57. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/__init__.py +0 -0
  58. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_agents_and_settings.py +0 -0
  59. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_dynamic_control.py +0 -0
  60. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_hook_events.py +0 -0
  61. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_hooks.py +0 -0
  62. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_include_partial_messages.py +0 -0
  63. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_mcp_resources.py +0 -0
  64. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_mcp_tools.py +0 -0
  65. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_sdk_mcp_resources.py +0 -0
  66. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_sdk_mcp_tools.py +0 -0
  67. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_slash_commands.py +0 -0
  68. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_stderr_callback.py +0 -0
  69. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_storage_parsing.py +0 -0
  70. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_structured_output.py +0 -0
  71. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_subagent_invocation.py +0 -0
  72. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/e2e/test_tool_permissions.py +0 -0
  73. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/mcp_server.py +0 -0
  74. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/mock_claude_server.py +0 -0
  75. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_api_content_blocks.py +0 -0
  76. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_changelog.py +0 -0
  77. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_client.py +0 -0
  78. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_elicitation_mcp_conversion.py +0 -0
  79. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_errors.py +0 -0
  80. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_get_usage.py +0 -0
  81. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_image.png +0 -0
  82. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_integration.py +0 -0
  83. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_line_framing.py +0 -0
  84. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_message_parser.py +0 -0
  85. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_result_error.py +0 -0
  86. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_sandbox_settings.py +0 -0
  87. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_sdk_0_3_types.py +0 -0
  88. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_sdk_mcp_integration.py +0 -0
  89. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_session.py +0 -0
  90. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_session_mutations.py +0 -0
  91. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_sessions.py +0 -0
  92. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_streaming_client.py +0 -0
  93. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_subprocess_buffering.py +0 -0
  94. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_tool_callbacks.py +0 -0
  95. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_transport.py +0 -0
  96. {clawd_code_sdk-1.2.5 → clawd_code_sdk-1.2.7}/tests/test_transport_close.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: clawd-code-sdk
3
- Version: 1.2.5
3
+ Version: 1.2.7
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
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "clawd-code-sdk"
3
- version = "1.2.5"
3
+ version = "1.2.7"
4
4
  description = "Python SDK for Claude Code"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
  import os
6
6
  from pathlib import Path
7
7
  from typing import TYPE_CHECKING, Any, Literal, Self, cast
8
+ from uuid import uuid4
8
9
 
9
10
  import anyenv
10
11
  from pydantic import ValidationError
@@ -272,8 +273,9 @@ class ClaudeSDKClient:
272
273
  ):
273
274
  self.query_usage.accumulate(usage)
274
275
  self.session_usage.accumulate(usage)
275
- # total_cost_usd is cumulative; derive per-query cost as delta
276
- self.query_cost = total_cost - self.session_cost
276
+ # total_cost_usd is cumulative; a query adds the delta of each of its
277
+ # results (a steered query has one per cycle)
278
+ self.query_cost += total_cost - self.session_cost
277
279
  self.session_cost = total_cost
278
280
  yield message
279
281
 
@@ -284,9 +286,12 @@ class ClaudeSDKClient:
284
286
  parent_tool_use_id: str | None = None,
285
287
  should_query: bool = True,
286
288
  priority: MessagePriority | None = None,
289
+ uuid: str | None = None,
287
290
  ) -> None:
288
291
  """Send a new user message with one or more content blocks.
289
292
 
293
+ Starts a new query: ``query_usage`` and ``query_cost`` count from here.
294
+
290
295
  Args:
291
296
  *prompts: One or more content blocks. Strings are converted to
292
297
  UserTextPrompt automatically. Pass multiple to combine, e.g.
@@ -295,11 +300,61 @@ class ClaudeSDKClient:
295
300
  parent_tool_use_id: If responding to a tool use, the tool_use block ID.
296
301
  should_query: Whether to query the model for a response. Defaults to True.
297
302
  priority: The priority of the message. Defaults to None.
303
+ uuid: ID of the message; the CLI then reports its progress as
304
+ ``CommandLifecycleMessage`` frames and names it in results.
298
305
  """
299
306
  prompt = prompts[0] if prompts else None
300
307
  self._logfire_prompt = str(prompt)
301
308
  self.query_usage.reset()
302
309
  self.query_cost = 0.0
310
+ await self._send_user_message(
311
+ prompts,
312
+ session_id=session_id,
313
+ parent_tool_use_id=parent_tool_use_id,
314
+ should_query=should_query,
315
+ priority=priority,
316
+ uuid=uuid,
317
+ )
318
+
319
+ async def steer(
320
+ self,
321
+ *prompts: str | UserPrompt,
322
+ priority: MessagePriority = "now",
323
+ session_id: str = "default",
324
+ ) -> str:
325
+ """Send a user message into the running query instead of starting a new one.
326
+
327
+ ``query_usage`` and ``query_cost`` keep counting the running query. With
328
+ ``"now"`` the CLI takes the message in at once: it ends the cycle that is
329
+ generating (which reports a result of its own) or moves a running foreground
330
+ tool to the background, and works on the message in the same query. ``"later"``
331
+ waits for the running cycle (e.g. while a permission request is open).
332
+
333
+ Args:
334
+ *prompts: Content blocks of the message (strings become text blocks).
335
+ priority: When the CLI takes the message in.
336
+ session_id: Session identifier for the message.
337
+
338
+ Returns:
339
+ The message's uuid, which ``CommandLifecycleMessage.command_uuid`` and the
340
+ results' ``user_message_uuids`` refer to.
341
+ """
342
+ message_uuid = str(uuid4())
343
+ await self._send_user_message(
344
+ prompts, session_id=session_id, priority=priority, uuid=message_uuid
345
+ )
346
+ return message_uuid
347
+
348
+ async def _send_user_message(
349
+ self,
350
+ prompts: tuple[str | UserPrompt, ...],
351
+ *,
352
+ session_id: str,
353
+ parent_tool_use_id: str | None = None,
354
+ should_query: bool = True,
355
+ priority: MessagePriority | None = None,
356
+ uuid: str | None = None,
357
+ ) -> None:
303
358
  self._ensure_connected()
304
359
  if not self._query:
305
360
  raise CLIConnectionError("Not connected. Call connect() first.")
@@ -322,6 +377,8 @@ class ClaudeSDKClient:
322
377
  "shouldQuery": should_query,
323
378
  "priority": priority,
324
379
  }
380
+ if uuid is not None:
381
+ wire_message["uuid"] = uuid
325
382
  if self.options.verbatim_prompts:
326
383
  wire_message["client_composed"] = True
327
384
  await self._query.write_json(wire_message)
@@ -507,10 +564,11 @@ class ClaudeSDKClient:
507
564
  other sources (user, project, etc.) to produce the effective settings.
508
565
 
509
566
  Args:
510
- settings: Settings to apply. Only non-None fields will be sent.
567
+ settings: Settings to apply. Only fields that were set explicitly (and are
568
+ not None) are sent: defaults would override every other settings source.
511
569
  """
512
570
  query = self._ensure_connected()
513
- serialized = settings.model_dump(by_alias=True, exclude_none=True)
571
+ serialized = settings.model_dump(by_alias=True, exclude_unset=True, exclude_none=True)
514
572
  req = SDKControlApplyFlagSettingsRequest(settings=serialized)
515
573
  await query._send_control_request(req)
516
574
 
@@ -348,6 +348,8 @@ from .messages import (
348
348
  SDKPermissionDenial,
349
349
  StreamEvent,
350
350
  AuthStatusMessage,
351
+ CommandLifecycleMessage,
352
+ CommandLifecycleState,
351
353
  ConversationResetMessage,
352
354
  SDKMessageOrigin,
353
355
  ToolProgressMessage,
@@ -472,6 +474,7 @@ MessageUnion = (
472
474
  | AuthStatusMessage
473
475
  | PromptSuggestionMessage
474
476
  | ConversationResetMessage
477
+ | CommandLifecycleMessage
475
478
  | SystemMessageUnion
476
479
  )
477
480
 
@@ -487,6 +490,7 @@ Message = Annotated[
487
490
  | Annotated[AuthStatusMessage, Tag("auth_status")]
488
491
  | Annotated[PromptSuggestionMessage, Tag("prompt_suggestion")]
489
492
  | Annotated[ConversationResetMessage, Tag("conversation_reset")]
493
+ | Annotated[CommandLifecycleMessage, Tag("command_lifecycle")]
490
494
  | Annotated[SystemMessageUnion, Tag("system")],
491
495
  Discriminator(_message_discriminator),
492
496
  ]
@@ -552,6 +556,8 @@ __all__ = [
552
556
  "ClaudeCodeSettings",
553
557
  "ClaudeOAuthWaitForCompletionResponse",
554
558
  "CommandHookHandler",
559
+ "CommandLifecycleMessage",
560
+ "CommandLifecycleState",
555
561
  "CommandsChangedMessage",
556
562
  "CompactBoundarySystemMessage",
557
563
  "ConfigOutput",
@@ -775,6 +775,28 @@ class ConversationResetMessage(BaseMessage):
775
775
  timestamp: str | None = None
776
776
 
777
777
 
778
+ CommandLifecycleState = Literal[
779
+ "queued", "started", "completed", "cancelled", "discarded", "refused"
780
+ ]
781
+ """Where a uuid-stamped user message is in the CLI's command queue."""
782
+
783
+
784
+ class CommandLifecycleMessage(BaseMessage):
785
+ """Progress of a user message that was sent with a ``uuid`` (capability ``msg_lifecycle_v1``).
786
+
787
+ ``queued``: waiting behind the running work. ``started``: taken in (the cycle it starts,
788
+ or the running cycle it was folded into). ``completed``: its work ended (after the
789
+ result for a message that started a cycle, before it for one folded into a cycle).
790
+ ``cancelled``: an interrupt stopped it. ``discarded`` / ``refused``: dropped before it ran.
791
+ """
792
+
793
+ type: Literal["command_lifecycle"] = "command_lifecycle"
794
+ command_uuid: str
795
+ """The ``uuid`` the message was sent with."""
796
+ state: CommandLifecycleState
797
+ session_id: str
798
+
799
+
778
800
  class PromptSuggestionMessage(BaseMessage):
779
801
  """Predicted next user prompt, emitted after each turn when promptSuggestions is enabled."""
780
802
 
@@ -818,6 +840,7 @@ MiscMessages = (
818
840
  | AuthStatusMessage
819
841
  | PromptSuggestionMessage
820
842
  | ConversationResetMessage
843
+ | CommandLifecycleMessage
821
844
  | StreamlinedTextMessage
822
845
  | StreamlinedToolUseSummaryMessage
823
846
  )
@@ -52,12 +52,16 @@ class ClaudeCodeModelInfo(ClaudeCodeBaseModel):
52
52
  """Whether the model supports fast mode."""
53
53
 
54
54
  supports_auto_mode: bool | None = None
55
+ """Whether the model supports auto mode."""
56
+
55
57
  supports_thinking_off: bool | None = None
58
+ """Whether thinking can be turned off for the model."""
59
+
56
60
  disabled: bool | None = None
57
61
  """Visible but not selectable (only set for entries in ``unavailable_models``)."""
62
+
58
63
  resolved_model: str | None = None
59
64
  """Concrete model ID this entry resolves to (e.g. for aliases)."""
60
- """Whether the model supports auto mode."""
61
65
 
62
66
 
63
67
  class ClaudeCodeCommandInfo(ClaudeCodeBaseModel):
@@ -0,0 +1,28 @@
1
+ """The apply_flag_settings control request sends only the settings that were set."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any, cast
6
+
7
+ from clawd_code_sdk import ClaudeAgentOptions, ClaudeSDKClient
8
+ from clawd_code_sdk.models.control import SDKControlApplyFlagSettingsRequest
9
+ from clawd_code_sdk.models.settings import ClaudeCodeSettings
10
+
11
+
12
+ class _RecordingQuery:
13
+ def __init__(self) -> None:
14
+ self.requests: list[Any] = []
15
+
16
+ async def _send_control_request(self, request: Any) -> dict[str, Any]:
17
+ self.requests.append(request)
18
+ return {}
19
+
20
+
21
+ async def test_only_set_fields_are_sent() -> None:
22
+ client = ClaudeSDKClient(ClaudeAgentOptions())
23
+ query = _RecordingQuery()
24
+ client._query = cast(Any, query)
25
+
26
+ await client.apply_flag_settings(ClaudeCodeSettings(effort_level="low"))
27
+
28
+ assert query.requests == [SDKControlApplyFlagSettingsRequest(settings={"effortLevel": "low"})]
@@ -0,0 +1,89 @@
1
+ """Steering a running query: uuid-stamped messages, lifecycle frames, cost per query."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Any, cast
6
+
7
+ from clawd_code_sdk import ClaudeAgentOptions, ClaudeSDKClient
8
+ from clawd_code_sdk.models import CommandLifecycleMessage, message_adapter
9
+
10
+
11
+ if TYPE_CHECKING:
12
+ from collections.abc import AsyncIterator
13
+
14
+
15
+ class _FakeQuery:
16
+ """Records written messages and replays the given CLI messages."""
17
+
18
+ def __init__(self, messages: list[dict[str, Any]] | None = None) -> None:
19
+ self.written: list[dict[str, Any]] = []
20
+ self.messages = messages or []
21
+
22
+ async def write_json(self, data: dict[str, Any]) -> None:
23
+ self.written.append(data)
24
+
25
+ async def receive_messages(self) -> AsyncIterator[dict[str, Any]]:
26
+ for message in self.messages:
27
+ yield message
28
+
29
+
30
+ def _client(query: _FakeQuery) -> ClaudeSDKClient:
31
+ client = ClaudeSDKClient(ClaudeAgentOptions())
32
+ client._query = cast(Any, query)
33
+ return client
34
+
35
+
36
+ def _result(total_cost: float, output_tokens: int) -> dict[str, Any]:
37
+ return {
38
+ "type": "result",
39
+ "subtype": "success",
40
+ "uuid": "r",
41
+ "session_id": "s",
42
+ "duration_ms": 1,
43
+ "duration_api_ms": 1,
44
+ "is_error": False,
45
+ "num_turns": 1,
46
+ "total_cost_usd": total_cost,
47
+ "usage": {"input_tokens": 1, "output_tokens": output_tokens},
48
+ "stop_reason": "end_turn",
49
+ }
50
+
51
+
52
+ async def test_steer_keeps_counting_the_running_query() -> None:
53
+ # The interrupted cycle's result, then the steered message's cycle
54
+ query = _FakeQuery([_result(0.25, 10), _result(0.75, 5)])
55
+ client = _client(query)
56
+ await client.query("count to 300")
57
+ message_uuid = await client.steer("stop and say BANANA")
58
+
59
+ async for _ in client.receive_messages():
60
+ pass
61
+
62
+ first, steered = query.written
63
+ assert "uuid" not in first
64
+ assert steered["uuid"] == message_uuid
65
+ assert steered["priority"] == "now"
66
+ assert steered["message"]["content"] == [{"type": "text", "text": "stop and say BANANA"}]
67
+ # Both cycles count for the query
68
+ assert client.query_cost == 0.75
69
+ assert client.query_usage.output_tokens == 15
70
+
71
+
72
+ async def test_query_with_uuid() -> None:
73
+ query = _FakeQuery()
74
+ await _client(query).query("hi", uuid="u1", priority="next")
75
+ assert query.written[0]["uuid"] == "u1"
76
+ assert query.written[0]["priority"] == "next"
77
+
78
+
79
+ def test_command_lifecycle_frame() -> None:
80
+ frame = {
81
+ "type": "command_lifecycle",
82
+ "command_uuid": "c9c43ffb",
83
+ "state": "queued",
84
+ "uuid": "7751841f",
85
+ "session_id": "466f26ed",
86
+ }
87
+ assert message_adapter.validate_python(frame) == CommandLifecycleMessage(
88
+ uuid="7751841f", command_uuid="c9c43ffb", state="queued", session_id="466f26ed"
89
+ )
File without changes
File without changes