refactor: support i18n
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { Resolver, useForm } from "react-hook-form"
|
||||
import { type Resolver, useForm } from "react-hook-form"
|
||||
import { z } from "zod/v4"
|
||||
import { LoaderCircleIcon, PlayIcon } from "lucide-react"
|
||||
import { toast } from "sonner"
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
type SkillDebugRunPayload,
|
||||
type SkillDebugRunResult,
|
||||
} from "@/lib/api/admin"
|
||||
import { useI18n } from "@/i18n/provider"
|
||||
|
||||
type DebugDialogProps = {
|
||||
open: boolean
|
||||
@@ -37,19 +38,21 @@ type DebugDialogProps = {
|
||||
onOpenChange: (open: boolean) => void
|
||||
}
|
||||
|
||||
const debugFormSchema = z.object({
|
||||
aiAgentId: z.string().trim().min(1, "请选择 AI Agent"),
|
||||
type TFunction = (key: string, values?: Record<string, string | number>) => string
|
||||
|
||||
function createDebugFormSchema(t: TFunction) {
|
||||
return z.object({
|
||||
aiAgentId: z.string().trim().min(1, t("skillDefinition.agentRequired")),
|
||||
conversationId: z.string().trim(),
|
||||
userMessage: z.string().trim().min(1, "请输入用户消息"),
|
||||
})
|
||||
userMessage: z.string().trim().min(1, t("skillDefinition.messageRequired")),
|
||||
})
|
||||
}
|
||||
|
||||
type DebugForm = z.infer<typeof debugFormSchema>
|
||||
|
||||
const debugFormResolver = zodResolver(debugFormSchema as never) as Resolver<
|
||||
z.input<typeof debugFormSchema>,
|
||||
undefined,
|
||||
z.output<typeof debugFormSchema>
|
||||
>
|
||||
type DebugForm = {
|
||||
aiAgentId: string
|
||||
conversationId: string
|
||||
userMessage: string
|
||||
}
|
||||
|
||||
const emptyForm: DebugForm = {
|
||||
aiAgentId: "",
|
||||
@@ -57,15 +60,17 @@ const emptyForm: DebugForm = {
|
||||
userMessage: "",
|
||||
}
|
||||
|
||||
const quickResumeActions = [
|
||||
{ label: "确认", value: "确认" },
|
||||
{ label: "取消", value: "取消" },
|
||||
]
|
||||
function getQuickResumeActions(t: TFunction) {
|
||||
return [
|
||||
{ label: t("skillDefinition.confirm"), value: t("skillDefinition.confirm") },
|
||||
{ label: t("skillDefinition.reject"), value: t("skillDefinition.reject") },
|
||||
]
|
||||
}
|
||||
|
||||
function ResultBlock({
|
||||
title,
|
||||
value,
|
||||
emptyText = "暂无数据",
|
||||
emptyText,
|
||||
}: {
|
||||
title: string
|
||||
value?: string
|
||||
@@ -116,6 +121,7 @@ function DebugDialogBody({
|
||||
skillName,
|
||||
onOpenChange,
|
||||
}: DebugDialogProps) {
|
||||
const t = useI18n()
|
||||
const formId = `skill-debug-form-${skillCode}`
|
||||
const [running, setRunning] = useState(false)
|
||||
const [resuming, setResuming] = useState(false)
|
||||
@@ -123,11 +129,12 @@ function DebugDialogBody({
|
||||
const [result, setResult] = useState<SkillDebugRunResult | null>(null)
|
||||
const [resumeResult, setResumeResult] = useState<SkillDebugRunResult | null>(null)
|
||||
const [resumeMessage, setResumeMessage] = useState("")
|
||||
const form = useForm<
|
||||
z.input<typeof debugFormSchema>,
|
||||
undefined,
|
||||
z.output<typeof debugFormSchema>
|
||||
>({
|
||||
const debugFormSchema = useMemo(() => createDebugFormSchema(t), [t])
|
||||
const debugFormResolver = useMemo(
|
||||
() => zodResolver(debugFormSchema) as Resolver<DebugForm>,
|
||||
[debugFormSchema],
|
||||
)
|
||||
const form = useForm<DebugForm>({
|
||||
resolver: debugFormResolver,
|
||||
defaultValues: emptyForm,
|
||||
})
|
||||
@@ -142,6 +149,7 @@ function DebugDialogBody({
|
||||
} = form
|
||||
|
||||
const selectedAgentId = watch("aiAgentId")
|
||||
const quickResumeActions = useMemo(() => getQuickResumeActions(t), [t])
|
||||
|
||||
useEffect(() => {
|
||||
async function loadAIAgents() {
|
||||
@@ -205,7 +213,7 @@ function DebugDialogBody({
|
||||
setResumeResult(null)
|
||||
setResumeMessage("")
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "Skill 调试失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.debugFailed"))
|
||||
setResult(null)
|
||||
} finally {
|
||||
setRunning(false)
|
||||
@@ -218,7 +226,7 @@ function DebugDialogBody({
|
||||
return
|
||||
}
|
||||
if (!nextMessage) {
|
||||
toast.error("请输入恢复消息")
|
||||
toast.error(t("skillDefinition.resumeMessageRequired"))
|
||||
return
|
||||
}
|
||||
const payload: SkillDebugResumePayload = {
|
||||
@@ -237,7 +245,7 @@ function DebugDialogBody({
|
||||
setResumeResult(data)
|
||||
setResumeMessage(nextMessage)
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "恢复调试失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.resumeFailed"))
|
||||
setResumeResult(null)
|
||||
} finally {
|
||||
setResuming(false)
|
||||
@@ -248,8 +256,8 @@ function DebugDialogBody({
|
||||
<ProjectDialog
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title={`调试 Skill · ${skillName || skillCode}`}
|
||||
description="强制指定当前 Skill,直接查看 route、tools、graph、HITL 和回复结果。"
|
||||
title={t("skillDefinition.debugTitle", { name: skillName || skillCode })}
|
||||
description={t("skillDefinition.debugDescription")}
|
||||
size="xl"
|
||||
allowFullscreen
|
||||
footer={
|
||||
@@ -260,11 +268,11 @@ function DebugDialogBody({
|
||||
disabled={running}
|
||||
onClick={() => onOpenChange(false)}
|
||||
>
|
||||
关闭
|
||||
{t("skillDefinition.close")}
|
||||
</Button>
|
||||
<Button type="submit" form={formId} disabled={running}>
|
||||
{running ? <LoaderCircleIcon className="animate-spin" /> : <PlayIcon />}
|
||||
{running ? "调试中..." : "开始调试"}
|
||||
{running ? t("skillDefinition.debugging") : t("skillDefinition.startDebug")}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
@@ -272,7 +280,7 @@ function DebugDialogBody({
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm">调试输入</CardTitle>
|
||||
<CardTitle className="text-sm">{t("skillDefinition.debugInput")}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form id={formId} onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
@@ -283,9 +291,9 @@ function DebugDialogBody({
|
||||
<OptionCombobox
|
||||
value={selectedAgentId}
|
||||
options={aiAgentOptions}
|
||||
placeholder="选择 AI Agent"
|
||||
searchPlaceholder="搜索 AI Agent"
|
||||
emptyText="未找到 AI Agent"
|
||||
placeholder={t("skillDefinition.selectAgent")}
|
||||
searchPlaceholder={t("skillDefinition.searchAgent")}
|
||||
emptyText={t("skillDefinition.emptyAgent")}
|
||||
onChange={(value) =>
|
||||
setValue("aiAgentId", value, { shouldValidate: true })
|
||||
}
|
||||
@@ -302,7 +310,7 @@ function DebugDialogBody({
|
||||
id="skill-debug-conversation-id"
|
||||
type="number"
|
||||
min={0}
|
||||
placeholder="可选,填已有会话 ID 以复用上下文"
|
||||
placeholder={t("skillDefinition.conversationPlaceholder")}
|
||||
aria-invalid={!!errors.conversationId}
|
||||
{...register("conversationId")}
|
||||
/>
|
||||
@@ -318,22 +326,22 @@ function DebugDialogBody({
|
||||
</FieldContent>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel>命中 Agent</FieldLabel>
|
||||
<FieldLabel>{t("skillDefinition.matchedAgent")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Input
|
||||
value={selectedAgent?.name || "未选择"}
|
||||
value={selectedAgent?.name || t("skillDefinition.noAgentSelected")}
|
||||
disabled
|
||||
/>
|
||||
</FieldContent>
|
||||
</Field>
|
||||
</div>
|
||||
<Field data-invalid={!!errors.userMessage}>
|
||||
<FieldLabel htmlFor="skill-debug-user-message">用户消息</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-debug-user-message">{t("skillDefinition.userMessage")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-debug-user-message"
|
||||
rows={5}
|
||||
placeholder="输入一段用户消息,调试当前 Skill 的路由、工具和回复。"
|
||||
placeholder={t("skillDefinition.userMessagePlaceholder")}
|
||||
aria-invalid={!!errors.userMessage}
|
||||
{...register("userMessage")}
|
||||
/>
|
||||
@@ -347,7 +355,7 @@ function DebugDialogBody({
|
||||
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm">调试摘要</CardTitle>
|
||||
<CardTitle className="text-sm">{t("skillDefinition.debugSummary")}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3 text-sm">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
@@ -359,38 +367,38 @@ function DebugDialogBody({
|
||||
<Badge variant="secondary">{result.interruptType}</Badge>
|
||||
) : null}
|
||||
{result?.interrupted ? (
|
||||
<Badge>已中断</Badge>
|
||||
<Badge>{t("skillDefinition.interrupted")}</Badge>
|
||||
) : (
|
||||
<Badge variant="outline">未中断</Badge>
|
||||
<Badge variant="outline">{t("skillDefinition.notInterrupted")}</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">Skill 名称</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.skillName")}</div>
|
||||
<div className="mt-1 font-medium">{result?.skillName || skillName}</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">Plan Reason</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{result?.planReason || "暂无"}
|
||||
{result?.planReason || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">Reply</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{result?.replyText || "暂无"}
|
||||
{result?.replyText || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">Checkpoint</div>
|
||||
<div className="mt-1 break-all">
|
||||
{result?.checkPointId || "暂无"}
|
||||
{result?.checkPointId || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">错误信息</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.errorMessage")}</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{result?.errorMessage || "暂无"}
|
||||
{result?.errorMessage || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -399,11 +407,11 @@ function DebugDialogBody({
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm">工具视图</CardTitle>
|
||||
<CardTitle className="text-sm">{t("skillDefinition.toolView")}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3 text-sm">
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">技能工具白名单</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.skillToolWhitelist")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{(result?.toolWhitelist ?? []).length > 0 ? (
|
||||
result?.toolWhitelist.map((toolCode) => (
|
||||
@@ -412,12 +420,12 @@ function DebugDialogBody({
|
||||
</Badge>
|
||||
))
|
||||
) : (
|
||||
<span className="text-muted-foreground">暂无</span>
|
||||
<span className="text-muted-foreground">{t("skillDefinition.none")}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">本轮实际暴露工具</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.exposedTools")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{(result?.exposedToolCodes ?? []).length > 0 ? (
|
||||
result?.exposedToolCodes.map((toolCode) => (
|
||||
@@ -426,12 +434,12 @@ function DebugDialogBody({
|
||||
</Badge>
|
||||
))
|
||||
) : (
|
||||
<span className="text-muted-foreground">暂无</span>
|
||||
<span className="text-muted-foreground">{t("skillDefinition.none")}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">本轮实际调用工具</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.invokedTools")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{(result?.invokedToolCodes ?? []).length > 0 ? (
|
||||
result?.invokedToolCodes.map((toolCode) => (
|
||||
@@ -440,7 +448,7 @@ function DebugDialogBody({
|
||||
</Badge>
|
||||
))
|
||||
) : (
|
||||
<span className="text-muted-foreground">暂无</span>
|
||||
<span className="text-muted-foreground">{t("skillDefinition.none")}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
@@ -449,29 +457,29 @@ function DebugDialogBody({
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4">
|
||||
<ResultBlock title="Skill Route Trace" value={result?.skillRouteTrace} />
|
||||
<ResultBlock title="Tool Search Trace" value={result?.toolSearchTrace} />
|
||||
<ResultBlock title="Graph Tool Trace" value={result?.graphToolTrace} />
|
||||
<ResultBlock title="Trace Data" value={result?.traceData} />
|
||||
<ResultBlock title="Skill Route Trace" value={result?.skillRouteTrace} emptyText={t("skillDefinition.emptyData")} />
|
||||
<ResultBlock title="Tool Search Trace" value={result?.toolSearchTrace} emptyText={t("skillDefinition.emptyData")} />
|
||||
<ResultBlock title="Graph Tool Trace" value={result?.graphToolTrace} emptyText={t("skillDefinition.emptyData")} />
|
||||
<ResultBlock title="Trace Data" value={result?.traceData} emptyText={t("skillDefinition.emptyData")} />
|
||||
</div>
|
||||
|
||||
{result?.interrupted && result.checkPointId ? (
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm">恢复调试</CardTitle>
|
||||
<CardTitle className="text-sm">{t("skillDefinition.resumeDebug")}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="rounded-lg bg-muted/50 p-3 text-sm">
|
||||
<div className="text-xs text-muted-foreground">当前 Checkpoint</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.currentCheckpoint")}</div>
|
||||
<div className="mt-1 break-all">{result.checkPointId}</div>
|
||||
</div>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="skill-debug-resume-message">恢复消息</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-debug-resume-message">{t("skillDefinition.resumeMessage")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-debug-resume-message"
|
||||
rows={3}
|
||||
placeholder="输入确认、取消或其他恢复消息"
|
||||
placeholder={t("skillDefinition.resumePlaceholder")}
|
||||
value={resumeMessage}
|
||||
onChange={(event) => setResumeMessage(event.target.value)}
|
||||
/>
|
||||
@@ -499,7 +507,7 @@ function DebugDialogBody({
|
||||
) : (
|
||||
<PlayIcon />
|
||||
)}
|
||||
{resuming ? "恢复中..." : "恢复调试"}
|
||||
{resuming ? t("skillDefinition.resuming") : t("skillDefinition.resumeDebugAction")}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
@@ -510,7 +518,7 @@ function DebugDialogBody({
|
||||
<div className="space-y-4">
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm">恢复结果</CardTitle>
|
||||
<CardTitle className="text-sm">{t("skillDefinition.resumeResult")}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3 text-sm">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
@@ -522,34 +530,34 @@ function DebugDialogBody({
|
||||
<Badge variant="secondary">{resumeResult.interruptType}</Badge>
|
||||
) : null}
|
||||
{resumeResult.interrupted ? (
|
||||
<Badge>仍在等待确认</Badge>
|
||||
<Badge>{t("skillDefinition.stillWaiting")}</Badge>
|
||||
) : (
|
||||
<Badge variant="outline">已恢复完成</Badge>
|
||||
<Badge variant="outline">{t("skillDefinition.resumeCompleted")}</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">恢复消息</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.resumeMessage")}</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{resumeMessage || "暂无"}
|
||||
{resumeMessage || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">恢复回复</div>
|
||||
<div className="text-xs text-muted-foreground">{t("skillDefinition.resumeReply")}</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{resumeResult.replyText || "暂无"}
|
||||
{resumeResult.replyText || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-lg bg-muted/50 p-3">
|
||||
<div className="text-xs text-muted-foreground">恢复 Plan Reason</div>
|
||||
<div className="text-xs text-muted-foreground">Resume Plan Reason</div>
|
||||
<div className="mt-1 whitespace-pre-wrap break-words">
|
||||
{resumeResult.planReason || "暂无"}
|
||||
{resumeResult.planReason || t("skillDefinition.none")}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<ResultBlock title="恢复 Tool Search Trace" value={resumeResult.toolSearchTrace} />
|
||||
<ResultBlock title="恢复 Graph Tool Trace" value={resumeResult.graphToolTrace} />
|
||||
<ResultBlock title="恢复 Trace Data" value={resumeResult.traceData} />
|
||||
<ResultBlock title="Resume Tool Search Trace" value={resumeResult.toolSearchTrace} emptyText={t("skillDefinition.emptyData")} />
|
||||
<ResultBlock title="Resume Graph Tool Trace" value={resumeResult.graphToolTrace} emptyText={t("skillDefinition.emptyData")} />
|
||||
<ResultBlock title="Resume Trace Data" value={resumeResult.traceData} emptyText={t("skillDefinition.emptyData")} />
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Resolver, useForm } from "react-hook-form";
|
||||
import { type Resolver, useForm } from "react-hook-form";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { OptionCombobox } from "@/components/option-combobox";
|
||||
@@ -23,7 +23,9 @@ import {
|
||||
type MCPToolCatalogItem,
|
||||
type SkillDefinition,
|
||||
} from "@/lib/api/admin";
|
||||
import { useI18n } from "@/i18n/provider";
|
||||
|
||||
type TFunction = (key: string, values?: Record<string, string | number>) => string;
|
||||
|
||||
type SkillEditDialogProps = {
|
||||
open: boolean;
|
||||
@@ -42,25 +44,29 @@ const emptyForm: EditForm = {
|
||||
remark: "",
|
||||
};
|
||||
|
||||
const skillFormSchema = z.object({
|
||||
function createSkillFormSchema(t: TFunction) {
|
||||
return z.object({
|
||||
code: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Skill 编码不能为空")
|
||||
.regex(/^[a-zA-Z0-9_-]+$/, "Skill 编码仅支持字母、数字、下划线和中划线"),
|
||||
name: z.string().trim().min(1, "Skill 名称不能为空"),
|
||||
.min(1, t("skillDefinition.codeRequired"))
|
||||
.regex(/^[a-zA-Z0-9_-]+$/, t("skillDefinition.codeInvalid")),
|
||||
name: z.string().trim().min(1, t("skillDefinition.nameRequired")),
|
||||
description: z.string().trim(),
|
||||
instruction: z.string().trim().min(1, "技能说明不能为空"),
|
||||
instruction: z.string().trim().min(1, t("skillDefinition.instructionRequired")),
|
||||
examplesText: z.string().trim(),
|
||||
remark: z.string().trim(),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
type EditForm = z.infer<typeof skillFormSchema>;
|
||||
const editFormResolver = zodResolver(skillFormSchema as never) as Resolver<
|
||||
z.input<typeof skillFormSchema>,
|
||||
undefined,
|
||||
z.output<typeof skillFormSchema>
|
||||
>;
|
||||
type EditForm = {
|
||||
code: string;
|
||||
name: string;
|
||||
description: string;
|
||||
instruction: string;
|
||||
examplesText: string;
|
||||
remark: string;
|
||||
};
|
||||
|
||||
function buildForm(item: SkillDefinition | null): EditForm {
|
||||
if (!item) {
|
||||
@@ -127,6 +133,7 @@ function SkillEditDialogBody({
|
||||
onOpenChange,
|
||||
onSubmit,
|
||||
}: SkillEditDialogBodyProps) {
|
||||
const t = useI18n();
|
||||
const formId = "skill-definition-edit-form";
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [toolCatalog, setToolCatalog] = useState<MCPToolCatalogItem[]>([]);
|
||||
@@ -134,11 +141,12 @@ function SkillEditDialogBody({
|
||||
string[]
|
||||
>([]);
|
||||
const [toolCodeToAdd, setToolCodeToAdd] = useState("");
|
||||
const form = useForm<
|
||||
z.input<typeof skillFormSchema>,
|
||||
undefined,
|
||||
z.output<typeof skillFormSchema>
|
||||
>({
|
||||
const skillFormSchema = useMemo(() => createSkillFormSchema(t), [t]);
|
||||
const editFormResolver = useMemo(
|
||||
() => zodResolver(skillFormSchema) as Resolver<EditForm>,
|
||||
[skillFormSchema],
|
||||
);
|
||||
const form = useForm<EditForm>({
|
||||
resolver: editFormResolver,
|
||||
defaultValues: emptyForm,
|
||||
});
|
||||
@@ -237,7 +245,7 @@ function SkillEditDialogBody({
|
||||
<ProjectDialog
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title={itemId ? "编辑" : "新建"}
|
||||
title={itemId ? t("skillDefinition.editTitle") : t("skillDefinition.createTitle")}
|
||||
size="xl"
|
||||
allowFullscreen
|
||||
footer={
|
||||
@@ -248,17 +256,17 @@ function SkillEditDialogBody({
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={saving}
|
||||
>
|
||||
取消
|
||||
{t("skillDefinition.cancel")}
|
||||
</Button>
|
||||
<Button type="submit" form={formId} disabled={saving || loading}>
|
||||
{saving ? "保存中..." : itemId ? "保存" : "创建"}
|
||||
{saving ? t("skillDefinition.saving") : itemId ? t("skillDefinition.save") : t("skillDefinition.create")}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
{loading ? (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<div className="text-muted-foreground">加载中...</div>
|
||||
<div className="text-muted-foreground">{t("skillDefinition.loading")}</div>
|
||||
</div>
|
||||
) : (
|
||||
<form
|
||||
@@ -268,11 +276,11 @@ function SkillEditDialogBody({
|
||||
>
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<Field data-invalid={!!errors.code}>
|
||||
<FieldLabel htmlFor="skill-code">编码</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-code">{t("skillDefinition.code")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Input
|
||||
id="skill-code"
|
||||
placeholder="例如:refund_skill"
|
||||
placeholder={t("skillDefinition.codePlaceholder")}
|
||||
aria-invalid={!!errors.code}
|
||||
{...register("code")}
|
||||
/>
|
||||
@@ -280,11 +288,11 @@ function SkillEditDialogBody({
|
||||
</FieldContent>
|
||||
</Field>
|
||||
<Field data-invalid={!!errors.name}>
|
||||
<FieldLabel htmlFor="skill-name">名称</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-name">{t("skillDefinition.name")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Input
|
||||
id="skill-name"
|
||||
placeholder="例如:退款处理"
|
||||
placeholder={t("skillDefinition.namePlaceholder")}
|
||||
aria-invalid={!!errors.name}
|
||||
{...register("name")}
|
||||
/>
|
||||
@@ -294,12 +302,12 @@ function SkillEditDialogBody({
|
||||
</div>
|
||||
|
||||
<Field data-invalid={!!errors.description}>
|
||||
<FieldLabel htmlFor="skill-description">描述</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-description">{t("skillDefinition.description")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-description"
|
||||
rows={3}
|
||||
placeholder="描述这个 Skill 的用途、边界和适用场景"
|
||||
placeholder={t("skillDefinition.descriptionPlaceholder")}
|
||||
aria-invalid={!!errors.description}
|
||||
{...register("description")}
|
||||
/>
|
||||
@@ -308,12 +316,12 @@ function SkillEditDialogBody({
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.instruction}>
|
||||
<FieldLabel htmlFor="skill-instruction">技能说明</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-instruction">{t("skillDefinition.instruction")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-instruction"
|
||||
rows={12}
|
||||
placeholder="请输入 Skill 文档内容,描述目标、步骤、工具使用规则和边界。"
|
||||
placeholder={t("skillDefinition.instructionPlaceholder")}
|
||||
aria-invalid={!!errors.instruction}
|
||||
{...register("instruction")}
|
||||
/>
|
||||
@@ -322,12 +330,12 @@ function SkillEditDialogBody({
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.examplesText}>
|
||||
<FieldLabel htmlFor="skill-examples">示例问法</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-examples">{t("skillDefinition.examples")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-examples"
|
||||
rows={5}
|
||||
placeholder={"每行一个典型用户问法,例如:\n我要申请退款\n帮我查下订单"}
|
||||
placeholder={t("skillDefinition.examplesPlaceholder")}
|
||||
aria-invalid={!!errors.examplesText}
|
||||
{...register("examplesText")}
|
||||
/>
|
||||
@@ -336,16 +344,16 @@ function SkillEditDialogBody({
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel>工具白名单</FieldLabel>
|
||||
<FieldLabel>{t("skillDefinition.toolWhitelist")}</FieldLabel>
|
||||
<FieldContent className="space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex-1">
|
||||
<OptionCombobox
|
||||
value={toolCodeToAdd}
|
||||
options={addableToolOptions}
|
||||
placeholder="选择该 Skill 允许使用的工具"
|
||||
searchPlaceholder="搜索 toolCode 或工具名"
|
||||
emptyText="没有可添加的工具"
|
||||
placeholder={t("skillDefinition.selectTool")}
|
||||
searchPlaceholder={t("skillDefinition.searchTool")}
|
||||
emptyText={t("skillDefinition.emptyTool")}
|
||||
onChange={handleAddToolWhitelist}
|
||||
/>
|
||||
</div>
|
||||
@@ -355,13 +363,13 @@ function SkillEditDialogBody({
|
||||
disabled={!toolCodeToAdd}
|
||||
onClick={() => handleAddToolWhitelist(toolCodeToAdd)}
|
||||
>
|
||||
添加
|
||||
{t("skillDefinition.add")}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{selectedToolOptions.length === 0 ? (
|
||||
<span className="text-sm text-muted-foreground">
|
||||
不限制时,Skill 会继承 Agent 的可用工具范围。
|
||||
{t("skillDefinition.inheritAgentTools")}
|
||||
</span>
|
||||
) : (
|
||||
selectedToolOptions.map((option) => (
|
||||
@@ -382,12 +390,12 @@ function SkillEditDialogBody({
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.remark}>
|
||||
<FieldLabel htmlFor="skill-remark">备注</FieldLabel>
|
||||
<FieldLabel htmlFor="skill-remark">{t("skillDefinition.remark")}</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="skill-remark"
|
||||
rows={3}
|
||||
placeholder="记录内部备注或维护说明"
|
||||
placeholder={t("skillDefinition.remarkPlaceholder")}
|
||||
aria-invalid={!!errors.remark}
|
||||
{...register("remark")}
|
||||
/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useCallback, useEffect, useState } from "react"
|
||||
import { useCallback, useEffect, useMemo, useState } from "react"
|
||||
import {
|
||||
BrainCircuitIcon,
|
||||
BugIcon,
|
||||
@@ -51,19 +51,35 @@ import {
|
||||
type PageResult,
|
||||
type SkillDefinition,
|
||||
} from "@/lib/api/admin"
|
||||
import { Status, StatusLabels } from "@/lib/generated/enums"
|
||||
import { getEnumLabel, getEnumOptions } from "@/lib/enums"
|
||||
import { useI18n } from "@/i18n/provider"
|
||||
import { Status } from "@/lib/generated/enums"
|
||||
import { formatDateTime } from "@/lib/utils"
|
||||
import { EditDialog } from "./_components/edit"
|
||||
import { DebugDialog } from "./_components/debug-dialog"
|
||||
|
||||
const statusFilterOptions = [
|
||||
{ value: "all", label: "全部状态" },
|
||||
...getEnumOptions(StatusLabels).map((option) => ({
|
||||
value: String(option.value),
|
||||
label: option.label,
|
||||
})),
|
||||
]
|
||||
type TFunction = (key: string, values?: Record<string, string | number>) => string
|
||||
|
||||
function statusLabel(status: number, t: TFunction) {
|
||||
if (status === Status.Ok) {
|
||||
return t("skillDefinition.statusOk")
|
||||
}
|
||||
if (status === Status.Disabled) {
|
||||
return t("skillDefinition.statusDisabled")
|
||||
}
|
||||
if (status === Status.Deleted) {
|
||||
return t("skillDefinition.statusDeleted")
|
||||
}
|
||||
return String(status)
|
||||
}
|
||||
|
||||
function getStatusFilterOptions(t: TFunction) {
|
||||
return [
|
||||
{ value: "all", label: t("skillDefinition.allStatus") },
|
||||
{ value: String(Status.Ok), label: t("skillDefinition.statusOk") },
|
||||
{ value: String(Status.Disabled), label: t("skillDefinition.statusDisabled") },
|
||||
{ value: String(Status.Deleted), label: t("skillDefinition.statusDeleted") },
|
||||
]
|
||||
}
|
||||
|
||||
type SkillRowProps = {
|
||||
item: SkillDefinition
|
||||
@@ -73,6 +89,7 @@ type SkillRowProps = {
|
||||
handleToggleStatus: (item: SkillDefinition) => void
|
||||
handleDelete: (item: SkillDefinition) => void
|
||||
handleRestore: (item: SkillDefinition) => void
|
||||
t: TFunction
|
||||
}
|
||||
|
||||
function SkillRow({
|
||||
@@ -83,6 +100,7 @@ function SkillRow({
|
||||
handleToggleStatus,
|
||||
handleDelete,
|
||||
handleRestore,
|
||||
t,
|
||||
}: SkillRowProps) {
|
||||
const isDeleted = item.status === Status.Deleted
|
||||
const statusBadgeVariant = isDeleted
|
||||
@@ -102,12 +120,12 @@ function SkillRow({
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<div className="font-medium">{item.name}</div>
|
||||
<Badge variant="outline">{item.code}</Badge>
|
||||
<Badge variant="secondary">白名单 {item.toolWhitelist.length}</Badge>
|
||||
<Badge variant="secondary">示例 {item.examples.length}</Badge>
|
||||
<Badge variant="secondary">{t("skillDefinition.whitelistCount", { count: item.toolWhitelist.length })}</Badge>
|
||||
<Badge variant="secondary">{t("skillDefinition.exampleCount", { count: item.examples.length })}</Badge>
|
||||
</div>
|
||||
<div className="mt-2 space-y-2">
|
||||
<div className="line-clamp-2 text-sm leading-6 text-muted-foreground">
|
||||
{item.description || "暂无描述"}
|
||||
{item.description || t("skillDefinition.noDescription")}
|
||||
</div>
|
||||
</div>
|
||||
{item.toolWhitelist.length > 0 ? (
|
||||
@@ -131,10 +149,10 @@ function SkillRow({
|
||||
checked={item.status === Status.Ok}
|
||||
disabled={actionLoadingId === item.id || isDeleted}
|
||||
onCheckedChange={() => void handleToggleStatus(item)}
|
||||
aria-label={`${item.name} 状态切换`}
|
||||
aria-label={t("skillDefinition.toggleStatus", { name: item.name })}
|
||||
/>
|
||||
<Badge variant={statusBadgeVariant}>
|
||||
{getEnumLabel(StatusLabels, item.status as keyof typeof StatusLabels)}
|
||||
{statusLabel(item.status, t)}
|
||||
</Badge>
|
||||
</div>
|
||||
</TableCell>
|
||||
@@ -150,15 +168,15 @@ function SkillRow({
|
||||
<ButtonGroup className="ml-auto">
|
||||
<Button variant="outline" size="sm" onClick={() => openDebugDialog(item)}>
|
||||
<BugIcon />
|
||||
调试
|
||||
{t("skillDefinition.debug")}
|
||||
</Button>
|
||||
<Button variant="outline" size="sm" onClick={() => openEditDialog(item)}>
|
||||
编辑
|
||||
{t("skillDefinition.edit")}
|
||||
</Button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger
|
||||
render={<Button variant="outline" size="icon-sm" />}
|
||||
aria-label={`更多操作 ${item.name}`}
|
||||
aria-label={t("skillDefinition.moreActions", { name: item.name })}
|
||||
>
|
||||
<MoreHorizontalIcon />
|
||||
</DropdownMenuTrigger>
|
||||
@@ -169,7 +187,7 @@ function SkillRow({
|
||||
onClick={() => void handleRestore(item)}
|
||||
>
|
||||
<RotateCcwIcon />
|
||||
{actionLoadingId === item.id ? "恢复中..." : "恢复"}
|
||||
{actionLoadingId === item.id ? t("skillDefinition.restoring") : t("skillDefinition.restore")}
|
||||
</DropdownMenuItem>
|
||||
) : (
|
||||
<DropdownMenuItem
|
||||
@@ -178,7 +196,7 @@ function SkillRow({
|
||||
className="text-destructive focus:text-destructive"
|
||||
>
|
||||
<Trash2Icon />
|
||||
{actionLoadingId === item.id ? "删除中..." : "删除"}
|
||||
{actionLoadingId === item.id ? t("skillDefinition.deleting") : t("skillDefinition.delete")}
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
@@ -190,6 +208,7 @@ function SkillRow({
|
||||
}
|
||||
|
||||
export default function DashboardSkillsPage() {
|
||||
const t = useI18n()
|
||||
const [nameInput, setNameInput] = useState("")
|
||||
const [codeInput, setCodeInput] = useState("")
|
||||
const [statusFilterInput, setStatusFilterInput] = useState("all")
|
||||
@@ -209,6 +228,7 @@ export default function DashboardSkillsPage() {
|
||||
results: [],
|
||||
page: { page: 1, limit: 20, total: 0 },
|
||||
})
|
||||
const statusFilterOptions = useMemo(() => getStatusFilterOptions(t), [t])
|
||||
|
||||
const loadData = useCallback(async () => {
|
||||
setLoading(true)
|
||||
@@ -222,11 +242,11 @@ export default function DashboardSkillsPage() {
|
||||
})
|
||||
setResult(data)
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "加载 Skills 失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.loadFailed"))
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [name, code, statusFilter, page, limit])
|
||||
}, [name, code, statusFilter, page, limit, t])
|
||||
|
||||
useEffect(() => {
|
||||
void loadData()
|
||||
@@ -298,16 +318,16 @@ export default function DashboardSkillsPage() {
|
||||
id: editingItem.id,
|
||||
...payload,
|
||||
})
|
||||
toast.success(`已更新 Skill:${editingItem.name}`)
|
||||
toast.success(t("skillDefinition.updated", { name: editingItem.name }))
|
||||
} else {
|
||||
await createSkillDefinition(payload)
|
||||
toast.success(`已创建 Skill:${payload.name}`)
|
||||
toast.success(t("skillDefinition.created", { name: payload.name }))
|
||||
}
|
||||
setDialogOpen(false)
|
||||
setEditingItem(null)
|
||||
await loadData()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "保存 Skill 失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.saveFailed"))
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
@@ -323,10 +343,10 @@ export default function DashboardSkillsPage() {
|
||||
setActionLoadingId(item.id)
|
||||
try {
|
||||
await updateSkillDefinitionStatus(item.id, nextStatus)
|
||||
toast.success(`已${nextStatus === Status.Ok ? "启用" : "停用"}:${item.name}`)
|
||||
toast.success(t(nextStatus === Status.Ok ? "skillDefinition.enabled" : "skillDefinition.disabled", { name: item.name }))
|
||||
await loadData()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "更新状态失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.statusUpdateFailed"))
|
||||
} finally {
|
||||
setActionLoadingId(null)
|
||||
}
|
||||
@@ -340,10 +360,10 @@ export default function DashboardSkillsPage() {
|
||||
setActionLoadingId(item.id)
|
||||
try {
|
||||
await deleteSkillDefinition(item.id)
|
||||
toast.success(`已删除 Skill:${item.name}`)
|
||||
toast.success(t("skillDefinition.deleted", { name: item.name }))
|
||||
await loadData()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "删除 Skill 失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.deleteFailed"))
|
||||
} finally {
|
||||
setActionLoadingId(null)
|
||||
}
|
||||
@@ -357,10 +377,10 @@ export default function DashboardSkillsPage() {
|
||||
setActionLoadingId(item.id)
|
||||
try {
|
||||
await restoreSkillDefinition(item.id)
|
||||
toast.success(`已恢复 Skill:${item.name}`)
|
||||
toast.success(t("skillDefinition.restored", { name: item.name }))
|
||||
await loadData()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "恢复 Skill 失败")
|
||||
toast.error(error instanceof Error ? error.message : t("skillDefinition.restoreFailed"))
|
||||
} finally {
|
||||
setActionLoadingId(null)
|
||||
}
|
||||
@@ -374,11 +394,11 @@ export default function DashboardSkillsPage() {
|
||||
<>
|
||||
<Button variant="outline" onClick={() => void loadData()} disabled={loading}>
|
||||
<RefreshCwIcon className={loading ? "animate-spin" : ""} />
|
||||
刷新
|
||||
{t("skillDefinition.refresh")}
|
||||
</Button>
|
||||
<Button onClick={openCreateDialog}>
|
||||
<PlusIcon />
|
||||
新建
|
||||
{t("skillDefinition.new")}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
@@ -389,7 +409,7 @@ export default function DashboardSkillsPage() {
|
||||
value={nameInput}
|
||||
onChange={(event) => setNameInput(event.target.value)}
|
||||
onKeyDown={handleFilterKeyDown}
|
||||
placeholder="按名称筛选"
|
||||
placeholder={t("skillDefinition.filterName")}
|
||||
className="pl-9"
|
||||
/>
|
||||
</div>
|
||||
@@ -397,22 +417,22 @@ export default function DashboardSkillsPage() {
|
||||
value={codeInput}
|
||||
onChange={(event) => setCodeInput(event.target.value)}
|
||||
onKeyDown={handleFilterKeyDown}
|
||||
placeholder="按编码筛选"
|
||||
placeholder={t("skillDefinition.filterCode")}
|
||||
className="w-full sm:w-56"
|
||||
/>
|
||||
<div className="w-full sm:w-36">
|
||||
<OptionCombobox
|
||||
value={statusFilterInput}
|
||||
options={statusFilterOptions}
|
||||
placeholder="全部状态"
|
||||
searchPlaceholder="搜索状态"
|
||||
emptyText="未找到状态"
|
||||
placeholder={t("skillDefinition.allStatus")}
|
||||
searchPlaceholder={t("skillDefinition.searchStatus")}
|
||||
emptyText={t("skillDefinition.emptyStatus")}
|
||||
onChange={setStatusFilterInput}
|
||||
/>
|
||||
</div>
|
||||
<Button variant="outline" onClick={applyFilters} disabled={loading}>
|
||||
<SearchIcon />
|
||||
查询
|
||||
{t("skillDefinition.query")}
|
||||
</Button>
|
||||
</DashboardToolbar>
|
||||
|
||||
@@ -435,9 +455,9 @@ export default function DashboardSkillsPage() {
|
||||
<TableHeader className="bg-muted/40">
|
||||
<TableRow>
|
||||
<TableHead>Skill</TableHead>
|
||||
<TableHead>状态</TableHead>
|
||||
<TableHead>最近更新</TableHead>
|
||||
<TableHead className="w-[168px] text-right">操作</TableHead>
|
||||
<TableHead>{t("skillDefinition.status")}</TableHead>
|
||||
<TableHead>{t("skillDefinition.updatedAt")}</TableHead>
|
||||
<TableHead className="w-[168px] text-right">{t("skillDefinition.actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
@@ -445,8 +465,8 @@ export default function DashboardSkillsPage() {
|
||||
<DashboardTableStateRow
|
||||
colSpan={4}
|
||||
loading={loading}
|
||||
loadingText="正在加载 Skill..."
|
||||
emptyText="没有匹配的 Skill"
|
||||
loadingText={t("skillDefinition.loadingRows")}
|
||||
emptyText={t("skillDefinition.emptyRows")}
|
||||
/>
|
||||
) : null}
|
||||
{result.results.map((item) => (
|
||||
@@ -459,6 +479,7 @@ export default function DashboardSkillsPage() {
|
||||
handleToggleStatus={handleToggleStatus}
|
||||
handleDelete={handleDelete}
|
||||
handleRestore={handleRestore}
|
||||
t={t}
|
||||
/>
|
||||
))}
|
||||
</TableBody>
|
||||
|
||||
Reference in New Issue
Block a user