agent-grammar 0.1.2__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.
- agent_grammar-0.1.2/.github/workflows/publish.yml +76 -0
- agent_grammar-0.1.2/.gitignore +17 -0
- agent_grammar-0.1.2/LICENSE +21 -0
- agent_grammar-0.1.2/PKG-INFO +136 -0
- agent_grammar-0.1.2/README.md +104 -0
- agent_grammar-0.1.2/pyproject.toml +57 -0
- agent_grammar-0.1.2/src/agent_grammar/__init__.py +7 -0
- agent_grammar-0.1.2/src/agent_grammar/_context.py +65 -0
- agent_grammar-0.1.2/src/agent_grammar/_models.py +50 -0
- agent_grammar-0.1.2/src/agent_grammar/cli/__init__.py +1 -0
- agent_grammar-0.1.2/src/agent_grammar/cli/export.py +92 -0
- agent_grammar-0.1.2/src/agent_grammar/cli/main.py +21 -0
- agent_grammar-0.1.2/src/agent_grammar/serve/__init__.py +1 -0
- agent_grammar-0.1.2/src/agent_grammar/serve/fastapi.py +100 -0
- agent_grammar-0.1.2/src/agent_grammar/serve/flask.py +10 -0
- agent_grammar-0.1.2/src/agent_grammar/templates/claude.md.j2 +22 -0
- agent_grammar-0.1.2/src/agent_grammar/templates/copilot.md.j2 +20 -0
- agent_grammar-0.1.2/src/agent_grammar/templates/cursor.md.j2 +17 -0
- agent_grammar-0.1.2/src/agent_grammar/templates/gemini.md.j2 +20 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/__init__.py +6 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/client.py +46 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/decorators.py +89 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/plugin.py +98 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/registry.py +29 -0
- agent_grammar-0.1.2/src/agent_grammar/testing/renderer.py +155 -0
- agent_grammar-0.1.2/tests/__init__.py +0 -0
- agent_grammar-0.1.2/tests/conftest.py +3 -0
- agent_grammar-0.1.2/tests/fixtures/__init__.py +0 -0
- agent_grammar-0.1.2/tests/fixtures/expected_workflows.md +23 -0
- agent_grammar-0.1.2/tests/fixtures/sample_app.py +23 -0
- agent_grammar-0.1.2/tests/test_cli.py +100 -0
- agent_grammar-0.1.2/tests/test_client_capture.py +63 -0
- agent_grammar-0.1.2/tests/test_decorators.py +101 -0
- agent_grammar-0.1.2/tests/test_plugin_e2e.py +138 -0
- agent_grammar-0.1.2/tests/test_renderer.py +94 -0
- agent_grammar-0.1.2/tests/test_serve_fastapi.py +155 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*.*.*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
name: Test (Python ${{ matrix.python-version }})
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: true
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ['3.10', '3.11', '3.12']
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
cache: 'pip'
|
|
24
|
+
|
|
25
|
+
- name: Install package with dev extras
|
|
26
|
+
run: |
|
|
27
|
+
python -m pip install --upgrade pip
|
|
28
|
+
pip install -e ".[dev]"
|
|
29
|
+
|
|
30
|
+
- name: Run tests
|
|
31
|
+
run: pytest
|
|
32
|
+
|
|
33
|
+
build:
|
|
34
|
+
name: Build distribution
|
|
35
|
+
needs: test
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
steps:
|
|
38
|
+
- uses: actions/checkout@v4
|
|
39
|
+
|
|
40
|
+
- name: Set up Python
|
|
41
|
+
uses: actions/setup-python@v5
|
|
42
|
+
with:
|
|
43
|
+
python-version: '3.12'
|
|
44
|
+
|
|
45
|
+
- name: Install build
|
|
46
|
+
run: |
|
|
47
|
+
python -m pip install --upgrade pip
|
|
48
|
+
pip install build
|
|
49
|
+
|
|
50
|
+
- name: Build sdist and wheel
|
|
51
|
+
run: python -m build
|
|
52
|
+
|
|
53
|
+
- name: Upload distribution artifacts
|
|
54
|
+
uses: actions/upload-artifact@v4
|
|
55
|
+
with:
|
|
56
|
+
name: python-package-distributions
|
|
57
|
+
path: dist/
|
|
58
|
+
|
|
59
|
+
publish:
|
|
60
|
+
name: Publish to PyPI
|
|
61
|
+
needs: build
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
environment:
|
|
64
|
+
name: pypi
|
|
65
|
+
url: https://pypi.org/p/agent-grammar
|
|
66
|
+
permissions:
|
|
67
|
+
id-token: write
|
|
68
|
+
steps:
|
|
69
|
+
- name: Download distribution artifacts
|
|
70
|
+
uses: actions/download-artifact@v4
|
|
71
|
+
with:
|
|
72
|
+
name: python-package-distributions
|
|
73
|
+
path: dist/
|
|
74
|
+
|
|
75
|
+
- name: Publish to PyPI
|
|
76
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dlfelps
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-grammar
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Test-gated AI agent workflow documentation for HTTP APIs.
|
|
5
|
+
Author-email: dlfelps <dlfelps@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: agents,ai,api,documentation,fastapi,llm,pytest
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Testing
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: click>=8.1
|
|
20
|
+
Requires-Dist: httpx>=0.24
|
|
21
|
+
Requires-Dist: jinja2>=3.1
|
|
22
|
+
Requires-Dist: pytest>=7.0
|
|
23
|
+
Requires-Dist: starlette>=0.27
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: fastapi>=0.100; extra == 'dev'
|
|
26
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
29
|
+
Provides-Extra: fastapi
|
|
30
|
+
Requires-Dist: fastapi>=0.100; extra == 'fastapi'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# agent-grammar
|
|
34
|
+
|
|
35
|
+
**Test-gated AI agent workflow documentation for HTTP APIs.**
|
|
36
|
+
|
|
37
|
+
`agent-grammar` lets API developers attach machine-readable workflow
|
|
38
|
+
documentation to their existing `pytest` suite. When integration tests pass,
|
|
39
|
+
a `workflows.md` blueprint is auto-compiled and served at a versioned route so
|
|
40
|
+
that external developers' AI agents (Cursor, Claude, Copilot, Gemini) can
|
|
41
|
+
fetch it and generate accurate integration code — no hallucinated endpoints,
|
|
42
|
+
no missing parameter bindings.
|
|
43
|
+
|
|
44
|
+
> Single source of truth: when business logic changes and tests are updated,
|
|
45
|
+
> the AI documentation re-compiles automatically.
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install agent-grammar # core (pytest + serve via Starlette)
|
|
51
|
+
pip install "agent-grammar[fastapi]" # adds FastAPI for serving
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
### 1. Annotate an integration test
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from agent_grammar import AgentTestClient, step_boundary, workflow
|
|
60
|
+
from app.main import app
|
|
61
|
+
|
|
62
|
+
client = AgentTestClient(app)
|
|
63
|
+
|
|
64
|
+
@workflow(
|
|
65
|
+
name="Material Onboarding Lifecycle",
|
|
66
|
+
intent="Secure a token, query the local DB for a zone, and register an asset.",
|
|
67
|
+
bindings=[
|
|
68
|
+
{"source": "Step 1.response.access_token", "target": "Step 3.headers.Authorization"},
|
|
69
|
+
{"source": "Step 2.mocked_db_result", "target": "Step 3.body.assigned_zone"},
|
|
70
|
+
],
|
|
71
|
+
)
|
|
72
|
+
def test_compile_material_onboarding():
|
|
73
|
+
auth = client.post("/v1/auth/token", json={"seed": "dev-token"})
|
|
74
|
+
assert auth.status_code == 200
|
|
75
|
+
token = auth.json()["access_token"]
|
|
76
|
+
|
|
77
|
+
with step_boundary(domain="Database", name="Query PostgreSQL for Zone UUID"):
|
|
78
|
+
zone_id = "mocked-uuid-from-db-query"
|
|
79
|
+
|
|
80
|
+
resp = client.post(
|
|
81
|
+
"/v1/materials",
|
|
82
|
+
json={"sku": "MAT-9901", "assigned_zone": zone_id},
|
|
83
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
84
|
+
)
|
|
85
|
+
assert resp.status_code == 201
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 2. Run the test suite
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pytest --agent-grammar-output=assets/workflows.md
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
When the test passes, `assets/workflows.md` is compiled. Failing tests are
|
|
95
|
+
excluded — that's the "Test-Gated" guarantee.
|
|
96
|
+
|
|
97
|
+
### 3. Serve the compiled markdown
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from fastapi import FastAPI
|
|
101
|
+
from agent_grammar.serve.fastapi import AgentTelemetryMiddleware, GrammarRouter
|
|
102
|
+
|
|
103
|
+
app = FastAPI(title="Core Engine API - v1")
|
|
104
|
+
|
|
105
|
+
def log_agent_metric(workflow_id: str) -> None:
|
|
106
|
+
print(f"METRIC: agent-driven request for workflow {workflow_id}")
|
|
107
|
+
|
|
108
|
+
app.add_middleware(AgentTelemetryMiddleware, on_detect=log_agent_metric)
|
|
109
|
+
app.include_router(
|
|
110
|
+
GrammarRouter(filepath="assets/workflows.md"),
|
|
111
|
+
prefix="/v1/agent-workflows",
|
|
112
|
+
)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 4. Publish system prompts for external developers
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
agent-grammar export-agent-docs \
|
|
119
|
+
--base-url https://api.production.com \
|
|
120
|
+
--api-version v1
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Writes `./agent-docs/{cursor,claude,copilot,gemini}-rules.md` ready for the
|
|
124
|
+
API host's developer portal.
|
|
125
|
+
|
|
126
|
+
## Configuration
|
|
127
|
+
|
|
128
|
+
| Option | Where | Default |
|
|
129
|
+
|---|---|---|
|
|
130
|
+
| `--agent-grammar-output` | pytest CLI | `assets/workflows.md` |
|
|
131
|
+
| `agent_grammar_output` | `pyproject.toml` `[tool.pytest.ini_options]` | `assets/workflows.md` |
|
|
132
|
+
| `--agent-grammar-disable` | pytest CLI | off |
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
MIT
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# agent-grammar
|
|
2
|
+
|
|
3
|
+
**Test-gated AI agent workflow documentation for HTTP APIs.**
|
|
4
|
+
|
|
5
|
+
`agent-grammar` lets API developers attach machine-readable workflow
|
|
6
|
+
documentation to their existing `pytest` suite. When integration tests pass,
|
|
7
|
+
a `workflows.md` blueprint is auto-compiled and served at a versioned route so
|
|
8
|
+
that external developers' AI agents (Cursor, Claude, Copilot, Gemini) can
|
|
9
|
+
fetch it and generate accurate integration code — no hallucinated endpoints,
|
|
10
|
+
no missing parameter bindings.
|
|
11
|
+
|
|
12
|
+
> Single source of truth: when business logic changes and tests are updated,
|
|
13
|
+
> the AI documentation re-compiles automatically.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install agent-grammar # core (pytest + serve via Starlette)
|
|
19
|
+
pip install "agent-grammar[fastapi]" # adds FastAPI for serving
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### 1. Annotate an integration test
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from agent_grammar import AgentTestClient, step_boundary, workflow
|
|
28
|
+
from app.main import app
|
|
29
|
+
|
|
30
|
+
client = AgentTestClient(app)
|
|
31
|
+
|
|
32
|
+
@workflow(
|
|
33
|
+
name="Material Onboarding Lifecycle",
|
|
34
|
+
intent="Secure a token, query the local DB for a zone, and register an asset.",
|
|
35
|
+
bindings=[
|
|
36
|
+
{"source": "Step 1.response.access_token", "target": "Step 3.headers.Authorization"},
|
|
37
|
+
{"source": "Step 2.mocked_db_result", "target": "Step 3.body.assigned_zone"},
|
|
38
|
+
],
|
|
39
|
+
)
|
|
40
|
+
def test_compile_material_onboarding():
|
|
41
|
+
auth = client.post("/v1/auth/token", json={"seed": "dev-token"})
|
|
42
|
+
assert auth.status_code == 200
|
|
43
|
+
token = auth.json()["access_token"]
|
|
44
|
+
|
|
45
|
+
with step_boundary(domain="Database", name="Query PostgreSQL for Zone UUID"):
|
|
46
|
+
zone_id = "mocked-uuid-from-db-query"
|
|
47
|
+
|
|
48
|
+
resp = client.post(
|
|
49
|
+
"/v1/materials",
|
|
50
|
+
json={"sku": "MAT-9901", "assigned_zone": zone_id},
|
|
51
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
52
|
+
)
|
|
53
|
+
assert resp.status_code == 201
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 2. Run the test suite
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pytest --agent-grammar-output=assets/workflows.md
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
When the test passes, `assets/workflows.md` is compiled. Failing tests are
|
|
63
|
+
excluded — that's the "Test-Gated" guarantee.
|
|
64
|
+
|
|
65
|
+
### 3. Serve the compiled markdown
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from fastapi import FastAPI
|
|
69
|
+
from agent_grammar.serve.fastapi import AgentTelemetryMiddleware, GrammarRouter
|
|
70
|
+
|
|
71
|
+
app = FastAPI(title="Core Engine API - v1")
|
|
72
|
+
|
|
73
|
+
def log_agent_metric(workflow_id: str) -> None:
|
|
74
|
+
print(f"METRIC: agent-driven request for workflow {workflow_id}")
|
|
75
|
+
|
|
76
|
+
app.add_middleware(AgentTelemetryMiddleware, on_detect=log_agent_metric)
|
|
77
|
+
app.include_router(
|
|
78
|
+
GrammarRouter(filepath="assets/workflows.md"),
|
|
79
|
+
prefix="/v1/agent-workflows",
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 4. Publish system prompts for external developers
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
agent-grammar export-agent-docs \
|
|
87
|
+
--base-url https://api.production.com \
|
|
88
|
+
--api-version v1
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Writes `./agent-docs/{cursor,claude,copilot,gemini}-rules.md` ready for the
|
|
92
|
+
API host's developer portal.
|
|
93
|
+
|
|
94
|
+
## Configuration
|
|
95
|
+
|
|
96
|
+
| Option | Where | Default |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `--agent-grammar-output` | pytest CLI | `assets/workflows.md` |
|
|
99
|
+
| `agent_grammar_output` | `pyproject.toml` `[tool.pytest.ini_options]` | `assets/workflows.md` |
|
|
100
|
+
| `--agent-grammar-disable` | pytest CLI | off |
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "agent-grammar"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Test-gated AI agent workflow documentation for HTTP APIs."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "dlfelps", email = "dlfelps@gmail.com" }]
|
|
13
|
+
keywords = ["pytest", "ai", "agents", "llm", "api", "documentation", "fastapi"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Framework :: Pytest",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Topic :: Software Development :: Testing",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"pytest>=7.0",
|
|
27
|
+
"starlette>=0.27",
|
|
28
|
+
"httpx>=0.24",
|
|
29
|
+
"jinja2>=3.1",
|
|
30
|
+
"click>=8.1",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
fastapi = ["fastapi>=0.100"]
|
|
35
|
+
dev = [
|
|
36
|
+
"fastapi>=0.100",
|
|
37
|
+
"pytest-cov",
|
|
38
|
+
"mypy",
|
|
39
|
+
"ruff",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
[project.entry-points.pytest11]
|
|
45
|
+
agent_grammar = "agent_grammar.testing.plugin"
|
|
46
|
+
|
|
47
|
+
[project.scripts]
|
|
48
|
+
agent-grammar = "agent_grammar.cli.main:cli"
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.wheel]
|
|
51
|
+
packages = ["src/agent_grammar"]
|
|
52
|
+
|
|
53
|
+
[tool.pytest.ini_options]
|
|
54
|
+
testpaths = ["tests"]
|
|
55
|
+
|
|
56
|
+
[tool.hatch.version]
|
|
57
|
+
path = "src/agent_grammar/__init__.py"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""ContextVar-based recorder lifecycle for the workflow capture pipeline."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from contextvars import ContextVar
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from agent_grammar._models import (
|
|
9
|
+
Binding,
|
|
10
|
+
BoundaryStep,
|
|
11
|
+
HttpStep,
|
|
12
|
+
WorkflowRecord,
|
|
13
|
+
slugify,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WorkflowRecorder:
|
|
18
|
+
"""Mutable builder collecting steps as a decorated test runs."""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
name: str,
|
|
23
|
+
intent: str,
|
|
24
|
+
bindings: list[Binding] | None = None,
|
|
25
|
+
) -> None:
|
|
26
|
+
self.name = name
|
|
27
|
+
self.intent = intent
|
|
28
|
+
self.bindings: list[Binding] = list(bindings) if bindings else []
|
|
29
|
+
self.steps: list[HttpStep | BoundaryStep] = []
|
|
30
|
+
self.complete: bool = False
|
|
31
|
+
|
|
32
|
+
def add_http_step(self, step: HttpStep) -> None:
|
|
33
|
+
self.steps.append(step)
|
|
34
|
+
|
|
35
|
+
def add_boundary_step(self, step: BoundaryStep) -> None:
|
|
36
|
+
self.steps.append(step)
|
|
37
|
+
|
|
38
|
+
def mark_complete(self) -> None:
|
|
39
|
+
self.complete = True
|
|
40
|
+
|
|
41
|
+
def build(self) -> WorkflowRecord:
|
|
42
|
+
return WorkflowRecord(
|
|
43
|
+
name=self.name,
|
|
44
|
+
slug=slugify(self.name),
|
|
45
|
+
intent=self.intent,
|
|
46
|
+
bindings=list(self.bindings),
|
|
47
|
+
steps=list(self.steps),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
_current_recorder: ContextVar[WorkflowRecorder | None] = ContextVar(
|
|
52
|
+
"agent_grammar_recorder", default=None
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def get_active_recorder() -> WorkflowRecorder | None:
|
|
57
|
+
return _current_recorder.get()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def set_active_recorder(recorder: WorkflowRecorder | None) -> Any:
|
|
61
|
+
return _current_recorder.set(recorder)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def reset_recorder(token: Any) -> None:
|
|
65
|
+
_current_recorder.reset(token)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Core data models for workflow recording and rendering."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Any, Union
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class HttpStep:
|
|
12
|
+
method: str
|
|
13
|
+
path: str
|
|
14
|
+
request_json: Any | None
|
|
15
|
+
status_code: int
|
|
16
|
+
response_json: Any | None
|
|
17
|
+
domain: str = "Core Service"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class BoundaryStep:
|
|
22
|
+
domain: str
|
|
23
|
+
name: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Step = Union[HttpStep, BoundaryStep]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class Binding:
|
|
31
|
+
source: str
|
|
32
|
+
target: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class WorkflowRecord:
|
|
37
|
+
name: str
|
|
38
|
+
slug: str
|
|
39
|
+
intent: str
|
|
40
|
+
bindings: list[Binding] = field(default_factory=list)
|
|
41
|
+
steps: list[Step] = field(default_factory=list)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
_SLUG_NON_ALNUM = re.compile(r"[^a-z0-9]+")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def slugify(name: str) -> str:
|
|
48
|
+
lowered = name.strip().lower()
|
|
49
|
+
collapsed = _SLUG_NON_ALNUM.sub("_", lowered)
|
|
50
|
+
return collapsed.strip("_")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""CLI entry points for agent-grammar."""
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""``agent-grammar export-agent-docs`` subcommand."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import click
|
|
8
|
+
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
9
|
+
|
|
10
|
+
PLATFORMS = ("cursor", "claude", "copilot", "gemini")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _build_environment() -> Environment:
|
|
14
|
+
return Environment(
|
|
15
|
+
loader=PackageLoader("agent_grammar", "templates"),
|
|
16
|
+
autoescape=select_autoescape(default=False),
|
|
17
|
+
trim_blocks=False,
|
|
18
|
+
lstrip_blocks=False,
|
|
19
|
+
keep_trailing_newline=True,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@click.command("export-agent-docs")
|
|
24
|
+
@click.option(
|
|
25
|
+
"--base-url",
|
|
26
|
+
required=True,
|
|
27
|
+
help="Production base URL of the API (e.g. https://api.example.com).",
|
|
28
|
+
)
|
|
29
|
+
@click.option(
|
|
30
|
+
"--api-version",
|
|
31
|
+
default="v1",
|
|
32
|
+
show_default=True,
|
|
33
|
+
help="API version namespace (used in the fetch URL and local file path).",
|
|
34
|
+
)
|
|
35
|
+
@click.option(
|
|
36
|
+
"--output-dir",
|
|
37
|
+
default="./agent-docs",
|
|
38
|
+
show_default=True,
|
|
39
|
+
type=click.Path(file_okay=False, dir_okay=True),
|
|
40
|
+
help="Directory to write the platform-specific system prompts.",
|
|
41
|
+
)
|
|
42
|
+
@click.option(
|
|
43
|
+
"--workflows-path",
|
|
44
|
+
default=None,
|
|
45
|
+
help=(
|
|
46
|
+
"Local file path where the agent should cache workflows. "
|
|
47
|
+
"Default: .agent/workflows_{version}.md"
|
|
48
|
+
),
|
|
49
|
+
)
|
|
50
|
+
@click.option(
|
|
51
|
+
"--platform",
|
|
52
|
+
"platforms",
|
|
53
|
+
multiple=True,
|
|
54
|
+
type=click.Choice(PLATFORMS),
|
|
55
|
+
default=PLATFORMS,
|
|
56
|
+
show_default=True,
|
|
57
|
+
help="Which platform rules to generate. Repeat to select multiple.",
|
|
58
|
+
)
|
|
59
|
+
def export_agent_docs(
|
|
60
|
+
base_url: str,
|
|
61
|
+
api_version: str,
|
|
62
|
+
output_dir: str,
|
|
63
|
+
workflows_path: str | None,
|
|
64
|
+
platforms: tuple[str, ...],
|
|
65
|
+
) -> None:
|
|
66
|
+
"""Generate platform-specific system prompts for API consumers."""
|
|
67
|
+
base = base_url.rstrip("/")
|
|
68
|
+
workflows_path = workflows_path or f".agent/workflows_{api_version}.md"
|
|
69
|
+
fetch_url = f"{base}/{api_version}/agent-workflows"
|
|
70
|
+
|
|
71
|
+
output = Path(output_dir)
|
|
72
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
|
|
74
|
+
env = _build_environment()
|
|
75
|
+
context = {
|
|
76
|
+
"base_url": base,
|
|
77
|
+
"api_version": api_version,
|
|
78
|
+
"workflows_path": workflows_path,
|
|
79
|
+
"fetch_url": fetch_url,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
written: list[Path] = []
|
|
83
|
+
for platform in platforms:
|
|
84
|
+
template = env.get_template(f"{platform}.md.j2")
|
|
85
|
+
rendered = template.render(**context)
|
|
86
|
+
target = output / f"{platform}-rules.md"
|
|
87
|
+
target.write_text(rendered, encoding="utf-8")
|
|
88
|
+
written.append(target)
|
|
89
|
+
|
|
90
|
+
click.echo(f"Wrote {len(written)} file(s) to {output}:")
|
|
91
|
+
for path in written:
|
|
92
|
+
click.echo(f" - {path}")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Top-level Click group for the ``agent-grammar`` console script."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
|
|
7
|
+
from agent_grammar import __version__
|
|
8
|
+
from agent_grammar.cli.export import export_agent_docs
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@click.group()
|
|
12
|
+
@click.version_option(version=__version__, prog_name="agent-grammar")
|
|
13
|
+
def cli() -> None:
|
|
14
|
+
"""agent-grammar: test-gated AI agent workflow documentation."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
cli.add_command(export_agent_docs)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == "__main__": # pragma: no cover
|
|
21
|
+
cli()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Server-side helpers for serving the compiled workflows.md."""
|