Files
leaudit-platform-frontend/app/routes/api.conversations.$id.name.tsx
T
2025-11-30 19:27:01 +08:00

67 lines
2.3 KiB
TypeScript

import { json, type ActionFunctionArgs } from '@remix-run/node';
import { difyClient } from '~/api/dify-chat/client.server';
import { commitSession, getSessionInfo } 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] Rename Conversation API - JWT不存在');
return json(
{ error: 'JWT认证失败,请重新登录' },
{
status: 401,
headers: {
'Set-Cookie': await commitSession(session),
},
}
);
}
const body = await request.json();
const { auto_generate, name } = body;
console.log('客戶端調用remix路由Rename Conversation API - 重命名会话:', {
id,
autoGenerate: auto_generate,
name,
hasJWT: !!frontendJWT
});
// 调用服务端API重命名会话
const data = await difyClient.renameConversation(id, name, auto_generate, frontendJWT);
return json(data, {
headers: {
'Set-Cookie': await commitSession(session),
},
});
} catch (error: any) {
console.error('❌ [API] Rename 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),
},
}
);
}
}