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
@@ -0,0 +1,85 @@
import { useState } from 'react'
import { CircleHelp, Sparkles, Bot, Ban } from 'lucide-react'
import { m } from '@/ui/paraglide/messages.js'
import { Button } from '@/ui/components/ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@/ui/components/ui/dialog'
export function AutofillSourceHelpTrigger() {
const [open, setOpen] = useState(false)
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button
type="button"
variant="ghost"
size="icon"
className="h-4 w-4 p-0 hover:bg-transparent text-muted-foreground hover:text-foreground inline-flex items-center justify-center shrink-0 ml-1"
title={m.autofill_source_help_title()}
>
<CircleHelp className="h-3.5 w-3.5" />
<span className="sr-only">{m.autofill_source_help_title()}</span>
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<CircleHelp className="h-5 w-5 text-muted-foreground" />
{m.autofill_source_help_title()}
</DialogTitle>
<DialogDescription>{m.autofill_source_help_desc()}</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-2">
{/* Option 1: None */}
<div className="rounded-lg border p-3.5 space-y-1.5 bg-muted/30">
<div className="flex items-center gap-2 font-medium text-sm">
<Ban className="h-4 w-4 text-muted-foreground shrink-0" />
<span>{m.autofill_source_none()}</span>
</div>
<p className="text-xs text-muted-foreground leading-relaxed">
{m.autofill_source_none_desc()}
</p>
<p className="text-xs italic text-muted-foreground/80">
{m.autofill_source_none_example()}
</p>
</div>
{/* Option 2: Content */}
<div className="rounded-lg border p-3.5 space-y-1.5 bg-muted/30">
<div className="flex items-center gap-2 font-medium text-sm">
<Sparkles className="h-4 w-4 text-amber-500 shrink-0" />
<span>{m.autofill_source_content()}</span>
</div>
<p className="text-xs text-muted-foreground leading-relaxed">
{m.autofill_source_content_desc()}
</p>
<p className="text-xs italic text-muted-foreground/80">
{m.autofill_source_content_example()}
</p>
</div>
{/* Option 3: Creation Context */}
<div className="rounded-lg border p-3.5 space-y-1.5 bg-muted/30">
<div className="flex items-center gap-2 font-medium text-sm">
<Bot className="h-4 w-4 text-blue-500 shrink-0" />
<span>{m.autofill_source_creation_context()}</span>
</div>
<p className="text-xs text-muted-foreground leading-relaxed">
{m.autofill_source_creation_context_desc()}
</p>
<p className="text-xs italic text-muted-foreground/80">
{m.autofill_source_creation_context_example()}
</p>
</div>
</div>
</DialogContent>
</Dialog>
)
}
+66 -27
View File
@@ -1,5 +1,6 @@
import {
FieldType,
AutofillSource,
type FieldInfo,
type FieldInfo as MetadataFieldInfo,
type SelectOption,
@@ -24,8 +25,32 @@ import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popo
import { Input } from '@/ui/components/ui/input'
import { Label } from '@/ui/components/ui/label'
import { Switch } from '@/ui/components/ui/switch'
import { ToggleGroup, ToggleGroupItem } from '@/ui/components/ui/toggle-group'
import { reorderFieldSubset } from '@/ui/lib/dnd-utils'
import { PointerActivationConstraints } from '@dnd-kit/dom'
import {
AlignLeft,
Calendar,
ChevronDown,
GripVertical,
Hash,
List,
Plus,
Search,
Settings,
Sparkles,
Bot,
Star,
Tags,
ToggleLeft,
Type,
X,
User,
Users,
} from 'lucide-react'
import { useMemo, useState, useEffect } from 'react'
import { AutofillSourceHelpTrigger } from '@/ui/components/autofill-source-help-dialog'
export const PREDEFINED_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'gray']
@@ -80,26 +105,6 @@ export function getRandomUnusedColor(existingOptions: SelectOption[] = []): stri
}
return PREDEFINED_COLORS[Math.floor(Math.random() * PREDEFINED_COLORS.length)]
}
import {
AlignLeft,
Calendar,
ChevronDown,
GripVertical,
Hash,
List,
Plus,
Search,
Settings,
Sparkles,
Star,
Tags,
ToggleLeft,
Type,
X,
User,
Users,
} from 'lucide-react'
import { useMemo, useState, useEffect } from 'react'
export const FIELD_TYPE_ICONS: Record<FieldType, React.ComponentType<{ className?: string }>> = {
[FieldType.text]: Type,
@@ -135,7 +140,12 @@ function SortableFieldItem({ field, index, onVisibilityChange }: SortableFieldIt
<GripVertical className="h-4 w-4 text-muted-foreground" />
</div>
{Icon && <Icon className="h-4 w-4 text-muted-foreground shrink-0" />}
{field.aiAutofill && <Sparkles className="h-4 w-4 text-muted-foreground shrink-0" />}
{field.config?.autofillSource === AutofillSource.CONTENT && (
<Sparkles className="h-4 w-4 text-muted-foreground shrink-0" />
)}
{field.config?.autofillSource === AutofillSource.CREATION_CONTEXT && (
<Bot className="h-4 w-4 text-muted-foreground shrink-0" />
)}
<span className="text-sm truncate">{field.config?.name}</span>
</div>
<Switch
@@ -158,7 +168,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
const [view, setView] = useState<'list' | 'create'>('list')
const [newFieldName, setNewFieldName] = useState('')
const [newFieldType, setNewFieldType] = useState<FieldType | ''>('')
const [newAiAutofill, setNewAiAutofill] = useState(false)
const [newAutofillSource, setNewAutofillSource] = useState<AutofillSource>(AutofillSource.NONE)
const [newDescription, setNewDescription] = useState('')
const [newOptions, setNewOptions] = useState<SelectOption[]>([])
@@ -265,10 +275,10 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
config: {
name: newFieldName,
type: newFieldType,
autofillSource: newAutofillSource,
...typeConfig,
},
label: newFieldName,
aiAutofill: newAiAutofill,
description: newDescription,
},
})
@@ -333,9 +343,38 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
/>
</div>
<div className="flex items-center space-x-2">
<Switch id="ai-autofill" checked={newAiAutofill} onCheckedChange={setNewAiAutofill} />
<Label htmlFor="ai-autofill">{m.ai_autofill()}</Label>
<div className="space-y-2">
<div className="flex items-center gap-1">
<Label>{m.autofill_source()}</Label>
<AutofillSourceHelpTrigger />
</div>
<ToggleGroup
type="single"
value={newAutofillSource}
onValueChange={(val: string) => {
if (val) setNewAutofillSource(val as AutofillSource)
}}
className="w-full grid grid-cols-3 border rounded-md p-1 gap-1"
>
<ToggleGroupItem
value={AutofillSource.NONE}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_none()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CONTENT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_content()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CREATION_CONTEXT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_creation_context()}
</ToggleGroupItem>
</ToggleGroup>
</div>
{(newFieldType === FieldType.select || newFieldType === FieldType.selectMulti) && (
@@ -424,7 +463,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
setView('list')
setNewFieldName('')
setNewFieldType('')
setNewAiAutofill(false)
setNewAutofillSource(AutofillSource.NONE)
setNewDescription('')
setNewOptions([])
}}
@@ -8,6 +8,7 @@ import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } fr
import { isSortable, useSortable } from '@dnd-kit/react/sortable'
import {
FieldType,
AutofillSource,
type FieldInfo,
type FieldInfo as MetadataFieldInfo,
type SelectOption,
@@ -22,6 +23,7 @@ import {
Plus,
Settings,
Sparkles,
Bot,
X,
} from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
@@ -44,8 +46,9 @@ import {
} from '@/ui/components/ui/dropdown-menu'
import { Input } from '@/ui/components/ui/input'
import { Label } from '@/ui/components/ui/label'
import { Switch } from '@/ui/components/ui/switch'
import { ToggleGroup, ToggleGroupItem } from '@/ui/components/ui/toggle-group'
import { cn } from '@/ui/lib/utils'
import { AutofillSourceHelpTrigger } from '@/ui/components/autofill-source-help-dialog'
type ManageFieldsDialogProps = {
projectId: string
@@ -111,7 +114,12 @@ function SortableFieldRow({
)}
{!isSortable && <div className="w-4" />} {/* Spacer */}
{Icon && <Icon className="h-4 w-4 text-muted-foreground shrink-0" />}
{field.aiAutofill && <Sparkles className="h-4 w-4 text-muted-foreground shrink-0" />}
{field.config?.autofillSource === AutofillSource.CONTENT && (
<Sparkles className="h-4 w-4 text-muted-foreground shrink-0" />
)}
{field.config?.autofillSource === AutofillSource.CREATION_CONTEXT && (
<Bot className="h-4 w-4 text-muted-foreground shrink-0" />
)}
<div className="flex-1 text-sm font-medium truncate">{field.config?.name}</div>
</div>
)
@@ -134,13 +142,13 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
const [editLabel, setEditLabel] = useState('')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const [editConfig, setEditConfig] = useState<any>({})
const [editAiAutofill, setEditAiAutofill] = useState(false)
const [editAutofillSource, setEditAutofillSource] = useState<AutofillSource>(AutofillSource.NONE)
const [editDescription, setEditDescription] = useState('')
// Creator State
const [newName, setNewName] = useState('')
const [newType, setNewType] = useState<FieldType | ''>('')
const [newAiAutofill, setNewAiAutofill] = useState(false)
const [newAutofillSource, setNewAutofillSource] = useState<AutofillSource>(AutofillSource.NONE)
const [newDescription, setNewDescription] = useState('')
const [newOptions, setNewOptions] = useState<SelectOption[]>([])
@@ -251,12 +259,12 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
if (field) {
setEditLabel(field.config?.name || '') // Using name as label for now if label missing
setEditConfig(field.config || {})
setEditAiAutofill(field.aiAutofill || false)
setEditAutofillSource(field.config?.autofillSource ?? AutofillSource.NONE)
setEditDescription(field.description || '')
} else {
setEditLabel('')
setEditConfig({})
setEditAiAutofill(false)
setEditAutofillSource(AutofillSource.NONE)
setEditDescription('')
}
}
@@ -272,7 +280,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
setSelectedFieldId(null)
setNewName('')
setNewType('')
setNewAiAutofill(false)
setNewAutofillSource(AutofillSource.NONE)
setNewDescription('')
setNewOptions([])
}
@@ -312,11 +320,11 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
config: {
name: newName,
type: newType,
autofillSource: newAutofillSource,
...typeConfig,
},
label: newName,
scope: selectedGroup === SCOPE_GROUPS.PROJECT ? undefined : selectedGroup,
aiAutofill: newAiAutofill,
description: newDescription,
},
})
@@ -331,8 +339,8 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
...selectedField.config,
...editConfig,
name: editLabel,
autofillSource: editAutofillSource,
},
aiAutofill: editAiAutofill,
description: editDescription,
},
})
@@ -396,9 +404,38 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
placeholder={m.description_optional_placeholder()}
/>
</div>
<div className="flex items-center space-x-2">
<Switch checked={newAiAutofill} onCheckedChange={setNewAiAutofill} />
<Label>{m.ai_autofill()}</Label>
<div className="space-y-2">
<div className="flex items-center gap-1">
<Label>{m.autofill_source()}</Label>
<AutofillSourceHelpTrigger />
</div>
<ToggleGroup
type="single"
value={newAutofillSource}
onValueChange={(val: string) => {
if (val) setNewAutofillSource(val as AutofillSource)
}}
className="w-full grid grid-cols-3 border rounded-md p-1 gap-1"
>
<ToggleGroupItem
value={AutofillSource.NONE}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_none()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CONTENT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_content()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CREATION_CONTEXT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_creation_context()}
</ToggleGroupItem>
</ToggleGroup>
</div>
{(newType === FieldType.select || newType === FieldType.selectMulti) && (
@@ -518,13 +555,42 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
/>
</div>
<div className="flex items-center space-x-2">
<Switch
checked={editAiAutofill}
onCheckedChange={setEditAiAutofill}
<div className="space-y-2">
<div className="flex items-center gap-1">
<Label>{m.autofill_source()}</Label>
<AutofillSourceHelpTrigger />
</div>
<ToggleGroup
type="single"
value={isCreating ? newAutofillSource : editAutofillSource}
onValueChange={(val) => {
if (val) {
if (isCreating) setNewAutofillSource(val as AutofillSource)
else setEditAutofillSource(val as AutofillSource)
}
}}
disabled={isReadOnly}
/>
<Label>{m.ai_autofill()}</Label>
className="w-full grid grid-cols-3 border rounded-md p-1 gap-1"
>
<ToggleGroupItem
value={AutofillSource.NONE}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_none()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CONTENT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_content()}
</ToggleGroupItem>
<ToggleGroupItem
value={AutofillSource.CREATION_CONTEXT}
className="text-[11px] leading-tight py-1 px-1 h-auto min-h-[32px] w-full text-center justify-center whitespace-normal font-medium"
>
{m.autofill_source_creation_context()}
</ToggleGroupItem>
</ToggleGroup>
</div>
{/* TODO: Add specific option editors here */}