# jorguzman100/ghostwriter architecture

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

- Interactive diagram: https://gitdiagram.com/jorguzman100/ghostwriter
- Repository: https://github.com/jorguzman100/ghostwriter

## Overview

#### Purpose and Entry Points
Ghostwriter is a client/server AI drafting tool for blog posts, emails, stories, and social posts. Users submit content-specific inputs, receive a processed draft plus review metadata, edit it, and export locally. The browser starts at `frontend/src/main.tsx` and renders the application in `frontend/src/App.tsx`. FastAPI is launched through `backend/app/main.py`.

#### Frontend Interaction and Export Boundary
The React/Vite frontend owns content-type selection, dynamic forms, generation settings, draft editing, dashboard presentation, and browser-side exports (plain text, rich text/HTML, Markdown, print/PDF). `frontend/src/App.tsx` is the primary UI/controller boundary; `frontend/src/App.css` contains its presentation layer. The frontend sends generation and delayed live-review requests to the API, then treats returned draft/check results as authoritative.

#### API Orchestration
`backend/app/main.py` defines the FastAPI application and request lifecycle: accept user payloads, validate them, normalize content-type-specific fields, build prompts, invoke the configured model provider, then return cleaned content and diagnostics. It also supports re-checking edited drafts rather than requiring another model generation. API schemas and stable response/request contracts are defined in `backend/app/schemas.py`.

#### Input Canonicalization and Prompt Assembly
Requests cross a normalization boundary before AI use: `backend/app/normalizer.py` converts differing blog, email, story, and social form shapes into a consistent internal representation. `backend/app/prompt_templates.py` selects content-specific instructions and examples, incorporating normalized fields plus generation controls such as temperature, token limit, and top-p. This keeps provider-facing prompts separate from UI payload design.

#### AI Provider Adapter and Configuration
`backend/app/ai_client.py` encapsulates model calls and provider selection. Runtime configuration is centralized in `backend/app/config.py`, using environment settings to select local Ollama by default or OpenAI when configured. There is no implied fallback between providers: the configured service is the generation dependency. Environment examples are provided in `.env.example`.

#### Post-Generation Quality Pipeline
Raw model output is processed before delivery. `backend/app/postprocessor.py` cleans formatting, Markdown, punctuation/capitalization, labels, and common AI artifacts; it also derives structure, word-count, platform-fit, and export-readiness feedback. `backend/app/safety.py` adds local heuristic checks for unsafe or unprofessional language. These checks are also relevant to the edited-draft review flow, separating generated text from final user-edited text.

#### Deployment Topology
`docker-compose.yml` composes the frontend, FastAPI backend, and Ollama runtime, with a persistent model volume for the default `gemma3:4b` download. Container boundaries are defined by `frontend/Dockerfile` and `backend/Dockerfile`. In Docker, the frontend is exposed at port 5173; the backend communicates with the configured AI provider over its service/network boundary. Local development can instead run Vite, Uvicorn, and a locally installed Ollama instance independently.

Main entry points: [`backend/app/ai_client.py`](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/ai_client.py) (Provider adapter), [`backend/app/main.py`](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/main.py) (API lifecycle), [`frontend/src/App.tsx`](https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/App.tsx) (React app controller), [`docker-compose.yml`](https://github.com/jorguzman100/ghostwriter/blob/main/docker-compose.yml) (Compose topology)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_browser(("Browser user<br/>client"))
  node_app["React app controller<br/>React/Vite UI<br/>[App.tsx]"]
  node_styles["Presentation layer<br/>CSS<br/>[App.css]"]
  node_frontend_entry["Frontend entry<br/>React bootstrap<br/>[main.tsx]"]
  node_exports["Local exports<br/>browser export"]
end

subgraph group_api["FastAPI backend"]
  node_api_main["API lifecycle<br/>FastAPI app<br/>[main.py]"]
  node_schemas["API contracts<br/>Pydantic schemas<br/>[schemas.py]"]
  node_normalizer["Input normalizer<br/>canonicalization<br/>[normalizer.py]"]
  node_prompts["Prompt assembly<br/>prompt templates"]
end

subgraph group_ai["AI and quality pipeline"]
  node_ai_client{{"Provider adapter<br/>AI client<br/>[ai_client.py]"}}
  node_config["Runtime configuration<br/>environment config<br/>[config.py]"]
  node_postprocessor["Draft postprocessor<br/>content cleanup<br/>[postprocessor.py]"]
  node_safety["Safety review<br/>heuristic checks<br/>[safety.py]"]
  node_openai{{"OpenAI<br/>model provider"}}
end

subgraph group_deploy["Deployment"]
  node_ollama{{"Ollama runtime<br/>model provider"}}
  node_compose["Compose topology<br/>Docker Compose<br/>[docker-compose.yml]"]
  node_frontend_image["Frontend container<br/>Docker image"]
  node_backend_image["Backend container<br/>Docker image"]
  node_env_example["Environment example<br/>env template<br/>[.env.example]"]
end

node_browser -->|"loads"| node_frontend_entry
node_frontend_entry -->|"renders"| node_app
node_app -->|"uses"| node_styles
node_app -->|"generate and review requests"| node_api_main
node_app -->|"exports edited draft"| node_exports
node_api_main -->|"validates contracts"| node_schemas
node_api_main -->|"normalizes input"| node_normalizer
node_normalizer -->|"canonical fields"| node_prompts
node_prompts -->|"provider prompt"| node_ai_client
node_ai_client -->|"reads provider selection"| node_config
node_ai_client -->|"default generation"| node_ollama
node_ai_client -.->|"when configured"| node_openai
node_ai_client -->|"raw model output"| node_postprocessor
node_postprocessor -->|"cleaned draft"| node_safety
node_safety -->|"draft and diagnostics"| node_api_main
node_api_main -->|"edited-draft recheck"| node_safety
node_compose -->|"builds and runs"| node_frontend_image
node_compose -->|"builds and runs"| node_backend_image
node_compose -->|"runs with model volume"| node_ollama
node_env_example -.->|"documents settings"| node_config

click node_app "https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/App.tsx"
click node_styles "https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/App.css"
click node_frontend_entry "https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/main.tsx"
click node_api_main "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/main.py"
click node_schemas "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/schemas.py"
click node_normalizer "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/normalizer.py"
click node_prompts "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/prompt_templates.py"
click node_ai_client "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/ai_client.py"
click node_config "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/config.py"
click node_postprocessor "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/postprocessor.py"
click node_safety "https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/safety.py"
click node_compose "https://github.com/jorguzman100/ghostwriter/blob/main/docker-compose.yml"
click node_frontend_image "https://github.com/jorguzman100/ghostwriter/blob/main/frontend/Dockerfile"
click node_backend_image "https://github.com/jorguzman100/ghostwriter/blob/main/backend/Dockerfile"
click node_env_example "https://github.com/jorguzman100/ghostwriter/blob/main/.env.example"

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_browser,node_app,node_styles,node_frontend_entry,node_exports toneBlue
class node_api_main,node_schemas,node_normalizer,node_prompts toneAmber
class node_ai_client,node_config,node_postprocessor,node_safety,node_openai toneMint
class node_ollama,node_compose,node_frontend_image,node_backend_image,node_env_example toneRose
```

## Components

### Frontend

Browser UI and local export.

- **Browser user**: client
- [React app controller](https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/App.tsx): React/Vite UI · Owns forms, editing, dashboard, and API calls. · `frontend/src/App.tsx`
- [Presentation layer](https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/App.css): CSS · `frontend/src/App.css`
- [Frontend entry](https://github.com/jorguzman100/ghostwriter/blob/main/frontend/src/main.tsx): React bootstrap · `frontend/src/main.tsx`
- **Local exports**: browser export · Text, HTML, Markdown, and print/PDF exports stay in the browser.

### FastAPI backend

Request orchestration and draft review.

- [API lifecycle](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/main.py): FastAPI app · Validates requests, orchestrates generation, and supports edited-draft review. · `backend/app/main.py`
- [API contracts](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/schemas.py): Pydantic schemas · `backend/app/schemas.py`
- [Input normalizer](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/normalizer.py): canonicalization · Converts content-specific form payloads to a shared representation. · `backend/app/normalizer.py`
- [Prompt assembly](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/prompt_templates.py): prompt templates · Builds content-specific provider prompts from normalized inputs and controls. · `backend/app/prompt_templates.py`

### AI and quality pipeline

Configured generation and local checks.

- [Provider adapter](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/ai_client.py): AI client · Selects and invokes the configured model provider. · `backend/app/ai_client.py`
- [Runtime configuration](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/config.py): environment config · Selects Ollama by default or OpenAI when configured. · `backend/app/config.py`
- [Draft postprocessor](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/postprocessor.py): content cleanup · Cleans model output and derives review metadata. · `backend/app/postprocessor.py`
- [Safety review](https://github.com/jorguzman100/ghostwriter/blob/main/backend/app/safety.py): heuristic checks · Checks generated and edited drafts for unsafe or unprofessional language. · `backend/app/safety.py`
- **OpenAI**: model provider · Alternative configured generation dependency.

### Deployment

Containerized default topology.

- **Ollama runtime**: model provider · Default service hosting gemma3:4b.
- [Compose topology](https://github.com/jorguzman100/ghostwriter/blob/main/docker-compose.yml): Docker Compose · Composes frontend, backend, Ollama, and persistent model storage. · `docker-compose.yml`
- [Frontend container](https://github.com/jorguzman100/ghostwriter/blob/main/frontend/Dockerfile): Docker image · Exposes the frontend on port 5173. · `frontend/Dockerfile`
- [Backend container](https://github.com/jorguzman100/ghostwriter/blob/main/backend/Dockerfile): Docker image · `backend/Dockerfile`
- [Environment example](https://github.com/jorguzman100/ghostwriter/blob/main/.env.example): env template · `.env.example`

## Connections

- Browser user → Frontend entry: loads
- Frontend entry → React app controller: renders
- React app controller → Presentation layer: uses
- React app controller → API lifecycle: generate and review requests
- React app controller → Local exports: exports edited draft
- API lifecycle → API contracts: validates contracts
- API lifecycle → Input normalizer: normalizes input
- Input normalizer → Prompt assembly: canonical fields
- Prompt assembly → Provider adapter: provider prompt
- Provider adapter → Runtime configuration: reads provider selection
- Provider adapter → Ollama runtime: default generation
- Provider adapter → OpenAI: when configured
- Provider adapter → Draft postprocessor: raw model output
- Draft postprocessor → Safety review: cleaned draft
- Safety review → API lifecycle: draft and diagnostics
- API lifecycle → Safety review: edited-draft recheck
- Compose topology → Frontend container: builds and runs
- Compose topology → Backend container: builds and runs
- Compose topology → Ollama runtime: runs with model volume
- Environment example → Runtime configuration: documents settings
