sprigpy 0.1.0.dev1__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.
- sprigpy-0.1.0.dev1/.gitignore +19 -0
- sprigpy-0.1.0.dev1/LICENSE +21 -0
- sprigpy-0.1.0.dev1/PKG-INFO +175 -0
- sprigpy-0.1.0.dev1/README.md +147 -0
- sprigpy-0.1.0.dev1/docs/knowledge/concepts/spec.md +52 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Interop/Program.cs +307 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Interop/Sprig.Interop.csproj +15 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/GeneratedCodeTests.cs +71 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/OperatorTests.cs +186 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/Ports.cs +26 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/RuntimeTests.cs +224 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/SchemaValidatorTests.cs +41 -0
- sprigpy-0.1.0.dev1/dotnet/tests/Sprig.Tests/Sprig.Tests.csproj +21 -0
- sprigpy-0.1.0.dev1/examples/__init__.py +0 -0
- sprigpy-0.1.0.dev1/examples/behavior/__init__.py +1 -0
- sprigpy-0.1.0.dev1/examples/behavior/behavior.py +105 -0
- sprigpy-0.1.0.dev1/examples/behavior/protocol.yml +52 -0
- sprigpy-0.1.0.dev1/examples/behavior/task.py +90 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions/BehaviorProtocol.Operators.Generated.cs +261 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions/BehaviorProtocol.Types.Generated.cs +447 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions/FipProtocol.Operators.Generated.cs +304 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions/FipProtocol.Types.Generated.cs +833 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions/Rig.cs +195 -0
- sprigpy-0.1.0.dev1/examples/bonsai/Extensions.csproj +15 -0
- sprigpy-0.1.0.dev1/examples/bonsai/NuGet.config +7 -0
- sprigpy-0.1.0.dev1/examples/bonsai/behavior_client.bonsai +141 -0
- sprigpy-0.1.0.dev1/examples/bonsai/fip_server.bonsai +200 -0
- sprigpy-0.1.0.dev1/examples/fip/__init__.py +1 -0
- sprigpy-0.1.0.dev1/examples/fip/fip.py +164 -0
- sprigpy-0.1.0.dev1/examples/fip/protocol.yml +84 -0
- sprigpy-0.1.0.dev1/examples/reward/__init__.py +0 -0
- sprigpy-0.1.0.dev1/examples/reward/client.py +50 -0
- sprigpy-0.1.0.dev1/examples/reward/protocol.yml +62 -0
- sprigpy-0.1.0.dev1/examples/reward/record.py +36 -0
- sprigpy-0.1.0.dev1/examples/reward/reward.py +121 -0
- sprigpy-0.1.0.dev1/examples/reward/server.py +73 -0
- sprigpy-0.1.0.dev1/pyproject.toml +85 -0
- sprigpy-0.1.0.dev1/spec.md +321 -0
- sprigpy-0.1.0.dev1/sprig.toml +17 -0
- sprigpy-0.1.0.dev1/src/sprig/__init__.py +52 -0
- sprigpy-0.1.0.dev1/src/sprig/base.py +47 -0
- sprigpy-0.1.0.dev1/src/sprig/client.py +415 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/__init__.py +1 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/cli.py +212 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/python.py +237 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/render.py +165 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/templates/bonsai/operators.cs.j2 +151 -0
- sprigpy-0.1.0.dev1/src/sprig/codegen/templates/python/protocol.py.j2 +58 -0
- sprigpy-0.1.0.dev1/src/sprig/envelope.py +138 -0
- sprigpy-0.1.0.dev1/src/sprig/loader.py +42 -0
- sprigpy-0.1.0.dev1/src/sprig/protocol.py +233 -0
- sprigpy-0.1.0.dev1/src/sprig/py.typed +0 -0
- sprigpy-0.1.0.dev1/src/sprig/recording.py +226 -0
- sprigpy-0.1.0.dev1/src/sprig/server.py +322 -0
- sprigpy-0.1.0.dev1/src/sprig/spec.py +497 -0
- sprigpy-0.1.0.dev1/src/sprig/transports/__init__.py +24 -0
- sprigpy-0.1.0.dev1/src/sprig/transports/http.py +180 -0
- sprigpy-0.1.0.dev1/src/sprig/transports/zmq.py +186 -0
- sprigpy-0.1.0.dev1/src/sprig/types.py +29 -0
- sprigpy-0.1.0.dev1/tests/conftest.py +193 -0
- sprigpy-0.1.0.dev1/tests/golden/BehaviorProtocol.Operators.Generated.cs +261 -0
- sprigpy-0.1.0.dev1/tests/golden/FipProtocol.Operators.Generated.cs +304 -0
- sprigpy-0.1.0.dev1/tests/golden/KitchenSinkProtocol.Operators.Generated.cs +291 -0
- sprigpy-0.1.0.dev1/tests/golden/behavior.py +105 -0
- sprigpy-0.1.0.dev1/tests/golden/fip.py +164 -0
- sprigpy-0.1.0.dev1/tests/golden/kitchen_sink.py +91 -0
- sprigpy-0.1.0.dev1/tests/specs/kitchen_sink.yml +80 -0
- sprigpy-0.1.0.dev1/tests/test_codegen.py +215 -0
- sprigpy-0.1.0.dev1/tests/test_interop.py +249 -0
- sprigpy-0.1.0.dev1/tests/test_recording.py +77 -0
- sprigpy-0.1.0.dev1/tests/test_runtime.py +306 -0
- sprigpy-0.1.0.dev1/tests/test_spec.py +160 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.venv/
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.pyc
|
|
4
|
+
.ruff_cache/
|
|
5
|
+
bin/
|
|
6
|
+
obj/
|
|
7
|
+
.vs/
|
|
8
|
+
*.user
|
|
9
|
+
dotnet/artifacts/
|
|
10
|
+
|
|
11
|
+
# Bonsai environment
|
|
12
|
+
.bonsai/Packages
|
|
13
|
+
.bonsai/Bonsai.exe.WebView2/
|
|
14
|
+
.bonsai/Settings/
|
|
15
|
+
.bonsai/*.exe
|
|
16
|
+
.bonsai/*.exe.settings
|
|
17
|
+
.bonsai/*.dll
|
|
18
|
+
*.bonsai.layout
|
|
19
|
+
site/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bruno Cruz
|
|
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,175 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sprigpy
|
|
3
|
+
Version: 0.1.0.dev1
|
|
4
|
+
Summary: Request/reply and pub/sub protocols written as YAML specs, with generated clients and servers for Python and Bonsai
|
|
5
|
+
Project-URL: Repository, https://github.com/bruno-f-cruz/sprig
|
|
6
|
+
Author: Bruno Cruz
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: bonsai,codegen,neuroscience,pubsub,rpc,zeromq
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Requires-Dist: jinja2>=3.1
|
|
19
|
+
Requires-Dist: jsonschema>=4.23
|
|
20
|
+
Requires-Dist: pydantic>=2.11
|
|
21
|
+
Requires-Dist: pyyaml>=6
|
|
22
|
+
Requires-Dist: pyzmq>=26
|
|
23
|
+
Provides-Extra: http
|
|
24
|
+
Requires-Dist: fastapi>=0.115; extra == 'http'
|
|
25
|
+
Requires-Dist: httpx>=0.27; extra == 'http'
|
|
26
|
+
Requires-Dist: uvicorn>=0.30; extra == 'http'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# sprig
|
|
30
|
+
|
|
31
|
+
[](https://github.com/bruno-f-cruz/sprig/actions/workflows/ci.yml) [](https://pypi.org/project/sprigpy/) [](https://www.nuget.org/packages/BFC.Sprig/)
|
|
32
|
+
|
|
33
|
+
Request/reply operations and pub/sub channels written once, as a YAML spec, with generated servers and clients for Python and Bonsai (C#).
|
|
34
|
+
|
|
35
|
+
```text
|
|
36
|
+
protocol.yml ──► sprig generate ──► Python module (pydantic models + protocol class)
|
|
37
|
+
│ └─► Bonsai operators + C# payload types (bonsai.sgen)
|
|
38
|
+
└─► sprig.load ─────► the same Python module, built at run time (from a file, a server, or a recording)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Transports: ZeroMQ (Python and Bonsai) and HTTP with server-sent events (Python). Both runtimes handle lifecycle, heartbeats, timeouts, schema checks, timestamps and recording for you.
|
|
42
|
+
|
|
43
|
+
| Package | Install |
|
|
44
|
+
|---|---|
|
|
45
|
+
| `sprigpy` (Python; `import sprig`) | `pip install sprigpy` (`sprigpy[http]` for HTTP) |
|
|
46
|
+
| `BFC.Sprig` (C# runtime) and `BFC.Bonsai.Sprig` (operators) | NuGet, or the Bonsai package manager |
|
|
47
|
+
|
|
48
|
+
## Write a spec
|
|
49
|
+
|
|
50
|
+
```yaml
|
|
51
|
+
# protocol.yml
|
|
52
|
+
sprig: 1
|
|
53
|
+
protocol: Fip
|
|
54
|
+
version: 0.3.0
|
|
55
|
+
endpoints:
|
|
56
|
+
zmq: {rpc: 6555, events: 6556} # default ports for servers and clients
|
|
57
|
+
|
|
58
|
+
operations:
|
|
59
|
+
Start: {request: StartRequest, reply: StartReply, errors: [AlreadyRunning]}
|
|
60
|
+
Stop: {request: Empty, reply: StopReply, errors: [NotRunning]}
|
|
61
|
+
|
|
62
|
+
channels:
|
|
63
|
+
Frames: {payload: FrameStats}
|
|
64
|
+
LedPower: {payload: LedPower, direction: client_to_server}
|
|
65
|
+
|
|
66
|
+
types: # JSON Schema; `type: object` is implied; every name is PascalCase
|
|
67
|
+
StartRequest:
|
|
68
|
+
properties:
|
|
69
|
+
FrameRateHz: {type: number, default: 20.0, exclusiveMinimum: 0, maximum: 100}
|
|
70
|
+
StartReply:
|
|
71
|
+
properties: {SessionId: {type: string}}
|
|
72
|
+
required: [SessionId]
|
|
73
|
+
# ...
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`Heartbeat` and `Lifecycle` channels are added to every protocol. Binary fields are `{type: string, format: byte}`. The loader checks names, references, defaults and ports, and reports `file:line: key: problem`. Full example: [examples/fip/protocol.yml](https://github.com/bruno-f-cruz/sprig/blob/main/examples/fip/protocol.yml).
|
|
77
|
+
|
|
78
|
+
## Generate
|
|
79
|
+
|
|
80
|
+
```toml
|
|
81
|
+
# sprig.toml
|
|
82
|
+
[[protocol]]
|
|
83
|
+
spec = "examples/fip/protocol.yml"
|
|
84
|
+
python = "examples/fip/fip.py"
|
|
85
|
+
bonsai = "examples/bonsai/Extensions"
|
|
86
|
+
namespace = "Examples.Fip"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
sprig generate # write every target
|
|
91
|
+
sprig generate --check # CI: fail if generated files are stale
|
|
92
|
+
sprig check protocol.yml # validate a spec
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Bonsai targets also need [bonsai.sgen](https://github.com/bonsai-rx/sgen) for the C# payload classes; `sprig generate` says how to install it when it is missing, and still writes the Python:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
# .NET 8 SDK or newer, then in your repository:
|
|
99
|
+
dotnet new tool-manifest
|
|
100
|
+
dotnet tool install bonsai.sgen --version 0.9.0
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Without a build step: `fip = sprig.load("protocol.yml")` gives the same module in memory.
|
|
104
|
+
|
|
105
|
+
## Python
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from sprig import Server, RpcFault, connect
|
|
109
|
+
from sprig.transports import ZmqServerTransport
|
|
110
|
+
from examples.fip.fip import AlreadyRunning, FipProtocol, StartReply, StartRequest
|
|
111
|
+
|
|
112
|
+
server = Server(FipProtocol)
|
|
113
|
+
|
|
114
|
+
@server.handle(FipProtocol.start)
|
|
115
|
+
async def start(request) -> StartReply:
|
|
116
|
+
if running:
|
|
117
|
+
raise RpcFault(AlreadyRunning(session_id=current))
|
|
118
|
+
return StartReply(session_id="s1")
|
|
119
|
+
|
|
120
|
+
await server.start(ZmqServerTransport()) # ports from the spec
|
|
121
|
+
|
|
122
|
+
async with await connect("zmq://rig", FipProtocol) as fip:
|
|
123
|
+
reply = await fip.invoke(FipProtocol.start, StartRequest(frame_rate_hz=30))
|
|
124
|
+
if reply.error: # errors are values, like HTTP
|
|
125
|
+
print(reply.error.code)
|
|
126
|
+
session = reply.raise_from_error().session_id # or raise RemoteError
|
|
127
|
+
async for event in fip.listen(FipProtocol.frames):
|
|
128
|
+
print(event.seq, event.timestamp, event.payload.fps)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Handlers, payloads and replies are typed for mypy/pyright. Python names are snake_case (`frame_rate_hz`, `FipProtocol.led_power`); the wire carries the spec's PascalCase names (`FrameRateHz`, `LedPower`). A client without the generated code uses `client.module`, built from the server's manifest.
|
|
132
|
+
|
|
133
|
+
## Bonsai
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
CreateFipProtocolServer -> ResourceSubject (Fip)
|
|
137
|
+
SubscribeSubject (Fip) -> Parse (Type = Start) -> Reply { ... -> StartReply } <- Fip (last input)
|
|
138
|
+
SubscribeSubject (Fip) -> Parse (Type = LedPower) -> SetLed -> Publish (Heartbeat) <- Fip
|
|
139
|
+
CreateBehaviorProtocolClient -> ResourceSubject (Behavior)
|
|
140
|
+
Timer -> MakeEmpty -> Request (Start) -> RaiseFromError -> ... <- Behavior
|
|
141
|
+
SubscribeSubject (Fip) -> Record (Path = D:\sessions)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`Parse` subscribes to a server or client; `Reply` (nested) answers parsed requests concurrently; `Request` emits replies with `Error`, and `RaiseFromError` turns an error into an exception; `Publish` sends on a channel; `Record` writes a recording. Wrong types and wiring fail at build time. See [Bonsai operators](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/codegen/bonsai-operators.md).
|
|
145
|
+
|
|
146
|
+
## Built in, on both sides
|
|
147
|
+
|
|
148
|
+
- `Lifecycle` started/stopping from servers, `lost` from clients after 3 missed heartbeats; heartbeat status re-sent every period.
|
|
149
|
+
- Every request gets exactly one response; `server_stopped` when a server stops first; clients never queue for an absent server.
|
|
150
|
+
- Servers check requests against the spec's schemas; clients check the server's protocol and major version.
|
|
151
|
+
- An internal table of live clients.
|
|
152
|
+
- Recordings: `protocol.yml`, `session.json`, `rpc.jsonl`, one JSONL file per channel; `sprig.open_recording(path)` reads them typed from their own spec, from Python or Bonsai.
|
|
153
|
+
|
|
154
|
+
## Repository
|
|
155
|
+
|
|
156
|
+
| Path | Contents |
|
|
157
|
+
|---|---|
|
|
158
|
+
| `src/sprig/` | spec loader, generator and CLI, runtime, transports, recording |
|
|
159
|
+
| `dotnet/src/` | `Sprig` and `Bonsai.Sprig` (net472, net8.0) |
|
|
160
|
+
| `dotnet/tests/` | MSTest suite; `Sprig.Interop`, the host the Python interop tests drive |
|
|
161
|
+
| `examples/` | FIP and behavior specs, generated modules, `examples/bonsai/` workflows and `Extensions` |
|
|
162
|
+
| `tests/` | pytest: spec, golden files, runtime, recording, Python <-> Bonsai |
|
|
163
|
+
| `scripts/` | `demo.ps1` (Bonsai FIP server + keyboard client), `regenerate.ps1` |
|
|
164
|
+
| `spec.md` | the protocol specification |
|
|
165
|
+
| `.bonsai/` | Bonsai 2.9.1 environment for the examples |
|
|
166
|
+
| `docs/knowledge/` | architecture notes |
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
uv sync --all-extras && dotnet tool restore
|
|
170
|
+
uv run sprig generate && dotnet build dotnet/Sprig.slnx
|
|
171
|
+
uv run pytest && uv run pytest -m interop && dotnet test dotnet/Sprig.slnx
|
|
172
|
+
.bonsai/setup.cmd && scripts/demo.ps1 # the example in the Bonsai editor with a keyboard-driven Python client
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
See [build and run](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/build-and-run.md), [testing](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/testing.md), [packaging](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/packaging.md). License: MIT.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# sprig
|
|
2
|
+
|
|
3
|
+
[](https://github.com/bruno-f-cruz/sprig/actions/workflows/ci.yml) [](https://pypi.org/project/sprigpy/) [](https://www.nuget.org/packages/BFC.Sprig/)
|
|
4
|
+
|
|
5
|
+
Request/reply operations and pub/sub channels written once, as a YAML spec, with generated servers and clients for Python and Bonsai (C#).
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
protocol.yml ──► sprig generate ──► Python module (pydantic models + protocol class)
|
|
9
|
+
│ └─► Bonsai operators + C# payload types (bonsai.sgen)
|
|
10
|
+
└─► sprig.load ─────► the same Python module, built at run time (from a file, a server, or a recording)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Transports: ZeroMQ (Python and Bonsai) and HTTP with server-sent events (Python). Both runtimes handle lifecycle, heartbeats, timeouts, schema checks, timestamps and recording for you.
|
|
14
|
+
|
|
15
|
+
| Package | Install |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `sprigpy` (Python; `import sprig`) | `pip install sprigpy` (`sprigpy[http]` for HTTP) |
|
|
18
|
+
| `BFC.Sprig` (C# runtime) and `BFC.Bonsai.Sprig` (operators) | NuGet, or the Bonsai package manager |
|
|
19
|
+
|
|
20
|
+
## Write a spec
|
|
21
|
+
|
|
22
|
+
```yaml
|
|
23
|
+
# protocol.yml
|
|
24
|
+
sprig: 1
|
|
25
|
+
protocol: Fip
|
|
26
|
+
version: 0.3.0
|
|
27
|
+
endpoints:
|
|
28
|
+
zmq: {rpc: 6555, events: 6556} # default ports for servers and clients
|
|
29
|
+
|
|
30
|
+
operations:
|
|
31
|
+
Start: {request: StartRequest, reply: StartReply, errors: [AlreadyRunning]}
|
|
32
|
+
Stop: {request: Empty, reply: StopReply, errors: [NotRunning]}
|
|
33
|
+
|
|
34
|
+
channels:
|
|
35
|
+
Frames: {payload: FrameStats}
|
|
36
|
+
LedPower: {payload: LedPower, direction: client_to_server}
|
|
37
|
+
|
|
38
|
+
types: # JSON Schema; `type: object` is implied; every name is PascalCase
|
|
39
|
+
StartRequest:
|
|
40
|
+
properties:
|
|
41
|
+
FrameRateHz: {type: number, default: 20.0, exclusiveMinimum: 0, maximum: 100}
|
|
42
|
+
StartReply:
|
|
43
|
+
properties: {SessionId: {type: string}}
|
|
44
|
+
required: [SessionId]
|
|
45
|
+
# ...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`Heartbeat` and `Lifecycle` channels are added to every protocol. Binary fields are `{type: string, format: byte}`. The loader checks names, references, defaults and ports, and reports `file:line: key: problem`. Full example: [examples/fip/protocol.yml](https://github.com/bruno-f-cruz/sprig/blob/main/examples/fip/protocol.yml).
|
|
49
|
+
|
|
50
|
+
## Generate
|
|
51
|
+
|
|
52
|
+
```toml
|
|
53
|
+
# sprig.toml
|
|
54
|
+
[[protocol]]
|
|
55
|
+
spec = "examples/fip/protocol.yml"
|
|
56
|
+
python = "examples/fip/fip.py"
|
|
57
|
+
bonsai = "examples/bonsai/Extensions"
|
|
58
|
+
namespace = "Examples.Fip"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
sprig generate # write every target
|
|
63
|
+
sprig generate --check # CI: fail if generated files are stale
|
|
64
|
+
sprig check protocol.yml # validate a spec
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Bonsai targets also need [bonsai.sgen](https://github.com/bonsai-rx/sgen) for the C# payload classes; `sprig generate` says how to install it when it is missing, and still writes the Python:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
# .NET 8 SDK or newer, then in your repository:
|
|
71
|
+
dotnet new tool-manifest
|
|
72
|
+
dotnet tool install bonsai.sgen --version 0.9.0
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Without a build step: `fip = sprig.load("protocol.yml")` gives the same module in memory.
|
|
76
|
+
|
|
77
|
+
## Python
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from sprig import Server, RpcFault, connect
|
|
81
|
+
from sprig.transports import ZmqServerTransport
|
|
82
|
+
from examples.fip.fip import AlreadyRunning, FipProtocol, StartReply, StartRequest
|
|
83
|
+
|
|
84
|
+
server = Server(FipProtocol)
|
|
85
|
+
|
|
86
|
+
@server.handle(FipProtocol.start)
|
|
87
|
+
async def start(request) -> StartReply:
|
|
88
|
+
if running:
|
|
89
|
+
raise RpcFault(AlreadyRunning(session_id=current))
|
|
90
|
+
return StartReply(session_id="s1")
|
|
91
|
+
|
|
92
|
+
await server.start(ZmqServerTransport()) # ports from the spec
|
|
93
|
+
|
|
94
|
+
async with await connect("zmq://rig", FipProtocol) as fip:
|
|
95
|
+
reply = await fip.invoke(FipProtocol.start, StartRequest(frame_rate_hz=30))
|
|
96
|
+
if reply.error: # errors are values, like HTTP
|
|
97
|
+
print(reply.error.code)
|
|
98
|
+
session = reply.raise_from_error().session_id # or raise RemoteError
|
|
99
|
+
async for event in fip.listen(FipProtocol.frames):
|
|
100
|
+
print(event.seq, event.timestamp, event.payload.fps)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Handlers, payloads and replies are typed for mypy/pyright. Python names are snake_case (`frame_rate_hz`, `FipProtocol.led_power`); the wire carries the spec's PascalCase names (`FrameRateHz`, `LedPower`). A client without the generated code uses `client.module`, built from the server's manifest.
|
|
104
|
+
|
|
105
|
+
## Bonsai
|
|
106
|
+
|
|
107
|
+
```text
|
|
108
|
+
CreateFipProtocolServer -> ResourceSubject (Fip)
|
|
109
|
+
SubscribeSubject (Fip) -> Parse (Type = Start) -> Reply { ... -> StartReply } <- Fip (last input)
|
|
110
|
+
SubscribeSubject (Fip) -> Parse (Type = LedPower) -> SetLed -> Publish (Heartbeat) <- Fip
|
|
111
|
+
CreateBehaviorProtocolClient -> ResourceSubject (Behavior)
|
|
112
|
+
Timer -> MakeEmpty -> Request (Start) -> RaiseFromError -> ... <- Behavior
|
|
113
|
+
SubscribeSubject (Fip) -> Record (Path = D:\sessions)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`Parse` subscribes to a server or client; `Reply` (nested) answers parsed requests concurrently; `Request` emits replies with `Error`, and `RaiseFromError` turns an error into an exception; `Publish` sends on a channel; `Record` writes a recording. Wrong types and wiring fail at build time. See [Bonsai operators](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/codegen/bonsai-operators.md).
|
|
117
|
+
|
|
118
|
+
## Built in, on both sides
|
|
119
|
+
|
|
120
|
+
- `Lifecycle` started/stopping from servers, `lost` from clients after 3 missed heartbeats; heartbeat status re-sent every period.
|
|
121
|
+
- Every request gets exactly one response; `server_stopped` when a server stops first; clients never queue for an absent server.
|
|
122
|
+
- Servers check requests against the spec's schemas; clients check the server's protocol and major version.
|
|
123
|
+
- An internal table of live clients.
|
|
124
|
+
- Recordings: `protocol.yml`, `session.json`, `rpc.jsonl`, one JSONL file per channel; `sprig.open_recording(path)` reads them typed from their own spec, from Python or Bonsai.
|
|
125
|
+
|
|
126
|
+
## Repository
|
|
127
|
+
|
|
128
|
+
| Path | Contents |
|
|
129
|
+
|---|---|
|
|
130
|
+
| `src/sprig/` | spec loader, generator and CLI, runtime, transports, recording |
|
|
131
|
+
| `dotnet/src/` | `Sprig` and `Bonsai.Sprig` (net472, net8.0) |
|
|
132
|
+
| `dotnet/tests/` | MSTest suite; `Sprig.Interop`, the host the Python interop tests drive |
|
|
133
|
+
| `examples/` | FIP and behavior specs, generated modules, `examples/bonsai/` workflows and `Extensions` |
|
|
134
|
+
| `tests/` | pytest: spec, golden files, runtime, recording, Python <-> Bonsai |
|
|
135
|
+
| `scripts/` | `demo.ps1` (Bonsai FIP server + keyboard client), `regenerate.ps1` |
|
|
136
|
+
| `spec.md` | the protocol specification |
|
|
137
|
+
| `.bonsai/` | Bonsai 2.9.1 environment for the examples |
|
|
138
|
+
| `docs/knowledge/` | architecture notes |
|
|
139
|
+
|
|
140
|
+
```sh
|
|
141
|
+
uv sync --all-extras && dotnet tool restore
|
|
142
|
+
uv run sprig generate && dotnet build dotnet/Sprig.slnx
|
|
143
|
+
uv run pytest && uv run pytest -m interop && dotnet test dotnet/Sprig.slnx
|
|
144
|
+
.bonsai/setup.cmd && scripts/demo.ps1 # the example in the Bonsai editor with a keyboard-driven Python client
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
See [build and run](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/build-and-run.md), [testing](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/testing.md), [packaging](https://github.com/bruno-f-cruz/sprig/blob/main/docs/knowledge/workflows/packaging.md). License: MIT.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
---
|
|
2
|
+
type: Reference
|
|
3
|
+
title: The spec (protocol.yml)
|
|
4
|
+
description: The authoring format - keys, defaults, the JSON Schema subset for types, the checks the loader runs (with file, line and key), normalization, and how the manifest relates.
|
|
5
|
+
resource: src/sprig/spec.py
|
|
6
|
+
tags: [spec, yaml, schema, manifest]
|
|
7
|
+
timestamp: 2026-11-30T00:00:00Z
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Schema
|
|
11
|
+
|
|
12
|
+
| Key | Required | Meaning |
|
|
13
|
+
|---|---|---|
|
|
14
|
+
| `sprig` | no (1) | spec format version; others are refused |
|
|
15
|
+
| `protocol` | yes | name: default server name, recording folder, C# prefix (`Fip` -> `FipProtocol`) |
|
|
16
|
+
| `version` | yes | semver; clients refuse another major version |
|
|
17
|
+
| `description` | no | docstrings, Bonsai descriptions |
|
|
18
|
+
| `endpoints.zmq.rpc` / `.events` | no (5555 / 5556) | default ports for servers and clients |
|
|
19
|
+
| `endpoints.http.port` | no (8080) | default HTTP port (Python) |
|
|
20
|
+
| `operations.<name>` | | `request`, `reply`, `errors` (type names), `description` |
|
|
21
|
+
| `channels.<name>` | | `payload`, `direction` (`server_to_client` default or `client_to_server`), `retain_last`, `description` |
|
|
22
|
+
| `types.<Name>` | | JSON Schema; `$ref: Name` = `#/$defs/Name`; `properties` implies `type: object`; `{}` = no fields |
|
|
23
|
+
|
|
24
|
+
Supported schema keywords: `type` (incl. `[x, "null"]`), `properties`, `required`, `items`, `additionalProperties`, `enum` (+ `x-enumNames` for integers), `$ref`, `oneOf`, `format` (`byte` = binary/base64, `date-time`), `default`, `description`, `title`, min/max, exclusive min/max, lengths, `pattern`, item counts. Inline objects are refused: name the type and `$ref` it.
|
|
25
|
+
|
|
26
|
+
Built in: `Empty`, `Heartbeat`, `HeartbeatCode`, `Lifecycle`, `LifecycleState`, and the `Heartbeat` and `Lifecycle` channels ([base protocol](base-protocol.md)).
|
|
27
|
+
|
|
28
|
+
## Checks
|
|
29
|
+
|
|
30
|
+
Every name (protocol, operations, channels, fields, types) is PascalCase with acronyms as words (`HttpPort`), and that is the name on the wire; Python converts members and fields to snake_case (`FrameRateHz` <-> `frame_rate_hz`, through `sprig.Model` aliases), C# uses them as written. A field whose snake_case is a Python keyword is refused. `Describe`, `Heartbeat`, `Lifecycle` reserved; every referenced type exists; payloads are object types; defaults match their schemas; ports valid and distinct. Errors read `file:line: key.path: message`, e.g. `protocol.yml:28: channels.Frames.about 2 Hz: unknown key 'about 2 Hz' (in {...} flow style, quote values that contain commas)`.
|
|
31
|
+
|
|
32
|
+
## Normalization and the manifest
|
|
33
|
+
|
|
34
|
+
`load_spec` fills defaults, expands refs, adds the built-ins. A normalized spec loads back unchanged; `Spec.digest()` hashes it without runtime fields. A server's `Describe` returns it plus `handled` per operation and `bound` addresses. Base types in a received manifest are replaced by the local runtime's.
|
|
35
|
+
|
|
36
|
+
# Examples
|
|
37
|
+
```yaml
|
|
38
|
+
sprig: 1
|
|
39
|
+
protocol: Fip
|
|
40
|
+
version: 0.3.0
|
|
41
|
+
endpoints:
|
|
42
|
+
zmq: {rpc: 6555, events: 6556}
|
|
43
|
+
operations:
|
|
44
|
+
Start: {request: StartRequest, reply: StartReply, errors: [AlreadyRunning]}
|
|
45
|
+
channels:
|
|
46
|
+
Frames: {payload: FrameStats}
|
|
47
|
+
types:
|
|
48
|
+
StartRequest:
|
|
49
|
+
properties:
|
|
50
|
+
FrameRateHz: {type: number, default: 20.0, exclusiveMinimum: 0, maximum: 100}
|
|
51
|
+
```
|
|
52
|
+
Full examples: `examples/fip/protocol.yml`, `examples/behavior/protocol.yml`, `tests/specs/kitchen_sink.yml`.
|