mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 04:29:41 +00:00
* refactor(agent)!: migrate sessions to Pi Durable Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server. BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs. * fix(agent): constrain session log dialog scrolling Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow. * fix(agent): hide entry IDs from session log card headers Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them. * test(e2e): handle browser wheel limits in session log checks Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions. * fix(agent): select model providers without inheriting unrelated auth * fix(webui): prevent resize feedback when toggling agent panel * test(agent): reproduce concurrent comment session races * fix(agent): serialize comment sessions and deduplicate history * docs(changelog): consolidate Pi Durable changes into one entry
242 lines
6.9 KiB
TypeScript
242 lines
6.9 KiB
TypeScript
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 {
|
|
AgentInfo,
|
|
AuditAction,
|
|
createAgentRequestSchema,
|
|
paginationParamsSchema,
|
|
updateAgentPermissionRequestSchema,
|
|
updateAgentRequestSchema,
|
|
} from '@shumai/dtos'
|
|
import { Hono } from 'hono'
|
|
import { HTTPException } from 'hono/http-exception'
|
|
import { agentEntriesResponse } from './agent-entries'
|
|
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
import type { Prisma } from '@shumai/db'
|
|
|
|
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 })
|
|
return agentEntriesResponse(entries)
|
|
})
|
|
.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
|