2026-08-13 20:06:09 +08:00
|
|
|
import { Type } from 'typebox'
|
2026-05-30 17:09:18 +08:00
|
|
|
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
2026-08-29 15:19:08 +08:00
|
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
2026-09-04 11:27:52 +08:00
|
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
2026-08-29 15:19:08 +08:00
|
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
|
|
|
import { type User } from '@shumai/db'
|
2026-09-04 11:27:52 +08:00
|
|
|
import { AuditAction } from '@shumai/dtos'
|
2026-05-30 17:09:18 +08:00
|
|
|
|
|
|
|
|
const createFolderSchema = Type.Object({
|
|
|
|
|
parent: Type.String({
|
|
|
|
|
description:
|
|
|
|
|
'The parent folder ID under which to create the new folder. This parameter is required.',
|
|
|
|
|
}),
|
|
|
|
|
name: Type.String({
|
|
|
|
|
description: 'The name of the new folder to create. This parameter is required.',
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-04 11:27:52 +08:00
|
|
|
export function createCreateFolderTool(
|
|
|
|
|
userId: string,
|
|
|
|
|
agentContext?: { teamId?: string; agentId?: string },
|
|
|
|
|
): AgentTool<typeof createFolderSchema> {
|
2026-05-30 17:09:18 +08:00
|
|
|
return {
|
|
|
|
|
name: 'create_folder',
|
|
|
|
|
label: 'Create Folder',
|
|
|
|
|
description: 'Create a new folder in a specified parent folder.',
|
|
|
|
|
parameters: createFolderSchema,
|
|
|
|
|
execute: async (_toolCallId, params) => {
|
2026-08-29 15:19:08 +08:00
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user: { id: userId } as User,
|
|
|
|
|
permission: Permission.Edit,
|
|
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: params.parent,
|
2026-07-22 10:47:47 +08:00
|
|
|
})
|
2026-08-29 15:19:08 +08:00
|
|
|
|
|
|
|
|
const newFolder = await assetService.createAsset({
|
|
|
|
|
name: params.name,
|
|
|
|
|
parentId: params.parent,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
creatorId: userId,
|
2026-09-04 11:27:52 +08:00
|
|
|
agentId: agentContext?.agentId,
|
2026-08-29 15:19:08 +08:00
|
|
|
})
|
|
|
|
|
|
2026-09-04 11:27:52 +08:00
|
|
|
if (agentContext?.teamId) {
|
|
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.folder_create,
|
|
|
|
|
teamId: agentContext.teamId,
|
|
|
|
|
userId,
|
|
|
|
|
agentId: agentContext.agentId,
|
|
|
|
|
projectId: newFolder.projectId ?? undefined,
|
|
|
|
|
itemId: newFolder.id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 15:19:08 +08:00
|
|
|
const result = {
|
|
|
|
|
id: newFolder.id,
|
|
|
|
|
name: newFolder.name,
|
|
|
|
|
type: newFolder.type,
|
|
|
|
|
size: Number(newFolder.sizeByte),
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 10:47:47 +08:00
|
|
|
return {
|
|
|
|
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
2026-08-31 20:43:05 +08:00
|
|
|
details: {},
|
2026-05-30 17:09:18 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|