Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# AGENTS — TanStack CMS monorepo

## Status: pre-publication — restructure freely

This library is **pre-publication**. None of the `@tanstack/cms-*` / `@tanstack/search-*`
packages are on npm, and **nothing outside this repo depends on them**. That means:

- **Break public APIs at will.** Rename, move, split, merge, or delete modules,
exports, entry points, and types whenever it makes the design better. No
backward-compatibility burden, no deprecation cycles, no changesets required
for consumer-facing breaks.
- **Reshape package boundaries freely** — new entry points (e.g. a tree-shakeable
`/read` surface), new packages, re-layering of `core` internals, etc.
- The only bar is **internal consistency**: the monorepo must still build and its
tests must pass after a change. Update all in-repo call sites (apps, examples,
starters) as part of the same change.

Don't preserve an awkward API out of compat fear — spindle, fold, and mutilate
as needed, then make the whole repo green.

## Architectural direction (edge-first reads)

Deploying to edge runtimes (e.g. Kody/workerd) proved that the **read/render
path must be tree-shakeable to a tiny bundle with no server engine** — no
`@tanstack/cms-core/server` barrel, no embedded PGlite (WASM/eval), no AI /
code-mode / MCP, no `node:*`. See issues #63 (edge-read refactor) and #64 (Kody
platform ask for the write/admin engine). Target split:

- **read path** (public pages): pure query + renderers, edge-deployable.
- **write / admin / migrate path**: the heavy engine, runs on a warm/Node-ish
runtime, never in the hot read worker.

## Capability injection — core never imports an optional capability

The engine ships no built-in storage, AI, code-mode, or MCP. Each is an **opt-in
capability the app injects** via `defineCms`, supplied by a dedicated entry whose
static imports pull the heavy dependency. Core never imports these packages, so
they only enter a bundle when the app imports the opt-in entry. This is enforced
at **bundle time** (not just runtime) by `packages/core/tests/bundle-seams.test.ts`
— a dynamic `import()` is not enough, because a single-file bundler inlines it.

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

Rules for new work:

- **Never statically or dynamically import an optional capability from a core
engine module** (`index`, `server`, `read`, `config`, `storage`, `schema`, …).
If a feature needs one, add a capability type to the config (typed opaquely or
via `import type`, which is erased) and a dedicated opt-in entry that provides
it; the engine reads the injected value.
- **Fail with an actionable error.** A missing capability must throw a
`NOT_CONFIGURED` `CmsError` naming the exact import to add — never silently
fall back to a package of its own.
- MCP tools are agent tools: an app serving `/api/mcp` also needs `ai.runtime`.
- Tests get the embedded engine back via the `@tanstack/cms-store-postgres/testing`
vitest `setupFiles` (it calls `setDefaultStorageFactory`, an opt-in escape
hatch that stays `null` in production so bundles remain clean).
1 change: 1 addition & 0 deletions app/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@tanstack/cms-core": "workspace:*",
"@tanstack/cms-migrate-wp": "workspace:*",
"@tanstack/cms-skills": "workspace:*",
"@tanstack/cms-store-postgres": "workspace:*",
"@tanstack/cms-ui-react": "workspace:*",
"@tanstack/db": "0.15.2",
"@tanstack/onboarding-core": "workspace:*",
Expand Down
6 changes: 6 additions & 0 deletions app/base/src/cms.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { createServerOnlyFn } from '@tanstack/react-start'
import { defineCms, interfaceScheduleHandlers } from '@tanstack/cms-core'
import { aiRuntime } from '@tanstack/cms-core/ai'
import { quickjsInterfaceRuntime } from '@tanstack/cms-core/interfaces-quickjs'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { seed } from './seed'
import { baseOps } from './operations'
import { definition } from './cms.definition'
import { collectionRuntimes } from './collection-runtime'

export const cms = defineCms(definition, {
storage: createPgliteStorage,
collections: collectionRuntimes,
blockTypes: [],
seed,
ops: baseOps,
interfaces: {
collection: 'interfaces',
runtime: quickjsInterfaceRuntime(),
scheduleSession: createServerOnlyFn(async (userId: string) =>
(await import('./auth.server')).scheduleSession(userId),
),
Expand All @@ -35,6 +40,7 @@ export const cms = defineCms(definition, {
events: { dispatch: 'manual' },
security: { trustForwardedHost: true },
ai: {
runtime: aiRuntime(),
textAdapter: createServerOnlyFn(async () =>
(await import('./ai.server')).textAdapter(),
),
Expand Down
13 changes: 7 additions & 6 deletions app/base/src/schema-registry.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@tanstack/cms-core'
import {
CmsError,
ensureDefaultAdapter,
getStorage,
resolveStorageAdapter,
shareCmsEventScope,
Expand Down Expand Up @@ -78,10 +77,8 @@ async function registryFrom(tx: StorageOps): Promise<SchemaRegistry> {
export async function readRegistry(): Promise<SchemaRegistry> {
// Open the registry before getStorage can run scheduled publishing, so
// scheduled documents use the persisted schema even on a cold process.
// `resolveStorageAdapter()` is synchronous, so the embedded default engine
// (when an app configures no `storage`) must be constructed first — otherwise
// it throws "Default storage is not initialized yet" on a cold process.
await ensureDefaultAdapter()
// `resolveStorageAdapter()` constructs the configured adapter synchronously
// (a `StorageFactory` is resolved on first access), so no priming step.
const adapter = resolveStorageAdapter()
await adapter.init()
if (await adapter.getMeta(SCHEMA_META_KEY)) {
Expand Down Expand Up @@ -606,7 +603,11 @@ export async function publishCollectionBlueprints(
.map((e) => [e.name, runtimeCollection(e)]),
)
if (cfg.storage) {
cfg.storage = snapshotStorage(cfg.storage, updatedRegistry.revision)
// `cfg.storage` may be a `StorageFactory`; snapshot the resolved adapter.
cfg.storage = snapshotStorage(
resolveStorageAdapter(),
updatedRegistry.revision,
)
}
const storeCtx = registryContext.getStore()
if (storeCtx) {
Expand Down
6 changes: 5 additions & 1 deletion app/base/tests/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { defineCms, normalizeSession } from '@tanstack/cms-core'
import { quickjsInterfaceRuntime } from '@tanstack/cms-core/interfaces-quickjs'
import {
accessContext,
approve,
Expand Down Expand Up @@ -86,7 +87,10 @@ beforeEach(() => {
blockTypes: [],
seed,
ops: baseOps,
interfaces: { collection: 'interfaces' },
interfaces: {
collection: 'interfaces',
runtime: quickjsInterfaceRuntime(),
},
dataDir: dir,
auth: {
getUser: ({ request }) => dbUser(request),
Expand Down
2 changes: 2 additions & 0 deletions app/base/tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { chat } from '@tanstack/ai'
import { defineCms } from '@tanstack/cms-core'
import { aiRuntime } from '@tanstack/cms-core/ai'
import {
closeStore,
listPending,
Expand Down Expand Up @@ -66,6 +67,7 @@ beforeEach(() => {
}),
},
ai: {
runtime: aiRuntime(),
textAdapter: () => ({ name: 'fake' }),
agentInstructions: instructions,
},
Expand Down
6 changes: 5 additions & 1 deletion app/base/tests/schema-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { beforeEach, afterEach, describe, it, expect } from 'vitest'
import { defineCms, getCmsConfig, normalizeSession } from '@tanstack/cms-core'
import { quickjsInterfaceRuntime } from '@tanstack/cms-core/interfaces-quickjs'
import type { CmsOpContext, Doc } from '@tanstack/cms-core'
import {
accessContext,
Expand Down Expand Up @@ -87,7 +88,10 @@ beforeEach(() => {
blockTypes: [],
seed,
ops: baseOps,
interfaces: { collection: 'interfaces' },
interfaces: {
collection: 'interfaces',
runtime: quickjsInterfaceRuntime(),
},
dataDir: dir,
events: { dispatch: 'manual' },
auth: {
Expand Down
7 changes: 6 additions & 1 deletion app/base/tests/worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { quickjsInterfaceRuntime } from '@tanstack/cms-core/interfaces-quickjs'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
Expand Down Expand Up @@ -52,7 +53,11 @@ describe('Base background interface worker', () => {
seed,
blockTypes: [],
dataDir: dir,
interfaces: { collection: 'interfaces', scheduleSession },
interfaces: {
collection: 'interfaces',
scheduleSession,
runtime: quickjsInterfaceRuntime(),
},
events: { dispatch: 'manual' },
eventHandlers: interfaceScheduleHandlers(
'interfaces',
Expand Down
5 changes: 5 additions & 0 deletions app/base/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export default defineConfig({
test: {
name: 'base',
environment: 'node',
// Core ships no storage default; restore the embedded PGlite engine for
// tests that don't inject their own `storage`. Base uses the on-disk
// variant: the recover-owner test spawns a subprocess that reads the
// database file, so it needs real persistence, not the shared in-memory VM.
setupFiles: ['@tanstack/cms-store-postgres/testing-disk'],
include: ['tests/**/*.test.ts'],
fileParallelism: false,
// Tests drive the embedded PGlite engine (WASM init) and multi-step
Expand Down
1 change: 1 addition & 0 deletions app/docs-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@tanstack/ai-anthropic": "^0.23.0",
"@tanstack/cms-api": "workspace:*",
"@tanstack/cms-core": "workspace:*",
"@tanstack/cms-store-postgres": "workspace:*",
"@tanstack/cms-ui-react": "workspace:*",
"@tanstack/react-router": "^1.170.46",
"@tanstack/react-start": "^1.169.4",
Expand Down
2 changes: 2 additions & 0 deletions app/docs-preview/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* tree never collides with the first.
*/
import { defineBlock, defineCms } from '@tanstack/cms-core'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { createServerOnlyFn } from '@tanstack/react-start'
import { definition } from './cms.definition'
import { askAgentConfig } from './agents/ask'
Expand Down Expand Up @@ -57,6 +58,7 @@ const ai = {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
...(initialDataDir ? { dataDir: initialDataDir } : {}),
blockTypes,
ai,
Expand Down
2 changes: 1 addition & 1 deletion app/prm/src/agents/ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Non-negotiable rules (§6.1):
4. CROSS-PERSON BRIEFINGS: When asked for a briefing (e.g. "Brief me on everyone I'm seeing at the Summit" or "Who do I know working on durable state for agents?"), group results in priority tier order (\`tier-1\`, \`tier-2\`, \`tier-3\`, \`unranked\`), include each person's headline, talk title + hook, and whether we have already interacted with them.`

export async function createAskExtraTools() {
const { loadTanstackAi } = await import('@tanstack/cms-core/server')
const { loadTanstackAi } = await import('@tanstack/cms-api')
const { toolDefinition } = await loadTanstackAi()
const offerNextStep = toolDefinition({
name: 'offer_next_step',
Expand Down
2 changes: 1 addition & 1 deletion app/prm/src/agents/enrich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Rules (§2, §4, §5, §8):
export async function createEnrichmentWebTools(
depth: EnrichmentDepth = 'deep',
) {
const { loadTanstackAi } = await import('@tanstack/cms-core/server')
const { loadTanstackAi } = await import('@tanstack/cms-api')
const { toolDefinition } = await loadTanstackAi()
const budget = createBudgetTracker(depth)
const provider = getSearchProvider()
Expand Down
4 changes: 4 additions & 0 deletions app/prm/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
import { createServerOnlyFn } from '@tanstack/react-start'
import { defineCms, PERMISSIONS } from '@tanstack/cms-core'
import { aiRuntime } from '@tanstack/cms-core/ai'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { seed } from './seed'
import { importPeopleOp } from './ops/import-people'
import { enrichAgentConfig, ENRICHMENT_SKILL_PROMPT } from './agents/enrich'
Expand All @@ -39,6 +41,7 @@ if (typeof process !== 'undefined' && process.versions?.node) {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
...(initialDataDir ? { dataDir: initialDataDir } : {}),
blockTypes: [],
ops: [importPeopleOp],
Expand Down Expand Up @@ -109,6 +112,7 @@ export const cms = defineCms(definition, {
],
},
ai: {
runtime: aiRuntime(),
textAdapter: async () => {
if (!process.env.ANTHROPIC_API_KEY) return null
const { anthropicText } = await import('@tanstack/ai-anthropic')
Expand Down
5 changes: 4 additions & 1 deletion app/prm/tests/crm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
PERMISSIONS,
} from '@tanstack/cms-core'
import type { AnyDocumentStateStrategy, Field } from '@tanstack/cms-core'
import { aiRuntime } from '@tanstack/cms-core/ai'
import {
accessContext,
actOnDocument,
Expand All @@ -44,6 +45,7 @@ import {
} from '@tanstack/cms-core/server'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { createCmsApiHandler, invokeAnyOp } from '@tanstack/cms-api'
import { cmsMcp } from '@tanstack/cms-api/mcp'
import { createCmsSearch } from '@tanstack/search-cms'
import { createSearchStore } from '@tanstack/search-headless'
import { crmCollections, person } from '../src/collections'
Expand Down Expand Up @@ -105,6 +107,7 @@ function setupCrmTest(extra?: {
],
},
ai: {
runtime: aiRuntime(),
...(extra?.textAdapter ? { textAdapter: extra.textAdapter } : {}),
agents: {
enrich: enrichAgentConfig,
Expand Down Expand Up @@ -613,7 +616,7 @@ describe('CRM acceptance criteria (§11)', () => {
it('§11.12 & §13: /api/mcp refuses unauthenticated calls, scopes read-only key to read tools, and attributes writes to mcp:<agent>', async () => {
setupCrmTest()
await seedContent()
const handler = createCmsApiHandler()
const handler = createCmsApiHandler({ mcp: cmsMcp })

// 1. Unauthenticated call -> 401
const unauth = await handler(
Expand Down
2 changes: 2 additions & 0 deletions examples/astro-starlight-after/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* is in ./cms.definition.ts.
*/
import { defineBlock, defineCms } from '@tanstack/cms-core'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { seedDocuments } from './seed-data'
import { definition } from './cms.definition'

Expand Down Expand Up @@ -37,6 +38,7 @@ const ai = {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
blockTypes,
ai,
seed: { documents: seedDocuments },
Expand Down
2 changes: 2 additions & 0 deletions examples/blog-voting/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* and block palette.
*/
import { defineBlock, defineCms } from '@tanstack/cms-core'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { createMockAuth } from '@tanstack/cms-ui-react'
import type { SeedStore } from '@tanstack/cms-core'
import { definition } from './cms.definition'
Expand Down Expand Up @@ -148,6 +149,7 @@ const ai = {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
blockTypes,
ai,
seed,
Expand Down
2 changes: 2 additions & 0 deletions examples/blog/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* and block palette.
*/
import { defineBlock, defineCms, livePublishing } from '@tanstack/cms-core'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { createMockAuth } from '@tanstack/cms-ui-react'
import type { SeedStore } from '@tanstack/cms-core'
import { definition } from './cms.definition'
Expand Down Expand Up @@ -171,6 +172,7 @@ const ai = {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
blockTypes,
ai,
seed,
Expand Down
1 change: 1 addition & 0 deletions examples/db-federated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@tanstack/cms-api": "workspace:*",
"@tanstack/cms-client": "workspace:*",
"@tanstack/cms-core": "workspace:*",
"@tanstack/cms-store-postgres": "workspace:*",
"@tanstack/cms-ui-react": "workspace:*",
"@tanstack/db": "0.15.2",
"@tanstack/query-core": "^5.105.0",
Expand Down
2 changes: 2 additions & 0 deletions examples/db-federated/src/cms.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* keyed by (locale, region).
*/
import { defineCms } from '@tanstack/cms-core'
import { createPgliteStorage } from '@tanstack/cms-store-postgres/pglite'
import { createMockAuth } from '@tanstack/cms-ui-react'
import type { SeedStore } from '@tanstack/cms-core'
import { definition } from './cms.definition'
Expand Down Expand Up @@ -80,6 +81,7 @@ const seed: SeedStore = {
}

export const cms = defineCms(definition, {
storage: createPgliteStorage,
blockTypes: [],
seed,
auth: createMockAuth(),
Expand Down
Loading
Loading