mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +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:
@@ -3,6 +3,7 @@ import { Type } from 'typebox'
|
||||
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import { quotaService } from '@shumai/core/src/quota/quota-service'
|
||||
|
||||
const readSkillSchema = Type.Object({
|
||||
skillId: Type.String({ description: 'The ID of the skill to read' }),
|
||||
@@ -41,11 +42,13 @@ export const createReadSkillTool = (
|
||||
}
|
||||
|
||||
const requiredLevel = ROLE_HIERARCHY[skill.permission] || 1
|
||||
const effectiveRole = userId
|
||||
? await resolveEffectiveRole(skill.teamId, projectId, userId)
|
||||
: null
|
||||
|
||||
if (userId) {
|
||||
// Resolve the user's effective role for the project context (project
|
||||
// override wins; project-scoped members without project access are denied).
|
||||
const effectiveRole = await resolveEffectiveRole(skill.teamId, projectId, userId)
|
||||
const userLevel = effectiveRole ? ROLE_HIERARCHY[effectiveRole] || 0 : 0
|
||||
if (userLevel < requiredLevel) {
|
||||
throw new Error(
|
||||
@@ -58,6 +61,18 @@ export const createReadSkillTool = (
|
||||
)
|
||||
}
|
||||
|
||||
// Quota pre-check
|
||||
await quotaService.checkQuota(
|
||||
{
|
||||
teamId: skill.teamId,
|
||||
userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_skill_call_count',
|
||||
resourceData: { id: params.skillId, skillId: params.skillId },
|
||||
},
|
||||
1,
|
||||
)
|
||||
|
||||
// Capture environment variables using agentService
|
||||
const envs = await agentService.getSkillEnvs(params.skillId)
|
||||
onEnvsAdded(envs)
|
||||
@@ -68,6 +83,18 @@ export const createReadSkillTool = (
|
||||
// Notify that a skill was successfully loaded (e.g. to enable restricted tools)
|
||||
await onSkillLoaded?.()
|
||||
|
||||
// Consume quota
|
||||
await quotaService.consumeQuota(
|
||||
{
|
||||
teamId: skill.teamId,
|
||||
userId,
|
||||
role: effectiveRole,
|
||||
resource: 'agent_skill_call_count',
|
||||
resourceData: { id: params.skillId, skillId: params.skillId },
|
||||
},
|
||||
1,
|
||||
)
|
||||
|
||||
return {
|
||||
content: [{ type: 'text', text: skillMdContent }],
|
||||
details: { skillId: params.skillId },
|
||||
|
||||
Reference in New Issue
Block a user