# cloudflare/agents-starter architecture

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

- Interactive diagram: https://gitdiagram.com/cloudflare/agents-starter
- Repository: https://github.com/cloudflare/agents-starter

## Overview

##### Project type and purpose
- This is a full-stack, single-repository starter kit for building AI-powered chat agents on Cloudflare’s serverless platform.
- The “frontend” is a React + Vite application that provides a modern chat UI. The “backend” is a Cloudflare Worker (server.ts) that uses the `agents` framework and an AI SDK to process user messages, handle tool execution, and stream responses back to the client.

##### File structure
- Top level
- `index.html`, `public/`, `src/styles.css` → static assets (the chat UI)
- `vite.config.ts` → build config for bundling the React app
- `wrangler.jsonc` → Cloudflare Worker configuration (bindings, cron triggers)
- `src/`
- `app.tsx`, `client.tsx` → front-end entry points (React root)
- `server.ts` → backend entry point (Cloudflare Worker handler)
- `tools.ts` → definitions of chat tools (some auto-executing, some requiring human confirmation)
- `utils.ts`, `shared.ts`, `lib/utils.ts` → shared helper functions
- `providers/` → React context providers (modal, tooltip)
- `components/` → UI components (buttons, inputs, cards, menu bar, etc.)
- `hooks/` → custom React hooks (theme, click-outside, menu navigation)
- Configs and CI
- `.github/workflows/sanity-check.yml` → CI pipeline
- `.dev.vars.example` → environment variable template

##### README for additional insights
- Confirms a two-part architecture:
a. **Interactive chat interface** in `app.tsx`
b. **Chat agent logic** in `server.ts` powered by the `agents` package and an AI SDK (OpenAI provider by default)
- Describes built-in features: tool system with confirmation, scheduling (one-time, delayed, cron), streaming responses, theme support, state management

a. **Client (Browser)**
- React + Vite bundle
- UI components (message list, tool confirmation dialogs, theme selector)
b. **Edge/Serverless Layer (Cloudflare Worker)**
- **Static Assets Handler**: serves the React app and assets on GET `/`
- **API Handler**: POST `/api/chat` → `server.ts`
- Invokes the `agents` engine
- Loads tool definitions from `tools.ts`
- For auto-executing tools, calls `execute` directly
- For human-in-the-loop tools, awaits confirmation then dispatches to `executions` in `tools.ts`
- Streams partial responses via `streamText` back to client
- **Scheduling Subsystem**: Cron triggers (configured in `wrangler.jsonc`) for delayed/recurring tasks
c. **External Services**
- **AI Model Provider**: OpenAI API (via `@ai-sdk/openai`) or alternative (e.g., `workers-ai-provider`)
- **Cloudflare Cron Scheduler**: triggers Worker for scheduled tasks

##### Relationships and interactions
- The browser loads the static React app from the Worker → the user sends a message → the React client POSTs to the Worker’s chat endpoint → the Worker processes the message through the agent pipeline → calls out to the AI provider and any requested tools → returns a streaming response to the client → the React UI renders messages in real-time.
- Scheduled tasks are created by the agent via a tool. These tasks are orchestrated by Cloudflare’s Cron binding, invoking the Worker at the scheduled time.

##### Architectural patterns and design principles
- **Separation of Concerns**: UI layer (React) vs. agent logic (server.ts) vs. tool implementations (tools.ts + executions)
- **Serverless Edge**: using Cloudflare Workers for low-latency, globally distributed execution
- **Streaming**: real-time response via `streamText`
- **Extensible tool system**: builder pattern in `tools.ts`, pluggable execute vs. confirm semantics
- **Declarative scheduling**: cron/delayed tasks configured in `wrangler.jsonc`

##### Technologies, frameworks, libraries
- Frontend: React, Vite, TypeScript, Tailwind or CSS (styles.css)
- Backend: Cloudflare Workers, `agents` npm package, `@ai-sdk/openai` (or alternative AI SDK), Wrangler CLI
- Testing: Vitest (tests folder)
- CI: GitHub Actions

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser/UI Layer
    subgraph "Browser / UI"
        direction TB
        Client["Client Browser"]:::frontend
        ReactBundle["React + Vite Bundle"]:::frontend
        UIComponents["UI Components"]:::frontend
        Hooks["Custom React Hooks"]:::frontend
        Providers["Context Providers"]:::frontend
    end

    %% Serverless/Edge Layer
    subgraph "Serverless / Edge (Cloudflare Worker)"
        direction TB
        Static["Static Assets Handler (GET /)"]:::backend
        API["Chat API Handler (POST /api/chat)"]:::backend
        Tools["Tool Definitions & Execution"]:::backend
        Scheduling["Scheduling Subsystem (Cron)"]:::backend
    end

    %% External Services
    subgraph "External Services"
        direction TB
        AI["OpenAI API"]:::external
        Cron["Cloudflare Cron Scheduler"]:::external
    end

    %% CI & Testing
    subgraph "CI & Testing"
        direction TB
        CI["GitHub Actions CI Pipeline"]:::other
        Testing["Testing Suite"]:::other
    end

    %% Data Flows
    Client -->|"loads bundle"| Static
    Client -->|"sends chat message"| API
    API -->|"uses agents engine"| Tools
    Tools -->|"calls AI model"| AI
    AI -->|"streams response"| API
    API -->|"streamText response"| Client
    Tools -->|"schedules tasks"| Scheduling
    Cron -->|"triggers cron binding"| Scheduling

    %% Click Events - Browser/UI
    click ReactBundle "https://github.com/cloudflare/agents-starter/blob/main/index.html"
    click ReactBundle "https://github.com/cloudflare/agents-starter/tree/main/public/"
    click ReactBundle "https://github.com/cloudflare/agents-starter/blob/main/vite.config.ts"
    click ReactBundle "https://github.com/cloudflare/agents-starter/blob/main/src/app.tsx"
    click ReactBundle "https://github.com/cloudflare/agents-starter/blob/main/src/client.tsx"
    click ReactBundle "https://github.com/cloudflare/agents-starter/blob/main/src/styles.css"

    click UIComponents "https://github.com/cloudflare/agents-starter/tree/main/src/components/"
    click Hooks "https://github.com/cloudflare/agents-starter/blob/main/src/hooks/useTheme.ts"
    click Hooks "https://github.com/cloudflare/agents-starter/blob/main/src/hooks/useClickOutside.tsx"
    click Hooks "https://github.com/cloudflare/agents-starter/blob/main/src/hooks/useMenuNavigation.tsx"
    click Providers "https://github.com/cloudflare/agents-starter/blob/main/src/providers/ModalProvider.tsx"
    click Providers "https://github.com/cloudflare/agents-starter/blob/main/src/providers/TooltipProvider.tsx"
    click Providers "https://github.com/cloudflare/agents-starter/blob/main/src/providers/index.tsx"

    %% Click Events - Serverless/Edge
    click API "https://github.com/cloudflare/agents-starter/blob/main/src/server.ts"
    click Tools "https://github.com/cloudflare/agents-starter/blob/main/src/tools.ts"
    click Scheduling "https://github.com/cloudflare/agents-starter/blob/main/wrangler.jsonc"

    %% Click Events - Shared Utilities
    click Tools "https://github.com/cloudflare/agents-starter/blob/main/src/utils.ts"
    click Tools "https://github.com/cloudflare/agents-starter/blob/main/src/shared.ts"
    click Tools "https://github.com/cloudflare/agents-starter/blob/main/src/lib/utils.ts"

    %% Click Events - CI & Testing
    click CI "https://github.com/cloudflare/agents-starter/blob/main/.github/workflows/sanity-check.yml"
    click Testing "https://github.com/cloudflare/agents-starter/blob/main/tests/index.test.ts"
    click Testing "https://github.com/cloudflare/agents-starter/blob/main/tests/tsconfig.json"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366D6,color:#0366D6
    classDef backend fill:#DFFFE0,stroke:#22863A,color:#22863A
    classDef external fill:#FFF5B1,stroke:#D29922,color:#D29922
    classDef other fill:#E5E5E5,stroke:#6A737D,color:#6A737D
```
