emdash-plugin-ai-comment-moderation
v1.0.0
Published
An EmDash plugin for comment moderation using Cloudflare Workers AI.
Readme
emdash-plugin-ai-comment-moderation
An EmDash plugin for comment moderation using Cloudflare Workers AI.
Features
- Spam & Toxicity Detection: Analyzes comments to identify spam and toxic content.
- KV-Driven Configuration: Settings are stored in EmDash's Key-Value store. No environment variables required.
- Fail-safe Fallback: Replicates EmDash's built-in 4-step moderation logic if the Cloudflare AI is unavailable or unconfigured.
Installation
Add the plugin to your astro-blog (or other EmDash-powered project):
"dependencies": {
"emdash-plugin-ai-comment-moderation": "file:../emdash-plugin-ai-comment-moderation"
}In your astro.config.mjs:
import { emdash } from "emdash/astro";
import { aiCommentModerationPlugin } from "emdash-plugin-ai-comment-moderation";
export default defineConfig({
// ...
integrations: [
emdash({
plugins: [aiCommentModerationPlugin()],
// ...
})
]
});Configuration
Seed or set the following keys in your plugin's KV store:
settings:accountId: Your Cloudflare Account ID.settings:apiToken: Your Cloudflare API Token (with access to Workers AI).settings:model: (Optional) The Cloudflare Workers AI model to run. Defaults to@cf/meta/llama-3.1-8b-instruct(seeded during installation).
Moderation Logic
- Empty check: Empty comments are automatically held as
pending. - AI Moderation:
- Queries Cloudflare Workers AI using the configured model.
- Parses the JSON response for
{ spam: boolean, toxic: boolean }. - Decisions:
spam=true: Marked asspam.toxic=true(and not spam): Marked aspending.- Otherwise: Marked as
approved.
- The raw decision (e.g.
AI: spam=false, toxic=false) is saved in the moderationreason.
- Fallback: If AI credentials are not configured, the API fails, or parsing fails, the plugin falls back to EmDash's default 4-step logic:
- Auto-approve if
commentsAutoApproveUsersis enabled and user is authenticated. - Auto-approve if collection moderation is set to
"none". - Auto-approve if collection moderation is
"first_time"and commenter is returning (has previous approved comments). - Otherwise, holds as
pending.
- Auto-approve if
