修复Dify对话记录加载问题:移除user参数,由后端自动管理

问题描述:
- 用户登录后无法加载历史对话记录
- 根本原因:前端传递的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>
This commit is contained in:
2025-10-30 10:37:45 +08:00
parent 05cc992c78
commit 3254cec5ca
8 changed files with 395 additions and 40 deletions
-3
View File
@@ -11,7 +11,6 @@ export async function action({ request }: ActionFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user } = await getSessionInfo(request);
// 检查 JWT 是否存在
if (!frontendJWT) {
@@ -36,7 +35,6 @@ export async function action({ request }: ActionFunctionArgs) {
} = body;
console.log('🚀 [API] Chat Messages API - 收到请求:', {
user,
queryLength: query?.length || 0,
queryPreview: query?.substring(0, 100) + (query?.length > 100 ? '...' : ''),
conversationId,
@@ -50,7 +48,6 @@ export async function action({ request }: ActionFunctionArgs) {
const response = await difyClient.createChatMessage(
inputs,
query,
user,
responseMode,
conversationId,
files,
+2 -3
View File
@@ -7,7 +7,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user, session } = await getSessionInfo(request);
const { session } = await getSessionInfo(request);
const { id } = params;
if (!id) {
@@ -32,7 +32,6 @@ export async function action({ request, params }: ActionFunctionArgs) {
const { auto_generate, name } = body;
console.log('💬 [API] Rename Conversation API - 重命名会话:', {
user,
id,
autoGenerate: auto_generate,
name,
@@ -40,7 +39,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
});
// 调用服务端API重命名会话
const data = await difyClient.renameConversation(id, name, user, auto_generate, frontendJWT);
const data = await difyClient.renameConversation(id, name, auto_generate, frontendJWT);
console.log('✅ [API] Rename Conversation API - Success');
+2 -3
View File
@@ -7,7 +7,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user, session } = await getSessionInfo(request);
const { session } = await getSessionInfo(request);
const { id } = params;
if (!id) {
@@ -32,13 +32,12 @@ export async function action({ request, params }: ActionFunctionArgs) {
if (method === 'DELETE') {
console.log('🗑️ [API] Delete Conversation API - 删除会话:', {
user,
id,
hasJWT: !!frontendJWT
});
// 调用服务端API删除会话
const data = await difyClient.deleteConversation(id, user, frontendJWT);
const data = await difyClient.deleteConversation(id, frontendJWT);
console.log('✅ [API] Delete Conversation API - Success');
+2 -3
View File
@@ -7,7 +7,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user, session } = await getSessionInfo(request);
const { session } = await getSessionInfo(request);
// 检查 JWT 是否存在
if (!frontendJWT) {
@@ -24,11 +24,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
console.log('💬 [API] Conversations API - 获取会话列表:', {
user,
hasJWT: !!frontendJWT
});
const data = await difyClient.getConversations(user, frontendJWT);
const data = await difyClient.getConversations(frontendJWT);
console.log('✅ [API] Conversations API - Success');
+2 -3
View File
@@ -7,7 +7,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user, session } = await getSessionInfo(request);
const { session } = await getSessionInfo(request);
const url = new URL(request.url);
const conversationId = url.searchParams.get('conversation_id');
@@ -33,12 +33,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
console.log('📨 [API] Messages API - 获取会话消息:', {
user,
conversationId,
hasJWT: !!frontendJWT
});
const data = await difyClient.getConversationMessages(user, conversationId, frontendJWT);
const data = await difyClient.getConversationMessages(conversationId, frontendJWT);
console.log('✅ [API] Messages API - Success');
+2 -3
View File
@@ -7,7 +7,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
// 获取用户会话信息和 JWT
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
const { user, session } = await getSessionInfo(request);
const { session } = await getSessionInfo(request);
// 检查 JWT 是否存在
if (!frontendJWT) {
@@ -24,11 +24,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
console.log('📋 [API] Parameters API - 获取应用参数:', {
user,
hasJWT: !!frontendJWT
});
const data = await difyClient.getApplicationParameters(user, frontendJWT);
const data = await difyClient.getApplicationParameters(frontendJWT);
console.log('✅ [API] Parameters API - Success');
+12 -22
View File
@@ -81,15 +81,11 @@ const difyFetch = async (endpoint: string, options: RequestInit = {}, jwt?: stri
return response;
};
// 生成用户ID
const generateUserId = (sessionId: string) => {
return `user_${DIFY_CONFIG.APP_ID}:${sessionId}`;
};
// Dify API 客户端 - 所有方法都需要传入 JWT
// 注意:user 参数已移除,由后端自动从 JWT 中提取 username
export const difyClient = {
// 获取应用参数
async getApplicationParameters(user: string, jwt?: string) {
async getApplicationParameters(jwt?: string) {
const response = await difyFetch('parameters', {
method: 'GET',
}, jwt);
@@ -97,9 +93,8 @@ export const difyClient = {
},
// 获取会话列表
async getConversations(user: string, jwt?: string) {
async getConversations(jwt?: string) {
const params = new URLSearchParams({
user,
limit: '100',
first_id: '',
});
@@ -111,9 +106,8 @@ export const difyClient = {
},
// 获取会话消息
async getConversationMessages(user: string, conversationId: string, jwt?: string) {
async getConversationMessages(conversationId: string, jwt?: string) {
const params = new URLSearchParams({
user,
conversation_id: conversationId,
limit: '20',
last_id: '',
@@ -129,7 +123,6 @@ export const difyClient = {
async createChatMessage(
inputs: Record<string, any>,
query: string,
user: string,
responseMode: string = 'streaming',
conversationId?: string,
files?: any[],
@@ -138,7 +131,7 @@ export const difyClient = {
const body = {
inputs,
query,
user,
// user 字段已移除,后端会自动从 JWT 中提取 username
response_mode: responseMode,
conversation_id: conversationId,
files: files || [],
@@ -147,7 +140,6 @@ export const difyClient = {
console.log('🌐 [DifyClient] 发送聊天消息:', {
queryLength: query.length,
queryPreview: query.substring(0, 100) + (query.length > 100 ? '...' : ''),
user,
responseMode,
conversationId,
hasInputs: !!inputs && Object.keys(inputs).length > 0,
@@ -181,11 +173,11 @@ export const difyClient = {
},
// 重命名会话
async renameConversation(conversationId: string, name: string, user: string, autoGenerate: boolean = false, jwt?: string) {
async renameConversation(conversationId: string, name: string, autoGenerate: boolean = false, jwt?: string) {
const body = {
name,
auto_generate: autoGenerate,
user,
// user 字段已移除,后端会自动从 JWT 中提取 username
};
const response = await difyFetch(`conversations/${conversationId}/name`, {
@@ -196,10 +188,9 @@ export const difyClient = {
},
// 删除会话
async deleteConversation(conversationId: string, user: string, jwt?: string) {
const body = {
user,
};
async deleteConversation(conversationId: string, jwt?: string) {
// user 字段已移除,后端会自动从 JWT 中提取 username
const body = {};
const response = await difyFetch(`conversations/${conversationId}`, {
method: 'DELETE',
@@ -209,10 +200,10 @@ export const difyClient = {
},
// 更新消息反馈
async updateMessageFeedback(messageId: string, rating: 'like' | 'dislike' | null, user: string, jwt?: string) {
async updateMessageFeedback(messageId: string, rating: 'like' | 'dislike' | null, jwt?: string) {
const body = {
rating,
user,
// user 字段已移除,后端会自动从 JWT 中提取 username
};
const response = await difyFetch(`messages/${messageId}/feedbacks`, {
@@ -225,6 +216,5 @@ export const difyClient = {
// 工具函数
export const difyUtils = {
generateUserId,
getConfig: () => DIFY_CONFIG,
};