mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 09:07:18 +00:00
Introduce @shumai/operations (pure definitions: Zod schemas, metadata, CLI hints) and @shumai/core/src/operations (implementOperation and the runOperation pipeline: validate, authorize, prepare, execute, validate output, audit). Migrate list_assets, create_folder, rename_asset, move_assets and delete_asset to Operations, and add list_projects. The agent now builds these tools from the registry, and the folder create/rename, file rename and reparent routes call the same Operations. Audit logs gain a nullable via column recording the surface. A lint rule keeps the Prisma client out of Operation implementations.
105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import { prisma } from '@shumai/db'
|
|
import type { Prisma } from '@shumai/db'
|
|
import type { AuditAction } from '@shumai/dtos'
|
|
import type { Via } from '@shumai/operations'
|
|
import { paginateQuery } from '@shumai/core/src/pagination'
|
|
import { logger } from '@shumai/core/src/logger'
|
|
|
|
export interface LogActionParams {
|
|
action: AuditAction
|
|
teamId: string
|
|
userId?: string
|
|
agentId?: string
|
|
projectId?: string
|
|
itemId?: string
|
|
/** Surface the action came through. Set by Operations; omitted elsewhere. */
|
|
via?: Via
|
|
}
|
|
|
|
export interface ListAuditLogsParams {
|
|
teamId: string
|
|
actions?: AuditAction[]
|
|
userIds?: string[]
|
|
agentIds?: string[]
|
|
itemId?: string
|
|
first?: number
|
|
after?: string
|
|
}
|
|
|
|
export class AuditLogService {
|
|
async logAction(params: LogActionParams) {
|
|
try {
|
|
return await prisma.auditLog.create({
|
|
data: {
|
|
action: params.action,
|
|
teamId: params.teamId,
|
|
userId: params.userId,
|
|
agentId: params.agentId,
|
|
projectId: params.projectId,
|
|
itemId: params.itemId,
|
|
via: params.via,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
logger.error({ err, params }, 'Failed to create audit log')
|
|
return null
|
|
}
|
|
}
|
|
|
|
async listAuditLogs(params: ListAuditLogsParams) {
|
|
const where: Prisma.AuditLogWhereInput = {
|
|
teamId: params.teamId,
|
|
}
|
|
|
|
if (params.actions && params.actions.length > 0) {
|
|
where.action = { in: params.actions }
|
|
}
|
|
|
|
if (params.userIds && params.userIds.length > 0) {
|
|
where.userId = { in: params.userIds }
|
|
}
|
|
|
|
if (params.agentIds && params.agentIds.length > 0) {
|
|
where.agentId = { in: params.agentIds }
|
|
}
|
|
|
|
if (params.itemId && params.itemId.trim() !== '') {
|
|
where.itemId = { contains: params.itemId.trim(), mode: 'insensitive' }
|
|
}
|
|
|
|
const result = await paginateQuery(
|
|
(skip, take) =>
|
|
prisma.auditLog.findMany({
|
|
where,
|
|
include: { agent: { include: { user: true } } },
|
|
orderBy: { id: 'desc' },
|
|
skip,
|
|
take,
|
|
}),
|
|
() => prisma.auditLog.count({ where }),
|
|
{ first: params.first, after: params.after },
|
|
)
|
|
|
|
return {
|
|
nodes: result.data.map((item) => ({
|
|
id: item.id,
|
|
action: item.action,
|
|
teamId: item.teamId,
|
|
userId: item.userId,
|
|
agentId: item.agentId,
|
|
agent: item.agent ? { id: item.agent.id, name: item.agent.user.name } : null,
|
|
projectId: item.projectId,
|
|
itemId: item.itemId,
|
|
createdAt: item.createdAt.toISOString(),
|
|
})),
|
|
pageInfo: {
|
|
hasNextPage: !!result.pageInfo.cursor,
|
|
endCursor: result.pageInfo.cursor || null,
|
|
},
|
|
total: result.pageInfo.total ?? 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
export const auditLogService = new AuditLogService()
|