# chatbotkit/node-sdk architecture

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

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

## Overview

##### Project Type and Purpose
- This is a monorepo for an open‑source SDK ecosystem (ChatBotKit Node SDK).
- It packages a core “SDK” and a set of framework‑specific extensions (React, Next.js, NextAuth, Fetch adapter, CLI), plus a scaffolding tool and a suite of examples targeting Node, Next.js, Cloudflare Workers, etc.
- Purpose: provide a unified, multi‑platform JavaScript SDK for building and deploying conversational AI bots, plus CLI tooling and turnkey example apps.

##### File Structure Analysis
- Top level:
- “packages/” – each subfolder is a published npm package:
– sdk (core API client)
– fetch (isomorphic fetch implementation)
– react (React UI components & hooks)
– next (Next.js helpers for Edge/serverless)
– nextauth (NextAuth.js adapter)
– cli (command‑line interface)
– placeholder (utility stub)
- “tools/create-cbk-app” – project scaffolding CLI wrapper
- “examples/…” – sample consumer applications organized by platform
- “scripts/” – monorepo build/export helpers
- “.changeset/”, GitHub workflows – versioning & CI
- pnpm-workspace.yaml + turbo.json – workspace and build orchestration

##### README Insights
- Identifies the six main packages and the scaffolding tool.
- Describes serverless compatibility, streaming, multi‑model support.
- Illustrates typical usage flows in Next.js, Cloudflare, Node, etc.
- Emphasizes integration points (SDK → React → Next.js → NextAuth, CLI, & Fetch)

##### Main Components
a. Core SDK Layer
- @chatbotkit/sdk
- @chatbotkit/fetch (low‑level HTTP adapter)
b. Extension/Integration Layer
- @chatbotkit/react (UI components, hooks)
- @chatbotkit/next (Edge/serverless integration, streaming wrappers)
- @chatbotkit/nextauth (auth adapter)
- @chatbotkit/cli (DevOps/management tool)
c. Tooling & Orchestration
- pnpm & Turborepo (workspace + build pipeline)
- .changeset (versioning)
- GitHub Actions (CI for docs & release)
- “tools/create-cbk-app” (app generator)
d. Consumer Apps / Examples
- Next.js examples (App Router, Pages Router, hybrid)
- Cloudflare Workers example
- Node CLI & SDK examples (CJS, ESM, TS)
e. External Services
- ChatBotKit Backend REST API (conversation, datasets, integrations)

##### Relationships & Interactions
- “@fetch” feeding HTTP requests for the SDK.
- “@react”, “@next”, “@nextauth” importing core SDK (and fetch) to provide higher‑level abstractions/UI.
- “cli” importing core SDK (and fetch) to provide command‑line commands.
- “create-cbk-app” using “cli” under the hood.
- “examples” depending on the various published packages to demonstrate usage.
- CI/CD pipeline steps (build → docs → release) orchestrated by Turborepo and GitHub Actions.

##### Architectural Patterns & Principles
- Monorepo with pnpm workspace and Turborepo for incremental builds.
- Modular package‑per‑concern (core vs. adapters vs. UI vs. tooling).
- Clear separation of API client (sdk) vs. transport (fetch) vs. UI (react) vs. framework glue (next/nextauth) vs. CLI.
- Examples as integration tests / reference implementations.
- CI ensures docs & version bump via .changeset.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Packages Group
    subgraph "Packages" 
        direction TB
        SDK["@chatbotkit/sdk"]:::core
        Fetch["@chatbotkit/fetch"]:::core
        React["@chatbotkit/react"]:::ui
        Next["@chatbotkit/next"]:::ui
        NextAuth["@chatbotkit/nextauth"]:::ui
        CLI["@chatbotkit/cli"]:::cli
        Placeholder["@chatbotkit/placeholder"]:::cli
    end

    %% Tools Group
    subgraph "Tools" 
        direction TB
        CreateApp["create-cbk-app"]:::cli
    end

    %% Examples Group
    subgraph "Examples" 
        direction TB
        NextjsExamples["Next.js Examples"]:::examples
        CloudflareExample["Cloudflare Workers Example"]:::examples
        SDKExamples["SDK Usage Examples"]:::examples
    end

    %% CI/CD and Orchestration
    subgraph "CI/CD & Orchestration" 
        direction TB
        Workspace["pnpm & Turborepo"]:::cicd
        Changeset[".changeset"]:::cicd
        GitHubActions["GitHub Actions"]:::cicd
    end

    %% External
    ChatBotAPI["ChatBotKit Backend API"]:::external

    %% Relationships
    Fetch -->|"provides HTTP for"| SDK
    SDK -->|"calls"| ChatBotAPI
    React -->|"imports"| SDK
    React -->|"imports"| Fetch
    Next -->|"imports"| SDK
    Next -->|"imports"| Fetch
    NextAuth -->|"imports"| SDK
    NextAuth -->|"imports"| Fetch
    CLI -->|"imports"| SDK
    CLI -->|"imports"| Fetch
    CreateApp -->|"uses CLI"| CLI
    NextjsExamples -->|"depends on"| React
    NextjsExamples -->|"depends on"| Next
    CloudflareExample -->|"depends on"| SDK
    CloudflareExample -->|"depends on"| Fetch
    SDKExamples -->|"depends on"| SDK
    SDKExamples -->|"depends on"| Fetch
    Workspace -->|"builds"| SDK
    Workspace -->|"builds"| Fetch
    Workspace -->|"builds"| React
    Workspace -->|"builds"| Next
    Workspace -->|"builds"| NextAuth
    Workspace -->|"builds"| CLI
    Changeset -->|"versioning"| SDK
    Changeset -->|"versioning"| Fetch
    GitHubActions -->|"CI for docs & release"| SDK
    GitHubActions -->|"CI for docs & release"| React

    %% Click Events
    click SDK "https://github.com/chatbotkit/node-sdk/tree/main/packages/sdk"
    click Fetch "https://github.com/chatbotkit/node-sdk/tree/main/packages/fetch"
    click React "https://github.com/chatbotkit/node-sdk/tree/main/packages/react"
    click Next "https://github.com/chatbotkit/node-sdk/tree/main/packages/next"
    click NextAuth "https://github.com/chatbotkit/node-sdk/tree/main/packages/nextauth"
    click CLI "https://github.com/chatbotkit/node-sdk/tree/main/packages/cli"
    click Placeholder "https://github.com/chatbotkit/node-sdk/tree/main/packages/placeholder"
    click CreateApp "https://github.com/chatbotkit/node-sdk/tree/main/tools/create-cbk-app"
    click Workspace "https://github.com/chatbotkit/node-sdk/blob/main/pnpm-workspace.yaml"
    click Workspace "https://github.com/chatbotkit/node-sdk/blob/main/turbo.json"
    click Changeset "https://github.com/chatbotkit/node-sdk/tree/main/.changeset/"
    click GitHubActions "https://github.com/chatbotkit/node-sdk/blob/main/.github/workflows/docs.yml"
    click CloudflareExample "https://github.com/chatbotkit/node-sdk/tree/main/examples/cloudflare/gpt4-stream"
    click NextjsExamples "https://github.com/chatbotkit/node-sdk/tree/main/examples/nextjs/"
    click SDKExamples "https://github.com/chatbotkit/node-sdk/tree/main/examples/sdk/cjs"
    click SDKExamples "https://github.com/chatbotkit/node-sdk/tree/main/examples/sdk/esm"
    click SDKExamples "https://github.com/chatbotkit/node-sdk/tree/main/examples/sdk/ts"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#0370B8,stroke-width:1px
    classDef ui fill:#E0F8D8,stroke:#2E7D32,stroke-width:1px
    classDef cli fill:#FFF4D0,stroke:#F9A825,stroke-width:1px
    classDef examples fill:#F5F5F5,stroke:#9E9E9E,stroke-width:1px
    classDef cicd fill:#FFFFFF,stroke:#616161,stroke-dasharray: 5 5,stroke-width:1px
    classDef external fill:#FFE0E0,stroke:#D32F2F,stroke-width:1px,stroke-dasharray: 2 2
```
