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'
import { authzService , Permission , ResourceType } from '@shumai/core/src/authz/authz'
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 { readFileMimeType } from '@shumai/core/src/utils/file-mime'
2026-07-02 15:26:10 +08:00
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
2026-08-29 15:19:08 +08:00
import { type User } from '@shumai/db'
import { Type , type TSchema } from 'typebox'
import * as fs from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
2026-05-30 19:24:04 +08:00
2026-08-13 20:06:09 +08:00
interface CreateVersionToolParams {
parent : string
path : string
metadata? : Record < string , unknown > | null
}
2026-05-30 17:09:18 +08:00
2026-08-13 20:06:09 +08:00
export function createCreateVersionTool ( userId : string , metadataSchema? : TSchema ) : 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 = {
2026-05-30 17:09:18 +08:00
name : 'create_version' ,
label : 'Create Version' ,
2026-08-13 20:06:09 +08:00
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.' ,
2026-05-30 17:09:18 +08:00
parameters : createVersionSchema ,
execute : async ( _toolCallId , params ) => {
2026-08-13 20:06:09 +08:00
// 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 )
2026-07-22 10:47:47 +08:00
if ( ! fs . existsSync ( absolutePath )) {
2026-08-13 20:06:09 +08:00
throw new Error ( `Local file not found at path: ${ p . path } ` )
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 fileSize = fs . statSync ( absolutePath ). size
2026-08-04 21:16:33 +08:00
const mimeType = readFileMimeType ( absolutePath )
2026-08-29 15:19:08 +08:00
const name = path . basename ( absolutePath )
2026-05-30 19:24:04 +08:00
2026-07-22 10:47:47 +08:00
// Generate compliant S3 key matching normal file upload format
2026-08-29 15:19:08 +08:00
const s3Key = `files/ ${ ulid () } / ${ sanitizeFilename ( name ) } `
2026-07-22 10:47:47 +08:00
await s3Service . uploadFileToKey ( absolutePath , s3Key , mimeType )
2026-05-30 19:24:04 +08:00
2026-08-29 15:19:08 +08:00
await authzService . hasPermission ({
user : { id : userId } as User ,
permission : Permission.Edit ,
type : ResourceType . Asset ,
id : p.parent ,
2026-07-22 10:47:47 +08:00
})
2026-08-29 15:19:08 +08:00
const asset = await assetService . createVersion ({
parentId : p.parent ,
name ,
key : s3Key ,
sizeByte : fileSize ,
contentType : mimeType ,
creatorId : userId ,
metadata : p.metadata ?? undefined ,
})
const result = {
id : asset.id ,
name : asset.name ,
type : asset . type ,
size : Number ( asset . sizeByte ),
}
2026-07-22 10:47:47 +08:00
return {
content : [{ type : 'text' , text : JSON.stringify ( result , null , 2 ) }],
details : result ,
2026-05-30 17:09:18 +08:00
}
},
}
2026-08-13 20:06:09 +08:00
return tool
2026-05-30 17:09:18 +08:00
}