chore: 注释ws
This commit is contained in:
parent
ba494c1396
commit
0e86fe5659
@ -1,10 +1,10 @@
|
||||
import ReturnArrow from "@/assets/icons/svg/returnArrow.svg";
|
||||
import Chat from "@/components/ask/chat";
|
||||
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, webSocketManager, 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";
|
||||
@ -97,92 +97,92 @@ export default function AskScreen() {
|
||||
};
|
||||
}, [isHello]);
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
webSocketManager.connect();
|
||||
// useFocusEffect(
|
||||
// useCallback(() => {
|
||||
// webSocketManager.connect();
|
||||
|
||||
const handleChatStream = (message: WsMessage) => {
|
||||
if (message.type === 'ChatStream') {
|
||||
setUserMessages(prevMessages => {
|
||||
const newMessages = [...prevMessages];
|
||||
const lastMessage = newMessages[newMessages.length - 1];
|
||||
// const handleChatStream = (message: WsMessage) => {
|
||||
// if (message.type === 'ChatStream') {
|
||||
// setUserMessages(prevMessages => {
|
||||
// const newMessages = [...prevMessages];
|
||||
// const lastMessage = newMessages[newMessages.length - 1];
|
||||
|
||||
if (lastMessage && lastMessage.role === Assistant) {
|
||||
if (typeof lastMessage.content === 'string') {
|
||||
if (lastMessage.content === 'keepSearchIng') {
|
||||
// 第一次收到流式消息,替换占位符
|
||||
lastMessage.content = message.chunk;
|
||||
} else {
|
||||
// 持续追加流式消息
|
||||
lastMessage.content += message.chunk;
|
||||
}
|
||||
} else {
|
||||
// 如果 content 是数组,则更新第一个 text 部分
|
||||
const textPart = lastMessage.content.find(p => p.type === 'text');
|
||||
if (textPart) {
|
||||
textPart.text = (textPart.text || '') + message.chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
return newMessages;
|
||||
});
|
||||
}
|
||||
};
|
||||
// if (lastMessage && lastMessage.role === Assistant) {
|
||||
// if (typeof lastMessage.content === 'string') {
|
||||
// if (lastMessage.content === 'keepSearchIng') {
|
||||
// // 第一次收到流式消息,替换占位符
|
||||
// lastMessage.content = message.chunk;
|
||||
// } else {
|
||||
// // 持续追加流式消息
|
||||
// lastMessage.content += message.chunk;
|
||||
// }
|
||||
// } else {
|
||||
// // 如果 content 是数组,则更新第一个 text 部分
|
||||
// const textPart = lastMessage.content.find(p => p.type === 'text');
|
||||
// if (textPart) {
|
||||
// textPart.text = (textPart.text || '') + message.chunk;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return newMessages;
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
const handleChatStreamEnd = (message: WsMessage) => {
|
||||
if (message.type === 'ChatStreamEnd') {
|
||||
setUserMessages(prevMessages => {
|
||||
const newMessages = [...prevMessages];
|
||||
const lastMessage = newMessages[newMessages.length - 1];
|
||||
if (lastMessage && lastMessage.role === Assistant) {
|
||||
// 使用最终消息替换流式消息,确保 message.message 存在
|
||||
if (message.message) {
|
||||
newMessages[newMessages.length - 1] = message.message as Message;
|
||||
} else {
|
||||
// 如果最终消息为空,则移除 'keepSearchIng' 占位符
|
||||
return prevMessages.filter(m => !(typeof m.content === 'string' && m.content === 'keepSearchIng'));
|
||||
}
|
||||
}
|
||||
return newMessages;
|
||||
});
|
||||
}
|
||||
};
|
||||
// const handleChatStreamEnd = (message: WsMessage) => {
|
||||
// if (message.type === 'ChatStreamEnd') {
|
||||
// setUserMessages(prevMessages => {
|
||||
// const newMessages = [...prevMessages];
|
||||
// const lastMessage = newMessages[newMessages.length - 1];
|
||||
// if (lastMessage && lastMessage.role === Assistant) {
|
||||
// // 使用最终消息替换流式消息,确保 message.message 存在
|
||||
// if (message.message) {
|
||||
// newMessages[newMessages.length - 1] = message.message as Message;
|
||||
// } else {
|
||||
// // 如果最终消息为空,则移除 'keepSearchIng' 占位符
|
||||
// return prevMessages.filter(m => !(typeof m.content === 'string' && m.content === 'keepSearchIng'));
|
||||
// }
|
||||
// }
|
||||
// return newMessages;
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
const handleError = (message: WsMessage) => {
|
||||
if (message.type === 'Error') {
|
||||
console.log(`WebSocket Error: ${message.code} - ${message.message}`);
|
||||
// 可以在这里添加错误提示,例如替换最后一条消息为错误信息
|
||||
setUserMessages(prev => {
|
||||
// 创建新的数组和新的消息对象
|
||||
return prev.map((msg, index) => {
|
||||
if (index === prev.length - 1 &&
|
||||
typeof msg.content === 'string' &&
|
||||
msg.content === 'keepSearchIng') {
|
||||
// 返回新的消息对象
|
||||
return {
|
||||
...msg,
|
||||
content: getWebSocketErrorMessage(message.code, t)
|
||||
};
|
||||
}
|
||||
return msg;
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
// const handleError = (message: WsMessage) => {
|
||||
// if (message.type === 'Error') {
|
||||
// console.log(`WebSocket Error: ${message.code} - ${message.message}`);
|
||||
// // 可以在这里添加错误提示,例如替换最后一条消息为错误信息
|
||||
// setUserMessages(prev => {
|
||||
// // 创建新的数组和新的消息对象
|
||||
// return prev.map((msg, index) => {
|
||||
// if (index === prev.length - 1 &&
|
||||
// typeof msg.content === 'string' &&
|
||||
// msg.content === 'keepSearchIng') {
|
||||
// // 返回新的消息对象
|
||||
// return {
|
||||
// ...msg,
|
||||
// content: getWebSocketErrorMessage(message.code, t)
|
||||
// };
|
||||
// }
|
||||
// return msg;
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
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 () => {
|
||||
webSocketManager.unsubscribe('ChatStream', handleChatStream);
|
||||
webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd);
|
||||
webSocketManager.unsubscribe('Error', handleError);
|
||||
// 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度
|
||||
// webSocketManager.disconnect();
|
||||
};
|
||||
}, [])
|
||||
);
|
||||
// return () => {
|
||||
// webSocketManager.unsubscribe('ChatStream', handleChatStream);
|
||||
// webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd);
|
||||
// webSocketManager.unsubscribe('Error', handleError);
|
||||
// // 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度
|
||||
// // webSocketManager.disconnect();
|
||||
// };
|
||||
// }, [])
|
||||
// );
|
||||
|
||||
// 创建动画样式
|
||||
const welcomeStyle = useAnimatedStyle(() => {
|
||||
@ -271,21 +271,21 @@ export default function AskScreen() {
|
||||
</Animated.View>
|
||||
|
||||
{/* 聊天页面 */}
|
||||
{/* <Animated.View
|
||||
style={[styles.absoluteView, chatStyle, { zIndex: 0 }]}
|
||||
>
|
||||
<Chat
|
||||
ref={chatListRef}
|
||||
userMessages={userMessages}
|
||||
sessionId={sessionId}
|
||||
setSelectedImages={setSelectedImages}
|
||||
selectedImages={selectedImages}
|
||||
style={styles.chatContainer}
|
||||
contentContainerStyle={styles.chatContentContainer}
|
||||
showsVerticalScrollIndicator={false}
|
||||
// onContentSizeChange={() => scrollToEnd()}
|
||||
/>
|
||||
</Animated.View> */}
|
||||
<Animated.View
|
||||
style={[styles.absoluteView, chatStyle, { zIndex: 0 }]}
|
||||
>
|
||||
<Chat
|
||||
ref={chatListRef}
|
||||
userMessages={userMessages}
|
||||
sessionId={sessionId}
|
||||
setSelectedImages={setSelectedImages}
|
||||
selectedImages={selectedImages}
|
||||
style={styles.chatContainer}
|
||||
contentContainerStyle={styles.chatContentContainer}
|
||||
showsVerticalScrollIndicator={false}
|
||||
// onContentSizeChange={() => scrollToEnd()}
|
||||
/>
|
||||
</Animated.View>
|
||||
</View>
|
||||
|
||||
{/* 输入框区域 */}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user