Metadata-Version: 2.4
Name: langchain-sealgraf
Version: 0.1.0
Summary: Sealgraf spend-approval tools for LangChain: 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: langchain,agents,spend-approval,sealgraf
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: langchain-core<2,>=1.8
Requires-Dist: httpx<0.29,>=0.27
Requires-Dist: pydantic<3,>=2
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Dynamic: license-file

# langchain-sealgraf

Sealgraf spend-approval tools for [LangChain](https://www.langchain.com/)
agents.

[Sealgraf](https://sealgraf.com) is the owner-approval layer for agent
spending: **the agent checks BEFORE buying → the owner approves above
their pre-set limits on Sealgraf's own surface → the completed purchase
is sealed into a receipt neither side can rewrite.** This package wraps
the full loop — including the token claim and the completion, which go
over Sealgraf's REST API — as four LangChain tools.

> Status: 0.1.0, built and unit-tested locally. Not yet published to
> PyPI.

## Install

```bash
pip install langchain-sealgraf
```

Depends only on `langchain-core`, `httpx`, and `pydantic` — not the
full `langchain` metapackage.

## Configure

The API key is an agent-scope Sealgraf key. It is host configuration,
never a tool argument (tool arguments are chosen by the model and land
in prompts and logs).

```bash
export SEALGRAF_API_KEY=<your agent-scope API key>
# optional:
export SEALGRAF_BASE_URL=https://sealgraf.com   # default
export SEALGRAF_TIMEOUT_SECONDS=20              # default
```

Or pass configuration in code:

```python
from langchain_sealgraf import get_sealgraf_tools

tools = get_sealgraf_tools(api_key="see_...", base_url="https://sealgraf.com")
# or hand in a pre-built client:
from langchain_sealgraf import SealgrafClient, get_sealgraf_tools
tools = get_sealgraf_tools(SealgrafClient("https://sealgraf.com", "see_..."))
```

## Use

```python
from langchain_sealgraf import get_sealgraf_tools

tools = get_sealgraf_tools()
# → [sealgraf_check_permission, sealgraf_claim_decision_token,
#    sealgraf_complete_purchase, sealgraf_get_receipt]

# Any LangChain agent that takes tools can run the loop:
agent = create_agent(model, tools=tools)  # LangChain v1 style
# or bind them to a chat model, a graph, or call them directly:
check = tools[0]
print(check.invoke({
    "category": "household",
    "amount_minor": 4250,          # $42.50, integer minor units
    "counterparty": "Lowe's",
    "description": "Trash bags",
}))
```

`SealgrafToolkit().get_tools()` returns the same four tools as a
bundle.

## The four tools

| Tool | Sealgraf operation | Purpose |
|---|---|---|
| `sealgraf_check_permission` | `POST /v1/preauthorize` | Ask permission BEFORE buying. Returns `allow` (with a single-use `decision_token`), `deny`, or `needs_human_approval`. |
| `sealgraf_claim_decision_token` | `POST /v1/preauthorize/{id}/decision-token` | After the owner approves, claim the single-use decision token. Fails with code `not_approved_yet` while the owner hasn't decided — wait and retry. |
| `sealgraf_complete_purchase` | `POST /v1/preauthorize/{id}/complete` | Report the finished purchase; Sealgraf seals it into a receipt. Retry-safe with the same `completion_nonce`. |
| `sealgraf_get_receipt` | `GET /v1/preauthorize/{id}` or `GET /v1/receipts/{id}` | Read a check's state or a sealed receipt. |

## Conventions

- **Outputs are JSON strings.** Each tool returns the parsed Sealgraf
  result serialized as JSON (per LangChain's string-return convention)
  with a stable shape: `outcome`, `state`, `preauth_id`,
  `decision_token`, `receipt_id`, `replayed`, ... A chain that needs
  the values can `json.loads` the output.
- **Failures raise.** Every failure — missing key, timeout, network
  error, non-2xx status, malformed response — raises `SealgrafError`
  (API failures raise `SealgrafApiError` with `.status_code`,
  `.detail`, and the server's stable `.code` when it sends one). The
  tools never return an error string: in a spend loop, a raised error
  fails the tool call outright, while an error-shaped string is one
  misreading away from a purchase. `needs_human_approval` is a normal
  outcome, not an error.
- **Fail closed.** A fresh `allow` without a decision token is treated
  as malformed and raises. An idempotent replay of a check returns the
  original record flagged `"replayed": true` with
  `"decision_token_redelivered": false` — Sealgraf never re-serves
  tokens, and a replay is never presented as a fresh permission.
- **Amounts are integer minor units** (cents): $42.50 is `4250`,
  everywhere.
- **Categories are a closed list** (Sealgraf's canonical 14:
  groceries, household, dining, travel, software, office_supplies,
  shopping, transport, entertainment, health, supplies, materials,
  equipment, fuel).
- **The key travels only in the `X-API-Key` header.** Decision tokens
  are passed through untouched and are never logged by this package.

## The spend loop

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

## Development

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

## License

MIT
