clawd-code-sdk 1.2.2__tar.gz → 1.2.4__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.2.2 → clawd_code_sdk-1.2.4}/PKG-INFO +2 -2
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/pyproject.toml +2 -2
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/anthropic_stream.py +11 -4
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/client.py +10 -3
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/__init__.py +26 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/content_blocks.py +152 -5
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/context_usage.py +47 -2
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/control.py +15 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/messages.py +9 -1
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/storage/replay.py +66 -3
- clawd_code_sdk-1.2.4/tests/test_api_content_blocks.py +183 -0
- clawd_code_sdk-1.2.4/tests/test_get_usage.py +91 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/.gitignore +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/LICENSE +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/README.md +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_bundled/.gitignore +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_errors.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/mcp_compat.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/query.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/sdk_mcp_bridge.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/session_mutations.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/sessions.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/transport/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/transport/subprocess_cli.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/transport/subprocess_utils.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_version.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/instrumentation.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/list_sessions.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/mcp_utils.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/agents.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/anthropic_types.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/base.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/hooks.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/input_types.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/mcp.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/options.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/output_types.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/permissions.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/prompt_requests.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/prompts.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/server_info.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/session.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/settings.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/system_messages.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/thinking.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/models/usage.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/py.typed +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/session.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/storage/ARCHITECTURE.md +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/storage/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/storage/helpers.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/storage/models.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/usage.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/conftest.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/__init__.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_agents_and_settings.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_dynamic_control.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_hook_events.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_hooks.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_include_partial_messages.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_mcp_resources.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_mcp_tools.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_sdk_mcp_resources.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_sdk_mcp_tools.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_slash_commands.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_stderr_callback.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_storage_parsing.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_structured_output.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_subagent_invocation.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/e2e/test_tool_permissions.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/mcp_server.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/mock_claude_server.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_changelog.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_client.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_elicitation_mcp_conversion.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_errors.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_image.png +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_integration.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_line_framing.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_message_parser.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_result_error.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_sandbox_settings.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_sdk_0_3_types.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_sdk_mcp_integration.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_session.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_session_mutations.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_sessions.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_streaming_client.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_subprocess_buffering.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_tool_callbacks.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/tests/test_transport.py +0 -0
- {clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/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.
|
|
3
|
+
Version: 1.2.4
|
|
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
|
|
@@ -17,7 +17,7 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.14
|
|
18
18
|
Classifier: Typing :: Typed
|
|
19
19
|
Requires-Python: >=3.13
|
|
20
|
-
Requires-Dist: anthropic>=
|
|
20
|
+
Requires-Dist: anthropic>=2.9.0
|
|
21
21
|
Requires-Dist: anyenv>=2.0.15
|
|
22
22
|
Requires-Dist: anyio>=4.0.0
|
|
23
23
|
Requires-Dist: jsonschema>=4.20.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "clawd-code-sdk"
|
|
3
|
-
version = "1.2.
|
|
3
|
+
version = "1.2.4"
|
|
4
4
|
description = "Python SDK for Claude Code"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.13"
|
|
@@ -19,7 +19,7 @@ classifiers = [
|
|
|
19
19
|
"Typing :: Typed",
|
|
20
20
|
]
|
|
21
21
|
dependencies = [
|
|
22
|
-
"anthropic>=
|
|
22
|
+
"anthropic>=2.9.0", # BetaFallbackBlock / BetaMCPToolListingBlock
|
|
23
23
|
"anyenv>=2.0.15",
|
|
24
24
|
"anyio>=4.0.0",
|
|
25
25
|
"logfire>=4.31.0",
|
|
@@ -42,6 +42,7 @@ from clawd_code_sdk.models.content_blocks import (
|
|
|
42
42
|
TextBlock,
|
|
43
43
|
ThinkingBlock,
|
|
44
44
|
ToolUseBlock,
|
|
45
|
+
is_server_block,
|
|
45
46
|
)
|
|
46
47
|
|
|
47
48
|
|
|
@@ -54,6 +55,7 @@ if TYPE_CHECKING:
|
|
|
54
55
|
)
|
|
55
56
|
|
|
56
57
|
from clawd_code_sdk.models import Message
|
|
58
|
+
from clawd_code_sdk.models.content_blocks import AssistantContentBlock, ServerContentBlock
|
|
57
59
|
|
|
58
60
|
|
|
59
61
|
async def to_anthropic_events(
|
|
@@ -171,9 +173,14 @@ def _synthesize_events(
|
|
|
171
173
|
|
|
172
174
|
|
|
173
175
|
def _to_anthropic_block(
|
|
174
|
-
block:
|
|
175
|
-
) -> ATextBlock | AThinkingBlock | AToolUseBlock | None:
|
|
176
|
-
"""Convert a content block to its Anthropic start-event form (empty content).
|
|
176
|
+
block: AssistantContentBlock,
|
|
177
|
+
) -> ATextBlock | AThinkingBlock | AToolUseBlock | ServerContentBlock | None:
|
|
178
|
+
"""Convert a content block to its Anthropic start-event form (empty content).
|
|
179
|
+
|
|
180
|
+
API-produced blocks already are Anthropic models and are sent complete.
|
|
181
|
+
"""
|
|
182
|
+
if is_server_block(block):
|
|
183
|
+
return block
|
|
177
184
|
match block:
|
|
178
185
|
case TextBlock():
|
|
179
186
|
return ATextBlock(type="text", text="")
|
|
@@ -186,7 +193,7 @@ def _to_anthropic_block(
|
|
|
186
193
|
|
|
187
194
|
|
|
188
195
|
def _to_anthropic_delta(
|
|
189
|
-
block:
|
|
196
|
+
block: AssistantContentBlock,
|
|
190
197
|
) -> BetaTextDelta | BetaThinkingDelta | BetaInputJSONDeltaType | None:
|
|
191
198
|
"""Convert a content block's content into a single delta event."""
|
|
192
199
|
from anthropic.types.beta import BetaInputJSONDelta
|
|
@@ -39,6 +39,7 @@ from clawd_code_sdk.models import (
|
|
|
39
39
|
SDKControlGetTaskOutputRequest,
|
|
40
40
|
SDKControlGetTaskOutputResponse,
|
|
41
41
|
SDKControlGetUsageRequest,
|
|
42
|
+
SDKControlGetUsageResponse,
|
|
42
43
|
SDKControlInitializeRequest,
|
|
43
44
|
SDKControlInterruptRequest,
|
|
44
45
|
SDKControlInterruptResponse,
|
|
@@ -629,11 +630,17 @@ class ClaudeSDKClient:
|
|
|
629
630
|
result = await query._send_control_request(request)
|
|
630
631
|
return SDKControlGetTaskOutputResponse.model_validate(result)
|
|
631
632
|
|
|
632
|
-
async def get_usage(self, *, skip_behaviors: bool =
|
|
633
|
-
"""Get
|
|
633
|
+
async def get_usage(self, *, skip_behaviors: bool = True) -> SDKControlGetUsageResponse:
|
|
634
|
+
"""Get session usage and plan rate-limit windows (experimental, shape may change).
|
|
635
|
+
|
|
636
|
+
Args:
|
|
637
|
+
skip_behaviors: Skip the (slow) scan of local transcripts that fills
|
|
638
|
+
``behaviors``.
|
|
639
|
+
"""
|
|
634
640
|
query = self._ensure_connected()
|
|
635
641
|
request = SDKControlGetUsageRequest(skip_behaviors=skip_behaviors or None)
|
|
636
|
-
|
|
642
|
+
result = await query._send_control_request(request)
|
|
643
|
+
return SDKControlGetUsageResponse.model_validate(result)
|
|
637
644
|
|
|
638
645
|
async def get_binary_version(self) -> dict[str, Any]:
|
|
639
646
|
"""Get version info of the running Claude Code binary."""
|
|
@@ -49,12 +49,16 @@ from .context_usage import (
|
|
|
49
49
|
SDKContextUsage,
|
|
50
50
|
SDKUsageReport,
|
|
51
51
|
ContextUsageMcpInstructions,
|
|
52
|
+
ModelScopedUsageWindow,
|
|
53
|
+
UsageRateLimits,
|
|
54
|
+
UsageWindow,
|
|
52
55
|
)
|
|
53
56
|
from .control import (
|
|
54
57
|
SDKControlInterruptResponse,
|
|
55
58
|
SDKControlBackgroundTasksRequest,
|
|
56
59
|
SDKControlGetTaskOutputRequest,
|
|
57
60
|
SDKControlGetTaskOutputResponse,
|
|
61
|
+
SDKControlGetUsageResponse,
|
|
58
62
|
SDKControlGetUsageRequest,
|
|
59
63
|
SDKControlGetBinaryVersionRequest,
|
|
60
64
|
SDKControlListModelsRequest,
|
|
@@ -306,7 +310,16 @@ from .content_blocks import (
|
|
|
306
310
|
AssistantContentBlock,
|
|
307
311
|
AssistantMessageContent,
|
|
308
312
|
ContentBlock,
|
|
313
|
+
Base64PDFSource,
|
|
314
|
+
ContentDocumentSource,
|
|
315
|
+
DocumentBlock,
|
|
316
|
+
DocumentSource,
|
|
317
|
+
FileDocumentSource,
|
|
309
318
|
ImageBlock,
|
|
319
|
+
PlainTextSource,
|
|
320
|
+
SERVER_CONTENT_BLOCK_TYPES,
|
|
321
|
+
ServerContentBlock,
|
|
322
|
+
URLPDFSource,
|
|
310
323
|
ImageSource,
|
|
311
324
|
MessageParam,
|
|
312
325
|
TextBlock,
|
|
@@ -487,6 +500,7 @@ If not provided, elicitation requests will be declined automatically.
|
|
|
487
500
|
"""
|
|
488
501
|
|
|
489
502
|
__all__ = [
|
|
503
|
+
"SERVER_CONTENT_BLOCK_TYPES",
|
|
490
504
|
"TOOL_INPUT_TYPES",
|
|
491
505
|
"TOOL_USE_RESULT_TYPES",
|
|
492
506
|
"APIRetrySystemMessage",
|
|
@@ -518,6 +532,7 @@ __all__ = [
|
|
|
518
532
|
"AsyncHookJSONOutput",
|
|
519
533
|
"AuthStatusMessage",
|
|
520
534
|
"BackgroundTasksChangedMessage",
|
|
535
|
+
"Base64PDFSource",
|
|
521
536
|
"BaseHookInput",
|
|
522
537
|
"BaseResultMessage",
|
|
523
538
|
"BaseSessionConfig",
|
|
@@ -539,6 +554,7 @@ __all__ = [
|
|
|
539
554
|
"CompactBoundarySystemMessage",
|
|
540
555
|
"ConfigOutput",
|
|
541
556
|
"ContentBlock",
|
|
557
|
+
"ContentDocumentSource",
|
|
542
558
|
"ContextUsageAgent",
|
|
543
559
|
"ContextUsageAttachmentBreakdown",
|
|
544
560
|
"ContextUsageCategory",
|
|
@@ -563,7 +579,9 @@ __all__ = [
|
|
|
563
579
|
"CwdChangedHookInput",
|
|
564
580
|
"CwdChangedHookSpecificOutput",
|
|
565
581
|
"DirectoryAddedHookInput",
|
|
582
|
+
"DocumentBlock",
|
|
566
583
|
"DocumentMediaType",
|
|
584
|
+
"DocumentSource",
|
|
567
585
|
"EditOutput",
|
|
568
586
|
"ElicitationAction",
|
|
569
587
|
"ElicitationCompleteMessage",
|
|
@@ -584,6 +602,7 @@ __all__ = [
|
|
|
584
602
|
"FastModeState",
|
|
585
603
|
"FileChangedHookInput",
|
|
586
604
|
"FileChangedHookSpecificOutput",
|
|
605
|
+
"FileDocumentSource",
|
|
587
606
|
"FileEditInput",
|
|
588
607
|
"FileReadInput",
|
|
589
608
|
"FileWriteInput",
|
|
@@ -659,6 +678,7 @@ __all__ = [
|
|
|
659
678
|
"MiscMessages",
|
|
660
679
|
"ModelRefusalFallbackMessage",
|
|
661
680
|
"ModelRefusalNoFallbackMessage",
|
|
681
|
+
"ModelScopedUsageWindow",
|
|
662
682
|
"ModelUsage",
|
|
663
683
|
"Network",
|
|
664
684
|
"NewSession",
|
|
@@ -686,6 +706,7 @@ __all__ = [
|
|
|
686
706
|
"PermissionUpdateDestination",
|
|
687
707
|
"Permissions",
|
|
688
708
|
"PlainTextMediaType",
|
|
709
|
+
"PlainTextSource",
|
|
689
710
|
"PluginInstallSystemMessage",
|
|
690
711
|
"PostCompactHookInput",
|
|
691
712
|
"PostModelSwitchHookInput",
|
|
@@ -746,6 +767,7 @@ __all__ = [
|
|
|
746
767
|
"SDKControlGetTaskOutputRequest",
|
|
747
768
|
"SDKControlGetTaskOutputResponse",
|
|
748
769
|
"SDKControlGetUsageRequest",
|
|
770
|
+
"SDKControlGetUsageResponse",
|
|
749
771
|
"SDKControlInitializeRequest",
|
|
750
772
|
"SDKControlInterruptRequest",
|
|
751
773
|
"SDKControlInterruptResponse",
|
|
@@ -792,6 +814,7 @@ __all__ = [
|
|
|
792
814
|
"Sandbox",
|
|
793
815
|
"SdkBeta",
|
|
794
816
|
"SdkPluginConfig",
|
|
817
|
+
"ServerContentBlock",
|
|
795
818
|
"SessionConfig",
|
|
796
819
|
"SessionEndHookInput",
|
|
797
820
|
"SessionKey",
|
|
@@ -853,7 +876,10 @@ __all__ = [
|
|
|
853
876
|
"ToolUseSummaryMessage",
|
|
854
877
|
"ToolsPreset",
|
|
855
878
|
"TriggerMetadata",
|
|
879
|
+
"URLPDFSource",
|
|
856
880
|
"Usage",
|
|
881
|
+
"UsageRateLimits",
|
|
882
|
+
"UsageWindow",
|
|
857
883
|
"UserContentBlock",
|
|
858
884
|
"UserDocumentPrompt",
|
|
859
885
|
"UserDocumentURLPrompt",
|
|
@@ -8,9 +8,26 @@ therefore serves both purposes, avoiding a redundant conversion layer.
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
10
|
from collections.abc import Sequence
|
|
11
|
-
from typing import TYPE_CHECKING, Annotated, Any, Literal, assert_never, cast
|
|
11
|
+
from typing import TYPE_CHECKING, Annotated, Any, Literal, TypeIs, assert_never, cast, get_args
|
|
12
12
|
|
|
13
13
|
from anthropic.types import Model
|
|
14
|
+
from anthropic.types.beta import (
|
|
15
|
+
BetaAdvisorToolResultBlock,
|
|
16
|
+
BetaBashCodeExecutionToolResultBlock,
|
|
17
|
+
BetaCodeExecutionToolResultBlock,
|
|
18
|
+
BetaCompactionBlock,
|
|
19
|
+
BetaContainerUploadBlock,
|
|
20
|
+
BetaFallbackBlock,
|
|
21
|
+
BetaMCPToolListingBlock,
|
|
22
|
+
BetaMCPToolResultBlock,
|
|
23
|
+
BetaMCPToolUseBlock,
|
|
24
|
+
BetaRedactedThinkingBlock,
|
|
25
|
+
BetaServerToolUseBlock,
|
|
26
|
+
BetaTextEditorCodeExecutionToolResultBlock,
|
|
27
|
+
BetaToolSearchToolResultBlock,
|
|
28
|
+
BetaWebFetchToolResultBlock,
|
|
29
|
+
BetaWebSearchToolResultBlock,
|
|
30
|
+
)
|
|
14
31
|
from anthropic.types.beta.beta_tool_use_block import Caller
|
|
15
32
|
from pydantic import BaseModel, ConfigDict, Discriminator
|
|
16
33
|
|
|
@@ -21,6 +38,7 @@ from clawd_code_sdk.models.base import ClaudeCodeBaseModel, StopReason, ToolName
|
|
|
21
38
|
if TYPE_CHECKING:
|
|
22
39
|
from logfire._internal.integrations.llm_providers.semconv import (
|
|
23
40
|
BlobPart,
|
|
41
|
+
MessagePart,
|
|
24
42
|
ReasoningPart,
|
|
25
43
|
TextPart,
|
|
26
44
|
ToolCallPart,
|
|
@@ -148,24 +166,153 @@ class ImageBlock(BaseContentBlock):
|
|
|
148
166
|
)
|
|
149
167
|
|
|
150
168
|
|
|
169
|
+
class Base64PDFSource(BaseContentBlock):
|
|
170
|
+
"""Base64-encoded PDF document."""
|
|
171
|
+
|
|
172
|
+
type: Literal["base64"] = "base64"
|
|
173
|
+
media_type: Literal["application/pdf"] = "application/pdf"
|
|
174
|
+
data: str
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class PlainTextSource(BaseContentBlock):
|
|
178
|
+
"""Plain text document."""
|
|
179
|
+
|
|
180
|
+
type: Literal["text"] = "text"
|
|
181
|
+
media_type: Literal["text/plain"] = "text/plain"
|
|
182
|
+
data: str
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class URLPDFSource(BaseContentBlock):
|
|
186
|
+
"""PDF document referenced by URL."""
|
|
187
|
+
|
|
188
|
+
type: Literal["url"] = "url"
|
|
189
|
+
url: str
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class FileDocumentSource(BaseContentBlock):
|
|
193
|
+
"""Document uploaded through the Files API."""
|
|
194
|
+
|
|
195
|
+
type: Literal["file"] = "file"
|
|
196
|
+
file_id: str
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class ContentDocumentSource(BaseContentBlock):
|
|
200
|
+
"""Document made of content blocks (text / images)."""
|
|
201
|
+
|
|
202
|
+
type: Literal["content"] = "content"
|
|
203
|
+
content: str | list[dict[str, Any]]
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
type DocumentSource = Annotated[
|
|
207
|
+
Base64PDFSource | PlainTextSource | URLPDFSource | FileDocumentSource | ContentDocumentSource,
|
|
208
|
+
Discriminator("type"),
|
|
209
|
+
]
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class DocumentBlock(BaseContentBlock):
|
|
213
|
+
"""Document content block (e.g. an attached PDF) in a user message."""
|
|
214
|
+
|
|
215
|
+
type: Literal["document"] = "document"
|
|
216
|
+
source: DocumentSource
|
|
217
|
+
title: str | None = None
|
|
218
|
+
context: str | None = None
|
|
219
|
+
citations: dict[str, Any] | None = None
|
|
220
|
+
|
|
221
|
+
|
|
151
222
|
# =============================================================================
|
|
152
223
|
# Unions and adapters
|
|
153
224
|
# =============================================================================
|
|
154
225
|
|
|
226
|
+
type ServerContentBlock = (
|
|
227
|
+
BetaRedactedThinkingBlock
|
|
228
|
+
| BetaServerToolUseBlock
|
|
229
|
+
| BetaWebSearchToolResultBlock
|
|
230
|
+
| BetaWebFetchToolResultBlock
|
|
231
|
+
| BetaAdvisorToolResultBlock
|
|
232
|
+
| BetaCodeExecutionToolResultBlock
|
|
233
|
+
| BetaBashCodeExecutionToolResultBlock
|
|
234
|
+
| BetaTextEditorCodeExecutionToolResultBlock
|
|
235
|
+
| BetaToolSearchToolResultBlock
|
|
236
|
+
| BetaMCPToolUseBlock
|
|
237
|
+
| BetaMCPToolResultBlock
|
|
238
|
+
| BetaContainerUploadBlock
|
|
239
|
+
| BetaCompactionBlock
|
|
240
|
+
| BetaFallbackBlock
|
|
241
|
+
| BetaMCPToolListingBlock
|
|
242
|
+
)
|
|
243
|
+
"""Assistant content blocks produced by the API itself (server tools, MCP connector, ...).
|
|
244
|
+
|
|
245
|
+
These use the Anthropic SDK's models directly; clawd has no extra behaviour for them.
|
|
246
|
+
"""
|
|
247
|
+
|
|
248
|
+
SERVER_CONTENT_BLOCK_TYPES: tuple[type[BaseModel], ...] = get_args(ServerContentBlock.__value__)
|
|
249
|
+
"""Runtime tuple of :data:`ServerContentBlock` members (for ``isinstance`` checks)."""
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def is_server_block(block: object) -> TypeIs[ServerContentBlock]:
|
|
253
|
+
"""Whether a content block was produced by the API itself (see :data:`ServerContentBlock`)."""
|
|
254
|
+
return isinstance(block, SERVER_CONTENT_BLOCK_TYPES)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def server_block_to_otel(block: ServerContentBlock) -> MessagePart | None:
|
|
258
|
+
"""OpenTelemetry semconv part for an API-produced block (None if it has no equivalent)."""
|
|
259
|
+
from logfire._internal.integrations.llm_providers.semconv import (
|
|
260
|
+
ReasoningPart,
|
|
261
|
+
ToolCallPart,
|
|
262
|
+
ToolCallResponsePart,
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
match block:
|
|
266
|
+
case BetaRedactedThinkingBlock():
|
|
267
|
+
return ReasoningPart(type="reasoning", content="[redacted]")
|
|
268
|
+
case (
|
|
269
|
+
BetaServerToolUseBlock(id=tool_id, name=name, input=input_)
|
|
270
|
+
| BetaMCPToolUseBlock(id=tool_id, name=name, input=input_)
|
|
271
|
+
):
|
|
272
|
+
return ToolCallPart(type="tool_call", id=tool_id, name=name, arguments=input_)
|
|
273
|
+
case (
|
|
274
|
+
BetaWebSearchToolResultBlock(tool_use_id=tool_id)
|
|
275
|
+
| BetaWebFetchToolResultBlock(tool_use_id=tool_id)
|
|
276
|
+
| BetaAdvisorToolResultBlock(tool_use_id=tool_id)
|
|
277
|
+
| BetaCodeExecutionToolResultBlock(tool_use_id=tool_id)
|
|
278
|
+
| BetaBashCodeExecutionToolResultBlock(tool_use_id=tool_id)
|
|
279
|
+
| BetaTextEditorCodeExecutionToolResultBlock(tool_use_id=tool_id)
|
|
280
|
+
| BetaToolSearchToolResultBlock(tool_use_id=tool_id)
|
|
281
|
+
| BetaMCPToolResultBlock(tool_use_id=tool_id)
|
|
282
|
+
):
|
|
283
|
+
response = block.model_dump(mode="json", exclude={"type", "tool_use_id"})
|
|
284
|
+
return ToolCallResponsePart(type="tool_call_response", id=tool_id, response=response)
|
|
285
|
+
case (
|
|
286
|
+
BetaContainerUploadBlock()
|
|
287
|
+
| BetaCompactionBlock()
|
|
288
|
+
| BetaFallbackBlock()
|
|
289
|
+
| BetaMCPToolListingBlock()
|
|
290
|
+
):
|
|
291
|
+
return None
|
|
292
|
+
case _ as unreachable:
|
|
293
|
+
assert_never(unreachable)
|
|
294
|
+
|
|
295
|
+
|
|
155
296
|
AssistantContentBlock = Annotated[
|
|
156
|
-
TextBlock | ThinkingBlock | ToolUseBlock,
|
|
297
|
+
TextBlock | ThinkingBlock | ToolUseBlock | ServerContentBlock,
|
|
157
298
|
Discriminator("type"),
|
|
158
299
|
]
|
|
159
|
-
"""Content blocks that appear in assistant messages."""
|
|
300
|
+
"""Content blocks that appear in assistant messages (any Messages API block type)."""
|
|
160
301
|
|
|
161
302
|
UserContentBlock = Annotated[
|
|
162
|
-
TextBlock | ToolResultBlock | ImageBlock,
|
|
303
|
+
TextBlock | ToolResultBlock | ImageBlock | DocumentBlock,
|
|
163
304
|
Discriminator("type"),
|
|
164
305
|
]
|
|
165
306
|
"""Content blocks that appear in user messages."""
|
|
166
307
|
|
|
167
308
|
ContentBlock = Annotated[
|
|
168
|
-
TextBlock
|
|
309
|
+
TextBlock
|
|
310
|
+
| ThinkingBlock
|
|
311
|
+
| ToolUseBlock
|
|
312
|
+
| ToolResultBlock
|
|
313
|
+
| ImageBlock
|
|
314
|
+
| DocumentBlock
|
|
315
|
+
| ServerContentBlock,
|
|
169
316
|
Discriminator("type"),
|
|
170
317
|
]
|
|
171
318
|
"""All content block types (used for storage/JSONL which doesn't distinguish by role)."""
|
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
from typing import Literal
|
|
6
6
|
|
|
7
|
-
from pydantic import Field
|
|
7
|
+
from pydantic import ConfigDict, Field
|
|
8
8
|
|
|
9
9
|
from clawd_code_sdk.models.base import ClaudeCodeBaseModel
|
|
10
10
|
from clawd_code_sdk.models.usage import ModelUsage, Usage
|
|
@@ -274,6 +274,7 @@ class UsageReportDisplayName(ClaudeCodeBaseModel):
|
|
|
274
274
|
"""Wrapper holding a display name."""
|
|
275
275
|
|
|
276
276
|
display_name: str
|
|
277
|
+
id: str | None = None
|
|
277
278
|
|
|
278
279
|
|
|
279
280
|
class UsageReportLimitScope(ClaudeCodeBaseModel):
|
|
@@ -296,7 +297,13 @@ class UsageReportLimit(ClaudeCodeBaseModel):
|
|
|
296
297
|
|
|
297
298
|
|
|
298
299
|
class UsageReportExtraUsage(ClaudeCodeBaseModel):
|
|
299
|
-
"""Extra (overage) usage state.
|
|
300
|
+
"""Extra (overage) usage state.
|
|
301
|
+
|
|
302
|
+
Passed through from the claude.ai usage endpoint, which adds fields freely; unknown
|
|
303
|
+
fields are ignored even in strict (test) mode.
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
model_config = ConfigDict(extra="ignore")
|
|
300
307
|
|
|
301
308
|
is_enabled: bool
|
|
302
309
|
monthly_limit: float | None
|
|
@@ -312,6 +319,44 @@ class UsageReportRateLimits(ClaudeCodeBaseModel):
|
|
|
312
319
|
extra_usage: UsageReportExtraUsage | None = None
|
|
313
320
|
|
|
314
321
|
|
|
322
|
+
class UsageWindow(ClaudeCodeBaseModel):
|
|
323
|
+
"""Utilization of one plan rate-limit window."""
|
|
324
|
+
|
|
325
|
+
model_config = ConfigDict(extra="ignore") # server-defined, see UsageReportExtraUsage
|
|
326
|
+
|
|
327
|
+
utilization: float | None
|
|
328
|
+
"""Percentage of the window used, 0-100."""
|
|
329
|
+
resets_at: str | None
|
|
330
|
+
"""ISO 8601 timestamp of the window's reset."""
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
class ModelScopedUsageWindow(UsageWindow):
|
|
334
|
+
"""Per-model weekly window."""
|
|
335
|
+
|
|
336
|
+
display_name: str
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
class UsageRateLimits(ClaudeCodeBaseModel):
|
|
340
|
+
"""Plan rate-limit windows from the claude.ai usage endpoint.
|
|
341
|
+
|
|
342
|
+
Only the fields documented by the TypeScript SDK are modelled; the endpoint carries
|
|
343
|
+
many experimental extras, which are ignored.
|
|
344
|
+
"""
|
|
345
|
+
|
|
346
|
+
model_config = ConfigDict(extra="ignore")
|
|
347
|
+
|
|
348
|
+
five_hour: UsageWindow | None = None
|
|
349
|
+
seven_day: UsageWindow | None = None
|
|
350
|
+
seven_day_oauth_apps: UsageWindow | None = None
|
|
351
|
+
seven_day_opus: UsageWindow | None = None
|
|
352
|
+
seven_day_sonnet: UsageWindow | None = None
|
|
353
|
+
model_scoped: list[ModelScopedUsageWindow] | None = None
|
|
354
|
+
"""Per-model weekly windows (absent when unknown, empty when there are none)."""
|
|
355
|
+
extra_usage: UsageReportExtraUsage | None = None
|
|
356
|
+
limits: list[UsageReportLimit] | None = None
|
|
357
|
+
"""The server's usage rows, in the server's order (render verbatim)."""
|
|
358
|
+
|
|
359
|
+
|
|
315
360
|
class SDKUsageReport(ClaudeCodeBaseModel):
|
|
316
361
|
"""Session usage report (same data as /usage)."""
|
|
317
362
|
|
|
@@ -15,6 +15,7 @@ from clawd_code_sdk.models.base import (
|
|
|
15
15
|
ModelName,
|
|
16
16
|
PermissionMode,
|
|
17
17
|
)
|
|
18
|
+
from clawd_code_sdk.models.context_usage import UsageRateLimits, UsageReportSession
|
|
18
19
|
from clawd_code_sdk.models.hooks import HookEvent, HookInput
|
|
19
20
|
from clawd_code_sdk.models.mcp import (
|
|
20
21
|
ExternalMcpServerConfig,
|
|
@@ -671,6 +672,20 @@ class SDKControlGetUsageRequest(_ControlBase):
|
|
|
671
672
|
skip_behaviors: bool | None = None
|
|
672
673
|
|
|
673
674
|
|
|
675
|
+
class SDKControlGetUsageResponse(ClaudeCodeBaseModel):
|
|
676
|
+
"""Structured /usage data: session totals and plan rate-limit windows. Experimental."""
|
|
677
|
+
|
|
678
|
+
session: UsageReportSession
|
|
679
|
+
"""Cost and usage accumulated by the current session."""
|
|
680
|
+
subscription_type: str | None
|
|
681
|
+
"""claude.ai plan (e.g. "pro", "max"), None without a subscription."""
|
|
682
|
+
rate_limits_available: bool
|
|
683
|
+
"""False when plan limits do not apply (API key, Bedrock, Vertex, ...)."""
|
|
684
|
+
rate_limits: UsageRateLimits | None
|
|
685
|
+
behaviors: dict[str, Any] | None = None
|
|
686
|
+
"""What contributes to usage, from a scan of local transcripts (None if skipped)."""
|
|
687
|
+
|
|
688
|
+
|
|
674
689
|
class SDKControlGetBinaryVersionRequest(_ControlBase):
|
|
675
690
|
"""Requests the version of the running Claude Code binary."""
|
|
676
691
|
|
|
@@ -404,7 +404,15 @@ class AssistantMessage(BaseModel):
|
|
|
404
404
|
"""Convert SDK content block objects into semconv OutputMessages."""
|
|
405
405
|
from logfire._internal.integrations.llm_providers.semconv import OutputMessage
|
|
406
406
|
|
|
407
|
-
|
|
407
|
+
from clawd_code_sdk.models.content_blocks import is_server_block, server_block_to_otel
|
|
408
|
+
|
|
409
|
+
parts: list[MessagePart] = []
|
|
410
|
+
for block in self.content:
|
|
411
|
+
if is_server_block(block):
|
|
412
|
+
if (part := server_block_to_otel(block)) is not None:
|
|
413
|
+
parts.append(part)
|
|
414
|
+
else:
|
|
415
|
+
parts.append(block.to_otel())
|
|
408
416
|
return OutputMessage(role="assistant", parts=parts)
|
|
409
417
|
|
|
410
418
|
|
|
@@ -54,11 +54,32 @@ from __future__ import annotations
|
|
|
54
54
|
import json as _json
|
|
55
55
|
from typing import TYPE_CHECKING, assert_never
|
|
56
56
|
|
|
57
|
-
from anthropic.types.beta import
|
|
57
|
+
from anthropic.types.beta import (
|
|
58
|
+
BetaAdvisorToolResultBlock,
|
|
59
|
+
BetaBashCodeExecutionToolResultBlock,
|
|
60
|
+
BetaCodeExecutionToolResultBlock,
|
|
61
|
+
BetaCompactionBlock,
|
|
62
|
+
BetaContainerUploadBlock,
|
|
63
|
+
BetaFallbackBlock,
|
|
64
|
+
BetaMCPToolListingBlock,
|
|
65
|
+
BetaMCPToolResultBlock,
|
|
66
|
+
BetaMCPToolUseBlock,
|
|
67
|
+
BetaMessage,
|
|
68
|
+
BetaRawContentBlockStartEvent,
|
|
69
|
+
BetaRawMessageStartEvent,
|
|
70
|
+
BetaRedactedThinkingBlock,
|
|
71
|
+
BetaServerToolUseBlock,
|
|
72
|
+
BetaTextEditorCodeExecutionToolResultBlock,
|
|
73
|
+
BetaToolSearchToolResultBlock,
|
|
74
|
+
BetaUsage,
|
|
75
|
+
BetaWebFetchToolResultBlock,
|
|
76
|
+
BetaWebSearchToolResultBlock,
|
|
77
|
+
)
|
|
58
78
|
|
|
59
79
|
from clawd_code_sdk.models import (
|
|
60
80
|
AssistantMessage,
|
|
61
81
|
AssistantMessageContent,
|
|
82
|
+
DocumentBlock,
|
|
62
83
|
ImageBlock,
|
|
63
84
|
MessageParam,
|
|
64
85
|
ResultErrorMessage,
|
|
@@ -184,8 +205,30 @@ def _make_block_start(
|
|
|
184
205
|
yield StreamEvent.block_start_tool_use(
|
|
185
206
|
tool_use_id=block_id, name=name, index=index, session_id=session_id, uuid=uuid
|
|
186
207
|
)
|
|
187
|
-
case ToolResultBlock() | ImageBlock():
|
|
208
|
+
case ToolResultBlock() | ImageBlock() | DocumentBlock():
|
|
188
209
|
return
|
|
210
|
+
case (
|
|
211
|
+
BetaRedactedThinkingBlock()
|
|
212
|
+
| BetaServerToolUseBlock()
|
|
213
|
+
| BetaWebSearchToolResultBlock()
|
|
214
|
+
| BetaWebFetchToolResultBlock()
|
|
215
|
+
| BetaAdvisorToolResultBlock()
|
|
216
|
+
| BetaCodeExecutionToolResultBlock()
|
|
217
|
+
| BetaBashCodeExecutionToolResultBlock()
|
|
218
|
+
| BetaTextEditorCodeExecutionToolResultBlock()
|
|
219
|
+
| BetaToolSearchToolResultBlock()
|
|
220
|
+
| BetaMCPToolUseBlock()
|
|
221
|
+
| BetaMCPToolResultBlock()
|
|
222
|
+
| BetaContainerUploadBlock()
|
|
223
|
+
| BetaCompactionBlock()
|
|
224
|
+
| BetaFallbackBlock()
|
|
225
|
+
| BetaMCPToolListingBlock()
|
|
226
|
+
):
|
|
227
|
+
# API-produced blocks arrive complete in their content_block_start.
|
|
228
|
+
event = BetaRawContentBlockStartEvent(
|
|
229
|
+
type="content_block_start", index=index, content_block=block
|
|
230
|
+
)
|
|
231
|
+
yield StreamEvent(event=event, session_id=session_id, uuid=uuid)
|
|
189
232
|
case _ as unreachable:
|
|
190
233
|
assert_never(unreachable)
|
|
191
234
|
|
|
@@ -220,7 +263,27 @@ def _make_block_delta(
|
|
|
220
263
|
session_id=session_id,
|
|
221
264
|
uuid=uuid,
|
|
222
265
|
)
|
|
223
|
-
|
|
266
|
+
# Results, images, documents and API-produced blocks carry no deltas.
|
|
267
|
+
case (
|
|
268
|
+
ToolResultBlock()
|
|
269
|
+
| ImageBlock()
|
|
270
|
+
| DocumentBlock()
|
|
271
|
+
| BetaRedactedThinkingBlock()
|
|
272
|
+
| BetaServerToolUseBlock()
|
|
273
|
+
| BetaWebSearchToolResultBlock()
|
|
274
|
+
| BetaWebFetchToolResultBlock()
|
|
275
|
+
| BetaAdvisorToolResultBlock()
|
|
276
|
+
| BetaCodeExecutionToolResultBlock()
|
|
277
|
+
| BetaBashCodeExecutionToolResultBlock()
|
|
278
|
+
| BetaTextEditorCodeExecutionToolResultBlock()
|
|
279
|
+
| BetaToolSearchToolResultBlock()
|
|
280
|
+
| BetaMCPToolUseBlock()
|
|
281
|
+
| BetaMCPToolResultBlock()
|
|
282
|
+
| BetaContainerUploadBlock()
|
|
283
|
+
| BetaCompactionBlock()
|
|
284
|
+
| BetaFallbackBlock()
|
|
285
|
+
| BetaMCPToolListingBlock()
|
|
286
|
+
):
|
|
224
287
|
return
|
|
225
288
|
case _ as unreachable:
|
|
226
289
|
assert_never(unreachable)
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Parsing of every Messages API content block type in assistant and user messages."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, get_args
|
|
6
|
+
|
|
7
|
+
from anthropic.types.beta import BetaContentBlock
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from clawd_code_sdk.anthropic_stream import _synthesize_events
|
|
11
|
+
from clawd_code_sdk.models import (
|
|
12
|
+
SERVER_CONTENT_BLOCK_TYPES,
|
|
13
|
+
AssistantMessage,
|
|
14
|
+
DocumentBlock,
|
|
15
|
+
TextBlock,
|
|
16
|
+
ThinkingBlock,
|
|
17
|
+
ToolUseBlock,
|
|
18
|
+
UserMessage,
|
|
19
|
+
message_adapter,
|
|
20
|
+
)
|
|
21
|
+
from clawd_code_sdk.models.content_blocks import is_server_block, server_block_to_otel
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
SERVER_BLOCKS: dict[str, dict[str, Any]] = {
|
|
25
|
+
"redacted_thinking": {"type": "redacted_thinking", "data": "EmwKAhgBEgy3va3pzix/LafPsn4a"},
|
|
26
|
+
"server_tool_use": {
|
|
27
|
+
"type": "server_tool_use",
|
|
28
|
+
"id": "srvtoolu_01",
|
|
29
|
+
"name": "web_search",
|
|
30
|
+
"input": {"query": "python 3.14"},
|
|
31
|
+
},
|
|
32
|
+
"web_search_tool_result": {
|
|
33
|
+
"type": "web_search_tool_result",
|
|
34
|
+
"tool_use_id": "srvtoolu_01",
|
|
35
|
+
"content": [
|
|
36
|
+
{
|
|
37
|
+
"type": "web_search_result",
|
|
38
|
+
"title": "Python 3.14",
|
|
39
|
+
"url": "https://python.org",
|
|
40
|
+
"encrypted_content": "abc",
|
|
41
|
+
"page_age": None,
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
"web_fetch_tool_result": {
|
|
46
|
+
"type": "web_fetch_tool_result",
|
|
47
|
+
"tool_use_id": "srvtoolu_02",
|
|
48
|
+
"content": {"type": "web_fetch_tool_result_error", "error_code": "url_not_accessible"},
|
|
49
|
+
},
|
|
50
|
+
"code_execution_tool_result": {
|
|
51
|
+
"type": "code_execution_tool_result",
|
|
52
|
+
"tool_use_id": "srvtoolu_03",
|
|
53
|
+
"content": {
|
|
54
|
+
"type": "code_execution_result",
|
|
55
|
+
"stdout": "1",
|
|
56
|
+
"stderr": "",
|
|
57
|
+
"return_code": 0,
|
|
58
|
+
"content": [],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
"mcp_tool_use": {
|
|
62
|
+
"type": "mcp_tool_use",
|
|
63
|
+
"id": "mcptoolu_01",
|
|
64
|
+
"name": "search",
|
|
65
|
+
"server_name": "docs",
|
|
66
|
+
"input": {"q": "x"},
|
|
67
|
+
},
|
|
68
|
+
"mcp_tool_result": {
|
|
69
|
+
"type": "mcp_tool_result",
|
|
70
|
+
"tool_use_id": "mcptoolu_01",
|
|
71
|
+
"is_error": False,
|
|
72
|
+
"content": [{"type": "text", "text": "found"}],
|
|
73
|
+
},
|
|
74
|
+
"container_upload": {"type": "container_upload", "file_id": "file_01"},
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _assistant(*blocks: dict[str, Any]) -> dict[str, Any]:
|
|
79
|
+
return {
|
|
80
|
+
"type": "assistant",
|
|
81
|
+
"session_id": "s",
|
|
82
|
+
"uuid": "u",
|
|
83
|
+
"parent_tool_use_id": None,
|
|
84
|
+
"message": {
|
|
85
|
+
"id": "msg_01",
|
|
86
|
+
"type": "message",
|
|
87
|
+
"role": "assistant",
|
|
88
|
+
"model": "claude-sonnet-5",
|
|
89
|
+
"stop_reason": None,
|
|
90
|
+
"stop_sequence": None,
|
|
91
|
+
"content": list(blocks),
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@pytest.mark.parametrize("block_type", sorted(SERVER_BLOCKS))
|
|
97
|
+
def test_assistant_message_accepts_api_block(block_type: str) -> None:
|
|
98
|
+
message = message_adapter.validate_python(_assistant(SERVER_BLOCKS[block_type]))
|
|
99
|
+
assert isinstance(message, AssistantMessage)
|
|
100
|
+
(block,) = message.content
|
|
101
|
+
assert is_server_block(block)
|
|
102
|
+
assert block.type == block_type
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_every_anthropic_content_block_type_is_accepted() -> None:
|
|
106
|
+
"""New Messages API block types must fail here, not at runtime."""
|
|
107
|
+
own = {TextBlock, ThinkingBlock, ToolUseBlock}
|
|
108
|
+
own_types = {cls.model_fields["type"].default for cls in own}
|
|
109
|
+
covered = own_types | {cls.model_fields["type"].default for cls in SERVER_CONTENT_BLOCK_TYPES}
|
|
110
|
+
api_types = {
|
|
111
|
+
cls.model_fields["type"].default for cls in get_args(get_args(BetaContentBlock)[0])
|
|
112
|
+
}
|
|
113
|
+
assert api_types <= covered
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_mixed_assistant_content_keeps_order() -> None:
|
|
117
|
+
data = _assistant(
|
|
118
|
+
{"type": "thinking", "thinking": "hmm", "signature": "sig"},
|
|
119
|
+
SERVER_BLOCKS["server_tool_use"],
|
|
120
|
+
SERVER_BLOCKS["web_search_tool_result"],
|
|
121
|
+
{"type": "text", "text": "Done."},
|
|
122
|
+
)
|
|
123
|
+
message = message_adapter.validate_python(data)
|
|
124
|
+
assert isinstance(message, AssistantMessage)
|
|
125
|
+
assert [b.type for b in message.content] == [
|
|
126
|
+
"thinking",
|
|
127
|
+
"server_tool_use",
|
|
128
|
+
"web_search_tool_result",
|
|
129
|
+
"text",
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_api_blocks_convert_to_stream_events_and_otel() -> None:
|
|
134
|
+
message = message_adapter.validate_python(
|
|
135
|
+
_assistant(SERVER_BLOCKS["server_tool_use"], SERVER_BLOCKS["web_search_tool_result"])
|
|
136
|
+
)
|
|
137
|
+
assert isinstance(message, AssistantMessage)
|
|
138
|
+
events = _synthesize_events(message)
|
|
139
|
+
starts = [e for e in events if e.type == "content_block_start"]
|
|
140
|
+
assert [s.content_block.type for s in starts] == ["server_tool_use", "web_search_tool_result"]
|
|
141
|
+
tool_use, tool_result = message.content
|
|
142
|
+
assert is_server_block(tool_use)
|
|
143
|
+
assert is_server_block(tool_result)
|
|
144
|
+
call = server_block_to_otel(tool_use)
|
|
145
|
+
response = server_block_to_otel(tool_result)
|
|
146
|
+
assert call is not None
|
|
147
|
+
assert call["type"] == "tool_call"
|
|
148
|
+
assert response is not None
|
|
149
|
+
assert response["type"] == "tool_call_response"
|
|
150
|
+
otel = message.to_otel()
|
|
151
|
+
assert [p["type"] for p in otel["parts"]] == ["tool_call", "tool_call_response"]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@pytest.mark.parametrize(
|
|
155
|
+
"source",
|
|
156
|
+
[
|
|
157
|
+
{"type": "base64", "media_type": "application/pdf", "data": "JVBERi0x"},
|
|
158
|
+
{"type": "text", "media_type": "text/plain", "data": "hello"},
|
|
159
|
+
{"type": "url", "url": "https://example.com/a.pdf"},
|
|
160
|
+
{"type": "file", "file_id": "file_01"},
|
|
161
|
+
],
|
|
162
|
+
)
|
|
163
|
+
def test_user_message_accepts_document(source: dict[str, Any]) -> None:
|
|
164
|
+
data = {
|
|
165
|
+
"type": "user",
|
|
166
|
+
"session_id": "s",
|
|
167
|
+
"uuid": "u",
|
|
168
|
+
"parent_tool_use_id": None,
|
|
169
|
+
"message": {
|
|
170
|
+
"role": "user",
|
|
171
|
+
"content": [
|
|
172
|
+
{"type": "document", "source": source, "title": "Spec"},
|
|
173
|
+
{"type": "text", "text": "Summarize this"},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
message = message_adapter.validate_python(data)
|
|
178
|
+
assert isinstance(message, UserMessage)
|
|
179
|
+
assert not isinstance(message.content, str)
|
|
180
|
+
document = message.content[0]
|
|
181
|
+
assert isinstance(document, DocumentBlock)
|
|
182
|
+
assert document.source.type == source["type"]
|
|
183
|
+
assert document.title == "Spec"
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""The get_usage control response (real response shape, trimmed)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from clawd_code_sdk.models import SDKControlGetUsageResponse
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
WINDOW_EXTRAS = {"limit_dollars": None, "used_dollars": None, "locked_reason": None}
|
|
11
|
+
|
|
12
|
+
RESPONSE: dict[str, Any] = {
|
|
13
|
+
"session": {
|
|
14
|
+
"total_cost_usd": 0,
|
|
15
|
+
"total_api_duration_ms": 0,
|
|
16
|
+
"total_duration_ms": 475,
|
|
17
|
+
"total_lines_added": 0,
|
|
18
|
+
"total_lines_removed": 0,
|
|
19
|
+
"model_usage": {},
|
|
20
|
+
},
|
|
21
|
+
"subscription_type": "pro",
|
|
22
|
+
"rate_limits_available": True,
|
|
23
|
+
"rate_limits": {
|
|
24
|
+
"five_hour": {
|
|
25
|
+
"utilization": 66,
|
|
26
|
+
"resets_at": "2026-11-20T09:39:59.762577+00:00",
|
|
27
|
+
**WINDOW_EXTRAS,
|
|
28
|
+
},
|
|
29
|
+
"seven_day": {
|
|
30
|
+
"utilization": 35,
|
|
31
|
+
"resets_at": "2026-11-22T12:59:59.762597+00:00",
|
|
32
|
+
**WINDOW_EXTRAS,
|
|
33
|
+
},
|
|
34
|
+
"seven_day_opus": None,
|
|
35
|
+
"extra_usage": {
|
|
36
|
+
"is_enabled": False,
|
|
37
|
+
"monthly_limit": None,
|
|
38
|
+
"used_credits": None,
|
|
39
|
+
"utilization": None,
|
|
40
|
+
"currency": None,
|
|
41
|
+
"spend_limit_reached": False,
|
|
42
|
+
},
|
|
43
|
+
"limits": [
|
|
44
|
+
{
|
|
45
|
+
"kind": "session",
|
|
46
|
+
"group": "session",
|
|
47
|
+
"percent": 66,
|
|
48
|
+
"resets_at": "2026-11-20T09:39:59.762577+00:00",
|
|
49
|
+
"severity": "normal",
|
|
50
|
+
"is_active": True,
|
|
51
|
+
"scope": None,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"kind": "weekly_scoped",
|
|
55
|
+
"group": "weekly",
|
|
56
|
+
"percent": 0,
|
|
57
|
+
"resets_at": "2026-11-22T13:00:00+00:00",
|
|
58
|
+
"severity": "normal",
|
|
59
|
+
"is_active": False,
|
|
60
|
+
"scope": {"model": {"id": None, "display_name": "Fable"}, "surface": None},
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
"model_scoped": [
|
|
64
|
+
{"display_name": "Fable", "utilization": 0, "resets_at": "2026-11-22T13:00:00+00:00"}
|
|
65
|
+
],
|
|
66
|
+
# Undocumented, experimental fields are ignored
|
|
67
|
+
"pace": {"status": "on_track"},
|
|
68
|
+
"spend": {"percent": 0},
|
|
69
|
+
},
|
|
70
|
+
"behaviors": None,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_parses_real_response() -> None:
|
|
75
|
+
usage = SDKControlGetUsageResponse.model_validate(RESPONSE)
|
|
76
|
+
assert usage.subscription_type == "pro"
|
|
77
|
+
limits = usage.rate_limits
|
|
78
|
+
assert limits is not None
|
|
79
|
+
assert limits.five_hour is not None
|
|
80
|
+
assert limits.five_hour.utilization == 66
|
|
81
|
+
assert limits.seven_day_opus is None
|
|
82
|
+
assert limits.model_scoped is not None
|
|
83
|
+
assert limits.model_scoped[0].display_name == "Fable"
|
|
84
|
+
assert limits.limits is not None
|
|
85
|
+
assert [row.kind for row in limits.limits] == ["session", "weekly_scoped"]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_without_plan_limits() -> None:
|
|
89
|
+
data = {**RESPONSE, "subscription_type": None, "rate_limits_available": False}
|
|
90
|
+
usage = SDKControlGetUsageResponse.model_validate({**data, "rate_limits": None})
|
|
91
|
+
assert usage.rate_limits is None
|
|
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
|
{clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/sdk_mcp_bridge.py
RENAMED
|
File without changes
|
{clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/session_mutations.py
RENAMED
|
File without changes
|
|
File without changes
|
{clawd_code_sdk-1.2.2 → clawd_code_sdk-1.2.4}/src/clawd_code_sdk/_internal/transport/__init__.py
RENAMED
|
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
|
|
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
|
|
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
|