mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 07:29:47 +00:00
* feat(agent): add permission controls for chat agents * refactor(agent): extract shared agent permission helpers Consolidate the duplicated role-hierarchy logic into packages/core/src/agent/permissions.ts: - getRoleLevel(): numeric level for a team role (fail closed at 0) - getAgentRequiredLevel(): required level for an agent permission (defaults to reviewer/1) - getAllowedAgentRoles(): inclusive role set for filtering The typed Record over TeamMemberRole makes the mapping exhaustive, so adding a new role fails compilation until all sites are updated. Used by ChatService.startOrContinueChat, AssetService comment mentions, getAgentChatContextActivity, AgentService.listAgents, and TeamService.getTeamMembers, replacing four inline copies of the hierarchy and the allowed-roles conditionals. Also fix the API permission-denial test to mock HTTPException(403) and assert a 403 response instead of a generic Error/500, matching real authzService behavior. * test(agent): fix flaky comment DAG tests by spacing ULID id generation ULID ids have 1ms timestamp granularity; back-to-back comment creates frequently land in the same millisecond, in which case Prisma ULIDs sort by their random suffix instead of creation order. The DAG spine in initializeAgentSessionActivity chains top-level comments by id, so a shuffled order produces paths that omit preceding comments, making the path-to-root assertions flaky (~80% of full-file runs locally). Add a 2ms sleep between consecutive comment creates in the affected tests, guaranteeing strictly increasing ULID timestamps. A 2ms delay is sufficient because the next create always fires at least 2ms after the previous ULID was generated. Production is unaffected: real comments arrive seconds apart via separate requests.
145 lines
4.2 KiB
TypeScript
145 lines
4.2 KiB
TypeScript
import { z } from 'zod'
|
|
import { AgentType } from '@shumai/db/enums'
|
|
|
|
export { AgentType }
|
|
export const agentTypeSchema = z.nativeEnum(AgentType)
|
|
|
|
export const thinkingLevelSchema = z.enum(['off', 'minimal', 'low', 'medium', 'high', 'xhigh'])
|
|
export type ThinkingLevel = z.infer<typeof thinkingLevelSchema>
|
|
|
|
export const agentPermissionSchema = z.enum(['owner', 'editor', 'reviewer'])
|
|
export type AgentPermission = z.infer<typeof agentPermissionSchema>
|
|
|
|
export const agentSkillSchema = z.object({
|
|
id: z.string().optional(),
|
|
skillId: z.string(),
|
|
skill: z
|
|
.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
})
|
|
.optional(),
|
|
})
|
|
export type AgentSkill = z.infer<typeof agentSkillSchema>
|
|
|
|
export const agentInfoSchema = z.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
type: agentTypeSchema,
|
|
enabled: z.boolean(),
|
|
permission: agentPermissionSchema.optional().default('reviewer'),
|
|
avatar: z.string().optional(),
|
|
providerId: z.string().optional(),
|
|
modelId: z.string().optional(),
|
|
thinkingLevel: thinkingLevelSchema.optional().default('off'),
|
|
systemPrompt: z.string().optional(),
|
|
soul: z.string().optional(),
|
|
skills: z.array(agentSkillSchema).optional(),
|
|
mcpServerIds: z.array(z.string()).optional(),
|
|
deniedTools: z.array(z.string()).optional(),
|
|
})
|
|
export type AgentInfo = z.infer<typeof agentInfoSchema>
|
|
|
|
const baseAgentRequest = z.object({
|
|
name: z.string().min(1),
|
|
type: agentTypeSchema,
|
|
enabled: z.boolean().optional().default(true),
|
|
permission: agentPermissionSchema.optional(),
|
|
avatar: z.string().optional(),
|
|
providerId: z.string().optional(),
|
|
modelId: z.string().optional(),
|
|
thinkingLevel: thinkingLevelSchema.optional().default('off'),
|
|
systemPrompt: z.string().optional(),
|
|
soul: z.string().optional(),
|
|
skills: z.array(z.string()).optional(),
|
|
mcpServerIds: z.array(z.string()).optional(),
|
|
deniedTools: z.array(z.string()).optional(),
|
|
})
|
|
|
|
export const createAgentRequestSchema = baseAgentRequest.superRefine((data, ctx) => {
|
|
if (data.type === 'chat' || data.type === 'autofill') {
|
|
if (!data.providerId) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
message: 'providerId is required for chat/autofill agents',
|
|
path: ['providerId'],
|
|
})
|
|
}
|
|
if (!data.modelId) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
message: 'modelId is required for chat/autofill agents',
|
|
path: ['modelId'],
|
|
})
|
|
}
|
|
}
|
|
})
|
|
export type CreateAgentRequest = z.infer<typeof createAgentRequestSchema>
|
|
|
|
export const updateAgentRequestSchema = baseAgentRequest.superRefine((data, ctx) => {
|
|
if (data.type === 'chat' || data.type === 'autofill') {
|
|
if (!data.providerId) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
message: 'providerId is required for chat/autofill agents',
|
|
path: ['providerId'],
|
|
})
|
|
}
|
|
if (!data.modelId) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
message: 'modelId is required for chat/autofill agents',
|
|
path: ['modelId'],
|
|
})
|
|
}
|
|
}
|
|
})
|
|
export type UpdateAgentRequest = z.infer<typeof updateAgentRequestSchema>
|
|
|
|
export const updateAgentPermissionRequestSchema = z.object({
|
|
permission: agentPermissionSchema,
|
|
})
|
|
export type UpdateAgentPermissionRequest = z.infer<typeof updateAgentPermissionRequestSchema>
|
|
|
|
export interface CreateAgentParams extends CreateAgentRequest {
|
|
teamId: string
|
|
}
|
|
|
|
export interface UpdateAgentParams extends UpdateAgentRequest {
|
|
agentId: string
|
|
}
|
|
|
|
export interface DeleteAgentParams {
|
|
agentId: string
|
|
}
|
|
|
|
export interface ListAgentsParams {
|
|
teamId: string
|
|
userId?: string
|
|
}
|
|
|
|
export const agentSessionEntrySchema = z.object({
|
|
id: z.string(),
|
|
sessionId: z.string(),
|
|
entry: z.unknown(),
|
|
})
|
|
export type AgentSessionEntryInfo = z.infer<typeof agentSessionEntrySchema>
|
|
|
|
export const agentSessionSchema = z.object({
|
|
id: z.string(),
|
|
name: z.string().nullable().optional(),
|
|
type: z.string(),
|
|
createdAt: z.string(),
|
|
creator: z
|
|
.object({
|
|
id: z.string(),
|
|
name: z.string().nullable().optional(),
|
|
email: z.string().nullable().optional(),
|
|
image: z.string().nullable().optional(),
|
|
})
|
|
.nullable()
|
|
.optional(),
|
|
agentId: z.string().optional(),
|
|
})
|
|
export type AgentSessionInfo = z.infer<typeof agentSessionSchema>
|