feat: add AI Guardian for AI call cost and abuse control#6
Open
LyndonDing wants to merge 1 commit into
Open
Conversation
Introduce AiGuardian as the unified gatekeeper for OpenAI calls, combining Redis-backed distributed locking, request idempotency, sliding-window rate limiting, quota reservation with stale recovery, and Postgres audit logging. - Add ai-guardian module (guardian, rate-limiter, quota-service, idempotency-guard, etc.) - Extend Prisma schema with User quota fields and AiAuditLog model - Integrate demo AI app with requestId idempotency and _meta response contract - Add admin dashboard for audit logs and usage statistics - Add Vitest tests, Wasp mocks, and pre-deploy/smoke test scripts - Configure Redis and AI Guardian env variables 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.
feat: add AI Guardian for AI call cost and abuse control
Summary
为 Open SaaS Demo AI 应用引入 AI Guardian 模块,在 AI 调用链路中统一实现成本与滥用控制。通过 Redis + Postgres 组合,提供分布式锁、请求幂等、滑动窗口限流、配额预扣/回滚、陈旧预留恢复及全链路审计日志,并配套 Admin 仪表盘与部署前检查脚本。
背景与目标
Demo AI App 的
generateGptResponse原先缺少系统级防护,存在重复扣费、并发刷量、无限调用等风险。本 PR 在不改变 Wasp 架构的前提下,将 AI 调用收口到AiGuardian统一门面,实现可观测、可配置、可测试的防护层。架构概览
调用生命周期:
enter()— 分布式锁 → 幂等检查 → 滑动窗口限流 → 配额预扣exit()— 写入 token/成本审计、确认或回滚配额、释放锁主要变更
核心模块 (
src/ai-guardian/)guardian.tsenter/exit/forceRelease)distributed-lock.tsidempotency-guard.tsrequestId的幂等保护rate-limiter.tsquota-service.tsstale-recovery.tscall-logger.tsoperations.ts数据层
User新增 5 个字段:aiDailyCallCount、aiDailyCallDate、aiMonthlyCallCount、aiMonthlyCallMonth、creditsVersionAiAuditLog表,记录 traceId、requestId、status、token 用量、估算成本等业务集成
generateGptResponse接入 AiGuardian,前端传入 UUIDrequestId,响应携带_meta(traceId / usage / quota)ai-client.ts,分离 OpenAI 调用逻辑DemoAppPage适配 requestId 生成与 Guardian 错误提示管理后台
/admin/ai-guardian仪表盘:审计日志列表 + 用量统计(成功率、限流/超额次数、Token 消耗、日维度 breakdown)测试与运维
run-unit-tests.sh、pre-deploy-check.sh、smoke-test.shpre-deploy-checklist.md配置
AI_GUARDIAN_*环境变量(Redis URL、限流阈值、日/月配额、Token 单价、锁 TTL、时区)ioredis、vitestTest plan
cd template/app && npm run test:unit(或./scripts/run-unit-tests.sh)wasp db migrate-dev,确认AiAuditLog表及User新字段创建成功.env.server中的AI_GUARDIAN_REDIS_URL和 OpenAI Key,启动wasp start_meta.traceId且AiAuditLog写入 SUCCESS 记录requestId重复提交,第二次应返回 DUPLICATE 或缓存结果AI_GUARDIAN_RATE_LIMIT_PER_MINUTE,应返回 429/admin/ai-guardian,确认审计日志和用量统计正常展示./scripts/pre-deploy-check.sh通过全部检查项部署注意事项
AI_GUARDIAN_REDIS_URL),否则自动降级为 DB 模式.env.server.exampleAI_GUARDIAN_QUOTA_TIMEZONE配置(默认 UTC)变更规模
feat: add AI Guardian for AI call cost and abuse control