# chaibuilder/sdk architecture

> Architecture diagram of the GitHub repository chaibuilder/sdk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chaibuilder/sdk
- Repository: https://github.com/chaibuilder/sdk

## Overview

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: [`src/core/components/chaibuilder-editor.tsx`](https://github.com/chaibuilder/sdk/blob/dev/src/core/components/chaibuilder-editor.tsx) (Editor shell), [`frameworks/nextjs/package/ChaiBuilder.ts`](https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/package/ChaiBuilder.ts) (Next package), [`src/render/render-chai-blocks.tsx`](https://github.com/chaibuilder/sdk/blob/dev/src/render/render-chai-blocks.tsx) (Render pipeline), [`src/hooks/index.ts`](https://github.com/chaibuilder/sdk/blob/dev/src/hooks/index.ts) (Builder hooks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_core["Builder engine"]
  node_node_core_editor["Editor shell<br/>React editor"]
  node_node_core_canvas["Canvas<br/>[canvas-area.tsx]"]
  node_node_core_frame["Frame layer<br/>iframe runtime<br/>[Frame.tsx]"]
  node_node_core_settings["Settings panel<br/>block settings UI<br/>[settings-panel.tsx]"]
  node_node_core_outline["Outline tools<br/>tree & library UI<br/>[list-tree.tsx]"]
  node_node_core_render["Render pipeline<br/>block renderer"]
  node_node_core_webblocks["Web blocks<br/>block primitives<br/>[index.ts]"]
  node_node_core_runtime["Runtime registry<br/>extension registry<br/>[index.ts]"]
  node_node_core_hooks["Builder hooks<br/>interaction hooks<br/>[index.ts]"]
end

subgraph group_group_app["CMS app layer"]
  node_node_app_pages["Pages domain<br/>CMS layer"]
  node_node_app_panels["App panels<br/>admin panels<br/>[index.tsx]"]
  node_node_app_hooks["Domain hooks<br/>data hooks"]
  node_node_app_actions["Server actions<br/>persistence ops"]
  node_node_app_drizzle[("Data schema<br/>drizzle schema<br/>[schema.ts]")]
  node_node_app_assets["Asset manager<br/>storage module<br/>[index.ts]"]
  node_node_app_ai["AI subsystem<br/>assistant flows<br/>[ask-ai-action.ts]"]
end

subgraph group_group_host["Next.js host"]
  node_node_host_editor["Builder app<br/>Next.js editor host<br/>[page.tsx]"]
  node_node_host_public["Public site<br/>Next.js public host<br/>[page.tsx]"]
  node_node_host_package["Next package<br/>integration package<br/>[ChaiBuilder.ts]"]
end

subgraph group_group_shared["Shared platform"]
  node_node_shared_types["Shared types<br/>ts contracts<br/>[index.ts]"]
  node_node_shared_state["Global state<br/>atom store<br/>[store.ts]"]
  node_node_shared_utils["Utilities<br/>helpers<br/>[index.ts]"]
  node_node_shared_docs["Docs<br/>documentation<br/>[README.md]"]
end

node_node_core_editor -->|"hosts"| node_node_core_canvas
node_node_core_editor -->|"wraps"| node_node_core_frame
node_node_core_editor -->|"coordinates"| node_node_core_settings
node_node_core_editor -->|"surfaces"| node_node_core_outline
node_node_core_canvas -->|"uses"| node_node_core_hooks
node_node_core_canvas -->|"renders via"| node_node_core_render
node_node_core_canvas -->|"reads/writes"| node_node_shared_state
node_node_core_settings -->|"uses"| node_node_core_hooks
node_node_core_outline -->|"uses"| node_node_core_hooks
node_node_core_render -->|"instantiates"| node_node_core_webblocks
node_node_core_render -->|"consults"| node_node_core_runtime
node_node_core_hooks -->|"persists through"| node_node_app_actions
node_node_core_hooks -->|"orchestrates"| node_node_app_pages
node_node_app_pages -->|"served by"| node_node_app_hooks
node_node_app_panels -->|"manages"| node_node_app_pages
node_node_app_hooks -->|"calls"| node_node_app_actions
node_node_app_actions -->|"stores in"| node_node_app_drizzle
node_node_app_actions -->|"uses"| node_node_app_assets
node_node_app_ai -->|"invokes"| node_node_app_actions
node_node_host_editor -->|"embeds"| node_node_core_editor
node_node_host_editor -->|"administers"| node_node_app_pages
node_node_host_public -->|"uses"| node_node_core_render
node_node_host_public -->|"configured by"| node_node_host_package
node_node_host_editor -->|"configured by"| node_node_host_package
node_node_host_package -->|"adapts"| node_node_core_runtime
node_node_host_package -->|"adapts"| node_node_core_render
node_node_host_package -->|"uses"| node_node_shared_utils
node_node_shared_docs -.->|"documents"| node_node_core_runtime
node_node_shared_docs -.->|"documents"| node_node_app_pages

click node_node_core_editor "https://github.com/chaibuilder/sdk/blob/dev/src/core/components/chaibuilder-editor.tsx"
click node_node_core_canvas "https://github.com/chaibuilder/sdk/blob/dev/src/core/components/canvas/canvas-area.tsx"
click node_node_core_frame "https://github.com/chaibuilder/sdk/blob/dev/src/core/frame/Frame.tsx"
click node_node_core_settings "https://github.com/chaibuilder/sdk/blob/dev/src/core/components/settings/settings-panel.tsx"
click node_node_core_outline "https://github.com/chaibuilder/sdk/blob/dev/src/core/components/sidepanels/panels/outline/list-tree.tsx"
click node_node_core_render "https://github.com/chaibuilder/sdk/blob/dev/src/render/render-chai-blocks.tsx"
click node_node_core_webblocks "https://github.com/chaibuilder/sdk/blob/dev/src/web-blocks/index.ts"
click node_node_core_runtime "https://github.com/chaibuilder/sdk/blob/dev/src/runtime/index.ts"
click node_node_core_hooks "https://github.com/chaibuilder/sdk/blob/dev/src/hooks/index.ts"
click node_node_app_pages "https://github.com/chaibuilder/sdk/blob/dev/src/pages/chaibuilder-pages.tsx"
click node_node_app_panels "https://github.com/chaibuilder/sdk/blob/dev/src/pages/panels/index.tsx"
click node_node_app_hooks "https://github.com/chaibuilder/sdk/blob/dev/src/pages/hooks/use-website-data.ts"
click node_node_app_actions "https://github.com/chaibuilder/sdk/blob/dev/src/actions/builder/chai-builder-actions-handler.ts"
click node_node_app_drizzle "https://github.com/chaibuilder/sdk/blob/dev/src/actions/drizzle/schema.ts"
click node_node_app_assets "https://github.com/chaibuilder/sdk/blob/dev/src/express/actions/assets/index.ts"
click node_node_app_ai "https://github.com/chaibuilder/sdk/blob/dev/src/actions/builder/ask-ai-action.ts"
click node_node_host_editor "https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/app/(builder)/editor/page.tsx"
click node_node_host_public "https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/app/(public)/[[...slug]]/page.tsx"
click node_node_host_package "https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/package/ChaiBuilder.ts"
click node_node_shared_types "https://github.com/chaibuilder/sdk/blob/dev/src/types/index.ts"
click node_node_shared_state "https://github.com/chaibuilder/sdk/blob/dev/src/atoms/store.ts"
click node_node_shared_utils "https://github.com/chaibuilder/sdk/blob/dev/src/utils/index.ts"
click node_node_shared_docs "https://github.com/chaibuilder/sdk/blob/dev/docs/README.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_core_editor,node_node_core_canvas,node_node_core_frame,node_node_core_settings,node_node_core_outline,node_node_core_render,node_node_core_webblocks,node_node_core_runtime,node_node_core_hooks toneBlue
class node_node_app_pages,node_node_app_panels,node_node_app_hooks,node_node_app_actions,node_node_app_drizzle,node_node_app_assets,node_node_app_ai toneAmber
class node_node_host_editor,node_node_host_public,node_node_host_package toneMint
class node_node_shared_types,node_node_shared_state,node_node_shared_utils,node_node_shared_docs toneRose
```

## Components

### Builder engine

Reusable editing, rendering, runtime, and block primitives.

- [Editor shell](https://github.com/chaibuilder/sdk/blob/dev/src/core/components/chaibuilder-editor.tsx): React editor · Top-level builder UI that assembles the canvas, panels, hotkeys, preview, and editor layout. · `src/core/components/chaibuilder-editor.tsx`
- [Canvas](https://github.com/chaibuilder/sdk/blob/dev/src/core/components/canvas/canvas-area.tsx): block editing canvas · Iframe/canvas layer for selecting, nesting, reordering, dragging, and inline editing blocks. · `src/core/components/canvas/canvas-area.tsx`
- [Frame layer](https://github.com/chaibuilder/sdk/blob/dev/src/core/frame/Frame.tsx): iframe runtime · Frame and frame-context boundary that isolates editable rendering from the surrounding editor chrome. · `src/core/frame/Frame.tsx`
- [Settings panel](https://github.com/chaibuilder/sdk/blob/dev/src/core/components/settings/settings-panel.tsx): block settings UI · Block settings, styling, breakpoint, and attribute editors for the selected block tree. · `src/core/components/settings/settings-panel.tsx`
- [Outline tools](https://github.com/chaibuilder/sdk/blob/dev/src/core/components/sidepanels/panels/outline/list-tree.tsx): tree & library UI · Outline sidebar, add-blocks, library saving, import HTML, and export-code workflows. · `src/core/components/sidepanels/panels/outline/list-tree.tsx`
- [Render pipeline](https://github.com/chaibuilder/sdk/blob/dev/src/render/render-chai-blocks.tsx): block renderer · Turns blocks into React output, applies bindings, async props, design tokens, and Tailwind classes. · `src/render/render-chai-blocks.tsx`
- [Web blocks](https://github.com/chaibuilder/sdk/blob/dev/src/web-blocks/index.ts): block primitives · Primitive block implementations such as text, image, form, link, layout, and custom wrappers. · `src/web-blocks/index.ts`
- [Runtime registry](https://github.com/chaibuilder/sdk/blob/dev/src/runtime/index.ts): extension registry · Registers page types, collections, global data providers, partial types, and host extensions. · `src/runtime/index.ts`
- [Builder hooks](https://github.com/chaibuilder/sdk/blob/dev/src/hooks/index.ts): interaction 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](https://github.com/chaibuilder/sdk/blob/dev/src/pages/chaibuilder-pages.tsx): CMS layer · Page lifecycle, revisions, templates, languages, SEO, publication, slug changes, and page locking. · `src/pages/chaibuilder-pages.tsx`
- [App panels](https://github.com/chaibuilder/sdk/blob/dev/src/pages/panels/index.tsx): admin panels · Page manager, SEO, AI, user info, password, and publishing dialogs used by the builder UI. · `src/pages/panels/index.tsx`
- [Domain hooks](https://github.com/chaibuilder/sdk/blob/dev/src/pages/hooks/use-website-data.ts): data hooks · React hooks for pages, collections, website settings, auth, preview, and server mutations. · `src/pages/hooks/use-website-data.ts`
- [Server actions](https://github.com/chaibuilder/sdk/blob/dev/src/actions/builder/chai-builder-actions-handler.ts): persistence ops · Server-facing operations for pages, libraries, revisions, SEO, publishing, and AI generation. · `src/actions/builder/chai-builder-actions-handler.ts`
- [Data schema](https://github.com/chaibuilder/sdk/blob/dev/src/actions/drizzle/schema.ts): drizzle schema · Versioned database schema, relations, and migrations that back builder persistence. · `src/actions/drizzle/schema.ts`
- [Asset manager](https://github.com/chaibuilder/sdk/blob/dev/src/express/actions/assets/index.ts): storage module · Digital asset and storage actions for upload, retrieval, deletion, and media workflows. · `src/express/actions/assets/index.ts`
- [AI subsystem](https://github.com/chaibuilder/sdk/blob/dev/src/actions/builder/ask-ai-action.ts): assistant flows · 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](https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/app/(builder)/editor/page.tsx): Next.js editor host · Next.js App Router editor area with builder routes, auth callback, preview, and revalidation APIs. · `frameworks/nextjs/app/(builder)/editor/page.tsx`
- [Public site](https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/app/(public)/[[...slug]]/page.tsx): Next.js public host · Public route that renders published pages for visitors from the shared page model. · `frameworks/nextjs/app/(public)/[[...slug]]/page.tsx`
- [Next package](https://github.com/chaibuilder/sdk/blob/dev/frameworks/nextjs/package/ChaiBuilder.ts): integration 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](https://github.com/chaibuilder/sdk/blob/dev/src/types/index.ts): ts contracts · Common repository-wide type definitions for blocks, actions, collections, partials, and editor props. · `src/types/index.ts`
- [Global state](https://github.com/chaibuilder/sdk/blob/dev/src/atoms/store.ts): atom store · Centralized builder and UI atoms/stores used across the editor, canvas, and panels. · `src/atoms/store.ts`
- [Utilities](https://github.com/chaibuilder/sdk/blob/dev/src/utils/index.ts): helpers · Cross-cutting helpers for Tailwind, theme generation, plugins, and general shared logic. · `src/utils/index.ts`
- [Docs](https://github.com/chaibuilder/sdk/blob/dev/docs/README.md): documentation · Repository documentation for concepts, APIs, extensions, rendering, and setup. · `docs/README.md`

## Connections

- 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.
