CAP (Cordum Agent Protocol) Python SDK
Project description
CAP Python SDK
Asyncio-first SDK with NATS helpers for CAP workers and clients.
Quick Start
-
Generate protobuf stubs into this SDK (one-time per proto change):
python -m grpc_tools.protoc \ -I../../proto \ --python_out=./cap/pb \ --grpc_python_out=./cap/pb \ ../../proto/cordum/agent/v1/*.proto
(Or run
./tools/make_protos.shfrom repo root withCAP_RUN_PY=1and copy/pythonintosdk/python/cap/pbif you want vendored stubs.) -
Install:
pip install -e .
-
Run a worker:
import asyncio from cap import worker from cap.pb.cordum.agent.v1 import job_pb2 async def handle(req: job_pb2.JobRequest): return job_pb2.JobResult( job_id=req.job_id, status=job_pb2.JOB_STATUS_SUCCEEDED, result_ptr=f"redis://res/{req.job_id}", worker_id="worker-echo-1", ) asyncio.run(worker.run_worker("nats://127.0.0.1:4222", "job.echo", handle))
-
Submit a job (client):
import asyncio from cryptography.hazmat.primitives.asymmetric import ec from cap import client from cap.pb.cordum.agent.v1 import job_pb2 import nats async def main(): nc = await nats.connect("nats://127.0.0.1:4222") priv = ec.generate_private_key(ec.SECP256R1()) req = job_pb2.JobRequest( job_id="job-echo-1", topic="job.echo", context_ptr="redis://ctx/job-echo-1", ) await client.submit_job(nc, req, "trace-1", "client-py", priv) await nc.drain() asyncio.run(main())
Files
cap/bus.py— NATS connector.cap/worker.py— worker skeleton with handler hook.cap/client.py— publish JobRequest tosys.job.submit.cap/pb/— protobuf stubs (generated).
Defaults
- Subjects:
sys.job.submit,sys.job.result,sys.heartbeat. - Protocol version:
1. - Signing:
submit_jobandrun_workersign envelopes when given anec.EllipticCurvePrivateKey. Signatures use deterministic protobuf serialization (map entries ordered by key) for cross-SDK verification. Generate a keypair withcryptography:from cryptography.hazmat.primitives.asymmetric import ec priv = ec.generate_private_key(ec.SECP256R1()) pub = priv.public_key()
- Set
public_keysonrun_workerto verify incoming packets. - Omit
public_keysto accept unsigned packets. - Pass
private_key=Nonetosubmit_jobif you want to send unsigned envelopes.
Swap out cap.bus if you need a different transport.
Runtime (High-Level SDK)
The runtime hides NATS/Redis plumbing and gives you typed handlers.
import asyncio
from pydantic import BaseModel
from cap.runtime import Agent, Context
class Input(BaseModel):
prompt: str
class Output(BaseModel):
summary: str
agent = Agent(retries=2)
@agent.job("job.summarize", input_model=Input, output_model=Output)
async def summarize(ctx: Context, data: Input) -> Output:
return Output(summary=data.prompt[:140])
asyncio.run(agent.run())
Environment
NATS_URL(defaultnats://127.0.0.1:4222)REDIS_URL(defaultredis://127.0.0.1:6379/0)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cap_sdk_python-2.5.1.tar.gz.
File metadata
- Download URL: cap_sdk_python-2.5.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b1ab2b11878bf54854f4c1a673c0efa771955d069e9cd894915d169121f5c2f
|
|
| MD5 |
fe3d2b7dfe0392aa1ffcab03a70705da
|
|
| BLAKE2b-256 |
21ced0a33fe3adf4b0e36af2d47e9f642de99fe7087dade761fcc4f0159ce138
|
File details
Details for the file cap_sdk_python-2.5.1-py3-none-any.whl.
File metadata
- Download URL: cap_sdk_python-2.5.1-py3-none-any.whl
- Upload date:
- Size: 26.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f07c1df9546bb3b8cb6bf3efbffcd57bc00b6a085a0d3d3f08e32a2c08402143
|
|
| MD5 |
0789e2dc6e52aa529d083c762c7e7ecc
|
|
| BLAKE2b-256 |
33273c80dbe1439750a4037d993081d2170126d269d6d76401e4c142ba26cd8e
|