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
+5 -7
View File
@@ -87,7 +87,7 @@ describe('MetadataService', () => {
expect(projectFields[2].visible).toBe(false)
})
it('AIAutofillAndDescription', async () => {
it('AutofillSourceAndDescription', async () => {
const team = await prisma.team.create({
data: { name: 'test-team' },
})
@@ -103,24 +103,22 @@ describe('MetadataService', () => {
key: 'teamfield1',
scope: 'TEAM',
team: { connect: { id: team.id } },
config: { name: 'Test Team Field', type: 'text' },
aiAutofill: true,
config: { name: 'Test Team Field', type: 'text', autofillSource: 'CONTENT' },
description: desc,
},
})
const newDesc = 'Updated description'
const updatedTeamField = await metadataService.updateTeamField(team.id, teamField.key, {
config: { name: 'Test Team Field', type: 'text' },
aiAutofill: false,
config: { name: 'Test Team Field', type: 'text', autofillSource: 'NONE' },
description: newDesc,
})
expect(updatedTeamField.aiAutofill).toBe(false)
expect(updatedTeamField.config?.autofillSource).toBe('NONE')
expect(updatedTeamField.description).toBe(newDesc)
// IDOR test
await expect(
metadataService.updateTeamField('wrong-team-id', teamField.key, {
config: { name: 'Hack', type: 'text' },
config: { name: 'Hack', type: 'text', autofillSource: 'NONE' },
}),
).rejects.toThrow('Field does not belong to this team')
})
+93 -75
View File
@@ -43,7 +43,6 @@ export class MetadataService {
scope: 'TEAM',
teamId: teamId,
config: req.config,
aiAutofill: req.aiAutofill,
description: req.description,
},
})
@@ -89,7 +88,6 @@ export class MetadataService {
where: { id: field.id },
data: {
config: req.config,
aiAutofill: req.aiAutofill ?? field.aiAutofill,
description: req.description ?? field.description,
},
})
@@ -125,7 +123,6 @@ export class MetadataService {
projectId: projectId,
teamId: p.teamId,
config: req.config,
aiAutofill: req.aiAutofill,
description: req.description,
},
})
@@ -239,7 +236,6 @@ export class MetadataService {
where: { id: field.id },
data: {
config: req.config,
aiAutofill: req.aiAutofill ?? field.aiAutofill,
description: req.description ?? field.description,
},
})
@@ -262,84 +258,106 @@ export class MetadataService {
assetId: string,
reqs: UpdateAssetMetadataRequest[],
allowReadOnly = false,
): Promise<void> {
await this.client.$transaction(async (tx) => {
await this.updateAssetMetadataInternal(tx, assetId, reqs, allowReadOnly)
})
}
/**
* Same as {@link updateAssetMetadata} but runs inside the caller's transaction,
* keeping the metadata write atomic with the surrounding changes.
*/
async updateAssetMetadataInTx(
tx: Prisma.TransactionClient,
assetId: string,
reqs: UpdateAssetMetadataRequest[],
allowReadOnly = false,
): Promise<void> {
await this.updateAssetMetadataInternal(tx, assetId, reqs, allowReadOnly)
}
private async updateAssetMetadataInternal(
client: Prisma.TransactionClient | typeof prisma,
assetId: string,
reqs: UpdateAssetMetadataRequest[],
allowReadOnly: boolean,
): Promise<void> {
const resolvedAssetId = await assetService.resolveLatestVersionId(assetId)
await this.client.$transaction(async (tx) => {
for (const req of reqs) {
const field = await tx.metadataField.findUnique({
where: { key: req.key },
})
if (field?.readOnly && !allowReadOnly) {
throw new Error(`Field ${field.key} is read-only`)
}
const value = req.value
let stringValue: string | null = null
let numberValue: number | null = null
let booleanValue: boolean | null = null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let jsonValue: any = null
let dateValue: Date | null = null
if (typeof value === 'string') {
// Check if it's a valid ISO date string
const date = new Date(value)
if (!isNaN(date.getTime()) && value.includes('T') && value.includes('-')) {
dateValue = date
} else {
stringValue = value
}
} else if (typeof value === 'number') {
numberValue = value
} else if (typeof value === 'boolean') {
booleanValue = value
} else if (value instanceof Date) {
dateValue = value
} else if (Array.isArray(value)) {
jsonValue = value
} else if (value === null) {
// all null
} else if (typeof value === 'object') {
jsonValue = value
} else {
stringValue = String(value)
}
await tx.assetMetadataValue.upsert({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: resolvedAssetId,
fieldKey: req.key,
},
},
create: {
assetId: resolvedAssetId,
fieldKey: req.key,
stringValue,
numberValue,
booleanValue,
jsonValue,
dateValue,
},
update: {
stringValue,
numberValue,
booleanValue,
jsonValue,
dateValue,
},
})
for (const req of reqs) {
const field = await client.metadataField.findUnique({
where: { key: req.key },
})
if (field?.readOnly && !allowReadOnly) {
throw new Error(`Field ${field.key} is read-only`)
}
// Touch asset
await tx.asset.update({
where: { id: resolvedAssetId },
data: {
updatedAt: new Date(),
const value = req.value
let stringValue: string | null = null
let numberValue: number | null = null
let booleanValue: boolean | null = null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let jsonValue: any = null
let dateValue: Date | null = null
if (typeof value === 'string') {
// Check if it's a valid ISO date string
const date = new Date(value)
if (!isNaN(date.getTime()) && value.includes('T') && value.includes('-')) {
dateValue = date
} else {
stringValue = value
}
} else if (typeof value === 'number') {
numberValue = value
} else if (typeof value === 'boolean') {
booleanValue = value
} else if (value instanceof Date) {
dateValue = value
} else if (Array.isArray(value)) {
jsonValue = value
} else if (value === null) {
// all null
} else if (typeof value === 'object') {
jsonValue = value
} else {
stringValue = String(value)
}
await client.assetMetadataValue.upsert({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldKey: {
assetId: resolvedAssetId,
fieldKey: req.key,
},
},
create: {
assetId: resolvedAssetId,
fieldKey: req.key,
stringValue,
numberValue,
booleanValue,
jsonValue,
dateValue,
},
update: {
stringValue,
numberValue,
booleanValue,
jsonValue,
dateValue,
},
})
}
// Touch asset
await client.asset.update({
where: { id: resolvedAssetId },
data: {
updatedAt: new Date(),
},
})
}
}