-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy path__init__.py
More file actions
102 lines (87 loc) · 2.29 KB
/
Copy path__init__.py
File metadata and controls
102 lines (87 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Claude SDK for Python."""
import os
from collections.abc import AsyncIterator
from ._errors import (
ClaudeSDKError,
CLIConnectionError,
CLIJSONDecodeError,
CLINotFoundError,
ProcessError,
)
from ._internal.client import InternalClient
from .types import (
AssistantMessage,
ClaudeCodeOptions,
ContentBlock,
McpServerConfig,
Message,
PermissionMode,
ResultMessage,
SystemMessage,
TextBlock,
ToolResultBlock,
ToolUseBlock,
UserMessage,
)
__version__ = "0.0.13"
__all__ = [
# Main function
"query",
# Types
"PermissionMode",
"McpServerConfig",
"UserMessage",
"AssistantMessage",
"SystemMessage",
"ResultMessage",
"Message",
"ClaudeCodeOptions",
"TextBlock",
"ToolUseBlock",
"ToolResultBlock",
"ContentBlock",
# Errors
"ClaudeSDKError",
"CLIConnectionError",
"CLINotFoundError",
"ProcessError",
"CLIJSONDecodeError",
]
async def query(
*, prompt: str, options: ClaudeCodeOptions | None = None
) -> AsyncIterator[Message]:
"""
Query Claude Code.
Python SDK for interacting with Claude Code.
Args:
prompt: The prompt to send to Claude
options: Optional configuration (defaults to ClaudeCodeOptions() if None).
Set options.permission_mode to control tool execution:
- 'default': CLI prompts for dangerous tools
- 'acceptEdits': Auto-accept file edits
- 'bypassPermissions': Allow all tools (use with caution)
Set options.cwd for working directory.
Yields:
Messages from the conversation
Example:
```python
# Simple usage
async for message in query(prompt="Hello"):
print(message)
# With options
async for message in query(
prompt="Hello",
options=ClaudeCodeOptions(
system_prompt="You are helpful",
cwd="/home/user"
)
):
print(message)
```
"""
if options is None:
options = ClaudeCodeOptions()
os.environ["CLAUDE_CODE_ENTRYPOINT"] = "sdk-py"
client = InternalClient()
async for message in client.process_query(prompt=prompt, options=options):
yield message