mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
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:
@@ -46,6 +46,12 @@ export enum AuditAction {
|
||||
comment_update = 'comment_update',
|
||||
comment_delete = 'comment_delete',
|
||||
comment_complete = 'comment_complete',
|
||||
quota_rule_create = 'quota_rule_create',
|
||||
quota_rule_update = 'quota_rule_update',
|
||||
quota_rule_delete = 'quota_rule_delete',
|
||||
quota_policy_create = 'quota_policy_create',
|
||||
quota_policy_update = 'quota_policy_update',
|
||||
quota_policy_delete = 'quota_policy_delete',
|
||||
}
|
||||
|
||||
export const auditLogSchema = z.object({
|
||||
|
||||
@@ -22,3 +22,4 @@ export * from './versionStack'
|
||||
export * from './chat'
|
||||
export * from './auditLog'
|
||||
export * from './watermark'
|
||||
export * from './quota'
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
import { z } from 'zod'
|
||||
import { QuotaScopeMode, QuotaPeriod, QuotaResourceType } from '@shumai/db/enums'
|
||||
|
||||
export { QuotaScopeMode, QuotaPeriod, QuotaResourceType }
|
||||
|
||||
export const quotaScopeModeSchema = z.nativeEnum(QuotaScopeMode)
|
||||
export type QuotaScopeModeEnum = z.infer<typeof quotaScopeModeSchema>
|
||||
|
||||
export const quotaPeriodSchema = z.enum([
|
||||
'1hour',
|
||||
'5hour',
|
||||
'1day',
|
||||
'7day',
|
||||
'one_hour',
|
||||
'five_hours',
|
||||
'one_day',
|
||||
'seven_days',
|
||||
])
|
||||
export type QuotaPeriodEnum = z.infer<typeof quotaPeriodSchema>
|
||||
|
||||
export function normalizeQuotaPeriod(period: QuotaPeriodEnum | string): QuotaPeriod {
|
||||
switch (period) {
|
||||
case '1hour':
|
||||
case 'one_hour':
|
||||
return 'one_hour'
|
||||
case '5hour':
|
||||
case 'five_hours':
|
||||
return 'five_hours'
|
||||
case '1day':
|
||||
case 'one_day':
|
||||
return 'one_day'
|
||||
case '7day':
|
||||
case 'seven_days':
|
||||
return 'seven_days'
|
||||
default:
|
||||
return 'one_hour'
|
||||
}
|
||||
}
|
||||
|
||||
export function formatQuotaPeriod(period: QuotaPeriod | string): string {
|
||||
switch (period) {
|
||||
case 'one_hour':
|
||||
case '1hour':
|
||||
return '1hour'
|
||||
case 'five_hours':
|
||||
case '5hour':
|
||||
return '5hour'
|
||||
case 'one_day':
|
||||
case '1day':
|
||||
return '1day'
|
||||
case 'seven_days':
|
||||
case '7day':
|
||||
return '7day'
|
||||
default:
|
||||
return period
|
||||
}
|
||||
}
|
||||
|
||||
export const quotaResourceTypeSchema = z.nativeEnum(QuotaResourceType)
|
||||
export type QuotaResourceTypeEnum = z.infer<typeof quotaResourceTypeSchema>
|
||||
|
||||
export const quotaRoleSchema = z.enum(['owner', 'editor', 'reviewer'])
|
||||
export type QuotaRole = z.infer<typeof quotaRoleSchema>
|
||||
|
||||
export const skillResourceDataSchema = z.object({
|
||||
id: z.string().min(1, 'Skill ID is required'),
|
||||
})
|
||||
|
||||
export const mcpResourceDataSchema = z.object({
|
||||
id: z.string().min(1, 'MCP server ID is required'),
|
||||
})
|
||||
|
||||
export const bashResourceDataSchema = z.object({
|
||||
match: z.string().min(1, 'Bash match wildcard is required'),
|
||||
})
|
||||
|
||||
export const toolResourceDataSchema = z.object({
|
||||
name: z.string().min(1, 'Tool name is required'),
|
||||
})
|
||||
export type ToolResourceData = z.infer<typeof toolResourceDataSchema>
|
||||
|
||||
export const quotaResourceDataSchema = z.record(z.string(), z.unknown())
|
||||
export type QuotaResourceData = z.infer<typeof quotaResourceDataSchema>
|
||||
|
||||
export const quotaRecordResponseSchema = z.object({
|
||||
id: z.string().nullable(),
|
||||
ruleId: z.string(),
|
||||
teamId: z.string(),
|
||||
userId: z.string().nullable().optional(),
|
||||
user: z
|
||||
.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
email: z.string(),
|
||||
image: z.string().nullable().optional(),
|
||||
})
|
||||
.nullable()
|
||||
.optional(),
|
||||
periodStart: z.string().nullable().optional(),
|
||||
periodEnd: z.string().nullable().optional(),
|
||||
consumed: z.number(),
|
||||
remaining: z.number(),
|
||||
percent: z.number(),
|
||||
isWindowActive: z.boolean(),
|
||||
})
|
||||
export type QuotaRecordResponse = z.infer<typeof quotaRecordResponseSchema>
|
||||
|
||||
export const listQuotaRecordsResponseSchema = z.object({
|
||||
records: z.array(quotaRecordResponseSchema),
|
||||
total: z.number(),
|
||||
})
|
||||
export type ListQuotaRecordsResponse = z.infer<typeof listQuotaRecordsResponseSchema>
|
||||
|
||||
export const quotaRuleResponseSchema = z.object({
|
||||
id: z.string(),
|
||||
teamId: z.string(),
|
||||
scopeMode: quotaScopeModeSchema,
|
||||
role: quotaRoleSchema.nullable().optional(),
|
||||
userIds: z.array(z.string()).nullable().optional(),
|
||||
resource: quotaResourceTypeSchema,
|
||||
resourceData: quotaResourceDataSchema.nullable().optional(),
|
||||
limit: z.number(),
|
||||
period: quotaPeriodSchema,
|
||||
enabled: z.boolean(),
|
||||
recordsCount: z.number().optional(),
|
||||
createdAt: z.string(),
|
||||
updatedAt: z.string(),
|
||||
})
|
||||
export type QuotaRuleResponse = z.infer<typeof quotaRuleResponseSchema>
|
||||
|
||||
export const listQuotaRulesResponseSchema = z.object({
|
||||
rules: z.array(quotaRuleResponseSchema),
|
||||
total: z.number(),
|
||||
})
|
||||
export type ListQuotaRulesResponse = z.infer<typeof listQuotaRulesResponseSchema>
|
||||
|
||||
export const createQuotaRuleRequestSchema = z
|
||||
.object({
|
||||
scopeMode: quotaScopeModeSchema,
|
||||
role: quotaRoleSchema.optional().nullable(),
|
||||
userIds: z.array(z.string()).optional().nullable(),
|
||||
resource: quotaResourceTypeSchema,
|
||||
resourceData: quotaResourceDataSchema.optional().nullable(),
|
||||
limit: z.number().positive('Limit must be greater than 0'),
|
||||
period: quotaPeriodSchema,
|
||||
enabled: z.boolean().optional().default(true),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.scopeMode === 'selected_members') {
|
||||
if (!data.userIds || data.userIds.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'userIds must contain at least one user when scopeMode is "selected_members"',
|
||||
path: ['userIds'],
|
||||
})
|
||||
}
|
||||
}
|
||||
if (data.resource === 'agent_skill_call_count') {
|
||||
const res = skillResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.id is required for agent_skill_call_count',
|
||||
path: ['resourceData', 'id'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_mcp_call_count') {
|
||||
const res = mcpResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.id is required for agent_mcp_call_count',
|
||||
path: ['resourceData', 'id'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_bash_call_count') {
|
||||
const res = bashResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.match is required for agent_bash_call_count',
|
||||
path: ['resourceData', 'match'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_tool_call_count') {
|
||||
const res = toolResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.name is required for agent_tool_call_count',
|
||||
path: ['resourceData', 'name'],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
export type CreateQuotaRuleRequest = z.input<typeof createQuotaRuleRequestSchema>
|
||||
|
||||
export const updateQuotaRuleRequestSchema = z
|
||||
.object({
|
||||
scopeMode: quotaScopeModeSchema.optional(),
|
||||
role: quotaRoleSchema.optional().nullable(),
|
||||
userIds: z.array(z.string()).optional().nullable(),
|
||||
resource: quotaResourceTypeSchema.optional(),
|
||||
resourceData: quotaResourceDataSchema.optional().nullable(),
|
||||
limit: z.number().positive('Limit must be greater than 0').optional(),
|
||||
period: quotaPeriodSchema.optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.scopeMode === 'selected_members' && data.userIds !== undefined) {
|
||||
if (!data.userIds || data.userIds.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'userIds must contain at least one user when scopeMode is "selected_members"',
|
||||
path: ['userIds'],
|
||||
})
|
||||
}
|
||||
}
|
||||
if (data.resource === 'agent_skill_call_count' && data.resourceData !== undefined) {
|
||||
const res = skillResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.id is required for agent_skill_call_count',
|
||||
path: ['resourceData', 'id'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_mcp_call_count' && data.resourceData !== undefined) {
|
||||
const res = mcpResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.id is required for agent_mcp_call_count',
|
||||
path: ['resourceData', 'id'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_bash_call_count' && data.resourceData !== undefined) {
|
||||
const res = bashResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.match is required for agent_bash_call_count',
|
||||
path: ['resourceData', 'match'],
|
||||
})
|
||||
}
|
||||
} else if (data.resource === 'agent_tool_call_count' && data.resourceData !== undefined) {
|
||||
const res = toolResourceDataSchema.safeParse(data.resourceData)
|
||||
if (!res.success) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'resourceData.name is required for agent_tool_call_count',
|
||||
path: ['resourceData', 'name'],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
export type UpdateQuotaRuleRequest = z.input<typeof updateQuotaRuleRequestSchema>
|
||||
Reference in New Issue
Block a user