api-operator 0.10.0__tar.gz → 0.11.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.
Files changed (69) hide show
  1. api_operator-0.11.0/.env.example +13 -0
  2. {api_operator-0.10.0 → api_operator-0.11.0}/CHANGELOG.md +22 -0
  3. {api_operator-0.10.0 → api_operator-0.11.0}/PKG-INFO +66 -52
  4. api_operator-0.11.0/README.md +183 -0
  5. api_operator-0.11.0/api_operator/__init__.py +37 -0
  6. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/base.py +4 -0
  7. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/openapi_generator.py +81 -4
  8. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/yaml_adapter.py +34 -0
  9. api_operator-0.11.0/api_operator/core/agent.py +482 -0
  10. api_operator-0.11.0/api_operator/core/audit.py +87 -0
  11. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/config.py +5 -0
  12. api_operator-0.11.0/api_operator/core/policy.py +232 -0
  13. api_operator-0.11.0/api_operator/core/quota.py +78 -0
  14. api_operator-0.11.0/api_operator/core/redaction.py +46 -0
  15. api_operator-0.11.0/api_operator/core/schemas.py +142 -0
  16. api_operator-0.11.0/api_operator/factory.py +59 -0
  17. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/server/app.py +18 -2
  18. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/server/cli.py +37 -5
  19. api_operator-0.11.0/docs/ROADMAP-vNEXT.md +481 -0
  20. api_operator-0.11.0/docs/audit.md +26 -0
  21. api_operator-0.11.0/docs/dry-run.md +30 -0
  22. api_operator-0.11.0/docs/policy.md +42 -0
  23. api_operator-0.11.0/docs/public-api-inventory.md +56 -0
  24. api_operator-0.11.0/docs/quotas.md +25 -0
  25. api_operator-0.11.0/examples/jsonplaceholder-adapter/README.md +16 -0
  26. api_operator-0.11.0/examples/jsonplaceholder-adapter/adapter.yaml +57 -0
  27. api_operator-0.11.0/examples/jsonplaceholder-adapter/policy.yaml +21 -0
  28. api_operator-0.11.0/examples/tenant-kit-adapter/policy.yaml +24 -0
  29. {api_operator-0.10.0 → api_operator-0.11.0}/pyproject.toml +2 -2
  30. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_agent.py +1 -1
  31. api_operator-0.11.0/tests/test_audit.py +89 -0
  32. api_operator-0.11.0/tests/test_dry_run.py +112 -0
  33. api_operator-0.11.0/tests/test_openapi_policy.py +149 -0
  34. api_operator-0.11.0/tests/test_policy.py +209 -0
  35. api_operator-0.11.0/tests/test_quota.py +55 -0
  36. api_operator-0.10.0/.env.example +0 -6
  37. api_operator-0.10.0/README.md +0 -169
  38. api_operator-0.10.0/api_operator/__init__.py +0 -8
  39. api_operator-0.10.0/api_operator/core/agent.py +0 -161
  40. api_operator-0.10.0/api_operator/factory.py +0 -26
  41. {api_operator-0.10.0 → api_operator-0.11.0}/.github/workflows/tests.yml +0 -0
  42. {api_operator-0.10.0 → api_operator-0.11.0}/.gitignore +0 -0
  43. {api_operator-0.10.0 → api_operator-0.11.0}/Dockerfile +0 -0
  44. {api_operator-0.10.0 → api_operator-0.11.0}/LICENSE +0 -0
  45. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/__init__.py +0 -0
  46. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/mock.py +0 -0
  47. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/registry.py +0 -0
  48. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/adapters/yaml_spec.py +0 -0
  49. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/executor.py +0 -0
  50. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/formatters.py +0 -0
  51. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/guardrails.py +0 -0
  52. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/memory.py +0 -0
  53. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/planner.py +0 -0
  54. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/core/planner_openai.py +0 -0
  55. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/rag/indexer.py +0 -0
  56. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/tools/base.py +0 -0
  57. {api_operator-0.10.0 → api_operator-0.11.0}/api_operator/tools/schema.py +0 -0
  58. {api_operator-0.10.0 → api_operator-0.11.0}/examples/tenant-kit-adapter/README.md +0 -0
  59. {api_operator-0.10.0 → api_operator-0.11.0}/examples/tenant-kit-adapter/adapter.yaml +0 -0
  60. {api_operator-0.10.0 → api_operator-0.11.0}/scripts/integration_tenant_kit.py +0 -0
  61. {api_operator-0.10.0 → api_operator-0.11.0}/tests/__init__.py +0 -0
  62. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_api.py +0 -0
  63. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_cli.py +0 -0
  64. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_core.py +0 -0
  65. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_formatters.py +0 -0
  66. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_integration_tenant_kit.py +0 -0
  67. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_mock_adapter.py +0 -0
  68. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_scenarios_e2e.py +0 -0
  69. {api_operator-0.10.0 → api_operator-0.11.0}/tests/test_yaml_adapter.py +0 -0
@@ -0,0 +1,13 @@
1
+ api_operator_HOST=127.0.0.1
2
+ api_operator_PORT=8100
3
+ api_operator_DEFAULT_ADAPTER=mock
4
+ api_operator_PLANNER=mock
5
+ # api_operator_OPENAI_API_KEY=
6
+ # api_operator_OPENAI_MODEL=gpt-4o-mini
7
+ api_operator_REQUIRE_CONFIRM_DANGEROUS=true
8
+ api_operator_AUDIT_ENABLED=true
9
+ # api_operator_AUDIT_PATH=./audit.jsonl
10
+ # api_operator_POLICY_PATH=./policy.yaml
11
+ # api_operator_QUOTA_BUDGET=100
12
+ api_operator_DRY_RUN=false
13
+ # api_operator_CORS_ORIGINS=http://localhost:3000
@@ -2,6 +2,28 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## [0.11.0] - 2026-11-23
6
+
7
+ Foundation for **Safe API Agent** (see `docs/ROADMAP-vNEXT.md`). Opt-in; existing adapters keep working.
8
+
9
+ ### Added
10
+
11
+ - **Policy engine** (`policy.yaml`): allow / deny / confirm, deny lists, confirm lists, session `max_calls`, role/tenant rules
12
+ - **Dry-run**: CLI `--dry-run`, HTTP `dry_run=true`, `DryRunReport` + YAML `would_call` preview
13
+ - **Audit**: `AuditSink` protocol, JSONL / memory sinks, sensitive-field redaction
14
+ - **Quota hooks**: `QuotaGateway` + in-memory budget gateway (host owns real billing)
15
+ - Shared schemas: `PolicyDecision`, `QuotaReservation`, `AuditEvent`, `DryRunReport`
16
+ - Docs: `docs/policy.md`, `docs/dry-run.md`, `docs/audit.md`, `docs/quotas.md`, `docs/public-api-inventory.md`
17
+ - Example `examples/tenant-kit-adapter/policy.yaml`
18
+ - Example `examples/jsonplaceholder-adapter/` (public REST, non–tenant-kit)
19
+ - OpenAPI generator: tag-based `requires_ability`, write methods marked dangerous, draft `policy.yaml`
20
+ - `scaffold-adapter` also writes `policy.yaml`
21
+
22
+ ### Changed
23
+
24
+ - Agent `status` may be `deny` (additive) for policy/quota/ability rejection
25
+ - README reframed as **Safe API Agent**; version `0.11.0`
26
+
5
27
  ## [0.10.0] - 2026-06-15
6
28
 
7
29
  ### Added
@@ -1,7 +1,7 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: api-operator
3
- Version: 0.10.0
4
- Summary: Standalone AI API Operator with pluggable adapters for multi-project APIs
3
+ Version: 0.11.0
4
+ Summary: Safe API Agent — run APIs with policy, quotas, and audit (pluggable adapters)
5
5
  Project-URL: Homepage, https://github.com/mohammedelkarsh/api-operator
6
6
  Project-URL: Repository, https://github.com/mohammedelkarsh/api-operator
7
7
  Project-URL: Issues, https://github.com/mohammedelkarsh/api-operator/issues
@@ -35,28 +35,33 @@ Provides-Extra: llm
35
35
  Requires-Dist: openai>=1.35.0; extra == 'llm'
36
36
  Description-Content-Type: text/markdown
37
37
 
38
- # API Operator
38
+ # API Operator — Safe API Agent
39
39
 
40
40
  [![Tests](https://github.com/mohammedelkarsh/api-operator/actions/workflows/tests.yml/badge.svg)](https://github.com/mohammedelkarsh/api-operator/actions/workflows/tests.yml)
41
+ [![PyPI](https://img.shields.io/pypi/v/api-operator.svg)](https://pypi.org/project/api-operator/)
41
42
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
42
43
  [![Python](https://img.shields.io/badge/python-3.11%2B-blue.svg)](https://www.python.org/downloads/)
43
44
 
44
- Standalone **AI operator runtime** with pluggable adapters. Talk to your HTTP APIs instead of clicking through admin panels.
45
+ Turn any OpenAPI / `adapter.yaml` into an AI agent that can **reason and act on your APIs** with **policy, dry-run, audit, and quota hooks**.
45
46
 
46
- Works with Laravel, Go, connectivity platforms, or any backend with a REST API.
47
+ > Retrieve (optional later) → Reason → Act — with permission, cost, and audit boundaries.
48
+
49
+ Works with Laravel, Go, or any REST API. First-class example: [laravel-tenant-kit](https://github.com/mohammedelkarsh/laravel-tenant-kit).
47
50
 
48
51
  ## Install
49
52
 
50
53
  ```bash
51
- git clone https://github.com/mohammedelkarsh/api-operator.git
52
- cd api-operator
53
- pip install -e ".[dev]"
54
+ pip install api-operator
55
+ # optional OpenAI planner
56
+ pip install "api-operator[llm]"
54
57
  ```
55
58
 
56
- Optional OpenAI planner:
59
+ Development:
57
60
 
58
61
  ```bash
59
- pip install -e ".[dev,llm]"
62
+ git clone https://github.com/mohammedelkarsh/api-operator.git
63
+ cd api-operator
64
+ pip install -e ".[dev]"
60
65
  ```
61
66
 
62
67
  ## Quick start
@@ -64,27 +69,52 @@ pip install -e ".[dev,llm]"
64
69
  ```bash
65
70
  api-operator demo
66
71
  api-operator tools --adapter mock
72
+ api-operator chat --adapter mock --dry-run
67
73
  ```
68
74
 
69
- ## What ships in the core package
75
+ Public API demo (no Laravel):
76
+
77
+ ```bash
78
+ api-operator chat \
79
+ --adapter yaml \
80
+ --config examples/jsonplaceholder-adapter/adapter.yaml \
81
+ --dry-run
82
+ ```
83
+
84
+ ## What ships in the core
70
85
 
71
86
  | In core | In your projects |
72
87
  |---------|------------------|
73
- | Agent runtime (plan, guard, execute) | `adapter.yaml` per API |
74
- | `mock` adapter (demo + tests) | OpenAPI specs |
75
- | `yaml` adapter | API tokens in `.env` |
76
- | CLI + HTTP server | Optional custom adapters |
88
+ | Agent runtime (plan → policy → quota → execute) | `adapter.yaml` + optional `policy.yaml` |
89
+ | Dry-run (`--dry-run` / `dry_run=true`) | OpenAPI specs |
90
+ | AuditSink + redaction | API tokens in `.env` |
91
+ | QuotaGateway hooks | Host billing (e.g. tenant-kit meters) |
92
+ | `mock` + `yaml` adapters | Optional custom adapters |
93
+
94
+ No hard dependency on Laravel, Postgres, or vector DBs.
95
+
96
+ ## Safe by default
97
+
98
+ 1. **Policy** — optional `policy.yaml` (allow / deny / confirm, session caps, roles)
99
+ 2. **Dry-run** — see the planned tool + upstream request without side effects
100
+ 3. **Audit** — JSONL / injectable sink; secrets redacted
101
+ 4. **Quota** — gate before execute; real billing stays in the host app
102
+
103
+ Docs: [policy](docs/policy.md) · [dry-run](docs/dry-run.md) · [audit](docs/audit.md) · [quotas](docs/quotas.md) · [roadmap](docs/ROADMAP-vNEXT.md)
77
104
 
78
105
  ## Build an adapter without Python
79
106
 
80
- ### Scaffold template
107
+ ### Scaffold
81
108
 
82
109
  ```bash
83
110
  api-operator scaffold-adapter my-api --output examples
111
+ # writes adapter.yaml + policy.yaml + README
84
112
  ```
85
113
 
86
114
  ### Generate from OpenAPI
87
115
 
116
+ Write methods (`POST`/`PATCH`/`PUT`/`DELETE`) are marked `dangerous`. Tag-based abilities are suggested. A draft `policy.yaml` is written next to the adapter.
117
+
88
118
  ```bash
89
119
  api-operator generate-from-openapi openapi.yaml \
90
120
  --output adapter.yaml \
@@ -92,9 +122,12 @@ api-operator generate-from-openapi openapi.yaml \
92
122
  --path-prefix /api
93
123
  ```
94
124
 
95
- ### Laravel Tenant Kit example
125
+ ### Examples
96
126
 
97
- See [`examples/tenant-kit-adapter/`](examples/tenant-kit-adapter/) — pairs with [laravel-tenant-kit](https://github.com/mohammedelkarsh/laravel-tenant-kit).
127
+ | Example | Purpose |
128
+ |---------|---------|
129
+ | [`examples/tenant-kit-adapter/`](examples/tenant-kit-adapter/) | Laravel Tenant Kit |
130
+ | [`examples/jsonplaceholder-adapter/`](examples/jsonplaceholder-adapter/) | Public REST demo |
98
131
 
99
132
  ```bash
100
133
  export TENANT_KIT_API_TOKEN="your-sanctum-token"
@@ -120,12 +153,14 @@ tools:
120
153
  description: List items
121
154
  method: GET
122
155
  path: /api/items
156
+ requires_ability: items:read
123
157
 
124
158
  - name: create_item
125
159
  description: Create item
126
160
  method: POST
127
161
  path: /api/items
128
162
  dangerous: true
163
+ requires_ability: items:write
129
164
  parameters:
130
165
  title:
131
166
  type: string
@@ -134,18 +169,7 @@ tools:
134
169
  title: "{title}"
135
170
  ```
136
171
 
137
- Tenant subdomain APIs:
138
-
139
- ```yaml
140
- - name: invite_member
141
- method: POST
142
- path: /api/team/invitations
143
- host: tenant
144
- tenant_param: subdomain
145
- parameters:
146
- subdomain: { type: string, required: true }
147
- email: { type: string, required: true }
148
- ```
172
+ Optional sibling `policy.yaml` loads automatically when you pass that adapter path.
149
173
 
150
174
  ## HTTP server
151
175
 
@@ -160,45 +184,35 @@ POST /v1/chat
160
184
  "config_path": "examples/tenant-kit-adapter/adapter.yaml",
161
185
  "adapter_config": { "token": "YOUR_TOKEN" },
162
186
  "message": "list workspaces",
163
- "abilities": ["workspaces:read"]
187
+ "abilities": ["workspaces:read"],
188
+ "dry_run": false
164
189
  }
165
190
  ```
166
191
 
167
- ## Integration test (Tenant Kit)
168
-
169
- With [Tenant Kit](https://github.com/mohammedelkarsh/laravel-tenant-kit) running:
192
+ ## Tests
170
193
 
171
194
  ```bash
172
- python scripts/integration_tenant_kit.py
173
- # Docker on port 8080:
174
- python scripts/integration_tenant_kit.py --base-url http://laravel-tenant-kit.test:8080
195
+ pytest -q
175
196
  ```
176
197
 
177
- Optional pytest marker (requires env vars):
198
+ Live Tenant Kit (optional):
178
199
 
179
200
  ```bash
180
- export TENANT_KIT_BASE_URL=http://laravel-tenant-kit.test
181
- export TENANT_KIT_API_TOKEN=your-token
201
+ python scripts/integration_tenant_kit.py
182
202
  pytest -m integration -q
183
203
  ```
184
204
 
185
- ## Tests
186
-
187
- ```bash
188
- pytest -q
189
- ```
190
-
191
205
  ## Configuration
192
206
 
193
- Copy `.env.example` to `.env` for local defaults (`api_operator_PLANNER`, port, etc.).
207
+ Copy `.env.example` → `.env`. Notable vars: `API_OPERATOR_PLANNER`, `API_OPERATOR_POLICY_PATH`, `API_OPERATOR_AUDIT_PATH`, `API_OPERATOR_QUOTA_BUDGET`, `API_OPERATOR_DRY_RUN`.
194
208
 
195
209
  ## Architecture
196
210
 
197
211
  ```
198
- api-operator (core) your projects
199
- ├── agent runtime ├── adapter.yaml
200
- ├── mock + yaml adapters ├── openapi.yaml
201
- └── scaffold / generate CLI └── HTTP APIs (Laravel, Go, …)
212
+ api-operator (Safe API Agent) your hosts
213
+ ├── plan → policy → quota → act ├── adapter.yaml + policy.yaml
214
+ ├── dry-run + audit hooks ├── OpenAPI
215
+ └── mock / yaml adapters └── Laravel / any REST API
202
216
  ```
203
217
 
204
218
  ## License
@@ -0,0 +1,183 @@
1
+ # API Operator — Safe API Agent
2
+
3
+ [![Tests](https://github.com/mohammedelkarsh/api-operator/actions/workflows/tests.yml/badge.svg)](https://github.com/mohammedelkarsh/api-operator/actions/workflows/tests.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/api-operator.svg)](https://pypi.org/project/api-operator/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
+ [![Python](https://img.shields.io/badge/python-3.11%2B-blue.svg)](https://www.python.org/downloads/)
7
+
8
+ Turn any OpenAPI / `adapter.yaml` into an AI agent that can **reason and act on your APIs** with **policy, dry-run, audit, and quota hooks**.
9
+
10
+ > Retrieve (optional later) → Reason → Act — with permission, cost, and audit boundaries.
11
+
12
+ Works with Laravel, Go, or any REST API. First-class example: [laravel-tenant-kit](https://github.com/mohammedelkarsh/laravel-tenant-kit).
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ pip install api-operator
18
+ # optional OpenAI planner
19
+ pip install "api-operator[llm]"
20
+ ```
21
+
22
+ Development:
23
+
24
+ ```bash
25
+ git clone https://github.com/mohammedelkarsh/api-operator.git
26
+ cd api-operator
27
+ pip install -e ".[dev]"
28
+ ```
29
+
30
+ ## Quick start
31
+
32
+ ```bash
33
+ api-operator demo
34
+ api-operator tools --adapter mock
35
+ api-operator chat --adapter mock --dry-run
36
+ ```
37
+
38
+ Public API demo (no Laravel):
39
+
40
+ ```bash
41
+ api-operator chat \
42
+ --adapter yaml \
43
+ --config examples/jsonplaceholder-adapter/adapter.yaml \
44
+ --dry-run
45
+ ```
46
+
47
+ ## What ships in the core
48
+
49
+ | In core | In your projects |
50
+ |---------|------------------|
51
+ | Agent runtime (plan → policy → quota → execute) | `adapter.yaml` + optional `policy.yaml` |
52
+ | Dry-run (`--dry-run` / `dry_run=true`) | OpenAPI specs |
53
+ | AuditSink + redaction | API tokens in `.env` |
54
+ | QuotaGateway hooks | Host billing (e.g. tenant-kit meters) |
55
+ | `mock` + `yaml` adapters | Optional custom adapters |
56
+
57
+ No hard dependency on Laravel, Postgres, or vector DBs.
58
+
59
+ ## Safe by default
60
+
61
+ 1. **Policy** — optional `policy.yaml` (allow / deny / confirm, session caps, roles)
62
+ 2. **Dry-run** — see the planned tool + upstream request without side effects
63
+ 3. **Audit** — JSONL / injectable sink; secrets redacted
64
+ 4. **Quota** — gate before execute; real billing stays in the host app
65
+
66
+ Docs: [policy](docs/policy.md) · [dry-run](docs/dry-run.md) · [audit](docs/audit.md) · [quotas](docs/quotas.md) · [roadmap](docs/ROADMAP-vNEXT.md)
67
+
68
+ ## Build an adapter without Python
69
+
70
+ ### Scaffold
71
+
72
+ ```bash
73
+ api-operator scaffold-adapter my-api --output examples
74
+ # writes adapter.yaml + policy.yaml + README
75
+ ```
76
+
77
+ ### Generate from OpenAPI
78
+
79
+ Write methods (`POST`/`PATCH`/`PUT`/`DELETE`) are marked `dangerous`. Tag-based abilities are suggested. A draft `policy.yaml` is written next to the adapter.
80
+
81
+ ```bash
82
+ api-operator generate-from-openapi openapi.yaml \
83
+ --output adapter.yaml \
84
+ --base-url http://api.example.test \
85
+ --path-prefix /api
86
+ ```
87
+
88
+ ### Examples
89
+
90
+ | Example | Purpose |
91
+ |---------|---------|
92
+ | [`examples/tenant-kit-adapter/`](examples/tenant-kit-adapter/) | Laravel Tenant Kit |
93
+ | [`examples/jsonplaceholder-adapter/`](examples/jsonplaceholder-adapter/) | Public REST demo |
94
+
95
+ ```bash
96
+ export TENANT_KIT_API_TOKEN="your-sanctum-token"
97
+ api-operator chat \
98
+ --adapter yaml \
99
+ --config examples/tenant-kit-adapter/adapter.yaml \
100
+ --base-url http://laravel-tenant-kit.test
101
+ ```
102
+
103
+ ## adapter.yaml (minimal)
104
+
105
+ ```yaml
106
+ name: my_project
107
+ description: My API adapter
108
+ base_url: http://api.example.test
109
+
110
+ auth:
111
+ type: bearer
112
+ env_token: MY_PROJECT_API_TOKEN
113
+
114
+ tools:
115
+ - name: list_items
116
+ description: List items
117
+ method: GET
118
+ path: /api/items
119
+ requires_ability: items:read
120
+
121
+ - name: create_item
122
+ description: Create item
123
+ method: POST
124
+ path: /api/items
125
+ dangerous: true
126
+ requires_ability: items:write
127
+ parameters:
128
+ title:
129
+ type: string
130
+ required: true
131
+ body:
132
+ title: "{title}"
133
+ ```
134
+
135
+ Optional sibling `policy.yaml` loads automatically when you pass that adapter path.
136
+
137
+ ## HTTP server
138
+
139
+ ```bash
140
+ api-operator serve --port 8100
141
+ ```
142
+
143
+ ```json
144
+ POST /v1/chat
145
+ {
146
+ "adapter": "yaml",
147
+ "config_path": "examples/tenant-kit-adapter/adapter.yaml",
148
+ "adapter_config": { "token": "YOUR_TOKEN" },
149
+ "message": "list workspaces",
150
+ "abilities": ["workspaces:read"],
151
+ "dry_run": false
152
+ }
153
+ ```
154
+
155
+ ## Tests
156
+
157
+ ```bash
158
+ pytest -q
159
+ ```
160
+
161
+ Live Tenant Kit (optional):
162
+
163
+ ```bash
164
+ python scripts/integration_tenant_kit.py
165
+ pytest -m integration -q
166
+ ```
167
+
168
+ ## Configuration
169
+
170
+ Copy `.env.example` → `.env`. Notable vars: `API_OPERATOR_PLANNER`, `API_OPERATOR_POLICY_PATH`, `API_OPERATOR_AUDIT_PATH`, `API_OPERATOR_QUOTA_BUDGET`, `API_OPERATOR_DRY_RUN`.
171
+
172
+ ## Architecture
173
+
174
+ ```
175
+ api-operator (Safe API Agent) your hosts
176
+ ├── plan → policy → quota → act ├── adapter.yaml + policy.yaml
177
+ ├── dry-run + audit hooks ├── OpenAPI
178
+ └── mock / yaml adapters └── Laravel / any REST API
179
+ ```
180
+
181
+ ## License
182
+
183
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,37 @@
1
+ """API Operator — Safe API Agent with pluggable adapters."""
2
+
3
+ from api_operator.core.agent import Agent, AgentResponse
4
+ from api_operator.core.config import Settings
5
+ from api_operator.core.audit import AuditSink, JsonlAuditSink, MemoryAuditSink
6
+ from api_operator.core.policy import PolicyContext, PolicyEngine
7
+ from api_operator.core.quota import InMemoryQuotaGateway, QuotaGateway, UnlimitedQuotaGateway
8
+ from api_operator.core.schemas import (
9
+ AuditEvent,
10
+ Decision,
11
+ DryRunReport,
12
+ PolicyDecision,
13
+ QuotaReservation,
14
+ )
15
+ from api_operator.tools.base import Tool, ToolRegistry
16
+
17
+ __all__ = [
18
+ "Agent",
19
+ "AgentResponse",
20
+ "AuditEvent",
21
+ "AuditSink",
22
+ "Decision",
23
+ "DryRunReport",
24
+ "InMemoryQuotaGateway",
25
+ "JsonlAuditSink",
26
+ "MemoryAuditSink",
27
+ "PolicyContext",
28
+ "PolicyDecision",
29
+ "PolicyEngine",
30
+ "QuotaGateway",
31
+ "QuotaReservation",
32
+ "Settings",
33
+ "Tool",
34
+ "ToolRegistry",
35
+ "UnlimitedQuotaGateway",
36
+ ]
37
+ __version__ = "0.11.0"
@@ -23,3 +23,7 @@ class Adapter(ABC):
23
23
 
24
24
  def docs_paths(self) -> list[str]:
25
25
  return []
26
+
27
+ def preview_tool_call(self, tool_name: str, arguments: dict[str, Any]) -> dict[str, Any] | None:
28
+ """Optional dry-run preview of the upstream request. Default: unsupported."""
29
+ return None
@@ -1,7 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
- import re
4
3
  import json
4
+ import re
5
5
  from pathlib import Path
6
6
  from typing import Any
7
7
 
@@ -10,6 +10,7 @@ import yaml
10
10
  from api_operator.adapters.yaml_spec import AdapterSpec, ParameterSpec, ToolSpec, save_adapter_spec
11
11
 
12
12
  _METHODS = {"get", "post", "put", "patch", "delete"}
13
+ _WRITE_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})
13
14
 
14
15
 
15
16
  def generate_adapter_from_openapi(
@@ -22,6 +23,7 @@ def generate_adapter_from_openapi(
22
23
  include_methods: set[str] | None = None,
23
24
  tag_filter: set[str] | None = None,
24
25
  dangerous_paths: set[str] | None = None,
26
+ suggest_abilities: bool = True,
25
27
  ) -> AdapterSpec:
26
28
  path = Path(spec_path)
27
29
  text = path.read_text(encoding="utf-8")
@@ -56,7 +58,13 @@ def generate_adapter_from_openapi(
56
58
  if tag_filter and not (set(tags) & tag_filter):
57
59
  continue
58
60
 
59
- tool = _operation_to_tool(route, method.upper(), operation, dangerous_paths)
61
+ tool = _operation_to_tool(
62
+ route,
63
+ method.upper(),
64
+ operation,
65
+ dangerous_paths,
66
+ suggest_abilities=suggest_abilities,
67
+ )
60
68
  if tool:
61
69
  tools.append(tool)
62
70
 
@@ -77,11 +85,45 @@ def generate_adapter_from_openapi(
77
85
  )
78
86
 
79
87
 
88
+ def generate_policy_draft(adapter: AdapterSpec) -> dict[str, Any]:
89
+ """Build a conservative policy.yaml draft from an AdapterSpec."""
90
+ confirm = [t.name for t in adapter.tools if t.dangerous]
91
+ deny: list[str] = []
92
+ max_calls: dict[str, int] = {}
93
+ for tool in adapter.tools:
94
+ if tool.method == "GET" and not tool.dangerous:
95
+ max_calls[tool.name] = 50
96
+
97
+ return {
98
+ "version": 1,
99
+ "name": adapter.name,
100
+ "description": f"Auto-generated draft policy for {adapter.name}",
101
+ "default": "allow",
102
+ "confirm_tools": confirm,
103
+ "deny_tools": deny,
104
+ "max_calls": max_calls,
105
+ "rules": [],
106
+ }
107
+
108
+
109
+ def save_policy_draft(adapter: AdapterSpec, path: str | Path) -> Path:
110
+ file_path = Path(path)
111
+ file_path.parent.mkdir(parents=True, exist_ok=True)
112
+ payload = generate_policy_draft(adapter)
113
+ file_path.write_text(
114
+ yaml.safe_dump(payload, sort_keys=False, allow_unicode=True),
115
+ encoding="utf-8",
116
+ )
117
+ return file_path
118
+
119
+
80
120
  def _operation_to_tool(
81
121
  route: str,
82
122
  method: str,
83
123
  operation: dict[str, Any],
84
124
  dangerous_paths: set[str],
125
+ *,
126
+ suggest_abilities: bool = True,
85
127
  ) -> ToolSpec | None:
86
128
  operation_id = operation.get("operationId")
87
129
  summary = operation.get("summary") or operation.get("description") or f"{method} {route}"
@@ -124,7 +166,9 @@ def _operation_to_tool(
124
166
  )
125
167
  body_template[str(pname)] = f"{{{pname}}}"
126
168
 
127
- dangerous = method in {"POST", "PUT", "PATCH", "DELETE"} or route in dangerous_paths
169
+ # Write methods are dangerous by default for Safe API Agent
170
+ dangerous = method in _WRITE_METHODS or route in dangerous_paths
171
+ ability = _ability_from_tags(operation.get("tags") or [], method) if suggest_abilities else None
128
172
 
129
173
  return ToolSpec(
130
174
  name=name,
@@ -134,9 +178,23 @@ def _operation_to_tool(
134
178
  parameters=parameters,
135
179
  body=body_template,
136
180
  dangerous=dangerous,
181
+ requires_ability=ability,
137
182
  )
138
183
 
139
184
 
185
+ def _ability_from_tags(tags: list[Any], method: str) -> str | None:
186
+ if not tags:
187
+ return None
188
+ raw = str(tags[0]).strip()
189
+ if not raw:
190
+ return None
191
+ slug = _slugify(raw)
192
+ if not slug:
193
+ return None
194
+ action = "write" if method in _WRITE_METHODS else "read"
195
+ return f"{slug}:{action}"
196
+
197
+
140
198
  def _tool_name(operation_id: str | None, method: str, route: str) -> str:
141
199
  if operation_id:
142
200
  return _slugify(operation_id)
@@ -187,6 +245,17 @@ tools:
187
245
  title: "{{title}}"
188
246
  """
189
247
 
248
+ SCAFFOLD_POLICY = """version: 1
249
+ name: {name}
250
+ description: Draft policy for {name} scaffold
251
+ default: allow
252
+ confirm_tools:
253
+ - create_item
254
+ max_calls:
255
+ list_items: 50
256
+ rules: []
257
+ """
258
+
190
259
 
191
260
  def scaffold_adapter(
192
261
  name: str,
@@ -194,6 +263,7 @@ def scaffold_adapter(
194
263
  *,
195
264
  base_url: str = "http://localhost:8000",
196
265
  description: str | None = None,
266
+ write_policy: bool = True,
197
267
  ) -> Path:
198
268
  directory = Path(output_dir)
199
269
  directory.mkdir(parents=True, exist_ok=True)
@@ -209,14 +279,21 @@ def scaffold_adapter(
209
279
  encoding="utf-8",
210
280
  )
211
281
 
282
+ if write_policy:
283
+ (directory / "policy.yaml").write_text(
284
+ SCAFFOLD_POLICY.format(name=name),
285
+ encoding="utf-8",
286
+ )
287
+
212
288
  readme = directory / "README.md"
213
289
  readme.write_text(
214
290
  f"""# {name} adapter (YAML)
215
291
 
216
- No Python required — edit `adapter.yaml` and run:
292
+ No Python required — edit `adapter.yaml` (and optional `policy.yaml`) then run:
217
293
 
218
294
  ```powershell
219
295
  python -m api_operator.server.cli chat --adapter yaml --config adapter.yaml
296
+ python -m api_operator.server.cli chat --adapter yaml --config adapter.yaml --dry-run
220
297
  ```
221
298
 
222
299
  Set token: