feat:重构dify前端组件以及转发逻辑
This commit is contained in:
@@ -1,7 +1,67 @@
|
||||
import { type ActionFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '../services/dify-client.server';
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
import { getSessionInfo } from '../utils/session.server';
|
||||
|
||||
/**
|
||||
* GET /api/chat-messages - 获取会话消息列表
|
||||
*/
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
try {
|
||||
// 获取用户会话信息和 JWT
|
||||
const { getUserSession } = await import("~/api/login/auth.server");
|
||||
const { frontendJWT } = await getUserSession(request);
|
||||
|
||||
// 检查 JWT 是否存在
|
||||
if (!frontendJWT) {
|
||||
console.error('❌ [API] Chat Messages GET - JWT不存在');
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
|
||||
{
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 从 URL 参数获取 conversation_id
|
||||
const url = new URL(request.url);
|
||||
const conversationId = url.searchParams.get('conversation_id');
|
||||
|
||||
if (!conversationId) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: '缺少 conversation_id 参数' }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
console.log('客戶端調用remix路由_Chat Messages GET - 获取消息列表:', { conversationId });
|
||||
|
||||
const result = await difyClient.getConversationMessages(conversationId, frontendJWT);
|
||||
|
||||
return new Response(JSON.stringify(result), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ [API] Chat Messages GET - Error:', error.message);
|
||||
const status = error.message?.includes('JWT认证失败') ? 401 : 500;
|
||||
return new Response(
|
||||
JSON.stringify({ error: error.message || 'Failed to get messages' }),
|
||||
{
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/chat-messages - 发送聊天消息
|
||||
*/
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
@@ -34,7 +94,7 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
response_mode: responseMode,
|
||||
} = body;
|
||||
|
||||
console.log('🚀 [API] Chat Messages API - 收到请求:', {
|
||||
console.log('客戶端調用remix路由_Chat Messages API - 收到请求:', {
|
||||
queryLength: query?.length || 0,
|
||||
queryPreview: query?.substring(0, 100) + (query?.length > 100 ? '...' : ''),
|
||||
conversationId,
|
||||
@@ -54,16 +114,9 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
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] 返回流式响应');
|
||||
console.log('Dify转发fastapi,返回流式响应');
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
headers: {
|
||||
@@ -78,7 +131,6 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
}
|
||||
|
||||
// 对于非流式响应,返回JSON
|
||||
console.log('📄 [API] 返回JSON响应');
|
||||
return new Response(JSON.stringify(response), {
|
||||
status: 200,
|
||||
headers: {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { json, type ActionFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '../services/dify-client.server';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
import { getSessionInfo, commitSession } from '../utils/session.server';
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs) {
|
||||
@@ -31,7 +31,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
|
||||
const body = await request.json();
|
||||
const { auto_generate, name } = body;
|
||||
|
||||
console.log('💬 [API] Rename Conversation API - 重命名会话:', {
|
||||
console.log('客戶端調用remix路由Rename Conversation API - 重命名会话:', {
|
||||
id,
|
||||
autoGenerate: auto_generate,
|
||||
name,
|
||||
@@ -41,8 +41,6 @@ export async function action({ request, params }: ActionFunctionArgs) {
|
||||
// 调用服务端API重命名会话
|
||||
const data = await difyClient.renameConversation(id, name, auto_generate, frontendJWT);
|
||||
|
||||
console.log('✅ [API] Rename Conversation API - Success');
|
||||
|
||||
return json(data, {
|
||||
headers: {
|
||||
'Set-Cookie': await commitSession(session),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { json, type ActionFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '../services/dify-client.server';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
import { getSessionInfo, commitSession } from '../utils/session.server';
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs) {
|
||||
@@ -31,16 +31,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
|
||||
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),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { json, type LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '../services/dify-client.server';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
import { getSessionInfo, commitSession } from '../utils/session.server';
|
||||
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
@@ -23,14 +23,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
||||
);
|
||||
}
|
||||
|
||||
console.log('💬 [API] Conversations API - 获取会话列表:', {
|
||||
hasJWT: !!frontendJWT
|
||||
});
|
||||
|
||||
const data = await difyClient.getConversations(frontendJWT);
|
||||
|
||||
console.log('✅ [API] Conversations API - Success');
|
||||
|
||||
return json(data, {
|
||||
headers: {
|
||||
'Set-Cookie': await commitSession(session),
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { type ActionFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
|
||||
/**
|
||||
* POST /api/messages/:messageId/feedbacks - 提交消息反馈
|
||||
*/
|
||||
export async function action({ request, params }: 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] Message Feedback - JWT不存在');
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
|
||||
{
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const { messageId } = params;
|
||||
if (!messageId) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: '缺少 messageId 参数' }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { rating } = body;
|
||||
|
||||
console.log('👍 [API] Message Feedback - 提交反馈:', {
|
||||
messageId,
|
||||
rating,
|
||||
});
|
||||
|
||||
const result = await difyClient.updateMessageFeedback(messageId, rating, frontendJWT);
|
||||
|
||||
console.log('✅ [API] Message Feedback - Success');
|
||||
return new Response(JSON.stringify(result), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ [API] Message Feedback - Error:', error.message);
|
||||
const status = error.message?.includes('JWT认证失败') ? 401 : 500;
|
||||
return new Response(
|
||||
JSON.stringify({ error: error.message || 'Failed to submit feedback' }),
|
||||
{
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { json, type LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { difyClient } from '../services/dify-client.server';
|
||||
import { difyClient } from '~/api/dify/client.server';
|
||||
import { getSessionInfo, commitSession } from '../utils/session.server';
|
||||
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
@@ -23,14 +23,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
||||
);
|
||||
}
|
||||
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user