From e1ecc484605ed807f2ef081f53b3246e35446ca3 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sat, 9 Aug 2025 00:35:46 +0800 Subject: [PATCH] fix simulator --- src/app/api/simulator/prompts/route.ts | 56 ++++++++++++++++++++++++++ src/app/simulator/new/page.tsx | 32 ++++++++------- 2 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/app/api/simulator/prompts/route.ts diff --git a/src/app/api/simulator/prompts/route.ts b/src/app/api/simulator/prompts/route.ts new file mode 100644 index 0000000..79a5022 --- /dev/null +++ b/src/app/api/simulator/prompts/route.ts @@ -0,0 +1,56 @@ +import { NextRequest, NextResponse } from 'next/server' +import { prisma } from '@/lib/prisma' +import { createServerSupabaseClient } from '@/lib/supabase-server' + +// GET /api/simulator/prompts - 获取用户的提示词列表,包含所有版本信息用于模拟器 +export async function GET(request: NextRequest) { + try { + const supabase = await createServerSupabaseClient() + const { data: { user } } = await supabase.auth.getUser() + + if (!user) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const { searchParams } = new URL(request.url) + const limit = parseInt(searchParams.get('limit') || '100') + + // 获取用户的提示词,包含所有版本 + const prompts = await prisma.prompt.findMany({ + where: { + userId: user.id, + }, + include: { + versions: { + orderBy: { version: 'desc' }, + select: { + id: true, + version: true, + content: true, + } + } + }, + orderBy: { updatedAt: 'desc' }, + take: limit, + }) + + // 转换数据结构,确保包含所有版本信息 + const promptsWithVersions = prompts.map(prompt => ({ + id: prompt.id, + name: prompt.name, + content: prompt.content, + versions: prompt.versions + })) + + return NextResponse.json({ + prompts: promptsWithVersions + }) + + } catch (error) { + console.error('Error fetching prompts for simulator:', error) + return NextResponse.json( + { error: 'Failed to fetch prompts' }, + { status: 500 } + ) + } +} \ No newline at end of file diff --git a/src/app/simulator/new/page.tsx b/src/app/simulator/new/page.tsx index b5c5856..26a6d08 100644 --- a/src/app/simulator/new/page.tsx +++ b/src/app/simulator/new/page.tsx @@ -1,6 +1,6 @@ 'use client' -import { useState, useEffect } from 'react' +import { useState, useEffect, useCallback } from 'react' import { useTranslations } from 'next-intl' import { useAuthUser } from '@/hooks/useAuthUser' import { useRouter } from 'next/navigation' @@ -69,23 +69,14 @@ export default function NewSimulatorRunPage() { const [frequencyPenalty, setFrequencyPenalty] = useState('0') const [presencePenalty, setPresencePenalty] = useState('0') - const selectedPrompt = prompts.find(p => p.id === selectedPromptId) - const selectedVersion = selectedPrompt?.versions.find(v => v.id === selectedVersionId) - const selectedModel = models.find(m => m.id === selectedModelId) - const promptContent = selectedVersion?.content || selectedPrompt?.content || '' - - useEffect(() => { - if (!authLoading && user) { - fetchData() - } - }, [user, authLoading]) - - const fetchData = async () => { + const fetchData = useCallback(async () => { + if (!user) return + try { setIsLoading(true) const [promptsResponse, modelsResponse] = await Promise.all([ - fetch('/api/prompts?limit=100'), + fetch('/api/simulator/prompts?limit=100'), fetch('/api/simulator/models') ]) @@ -107,7 +98,18 @@ export default function NewSimulatorRunPage() { } finally { setIsLoading(false) } - } + }, [user]) + + const selectedPrompt = prompts.find(p => p.id === selectedPromptId) + const selectedVersion = selectedPrompt?.versions.find(v => v.id === selectedVersionId) + const selectedModel = models.find(m => m.id === selectedModelId) + const promptContent = selectedVersion?.content || selectedPrompt?.content || '' + + useEffect(() => { + if (!authLoading && user) { + fetchData() + } + }, [user, authLoading, fetchData]) const handlePromptChange = (promptId: string) => { setSelectedPromptId(promptId)