# @adia-ai/a2ui-corpus

Corpus and operational-learning artifacts for the gen-UI pipeline —
patterns, fragments, compositions, exemplars, feedback, gaps, eval
fixtures. Pure data plus the scripts that maintain it. No runtime.

> The pipeline reads this package; this package never reads the pipeline.
> See [`@adia-ai/a2ui-compose`](../compose) for engine code,
> [`@adia-ai/web-components`](../../web-components) for UI atoms,
> [`@adia-ai/a2ui-utils`](../utils) for the A2UI runtime (renderer,
> registry, streams, wiring),
> [`@adia-ai/a2ui-mcp`](../mcp) for the MCP server.

## Dependency direction

```
a2ui-compose   ──reads──▶  a2ui-corpus
a2ui-mcp       ──reads──▶  a2ui-compose, a2ui-corpus
web-components ◀─reads──   a2ui-corpus (exemplars use AdiaUI tags)
```

No back-writes. No circular reads. Web-components ships UI atoms only;
corpus lives here; runtime lives in a2ui-compose.

## Layout

```
a2ui/corpus/
├── patterns/                ~97 full-canvas A2UI templates (retrieval corpus)
│   ├── <domain>/<name>.json   agent · data · forms · layout · navigation · settings
│   ├── _components.json       canonical name + aliases map (Carousel → Swiper)
│   └── _taxonomy.json         concept synonyms (auth → [login, signup, otp])
│
├── fragments/               atomic shapes — building blocks for Zettel
│   ├── action/                  button rows, toolbars, inline CTAs
│   ├── content/                 stat cards, list items, media blocks
│   ├── form/                    field, field-row, field-group recipes
│   └── layout/                  section, grid, card scaffolding
│
├── compositions/            named multi-fragment recipes
│   ├── agent/ data/ form/ layout/ navigation/ settings/
│                              each = pre-assembled fragment set with keywords + tags
│
├── exemplars/               hand-authored HTML sources
│   ├── ui/                      component-level exemplars
│   └── prose/                   prose + mixed-content exemplars
│                              extract.js compiles these → chunks/ + catalog
│
├── evals/                   held-out.jsonl + eval fixtures
├── chunks/                  gen-UI training chunks (702 unique, 852 instances)
│                              — harvested from data-chunk markers across
│                              site/pages/* + corpus exemplars (since 0.0.3).
│                              Powers compose_from_chunks in @adia-ai/a2ui-mcp.
├── pages/                   extracted full-page A2UI   (build output)
├── feedback/                daily JSONL — user feedback events
├── gaps/                    gap registry — prompts with missing coverage
├── scripts/                 maintenance tooling (extract, ingest, feedback, ticket)
│   └── chunk-library.js       in-memory loader + keyword/semantic search over chunks/
│
├── catalog-a2ui_0_9.json       aggregated artifact — what a2ui-compose + a2ui-mcp read
├── catalog-a2ui_0_9_rules.txt  natural-language composition rules (per component)
├── common_types.json           shared A2UI type shapes
├── pattern-embeddings.json     pre-computed embeddings for pattern semantic search
├── chunk-embeddings.json       pre-computed embeddings for chunk semantic search (0.0.4+)
├── functions.json              declarative wiring-engine function catalog
├── manifest.json               extraction metadata (what / when / counts)
├── pattern-specs.md            written specs for each pattern category
└── data-flow.md                how the 4 signal sources feed the pipeline
```

## What's committed vs generated vs published

| Kind                       | Committed | Published to npm | Source of truth                        |
|----------------------------|:---------:|:----------------:|----------------------------------------|
| Patterns (`patterns/`)     | ✓         | ✓                | Hand-authored JSON                     |
| Fragments/compositions     | ✓         | ✓                | Hand-authored JSON                     |
| Exemplars (`exemplars/`)   | ✓         | ✓                | Hand-authored HTML                     |
| Extracted chunks/pages     | ✓         | ✓                | `scripts/extract.js` (from exemplars)  |
| `catalog-a2ui_0_9.json`    | ✓         | ✓                | `scripts/build-catalog.js` (aggregated)|
| `chunk-embeddings.json`    | ✓         | **✗ since 0.2.1**| `scripts/build/embeddings-chunks.mjs`  |
| `pattern-embeddings.json`  | ✓         | **✗ since 0.2.1**| `scripts/build/embeddings.mjs`         |
| Feedback JSONL             | ✓         | ✓                | Written by `@adia-ai/a2ui-retrieval` at runtime |
| Gap registry               | ✓         | ✓                | Written by `@adia-ai/a2ui-retrieval` at runtime |

Extracted artifacts are committed for convenience (avoids a build step to
read the pipeline), but the scripts are authoritative — regenerate via
`npm run pipeline` if anything drifts.

### Why embeddings ship via git, not npm

`chunk-embeddings.json` (~20 MB) and `pattern-embeddings.json` (~3 MB) are
**committed in git** so the monorepo's own pipeline runs without a network
round-trip, but **excluded from the published npm tarball** — every
`npm i @adia-ai/a2ui-corpus` was pulling 23 MB of pre-computed float
arrays that consumers had no reliable way to address (the
`chunk-embedding-retriever` resolves them via a relative-path that breaks
under a `node_modules/@adia-ai/a2ui-corpus/` install layout).

Consumers who want embedding-based retrieval either:

1. Regenerate locally — `npm run build:embeddings:all` produces both
   files in your `node_modules/@adia-ai/a2ui-corpus/` checkout. Requires
   API access to your embedding provider.
2. Use the keyword-only fallback — `chunk-library.searchChunks()` and
   `pattern-library.searchPatterns()` work without embeddings; the
   embedding-aware `searchChunksAsync` / `searchPatternsAsync` paths
   fall through to keyword scoring when the index file is absent
   (`chunk-embedding-retriever.js` returns `null` gracefully).

## Scripts

All run from repo root via npm:

```bash
npm run pipeline             # extract + ingest (full refresh of chunks/ + catalog)
npm run pipeline:stats       # report counts without writing
npm run build:catalog        # aggregate patterns/fragments/compositions → catalog
npm run build:catalog -- --verify   # fail if catalog is stale vs source

npm run feedback:report      # human-readable feedback digest
npm run feedback:promote     # promote high-confidence feedback → new patterns

npm run ticket               # open ticket tracker
npm run ticket:list          # list open tickets
npm run ticket:create        # create a ticket against corpus/pipeline
```

Script inventory (`scripts/`):

| Script                    | Purpose                                              |
|---------------------------|------------------------------------------------------|
| `extract.js`              | Exemplar HTML → A2UI chunks & pages                  |
| `ingest.js`               | Index extracted chunks into the retrieval corpus     |
| `run-pipeline.mjs`        | Orchestrates harvest → extract → ingest → index      |
| `build-pattern-index.mjs` | Computes the keyword inverted index (`patterns/_index.json`) |
| `chunk-library.js`        | In-memory loader + keyword/semantic search over `chunks/` |
| `feedback-report.js`      | Aggregates feedback JSONL into a readable digest     |
| `feedback-promote.js`     | Moves high-confidence feedback into the pattern lib  |
| `ticket.mjs`              | Corpus/pipeline issue tracker                        |

Repo-side build scripts (not in tarball; run from the workspace root):

| Script                              | Purpose                                                  |
|-------------------------------------|----------------------------------------------------------|
| `npm run harvest:chunks`            | Walks site/pages/* + corpus exemplars, harvests every `[data-chunk]` element, writes `chunks/<name>.json` + `_index.json` |
| `npm run build:embeddings`          | Generates `pattern-embeddings.json` (113 patterns × 1536d) |
| `npm run build:embeddings:chunks`   | Generates `chunk-embeddings.json` (702 chunks × 1536d)   |
| `npm run build:embeddings:all`      | Both embedding indices in sequence                       |

## Exports

```javascript
// Catalog — the aggregated read-target for engines
import catalog from '@adia-ai/a2ui-corpus';

// Alias map + taxonomy
import aliases from '@adia-ai/a2ui-corpus/patterns';

// Held-out eval fixture
import fixture from '@adia-ai/a2ui-corpus/evals/held-out.jsonl?raw';

// Exemplar source (for scripts + docs)
import html from '@adia-ai/a2ui-corpus/exemplars/ui/pricing-tiers.html?raw';

// Chunk corpus (since 0.0.3 / 0.0.4)
import chunkIndex from '@adia-ai/a2ui-corpus/chunks';   // _index.json (metadata only)
import { searchChunks, searchChunksAsync, getChunk }
  from '@adia-ai/a2ui-corpus/chunk-library';            // in-memory query API
```

## Authoring order — demo page → exemplar → training

When adding coverage for a new intent:

1. **Site demo page** — author the HTML in `site/pages/examples/**`
   using pure primitive composition (no `<style>` block, no inline
   styles, only real components). See repo-root `AGENTS.md`.
2. **Exemplar** — copy the working markup into
   `a2ui/corpus/exemplars/ui/<name>.html`. Strip non-essential chrome;
   keep attributes that encode intent.
3. **Extract + ingest** — `npm run pipeline`. The scripts write
   `chunks/`, `pages/`, and refresh the catalog.
4. **Verify** — `npm run eval:diff -- --engine zettel` should still hold
   coverage 100%, avgScore ≥ 88. Regression thresholds in `AGENTS.md`.

See `data-flow.md` for the full 4-signal pipeline (exemplars →
fragments → compositions → feedback).

## Regression floors

The pipeline must hold these thresholds — tracked in the held-out
benchmark:

- **Fragment reuse ratio ≥ 29.9%** — 167 refs / 559 composition nodes
- **Zettel**: coverage 100%, avgScore ≥ 88, MRR ≥ 0.94
- **Monolithic**: coverage 100%, avgScore ≥ 95
- **Dogfood**: 20/20 intents at avg ≥ 95

## What this package does NOT contain

- Pipeline runtime — `gen-ui/`
- UI custom elements — `web-components/`
- MCP transport — `gen-ui-mcp/`
- Site / playground UI — `/site/`

If a file here is `.js` / `.mjs`, it's a maintenance script, not runtime.
Runtime readers go through `gen-ui/retrieval/*`.

## License

MIT
