# canvasxai/sketch architecture

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

- Interactive diagram: https://gitdiagram.com/canvasxai/sketch
- Repository: https://github.com/canvasxai/sketch

## Overview

1. **Overall shape**
   - This is a TypeScript monorepo centered on `packages/server`, which runs the product backend, agent runtime, channel adapters, and HTTP API in one Node.js 24 process.
   - `packages/web` is the React/Vite admin and conversation UI.
   - `packages/shared` contains cross-cutting contracts and validation types.
   - `packages/ui` is the shared design-system/component layer.
   - `apps/macos` is a separate Swift companion app for local Sketch/Claude bridging and menu-bar UX.

2. **Primary runtime boundary**
   - The server is the architectural core. Its entrypoint/bootstrap and `http.ts` wire together API routes, background services, queues, and agent execution.
   - The README’s flow is accurate: external channel message → channel gateway/adapter → per-channel sequential queue → agent runner → Claude Agent SDK → workspace-scoped tools/memory.

3. **Inbound channels**
   - `packages/server/src/slack` handles Slack via Bolt-style integration, including DMs, channel mentions, and thread handling.
   - `packages/server/src/whatsapp` handles WhatsApp via Baileys plus managed/WATI provider support, including group chats, media, templates, and proactive delivery.
   - These adapters normalize inbound messages into the server’s internal message/session model before the agent layer sees them.

4. **Agent execution layer**
   - `packages/server/src/agent` is the core orchestration layer around the Claude Agent SDK.
   - It owns prompt construction, permissions, environment, concurrency limits, tool progress, session management, runtime contracts, and tool wrappers.
   - `packages/server/src/agents` adds higher-level agent definitions, scheduling, output rendering, and delivery policies.

5. **Workspace and safety boundary**
   - The agent runtime is sandboxed per workspace, with file/path guards and tool अनुमति checks before any tool invocation can touch local resources.
   - Workspaces are scoped per user and also shared at channel or team levels where needed.
   - The repo emphasizes this boundary in `packages/server/src/agent/runtime/path-guard.ts`, `workspace-tools.ts`, and related permission/validation modules.

6. **Persistence and domain model**
   - `packages/server/src/db` contains Kysely-based schema, migrations, and repositories.
   - SQLite is the default persistence layer; PostgreSQL is supported for broader deployment/testing scenarios.
   - The data model is broad: users, channels, sessions, scheduled tasks, connectors, entities, file access, API tokens, WhatsApp state, local devices, agent runs, and delivery records.
   - Runtime state is stored under `data/`, including per-user workspaces and the database.

7. **Connector and knowledge-ingestion plane**
   - `packages/server/src/connectors` is the large ingestion/sync subsystem for external systems like Gmail, Google Calendar/Drive, Microsoft Graph, Notion, Linear, ClickUp, Zoho CRM, Fireflies, and others.
   - These connectors do more than fetch data: they normalize, enrich, embed, deduplicate, and map external objects into the internal entity graph.
   - Subpackages like embeddings, search, enrichment, sync, hierarchy mapping, and entity linking show that this is a pipeline, not a simple API wrapper.

8. **Entity graph and materialization**
   - `packages/server/src/entities` is where external facts and conversations are turned into durable org knowledge.
   - It contains materializers for people, projects, tasks, features, decisions, commitments, mentions, relations, and replay/merge/review flows.
   - This layer sits between raw connector data and the user-facing “knowledge graph” / review workflows in the web app.

9. **Web application surfaces**
   - `packages/web/src/routes` defines the user-facing application: onboarding, dashboard, chat, channels, connections, files, projects, review queue, settings, skills, team, usage, and workspace browsing.
   - The UI consumes the server API via `packages/web/src/lib/api.ts` and exposes operational/admin workflows for connecting integrations, configuring channels, and reviewing entity suggestions.
   - The “files” and “workspace” routes are especially important because they expose the user’s isolated storage and graph-backed browsing experience.

10. **Auth, identity, and per-user integrations**
   - `packages/server/src/auth` and `packages/server/src/api/auth*` implement login and identity flows including JWT, magic links, password/email verification, OAuth token handling, and API tokens.
   - `packages/server/src/api/connectors*`, `provider-identities.ts`, and related repositories support per-user authorization so integrations act on a specific user’s behalf.
   - The architecture strongly separates org-level capabilities from user-owned credentials and permissions.

11. **Automation, scheduling, and delivery**
   - `packages/server/src/scheduler`, `packages/server/src/workflows`, `packages/server/src/automation`, and `packages/server/src/agents` cover background execution for scheduled tasks, daily briefs, delivery capture, and workflow steps.
   - These jobs can target channels, conversations, inbox workflows, or external delivery endpoints.
   - This is the main async/cron plane alongside live chat handling.

12. **Local device and companion-client path**
   - `packages/server/src/local-devices` plus `packages/server/src/api/local-devices.ts` support local Claude session bridging and device tokens.
   - `apps/macos` provides the native front-end for that local experience, with keychain storage and a relay client, implying a sidecar/companion relationship rather than a standalone app backend.

13. **Operational and maintenance plane**
   - The repo includes many DB migrations plus backfill/eval scripts under `scripts/` and `packages/server/scripts`, indicating active data correction and model-evolution workflows.
   - GitHub Actions, Docker assets, and the self-hosting docs show the deployment model: self-hostable, typically one server process plus storage, with optional containerization.
   - Biome, Vitest, pnpm workspaces, and tsdown are the main build/test toolchain, but they are secondary to the runtime architecture.

Main entry points: [`packages/server/src/agent`](https://github.com/canvasxai/sketch/tree/main/packages/server/src/agent) (Agent Core), [`packages/server/src/http.ts`](https://github.com/canvasxai/sketch/blob/main/packages/server/src/http.ts) (HTTP API), [`packages/server/src/db`](https://github.com/canvasxai/sketch/tree/main/packages/server/src/db) (Database), [`packages/server/src/bootstrap.ts`](https://github.com/canvasxai/sketch/blob/main/packages/server/src/bootstrap.ts) (Bootstrap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Server"]
  node_server_bootstrap["Bootstrap<br/>entry/http<br/>[bootstrap.ts]"]
  node_server_http["HTTP API<br/>api/router<br/>[http.ts]"]
  node_server_agent{{"Agent Core<br/>runtime/orchestrator"}}
  node_server_channels["Channels<br/>adapters/inbound"]
  node_server_connectors{{"Connectors<br/>ingestion/pipeline"}}
  node_server_entities{{"Entity Graph<br/>knowledge/materialization"}}
  node_server_workflows["Automation<br/>async/jobs"]
  node_server_scheduler["Scheduler<br/>cron/runner"]
  node_server_db[("Database<br/>persistence/kysely")]
  node_server_auth["Auth<br/>identity/security"]
  node_server_local["Local Devices<br/>bridge/device"]
  node_server_workspace{{"Workspace<br/>scope/safety"}}
  node_server_mcp["MCP<br/>tooling/protocol"]
end

subgraph group_web["Web App"]
  node_web_routes["Web Routes<br/>ui/pages"]
  node_web_components["Web Components<br/>ui/features"]
  node_web_api["Web API Client<br/>client/api<br/>[api.ts]"]
end

subgraph group_shared["Shared Packages"]
  node_shared_contracts["Contracts<br/>shared/types"]
  node_ui_kit["UI Kit<br/>shared/design-system"]
end

subgraph group_macos["MacOS Companion"]
  node_macos_app["MacOS App<br/>native/client"]
end

subgraph group_ops["Operations"]
  node_ops_scripts["Scripts<br/>maintenance/tools"]
  node_ops_migrations["Migrations<br/>schema/history"]
end

node_server_bootstrap -->|"wires"| node_server_http
node_server_bootstrap -->|"starts"| node_server_agent
node_server_bootstrap -->|"starts"| node_server_channels
node_server_http -->|"auth"| node_server_auth
node_server_http -->|"scope"| node_server_workspace
node_server_http -->|"bridge"| node_server_local
node_server_http -->|"configure"| node_server_connectors
node_server_channels -->|"dispatch"| node_server_agent
node_server_channels -->|"persist"| node_server_db
node_server_agent -->|"guard"| node_server_workspace
node_server_agent -->|"store"| node_server_db
node_server_agent -->|"tools"| node_server_mcp
node_server_connectors -->|"materialize"| node_server_entities
node_server_connectors -->|"sync state"| node_server_db
node_server_entities -->|"persist"| node_server_db
node_server_scheduler -->|"run"| node_server_workflows
node_server_workflows -->|"invoke"| node_server_agent
node_server_workflows -->|"record"| node_server_db
node_web_routes -->|"calls"| node_web_api
node_web_routes -->|"composes"| node_web_components
node_web_components -->|"uses"| node_ui_kit
node_web_api -->|"types"| node_shared_contracts
node_server_http -->|"types"| node_shared_contracts
node_server_agent -->|"types"| node_shared_contracts
node_macos_app -->|"relays"| node_server_local
node_ops_scripts -.->|"repair"| node_server_db
node_ops_migrations -.->|"evolve"| node_server_db

click node_server_bootstrap "https://github.com/canvasxai/sketch/blob/main/packages/server/src/bootstrap.ts"
click node_server_http "https://github.com/canvasxai/sketch/blob/main/packages/server/src/http.ts"
click node_server_agent "https://github.com/canvasxai/sketch/tree/main/packages/server/src/agent"
click node_server_channels "https://github.com/canvasxai/sketch/tree/main/packages/server/src/slack"
click node_server_connectors "https://github.com/canvasxai/sketch/tree/main/packages/server/src/connectors"
click node_server_entities "https://github.com/canvasxai/sketch/tree/main/packages/server/src/entities"
click node_server_workflows "https://github.com/canvasxai/sketch/tree/main/packages/server/src/workflows"
click node_server_scheduler "https://github.com/canvasxai/sketch/tree/main/packages/server/src/scheduler"
click node_server_db "https://github.com/canvasxai/sketch/tree/main/packages/server/src/db"
click node_server_auth "https://github.com/canvasxai/sketch/tree/main/packages/server/src/auth"
click node_server_local "https://github.com/canvasxai/sketch/tree/main/packages/server/src/local-devices"
click node_server_workspace "https://github.com/canvasxai/sketch/tree/main/packages/server/src/workspace"
click node_server_mcp "https://github.com/canvasxai/sketch/tree/main/packages/server/src/mcp"
click node_web_routes "https://github.com/canvasxai/sketch/tree/main/packages/web/src/routes"
click node_web_components "https://github.com/canvasxai/sketch/tree/main/packages/web/src/components"
click node_web_api "https://github.com/canvasxai/sketch/blob/main/packages/web/src/lib/api.ts"
click node_shared_contracts "https://github.com/canvasxai/sketch/tree/main/packages/shared/src"
click node_ui_kit "https://github.com/canvasxai/sketch/tree/main/packages/ui/src"
click node_macos_app "https://github.com/canvasxai/sketch/tree/main/apps/macos/Sources/SketchLocal"
click node_ops_scripts "https://github.com/canvasxai/sketch/tree/main/scripts"
click node_ops_migrations "https://github.com/canvasxai/sketch/tree/main/packages/server/src/db/migrations"

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_server_bootstrap,node_server_http,node_server_agent,node_server_channels,node_server_connectors,node_server_entities,node_server_workflows,node_server_scheduler,node_server_db,node_server_auth,node_server_local,node_server_workspace,node_server_mcp toneBlue
class node_web_routes,node_web_components,node_web_api toneAmber
class node_shared_contracts,node_ui_kit toneMint
class node_macos_app toneRose
class node_ops_scripts,node_ops_migrations toneIndigo
```

## Components

### Server

Main Node.js backend: API, agent runtime, channels, persistence, connectors, and async jobs.

- [Bootstrap](https://github.com/canvasxai/sketch/blob/main/packages/server/src/bootstrap.ts): entry/http · Starts the server process and wires the runtime together. · `packages/server/src/bootstrap.ts`
- [HTTP API](https://github.com/canvasxai/sketch/blob/main/packages/server/src/http.ts): api/router · Top-level HTTP surface for auth, chats, settings, workspace, and operational endpoints. · `packages/server/src/http.ts`
- [Agent Core](https://github.com/canvasxai/sketch/tree/main/packages/server/src/agent): runtime/orchestrator · Claude Agent SDK orchestration: prompts, permissions, sessions, tools, concurrency, and progress. · `packages/server/src/agent`
- [Channels](https://github.com/canvasxai/sketch/tree/main/packages/server/src/slack): adapters/inbound · Slack and WhatsApp adapters that normalize external messages into internal conversations. · `packages/server/src/slack`
- [Connectors](https://github.com/canvasxai/sketch/tree/main/packages/server/src/connectors): ingestion/pipeline · Sync, enrich, embed, and normalize external systems into internal facts and entities. · `packages/server/src/connectors`
- [Entity Graph](https://github.com/canvasxai/sketch/tree/main/packages/server/src/entities): knowledge/materialization · Turns incoming facts and conversations into durable people, projects, tasks, decisions, and relations. · `packages/server/src/entities`
- [Automation](https://github.com/canvasxai/sketch/tree/main/packages/server/src/workflows): async/jobs · Scheduled tasks, delivery flows, and background workflow execution. · `packages/server/src/workflows`
- [Scheduler](https://github.com/canvasxai/sketch/tree/main/packages/server/src/scheduler): cron/runner · Schedules and dispatches timed jobs and delivery capture. · `packages/server/src/scheduler`
- [Database](https://github.com/canvasxai/sketch/tree/main/packages/server/src/db): persistence/kysely · Schema, migrations, and repositories over SQLite/PostgreSQL. · `packages/server/src/db`
- [Auth](https://github.com/canvasxai/sketch/tree/main/packages/server/src/auth): identity/security · JWT, magic links, OAuth, API tokens, and per-user credential handling. · `packages/server/src/auth`
- [Local Devices](https://github.com/canvasxai/sketch/tree/main/packages/server/src/local-devices): bridge/device · Local Claude session bridging and device token gateway. · `packages/server/src/local-devices`
- [Workspace](https://github.com/canvasxai/sketch/tree/main/packages/server/src/workspace): scope/safety · Workspace scoping, validation, and file/path guards for tool access. · `packages/server/src/workspace`
- [MCP](https://github.com/canvasxai/sketch/tree/main/packages/server/src/mcp): tooling/protocol · OAuth and server transport for MCP servers and tool exposure. · `packages/server/src/mcp`

### Web App

React/Vite admin and conversation UI.

- [Web Routes](https://github.com/canvasxai/sketch/tree/main/packages/web/src/routes): ui/pages · User-facing routes for onboarding, chat, channels, connections, files, review, settings, team, and usage. · `packages/web/src/routes`
- [Web Components](https://github.com/canvasxai/sketch/tree/main/packages/web/src/components): ui/features · Feature components for agents, briefs, entities, connections, skills, workspace, and chat surfaces. · `packages/web/src/components`
- [Web API Client](https://github.com/canvasxai/sketch/blob/main/packages/web/src/lib/api.ts): client/api · Typed client used by the web app to call the server API. · `packages/web/src/lib/api.ts`

### Shared Packages

Cross-cutting contracts and reusable UI.

- [Contracts](https://github.com/canvasxai/sketch/tree/main/packages/shared/src): shared/types · Shared validation and domain contracts used by server and web. · `packages/shared/src`
- [UI Kit](https://github.com/canvasxai/sketch/tree/main/packages/ui/src): shared/design-system · Reusable component primitives and styling used by the web app. · `packages/ui/src`

### MacOS Companion

Native local bridge and menu-bar client.

- [MacOS App](https://github.com/canvasxai/sketch/tree/main/apps/macos/Sources/SketchLocal): native/client · Swift menu-bar companion app for local bridging and UX. · `apps/macos/Sources/SketchLocal`

### Operations

Scripts, migrations, and deployment/maintenance surface.

- [Scripts](https://github.com/canvasxai/sketch/tree/main/scripts): maintenance/tools · Backfills, checks, recomputation, and one-off maintenance tasks. · `scripts`
- [Migrations](https://github.com/canvasxai/sketch/tree/main/packages/server/src/db/migrations): schema/history · Database evolution history and data corrections. · `packages/server/src/db/migrations`

## Connections

- Bootstrap → HTTP API: wires · Bootstraps the HTTP surface and runtime services.
- Bootstrap → Agent Core: starts · Initializes agent runtime subsystems.
- Bootstrap → Channels: starts · Brings up Slack and WhatsApp listeners/adapters.
- HTTP API → Auth: auth · Handles login, tokens, OAuth, and session identity.
- HTTP API → Workspace: scope · Exposes workspace browsing and validation APIs.
- HTTP API → Local Devices: bridge · Manages local device/session bridging endpoints.
- HTTP API → Connectors: configure · Creates and manages external integrations.
- Channels → Agent Core: dispatch · Routes normalized inbound messages to agent execution.
- Channels → Database: persist · Stores channel/session state and message metadata.
- Agent Core → Workspace: guard · Constrains tools and file access to workspace boundaries.
- Agent Core → Database: store · Persists runs, sessions, messages, usage, and outputs.
- Agent Core → MCP: tools · Calls out through MCP tool transport when configured.
- Connectors → Entity Graph: materialize · Converts synced external data into durable knowledge objects.
- Connectors → Database: sync state · Keeps connector credentials, cursors, and sync state.
- Entity Graph → Database: persist · Writes entity graph, review, merge, and provenance records.
- Scheduler → Automation: run · Triggers scheduled execution and delivery capture.
- Automation → Agent Core: invoke · Uses agent runtime for scheduled briefs and automated outputs.
- Automation → Database: record · Stores automation runs and delivery results.
- Web Routes → Web API Client: calls · Consumes the server API from page-level routes.
- Web Routes → Web Components: composes · Builds screens from reusable feature components.
- Web Components → UI Kit: uses · Renders with shared design-system primitives.
- Web API Client → Contracts: types · Relies on shared request/response and validation contracts.
- HTTP API → Contracts: types · Uses shared contracts for API and workspace validation.
- Agent Core → Contracts: types · Shares agent and workspace contracts with the rest of the stack.
- MacOS App → Local Devices: relays · Acts as the native companion for local session/device bridging.
- Scripts → Database: repair · Runs backfills and consistency checks against stored data.
- Migrations → Database: evolve · Changes the persisted schema over time.
