# willchen96/mike architecture

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

- Interactive diagram: https://gitdiagram.com/willchen96/mike
- Repository: https://github.com/willchen96/mike

## Overview

#### Purpose and Entry Points
Mike is a legal-AI platform for document review, drafting, research, project workspaces, tabular reviews, and reusable workflows. The browser application is a Next.js App Router frontend rooted at `frontend/src/app/layout.tsx` and `frontend/src/app/page.tsx`. The API is an Express service bootstrapped by `backend/src/index.ts` and composed in `backend/src/app.ts`.

#### Client Application and Workspace Domains
The frontend organizes the product around assistant chats, projects, document library, tabular reviews, workflows, and account settings. Route pages establish workspace boundaries; feature components contain the interactive views.
- Assistant/chat: `frontend/src/app/(pages)/assistant/chat/[id]/page.tsx`, `frontend/src/app/components/assistant/ChatView.tsx`
- Projects: `frontend/src/app/(pages)/projects/[id]/page.tsx`, `frontend/src/app/components/projects/ProjectWorkspace.tsx`
- Tabular review: `frontend/src/app/(pages)/tabular-reviews/[id]/page.tsx`, `frontend/src/app/components/tabular/TabularReviewView.tsx`
- Workflows: `frontend/src/app/(pages)/workflows/assistant/[id]/page.tsx`, `frontend/src/app/components/workflows/WorkflowDetailPage.tsx`

Authentication and browser-side Supabase state cross the frontend/backend boundary through `frontend/src/app/contexts/AuthContext.tsx`, `frontend/src/app/lib/supabase.ts`, and API calls in `frontend/src/app/lib/mikeApi.ts`.

#### HTTP API, Identity, and Authorization
Express routes expose domain APIs for chats, project chats, documents, library, tabular reviews, workflows, models, user settings, downloads, and case law. Authentication middleware validates Supabase identity, while backend access logic should enforce ownership/sharing rules before data or storage operations.
- API boundaries: `backend/src/routes/chat.ts`, `backend/src/routes/projects.ts`, `backend/src/routes/documents.ts`
- Auth/access: `backend/src/middleware/auth.ts`, `backend/src/lib/access.ts`
- Supabase service access: `backend/src/lib/supabase.ts`

#### AI Assistant Execution
Chat requests are validated, assembled with document/project/workflow context, and streamed back to the client. The frontend consumes server-sent events through `frontend/src/app/hooks/useAssistantChat.ts`; assistant rendering displays streamed content, citations, and edit/tool events in `frontend/src/app/components/assistant/AssistantMessage.tsx`.

The backend model layer abstracts Anthropic, Gemini, OpenAI, and Ollama. Chat tool dispatch enables document operations and CourtListener research; citations are verified and persisted as chat-message metadata.
- Orchestration: `backend/src/lib/chat/index.ts`, `backend/src/lib/chat/streaming.ts`, `backend/src/lib/chat/contextBuilders.ts`
- Providers: `backend/src/lib/llm/index.ts`, `backend/src/lib/llm/openai.ts`, `backend/src/lib/llm/ollama.ts`
- Tools/research: `backend/src/lib/chat/tools/toolDispatcher.ts`, `backend/src/lib/chat/tools/courtlistenerTools.ts`, `backend/src/lib/courtlistener.ts`

#### Documents, Versions, and Exports
Documents are uploaded to S3-compatible storage and represented as versioned database records. Backend processing handles type detection, extraction/conversion, DOCX tracked changes, spreadsheets, and controlled downloads. The client offers document tables and specialized PDF, DOCX, and spreadsheet views.
- API/storage/version boundary: `backend/src/routes/documents.ts`, `backend/src/lib/storage.ts`, `backend/src/lib/documentVersions.ts`
- Content processing: `backend/src/lib/convert.ts`, `backend/src/lib/docxTrackedChanges.ts`, `backend/src/lib/spreadsheet.ts`
- Secure export/download primitives: `backend/src/lib/downloadTokens.ts`, `backend/src/lib/manifestSigning.ts`

#### Persistence, Sharing, and Workflows
Supabase Postgres is the system of record for users, chats/messages/citations, projects and sharing, documents/versions, tabular reviews, workflow definitions, API keys, and MCP connectors. Fresh deployment schema is in `backend/schema.sql`; incremental production evolution is in `backend/migrations/`. Workflow metadata and system workflow support are centralized in `backend/src/routes/workflows.ts` and `backend/src/lib/systemWorkflows.ts`.

#### Infrastructure and External Services
`docker-compose.yml` runs the local full stack: frontend/backend, Supabase Postgres/Auth/data gateway, RustFS object storage, and Mailpit. Production substitutes managed Supabase and R2/another S3-compatible store. Supported external AI services are Anthropic, Gemini, OpenAI, OpenRouter, and optional local Ollama; provider credentials may be global or encrypted per-user (`backend/src/lib/userApiKeys.ts`). CourtListener supports live and optional local-bulk US case-law lookup. MCP integrations, including OAuth-capable connectors, are handled by `backend/src/lib/mcp/client.ts` and `backend/src/lib/mcp/oauth.ts`.

Main entry points: [`backend/src/routes/documents.ts`](https://github.com/willchen96/mike/blob/main/backend/src/routes/documents.ts) (Document API & versions), [`backend/src/routes/chat.ts`](https://github.com/willchen96/mike/blob/main/backend/src/routes/chat.ts) (Domain routes), [`backend/src/lib/chat/index.ts`](https://github.com/willchen96/mike/blob/main/backend/src/lib/chat/index.ts) (Chat orchestration), [`backend/schema.sql`](https://github.com/willchen96/mike/blob/main/backend/schema.sql) (Supabase Postgres & Auth)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser app"]
  node_next_app["Next.js App Router<br/>frontend shell<br/>[layout.tsx]"]
  node_workspace_routes["Workspace routes<br/>route pages<br/>[page.tsx]"]
  node_workspace_views["Workspace views<br/>feature components<br/>[ChatView.tsx]"]
  node_browser_auth["Browser auth &amp; API<br/>client state<br/>[AuthContext.tsx]"]
  node_chat_stream_client["Assistant stream client<br/>chat hook"]
  node_assistant_rendering["Assistant rendering<br/>message view"]
end

subgraph group_api["Backend API"]
  node_express_app["Express API<br/>service composition<br/>[app.ts]"]
  node_domain_routes["Domain routes<br/>HTTP routes<br/>[chat.ts]"]
  node_auth_access["Identity &amp; access<br/>middleware and policy<br/>[auth.ts]"]
end

subgraph group_ai["AI execution"]
  node_chat_orchestrator["Chat orchestration<br/>streaming pipeline<br/>[index.ts]"]
  node_llm_gateway["LLM gateway<br/>provider abstraction<br/>[index.ts]"]
  node_tool_dispatch["Tool &amp; research dispatch<br/>chat tools<br/>[toolDispatcher.ts]"]
  node_mcp_connectors["MCP connectors<br/>integration client<br/>[client.ts]"]
end

subgraph group_content["Document services"]
  node_document_api["Document API &amp; versions<br/>document boundary<br/>[documents.ts]"]
  node_content_processing["Content processing<br/>conversion pipeline<br/>[convert.ts]"]
  node_secure_exports["Secure downloads<br/>export controls<br/>[downloadTokens.ts]"]
end

subgraph group_data["Data and external services"]
  node_supabase[("Supabase Postgres &amp; Auth<br/>system of record<br/>[schema.sql]")]
  node_object_storage[("S3-compatible storage<br/>object store<br/>[storage.ts]")]
  node_model_providers{{"AI model providers<br/>external AI<br/>[openai.ts]"}}
  node_courtlistener{{"CourtListener<br/>case-law service<br/>[courtlistener.ts]"}}
end

node_next_app -->|"renders"| node_workspace_routes
node_workspace_routes -->|"mounts"| node_workspace_views
node_workspace_views -->|"submits chat"| node_chat_stream_client
node_chat_stream_client -->|"streams events"| node_assistant_rendering
node_browser_auth -->|"authenticated API calls"| node_express_app
node_chat_stream_client -->|"SSE chat request"| node_domain_routes
node_express_app -->|"composes"| node_domain_routes
node_domain_routes -->|"authorizes"| node_auth_access
node_auth_access -->|"validates identity and access"| node_supabase
node_domain_routes -->|"chat requests"| node_chat_orchestrator
node_chat_orchestrator -->|"model generation"| node_llm_gateway
node_chat_orchestrator -->|"tool calls"| node_tool_dispatch
node_tool_dispatch -->|"legal research"| node_courtlistener
node_tool_dispatch -->|"external tools"| node_mcp_connectors
node_llm_gateway -->|"provider requests"| node_model_providers
node_chat_orchestrator -->|"messages and citations"| node_supabase
node_domain_routes -->|"document operations"| node_document_api
node_document_api -->|"processes content"| node_content_processing
node_document_api -->|"reads and writes objects"| node_object_storage
node_document_api -->|"versions and metadata"| node_supabase
node_document_api -->|"controlled exports"| node_secure_exports
node_secure_exports -->|"serves signed files"| node_object_storage
node_mcp_connectors -->|"connector metadata"| node_supabase

click node_next_app "https://github.com/willchen96/mike/blob/main/frontend/src/app/layout.tsx"
click node_workspace_routes "https://github.com/willchen96/mike/blob/main/frontend/src/app/(pages)/assistant/chat/%5Bid%5D/page.tsx"
click node_workspace_views "https://github.com/willchen96/mike/blob/main/frontend/src/app/components/assistant/ChatView.tsx"
click node_browser_auth "https://github.com/willchen96/mike/blob/main/frontend/src/app/contexts/AuthContext.tsx"
click node_chat_stream_client "https://github.com/willchen96/mike/blob/main/frontend/src/app/hooks/useAssistantChat.ts"
click node_assistant_rendering "https://github.com/willchen96/mike/blob/main/frontend/src/app/components/assistant/AssistantMessage.tsx"
click node_express_app "https://github.com/willchen96/mike/blob/main/backend/src/app.ts"
click node_domain_routes "https://github.com/willchen96/mike/blob/main/backend/src/routes/chat.ts"
click node_auth_access "https://github.com/willchen96/mike/blob/main/backend/src/middleware/auth.ts"
click node_chat_orchestrator "https://github.com/willchen96/mike/blob/main/backend/src/lib/chat/index.ts"
click node_llm_gateway "https://github.com/willchen96/mike/blob/main/backend/src/lib/llm/index.ts"
click node_tool_dispatch "https://github.com/willchen96/mike/blob/main/backend/src/lib/chat/tools/toolDispatcher.ts"
click node_mcp_connectors "https://github.com/willchen96/mike/blob/main/backend/src/lib/mcp/client.ts"
click node_document_api "https://github.com/willchen96/mike/blob/main/backend/src/routes/documents.ts"
click node_content_processing "https://github.com/willchen96/mike/blob/main/backend/src/lib/convert.ts"
click node_secure_exports "https://github.com/willchen96/mike/blob/main/backend/src/lib/downloadTokens.ts"
click node_supabase "https://github.com/willchen96/mike/blob/main/backend/schema.sql"
click node_object_storage "https://github.com/willchen96/mike/blob/main/backend/src/lib/storage.ts"
click node_model_providers "https://github.com/willchen96/mike/blob/main/backend/src/lib/llm/openai.ts"
click node_courtlistener "https://github.com/willchen96/mike/blob/main/backend/src/lib/courtlistener.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_next_app,node_workspace_routes,node_workspace_views,node_browser_auth,node_chat_stream_client,node_assistant_rendering toneBlue
class node_express_app,node_domain_routes,node_auth_access toneAmber
class node_chat_orchestrator,node_llm_gateway,node_tool_dispatch,node_mcp_connectors toneMint
class node_document_api,node_content_processing,node_secure_exports toneRose
class node_supabase,node_object_storage,node_model_providers,node_courtlistener toneIndigo
```

## Components

### Browser app

Next.js workspace experience.

- [Next.js App Router](https://github.com/willchen96/mike/blob/main/frontend/src/app/layout.tsx): frontend shell · `frontend/src/app/layout.tsx`
- [Workspace routes](https://github.com/willchen96/mike/blob/main/frontend/src/app/(pages)/assistant/chat/%5Bid%5D/page.tsx): route pages · Chats, projects, tabular reviews, and workflows establish workspace boundaries. · `frontend/src/app/(pages)/assistant/chat/[id]/page.tsx`
- [Workspace views](https://github.com/willchen96/mike/blob/main/frontend/src/app/components/assistant/ChatView.tsx): feature components · Interactive chat, project, tabular, and workflow views. · `frontend/src/app/components/assistant/ChatView.tsx`
- [Browser auth & API](https://github.com/willchen96/mike/blob/main/frontend/src/app/contexts/AuthContext.tsx): client state · Supabase session state and backend API calls. · `frontend/src/app/contexts/AuthContext.tsx`
- [Assistant stream client](https://github.com/willchen96/mike/blob/main/frontend/src/app/hooks/useAssistantChat.ts): chat hook · Consumes SSE assistant events. · `frontend/src/app/hooks/useAssistantChat.ts`
- [Assistant rendering](https://github.com/willchen96/mike/blob/main/frontend/src/app/components/assistant/AssistantMessage.tsx): message view · Renders streamed content, citations, and tool events. · `frontend/src/app/components/assistant/AssistantMessage.tsx`

### Backend API

Express domain and authorization boundary.

- [Express API](https://github.com/willchen96/mike/blob/main/backend/src/app.ts): service composition · Bootstrapped API service for product domains. · `backend/src/app.ts`
- [Domain routes](https://github.com/willchen96/mike/blob/main/backend/src/routes/chat.ts): HTTP routes · Chats, projects, documents, library, reviews, workflows, and settings. · `backend/src/routes/chat.ts`
- [Identity & access](https://github.com/willchen96/mike/blob/main/backend/src/middleware/auth.ts): middleware and policy · Validates Supabase identity and enforces ownership and sharing. · `backend/src/middleware/auth.ts`

### AI execution

Streaming assistant orchestration and integrations.

- [Chat orchestration](https://github.com/willchen96/mike/blob/main/backend/src/lib/chat/index.ts): streaming pipeline · Builds context, validates requests, and streams responses. · `backend/src/lib/chat/index.ts`
- [LLM gateway](https://github.com/willchen96/mike/blob/main/backend/src/lib/llm/index.ts): provider abstraction · Selects and invokes configured model providers. · `backend/src/lib/llm/index.ts`
- [Tool & research dispatch](https://github.com/willchen96/mike/blob/main/backend/src/lib/chat/tools/toolDispatcher.ts): chat tools · Runs document actions and case-law research tools. · `backend/src/lib/chat/tools/toolDispatcher.ts`
- [MCP connectors](https://github.com/willchen96/mike/blob/main/backend/src/lib/mcp/client.ts): integration client · Supports MCP and OAuth-capable connectors. · `backend/src/lib/mcp/client.ts`

### Document services

Versioned files, processing, and export controls.

- [Document API & versions](https://github.com/willchen96/mike/blob/main/backend/src/routes/documents.ts): document boundary · Manages versioned document records and storage operations. · `backend/src/routes/documents.ts`
- [Content processing](https://github.com/willchen96/mike/blob/main/backend/src/lib/convert.ts): conversion pipeline · Extraction, conversion, tracked changes, and spreadsheet handling. · `backend/src/lib/convert.ts`
- [Secure downloads](https://github.com/willchen96/mike/blob/main/backend/src/lib/downloadTokens.ts): export controls · Issues controlled download tokens and signed manifests. · `backend/src/lib/downloadTokens.ts`

### Data and external services

System-of-record and connected providers.

- [Supabase Postgres & Auth](https://github.com/willchen96/mike/blob/main/backend/schema.sql): system of record · Stores identities, workspaces, chats, documents, workflows, and integration metadata. · `backend/schema.sql`
- [S3-compatible storage](https://github.com/willchen96/mike/blob/main/backend/src/lib/storage.ts): object store · Stores uploaded document objects and exports. · `backend/src/lib/storage.ts`
- [AI model providers](https://github.com/willchen96/mike/blob/main/backend/src/lib/llm/openai.ts): external AI · Anthropic, Gemini, OpenAI, OpenRouter, and optional Ollama. · `backend/src/lib/llm/openai.ts`
- [CourtListener](https://github.com/willchen96/mike/blob/main/backend/src/lib/courtlistener.ts): case-law service · Live or local-bulk US case-law research. · `backend/src/lib/courtlistener.ts`

## Connections

- Next.js App Router → Workspace routes: renders
- Workspace routes → Workspace views: mounts
- Workspace views → Assistant stream client: submits chat
- Assistant stream client → Assistant rendering: streams events
- Browser auth & API → Express API: authenticated API calls
- Assistant stream client → Domain routes: SSE chat request
- Express API → Domain routes: composes
- Domain routes → Identity & access: authorizes
- Identity & access → Supabase Postgres & Auth: validates identity and access
- Domain routes → Chat orchestration: chat requests
- Chat orchestration → LLM gateway: model generation
- Chat orchestration → Tool & research dispatch: tool calls
- Tool & research dispatch → CourtListener: legal research
- Tool & research dispatch → MCP connectors: external tools
- LLM gateway → AI model providers: provider requests
- Chat orchestration → Supabase Postgres & Auth: messages and citations
- Domain routes → Document API & versions: document operations
- Document API & versions → Content processing: processes content
- Document API & versions → S3-compatible storage: reads and writes objects
- Document API & versions → Supabase Postgres & Auth: versions and metadata
- Document API & versions → Secure downloads: controlled exports
- Secure downloads → S3-compatible storage: serves signed files
- MCP connectors → Supabase Postgres & Auth: connector metadata
