# ShopDB - guide for an AI agent

ShopDB is a plugin-based asset-management system (PCs, printers, machines,
network devices, measuring tools, applications, knowledge base, USB,
warranties, backups and more) for manufacturing sites. Every site runs its
own instance.

This file is the machine entry point, served at `/api/docs/llms.txt`. It covers
the API, which is what most callers need, and routes everything else.

## Routing - read this before opening any document

`docs/INDEX.md` is the routing table. Rows are TASKS, each row gives the
smallest set of pages that answers it, and each row says how many lines that
costs. Find the row, read those pages, skip the rest. The corpus is far larger
than any one context window and almost none of it applies to a given question.

Every page opens with an HTML comment naming `purpose`
(tutorial/howto/reference/explanation), `audience`, `status` and a one-sentence
`summary`, plus `loadwith` for the pages it is not complete without. Filter on
that metadata before opening anything: `status: legacy` means true but
superseded (do not follow it for new work) and `status: historical` means a
finished migration or a proposal kept as a record, so neither is an answer to
"how do I do this today".

`docs/START-HERE.md` is the same routing for a human, and shorter.

Not an API question? Still start from the index rather than guessing at a
filename: its rows cover installing and running a site, loading its data, the
shop-floor PCs, and building on the platform. For "why is it built this way",
`docs/adr/README.md` says which decision governs the question and whether it
still holds. For writing a plugin, `docs/PLUGIN-HOOKS.md` is the contract and
everything else defers to it.

## Answering "how do I install or fix this server"

The people running these servers are often not Windows, IIS or Python
specialists. `docs/INSTALL-WINDOWS.md` (a new site, one offline installer
`.exe`) and `docs/OPERATE-WINDOWS.md` (restart, logs, backups, health) are
authoritative, and both ship in `docs/` inside the install directory on every
installed server.

Do NOT walk anyone through `docs/INSTALL-WINDOWS-IIS.md` for a new site. It is
the manual procedure kept for servers that predate the installer, and following
it produces a server the installer then refuses to upgrade.

Before diagnosing anything, ask for the output of `shopdb-admin.ps1 check -Json`
from the install directory (default `C:\shopdb-flask`). It reports version,
publishing method, IIS and pool state, HTTP reachability, database host and
reachability, Python version, installed plugins and errors, and it contains no
secrets. The other day-2 verbs are `status`, `start`, `stop`, `restart`, `logs`,
`open`, `backup`, `repair`, `catalog`, `sessions`, `plugins`, `add-plugin`,
`verify`, `uninstall`. `verify -Path <name>` answers "does this server carry
component X" from the on-box CycloneDX SBOM.

Python is 3.14 and the wheelhouse is locked to it; an upgrade against a venv
built by a different minor version is refused by design.

## Bulk-loading a site's data

Two routes, and the right one depends on what the site has:

- SPREADSHEET, no developer (the common case): `flask csv templates --out <dir>`
  generates templates FROM THE LIVE SCHEMA, then `flask csv import --dir <dir>`
  checks and `--commit` applies. Do NOT hand-write the templates - generate
  them. `docs/CSV-IMPORT.md`.
- A SOURCE DATABASE to script against: the HTTP import API,
  `docs/IMPORT-API.md`, with `docs/IMPORT-ADOPTION.md` for the loader around it.

User accounts are deliberately not CSV-importable.

## Base URL

Each site runs its own instance (ADR-004), so there is no single production URL:
it is whatever that site serves, e.g. `https://shopdb.example.net/shopdb`.
All API paths are under `/api` (e.g. `<base>/api/assets`). Dev: `http://localhost:5001`.

## Auth

Three schemes:

- **Bearer JWT** - most endpoints. Get one by logging in, or use a managed
  Personal Access Token (PAT). Send `Authorization: Bearer <token>`.
  - Login: `POST /api/auth/login` `{ "username": "...", "password": "..." }`
    -> `data.access_token`. Refresh: `POST /api/auth/refresh`.
  - PATs are minted in the UI (Settings > API Tokens); a *scoped* PAT is limited
    to named permissions and suspends the admin bypass.
- **X-API-Key** - unattended/service endpoints (collector ingest, GE-Enforce
  fetch). Send `X-API-Key: <managed-token>`. How a shop-floor PC gets its own
  credential is `docs/COLLECTOR-AUTH.md`.
- **Public** - some read endpoints (e.g. printer install-list, employee search,
  dashboards) need no auth.

Auth level per endpoint is in the OpenAPI `security` field: `bearerAuth`,
`apiKeyAuth`, or none. Admin-only and permission-gated routes both use bearer.

## Response envelope

JSON endpoints return `{ "status": "success", "data": <payload>, "meta": {...} }`.
Errors nest under `data`, NOT at the top level:
`{ "status": "error", "data": { "error": { "code": "...", "message": "...", "details": {...} } } }`
with an HTTP 4xx/5xx. Reading `message` or `code` off the root gives `undefined`.
Lists carry `meta.pagination.{page, perpage, total, pages}` - again nested, not
`meta.total`. A few feed endpoints (screensaver, some installer text formats)
return raw text/JSON without the envelope - noted per route.

## Common recipes

- Search everything: `GET /api/search?q=<term>` (multi-word = AND across words).
- List assets on the map: `GET /api/assets/map`.
- List a type: `GET /api/printers`, `/api/computers`, `/api/machines`,
  `/api/network`, `/api/measuringtools` (paginated: `?page=&perpage=`). A
  plugin that is disabled at that site serves none of its routes.
- Get one: `GET /api/printers/<id>` etc.
- Create (bearer): `POST /api/printers` `{assetnumber, windowsname, vendorid, ...}`.
- Reports: `GET /api/reports` (list), `GET /api/reports/pc-relationships` (PC<->machine).
- Printer installer data: `GET /api/printers/install-list` (public; add
  `?format=text` for a pipe-delimited variant); `GET /api/printers/pc-default?machine=<n>`.
- Collector ingest (X-API-Key): `POST /api/collector/computers`.
- GE-Enforce: `GET /api/geenforce/manifest?pctype=<scope>`,
  `GET /api/geenforce/payload/<sha256>`, `POST /api/geenforce/report`.
- Import (admin PAT, preserves timestamps with `X-Import-Mode`): `docs/IMPORT-API.md`.

## Conventions

- DB-mirrored params/fields use lowercase concatenated names (no underscores):
  `locationid`, `vendorid`, `windowsname` - match them exactly.
- IDs in paths are integers.
- Plugin endpoints live under the plugin's prefix (`/api/<plugin>/...`).

## Full reference

- Machine spec: `GET /api/docs/openapi.json` (OpenAPI 3.1). Operation and
  plugin counts live in `docs/PROJECT-MAP.md`, which is generated.
- Interactive: `GET /api/docs` (Redoc).
- Human reference: the same spec at `GET /api/docs`, and `docs/INDEX.md` for everything else.
