How akshat3144/synapse works
1. System shape
Synapse is a two-tier application: a Python FastAPI backend that continuously ingests pharma signals and generates intelligence, and a Next.js frontend that presents dashboards, entity views, alerts, and strategic briefs. The backend owns all business logic and data persistence; the frontend is a presentation layer that consumes API and WebSocket feeds.
2. Primary data flow
External sources feed the backend ingestion services: openFDA (recalls, adverse events, drug labels), NewsAPI (pharma news), and ClinicalTrials.gov (trial updates). Those raw signals are normalized into event records, stored, broadcast live, and then used as input for clustering and brief generation.
3. Backend layering
The backend is split into a few clear boundaries:
- services/ for source polling and intelligence processing
- routes/ for HTTP API endpoints
- store.py / store_supabase.py for persistence
- scheduler.py for recurring jobs
- ws_manager.py for real-time push updates
- models.py for shared data contracts
4. Ingestion services
Each source has its own monitor:
- fda_monitor.py for recalls and adverse events
- label_monitor.py for label/SPL changes
- news_crawler.py for NewsAPI and event classification
- clinical_trials.py for trial updates
These services produce a common event shape so downstream logic does not depend on source-specific formats.
5. Intelligence pipeline
The agentic core lives mainly in brief_generator.py and summarizer.py. Events are clustered by entity overlap into signal clusters, severity is assigned, and then unbriefed clusters are turned into strategic briefs. This is the main “analysis” boundary of the system, separate from raw ingestion and separate from presentation.
6. Agentic scheduling
scheduler.py orchestrates two kinds of background work: frequent source polling and the autonomous scan loop. The scan loop runs every 20 minutes, finds interesting clusters, skips recently briefed entities, and generates up to a few new briefs per cycle. There is also a manual trigger path via API for on-demand polling or brief generation.
7. Persistence boundary
Storage is abstracted behind the store layer. store_supabase.py persists to Supabase/PostgreSQL using a schema defined in supabase_schema.sql, while the app can fall back to an in-memory store if Supabase credentials are absent. This means the rest of the code treats storage as a stable interface rather than depending directly on database details.
8. Data model
models.py defines the shared domain objects used across ingestion, API, and UI: events, alerts, entities, signal clusters, briefs, SWOT/Porter/GTM structures, and source status. This keeps the API payloads and frontend TypeScript types aligned with the backend domain model.
9. API surface
The FastAPI routes are organized by concern:
- events.py for event listing, detail, stats, search, trends
- alerts.py for alert rules and dismissals
- entities.py for tracked entities and entity analysis
- briefs.py for briefs, clusters, and scan/generate actions
- sources.py for source health
These routes expose both operational data and derived intelligence to the frontend.
10. Real-time delivery
ws_manager.py plus the /ws/feed endpoint provide a live event feed. New ingested events are pushed to connected clients, so the UI can update without polling. This is a key architectural boundary between batch/background processing and interactive display.
11. Frontend structure
The frontend is a Next.js App Router application. Page routes map directly to the backend concepts: dashboard, briefs, entities, events, alerts, and search. UI components are split into layout shell components, dashboard widgets, and shared event/status badges. The frontend is clearly a consumer of backend intelligence rather than a source of business logic.
12. Frontend data access
frontend/lib/api.ts centralizes REST calls, websocket.ts handles live feed connections and reconnects, and hooks.ts exposes reusable data hooks for pages/components. types.ts mirrors backend models so the dashboard, detail pages, and charts can render structured intelligence consistently.
13. Operational behavior
On startup, the backend immediately begins polling source APIs and scheduling the agentic scan. The system also exposes diagnostics and source-status endpoints so operators can see API key health, poll times, and scheduler state. That makes the backend both the runtime engine and the observability point for the app.
14. Important architectural takeaway
The core architecture is an event-to-intelligence pipeline: external sources → normalized events → persistence/live feed → clustering and brief generation → API + WebSocket delivery → frontend analytics and drill-down views. The main design boundaries are source adapters, intelligence processing, storage abstraction, and the presentation layer.
Main entry points: store.py Store layer · main.py App entry · api.ts API client · scheduler.py Scheduler
Components 28
Backend
FastAPI engine for ingestion, intelligence, persistence, and live delivery.
- App entry · Starts the backend, wires config, routes, scheduler, and websocket feed.
backend/main.py - Config · Loads environment-driven settings and service toggles.
backend/config.py - Domain models · Defines events, alerts, entities, clusters, briefs, and source status shapes.
backend/models.py - Store layer · Abstract persistence boundary with in-memory and Supabase-backed implementations.
backend/store.py - Supabase store · Persists application data to Supabase/PostgreSQL using the SQL schema.
backend/store_supabase.py - DB schema · Defines the Supabase/PostgreSQL tables and migration structure.
backend/supabase_schema.sql - Scheduler · Runs source polling and the recurring autonomous scan loop.
backend/scheduler.py - Live feed · Broadcasts newly ingested events to connected clients.
backend/ws_manager.py - FDA monitor · Polls openFDA recall and adverse-event data and normalizes it into events.
backend/services/fda_monitor.py - Label monitor · Tracks drug label and SPL changes as normalized event signals.
backend/services/label_monitor.py - News crawler · Collects pharma news from NewsAPI and classifies relevant events.
backend/services/news_crawler.py - Trial monitor · Ingests ClinicalTrials.gov updates into the common event stream.
backend/services/clinical_trials.py - Brief generator · Clusters events by entity overlap, scores severity, and creates strategic briefs.
backend/services/brief_generator.py - Summarizer · Produces concise narrative summaries for briefs and clusters.
backend/services/summarizer.py - Events API · Exposes event lists, details, stats, search, and trends.
backend/routes/events.py - Briefs API · Serves briefs, clusters, and scan or generate actions.
backend/routes/briefs.py - Entities API · Provides tracked entities and entity-level analysis.
backend/routes/entities.py - Alerts API · Manages alert rules and dismissals.
backend/routes/alerts.py - Sources API · Exposes source health and operational diagnostics.
backend/routes/sources.py
Frontend
Next.js presentation layer for dashboards and drill-down views.
- App shell · Top-level App Router shell for the UI.
frontend/app/layout.tsx - Pages · Dashboard, briefs, entities, events, alerts, and search screens.
frontend/app/page.tsx - Dashboard widgets · Visualizes intelligence with KPIs, trends, entity rankings, and feeds.
frontend/components/dashboard/trend-chart.tsx - Layout UI · Provides header, sidebar, and status bar navigation chrome.
frontend/components/layout/header.tsx - Shared UI · Reusable cards, badges, empty states, and loading indicators.
frontend/components/shared/event-card.tsx - API client · Centralizes backend HTTP requests for pages and widgets.
frontend/lib/api.ts - WebSocket client · Handles live feed connections, reconnection, and streaming updates.
frontend/lib/websocket.ts - Data hooks · Reusable data-fetching and subscription hooks for the UI.
frontend/lib/hooks.ts - UI types · Mirrors backend domain models for consistent frontend rendering.
frontend/lib/types.ts
Connections 48
- App entry → Config: loads · Bootstraps runtime settings from configuration.
- App entry → Events API: serves · Registers API routes for event access.
- App entry → Briefs API: serves · Registers API routes for briefs and scans.
- App entry → Entities API: serves · Registers API routes for entity drill-downs.
- App entry → Alerts API: serves · Registers API routes for alert management.
- App entry → Sources API: serves · Registers operational and health endpoints.
- App entry → Scheduler: starts · Launches recurring polling and autonomous analysis.
- App entry → Live feed: exposes · Enables the live event feed endpoint.
- Events API → Store layer: reads · Fetches persisted events and derived views.
- Briefs API → Brief generator: triggers · Allows manual scan and brief generation actions.
- Briefs API → Store layer: reads/writes · Persists and retrieves briefs and clusters.
- Entities API → Store layer: reads · Serves entity history and analysis from storage.
- Alerts API → Store layer: reads/writes · Stores alert rules and dismissal state.
- Sources API → Store layer: reads · Returns source status and polling diagnostics.
- Scheduler → FDA monitor: polls · Runs frequent FDA ingestion jobs.
- Scheduler → Label monitor: polls · Runs label change monitoring jobs.
- Scheduler → News crawler: polls · Runs news ingestion jobs.
- Scheduler → Trial monitor: polls · Runs trial update ingestion jobs.
- Scheduler → Brief generator: scans · Invokes the autonomous cluster-to-brief loop.
- FDA monitor → Store layer: writes · Persists normalized event records.
- Label monitor → Store layer: writes · Persists normalized event records.
- News crawler → Store layer: writes · Persists normalized event records.
- Trial monitor → Store layer: writes · Persists normalized event records.
- FDA monitor → Live feed: pushes · Broadcasts newly ingested events live.
- Label monitor → Live feed: pushes · Broadcasts newly ingested events live.
- News crawler → Live feed: pushes · Broadcasts newly ingested events live.
- Trial monitor → Live feed: pushes · Broadcasts newly ingested events live.
- Store layer → Brief generator: feeds · Provides events and prior briefs for analysis.
- Brief generator → Summarizer: uses · Generates concise strategic narrative output.
- Brief generator → Store layer: writes · Stores clusters, briefs, and analysis results.
- Brief generator → Live feed: publishes · Emits new intelligence updates to live clients.
- Store layer → Supabase store: implements · Backed by the Supabase/PostgreSQL adapter when available.
- Supabase store → DB schema: matches · Uses the defined database schema.
- App shell → Pages: hosts · Wraps the route-level application pages.
- Pages → API client: fetches · Loads backend data for screens and detail views.
- Pages → WebSocket client: subscribes · Consumes live feed updates where needed.
- Pages → Dashboard widgets: renders · Composes dashboard analytics and widgets.
- Pages → Shared UI: uses · Reuses common visual components across views.
- App shell → Layout UI: wraps · Provides persistent navigation and status chrome.
- Pages → Data hooks: uses · Coordinates data access and subscriptions.
- Data hooks → API client: calls · Wraps REST access into reusable hooks.
- Data hooks → WebSocket client: subscribes · Wraps websocket updates into reusable hooks.
- API client → Events API: calls · Consumes events endpoints.
- API client → Briefs API: calls · Consumes briefs endpoints.
- API client → Entities API: calls · Consumes entities endpoints.
- API client → Alerts API: calls · Consumes alerts endpoints.
- API client → Sources API: calls · Consumes source health endpoints.
- UI types → Domain models: mirrors · Keeps UI payload shapes aligned with backend domain models.