feat(metadata): replace aiAutofill boolean with autofillSource enum (#302)

* 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
This commit is contained in:
Yiling-J
2026-08-13 13:41:46 +08:00
committed by GitHub
parent 96493a5493
commit 41b73feeab
57 changed files with 1118 additions and 663 deletions
+7 -7
View File
@@ -70,31 +70,31 @@ describe('createCreateFileTool', () => {
expect(result.details).toEqual({ id: 'file-1', name: 'test', type: 'file' })
})
it('should forward the optional context to the agent tool workflow when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-context.md')
it('should forward the optional metadata to the agent tool workflow when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-metadata.md')
const tool = createCreateFileTool('user-1')
await tool.execute('call-1', {
parent: 'folder-1',
path: filePath,
context: 'Generated using gemini',
metadata: { prompt: 'Generated using gemini' },
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({ context: 'Generated using gemini' }),
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
}),
)
})
it('should not include context in args when not provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-no-context.md')
it('should not include metadata in args when not provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-file-no-metadata.md')
const tool = createCreateFileTool('user-1')
await tool.execute('call-1', { parent: 'folder-1', path: filePath })
const callArgs = vi.mocked(executeAgentToolWorkflow).mock.calls[0][0].args
expect(callArgs).not.toHaveProperty('context')
expect(callArgs).not.toHaveProperty('metadata')
})
it('should throw when the local file does not exist', async () => {
+5 -7
View File
@@ -30,13 +30,11 @@ const createFileSchema = Type.Object({
}),
}),
),
context: Type.Optional(
Type.String({
metadata: Type.Optional(
Type.Record(Type.String(), Type.Any(), {
description:
'Optional context passed to the AI metadata autofill workflow for fields that cannot be inferred from file content alone ' +
'(e.g. the model or prompt used to generate the asset). ' +
'Use list_autofill_fields first to check which fields can be autofilled. ' +
'Keep the context short and only include information relevant to those fields.',
'Key-value map of metadata field keys and their values. Use the "key" field from list_autofill_fields data as the map key.' +
'Use list_autofill_fields first to inspect available fields.',
}),
),
})
@@ -104,7 +102,7 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
name,
size,
contentType: mimeType,
...(params.context ? { context: params.context } : {}),
...(params.metadata ? { metadata: params.metadata } : {}),
},
userId,
assetId: params.parent,
@@ -67,31 +67,31 @@ describe('createCreateVersionTool', () => {
expect(result.details).toEqual({ id: 'file-1', name: 'v2', type: 'file' })
})
it('should forward the optional context to the agent tool workflow when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-context.md')
it('should forward the optional metadata to the agent tool workflow when provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-metadata.md')
const tool = createCreateVersionTool('user-1')
await tool.execute('call-1', {
parent: 'file-1',
path: filePath,
context: 'Generated using gemini',
metadata: { prompt: 'Generated using gemini' },
})
expect(executeAgentToolWorkflow).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({ context: 'Generated using gemini' }),
args: expect.objectContaining({ metadata: { prompt: 'Generated using gemini' } }),
}),
)
})
it('should not include context in args when not provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-no-context.md')
it('should not include metadata in args when not provided', async () => {
const filePath = createTempFile('# Hello from disk', 'create-version-no-metadata.md')
const tool = createCreateVersionTool('user-1')
await tool.execute('call-1', { parent: 'file-1', path: filePath })
const callArgs = vi.mocked(executeAgentToolWorkflow).mock.calls[0][0].args
expect(callArgs).not.toHaveProperty('context')
expect(callArgs).not.toHaveProperty('metadata')
})
it('should throw when the local file does not exist', async () => {
+5 -7
View File
@@ -17,13 +17,11 @@ const createVersionSchema = Type.Object({
description:
'The absolute or relative local path to the file on disk. This parameter is required.',
}),
context: Type.Optional(
Type.String({
metadata: Type.Optional(
Type.Record(Type.String(), Type.Any(), {
description:
'Optional context passed to the AI metadata autofill workflow for fields that cannot be inferred from file content alone ' +
'(e.g. the model or prompt used to generate the asset). ' +
'Use list_autofill_fields first to check which fields can be autofilled. ' +
'Keep the context short and only include information relevant to those fields.',
'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.',
}),
),
})
@@ -55,7 +53,7 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
name: path.basename(absolutePath),
size: fileSize,
contentType: mimeType,
...(params.context ? { context: params.context } : {}),
...(params.metadata ? { metadata: params.metadata } : {}),
},
userId,
assetId: params.parent,
@@ -16,8 +16,8 @@ export function createListAutofillFieldsTool(
name: 'list_autofill_fields',
label: 'List Autofill Fields',
description:
'List the AI-autofillable metadata fields defined for the project of a parent folder, including their types and available options. ' +
'Use this before creating a file or version so you can pass relevant context (e.g. the model or prompt used to generate the asset) to create_file/create_version.',
'List the CREATION_CONTEXT metadata fields defined for the project of a parent folder (e.g. model or prompt used to generate the asset). ' +
'Use this before calling create_file or create_version so you can populate their field keys and values in the "metadata" dictionary parameter.',
parameters: listAutofillFieldsSchema,
execute: async (_toolCallId, params) => {
const result = await executeAgentToolWorkflow({