feat: remove priority field from SkillDefinition and related services

This commit is contained in:
mlogclub
2026-04-14 17:50:33 +08:00
parent d32e65bf68
commit e4cec52661
3 changed files with 43 additions and 205 deletions
@@ -10,7 +10,6 @@ type SkillDefinitionResponse struct {
Instruction string `json:"instruction"` Instruction string `json:"instruction"`
Examples []string `json:"examples"` Examples []string `json:"examples"`
ToolWhitelist []string `json:"toolWhitelist"` ToolWhitelist []string `json:"toolWhitelist"`
Priority int `json:"priority"`
Status int `json:"status"` Status int `json:"status"`
StatusName string `json:"statusName"` StatusName string `json:"statusName"`
Remark string `json:"remark"` Remark string `json:"remark"`
+43 -195
View File
@@ -1,28 +1,9 @@
"use client" "use client"
import { import { useCallback, useEffect, useState } from "react"
closestCenter,
DndContext,
KeyboardSensor,
MouseSensor,
TouchSensor,
useSensor,
useSensors,
type DragEndEvent,
} from "@dnd-kit/core"
import {
arrayMove,
SortableContext,
sortableKeyboardCoordinates,
useSortable,
verticalListSortingStrategy,
} from "@dnd-kit/sortable"
import { CSS } from "@dnd-kit/utilities"
import { useCallback, useEffect, useState, type CSSProperties } from "react"
import { import {
BrainCircuitIcon, BrainCircuitIcon,
BugIcon, BugIcon,
GripVerticalIcon,
MoreHorizontalIcon, MoreHorizontalIcon,
PlusIcon, PlusIcon,
RefreshCwIcon, RefreshCwIcon,
@@ -57,18 +38,14 @@ import {
deleteSkillDefinition, deleteSkillDefinition,
fetchSkillDefinitions, fetchSkillDefinitions,
updateSkillDefinition, updateSkillDefinition,
updateSkillDefinitionPriority,
updateSkillDefinitionStatus, updateSkillDefinitionStatus,
type CreateSkillDefinitionPayload, type CreateSkillDefinitionPayload,
type PageResult, type PageResult,
type SkillDefinition, type SkillDefinition,
} from "@/lib/api/admin" } from "@/lib/api/admin"
import { import { Status, StatusLabels } from "@/lib/generated/enums"
Status,
StatusLabels,
} from "@/lib/generated/enums"
import { getEnumLabel, getEnumOptions } from "@/lib/enums" import { getEnumLabel, getEnumOptions } from "@/lib/enums"
import { cn, formatDateTime } from "@/lib/utils" import { formatDateTime } from "@/lib/utils"
import { EditDialog } from "./_components/edit" import { EditDialog } from "./_components/edit"
import { DebugDialog } from "./_components/debug-dialog" import { DebugDialog } from "./_components/debug-dialog"
@@ -80,9 +57,8 @@ const statusFilterOptions = [
})), })),
] ]
type SortableSkillRowProps = { type SkillRowProps = {
item: SkillDefinition item: SkillDefinition
disabled: boolean
actionLoadingId: number | null actionLoadingId: number | null
openEditDialog: (item: SkillDefinition) => void openEditDialog: (item: SkillDefinition) => void
openDebugDialog: (item: SkillDefinition) => void openDebugDialog: (item: SkillDefinition) => void
@@ -90,55 +66,16 @@ type SortableSkillRowProps = {
handleDelete: (item: SkillDefinition) => void handleDelete: (item: SkillDefinition) => void
} }
function SortableSkillRow({ function SkillRow({
item, item,
disabled,
actionLoadingId, actionLoadingId,
openEditDialog, openEditDialog,
openDebugDialog, openDebugDialog,
handleToggleStatus, handleToggleStatus,
handleDelete, handleDelete,
}: SortableSkillRowProps) { }: SkillRowProps) {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
} = useSortable({
id: item.id,
disabled,
})
const style: CSSProperties = {
transform: CSS.Transform.toString(transform),
transition,
}
return ( return (
<TableRow <TableRow>
ref={setNodeRef}
style={style}
className={cn(
isDragging && "relative z-10 bg-muted/60 shadow-sm",
!disabled && "cursor-move",
)}
>
<TableCell className="w-14">
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 cursor-grab active:cursor-grabbing"
disabled={disabled}
aria-label={`拖拽排序 ${item.name}`}
{...attributes}
{...listeners}
>
<GripVerticalIcon className="size-4 text-muted-foreground" />
</Button>
</TableCell>
<TableCell> <TableCell>
<div className="flex items-start gap-3"> <div className="flex items-start gap-3">
<div className="mt-0.5 flex size-10 items-center justify-center rounded-2xl bg-muted text-muted-foreground"> <div className="mt-0.5 flex size-10 items-center justify-center rounded-2xl bg-muted text-muted-foreground">
@@ -148,12 +85,8 @@ function SortableSkillRow({
<div className="flex flex-wrap items-center gap-2"> <div className="flex flex-wrap items-center gap-2">
<div className="font-medium">{item.name}</div> <div className="font-medium">{item.name}</div>
<Badge variant="outline">{item.code}</Badge> <Badge variant="outline">{item.code}</Badge>
<Badge variant="secondary"> <Badge variant="secondary"> {item.toolWhitelist.length}</Badge>
{item.toolWhitelist.length} <Badge variant="secondary"> {item.examples.length}</Badge>
</Badge>
<Badge variant="secondary">
{item.examples.length}
</Badge>
</div> </div>
<div className="mt-1 line-clamp-2 text-sm text-muted-foreground"> <div className="mt-1 line-clamp-2 text-sm text-muted-foreground">
{item.description || "暂无描述"} {item.description || "暂无描述"}
@@ -169,9 +102,7 @@ function SortableSkillRow({
</Badge> </Badge>
))} ))}
{item.toolWhitelist.length > 3 ? ( {item.toolWhitelist.length > 3 ? (
<Badge variant="outline"> <Badge variant="outline">+{item.toolWhitelist.length - 3}</Badge>
+{item.toolWhitelist.length - 3}
</Badge>
) : null} ) : null}
</div> </div>
) : null} ) : null}
@@ -186,21 +117,11 @@ function SortableSkillRow({
onCheckedChange={() => void handleToggleStatus(item)} onCheckedChange={() => void handleToggleStatus(item)}
aria-label={`${item.name} 状态切换`} aria-label={`${item.name} 状态切换`}
/> />
<Badge <Badge variant={item.status === Status.Ok ? "default" : "outline"}>
variant={ {getEnumLabel(StatusLabels, item.status as keyof typeof StatusLabels)}
item.status === Status.Ok
? "default"
: "outline"
}
>
{getEnumLabel(
StatusLabels,
item.status as keyof typeof StatusLabels
)}
</Badge> </Badge>
</div> </div>
</TableCell> </TableCell>
<TableCell>{item.priority}</TableCell>
<TableCell> <TableCell>
<div className="space-y-1 text-sm"> <div className="space-y-1 text-sm">
<div>{formatDateTime(item.updatedAt)}</div> <div>{formatDateTime(item.updatedAt)}</div>
@@ -211,19 +132,11 @@ function SortableSkillRow({
</TableCell> </TableCell>
<TableCell className="text-right"> <TableCell className="text-right">
<ButtonGroup className="ml-auto"> <ButtonGroup className="ml-auto">
<Button <Button variant="outline" size="sm" onClick={() => openDebugDialog(item)}>
variant="outline"
size="sm"
onClick={() => openDebugDialog(item)}
>
<BugIcon /> <BugIcon />
</Button> </Button>
<Button <Button variant="outline" size="sm" onClick={() => openEditDialog(item)}>
variant="outline"
size="sm"
onClick={() => openEditDialog(item)}
>
</Button> </Button>
<DropdownMenu> <DropdownMenu>
@@ -262,7 +175,6 @@ export default function DashboardSkillsPage() {
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false) const [saving, setSaving] = useState(false)
const [actionLoadingId, setActionLoadingId] = useState<number | null>(null) const [actionLoadingId, setActionLoadingId] = useState<number | null>(null)
const [sorting, setSorting] = useState(false)
const [dialogOpen, setDialogOpen] = useState(false) const [dialogOpen, setDialogOpen] = useState(false)
const [debugDialogOpen, setDebugDialogOpen] = useState(false) const [debugDialogOpen, setDebugDialogOpen] = useState(false)
const [editingItem, setEditingItem] = useState<SkillDefinition | null>(null) const [editingItem, setEditingItem] = useState<SkillDefinition | null>(null)
@@ -272,18 +184,6 @@ export default function DashboardSkillsPage() {
page: { page: 1, limit: 20, total: 0 }, page: { page: 1, limit: 20, total: 0 },
}) })
const sensors = useSensors(
useSensor(MouseSensor, {
activationConstraint: { distance: 8 },
}),
useSensor(TouchSensor, {
activationConstraint: { delay: 150, tolerance: 8 },
}),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
}),
)
const loadData = useCallback(async () => { const loadData = useCallback(async () => {
setLoading(true) setLoading(true)
try { try {
@@ -388,10 +288,7 @@ export default function DashboardSkillsPage() {
} }
async function handleToggleStatus(item: SkillDefinition) { async function handleToggleStatus(item: SkillDefinition) {
const nextStatus = const nextStatus = item.status === Status.Ok ? Status.Disabled : Status.Ok
item.status === Status.Ok
? Status.Disabled
: Status.Ok
setActionLoadingId(item.id) setActionLoadingId(item.id)
try { try {
@@ -418,41 +315,6 @@ export default function DashboardSkillsPage() {
} }
} }
async function handleDragEnd(event: DragEndEvent) {
const { active, over } = event
if (!over || active.id === over.id || sorting) {
return
}
const previousResults = result.results
const oldIndex = previousResults.findIndex((item) => item.id === active.id)
const newIndex = previousResults.findIndex((item) => item.id === over.id)
if (oldIndex < 0 || newIndex < 0) {
return
}
const nextResults = arrayMove(previousResults, oldIndex, newIndex)
setResult((current) => ({
...current,
results: nextResults,
}))
setSorting(true)
try {
await updateSkillDefinitionPriority(nextResults.map((item) => item.id))
toast.success("Skill 优先级已更新")
await loadData()
} catch (error) {
setResult((current) => ({
...current,
results: previousResults,
}))
toast.error(error instanceof Error ? error.message : "更新优先级失败")
} finally {
setSorting(false)
}
}
return ( return (
<> <>
<div className="flex flex-1 flex-col gap-6 p-4 lg:p-6"> <div className="flex flex-1 flex-col gap-6 p-4 lg:p-6">
@@ -500,50 +362,36 @@ export default function DashboardSkillsPage() {
<div className="space-y-4"> <div className="space-y-4">
<div className="overflow-hidden rounded-2xl border bg-background"> <div className="overflow-hidden rounded-2xl border bg-background">
<DndContext <Table>
sensors={sensors} <TableHeader className="bg-muted/40">
collisionDetection={closestCenter} <TableRow>
onDragEnd={handleDragEnd} <TableHead>Skill</TableHead>
> <TableHead></TableHead>
<Table> <TableHead></TableHead>
<TableHeader className="bg-muted/40"> <TableHead className="w-[168px] text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{!loading && result.results.length === 0 ? (
<TableRow> <TableRow>
<TableHead className="w-14"></TableHead> <TableCell colSpan={4} className="py-12 text-center text-muted-foreground">
<TableHead>Skill</TableHead> Skill
<TableHead></TableHead> </TableCell>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="w-[168px] text-right"></TableHead>
</TableRow> </TableRow>
</TableHeader> ) : null}
<TableBody> {result.results.map((item) => (
{!loading && result.results.length === 0 ? ( <SkillRow
<TableRow> key={item.id}
<TableCell colSpan={6} className="py-12 text-center text-muted-foreground"> item={item}
Skill actionLoadingId={actionLoadingId}
</TableCell> openEditDialog={openEditDialog}
</TableRow> openDebugDialog={openDebugDialog}
) : null} handleToggleStatus={handleToggleStatus}
<SortableContext handleDelete={handleDelete}
items={result.results.map((item) => item.id)} />
strategy={verticalListSortingStrategy} ))}
> </TableBody>
{result.results.map((item) => ( </Table>
<SortableSkillRow
key={item.id}
item={item}
disabled={sorting}
actionLoadingId={actionLoadingId}
openEditDialog={openEditDialog}
openDebugDialog={openDebugDialog}
handleToggleStatus={handleToggleStatus}
handleDelete={handleDelete}
/>
))}
</SortableContext>
</TableBody>
</Table>
</DndContext>
<div className="border-t px-4 py-3"> <div className="border-t px-4 py-3">
<ListPagination <ListPagination
page={result.page.page} page={result.page.page}
-9
View File
@@ -278,7 +278,6 @@ export type SkillDefinition = {
instruction: string instruction: string
examples: string[] examples: string[]
toolWhitelist: string[] toolWhitelist: string[]
priority: number
status: number status: number
statusName: string statusName: string
remark: string remark: string
@@ -295,7 +294,6 @@ export type CreateSkillDefinitionPayload = {
instruction: string instruction: string
examples: string[] examples: string[]
toolWhitelist: string[] toolWhitelist: string[]
priority?: number
remark: string remark: string
} }
@@ -934,13 +932,6 @@ export function deleteSkillDefinition(id: number) {
}) })
} }
export function updateSkillDefinitionPriority(ids: number[]) {
return request<void>("/api/console/skill-definition/update_priority", {
method: "POST",
body: JSON.stringify(ids),
})
}
export function debugRunSkillDefinition(payload: SkillDebugRunPayload) { export function debugRunSkillDefinition(payload: SkillDebugRunPayload) {
return request<SkillDebugRunResult>("/api/console/skill-definition/debug_run", { return request<SkillDebugRunResult>("/api/console/skill-definition/debug_run", {
method: "POST", method: "POST",