Skip to content

Architecture

Vitor edited this page Dec 3, 2026 · 3 revisions

botticelli

Architecture

TAB. I — Architectura

Status: the modules core, sources/pinterest, pipeline, library, cache and the MCP server src/server/ are built (M0 to M3b). Boxes with a dashed border below are planned.

In one minute

Every source returns the same Ref shape. A source-agnostic pipeline filters, dedupes, colours and ranks the refs. A library saves the keepers to disk. A server puts an MCP tool in front of all of it, and wires in the cache. Pinterest has no public search API, so the Pinterest adapter is built to survive change: endpoints are data, and pins are found by shape.

botticelli has one rule: the pipeline, the library and the server only ever see Refs and Sources. Where a Ref came from does not matter after the adapter returns it (ADR 0001).

Module map

flowchart LR
  subgraph sources["src/sources"]
    pin["pinterest (built, M1)"]
    arena["arena (planned, M6)"]
  end
  subgraph pipeline["src/pipeline (built, M2)"]
    filters["filters"] --> dedupe["dedupe (dhash)"] --> pal["palette"] --> rank["rank"] --> sheet["sheet"]
  end
  subgraph storage["storage"]
    lib["src/library (built, M3a)"]
    cache["src/cache (built, M3a)"]
  end
  server["src/server: MCP tools, handles (built, M3b)"]

  pin --> filters
  arena -.-> filters
  rank --> lib
  sheet --> server
  lib --> server
  cache --- server
  server --> pin
  server -.-> arena

  style arena stroke-dasharray: 5 5
Loading
Module Path Role Status
core src/core/ Ref, Page, Capabilities, the error classes built (M0)
sources src/sources/ The Source interface and one folder per adapter interface built (M0), Pinterest built (M1)
pipeline src/pipeline/ Filters, dedupe, palette, rank, sheet, pure-JS image code built (M2)
library src/library/ Saved refs on disk, sidecars, index.json, path safety built (M3a)
cache src/cache/ Disk cache with TTL and a size cap built (M3a). Wired by the server (M3b)
server src/server/ Eight MCP tools, session handles, the config file, the stdio entry built (M3b). visual_search and health are planned

The server wires the disk cache. Search pages are cached for 24 hours and thumbnails for 7 days (Library and moodboards). The Pinterest source itself does not use the cache. The Moodboard page is written by src/library/moodboard.ts and is part of the library module.

Inside src/server/: config.ts reads botticelli.config.json, handles.ts keeps the Handles of one session, context.ts holds the shared state and the cached calls, tools/read.ts has the six read-only tools, tools/library.ts has save and moodboard, server.ts builds the MCP server from a ServerContext (createBotticelliServer), and index.ts is the stdio entry and the composition root where Sources are registered (Writing a Source). The server only sees Refs and Sources. It has no Pinterest-specific code.

The Source interface

A Source is one place Refs come from. Each adapter lives in src/sources/<id>/ and implements Source from src/sources/types.ts:

export interface Source {
  readonly id: SourceId;
  capabilities(): Capabilities;

  search(query: string, opts?: PageOptions): Promise<Page>;
  related?(ref: string, opts?: PageOptions): Promise<Page>;
  collection?(url: string, opts?: PageOptions): Promise<CollectionPage>;
  get?(ref: string): Promise<Ref>;
  visualSearch?(imagePath: string, opts?: PageOptions): Promise<Page>;
  fetchImage(url: string, size: ImageSize): Promise<FetchedImage>;
}

Rules:

  • search and fetchImage are required. The rest are optional.
  • capabilities() says what the Source can do right now. It can change with login.
  • An optional method is left out when the adapter has no implementation. It throws CapabilityError when the implementation exists but is unavailable now (for example, visual search while logged out).
  • fetchImage only accepts the Source's own CDN hosts. Anything else throws RefusedError. Sizes are thumb, medium, large and original. The adapter maps each onto its CDN renditions and falls back to the nearest one.
  • get and related mark a RefusedError as final when the input is one of the Source's own pages that is not a ref (a board URL, a short link that leads nowhere). The server then reports that reason. A non-final refusal of an http(s) URL lets the server try the input as an image URL, which fetchImage accepts or refuses.
  • Pages carry an opaque next cursor. A caller passes it back as cursor. It is never parsed outside the adapter.

Capabilities

A Capability is one of search, related, collection, get, visualSearch. The Pinterest adapter reports this today:

{ search: true, related: true, collection: true, get: true, visualSearch: false }

visualSearch stays false until M5 shows the logged-in probe works (ADR 0004). See Sources for the full table.

Errors

Class Meaning
CapabilityError The Source cannot do this right now
SourceError The Source answered with an error for one request. Has an optional HTTP-like status
NetworkError The host could not be reached at all (DNS, TCP, TLS, timeout, a stalled body)
RefusedError An input was refused for safety: a path outside the Library, an unknown CDN host, a bad cursor. final: true means the input is the Source's own page and must not be retried as an image URL

How a search flows

sequenceDiagram
  autonumber
  participant A as Agent
  participant T as MCP tool search (built)
  participant S as Source.search (built)
  participant R as Endpoint registry (built)
  participant X as Transport (built)
  participant E as extractRefs (built)
  participant P as Pipeline (built)
  A->>T: search(query, filters, sheet)
  T->>S: search(query, limit, cursor)
  S->>R: candidates("search")
  R-->>S: ordered Candidates
  S->>X: GET /resource/BaseSearchResource/get/
  X-->>S: JSON (or an error)
  S->>E: extractRefs(body)
  E-->>S: Refs and raw item count
  S-->>T: Page of Refs, next cursor
  T->>P: thumbnails, dhash, filters, dedupe, palette, rank
  P-->>T: kept Refs, dropped with reasons
  T->>T: assign handles (#1, #2 ...)
  T->>P: renderSheet(tiles)
  P-->>T: one JPEG
  T-->>A: numbered Refs and the sheet
Loading

Every step is built. The search tool in src/server/tools/read.ts chains them. Search pages come from the disk cache for 24 hours, and thumbnails for 7 days, so a repeat query does not touch the network. At most 6 thumbnails are fetched at once.

The pipeline

TAB. II — Cribrum

The Pipeline is source-agnostic. Every step is a pure function of its input. It works on thumbnails, never on originals (ADR 0003).

Step Function What it does
Image code decode, resize, encodeJpeg JPEG and PNG in pure JS. Refuses images over 4096 px a side
Filters applyFilters Drops refs and says why. See below
Dedupe dhash, hamming, dedupe 64-bit difference hash of the thumbnail. A Repost is a ref within 6 bits of one already kept
Palette palette, mergePalettes Up to 5 colours by k-means in L*a*b* over at most 64×64 pixels. mergePalettes builds one palette for a set
Rank rank, score Stable sort: known origin +2, low-res −3, log10(saves+1), width up to 1600
Sheet renderSheet One JPEG grid with a number in each cell's corner. At most 36 tiles, 1568 px wide by default

Filter order is: blocked domain, media, low-res, minimum width, orientation. The first failing filter gives the reason. The default blocked domains are shops and stock sites (Payhip, Etsy, Creative Market, Envato, Gumroad, Canva, Shutterstock and similar). Read the list in src/pipeline/filters.ts.

Pinterest resilience layers

Pinterest has no public search API, so botticelli reads the same JSON resources the website uses (ADR 0002). Those change without notice. Four layers deal with it. The first two are built.

flowchart TD
  call["A call: search, related, pin, board"] --> L1
  subgraph L1["Layer 1: endpoint registry (built, M1)"]
    c1["Candidate 1"] -->|fails| c2["Candidate 2"] -->|fails| c3["..."]
  end
  L1 --> L2["Layer 2: shape-based extraction (built, M1)"]
  L1 -.->|every Candidate failed| L3["Layer 3: Hunter (planned, M4)"]
  L3 -.->|new Candidate, validated| L1
  L4["Layer 4: health check (planned, M4)"] -.->|a Capability fails| L3
  L2 --> refs["Refs"]
  style L3 stroke-dasharray: 5 5
  style L4 stroke-dasharray: 5 5
Loading
  1. Endpoints as data. Each Capability has an ordered list of Candidates in endpoints.default.json. If one fails, the next is tried. The one that worked is promoted and saved.
  2. Shape-based extraction. Pins are found by what they look like, not by a fixed path in the JSON.
  3. Hunter (planned, M4). Opens the real site in a headless browser, sniffs which resource it uses now, validates it without the browser and promotes it.
  4. Health (planned, M4). Checks every Capability of every Source. A failure triggers the Hunter.

Details and the exact rules are in Resilience.

Worked example: one ref through the system

A logged-out search for brutalist poster typography returns a pin object. Here is what happens to it.

  1. extractRefs finds the object because it has an id and an images entry on i.pinimg.com. It drops promoted pins and keeps the rest.
  2. toRef in src/sources/pinterest/extract.ts makes a Ref: source: "pinterest", imageUrl from the largest image, lowRes if it is under 500 px wide, sourceDomain from the pin's domain. The id is anonymous (e1_…), so key is a hash of the image URL and id is absent.
  3. applyFilters checks the domain against the blocked list, then media, low-res, width and orientation. A pin whose sourceDomain is etsy.com is dropped with the reason blocked domain: etsy.com.
  4. fetchImage(url, 'thumb') downloads a 236 px rendition. decode turns it into pixels.
  5. dhash gives a 16-character hash. dedupe compares it to the refs already kept. A hash within 6 bits is a Repost and is dropped, with a pointer to the ref it copies.
  6. palette gives up to 5 colours. rank scores the ref.
  7. renderSheet draws the thumbnail in a numbered cell.

A runnable version of steps 4 to 7 is in Getting started.

Troubleshooting / FAQ

Why one Ref shape for every source? So the pipeline, library and server never need to know the source. A source-specific field that does not fit stays out until a second source needs it (ADR 0001).

Why no sharp or canvas? They are native modules and break across Node versions and platforms. Thumbnails are small, so pure JS is fast enough (ADR 0003).

Why does the pipeline use thumbnails? A 236 px thumbnail is enough for a dhash, a palette and a sheet cell. It keeps memory and time low.

Can a Source skip related? Yes. Leave the method out. The tool answers with a clear error that names the Source and the Capability.

Where do I add a new Source? In src/sources/<id>/. See Writing a Source and CONTRIBUTING.md.

Clone this wiki locally