mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 07:58:46 +00:00
* 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
109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
|
import { type User } from '@shumai/db'
|
|
import { AuditAction } from '@shumai/dtos'
|
|
import { Type, type TSchema } from 'typebox'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { ulid } from 'ulid'
|
|
|
|
interface CreateVersionToolParams {
|
|
parent: string
|
|
path: string
|
|
metadata?: Record<string, unknown> | null
|
|
}
|
|
|
|
export function createCreateVersionTool(
|
|
userId: string,
|
|
metadataSchema?: TSchema,
|
|
agentContext?: { teamId?: string; agentId?: string },
|
|
): AgentTool {
|
|
const createVersionSchema = Type.Object(
|
|
{
|
|
parent: Type.String({
|
|
description:
|
|
'The parent file ID for which to create a new version. This parameter is required.',
|
|
}),
|
|
path: Type.String({
|
|
description:
|
|
'The absolute or relative local path to the file on disk. This parameter is required.',
|
|
}),
|
|
...(metadataSchema ? { metadata: Type.Union([metadataSchema, Type.Null()]) } : {}),
|
|
},
|
|
{ additionalProperties: false },
|
|
)
|
|
|
|
const tool: AgentTool = {
|
|
name: 'create_version',
|
|
label: 'Create Version',
|
|
description:
|
|
'Create a new version of an existing file from a local file path. ' +
|
|
'The optional "metadata" parameter accepts only the CREATION_CONTEXT fields declared in its schema; never invent field keys, and use null for fields you do not know.',
|
|
parameters: createVersionSchema,
|
|
execute: async (_toolCallId, params) => {
|
|
// The harness validates tool args against `createVersionSchema` before calling
|
|
// execute, so `params` is guaranteed to match this shape.
|
|
const p = params as CreateVersionToolParams
|
|
const absolutePath = path.resolve(process.cwd(), p.path)
|
|
if (!fs.existsSync(absolutePath)) {
|
|
throw new Error(`Local file not found at path: ${p.path}`)
|
|
}
|
|
|
|
const fileSize = fs.statSync(absolutePath).size
|
|
const mimeType = readFileMimeType(absolutePath)
|
|
const name = path.basename(absolutePath)
|
|
|
|
// Generate compliant S3 key matching normal file upload format
|
|
const s3Key = `files/${ulid()}/${sanitizeFilename(name)}`
|
|
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
|
|
|
|
await authzService.hasPermission({
|
|
user: { id: userId } as User,
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: p.parent,
|
|
})
|
|
|
|
const asset = await assetService.createVersion({
|
|
parentId: p.parent,
|
|
name,
|
|
key: s3Key,
|
|
sizeByte: fileSize,
|
|
contentType: mimeType,
|
|
creatorId: userId,
|
|
agentId: agentContext?.agentId,
|
|
metadata: p.metadata ?? undefined,
|
|
})
|
|
|
|
if (agentContext?.teamId) {
|
|
await auditLogService.logAction({
|
|
action: AuditAction.file_create,
|
|
teamId: agentContext.teamId,
|
|
userId,
|
|
agentId: agentContext.agentId,
|
|
projectId: asset.projectId ?? undefined,
|
|
itemId: asset.id,
|
|
})
|
|
}
|
|
|
|
const result = {
|
|
id: asset.id,
|
|
name: asset.name,
|
|
type: asset.type,
|
|
size: Number(asset.sizeByte),
|
|
}
|
|
|
|
return {
|
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
details: {},
|
|
}
|
|
},
|
|
}
|
|
return tool
|
|
}
|