Metadata-Version: 2.4
Name: crewai-sealgraf
Version: 0.1.1
Summary: Sealgraf spend-approval tools for CrewAI: check a purchase against the owner's limits before buying, owner approval on Sealgraf's own surface, sealed receipts.
Author: Sai Arun Gunuganti
License: MIT
Project-URL: Homepage, https://sealgraf.com
Project-URL: Documentation, https://sealgraf.com/developers/setup
Keywords: crewai,agents,spend-approval,sealgraf,receipts
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: crewai<2,>=1.15
Requires-Dist: httpx<0.29,>=0.27
Requires-Dist: pydantic<3,>=2
Dynamic: license-file

# crewai-sealgraf

Sealgraf spend-approval tools for [CrewAI](https://crewai.com): before an
agent buys anything, it checks the purchase against limits the owner set
in advance; purchases above the owner's ask-line wait for the owner's own
approval on Sealgraf's surface; every completed purchase is sealed into a
receipt neither side can rewrite.

## The spend loop

```
check_permission ── allow ──────────────► buy ──► complete_purchase ──► receipt
       │
       ├── deny ────────────────────────► do not buy
       │
       └── needs_human_approval ──► owner approves in Sealgraf
                                   claim_decision_token ──► buy
                                   complete_purchase ──► receipt
```

`get_receipt` reads back either a check (by `preauth_id`, via
`GET /v1/preauthorize/{id}`) or a sealed receipt (by `receipt_id`,
via `GET /v1/receipts/{id}`) at any point.

## Install

```bash
pip install crewai-sealgraf
```

## Configure

The tools need an **agent-scope Sealgraf API key**. The key is host
configuration, never a tool argument (tool arguments are chosen by the
model and visible in prompts and logs). Pass it in code or set the
environment:

```bash
export SEALGRAF_API_KEY=<redacted>
# optional; defaults to https://sealgraf.com
export SEALGRAF_BASE_URL=https://sealgraf.com
```

## Use

```python
from crewai import Agent
from crewai_sealgraf import sealgraf_tools

buyer = Agent(
    role="Purchasing agent",
    goal="Buy approved supplies within the owner's limits",
    backstory="...",
    tools=sealgraf_tools(),          # reads SEALGRAF_API_KEY
)

# or explicitly:
tools = sealgraf_tools(api_key="see_...", base_url="https://sealgraf.com")
```

The four tools:

| Tool | Name for the model | What it does |
|---|---|---|
| `SealgrafCheckPermissionTool` | `sealgraf_check_permission` | Ask before buying. Returns `allow` (with a single-use `decision_token`), `deny`, or `needs_human_approval`. |
| `SealgrafClaimDecisionTokenTool` | `sealgraf_claim_decision_token` | After the owner approves, claim the single-use decision token. Fails with a stable code (e.g. `not_approved_yet`) while the owner has not decided. |
| `SealgrafCompletePurchaseTool` | `sealgraf_complete_purchase` | After the purchase is made, report it; Sealgraf seals it into a receipt. Amounts are integer minor units (cents). |
| `SealgrafGetReceiptTool` | `sealgraf_get_receipt` | Read a check's state (by `preauth_id`) or a sealed receipt (by `receipt_id`). |

Every tool returns the parsed Sealgraf result as JSON — the same shapes
as the Sealgraf Dify plugin and MCP surfaces.

## Safety behaviour

- **Fail closed.** Any failure — timeout, network error, non-2xx,
  malformed response, a token the server did not actually issue — raises
  and fails the tool call. A tool never renders an ambiguous answer as
  permission.
- **Replay honesty.** Repeating a check with the same idempotency key
  returns the original check flagged `"replayed": true`; a replayed
  `allow` does **not** re-serve the decision token
  (`"decision_token_redelivered": false`). Use the token from the first
  response; if it is lost, run a fresh check without the idempotency key.
- **Tokens are single-use** and are passed through untouched; they are
  never logged by this package.
- **The amount paid is required.** `sealgraf_complete_purchase` needs
  `completed_amount_minor` — what was actually paid, in cents. Sealgraf
  seals it as the amount the agent reported; it does not see or move
  the payment itself.
- **No verification link.** This package does not create a shareable
  check link or a receipt view. Its tools return the sealed receipt
  record; the owner views and shares receipts in their Sealgraf
  account.
- **Waiting for approval.** After a `needs_human_approval` check, the
  owner's decision takes human time. Poll `sealgraf_get_receipt` for
  the preauth's state until it reads `authorized`, *then* claim — do
  not call `sealgraf_claim_decision_token` repeatedly while waiting:
  every early claim fails with `not_approved_yet`, and an agent
  executor will happily retry it in a tight loop (observed live: 6
  retries in one run). The refusal is harmless — no token exists
  before approval — but the polling pattern is cheaper and clearer.

## Development

```bash
python -m venv .venv && .venv/bin/pip install -e . pytest
.venv/bin/python -m pytest tests/   # mocked HTTP only; no network
```
