How open-legal-products/mike works
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: index.ts API server · app Next.js app · documents.ts Documents API · caseLaw.ts Case law API
Components 26
Frontend app
Next.js user workspace and UI state
- Next.js app · App Router entrypoints, layouts, and authenticated page groups.
frontend/src/app - Auth state · Supabase session handling, user profile state, sidebar chrome, and chat history.
frontend/src/app/contexts - Assistant UI · Chat views, message rendering, citations, document panels, and model selection.
frontend/src/app/components/assistant - Projects UI · Project explorer, document views, project workspace, and project-level assistant entrypoints.
frontend/src/app/components/projects - Tabular UI · Spreadsheet-like review tables, side panels, citation display, and export flows.
frontend/src/app/components/tabular - Workflow UI · Workflow picker, prompt editor, workflow detail, and use-workflow flows.
frontend/src/app/components/workflows - Account UI · API keys, connectors, model preferences, privacy, and security screens.
frontend/src/app/(pages)/account - API client · 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 · Backend runtime entrypoint that mounts routes and orchestration services.
backend/src/index.ts - Auth gate · Authentication and authorization middleware for protected backend routes.
backend/src/middleware/auth.ts - Projects API · Project CRUD, overview RPCs, sharing metadata, and workspace boundaries.
backend/src/routes/projects.ts - Documents API · Document upload, versioning, conversion, tracked changes, and metadata access.
backend/src/routes/documents.ts - Downloads API · Signed token download flow that shields raw storage paths from clients.
backend/src/routes/downloads.ts - Chat API · Assistant conversations, streaming responses, citations, and project chat.
backend/src/routes/chat.ts - Tabular API · Tabular review storage, linked documents, collaboration, and workflow metadata.
backend/src/routes/tabular.ts - Workflows API · Workflow templates, metadata, sharing, and open-source submission support.
backend/src/routes/workflows.ts - Case law API · CourtListener lookup, citation verification, opinion fetch, and cached bulk access.
backend/src/routes/caseLaw.ts - User API · Profile, API keys, MFA, connectors, exports, cleanup, and preferences.
backend/src/routes/user.ts - Chat core · Context builders, prompts, citations, streaming, and tool execution.
backend/src/lib/chat - Tool dispatcher · Central tool routing for document operations and CourtListener-powered helpers.
backend/src/lib/chat/tools/toolDispatcher.ts - LLM adapters · Anthropic, Gemini, and OpenAI provider adapters with user or instance keys.
backend/src/lib/llm - Storage layer · 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 · System of record for auth, permissions, chats, projects, reviews, workflows, and profiles.
- R2 storage · Binary and cached document storage for uploads, versions, and fetched legal opinions.
- Model providers · Anthropic, Gemini, and OpenAI API endpoints used server-side.
- CourtListener · Legal research source for case lookup, opinions, and citation support.
Connections 35
- 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.