feat:重构dify前端组件以及转发逻辑
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { useState, useCallback, useRef } from 'react';
|
||||
import { produce } from 'immer';
|
||||
import { useBoolean, useGetState } from 'ahooks';
|
||||
import { sendChatMessage, updateFeedback, generateConversationName } from '../services/api.client';
|
||||
import type { ChatItem, Feedbacktype, ThoughtItem, VisionFile, MessageEnd, MessageReplace } from '../types/dify_chat';
|
||||
import { CHAT_CONFIG } from '../config/chat';
|
||||
import { produce } from 'immer';
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
import { sendChatMessage, updateFeedback, generateConversationName } from '~/api/dify';
|
||||
import type { ChatItem, Feedbacktype, MessageEnd, MessageReplace, VisionFile } from '~/api/dify';
|
||||
|
||||
/**
|
||||
* 聊天消息处理钩子
|
||||
@@ -61,22 +60,9 @@ export default function useChatMessage({
|
||||
questionItem: ChatItem,
|
||||
originalResponseId?: string
|
||||
) => {
|
||||
// console.log('🔄 [useChatMessage] 更新聊天列表:', {
|
||||
// responseItemId: responseItem.id,
|
||||
// responseContentLength: responseItem.content.length,
|
||||
// responsePreview: responseItem.content.substring(0, 50) + (responseItem.content.length > 50 ? '...' : ''),
|
||||
// originalResponseId,
|
||||
// questionId,
|
||||
// placeholderAnswerId
|
||||
// });
|
||||
|
||||
setChatList(produce(getChatList(), (draft) => {
|
||||
// console.log('📝 [useChatMessage] 当前聊天列表:', draft.map(item => ({
|
||||
// id: item.id,
|
||||
// contentLength: item.content.length,
|
||||
// contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
||||
// isAnswer: item.isAnswer
|
||||
// })));
|
||||
|
||||
|
||||
// 移除占位符
|
||||
const placeholderIndex = draft.findIndex(item => item.id === placeholderAnswerId);
|
||||
@@ -112,26 +98,11 @@ export default function useChatMessage({
|
||||
}
|
||||
|
||||
if (responseIndex !== -1) {
|
||||
// console.log('✏️ [useChatMessage] 更新现有响应:', {
|
||||
// responseIndex,
|
||||
// oldContentLength: draft[responseIndex].content.length,
|
||||
// newContentLength: responseItem.content.length
|
||||
// });
|
||||
|
||||
draft[responseIndex] = { ...responseItem };
|
||||
} else {
|
||||
// console.log('➕ [useChatMessage] 添加新响应:', {
|
||||
// responseId: responseItem.id,
|
||||
// contentLength: responseItem.content.length
|
||||
// });
|
||||
draft.push({ ...responseItem });
|
||||
}
|
||||
|
||||
// console.log('📝 [useChatMessage] 更新后聊天列表:', draft.map(item => ({
|
||||
// id: item.id,
|
||||
// contentLength: item.content.length,
|
||||
// contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
||||
// isAnswer: item.isAnswer
|
||||
// })));
|
||||
}));
|
||||
}, [getChatList, setChatList]);
|
||||
|
||||
@@ -274,27 +245,11 @@ export default function useChatMessage({
|
||||
// 发送消息
|
||||
await sendChatMessage(data, {
|
||||
onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }) => {
|
||||
// console.log('📨 [useChatMessage] 收到流式数据:', {
|
||||
// messageLength: message.length,
|
||||
// message: message.substring(0, 100) + (message.length > 100 ? '...' : ''),
|
||||
// isFirstMessage,
|
||||
// messageId,
|
||||
// newConversationId,
|
||||
// taskId,
|
||||
// isAgentMode,
|
||||
// currentContentLength: responseItem.content.length
|
||||
// });
|
||||
|
||||
if (!isAgentMode) {
|
||||
// 累积消息内容
|
||||
const oldContent = responseItem.content;
|
||||
responseItem.content = responseItem.content + message;
|
||||
// console.log('📝 [useChatMessage] 累积消息内容:', {
|
||||
// oldLength: oldContent.length,
|
||||
// newLength: responseItem.content.length,
|
||||
// addedLength: message.length,
|
||||
// preview: responseItem.content.substring(0, 50) + '...'
|
||||
// });
|
||||
} else {
|
||||
const lastThought = responseItem.agent_thoughts?.[responseItem.agent_thoughts?.length - 1];
|
||||
if (lastThought) {
|
||||
@@ -323,13 +278,6 @@ export default function useChatMessage({
|
||||
|
||||
setMessageTaskId(taskId || '');
|
||||
|
||||
// 检查是否切换到其他会话
|
||||
// console.log('🔍 会话检查:', {
|
||||
// prevTempNewConversationId,
|
||||
// conversationId,
|
||||
// isEqual: prevTempNewConversationId === conversationId
|
||||
// });
|
||||
|
||||
// 修复新会话的匹配逻辑
|
||||
const isNewConversationMatch = (prevTempNewConversationId === '-1' && conversationId === null) ||
|
||||
(prevTempNewConversationId === conversationId);
|
||||
@@ -340,25 +288,6 @@ export default function useChatMessage({
|
||||
return;
|
||||
}
|
||||
|
||||
// console.log('🔄 准备调用updateCurrentQA:', {
|
||||
// responseItemId: responseItem.id,
|
||||
// responseContent: responseItem.content,
|
||||
// questionId,
|
||||
// placeholderAnswerId,
|
||||
// originalResponseId
|
||||
// });
|
||||
|
||||
// console.log('🔄 [useChatMessage] 准备调用updateCurrentQA:', {
|
||||
// responseItemId: responseItem.id,
|
||||
// responseContentLength: responseItem.content.length,
|
||||
// responsePreview: responseItem.content.substring(0, 100) + (responseItem.content.length > 100 ? '...' : ''),
|
||||
// questionId,
|
||||
// placeholderAnswerId,
|
||||
// originalResponseId,
|
||||
// isAgentMode,
|
||||
// agentThoughtsCount: responseItem.agent_thoughts?.length || 0
|
||||
// });
|
||||
|
||||
// 更新当前问答(使用防抖)
|
||||
updateCurrentQA({
|
||||
responseItem: { ...responseItem }, // 创建副本避免引用问题
|
||||
@@ -370,7 +299,6 @@ export default function useChatMessage({
|
||||
},
|
||||
|
||||
onCompleted: async (hasError?: boolean) => {
|
||||
// console.log('✅ 消息发送完成:', { hasError });
|
||||
|
||||
// 立即更新最终状态
|
||||
if (currentResponseRef.current) {
|
||||
@@ -541,10 +469,7 @@ export default function useChatMessage({
|
||||
*/
|
||||
const handleFeedback = useCallback(async (messageId: string, feedback: Feedbacktype) => {
|
||||
try {
|
||||
await updateFeedback({
|
||||
url: `messages/${messageId}/feedbacks`,
|
||||
body: feedback,
|
||||
});
|
||||
await updateFeedback(messageId, feedback);
|
||||
|
||||
// 更新聊天列表中的反馈
|
||||
setChatList(produce(getChatList(), (draft) => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useState, useCallback } from 'react';
|
||||
import { useParams } from '@remix-run/react';
|
||||
import { produce } from 'immer';
|
||||
import { useGetState, useLocalStorageState } from 'ahooks';
|
||||
import type { ConversationItem } from '../types/dify_chat';
|
||||
import type { ConversationItem } from '~/api/dify';
|
||||
import { CHAT_CONFIG } from '../config/chat';
|
||||
|
||||
// 本地存储键名
|
||||
|
||||
Reference in New Issue
Block a user