Skip to content

OwenCedaric/cf-worker-summary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Article Summarizer Cloudflare Worker

This Cloudflare Worker provides an API endpoint for summarizing web articles using various AI providers. It supports OpenAI, Anthropic, and Cloudflare‘s AI services. The worker can generate summaries in multiple languages and includes features like caching and rate limiting.

Features

  • Summarizes web articles from allowed domains
  • Document Support: Direct summarization of PDF, Word, Excel, PowerPoint and other document formats
  • Support for multiple AI providers (OpenAI, Anthropic, Google, Cloudflare AI)
  • Engineered Architecture: Based on Hono framework, providing standard routing and middleware support
  • Structured Output: Automatically generates TL;DR, Key Takeaways, and Context
  • High-Performance Caching: Uses Stale-While-Revalidate mechanism for background updates
  • Hybrid Content Extraction: Combines Jina Reader and Cloudflare Workers AI for optimal results
  • Automatic language detection and multi-language support
  • Configurable truncation and custom prompt templates

Setup

  1. Clone this repository or copy the worker script to your Cloudflare Workers project.
  2. Set up the required environment variables (see below).
  3. Configure the necessary Cloudflare Worker bindings (see below).
  4. Deploy the worker to your Cloudflare account.

Environment Variables

The following environment variables need to be set:

Required

  • ALLOWED_DOMAINS: Comma-separated list of allowed domains for article URLs (e.g., *.example.com).

Optional (AI Configuration)

  • AI_PROVIDER: The AI service provider to use (openai, anthropic, google, or cloudflare). Defaults to cloudflare if not set.
  • AI_MODEL: The specific AI model to use. Defaults to @cf/meta/llama-4-scout-17b-16e-instruct if not set.
  • AI_API_KEY: Your API key for the chosen AI provider. Not required for cloudflare provider if AI resource is bound.
  • AI_ENDPOINT: (Optional) Custom endpoint URL for the AI API.
  • PROMPT_TEMPLATE: (Optional) Custom prompt template for AI requests.

Other Options

  • CACHE_TTL: Cache time-to-live in seconds (default 604800 for 7 days).
  • MAX_CONTENT_LENGTH: Maximum allowed length of article content to process (default 10000).
  • SUMMARY_MIN_LENGTH: Minimum length requirements for generated summaries (default 200).

PROMPT_TEMPLATE

The PROMPT_TEMPLATE environment variable allows you to customize the AI prompt instructions. By default, the worker uses a highly optimized Simplified Chinese instruction set that guides the model to:

  • Plain Text Output: Directly output a high-quality summary without any Markdown syntax.
  • Deep Summary: Extract 2-3 core takeaways into a single, cohesive, and professional paragraph.
  • High Stability: This format ensures maximum output consistency across all modern LLMs.

If customizing, it's recommended to keep the ${language} placeholder for multilingual support.

Cloudflare Worker Bindings

This Worker requires specific Cloudflare Worker bindings to function correctly:

AI Binding

Used to call Cloudflare AI models directly.

  1. Add an AI binding in the Cloudflare Worker settings.

D1 Database Binding

Used for caching generated summaries.

  1. Create a new D1 database in the Cloudflare D1 console.

  2. Create the required table using the following SQL statement:

    CREATE TABLE IF NOT EXISTS summaries (
      article_url TEXT NOT NULL,
      summary TEXT NOT NULL,
      model TEXT NOT NULL,
      language TEXT NOT NULL,
      created_at INTEGER NOT NULL,
      PRIMARY KEY (article_url, language)
    );
  3. In your Worker‘s settings, add a D1 Database binding.

  4. Name the binding DB.

AI Worker Binding (MUST, for Cloudflare AI)

You need to set up an AI Worker binding.

  1. Ensure that AI capabilities are enabled for your Cloudflare account.
  2. In your Worker’s settings, add an AI binding.
  3. Name the binding AI.

Deployment

After setting up all necessary environment variables and bindings, you can deploy your Worker:

  1. Using the Wrangler CLI:
    wrangler deploy
    
  2. Or by uploading your Worker code through the Cloudflare console.

Ensure you have correctly configured all necessary bindings and environment variables before deployment.

Usage

To use the API, send a GET request to the worker‘s URL with the following query parameters:

  • url: The URL of the article to summarize
  • lang: (Optional) The language for the summary. If not provided, it will be determined based on the Accept-Language header or default to English.

Example:

https://your-worker-url.workers.dev/summary?url=https://example.com/article&lang=en

The API will return a JSON response with the following structure:

{
  "summary": "The generated summary of the article",
  "model": "The AI model used for summarization"
}

Multi-language Support

The worker supports generating summaries in multiple languages. It automatically extracts BCP-47 language codes (e.g., en-US, zh-CN) from user request parameters or the Accept-Language header and passes them directly to the AI model. Modern LLMs are natively capable of understanding these codes and outputting content in the corresponding language.

Error Handling

The API will return appropriate error messages and status codes for various error conditions, such as:

  • Invalid or missing article URL
  • Article from a non-allowed domain
  • Rate limit exceeded
  • Content too long
  • AI API errors

Detailed Features

Based on Hono Framework

The project has been refactored using the Hono framework. This provides:

  • Semantic Routing: Clear and standard API routing.
  • Improved Middlewares: Standard CORS and error handling.
  • Modern Standards: Consistent with Cloudflare Workers' best practices.

Caching Strategy (SWR)

Summaries are persisted in the D1 database.

  • Stale-While-Revalidate: If the cache is expired (exceeding CACHE_TTL), the worker returns the stale summary immediately while triggering a fresh summary generation in the background.
  • This ensures users always get sub-second responses while maintaining eventual consistency of content.

Content Fetching

The worker uses a hybrid approach to ensure high-quality content extraction:

  1. Direct Document Detection: If the URL points to a document (PDF, Docx, etc.), it uses Cloudflare Workers AI toMarkdown() directly.
  2. Jina Reader (Primary): For standard web URLs, it defaults to using Jina Reader for its superior anti-bot and cleaning capabilities.
  3. Cloudflare Fallback: If Jina Reader fails, it falls back to a direct fetch and uses Cloudflare's toMarkdown() for robust content extraction from HTML.

Security Considerations

  • Ensure that the ALLOWED_DOMAINS environment variable is properly set to prevent summarization of unauthorized websites.
  • Keep your AI API keys secure and do not expose them in the code or public repositories.
  • The worker implements CORS headers. Adjust these if you need to restrict access to specific origins.

Limitations

  • The worker can only summarize articles from the allowed domains specified in the ALLOWED_DOMAINS environment variable.
  • The maximum content length that can be processed is limited by the MAX_CONTENT_LENGTH environment variable.
  • The quality and accuracy of summaries depend on the chosen AI provider and model.
  • The prompt templates cannot be modified dynamically during runtime. They must be set as environment variables before deploying the worker.

Contributing

Contributions to improve the worker are welcome. Please submit issues and pull requests on the project‘s repository.

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPLv3). You may use, modify, and distribute this software under the terms of the AGPLv3.

The full text of the license can be found in the LICENSE file or at the following link: https://www.gnu.org/licenses/agpl-3.0.html

Key License Terms

  1. You have the freedom to run, study, and modify the program.
  2. You can redistribute copies of the original or modified versions of the program, provided you comply with the same license.
  3. If you modify this software to offer it as a service over a network, you must make the complete source code of your modifications available to the recipients of the service.

For more information on your rights and obligations, please refer to the full AGPLv3 license text.

About

基于 Cloudflare Workers 的博客 AI 摘要服务。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors