Integrate OpenAI moderation endpoint (conflict-clean)#17
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAdds optional synchronous OpenAI moderation: comment POSTs are pre-moderated and rejected (400) if flagged; page title generation is pre-moderated and, if flagged, schedules slug banning and returns 404 noindex to avoid generation when OPENAI_API_KEY is set. ChangesOpenAI Moderation System
Sequence Diagram(s)sequenceDiagram
participant Client
participant CommentWorker as Comment Worker
participant PageWorker as Page Worker
participant OpenAI as OpenAI Moderation API
participant DB as Database
rect rgba(230,180,200,0.5)
Note over Client,CommentWorker: Comment Creation Flow
Client->>CommentWorker: POST /api/comments
CommentWorker->>OpenAI: openaiModerate(commentText, apiKey)
OpenAI-->>CommentWorker: flagged boolean
alt flagged
CommentWorker-->>Client: 400 (rejected)
else not flagged
CommentWorker->>DB: insert comment
CommentWorker-->>Client: 201 (created)
end
end
rect rgba(180,210,230,0.5)
Note over Client,PageWorker: Page Generation Flow
Client->>PageWorker: GET /api/page/:slug
PageWorker->>OpenAI: openaiModerate(title, apiKey)
OpenAI-->>PageWorker: flagged boolean
alt flagged
PageWorker->>DB: upsert article_moderation (banned)
PageWorker->>PageWorker: delete from ARTICLES KV
PageWorker-->>Client: 404 noindex (banned)
else not flagged
PageWorker-->>Client: stream generated content
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/worker/comments.ts`:
- Around line 425-431: The moderation call using openaiModerate guarded by
c.env.OPENAI_API_KEY is currently executed before ensureUser, letting expensive
OpenAI calls run even when ensureUser later rate-limits and returns 429; move
the entire block that calls openaiModerate and returns the flagged error so it
runs after the ensureUser invocation (preserve the same c.env.OPENAI_API_KEY
check and the returned JSON error) so cheap rate-limiting in ensureUser happens
first and only allowed requests consume OpenAI quota.
In `@src/worker/moderation.ts`:
- Around line 31-49: The fetch in openaiModerate lacks a timeout and can hang;
update openaiModerate to use an AbortController with a hard timeout (e.g., 3–10s
or configurable) by creating an AbortController, passing its signal into fetch,
scheduling a setTimeout to call controller.abort() after the timeout, and
clearing that timeout on success; ensure the catch handles aborted requests
(still returning false) and that the timer is always cleared to avoid leaks.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 274e4fbc-f9b7-4d46-bb22-0c3bae4ab1d6
📒 Files selected for processing (4)
src/worker/comments.tssrc/worker/index.tssrc/worker/moderation.tswrangler.toml
# Conflicts: # src/worker/comments.ts # src/worker/index.ts
This is a conflict-clean replacement for the moderation work originally proposed in PR #4.
Why this PR exists:
wrangler.tomlwhile adding OpenAI moderation configuration.moderation.ts.Please review this as the replacement for PR #4.
Related prior PR: #4
Summary by CodeRabbit
New Features
Chores