# btahir/open-deep-research architecture

> Architecture diagram of the GitHub repository btahir/open-deep-research, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/btahir/open-deep-research
- Repository: https://github.com/btahir/open-deep-research

## Overview

##### Main Components
- Frontend Interface:
- Next.js pages located in the “app” directory, including the main entry (app/page.tsx), layout (app/layout.tsx), and dedicated feature pages (such as app/flow/page.tsx and app/report/[id]/page.tsx).
- UI components stored in “components” (including components/ui, flows, analytics, etc.) which build the interactive elements and visualization (such as the visual research flow, report generation, and knowledge base).
- Backend/API Endpoints:
- API routes found in “app/api” (for actions like analyze-results, consolidate-report, download, fetch-content, generate-question, optimize-research, parse-document, report, search).
- These endpoints are responsible for handling requests from the UI, processing data, and interacting with external services.
- Business Logic & Utilities:
- Libraries in the “lib” folder (such as config, documents, file-upload, gemini, knowledge-base, redis, and utils) that abstract out the functionalities such as rate limiting, search provider configuration, file handling, and integration with external AI platforms.
- External/Third-Party Services:
- Search APIs (Google Custom Search & Bing Search) for fetching search results.
- AI platforms (Google Gemini, OpenAI, Anthropic, DeepSeek, OpenRouter, Ollama for local models) which are configured under “lib/config.ts”.
- Content extraction service (JinaAI) which processes retrieved webpages.
- Upstash Redis used for rate limiting.
- Local File and Knowledge Base Support:
- The system supports file uploads (TXT, PDF, DOCX) integrated via file-upload utilities and custom UI actions.
- Knowledge Base functionality to save and retrieve previously generated reports (with data stored in browser localStorage via “lib/localStorage-utils.ts” and related hooks).

##### Relationships and Interactions
- The Frontend interacts with the API endpoints via HTTP requests.
- The API layer encapsulates business logic and uses “lib” utilities to connect with external search and AI services.
- The results from searches, content extraction, and AI report generation are relayed back to the UI which dynamically renders research flows and report content.
- Additionally, the configuration file “lib/config.ts” ties together platform-specific settings (rate limits, API keys, and model configurations) that influence how the backend functions call external APIs.

##### Architectural Patterns & Design Principles
- Modular separation of concerns: The project cleanly divides UI components, API endpoints, and library/helpers.
- API-centric design: Modern REST-like endpoints using Next.js API routes to separate business logic from presentation.
- Integration-centric architecture: Multiple external services (search providers, AI platforms, rate limiting via Redis) are abstracted via configuration and thin API wrappers.
- Dynamic, recursive “flow” design to support deep research, where UI components like “question-node”, “report-node”, and “search-node” visually map out research progress.

##### Final Recommendations for the Principal Software Engineer
- Revisit key areas like the “Flow” component to ensure its recursive and dynamic nature is visually expressed.

## Diagram (Mermaid)

```mermaid
graph TD
  %% Frontend UI
  subgraph "Frontend UI"
    subgraph "Next.js Pages"
      N_Main["Main Entry (app/page.tsx)"]
      N_Layout["Layout (app/layout.tsx)"]
      N_Flow["Flow Page (app/flow/page.tsx)"]
      N_Report["Report Detail Page (app/report/[id]/page.tsx)"]
    end
    subgraph "UI Components"
      N_General["General UI (components/ui)"]
      N_FlowUI["Flow UI (components/flow)"]
      N_Additional["Additional UI (components)"]
    end
  end

  %% API Layer
  subgraph "API Layer"
    API_Analyze["analyze-results (app/api/analyze-results/route.ts)"]
    API_Consolidate["consolidate-report (app/api/consolidate-report/route.ts)"]
    API_Download["download (app/api/download/route.ts)"]
    API_Fetch["fetch-content (app/api/fetch-content/route.ts)"]
    API_GenQuest["generate-question (app/api/generate-question/route.ts)"]
    API_OptResearch["optimize-research (app/api/optimize-research/route.ts)"]
    API_ParseDoc["parse-document (app/api/parse-document/route.ts)"]
    API_Report["report (app/api/report/route.ts)"]
    API_Search["search (app/api/search/route.ts)"]
  end

  %% Business Logic & Utilities
  subgraph "Business Logic & Utilities"
    BL_Config["Config (lib/config.ts)"]
    BL_Documents["Documents (lib/documents.ts)"]
    BL_FileUpload["File Upload (lib/file-upload.ts)"]
    BL_Gemini["Gemini (lib/gemini.ts)"]
    BL_KB["Knowledge Base (lib/knowledge-base.ts)"]
    BL_LocalStorage["Local Storage Utils (lib/localStorage-utils.ts)"]
    BL_Redis["Redis (lib/redis.ts)"]
    BL_Models["Models (lib/models.ts)"]
    BL_Utils["Utils (lib/utils.ts)"]
    KB_Hooks["KB Hooks (hooks/use-knowledge-base.ts)"]
  end

  %% External Services
  subgraph "External Services"
    ES_SearchAPIs["Search APIs (Google, Bing)"]
    ES_ContentExtraction["Content Extraction (JinaAI)"]
    ES_AIPlatforms["AI Platforms (Gemini, OpenAI, Anthropic, DeepSeek, OpenRouter, Ollama)"]
    ES_RateLimiting["Rate Limiting (Upstash Redis)"]
  end

  %% Data Flow: Primary Request-Response Chain
  N_Main -->|"HTTP_Request"| API_Analyze
  API_Analyze -->|"Process_Request"| BL_Config
  BL_Config -.->|"External_Call"| ES_SearchAPIs
  ES_SearchAPIs -.->|"External_Response"| BL_Config
  BL_Config -->|"Return_Result"| API_Analyze
  API_Analyze -->|"HTTP_Response"| N_Main

  %% Additional Flow for AI Platform usage via generate-question API
  API_GenQuest -->|"Process_Request"| BL_Gemini
  BL_Gemini -.->|"External_Call"| ES_AIPlatforms
  ES_AIPlatforms -.->|"External_Response"| BL_Gemini
  BL_Gemini -->|"Return_Result"| API_GenQuest

  %% Additional direct UI interaction with API Search endpoint
  N_FlowUI -->|"Initiate_Flow"| API_Search
  API_Search -->|"Return_Result"| N_FlowUI

  %% Styling
  class N_Main,N_Layout,N_Flow,N_Report,N_General,N_FlowUI,N_Additional frontend
  class API_Analyze,API_Consolidate,API_Download,API_Fetch,API_GenQuest,API_OptResearch,API_ParseDoc,API_Report,API_Search api
  class BL_Config,BL_Documents,BL_FileUpload,BL_Gemini,BL_KB,BL_LocalStorage,BL_Redis,BL_Models,BL_Utils,KB_Hooks business
  class ES_SearchAPIs,ES_ContentExtraction,ES_AIPlatforms,ES_RateLimiting external

  classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
  classDef api fill:#fff3cd,stroke:#856404,stroke-width:2px;
  classDef business fill:#d4edda,stroke:#155724,stroke-width:2px;
  classDef external fill:#f8d7da,stroke:#721c24,stroke-width:2px;

%% Click Events for Frontend Next.js Pages
click N_Main "https://github.com/btahir/open-deep-research/blob/main/app/page.tsx"
click N_Layout "https://github.com/btahir/open-deep-research/blob/main/app/layout.tsx"
click N_Flow "https://github.com/btahir/open-deep-research/blob/main/app/flow/page.tsx"
click N_Report "https://github.com/btahir/open-deep-research/blob/main/app/report/[id]/page.tsx"

%% Click Events for UI Components
click N_General "https://github.com/btahir/open-deep-research/tree/main/components/ui"
click N_FlowUI "https://github.com/btahir/open-deep-research/tree/main/components/flow"
click N_Additional "https://github.com/btahir/open-deep-research/tree/main/components"

%% Click Events for API Endpoints
click API_Analyze "https://github.com/btahir/open-deep-research/blob/main/app/api/analyze-results/route.ts"
click API_Consolidate "https://github.com/btahir/open-deep-research/blob/main/app/api/consolidate-report/route.ts"
click API_Download "https://github.com/btahir/open-deep-research/blob/main/app/api/download/route.ts"
click API_Fetch "https://github.com/btahir/open-deep-research/blob/main/app/api/fetch-content/route.ts"
click API_GenQuest "https://github.com/btahir/open-deep-research/blob/main/app/api/generate-question/route.ts"
click API_OptResearch "https://github.com/btahir/open-deep-research/blob/main/app/api/optimize-research/route.ts"
click API_ParseDoc "https://github.com/btahir/open-deep-research/blob/main/app/api/parse-document/route.ts"
click API_Report "https://github.com/btahir/open-deep-research/blob/main/app/api/report/route.ts"
click API_Search "https://github.com/btahir/open-deep-research/blob/main/app/api/search/route.ts"

%% Click Events for Business Logic & Utilities
click BL_Config "https://github.com/btahir/open-deep-research/blob/main/lib/config.ts"
click BL_Documents "https://github.com/btahir/open-deep-research/blob/main/lib/documents.ts"
click BL_FileUpload "https://github.com/btahir/open-deep-research/blob/main/lib/file-upload.ts"
click BL_Gemini "https://github.com/btahir/open-deep-research/blob/main/lib/gemini.ts"
click BL_KB "https://github.com/btahir/open-deep-research/blob/main/lib/knowledge-base.ts"
click BL_LocalStorage "https://github.com/btahir/open-deep-research/blob/main/lib/localStorage-utils.ts"
click BL_Redis "https://github.com/btahir/open-deep-research/blob/main/lib/redis.ts"
click BL_Models "https://github.com/btahir/open-deep-research/blob/main/lib/models.ts"
click BL_Utils "https://github.com/btahir/open-deep-research/blob/main/lib/utils.ts"
click KB_Hooks "https://github.com/btahir/open-deep-research/blob/main/hooks/use-knowledge-base.ts"
```
