2026-05-30 17:09:18 +08:00
|
|
|
import { Type } from '@sinclair/typebox'
|
|
|
|
|
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
|
|
|
|
import { executeAgentToolWorkflow } from './utils'
|
2026-05-30 19:24:04 +08:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import * as path from 'path'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
2026-08-04 21:16:33 +08:00
|
|
|
import { getFileMimeType, readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
2026-05-31 10:48:06 +08:00
|
|
|
import { ulid } from 'ulid'
|
2026-07-02 15:26:10 +08:00
|
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
2026-05-30 19:24:04 +08:00
|
|
|
|
2026-05-30 17:09:18 +08:00
|
|
|
const createFileSchema = Type.Object({
|
|
|
|
|
parent: Type.String({
|
|
|
|
|
description: 'The parent folder ID under which to create the file. This parameter is required.',
|
|
|
|
|
}),
|
2026-08-04 21:16:33 +08:00
|
|
|
path: Type.Optional(
|
|
|
|
|
Type.String({
|
|
|
|
|
description:
|
|
|
|
|
'The absolute or relative local path to an existing file on disk. Provide either this or "data", but not both.',
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
data: Type.Optional(
|
|
|
|
|
Type.Object({
|
|
|
|
|
name: Type.String({
|
|
|
|
|
minLength: 1,
|
|
|
|
|
description:
|
|
|
|
|
'The desired name for the new file, including its extension (e.g. "notes.md").',
|
|
|
|
|
}),
|
|
|
|
|
content: Type.String({
|
|
|
|
|
description: 'The full content of the file as a plain string.',
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-08-05 10:33:02 +08:00
|
|
|
context: Type.Optional(
|
|
|
|
|
Type.String({
|
|
|
|
|
description:
|
|
|
|
|
'Optional short context about this file and how it was generated (max 50 words, e.g. "Generated using gemini"). ' +
|
|
|
|
|
'Passed to the AI metadata autofill workflow to fill fields that cannot be determined from file content alone. ' +
|
|
|
|
|
'Check autofillable fields with list_autofill_fields first.',
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-05-30 17:09:18 +08:00
|
|
|
})
|
|
|
|
|
|
2026-08-04 21:16:33 +08:00
|
|
|
function assertExactlyOneSource(params: {
|
|
|
|
|
path?: string
|
|
|
|
|
data?: { name: string; content: string }
|
|
|
|
|
}) {
|
|
|
|
|
const hasPath = params.path !== undefined
|
|
|
|
|
const hasData = params.data !== undefined
|
|
|
|
|
if (hasPath === hasData) {
|
|
|
|
|
throw new Error('Provide exactly one of "path" (a local file) or "data" (name and content).')
|
|
|
|
|
}
|
|
|
|
|
return hasPath ? params.path! : params.data!
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 17:09:18 +08:00
|
|
|
export function createCreateFileTool(userId: string): AgentTool<typeof createFileSchema> {
|
|
|
|
|
return {
|
|
|
|
|
name: 'create_file',
|
|
|
|
|
label: 'Create File',
|
2026-08-04 21:16:33 +08:00
|
|
|
description:
|
|
|
|
|
'Create a new file in a specified parent folder, either from a local file path or directly from name and content.',
|
2026-05-30 17:09:18 +08:00
|
|
|
parameters: createFileSchema,
|
|
|
|
|
execute: async (_toolCallId, params) => {
|
2026-08-04 21:16:33 +08:00
|
|
|
const source = assertExactlyOneSource(params)
|
|
|
|
|
|
|
|
|
|
let name: string
|
|
|
|
|
let size: number
|
|
|
|
|
let mimeType: string
|
|
|
|
|
let s3Key: string
|
|
|
|
|
|
|
|
|
|
if (typeof source === 'string') {
|
|
|
|
|
// Branch A: upload an existing local file
|
|
|
|
|
const absolutePath = path.resolve(process.cwd(), source)
|
|
|
|
|
if (!fs.existsSync(absolutePath)) {
|
|
|
|
|
throw new Error(`Local file not found at path: ${source}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = path.basename(absolutePath)
|
|
|
|
|
size = fs.statSync(absolutePath).size
|
|
|
|
|
mimeType = readFileMimeType(absolutePath)
|
|
|
|
|
|
|
|
|
|
// Generate compliant S3 key matching normal file upload format
|
|
|
|
|
s3Key = `files/${ulid()}/${sanitizeFilename(name)}`
|
|
|
|
|
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
|
|
|
|
|
} else {
|
|
|
|
|
// Branch B: create the file directly from provided name and content
|
|
|
|
|
name = sanitizeFilename(source.name)
|
|
|
|
|
const content = Buffer.from(source.content, 'utf-8')
|
|
|
|
|
size = content.byteLength
|
|
|
|
|
mimeType = getFileMimeType(null, source.name, 'text/plain')
|
|
|
|
|
|
|
|
|
|
s3Key = `files/${ulid()}/${name}`
|
|
|
|
|
await s3Service.putObject(process.env.S3_BUCKET || 'shumai', s3Key, content, size, mimeType)
|
2026-07-22 10:47:47 +08:00
|
|
|
}
|
2026-05-30 19:24:04 +08:00
|
|
|
|
2026-07-22 10:47:47 +08:00
|
|
|
const result = await executeAgentToolWorkflow({
|
|
|
|
|
toolName: 'create_file',
|
|
|
|
|
args: {
|
|
|
|
|
parent: params.parent,
|
|
|
|
|
s3Key,
|
2026-08-04 21:16:33 +08:00
|
|
|
name,
|
|
|
|
|
size,
|
2026-07-22 10:47:47 +08:00
|
|
|
contentType: mimeType,
|
2026-08-05 10:33:02 +08:00
|
|
|
...(params.context ? { context: params.context } : {}),
|
2026-07-22 10:47:47 +08:00
|
|
|
},
|
|
|
|
|
userId,
|
|
|
|
|
assetId: params.parent,
|
|
|
|
|
})
|
|
|
|
|
return {
|
|
|
|
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
|
|
|
details: result,
|
2026-05-30 17:09:18 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|