"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { ResponsiveLayout } from "@/components/layout"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { fetchPrompts, updatePrompt, type PromptItem } from "@/features/agent/services/promptsApi"; import { toast } from "@/hooks/useToast"; import type { I18nKey } from "@/lib/i18n/dict"; import { useI18n } from "@/lib/i18n/provider"; const PROMPT_HINT_KEYS: Partial> = { rag_prompt: "agent.prompts.hint.rag_prompt", rag_prompt_with_web_optional: "agent.prompts.hint.rag_prompt_with_web_optional", no_kb_prompt: "agent.prompts.hint.no_kb_prompt", web_search_result_prompt: "agent.prompts.hint.web_search_result_prompt", no_source_reply: "agent.prompts.hint.no_source_reply", ai_fail_reply: "agent.prompts.hint.ai_fail_reply", }; const PROMPT_USAGE_KEYS: Partial> = { rag_prompt: "agent.prompts.usage.rag_prompt", rag_prompt_with_web_optional: "agent.prompts.usage.rag_prompt_with_web_optional", no_kb_prompt: "agent.prompts.usage.no_kb_prompt", web_search_result_prompt: "agent.prompts.usage.web_search_result_prompt", no_source_reply: "agent.prompts.usage.no_source_reply", ai_fail_reply: "agent.prompts.usage.ai_fail_reply", }; function getTextareaMinHeight(key: string): string { return key === "no_source_reply" || key === "ai_fail_reply" ? "min-h-[80px]" : "min-h-[200px]"; } export default function PromptsPage({ embedded = false }: { embedded?: boolean }) { const router = useRouter(); const { t } = useI18n(); const [userId, setUserId] = useState(null); const [prompts, setPrompts] = useState([]); const [loading, setLoading] = useState(true); const [savingKey, setSavingKey] = useState(null); const [error, setError] = useState(""); useEffect(() => { const storedUserId = localStorage.getItem("agent_user_id"); if (!storedUserId) { router.push("/"); return; } setUserId(Number.parseInt(storedUserId, 10)); }, [router]); const loadPrompts = async () => { if (!userId) return; try { setLoading(true); setError(""); const data = await fetchPrompts(userId); setPrompts(data); } catch (e) { console.error("加载提示词失败:", e); setError((e as Error).message || t("agent.prompts.loadFailed")); } finally { setLoading(false); } }; useEffect(() => { if (userId) loadPrompts(); }, [userId]); const handleSave = async (key: string, content: string) => { if (!userId) return; setSavingKey(key); try { await updatePrompt(userId, key, content); toast.success(t("agent.prompts.saveSuccess")); await loadPrompts(); } catch (e) { toast.error((e as Error).message || t("agent.prompts.saveFailed")); } finally { setSavingKey(null); } }; const handleContentChange = (key: string, content: string) => { setPrompts((prev) => prev.map((p) => (p.key === key ? { ...p, content } : p)) ); }; if (!userId) return null; const headerContent = (

{t("agent.prompts.title")}

{t("agent.prompts.subtitle")}
{!embedded && ( )}
); const mainContent = (
{error && (
{error}
)} {loading ? (
{t("common.loading")}
) : ( prompts.map((item) => { const usageKey = PROMPT_USAGE_KEYS[item.key]; const hintKey = PROMPT_HINT_KEYS[item.key] ?? "agent.prompts.hint.default"; return ( {item.name} {usageKey && (

{t("agent.prompts.usageLabel")} {t(usageKey)}

)}

{t(hintKey)}