Add AI usage controls with quota reservation, rate limiting, and call logs#3
Open
yuanmomoya wants to merge 1 commit into
Open
Add AI usage controls with quota reservation, rate limiting, and call logs#3yuanmomoya wants to merge 1 commit into
yuanmomoya wants to merge 1 commit into
Conversation
… logs Introduce a shared AI abuse-control layer (idempotency, sliding-window rate limits, credit pre-reservation/refund, and AiUsageLog auditing) for the Day Scheduler, Prompt Optimizer, and HTML animation features, plus admin usage dashboard and pending-log cleanup job. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AiUsageLogauditing) for the Day Scheduler, Prompt Optimizer, and HTML animation features./demo-appflow to reserve credits before OpenAI calls, refund on failure, and prevent duplicate/concurrent abuse.Problem
The existing AI schedule generation decremented credits after the OpenAI call and had no idempotency, rate limit, or usage logging. Concurrent requests could overspend AI budget and bypass user quotas.
Solution
AiUsageLogmodel with unique(userId, operation, idempotencyKey)constraint.src/ai/usageControl.tsfor idempotency checks, DB-backed rate limiting, credit pre-reservation, and failure refunds.generateGptResponseto use the control layer; added Prompt Optimizer and HTML animation actions reusing the same infrastructure./admin/ai-usagedashboard for call volume, tokens, and estimated cost.Design Trade-offs
requestHashinstead of raw prompt to reduce privacy risk.OPENAI_API_KEYis optional at startup; placeholder keys return 503 for AI actions to allow local dev without real keys.Testing
npm run lintnpm run prettier:checkprisma validate./opensaas-sh/tools/patch.shMigration
After pulling, run:
This creates the new
AiUsageLog,PromptOptimization,HtmlAnimation, andAnimationRenderJobtables.Follow-ups