feat(quota): add 2-level database-backed resource quota system (#318)

* feat(quota): add database-backed resource quota system

* feat(webui): add resource quota management in team settings (#319)

* feat(webui): add resource quota settings in team management

* refactor(webui): update quotas list to full row layout and restrict skill/mcp to select list

* feat(quota): refactor quota system to 2-level rules and records

* fix(webui): fix overflow and scroll area in quota usage records dialog

* fix(webui): ensure quota records scroll area has explicit max height

* feat(webui): improve quota cards interaction and restrict editable fields in edit mode

* fix(quota): serialize initial records, validate merged updates, and reset on dimension changes (#320)

* feat(quota): replace network quota with agent tools quota (#321)

* feat(webui): show remaining time until next refresh in quota usage records

* fix(webui): rename bash quota resource to Agent Bash Calls

* refactor(quota): remove unused reserved field from quota records

* refactor(quota): remove quota rule cache (#322)
This commit is contained in:
Yiling-J
2026-08-17 20:24:05 +08:00
committed by GitHub
parent dd82daf65c
commit eed0264b9c
200 changed files with 13118 additions and 50 deletions
@@ -0,0 +1,68 @@
-- CreateEnum
CREATE TYPE "QuotaScopeMode" AS ENUM ('all_members', 'each_member', 'selected_members');
-- CreateEnum
CREATE TYPE "QuotaPeriod" AS ENUM ('1hour', '5hour', '1day', '7day');
-- CreateEnum
CREATE TYPE "QuotaResourceType" AS ENUM ('agent_total_tokens', 'agent_cost', 'agent_skill_call_count', 'agent_mcp_call_count', 'agent_bash_call_count', 'agent_tool_call_count');
-- CreateTable
CREATE TABLE "quota_rules" (
"id" TEXT NOT NULL,
"team_id" TEXT NOT NULL,
"scope_mode" "QuotaScopeMode" NOT NULL,
"role" "TeamMemberRole",
"user_ids" JSONB,
"resource" "QuotaResourceType" NOT NULL,
"resource_data" JSONB,
"limit" DOUBLE PRECISION NOT NULL,
"period" "QuotaPeriod" NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "quota_rules_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "quota_records" (
"id" TEXT NOT NULL,
"rule_id" TEXT NOT NULL,
"team_id" TEXT NOT NULL,
"user_id" TEXT,
"period_start" TIMESTAMP(3),
"period_end" TIMESTAMP(3),
"consumed" DOUBLE PRECISION NOT NULL DEFAULT 0,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "quota_records_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "quota_rules_team_id_enabled_idx" ON "quota_rules"("team_id", "enabled");
-- CreateIndex
CREATE INDEX "quota_rules_team_id_resource_idx" ON "quota_rules"("team_id", "resource");
-- CreateIndex
CREATE INDEX "quota_records_team_id_user_id_idx" ON "quota_records"("team_id", "user_id");
-- CreateIndex
CREATE INDEX "quota_records_rule_id_idx" ON "quota_records"("rule_id");
-- CreateIndex
CREATE UNIQUE INDEX "quota_records_rule_id_user_id_key" ON "quota_records"("rule_id", "user_id");
-- AddForeignKey
ALTER TABLE "quota_rules" ADD CONSTRAINT "quota_rules_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "teams"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "quota_records" ADD CONSTRAINT "quota_records_rule_id_fkey" FOREIGN KEY ("rule_id") REFERENCES "quota_rules"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "quota_records" ADD CONSTRAINT "quota_records_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "teams"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "quota_records" ADD CONSTRAINT "quota_records_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+70
View File
@@ -135,6 +135,28 @@ enum WatermarkFileStatus {
failed
}
enum QuotaScopeMode {
all_members
each_member
selected_members
}
enum QuotaPeriod {
one_hour @map("1hour")
five_hours @map("5hour")
one_day @map("1day")
seven_days @map("7day")
}
enum QuotaResourceType {
agent_total_tokens
agent_cost
agent_skill_call_count
agent_mcp_call_count
agent_bash_call_count
agent_tool_call_count
}
// ----------------------------------------------------------------------
// Models
// ----------------------------------------------------------------------
@@ -172,6 +194,7 @@ model User {
apiTokens ApiToken[] @relation("UserToApiToken")
aiUsages AiUsage[] @relation("UserToAiUsage")
auditLogs AuditLog[] @relation("UserToAuditLog")
quotaRecords QuotaRecord[] @relation("UserToQuotaRecord")
@@map("users")
}
@@ -258,6 +281,8 @@ model Team {
aiUsages AiUsage[] @relation("TeamToAiUsage")
auditLogs AuditLog[] @relation("TeamToAuditLog")
watermarkTemplates WatermarkTemplate[] @relation("TeamToWatermarkTemplate")
quotaRules QuotaRule[] @relation("TeamToQuotaRule")
quotaRecords QuotaRecord[] @relation("TeamToQuotaRecord")
@@map("teams")
}
@@ -1018,4 +1043,49 @@ model AuditLog {
@@map("audit_logs")
}
model QuotaRule {
id String @id @default(ulid())
teamId String @map("team_id")
scopeMode QuotaScopeMode @map("scope_mode")
role TeamMemberRole? @map("role")
/// [QuotaUserIds]
userIds Json? @map("user_ids")
resource QuotaResourceType @map("resource")
/// [QuotaResourceData]
resourceData Json? @map("resource_data")
limit Float @map("limit")
period QuotaPeriod @map("period")
enabled Boolean @default(true) @map("enabled")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
team Team @relation("TeamToQuotaRule", fields: [teamId], references: [id], onDelete: Cascade)
records QuotaRecord[] @relation("QuotaRuleToRecord")
@@index([teamId, enabled])
@@index([teamId, resource])
@@map("quota_rules")
}
model QuotaRecord {
id String @id @default(ulid())
ruleId String @map("rule_id")
teamId String @map("team_id")
userId String? @map("user_id")
periodStart DateTime? @map("period_start")
periodEnd DateTime? @map("period_end")
consumed Float @default(0) @map("consumed")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
rule QuotaRule @relation("QuotaRuleToRecord", fields: [ruleId], references: [id], onDelete: Cascade)
team Team @relation("TeamToQuotaRecord", fields: [teamId], references: [id], onDelete: Cascade)
user User? @relation("UserToQuotaRecord", fields: [userId], references: [id], onDelete: Cascade)
@@unique([ruleId, userId])
@@index([teamId, userId])
@@index([ruleId])
@@map("quota_records")
}