Add comprehensive documentation for Claude Agent SDK including usage patterns, test examples, and development guide. Includes: - CLAUDE.md with project structure and quick reference - SDK test examples for query, client, and hooks - Shared test utilities for auth and tmpdir management - Detailed SDK overview and capabilities documentation
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
"""Example: query() one-shot — Read and Edit files.
|
|
|
|
Demonstrates:
|
|
- .env-based auth (ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN)
|
|
- query() async iterator for simple one-shot tasks
|
|
- allowed_tools for auto-approval
|
|
- permission_mode="acceptEdits"
|
|
- Reading and editing a file in cwd
|
|
- Manual tmpdir management (avoids Windows cleanup races)
|
|
|
|
Usage:
|
|
cd docs/claude
|
|
../../.venv/Scripts/python test_query_read_edit.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
|
|
from _sdk_test_common import auth_env, make_tmpdir, remove_tmpdir, setup_auth
|
|
|
|
|
|
async def main() -> int:
|
|
ok, msg = setup_auth()
|
|
print(msg)
|
|
if not ok:
|
|
return 1
|
|
|
|
from claude_agent_sdk import (
|
|
AssistantMessage,
|
|
ClaudeAgentOptions,
|
|
ResultMessage,
|
|
TextBlock,
|
|
ToolUseBlock,
|
|
query,
|
|
)
|
|
|
|
tmpdir = make_tmpdir("read-edit-")
|
|
try:
|
|
test_file = tmpdir / "hello.txt"
|
|
test_file.write_text("hello world\n", encoding="utf-8")
|
|
print(f"cwd: {tmpdir}")
|
|
|
|
# --- Step 1: Read ---
|
|
print("\n--- Read ---")
|
|
opts = ClaudeAgentOptions(
|
|
cwd=str(tmpdir),
|
|
allowed_tools=["Read"],
|
|
permission_mode="acceptEdits",
|
|
max_turns=2,
|
|
env=auth_env(),
|
|
)
|
|
async for msg in query(prompt="Read hello.txt and show its content.", options=opts):
|
|
if isinstance(msg, AssistantMessage):
|
|
for block in msg.content:
|
|
if isinstance(block, ToolUseBlock):
|
|
print(f" ToolUse: {block.name}({block.input})")
|
|
elif isinstance(block, TextBlock):
|
|
print(f" Claude: {block.text[:200]}")
|
|
elif isinstance(msg, ResultMessage):
|
|
print(f" Result: {msg.result!r:.200}")
|
|
|
|
# --- Step 2: Edit ---
|
|
print("\n--- Edit ---")
|
|
opts = ClaudeAgentOptions(
|
|
cwd=str(tmpdir),
|
|
allowed_tools=["Read", "Edit"],
|
|
permission_mode="acceptEdits",
|
|
max_turns=3,
|
|
env=auth_env(),
|
|
)
|
|
async for msg in query(
|
|
prompt=f"Edit hello.txt in {tmpdir}. Add '# edited' as the first line.",
|
|
options=opts,
|
|
):
|
|
if isinstance(msg, AssistantMessage):
|
|
for block in msg.content:
|
|
if isinstance(block, ToolUseBlock):
|
|
print(f" ToolUse: {block.name}({block.input})")
|
|
elif isinstance(block, TextBlock):
|
|
print(f" Claude: {block.text[:200]}")
|
|
elif isinstance(msg, ResultMessage):
|
|
print(f" Result: {msg.result!r:.200}")
|
|
|
|
content = test_file.read_text(encoding="utf-8")
|
|
print(f"\nFile after edit:\n{content}")
|
|
ok = "# edited" in content
|
|
print("PASS" if ok else "FAIL")
|
|
return 0 if ok else 2
|
|
finally:
|
|
remove_tmpdir(tmpdir)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(asyncio.run(main()))
|