fix(agent): pre-upload files in agent tools before database workflow execution to ensure worker isolation

This commit is contained in:
yiling
2026-05-30 19:24:04 +08:00
parent 2e685ceccb
commit ff5196a517
4 changed files with 133 additions and 83 deletions
+52 -1
View File
@@ -1,6 +1,40 @@
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 '@/services/s3/s3'
import { detectSupportedMimeType } from '@/utils/mime'
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',
}
return mimeTypes[ext] || 'application/octet-stream'
}
const createFileSchema = Type.Object({
parent: Type.String({
@@ -20,9 +54,26 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
parameters: createFileSchema,
execute: async (_toolCallId, params) => {
try {
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)
// Pre-upload the file to S3
const s3Key = await s3Service.uploadFile(absolutePath, mimeType)
const result = await executeAgentToolWorkflow({
toolName: 'create_file',
args: params,
args: {
parent: params.parent,
s3Key,
name: path.basename(absolutePath),
size: fileSize,
contentType: mimeType,
},
userId,
assetId: params.parent,
})
+52 -1
View File
@@ -1,6 +1,40 @@
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 '@/services/s3/s3'
import { detectSupportedMimeType } from '@/utils/mime'
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',
}
return mimeTypes[ext] || 'application/octet-stream'
}
const createVersionSchema = Type.Object({
parent: Type.String({
@@ -21,9 +55,26 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
parameters: createVersionSchema,
execute: async (_toolCallId, params) => {
try {
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)
// Pre-upload the file to S3
const s3Key = await s3Service.uploadFile(absolutePath, mimeType)
const result = await executeAgentToolWorkflow({
toolName: 'create_version',
args: params,
args: {
parent: params.parent,
s3Key,
name: path.basename(absolutePath),
size: fileSize,
contentType: mimeType,
},
userId,
assetId: params.parent,
})
+11 -2
View File
@@ -14,6 +14,7 @@ import {
executeAgentToolActivity,
} from './db'
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
import { s3Service } from '@/services/s3/s3'
describe('Database Activities', () => {
setupTestDbHooks()
@@ -408,12 +409,16 @@ describe('Database Activities', () => {
fs.writeFileSync(tempFilePath, 'hello Shumai!')
try {
const s3Key = await s3Service.uploadFile(tempFilePath, 'text/plain')
const fileResult = await executeAgentToolActivity({
taskId: 'task2',
toolName: 'create_file',
args: {
parent: parentFolder.id,
path: tempFilePath,
s3Key,
name: 'temp_test_file.txt',
size: fs.statSync(tempFilePath).size,
contentType: 'text/plain',
},
userId: user.id,
})
@@ -446,12 +451,16 @@ describe('Database Activities', () => {
fs.writeFileSync(versionTempPath, 'hello Shumai V2!')
try {
const s3KeyV2 = await s3Service.uploadFile(versionTempPath, 'text/plain')
const versionResult = await executeAgentToolActivity({
taskId: 'task4',
toolName: 'create_version',
args: {
parent: fileResult.id,
path: versionTempPath,
s3Key: s3KeyV2,
name: 'temp_version_file.txt',
size: fs.statSync(versionTempPath).size,
contentType: 'text/plain',
},
userId: user.id,
})
+18 -79
View File
@@ -6,15 +6,11 @@ import { metadataService } from '@/services/metadata/metadata'
import { UpdateAssetMetadataRequest } from '@/dtos/metadata'
import { WorkflowTaskStatus, AssetStatus, Prisma, AssetType } from '@/generated/prisma/client'
import type { AgentExecutionContext } from './agent'
import { s3Service } from '@/services/s3/s3'
import { assetService } from '@/services/asset/asset'
import { VersionStackService } from '@/services/versionStack/versionStack'
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
import { generateKeyBetween } from 'jittered-fractional-indexing'
import { detectSupportedMimeType } from '@/utils/mime'
import { paginateQuery, encodeCursor } from '@/services/pagination'
import * as fs from 'fs'
import * as path from 'path'
export interface InitializeAgentSessionParams {
teamId: string
@@ -628,43 +624,6 @@ export async function updateWorkflowTaskActivity(params: UpdateWorkflowTaskParam
// Agent System Tools & Context Activities
// ==========================================
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 S3 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',
'.json': 'application/json',
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.ts': 'application/typescript',
'.zip': 'application/zip',
}
return mimeTypes[ext] || 'application/octet-stream'
}
function generateSortIndex(previous?: string | null): string {
if (!previous) return generateKeyBetween(null, null)
return generateKeyBetween(previous, null)
@@ -836,10 +795,13 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
case 'create_file': {
const parent = args.parent
const filePath = args.path
if (!parent || !filePath) {
const s3Key = args.s3Key as string
const name = args.name as string
const fileSize = args.size as number
const mimeType = args.contentType as string
if (!parent || !s3Key || !name || fileSize === undefined || !mimeType) {
throw ApplicationFailure.create({
message: 'parent and path parameters are required',
message: 'parent, s3Key, name, size, and contentType parameters are required',
nonRetryable: true,
})
}
@@ -852,25 +814,12 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
id: parent,
})
if (!fs.existsSync(filePath)) {
throw ApplicationFailure.create({
message: `Local file not found at path: ${filePath}`,
nonRetryable: true,
})
}
const fileSize = fs.statSync(filePath).size
const mimeType = getMimeType(filePath)
// Upload file to S3
const key = await s3Service.uploadFile(filePath, mimeType)
// Create asset via assetService
const newFile = await assetService.createAsset({
name: path.basename(filePath),
name,
type: 'file',
parentId: parent,
key,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
@@ -896,10 +845,13 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
case 'create_version': {
const parent = args.parent // parent file id
const filePath = args.path
if (!parent || !filePath) {
const s3Key = args.s3Key as string
const name = args.name as string
const fileSize = args.size as number
const mimeType = args.contentType as string
if (!parent || !s3Key || !name || fileSize === undefined || !mimeType) {
throw ApplicationFailure.create({
message: 'parent and path parameters are required',
message: 'parent, s3Key, name, size, and contentType parameters are required',
nonRetryable: true,
})
}
@@ -923,29 +875,16 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
id: parent,
})
if (!fs.existsSync(filePath)) {
throw ApplicationFailure.create({
message: `Local file not found at path: ${filePath}`,
nonRetryable: true,
})
}
const fileSize = fs.statSync(filePath).size
const mimeType = getMimeType(filePath)
// Upload file to S3
const key = await s3Service.uploadFile(filePath, mimeType)
// If already in a version stack
if (parentFile.parentId && parentFile.parent?.type === AssetType.version_stack) {
const stackId = parentFile.parentId
// Create the new file asset
const newFile = await assetService.createAsset({
name: path.basename(filePath),
name,
type: 'file',
parentId: stackId,
key,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,
@@ -998,10 +937,10 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
// Create the new file asset under parent folder first (so that createVersionStack can run on them,
// which enforces both files must have same parent initially)
const newFile = await assetService.createAsset({
name: path.basename(filePath),
name,
type: 'file',
parentId: folderParentId,
key,
key: s3Key,
sizeByte: fileSize,
contentType: mimeType,
creatorId: userId,