From f2d7037ca2d3d657441aa88d2d2a39270621b079 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 8 May 2026 20:15:54 +0800 Subject: [PATCH] feat: add save feedback prompts, featured agent fields, dynamic Dify API config, and chat improvements - Add success/error feedback messages near submit buttons in all admin forms - Display success prompt for 1s before redirect after save - Show API error details on save failure - Add isFeatured/featuredOrder fields to Agent model and admin UI - Add difyApiUrl/difyApiKey fields for per-agent Dify API configuration - Show featured badge column in agent admin list - Display featured agents on homepage sorted by order - Refactor chat page streaming with AbortController and stable userId - Improve Dify API proxy to use per-agent credentials --- app/admin/agents/AgentForm.tsx | 85 +++++++++++++++++- app/admin/agents/[id]/edit/page.tsx | 4 + app/admin/agents/page.tsx | 10 +++ app/admin/categories/CategoryForm.tsx | 20 ++++- app/admin/news/NewsForm.tsx | 20 ++++- app/agents/[slug]/chat/page.tsx | 84 ++++++++--------- app/api/admin/agents/[id]/route.ts | 4 + app/api/admin/agents/route.ts | 4 + app/api/chat/route.ts | 56 ++++++++---- app/page.tsx | 2 + package.json | 3 + prisma/dev.db | Bin 86016 -> 73728 bytes .../migration.sql | 29 ++++++ prisma/schema.prisma | 4 + prisma/seed.ts | 61 ++++++++----- 测试/测试1.txt | 14 --- 16 files changed, 298 insertions(+), 102 deletions(-) create mode 100644 prisma/migrations/20260508102405_add_featured_fields/migration.sql delete mode 100644 测试/测试1.txt diff --git a/app/admin/agents/AgentForm.tsx b/app/admin/agents/AgentForm.tsx index e9e4222..753d784 100644 --- a/app/admin/agents/AgentForm.tsx +++ b/app/admin/agents/AgentForm.tsx @@ -18,7 +18,11 @@ export default function AgentForm({ features?: string hotQuestions?: string quickQuestions?: string + difyApiUrl?: string + difyApiKey?: string status?: string + isFeatured?: boolean + featuredOrder?: number } }) { const router = useRouter() @@ -31,10 +35,15 @@ export default function AgentForm({ features: agent?.features || "", hotQuestions: agent?.hotQuestions ? JSON.parse(agent.hotQuestions).join('\n') : '', quickQuestions: agent?.quickQuestions ? JSON.parse(agent.quickQuestions).join('\n') : '', + difyApiUrl: agent?.difyApiUrl || "", + difyApiKey: agent?.difyApiKey || "", status: agent?.status || "active", + isFeatured: agent?.isFeatured ?? false, + featuredOrder: agent?.featuredOrder ?? 0, }) const [loading, setLoading] = useState(false) const [error, setError] = useState("") + const [success, setSuccess] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() @@ -50,6 +59,8 @@ export default function AgentForm({ const body = { ...formData, + isFeatured: formData.isFeatured, + featuredOrder: formData.featuredOrder, hotQuestions: JSON.stringify(formData.hotQuestions.split('\n').filter(q => q.trim())), quickQuestions: JSON.stringify(formData.quickQuestions.split('\n').filter(q => q.trim())), } @@ -61,9 +72,12 @@ export default function AgentForm({ }) if (res.ok) { + setSuccess(true) + await new Promise(r => setTimeout(r, 1000)) router.push("/admin/agents") } else { - setError("保存失败") + const data = await res.json().catch(() => ({})) + setError(data.error || "保存失败") } } catch (err) { setError("保存失败,请稍后重试") @@ -185,6 +199,34 @@ export default function AgentForm({ /> +
+

热门智能体配置

+
+ +
+ + setFormData({ ...formData, featuredOrder: parseInt(e.target.value) || 0 })} + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" + placeholder="0" + /> +
+
+
+
+
+
+ + setFormData({ ...formData, difyApiUrl: e.target.value })} + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" + placeholder="https://api.dify.ai/v1" + /> +
+
+ + setFormData({ ...formData, difyApiKey: e.target.value })} + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" + placeholder="app-xxxxxxxxxxxx" + /> +
+
+ + {success && ( +
+ 保存成功 +
+ )} + + {error && ( +
+ {error} +
+ )} +
diff --git a/app/admin/agents/page.tsx b/app/admin/agents/page.tsx index be4ffd7..503d119 100644 --- a/app/admin/agents/page.tsx +++ b/app/admin/agents/page.tsx @@ -39,6 +39,7 @@ export default async function AdminAgentsPage() { 名称 分类 + 热门 状态 使用次数 操作 @@ -59,6 +60,15 @@ export default async function AdminAgentsPage() { {agent.category?.name || "-"} + + {agent.isFeatured ? ( + + 热门 + + ) : ( + - + )} + { e.preventDefault() @@ -41,9 +42,12 @@ export default function CategoryForm({ }) if (res.ok) { + setSuccess(true) + await new Promise(r => setTimeout(r, 1000)) router.push("/admin/categories") } else { - setError("保存失败") + const data = await res.json().catch(() => ({})) + setError(data.error || "保存失败") } } catch (err) { setError("保存失败,请稍后重试") @@ -100,10 +104,22 @@ export default function CategoryForm({ /> + {success && ( +
+ 保存成功 +
+ )} + + {error && ( +
+ {error} +
+ )} +
+ {success && ( +
+ 保存成功 +
+ )} + + {error && ( +
+ {error} +
+ )} +