移除聊天组件中的调试日志
This commit is contained in:
@@ -389,7 +389,7 @@ export default function Chat() {
|
|||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
console.log('🚀 开始初始化聊天应用...');
|
// console.log('🚀 开始初始化聊天应用...');
|
||||||
|
|
||||||
// 并行获取会话列表和应用参数
|
// 并行获取会话列表和应用参数
|
||||||
const [conversationData, appParams] = await Promise.all([
|
const [conversationData, appParams] = await Promise.all([
|
||||||
@@ -441,7 +441,7 @@ export default function Chat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setInited(true);
|
setInited(true);
|
||||||
console.log('✅ 聊天应用初始化完成');
|
// console.log('✅ 聊天应用初始化完成');
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error('❌ 初始化失败:', e);
|
console.error('❌ 初始化失败:', e);
|
||||||
if (e.status === 404) {
|
if (e.status === 404) {
|
||||||
|
|||||||
@@ -17,12 +17,12 @@ interface MarkdownProps {
|
|||||||
* 使用 react-markdown 库进行标准 Markdown 解析,支持流式渲染
|
* 使用 react-markdown 库进行标准 Markdown 解析,支持流式渲染
|
||||||
*/
|
*/
|
||||||
export default function Markdown({ content, className = '' }: MarkdownProps) {
|
export default function Markdown({ content, className = '' }: MarkdownProps) {
|
||||||
console.log('🎨 [Markdown] 渲染组件:', {
|
// console.log('🎨 [Markdown] 渲染组件:', {
|
||||||
contentLength: content?.length || 0,
|
// contentLength: content?.length || 0,
|
||||||
contentPreview: content?.substring(0, 100) + (content && content.length > 100 ? '...' : ''),
|
// contentPreview: content?.substring(0, 100) + (content && content.length > 100 ? '...' : ''),
|
||||||
className,
|
// className,
|
||||||
hasContent: !!content
|
// hasContent: !!content
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
console.log('⚠️ [Markdown] 内容为空,返回null');
|
console.log('⚠️ [Markdown] 内容为空,返回null');
|
||||||
|
|||||||
@@ -82,22 +82,22 @@ const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(({
|
|||||||
|
|
||||||
setDeleteLoading(true);
|
setDeleteLoading(true);
|
||||||
try {
|
try {
|
||||||
console.log('🗑️ 开始删除会话:', deletingConversation.id);
|
// console.log('🗑️ 开始删除会话:', deletingConversation.id);
|
||||||
|
|
||||||
// 调用API删除服务器端的会话
|
// 调用API删除服务器端的会话
|
||||||
const response = await deleteConversation(deletingConversation.id);
|
const response = await deleteConversation(deletingConversation.id);
|
||||||
console.log('✅ 服务器端会话删除响应:', response);
|
// console.log('✅ 服务器端会话删除响应:', response);
|
||||||
|
|
||||||
// 检查响应是否成功
|
// 检查响应是否成功
|
||||||
if (response && (response as any).result === 'success') {
|
if (response && (response as any).result === 'success') {
|
||||||
console.log('✅ 服务器端会话删除成功');
|
// console.log('✅ 服务器端会话删除成功');
|
||||||
message.success('会话删除成功');
|
message.success('会话删除成功');
|
||||||
setDeleteModalVisible(false);
|
setDeleteModalVisible(false);
|
||||||
|
|
||||||
// 通知父组件会话已删除
|
// 通知父组件会话已删除
|
||||||
onConversationDeleted?.(deletingConversation.id);
|
onConversationDeleted?.(deletingConversation.id);
|
||||||
|
|
||||||
console.log('✅ 会话删除完成:', deletingConversation.id);
|
// console.log('✅ 会话删除完成:', deletingConversation.id);
|
||||||
} else {
|
} else {
|
||||||
throw new Error((response as any)?.error || '删除会话失败');
|
throw new Error((response as any)?.error || '删除会话失败');
|
||||||
}
|
}
|
||||||
@@ -130,22 +130,22 @@ const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(({
|
|||||||
|
|
||||||
setRenameLoading(true);
|
setRenameLoading(true);
|
||||||
try {
|
try {
|
||||||
console.log('✏️ 开始重命名会话:', { conversationId: renamingConversation.id, newName: newName.trim() });
|
// console.log('✏️ 开始重命名会话:', { conversationId: renamingConversation.id, newName: newName.trim() });
|
||||||
|
|
||||||
// 调用API重命名服务器端的会话
|
// 调用API重命名服务器端的会话
|
||||||
const response = await renameConversation(renamingConversation.id, newName.trim(), false);
|
const response = await renameConversation(renamingConversation.id, newName.trim(), false);
|
||||||
console.log('✅ 服务器端会话重命名响应:', response);
|
// console.log('✅ 服务器端会话重命名响应:', response);
|
||||||
|
|
||||||
// 检查响应是否成功
|
// 检查响应是否成功
|
||||||
if (response && (response as any).name) {
|
if (response && (response as any).name) {
|
||||||
console.log('✅ 服务器端会话重命名成功');
|
// console.log('✅ 服务器端会话重命名成功');
|
||||||
message.success('重命名成功');
|
message.success('重命名成功');
|
||||||
setRenameModalVisible(false);
|
setRenameModalVisible(false);
|
||||||
|
|
||||||
// 通知父组件会话已重命名
|
// 通知父组件会话已重命名
|
||||||
onConversationRenamed?.(renamingConversation.id, (response as any).name);
|
onConversationRenamed?.(renamingConversation.id, (response as any).name);
|
||||||
|
|
||||||
console.log('✅ 会话重命名完成:', renamingConversation.id, '->', (response as any).name);
|
// console.log('✅ 会话重命名完成:', renamingConversation.id, '->', (response as any).name);
|
||||||
} else {
|
} else {
|
||||||
throw new Error((response as any)?.error || '重命名会话失败');
|
throw new Error((response as any)?.error || '重命名会话失败');
|
||||||
}
|
}
|
||||||
@@ -213,27 +213,27 @@ const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(({
|
|||||||
useImperativeHandle(ref, () => ({
|
useImperativeHandle(ref, () => ({
|
||||||
autoRename: async (conversationId: string) => {
|
autoRename: async (conversationId: string) => {
|
||||||
try {
|
try {
|
||||||
console.log('🏷️ 开始自动重命名会话为"新对话":', conversationId);
|
// console.log('🏷️ 开始自动重命名会话为"新对话":', conversationId);
|
||||||
|
|
||||||
// 调用API将会话重命名为固定的"新对话"
|
// 调用API将会话重命名为固定的"新对话"
|
||||||
const response = await renameConversation(conversationId, '新对话', false);
|
const response = await renameConversation(conversationId, '新对话', false);
|
||||||
console.log('✅ 服务器端会话重命名响应:', response);
|
// console.log('✅ 服务器端会话重命名响应:', response);
|
||||||
|
|
||||||
// 检查响应是否成功
|
// 检查响应是否成功
|
||||||
if (response && (response as any).name) {
|
if (response && (response as any).name) {
|
||||||
console.log('✅ 服务器端会话重命名成功');
|
// console.log('✅ 服务器端会话重命名成功');
|
||||||
|
|
||||||
// 通知父组件会话已重命名
|
// 通知父组件会话已重命名
|
||||||
onConversationRenamed?.(conversationId, (response as any).name);
|
onConversationRenamed?.(conversationId, (response as any).name);
|
||||||
|
|
||||||
console.log('✅ 会话重命名完成:', conversationId, '->', (response as any).name);
|
// console.log('✅ 会话重命名完成:', conversationId, '->', (response as any).name);
|
||||||
} else {
|
} else {
|
||||||
throw new Error((response as any)?.error || '重命名会话失败');
|
throw new Error((response as any)?.error || '重命名会话失败');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ 重命名会话失败:', error);
|
console.error('❌ 重命名会话失败:', error);
|
||||||
// 重命名失败时不显示错误消息,避免打扰用户
|
// 重命名失败时不显示错误消息,避免打扰用户
|
||||||
console.warn('⚠️ 重命名失败,会话将保持默认名称');
|
// console.warn('⚠️ 重命名失败,会话将保持默认名称');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -61,27 +61,27 @@ export default function useChatMessage({
|
|||||||
questionItem: ChatItem,
|
questionItem: ChatItem,
|
||||||
originalResponseId?: string
|
originalResponseId?: string
|
||||||
) => {
|
) => {
|
||||||
console.log('🔄 [useChatMessage] 更新聊天列表:', {
|
// console.log('🔄 [useChatMessage] 更新聊天列表:', {
|
||||||
responseItemId: responseItem.id,
|
// responseItemId: responseItem.id,
|
||||||
responseContentLength: responseItem.content.length,
|
// responseContentLength: responseItem.content.length,
|
||||||
responsePreview: responseItem.content.substring(0, 50) + (responseItem.content.length > 50 ? '...' : ''),
|
// responsePreview: responseItem.content.substring(0, 50) + (responseItem.content.length > 50 ? '...' : ''),
|
||||||
originalResponseId,
|
// originalResponseId,
|
||||||
questionId,
|
// questionId,
|
||||||
placeholderAnswerId
|
// placeholderAnswerId
|
||||||
});
|
// });
|
||||||
|
|
||||||
setChatList(produce(getChatList(), (draft) => {
|
setChatList(produce(getChatList(), (draft) => {
|
||||||
console.log('📝 [useChatMessage] 当前聊天列表:', draft.map(item => ({
|
// console.log('📝 [useChatMessage] 当前聊天列表:', draft.map(item => ({
|
||||||
id: item.id,
|
// id: item.id,
|
||||||
contentLength: item.content.length,
|
// contentLength: item.content.length,
|
||||||
contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
// contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
||||||
isAnswer: item.isAnswer
|
// isAnswer: item.isAnswer
|
||||||
})));
|
// })));
|
||||||
|
|
||||||
// 移除占位符
|
// 移除占位符
|
||||||
const placeholderIndex = draft.findIndex(item => item.id === placeholderAnswerId);
|
const placeholderIndex = draft.findIndex(item => item.id === placeholderAnswerId);
|
||||||
if (placeholderIndex !== -1) {
|
if (placeholderIndex !== -1) {
|
||||||
console.log('🗑️ [useChatMessage] 移除占位符:', placeholderAnswerId, 'at index:', placeholderIndex);
|
// console.log('🗑️ [useChatMessage] 移除占位符:', placeholderAnswerId, 'at index:', placeholderIndex);
|
||||||
draft.splice(placeholderIndex, 1);
|
draft.splice(placeholderIndex, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,12 +94,12 @@ export default function useChatMessage({
|
|||||||
|
|
||||||
// 更新或添加响应 - 考虑ID可能已经改变的情况
|
// 更新或添加响应 - 考虑ID可能已经改变的情况
|
||||||
let responseIndex = draft.findIndex(item => item.id === responseItem.id);
|
let responseIndex = draft.findIndex(item => item.id === responseItem.id);
|
||||||
console.log('🔍 [useChatMessage] 查找响应索引 (当前ID):', { responseItemId: responseItem.id, responseIndex });
|
// console.log('🔍 [useChatMessage] 查找响应索引 (当前ID):', { responseItemId: responseItem.id, responseIndex });
|
||||||
|
|
||||||
// 如果找不到当前ID的响应,尝试查找原始ID
|
// 如果找不到当前ID的响应,尝试查找原始ID
|
||||||
if (responseIndex === -1 && originalResponseId) {
|
if (responseIndex === -1 && originalResponseId) {
|
||||||
responseIndex = draft.findIndex(item => item.id === originalResponseId);
|
responseIndex = draft.findIndex(item => item.id === originalResponseId);
|
||||||
console.log('🔍 [useChatMessage] 查找响应索引 (原始ID):', { originalResponseId, responseIndex });
|
// console.log('🔍 [useChatMessage] 查找响应索引 (原始ID):', { originalResponseId, responseIndex });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果找不到任何匹配的响应,查找最后一个AI回答
|
// 如果找不到任何匹配的响应,查找最后一个AI回答
|
||||||
@@ -108,30 +108,30 @@ export default function useChatMessage({
|
|||||||
item.isAnswer &&
|
item.isAnswer &&
|
||||||
index > draft.findIndex(q => q.id === questionId)
|
index > draft.findIndex(q => q.id === questionId)
|
||||||
);
|
);
|
||||||
console.log('🔍 [useChatMessage] 查找响应索引 (最后AI回答):', { responseIndex });
|
// console.log('🔍 [useChatMessage] 查找响应索引 (最后AI回答):', { responseIndex });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseIndex !== -1) {
|
if (responseIndex !== -1) {
|
||||||
console.log('✏️ [useChatMessage] 更新现有响应:', {
|
// console.log('✏️ [useChatMessage] 更新现有响应:', {
|
||||||
responseIndex,
|
// responseIndex,
|
||||||
oldContentLength: draft[responseIndex].content.length,
|
// oldContentLength: draft[responseIndex].content.length,
|
||||||
newContentLength: responseItem.content.length
|
// newContentLength: responseItem.content.length
|
||||||
});
|
// });
|
||||||
draft[responseIndex] = { ...responseItem };
|
draft[responseIndex] = { ...responseItem };
|
||||||
} else {
|
} else {
|
||||||
console.log('➕ [useChatMessage] 添加新响应:', {
|
// console.log('➕ [useChatMessage] 添加新响应:', {
|
||||||
responseId: responseItem.id,
|
// responseId: responseItem.id,
|
||||||
contentLength: responseItem.content.length
|
// contentLength: responseItem.content.length
|
||||||
});
|
// });
|
||||||
draft.push({ ...responseItem });
|
draft.push({ ...responseItem });
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('📝 [useChatMessage] 更新后聊天列表:', draft.map(item => ({
|
// console.log('📝 [useChatMessage] 更新后聊天列表:', draft.map(item => ({
|
||||||
id: item.id,
|
// id: item.id,
|
||||||
contentLength: item.content.length,
|
// contentLength: item.content.length,
|
||||||
contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
// contentPreview: item.content.substring(0, 20) + (item.content.length > 20 ? '...' : ''),
|
||||||
isAnswer: item.isAnswer
|
// isAnswer: item.isAnswer
|
||||||
})));
|
// })));
|
||||||
}));
|
}));
|
||||||
}, [getChatList, setChatList]);
|
}, [getChatList, setChatList]);
|
||||||
|
|
||||||
@@ -269,27 +269,27 @@ export default function useChatMessage({
|
|||||||
// 发送消息
|
// 发送消息
|
||||||
await sendChatMessage(data, {
|
await sendChatMessage(data, {
|
||||||
onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }) => {
|
onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }) => {
|
||||||
console.log('📨 [useChatMessage] 收到流式数据:', {
|
// console.log('📨 [useChatMessage] 收到流式数据:', {
|
||||||
messageLength: message.length,
|
// messageLength: message.length,
|
||||||
message: message.substring(0, 100) + (message.length > 100 ? '...' : ''),
|
// message: message.substring(0, 100) + (message.length > 100 ? '...' : ''),
|
||||||
isFirstMessage,
|
// isFirstMessage,
|
||||||
messageId,
|
// messageId,
|
||||||
newConversationId,
|
// newConversationId,
|
||||||
taskId,
|
// taskId,
|
||||||
isAgentMode,
|
// isAgentMode,
|
||||||
currentContentLength: responseItem.content.length
|
// currentContentLength: responseItem.content.length
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (!isAgentMode) {
|
if (!isAgentMode) {
|
||||||
// 累积消息内容
|
// 累积消息内容
|
||||||
const oldContent = responseItem.content;
|
const oldContent = responseItem.content;
|
||||||
responseItem.content = responseItem.content + message;
|
responseItem.content = responseItem.content + message;
|
||||||
console.log('📝 [useChatMessage] 累积消息内容:', {
|
// console.log('📝 [useChatMessage] 累积消息内容:', {
|
||||||
oldLength: oldContent.length,
|
// oldLength: oldContent.length,
|
||||||
newLength: responseItem.content.length,
|
// newLength: responseItem.content.length,
|
||||||
addedLength: message.length,
|
// addedLength: message.length,
|
||||||
preview: responseItem.content.substring(0, 50) + '...'
|
// preview: responseItem.content.substring(0, 50) + '...'
|
||||||
});
|
// });
|
||||||
} else {
|
} else {
|
||||||
const lastThought = responseItem.agent_thoughts?.[responseItem.agent_thoughts?.length - 1];
|
const lastThought = responseItem.agent_thoughts?.[responseItem.agent_thoughts?.length - 1];
|
||||||
if (lastThought) {
|
if (lastThought) {
|
||||||
@@ -340,16 +340,16 @@ export default function useChatMessage({
|
|||||||
// originalResponseId
|
// originalResponseId
|
||||||
// });
|
// });
|
||||||
|
|
||||||
console.log('🔄 [useChatMessage] 准备调用updateCurrentQA:', {
|
// console.log('🔄 [useChatMessage] 准备调用updateCurrentQA:', {
|
||||||
responseItemId: responseItem.id,
|
// responseItemId: responseItem.id,
|
||||||
responseContentLength: responseItem.content.length,
|
// responseContentLength: responseItem.content.length,
|
||||||
responsePreview: responseItem.content.substring(0, 100) + (responseItem.content.length > 100 ? '...' : ''),
|
// responsePreview: responseItem.content.substring(0, 100) + (responseItem.content.length > 100 ? '...' : ''),
|
||||||
questionId,
|
// questionId,
|
||||||
placeholderAnswerId,
|
// placeholderAnswerId,
|
||||||
originalResponseId,
|
// originalResponseId,
|
||||||
isAgentMode,
|
// isAgentMode,
|
||||||
agentThoughtsCount: responseItem.agent_thoughts?.length || 0
|
// agentThoughtsCount: responseItem.agent_thoughts?.length || 0
|
||||||
});
|
// });
|
||||||
|
|
||||||
// 更新当前问答(使用防抖)
|
// 更新当前问答(使用防抖)
|
||||||
updateCurrentQA({
|
updateCurrentQA({
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ export default function useConversation() {
|
|||||||
isSetToLocalStorage = true,
|
isSetToLocalStorage = true,
|
||||||
newConversationName = ''
|
newConversationName = ''
|
||||||
) => {
|
) => {
|
||||||
console.log('🔄 设置当前会话ID:', { id, appId, isSetToLocalStorage });
|
// console.log('🔄 设置当前会话ID:', { id, appId, isSetToLocalStorage });
|
||||||
|
|
||||||
doSetCurrConversationId(id);
|
doSetCurrConversationId(id);
|
||||||
|
|
||||||
@@ -130,18 +130,18 @@ export default function useConversation() {
|
|||||||
|
|
||||||
globalThis.localStorage?.setItem(storageConversationIdKey, JSON.stringify(conversationIdInfo));
|
globalThis.localStorage?.setItem(storageConversationIdKey, JSON.stringify(conversationIdInfo));
|
||||||
|
|
||||||
console.log('💾 会话ID已保存到localStorage:', {
|
// console.log('💾 会话ID已保存到localStorage:', {
|
||||||
appUrlKey,
|
// appUrlKey,
|
||||||
conversationId: id,
|
// conversationId: id,
|
||||||
fullStorage: conversationIdInfo
|
// fullStorage: conversationIdInfo
|
||||||
});
|
// });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('保存会话ID到本地存储失败:', error);
|
console.error('保存会话ID到本地存储失败:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 不进行URL导航,保持单页面应用模式
|
// 不进行URL导航,保持单页面应用模式
|
||||||
console.log('✅ 会话切换完成,当前会话ID:', id);
|
// console.log('✅ 会话切换完成,当前会话ID:', id);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+63
-63
@@ -122,11 +122,11 @@ const handleStream = (
|
|||||||
onNodeFinished?: IOnNodeFinished,
|
onNodeFinished?: IOnNodeFinished,
|
||||||
onError?: IOnError,
|
onError?: IOnError,
|
||||||
) => {
|
) => {
|
||||||
console.log('🌊 [handleStream] 开始处理流式响应:', {
|
// console.log('🌊 [handleStream] 开始处理流式响应:', {
|
||||||
status: response.status,
|
// status: response.status,
|
||||||
statusText: response.statusText,
|
// statusText: response.statusText,
|
||||||
headers: Object.fromEntries(response.headers.entries())
|
// headers: Object.fromEntries(response.headers.entries())
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error('❌ [handleStream] 响应错误:', response.status, response.statusText);
|
console.error('❌ [handleStream] 响应错误:', response.status, response.statusText);
|
||||||
@@ -141,19 +141,19 @@ const handleStream = (
|
|||||||
let isFirstMessage = true;
|
let isFirstMessage = true;
|
||||||
let messageCount = 0;
|
let messageCount = 0;
|
||||||
|
|
||||||
console.log('📖 [handleStream] 获取reader:', !!reader);
|
// console.log('📖 [handleStream] 获取reader:', !!reader);
|
||||||
|
|
||||||
function read() {
|
function read() {
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
reader?.read().then((result: any) => {
|
reader?.read().then((result: any) => {
|
||||||
console.log('📨 [handleStream] 读取数据块:', {
|
// console.log('📨 [handleStream] 读取数据块:', {
|
||||||
done: result.done,
|
// done: result.done,
|
||||||
valueLength: result.value?.length,
|
// valueLength: result.value?.length,
|
||||||
messageCount: ++messageCount
|
// messageCount: ++messageCount
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (result.done) {
|
if (result.done) {
|
||||||
console.log('✅ [handleStream] 流式响应完成, 总消息数:', messageCount);
|
// console.log('✅ [handleStream] 流式响应完成, 总消息数:', messageCount);
|
||||||
onCompleted && onCompleted();
|
onCompleted && onCompleted();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -162,31 +162,31 @@ const handleStream = (
|
|||||||
buffer += chunk;
|
buffer += chunk;
|
||||||
const lines = buffer.split('\n');
|
const lines = buffer.split('\n');
|
||||||
|
|
||||||
console.log('🔍 [handleStream] 处理数据块:', {
|
// console.log('🔍 [handleStream] 处理数据块:', {
|
||||||
chunkLength: chunk.length,
|
// chunkLength: chunk.length,
|
||||||
bufferLength: buffer.length,
|
// bufferLength: buffer.length,
|
||||||
linesCount: lines.length,
|
// linesCount: lines.length,
|
||||||
chunk: chunk.substring(0, 100) + (chunk.length > 100 ? '...' : '')
|
// chunk: chunk.substring(0, 100) + (chunk.length > 100 ? '...' : '')
|
||||||
});
|
// });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
lines.forEach((message, index) => {
|
lines.forEach((message, index) => {
|
||||||
if (message.startsWith('data: ')) {
|
if (message.startsWith('data: ')) {
|
||||||
const jsonStr = message.substring(6);
|
const jsonStr = message.substring(6);
|
||||||
console.log(`📋 [handleStream] 解析消息 ${index}:`, {
|
// console.log(`📋 [handleStream] 解析消息 ${index}:`, {
|
||||||
jsonLength: jsonStr.length,
|
// jsonLength: jsonStr.length,
|
||||||
preview: jsonStr.substring(0, 200) + (jsonStr.length > 200 ? '...' : '')
|
// preview: jsonStr.substring(0, 200) + (jsonStr.length > 200 ? '...' : '')
|
||||||
});
|
// });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bufferObj = JSON.parse(jsonStr) as Record<string, any>;
|
bufferObj = JSON.parse(jsonStr) as Record<string, any>;
|
||||||
console.log('✨ [handleStream] JSON解析成功:', {
|
// console.log('✨ [handleStream] JSON解析成功:', {
|
||||||
event: bufferObj.event,
|
// event: bufferObj.event,
|
||||||
hasAnswer: !!bufferObj.answer,
|
// hasAnswer: !!bufferObj.answer,
|
||||||
answerLength: bufferObj.answer?.length || 0,
|
// answerLength: bufferObj.answer?.length || 0,
|
||||||
conversationId: bufferObj.conversation_id,
|
// conversationId: bufferObj.conversation_id,
|
||||||
messageId: bufferObj.id || bufferObj.message_id
|
// messageId: bufferObj.id || bufferObj.message_id
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.warn('⚠️ [handleStream] JSON解析失败:', e, 'JSON:', jsonStr);
|
console.warn('⚠️ [handleStream] JSON解析失败:', e, 'JSON:', jsonStr);
|
||||||
@@ -218,14 +218,14 @@ const handleStream = (
|
|||||||
|
|
||||||
if (bufferObj.event === 'message' || bufferObj.event === 'agent_message') {
|
if (bufferObj.event === 'message' || bufferObj.event === 'agent_message') {
|
||||||
const answer = unicodeToChar(bufferObj.answer);
|
const answer = unicodeToChar(bufferObj.answer);
|
||||||
console.log('💬 [handleStream] 处理消息事件:', {
|
// console.log('💬 [handleStream] 处理消息事件:', {
|
||||||
event: bufferObj.event,
|
// event: bufferObj.event,
|
||||||
isFirstMessage,
|
// isFirstMessage,
|
||||||
answerLength: answer.length,
|
// answerLength: answer.length,
|
||||||
answer: answer.substring(0, 50) + (answer.length > 50 ? '...' : ''),
|
// answer: answer.substring(0, 50) + (answer.length > 50 ? '...' : ''),
|
||||||
conversationId: bufferObj.conversation_id,
|
// conversationId: bufferObj.conversation_id,
|
||||||
messageId: bufferObj.id || bufferObj.message_id
|
// messageId: bufferObj.id || bufferObj.message_id
|
||||||
});
|
// });
|
||||||
|
|
||||||
onData(answer, isFirstMessage, {
|
onData(answer, isFirstMessage, {
|
||||||
conversationId: bufferObj.conversation_id,
|
conversationId: bufferObj.conversation_id,
|
||||||
@@ -234,44 +234,44 @@ const handleStream = (
|
|||||||
});
|
});
|
||||||
isFirstMessage = false;
|
isFirstMessage = false;
|
||||||
} else if (bufferObj.event === 'agent_thought' && onThought) {
|
} else if (bufferObj.event === 'agent_thought' && onThought) {
|
||||||
console.log('🤔 [handleStream] 处理思考事件:', bufferObj.event);
|
// console.log('🤔 [handleStream] 处理思考事件:', bufferObj.event);
|
||||||
onThought(bufferObj as ThoughtItem);
|
onThought(bufferObj as ThoughtItem);
|
||||||
} else if (bufferObj.event === 'message_file' && onFile) {
|
} else if (bufferObj.event === 'message_file' && onFile) {
|
||||||
console.log('📁 [handleStream] 处理文件事件:', bufferObj.event);
|
// console.log('📁 [handleStream] 处理文件事件:', bufferObj.event);
|
||||||
onFile(bufferObj as VisionFile);
|
onFile(bufferObj as VisionFile);
|
||||||
} else if (bufferObj.event === 'message_end' && onMessageEnd) {
|
} else if (bufferObj.event === 'message_end' && onMessageEnd) {
|
||||||
console.log('🏁 [handleStream] 处理消息结束事件:', bufferObj.event);
|
// console.log('🏁 [handleStream] 处理消息结束事件:', bufferObj.event);
|
||||||
onMessageEnd(bufferObj as MessageEnd);
|
onMessageEnd(bufferObj as MessageEnd);
|
||||||
} else if (bufferObj.event === 'message_replace' && onMessageReplace) {
|
} else if (bufferObj.event === 'message_replace' && onMessageReplace) {
|
||||||
console.log('🔄 [handleStream] 处理消息替换事件:', bufferObj.event);
|
// console.log('🔄 [handleStream] 处理消息替换事件:', bufferObj.event);
|
||||||
onMessageReplace(bufferObj as MessageReplace);
|
onMessageReplace(bufferObj as MessageReplace);
|
||||||
} else if (bufferObj.event === 'workflow_started' && onWorkflowStarted) {
|
} else if (bufferObj.event === 'workflow_started' && onWorkflowStarted) {
|
||||||
console.log('🚀 [handleStream] 处理工作流开始事件:', bufferObj.event);
|
// console.log('🚀 [handleStream] 处理工作流开始事件:', bufferObj.event);
|
||||||
onWorkflowStarted(bufferObj as WorkflowStartedResponse);
|
onWorkflowStarted(bufferObj as WorkflowStartedResponse);
|
||||||
} else if (bufferObj.event === 'workflow_finished' && onWorkflowFinished) {
|
} else if (bufferObj.event === 'workflow_finished' && onWorkflowFinished) {
|
||||||
console.log('🎯 [handleStream] 处理工作流完成事件:', bufferObj.event);
|
// console.log('🎯 [handleStream] 处理工作流完成事件:', bufferObj.event);
|
||||||
onWorkflowFinished(bufferObj as WorkflowFinishedResponse);
|
onWorkflowFinished(bufferObj as WorkflowFinishedResponse);
|
||||||
} else if (bufferObj.event === 'node_started' && onNodeStarted) {
|
} else if (bufferObj.event === 'node_started' && onNodeStarted) {
|
||||||
console.log('🔗 [handleStream] 处理节点开始事件:', bufferObj.event);
|
// console.log('🔗 [handleStream] 处理节点开始事件:', bufferObj.event);
|
||||||
onNodeStarted(bufferObj as NodeStartedResponse);
|
onNodeStarted(bufferObj as NodeStartedResponse);
|
||||||
} else if (bufferObj.event === 'node_finished' && onNodeFinished) {
|
} else if (bufferObj.event === 'node_finished' && onNodeFinished) {
|
||||||
console.log('✅ [handleStream] 处理节点完成事件:', bufferObj.event);
|
// console.log('✅ [handleStream] 处理节点完成事件:', bufferObj.event);
|
||||||
onNodeFinished(bufferObj as NodeFinishedResponse);
|
onNodeFinished(bufferObj as NodeFinishedResponse);
|
||||||
} else {
|
} else {
|
||||||
console.log('❓ [handleStream] 未知事件类型:', bufferObj.event);
|
// console.log('❓ [handleStream] 未知事件类型:', bufferObj.event);
|
||||||
}
|
}
|
||||||
} else if (message.trim()) {
|
} else if (message.trim()) {
|
||||||
console.log('📝 [handleStream] 非data消息:', message.substring(0, 100));
|
// console.log('📝 [handleStream] 非data消息:', message.substring(0, 100));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 保留最后一行(可能是不完整的消息)
|
// 保留最后一行(可能是不完整的消息)
|
||||||
const lastLine = lines[lines.length - 1];
|
const lastLine = lines[lines.length - 1];
|
||||||
buffer = lastLine;
|
buffer = lastLine;
|
||||||
console.log('💾 [handleStream] 保留缓冲区:', {
|
// console.log('💾 [handleStream] 保留缓冲区:', {
|
||||||
lastLineLength: lastLine.length,
|
// lastLineLength: lastLine.length,
|
||||||
preview: lastLine.substring(0, 50)
|
// preview: lastLine.substring(0, 50)
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.error('❌ [handleStream] 解析响应时出错:', err);
|
console.error('❌ [handleStream] 解析响应时出错:', err);
|
||||||
@@ -286,10 +286,10 @@ const handleStream = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!hasError) {
|
if (!hasError) {
|
||||||
console.log('🔄 [handleStream] 继续读取下一块...');
|
// console.log('🔄 [handleStream] 继续读取下一块...');
|
||||||
read();
|
read();
|
||||||
} else {
|
} else {
|
||||||
console.log('🛑 [handleStream] 因错误停止读取');
|
// console.log('🛑 [handleStream] 因错误停止读取');
|
||||||
}
|
}
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.error('❌ [handleStream] 读取流时出错:', err);
|
console.error('❌ [handleStream] 读取流时出错:', err);
|
||||||
@@ -348,11 +348,11 @@ const baseFetch = (url: string, fetchOptions: any, needAllResponseContent: boole
|
|||||||
.then((res: Response) => {
|
.then((res: Response) => {
|
||||||
const resClone = res.clone();
|
const resClone = res.clone();
|
||||||
|
|
||||||
console.log('📥 API Response:', {
|
// console.log('📥 API Response:', {
|
||||||
status: res.status,
|
// status: res.status,
|
||||||
statusText: res.statusText,
|
// statusText: res.statusText,
|
||||||
url: urlWithPrefix
|
// url: urlWithPrefix
|
||||||
});
|
// });
|
||||||
|
|
||||||
// 错误处理
|
// 错误处理
|
||||||
if (!/^(2|3)\d{2}$/.test(res.status.toString())) {
|
if (!/^(2|3)\d{2}$/.test(res.status.toString())) {
|
||||||
@@ -456,11 +456,11 @@ export const ssePost = (
|
|||||||
|
|
||||||
return fetch(urlWithPrefix, options)
|
return fetch(urlWithPrefix, options)
|
||||||
.then((res: Response) => {
|
.then((res: Response) => {
|
||||||
console.log('📡 SSE Response:', {
|
// console.log('📡 SSE Response:', {
|
||||||
status: res.status,
|
// status: res.status,
|
||||||
statusText: res.statusText,
|
// statusText: res.statusText,
|
||||||
url: urlWithPrefix
|
// url: urlWithPrefix
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (!/^(2|3)\d{2}$/.test(res.status.toString())) {
|
if (!/^(2|3)\d{2}$/.test(res.status.toString())) {
|
||||||
res.json().then((data: any) => {
|
res.json().then((data: any) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user