quantawatch 0.1.0__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.
- quantawatch-0.1.0/.gitignore +57 -0
- quantawatch-0.1.0/PKG-INFO +204 -0
- quantawatch-0.1.0/README.md +172 -0
- quantawatch-0.1.0/pyproject.toml +52 -0
- quantawatch-0.1.0/quantawatch/__init__.py +37 -0
- quantawatch-0.1.0/quantawatch/client.py +166 -0
- quantawatch-0.1.0/quantawatch/middleware.py +185 -0
- quantawatch-0.1.0/quantawatch/protect.py +174 -0
- quantawatch-0.1.0/quantawatch/types.py +84 -0
- quantawatch-0.1.0/tests/__init__.py +0 -0
- quantawatch-0.1.0/tests/test_client.py +280 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Rust
|
|
2
|
+
/target/
|
|
3
|
+
**/*.rs.bk
|
|
4
|
+
|
|
5
|
+
# Node
|
|
6
|
+
node_modules/
|
|
7
|
+
dist/
|
|
8
|
+
.vite/
|
|
9
|
+
|
|
10
|
+
# Python
|
|
11
|
+
__pycache__/
|
|
12
|
+
*.pyc
|
|
13
|
+
*.pyo
|
|
14
|
+
*.egg-info/
|
|
15
|
+
.eggs/
|
|
16
|
+
*.whl
|
|
17
|
+
|
|
18
|
+
# IDE
|
|
19
|
+
.idea/
|
|
20
|
+
.vscode/
|
|
21
|
+
*.swp
|
|
22
|
+
*.swo
|
|
23
|
+
|
|
24
|
+
# OS
|
|
25
|
+
.DS_Store
|
|
26
|
+
Thumbs.db
|
|
27
|
+
|
|
28
|
+
# QuantaWatch runtime
|
|
29
|
+
/audit/
|
|
30
|
+
/keys/
|
|
31
|
+
*.key
|
|
32
|
+
*.pem
|
|
33
|
+
/data/
|
|
34
|
+
scans/
|
|
35
|
+
quantawatch.yaml
|
|
36
|
+
!quantawatch.yaml.example
|
|
37
|
+
|
|
38
|
+
# Generated verification/demo artifacts
|
|
39
|
+
cbom.json
|
|
40
|
+
cbom-tampered.json
|
|
41
|
+
evidence.json
|
|
42
|
+
evidence-tampered.json
|
|
43
|
+
|
|
44
|
+
# Claude Code local settings (personal, per-machine)
|
|
45
|
+
.claude/settings.local.json
|
|
46
|
+
|
|
47
|
+
# Environment
|
|
48
|
+
.env
|
|
49
|
+
.env.*
|
|
50
|
+
|
|
51
|
+
# Terraform
|
|
52
|
+
.terraform/
|
|
53
|
+
*.tfstate
|
|
54
|
+
*.tfstate.*
|
|
55
|
+
*.tfvars
|
|
56
|
+
!*.tfvars.example
|
|
57
|
+
crash.log
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quantawatch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for QuantaWatch — AI agent security gateway
|
|
5
|
+
Project-URL: Homepage, https://github.com/dyber-pqc/QuantaWatch
|
|
6
|
+
Project-URL: Documentation, https://github.com/dyber-pqc/QuantaWatch#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/dyber-pqc/QuantaWatch.git
|
|
8
|
+
Project-URL: Issues, https://github.com/dyber-pqc/QuantaWatch/issues
|
|
9
|
+
Author: QuantaWatch Team
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
Keywords: agent,ai,audit,gateway,llm,security
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: httpx>=0.25
|
|
25
|
+
Requires-Dist: pydantic>=2
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
29
|
+
Provides-Extra: langchain
|
|
30
|
+
Requires-Dist: langchain-core>=0.1; extra == 'langchain'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# QuantaWatch Python SDK
|
|
34
|
+
|
|
35
|
+
[](../../LICENSE)
|
|
36
|
+
|
|
37
|
+
Python SDK for [QuantaWatch](https://github.com/dyber-pqc/QuantaWatch) — the post-quantum security layer for AI agents.
|
|
38
|
+
|
|
39
|
+
Route your AI SDK traffic through the QuantaWatch gateway for threat detection, policy enforcement, and tamper-evident audit logging — all signed with post-quantum cryptography (ML-DSA-65).
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
> Not yet published to PyPI. Install from source until the first release.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
git clone https://github.com/dyber-pqc/QuantaWatch.git
|
|
47
|
+
pip install -e QuantaWatch/sdk/python
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Requirements:** Python 3.10+
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
### Option 1: Protect an existing AI client (recommended)
|
|
55
|
+
|
|
56
|
+
The `protect()` wrapper transparently routes your existing SDK's HTTP traffic through the gateway. No code changes needed beyond the wrap call.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from anthropic import Anthropic
|
|
60
|
+
from quantawatch import protect
|
|
61
|
+
|
|
62
|
+
# One line — all API calls now flow through QuantaWatch
|
|
63
|
+
client = protect(Anthropic(), gateway_url="http://localhost:9090")
|
|
64
|
+
|
|
65
|
+
# Use the client exactly as before
|
|
66
|
+
response = client.messages.create(
|
|
67
|
+
model="claude-sonnet-4-20250514",
|
|
68
|
+
max_tokens=1024,
|
|
69
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
70
|
+
)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Works with any httpx-based SDK, including OpenAI:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from openai import OpenAI
|
|
77
|
+
from quantawatch import protect
|
|
78
|
+
|
|
79
|
+
client = protect(OpenAI(), gateway_url="http://localhost:9090")
|
|
80
|
+
response = client.chat.completions.create(
|
|
81
|
+
model="gpt-4o",
|
|
82
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Option 2: Use the gateway client directly
|
|
87
|
+
|
|
88
|
+
The `QuantaWatchClient` gives you direct access to the gateway proxy and admin API.
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import asyncio
|
|
92
|
+
from quantawatch import QuantaWatchClient
|
|
93
|
+
|
|
94
|
+
async def main():
|
|
95
|
+
async with QuantaWatchClient() as qw:
|
|
96
|
+
# Proxy a raw request through the gateway
|
|
97
|
+
response = await qw.proxy_request(
|
|
98
|
+
"POST",
|
|
99
|
+
"/v1/messages",
|
|
100
|
+
headers={
|
|
101
|
+
"x-api-key": "sk-ant-...",
|
|
102
|
+
"anthropic-version": "2023-06-01",
|
|
103
|
+
"Content-Type": "application/json",
|
|
104
|
+
},
|
|
105
|
+
body=b'{"model":"claude-sonnet-4-20250514","max_tokens":1024,"messages":[{"role":"user","content":"Hello!"}]}',
|
|
106
|
+
)
|
|
107
|
+
print(response.status_code, response.text)
|
|
108
|
+
|
|
109
|
+
# Query the admin API
|
|
110
|
+
sessions = await qw.get_sessions()
|
|
111
|
+
stats = await qw.get_stats()
|
|
112
|
+
audit = await qw.get_audit_entries(limit=10)
|
|
113
|
+
|
|
114
|
+
# Verify audit chain integrity
|
|
115
|
+
result = await qw.verify_audit_chain()
|
|
116
|
+
print("Audit chain valid:", result.get("valid"))
|
|
117
|
+
|
|
118
|
+
asyncio.run(main())
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## API Reference
|
|
122
|
+
|
|
123
|
+
### `protect(client, *, gateway_url="http://localhost:9090")`
|
|
124
|
+
|
|
125
|
+
Wraps an AI SDK client so its HTTP traffic flows through the QuantaWatch gateway.
|
|
126
|
+
|
|
127
|
+
| Parameter | Type | Default | Description |
|
|
128
|
+
|-----------|------|---------|-------------|
|
|
129
|
+
| `client` | `Any` | — | An AI SDK client (Anthropic, OpenAI, or any httpx-based client) |
|
|
130
|
+
| `gateway_url` | `str` | `http://localhost:9090` | URL of the QuantaWatch gateway |
|
|
131
|
+
|
|
132
|
+
Returns a proxy object that behaves identically to the original client.
|
|
133
|
+
|
|
134
|
+
### `QuantaWatchClient`
|
|
135
|
+
|
|
136
|
+
Async client for the QuantaWatch gateway and admin APIs.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
QuantaWatchClient(
|
|
140
|
+
gateway_url="http://localhost:9090",
|
|
141
|
+
admin_url="http://localhost:9091",
|
|
142
|
+
timeout=30.0,
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
| Method | Description |
|
|
147
|
+
|--------|-------------|
|
|
148
|
+
| `proxy_request(method, path, headers, body)` | Proxy an HTTP request through the gateway |
|
|
149
|
+
| `get_sessions()` | List all gateway sessions |
|
|
150
|
+
| `get_audit_entries(limit=100)` | Fetch recent audit log entries |
|
|
151
|
+
| `get_stats()` | Fetch aggregate gateway statistics |
|
|
152
|
+
| `verify_audit_chain()` | Verify cryptographic audit chain integrity |
|
|
153
|
+
| `close()` | Close the underlying HTTP client |
|
|
154
|
+
|
|
155
|
+
### Types
|
|
156
|
+
|
|
157
|
+
All response types are [Pydantic](https://docs.pydantic.dev/) models:
|
|
158
|
+
|
|
159
|
+
- `SessionInfo` — Gateway session with agent name, token count, PQC key fingerprint
|
|
160
|
+
- `AuditEntry` — Signed audit log entry with sequence number and hash chain
|
|
161
|
+
- `GatewayStats` — Aggregate stats (sessions, requests, threats, audit entries)
|
|
162
|
+
- `ThreatAssessment` — Threat analysis result with severity and blocked status
|
|
163
|
+
- `DetectedThreat` — Individual threat with category, severity, confidence, and pattern name
|
|
164
|
+
|
|
165
|
+
## Prerequisites
|
|
166
|
+
|
|
167
|
+
The SDK requires a running QuantaWatch gateway. Start one with Docker:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
git clone https://github.com/dyber-pqc/QuantaWatch.git
|
|
171
|
+
cd QuantaWatch
|
|
172
|
+
cp quantawatch.yaml.example quantawatch.yaml
|
|
173
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
174
|
+
docker compose up -d
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Or build from source:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
cargo run -p qw-gateway -- quantawatch.yaml
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# Install with dev dependencies
|
|
187
|
+
pip install -e ".[dev]"
|
|
188
|
+
|
|
189
|
+
# Run tests
|
|
190
|
+
pytest
|
|
191
|
+
|
|
192
|
+
# Type checking (if mypy installed)
|
|
193
|
+
mypy quantawatch/
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
[Apache License 2.0](../../LICENSE)
|
|
199
|
+
|
|
200
|
+
## Links
|
|
201
|
+
|
|
202
|
+
- [QuantaWatch repository](https://github.com/dyber-pqc/QuantaWatch)
|
|
203
|
+
- [OpenAPI spec](https://github.com/dyber-pqc/QuantaWatch/blob/main/docs/openapi.yaml)
|
|
204
|
+
- [Examples](https://github.com/dyber-pqc/QuantaWatch/tree/main/examples)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# QuantaWatch Python SDK
|
|
2
|
+
|
|
3
|
+
[](../../LICENSE)
|
|
4
|
+
|
|
5
|
+
Python SDK for [QuantaWatch](https://github.com/dyber-pqc/QuantaWatch) — the post-quantum security layer for AI agents.
|
|
6
|
+
|
|
7
|
+
Route your AI SDK traffic through the QuantaWatch gateway for threat detection, policy enforcement, and tamper-evident audit logging — all signed with post-quantum cryptography (ML-DSA-65).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
> Not yet published to PyPI. Install from source until the first release.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
git clone https://github.com/dyber-pqc/QuantaWatch.git
|
|
15
|
+
pip install -e QuantaWatch/sdk/python
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Requirements:** Python 3.10+
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Option 1: Protect an existing AI client (recommended)
|
|
23
|
+
|
|
24
|
+
The `protect()` wrapper transparently routes your existing SDK's HTTP traffic through the gateway. No code changes needed beyond the wrap call.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from anthropic import Anthropic
|
|
28
|
+
from quantawatch import protect
|
|
29
|
+
|
|
30
|
+
# One line — all API calls now flow through QuantaWatch
|
|
31
|
+
client = protect(Anthropic(), gateway_url="http://localhost:9090")
|
|
32
|
+
|
|
33
|
+
# Use the client exactly as before
|
|
34
|
+
response = client.messages.create(
|
|
35
|
+
model="claude-sonnet-4-20250514",
|
|
36
|
+
max_tokens=1024,
|
|
37
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Works with any httpx-based SDK, including OpenAI:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from openai import OpenAI
|
|
45
|
+
from quantawatch import protect
|
|
46
|
+
|
|
47
|
+
client = protect(OpenAI(), gateway_url="http://localhost:9090")
|
|
48
|
+
response = client.chat.completions.create(
|
|
49
|
+
model="gpt-4o",
|
|
50
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Option 2: Use the gateway client directly
|
|
55
|
+
|
|
56
|
+
The `QuantaWatchClient` gives you direct access to the gateway proxy and admin API.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import asyncio
|
|
60
|
+
from quantawatch import QuantaWatchClient
|
|
61
|
+
|
|
62
|
+
async def main():
|
|
63
|
+
async with QuantaWatchClient() as qw:
|
|
64
|
+
# Proxy a raw request through the gateway
|
|
65
|
+
response = await qw.proxy_request(
|
|
66
|
+
"POST",
|
|
67
|
+
"/v1/messages",
|
|
68
|
+
headers={
|
|
69
|
+
"x-api-key": "sk-ant-...",
|
|
70
|
+
"anthropic-version": "2023-06-01",
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
},
|
|
73
|
+
body=b'{"model":"claude-sonnet-4-20250514","max_tokens":1024,"messages":[{"role":"user","content":"Hello!"}]}',
|
|
74
|
+
)
|
|
75
|
+
print(response.status_code, response.text)
|
|
76
|
+
|
|
77
|
+
# Query the admin API
|
|
78
|
+
sessions = await qw.get_sessions()
|
|
79
|
+
stats = await qw.get_stats()
|
|
80
|
+
audit = await qw.get_audit_entries(limit=10)
|
|
81
|
+
|
|
82
|
+
# Verify audit chain integrity
|
|
83
|
+
result = await qw.verify_audit_chain()
|
|
84
|
+
print("Audit chain valid:", result.get("valid"))
|
|
85
|
+
|
|
86
|
+
asyncio.run(main())
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API Reference
|
|
90
|
+
|
|
91
|
+
### `protect(client, *, gateway_url="http://localhost:9090")`
|
|
92
|
+
|
|
93
|
+
Wraps an AI SDK client so its HTTP traffic flows through the QuantaWatch gateway.
|
|
94
|
+
|
|
95
|
+
| Parameter | Type | Default | Description |
|
|
96
|
+
|-----------|------|---------|-------------|
|
|
97
|
+
| `client` | `Any` | — | An AI SDK client (Anthropic, OpenAI, or any httpx-based client) |
|
|
98
|
+
| `gateway_url` | `str` | `http://localhost:9090` | URL of the QuantaWatch gateway |
|
|
99
|
+
|
|
100
|
+
Returns a proxy object that behaves identically to the original client.
|
|
101
|
+
|
|
102
|
+
### `QuantaWatchClient`
|
|
103
|
+
|
|
104
|
+
Async client for the QuantaWatch gateway and admin APIs.
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
QuantaWatchClient(
|
|
108
|
+
gateway_url="http://localhost:9090",
|
|
109
|
+
admin_url="http://localhost:9091",
|
|
110
|
+
timeout=30.0,
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| Method | Description |
|
|
115
|
+
|--------|-------------|
|
|
116
|
+
| `proxy_request(method, path, headers, body)` | Proxy an HTTP request through the gateway |
|
|
117
|
+
| `get_sessions()` | List all gateway sessions |
|
|
118
|
+
| `get_audit_entries(limit=100)` | Fetch recent audit log entries |
|
|
119
|
+
| `get_stats()` | Fetch aggregate gateway statistics |
|
|
120
|
+
| `verify_audit_chain()` | Verify cryptographic audit chain integrity |
|
|
121
|
+
| `close()` | Close the underlying HTTP client |
|
|
122
|
+
|
|
123
|
+
### Types
|
|
124
|
+
|
|
125
|
+
All response types are [Pydantic](https://docs.pydantic.dev/) models:
|
|
126
|
+
|
|
127
|
+
- `SessionInfo` — Gateway session with agent name, token count, PQC key fingerprint
|
|
128
|
+
- `AuditEntry` — Signed audit log entry with sequence number and hash chain
|
|
129
|
+
- `GatewayStats` — Aggregate stats (sessions, requests, threats, audit entries)
|
|
130
|
+
- `ThreatAssessment` — Threat analysis result with severity and blocked status
|
|
131
|
+
- `DetectedThreat` — Individual threat with category, severity, confidence, and pattern name
|
|
132
|
+
|
|
133
|
+
## Prerequisites
|
|
134
|
+
|
|
135
|
+
The SDK requires a running QuantaWatch gateway. Start one with Docker:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
git clone https://github.com/dyber-pqc/QuantaWatch.git
|
|
139
|
+
cd QuantaWatch
|
|
140
|
+
cp quantawatch.yaml.example quantawatch.yaml
|
|
141
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
142
|
+
docker compose up -d
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Or build from source:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
cargo run -p qw-gateway -- quantawatch.yaml
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Development
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# Install with dev dependencies
|
|
155
|
+
pip install -e ".[dev]"
|
|
156
|
+
|
|
157
|
+
# Run tests
|
|
158
|
+
pytest
|
|
159
|
+
|
|
160
|
+
# Type checking (if mypy installed)
|
|
161
|
+
mypy quantawatch/
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
[Apache License 2.0](../../LICENSE)
|
|
167
|
+
|
|
168
|
+
## Links
|
|
169
|
+
|
|
170
|
+
- [QuantaWatch repository](https://github.com/dyber-pqc/QuantaWatch)
|
|
171
|
+
- [OpenAPI spec](https://github.com/dyber-pqc/QuantaWatch/blob/main/docs/openapi.yaml)
|
|
172
|
+
- [Examples](https://github.com/dyber-pqc/QuantaWatch/tree/main/examples)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "quantawatch"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python SDK for QuantaWatch — AI agent security gateway"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "QuantaWatch Team" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["ai", "security", "gateway", "agent", "llm", "audit"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Security",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"httpx>=0.25",
|
|
31
|
+
"pydantic>=2",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
langchain = ["langchain-core>=0.1"]
|
|
36
|
+
dev = [
|
|
37
|
+
"pytest>=7",
|
|
38
|
+
"pytest-asyncio>=0.21",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/dyber-pqc/QuantaWatch"
|
|
43
|
+
Documentation = "https://github.com/dyber-pqc/QuantaWatch#readme"
|
|
44
|
+
Repository = "https://github.com/dyber-pqc/QuantaWatch.git"
|
|
45
|
+
Issues = "https://github.com/dyber-pqc/QuantaWatch/issues"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["quantawatch"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
asyncio_mode = "auto"
|
|
52
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""QuantaWatch Python SDK -- AI agent security gateway client.
|
|
2
|
+
|
|
3
|
+
Quickstart::
|
|
4
|
+
|
|
5
|
+
from quantawatch import QuantaWatchClient, protect
|
|
6
|
+
|
|
7
|
+
# Option 1: Direct gateway client
|
|
8
|
+
async with QuantaWatchClient() as qw:
|
|
9
|
+
sessions = await qw.get_sessions()
|
|
10
|
+
stats = await qw.get_stats()
|
|
11
|
+
|
|
12
|
+
# Option 2: Wrap an existing AI client
|
|
13
|
+
from anthropic import Anthropic
|
|
14
|
+
client = protect(Anthropic(), gateway_url="http://localhost:9090")
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from quantawatch.client import QuantaWatchClient
|
|
18
|
+
from quantawatch.protect import protect
|
|
19
|
+
from quantawatch.types import (
|
|
20
|
+
AuditEntry,
|
|
21
|
+
DetectedThreat,
|
|
22
|
+
GatewayStats,
|
|
23
|
+
SessionInfo,
|
|
24
|
+
ThreatAssessment,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__version__ = "0.1.0"
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"QuantaWatchClient",
|
|
31
|
+
"protect",
|
|
32
|
+
"AuditEntry",
|
|
33
|
+
"DetectedThreat",
|
|
34
|
+
"GatewayStats",
|
|
35
|
+
"SessionInfo",
|
|
36
|
+
"ThreatAssessment",
|
|
37
|
+
]
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""QuantaWatch gateway client.
|
|
2
|
+
|
|
3
|
+
Provides an async client for interacting with both the QuantaWatch gateway
|
|
4
|
+
(proxying AI API requests) and the admin API (sessions, audit, stats).
|
|
5
|
+
|
|
6
|
+
Usage::
|
|
7
|
+
|
|
8
|
+
async with QuantaWatchClient() as qw:
|
|
9
|
+
# Proxy a request through the gateway
|
|
10
|
+
resp = await qw.proxy_request("POST", "/v1/messages", headers, body)
|
|
11
|
+
|
|
12
|
+
# Query the admin API
|
|
13
|
+
sessions = await qw.get_sessions()
|
|
14
|
+
stats = await qw.get_stats()
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
import httpx
|
|
22
|
+
|
|
23
|
+
from quantawatch.types import AuditEntry, GatewayStats, SessionInfo
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class QuantaWatchClient:
|
|
27
|
+
"""Async client for the QuantaWatch security gateway.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
gateway_url:
|
|
32
|
+
Base URL of the QuantaWatch gateway proxy (default ``http://localhost:9090``).
|
|
33
|
+
admin_url:
|
|
34
|
+
Base URL of the QuantaWatch admin API (default ``http://localhost:9091``).
|
|
35
|
+
timeout:
|
|
36
|
+
Request timeout in seconds (default 30).
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(
|
|
40
|
+
self,
|
|
41
|
+
gateway_url: str = "http://localhost:9090",
|
|
42
|
+
admin_url: str = "http://localhost:9091",
|
|
43
|
+
*,
|
|
44
|
+
timeout: float = 30.0,
|
|
45
|
+
) -> None:
|
|
46
|
+
self.gateway_url = gateway_url.rstrip("/")
|
|
47
|
+
self.admin_url = admin_url.rstrip("/")
|
|
48
|
+
self._client = httpx.AsyncClient(timeout=timeout)
|
|
49
|
+
|
|
50
|
+
# -- Gateway proxy --------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
async def proxy_request(
|
|
53
|
+
self,
|
|
54
|
+
method: str,
|
|
55
|
+
path: str,
|
|
56
|
+
headers: dict[str, str] | None = None,
|
|
57
|
+
body: bytes | None = None,
|
|
58
|
+
) -> httpx.Response:
|
|
59
|
+
"""Proxy an HTTP request through the QuantaWatch gateway.
|
|
60
|
+
|
|
61
|
+
The gateway will inspect the request for threats, log it to the
|
|
62
|
+
audit trail, and forward it to the upstream AI provider.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
method:
|
|
67
|
+
HTTP method (GET, POST, etc.).
|
|
68
|
+
path:
|
|
69
|
+
Request path (e.g. ``/v1/messages``).
|
|
70
|
+
headers:
|
|
71
|
+
HTTP headers to forward. The ``Authorization`` header is typically
|
|
72
|
+
required by the upstream provider.
|
|
73
|
+
body:
|
|
74
|
+
Raw request body bytes, or *None* for bodyless requests.
|
|
75
|
+
|
|
76
|
+
Returns
|
|
77
|
+
-------
|
|
78
|
+
httpx.Response
|
|
79
|
+
The upstream response, as received through the gateway.
|
|
80
|
+
"""
|
|
81
|
+
url = f"{self.gateway_url}{path}"
|
|
82
|
+
return await self._client.request(
|
|
83
|
+
method,
|
|
84
|
+
url,
|
|
85
|
+
headers=headers or {},
|
|
86
|
+
content=body,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# -- Admin API ------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
async def get_sessions(self) -> list[SessionInfo]:
|
|
92
|
+
"""Fetch all sessions from the admin API.
|
|
93
|
+
|
|
94
|
+
Returns
|
|
95
|
+
-------
|
|
96
|
+
list[SessionInfo]
|
|
97
|
+
A list of current and historical gateway sessions.
|
|
98
|
+
"""
|
|
99
|
+
resp = await self._client.get(f"{self.admin_url}/api/sessions")
|
|
100
|
+
resp.raise_for_status()
|
|
101
|
+
return [SessionInfo.model_validate(s) for s in resp.json()]
|
|
102
|
+
|
|
103
|
+
async def get_audit_entries(self, limit: int = 100) -> list[AuditEntry]:
|
|
104
|
+
"""Fetch recent audit log entries.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
limit:
|
|
109
|
+
Maximum number of entries to return (default 100).
|
|
110
|
+
|
|
111
|
+
Returns
|
|
112
|
+
-------
|
|
113
|
+
list[AuditEntry]
|
|
114
|
+
Audit entries ordered by sequence number (most recent last).
|
|
115
|
+
"""
|
|
116
|
+
resp = await self._client.get(
|
|
117
|
+
f"{self.admin_url}/api/audit",
|
|
118
|
+
params={"limit": limit},
|
|
119
|
+
)
|
|
120
|
+
resp.raise_for_status()
|
|
121
|
+
return [AuditEntry.model_validate(e) for e in resp.json()]
|
|
122
|
+
|
|
123
|
+
async def get_stats(self) -> GatewayStats:
|
|
124
|
+
"""Fetch aggregate gateway statistics.
|
|
125
|
+
|
|
126
|
+
Returns
|
|
127
|
+
-------
|
|
128
|
+
GatewayStats
|
|
129
|
+
Current gateway statistics (sessions, requests, threats, etc.).
|
|
130
|
+
"""
|
|
131
|
+
resp = await self._client.get(f"{self.admin_url}/api/stats")
|
|
132
|
+
resp.raise_for_status()
|
|
133
|
+
return GatewayStats.model_validate(resp.json())
|
|
134
|
+
|
|
135
|
+
async def verify_audit_chain(self) -> dict[str, Any]:
|
|
136
|
+
"""Verify the integrity of the cryptographic audit chain.
|
|
137
|
+
|
|
138
|
+
The admin API checks that every audit entry's signature is valid
|
|
139
|
+
and that the chain is unbroken from the first entry.
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
dict
|
|
144
|
+
Verification result with keys like ``valid`` (bool),
|
|
145
|
+
``entries_checked`` (int), and ``errors`` (list).
|
|
146
|
+
"""
|
|
147
|
+
resp = await self._client.get(f"{self.admin_url}/api/audit/verify")
|
|
148
|
+
resp.raise_for_status()
|
|
149
|
+
return resp.json()
|
|
150
|
+
|
|
151
|
+
# -- Lifecycle ------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
async def close(self) -> None:
|
|
154
|
+
"""Close the underlying HTTP client and release resources."""
|
|
155
|
+
await self._client.aclose()
|
|
156
|
+
|
|
157
|
+
async def __aenter__(self) -> "QuantaWatchClient":
|
|
158
|
+
return self
|
|
159
|
+
|
|
160
|
+
async def __aexit__(
|
|
161
|
+
self,
|
|
162
|
+
exc_type: type[BaseException] | None,
|
|
163
|
+
exc_val: BaseException | None,
|
|
164
|
+
exc_tb: Any,
|
|
165
|
+
) -> None:
|
|
166
|
+
await self.close()
|