feat(asset): record and display agent provenance on assets and audit logs (#380)

* feat(asset): record and display agent provenance on assets and audit logs

* style(webui): apply 30% translucent frosted glass background to bot badges

* style(webui): match bot icon badge style to thumbnail media overlay (bg-black/60 text-white)

* style(webui): use "AI" text badge with pdf overlay style instead of bot icon
This commit is contained in:
Yiling-J
2026-09-04 11:27:52 +08:00
committed by GitHub
parent 52330869a2
commit dd0ea05a88
39 changed files with 1699 additions and 108 deletions
+136
View File
@@ -3821,6 +3821,65 @@ describe('AssetService — natural sort by name', () => {
expect(triggerSpy).toHaveBeenCalledWith(expect.anything(), fileInfo.id, team.id, project.id)
})
it('creates a file with agentId and returns agent info in toAssetInfos', async () => {
const user = await prisma.user.create({
data: { name: 'HumanUser', email: `human-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'AgentFileTeam' } })
const project = await prisma.project.create({
data: { name: 'AgentFileProject', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const agentUser = await prisma.user.create({
data: {
name: 'AssetBot',
email: `bot-${Date.now()}@example.com`,
type: 'agent',
},
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'test', model: 'test' },
},
})
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockResolvedValue(undefined)
const fileInfo = await assetService.createFile({
parentId: rootFolder.id,
name: 'generated.png',
key: 'files/test/generated.png',
sizeByte: 2048,
contentType: 'image/png',
creatorId: user.id,
agentId: agent.id,
})
expect(fileInfo.id).toBeDefined()
expect(fileInfo.agentId).toBe(agent.id)
expect(fileInfo.agent).toEqual({
id: agent.id,
name: 'AssetBot',
})
const fetched = await assetService.getAsset({ assetId: fileInfo.id })
expect(fetched.agentId).toBe(agent.id)
expect(fetched.agent).toEqual({
id: agent.id,
name: 'AssetBot',
})
})
it('rejects if metadata contains invalid keys', async () => {
const user = await prisma.user.create({
data: { name: 'FileUser2', email: `fileuser2-${Date.now()}@example.com` },
@@ -4085,6 +4144,83 @@ describe('AssetService — natural sort by name', () => {
expect(rootFolderAfter?.fileCount).toBe(1)
expect(rootFolderAfter?.sizeByte).toBe(2000n)
})
it('creates a version with agentId and preserves agent provenance in version stack', async () => {
const user = await prisma.user.create({
data: { name: 'VersionUser2', email: `veruser2-${Date.now()}@example.com` },
})
const team = await prisma.team.create({ data: { name: 'VersionTeam2' } })
const project = await prisma.project.create({
data: { name: 'VersionProject2', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'RootFolder',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const agentUser = await prisma.user.create({
data: {
name: 'VersionBot',
email: `verbot-${Date.now()}@example.com`,
type: 'agent',
},
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'test', model: 'test' },
},
})
const v1 = await prisma.asset.create({
data: {
name: 'document.txt',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.uploaded,
sizeByte: 100n,
creatorId: user.id,
},
})
vi.spyOn(uploadService, 'triggerPostUploadWorkflows').mockResolvedValue(undefined)
const v2Info = await assetService.createVersion({
parentId: v1.id,
name: 'document.txt',
key: 'files/test/v2.txt',
sizeByte: 200,
contentType: 'text/plain',
creatorId: user.id,
agentId: agent.id,
})
expect(v2Info.id).toBeDefined()
expect(v2Info.agentId).toBe(agent.id)
expect(v2Info.agent).toEqual({
id: agent.id,
name: 'VersionBot',
})
const stack = await prisma.asset.findFirst({
where: { parentId: rootFolder.id, type: AssetType.version_stack },
})
expect(stack).toBeDefined()
const stackVersions = await assetService.getStackVersions(stack!.id)
expect(stackVersions).toHaveLength(2)
const v2StackVersion = stackVersions.find((v) => v.id === v2Info.id)
expect(v2StackVersion?.agent).toEqual({
id: agent.id,
name: 'VersionBot',
})
})
})
describe('listChildren type filtering', () => {
+73 -80
View File
@@ -34,20 +34,38 @@ import { uploadService } from '@shumai/core/src/upload/upload'
export const assetInclude = {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
target: {
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
children: {
include: { creator: true, metadataValues: true, storageKey: true },
where: { isDeleted: false },
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
},
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
},
children: {
include: { creator: true, metadataValues: true, storageKey: true },
where: { isDeleted: false },
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
},
take: 3,
orderBy: { sortIndex: 'asc' },
},
} as const
@@ -101,30 +119,7 @@ export class AssetService {
const assets = await this.prismaClient.asset.findMany({
where: { id: { in: ids } },
include: {
creator: true,
metadataValues: true,
storageKey: true,
target: {
include: {
creator: true,
metadataValues: true,
storageKey: true,
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
},
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
include: assetInclude,
})
const assetMap = new Map(assets.map((a) => [a.id, a]))
@@ -711,30 +706,7 @@ export class AssetService {
async (skip, take) => {
return this.prismaClient.asset.findMany({
where,
include: {
creator: true,
metadataValues: true,
storageKey: true,
target: {
include: {
creator: true,
metadataValues: true,
storageKey: true,
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
},
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
include: assetInclude,
orderBy,
skip,
take,
@@ -798,22 +770,30 @@ export class AssetService {
if (req.sizeByte) data.sizeByte = req.sizeByte
if (req.contentType) data.mediaType = req.contentType
if (req.creatorId) data.creator = { connect: { id: req.creatorId } }
if (req.agentId) data.agent = { connect: { id: req.agentId } }
const executeInTx = async (tx: Prisma.TransactionClient) => {
const createdAsset = await tx.asset.create({
data,
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
target: {
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
},
take: 3,
orderBy: { sortIndex: 'asc' },
},
@@ -821,7 +801,12 @@ export class AssetService {
},
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
},
take: 3,
orderBy: { sortIndex: 'asc' },
},
@@ -853,9 +838,10 @@ export class AssetService {
sizeByte: number
contentType: string
creatorId: string
agentId?: string
metadata?: Record<string, unknown>
}): Promise<AssetInfo> {
const { parentId, name, key, sizeByte, contentType, creatorId, metadata } = params
const { parentId, name, key, sizeByte, contentType, creatorId, agentId, metadata } = params
const parentAsset = await this.prismaClient.asset.findUnique({
where: { id: parentId },
@@ -884,6 +870,7 @@ export class AssetService {
sizeByte,
contentType,
creatorId,
agentId,
},
tx,
)
@@ -916,9 +903,10 @@ export class AssetService {
sizeByte: number
contentType: string
creatorId: string
agentId?: string
metadata?: Record<string, unknown>
}): Promise<AssetInfo> {
const { parentId, name, key, sizeByte, contentType, creatorId, metadata } = params
const { parentId, name, key, sizeByte, contentType, creatorId, agentId, metadata } = params
const targetAsset = await this.prismaClient.asset.findUnique({
where: { id: parentId },
@@ -961,6 +949,7 @@ export class AssetService {
sizeByte,
contentType,
creatorId,
agentId,
},
tx,
)
@@ -1006,30 +995,7 @@ export class AssetService {
async getAsset(req: GetAssetRequest): Promise<AssetInfo> {
const a = await this.prismaClient.asset.findUnique({
where: { id: req.assetId },
include: {
creator: true,
metadataValues: true,
storageKey: true,
target: {
include: {
creator: true,
metadataValues: true,
storageKey: true,
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
},
children: {
where: { isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
take: 3,
orderBy: { sortIndex: 'asc' },
},
},
include: assetInclude,
})
if (!a) throw new Error('Asset not found')
@@ -1914,7 +1880,12 @@ export class AssetService {
if (stackIds.size > 0) {
const allVersions = await this.prismaClient.asset.findMany({
where: { parentId: { in: Array.from(stackIds) }, isDeleted: false },
include: { creator: true, metadataValues: true, storageKey: true },
include: {
creator: true,
agent: { include: { user: true } },
metadataValues: true,
storageKey: true,
},
})
for (const v of allVersions) {
const list = versionsMap.get(v.parentId!) || []
@@ -1974,6 +1945,12 @@ export class AssetService {
image: await getAvatarUrl(v.creator.image),
}
: null,
agent: v.agent
? {
id: v.agent.id,
name: v.agent.user.name,
}
: null,
}
}),
),
@@ -2026,6 +2003,13 @@ export class AssetService {
}
: null
const agent = latestVersion.agent
? {
id: latestVersion.agent.id,
name: latestVersion.agent.user.name,
}
: null
const fieldValues: FieldValueInfo[] = []
if ('metadataValues' in latestVersion && latestVersion.metadataValues) {
for (const mv of latestVersion.metadataValues) {
@@ -2101,6 +2085,8 @@ export class AssetService {
deletedAt: a.deletedAt ? a.deletedAt.toISOString() : null,
projectId: a.projectId,
creator,
agentId: latestVersion.agentId ?? null,
agent,
fieldValues,
sortIndex: a.sortIndex,
hasAgentsMd: hasAgentMdSet.has(a.id),
@@ -2119,11 +2105,12 @@ export class AssetService {
previewUrl: string | null
createdAt: string
creator: { id: string; name: string | null; image?: string | null } | null
agent?: { id: string; name: string } | null
}>
> {
const versions = await this.prismaClient.asset.findMany({
where: { parentId: stackId, isDeleted: false },
include: { creator: true },
include: { creator: true, agent: { include: { user: true } } },
})
versions.sort((x, y) => {
@@ -2147,6 +2134,12 @@ export class AssetService {
image: await getAvatarUrl(v.creator.image),
}
: null,
agent: v.agent
? {
id: v.agent.id,
name: v.agent.user.name,
}
: null,
}
}),
)
@@ -116,4 +116,57 @@ describe('AuditLogService', () => {
expect(result.nodes.map((n) => n.action)).toContain(AuditAction.kanban_task_create)
expect(result.nodes.map((n) => n.action)).toContain(AuditAction.kanban_goal_create)
})
it('records and filters agent provenance in audit logs', async () => {
const user = await prisma.user.create({
data: { name: 'Audit Human User', email: 'audit_human@example.com', password: 'password' },
})
const team = await prisma.team.create({
data: { name: 'Agent Audit Team' },
})
const agentUser = await prisma.user.create({
data: {
name: 'Creation Bot',
email: 'creation_bot@example.com',
type: 'agent',
},
})
const agent = await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'test', model: 'test' },
},
})
const log = await auditLogService.logAction({
action: AuditAction.file_create,
teamId: team.id,
userId: user.id,
agentId: agent.id,
itemId: 'file-xyz',
})
expect(log).toBeDefined()
expect(log?.agentId).toBe(agent.id)
const resultWithAgent = await auditLogService.listAuditLogs({
teamId: team.id,
agentIds: [agent.id],
})
expect(resultWithAgent.total).toBe(1)
expect(resultWithAgent.nodes[0].agentId).toBe(agent.id)
expect(resultWithAgent.nodes[0].agent).toEqual({
id: agent.id,
name: 'Creation Bot',
})
const resultOtherAgent = await auditLogService.listAuditLogs({
teamId: team.id,
agentIds: ['non-existent-agent'],
})
expect(resultOtherAgent.total).toBe(0)
})
})
+10
View File
@@ -8,6 +8,7 @@ export interface LogActionParams {
action: AuditAction
teamId: string
userId?: string
agentId?: string
projectId?: string
itemId?: string
}
@@ -16,6 +17,7 @@ export interface ListAuditLogsParams {
teamId: string
actions?: AuditAction[]
userIds?: string[]
agentIds?: string[]
itemId?: string
first?: number
after?: string
@@ -29,6 +31,7 @@ export class AuditLogService {
action: params.action,
teamId: params.teamId,
userId: params.userId,
agentId: params.agentId,
projectId: params.projectId,
itemId: params.itemId,
},
@@ -52,6 +55,10 @@ export class AuditLogService {
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' }
}
@@ -60,6 +67,7 @@ export class AuditLogService {
(skip, take) =>
prisma.auditLog.findMany({
where,
include: { agent: { include: { user: true } } },
orderBy: { id: 'desc' },
skip,
take,
@@ -74,6 +82,8 @@ export class AuditLogService {
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(),