import { json, type ActionFunctionArgs } from '@remix-run/node'; import { difyClient } from '../services/dify-client.server'; import { getSessionInfo, commitSession } from '../utils/session.server'; export async function action({ request, params }: ActionFunctionArgs) { try { // 获取用户会话信息和 JWT const { getUserSession } = await import("~/api/login/auth.server"); const { frontendJWT } = await getUserSession(request); const { session } = await getSessionInfo(request); const { id } = params; if (!id) { return json({ error: '会话ID不能为空' }, { status: 400 }); } // 检查 JWT 是否存在 if (!frontendJWT) { console.error('❌ [API] Delete Conversation API - JWT不存在'); return json( { error: 'JWT认证失败,请重新登录' }, { status: 401, headers: { 'Set-Cookie': await commitSession(session), }, } ); } const method = request.method; if (method === 'DELETE') { console.log('🗑️ [API] Delete Conversation API - 删除会话:', { id, hasJWT: !!frontendJWT }); // 调用服务端API删除会话 const data = await difyClient.deleteConversation(id, frontendJWT); console.log('✅ [API] Delete Conversation API - Success'); return json(data, { headers: { 'Set-Cookie': await commitSession(session), }, }); } return json({ error: '不支持的请求方法' }, { status: 405 }); } catch (error: any) { console.error('❌ [API] Delete Conversation API - Error:', error); // 检查是否是JWT认证失败 const status = error.message?.includes('JWT认证失败') ? 401 : 500; return json( { error: error.message || '删除会话失败' }, { status, headers: { 'Set-Cookie': await commitSession((await getSessionInfo(request)).session), }, } ); } }