refactor: support i18n

This commit is contained in:
mlogclub
2026-05-25 12:06:15 +08:00
parent 309ac1fe9e
commit 988f55c80d
179 changed files with 10968 additions and 3763 deletions
@@ -17,12 +17,15 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Textarea } from "@/components/ui/textarea";
import { useI18n } from "@/i18n/provider";
import { getKnowledgeAnswerStatusLabel } from "@/lib/knowledge-i18n";
type DebugPanelProps = {
knowledgeBaseId: number | null;
};
export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
const t = useI18n();
const [question, setQuestion] = useState("");
const [topK, setTopK] = useState("5");
const [scoreThreshold, setScoreThreshold] = useState("0.2");
@@ -34,11 +37,11 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
async function handleSearch() {
if (!knowledgeBaseId) {
toast.error("请先选择知识库");
toast.error(t("knowledge.selectKnowledgeBaseFirst"));
return;
}
if (!question.trim()) {
toast.error("请输入调试问题");
toast.error(t("knowledge.debugQuestionRequired"));
return;
}
@@ -52,9 +55,9 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
rerankLimit: Number(rerankLimit) || undefined,
});
setSearchResult(data);
toast.success(`检索完成,命中 ${data.hitCount}`);
toast.success(t("knowledge.searchCompleted", { count: data.hitCount }));
} catch (error) {
toast.error(error instanceof Error ? error.message : "检索失败");
toast.error(error instanceof Error ? error.message : t("knowledge.searchFailed"));
} finally {
setSearching(false);
}
@@ -62,11 +65,11 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
async function handleAnswer() {
if (!knowledgeBaseId) {
toast.error("请先选择知识库");
toast.error(t("knowledge.selectKnowledgeBaseFirst"));
return;
}
if (!question.trim()) {
toast.error("请输入调试问题");
toast.error(t("knowledge.debugQuestionRequired"));
return;
}
@@ -80,9 +83,11 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
rerankLimit: Number(rerankLimit) || undefined,
});
setAnswerResult(data);
toast.success(`问答完成,状态:${data.answerStatusName}`);
toast.success(t("knowledge.answerCompleted", {
status: getKnowledgeAnswerStatusLabel(data.answerStatus, data.answerStatusName, t),
}));
} catch (error) {
toast.error(error instanceof Error ? error.message : "问答失败");
toast.error(error instanceof Error ? error.message : t("knowledge.answerFailed"));
} finally {
setAnswering(false);
}
@@ -94,32 +99,32 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
<Textarea
value={question}
onChange={(event) => setQuestion(event.target.value)}
placeholder="输入问题,测试知识库召回和回答效果"
placeholder={t("knowledge.debugQuestionPlaceholder")}
rows={5}
className="text-sm"
/>
<div className="grid grid-cols-3 gap-3">
<div className="space-y-1.5">
<Label htmlFor="topk" className="text-xs">TopK</Label>
<Input id="topk" value={topK} onChange={(event) => setTopK(event.target.value)} placeholder="召回数量" className="h-8" />
<Input id="topk" value={topK} onChange={(event) => setTopK(event.target.value)} placeholder={t("knowledge.topKPlaceholder")} className="h-8" />
</div>
<div className="space-y-1.5">
<Label htmlFor="threshold" className="text-xs"></Label>
<Input id="threshold" value={scoreThreshold} onChange={(event) => setScoreThreshold(event.target.value)} placeholder="最低分数" className="h-8" />
<Label htmlFor="threshold" className="text-xs">{t("knowledge.scoreThreshold")}</Label>
<Input id="threshold" value={scoreThreshold} onChange={(event) => setScoreThreshold(event.target.value)} placeholder={t("knowledge.scoreThresholdPlaceholder")} className="h-8" />
</div>
<div className="space-y-1.5">
<Label htmlFor="rerank" className="text-xs"></Label>
<Input id="rerank" value={rerankLimit} onChange={(event) => setRerankLimit(event.target.value)} placeholder="重排条数" className="h-8" />
<Label htmlFor="rerank" className="text-xs">{t("knowledge.rerankLimitLabel")}</Label>
<Input id="rerank" value={rerankLimit} onChange={(event) => setRerankLimit(event.target.value)} placeholder={t("knowledge.rerankLimitPlaceholder")} className="h-8" />
</div>
</div>
<div className="flex gap-2">
<Button className="flex-1" variant="outline" onClick={() => void handleSearch()} disabled={searching}>
<SearchIcon className="mr-2 size-4" />
{searching ? "检索中..." : "调试检索"}
{searching ? t("knowledge.searching") : t("knowledge.debugSearch")}
</Button>
<Button className="flex-1" onClick={() => void handleAnswer()} disabled={answering}>
<SparklesIcon className="mr-2 size-4" />
{answering ? "生成中..." : "调试问答"}
{answering ? t("knowledge.generating") : t("knowledge.debugAnswer")}
</Button>
</div>
</div>
@@ -131,12 +136,14 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
<CardHeader className="pb-2">
<CardTitle className="flex items-center gap-2 text-sm">
<BotIcon className="size-4" />
{t("knowledge.answerResult")}
</CardTitle>
</CardHeader>
<CardContent className="space-y-3 text-sm">
<div className="flex flex-wrap items-center gap-2">
<Badge variant="secondary">{answerResult.answerStatusName}</Badge>
<Badge variant="secondary">
{getKnowledgeAnswerStatusLabel(answerResult.answerStatus, answerResult.answerStatusName, t)}
</Badge>
<span className="text-xs text-muted-foreground">
{answerResult.latencyMs}ms · {answerResult.modelName || "fallback"}
</span>
@@ -146,7 +153,7 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
</div>
{answerResult.citations.length > 0 ? (
<div className="space-y-2">
<div className="text-xs font-medium text-muted-foreground"></div>
<div className="text-xs font-medium text-muted-foreground">{t("knowledge.citationSources")}</div>
{answerResult.citations.map((citation) => (
<div
key={`${citation.documentId}-${citation.chunkNo}-${citation.sectionPath}`}
@@ -175,11 +182,11 @@ export function DebugPanel({ knowledgeBaseId }: DebugPanelProps) {
{searchResult ? (
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm"></CardTitle>
<CardTitle className="text-sm">{t("knowledge.searchHits")}</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="text-xs text-muted-foreground">
{searchResult.hitCount} · {searchResult.latencyMs}ms
{t("knowledge.hitsSummary", { count: searchResult.hitCount, latency: searchResult.latencyMs })}
</div>
{searchResult.results.map((item) => (
<div key={`${item.chunkId}-${item.documentId}`} className="rounded-md border bg-background p-3">
@@ -221,5 +228,5 @@ function getSearchResultLabel(item: {
if (item.faqId && item.faqId > 0) {
return `FAQ ${item.faqId}`
}
return `文档 ${item.documentId ?? 0}`
return `Document ${item.documentId ?? 0}`
}