File size: 6,299 Bytes
28c1fa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# CourtMitra MissionOS

**Citizen-side grievance + legal-action autopilot for India.** A citizen describes a problem once (WhatsApp, Telegram, or browser); the system turns it into a **mission**, builds the evidence file, grounds it in Indian law/procedure, picks an official route, drafts + sends approved actions, follows up, and records the outcome.

## Architecture

```
apps/{web, api, worker}
packages/{domain, ports, adapters, db, contracts, prompts, ui}
```

| Layer | Tech | Role |
|---|---|---|
| `apps/web` | Next.js (App Router), i18n | Multi-tab mission UI, public case pages, ledger |
| `apps/api` | NestJS + Fastify | REST endpoints, action preview/consent/execution |
| `apps/worker` | Inngest | Async workflow engine: intake, evidence, route-plan, action execution, followups |
| `packages/domain` | Pure TS | Entities, policies (`canResolve`), use-cases β€” zero framework deps |
| `packages/ports` | TS interfaces | Adapter contracts for channels, storage, LLM, redaction |
| `packages/adapters` | Various | Resend (email), Telegram, WhatsApp, social copy-ready |
| `packages/db` | Postgres + Drizzle ORM | Schema, migrations, seeds, materialized views |
| `packages/contracts` | Zod + OpenAPI | Single source of truth for all API I/O and LLM output shapes |
| `packages/prompts` | Versioned prompts | Evals included; no inline prompts in controllers |
| `packages/ui` | shadcn/ui + Tailwind | Shared UI components |

## Platform Features

### Lifecycle (10 states)
`route_plan_ready` β†’ `actions_preview_ready` β†’ `actions_awaiting_consent` β†’ `action_executing` β†’ `user_confirmed_external_submission` β†’ `waiting_for_response` β†’ `resolver_reply_received` β†’ `resolution_claimed` β†’ `resolved`

Plus: `created`, `intake_in_progress`, `needs_user_input`, `human_review_required`, `facts_confirmed`, `closed_unresolved`, `paused`, `blocked`, `failed`

### Mission Page (6 tabs)
1. **Overview** β€” current status, next action, timeline
2. **Facts** β€” extracted evidence timeline, missing info questions
3. **Route** β€” route plan steps with legal citations
4. **Action** β€” preview, approve, consent, execute, confirm external
5. **Follow-up** β€” waiting timer, social media escalation (X/Threads copy-drafts)
6. **Outcome** β€” resolution filing, ledger publication

### Security & Safety
- **Rank-guarded state machine** β€” no snapshot can regress the mission lifecycle (`STATE_RANK`)
- **Consent-first** β€” every send is gated by a hash-verified consent grant
- **Redaction** β€” Presidio-based PII redaction before any public output
- **Stop-and-refer** β€” emergency/criminal flags trigger `human_review_required`
- **Idempotency keys** β€” all write endpoints and webhooks are idempotent

### Social Proof & Leverage
- **EscalateSocialCard** β€” copy-ready X.com/Threads drafts for immediate public posting
- **Swarms** β€” entity resolution tracks repeat offenders (Flipkart, Amazon, Paytm, etc.)
- **Public Case Pages** β€” private-link anonymized summaries for social sharing
- **Ledger** β€” `ledger_stats_view` + `repeat_offender_view` materialized views

### Demo Data
- 10+ realistic missions across all lifecycle states, domains (consumer, payment, cyber_fraud), and states (Delhi, Maharashtra, Karnataka, etc.)
- 7 swarms with aggregated case counts
- 1 takedown request (Meta/Instagram)
- 5 public case pages with anonymized summaries
- 8 case lookups and 8 scheduled follow-ups

## Quick Start

```bash
# Prerequisites
node >= 20
pnpm >= 10
docker  (for local Postgres)

# Install
pnpm install

# Start infrastructure (Postgres + Redis)
pnpm infra:up

# Run migrations
pnpm db:migrate

# Seed demo data
pnpm db:seed

# Start dev servers (web + api + worker)
pnpm dev
```

Open `http://localhost:3000` in your browser.

## Commands

| Command | Description |
|---|---|
| `pnpm dev` | Run web + api + worker in parallel |
| `pnpm build` | Build all packages |
| `pnpm typecheck` | `tsc --noEmit` across all 10 packages |
| `pnpm lint` | ESLint with dependency-boundary rules |
| `pnpm test` | Vitest (unit + integration) |
| `pnpm test:contracts` | Zod/OpenAPI contract validation |
| `pnpm db:migrate` | Run Drizzle migrations |
| `pnpm db:seed` | Seed route rules + curated citations |
| `pnpm infra:up` | Docker compose: Postgres + Redis |
| `pnpm infra:down` | Stop Docker containers |

## Environment

Copy `infra/.env.example` to `infra/.env` (see `infra/.env` for current values). Key variables:

| Variable | Required | Description |
|---|---|---|
| `DATABASE_URL` | Yes | Postgres connection string |
| `RESEND_API_KEY` | For email | Resend email API key |
| `TELEGRAM_BOT_TOKEN` | For Telegram | Telegram bot token |
| `WHATSAPP_*` | For WhatsApp | WhatsApp Cloud API credentials |
| `X_API_*` | For X posting | X/Twitter API v2 credentials |
| `OPENAI_API_KEY` | For LLM | OpenAI / OpenRouter API key |

## Key Design Decisions

- **Hexagonal architecture** β€” domain is pure TS with zero framework imports
- **Everything external is an Action** β€” adapters never called directly from workflows
- **State machine is rank-guarded** β€” no transition can regress the lifecycle
- **Contracts-first** β€” Zod schemas in `packages/contracts` are single source of truth; API routes and frontend import them
- **Database transactions + events** β€” all mutations go through Drizzle transactions; mission events are append-only audit
- **No fake submissions** β€” `proofState` accurately reflects real delivery status
- **No legal claim without cited source** β€” every legal statement carries `cited_chunk_ids`
- **Public pages are private-link only** β€” no indexing, no defamation risk
- **i18n** β€” English + Hindi (`next-intl`), language switcher in header

## Loading Demo Data

```bash
# Seed all demo missions with full lifecycle data
pnpm exec tsx packages/db/src/seeds/demo-data.ts
```

This creates 10+ missions across all lifecycle states, swarms, takedown requests, public case pages, and ledger entries. It preserves existing user missions.

## Non-Goals (Phase 2)

- Complaint swarm dashboard
- Live social auto-publishing (API keys needed)
- Company resolver portal
- Qdrant/rerank RAG upgrade
- ASR/voice transcription
- Supervised Playwright portal automation
- eCourts real-time case scraping
- AI-run public social account