mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
* feat(embedding): update compose tiers, docs, and add regenerate-embeddings CLI - Add Lite, Embedding, and Full Docker Compose configurations (and Aliyun mirrors) - Set CONCURRENCY_AGENT to 1 for compose files containing embedding-server - Update README and README_CN with compose options, recommended RAM, and semantic search links - Add LiteRT-LM Embedding Server setup guide in docs/using-shumai/embedding-server.mdx - Add shumai regenerate-embeddings CLI command and EmbeddingService to regenerate embeddings across teams - Add comprehensive test coverage for EmbeddingService * fix(docker): remove basic auth for turntable-renderer in full compose * fix(agent): lock and disable embedding agent when ENABLE_EMBEDDING_AGENT is false Ensure that when ENABLE_EMBEDDING_AGENT is set to 'false', existing embedding agents are automatically disabled on startup, marked as read-only, and blocked from manual API modifications or creations. Additionally guard background task creation and semantic search when the environment variable disables embedding agents. * test(upload): remove flaky status assertion on workflow tasks
247 lines
7.0 KiB
TypeScript
247 lines
7.0 KiB
TypeScript
import { Hono } from 'hono'
|
|
import { HTTPException } from 'hono/http-exception'
|
|
import { zValidator } from '@hono/zod-validator'
|
|
import { agentService } from '@shumai/core/src/agent/agent'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import {
|
|
createAgentRequestSchema,
|
|
updateAgentRequestSchema,
|
|
updateAgentPermissionRequestSchema,
|
|
paginationParamsSchema,
|
|
AgentInfo,
|
|
AuditAction,
|
|
} from '@shumai/dtos'
|
|
|
|
import type { Prisma } from '@shumai/db'
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
|
|
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,
|
|
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 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',
|
|
})
|
|
}
|
|
|
|
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 isEmbeddingManagedByEnv =
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
|
throw new HTTPException(403, {
|
|
message: 'Embedding agent 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 isEmbeddingManagedByEnv =
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
|
throw new HTTPException(403, {
|
|
message: 'Embedding agent 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 isEmbeddingManagedByEnv =
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
|
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
|
if (existingAgent?.type === 'embedding' && isEmbeddingManagedByEnv) {
|
|
throw new HTTPException(403, {
|
|
message: 'Embedding agent 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', 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,
|
|
})
|
|
|
|
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)
|
|
})
|
|
.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
|