Metadata-Version: 2.4
Name: spm-client
Version: 0.2.0
Summary: SPM client: evidence-gated memory (ingest, recall, read, receipts) and the compatible Gateway.
Author: Veridical Tech, Inc.
License-Expression: LicenseRef-Proprietary
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: httpx<1,>=0.27
Provides-Extra: test
Requires-Dist: pytest<10,>=8; extra == "test"
Requires-Dist: respx<1,>=0.21; extra == "test"
Requires-Dist: httpx<1,>=0.27; extra == "test"
Provides-Extra: dev
Requires-Dist: pytest<10,>=8; extra == "dev"
Requires-Dist: pytest-asyncio<2,>=0.24; extra == "dev"
Requires-Dist: respx<1,>=0.21; extra == "dev"

# SPM Python client

Client for SPM's evidence-gated memory: store observations, recall with a
verdict, read the exact cited text, export, and subscribe to lifecycle events.
The OpenAI-compatible Gateway methods remain available at the bottom.

**Status**: pre-release (0.2.0). The licence is proprietary (see the
repository `LICENSE`); the package is safe to install and use, and the API
below is versioned with `/v2`.

## Install

```bash
pip install spm-client
# or, in this repository:
pip install -e sdk/python
```

## Memory

```python
from spm_client import SpmClient

with SpmClient("your-api-key", base_url="https://api.spmos.ai") as client:
    client.remember(
        "Acme 完成 A 轮融资 8000 万元",
        idempotency_key="acme-financing-2026-11",
        topic="financing",
        user_partition="ent-91330",
        source_timestamp="2026-11-30T00:00:00Z",
    )

    result = client.recall("Acme 最近融资多少", user_partition="ent-91330", topics=["financing"])
    if result["gate_reason"] == "passed":
        print(result["answer"])
        for ref in result["evidence_refs"]:
            print(ref["source_id"], ref["console_url"])
        print(client.read([r["read_token"] for r in result["evidence_refs"]])["spans"])
    else:
        print("no supporting memory:", result["gate_reason"])
```

`gate_reason` is `passed` or a refusal (`no_candidate`,
`no_semantic_support`, `superseded`, …). Treat a refusal as information, not
an error: it means no stored source supports the claim.

### Conventions

- `idempotency_key` (≥8 chars) makes a write safe to retry; the same key with
  a different payload is a conflict.
- Re-observing the same `source_id` with changed content increments its
  version instead of creating a second source.
- `user_partition` isolates an entity or end user; the key needs
  `memory:partition` for it.
- Read tokens live ~300 seconds — call `read` soon after `recall`.
- Limits: 12 000 chars per source, `top_k` ≤ 100, 500 items per batch, 32
  read tokens per call.

### Export and deletion

```python
bundle = client.export(user_partition="ent-91330")      # checksummed
client.import_bundle(bundle, user_partition="ent-copy") # ids are re-tagged
client.purge(source_id="s1")                            # signed receipt
client.wipe_partition("ent-91330")                      # per-source receipts
```

### Events

```python
sub = client.subscribe("https://hooks.example/spm", ["source.accepted", "source.purged"])
# Deliveries carry X-SPM-Signature: t=<ts>,v1=<hmac-sha256 of "ts.body">
client.replay_events(since_id="evt_...")
client.unsubscribe(sub["id"])
```

## Errors

```python
from spm_client import AuthError, NotFoundError, QuotaError, ServerError, SpmError

try:
    client.recall("q")
except AuthError:
    ...
except QuotaError:
    ...
except ServerError as exc:
    print(exc.status_code, exc)
```

All errors carry the server's `code` when it sent one.

## Gateway

`SpmClient` also speaks the OpenAI/Anthropic-compatible Gateway:

```python
with SpmClient("your-api-key") as client:
    reply = client.session().send("Hello, remember my name is Alice.")
    print(reply)
```

| Method | Description |
|---|---|
| `client.health()` | Gateway health |
| `client.chat(messages, model=..., stream=...)` | Raw chat completion |
| `client.session(system_prompt=...)` | Stateful `ChatSession` |
| `client.list_receipts()` | Request receipts |

## API reference (memory)

| Method | Endpoint |
|---|---|
| `remember(text, idempotency_key=..., …)` | `POST /v2/ingest` |
| `remember_many(items)` | `POST /v2/ingest/batch` |
| `recall(query, topics=..., since=..., until=..., …)` | `POST /v2/recall` |
| `recall_text(query, …)` | recall + read joined |
| `read(tokens)` | `POST /v2/read` |
| `purge(source_id=...)` | `POST /v2/purge` |
| `partitions()` / `export_partition(p)` / `wipe_partition(p)` | `/v2/partitions…` |
| `export()` / `import_bundle(bundle)` | `/v2/export`, `/v2/import` |
| `subscribe` / `subscriptions` / `unsubscribe` / `replay_events` | `/v2/events…` |
