From b35bb3cbeb9bf5281c85e6ba088e5b778fde2dc0 Mon Sep 17 00:00:00 2001 From: Junhui Chen Date: Thu, 7 Aug 2025 00:47:17 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=B3=A8=E9=87=8Aws?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(tabs)/ask.tsx | 273 ++++++++++++++++++++++----------------------- 1 file changed, 136 insertions(+), 137 deletions(-) diff --git a/app/(tabs)/ask.tsx b/app/(tabs)/ask.tsx index e1568f6..7d9fa4e 100644 --- a/app/(tabs)/ask.tsx +++ b/app/(tabs)/ask.tsx @@ -4,8 +4,7 @@ import AskHello from "@/components/ask/hello"; import SendMessage from "@/components/ask/send"; import { ThemedText } from "@/components/ThemedText"; import { fetchApi } from "@/lib/server-api-util"; -import { getWebSocketErrorMessage, getWebSocketManager, WsMessage } from "@/lib/websocket-util"; -import { Assistant, Message } from "@/types/ask"; +import { Message } from "@/types/ask"; import { useFocusEffect, useLocalSearchParams, useRouter } from "expo-router"; import { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from "react-i18next"; @@ -98,163 +97,163 @@ export default function AskScreen() { }; }, [isHello]); - useFocusEffect( - useCallback(() => { - // 确保在组件挂载时才连接 - const webSocketManager = getWebSocketManager(); - webSocketManager.connect(); + // useFocusEffect( + // useCallback(() => { + // // 确保在组件挂载时才连接 + // const webSocketManager = getWebSocketManager(); + // webSocketManager.connect(); - let isMounted = true; + // let isMounted = true; - const handleChatStream = (message: WsMessage) => { - // 确保组件仍然挂载 - if (!isMounted) return; + // const handleChatStream = (message: WsMessage) => { + // // 确保组件仍然挂载 + // if (!isMounted) return; - if (message.type === 'ChatStream') { - setUserMessages(prevMessages => { - // 使用 try-catch 包装以防止错误导致应用崩溃 - try { - const lastMessage = prevMessages[prevMessages.length - 1]; + // if (message.type === 'ChatStream') { + // setUserMessages(prevMessages => { + // // 使用 try-catch 包装以防止错误导致应用崩溃 + // try { + // const lastMessage = prevMessages[prevMessages.length - 1]; - // 检查是否是有效的助手消息 - if (!lastMessage || lastMessage.role !== Assistant) { - return prevMessages; - } + // // 检查是否是有效的助手消息 + // if (!lastMessage || lastMessage.role !== Assistant) { + // return prevMessages; + // } - // 创建新的消息数组 - const newMessages = [...prevMessages]; + // // 创建新的消息数组 + // const newMessages = [...prevMessages]; - if (typeof lastMessage.content === 'string') { - if (lastMessage.content === 'keepSearchIng') { - // 第一次收到流式消息,替换占位符 - newMessages[newMessages.length - 1] = { - ...lastMessage, - content: message.chunk - }; - } else { - // 持续追加流式消息 - newMessages[newMessages.length - 1] = { - ...lastMessage, - content: lastMessage.content + message.chunk - }; - } - } else if (Array.isArray(lastMessage.content)) { - // 如果 content 是数组,则更新第一个 text 部分 - const textPartIndex = lastMessage.content.findIndex(p => p.type === 'text'); - if (textPartIndex !== -1) { - const updatedContent = [...lastMessage.content]; - updatedContent[textPartIndex] = { - ...updatedContent[textPartIndex], - text: (updatedContent[textPartIndex].text || '') + message.chunk - }; + // if (typeof lastMessage.content === 'string') { + // if (lastMessage.content === 'keepSearchIng') { + // // 第一次收到流式消息,替换占位符 + // newMessages[newMessages.length - 1] = { + // ...lastMessage, + // content: message.chunk + // }; + // } else { + // // 持续追加流式消息 + // newMessages[newMessages.length - 1] = { + // ...lastMessage, + // content: lastMessage.content + message.chunk + // }; + // } + // } else if (Array.isArray(lastMessage.content)) { + // // 如果 content 是数组,则更新第一个 text 部分 + // const textPartIndex = lastMessage.content.findIndex(p => p.type === 'text'); + // if (textPartIndex !== -1) { + // const updatedContent = [...lastMessage.content]; + // updatedContent[textPartIndex] = { + // ...updatedContent[textPartIndex], + // text: (updatedContent[textPartIndex].text || '') + message.chunk + // }; - newMessages[newMessages.length - 1] = { - ...lastMessage, - content: updatedContent - }; - } - } + // newMessages[newMessages.length - 1] = { + // ...lastMessage, + // content: updatedContent + // }; + // } + // } - return newMessages; - } catch (error) { - console.error('处理 ChatStream 消息时出错:', error); - // 发生错误时返回原始消息数组 - return prevMessages; - } - }); - } - }; + // return newMessages; + // } catch (error) { + // console.error('处理 ChatStream 消息时出错:', error); + // // 发生错误时返回原始消息数组 + // return prevMessages; + // } + // }); + // } + // }; - const handleChatStreamEnd = (message: WsMessage) => { - // 确保组件仍然挂载 - if (!isMounted) return; + // const handleChatStreamEnd = (message: WsMessage) => { + // // 确保组件仍然挂载 + // if (!isMounted) return; - if (message.type === 'ChatStreamEnd') { - setUserMessages(prevMessages => { - // 使用 try-catch 包装以防止错误导致应用崩溃 - try { - const lastMessage = prevMessages[prevMessages.length - 1]; + // if (message.type === 'ChatStreamEnd') { + // setUserMessages(prevMessages => { + // // 使用 try-catch 包装以防止错误导致应用崩溃 + // try { + // const lastMessage = prevMessages[prevMessages.length - 1]; - // 检查是否是有效的助手消息 - if (!lastMessage || lastMessage.role !== Assistant) { - return prevMessages; - } + // // 检查是否是有效的助手消息 + // if (!lastMessage || lastMessage.role !== Assistant) { + // return prevMessages; + // } - // 使用最终消息替换流式消息,确保 message.message 存在 - if (message.message) { - const newMessages = [...prevMessages]; - newMessages[newMessages.length - 1] = message.message as Message; - return newMessages; - } else { - // 如果最终消息为空,则移除 'keepSearchIng' 占位符 - return prevMessages.filter(m => - !(typeof m.content === 'string' && m.content === 'keepSearchIng') - ); - } - } catch (error) { - console.error('处理 ChatStreamEnd 消息时出错:', error); - // 发生错误时返回原始消息数组 - return prevMessages; - } - }); - } - }; + // // 使用最终消息替换流式消息,确保 message.message 存在 + // if (message.message) { + // const newMessages = [...prevMessages]; + // newMessages[newMessages.length - 1] = message.message as Message; + // return newMessages; + // } else { + // // 如果最终消息为空,则移除 'keepSearchIng' 占位符 + // return prevMessages.filter(m => + // !(typeof m.content === 'string' && m.content === 'keepSearchIng') + // ); + // } + // } catch (error) { + // console.error('处理 ChatStreamEnd 消息时出错:', error); + // // 发生错误时返回原始消息数组 + // return prevMessages; + // } + // }); + // } + // }; - const handleError = (message: WsMessage) => { - // 确保组件仍然挂载 - if (!isMounted) return; + // const handleError = (message: WsMessage) => { + // // 确保组件仍然挂载 + // if (!isMounted) return; - if (message.type === 'Error') { - console.log(`WebSocket Error: ${message.code} - ${message.message}`); + // if (message.type === 'Error') { + // console.log(`WebSocket Error: ${message.code} - ${message.message}`); - setUserMessages(prevMessages => { - // 使用 try-catch 包装以防止错误导致应用崩溃 - try { - const lastMessage = prevMessages[prevMessages.length - 1]; + // setUserMessages(prevMessages => { + // // 使用 try-catch 包装以防止错误导致应用崩溃 + // try { + // const lastMessage = prevMessages[prevMessages.length - 1]; - // 检查是否是有效的助手消息且包含占位符 - if (!lastMessage || - lastMessage.role !== Assistant || - typeof lastMessage.content !== 'string' || - lastMessage.content !== 'keepSearchIng') { - return prevMessages; - } + // // 检查是否是有效的助手消息且包含占位符 + // if (!lastMessage || + // lastMessage.role !== Assistant || + // typeof lastMessage.content !== 'string' || + // lastMessage.content !== 'keepSearchIng') { + // return prevMessages; + // } - // 替换占位符为错误消息 - const newMessages = [...prevMessages]; - newMessages[newMessages.length - 1] = { - ...lastMessage, - content: getWebSocketErrorMessage(message.code, t) - }; + // // 替换占位符为错误消息 + // const newMessages = [...prevMessages]; + // newMessages[newMessages.length - 1] = { + // ...lastMessage, + // content: getWebSocketErrorMessage(message.code, t) + // }; - return newMessages; - } catch (error) { - console.error('处理 Error 消息时出错:', error); - // 发生错误时返回原始消息数组 - return prevMessages; - } - }); - } - }; + // return newMessages; + // } catch (error) { + // console.error('处理 Error 消息时出错:', error); + // // 发生错误时返回原始消息数组 + // return prevMessages; + // } + // }); + // } + // }; - webSocketManager.subscribe('ChatStream', handleChatStream); - webSocketManager.subscribe('ChatStreamEnd', handleChatStreamEnd); - webSocketManager.subscribe('Error', handleError); + // webSocketManager.subscribe('ChatStream', handleChatStream); + // webSocketManager.subscribe('ChatStreamEnd', handleChatStreamEnd); + // webSocketManager.subscribe('Error', handleError); - return () => { - // 设置组件卸载标志 - isMounted = false; + // return () => { + // // 设置组件卸载标志 + // isMounted = false; - // 清理订阅 - webSocketManager.unsubscribe('ChatStream', handleChatStream); - webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd); - webSocketManager.unsubscribe('Error', handleError); + // // 清理订阅 + // webSocketManager.unsubscribe('ChatStream', handleChatStream); + // webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd); + // webSocketManager.unsubscribe('Error', handleError); - // 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度 - webSocketManager.disconnect(); - }; - }, [t]) - ); + // // 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度 + // webSocketManager.disconnect(); + // }; + // }, [t]) + // ); // 创建动画样式 const welcomeStyle = useAnimatedStyle(() => {