mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 22:59:09 +00:00
* feat(metadata): replace aiAutofill boolean with autofillSource enum * fix(agent): validate creation-context metadata and make writes transactional Address code review feedback on the autofillSource migration: - Update the agent system prompt to reference the 'metadata' parameter of create_file/create_version instead of the removed 'context' parameter, which was being silently discarded by the activity - Write metadata inside the caller's transaction via the new metadataService.updateAssetMetadataInTx, keeping metadata rows atomic with asset creation instead of opening a separate non-atomic transaction - Reject metadata keys that are not defined CREATION_CONTEXT fields of the project with a non-retryable ApplicationFailure before creating the asset - Remove dead agentContext handling from autofillAiActivity and its tests, and drop the unused ai_autofill i18n keys (paraglide recompiled) - Derive the DTO autofillSource enum from the AutofillSource const instead of a hardcoded z.enum to prevent drift
68 lines
2.4 KiB
TypeScript
68 lines
2.4 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 { readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
|
import { ulid } from 'ulid'
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
|
|
|
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.',
|
|
}),
|
|
metadata: Type.Optional(
|
|
Type.Record(Type.String(), Type.Any(), {
|
|
description:
|
|
'Key-value map of CREATION_CONTEXT metadata field keys and their values (e.g. prompt, model, provider). ' +
|
|
'Use list_autofill_fields first to inspect available CREATION_CONTEXT fields.',
|
|
}),
|
|
),
|
|
})
|
|
|
|
export function createCreateVersionTool(userId: string): AgentTool<typeof createVersionSchema> {
|
|
return {
|
|
name: 'create_version',
|
|
label: 'Create Version',
|
|
description: 'Create a new version of an existing file from a local file path.',
|
|
parameters: createVersionSchema,
|
|
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 = readFileMimeType(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_version',
|
|
args: {
|
|
parent: params.parent,
|
|
s3Key,
|
|
name: path.basename(absolutePath),
|
|
size: fileSize,
|
|
contentType: mimeType,
|
|
...(params.metadata ? { metadata: params.metadata } : {}),
|
|
},
|
|
userId,
|
|
assetId: params.parent,
|
|
})
|
|
return {
|
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
details: result,
|
|
}
|
|
},
|
|
}
|
|
}
|