Skip to main content

CAP (Cordum Agent Protocol) Python SDK

Project description

CAP Python SDK

Asyncio-first SDK with NATS helpers for CAP workers and clients.

Quick Start

  1. 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.sh from repo root with CAP_RUN_PY=1 and copy /python into sdk/python/cap/pb if you want vendored stubs.)

  2. Install:

    pip install -e .
    
  3. 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))
    
  4. 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 to sys.job.submit.
  • cap/pb/ — protobuf stubs (generated).

Defaults

  • Subjects: sys.job.submit, sys.job.result, sys.heartbeat.
  • Protocol version: 1.
  • Signing: submit_job and run_worker sign envelopes when given an ec.EllipticCurvePrivateKey. Signatures use deterministic protobuf serialization (map entries ordered by key) for cross-SDK verification. Generate a keypair with cryptography:
    from cryptography.hazmat.primitives.asymmetric import ec
    priv = ec.generate_private_key(ec.SECP256R1())
    pub = priv.public_key()
    
  • Set public_keys on run_worker to verify incoming packets.
  • Omit public_keys to accept unsigned packets.
  • Pass private_key=None to submit_job if 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 (default nats://127.0.0.1:4222)
  • REDIS_URL (default redis://127.0.0.1:6379/0)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cap_sdk_python-2.5.2.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cap_sdk_python-2.5.2-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file cap_sdk_python-2.5.2.tar.gz.

File metadata

  • Download URL: cap_sdk_python-2.5.2.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cap_sdk_python-2.5.2.tar.gz
Algorithm Hash digest
SHA256 3dc08ec723e3baf21cadafb78b8b71232a3dffe40e39b17fc11a9773c50caee0
MD5 5de66af59f9566b751304353b962f5ee
BLAKE2b-256 93f96ebd66f8b6c17a296a9e78613b5864a5a6e40baf48cbea1970b7b2ba59fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for cap_sdk_python-2.5.2.tar.gz:

Publisher: publish-python.yml on cordum-io/cap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cap_sdk_python-2.5.2-py3-none-any.whl.

File metadata

  • Download URL: cap_sdk_python-2.5.2-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cap_sdk_python-2.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 131fb1023e98552ee8c3fef9a8ed46c0f6451ad0c029abb91775e9313123f2be
MD5 7ea8716450cd415e0136c80ba2287878
BLAKE2b-256 78ea46011aa84665c1fe25d06f0f239a54b68b75488d178647728fc13bd9ac8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cap_sdk_python-2.5.2-py3-none-any.whl:

Publisher: publish-python.yml on cordum-io/cap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page