How chaibuilder/sdk works
1. What this repository is
- This is a monorepo for Chai Builder, a React-based visual website builder.
- It contains the core builder SDK, a Next.js integration/app, shared runtime/rendering code, and a large set of builder UI modules.
- The codebase is split between framework-agnostic primitives and framework-specific hosting layers.
2. Main architectural boundary: core engine vs host app
- src/core, src/render, src/runtime, src/web-blocks, and src/hooks form the reusable builder engine.
- src/pages, src/routes, and src/actions provide the higher-level app features for page management, persistence, auth, media, SEO, and AI.
- frameworks/nextjs is the adapter/example host that shows how the engine is embedded into a real Next.js app.
3. Builder UI shell and interaction layer
- The editor is built from src/core/components, src/pages/panels, src/pages/client/components, and src/components/ui.
- This layer handles the canvas, topbar, side panels, settings panels, page manager, drag-and-drop, undo/redo, keyboard shortcuts, and modal workflows.
- State is centralized through atom/store modules in src/atoms, with hooks orchestrating user interactions and derived state.
4. Canvas and block editing model
- The visual canvas lives under src/core/components/canvas and src/core/frame.
- The editor manipulates a tree of blocks, not raw DOM; blocks can be selected, nested, reordered, styled, duplicated, or converted into partials/library items.
- Drag-and-drop, floating block actions, inline text editing, and breakpoint-aware styling all sit on top of this block tree model.
5. Rendering pipeline
- Rendering is separated from editing.
- src/render turns the block model into React output, applies bindings, design tokens, Tailwind class generation, async blocks, and page-level rendering rules.
- src/web-blocks contains the primitive block implementations used by the renderer, such as text, image, form, link, layout, and custom wrapper blocks.
6. Runtime registration and extensibility
- src/runtime is the plugin/registration layer that lets the host app register page types, collections, global data providers, partial types, and custom controls.
- src/runtime/client registers builder-specific extensions such as top bars, sidebar panels, add-block tabs, media manager hooks, and library integration.
- This is the main extension seam: the builder core can be embedded in another React app while still allowing host-specific behavior.
7. Persistence and server-side operations
- src/actions contains server-facing operations for pages, libraries, revisions, settings, SEO fields, publishing, slug changes, and AI-assisted generation.
- It uses Drizzle schemas/migrations under src/actions/drizzle, so the data model is explicit and versioned.
- These actions are the boundary between the editor state and durable storage.
8. External services and platform dependencies
- Supabase is used for auth, admin access, storage, and realtime/page-lock behavior; see src/pages/hooks, src/express, and the Next.js app’s Supabase helpers.
- Realtime collaboration/page locking is built around Supabase realtime adapters.
- The Next.js framework package is configured for deployment tooling that includes OpenNext/Wrangler, indicating serverless-hosted runtime support.
9. Content management domain
- src/pages implements the CMS-like layer: page lists, page creation, revisions, templates, language variants, SEO, JSON-LD, and publication flow.
- src/pages/hooks contains the data-fetching and mutation hooks for that domain.
- The page system is not just static rendering; it includes lifecycle management such as draft, publish, restore, duplicate, lock, and version compare.
10. AI subsystem
- AI is a first-class feature, with prompts and assistant flows in src/actions/builder, src/core/components/ai, src/pages/panels/ai-panel, and src/hooks/use-ask-ai.ts.
- AI can generate HTML, SEO fields, and assist with block editing or content transformations.
- The AI layer is integrated into the builder workflow, not isolated as a separate service.
11. Templates, libraries, and import/export
- public/library-blocks contains prebuilt block templates used by the block library UI.
- The builder supports importing HTML, saving blocks to libraries, partial blocks, and exporting code through the outline/sidebar tools.
- Library management is a distinct subsystem from page editing, but it feeds the same block model.
12. Next.js integration package
- frameworks/nextjs is a concrete integration of the SDK into a Next.js App Router application.
- It has separate routes for the builder/editor, public page rendering, and API endpoints for preview/revalidation/auth callbacks.
- This package demonstrates the runtime split between editor-only capabilities and public-facing page delivery.
13. Public site rendering vs editor rendering
- The public route under frameworks/nextjs/app/(public)/[[...slug]] renders published content for visitors.
- The builder route under frameworks/nextjs/app/(builder) hosts the editor UI and administrative APIs.
- Preview/revalidate routes bridge editor changes to the public rendering path.
14. Design system and styling
- Tailwind CSS is the primary styling substrate across the repo, with theme presets, tokens, fonts, and class utilities centralized in src/core/constants, src/core/utils, and related helpers.
- The editor has its own styling controls, but output is ultimately normalized to runtime-renderable classes and design tokens.
- This keeps visual editing and generated output aligned.
15. Quality, tooling, and delivery
- The repo uses Vitest for unit tests, Playwright for end-to-end tests, ESLint/Prettier for code quality, and GitHub Actions for CI, docs, releases, and sitemap generation.
- The docs site under docs/ is part of the repository’s distribution and explains the builder’s concepts, APIs, and extension points.
- Overall, the architecture is a layered system: reusable builder engine, domain-specific CMS/admin layer, and framework-specific host integrations.
Main entry points: chaibuilder-editor.tsx Editor shell · ChaiBuilder.ts Next package · render-chai-blocks.tsx Render pipeline · index.ts Builder hooks
Components 23
Builder engine
Reusable editing, rendering, runtime, and block primitives.
- Editor shell · Top-level builder UI that assembles the canvas, panels, hotkeys, preview, and editor layout.
src/core/components/chaibuilder-editor.tsx - Canvas · Iframe/canvas layer for selecting, nesting, reordering, dragging, and inline editing blocks.
src/core/components/canvas/canvas-area.tsx - Frame layer · Frame and frame-context boundary that isolates editable rendering from the surrounding editor chrome.
src/core/frame/Frame.tsx - Settings panel · Block settings, styling, breakpoint, and attribute editors for the selected block tree.
src/core/components/settings/settings-panel.tsx - Outline tools · Outline sidebar, add-blocks, library saving, import HTML, and export-code workflows.
src/core/components/sidepanels/panels/outline/list-tree.tsx - Render pipeline · Turns blocks into React output, applies bindings, async props, design tokens, and Tailwind classes.
src/render/render-chai-blocks.tsx - Web blocks · Primitive block implementations such as text, image, form, link, layout, and custom wrappers.
src/web-blocks/index.ts - Runtime registry · Registers page types, collections, global data providers, partial types, and host extensions.
src/runtime/index.ts - Builder hooks · Stateful hooks for block operations, selection, partials, canvas state, AI, and save flows.
src/hooks/index.ts
CMS app layer
Page management, persistence, AI, media, and editor-side workflows.
- Pages domain · Page lifecycle, revisions, templates, languages, SEO, publication, slug changes, and page locking.
src/pages/chaibuilder-pages.tsx - App panels · Page manager, SEO, AI, user info, password, and publishing dialogs used by the builder UI.
src/pages/panels/index.tsx - Domain hooks · React hooks for pages, collections, website settings, auth, preview, and server mutations.
src/pages/hooks/use-website-data.ts - Server actions · Server-facing operations for pages, libraries, revisions, SEO, publishing, and AI generation.
src/actions/builder/chai-builder-actions-handler.ts - Data schema · Versioned database schema, relations, and migrations that back builder persistence.
src/actions/drizzle/schema.ts - Asset manager · Digital asset and storage actions for upload, retrieval, deletion, and media workflows.
src/express/actions/assets/index.ts - AI subsystem · Prompt handling, AI chat, HTML generation, SEO generation, and AI-assisted editing paths.
src/actions/builder/ask-ai-action.ts
Next.js host
Concrete Next.js integration, public rendering, and deployment glue.
- Builder app · Next.js App Router editor area with builder routes, auth callback, preview, and revalidation APIs.
frameworks/nextjs/app/(builder)/editor/page.tsx - Public site · Public route that renders published pages for visitors from the shared page model.
frameworks/nextjs/app/(public)/[[...slug]]/page.tsx - Next package · Framework package that adapts the SDK to Next.js with runtime, rendering, styles, and data helpers.
frameworks/nextjs/package/ChaiBuilder.ts
Shared platform
Shared types, state, utilities, docs, and build/test tooling.
- Shared types · Common repository-wide type definitions for blocks, actions, collections, partials, and editor props.
src/types/index.ts - Global state · Centralized builder and UI atoms/stores used across the editor, canvas, and panels.
src/atoms/store.ts - Utilities · Cross-cutting helpers for Tailwind, theme generation, plugins, and general shared logic.
src/utils/index.ts - Docs · Repository documentation for concepts, APIs, extensions, rendering, and setup.
docs/README.md
Connections 29
- Editor shell → Canvas: hosts · The editor shell embeds the editing canvas.
- Editor shell → Frame layer: wraps · The editor shell relies on the frame boundary for isolated editing.
- Editor shell → Settings panel: coordinates · The editor shell drives the settings panel for selected blocks.
- Editor shell → Outline tools: surfaces · The editor shell exposes outline and library workflows.
- Canvas → Builder hooks: uses · Canvas interaction is orchestrated through builder hooks.
- Canvas → Render pipeline: renders via · The canvas delegates block output to the rendering pipeline.
- Canvas → Global state: reads/writes · Selection, drag, and editor state are centralized in atoms.
- Settings panel → Builder hooks: uses · Settings panels mutate blocks and styling through hooks.
- Outline tools → Builder hooks: uses · Outline actions such as add, import, save, and export rely on hooks.
- Render pipeline → Web blocks: instantiates · Renderer composes the primitive web blocks into output.
- Render pipeline → Runtime registry: consults · Rendering behavior depends on runtime registrations and host-provided extensions.
- Builder hooks → Server actions: persists through · Hooks save, load, and mutate durable page and library data via server actions.
- Builder hooks → Pages domain: orchestrates · Hooks support page selection, language variants, revisions, and lifecycle flows.
- Pages domain → Domain hooks: served by · The page domain is accessed through domain-specific React hooks.
- App panels → Pages domain: manages · Panels expose page manager, SEO, publishing, and page lifecycle workflows.
- Domain hooks → Server actions: calls · Domain hooks fetch and mutate state through server actions.
- Server actions → Data schema: stores in · Actions read and write data through the explicit Drizzle schema.
- Server actions → Asset manager: uses · Builder actions rely on asset and storage operations for media workflows.
- AI subsystem → Server actions: invokes · AI-assisted generation is implemented as part of the server action layer.
- Builder app → Editor shell: embeds · The Next.js editor route hosts the reusable builder editor shell.
- Builder app → Pages domain: administers · Builder routes serve the CMS-like page management layer.
- Public site → Render pipeline: uses · Public rendering relies on the shared render pipeline.
- Public site → Next package: configured by · The public app uses the integration package for rendering and data access.
- Builder app → Next package: configured by · The editor app is built on top of the Next.js integration package.
- Next package → Runtime registry: adapts · The package wires host-specific runtime registrations into the core engine.
- Next package → Render pipeline: adapts · The package exposes Next-specific rendering helpers on top of the shared renderer.
- Next package → Utilities: uses · The package depends on shared styling and utility helpers.
- Docs → Runtime registry: documents · The docs explain extension points and runtime registration.
- Docs → Pages domain: documents · The docs cover page management, rendering, and CMS concepts.