Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/worker/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "./identity";
import { slugify } from "./slug";
import { rateLimit, clientIp } from "./ratelimit";
import { moderateCommentNow } from "./moderation";
import { moderateCommentNow, openaiModerate } from "./moderation";

export interface CommentsEnv {
DB: D1Database;
Expand All @@ -16,6 +16,7 @@ export interface CommentsEnv {
OPENROUTER_MODEL: string;
OPENROUTER_MODERATION_MODEL?: string;
IDENT_PER_IP_PER_HOUR?: string;
OPENAI_API_KEY?: string;
}

const COOKIE_NAME = "hu_uid";
Expand Down Expand Up @@ -421,6 +422,14 @@ export function createCommentsApp() {
return c.json({ error: `comment exceeds ${MAX_BODY_LEN} chars` }, 400);
}

// Pre-save moderation
if (c.env.OPENAI_API_KEY) {
const flagged = await openaiModerate(body, c.env.OPENAI_API_KEY);
if (flagged) {
return c.json({ error: "comment was flagged by moderation" }, 400);
}
}

if (parent_id) {
const parent = await c.env.DB
.prepare("SELECT id, slug FROM comments WHERE id = ?")
Expand Down
22 changes: 22 additions & 0 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { isLikelyVpn } from "./vpn";
import { isPermanentlyBlockedSlug } from "./blocklist";
import { loadHints, saveHints } from "./hints";
import {
banSlugNow,
countRecentBansByIp,
enqueueArticleForModeration,
isSlugBanned,
openaiModerate,
runSweep,
} from "./moderation";

Expand All @@ -38,6 +40,9 @@ export interface Env {
// Per-IP rate limit for /api/search LLM-backed suggestions. Over the
// limit, search still returns DB matches but skips the hallucination call.
SEARCH_PER_IP_PER_HOUR?: string;
// Optional: OpenAI API key for synchronous pre-generation moderation.
// Set via: pnpm wrangler secret put OPENAI_API_KEY
OPENAI_API_KEY?: string;
}

interface StoredArticle {
Expand Down Expand Up @@ -552,6 +557,23 @@ app.get("/api/page/:slug", async (c) => {

const title = slugToTitle(slug);

// Check title against OpenAI Moderation API before spending LLM tokens
if (c.env.OPENAI_API_KEY) {
const flagged = await openaiModerate(title, c.env.OPENAI_API_KEY);
if (flagged) {
c.executionCtx.waitUntil(
banSlugNow(slug, c.env).catch((e) =>
console.error("banSlugNow failed", e)
)
);
return c.json(
{ error: "this entry has been removed by moderation", banned: true },
404,
{ "x-robots-tag": "noindex" }
);
}
}

// Pull every prior link-context blurb other articles have written about
// this slug. These become CANON the LLM must respect.
let priorHints: string[] = [];
Expand Down
52 changes: 52 additions & 0 deletions src/worker/moderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,58 @@ export interface ModerationEnv {
OPENROUTER_API_KEY: string;
OPENROUTER_MODEL: string;
OPENROUTER_MODERATION_MODEL?: string;
OPENAI_API_KEY?: string;
}

/**
* Call the OpenAI Moderation API and return true if the text was flagged.
* If this fails, the async sweep can still catch stragglers.
*/
export async function openaiModerate(
text: string,
apiKey: string
): Promise<boolean> {
try {
const res = await fetch("https://api.openai.com/v1/moderations", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({ model: "omni-moderation-latest", input: text }),
});
if (!res.ok) return false;
const json: any = await res.json();
return json?.results?.[0]?.flagged === true;
} catch {
return false;
}
}

/**
* Immediately mark slug as banned in DB and delete it from KV.
* Used when the pre-check flags a slug before generation starts.
*/
export async function banSlugNow(
slug: string,
env: ModerationEnv
): Promise<void> {
const now = Date.now();
try {
await env.ARTICLES.delete(slug);
} catch {}
try {
await env.DB
.prepare(
`INSERT INTO article_moderation (slug, status, reason, enqueued_at, checked_at)
VALUES (?, 'banned', ?, ?, ?)
ON CONFLICT(slug) DO UPDATE SET status='banned', reason=excluded.reason, checked_at=excluded.checked_at`
)
.bind(slug, "openai-moderation-precheck", now, now)
.run();
} catch (e) {
console.error("banSlugNow: DB write failed", slug, e);
}
}

const BATCH_SIZE = 30;
Expand Down
3 changes: 3 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ SEARCH_PER_IP_PER_HOUR = "15"

# OPENROUTER_API_KEY should be set as a secret:
# pnpm wrangler secret put OPENROUTER_API_KEY

# Enable OpenAI Moderation API for slugs and comments
# pnpm wrangler secret put OPENAI_API_KEY