# srijitagoswami/mailxp architecture

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

- Interactive diagram: https://gitdiagram.com/srijitagoswami/mailxp
- Repository: https://github.com/srijitagoswami/mailxp

## Overview

MailXP combines a Phaser office game with a Next.js inbox and progression app. The main player journey is to connect Gmail, sync and read messages, optionally request advisory AI analysis, and advance through game, rewards, and shop features. The graph preserves the documented product branches and central runtime modules. Source excerpts are partial; relationships not established by them or the README are omitted rather than inferred.

Main entry points: [`app/api/v1`](https://github.com/srijitagoswami/mailxp/tree/main/app/api/v1) (Versioned API), [`lib/server/aiAnalysis.ts`](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/aiAnalysis.ts) (AI analysis), [`lib/server/worker.ts`](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/worker.ts) (Job worker), [`components/apiClient.ts`](https://github.com/srijitagoswami/mailxp/blob/main/components/apiClient.ts) (API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Player experience"]
  node_app["App shell<br/>[AppShell.tsx]"]
  node_inbox["Inbox screen<br/>[InboxScreen.tsx]"]
  node_settings["Settings screen<br/>[SettingsScreen.tsx]"]
  node_shopui["Shop screen<br/>[ShopScreen.tsx]"]
  node_client["API client<br/>[apiClient.ts]"]
end

subgraph group_game["Game runtime"]
  node_canvas["Phaser canvas<br/>[PhaserCanvas.tsx]"]
  node_office["Office scene<br/>[OfficeScene.ts]"]
  node_quests["Quest config<br/>[questConfig.ts]"]
  node_bridge["Game bridge<br/>[bridge.ts]"]
  node_profile["Local profile<br/>[profile.ts]"]
  node_profile_store[("Profile storage<br/>[profileStorage.ts]")]
  node_economy["Game economy<br/>[economy.ts]"]
end

subgraph group_mail["Mail intelligence"]
  node_gmail["Gmail sync<br/>[gmailSync.ts]"]
  node_google["Google OAuth<br/>[googleOAuth.ts]"]
  node_analysis["AI analysis<br/>[aiAnalysis.ts]"]
end

subgraph group_platform["API and identity"]
  node_api["Versioned API"]
  node_contract["API contract<br/>[contract.ts]"]
  node_session["Sessions<br/>[session.ts]"]
  node_http["HTTP helpers<br/>[http.ts]"]
end

subgraph group_services["Server services"]
  node_db[("PostgreSQL<br/>[prisma.ts]")]
  node_worker["Job worker<br/>[worker.ts]"]
  node_jobs["Job queue<br/>[jobQueue.ts]"]
  node_outbox[("Event outbox<br/>[outbox.ts]")]
  node_audit[("Audit records<br/>[audit.ts]")]
  node_events["Live events<br/>[sseHub.ts]"]
  node_rate["Rate limits<br/>[rateLimit.ts]"]
  node_idempotency["Idempotency<br/>[idempotency.ts]"]
  node_logger["Structured logger<br/>[logger.ts]"]
end

node_player(("Player"))
node_gmail_service["Gmail"]
node_gemini["Gemini API"]

node_player -.->|"uses"| node_app
node_app -.->|"opens"| node_inbox
node_app -.->|"hosts"| node_canvas
node_player -.->|"connects account"| node_google
node_google -.->|"authorizes access"| node_gmail_service
node_inbox -.->|"requests messages"| node_client
node_client -.->|"calls"| node_api
node_api -.->|"uses shapes"| node_contract
node_api -.->|"authenticates"| node_session
node_api -.->|"formats responses"| node_http
node_api -.->|"queues sync"| node_gmail
node_gmail -.->|"reads messages"| node_gmail_service
node_player -.->|"requests analysis"| node_analysis
node_analysis -.->|"analyzes message"| node_gemini
node_worker -->|"runs sync"| node_gmail
node_worker -->|"runs analysis"| node_analysis
node_worker -->|"claims and completes"| node_jobs
node_worker -->|"relays events"| node_outbox
node_analysis -->|"uses"| node_jobs
node_analysis -->|"awards progression"| node_economy
node_api -->|"handles purchases"| node_economy
node_api -->|"records events"| node_audit
node_api -->|"checks limits"| node_rate
node_api -->|"accesses data"| node_db
node_settings -->|"calls"| node_client
node_shopui -.->|"calls"| node_client
node_canvas -.->|"renders"| node_office
node_office -.->|"dispatches interactions"| node_bridge
node_bridge -->|"uses configuration"| node_quests
node_profile -->|"reads and writes"| node_profile_store
node_profile -.->|"uses configuration"| node_quests
node_economy -.->|"records purchase"| node_audit
node_http -->|"resolves request IDs"| node_logger
node_audit -->|"redacts metadata"| node_logger
node_api -->|"guards purchases"| node_idempotency
node_api -.->|"streams updates"| node_events

click node_app "https://github.com/srijitagoswami/mailxp/blob/main/components/AppShell.tsx"
click node_inbox "https://github.com/srijitagoswami/mailxp/blob/main/components/screens/InboxScreen.tsx"
click node_settings "https://github.com/srijitagoswami/mailxp/blob/main/components/screens/SettingsScreen.tsx"
click node_shopui "https://github.com/srijitagoswami/mailxp/blob/main/components/screens/ShopScreen.tsx"
click node_client "https://github.com/srijitagoswami/mailxp/blob/main/components/apiClient.ts"
click node_canvas "https://github.com/srijitagoswami/mailxp/blob/main/components/PhaserCanvas.tsx"
click node_office "https://github.com/srijitagoswami/mailxp/blob/main/game/scenes/OfficeScene.ts"
click node_quests "https://github.com/srijitagoswami/mailxp/blob/main/game/data/questConfig.ts"
click node_bridge "https://github.com/srijitagoswami/mailxp/blob/main/game/bridge.ts"
click node_profile "https://github.com/srijitagoswami/mailxp/blob/main/game/profile.ts"
click node_profile_store "https://github.com/srijitagoswami/mailxp/blob/main/game/profileStorage.ts"
click node_api "https://github.com/srijitagoswami/mailxp/tree/main/app/api/v1"
click node_contract "https://github.com/srijitagoswami/mailxp/blob/main/game/backend/contract.ts"
click node_session "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/session.ts"
click node_http "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/http.ts"
click node_db "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/prisma.ts"
click node_gmail "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/gmailSync.ts"
click node_google "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/googleOAuth.ts"
click node_analysis "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/aiAnalysis.ts"
click node_worker "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/worker.ts"
click node_jobs "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/jobQueue.ts"
click node_outbox "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/outbox.ts"
click node_economy "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/economy.ts"
click node_audit "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/audit.ts"
click node_events "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/sseHub.ts"
click node_rate "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/rateLimit.ts"
click node_idempotency "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/idempotency.ts"
click node_logger "https://github.com/srijitagoswami/mailxp/blob/main/lib/server/logger.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_app,node_inbox,node_settings,node_shopui,node_client toneBlue
class node_canvas,node_office,node_quests,node_bridge,node_profile,node_profile_store,node_economy toneAmber
class node_gmail,node_google,node_analysis,node_gemini toneMint
class node_api,node_contract,node_session,node_http toneRose
class node_db,node_worker,node_jobs,node_outbox,node_audit,node_events,node_rate,node_idempotency,node_logger,node_player,node_gmail_service toneIndigo
```

## Components

### Player experience

- [App shell](https://github.com/srijitagoswami/mailxp/blob/main/components/AppShell.tsx): `components/AppShell.tsx`
- [Inbox screen](https://github.com/srijitagoswami/mailxp/blob/main/components/screens/InboxScreen.tsx): `components/screens/InboxScreen.tsx`
- [Settings screen](https://github.com/srijitagoswami/mailxp/blob/main/components/screens/SettingsScreen.tsx): `components/screens/SettingsScreen.tsx`
- [Shop screen](https://github.com/srijitagoswami/mailxp/blob/main/components/screens/ShopScreen.tsx): `components/screens/ShopScreen.tsx`
- [API client](https://github.com/srijitagoswami/mailxp/blob/main/components/apiClient.ts): `components/apiClient.ts`

### Game runtime

- [Phaser canvas](https://github.com/srijitagoswami/mailxp/blob/main/components/PhaserCanvas.tsx): `components/PhaserCanvas.tsx`
- [Office scene](https://github.com/srijitagoswami/mailxp/blob/main/game/scenes/OfficeScene.ts): `game/scenes/OfficeScene.ts`
- [Quest config](https://github.com/srijitagoswami/mailxp/blob/main/game/data/questConfig.ts): `game/data/questConfig.ts`
- [Game bridge](https://github.com/srijitagoswami/mailxp/blob/main/game/bridge.ts): `game/bridge.ts`
- [Local profile](https://github.com/srijitagoswami/mailxp/blob/main/game/profile.ts): `game/profile.ts`
- [Profile storage](https://github.com/srijitagoswami/mailxp/blob/main/game/profileStorage.ts): `game/profileStorage.ts`
- [Game economy](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/economy.ts): `lib/server/economy.ts`

### Mail intelligence

- [Gmail sync](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/gmailSync.ts): `lib/server/gmailSync.ts`
- [Google OAuth](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/googleOAuth.ts): `lib/server/googleOAuth.ts`
- [AI analysis](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/aiAnalysis.ts): `lib/server/aiAnalysis.ts`

### API and identity

- [Versioned API](https://github.com/srijitagoswami/mailxp/tree/main/app/api/v1): `app/api/v1`
- [API contract](https://github.com/srijitagoswami/mailxp/blob/main/game/backend/contract.ts): `game/backend/contract.ts`
- [Sessions](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/session.ts): `lib/server/session.ts`
- [HTTP helpers](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/http.ts): `lib/server/http.ts`

### Server services

- [PostgreSQL](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/prisma.ts): `lib/server/prisma.ts`
- [Job worker](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/worker.ts): `lib/server/worker.ts`
- [Job queue](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/jobQueue.ts): `lib/server/jobQueue.ts`
- [Event outbox](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/outbox.ts): `lib/server/outbox.ts`
- [Audit records](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/audit.ts): `lib/server/audit.ts`
- [Live events](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/sseHub.ts): `lib/server/sseHub.ts`
- [Rate limits](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/rateLimit.ts): `lib/server/rateLimit.ts`
- [Idempotency](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/idempotency.ts): `lib/server/idempotency.ts`
- [Structured logger](https://github.com/srijitagoswami/mailxp/blob/main/lib/server/logger.ts): `lib/server/logger.ts`

### Other components

- **Player**
- **Gmail**
- **Gemini API**

## Connections

- Player → App shell: uses (evidence: `README.md`)
- App shell → Inbox screen: opens (evidence: `README.md`)
- App shell → Phaser canvas: hosts (evidence: `README.md`)
- Player → Google OAuth: connects account (evidence: `README.md`)
- Google OAuth → Gmail: authorizes access (evidence: `README.md`)
- Inbox screen → API client: requests messages (evidence: `README.md`)
- API client → Versioned API: calls (evidence: `components/apiClient.ts`)
- Versioned API → API contract: uses shapes (evidence: `app/api/v1/gmail/messages/route.ts`)
- Versioned API → Sessions: authenticates (evidence: `app/api/v1/ai/settings/route.ts`)
- Versioned API → HTTP helpers: formats responses (evidence: `app/api/v1/ai/settings/route.ts`)
- Versioned API → Gmail sync: queues sync (evidence: `README.md`)
- Gmail sync → Gmail: reads messages (evidence: `README.md`)
- Player → AI analysis: requests analysis (evidence: `README.md`)
- AI analysis → Gemini API: analyzes message (evidence: `lib/server/aiAnalysis.ts`)
- Job worker → Gmail sync: runs sync (evidence: `lib/server/worker.ts`)
- Job worker → AI analysis: runs analysis (evidence: `lib/server/worker.ts`)
- Job worker → Job queue: claims and completes (evidence: `lib/server/worker.ts`)
- Job worker → Event outbox: relays events (evidence: `lib/server/worker.ts`)
- AI analysis → Job queue: uses (evidence: `lib/server/aiAnalysis.ts`)
- AI analysis → Game economy: awards progression (evidence: `lib/server/aiAnalysis.ts`)
- Versioned API → Game economy: handles purchases (evidence: `app/api/v1/shop/purchases/route.ts`)
- Versioned API → Audit records: records events (evidence: `app/api/v1/ai/settings/route.ts`)
- Versioned API → Rate limits: checks limits (evidence: `app/api/v1/ai/settings/route.ts`)
- Versioned API → PostgreSQL: accesses data (evidence: `app/api/v1/ai/settings/route.ts`)
- Settings screen → API client: calls (evidence: `components/screens/SettingsScreen.tsx`)
- Shop screen → API client: calls (evidence: `components/apiClient.ts`)
- Phaser canvas → Office scene: renders (evidence: `README.md`)
- Office scene → Game bridge: dispatches interactions (evidence: `game/bridge.ts`)
- Game bridge → Quest config: uses configuration (evidence: `game/bridge.ts`)
- Local profile → Profile storage: reads and writes (evidence: `game/profile.ts`)
- Local profile → Quest config: uses configuration (evidence: `game/profile.ts`)
- Game economy → Audit records: records purchase (evidence: `lib/server/economy.ts`)
- HTTP helpers → Structured logger: resolves request IDs (evidence: `lib/server/http.ts`)
- Audit records → Structured logger: redacts metadata (evidence: `lib/server/audit.ts`)
- Versioned API → Idempotency: guards purchases (evidence: `app/api/v1/shop/purchases/route.ts`)
- Versioned API → Live events: streams updates (evidence: `app/api/v1/events/route.ts`)
