|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# 🐍 hawk-sdk-python Architecture |
| 4 | + |
| 5 | +**Python SDK for the Hawk Daemon API** |
| 6 | + |
| 7 | +[](https://python.org/) |
| 8 | +[]() |
| 9 | + |
| 10 | +</div> |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## 🎯 Overview |
| 15 | + |
| 16 | +Idiomatic Python client for the hawk daemon HTTP API. Provides both **sync** and **async** clients, SSE streaming, a tool decorator, agent abstraction, and workflow builder. Uses `httpx` for HTTP transport and `Pydantic` for response models. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 🧱 Modules |
| 21 | + |
| 22 | +``` |
| 23 | +src/hawk/ |
| 24 | +├── __init__.py 📤 Public exports |
| 25 | +├── client.py 🔌 HawkClient (sync) + AsyncHawkClient (async) |
| 26 | +├── types.py 📋 Pydantic models (ChatResponse, Session, Stats) |
| 27 | +├── errors.py ❌ HawkAPIError base + subclasses, parse_error() |
| 28 | +├── retry.py 🔄 RetryConfig, with_retry_sync(), with_retry() |
| 29 | +├── streaming.py 📡 StreamReader, AsyncStreamReader, StreamEvent |
| 30 | +├── agent.py 🤖 Agent (conversation history, async support) |
| 31 | +├── tools.py 🛠️ @tool() decorator, chat_with_tools() |
| 32 | +├── workflow.py 🔧 Workflow builder |
| 33 | +├── discovery.py 🔍 Auto-discover running hawk daemon on localhost |
| 34 | +├── memory_tools.py 🧠 Memory graph operations (yaad integration) |
| 35 | +├── evaluate.py 📊 Evaluation helpers |
| 36 | +└── tracing.py 📈 OpenTelemetry tracing support |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## 📤 Client Usage |
| 42 | + |
| 43 | +```python |
| 44 | +from hawk import HawkClient, AsyncHawkClient |
| 45 | + |
| 46 | +# 🔌 Sync client |
| 47 | +with HawkClient(base_url="http://localhost:4590", api_key="sk-...") as client: |
| 48 | + health = client.health() |
| 49 | + response = client.chat("list files in src/") |
| 50 | + print(response.response) |
| 51 | + |
| 52 | +# 📡 Async client |
| 53 | +async with AsyncHawkClient() as client: |
| 54 | + async for event in client.chat_stream("explain this code"): |
| 55 | + print(event.data, end="", flush=True) |
| 56 | + |
| 57 | +# 📋 Sessions |
| 58 | +sessions = client.list_sessions(limit=10) |
| 59 | +msgs = client.get_session_messages(session_id) |
| 60 | +client.delete_session(session_id) |
| 61 | +``` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## 🛠️ Tool Decorator |
| 66 | + |
| 67 | +```python |
| 68 | +from hawk import tool, HawkClient |
| 69 | + |
| 70 | +@tool() |
| 71 | +def read_file(path: str) -> str: |
| 72 | + with open(path) as f: |
| 73 | + return f.read() |
| 74 | + |
| 75 | +with HawkClient() as client: |
| 76 | + response = client.chat_with_tools("read config.json", tools=[read_file]) |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 🤖 Agent (Higher-Level) |
| 82 | + |
| 83 | +```python |
| 84 | +from hawk import Agent |
| 85 | + |
| 86 | +agent = Agent(base_url="http://localhost:4590") |
| 87 | +resp1 = agent.chat("refactor this function") |
| 88 | +resp2 = agent.chat("now add type hints") # continues same session |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## ❌ Error Handling |
| 94 | + |
| 95 | +```python |
| 96 | +from hawk.errors import NotFoundError, RateLimitError |
| 97 | + |
| 98 | +try: |
| 99 | + response = client.chat("...") |
| 100 | +except RateLimitError as e: |
| 101 | + time.sleep(e.retry_after or 1) |
| 102 | +except NotFoundError: |
| 103 | + ... |
| 104 | +``` |
| 105 | + |
| 106 | +| Error Class | HTTP Status | |
| 107 | +|-------------|:-----------:| |
| 108 | +| `NotFoundError` | 404 | |
| 109 | +| `RateLimitError` | 429 | |
| 110 | +| `InternalServerError` | 500 | |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 🔄 Retry & Streaming |
| 115 | + |
| 116 | +| Feature | Behavior | |
| 117 | +|---------|----------| |
| 118 | +| **Auto-retry** | 429, 500, 502, 503, 504 with exponential backoff + jitter | |
| 119 | +| **Retry-After: 0** | Valid — don't retry immediately | |
| 120 | +| **Dual client** | Every method on both `HawkClient` and `AsyncHawkClient` | |
0 commit comments