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>
109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
import { type ActionFunctionArgs } from '@remix-run/node';
|
|
import { difyClient } from '../services/dify-client.server';
|
|
import { getSessionInfo } from '../utils/session.server';
|
|
|
|
export async function action({ request }: ActionFunctionArgs) {
|
|
if (request.method !== 'POST') {
|
|
return new Response('Method not allowed', { status: 405 });
|
|
}
|
|
|
|
try {
|
|
// 获取用户会话信息和 JWT
|
|
const { getUserSession } = await import("~/api/login/auth.server");
|
|
const { frontendJWT } = await getUserSession(request);
|
|
|
|
// 检查 JWT 是否存在
|
|
if (!frontendJWT) {
|
|
console.error('❌ [API] Chat Messages API - JWT不存在');
|
|
return new Response(
|
|
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
|
|
{
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
|
|
const {
|
|
inputs,
|
|
query,
|
|
files,
|
|
conversation_id: conversationId,
|
|
response_mode: responseMode,
|
|
} = body;
|
|
|
|
console.log('🚀 [API] Chat Messages API - 收到请求:', {
|
|
queryLength: query?.length || 0,
|
|
queryPreview: query?.substring(0, 100) + (query?.length > 100 ? '...' : ''),
|
|
conversationId,
|
|
responseMode,
|
|
hasInputs: !!inputs,
|
|
hasFiles: !!files && files.length > 0,
|
|
filesCount: files?.length || 0,
|
|
hasJWT: !!frontendJWT
|
|
});
|
|
|
|
const response = await difyClient.createChatMessage(
|
|
inputs,
|
|
query,
|
|
responseMode,
|
|
conversationId,
|
|
files,
|
|
frontendJWT // 传递 JWT
|
|
);
|
|
|
|
console.log('📡 [API] Dify响应状态:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
hasBody: !!response.body,
|
|
headers: Object.fromEntries(response.headers.entries())
|
|
});
|
|
|
|
// 对于流式响应,直接返回流
|
|
if (responseMode === 'streaming') {
|
|
console.log('🌊 [API] 返回流式响应');
|
|
return new Response(response.body, {
|
|
status: response.status,
|
|
headers: {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
'Connection': 'keep-alive',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'POST',
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
},
|
|
});
|
|
}
|
|
|
|
// 对于非流式响应,返回JSON
|
|
console.log('📄 [API] 返回JSON响应');
|
|
return new Response(JSON.stringify(response), {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
} catch (error: any) {
|
|
console.error('❌ [API] Chat Messages API - Error:', {
|
|
message: error.message,
|
|
stack: error.stack,
|
|
name: error.name
|
|
});
|
|
|
|
// 检查是否是JWT认证失败
|
|
const status = error.message?.includes('JWT认证失败') ? 401 : 500;
|
|
|
|
return new Response(
|
|
JSON.stringify({ error: error.message || 'Failed to send message' }),
|
|
{
|
|
status,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|