import { zValidator } from '@hono/zod-validator' import { agentService } from '@shumai/core/src/agent/agent' import { getSystemAgentSetting } from '@shumai/core/src/agent/system-agents' import { agentSessionService } from '@shumai/core/src/agent/session-service' import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz' import { AgentInfo, AuditAction, createAgentRequestSchema, paginationParamsSchema, updateAgentPermissionRequestSchema, updateAgentRequestSchema, } from '@shumai/dtos' import { Hono } from 'hono' import { HTTPException } from 'hono/http-exception' import { z } from 'zod' import { agentEntriesResponse } from './agent-entries' import { auditLogService } from '@shumai/core/src/auditLog/auditLog' import type { Prisma } from '@shumai/db' type User = Prisma.UserGetPayload> const sessionEntriesQuerySchema = z.object({ includeUsage: z.enum(['true', 'false']).optional() }) const route = new Hono<{ Variables: { user: User } }>() .get('/teams/:teamId/agents', async (c) => { const teamId = c.req.param('teamId') const userReq = c.get('user') await authzService.hasPermission({ user: userReq, permission: Permission.Read, type: ResourceType.Team, id: teamId, }) const agents = await agentService.listAgents({ teamId, userId: userReq?.id }) const res: AgentInfo[] = await Promise.all( agents.map((agent) => agentService.toAgentInfo(agent)), ) return c.json(res, 200) }) .get('/projects/:projectId/chat-agents', async (c) => { const projectId = c.req.param('projectId') const userReq = c.get('user') await authzService.hasPermission({ user: userReq, permission: Permission.Read, type: ResourceType.Project, id: projectId, }) const agents = await agentService.listProjectChatAgents(projectId, userReq?.id) return c.json(agents, 200) }) .post('/teams/:teamId/agents', zValidator('json', createAgentRequestSchema), async (c) => { const teamId = c.req.param('teamId') const userReq = c.get('user') const req = c.req.valid('json') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.Team, id: teamId, }) const managedSetting = getSystemAgentSetting(req.type) if (managedSetting) { throw new HTTPException(403, { message: `${managedSetting.name} is managed by environment configuration and cannot be created`, }) } const agent = await agentService.createAgent({ teamId, ...req, }) if (!agent) throw new Error('failed to create agent') const info = await agentService.toAgentInfo(agent) await auditLogService.logAction({ action: AuditAction.agent_create, teamId, userId: userReq?.id, itemId: agent.id, }) return c.json(info, 200) }) .put('/agents/:agentId', zValidator('json', updateAgentRequestSchema), async (c) => { const agentId = c.req.param('agentId') const userReq = c.get('user') const req = c.req.valid('json') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.Agent, id: agentId, }) const existingAgent = await agentService.getAgent({ agentId }) const managedSetting = (existingAgent && getSystemAgentSetting(existingAgent.type)) ?? getSystemAgentSetting(req.type) if (managedSetting) { throw new HTTPException(403, { message: `${managedSetting.name} is managed by environment configuration and cannot be modified`, }) } const agent = await agentService.updateAgent({ agentId, ...req, }) await auditLogService.logAction({ action: AuditAction.agent_update, teamId: agent.teamId, userId: userReq?.id, itemId: agent.id, }) const info = await agentService.toAgentInfo(agent) return c.json(info, 200) }) .patch( '/agents/:agentId/permission', zValidator('json', updateAgentPermissionRequestSchema), async (c) => { const agentId = c.req.param('agentId') const userReq = c.get('user') const req = c.req.valid('json') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.Agent, id: agentId, }) const existingAgent = await agentService.getAgent({ agentId }) const managedSetting = existingAgent && getSystemAgentSetting(existingAgent.type) if (managedSetting) { throw new HTTPException(403, { message: `${managedSetting.name} is managed by environment configuration and cannot be modified`, }) } const agent = await agentService.updateAgentPermission(agentId, req.permission) await auditLogService.logAction({ action: AuditAction.agent_update, teamId: agent.teamId, userId: userReq?.id, itemId: agent.id, }) const info = await agentService.toAgentInfo(agent) return c.json(info, 200) }, ) .delete('/agents/:agentId', async (c) => { const agentId = c.req.param('agentId') const userReq = c.get('user') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.Agent, id: agentId, }) const existingAgent = await agentService.getAgent({ agentId }) const managedSetting = existingAgent && getSystemAgentSetting(existingAgent.type) if (managedSetting) { throw new HTTPException(403, { message: `${managedSetting.name} is managed by environment configuration and cannot be deleted`, }) } await agentService.deleteAgent({ agentId, }) if (existingAgent) { await auditLogService.logAction({ action: AuditAction.agent_delete, teamId: existingAgent.teamId, userId: userReq?.id, itemId: agentId, }) } return new Response(null, { status: 204 }) }) .get( '/agent-sessions/:sessionId/entries', zValidator('query', sessionEntriesQuerySchema), async (c) => { const sessionId = c.req.param('sessionId') const userReq = c.get('user') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.AgentSession, id: sessionId, }) if (c.req.valid('query').includeUsage === 'true') { return agentEntriesResponse(await agentSessionService.getLogs(sessionId)) } return agentEntriesResponse(await agentService.getSessionEntries({ sessionId })) }, ) .get('/teams/:teamId/agent-sessions', zValidator('query', paginationParamsSchema), async (c) => { const teamId = c.req.param('teamId') const userReq = c.get('user') const query = c.req.valid('query') await authzService.hasPermission({ user: userReq, permission: Permission.Admin, type: ResourceType.Team, id: teamId, }) const res = await agentService.listSessions(teamId, query) return c.json(res, 200) }) export default route