mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 12:57:16 +00:00
92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { Type } from '@sinclair/typebox'
|
|
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
|
import { executeAgentToolWorkflow } from './utils'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { detectSupportedMimeType } from '@shumai/core/src/utils/mime'
|
|
import { ulid } from 'ulid'
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
|
|
|
function getMimeType(filePath: string): string {
|
|
try {
|
|
const fd = fs.openSync(filePath, 'r')
|
|
try {
|
|
const buffer = Buffer.alloc(4100)
|
|
const bytesRead = fs.readSync(fd, buffer, 0, 4100, 0)
|
|
const detected = detectSupportedMimeType(new Uint8Array(buffer.subarray(0, bytesRead)))
|
|
if (detected) return detected
|
|
} finally {
|
|
fs.closeSync(fd)
|
|
}
|
|
} catch {
|
|
/* Ignore detection errors and fallback to extension mapping */
|
|
}
|
|
|
|
const ext = path.extname(filePath).toLowerCase()
|
|
const mimeTypes: Record<string, string> = {
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.gif': 'image/gif',
|
|
'.webp': 'image/webp',
|
|
'.svg': 'image/svg+xml',
|
|
'.mp4': 'video/mp4',
|
|
'.mov': 'video/quicktime',
|
|
'.pdf': 'application/pdf',
|
|
'.txt': 'text/plain',
|
|
'.md': 'text/markdown',
|
|
'.markdown': 'text/markdown',
|
|
}
|
|
return mimeTypes[ext] || 'application/octet-stream'
|
|
}
|
|
|
|
const createFileSchema = Type.Object({
|
|
parent: Type.String({
|
|
description: 'The parent folder ID under which to create the file. This parameter is required.',
|
|
}),
|
|
path: Type.String({
|
|
description:
|
|
'The absolute or relative local path to the file on disk. This parameter is required.',
|
|
}),
|
|
})
|
|
|
|
export function createCreateFileTool(userId: string): AgentTool<typeof createFileSchema> {
|
|
return {
|
|
name: 'create_file',
|
|
label: 'Create File',
|
|
description: 'Create a new file in a specified parent folder from a local file path.',
|
|
parameters: createFileSchema,
|
|
execute: async (_toolCallId, params) => {
|
|
const absolutePath = path.resolve(process.cwd(), params.path)
|
|
if (!fs.existsSync(absolutePath)) {
|
|
throw new Error(`Local file not found at path: ${params.path}`)
|
|
}
|
|
|
|
const fileSize = fs.statSync(absolutePath).size
|
|
const mimeType = getMimeType(absolutePath)
|
|
|
|
// Generate compliant S3 key matching normal file upload format
|
|
const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
|
|
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
|
|
|
|
const result = await executeAgentToolWorkflow({
|
|
toolName: 'create_file',
|
|
args: {
|
|
parent: params.parent,
|
|
s3Key,
|
|
name: path.basename(absolutePath),
|
|
size: fileSize,
|
|
contentType: mimeType,
|
|
},
|
|
userId,
|
|
assetId: params.parent,
|
|
})
|
|
return {
|
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
details: result,
|
|
}
|
|
},
|
|
}
|
|
}
|