2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { zValidator } from '@hono/zod-validator'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { agentService } from '@shumai/core/src/agent/agent'
|
|
|
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
2026-06-03 19:56:07 +08:00
|
|
|
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
2026-05-19 16:05:09 +08:00
|
|
|
import {
|
|
|
|
|
createAgentRequestSchema,
|
|
|
|
|
updateAgentRequestSchema,
|
2026-07-29 10:10:07 +08:00
|
|
|
paginationParamsSchema,
|
2026-05-19 16:05:09 +08:00
|
|
|
AgentInfo,
|
|
|
|
|
AgentType,
|
2026-08-01 15:28:23 +08:00
|
|
|
AuditAction,
|
2026-06-01 23:08:58 +08:00
|
|
|
} from '@shumai/dtos'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-06-01 23:16:22 +08:00
|
|
|
import type { Prisma } from '@shumai/db'
|
2026-08-01 15:28:23 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
2026-08-01 11:03:50 +08:00
|
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
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,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: teamId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const agents = await agentService.listAgents({ teamId })
|
|
|
|
|
|
2026-06-03 19:56:07 +08:00
|
|
|
const res: AgentInfo[] = await Promise.all(
|
|
|
|
|
agents.map(async (agent) => {
|
|
|
|
|
const config = agent.config as unknown as PrismaJson.AgentConfig
|
|
|
|
|
return {
|
|
|
|
|
id: agent.id,
|
|
|
|
|
name: agent.user.name,
|
|
|
|
|
type: agent.type as AgentType,
|
|
|
|
|
enabled: agent.enabled,
|
|
|
|
|
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
|
|
|
|
|
providerId: agent.providerId || undefined,
|
|
|
|
|
modelId: agent.modelId || undefined,
|
2026-06-22 09:58:35 +08:00
|
|
|
thinkingLevel: config.thinkingLevel || 'off',
|
2026-06-03 19:56:07 +08:00
|
|
|
systemPrompt: config.systemPrompt,
|
|
|
|
|
soul: agent.soul || undefined,
|
|
|
|
|
skills: agent.skills.map((s) => ({
|
|
|
|
|
id: s.id,
|
|
|
|
|
skillId: s.skillId,
|
|
|
|
|
skill: s.skill,
|
|
|
|
|
})),
|
2026-07-16 15:12:26 +08:00
|
|
|
deniedTools: config.deniedTools || [],
|
2026-06-03 19:56:07 +08:00
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
return c.json(res, 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,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: teamId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const agent = await agentService.createAgent({
|
|
|
|
|
teamId,
|
|
|
|
|
...req,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!agent) throw new Error('failed to create agent')
|
|
|
|
|
|
|
|
|
|
const config = agent.config as unknown as PrismaJson.AgentConfig
|
|
|
|
|
const info: AgentInfo = {
|
|
|
|
|
id: agent.id,
|
|
|
|
|
name: agent.user.name,
|
|
|
|
|
type: agent.type as AgentType,
|
|
|
|
|
enabled: agent.enabled,
|
2026-06-03 19:56:07 +08:00
|
|
|
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
|
2026-05-19 16:05:09 +08:00
|
|
|
providerId: agent.providerId || undefined,
|
|
|
|
|
modelId: agent.modelId || undefined,
|
2026-06-22 09:58:35 +08:00
|
|
|
thinkingLevel: config.thinkingLevel || 'off',
|
2026-05-19 16:05:09 +08:00
|
|
|
systemPrompt: config.systemPrompt,
|
|
|
|
|
soul: agent.soul || undefined,
|
|
|
|
|
skills: agent.skills.map((s) => ({
|
|
|
|
|
id: s.id,
|
|
|
|
|
skillId: s.skillId,
|
|
|
|
|
skill: s.skill,
|
|
|
|
|
})),
|
2026-07-16 15:12:26 +08:00
|
|
|
deniedTools: config.deniedTools || [],
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 11:03:50 +08:00
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.agent_create,
|
|
|
|
|
teamId,
|
|
|
|
|
userId: userReq?.id,
|
|
|
|
|
itemId: agent.id,
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
return c.json(info, 200)
|
|
|
|
|
})
|
2026-05-25 23:51:37 +08:00
|
|
|
.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')
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user: userReq,
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Agent,
|
|
|
|
|
id: agentId,
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const agent = await agentService.updateAgent({
|
|
|
|
|
agentId,
|
|
|
|
|
...req,
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-08-01 11:03:50 +08:00
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.agent_update,
|
|
|
|
|
teamId: agent.teamId,
|
|
|
|
|
userId: userReq?.id,
|
|
|
|
|
itemId: agent.id,
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const config = agent.config as unknown as PrismaJson.AgentConfig
|
|
|
|
|
const info: AgentInfo = {
|
|
|
|
|
id: agent.id,
|
|
|
|
|
name: agent.user.name,
|
|
|
|
|
type: agent.type as AgentType,
|
|
|
|
|
enabled: agent.enabled,
|
2026-06-03 19:56:07 +08:00
|
|
|
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
|
2026-05-25 23:51:37 +08:00
|
|
|
providerId: agent.providerId || undefined,
|
|
|
|
|
modelId: agent.modelId || undefined,
|
2026-06-22 09:58:35 +08:00
|
|
|
thinkingLevel: config.thinkingLevel || 'off',
|
2026-05-25 23:51:37 +08:00
|
|
|
systemPrompt: config.systemPrompt,
|
|
|
|
|
soul: agent.soul || undefined,
|
|
|
|
|
skills: agent.skills.map((s) => ({
|
|
|
|
|
id: s.id,
|
|
|
|
|
skillId: s.skillId,
|
|
|
|
|
skill: s.skill,
|
|
|
|
|
})),
|
2026-07-16 15:12:26 +08:00
|
|
|
deniedTools: config.deniedTools || [],
|
2026-05-25 23:51:37 +08:00
|
|
|
}
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
return c.json(info, 200)
|
|
|
|
|
})
|
|
|
|
|
.delete('/agents/:agentId', async (c) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const agentId = c.req.param('agentId')
|
|
|
|
|
const userReq = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user: userReq,
|
|
|
|
|
permission: Permission.Admin,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Agent,
|
|
|
|
|
id: agentId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-08-01 11:03:50 +08:00
|
|
|
const existingAgent = await prisma.agent.findUnique({
|
|
|
|
|
where: { id: agentId },
|
|
|
|
|
select: { teamId: true },
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
await agentService.deleteAgent({
|
|
|
|
|
agentId,
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-01 11:03:50 +08:00
|
|
|
if (existingAgent) {
|
|
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.agent_delete,
|
|
|
|
|
teamId: existingAgent.teamId,
|
|
|
|
|
userId: userReq?.id,
|
|
|
|
|
itemId: agentId,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
return new Response(null, { status: 204 })
|
|
|
|
|
})
|
2026-05-25 23:51:37 +08:00
|
|
|
.get('/agent-sessions/:sessionId/entries', async (c) => {
|
2026-05-21 14:58:31 +08:00
|
|
|
const sessionId = c.req.param('sessionId')
|
|
|
|
|
const userReq = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user: userReq,
|
|
|
|
|
permission: Permission.Admin,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.AgentSession,
|
|
|
|
|
id: sessionId,
|
2026-05-21 14:58:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const entries = await agentService.getSessionEntries({ sessionId })
|
|
|
|
|
const infos = entries.map((entry) => ({
|
|
|
|
|
id: entry.id,
|
|
|
|
|
sessionId: entry.sessionId,
|
|
|
|
|
entry: entry.entry,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
return c.json(infos, 200)
|
|
|
|
|
})
|
2026-07-29 10:10:07 +08:00
|
|
|
.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)
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
export default route
|