fix(web): 修复 useChat 无限请求问题

- 用 useRef 存储回调函数,避免依赖变化导致无限循环
- 添加 onSessionNotFound 回调,会话不存在时自动创建新会话
- 限制 WebSocket 重连次数为 5 次
- 添加 web:dev 快捷脚本
This commit is contained in:
2025-12-12 14:27:54 +08:00
parent 20765efe62
commit 2fe1c55997
4 changed files with 45 additions and 13 deletions
+3 -1
View File
@@ -10,9 +10,10 @@ import { ChatInput } from '../components/ChatInput';
interface ChatPageProps {
sessionId: string;
onSessionNotFound?: () => void;
}
export function ChatPage({ sessionId }: ChatPageProps) {
export function ChatPage({ sessionId, onSessionNotFound }: ChatPageProps) {
const {
messages,
isConnected,
@@ -25,6 +26,7 @@ export function ChatPage({ sessionId }: ChatPageProps) {
onError: (error) => {
console.error('Chat error:', error);
},
onSessionNotFound,
});
const messagesEndRef = useRef<HTMLDivElement>(null);