2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
2026-10-15 15:49:24 +08:00
|
|
|
import { HTTPException } from 'hono/http-exception'
|
2026-05-19 16:05:09 +08:00
|
|
|
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-05-19 16:05:09 +08:00
|
|
|
import {
|
|
|
|
|
createAgentRequestSchema,
|
|
|
|
|
updateAgentRequestSchema,
|
2026-08-16 11:16:14 +08:00
|
|
|
updateAgentPermissionRequestSchema,
|
2026-07-29 10:10:07 +08:00
|
|
|
paginationParamsSchema,
|
2026-05-19 16:05:09 +08:00
|
|
|
AgentInfo,
|
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 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
|
|
|
})
|
|
|
|
|
|
2026-08-16 11:16:14 +08:00
|
|
|
const agents = await agentService.listAgents({ teamId, userId: userReq?.id })
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-06-03 19:56:07 +08:00
|
|
|
const res: AgentInfo[] = await Promise.all(
|
2026-08-16 15:44:45 +08:00
|
|
|
agents.map((agent) => agentService.toAgentInfo(agent)),
|
2026-06-03 19:56:07 +08:00
|
|
|
)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
return c.json(res, 200)
|
|
|
|
|
})
|
2026-08-16 15:44:45 +08:00
|
|
|
.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)
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
.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
|
|
|
})
|
|
|
|
|
|
2026-10-16 21:20:25 +08:00
|
|
|
const isEmbeddingManagedByEnv =
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
|
|
|
|
|
|
if (req.type === 'embedding' && isEmbeddingManagedByEnv) {
|
|
|
|
|
throw new HTTPException(403, {
|
|
|
|
|
message: 'Embedding agent is managed by environment configuration and cannot be created',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
const agent = await agentService.createAgent({
|
|
|
|
|
teamId,
|
|
|
|
|
...req,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!agent) throw new Error('failed to create agent')
|
|
|
|
|
|
2026-08-16 15:44:45 +08:00
|
|
|
const info = await agentService.toAgentInfo(agent)
|
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-10-16 21:20:25 +08:00
|
|
|
const isEmbeddingManagedByEnv =
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
|
|
2026-10-15 15:49:24 +08:00
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
2026-10-16 21:20:25 +08:00
|
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
2026-10-15 15:49:24 +08:00
|
|
|
throw new HTTPException(403, {
|
|
|
|
|
message: 'Embedding agent is managed by environment configuration and cannot be modified',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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-08-16 15:44:45 +08:00
|
|
|
const info = await agentService.toAgentInfo(agent)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
return c.json(info, 200)
|
|
|
|
|
})
|
2026-08-16 11:16:14 +08:00
|
|
|
.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,
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-16 21:20:25 +08:00
|
|
|
const isEmbeddingManagedByEnv =
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
|
|
2026-10-15 15:49:24 +08:00
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
2026-10-16 21:20:25 +08:00
|
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
2026-10-15 15:49:24 +08:00
|
|
|
throw new HTTPException(403, {
|
|
|
|
|
message: 'Embedding agent is managed by environment configuration and cannot be modified',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 11:16:14 +08:00
|
|
|
const agent = await agentService.updateAgentPermission(agentId, req.permission)
|
|
|
|
|
|
|
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.agent_update,
|
|
|
|
|
teamId: agent.teamId,
|
|
|
|
|
userId: userReq?.id,
|
|
|
|
|
itemId: agent.id,
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-16 15:44:45 +08:00
|
|
|
const info = await agentService.toAgentInfo(agent)
|
2026-08-16 11:16:14 +08:00
|
|
|
|
|
|
|
|
return c.json(info, 200)
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-05-25 23:51:37 +08:00
|
|
|
.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-10-16 21:20:25 +08:00
|
|
|
const isEmbeddingManagedByEnv =
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
|
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
|
|
2026-08-01 16:25:01 +08:00
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
2026-10-16 21:20:25 +08:00
|
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
2026-10-15 15:49:24 +08:00
|
|
|
throw new HTTPException(403, {
|
|
|
|
|
message: 'Embedding agent is managed by environment configuration and cannot be deleted',
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-08-01 11:03:50 +08:00
|
|
|
|
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
|