Metadata-Version: 2.4
Name: zanii-campus
Version: 0.2.0
Summary: Zanii Campus firm SDK: declare agents and deployments, count outcomes locally, send signed counts only
License-Expression: Apache-2.0
Project-URL: Homepage, https://campus.zanii.agency
Keywords: ai-agents,difc,oversight,ed25519,audit
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: zanii==0.40.1
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Dynamic: license-file

# zanii-campus

The firm SDK for Zanii Campus (Python). Your company declares its AI agents and where they run, counts outcomes on
your own servers and sends DIFC one signed batch of counts per deployment every five minutes. Records are signed
byte for byte like the TypeScript SDK (`@zanii/campus-sdk`).

## What is sent

Counts only, signed with your company key:

- Your agents: DID, a short product name, a kind (chatbot, voice, workflow, ...).
- Your deployments: an id, the agent, a client pseudonym you choose (never a client name), sector, country, channel, start and end dates.
- Outcome batches: per deployment and period, the number of sessions and queries, how many were resolved, escalated,
  failed or refused, and p50/p95 latency in milliseconds.

## What is never sent

Prompts, answers, documents, customer names, phone numbers, IDs, emails or any other text from a conversation. Every
record is a closed shape and `sign_record` refuses any unknown field before a request is made. The server rejects
any content too, so even a modified client cannot send it.

## Quickstart (10 minutes)

```sh
pip install zanii-campus
```

1. Create your company key once and keep it in a secret store (vault, KMS, sealed file). It never leaves your company.

```python
from zanii_campus import new_company_key
key, did = new_company_key()  # save key.hex() to your secret store
```

2. Join with the invite DIFC sent you. The token is shown once: store it like a password.

```python
import os
from zanii_campus import CampusClient
campus = CampusClient("https://campus.zanii.agency", company_key=os.environ["CAMPUS_KEY"], state_dir="./var/campus")
token = campus.join("inv_...", "7574")["token"]  # your DIFC registration number
```

Afterwards construct the client with `token=os.environ["CAMPUS_TOKEN"]`.

3. Register each agent and each deployment.

Any kind of agent works the same way: a chat assistant, a trading agent, a document reviewer, a voice line, a
back-office workflow, a data pipeline. Each deployment says what one counted item is (`unit`: conversation, task,
request, document, transaction, call, job, other) and every item ends one of four ways:
`resolved` (completed by the agent), `escalated` (handed to a person), `failed`, or `refused` (blocked by a rule).

```python
# Example: a trading agent deployed for a brokerage, reached through an API
campus.register_agent("did:key:z6Mk...", "Order execution agent", "trading")
campus.deploy("dep-broker-01", "did:key:z6Mk...", {"ref": "client-7f3a", "sector": "finance", "country": "AE"}, "api", unit="transaction")
```

4. Count outcomes around your agent's work (sync or async functions).

```python
track = campus.tracker("dep-broker-01")

@track.wrap
async def execute(order): ...              # resolved on return, failed on exception

execute2 = track.wrap(my_agent.execute, lambda result, error: "failed" if error else "escalated" if result.needs_approval else "resolved")
track.record("refused", new_session=True)  # or count by hand
campus.close()                              # on shutdown: sends the open period
```

## API

- `CampusClient(base_url, token=None, *, company_key, opener=None, state_dir=None, auto_flush=True, max_queued=10000, on_reject=None, on_error=None)`
- `join(invite, reg_no)` returns `{"company", "token"}`; `me()`; `register_agent(agent_did, name, kind)`;
  `deploy(id, agent_did, client, channel, started=None, ended=None)` (send again with `ended` to end it).
- `tracker(deployment_id, period_ms=300000)` returns a tracker with `record(outcome, latency_ms=None, new_session=False)`,
  `wrap(fn, classify=None)` and `flush()`.
- `flush()`, `close()`, `pending`, `new_company_key()`, `CampusError` (status, reasons, retry_after), and the contract
  checks (`sign_record`, `verify_record`, `record_hash`, `check_*`, `ContractError`).

## Reliability

Counting is in memory; one batch per deployment per period; up to 100 batches per request. On 429, 503 or a network
error the SDK backs off with exponential jitter (never sooner than `Retry-After`), keeps counting and resends the same
signed batches later (same nonce, so a resend is a duplicate, never a double count). With `state_dir`, unsent batches
are kept on disk (JSON lines, at most 10,000) and resent after a restart. A batch the server rejects is dropped and
reported to `on_reject`. Empty periods are not sent. Standard library HTTP only; the one dependency is `zanii`.

Apache-2.0.

## Changelog

- **0.2.0**: deployments can say what one counted item is (`unit`: conversation, task, request, document, transaction, call, job, other); the counts are documented for any kind of agent, not only chat. Optional, so 0.1.0 code keeps working.
- **0.1.0**: first release.
