Metadata-Version: 2.5
Name: agentic-platform-standin
Version: 0.1.0
Summary: A stand-in for the Agentic Platform /open/v1 API, for app development and SDK conformance tests
Project-URL: Python SDK, https://pypi.org/project/agentic-platform-sdk/
Author: Agentic Platform contributors
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: agentic-platform,mock,open-api,stand-in,testing
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.11
Requires-Dist: fastapi>=0.115
Requires-Dist: httpx>=0.27
Requires-Dist: jieba>=0.42
Requires-Dist: numpy>=1.26
Requires-Dist: onnx==1.19.1
Requires-Dist: onnxruntime==1.27.0
Requires-Dist: pillow>=10
Requires-Dist: psycopg[binary]>=3.2
Requires-Dist: pyjwt[crypto]>=2.9
Requires-Dist: python-multipart>=0.0.9
Requires-Dist: sqlalchemy>=2.0.30
Requires-Dist: uvicorn[standard]>=0.30
Provides-Extra: dev
Requires-Dist: pymupdf>=1.24; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff==0.16.10; extra == 'dev'
Provides-Extra: embedder
Requires-Dist: sentence-transformers>=3.0; extra == 'embedder'
Provides-Extra: pdf
Requires-Dist: pymupdf>=1.24; extra == 'pdf'
Provides-Extra: vision-torch
Requires-Dist: torch==2.8.0; extra == 'vision-torch'
Requires-Dist: torchvision==0.23.0; extra == 'vision-torch'
Description-Content-Type: text/markdown

# agentic-platform-standin

A small FastAPI imitation of the platform's `/open/v1` API: `me`, chat completions, models and model aliases,
knowledge bases, ontologies, image-classification training, datasets, toolchains and their tasks, A2A agents
(card and JSON-RPC with SSE) and the OpenAPI document. Every operation of the platform's OpenAPI contract is served under
the platform's `operationId`, except `createA2ATaskFlat` (retired); tests in the source repository keep it so. Apps develop against it, and the SDK's conformance runner tests
against it. It is not the platform: it checks tokens the way the platform does (RS256 via JWKS, `iss`, `aud`, `azp`)
but keeps its own SQLite (or Postgres) state.

The platform backend never imports it.

## Install and test

From PyPI: `pip install agentic-platform-standin` (add `[embedder]` for the bge embedder, `[pdf]` to index PDF files), then
`python -m agentic_platform_standin`. From a checkout:

```
python3.11 -m venv .venv && .venv/bin/pip install -e '.[dev]'
.venv/bin/pytest -q            # deselects the torch tests by default
```

Extras:

- `[embedder]`: `sentence-transformers` (pulls in torch), for the BAAI/bge embedder. Without it, set
  `STANDIN_EMBEDDER=hash`.
- `[pdf]`: PyMuPDF, to index PDF files in a knowledge base. PyMuPDF is AGPL-3.0 (or a commercial licence from its
  vendor), so it is not a hard dependency of this Apache-2.0 package. Without it a PDF upload ends `failed`
  (`knowledge_base.index_failed`) and the log names the extra.
- `[vision-torch]`: `torch` and `torchvision`, for image-classification training. Run those tests with
  `pytest -m torch`.

## Run

```
STANDIN_ISSUER=... STANDIN_JWKS_URL=... STANDIN_EMBEDDER=hash .venv/bin/python -m agentic_platform_standin
```

Serves on `STANDIN_HOST`:`STANDIN_PORT` (default `0.0.0.0:8013`).

| Variable | Default | |
|---|---|---|
| `STANDIN_ISSUER`, `STANDIN_JWKS_URL` | required | token issuer and its JWKS |
| `STANDIN_AUDIENCE` | `agentic-platform` | `aud` |
| `STANDIN_APP_CLIENT` | `example-app` | comma-separated allowed `azp` values; the first is the `owner_app` of what is created |
| `STANDIN_MEMBERS` | `alice@example.test:admin,bob@example.test:member` | `email:role` pairs |
| `STANDIN_WORKSPACE_ID`, `STANDIN_WORKSPACE_NAME` | a fixed UUID (`DEFAULT_WORKSPACE_ID` in `agentic_platform_standin/settings.py`), `Example workspace` | the one workspace; the platform's ids are UUIDs (`OpenMe.workspace_id`) |
| `STANDIN_DATABASE_URL` | `sqlite:///./var/standin.db` | |
| `STANDIN_OLLAMA_URL`, `STANDIN_DEFAULT_MODEL`, `STANDIN_ALIASES` | see `agentic_platform_standin/settings.py` | chat backend and model aliases |
| `STANDIN_EMBEDDER` | `bge` | `hash` for a dependency-free embedder |
| `STANDIN_VISION_DIR` | `./var/vision` | datasets and trained models |
| `STANDIN_VISION_TRAINER` | `scripted` | `scripted` or `torch`, see "Training" |
| `STANDIN_VISION_SCRIPTED_S` | `0` | seconds a scripted job takes |
| `STANDIN_API_KEYS` | empty | `agk_...=email[@app][!dpop]` pairs, comma-separated, see "API keys and DPoP" |
| `STANDIN_DPOP_NONCE` | off | `1`: a proof without a server nonce is challenged once |
| `STANDIN_DPOP_REQUIRED` | off | `1`: an unbound key is refused (`gateway.dpop_binding_required`) |
| `STANDIN_PUBLIC_URL` | empty | the origin a DPoP `htu` and an A2A card URL use (default: the request's own) |
| `STANDIN_CONTROL` | off | `1`: serve the `/_standin/` control API and record requests |
| `STANDIN_OPENAPI_PATH` | the packaged `agentic_platform_standin/openapi.json` | the file `GET /open/v1/openapi.json` serves; the package carries a copy of the platform's OpenAPI contract |
| `STANDIN_HOST` | `0.0.0.0` | the interface `python -m agentic_platform_standin` binds; the image and compose need every interface. `run_standin()` sets `127.0.0.1` (its `env` can override it) |
| `STANDIN_PORT` | `8013` | |

The full list is in `agentic_platform_standin/settings.py`.

## Owner app

The owner app of what a caller creates is the token's `azp`, or an API key's app. Another app can read an
ontology or a knowledge base, and gets 403 `ontology.owned_by_other_app` / `knowledge_base.owned_by_other_app` when
it writes to it.

## API keys and DPoP

`STANDIN_API_KEYS="agk_x=alice@example.test,agk_y=bob@example.test@other-app!dpop"`. The email must be a member
(`STANDIN_MEMBERS`). An optional second `@app` segment is the key's app (default: the first `STANDIN_APP_CLIENT`);
a `!dpop` suffix binds the key to a DPoP proof key, set by the first proof it sends. `Authorization: Bearer agk_...`
and `DPoP agk_...` are both accepted, next to the JWT path. `/me` for a key answers `client_id` set, `user` null,
`workspaces` empty.

A proof is checked as the platform checks it: `typ`, ES256, the signature by the header `jwk`, `htm` / `htu`, `iat`
within 60 s, a `jti` not seen in 5 minutes, `ath` of the key. Every refusal is 401: `gateway.dpop_proof_required`,
`gateway.dpop_proof_invalid`, `gateway.dpop_binding_required`, `gateway.dpop_nonce_required` (with a `DPoP-Nonce`
header, and on every 401 in nonce mode).

## Datasets, toolchains, A2A

All three live in memory and start empty: seed them with the control API. The stand-in runs nothing.

- A seeded **toolchain** has a `behavior`: `succeed` (default, the task is `queued` when filed and `completed` from
  its first read on), `fail` (ends `failed` with `toolchain_task.shard_failed`; a retry completes) or `hold`
  (`running` until cancelled). A `callback_url` is validated (https) and echoed, never delivered.
- The **A2A agent** (seed `{"kind": "agent", "name": "agent-echo"}`) echoes the text it is sent. JSON-RPC is the 1.0
  wire only: a missing, empty or 0.3 `A2A-Version` answers -32601 to the 1.0 method names, as the platform does, but
  the 0.3 wire itself is not imitated; the card route serves the 1.0 card for every supported version. Not
  imitated: push notifications, history, tenants, task continuation (`taskId` is -32004).
  With the chat-events extension the scripted run streams its answer as `answer` chunks (the first replaces, the next
  append, an empty closing chunk is logged) and ends with the derived `response` artifact and terminal status.
  A task sent with `returnImmediately` stays working until its first `GetTask` (completes) or a `CancelTask`.

## Training

`scripted` (default) completes a job after `STANDIN_VISION_SCRIPTED_S` seconds with a fixed tiny ONNX model (a
per-channel mean and a fixed linear map, built with `onnx` at run time) and perfect scripted metrics. It needs no
torch and no weights. `torch` trains for real, as before (the image has torch; tests that fake `runner._train` use it).

## Control API

Only with `STANDIN_CONTROL=1`, and never under `/open/`. Without the flag the routes do not exist (404).

| Call | |
|---|---|
| `POST /_standin/reset` | every row, in-memory object, stub and recorded request is dropped |
| `POST /_standin/seed {"kind", "name", ...}` | returns `{"id"}` |
| `POST /_standin/stubs {"match": {"operation"}, "times", "response": {"status", "headers", "body"}}` | answers that operation instead of handling it |
| `POST /_standin/redirect {"operation", "times"}` | answers that operation with a 302 |
| `GET /_standin/requests?since=<seq>` | `{seq, operation, method, path, headers, body}` of each `/open/` request |

Seed kinds: `knowledge_base`, `ontology` (`name`, `description?`, `owner_app?`); `dataset` (`name`, `format?`,
`num_samples?`, `size_bytes?`, `tags?`, `description?`; answers `{"id", "version_id"}`, the version is what a
toolchain task takes as input); `toolchain` (`name`, `id?`, `behavior?`, `params_schema?`, `version?`, `category?`,
`scenario?`, `applies_to?`, `shardable?`); `agent` (`name?`, `description?`, `published?`).

Stubs match by `operationId`. The stubs of one operation are consumed in the order they were registered, each
`times` times, then normal handling resumes. A stub answers before authentication. A string `body` is sent as text
(`text/plain` unless `headers` gives a content type), anything else as JSON, no body as an empty one.

Recorded requests have lower-case header names; the `authorization` and `dpop` values are `"<present>"`. `body` is
the parsed JSON body of a JSON request; a multipart or other body is recorded as `null`. Each record also has `query`.

## Image

```
docker build -t agentic-platform-standin:dev .
```

(run from a source checkout of this package). Installs CPU torch from the PyTorch index, then the package with `[embedder,pdf]` (which carries the OpenAPI
snapshot), and bakes the bge model and the ImageNet weights, so the container needs no network at runtime. Build args: `PIP_INDEX_URL`, `PIP_TRUSTED_HOST`, `TORCH_INDEX_URL`, `HF_ENDPOINT`.
