# open-legal-products/mike architecture

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

- Interactive diagram: https://gitdiagram.com/open-legal-products/mike
- Repository: https://github.com/open-legal-products/mike

## Overview

1. **Top-level architecture**
   - Mike is split into two deployable apps: a **Next.js frontend** in `frontend/` and an **Express backend** in `backend/`.
   - The frontend is the user-facing workspace; the backend owns authentication-aware APIs, document processing, AI orchestration, and database/storage access.

2. **Request and identity flow**
   - Users authenticate through **Supabase Auth** from the frontend.
   - The frontend uses the public Supabase key for session handling, then calls the backend API with the user’s session context.
   - The backend uses the **Supabase service role key** to enforce server-side authorization and access Postgres directly.

3. **Frontend structure**
   - The app uses the **Next.js App Router** with page groups for `assistant`, `projects`, `tabular-reviews`, `workflows`, and `account`.
   - `frontend/src/app/components/` contains the actual workspace UI: chat panels, document views, project explorers, tabular review tables, modal flows, and account/settings screens.
   - The frontend is stateful around chat history, sidebar/page chrome, user profile, and selected model/provider preferences.

4. **Backend API boundaries**
   - The backend is organized by domain routes in `backend/src/routes/`: `chat`, `projectChat`, `documents`, `downloads`, `projects`, `tabular`, `workflows`, `caseLaw`, and `user`.
   - Middleware in `backend/src/middleware/auth.ts` is the gatekeeper for authenticated access.
   - This backend is the only tier that talks to Supabase tables, object storage, and model providers.

5. **Document-centric core**
   - Documents and versions are a primary domain: there are utilities for document types, versioning, uploads, tracked DOCX changes, and conversion.
   - Files are stored in **R2-compatible object storage** via `backend/src/lib/storage.ts` and `upload.ts`, while metadata lives in Supabase/Postgres.
   - The backend also handles secure downloads using signed tokens, so the frontend does not expose raw storage paths.

6. **AI assistant pipeline**
   - Assistant behavior is split into context building, prompts, streaming, citations, and tool execution under `backend/src/lib/chat/`.
   - The backend assembles chat context from project documents, prior messages, case law results, and tool outputs, then streams model responses back to the frontend.
   - Chat messages can include citations and document-edit related events, which the frontend renders in the assistant views.

7. **Tooling inside assistant runs**
   - Tool dispatch is centralized in `backend/src/lib/chat/tools/toolDispatcher.ts`.
   - Tools include document operations and CourtListener-powered legal research helpers.
   - This makes the assistant more than a pure text generator: it can inspect documents, produce edits, and ground answers in legal sources.

8. **Model-provider abstraction**
   - `backend/src/lib/llm/` abstracts multiple LLM vendors: **Anthropic, Gemini, and OpenAI**.
   - The backend can use instance-level API keys from environment variables or user-scoped keys stored in the database.
   - Frontend account screens expose model availability and API-key management, but actual provider calls happen server-side.

9. **Legal research and citation infrastructure**
   - CourtListener is a first-class integration for case lookup, citation verification, and opinion fetching.
   - Optional bulk data support uses Postgres indexes plus cached opinion JSON in R2 before falling back to the live CourtListener API.
   - This capability feeds both the assistant and dedicated case-law panels in the UI.

10. **Projects and workspaces**
    - Projects are the main user workspace boundary: documents, chats, and tabular reviews are attached to a project.
    - Frontend routes like `projects/[id]/assistant` and `projects/[id]/tabular-reviews` show that each project can host multiple work products.
    - Backend overview RPCs and shared-with metadata suggest project-level collaboration and permissioning are important data boundaries.

11. **Tabular reviews**
    - Tabular review is its own subsystem on both frontend and backend.
    - The frontend provides spreadsheet-like editing, citation display, export, and review-specific side panels.
    - The backend stores review data, document links, shared access, and workflow-related metadata in the database.

12. **Reusable workflows**
    - Workflows are separate from ad hoc chats: they appear as a structured template system with prompt editing and “use workflow” flows.
    - There are dedicated workflow routes, workflow metadata migrations, and open-source workflow submission support.
    - The `scripts/build-workflows.js` utility suggests workflows are also generated or packaged as a build artifact.

13. **MCP and external connectors**
    - The backend includes an MCP connector subsystem with OAuth support, indicating integration with external tools/services through standardized connectors.
    - The account UI includes connector management, so this is a user-configurable integration boundary rather than a hardcoded backend dependency.

14. **Data model and change history**
    - `backend/schema.sql` defines the fresh Supabase schema, while `backend/migrations/` captures the project’s evolution.
    - The migration history shows feature growth around document versions, user API keys, org/profile data, MFA, email, shared access, chat citations, and workflow metadata.
    - For architecture diagrams, think of Supabase Postgres as the system-of-record for users, projects, chats, reviews, workflows, and permissions; R2 as the binary/object store; and the backend as the orchestrator between them.

15. **Operational constraints**
    - The backend expects Node/Bun-based execution, Supabase connectivity, an S3-compatible bucket, and at least one model provider key.
    - DOC/DOCX conversion depends on local LibreOffice availability, which affects document-processing paths but not the frontend architecture.

Main entry points: [`backend/src/index.ts`](https://github.com/open-legal-products/mike/blob/main/backend/src/index.ts) (API server), [`frontend/src/app`](https://github.com/open-legal-products/mike/tree/main/frontend/src/app) (Next.js app), [`backend/src/routes/documents.ts`](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/documents.ts) (Documents API), [`backend/src/routes/caseLaw.ts`](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/caseLaw.ts) (Case law API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend app"]
  node_frontend_app["Next.js app<br/>frontend shell"]
  node_frontend_auth["Auth state<br/>client context"]
  node_frontend_assistant["Assistant UI<br/>workspace UI"]
  node_frontend_projects["Projects UI<br/>workspace UI"]
  node_frontend_tabular["Tabular UI<br/>workspace UI"]
  node_frontend_workflows["Workflow UI<br/>workspace UI"]
  node_frontend_account["Account UI<br/>settings UI"]
  node_frontend_api["API client<br/>client bridge<br/>[mikeApi.ts]"]
end

subgraph group_backend["Backend API"]
  node_backend_entry["API server<br/>express app<br/>[index.ts]"]
  node_backend_auth["Auth gate<br/>middleware<br/>[auth.ts]"]
  node_backend_projects["Projects API<br/>domain routes<br/>[projects.ts]"]
  node_backend_documents["Documents API<br/>domain routes<br/>[documents.ts]"]
  node_backend_downloads["Downloads API<br/>secure delivery<br/>[downloads.ts]"]
  node_backend_chat["Chat API<br/>assistant routes<br/>[chat.ts]"]
  node_backend_tabular["Tabular API<br/>review routes<br/>[tabular.ts]"]
  node_backend_workflows["Workflows API<br/>workflow routes<br/>[workflows.ts]"]
  node_backend_case_law["Case law API<br/>legal research<br/>[caseLaw.ts]"]
  node_backend_user["User API<br/>account routes<br/>[user.ts]"]
  node_backend_chat_core["Chat core<br/>assistant pipeline"]
  node_backend_tooling["Tool dispatcher<br/>assistant tools<br/>[toolDispatcher.ts]"]
  node_backend_llm["LLM adapters<br/>model abstraction"]
  node_backend_storage["Storage layer<br/>storage + supabase<br/>[storage.ts]"]
end

subgraph group_infra["Data and services"]
  node_infra_supabase[("Supabase<br/>postgres auth")]
  node_infra_r2[("R2 storage<br/>object store")]
  node_infra_llm{{"Model providers<br/>external AI"}}
  node_infra_courtlistener{{"CourtListener<br/>external legal API"}}
end

node_frontend_app -->|"loads state"| node_frontend_auth
node_frontend_app -->|"renders"| node_frontend_assistant
node_frontend_app -->|"renders"| node_frontend_projects
node_frontend_app -->|"renders"| node_frontend_tabular
node_frontend_app -->|"renders"| node_frontend_workflows
node_frontend_app -->|"renders"| node_frontend_account
node_frontend_auth -->|"auth context"| node_frontend_api
node_frontend_api -->|"requests"| node_backend_entry
node_backend_entry -->|"guards"| node_backend_auth
node_backend_entry -->|"mounts"| node_backend_projects
node_backend_entry -->|"mounts"| node_backend_documents
node_backend_entry -->|"mounts"| node_backend_downloads
node_backend_entry -->|"mounts"| node_backend_chat
node_backend_entry -->|"mounts"| node_backend_tabular
node_backend_entry -->|"mounts"| node_backend_workflows
node_backend_entry -->|"mounts"| node_backend_case_law
node_backend_entry -->|"mounts"| node_backend_user
node_backend_chat -->|"uses"| node_backend_chat_core
node_backend_chat_core -->|"dispatches"| node_backend_tooling
node_backend_chat_core -->|"streams via"| node_backend_llm
node_backend_tooling -->|"edits docs"| node_backend_documents
node_backend_tooling -->|"researches"| node_backend_case_law
node_backend_documents -->|"stores metadata"| node_backend_storage
node_backend_documents -->|"stores files"| node_infra_r2
node_backend_downloads -->|"signs access"| node_infra_r2
node_backend_storage -->|"reads/writes"| node_infra_supabase
node_backend_llm -->|"calls"| node_infra_llm
node_backend_case_law -->|"queries"| node_infra_courtlistener
node_backend_case_law -->|"caches"| node_infra_r2
node_frontend_assistant -->|"streams chat"| node_backend_chat
node_frontend_projects -->|"loads workspaces"| node_backend_projects
node_frontend_projects -->|"manages docs"| node_backend_documents
node_frontend_tabular -->|"edits reviews"| node_backend_tabular
node_frontend_workflows -->|"uses workflows"| node_backend_workflows
node_frontend_account -->|"manages account"| node_backend_user

click node_frontend_app "https://github.com/open-legal-products/mike/tree/main/frontend/src/app"
click node_frontend_auth "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/contexts"
click node_frontend_assistant "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/assistant"
click node_frontend_projects "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/projects"
click node_frontend_tabular "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/tabular"
click node_frontend_workflows "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/workflows"
click node_frontend_account "https://github.com/open-legal-products/mike/tree/main/frontend/src/app/(pages)/account"
click node_frontend_api "https://github.com/open-legal-products/mike/blob/main/frontend/src/app/lib/mikeApi.ts"
click node_backend_entry "https://github.com/open-legal-products/mike/blob/main/backend/src/index.ts"
click node_backend_auth "https://github.com/open-legal-products/mike/blob/main/backend/src/middleware/auth.ts"
click node_backend_projects "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/projects.ts"
click node_backend_documents "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/documents.ts"
click node_backend_downloads "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/downloads.ts"
click node_backend_chat "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/chat.ts"
click node_backend_tabular "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/tabular.ts"
click node_backend_workflows "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/workflows.ts"
click node_backend_case_law "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/caseLaw.ts"
click node_backend_user "https://github.com/open-legal-products/mike/blob/main/backend/src/routes/user.ts"
click node_backend_chat_core "https://github.com/open-legal-products/mike/tree/main/backend/src/lib/chat"
click node_backend_tooling "https://github.com/open-legal-products/mike/blob/main/backend/src/lib/chat/tools/toolDispatcher.ts"
click node_backend_llm "https://github.com/open-legal-products/mike/tree/main/backend/src/lib/llm"
click node_backend_storage "https://github.com/open-legal-products/mike/blob/main/backend/src/lib/storage.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_frontend_app,node_frontend_auth,node_frontend_assistant,node_frontend_projects,node_frontend_tabular,node_frontend_workflows,node_frontend_account,node_frontend_api toneBlue
class node_backend_entry,node_backend_auth,node_backend_projects,node_backend_documents,node_backend_downloads,node_backend_chat,node_backend_tabular,node_backend_workflows,node_backend_case_law,node_backend_user,node_backend_chat_core,node_backend_tooling,node_backend_llm,node_backend_storage toneAmber
class node_infra_supabase,node_infra_r2,node_infra_llm,node_infra_courtlistener toneMint
```

## Components

### Frontend app

Next.js user workspace and UI state

- [Next.js app](https://github.com/open-legal-products/mike/tree/main/frontend/src/app): frontend shell · App Router entrypoints, layouts, and authenticated page groups. · `frontend/src/app`
- [Auth state](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/contexts): client context · Supabase session handling, user profile state, sidebar chrome, and chat history. · `frontend/src/app/contexts`
- [Assistant UI](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/assistant): workspace UI · Chat views, message rendering, citations, document panels, and model selection. · `frontend/src/app/components/assistant`
- [Projects UI](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/projects): workspace UI · Project explorer, document views, project workspace, and project-level assistant entrypoints. · `frontend/src/app/components/projects`
- [Tabular UI](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/tabular): workspace UI · Spreadsheet-like review tables, side panels, citation display, and export flows. · `frontend/src/app/components/tabular`
- [Workflow UI](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/components/workflows): workspace UI · Workflow picker, prompt editor, workflow detail, and use-workflow flows. · `frontend/src/app/components/workflows`
- [Account UI](https://github.com/open-legal-products/mike/tree/main/frontend/src/app/(pages)/account): settings UI · API keys, connectors, model preferences, privacy, and security screens. · `frontend/src/app/(pages)/account`
- [API client](https://github.com/open-legal-products/mike/blob/main/frontend/src/app/lib/mikeApi.ts): client bridge · Frontend request layer for backend calls, uploads, and model-aware workspace actions. · `frontend/src/app/lib/mikeApi.ts`

### Backend API

Express services for auth, data, AI, and storage

- [API server](https://github.com/open-legal-products/mike/blob/main/backend/src/index.ts): express app · Backend runtime entrypoint that mounts routes and orchestration services. · `backend/src/index.ts`
- [Auth gate](https://github.com/open-legal-products/mike/blob/main/backend/src/middleware/auth.ts): middleware · Authentication and authorization middleware for protected backend routes. · `backend/src/middleware/auth.ts`
- [Projects API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/projects.ts): domain routes · Project CRUD, overview RPCs, sharing metadata, and workspace boundaries. · `backend/src/routes/projects.ts`
- [Documents API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/documents.ts): domain routes · Document upload, versioning, conversion, tracked changes, and metadata access. · `backend/src/routes/documents.ts`
- [Downloads API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/downloads.ts): secure delivery · Signed token download flow that shields raw storage paths from clients. · `backend/src/routes/downloads.ts`
- [Chat API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/chat.ts): assistant routes · Assistant conversations, streaming responses, citations, and project chat. · `backend/src/routes/chat.ts`
- [Tabular API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/tabular.ts): review routes · Tabular review storage, linked documents, collaboration, and workflow metadata. · `backend/src/routes/tabular.ts`
- [Workflows API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/workflows.ts): workflow routes · Workflow templates, metadata, sharing, and open-source submission support. · `backend/src/routes/workflows.ts`
- [Case law API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/caseLaw.ts): legal research · CourtListener lookup, citation verification, opinion fetch, and cached bulk access. · `backend/src/routes/caseLaw.ts`
- [User API](https://github.com/open-legal-products/mike/blob/main/backend/src/routes/user.ts): account routes · Profile, API keys, MFA, connectors, exports, cleanup, and preferences. · `backend/src/routes/user.ts`
- [Chat core](https://github.com/open-legal-products/mike/tree/main/backend/src/lib/chat): assistant pipeline · Context builders, prompts, citations, streaming, and tool execution. · `backend/src/lib/chat`
- [Tool dispatcher](https://github.com/open-legal-products/mike/blob/main/backend/src/lib/chat/tools/toolDispatcher.ts): assistant tools · Central tool routing for document operations and CourtListener-powered helpers. · `backend/src/lib/chat/tools/toolDispatcher.ts`
- [LLM adapters](https://github.com/open-legal-products/mike/tree/main/backend/src/lib/llm): model abstraction · Anthropic, Gemini, and OpenAI provider adapters with user or instance keys. · `backend/src/lib/llm`
- [Storage layer](https://github.com/open-legal-products/mike/blob/main/backend/src/lib/storage.ts): storage + supabase · Supabase/Postgres access, object storage, uploads, downloads, and document metadata. · `backend/src/lib/storage.ts`

### Data and services

External systems and shared runtime dependencies

- **Supabase**: postgres auth · System of record for auth, permissions, chats, projects, reviews, workflows, and profiles.
- **R2 storage**: object store · Binary and cached document storage for uploads, versions, and fetched legal opinions.
- **Model providers**: external AI · Anthropic, Gemini, and OpenAI API endpoints used server-side.
- **CourtListener**: external legal API · Legal research source for case lookup, opinions, and citation support.

## Connections

- Next.js app → Auth state: loads state · App shell initializes auth, chrome, and user profile context.
- Next.js app → Assistant UI: renders · Assistant pages mount the chat workspace and message panels.
- Next.js app → Projects UI: renders · Project pages mount project workspace and document explorers.
- Next.js app → Tabular UI: renders · Tabular review pages mount spreadsheet-style review views.
- Next.js app → Workflow UI: renders · Workflow pages mount template browsing and editing flows.
- Next.js app → Account UI: renders · Account pages mount key, connector, model, and security screens.
- Auth state → API client: auth context · Session and profile state are attached to backend requests.
- API client → API server: requests · The frontend calls the backend for workspace, chat, and account actions.
- API server → Auth gate: guards · Protected routes pass through authentication middleware.
- API server → Projects API: mounts · Project domain endpoints handle workspace boundaries and sharing.
- API server → Documents API: mounts · Document routes handle uploads, versions, and conversion.
- API server → Downloads API: mounts · Download routes issue secure document delivery.
- API server → Chat API: mounts · Chat routes stream assistant responses and project chats.
- API server → Tabular API: mounts · Tabular routes manage review data and collaboration.
- API server → Workflows API: mounts · Workflow routes manage templates and submissions.
- API server → Case law API: mounts · Case law routes expose legal research capabilities.
- API server → User API: mounts · User routes manage profiles, keys, settings, and connectors.
- Chat API → Chat core: uses · Chat endpoints delegate to context, prompt, citation, and streaming logic.
- Chat core → Tool dispatcher: dispatches · Assistant runs invoke document and legal research tools.
- Chat core → LLM adapters: streams via · Model responses are produced through provider adapters.
- Tool dispatcher → Documents API: edits docs · Tools can inspect and modify project documents and versions.
- Tool dispatcher → Case law API: researches · Tools can search and fetch legal sources for grounded answers.
- Documents API → Storage layer: stores metadata · Document metadata and version records are persisted server-side.
- Documents API → R2 storage: stores files · Uploads, versions, and cached binaries live in object storage.
- Downloads API → R2 storage: signs access · Secure downloads resolve object storage through signed tokens.
- Storage layer → Supabase: reads/writes · The backend uses Supabase/Postgres as the system of record.
- LLM adapters → Model providers: calls · Provider adapters connect to external model APIs.
- Case law API → CourtListener: queries · Legal research endpoints integrate with CourtListener.
- Case law API → R2 storage: caches · Bulk opinions and cached JSON can be stored in object storage.
- Assistant UI → Chat API: streams chat · Assistant UI consumes streaming messages, citations, and edit events.
- Projects UI → Projects API: loads workspaces · Project pages request project overview and collaboration data.
- Projects UI → Documents API: manages docs · Project document views upload, inspect, and open document versions.
- Tabular UI → Tabular API: edits reviews · Tabular review UI persists rows, citations, and linked documents.
- Workflow UI → Workflows API: uses workflows · Workflow UI loads templates and launches structured runs.
- Account UI → User API: manages account · Account screens manage keys, connectors, MFA, and preferences.
