Skip to content

refactor: inject AI / code-mode / MCP / storage as opt-in capabilities (bundle-time seams) - #66

Closed
jherr wants to merge 9 commits into
feat/edge-read-entryfrom
feat/capability-seams
Closed

jherr wants to merge 9 commits into
feat/edge-read-entryfrom
feat/capability-seams

Conversation

@jherr

@jherr jherr commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

What

Cracks four tight couplings in @tanstack/cms-core so AI, code-mode, MCP, and the embedded PGlite storage default are opt-in capabilities the app injects — the engine never imports them. Enforced at bundle time, which is the whole point: a dynamic import() isn't enough (a single-file bundler inlines it), so these are the seams that actually keep an edge/worker bundle from dragging in PGlite+WASM, QuickJS, @tanstack/ai, the MCP SDK, zod-v3, and sucrase.

Motivated by the Kody deploy that ballooned to 1.7 MB and ~40% cold-start crashes (see AGENTS.md, refs #63).

The seams

Capability Inject into defineCms Opt-in entry
Storage storage: createPgliteStorage (a StorageFactory) @tanstack/cms-store-postgres/pglite
AI agent tools ai: { runtime: aiRuntime() } @tanstack/cms-core/ai
Code-mode interfaces interfaces: { collection, runtime: quickjsInterfaceRuntime() } @tanstack/cms-core/interfaces-quickjs
WebMCP (browser) import startWebMcpServer @tanstack/cms-core/webmcp
HTTP MCP (server) createCmsApiHandler({ mcp: cmsMcp }) @tanstack/cms-api/mcp

Each missing capability throws a NOT_CONFIGURED CmsError naming the exact import to add — core never silently falls back to a package of its own.

Commits (5, each independently green)

  1. Storage — remove the auto-embedded-PGlite default; storage required (factory form); delete the async prime-then-sync-read dance; opt-in setDefaultStorageFactory + @tanstack/cms-store-postgres/testing vitest setup restore the embedded engine for tests; migrate all 25 in-repo configs.
  2. Interfaces/code-mode — inject the QuickJS + stripTypeScript runtime; interfaces.runtime required.
  3. AI — cmsTools reads ai.runtime; the @tanstack/ai loader moves out of core to @tanstack/cms-api.
  4. MCP — startWebMcpServer → core/webmcp; cmsMcp → api/mcp; /api/mcp is an explicit mount.
  5. Lock-in — bundle-seams.test.ts esbuilds each entry and asserts capability presence/absence via the metafile; AGENTS.md rule.

Verification

test:types (37), test:lib (24 projects — core 656, api 226, ui-react 146, base 42, prm 10, …), test:definitions (27/27), test:starters, all 10 example configs, bundle-seams 5/5, sherif clean. One pre-existing unrelated failure: react-onboarding:test:build (publint styles.css export — untouched here).

Notes

🤖 Generated with Claude Code

jherr and others added 5 commits December 2, 2026 16:01
…eam 1)

Core no longer dynamically imports @tanstack/cms-store-postgres/pglite as an
implicit fallback when a config omits `storage`. Instead `storage` is required
and accepts a `StorageFactory` the engine resolves once with the data dir:

    import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
    defineCms({ storage: createPgliteStorage })

A missing `storage` now throws a NOT_CONFIGURED CmsError naming the exact
import to add. Because core never references a storage package, a server
bundle that injects e.g. createPostgresStorage no longer drags PGlite+WASM
into its graph — the whole point of the seam.

- config: `storage?: StorageAdapter | StorageFactory`; new StorageFactory /
  StorageFactoryContext types.
- storage.ts: replace getDefaultAdapter/requireDefaultAdapter/ensureDefaultAdapter
  with a synchronous `baseAdapterFor`; drop the async prime-then-sync-read dance
  (simplifies the d6040b5 hazard away). Remove ensureDefaultAdapter call sites
  (transfer, content-migrations, base schema-registry).
- new opt-in `setDefaultStorageFactory` escape hatch (null by default, so
  production still errors and bundles stay clean) + `@tanstack/cms-store-postgres/testing`
  vitest setup that wires it to createPgliteStorage — restores the embedded
  engine for tests without editing every defineCms call site.
- migrate all 25 in-repo configs to inject `storage: createPgliteStorage` and
  add the dep; wire the test setup into core/api/client/cli/ui-react/base.

Green: core 656, api 226, client 37, ui-react 146, cli 27, base 42. sherif clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Interfaces no longer dynamic-import @tanstack/ai-isolate-quickjs or
@tanstack/ai-code-mode from inside the engine. `defineCms({ interfaces })`
now requires a `runtime` (type-enforced), and a new opt-in entry supplies the
default:

    import { quickjsInterfaceRuntime } from '@tanstack/cms-core/interfaces-quickjs'
    defineCms({ interfaces: { collection: 'interfaces', runtime: quickjsInterfaceRuntime() } })

Only that entry statically imports the QuickJS/code-mode (WASM) packages, so a
build that doesn't enable interfaces never pulls them. Missing runtime throws a
NOT_CONFIGURED CmsError naming the import.

- new InterfaceRuntime / IsolateDriver / IsolateContext / IsolateBinding
  structural types in the client-safe interfaces module (no peer imports).
- server/interfaces.ts reads config.interfaces.runtime; dynamic imports gone.
- new ./interfaces-quickjs entry (exports + tsdown); core already devDeps the
  two peers.
- api/interface-ops narrows the structural driver to code-mode's own type at
  the createCodeMode boundary.
- all 8 interface config sites inject quickjsInterfaceRuntime().

Green: core 656, api 226, ui-react 146, base 42. sherif clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…m 3)

`cmsTools` no longer loads @tanstack/ai itself. `defineCms({ ai })` takes a
`runtime` built by `aiRuntime()` from the new `@tanstack/cms-core/ai` entry
(the only place that statically imports @tanstack/ai). Core is now entirely
free of @tanstack/ai — a server bundle without agent tools never pulls it.
Missing runtime → NOT_CONFIGURED CmsError naming the import.

- new CmsAiRuntime type (toolDefinition typed opaquely, narrowed server-side)
  + CmsAiConfig.runtime; new ./ai entry (exports + tsdown).
- the @tanstack/ai loader (chat/image/tools) moves out of core/optional.ts to
  @tanstack/cms-api (ai.ts, re-exported from the index); api ops + prm agents
  repoint to it. optional.ts keeps only the generic isMissingModule/missingPeer.
- api test helper injects aiRuntime() by default; app/base + app/prm configs
  and their agent-tool tests inject it; optional-peers stubs ./ai under the
  @tanstack/ai-absent mock.

Green: core 656, api 226, ui-react 146, base 42, prm 10. sherif clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Neither the core main entry nor the api main entry references the MCP SDK now.

WebMCP (browser): `startWebMcpServer` moves from the `@tanstack/cms-core`
main entry to its own `@tanstack/cms-core/webmcp` entry (SDK still dynamically
imported inside, so importing the entry stays SSR-safe). ui-react/ui-dom import
from the subpath.

HTTP MCP (server): `cmsMcp` (and `@tanstack/ai-mcp`) move to the
`@tanstack/cms-api/mcp` subpath, out of the api main entry. `/api/mcp` is no
longer auto-served — `handleMcpApi(request, mcp)` takes the server and
`createCmsApiHandler({ mcp: cmsMcp })` mounts it; unmounted → NOT_CONFIGURED
naming the import. A single-file api bundle without MCP never pulls
`@tanstack/ai-mcp`. The ui-react `/api/mcp` virtual route wires `cmsMcp`, so
including that route is the opt-in; the headless-api starter mounts it (and
gains `ai.runtime`, since MCP tools are agent tools).

Green: core 656, api 226, ui-react 146, ui-dom 12, starters (team-ops,
admin-only, headless-api, contact-cards). sherif clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… (seam 5)

New packages/core/tests/bundle-seams.test.ts esbuilds each core entry and
asserts against the metafile that `core`, `core/server`, and `core/read` pull
NO optional capability (PGlite, @tanstack/ai, ai-code-mode, QuickJS, MCP SDK,
MCP-B, ai-mcp, store-postgres, sucrase), while `core/ai` and
`core/interfaces-quickjs` DO carry theirs. This pins the seams at bundle time —
a dynamic import can't sneak a capability back in, since a single-file bundler
would inline it. Adds esbuild as a core devDep.

AGENTS.md documents the capability-injection rule + the inject/opt-in-entry
table for all five capabilities. Also mounts cmsMcp in the prm /api/mcp test.

Full sweep green: test:types (37), test:lib (24), test:definitions (27/27),
test:starters, all example configs, bundle-seams (5/5), sherif. (Pre-existing
unrelated failure: react-onboarding publint styles.css export — untouched.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b0f7b54

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/cms-api

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-api@66

@tanstack/cms-cli

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-cli@66

@tanstack/cms-client

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-client@66

@tanstack/cms-compat-wp

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-compat-wp@66

@tanstack/cms-connect-atproto

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-connect-atproto@66

@tanstack/cms-migrate-wp

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-migrate-wp@66

@tanstack/cms-skills

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-skills@66

@tanstack/cms-core

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-core@66

@tanstack/cms-media-s3

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-media-s3@66

@tanstack/search-ai

npm i https://pkg.pr.new/TanStack/cms/@tanstack/search-ai@66

@tanstack/search-cms

npm i https://pkg.pr.new/TanStack/cms/@tanstack/search-cms@66

@tanstack/search-core

npm i https://pkg.pr.new/TanStack/cms/@tanstack/search-core@66

@tanstack/search-headless

npm i https://pkg.pr.new/TanStack/cms/@tanstack/search-headless@66

@tanstack/search-ui-react

npm i https://pkg.pr.new/TanStack/cms/@tanstack/search-ui-react@66

@tanstack/cms-store-postgres

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-store-postgres@66

@tanstack/cms-ui-dom

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-ui-dom@66

@tanstack/cms-ui-react

npm i https://pkg.pr.new/TanStack/cms/@tanstack/cms-ui-react@66

commit: b0f7b54

…follow-up)

The Packed Consumer check builds a fresh app from the packed tarballs; its
generated cms.config and smoke-test defineCms relied on the removed implicit
PGlite default and hit MISSING_STORAGE at boot. Inject
`storage: createPgliteStorage` (the fixture already installs store-postgres).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jherr and others added 2 commits December 2, 2026 17:32
- cms-tools: the agent-tool cast uses a type-only `import('@tanstack/ai')` to
  keep the precise type without a value import (bundle seam); annotate it with
  an oxlint-disable for consistent-type-imports.
- api: merge the new `loadTanstackAi` imports into the existing `./ai` import
  lines (import/no-duplicates).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jherr

jherr commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #68, which flattens this stack into a single PR against main.

@jherr jherr closed this Dec 3, 2026
@jherr
jherr deleted the feat/capability-seams branch December 3, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant