import { NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { auth } from '@/lib/auth' import { headers } from 'next/headers' export async function POST( request: Request, context: { params: Promise<{ id: string }> } ) { try { const params = await context.params const session = await auth.api.getSession({ headers: await headers() }) if (!session?.user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { permissions } = await request.json() if (!permissions || !['private', 'public'].includes(permissions)) { return NextResponse.json( { error: 'Invalid permissions value' }, { status: 400 } ) } // 检查 simulator run 是否存在且属于当前用户 const run = await prisma.simulatorRun.findFirst({ where: { id: params.id, userId: session.user.id } }) if (!run) { return NextResponse.json( { error: 'Simulator run not found' }, { status: 404 } ) } // 只有已完成的 run 才能共享 if (run.status !== 'completed') { return NextResponse.json( { error: 'Only completed simulator runs can be shared' }, { status: 400 } ) } // 更新权限设置 const updatedRun = await prisma.simulatorRun.update({ where: { id: params.id }, data: { permissions, // 如果设为 public,重置 visibility 为等待审核状态 visibility: permissions === 'public' ? null : null }, include: { prompt: { select: { id: true, name: true, content: true } }, model: { select: { id: true, name: true, provider: true, modelId: true, outputType: true, description: true, maxTokens: true } } } }) return NextResponse.json(updatedRun) } catch (error) { console.error('Error toggling simulator run share status:', error) return NextResponse.json( { error: 'Failed to update share status' }, { status: 500 } ) } }