# ahacad/hn-summarizer architecture

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

- Interactive diagram: https://gitdiagram.com/ahacad/hn-summarizer
- Repository: https://github.com/ahacad/hn-summarizer

## Overview

##### Main Components
- Data Collection Layer
- This includes the HackerNews API client (see src/services/hackernews/client.ts) and the story-fetcher worker (src/workers/story-fetcher.ts). It is responsible for polling HackerNews for top stories.
- Content Extraction & Processing Layer
- The extraction and cleaning services (located in src/services/content, such as extractor.ts and cleaner.ts) convert raw article content into a clean format.
- Summarization Layer
- Summarization functionality is encapsulated in src/services/summarization (e.g., google-ai.ts, prompt-templates.ts) which leverages Google AI (Gemini) to generate concise summaries.
- Storage Layer
- Uses two distinct storage components:
▫ D1 (SQL-based) for storing metadata and story-related information (src/storage/d1).
▫ R2 (object storage) for content/summaries (src/storage/r2).
- Notification Layer
- Handles the delivery of summaries via messaging platforms. This includes the Telegram service (src/services/notifications/telegram.ts) and the Discord service (src/services/notifications/discord.ts), as well as the notification-sender worker (src/workers/notification-sender.ts).
- API Routing Layer
- The REST API routes (under src/api and its routes directory) facilitate external interaction and possibly trigger manual refreshes/inspections.
- Worker Handlers
- Several worker files exist (in src/workers) to coordinate scheduled tasks (content processing, summary generation, notifications).

##### Relationships and Interactions
- The system starts with the story-fetcher worker that retrieves top stories via the HackerNews API client.
- Each story’s content is then passed to the content extraction services (extractor and cleaner) to remove extraneous elements.
- The cleaned content is processed by the summarization service utilizing Google AI (Gemini), where prompt templates (from prompt-templates.ts) are applied.
- Processed data is stored: metadata or story info is saved in D1; the extracted content and summaries are saved in R2.
- The notification worker takes the generated summary and utilizes the notification services (Telegram and/or Discord) to deliver the updates.
- The API routes expose endpoints for potential real-time interactions or status checks.
- Additional utilities (in src/utils) provide logging, error handling, and metrics which are part of the overall system health and debugging process.

##### Architectural Patterns & Design Principles
- Serverless Architecture – Every major operation (fetching, processing, summarizing, sending notifications) is implemented as a discrete worker function that runs on Cloudflare Workers.
- Modularity and Extensibility – The project is highly modular with separate services for distinct functions, making it easy to extend (e.g., integrating additional notification channels or another LLM provider in the future).

##### Relevant Technologies and Frameworks
- Cloudflare Workers (serverless compute)
- Cloudflare D1 (database) and R2 (object storage)
- Google AI (Gemini) for summarization operations
- External Firecrawl API for content extraction
- Node.js as the runtime environment
- Testing with Jest, including several tests across components to ensure reliability

##### Final Elements
- Clear and concise labels for each component.
- Detailed notes or legends that help explain what each component does and what technologies it represents.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Collection Layer
    subgraph "Data Collection Layer"
        HNC["HackerNews API Client"]:::data
        SFW["Story-Fetcher Worker"]:::worker
    end
    HNC -->|"fetchStories"| SFW
    HNC -->|"calls"| HN_API

    %% Content Extraction & Processing Layer
    subgraph "Content Extraction & Processing Layer"
        EX["Content Extractor"]:::extract
        CL["Content Cleaner"]:::extract
        FC["Firecrawl Client"]:::extract
    end
    SFW -->|"sendsStories"| EX
    EX -->|"extractsContent"| CL
    EX -->|"uses"| FC
    FC -->|"calls"| FCAPI

    %% Worker Handlers
    subgraph "Worker Handlers"
        CPW["Content Processor Worker"]:::worker
        SGW["Summary Generator Worker"]:::worker
    end
    EX -->|"triggers"| CPW
    CL -->|"triggers"| CPW

    %% Summarization Layer
    subgraph "Summarization Layer"
        GAI["Google AI Summarization Service"]:::summarize
        PT["Prompt Templates"]:::summarize
    end
    CL -->|"sendsCleanContent"| GAI
    GAI -->|"uses"| PT
    GAI -->|"summarizes"| GAI_EXT
    GAI -->|"triggers"| SGW

    %% Storage Layer
    subgraph "Storage Layer"
        D1["D1 Storage (Story Metadata)"]:::storage
        R2["R2 Storage (Content/Summary)"]:::storage
    end
    GAI -->|"storesMetadata"| D1
    GAI -->|"storesContent"| R2

    %% Notification Layer
    subgraph "Notification Layer"
        NSW["Notification-Sender Worker"]:::worker
        TG["Telegram Notification Service"]:::notification
        DS["Discord Notification Service"]:::notification
    end
    GAI -->|"notifies"| NSW
    NSW -->|"sendsTo"| TG
    NSW -->|"sendsTo"| DS

    %% API Routing Layer
    subgraph "API Routing Layer"
        AR["API Router"]:::api
        ARS["API Route for Stories"]:::api
        ARSUM["API Route for Summaries"]:::api
        ARW["API Route for Workers"]:::api
    end
    AR -->|"routesTo"| ARS
    AR -->|"routesTo"| ARSUM
    AR -->|"routesTo"| ARW

    %% External Nodes
    HN_API["HackerNews API"]:::external
    FCAPI["Firecrawl API"]:::external
    GAI_EXT["Google Gemini AI"]:::external

    %% Click Events
    click HNC "https://github.com/ahacad/hn-summarizer/blob/master/src/services/hackernews/client.ts"
    click SFW "https://github.com/ahacad/hn-summarizer/blob/master/src/workers/story-fetcher.ts"
    click EX "https://github.com/ahacad/hn-summarizer/blob/master/src/services/content/extractor.ts"
    click CL "https://github.com/ahacad/hn-summarizer/blob/master/src/services/content/cleaner.ts"
    click FC "https://github.com/ahacad/hn-summarizer/blob/master/src/services/content/firecrawl-client.ts"
    click GAI "https://github.com/ahacad/hn-summarizer/blob/master/src/services/summarization/google-ai.ts"
    click PT "https://github.com/ahacad/hn-summarizer/blob/master/src/services/summarization/prompt-templates.ts"
    click D1 "https://github.com/ahacad/hn-summarizer/blob/master/src/storage/d1/story-repository.ts"
    click R2 "https://github.com/ahacad/hn-summarizer/blob/master/src/storage/r2/content-repository.ts"
    click TG "https://github.com/ahacad/hn-summarizer/blob/master/src/services/notifications/telegram.ts"
    click DS "https://github.com/ahacad/hn-summarizer/blob/master/src/services/notifications/discord.ts"
    click NSW "https://github.com/ahacad/hn-summarizer/blob/master/src/workers/notification-sender.ts"
    click AR "https://github.com/ahacad/hn-summarizer/blob/master/src/api/router.ts"
    click ARS "https://github.com/ahacad/hn-summarizer/blob/master/src/api/routes/stories.ts"
    click ARSUM "https://github.com/ahacad/hn-summarizer/blob/master/src/api/routes/summaries.ts"
    click ARW "https://github.com/ahacad/hn-summarizer/blob/master/src/api/routes/workers.ts"
    click CPW "https://github.com/ahacad/hn-summarizer/blob/master/src/workers/content-processor.ts"
    click SGW "https://github.com/ahacad/hn-summarizer/blob/master/src/workers/summary-generator.ts"

    %% Styles
    classDef data fill:#f9c74f,stroke:#000,stroke-width:1px;
    classDef extract fill:#90be6d,stroke:#000,stroke-width:1px;
    classDef summarize fill:#f9844a,stroke:#000,stroke-width:1px;
    classDef storage fill:#577590,stroke:#fff,stroke-width:1px;
    classDef notification fill:#277da1,stroke:#fff,stroke-width:1px;
    classDef api fill:#9d4edd,stroke:#fff,stroke-width:1px;
    classDef worker fill:#f94144,stroke:#000,stroke-width:1px;
    classDef external fill:#6a4c93,stroke:#fff,stroke-width:1px,stroke-dasharray: 5,5;
```
