# ilrein/openwrite architecture

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

- Interactive diagram: https://gitdiagram.com/ilrein/openwrite
- Repository: https://github.com/ilrein/openwrite

## Overview

OpenWrite is a web platform for planning and writing long-form fiction. A writer creates a project, develops a story map, expands story elements with AI, promotes chapter nodes into the manuscript, and edits chapters with autosave; Codex entries provide structured world-building alongside that workflow. The web client talks to a Hono API backed by D1, with authentication and encrypted user-supplied AI credentials. The README documents product capabilities; sampled source confirms key server wiring and graph-generation calls, while several UI and API-client connections are based on product documentation rather than inspected call sites.

Main entry points: [`apps/server/src/routers/index.ts`](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/index.ts) (Application routes), [`apps/server/src/routers/graph.ts`](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/graph.ts) (Story graph), [`apps/server/src/db/index.ts`](https://github.com/ilrein/openwrite/blob/main/apps/server/src/db/index.ts) (D1 database), [`apps/web/src/routes/dashboard/projects.tsx`](https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/dashboard/projects.tsx) (Project workspace)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Writing workspace"]
  node_webapp["Web application<br/>[main.tsx]"]
  node_projects["Project workspace<br/>[projects.tsx]"]
  node_canvas["Story map<br/>[canvas.tsx]"]
  node_codex["World codex<br/>[$type.tsx]"]
  node_editor["Manuscript editor<br/>[write.tsx]"]
  node_client["API clients<br/>[index.ts]"]
end

subgraph group_api["Application API"]
  node_api_entry["Worker API<br/>[index.ts]"]
  node_auth_routes["Auth and workspace<br/>[auth.ts]"]
  node_router["Application routes<br/>[index.ts]"]
  node_content["Chapters and content<br/>[content.ts]"]
  node_graph["Story graph<br/>[graph.ts]"]
  node_ai["AI completion<br/>[ai.ts]"]
  node_providers["AI key management<br/>[ai-providers.ts]"]
  node_expansion["Story expansion<br/>[story-expansion.ts]"]
end

subgraph group_data["Persistence and identity"]
  node_database[("D1 database<br/>[index.ts]")]
  node_schema["Domain schema<br/>[project.ts]"]
  node_middleware["Access middleware<br/>[auth.ts]"]
end

subgraph group_integrations["External services"]
  node_ai_service{{"AI providers"}}
  node_email_service{{"Email delivery"}}
end

node_writer(("Writer"))

node_writer -->|"uses"| node_webapp
node_webapp -.->|"routes to"| node_projects
node_projects -->|"opens story map"| node_canvas
node_projects -->|"opens codex"| node_codex
node_projects -->|"opens manuscript"| node_editor
node_webapp -.->|"requests data"| node_client
node_client -.->|"calls API"| node_api_entry
node_api_entry -->|"mounts"| node_router
node_api_entry -->|"forwards auth"| node_auth_routes
node_router -->|"mounts"| node_content
node_router -->|"mounts"| node_graph
node_router -->|"mounts"| node_ai
node_router -->|"mounts"| node_providers
node_graph -->|"parses expansion"| node_expansion
node_graph -->|"requests completion"| node_ai
node_graph -->|"promotes chapter"| node_content
node_content -->|"reads and writes"| node_database
node_graph -->|"reads and writes"| node_database
node_middleware -->|"checks session"| node_auth_routes
node_middleware -->|"checks membership"| node_database
node_auth_routes -->|"stores identity"| node_database
node_auth_routes -->|"sends actions"| node_email_service
node_ai -->|"calls provider"| node_ai_service
node_providers -->|"configures keys"| node_ai_service
node_database -.->|"uses schema"| node_schema

click node_webapp "https://github.com/ilrein/openwrite/blob/main/apps/web/src/main.tsx"
click node_projects "https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/dashboard/projects.tsx"
click node_canvas "https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/canvas.tsx"
click node_codex "https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/codex/%24type.tsx"
click node_editor "https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/write.tsx"
click node_client "https://github.com/ilrein/openwrite/blob/main/apps/web/src/lib/api/index.ts"
click node_api_entry "https://github.com/ilrein/openwrite/blob/main/apps/server/src/index.ts"
click node_auth_routes "https://github.com/ilrein/openwrite/blob/main/apps/server/src/lib/auth.ts"
click node_router "https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/index.ts"
click node_content "https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/content.ts"
click node_graph "https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/graph.ts"
click node_ai "https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/ai.ts"
click node_providers "https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/ai-providers.ts"
click node_expansion "https://github.com/ilrein/openwrite/blob/main/apps/server/src/lib/story-expansion.ts"
click node_database "https://github.com/ilrein/openwrite/blob/main/apps/server/src/db/index.ts"
click node_schema "https://github.com/ilrein/openwrite/blob/main/apps/server/src/db/schema/project.ts"
click node_middleware "https://github.com/ilrein/openwrite/blob/main/apps/server/src/middleware/auth.ts"

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_webapp,node_projects,node_canvas,node_codex,node_editor,node_client toneBlue
class node_api_entry,node_auth_routes,node_router,node_content,node_graph,node_ai,node_providers,node_expansion toneAmber
class node_database,node_schema,node_middleware toneMint
class node_ai_service,node_email_service toneRose
class node_writer toneIndigo
```

## Components

### Writing workspace

- [Web application](https://github.com/ilrein/openwrite/blob/main/apps/web/src/main.tsx): `apps/web/src/main.tsx`
- [Project workspace](https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/dashboard/projects.tsx): `apps/web/src/routes/dashboard/projects.tsx`
- [Story map](https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/canvas.tsx): `apps/web/src/routes/projects/$projectId/canvas.tsx`
- [World codex](https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/codex/%24type.tsx): `apps/web/src/routes/projects/$projectId/codex/$type.tsx`
- [Manuscript editor](https://github.com/ilrein/openwrite/blob/main/apps/web/src/routes/projects/%24projectId/write.tsx): `apps/web/src/routes/projects/$projectId/write.tsx`
- [API clients](https://github.com/ilrein/openwrite/blob/main/apps/web/src/lib/api/index.ts): `apps/web/src/lib/api/index.ts`

### Application API

- [Worker API](https://github.com/ilrein/openwrite/blob/main/apps/server/src/index.ts): `apps/server/src/index.ts`
- [Auth and workspace](https://github.com/ilrein/openwrite/blob/main/apps/server/src/lib/auth.ts): `apps/server/src/lib/auth.ts`
- [Application routes](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/index.ts): `apps/server/src/routers/index.ts`
- [Chapters and content](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/content.ts): `apps/server/src/routers/content.ts`
- [Story graph](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/graph.ts): `apps/server/src/routers/graph.ts`
- [AI completion](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/ai.ts): `apps/server/src/routers/ai.ts`
- [AI key management](https://github.com/ilrein/openwrite/blob/main/apps/server/src/routers/ai-providers.ts): `apps/server/src/routers/ai-providers.ts`
- [Story expansion](https://github.com/ilrein/openwrite/blob/main/apps/server/src/lib/story-expansion.ts): `apps/server/src/lib/story-expansion.ts`

### Persistence and identity

- [D1 database](https://github.com/ilrein/openwrite/blob/main/apps/server/src/db/index.ts): `apps/server/src/db/index.ts`
- [Domain schema](https://github.com/ilrein/openwrite/blob/main/apps/server/src/db/schema/project.ts): `apps/server/src/db/schema/project.ts`
- [Access middleware](https://github.com/ilrein/openwrite/blob/main/apps/server/src/middleware/auth.ts): `apps/server/src/middleware/auth.ts`

### External services

- **AI providers**
- **Email delivery**

### Other components

- **Writer**

## Connections

- Writer → Web application: uses (evidence: `README.md`)
- Web application → Project workspace: routes to
- Project workspace → Story map: opens story map (evidence: `README.md`)
- Project workspace → World codex: opens codex (evidence: `README.md`)
- Project workspace → Manuscript editor: opens manuscript (evidence: `README.md`)
- Web application → API clients: requests data
- API clients → Worker API: calls API
- Worker API → Application routes: mounts (evidence: `apps/server/src/index.ts`)
- Worker API → Auth and workspace: forwards auth (evidence: `apps/server/src/index.ts`)
- Application routes → Chapters and content: mounts (evidence: `apps/server/src/routers/index.ts`)
- Application routes → Story graph: mounts (evidence: `apps/server/src/routers/index.ts`)
- Application routes → AI completion: mounts (evidence: `apps/server/src/routers/index.ts`)
- Application routes → AI key management: mounts (evidence: `apps/server/src/routers/index.ts`)
- Story graph → Story expansion: parses expansion (evidence: `apps/server/src/routers/graph.ts`)
- Story graph → AI completion: requests completion (evidence: `apps/server/src/routers/graph.ts`)
- Story graph → Chapters and content: promotes chapter (evidence: `apps/server/src/routers/graph.ts`)
- Chapters and content → D1 database: reads and writes (evidence: `apps/server/src/routers/content.ts`)
- Story graph → D1 database: reads and writes (evidence: `apps/server/src/routers/graph.ts`)
- Access middleware → Auth and workspace: checks session (evidence: `apps/server/src/middleware/auth.ts`)
- Access middleware → D1 database: checks membership (evidence: `apps/server/src/middleware/auth.ts`)
- Auth and workspace → D1 database: stores identity (evidence: `apps/server/src/lib/auth.ts`)
- Auth and workspace → Email delivery: sends actions (evidence: `apps/server/src/lib/auth.ts`)
- AI completion → AI providers: calls provider (evidence: `README.md`)
- AI key management → AI providers: configures keys (evidence: `README.md`)
- D1 database → Domain schema: uses schema
