3254cec5ca
问题描述:
- 用户登录后无法加载历史对话记录
- 根本原因:前端传递的user值与实际用户不一致,导致Dify无法找到对应的对话
解决方案:
- 后端已实现user字段自动填充功能(v1.2.7-post2)
- 前端采用方案A:完全移除user参数传递
- 让后端从JWT中自动提取username并管理user字段
修改内容:
1. dify-client.server.ts
- 移除所有方法的user参数
- GET请求移除user查询参数
- POST/DELETE请求移除user字段
- 移除generateUserId工具函数
2. 所有API路由
- 移除getSessionInfo中的user解构
- 移除difyClient方法调用中的user参数传递
- 日志中移除user信息输出
影响接口:
- GET /dify/conversations - 会话列表
- GET /dify/messages - 消息历史
- POST /dify/chat-messages - 发送消息
- POST /dify/conversations/{id}/name - 重命名会话
- DELETE /dify/conversations/{id} - 删除会话
- POST /dify/messages/{id}/feedbacks - 消息反馈
参考文档:docs/dify-frontend-user-field-guide.md
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { json, type LoaderFunctionArgs } from '@remix-run/node';
|
|
import { difyClient } from '../services/dify-client.server';
|
|
import { getSessionInfo, commitSession } from '../utils/session.server';
|
|
|
|
export async function loader({ request }: LoaderFunctionArgs) {
|
|
try {
|
|
// 获取用户会话信息和 JWT
|
|
const { getUserSession } = await import("~/api/login/auth.server");
|
|
const { frontendJWT } = await getUserSession(request);
|
|
const { session } = await getSessionInfo(request);
|
|
|
|
// 检查 JWT 是否存在
|
|
if (!frontendJWT) {
|
|
console.error('❌ [API] Parameters API - JWT不存在');
|
|
return json(
|
|
{ error: 'JWT认证失败,请重新登录' },
|
|
{
|
|
status: 401,
|
|
headers: {
|
|
'Set-Cookie': await commitSession(session),
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
console.log('📋 [API] Parameters API - 获取应用参数:', {
|
|
hasJWT: !!frontendJWT
|
|
});
|
|
|
|
const data = await difyClient.getApplicationParameters(frontendJWT);
|
|
|
|
console.log('✅ [API] Parameters API - Success');
|
|
|
|
return json(data, {
|
|
headers: {
|
|
'Set-Cookie': await commitSession(session),
|
|
},
|
|
});
|
|
} catch (error: any) {
|
|
console.error('❌ [API] Parameters API - Error:', error);
|
|
|
|
// 检查是否是JWT认证失败
|
|
const status = error.message?.includes('JWT认证失败') ? 401 : 500;
|
|
|
|
return json(
|
|
{ error: error.message || 'Failed to fetch parameters' },
|
|
{
|
|
status,
|
|
headers: {
|
|
'Set-Cookie': await commitSession((await getSessionInfo(request)).session),
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|